#include "game_types.h"

/* Bignum_ApplyImpulse2 @ 0x0801D520 (real 88B) -- impulse with size-tiered zero fill. Mirrors src/matched/Bignum_ApplyImpulse2.c. */
/* Applies the impulse over (p, a*b, b); a null result returns NULL, an
   oversized header frees via FreeAll, otherwise zero-fills 3/5/7/9 words by
   the ((header & ~4) - 4) size tier. Returns the node. */
extern int *Physics_ApplyImpulse(int *p, int x, int y);
extern void Physics_FreeAll(int *p, int zero);

int *Bignum_ApplyImpulse2(int *p, int a, int b)
{
    int *r = Physics_ApplyImpulse(p, a * b, b);
    unsigned n;
    int words, i;
    if (r == 0)
        return 0;
    n = ((unsigned)*(int *)((char *)r - 4) & ~4u) - 4;
    if (n > 36) {
        Physics_FreeAll(r, 0);
        return r;
    }
    words = n <= 19 ? 3 : n <= 27 ? 5 : n <= 35 ? 7 : 9;
    for (i = 0; i < words; i++)
        r[i] = 0;
    return r;
}
