#include "game_types.h"

/* Float_Negate @ 0x0801A39C (real 186B) -- negate driver with 7-call chain. Mirrors src/matched/Float_Negate.c. */
/* Finalizes a, scales (r6 - b) through CompareAndAlloc, and -- past the 4096
   gate -- chains up to three Float_Init probes plus the 0x02000624 ledger
   update; the Lalt path records ((r6 - r4) | 1) instead. Always ends in
   CleanupAndFree; returns 0 normally, 1 via Lalt. */
extern void Physics_Finalize(int *p);
extern int Mem_CompareAndAlloc(int a, int b);
extern int Float_Init(int a, int b);
extern void Physics_CleanupAndFree(int *p);

int Float_Negate(int *a, int b)
{
    int *r8 = (int *)0x02000208;
    int r6 = (*(int **)(r8 + 2))[1] & ~4;
    int r4 = (Mem_CompareAndAlloc(r6 - b + 0xFEF, 4096) - 1) << 12;
    if (r4 >= 4096) {
        int t = Float_Init((int)a, 0);
        if (t == (*(int **)(r8 + 2))[0] + r6) {
            if (Float_Init((int)a, -r4) == -1) {
                int v = Float_Init((int)a, 0);
                int w = v - (*(int **)(r8 + 2))[0];
                if (w > 15) {
                    *(int *)0x02000624 = v - *(int *)0x02000618;
                    (*(int **)(r8 + 2))[1] = w | 1;
                }
            } else {
                int *e = *(int **)(r8 + 2);
                e[1] = (r6 - r4) | 1;
                *(int *)0x02000624 -= r4;
                Physics_CleanupAndFree(a);
                return 1;
            }
        }
    }
    Physics_CleanupAndFree(a);
    return 0;
}
