#include "game_types.h"

/* Image_LoadFromROM @ 0x0801343C (real 72B) -- walks the node chain masking and unlinking. Mirrors src/matched/Image_LoadFromROM.c. */
/* When flag bit 128 is set, walks the chain from q+32 zeroing head bytes
   (firing the InitSystem indirection for masked kinds), unlinking
   self-pointing tails, and parking 0 back into q+32. */
extern void Image_InitSystem(void); /* bx r3: target from the pool chain */

void Image_LoadFromROM(int dummy, unsigned char *q)
{
    int *n;
    (void)dummy;
    if ((q[0] & 128) == 0)
        return;
    n = *(int **)(q + 32);
    while (n != 0) {
        if (((unsigned char *)n)[0] != 0) {
            if ((((unsigned char *)n)[1] & 7) != 0)
                ((void (*)(void))*(int **)(*(int **)(*(int **)0x03007FF0 + 11)))();
            ((unsigned char *)n)[0] = 0;
        }
        n[11] = 0;
        {
            int *m = (int *)n[13];
            if (m == n) {
                m = 0;
                n[13] = 0;
            }
            n = m;
        }
    }
    *(int **)(q + 32) = 0;
}
