decompiled_code:
unsigned __int16 __fastcall mmu_putbyte(unsigned __int16 a1, unsigned __int8 a2)
{
unsigned __int16 result; // ax

result = a1 & 0xF000;
if ( (a1 & 0xF000) == 0xC000 )
goto LABEL_27;
if ( (a1 & 0xF000u) <= 0xC000 )
{
if ( result != 28672 )
{
if ( result > 0x7000u )
{
if ( result == 0x9000 )
goto LABEL_20;
if ( result > 0x9000u )
{
if ( (a1 & 0xE000) != 0xA000 )
goto LABEL_8;
return mbc_putbyte(a1, a2);
}
if ( result == 0x8000 )
{
LABEL_20:
gpu[(a1 & 0x1FFF) + 23] = a2;
return result;
}
LABEL_8:
_assert_fail("false", "src/mmu.c", 0x11Fu, "mmu_putbyte");
}
if ( result != 12288 )
{
if ( result <= 0x3000u )
{
if ( result != 0x2000 && (a1 & 0xE000) != 0 )
goto LABEL_8;
}
else if ( result != 24576 && (a1 & 0xE000) != 0x4000 )
{
goto LABEL_8;
}
}
}
return mbc_putbyte(a1, a2);
}
if ( result == 0xE000 )
goto LABEL_27;
if ( result != 0xF000 )
{
if ( result != 0xD000 )
goto LABEL_8;
goto LABEL_27;
}
result = a1 & 0xF00;
if ( (a1 & 0xF00) != 3584 )
{
if ( (a1 & 0xF00u) <= 0xE00 )
{
if ( result == 1792 )
goto LABEL_27;
if ( result <= 0x700u )
{
if ( result == 768 )
goto LABEL_27;
if ( result <= 0x300u )
{
if ( result == 512 || (a1 & 0xE00) == 0 )
goto LABEL_27;
}
else
{
if ( result == 1536 )
goto LABEL_27;
result = a1 & 0xE00;
if ( (a1 & 0xE00) == 1024 )
goto LABEL_27;
}
}
else
{
if ( result == 2816 )
goto LABEL_27;
if ( result > 0xB00u )
{
result = a1 & 0xE00;
if ( (a1 & 0xE00) != 3072 )
goto LABEL_18;
LABEL_27:
mmu[(a1 & 0x1FFF) + 259] = a2;
return result;
}
if ( result == 2560 )
goto LABEL_27;
result = a1 & 0xE00;
if ( (a1 & 0xE00) == 2048 )
goto LABEL_27;
}
LABEL_18:
_assert_fail("false", "src/mmu.c", 0x11Au, "mmu_putbyte");
}
if ( result != 3840 )
goto LABEL_18;
if ( a1 == 0xFFFF )
{
mmu[257] = a2;
}
else if ( a1 <= 0xFF7Fu )
{
if ( a1 > 0xFF3Fu )
return gpu_putbyte(a1, a2);
if ( a1 > 0xFF07u )
{
if ( a1 == 0xFF0F )
mmu[258] = a2;
}
else if ( a1 > 0xFF03u )
{
result = timer_putbyte(a1, a2);
}
else if ( a1 == 0xFF00 )
{
result = joypad_putbyte(a2);
}
return result;
}
mmu[(a1 & 0x7F) + 8451] = a2;
return result;
}
if ( a1 <= 0xFE9Fu )
gpu[(unsigned __int8)a1 + 8215] = a2;
return result;
}

canonical_decompiled_code:
unsigned __int16 __fastcall func3(unsigned __int16 a1, unsigned __int8 a2)
{
unsigned __int16 result; // ax

result = a1 & 0xF000;
if ( (a1 & 0xF000) == 0xC000 )
goto LABEL_27;
if ( (a1 & 0xF000u) <= 0xC000 )
{
if ( result != 28672 )
{
if ( result > 0x7000u )
{
if ( result == 0x9000 )
goto LABEL_20;
if ( result > 0x9000u )
{
if ( (a1 & 0xE000) != 0xA000 )
goto LABEL_8;
return mbc_putbyte(a1, a2);
}
if ( result == 0x8000 )
{
LABEL_20:
gpu[(a1 & 0x1FFF) + 23] = a2;
return result;
}
LABEL_8:
_assert_fail("false", "src/mmu.c", 0x11Fu, "mmu_putbyte");
}
if ( result != 12288 )
{
if ( result <= 0x3000u )
{
if ( result != 0x2000 && (a1 & 0xE000) != 0 )
goto LABEL_8;
}
else if ( result != 24576 && (a1 & 0xE000) != 0x4000 )
{
goto LABEL_8;
}
}
}
return mbc_putbyte(a1, a2);
}
if ( result == 0xE000 )
goto LABEL_27;
if ( result != 0xF000 )
{
if ( result != 0xD000 )
goto LABEL_8;
goto LABEL_27;
}
result = a1 & 0xF00;
if ( (a1 & 0xF00) != 3584 )
{
if ( (a1 & 0xF00u) <= 0xE00 )
{
if ( result == 1792 )
goto LABEL_27;
if ( result <= 0x700u )
{
if ( result == 768 )
goto LABEL_27;
if ( result <= 0x300u )
{
if ( result == 512 || (a1 & 0xE00) == 0 )
goto LABEL_27;
}
else
{
if ( result == 1536 )
goto LABEL_27;
result = a1 & 0xE00;
if ( (a1 & 0xE00) == 1024 )
goto LABEL_27;
}
}
else
{
if ( result == 2816 )
goto LABEL_27;
if ( result > 0xB00u )
{
result = a1 & 0xE00;
if ( (a1 & 0xE00) != 3072 )
goto LABEL_18;
LABEL_27:
mmu[(a1 & 0x1FFF) + 259] = a2;
return result;
}
if ( result == 2560 )
goto LABEL_27;
result = a1 & 0xE00;
if ( (a1 & 0xE00) == 2048 )
goto LABEL_27;
}
LABEL_18:
_assert_fail("false", "src/mmu.c", 0x11Au, "mmu_putbyte");
}
if ( result != 3840 )
goto LABEL_18;
if ( a1 == 0xFFFF )
{
mmu[257] = a2;
}
else if ( a1 <= 0xFF7Fu )
{
if ( a1 > 0xFF3Fu )
return gpu_putbyte(a1, a2);
if ( a1 > 0xFF07u )
{
if ( a1 == 0xFF0F )
mmu[258] = a2;
}
else if ( a1 > 0xFF03u )
{
result = timer_putbyte(a1, a2);
}
else if ( a1 == 0xFF00 )
{
result = joypad_putbyte(a2);
}
return result;
}
mmu[(a1 & 0x7F) + 8451] = a2;
return result;
}
if ( a1 <= 0xFE9Fu )
gpu[(unsigned __int8)a1 + 8215] = a2;
return result;
}

original_code:
void mmu_putbyte(Word addr, Byte value)
{
    switch (addr & 0xF000)
    {
        case 0x0000:
        case 0x1000:
        case 0x2000:
        case 0x3000:
        case 0x4000:
        case 0x5000:
        case 0x6000:
        case 0x7000:
            mbc_putbyte(addr, value);
            break;
        case 0x8000:
        case 0x9000:
            gpu.vram[addr & 0x1FFF] = value;
            break;
        case 0xA000:
        case 0xB000:
            mbc_putbyte(addr, value);
            break;
        case 0xC000:
        case 0xD000:
            mmu.wram[addr & 0x1FFF] = value;
            break;
        case 0xE000:
            mmu.wram[addr & 0x1FFF] = value;
            break;
        case 0xF000:
            switch (addr & 0x0F00)
            {
                case 0x000: case 0x100: case 0x200: case 0x300:
                case 0x400: case 0x500: case 0x600: case 0x700:
                case 0x800: case 0x900: case 0xA00: case 0xB00:
                case 0xC00: case 0xD00:
                    mmu.wram[addr & 0x1FFF] = value;
                    break;
                case 0xE00:
                    if (addr < 0xFEA0)
                    {
                        gpu.oam[addr & 0xFF] = value;
                    }
                    break;
                case 0xF00:
                    if (addr == 0xFFFF)
                    {
                        mmu.ienable = value;
                    }
                    if (addr >= 0xFF80)
                    {
                        mmu.zram[addr & 0x7F] = value;
                    }
                    else if (addr >= 0xFF40)
                    {
                        gpu_putbyte(addr, value);
                    }
                    else
                    {
                        switch (addr)
                        {
                            case 0xFF00:
                                joypad_putbyte(value);
                                break;
                            case 0xFF04:
                            case 0xFF05:
                            case 0xFF06:
                            case 0xFF07:
                                timer_putbyte(addr, value);
                                break;
                            case 0xFF0F:
                                mmu.iflag = value;
                                break;
                        }
                    }
                    break;
                default:
                    ((0) ? (void) (0) : __assert_fail ("false", "src/mmu.c", 282, __extension__ __PRETTY_FUNCTION__));
            }
            break;
        default:
            ((0) ? (void) (0) : __assert_fail ("false", "src/mmu.c", 287, __extension__ __PRETTY_FUNCTION__));
    }
}

canonical_original_code:
void mmu_putbyte(unsigned short addr, unsigned char value)
{
    switch (addr & 0xF000)
    {
        case 0x0000:
        case 0x1000:
        case 0x2000:
        case 0x3000:
        case 0x4000:
        case 0x5000:
        case 0x6000:
        case 0x7000:
            mbc_putbyte(addr, value);
            break;
        case 0x8000:
        case 0x9000:
            gpu.vram[addr & 0x1FFF] = value;
            break;
        case 0xA000:
        case 0xB000:
            mbc_putbyte(addr, value);
            break;
        case 0xC000:
        case 0xD000:
            mmu.wram[addr & 0x1FFF] = value;
            break;
        case 0xE000:
            mmu.wram[addr & 0x1FFF] = value;
            break;
        case 0xF000:
            switch (addr & 0x0F00)
            {
                case 0x000: case 0x100: case 0x200: case 0x300:
                case 0x400: case 0x500: case 0x600: case 0x700:
                case 0x800: case 0x900: case 0xA00: case 0xB00:
                case 0xC00: case 0xD00:
                    mmu.wram[addr & 0x1FFF] = value;
                    break;
                case 0xE00:
                    if (addr < 0xFEA0)
                    {
                        gpu.oam[addr & 0xFF] = value;
                    }
                    break;
                case 0xF00:
                    if (addr == 0xFFFF)
                    {
                        mmu.ienable = value;
                    }
                    if (addr >= 0xFF80)
                    {
                        mmu.zram[addr & 0x7F] = value;
                    }
                    else if (addr >= 0xFF40)
                    {
                        gpu_putbyte(addr, value);
                    }
                    else
                    {
                        switch (addr)
                        {
                            case 0xFF00:
                                joypad_putbyte(value);
                                break;
                            case 0xFF04:
                            case 0xFF05:
                            case 0xFF06:
                            case 0xFF07:
                                timer_putbyte(addr, value);
                                break;
                            case 0xFF0F:
                                mmu.iflag = value;
                                break;
                        }
                    }
                    break;
                default:
                    ((0) ? (void) (0) : __assert_fail ("false", "src/mmu.c", 282, __extension__ __PRETTY_FUNCTION__));
            }
            break;
        default:
            ((0) ? (void) (0) : __assert_fail ("false", "src/mmu.c", 287, __extension__ __PRETTY_FUNCTION__));
    }
}


decompiled_code:
unsigned __int16 __fastcall mmu_putword(unsigned __int16 a1, __int16 a2)
{
mmu_putbyte(a1, a2);
return mmu_putbyte(a1 + 1, HIBYTE(a2));
}

canonical_decompiled_code:
unsigned __int16 __fastcall func4(unsigned __int16 a1, __int16 a2)
{
func3(a1, a2);
return func3(a1 + 1, HIBYTE(a2));
}

original_code:
void mmu_putword(Word addr, Word value)
{
    mmu_putbyte(addr, value & 0xFF);
    mmu_putbyte(addr + 1, value >> 8);
}

canonical_original_code:
void mmu_putword(unsigned short addr, unsigned short value)
{
    mmu_putbyte(addr, value & 0xFF);
    mmu_putbyte(addr + 1, value >> 8);
}

variable_types:
addr
unsigned short

variable_types:
value
unsigned short

return_type: void


