Search code examples
cassemblybinaryfiles

What does this Semi-Decompiled Code do (C)


I've translated an objdumped file into ad-hoc C code and I'm trying to figure out what it does. In particular, I'm stuck on once section. I'll first give the larger section and then the part I'm stuck on.

Larger section of code:

while (true) {
    bool v3 = v2 == 5 | v2 < 5 ^ (4 - v2 & v2) < 0; // 0x8048600
    // branch -> 0x80485a7
    while (true) {
        // 0x80485a7
        if ((int32_t)file == g1) {
            // 0x80485af
            printf("guess %d (of 5)? ", v2);
            // branch -> 0x80485bf
        }
        // 0x80485bf
        int32_t str2;
        char * str = fgets((char *)&str2, 512, file); // 0x80485d2
        if (str == NULL) {
            // 0x80485fd
            if (!v3) {
                // break -> 0x8048602
                break;
            }
            // continue -> 0x80485a7
            continue;
        } else {
            int32_t str3 = *(int32_t *)(4 * v2 + 0x80498ec); // 0x80485db
            if (strcmp(str, (char *)str3) != 0) {
                // 0x80485ee
                bomb();
                // branch -> 0x80485f3
            }
            int32_t v4 = v2 + 1; // 0x80485f3
            if (v4 >= 6) {
                // break (via goto) -> 0x8048602
                goto lab_0x8048602;
            }
            v2 = v4;
            // continue (via goto) -> 0x80485a7
            goto lab_0x80485a7;
        }
        // 0x8048602
        success();
        return 0;
    }
  lab_0x8048602:
    // 0x8048602
    success();
    return 0;
}

The part that is giving me trouble in particular:

 int32_t str3 = *(int32_t *)(4 * v2 + 0x80498ec); // 0x80485db
            if (strcmp(str, (char *)str3) != 0) {
                // 0x80485ee
                bomb();
                // branch -> 0x80485f3
            }

I understand that strcmp returns 1,0,-1, but what exactly is this statement testing? I'm not entirely sure what the value of str3 is given: *(int32_t *)(4 * v2 + 0x80498ec);. I understand this casts the value at that address to something, but I"m not sure what.


Solution

  • Apparently, 0x80498ec is an array of pointers to char like

    char *msg [] = {"One", "two", "three", "four", "five"};
    

    and v2 is an index into this pointer array.

    The line could possibly originally have read

    if (strcmp (str, msg[v2]) != 0)
       bomb ();