#include "game_types.h"

/* Sub_080136B8 @ 0x080136B8 (real 74B) -- cursor byte latch + masked node flag walk. Mirrors src/matched/Sub_080136B8.c. */
/* Latches cursor bytes below 128 into field 5 (advancing the cursor),
   then walks the chain from field 32 setting bit 64 on the first node with
   bits 131 set, bit 64 clear, and byte 17 matching -- unlinking
   self-pointing tails along the way. */
void Sub_080136B8(int unused, char *st)
{
    unsigned char **cursor = (unsigned char **)(st + 64);
    unsigned char *p = *cursor;
    int v;
    int *n;
    (void)unused;
    v = *p;
    if (v < 128) {
        st[5] = (char)v;
        *cursor = p + 1;
    }
    n = *(int **)(st + 32);
    while (n != 0) {
        if ((((unsigned char *)n)[0] & 131) != 0 &&
            (((unsigned char *)n)[0] & 64) == 0 &&
            ((unsigned char *)n)[17] == v)
            break;
        {
            int *m = (int *)n[13];
            if (m == n) {
                m = 0;
                n[13] = 0;
            }
            n = m;
        }
    }
    if (n != 0)
        ((unsigned char *)n)[0] |= 64;
}