decompiled_code:
__int64 __fastcall gbe_reset(__int64 a1)
{
gpu_reset();
mmu_reset();
z80_reset();
return mbc_load(a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
gpu_reset();
mmu_reset();
z80_reset();
return mbc_load(a1);
}

original_code:
void gbe_reset(const char *filename)
{
    gpu_reset();
    mmu_reset();
    z80_reset();
    mbc_load(filename);
}

canonical_original_code:
void gbe_reset(char *filename)
{
    gpu_reset();
    mmu_reset();
    z80_reset();
    mbc_load(filename);
}

variable_types:
filename
char *


decompiled_code:
__int64 joypad_reset()
{
__int64 result; // rax

result = 3840LL;
*((_BYTE *)&joypad + 2) = 15;
joypad = 3840;
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax

result = 3840LL;
*((_BYTE *)&joypad + 2) = 15;
joypad = 3840;
return result;
}

original_code:
void joypad_reset()
{
    joypad.column = 0;
    joypad.dpad = 0xF;
    joypad.button = 0xF;
}


decompiled_code:
__int64 joypad_getbyte()
{
__int64 result; // rax

result = *((unsigned __int8 *)&joypad + 2);
if ( (_BYTE)joypad != 16 )
{
result = HIBYTE(joypad);
if ( (_BYTE)joypad != 32 )
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax

result = *((unsigned __int8 *)&joypad + 2);
if ( (_BYTE)joypad != 16 )
{
result = HIBYTE(joypad);
if ( (_BYTE)joypad != 32 )
result = 0LL;
}
return result;
}

original_code:
Byte joypad_getbyte()
{
    switch (joypad.column)
    {
        case 0x20:
            return joypad.dpad;
        case 0x10:
            return joypad.button;
        default:
            return 0;
    }
}

canonical_original_code:
unsigned char joypad_getbyte()
{
    switch (joypad.column)
    {
        case 0x20:
            return joypad.dpad;
        case 0x10:
            return joypad.button;
        default:
            return 0;
    }
}

return_type: unsigned char


decompiled_code:
void __fastcall joypad_putbyte(char a1)
{
LOBYTE(joypad) = a1 & 0x30;
}

canonical_decompiled_code:
void __fastcall func2(char a1)
{
LOBYTE(joypad) = a1 & 0x30;
}

original_code:
void joypad_putbyte(Byte value)
{
    joypad.column = value & 0x30;
}

canonical_original_code:
void joypad_putbyte(unsigned char value)
{
    joypad.column = value & 0x30;
}

variable_types:
value
unsigned char


decompiled_code:
void __fastcall joypad_keydown(unsigned int a1)
{
if ( a1 == 273 )
{
HIBYTE(joypad) &= 0xFBu;
mmu[258] |= 0x10u;
}
else if ( a1 > 0x111 )
{
switch ( a1 )
{
case 0x113u:
HIBYTE(joypad) &= 0xFEu;
mmu[258] |= 0x10u;
break;
case 0x114u:
HIBYTE(joypad) &= 0xFDu;
mmu[258] |= 0x10u;
break;
case 0x112u:
HIBYTE(joypad) &= 0xF7u;
mmu[258] |= 0x10u;
break;
default:
goto LABEL_7;
}
}
else if ( a1 == 120 )
{
*((_BYTE *)&joypad + 2) &= 0xFDu;
mmu[258] |= 0x10u;
}
else
{
if ( a1 > 0x78 )
{
if ( a1 == 122 )
*((_BYTE *)&joypad + 2) &= 0xFEu;
goto LABEL_7;
}
if ( a1 != 13 )
{
if ( a1 == 32 )
{
*((_BYTE *)&joypad + 2) &= 0xFBu;
mmu[258] |= 0x10u;
return;
}
LABEL_7:
mmu[258] |= 0x10u;
return;
}
*((_BYTE *)&joypad + 2) &= 0xF7u;
mmu[258] |= 0x10u;
}
}

canonical_decompiled_code:
void __fastcall func3(unsigned int a1)
{
if ( a1 == 273 )
{
HIBYTE(joypad) &= 0xFBu;
mmu[258] |= 0x10u;
}
else if ( a1 > 0x111 )
{
switch ( a1 )
{
case 0x113u:
HIBYTE(joypad) &= 0xFEu;
mmu[258] |= 0x10u;
break;
case 0x114u:
HIBYTE(joypad) &= 0xFDu;
mmu[258] |= 0x10u;
break;
case 0x112u:
HIBYTE(joypad) &= 0xF7u;
mmu[258] |= 0x10u;
break;
default:
goto LABEL_7;
}
}
else if ( a1 == 120 )
{
*((_BYTE *)&joypad + 2) &= 0xFDu;
mmu[258] |= 0x10u;
}
else
{
if ( a1 > 0x78 )
{
if ( a1 == 122 )
*((_BYTE *)&joypad + 2) &= 0xFEu;
goto LABEL_7;
}
if ( a1 != 13 )
{
if ( a1 == 32 )
{
*((_BYTE *)&joypad + 2) &= 0xFBu;
mmu[258] |= 0x10u;
return;
}
LABEL_7:
mmu[258] |= 0x10u;
return;
}
*((_BYTE *)&joypad + 2) &= 0xF7u;
mmu[258] |= 0x10u;
}
}

original_code:
void joypad_keydown(SDLKey key)
{
    switch (key)
    {
        case SDLK_DOWN:
            joypad.dpad &= ~0x08;
            break;
        case SDLK_UP:
            joypad.dpad &= ~0x04;
            break;
        case SDLK_LEFT:
            joypad.dpad &= ~0x02;
            break;
        case SDLK_RIGHT:
            joypad.dpad &= ~0x01;
            break;
        case SDLK_RETURN:
            joypad.button &= ~0x08;
            break;
        case SDLK_SPACE:
            joypad.button &= ~0x04;
            break;
        case SDLK_x:
            joypad.button &= ~0x02;
            break;
        case SDLK_z:
            joypad.button &= ~0x01;
            break;
        default:
            break;
    }
    mmu.iflag |= 0x10;
}

canonical_original_code:
void joypad_keydown(enum SDLKey key)
{
    switch (key)
    {
        case SDLK_DOWN:
            joypad.dpad &= ~0x08;
            break;
        case SDLK_UP:
            joypad.dpad &= ~0x04;
            break;
        case SDLK_LEFT:
            joypad.dpad &= ~0x02;
            break;
        case SDLK_RIGHT:
            joypad.dpad &= ~0x01;
            break;
        case SDLK_RETURN:
            joypad.button &= ~0x08;
            break;
        case SDLK_SPACE:
            joypad.button &= ~0x04;
            break;
        case SDLK_x:
            joypad.button &= ~0x02;
            break;
        case SDLK_z:
            joypad.button &= ~0x01;
            break;
        default:
            break;
    }
    mmu.iflag |= 0x10;
}

variable_types: enum SDLKey { SDLK_UNKNOWN=0, SDLK_FIRST=0, SDLK_BACKSPACE=8, SDLK_TAB=9, SDLK_CLEAR=12, SDLK_RETURN=13, SDLK_PAUSE=19, SDLK_ESCAPE=27, SDLK_SPACE=32, SDLK_EXCLAIM=33, SDLK_QUOTEDBL=34, SDLK_HASH=35, SDLK_DOLLAR=36, SDLK_AMPERSAND=38, SDLK_QUOTE=39, SDLK_LEFTPAREN=40, SDLK_RIGHTPAREN=41, SDLK_ASTERISK=42, SDLK_PLUS=43, SDLK_COMMA=44, SDLK_MINUS=45, SDLK_PERIOD=46, SDLK_SLASH=47, SDLK_0=48, SDLK_1=49, SDLK_2=50, SDLK_3=51, SDLK_4=52, SDLK_5=53, SDLK_6=54, SDLK_7=55, SDLK_8=56, SDLK_9=57, SDLK_COLON=58, SDLK_SEMICOLON=59, SDLK_LESS=60, SDLK_EQUALS=61, SDLK_GREATER=62, SDLK_QUESTION=63, SDLK_AT=64, SDLK_LEFTBRACKET=91, SDLK_BACKSLASH=92, SDLK_RIGHTBRACKET=93, SDLK_CARET=94, SDLK_UNDERSCORE=95, SDLK_BACKQUOTE=96, SDLK_a=97, SDLK_b=98, SDLK_c=99, SDLK_d=100, SDLK_e=101, SDLK_f=102, SDLK_g=103, SDLK_h=104, SDLK_i=105, SDLK_j=106, SDLK_k=107, SDLK_l=108, SDLK_m=109, SDLK_n=110, SDLK_o=111, SDLK_p=112, SDLK_q=113, SDLK_r=114, SDLK_s=115, SDLK_t=116, SDLK_u=117, SDLK_v=118, SDLK_w=119, SDLK_x=120, SDLK_y=121, SDLK_z=122, SDLK_DELETE=127, SDLK_WORLD_0=160, SDLK_WORLD_1=161, SDLK_WORLD_2=162, SDLK_WORLD_3=163, SDLK_WORLD_4=164, SDLK_WORLD_5=165, SDLK_WORLD_6=166, SDLK_WORLD_7=167, SDLK_WORLD_8=168, SDLK_WORLD_9=169, SDLK_WORLD_10=170, SDLK_WORLD_11=171, SDLK_WORLD_12=172, SDLK_WORLD_13=173, SDLK_WORLD_14=174, SDLK_WORLD_15=175, SDLK_WORLD_16=176, SDLK_WORLD_17=177, SDLK_WORLD_18=178, SDLK_WORLD_19=179, SDLK_WORLD_20=180, SDLK_WORLD_21=181, SDLK_WORLD_22=182, SDLK_WORLD_23=183, SDLK_WORLD_24=184, SDLK_WORLD_25=185, SDLK_WORLD_26=186, SDLK_WORLD_27=187, SDLK_WORLD_28=188, SDLK_WORLD_29=189, SDLK_WORLD_30=190, SDLK_WORLD_31=191, SDLK_WORLD_32=192, SDLK_WORLD_33=193, SDLK_WORLD_34=194, SDLK_WORLD_35=195, SDLK_WORLD_36=196, SDLK_WORLD_37=197, SDLK_WORLD_38=198, SDLK_WORLD_39=199, SDLK_WORLD_40=200, SDLK_WORLD_41=201, SDLK_WORLD_42=202, SDLK_WORLD_43=203, SDLK_WORLD_44=204, SDLK_WORLD_45=205, SDLK_WORLD_46=206, SDLK_WORLD_47=207, SDLK_WORLD_48=208, SDLK_WORLD_49=209, SDLK_WORLD_50=210, SDLK_WORLD_51=211, SDLK_WORLD_52=212, SDLK_WORLD_53=213, SDLK_WORLD_54=214, SDLK_WORLD_55=215, SDLK_WORLD_56=216, SDLK_WORLD_57=217, SDLK_WORLD_58=218, SDLK_WORLD_59=219, SDLK_WORLD_60=220, SDLK_WORLD_61=221, SDLK_WORLD_62=222, SDLK_WORLD_63=223, SDLK_WORLD_64=224, SDLK_WORLD_65=225, SDLK_WORLD_66=226, SDLK_WORLD_67=227, SDLK_WORLD_68=228, SDLK_WORLD_69=229, SDLK_WORLD_70=230, SDLK_WORLD_71=231, SDLK_WORLD_72=232, SDLK_WORLD_73=233, SDLK_WORLD_74=234, SDLK_WORLD_75=235, SDLK_WORLD_76=236, SDLK_WORLD_77=237, SDLK_WORLD_78=238, SDLK_WORLD_79=239, SDLK_WORLD_80=240, SDLK_WORLD_81=241, SDLK_WORLD_82=242, SDLK_WORLD_83=243, SDLK_WORLD_84=244, SDLK_WORLD_85=245, SDLK_WORLD_86=246, SDLK_WORLD_87=247, SDLK_WORLD_88=248, SDLK_WORLD_89=249, SDLK_WORLD_90=250, SDLK_WORLD_91=251, SDLK_WORLD_92=252, SDLK_WORLD_93=253, SDLK_WORLD_94=254, SDLK_WORLD_95=255, SDLK_KP0=256, SDLK_KP1=257, SDLK_KP2=258, SDLK_KP3=259, SDLK_KP4=260, SDLK_KP5=261, SDLK_KP6=262, SDLK_KP7=263, SDLK_KP8=264, SDLK_KP9=265, SDLK_KP_PERIOD=266, SDLK_KP_DIVIDE=267, SDLK_KP_MULTIPLY=268, SDLK_KP_MINUS=269, SDLK_KP_PLUS=270, SDLK_KP_ENTER=271, SDLK_KP_EQUALS=272, SDLK_UP=273, SDLK_DOWN=274, SDLK_RIGHT=275, SDLK_LEFT=276, SDLK_INSERT=277, SDLK_HOME=278, SDLK_END=279, SDLK_PAGEUP=280, SDLK_PAGEDOWN=281, SDLK_F1=282, SDLK_F2=283, SDLK_F3=284, SDLK_F4=285, SDLK_F5=286, SDLK_F6=287, SDLK_F7=288, SDLK_F8=289, SDLK_F9=290, SDLK_F10=291, SDLK_F11=292, SDLK_F12=293, SDLK_F13=294, SDLK_F14=295, SDLK_F15=296, SDLK_NUMLOCK=300, SDLK_CAPSLOCK=301, SDLK_SCROLLOCK=302, SDLK_RSHIFT=303, SDLK_LSHIFT=304, SDLK_RCTRL=305, SDLK_LCTRL=306, SDLK_RALT=307, SDLK_LALT=308, SDLK_RMETA=309, SDLK_LMETA=310, SDLK_LSUPER=311, SDLK_RSUPER=312, SDLK_MODE=313, SDLK_COMPOSE=314, SDLK_HELP=315, SDLK_PRINT=316, SDLK_SYSREQ=317, SDLK_BREAK=318, SDLK_MENU=319, SDLK_POWER=320, SDLK_EURO=321, SDLK_UNDO=322, SDLK_LAST=323 }


decompiled_code:
void __fastcall joypad_keyup(unsigned int a1)
{
if ( a1 == 273 )
{
HIBYTE(joypad) |= 4u;
mmu[258] |= 0x10u;
}
else if ( a1 > 0x111 )
{
switch ( a1 )
{
case 0x113u:
HIBYTE(joypad) |= 1u;
mmu[258] |= 0x10u;
break;
case 0x114u:
HIBYTE(joypad) |= 2u;
mmu[258] |= 0x10u;
break;
case 0x112u:
HIBYTE(joypad) |= 8u;
mmu[258] |= 0x10u;
break;
default:
goto LABEL_7;
}
}
else if ( a1 == 120 )
{
*((_BYTE *)&joypad + 2) |= 2u;
mmu[258] |= 0x10u;
}
else
{
if ( a1 > 0x78 )
{
if ( a1 == 122 )
*((_BYTE *)&joypad + 2) |= 1u;
goto LABEL_7;
}
if ( a1 != 13 )
{
if ( a1 == 32 )
{
*((_BYTE *)&joypad + 2) |= 4u;
mmu[258] |= 0x10u;
return;
}
LABEL_7:
mmu[258] |= 0x10u;
return;
}
*((_BYTE *)&joypad + 2) |= 8u;
mmu[258] |= 0x10u;
}
}

canonical_decompiled_code:
void __fastcall func4(unsigned int a1)
{
if ( a1 == 273 )
{
HIBYTE(joypad) |= 4u;
mmu[258] |= 0x10u;
}
else if ( a1 > 0x111 )
{
switch ( a1 )
{
case 0x113u:
HIBYTE(joypad) |= 1u;
mmu[258] |= 0x10u;
break;
case 0x114u:
HIBYTE(joypad) |= 2u;
mmu[258] |= 0x10u;
break;
case 0x112u:
HIBYTE(joypad) |= 8u;
mmu[258] |= 0x10u;
break;
default:
goto LABEL_7;
}
}
else if ( a1 == 120 )
{
*((_BYTE *)&joypad + 2) |= 2u;
mmu[258] |= 0x10u;
}
else
{
if ( a1 > 0x78 )
{
if ( a1 == 122 )
*((_BYTE *)&joypad + 2) |= 1u;
goto LABEL_7;
}
if ( a1 != 13 )
{
if ( a1 == 32 )
{
*((_BYTE *)&joypad + 2) |= 4u;
mmu[258] |= 0x10u;
return;
}
LABEL_7:
mmu[258] |= 0x10u;
return;
}
*((_BYTE *)&joypad + 2) |= 8u;
mmu[258] |= 0x10u;
}
}

original_code:
void joypad_keyup(SDLKey key)
{
    switch (key)
    {
        case SDLK_DOWN:
            joypad.dpad |= 0x08;
            break;
        case SDLK_UP:
            joypad.dpad |= 0x04;
            break;
        case SDLK_LEFT:
            joypad.dpad |= 0x02;
            break;
        case SDLK_RIGHT:
            joypad.dpad |= 0x01;
            break;
        case SDLK_RETURN:
            joypad.button |= 0x08;
            break;
        case SDLK_SPACE:
            joypad.button |= 0x04;
            break;
        case SDLK_x:
            joypad.button |= 0x02;
            break;
        case SDLK_z:
            joypad.button |= 0x01;
            break;
        default:
            break;
    }
    mmu.iflag |= 0x10;
}

canonical_original_code:
void joypad_keyup(enum SDLKey key)
{
    switch (key)
    {
        case SDLK_DOWN:
            joypad.dpad |= 0x08;
            break;
        case SDLK_UP:
            joypad.dpad |= 0x04;
            break;
        case SDLK_LEFT:
            joypad.dpad |= 0x02;
            break;
        case SDLK_RIGHT:
            joypad.dpad |= 0x01;
            break;
        case SDLK_RETURN:
            joypad.button |= 0x08;
            break;
        case SDLK_SPACE:
            joypad.button |= 0x04;
            break;
        case SDLK_x:
            joypad.button |= 0x02;
            break;
        case SDLK_z:
            joypad.button |= 0x01;
            break;
        default:
            break;
    }
    mmu.iflag |= 0x10;
}

user_defined_types: enum SDLKey { SDLK_UNKNOWN=0, SDLK_FIRST=0, SDLK_BACKSPACE=8, SDLK_TAB=9, SDLK_CLEAR=12, SDLK_RETURN=13, SDLK_PAUSE=19, SDLK_ESCAPE=27, SDLK_SPACE=32, SDLK_EXCLAIM=33, SDLK_QUOTEDBL=34, SDLK_HASH=35, SDLK_DOLLAR=36, SDLK_AMPERSAND=38, SDLK_QUOTE=39, SDLK_LEFTPAREN=40, SDLK_RIGHTPAREN=41, SDLK_ASTERISK=42, SDLK_PLUS=43, SDLK_COMMA=44, SDLK_MINUS=45, SDLK_PERIOD=46, SDLK_SLASH=47, SDLK_0=48, SDLK_1=49, SDLK_2=50, SDLK_3=51, SDLK_4=52, SDLK_5=53, SDLK_6=54, SDLK_7=55, SDLK_8=56, SDLK_9=57, SDLK_COLON=58, SDLK_SEMICOLON=59, SDLK_LESS=60, SDLK_EQUALS=61, SDLK_GREATER=62, SDLK_QUESTION=63, SDLK_AT=64, SDLK_LEFTBRACKET=91, SDLK_BACKSLASH=92, SDLK_RIGHTBRACKET=93, SDLK_CARET=94, SDLK_UNDERSCORE=95, SDLK_BACKQUOTE=96, SDLK_a=97, SDLK_b=98, SDLK_c=99, SDLK_d=100, SDLK_e=101, SDLK_f=102, SDLK_g=103, SDLK_h=104, SDLK_i=105, SDLK_j=106, SDLK_k=107, SDLK_l=108, SDLK_m=109, SDLK_n=110, SDLK_o=111, SDLK_p=112, SDLK_q=113, SDLK_r=114, SDLK_s=115, SDLK_t=116, SDLK_u=117, SDLK_v=118, SDLK_w=119, SDLK_x=120, SDLK_y=121, SDLK_z=122, SDLK_DELETE=127, SDLK_WORLD_0=160, SDLK_WORLD_1=161, SDLK_WORLD_2=162, SDLK_WORLD_3=163, SDLK_WORLD_4=164, SDLK_WORLD_5=165, SDLK_WORLD_6=166, SDLK_WORLD_7=167, SDLK_WORLD_8=168, SDLK_WORLD_9=169, SDLK_WORLD_10=170, SDLK_WORLD_11=171, SDLK_WORLD_12=172, SDLK_WORLD_13=173, SDLK_WORLD_14=174, SDLK_WORLD_15=175, SDLK_WORLD_16=176, SDLK_WORLD_17=177, SDLK_WORLD_18=178, SDLK_WORLD_19=179, SDLK_WORLD_20=180, SDLK_WORLD_21=181, SDLK_WORLD_22=182, SDLK_WORLD_23=183, SDLK_WORLD_24=184, SDLK_WORLD_25=185, SDLK_WORLD_26=186, SDLK_WORLD_27=187, SDLK_WORLD_28=188, SDLK_WORLD_29=189, SDLK_WORLD_30=190, SDLK_WORLD_31=191, SDLK_WORLD_32=192, SDLK_WORLD_33=193, SDLK_WORLD_34=194, SDLK_WORLD_35=195, SDLK_WORLD_36=196, SDLK_WORLD_37=197, SDLK_WORLD_38=198, SDLK_WORLD_39=199, SDLK_WORLD_40=200, SDLK_WORLD_41=201, SDLK_WORLD_42=202, SDLK_WORLD_43=203, SDLK_WORLD_44=204, SDLK_WORLD_45=205, SDLK_WORLD_46=206, SDLK_WORLD_47=207, SDLK_WORLD_48=208, SDLK_WORLD_49=209, SDLK_WORLD_50=210, SDLK_WORLD_51=211, SDLK_WORLD_52=212, SDLK_WORLD_53=213, SDLK_WORLD_54=214, SDLK_WORLD_55=215, SDLK_WORLD_56=216, SDLK_WORLD_57=217, SDLK_WORLD_58=218, SDLK_WORLD_59=219, SDLK_WORLD_60=220, SDLK_WORLD_61=221, SDLK_WORLD_62=222, SDLK_WORLD_63=223, SDLK_WORLD_64=224, SDLK_WORLD_65=225, SDLK_WORLD_66=226, SDLK_WORLD_67=227, SDLK_WORLD_68=228, SDLK_WORLD_69=229, SDLK_WORLD_70=230, SDLK_WORLD_71=231, SDLK_WORLD_72=232, SDLK_WORLD_73=233, SDLK_WORLD_74=234, SDLK_WORLD_75=235, SDLK_WORLD_76=236, SDLK_WORLD_77=237, SDLK_WORLD_78=238, SDLK_WORLD_79=239, SDLK_WORLD_80=240, SDLK_WORLD_81=241, SDLK_WORLD_82=242, SDLK_WORLD_83=243, SDLK_WORLD_84=244, SDLK_WORLD_85=245, SDLK_WORLD_86=246, SDLK_WORLD_87=247, SDLK_WORLD_88=248, SDLK_WORLD_89=249, SDLK_WORLD_90=250, SDLK_WORLD_91=251, SDLK_WORLD_92=252, SDLK_WORLD_93=253, SDLK_WORLD_94=254, SDLK_WORLD_95=255, SDLK_KP0=256, SDLK_KP1=257, SDLK_KP2=258, SDLK_KP3=259, SDLK_KP4=260, SDLK_KP5=261, SDLK_KP6=262, SDLK_KP7=263, SDLK_KP8=264, SDLK_KP9=265, SDLK_KP_PERIOD=266, SDLK_KP_DIVIDE=267, SDLK_KP_MULTIPLY=268, SDLK_KP_MINUS=269, SDLK_KP_PLUS=270, SDLK_KP_ENTER=271, SDLK_KP_EQUALS=272, SDLK_UP=273, SDLK_DOWN=274, SDLK_RIGHT=275, SDLK_LEFT=276, SDLK_INSERT=277, SDLK_HOME=278, SDLK_END=279, SDLK_PAGEUP=280, SDLK_PAGEDOWN=281, SDLK_F1=282, SDLK_F2=283, SDLK_F3=284, SDLK_F4=285, SDLK_F5=286, SDLK_F6=287, SDLK_F7=288, SDLK_F8=289, SDLK_F9=290, SDLK_F10=291, SDLK_F11=292, SDLK_F12=293, SDLK_F13=294, SDLK_F14=295, SDLK_F15=296, SDLK_NUMLOCK=300, SDLK_CAPSLOCK=301, SDLK_SCROLLOCK=302, SDLK_RSHIFT=303, SDLK_LSHIFT=304, SDLK_RCTRL=305, SDLK_LCTRL=306, SDLK_RALT=307, SDLK_LALT=308, SDLK_RMETA=309, SDLK_LMETA=310, SDLK_LSUPER=311, SDLK_RSUPER=312, SDLK_MODE=313, SDLK_COMPOSE=314, SDLK_HELP=315, SDLK_PRINT=316, SDLK_SYSREQ=317, SDLK_BREAK=318, SDLK_MENU=319, SDLK_POWER=320, SDLK_EURO=321, SDLK_UNDO=322, SDLK_LAST=323 }


repo: jlmjlm/atc-ai

decompiled_code:
int scroll_down()
{
int result; // eax
int v1; // edx
int v2; // edx

result = (unsigned int)memmove(
(void *)(display + screen_width * (sr_start + 1)),
(const void *)(display + screen_width * (sr_start + 1) - screen_width),
sr_end * screen_width - (__int64)(screen_width * (sr_start + 1) - screen_width));
v1 = screen_width;
if ( screen_width > 0 )
{
result = 0;
do
{
v2 = result + sr_start * v1;
++result;
*(_BYTE *)(display + v2) = 32;
v1 = screen_width;
}
while ( screen_width > result );
}
if ( verbose )
result = fprintf(logff, "Scrolled down. New display:\n%.*s\n", screen_height * v1, (const char *)display);
return result;
}

canonical_decompiled_code:
int func1()
{
int result; // eax
int v1; // edx
int v2; // edx

result = (unsigned int)memmove(
(void *)(display + screen_width * (sr_start + 1)),
(const void *)(display + screen_width * (sr_start + 1) - screen_width),
sr_end * screen_width - (__int64)(screen_width * (sr_start + 1) - screen_width));
v1 = screen_width;
if ( screen_width > 0 )
{
result = 0;
do
{
v2 = result + sr_start * v1;
++result;
*(_BYTE *)(display + v2) = 32;
v1 = screen_width;
}
while ( screen_width > result );
}
if ( verbose )
result = fprintf(logff, "Scrolled down. New display:\n%.*s\n", screen_height * v1, (const char *)display);
return result;
}

original_code:
static void scroll_down() {
    char *start = &(display[(sr_start+1)*screen_width + (0)]);
    char *from = &(display[(sr_start)*screen_width + (0)]);
    char *end = &(display[(sr_end)*screen_width + (0)]);
    memmove(start, from, end-from);
    for (int i = 0; i < screen_width; i++)
        (display[(sr_start)*screen_width + (i)]) = ' ';
    if (verbose) {
        fprintf(logff, "Scrolled down.  New display:\n%.*s\n",
                screen_height*screen_width, display);
    }
}

variable_types:
start
char *

variable_types:
from
char *

variable_types:
end
char *

variable_types: i int


decompiled_code:
int scroll_up()
{
int result; // eax
int v1; // edx
int v2; // edx

result = (unsigned int)memmove(
(void *)(display + screen_width * sr_start),
(const void *)(display + screen_width + screen_width * sr_start),
screen_width * sr_end - (__int64)(screen_width * sr_start));
v1 = screen_width;
if ( screen_width > 0 )
{
result = 0;
do
{
v2 = result + sr_end * v1;
++result;
*(_BYTE *)(display + v2) = 32;
v1 = screen_width;
}
while ( screen_width > result );
}
if ( verbose )
result = fprintf(logff, "Scrolled up. New display:\n%.*s\n", screen_height * v1, (const char *)display);
return result;
}

canonical_decompiled_code:
int func2()
{
int result; // eax
int v1; // edx
int v2; // edx

result = (unsigned int)memmove(
(void *)(display + screen_width * sr_start),
(const void *)(display + screen_width + screen_width * sr_start),
screen_width * sr_end - (__int64)(screen_width * sr_start));
v1 = screen_width;
if ( screen_width > 0 )
{
result = 0;
do
{
v2 = result + sr_end * v1;
++result;
*(_BYTE *)(display + v2) = 32;
v1 = screen_width;
}
while ( screen_width > result );
}
if ( verbose )
result = fprintf(logff, "Scrolled up. New display:\n%.*s\n", screen_height * v1, (const char *)display);
return result;
}

original_code:
static void scroll_up() {
    char *start = &(display[(sr_start)*screen_width + (0)]);
    char *from = &(display[(sr_start+1)*screen_width + (0)]);
    char *end = &(display[(sr_end)*screen_width + (0)]);
    memmove(start, from, end-start);
    for (int i = 0; i < screen_width; i++)
        (display[(sr_end)*screen_width + (i)]) = ' ';
    if (verbose) {
        fprintf(logff, "Scrolled up.  New display:\n%.*s\n",
                screen_height*screen_width, display);
    }
}


decompiled_code:
int __fastcall update_display_char(char a1)
{
int result; // eax
int v2; // ebp
const unsigned __int16 **v3; // r8
__int64 v4; // rcx
int v5; // edx
int v6; // eax
int v7; // [rsp+Ch] [rbp-3Ch] BYREF
__int64 v8[2]; // [rsp+10h] [rbp-38h]
__int64 v9[5]; // [rsp+20h] [rbp-28h]

if ( !sr_end )
sr_end = screen_height - 1;
result = esc_size_5;
if ( a1 == 27 )
{
if ( esc_size_5 )
{
fwrite("warning: Escape sequence [\\33", 1uLL, 0x1DuLL, logff);
display_esc_seq_0(logff);
result = fwrite("] aborted by an ESC\n", 1uLL, 0x14uLL, logff);
}
esc_4[0] = 27;
esc_size_5 = 1;
return result;
}
if ( !esc_size_5 )
{
result = (unsigned __int8)(a1 - 7);
switch ( a1 )
{
case 7:
case 14:
case 15:
return result;
case 8:
result = cur_col_2 - 1;
if ( cur_col_2 - 1 < 0 )
result = 0;
cur_col_2 = result;
goto LABEL_33;
case 9:
cur_col_2 = (cur_col_2 + 8) & 0xFFFFFFF8;
result = cur_col_2;
if ( cur_col_2 < screen_width )
return result;
cur_col_2 = 0;
LABEL_30:
if ( cur_row_3 == sr_end )
{
result = scroll_up();
}
else
{
result = cur_row_3 + 1;
if ( cur_row_3 + 1 < screen_height )
++cur_row_3;
}
LABEL_33:
at_sr_bottom_1 = 0;
break;
case 10:
goto LABEL_30;
case 13:
cur_col_2 = 0;
at_sr_bottom_1 = 0;
return result;
default:
if ( ((*_ctype_b_loc())[a1] & 0x4000) == 0 )
fprintf(logff, "Warning: Got unhandled non-printable character \\%o\n", (unsigned int)a1);
if ( at_sr_bottom_1 )
{
scroll_up();
at_sr_bottom_1 = 0;
}
*(_BYTE *)(display + cur_col_2 + screen_width * cur_row_3) = a1;
result = cur_col_2 + 1;
if ( cur_col_2 + 1 < screen_width )
{
++cur_col_2;
}
else
{
cur_col_2 = 0;
result = cur_row_3;
if ( cur_row_3 == sr_end )
{
at_sr_bottom_1 = 1;
}
else
{
result = cur_row_3 + 1;
if ( cur_row_3 + 1 < screen_height )
++cur_row_3;
}
}
return result;
}
return result;
}
v2 = esc_size_5 + 1;
esc_4[esc_size_5] = a1;
result = (unsigned __int8)byte_9E1;
esc_size_5 = v2;
if ( (unsigned __int8)(byte_9E1 - 40) <= 1u && v2 == 3 )
goto LABEL_25;
if ( (unsigned __int8)(byte_9E1 - 61) <= 1u )
{
if ( v2 == 2 )
goto LABEL_25;
goto LABEL_10;
}
switch ( byte_9E1 )
{
case 'D':
if ( v2 != 2 )
goto LABEL_10;
result = cur_row_3;
if ( cur_row_3 == sr_end )
{
result = scroll_up();
}
else if ( cur_row_3 < screen_height - 1 )
{
result = ++cur_row_3;
}
LABEL_25:
esc_size_5 = 0;
return result;
case 'M':
if ( v2 == 2 )
{
result = cur_row_3;
if ( cur_row_3 == sr_start )
{
result = scroll_down();
}
else if ( cur_row_3 > 0 )
{
result = --cur_row_3;
}
goto LABEL_25;
}
goto LABEL_10;
case '7':
if ( v2 == 2 )
{
saved_row = cur_row_3;
result = cur_col_2;
saved_col = cur_col_2;
goto LABEL_25;
}
LABEL_10:
if ( v2 != 20 )
return result;
LABEL_56:
cleanup();
fwrite("Unknown escape sequence \\33", 1uLL, 0x1BuLL, stderr);
display_esc_seq_0(stderr);
fputc(10, stderr);
exit(27);
case '8':
if ( v2 == 2 )
{
esc_size_5 = 0;
cur_row_3 = saved_row;
result = saved_col;
cur_col_2 = saved_col;
return result;
}
goto LABEL_10;
case 'H':
if ( v2 == 2 )
{
result = fwrite("Warning: Ignoring request to set tab-stop.\n", 1uLL, 0x2BuLL, logff);
esc_size_5 = 0;
return result;
}
goto LABEL_10;
case '[':
v3 = _ctype_b_loc();
result = a1;
if ( ((*v3)[a1] & 0x400) != 0 )
{
result = (unsigned __int8)(a1 - 65);
switch ( a1 )
{
case 'A':
case 'B':
case 'C':
case 'D':
v8[0] = 0x1FFFFFFFFLL;
v9[1] = 0xFFFFFFFF00000001LL;
v8[1] = 0LL;
v9[0] = 0LL;
v7 = 1;
_isoc99_sscanf(&unk_9E2, "%d", &v7);
v4 = (char)(a1 - 65);
v5 = cur_row_3 + v7 * *((_DWORD *)v8 + v4);
v6 = *((_DWORD *)v9 + v4) * v7;
cur_row_3 = v5;
result = cur_col_2 + v6;
cur_col_2 = result;
if ( v5 >= screen_height )
{
v5 = screen_height - 1;
cur_row_3 = screen_height - 1;
}
if ( v5 < 0 )
cur_row_3 = 0;
if ( result >= screen_width )
{
result = screen_width - 1;
cur_col_2 = screen_width - 1;
}
if ( result < 0 )
cur_col_2 = 0;
at_sr_bottom_1 = 0;
break;
case 'H':
if ( v2 == 3 )
{
cur_col_2 = 0;
cur_row_3 = 0;
at_sr_bottom_1 = 0;
}
else
{
result = _isoc99_sscanf(&unk_9E2, "%d;%d", &cur_row_3, &cur_col_2);
v7 = result;
if ( result != 2 )
{
cleanup();
fwrite("Invalid cursor position sequence [\\33", 1uLL, 0x25uLL, stderr);
display_esc_seq_0(stderr);
fwrite("]\n", 1uLL, 2uLL, stderr);
exit(72);
}
--cur_row_3;
--cur_col_2;
at_sr_bottom_1 = 0;
}
break;
case 'J':
case 'K':
case 'h':
case 'i':
case 'l':
case 'm':
goto LABEL_25;
case 'g':
result = fwrite("Warning: Ignoring request to clear tab-stop.\n", 1uLL, 0x2DuLL, logff);
break;
case 'r':
if ( v2 == 3 )
{
sr_start = 0;
cur_col_2 = 0;
result = screen_height - 1;
at_sr_bottom_1 = 0;
sr_end = screen_height - 1;
cur_row_3 = 0;
}
else
{
result = _isoc99_sscanf(&unk_9E2, "%d;%d", &sr_start, &sr_end);
v7 = result;
if ( result != 2 )
{
cleanup();
fwrite("Invalid scroll region sequence [\\33", 1uLL, 0x23uLL, stderr);
display_esc_seq_0(stderr);
fwrite("]\n", 1uLL, 2uLL, stderr);
exit(114);
}
--sr_start;
--sr_end;
cur_col_2 = 0;
cur_row_3 = 0;
at_sr_bottom_1 = 0;
}
break;
default:
fwrite("warning: Unknown escape sequence [\\33", 1uLL, 0x25uLL, logff);
display_esc_seq_0(logff);
result = fwrite("], ignoring.\n", 1uLL, 0xDuLL, logff);
break;
}
goto LABEL_25;
}
goto LABEL_10;
}
if ( v2 == 20 )
goto LABEL_56;
if ( (unsigned __int8)(byte_9E1 - 40) > 1u && v2 == 2 )
{
result = fprintf(logff, "Warning: Ignoring unknown escape sequence [\\33 \\%o]\n", (unsigned int)byte_9E1);
esc_size_5 = 0;
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(char a1)
{
int result; // eax
int v2; // ebp
const unsigned __int16 **v3; // r8
__int64 v4; // rcx
int v5; // edx
int v6; // eax
int v7; // [rsp+Ch] [rbp-3Ch] BYREF
__int64 v8[2]; // [rsp+10h] [rbp-38h]
__int64 v9[5]; // [rsp+20h] [rbp-28h]

if ( !sr_end )
sr_end = screen_height - 1;
result = esc_size_5;
if ( a1 == 27 )
{
if ( esc_size_5 )
{
fwrite("warning: Escape sequence [\\33", 1uLL, 0x1DuLL, logff);
func0(logff);
result = fwrite("] aborted by an ESC\n", 1uLL, 0x14uLL, logff);
}
esc_4[0] = 27;
esc_size_5 = 1;
return result;
}
if ( !esc_size_5 )
{
result = (unsigned __int8)(a1 - 7);
switch ( a1 )
{
case 7:
case 14:
case 15:
return result;
case 8:
result = cur_col_2 - 1;
if ( cur_col_2 - 1 < 0 )
result = 0;
cur_col_2 = result;
goto LABEL_33;
case 9:
cur_col_2 = (cur_col_2 + 8) & 0xFFFFFFF8;
result = cur_col_2;
if ( cur_col_2 < screen_width )
return result;
cur_col_2 = 0;
LABEL_30:
if ( cur_row_3 == sr_end )
{
result = func2();
}
else
{
result = cur_row_3 + 1;
if ( cur_row_3 + 1 < screen_height )
++cur_row_3;
}
LABEL_33:
at_sr_bottom_1 = 0;
break;
case 10:
goto LABEL_30;
case 13:
cur_col_2 = 0;
at_sr_bottom_1 = 0;
return result;
default:
if ( ((*_ctype_b_loc())[a1] & 0x4000) == 0 )
fprintf(logff, "Warning: Got unhandled non-printable character \\%o\n", (unsigned int)a1);
if ( at_sr_bottom_1 )
{
func2();
at_sr_bottom_1 = 0;
}
*(_BYTE *)(display + cur_col_2 + screen_width * cur_row_3) = a1;
result = cur_col_2 + 1;
if ( cur_col_2 + 1 < screen_width )
{
++cur_col_2;
}
else
{
cur_col_2 = 0;
result = cur_row_3;
if ( cur_row_3 == sr_end )
{
at_sr_bottom_1 = 1;
}
else
{
result = cur_row_3 + 1;
if ( cur_row_3 + 1 < screen_height )
++cur_row_3;
}
}
return result;
}
return result;
}
v2 = esc_size_5 + 1;
esc_4[esc_size_5] = a1;
result = (unsigned __int8)byte_9E1;
esc_size_5 = v2;
if ( (unsigned __int8)(byte_9E1 - 40) <= 1u && v2 == 3 )
goto LABEL_25;
if ( (unsigned __int8)(byte_9E1 - 61) <= 1u )
{
if ( v2 == 2 )
goto LABEL_25;
goto LABEL_10;
}
switch ( byte_9E1 )
{
case 'D':
if ( v2 != 2 )
goto LABEL_10;
result = cur_row_3;
if ( cur_row_3 == sr_end )
{
result = func2();
}
else if ( cur_row_3 < screen_height - 1 )
{
result = ++cur_row_3;
}
LABEL_25:
esc_size_5 = 0;
return result;
case 'M':
if ( v2 == 2 )
{
result = cur_row_3;
if ( cur_row_3 == sr_start )
{
result = func1();
}
else if ( cur_row_3 > 0 )
{
result = --cur_row_3;
}
goto LABEL_25;
}
goto LABEL_10;
case '7':
if ( v2 == 2 )
{
saved_row = cur_row_3;
result = cur_col_2;
saved_col = cur_col_2;
goto LABEL_25;
}
LABEL_10:
if ( v2 != 20 )
return result;
LABEL_56:
cleanup();
fwrite("Unknown escape sequence \\33", 1uLL, 0x1BuLL, stderr);
func0(stderr);
fputc(10, stderr);
exit(27);
case '8':
if ( v2 == 2 )
{
esc_size_5 = 0;
cur_row_3 = saved_row;
result = saved_col;
cur_col_2 = saved_col;
return result;
}
goto LABEL_10;
case 'H':
if ( v2 == 2 )
{
result = fwrite("Warning: Ignoring request to set tab-stop.\n", 1uLL, 0x2BuLL, logff);
esc_size_5 = 0;
return result;
}
goto LABEL_10;
case '[':
v3 = _ctype_b_loc();
result = a1;
if ( ((*v3)[a1] & 0x400) != 0 )
{
result = (unsigned __int8)(a1 - 65);
switch ( a1 )
{
case 'A':
case 'B':
case 'C':
case 'D':
v8[0] = 0x1FFFFFFFFLL;
v9[1] = 0xFFFFFFFF00000001LL;
v8[1] = 0LL;
v9[0] = 0LL;
v7 = 1;
_isoc99_sscanf(&unk_9E2, "%d", &v7);
v4 = (char)(a1 - 65);
v5 = cur_row_3 + v7 * *((_DWORD *)v8 + v4);
v6 = *((_DWORD *)v9 + v4) * v7;
cur_row_3 = v5;
result = cur_col_2 + v6;
cur_col_2 = result;
if ( v5 >= screen_height )
{
v5 = screen_height - 1;
cur_row_3 = screen_height - 1;
}
if ( v5 < 0 )
cur_row_3 = 0;
if ( result >= screen_width )
{
result = screen_width - 1;
cur_col_2 = screen_width - 1;
}
if ( result < 0 )
cur_col_2 = 0;
at_sr_bottom_1 = 0;
break;
case 'H':
if ( v2 == 3 )
{
cur_col_2 = 0;
cur_row_3 = 0;
at_sr_bottom_1 = 0;
}
else
{
result = _isoc99_sscanf(&unk_9E2, "%d;%d", &cur_row_3, &cur_col_2);
v7 = result;
if ( result != 2 )
{
cleanup();
fwrite("Invalid cursor position sequence [\\33", 1uLL, 0x25uLL, stderr);
func0(stderr);
fwrite("]\n", 1uLL, 2uLL, stderr);
exit(72);
}
--cur_row_3;
--cur_col_2;
at_sr_bottom_1 = 0;
}
break;
case 'J':
case 'K':
case 'h':
case 'i':
case 'l':
case 'm':
goto LABEL_25;
case 'g':
result = fwrite("Warning: Ignoring request to clear tab-stop.\n", 1uLL, 0x2DuLL, logff);
break;
case 'r':
if ( v2 == 3 )
{
sr_start = 0;
cur_col_2 = 0;
result = screen_height - 1;
at_sr_bottom_1 = 0;
sr_end = screen_height - 1;
cur_row_3 = 0;
}
else
{
result = _isoc99_sscanf(&unk_9E2, "%d;%d", &sr_start, &sr_end);
v7 = result;
if ( result != 2 )
{
cleanup();
fwrite("Invalid scroll region sequence [\\33", 1uLL, 0x23uLL, stderr);
func0(stderr);
fwrite("]\n", 1uLL, 2uLL, stderr);
exit(114);
}
--sr_start;
--sr_end;
cur_col_2 = 0;
cur_row_3 = 0;
at_sr_bottom_1 = 0;
}
break;
default:
fwrite("warning: Unknown escape sequence [\\33", 1uLL, 0x25uLL, logff);
func0(logff);
result = fwrite("], ignoring.\n", 1uLL, 0xDuLL, logff);
break;
}
goto LABEL_25;
}
goto LABEL_10;
}
if ( v2 == 20 )
goto LABEL_56;
if ( (unsigned __int8)(byte_9E1 - 40) > 1u && v2 == 2 )
{
result = fprintf(logff, "Warning: Ignoring unknown escape sequence [\\33 \\%o]\n", (unsigned int)byte_9E1);
esc_size_5 = 0;
}
return result;
}

original_code:
static void update_display_char(char c) {
    static int cur_row, cur_col;
    static _Bool at_sr_bottom;
    static int esc_size;
    static char esc[20];
    void display_esc_seq(FILE *out) {
        for (int i = 1; i < esc_size; i++) {
            if (((*__ctype_b_loc ())[(int) ((esc[i]))] & (unsigned short int) _ISgraph))
                fprintf(out, " %c", esc[i]);
            else
                fprintf(out, " \\%o", esc[i]);
        }
    }
    if (sr_end == 0)
        sr_end = screen_height-1;
    if (c == '\33') {
        if (esc_size) {
            fprintf(logff, "warning: Escape sequence [\\33");
            display_esc_seq(logff);
            fprintf(logff, "] aborted by an ESC\n");
        }
        esc[0] = c;
        esc_size = 1;
        return;
    }
    if (esc_size) {
        esc[esc_size++] = c;
        if ((esc[1] == '(' || esc[1] == ')') && esc_size == 3) {
            trace("ignoring: %.*s\n", esc_size, esc);
            esc_size = 0;
            return;
        }
        if ((esc[1] == '>' || esc[1] == '=') && esc_size == 2) {
            trace("ignoring: %.*s\n", esc_size, esc);
            esc_size = 0;
            return;
        }
        if (esc[1] == 'D' && esc_size == 2) {
            trace("index: %.*s\n", esc_size, esc);
            if (cur_row == sr_end)
                scroll_up();
            else if (cur_row < screen_height-1)
                cur_row++;
            esc_size = 0;
            return;
        }
        if (esc[1] == 'M' && esc_size == 2) {
            trace("reverse index: %.*s\n", esc_size, esc);
            if (cur_row == sr_start)
                scroll_down();
            else if (cur_row > 0)
                cur_row--;
            esc_size = 0;
            return;
        }
        if (esc[1] == '7' && esc_size == 2) {
            saved_row = cur_row;
            saved_col = cur_col;
            esc_size = 0;
            return;
        }
        if (esc[1] == '8' && esc_size == 2) {
            cur_row = saved_row;
            cur_col = saved_col;
            esc_size = 0;
            return;
        }
        if (esc[1] == 'H' && esc_size == 2) {
            fprintf(logff, "Warning: Ignoring request to set tab-stop.\n");
            esc_size = 0;
            return;
        }
        if (esc[1] == '[' && ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISalpha)) {
            int rv;
            switch (c) {
                default:
                    fprintf(logff, "warning: Unknown escape sequence [\\33");
                    display_esc_seq(logff);
                    fprintf(logff, "], ignoring.\n");
                    break;
                case 'm':
                case 'h':
                case 'l':
                case 'i':
                case 'J': case 'K':
                    trace("ignoring: %.*s\n", esc_size, esc);
                    break;
                case 'g':
                    fprintf(logff, "Warning: Ignoring request to clear "
                                   "tab-stop.\n");
                    break;
                case 'r':
                    if (esc_size == 3) {
                        trace("setting default scroll region "
                              "(entire display)\n");
                        sr_start = 0;
                        sr_end = screen_height-1;
                        cur_row = cur_col = 0;
                        at_sr_bottom = 0;
                        break;
                    }
                    rv = sscanf(esc+2, "%d;%d", &sr_start, &sr_end);
                    if (rv != 2) {
                        cleanup();
                        fprintf(stderr, "Invalid scroll region sequence [\\33");
                        display_esc_seq(stderr);
                        fprintf(stderr, "]\n");
                        exit('r');
                    }
                    sr_start--; sr_end--;
                    cur_row = cur_col = 0;
                    at_sr_bottom = 0;
                    trace("setting scroll region to %d-%d: %.*s\n",
                          sr_start, sr_end, esc_size, esc);
                    break;
                case 'H':
                    if (esc_size == 3) {
                        trace("going to (0, 0): %.*s\n", esc_size, esc);
                        cur_row = cur_col = 0;
                        at_sr_bottom = 0;
                        break;
                    }
                    rv = sscanf(esc+2, "%d;%d", &cur_row, &cur_col);
                    if (rv != 2) {
                        cleanup();
                        fprintf(stderr,
                                "Invalid cursor position sequence [\\33");
                        display_esc_seq(stderr);
                        fprintf(stderr, "]\n");
                        exit('H');
                    }
                    cur_row--; cur_col--;
                    trace("going to (%d, %d): %.*s\n", cur_row, cur_col,
                          esc_size, esc);
                    at_sr_bottom = 0;
                    break;
                case 'A':
                case 'B':
                case 'C':
                case 'D':;
                    int drow[4] = { -1, 1, 0, 0 };
                    int dcol[4] = { 0, 0, 1, -1 };
                    rv = 1;
                    sscanf(esc+2, "%d", &rv);
                    cur_row += rv * drow[c-'A'];
                    cur_col += rv * dcol[c-'A'];
                    if (cur_row >= screen_height)
                        cur_row = screen_height-1;
                    if (cur_row < 0)
                        cur_row = 0;
                    if (cur_col >= screen_width)
                        cur_col = screen_width-1;
                    if (cur_col < 0)
                        cur_col = 0;
                    trace("going to (%d, %d): %.*s\n", cur_row, cur_col,
                          esc_size, esc);
                    at_sr_bottom = 0;
                    break;
            }
            esc_size = 0;
            return;
        }
        if (esc_size == 20) {
            cleanup();
            fprintf(stderr, "Unknown escape sequence \\33");
            display_esc_seq(stderr);
            fprintf(stderr, "\n");
            exit('\33');
        }
        if (esc_size == 2 && esc[1] != '[' && esc[1] != '(' && esc[1] != ')') {
            fprintf(logff, "Warning: Ignoring unknown escape sequence "
                           "[\\33 \\%o]\n", esc[1]);
            esc_size = 0;
        }
        return;
    }
    switch (c) {
        case '\a': case 016: case 017:
            trace("ignoring: \\%o\n", c);
            return;
        case '\r':
            cur_col = 0;
            trace("carriage return: going to (%d, %d)\n", cur_row, cur_col);
            at_sr_bottom = 0;
            return;
        case '\t':
            cur_col = (cur_col+8) & ~7;
            if (cur_col < screen_width)
                return;
            cur_col = 0;
        case '\n':
            if (cur_row == sr_end)
                scroll_up();
            else if (cur_row+1 < screen_height)
                cur_row++;
            at_sr_bottom = 0;
            trace("line feed: going to (%d, %d)\n", cur_row, cur_col);
            return;
        case '\b':
            cur_col--;
            if (cur_col < 0)
                cur_col = 0;
            trace("going to (%d, %d): %c\n", cur_row, cur_col, c);
            at_sr_bottom = 0;
            return;
        default:
            if (!((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISprint)) {
                fprintf(logff, "Warning: Got unhandled non-printable "
                               "character \\%o\n", c);
            }
            if (at_sr_bottom) {
                scroll_up();
                at_sr_bottom = 0;
            }
            (display[(cur_row)*screen_width + (cur_col)]) = c;
            trace("setting (%d, %d) to '%c' and ", cur_row, cur_col, c);
            if (cur_col+1 < screen_width) {
                cur_col++;
                trace("going to (%d, %d)\n", cur_row, cur_col);
                return;
            }
            cur_col = 0;
            if (cur_row == sr_end)
                at_sr_bottom = 1;
            else if (cur_row+1 < screen_height)
                cur_row++;
            trace("going to (%d, %d)\n", cur_row, cur_col);
            return;
    }
}

canonical_original_code:
static void update_display_char(char c) {
    static int cur_row; static int cur_col;
    static _Bool at_sr_bottom;
    static int esc_size;
    static char esc[20];
    void display_esc_seq(FILE *out) {
        for (int i = 1; i < esc_size; i++) {
            if (((*__ctype_b_loc ())[(int) ((esc[i]))] & (unsigned short) _ISgraph))
                fprintf(out, " %c", esc[i]);
            else
                fprintf(out, " \\%o", esc[i]);
        }
    }
    if (sr_end == 0)
        sr_end = screen_height-1;
    if (c == '\33') {
        if (esc_size) {
            fprintf(logff, "warning: Escape sequence [\\33");
            display_esc_seq(logff);
            fprintf(logff, "] aborted by an ESC\n");
        }
        esc[0] = c;
        esc_size = 1;
        return;
    }
    if (esc_size) {
        esc[esc_size++] = c;
        if ((esc[1] == '(' || esc[1] == ')') && esc_size == 3) {
            trace("ignoring: %.*s\n", esc_size, esc);
            esc_size = 0;
            return;
        }
        if ((esc[1] == '>' || esc[1] == '=') && esc_size == 2) {
            trace("ignoring: %.*s\n", esc_size, esc);
            esc_size = 0;
            return;
        }
        if (esc[1] == 'D' && esc_size == 2) {
            trace("index: %.*s\n", esc_size, esc);
            if (cur_row == sr_end)
                scroll_up();
            else if (cur_row < screen_height-1)
                cur_row++;
            esc_size = 0;
            return;
        }
        if (esc[1] == 'M' && esc_size == 2) {
            trace("reverse index: %.*s\n", esc_size, esc);
            if (cur_row == sr_start)
                scroll_down();
            else if (cur_row > 0)
                cur_row--;
            esc_size = 0;
            return;
        }
        if (esc[1] == '7' && esc_size == 2) {
            saved_row = cur_row;
            saved_col = cur_col;
            esc_size = 0;
            return;
        }
        if (esc[1] == '8' && esc_size == 2) {
            cur_row = saved_row;
            cur_col = saved_col;
            esc_size = 0;
            return;
        }
        if (esc[1] == 'H' && esc_size == 2) {
            fprintf(logff, "Warning: Ignoring request to set tab-stop.\n");
            esc_size = 0;
            return;
        }
        if (esc[1] == '[' && ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISalpha)) {
            int rv;
            switch (c) {
                default:
                    fprintf(logff, "warning: Unknown escape sequence [\\33");
                    display_esc_seq(logff);
                    fprintf(logff, "], ignoring.\n");
                    break;
                case 'm':
                case 'h':
                case 'l':
                case 'i':
                case 'J': case 'K':
                    trace("ignoring: %.*s\n", esc_size, esc);
                    break;
                case 'g':
                    fprintf(logff, "Warning: Ignoring request to clear "
                                   "tab-stop.\n");
                    break;
                case 'r':
                    if (esc_size == 3) {
                        trace("setting default scroll region "
                              "(entire display)\n");
                        sr_start = 0;
                        sr_end = screen_height-1;
                        cur_row = cur_col = 0;
                        at_sr_bottom = 0;
                        break;
                    }
                    rv = sscanf(esc+2, "%d;%d", &sr_start, &sr_end);
                    if (rv != 2) {
                        cleanup();
                        fprintf(stderr, "Invalid scroll region sequence [\\33");
                        display_esc_seq(stderr);
                        fprintf(stderr, "]\n");
                        exit('r');
                    }
                    sr_start--; sr_end--;
                    cur_row = cur_col = 0;
                    at_sr_bottom = 0;
                    trace("setting scroll region to %d-%d: %.*s\n",
                          sr_start, sr_end, esc_size, esc);
                    break;
                case 'H':
                    if (esc_size == 3) {
                        trace("going to (0, 0): %.*s\n", esc_size, esc);
                        cur_row = cur_col = 0;
                        at_sr_bottom = 0;
                        break;
                    }
                    rv = sscanf(esc+2, "%d;%d", &cur_row, &cur_col);
                    if (rv != 2) {
                        cleanup();
                        fprintf(stderr,
                                "Invalid cursor position sequence [\\33");
                        display_esc_seq(stderr);
                        fprintf(stderr, "]\n");
                        exit('H');
                    }
                    cur_row--; cur_col--;
                    trace("going to (%d, %d): %.*s\n", cur_row, cur_col,
                          esc_size, esc);
                    at_sr_bottom = 0;
                    break;
                case 'A':
                case 'B':
                case 'C':
                case 'D':;
                    int drow[4] = { -1, 1, 0, 0 };
                    int dcol[4] = { 0, 0, 1, -1 };
                    rv = 1;
                    sscanf(esc+2, "%d", &rv);
                    cur_row += rv * drow[c-'A'];
                    cur_col += rv * dcol[c-'A'];
                    if (cur_row >= screen_height)
                        cur_row = screen_height-1;
                    if (cur_row < 0)
                        cur_row = 0;
                    if (cur_col >= screen_width)
                        cur_col = screen_width-1;
                    if (cur_col < 0)
                        cur_col = 0;
                    trace("going to (%d, %d): %.*s\n", cur_row, cur_col,
                          esc_size, esc);
                    at_sr_bottom = 0;
                    break;
            }
            esc_size = 0;
            return;
        }
        if (esc_size == 20) {
            cleanup();
            fprintf(stderr, "Unknown escape sequence \\33");
            display_esc_seq(stderr);
            fprintf(stderr, "\n");
            exit('\33');
        }
        if (esc_size == 2 && esc[1] != '[' && esc[1] != '(' && esc[1] != ')') {
            fprintf(logff, "Warning: Ignoring unknown escape sequence "
                           "[\\33 \\%o]\n", esc[1]);
            esc_size = 0;
        }
        return;
    }
    switch (c) {
        case '\a': case 016: case 017:
            trace("ignoring: \\%o\n", c);
            return;
        case '\r':
            cur_col = 0;
            trace("carriage return: going to (%d, %d)\n", cur_row, cur_col);
            at_sr_bottom = 0;
            return;
        case '\t':
            cur_col = (cur_col+8) & ~7;
            if (cur_col < screen_width)
                return;
            cur_col = 0;
        case '\n':
            if (cur_row == sr_end)
                scroll_up();
            else if (cur_row+1 < screen_height)
                cur_row++;
            at_sr_bottom = 0;
            trace("line feed: going to (%d, %d)\n", cur_row, cur_col);
            return;
        case '\b':
            cur_col--;
            if (cur_col < 0)
                cur_col = 0;
            trace("going to (%d, %d): %c\n", cur_row, cur_col, c);
            at_sr_bottom = 0;
            return;
        default:
            if (!((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISprint)) {
                fprintf(logff, "Warning: Got unhandled non-printable "
                               "character \\%o\n", c);
            }
            if (at_sr_bottom) {
                scroll_up();
                at_sr_bottom = 0;
            }
            (display[(cur_row)*screen_width + (cur_col)]) = c;
            trace("setting (%d, %d) to '%c' and ", cur_row, cur_col, c);
            if (cur_col+1 < screen_width) {
                cur_col++;
                trace("going to (%d, %d)\n", cur_row, cur_col);
                return;
            }
            cur_col = 0;
            if (cur_row == sr_end)
                at_sr_bottom = 1;
            else if (cur_row+1 < screen_height)
                cur_row++;
            trace("going to (%d, %d)\n", cur_row, cur_col);
            return;
    }
}

variable_types: c char

variable_types:
esc
char [20]

variable_types: rv int

variable_types:
drow
int [4]

variable_types:
dcol
int [4]


decompiled_code:
int __fastcall update_display(char *a1, int a2)
{
char *v2; // rbx
int result; // eax
__int64 v4; // rbp
char v5; // di

v2 = a1;
result = vwrite(1LL, a1, (unsigned int)a2);
if ( a2 > 0 )
{
v4 = (__int64)&a1[a2 - 1 + 1];
do
{
v5 = *v2++;
result = update_display_char(v5);
}
while ( v2 != (char *)v4 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(char *a1, int a2)
{
char *v2; // rbx
int result; // eax
__int64 v4; // rbp
char v5; // di

v2 = a1;
result = vwrite(1LL, a1, (unsigned int)a2);
if ( a2 > 0 )
{
v4 = (__int64)&a1[a2 - 1 + 1];
do
{
v5 = *v2++;
result = func3(v5);
}
while ( v2 != (char *)v4 );
}
return result;
}

original_code:
void update_display(const char *buf, int nchar) {
    vwrite(1, buf, nchar);
    for (int i = 0; i < nchar; i++)
        update_display_char(buf[i]);
}

canonical_original_code:
void update_display(char *buf, int nchar) {
    vwrite(1, buf, nchar);
    for (int i = 0; i < nchar; i++)
        update_display_char(buf[i]);
}

variable_types:
buf
char *

variable_types: nchar int


decompiled_code:
__int64 __fastcall queue_string(char *a1)
{
char v1; // dl
__int64 result; // rax
__int64 v3; // rcx

v1 = *a1;
for ( result = (unsigned int)tqtail; *a1; v1 = *a1 )
{
v3 = (unsigned int)result;
++a1;
result = ((_WORD)result + 1) & 0x3FF;
tqueue[v3] = v1;
tqtail = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1)
{
char v1; // dl
__int64 result; // rax
__int64 v3; // rcx

v1 = *a1;
for ( result = (unsigned int)tqtail; *a1; v1 = *a1 )
{
v3 = (unsigned int)result;
++a1;
result = ((_WORD)result + 1) & 0x3FF;
tqueue[v3] = v1;
tqtail = result;
}
return result;
}

original_code:
static void queue_string(const char s[]) {
    while (*s) {
        tqueue[tqtail] = *s;
        tqtail = (tqtail+1)%1024u;
        s++;
    }
}

canonical_original_code:
static void queue_string(char s[]) {
    while (*s) {
        tqueue[tqtail] = *s;
        tqtail = (tqtail+1)%1024u;
        s++;
    }
}

variable_types:
s
char []


decompiled_code:
__int64 __fastcall order_new_bearing(char a1, int a2)
{
char v3[40]; // [rsp+0h] [rbp-28h] BYREF

sprintf(v3, "%ct%c\n", (unsigned int)a1, (unsigned int)*(char *)(bearings + 32LL * a2 + 12));
return queue_string(v3);
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1, int a2)
{
char v3[40]; // [rsp+0h] [rbp-28h] BYREF

sprintf(v3, "%ct%c\n", (unsigned int)a1, (unsigned int)*(char *)(bearings + 32LL * a2 + 12));
return func0(v3);
}

original_code:
void order_new_bearing(char id, int bearing) {
    char order[20];
    sprintf(order, "%ct%c\n", id, bearings[bearing].key);
    queue_string(order);
}

variable_types: id char

variable_types: bearing int

variable_types:
order
char [20]


decompiled_code:
__int64 __fastcall order_new_altitude(char a1, unsigned int a2)
{
char v3[40]; // [rsp+0h] [rbp-28h] BYREF

sprintf(v3, "%ca%d\n", (unsigned int)a1, a2);
return queue_string(v3);
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1, unsigned int a2)
{
char v3[40]; // [rsp+0h] [rbp-28h] BYREF

sprintf(v3, "%ca%d\n", (unsigned int)a1, a2);
return func0(v3);
}

original_code:
void order_new_altitude(char id, int alt) {
    char order[20];
    sprintf(order, "%ca%d\n", id, alt);
    queue_string(order);
}

variable_types: alt int


decompiled_code:
__int64 __fastcall land_at_airport(char a1, unsigned int a2)
{
char v3[40]; // [rsp+0h] [rbp-28h] BYREF

sprintf(v3, "%ctta%d\n%ca0\n", (unsigned int)a1, a2, (unsigned int)a1);
return queue_string(v3);
}

canonical_decompiled_code:
__int64 __fastcall func3(char a1, unsigned int a2)
{
char v3[40]; // [rsp+0h] [rbp-28h] BYREF

sprintf(v3, "%ctta%d\n%ca0\n", (unsigned int)a1, a2, (unsigned int)a1);
return func0(v3);
}

original_code:
void land_at_airport(char id, int airport_num) {
    char order[20];
    sprintf(order, "%ctta%d\n%ca0\n", id, airport_num, id);
    queue_string(order);
}


decompiled_code:
__int64 next_tick()
{
return queue_string("\n");
}

canonical_decompiled_code:
__int64 func4()
{
return func0("\n");
}

original_code:
void next_tick() {
    queue_string("\n");
}


decompiled_code:
__int64 mark_msg()
{
char *v0; // rdi

v0 = "zm";
mark_sent = 1;
if ( mark_sense == 1 )
v0 = "zu";
mark_sense ^= 1u;
return queue_string(v0);
}

canonical_decompiled_code:
__int64 func5()
{
char *v0; // rdi

v0 = "zm";
mark_sent = 1;
if ( mark_sense == 1 )
v0 = "zu";
mark_sense ^= 1u;
return func0(v0);
}

original_code:
void mark_msg() {
    mark_sense = !mark_sense;
    mark_sent = 1;
    queue_string(mark_sense ? "zm" : "zu");
}


decompiled_code:
__int64 de_mark_msg()
{
__int64 result; // rax
char v1[3]; // [rsp+Dh] [rbp-3h] BYREF

v1[2] = 0;
v1[0] = erase_char;
v1[1] = erase_char;
result = queue_string(v1);
mark_sent = 0;
return result;
}

canonical_decompiled_code:
__int64 func6()
{
__int64 result; // rax
char v1[3]; // [rsp+Dh] [rbp-3h] BYREF

v1[2] = 0;
v1[0] = erase_char;
v1[1] = erase_char;
result = func0(v1);
mark_sent = 0;
return result;
}

original_code:
void de_mark_msg() {
    char str[3] = { erase_char, erase_char, '\0' };
    queue_string(str);
    mark_sent = 0;
}

variable_types:
str
char [3]


decompiled_code:
__int64 get_ptm()
{
unsigned int v0; // er12
unsigned __int16 v2; // [rsp+8h] [rbp-10h] BYREF
unsigned __int16 v3; // [rsp+Ah] [rbp-Eh]

v0 = open("/dev/ptmx", 258);
grantpt(v0);
unlockpt(v0);
ioctl(0, 0x5413uLL, &v2);
ioctl(v0, 0x5414uLL, &v2);
screen_height = v2;
screen_width = v3;
display = count_malloc("get_ptm", v3 * v2 + 1);
memset(display, 32, screen_width * screen_height);
*(_BYTE *)(display + screen_width * screen_height) = 0;
return v0;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // er12
unsigned __int16 v2; // [rsp+8h] [rbp-10h] BYREF
unsigned __int16 v3; // [rsp+Ah] [rbp-Eh]

v0 = open("/dev/ptmx", 258);
grantpt(v0);
unlockpt(v0);
ioctl(0, 0x5413uLL, &v2);
ioctl(v0, 0x5414uLL, &v2);
screen_height = v2;
screen_width = v3;
display = count_malloc("get_ptm", v3 * v2 + 1);
memset(display, 32, screen_width * screen_height);
*(_BYTE *)(display + screen_width * screen_height) = 0;
return v0;
}

original_code:
int get_ptm() {
    struct winsize ws;
    int ptm = open("/dev/ptmx", 02|0400);
    grantpt(ptm);
    unlockpt(ptm);
    ioctl(0, 0x5413, &ws);
    ioctl(ptm, 0x5414, &ws);
    screen_height = ws.ws_row;
    screen_width = ws.ws_col;
    display = count_malloc(__func__, screen_height*screen_width+1);
    memset(display, ' ', screen_height*screen_width);
    display[screen_height*screen_width] = '\0';
    return ptm;
}

variable_types: struct winsize { unsigned short ws_row; unsigned short ws_col; unsigned short ws_xpixel; unsigned short ws_ypixel; }

variable_types: ptm int

return_type: int


spawn

decompiled_code:
__pid_t __fastcall spawn(char *file, char **argv, int fd)
{
__pid_t result; // eax
char *v6; // r14
int v7; // edi
int v8; // ebp
int *v9; // rax
char *v10; // rax

fflush(stderr);
result = fork();
if ( !result )
{
setenv("TERM", "vt100", 1);
unsetenv("TERMCAP");
v6 = ptsname(fd);
close(fd);
close(0);
close(1);
close(2);
v7 = open("/dev/tty", 2);
if ( v7 != -1 )
ioctl(v7, 0x5422uLL);
v8 = open(v6, 2);
setsid();
ioctl(v8, 0x540EuLL);
dup2(v8, 0);
dup2(v8, 1);
dup2(v8, 2);
*argv = file;
execvp(file, argv);
v9 = _errno_location();
v10 = strerror(*v9);
fprintf(stderr, "exec of %s failed: %s\n", file, v10);
fflush(stderr);
exit(-1);
}
return result;
}

canonical_decompiled_code:
__pid_t __fastcall func1(char *file, char **argv, int fd)
{
__pid_t result; // eax
char *v6; // r14
int v7; // edi
int v8; // ebp
int *v9; // rax
char *v10; // rax

fflush(stderr);
result = fork();
if ( !result )
{
setenv("TERM", "vt100", 1);
unsetenv("TERMCAP");
v6 = ptsname(fd);
close(fd);
close(0);
close(1);
close(2);
v7 = open("/dev/tty", 2);
if ( v7 != -1 )
ioctl(v7, 0x5422uLL);
v8 = open(v6, 2);
setsid();
ioctl(v8, 0x540EuLL);
dup2(v8, 0);
dup2(v8, 1);
dup2(v8, 2);
*argv = file;
execvp(file, argv);
v9 = _errno_location();
v10 = strerror(*v9);
fprintf(stderr, "exec of %s failed: %s\n", file, v10);
fflush(stderr);
exit(-1);
}
return result;
}

original_code:
int spawn(const char *cmd, const char *args[], int ptm) {
    fflush(stderr);
    int pid = fork();
    if (pid == 0) {
        setenv("TERM", "vt100", 1);
        unsetenv("TERMCAP");
        const char *ptsfn = ptsname(ptm);
        close(ptm); close(0); close(1); close(2);
        int dt = open("/dev/tty", 02);
        if (dt != -1)
            ioctl(dt, 0x5422);
        int pts = open(ptsfn, 02);
        setsid();
        ioctl(pts, 0x540E);
        dup2(pts, 0);
        dup2(pts, 1);
        dup2(pts, 2);
        args[0] = cmd;
        execvp(cmd, (char *const *)args);
        fprintf(stderr, "exec of %s failed: %s\n", cmd, strerror((*__errno_location ())));
        fflush(stderr);
        _exit(-1);
    }
    return pid;
}

canonical_original_code:
int spawn(char *cmd, char *args[], int ptm) {
    fflush(stderr);
    int pid = fork();
    if (pid == 0) {
        setenv("TERM", "vt100", 1);
        unsetenv("TERMCAP");
        char *ptsfn = ptsname(ptm);
        close(ptm); close(0); close(1); close(2);
        int dt = open("/dev/tty", 02);
        if (dt != -1)
            ioctl(dt, 0x5422);
        int pts = open(ptsfn, 02);
        setsid();
        ioctl(pts, 0x540E);
        dup2(pts, 0);
        dup2(pts, 1);
        dup2(pts, 2);
        args[0] = cmd;
        execvp(cmd, (char **)args);
        fprintf(stderr, "exec of %s failed: %s\n", cmd, strerror((*__errno_location ())));
        fflush(stderr);
        _exit(-1);
    }
    return pid;
}

variable_types:
cmd
char *

variable_types:
args
char *[]

variable_types: pid int

variable_types:
ptsfn
char *

variable_types: dt int

variable_types: pts int


decompiled_code:
void __fastcall __noreturn count_packets(void *a1)
{
_WORD *v1; // rbx
void *i; // rsi

in_buff = malloc(0x5EEuLL);
v1 = in_buff;
for ( i = in_buff; (unsigned int)recvfrom(sock_fd, i, 0x5EEuLL, 0, 0LL, 0LL) != -1; i = in_buff )
{
if ( v1[6] == 25600 && *(_DWORD *)v1 == snd_mac && v1[2] == word_4040DC )
++total_recv_packets;
}
perror("recvfrom():");
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(void *a1)
{
_WORD *v1; // rbx
void *i; // rsi

in_buff = malloc(0x5EEuLL);
v1 = in_buff;
for ( i = in_buff; (unsigned int)recvfrom(sock_fd, i, 0x5EEuLL, 0, 0LL, 0LL) != -1; i = in_buff )
{
if ( v1[6] == 25600 && *(_DWORD *)v1 == snd_mac && v1[2] == word_4040DC )
++total_recv_packets;
}
perror("recvfrom():");
exit(1);
}

original_code:
void * count_packets(void *ptr) {
 int recv;
 in_buff = (void*)malloc(1514 +4);
 struct ethhdr *in_hdr = (struct ethhdr *)in_buff;
 while (1) {
  recv = recvfrom(sock_fd, in_buff, 1518, 0, ((void *)0), ((void *)0));
  if (recv == -1) {
   perror("recvfrom():");
   exit(1);
  }
  if (in_hdr->h_proto == 0x6400 && memcmp( (const void*)in_hdr->h_dest, (const void*)snd_mac, 6) == 0 ) {
   total_recv_packets++;
  }
 }
}

canonical_original_code:
void *count_packets(void *ptr) {
 int recv;
 in_buff = (void *)malloc(1514 +4);
 struct ethhdr *in_hdr = (struct ethhdr *)in_buff;
 while (1) {
  recv = recvfrom(sock_fd, in_buff, 1518, 0, ((void *)0), ((void *)0));
  if (recv == -1) {
   perror("recvfrom():");
   exit(1);
  }
  if (in_hdr->h_proto == 0x6400 && memcmp( (void *)in_hdr->h_dest, (void *)snd_mac, 6) == 0 ) {
   total_recv_packets++;
  }
 }
}

variable_types:
ptr
void *

variable_types: recv int

variable_types: struct ethhdr { unsigned char h_dest[6]; unsigned char h_source[6]; unsigned short h_proto; } *

return_type: void *


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
char *v4; // r12
const char *v5; // rdi
int v6; // ebp
int v7; // ebx
__int64 i; // rax
int v9; // er13
__int64 j; // rax
char v11; // dl
int v12; // er15
__int16 v13; // r14
_DWORD *v14; // r12
__useconds_t v15; // ebp
__int64 v16; // rcx
_DWORD *v17; // rdi
__int64 v18; // [rsp+0h] [rbp-A8h]
int v19; // [rsp+12h] [rbp-96h]
__int16 v20; // [rsp+16h] [rbp-92h]
pthread_t newthread; // [rsp+18h] [rbp-90h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-88h] BYREF
__int16 v23; // [rsp+30h] [rbp-78h]
__int16 v24; // [rsp+32h] [rbp-76h]
char dest[16]; // [rsp+40h] [rbp-68h] BYREF
int v26[22]; // [rsp+50h] [rbp-58h]

v3 = (char *)malloc(0x5EEuLL);
out_buff = v3;
if ( argc != 4 )
{
printf("Missing arguments.\n%s [src_ifname] [dest_ifname] [time msec] \n", *argv);
exit(1);
}
v4 = v3;
v5 = argv[3];
sender_ifname = (char *)argv[1];
receiver_ifname = (char *)argv[2];
v6 = atoi(v5);
v7 = (unsigned __int16)_bswap_16_constprop_0(17LL, 3LL);
sock_fd = socket(17, 3, v7);
if ( sock_fd != -1 )
{
printf("Successfully opened socket: %i\n", (unsigned int)sock_fd);
strncpy(dest, receiver_ifname, 0x10uLL);
if ( ioctl(sock_fd, 0x8933uLL, dest) != -1 )
{
printf("Successfully got interface index: %i\n", (unsigned int)v26[0]);
if ( ioctl(sock_fd, 0x8927uLL, dest) != -1 )
{
for ( i = 0LL; i != 6; ++i )
*((_BYTE *)&v19 + i) = *((_BYTE *)v26 + i + 2);
strncpy(dest, sender_ifname, 0x10uLL);
if ( ioctl(sock_fd, 0x8933uLL, dest) != -1 )
{
v9 = v26[0];
printf("Successfully got interface index: %i\n", (unsigned int)v26[0]);
if ( ioctl(sock_fd, 0x8927uLL, dest) != -1 )
{
for ( j = 0LL; j != 6; *(_BYTE *)(j + 4210903) = v11 )
v11 = *((_BYTE *)v26 + j++ + 2);
printf(
"SENDER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
(unsigned __int8)snd_mac,
BYTE1(snd_mac),
BYTE2(snd_mac),
HIBYTE(snd_mac),
(unsigned __int8)word_4040DC,
HIBYTE(word_4040DC));
LODWORD(v18) = HIBYTE(v20);
v12 = v19;
v13 = v20;
printf(
"RECEIVER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
(unsigned __int8)v19,
BYTE1(v19),
BYTE2(v19),
HIBYTE(v19),
(unsigned __int8)v20,
v18);
addr.sa_family = 17;
*(_WORD *)addr.sa_data = v7;
*(_DWORD *)&addr.sa_data[2] = v9;
addr.sa_data[9] = 6;
*(_DWORD *)&addr.sa_data[10] = v12;
v23 = v13;
v24 = 0;
if ( bind(sock_fd, &addr, 0x14u) != -1 )
{
v14 = v4 + 14;
v15 = 1000 * v6;
strncpy(dest, sender_ifname, 0x10uLL);
ioctl(sock_fd, 0x8913uLL, dest);
LOWORD(v26[0]) |= 0x100u;
ioctl(sock_fd, 0x8913uLL, dest);
signal(2, sigint);
puts("Sending packets....");
v16 = 25LL;
*(_DWORD *)((char *)v14 - 14) = v19;
*((_WORD *)v14 - 5) = v20;
*(v14 - 2) = snd_mac;
*((_WORD *)v14 - 2) = word_4040DC;
v17 = v14;
while ( v16 )
{
*v17++ = 0;
--v16;
}
pthread_create(&newthread, 0LL, count_packets, 0LL);
while ( 1 )
{
sendto(sock_fd, out_buff, 0x72uLL, 0, &addr, 0x14u);
++total_sent_packets;
if ( __ROR8__(0xCCCCCCCCCCCCCCCDLL * total_sent_packets + 0x1999999999999998LL, 1) <= 0x1999999999999998uLL )
printf("\rtotal_sent_packets: %ld total_recv_packets: %ld", total_sent_packets, total_recv_packets);
usleep(v15);
}
}
perror("binding:");
exit(1);
}
}
}
}
perror("SIOCGIFINDEX");
exit(1);
}
perror("socket():");
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
char *v4; // r12
const char *v5; // rdi
int v6; // ebp
int v7; // ebx
__int64 i; // rax
int v9; // er13
__int64 j; // rax
char v11; // dl
int v12; // er15
__int16 v13; // r14
_DWORD *v14; // r12
__useconds_t v15; // ebp
__int64 v16; // rcx
_DWORD *v17; // rdi
__int64 v18; // [rsp+0h] [rbp-A8h]
int v19; // [rsp+12h] [rbp-96h]
__int16 v20; // [rsp+16h] [rbp-92h]
pthread_t newthread; // [rsp+18h] [rbp-90h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-88h] BYREF
__int16 v23; // [rsp+30h] [rbp-78h]
__int16 v24; // [rsp+32h] [rbp-76h]
char dest[16]; // [rsp+40h] [rbp-68h] BYREF
int v26[22]; // [rsp+50h] [rbp-58h]

v3 = (char *)malloc(0x5EEuLL);
out_buff = v3;
if ( argc != 4 )
{
printf("Missing arguments.\n%s [src_ifname] [dest_ifname] [time msec] \n", *argv);
exit(1);
}
v4 = v3;
v5 = argv[3];
sender_ifname = (char *)argv[1];
receiver_ifname = (char *)argv[2];
v6 = atoi(v5);
v7 = (unsigned __int16)func9(17LL, 3LL);
sock_fd = socket(17, 3, v7);
if ( sock_fd != -1 )
{
printf("Successfully opened socket: %i\n", (unsigned int)sock_fd);
strncpy(dest, receiver_ifname, 0x10uLL);
if ( ioctl(sock_fd, 0x8933uLL, dest) != -1 )
{
printf("Successfully got interface index: %i\n", (unsigned int)v26[0]);
if ( ioctl(sock_fd, 0x8927uLL, dest) != -1 )
{
for ( i = 0LL; i != 6; ++i )
*((_BYTE *)&v19 + i) = *((_BYTE *)v26 + i + 2);
strncpy(dest, sender_ifname, 0x10uLL);
if ( ioctl(sock_fd, 0x8933uLL, dest) != -1 )
{
v9 = v26[0];
printf("Successfully got interface index: %i\n", (unsigned int)v26[0]);
if ( ioctl(sock_fd, 0x8927uLL, dest) != -1 )
{
for ( j = 0LL; j != 6; *(_BYTE *)(j + 4210903) = v11 )
v11 = *((_BYTE *)v26 + j++ + 2);
printf(
"SENDER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
(unsigned __int8)snd_mac,
BYTE1(snd_mac),
BYTE2(snd_mac),
HIBYTE(snd_mac),
(unsigned __int8)word_4040DC,
HIBYTE(word_4040DC));
LODWORD(v18) = HIBYTE(v20);
v12 = v19;
v13 = v20;
printf(
"RECEIVER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
(unsigned __int8)v19,
BYTE1(v19),
BYTE2(v19),
HIBYTE(v19),
(unsigned __int8)v20,
v18);
addr.sa_family = 17;
*(_WORD *)addr.sa_data = v7;
*(_DWORD *)&addr.sa_data[2] = v9;
addr.sa_data[9] = 6;
*(_DWORD *)&addr.sa_data[10] = v12;
v23 = v13;
v24 = 0;
if ( bind(sock_fd, &addr, 0x14u) != -1 )
{
v14 = v4 + 14;
v15 = 1000 * v6;
strncpy(dest, sender_ifname, 0x10uLL);
ioctl(sock_fd, 0x8913uLL, dest);
LOWORD(v26[0]) |= 0x100u;
ioctl(sock_fd, 0x8913uLL, dest);
signal(2, sigint);
puts("Sending packets....");
v16 = 25LL;
*(_DWORD *)((char *)v14 - 14) = v19;
*((_WORD *)v14 - 5) = v20;
*(v14 - 2) = snd_mac;
*((_WORD *)v14 - 2) = word_4040DC;
v17 = v14;
while ( v16 )
{
*v17++ = 0;
--v16;
}
pthread_create(&newthread, 0LL, count_packets, 0LL);
while ( 1 )
{
sendto(sock_fd, out_buff, 0x72uLL, 0, &addr, 0x14u);
++total_sent_packets;
if ( __ROR8__(0xCCCCCCCCCCCCCCCDLL * total_sent_packets + 0x1999999999999998LL, 1) <= 0x1999999999999998uLL )
printf("\rtotal_sent_packets: %ld total_recv_packets: %ld", total_sent_packets, total_recv_packets);
usleep(v15);
}
}
perror("binding:");
exit(1);
}
}
}
}
perror("SIOCGIFINDEX");
exit(1);
}
perror("socket():");
exit(1);
}

original_code:
int main(int argc, char *argv[]) {
 pthread_t thread1;
 unsigned char rcv_mac[6];
 struct ifreq ifr;
 out_buff = (void*)malloc(1514 +4);
 unsigned char* data_ptr = out_buff + 14;
 struct ethhdr *out_hdr = (struct ethhdr *)out_buff;
 struct sockaddr_ll s_addr;
 int ifindex = 0;
 int i;
 int sent;
 int time;
 if (argc !=4)
     {
      printf("Missing arguments.\n"
       "%s [src_ifname] [dest_ifname] [time msec] \n",argv[0]);
      exit(1);
 }
 sender_ifname = argv[1];
 receiver_ifname = argv[2];
 time = atoi(argv[3]);
 sock_fd = socket(17, SOCK_RAW, htons(0x0003));
 if (sock_fd == -1) {
  perror("socket():");
  exit(1);
 }
 printf("Successfully opened socket: %i\n", sock_fd);
 strncpy(ifr.ifr_ifrn.ifrn_name, receiver_ifname, 16);
 if (ioctl(sock_fd, 0x8933, &ifr) == -1) {
  perror("SIOCGIFINDEX");
  exit(1);
 }
 ifindex = ifr.ifr_ifru.ifru_ivalue;
 printf("Successfully got interface index: %i\n", ifindex);
 if (ioctl(sock_fd, 0x8927, &ifr) == -1) {
  perror("SIOCGIFINDEX");
  exit(1);
 }
 for (i = 0; i < 6; i++) {
  rcv_mac[i] = ifr.ifr_ifru.ifru_hwaddr.sa_data[i];
 }
 strncpy(ifr.ifr_ifrn.ifrn_name, sender_ifname, 16);
 if (ioctl(sock_fd, 0x8933, &ifr) == -1) {
  perror("SIOCGIFINDEX");
  exit(1);
 }
 ifindex = ifr.ifr_ifru.ifru_ivalue;
 printf("Successfully got interface index: %i\n", ifindex);
 if (ioctl(sock_fd, 0x8927, &ifr) == -1) {
  perror("SIOCGIFINDEX");
  exit(1);
 }
 for (i = 0; i < 6; i++) {
  snd_mac[i] = ifr.ifr_ifru.ifru_hwaddr.sa_data[i];
 }
 printf("SENDER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
   snd_mac[0],snd_mac[1],snd_mac[2],snd_mac[3],snd_mac[4],snd_mac[5]);
 printf("RECEIVER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  rcv_mac[0],rcv_mac[1],rcv_mac[2],rcv_mac[3],rcv_mac[4],rcv_mac[5]);
 s_addr.sll_family = 17;
 s_addr.sll_protocol = htons(0x0003);
 s_addr.sll_ifindex = ifindex;
 s_addr.sll_halen = 6;
 s_addr.sll_addr[0] = rcv_mac[0];
 s_addr.sll_addr[1] = rcv_mac[1];
 s_addr.sll_addr[2] = rcv_mac[2];
 s_addr.sll_addr[3] = rcv_mac[3];
 s_addr.sll_addr[4] = rcv_mac[4];
 s_addr.sll_addr[5] = rcv_mac[5];
 s_addr.sll_addr[6] = 0x00;
 s_addr.sll_addr[7] = 0x00;
 if (bind(sock_fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1) {
  perror("binding:");
  exit(1);
 }
 strncpy(ifr.ifr_ifrn.ifrn_name,sender_ifname,16);
 ioctl(sock_fd,0x8913,&ifr);
 ifr.ifr_ifru.ifru_flags |= IFF_PROMISC;
 ioctl(sock_fd,0x8913,&ifr);
 signal(2, sigint);
 printf("Sending packets....\n");
 memcpy(out_hdr->h_dest, (void*)rcv_mac, 6);
 memcpy(out_hdr->h_source, (void*)snd_mac, 6);
 for (i = 0; i < 100; i++) {
  data_ptr[i] = (unsigned char)(0);
 }
 pthread_create( &thread1, ((void *)0), count_packets, (void*) ((void *)0));
 while (1) {
  sent = sendto(sock_fd, out_buff, i+14, 0, (struct sockaddr*)&s_addr, sizeof(s_addr));
  total_sent_packets++;
  if (!(total_sent_packets%10)){
   printf("\rtotal_sent_packets: %ld "
    "total_recv_packets: %ld",total_sent_packets,total_recv_packets);
  }
  usleep(time*1000);
 }
 return -1;
}

canonical_original_code:
int main(int argc, char *argv[]) {
 unsigned long thread1;
 unsigned char rcv_mac[6];
 struct ifreq ifr;
 out_buff = (void *)malloc(1514 +4);
 unsigned char *data_ptr = out_buff + 14;
 struct ethhdr *out_hdr = (struct ethhdr *)out_buff;
 struct sockaddr_ll s_addr;
 int ifindex = 0;
 int i;
 int sent;
 int time;
 if (argc !=4)
     {
      printf("Missing arguments.\n"
       "%s [src_ifname] [dest_ifname] [time msec] \n",argv[0]);
      exit(1);
 }
 sender_ifname = argv[1];
 receiver_ifname = argv[2];
 time = atoi(argv[3]);
 sock_fd = socket(17, SOCK_RAW, htons(0x0003));
 if (sock_fd == -1) {
  perror("socket():");
  exit(1);
 }
 printf("Successfully opened socket: %i\n", sock_fd);
 strncpy(ifr.ifr_ifrn.ifrn_name, receiver_ifname, 16);
 if (ioctl(sock_fd, 0x8933, &ifr) == -1) {
  perror("SIOCGIFINDEX");
  exit(1);
 }
 ifindex = ifr.ifr_ifru.ifru_ivalue;
 printf("Successfully got interface index: %i\n", ifindex);
 if (ioctl(sock_fd, 0x8927, &ifr) == -1) {
  perror("SIOCGIFINDEX");
  exit(1);
 }
 for (i = 0; i < 6; i++) {
  rcv_mac[i] = ifr.ifr_ifru.ifru_hwaddr.sa_data[i];
 }
 strncpy(ifr.ifr_ifrn.ifrn_name, sender_ifname, 16);
 if (ioctl(sock_fd, 0x8933, &ifr) == -1) {
  perror("SIOCGIFINDEX");
  exit(1);
 }
 ifindex = ifr.ifr_ifru.ifru_ivalue;
 printf("Successfully got interface index: %i\n", ifindex);
 if (ioctl(sock_fd, 0x8927, &ifr) == -1) {
  perror("SIOCGIFINDEX");
  exit(1);
 }
 for (i = 0; i < 6; i++) {
  snd_mac[i] = ifr.ifr_ifru.ifru_hwaddr.sa_data[i];
 }
 printf("SENDER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
   snd_mac[0],snd_mac[1],snd_mac[2],snd_mac[3],snd_mac[4],snd_mac[5]);
 printf("RECEIVER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  rcv_mac[0],rcv_mac[1],rcv_mac[2],rcv_mac[3],rcv_mac[4],rcv_mac[5]);
 s_addr.sll_family = 17;
 s_addr.sll_protocol = htons(0x0003);
 s_addr.sll_ifindex = ifindex;
 s_addr.sll_halen = 6;
 s_addr.sll_addr[0] = rcv_mac[0];
 s_addr.sll_addr[1] = rcv_mac[1];
 s_addr.sll_addr[2] = rcv_mac[2];
 s_addr.sll_addr[3] = rcv_mac[3];
 s_addr.sll_addr[4] = rcv_mac[4];
 s_addr.sll_addr[5] = rcv_mac[5];
 s_addr.sll_addr[6] = 0x00;
 s_addr.sll_addr[7] = 0x00;
 if (bind(sock_fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1) {
  perror("binding:");
  exit(1);
 }
 strncpy(ifr.ifr_ifrn.ifrn_name,sender_ifname,16);
 ioctl(sock_fd,0x8913,&ifr);
 ifr.ifr_ifru.ifru_flags |= IFF_PROMISC;
 ioctl(sock_fd,0x8913,&ifr);
 signal(2, sigint);
 printf("Sending packets....\n");
 memcpy(out_hdr->h_dest, (void *)rcv_mac, 6);
 memcpy(out_hdr->h_source, (void *)snd_mac, 6);
 for (i = 0; i < 100; i++) {
  data_ptr[i] = (unsigned char)(0);
 }
 pthread_create( &thread1, ((void *)0), count_packets, (void *) ((void *)0));
 while (1) {
  sent = sendto(sock_fd, out_buff, i+14, 0, (struct sockaddr *)&s_addr, sizeof(s_addr));
  total_sent_packets++;
  if (!(total_sent_packets%10)){
   printf("\rtotal_sent_packets: %ld "
    "total_recv_packets: %ld",total_sent_packets,total_recv_packets);
  }
  usleep(time*1000);
 }
 return -1;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; void *ifru_data; struct if_settings { unsigned int type; unsigned int size; union { struct raw_hdlc_proto { unsigned short encoding; unsigned short parity; } *raw_hdlc; struct cisco_proto { unsigned int interval; unsigned int timeout; } *cisco; struct fr_proto { unsigned int t391; unsigned int t392; unsigned int n391; unsigned int n392; unsigned int n393; unsigned short lmi; unsigned short dce; } *fr; struct fr_proto_pvc { unsigned int dlci; } *fr_pvc; struct fr_proto_pvc_info { unsigned int dlci; char master[16]; } *fr_pvc_info; struct sync_serial_settings { unsigned int clock_rate; unsigned int clock_type; unsigned short loopback; } *sync; struct te1_settings { unsigned int clock_rate; unsigned int clock_type; unsigned short loopback; unsigned int slot_map; } *te1; } ifs_ifsu; } ifru_settings; } ifr_ifru; }

variable_types: struct sockaddr_ll { unsigned short sll_family; unsigned short sll_protocol; int sll_ifindex; unsigned short sll_hatype; unsigned char sll_pkttype; unsigned char sll_halen; unsigned char sll_addr[8]; }

variable_types: ifindex int

variable_types: i int

variable_types: sent int

variable_types: time int

user_defined_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[16]; char ifru_newname[16]; void *ifru_data; struct if_settings ifru_settings; } ifr_ifru; }

user_defined_types: struct ethhdr { unsigned char h_dest[6]; unsigned char h_source[6]; unsigned short h_proto; }

user_defined_types: struct if_settings { unsigned int type; unsigned int size; union { struct raw_hdlc_proto *raw_hdlc; struct cisco_proto *cisco; struct fr_proto *fr; struct fr_proto_pvc *fr_pvc; struct fr_proto_pvc_info *fr_pvc_info; struct sync_serial_settings *sync; struct te1_settings *te1; } ifs_ifsu; }


sigint

decompiled_code:
void sigint()
{
int v0; // ebp
float v1; // xmm0_4
char dest[16]; // [rsp+10h] [rbp-38h] BYREF
__int16 v3; // [rsp+20h] [rbp-28h]

v0 = sock_fd;
if ( sock_fd != -1 )
{
strncpy(dest, sender_ifname, 0x10uLL);
ioctl(v0, 0x8913uLL, dest);
v3 &= 0xFEFFu;
ioctl(sock_fd, 0x8914uLL, dest);
close(sock_fd);
free(out_buff);
free(in_buff);
v1 = 1.0 - (double)(int)total_recv_packets / (double)(int)total_sent_packets;
puts("Client terminating....");
printf("Totally sent: %ld packets\n", total_sent_packets);
printf("Totally recv: %ld packets\n", total_recv_packets);
printf("Ploss=%f\n", v1);
exit(0);
}
}

canonical_decompiled_code:
void func8()
{
int v0; // ebp
float v1; // xmm0_4
char dest[16]; // [rsp+10h] [rbp-38h] BYREF
__int16 v3; // [rsp+20h] [rbp-28h]

v0 = sock_fd;
if ( sock_fd != -1 )
{
strncpy(dest, sender_ifname, 0x10uLL);
ioctl(v0, 0x8913uLL, dest);
v3 &= 0xFEFFu;
ioctl(sock_fd, 0x8914uLL, dest);
close(sock_fd);
free(out_buff);
free(in_buff);
v1 = 1.0 - (double)(int)total_recv_packets / (double)(int)total_sent_packets;
puts("Client terminating....");
printf("Totally sent: %ld packets\n", total_sent_packets);
printf("Totally recv: %ld packets\n", total_recv_packets);
printf("Ploss=%f\n", v1);
exit(0);
}
}

original_code:
void sigint(int signum) {
 struct ifreq ifr;
 if (sock_fd == -1)
  return;
 strncpy(ifr.ifr_ifrn.ifrn_name, sender_ifname, 16);
 ioctl(sock_fd, 0x8913, &ifr);
 ifr.ifr_ifru.ifru_flags &= ~IFF_PROMISC;
 ioctl(sock_fd, 0x8914, &ifr);
 close(sock_fd);
 free(out_buff);
 free(in_buff);
 float loss = 1-((double)total_recv_packets/(double)total_sent_packets);
 printf("Client terminating....\n");
 printf("Totally sent: %ld packets\n", total_sent_packets);
 printf("Totally recv: %ld packets\n", total_recv_packets);
 printf("Ploss=%f\n", loss);
 exit(0);
}

variable_types: signum int

variable_types: loss float

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }

user_defined_types: struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; }

user_defined_types: struct raw_hdlc_proto { unsigned short encoding; unsigned short parity; }

user_defined_types: struct cisco_proto { unsigned int interval; unsigned int timeout; }

user_defined_types: struct fr_proto { unsigned int t391; unsigned int t392; unsigned int n391; unsigned int n392; unsigned int n393; unsigned short lmi; unsigned short dce; }

user_defined_types: struct fr_proto_pvc { unsigned int dlci; }

user_defined_types: struct fr_proto_pvc_info { unsigned int dlci; char master[16]; }

user_defined_types: struct sync_serial_settings { unsigned int clock_rate; unsigned int clock_type; unsigned short loopback; }

user_defined_types: struct te1_settings { unsigned int clock_rate; unsigned int clock_type; unsigned short loopback; unsigned int slot_map; }


decompiled_code:
__int64 __fastcall put_mac_address(__int64 a1, int a2)
{
__int64 result; // rax

result = indexer;
if ( a2 > 0 )
result = indexer + 6LL * a2;
*(_DWORD *)result = a1;
*(_WORD *)(result + 4) = WORD2(a1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
__int64 result; // rax

result = indexer;
if ( a2 > 0 )
result = indexer + 6LL * a2;
*(_DWORD *)result = a1;
*(_WORD *)(result + 4) = WORD2(a1);
return result;
}

original_code:
void put_mac_address(struct mac_address addr, int pos)
{
 int i;
 void *ptr = indexer;
  for (i=0; i < pos ; i++) {
   ptr = ptr + sizeof(struct mac_address);
  }
  memcpy(ptr, &addr, sizeof(struct mac_address));
}

variable_types: struct mac_address { unsigned char addr[6]; }

variable_types: pos int


decompiled_code:
__int64 __fastcall get_mac_address(int a1)
{
__int64 result; // rax

result = 0LL;
if ( array_size > a1 )
result = indexer + 6LL * a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
__int64 result; // rax

result = 0LL;
if ( array_size > a1 )
result = indexer + 6LL * a1;
return result;
}

original_code:
struct mac_address * get_mac_address(int pos) {
 void * ptr = indexer;
 ptr = ptr + (sizeof(struct mac_address)*pos);
 return ((pos >= array_size) ? ((void *)0) : (struct mac_address*)ptr);
}

canonical_original_code:
struct mac_address *get_mac_address(int pos) {
 void *ptr = indexer;
 ptr = ptr + (sizeof(struct mac_address)*pos);
 return ((pos >= array_size) ? ((void *)0) : (struct mac_address *)ptr);
}

return_type: struct mac_address { unsigned char addr[6]; } *


decompiled_code:
__int64 __fastcall find_pos_by_mac_address(__int64 a1)
{
__int64 v1; // rax
int v2; // er8

v1 = indexer;
v2 = 0;
while ( *(_DWORD *)v1 != *(_DWORD *)a1 || *(_WORD *)(v1 + 4) != *(_WORD *)(a1 + 4) )
{
if ( array_size <= v2 )
return 0xFFFFFFFFLL;
++v2;
v1 += 6LL;
}
if ( array_size > v2 )
return (unsigned int)v2;
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // rax
int v2; // er8

v1 = indexer;
v2 = 0;
while ( *(_DWORD *)v1 != *(_DWORD *)a1 || *(_WORD *)(v1 + 4) != *(_WORD *)(a1 + 4) )
{
if ( array_size <= v2 )
return 0xFFFFFFFFLL;
++v2;
v1 += 6LL;
}
if ( array_size > v2 )
return (unsigned int)v2;
return 0xFFFFFFFFLL;
}

original_code:
int find_pos_by_mac_address(struct mac_address *addr) {
 int i=0;
 void * ptr = indexer;
 while(memcmp(ptr,addr,sizeof(struct mac_address)) && i < array_size)
 {
  i++;
  ptr = ptr + sizeof(struct mac_address);
 }
 return ((i >= array_size) ? -1 : i);
}

canonical_original_code:
int find_pos_by_mac_address(struct mac_address *addr) {
 int i = 0;
 void *ptr = indexer;
 while(memcmp(ptr,addr,sizeof(struct mac_address)) && i < array_size)
 {
  i++;
  ptr = ptr + sizeof(struct mac_address);
 }
 return ((i >= array_size) ? -1 : i);
}

variable_types: struct mac_address { unsigned char addr[6]; } *


decompiled_code:
void print_mac_address_array()
{
__int64 v0; // rbx
int v1; // ebp
_WORD *v2; // rax
__int16 v3; // dx
_WORD *v4; // rcx
__int64 v5; // rsi

v0 = indexer;
if ( array_size > 0 )
{
v1 = 0;
do
{
v0 += 6LL;
v2 = malloc(6uLL);
v3 = *(_WORD *)(v0 - 2);
v4 = v2;
LODWORD(v2) = *(_DWORD *)(v0 - 6);
v4[2] = v3;
*(_DWORD *)v4 = (_DWORD)v2;
v5 = (unsigned int)v1++;
printf(
"A[%d]:%02X:%02X:%02X:%02X:%02X:%02X\n",
v5,
(unsigned __int8)v2,
BYTE1(v2),
BYTE2(v2),
(unsigned int)v2 >> 24,
(unsigned __int8)v3,
HIBYTE(v3));
}
while ( array_size > v1 );
}
}

canonical_decompiled_code:
void func3()
{
__int64 v0; // rbx
int v1; // ebp
_WORD *v2; // rax
__int16 v3; // dx
_WORD *v4; // rcx
__int64 v5; // rsi

v0 = indexer;
if ( array_size > 0 )
{
v1 = 0;
do
{
v0 += 6LL;
v2 = malloc(6uLL);
v3 = *(_WORD *)(v0 - 2);
v4 = v2;
LODWORD(v2) = *(_DWORD *)(v0 - 6);
v4[2] = v3;
*(_DWORD *)v4 = (_DWORD)v2;
v5 = (unsigned int)v1++;
printf(
"A[%d]:%02X:%02X:%02X:%02X:%02X:%02X\n",
v5,
(unsigned __int8)v2,
BYTE1(v2),
BYTE2(v2),
(unsigned int)v2 >> 24,
(unsigned __int8)v3,
HIBYTE(v3));
}
while ( array_size > v1 );
}
}

original_code:
void print_mac_address_array() {
 int i=0;
 void * ptr = indexer;
 while (i < array_size) {
  struct mac_address *a = malloc(sizeof(struct mac_address));
  memcpy(a,ptr,sizeof(struct mac_address));
  printf("A[%d]:%02X:%02X:%02X:%02X:%02X:%02X\n",
         i,a->addr[0], a->addr[1], a->addr[2],
         a->addr[3], a->addr[4], a->addr[5]);
  i++;
  ptr = ptr + sizeof(struct mac_address);
 }
}

canonical_original_code:
void print_mac_address_array() {
 int i = 0;
 void *ptr = indexer;
 while (i < array_size) {
  struct mac_address *a = malloc(sizeof(struct mac_address));
  memcpy(a,ptr,sizeof(struct mac_address));
  printf("A[%d]:%02X:%02X:%02X:%02X:%02X:%02X\n",
         i,a->addr[0], a->addr[1], a->addr[2],
         a->addr[3], a->addr[4], a->addr[5]);
  i++;
  ptr = ptr + sizeof(struct mac_address);
 }
}

user_defined_types: struct mac_address { unsigned char addr[6]; }


decompiled_code:
void *__fastcall init_probability(int a1)
{
array_size = a1;
indexer = (__int64)malloc(6LL * a1);
if ( !indexer )
{
printf("Problem allocating vector");
exit(1);
}
return calloc(96LL * a1 * a1, 1uLL);
}

canonical_decompiled_code:
void *__fastcall func7(int a1)
{
array_size = a1;
indexer = (__int64)malloc(6LL * a1);
if ( !indexer )
{
printf("Problem allocating vector");
exit(1);
}
return calloc(96LL * a1 * a1, 1uLL);
}

original_code:
double * init_probability(int size) {
 array_size = size;
 indexer = malloc(sizeof(struct mac_address)*array_size);
 if (indexer==((void *)0)) {
  printf("Problem allocating vector");
  exit(1);
 }
  double * mat = malloc(sizeof(double)*(size*size)*12);
  memset(mat,0,sizeof(double)*(size*size)*12);
  return mat;
}

canonical_original_code:
double *init_probability(int size) {
 array_size = size;
 indexer = malloc(sizeof(struct mac_address)*array_size);
 if (indexer==((void *)0)) {
  printf("Problem allocating vector");
  exit(1);
 }
  double *mat = malloc(sizeof(double)*(size*size)*12);
  memset(mat,0,sizeof(double)*(size*size)*12);
  return mat;
}

variable_types: size int

variable_types:
mat
double *

return_type: double *


decompiled_code:
void kill_handler()
{
running = 0;
}

canonical_decompiled_code:
void func0()
{
running = 0;
}

original_code:
void kill_handler() {
 running = 0;
}

canonical_original_code:
void kill_handler() {
 running = 0;
}


decompiled_code:
double generate_random_double()
{
return (double)rand() * 4.656612873077393e-10;
}

original_code:
double generate_random_double()
{
 return rand()/((double)2147483647 +1);
}

return_type: double


decompiled_code:
__int64 __fastcall send_tx_info_frame_nl(__int64 a1, unsigned int a2, unsigned int a3, __int64 a4, __int64 a5)
{
unsigned int v8; // eax
unsigned int v9; // er12
__int64 v11; // [rsp-8h] [rbp-30h]

msg = (void *)nlmsg_alloc();
if ( !msg )
{
puts("Error allocating new message MSG!");
goto LABEL_6;
}
v8 = genl_family_get_id(family);
genlmsg_put(msg, 0LL, 0LL, v8, 0LL, 1LL, 3LL, 1LL);
nla_put(msg, 2LL, 6LL, a1);
nla_put_u32(msg, 4LL, a2);
nla_put_u32(msg, 6LL, a3);
nla_put(msg, 7LL, 10LL, a4);
v9 = nla_put_u64(msg, 8LL, a5);
if ( v9 )
{
puts("Error filling payload");
LABEL_6:
v9 = -1;
nlmsg_free(msg);
return v9;
}
nl_send_auto_complete(sock, msg, v11);
nlmsg_free(msg);
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned int a2, unsigned int a3, __int64 a4, __int64 a5)
{
unsigned int v8; // eax
unsigned int v9; // er12
__int64 v11; // [rsp-8h] [rbp-30h]

msg = (void *)nlmsg_alloc();
if ( !msg )
{
puts("Error allocating new message MSG!");
goto LABEL_6;
}
v8 = genl_family_get_id(family);
genlmsg_put(msg, 0LL, 0LL, v8, 0LL, 1LL, 3LL, 1LL);
nla_put(msg, 2LL, 6LL, a1);
nla_put_u32(msg, 4LL, a2);
nla_put_u32(msg, 6LL, a3);
nla_put(msg, 7LL, 10LL, a4);
v9 = nla_put_u64(msg, 8LL, a5);
if ( v9 )
{
puts("Error filling payload");
LABEL_6:
v9 = -1;
nlmsg_free(msg);
return v9;
}
nl_send_auto_complete(sock, msg, v11);
nlmsg_free(msg);
return v9;
}

original_code:
int send_tx_info_frame_nl(struct mac_address *src,
     unsigned int flags, int signal,
     struct hwsim_tx_rate *tx_attempts,
     unsigned long cookie)
{
 msg = nlmsg_alloc();
 if (!msg) {
  printf("Error allocating new message MSG!\n");
  goto out;
 }
 genlmsg_put(msg, 0, 0, genl_family_get_id(family),
      0, 0x01, 3, 1);
 int rc;
 rc = nla_put(msg, 2,
       sizeof(struct mac_address), src);
 rc = nla_put_u32(msg, 4, flags);
 rc = nla_put_u32(msg, 6, signal);
 rc = nla_put(msg, 7,
       5 *
       sizeof(struct hwsim_tx_rate), tx_attempts);
 rc = nla_put_u64(msg, 8, cookie);
 if(rc!=0) {
  printf("Error filling payload\n");
  goto out;
 }
 nl_send_auto_complete(sock,msg);
 nlmsg_free(msg);
 return 0;
out:
 nlmsg_free(msg);
 return -1;
}

variable_types:
flags
unsigned int

variable_types: signal int

variable_types: struct hwsim_tx_rate { char idx; unsigned char count; } *

variable_types:
cookie
unsigned long

variable_types: rc int


decompiled_code:
__int64 __fastcall send_cloned_frame_msg(__int64 a1, __int64 a2, unsigned int a3, unsigned int a4, unsigned int a5)
{
unsigned int v8; // eax
unsigned int v9; // er12
__int64 v11; // [rsp-8h] [rbp-30h]

msg = (void *)nlmsg_alloc();
if ( !msg )
{
puts("Error allocating new message MSG!");
goto LABEL_6;
}
v8 = genl_family_get_id(family);
genlmsg_put(msg, 0LL, 0LL, v8, 0LL, 1LL, 2LL, 1LL);
nla_put(msg, 1LL, 6LL, a1);
nla_put(msg, 3LL, a3, a2);
nla_put_u32(msg, 5LL, a4);
v9 = nla_put_u32(msg, 6LL, a5);
if ( v9 )
{
puts("Error filling payload");
LABEL_6:
v9 = -1;
nlmsg_free(msg);
return v9;
}
nl_send_auto_complete(sock, msg, v11);
nlmsg_free(msg);
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, unsigned int a3, unsigned int a4, unsigned int a5)
{
unsigned int v8; // eax
unsigned int v9; // er12
__int64 v11; // [rsp-8h] [rbp-30h]

msg = (void *)nlmsg_alloc();
if ( !msg )
{
puts("Error allocating new message MSG!");
goto LABEL_6;
}
v8 = genl_family_get_id(family);
genlmsg_put(msg, 0LL, 0LL, v8, 0LL, 1LL, 2LL, 1LL);
nla_put(msg, 1LL, 6LL, a1);
nla_put(msg, 3LL, a3, a2);
nla_put_u32(msg, 5LL, a4);
v9 = nla_put_u32(msg, 6LL, a5);
if ( v9 )
{
puts("Error filling payload");
LABEL_6:
v9 = -1;
nlmsg_free(msg);
return v9;
}
nl_send_auto_complete(sock, msg, v11);
nlmsg_free(msg);
return v9;
}

original_code:
int send_cloned_frame_msg(struct mac_address *dst,
     char *data, int data_len, int rate_idx, int signal)
{
 msg = nlmsg_alloc();
 if (!msg) {
  printf("Error allocating new message MSG!\n");
  goto out;
 }
 genlmsg_put(msg, 0, 0, genl_family_get_id(family),
      0, 0x01, 2, 1);
 int rc;
 rc = nla_put(msg, 1,
       sizeof(struct mac_address), dst);
 rc = nla_put(msg, 3, data_len, data);
 rc = nla_put_u32(msg, 5, rate_idx);
 rc = nla_put_u32(msg, 6, signal);
 if(rc!=0) {
  printf("Error filling payload\n");
  goto out;
 }
 nl_send_auto_complete(sock,msg);
 nlmsg_free(msg);
 return 0;
out:
 nlmsg_free(msg);
 return -1;
}

variable_types:
data
char *


decompiled_code:
__int64 __fastcall get_signal_by_rate(int a1)
{
__int64 v2[6]; // [rsp+0h] [rbp-30h]

v2[0] = 0xFFFFFFB3FFFFFFB0LL;
v2[1] = 0xFFFFFFB9FFFFFFB6LL;
v2[2] = 0xFFFFFFBEFFFFFFBBLL;
v2[3] = 0xFFFFFFC2FFFFFFC0LL;
v2[4] = 0xFFFFFFC8FFFFFFC5LL;
v2[5] = 0xFFFFFFCEFFFFFFCBLL;
return *((unsigned int *)v2 + a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
__int64 v2[6]; // [rsp+0h] [rbp-30h]

v2[0] = 0xFFFFFFB3FFFFFFB0LL;
v2[1] = 0xFFFFFFB9FFFFFFB6LL;
v2[2] = 0xFFFFFFBEFFFFFFBBLL;
v2[3] = 0xFFFFFFC2FFFFFFC0LL;
v2[4] = 0xFFFFFFC8FFFFFFC5LL;
v2[5] = 0xFFFFFFCEFFFFFFCBLL;
return *((unsigned int *)v2 + a1);
}

original_code:
int get_signal_by_rate(int rate_idx)
{
 const int rate2signal [] =
  { -80,-77,-74,-71,-69,-66,-64,-62,-59,-56,-53,-50 };
 if (rate_idx >= 0 || rate_idx < 12)
  return rate2signal[rate_idx];
 return 0;
}

canonical_original_code:
int get_signal_by_rate(int rate_idx)
{
 int rate2signal[] = { -80,-77,-74,-71,-69,-66,-64,-62,-59,-56,-53,-50 };
 if (rate_idx >= 0 || rate_idx < 12)
  return rate2signal[rate_idx];
 return 0;
}