decompiled_code:
__int64 lookup_kas_init()
{
puts("Initalizing kallsyms db");
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
puts("Initalizing kallsyms db");
return 0LL;
}

original_code:
static int lookup_kas_init(void)
{
 printf("Initalizing kallsyms db\n");
 return 0;
}

canonical_original_code:
static int lookup_kas_init()
{
 printf("Initalizing kallsyms db\n");
 return 0;
}


decompiled_code:
__int64 lookup_bfd_sym()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 1LL;
}

original_code:
static int lookup_bfd_sym(void *pc, struct loc_result *location)
{
 return 1;
}

variable_types:
pc
void *

variable_types: struct loc_result { char *symbol; unsigned long long offset; } *

return_type: int


decompiled_code:
__int64 lookup_bfd_init()
{
size_t v0; // rax
char *v1; // rbx
__int64 result; // rax
struct stat v3; // [rsp+0h] [rbp-228h] BYREF
struct utsname name; // [rsp+90h] [rbp-198h] BYREF

if ( uname(&name) < 0 )
return 0xFFFFFFFFLL;
v0 = strlen(name.release);
v1 = (char *)malloc(v0 + 27);
sprintf(v1, "/usr/lib/debug/lib/modules/%s", name.release);
if ( stat(v1, &v3) < 0 )
{
free(v1);
result = 0xFFFFFFFFLL;
}
else
{
free(v1);
bfd_init();
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
size_t v0; // rax
char *v1; // rbx
__int64 result; // rax
struct stat v3; // [rsp+0h] [rbp-228h] BYREF
struct utsname name; // [rsp+90h] [rbp-198h] BYREF

if ( uname(&name) < 0 )
return 0xFFFFFFFFLL;
v0 = strlen(name.release);
v1 = (char *)malloc(v0 + 27);
sprintf(v1, "/usr/lib/debug/lib/modules/%s", name.release);
if ( stat(v1, &v3) < 0 )
{
free(v1);
result = 0xFFFFFFFFLL;
}
else
{
free(v1);
bfd_init();
result = 0LL;
}
return result;
}

original_code:
static int lookup_bfd_init(void)
{
 struct utsname uts;
 struct stat sb;
 char *dbibuf;
 if(uname(&uts)<0)
  return-1;
 dbibuf = malloc(strlen("/usr/lib/debug/lib/modules") + strlen(uts.release) + 1);
 sprintf(dbibuf,"/usr/lib/debug/lib/modules/%s", uts.release);
 if (stat(dbibuf,&sb) < 0) {
  free(dbibuf);
  goto out_fail;
 }
 free(dbibuf);
 bfd_init();
 return 0;
out_fail:
 return-1;
}

canonical_original_code:
static int lookup_bfd_init()
{
 struct utsname uts;
 struct stat sb;
 char *dbibuf;
 if(uname(&uts)<0)
  return-1;
 dbibuf = malloc(strlen("/usr/lib/debug/lib/modules") + strlen(uts.release) + 1);
 sprintf(dbibuf,"/usr/lib/debug/lib/modules/%s", uts.release);
 if (stat(dbibuf,&sb) < 0) {
  free(dbibuf);
  goto out_fail;
 }
 free(dbibuf);
 bfd_init();
 return 0;
out_fail:
 return-1;
}

variable_types: struct utsname { char sysname[65]; char nodename[65]; char release[65]; char version[65]; char machine[65]; char domainname[65]; }

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types:
dbibuf
char *

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


repo: DEGoodmanWilson/FlashFIFO

decompiled_code:
void __fastcall store_write(unsigned int a1, __int64 a2, __int64 a3)
{
__int64 v4; // rsi
unsigned __int64 v5; // rcx
__int64 v6; // rax

v4 = a1;
v5 = a3 + a1;
if ( a1 < v5 )
{
v6 = a1;
do
{
store[a1] &= *(_BYTE *)(a2 + v6 - v4);
v6 = ++a1;
}
while ( a1 < v5 );
}
}

canonical_decompiled_code:
void __fastcall func0(unsigned int a1, __int64 a2, __int64 a3)
{
__int64 v4; // rsi
unsigned __int64 v5; // rcx
__int64 v6; // rax

v4 = a1;
v5 = a3 + a1;
if ( a1 < v5 )
{
v6 = a1;
do
{
store[a1] &= *(_BYTE *)(a2 + v6 - v4);
v6 = ++a1;
}
while ( a1 < v5 );
}
}

original_code:
static void store_write(uint32_t addr, void*data, size_t n)
{
    for (uint32_t i = addr; i < (addr + n); ++i)
    {
        store[i] &= *(uint8_t*) (data + i - addr);
    }
}

canonical_original_code:
static void store_write(unsigned int addr, void *data, unsigned long n)
{
    for (unsigned int i = addr; i < (addr + n); ++i)
    {
        store[i] &= *(unsigned char *) (data + i - addr);
    }
}

variable_types:
addr
unsigned int

variable_types:
data
void *

variable_types:
n
unsigned long

variable_types:
i
unsigned int

return_type: void


decompiled_code:
__int64 __fastcall store_erase_page(unsigned __int16 a1)
{
__int64 result; // rax
int v2; // ecx

LODWORD(result) = a1 << 7;
v2 = result + ((int)result + 128 < (unsigned int)(result + 1) ? 0xFFFFFF81 : 0) + 128;
do
{
store[(unsigned int)result] = -1;
result = (unsigned int)(result + 1);
}
while ( (_DWORD)result != v2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int16 a1)
{
__int64 result; // rax
int v2; // ecx

LODWORD(result) = a1 << 7;
v2 = result + ((int)result + 128 < (unsigned int)(result + 1) ? 0xFFFFFF81 : 0) + 128;
do
{
store[(unsigned int)result] = -1;
result = (unsigned int)(result + 1);
}
while ( (_DWORD)result != v2 );
return result;
}

original_code:
static void store_erase_page(uint16_t page_num)
{
    uint32_t addr = page_num*( 128 );
    for (uint32_t i = addr; i < (addr + ( 128 )); ++i)
        store[i] = 0xFF;
}

canonical_original_code:
static void store_erase_page(unsigned short page_num)
{
    unsigned int addr = page_num*( 128 );
    for (unsigned int i = addr; i < (addr + ( 128 )); ++i)
        store[i] = 0xFF;
}


decompiled_code:
void __fastcall store_read(int a1, _BYTE *a2, __int64 a3)
{
_BYTE *v3; // rax
int v4; // edi

if ( a3 )
{
v3 = a2;
v4 = a1 - (_DWORD)a2;
do
{
*v3 = store[v4 + (_DWORD)v3];
++v3;
}
while ( v3 != &a2[a3] );
}
}

canonical_decompiled_code:
void __fastcall func2(int a1, _BYTE *a2, __int64 a3)
{
_BYTE *v3; // rax
int v4; // edi

if ( a3 )
{
v3 = a2;
v4 = a1 - (_DWORD)a2;
do
{
*v3 = store[v4 + (_DWORD)v3];
++v3;
}
while ( v3 != &a2[a3] );
}
}

original_code:
static void store_read(uint32_t addr, void* data, size_t n)
{
    for (uint32_t i = 0; i < n; ++i)
        *(uint8_t*) (data + i) = store[i + addr];
}

canonical_original_code:
static void store_read(unsigned int addr, void *data, unsigned long n)
{
    for (unsigned int i = 0; i < n; ++i)
        *(unsigned char *) (data + i) = store[i + addr];
}


decompiled_code:
char *flash_init()
{
char *result; // rax

is_off = 0;
fail_after = 0;
write_count = 0;
result = store;
do
*result++ = -1;
while ( result != (char *)(unsigned int)((_DWORD)store + 0x2000) );
return result;
}

canonical_decompiled_code:
char *func3()
{
char *result; // rax

is_off = 0;
fail_after = 0;
write_count = 0;
result = store;
do
*result++ = -1;
while ( result != (char *)(unsigned int)((_DWORD)store + 0x2000) );
return result;
}

original_code:
void flash_init(void)
{
    write_count = fail_after = is_off = 0;
    for (uint32_t i = 0; i < ( 64 * ( 128 ) ); ++i)
        store[i] = 0xFF;
}

canonical_original_code:
void flash_init()
{
    write_count = fail_after = is_off = 0;
    for (unsigned int i = 0; i < ( 64 * ( 128 ) ); ++i)
        store[i] = 0xFF;
}


decompiled_code:
void __fastcall flash_force_fail(char a1)
{
fail_after = a1;
write_count = 0;
}

canonical_decompiled_code:
void __fastcall func4(char a1)
{
fail_after = a1;
write_count = 0;
}

original_code:
void flash_force_fail(uint8_t count)
{
    fail_after = count;
    write_count = 0;
}

canonical_original_code:
void flash_force_fail(unsigned char count)
{
    fail_after = count;
    write_count = 0;
}

variable_types:
count
unsigned char


decompiled_code:
void flash_force_succeed()
{
fail_after = 0;
write_count = 0;
is_off = 0;
}

canonical_decompiled_code:
void func5()
{
fail_after = 0;
write_count = 0;
is_off = 0;
}

original_code:
void flash_force_succeed(void)
{
    fail_after = 0;
    write_count = 0;
    is_off = 0;
}


decompiled_code:
__int64 __fastcall flash_write(unsigned int a1, __int64 a2, __int64 a3)
{
unsigned int v3; // ebx
__int64 result; // rax

if ( a1 > 0x1FFF )
_assert_fail("addr < FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 0x57u, "flash_write");
v3 = a3;
if ( a3 + (unsigned __int64)a1 > 0x2000 )
_assert_fail("addr + n <= FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 0x58u, "flash_write");
if ( fail_after && fail_after == write_count )
is_off = 1;
++write_count;
result = 0LL;
if ( !is_off )
{
store_write(a1, a2, a3);
result = v3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1, __int64 a2, __int64 a3)
{
unsigned int v3; // ebx
__int64 result; // rax

if ( a1 > 0x1FFF )
_assert_fail("addr < FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 0x57u, "flash_write");
v3 = a3;
if ( a3 + (unsigned __int64)a1 > 0x2000 )
_assert_fail("addr + n <= FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 0x58u, "flash_write");
if ( fail_after && fail_after == write_count )
is_off = 1;
++write_count;
result = 0LL;
if ( !is_off )
{
func0(a1, a2, a3);
result = v3;
}
return result;
}

original_code:
int flash_write(uint32_t addr, void*data, size_t n)
{
    ((addr < ( 64 * ( 128 ) )) ? (void) (0) : __assert_fail ("addr < FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 87, __extension__ __PRETTY_FUNCTION__));
    ((addr + n <= ( 64 * ( 128 ) )) ? (void) (0) : __assert_fail ("addr + n <= FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 88, __extension__ __PRETTY_FUNCTION__));
    if (fail_after)
    {
        if (fail_after == write_count)
        {
            is_off = 1;
        }
    }
    write_count++;
    if (is_off) return 0;
    store_write(addr, data, n);
    return n;
}

canonical_original_code:
int flash_write(unsigned int addr, void *data, unsigned long n)
{
    ((addr < ( 64 * ( 128 ) )) ? (void) (0) : __assert_fail ("addr < FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 87, __extension__ __PRETTY_FUNCTION__));
    ((addr + n <= ( 64 * ( 128 ) )) ? (void) (0) : __assert_fail ("addr + n <= FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 88, __extension__ __PRETTY_FUNCTION__));
    if (fail_after)
    {
        if (fail_after == write_count)
        {
            is_off = 1;
        }
    }
    write_count++;
    if (is_off) return 0;
    store_write(addr, data, n);
    return n;
}


decompiled_code:
__int64 __fastcall flash_read(unsigned int a1, _BYTE *a2, __int64 a3)
{
unsigned int v3; // ebx

if ( a1 > 0x1FFF )
_assert_fail("addr < FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 0x6Eu, "flash_read");
v3 = a3;
if ( a3 + (unsigned __int64)a1 > 0x2000 )
_assert_fail("addr + n <= FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 0x6Fu, "flash_read");
store_read(a1, a2, a3);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1, _BYTE *a2, __int64 a3)
{
unsigned int v3; // ebx

if ( a1 > 0x1FFF )
_assert_fail("addr < FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 0x6Eu, "flash_read");
v3 = a3;
if ( a3 + (unsigned __int64)a1 > 0x2000 )
_assert_fail("addr + n <= FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 0x6Fu, "flash_read");
func2(a1, a2, a3);
return v3;
}

original_code:
int flash_read(uint32_t addr, void* data, size_t n)
{
    ((addr < ( 64 * ( 128 ) )) ? (void) (0) : __assert_fail ("addr < FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 110, __extension__ __PRETTY_FUNCTION__));
    ((addr + n <= ( 64 * ( 128 ) )) ? (void) (0) : __assert_fail ("addr + n <= FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 111, __extension__ __PRETTY_FUNCTION__));
    store_read(addr, data, n);
    return n;
}

canonical_original_code:
int flash_read(unsigned int addr, void *data, unsigned long n)
{
    ((addr < ( 64 * ( 128 ) )) ? (void) (0) : __assert_fail ("addr < FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 110, __extension__ __PRETTY_FUNCTION__));
    ((addr + n <= ( 64 * ( 128 ) )) ? (void) (0) : __assert_fail ("addr + n <= FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 111, __extension__ __PRETTY_FUNCTION__));
    store_read(addr, data, n);
    return n;
}


decompiled_code:
__int64 __fastcall flash_erase(unsigned int a1, __int64 a2)
{
if ( a1 > 0x1FFF )
_assert_fail("addr < FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 0x77u, "flash_erase");
if ( a2 + (unsigned __int64)a1 > 0x2000 )
_assert_fail("len + addr <= FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 0x78u, "flash_erase");
if ( (a2 & 0x7F) != 0 )
_assert_fail("len % FLASH_PAGE_SIZE == 0", "Test/flash_port_mock.c", 0x79u, "flash_erase");
if ( (a1 & 0x7F) != 0 )
_assert_fail("addr % FLASH_PAGE_SIZE == 0", "Test/flash_port_mock.c", 0x7Au, "flash_erase");
return store_erase_page(a1 >> 7);
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, __int64 a2)
{
if ( a1 > 0x1FFF )
_assert_fail("addr < FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 0x77u, "flash_erase");
if ( a2 + (unsigned __int64)a1 > 0x2000 )
_assert_fail("len + addr <= FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 0x78u, "flash_erase");
if ( (a2 & 0x7F) != 0 )
_assert_fail("len % FLASH_PAGE_SIZE == 0", "Test/flash_port_mock.c", 0x79u, "flash_erase");
if ( (a1 & 0x7F) != 0 )
_assert_fail("addr % FLASH_PAGE_SIZE == 0", "Test/flash_port_mock.c", 0x7Au, "flash_erase");
return func1(a1 >> 7);
}

original_code:
void flash_erase(uint32_t addr, size_t len)
{
    ((addr < ( 64 * ( 128 ) )) ? (void) (0) : __assert_fail ("addr < FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 119, __extension__ __PRETTY_FUNCTION__));
    ((len + addr <= ( 64 * ( 128 ) )) ? (void) (0) : __assert_fail ("len + addr <= FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 120, __extension__ __PRETTY_FUNCTION__));
    ((len % ( 128 ) == 0) ? (void) (0) : __assert_fail ("len % FLASH_PAGE_SIZE == 0", "Test/flash_port_mock.c", 121, __extension__ __PRETTY_FUNCTION__));
    ((addr % ( 128 ) == 0) ? (void) (0) : __assert_fail ("addr % FLASH_PAGE_SIZE == 0", "Test/flash_port_mock.c", 122, __extension__ __PRETTY_FUNCTION__));
    store_erase_page(addr / ( 128 ));
}

canonical_original_code:
void flash_erase(unsigned int addr, unsigned long len)
{
    ((addr < ( 64 * ( 128 ) )) ? (void) (0) : __assert_fail ("addr < FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 119, __extension__ __PRETTY_FUNCTION__));
    ((len + addr <= ( 64 * ( 128 ) )) ? (void) (0) : __assert_fail ("len + addr <= FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 120, __extension__ __PRETTY_FUNCTION__));
    ((len % ( 128 ) == 0) ? (void) (0) : __assert_fail ("len % FLASH_PAGE_SIZE == 0", "Test/flash_port_mock.c", 121, __extension__ __PRETTY_FUNCTION__));
    ((addr % ( 128 ) == 0) ? (void) (0) : __assert_fail ("addr % FLASH_PAGE_SIZE == 0", "Test/flash_port_mock.c", 122, __extension__ __PRETTY_FUNCTION__));
    store_erase_page(addr / ( 128 ));
}

variable_types:
len
unsigned long


decompiled_code:
__int64 __fastcall read_chunk(int fd, unsigned int a2, unsigned int *a3, void *a4, size_t a5, int a6)
{
void *v8; // r12
size_t v9; // rbp
size_t v11; // rsi
__int64 result; // rax
__off_t v13; // rdx
int *v14; // rax
__off_t v15; // rbx
int *v16; // rax
__off_t v17; // rax
__off_t v18; // rax
__off_t v19; // rbx
int *v20; // rax

if ( !a3 )
{
fprintf(stderr, "read_chunk: NULL header for chunk ID 0x%08X\n", a2);
return 0LL;
}
v8 = a4;
v9 = a5;
if ( !a4 || !a5 )
{
v9 = 0LL;
v8 = 0LL;
}
if ( read(fd, a3, 8uLL) == 8 )
{
if ( *a3 != a2 )
{
v17 = lseek(fd, 0LL, 1);
fprintf(stderr, "Expected chunk ID = 0x%08x. Read chunk ID = 0x%08X. Offset = %d.\n", a2, *a3, v17);
return 0LL;
}
if ( v8 )
{
if ( !v9 )
_assert_fail("data_size > 0", "wavheader.c", 0x64u, "read_chunk");
if ( a3[1] < v9 )
{
v18 = lseek(fd, 0LL, 1);
fprintf(
stderr,
"Tried to read more data than the chunk contains for ID 0x%08X. Chunk data size = %d bytes, read size = %d byte"
"s. Offset = %d.\n",
a2,
a3[1],
v9,
v18);
return 0LL;
}
if ( read(fd, v8, v9) != v9 )
{
v19 = lseek(fd, 0LL, 1);
v20 = _errno_location();
fprintf(
stderr,
"Error while reading chunk data for chunk ID 0x%08X. Error = %d. Offset = %d.\n",
a2,
(unsigned int)*v20,
v19);
return 0LL;
}
v11 = a3[1] - v9;
}
else
{
v11 = a3[1];
}
result = 1LL;
if ( a6 )
{
v13 = lseek(fd, (a3[1] & 1) + v11, 1);
result = 1LL;
if ( v13 == -1 )
{
v14 = _errno_location();
fprintf(stderr, "Error while seeking to the end of chunk ID 0x%08X. Error = %d.\n", a2, (unsigned int)*v14);
result = 0LL;
}
}
}
else
{
v15 = lseek(fd, 0LL, 1);
v16 = _errno_location();
fprintf(
stderr,
"Error reading the header for chunk ID 0x%08X. Error = %d. Offset = %d.\n",
a2,
(unsigned int)*v16,
v15);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, unsigned int a2, unsigned int *a3, void *a4, size_t a5, int a6)
{
void *v8; // r12
size_t v9; // rbp
size_t v11; // rsi
__int64 result; // rax
__off_t v13; // rdx
int *v14; // rax
__off_t v15; // rbx
int *v16; // rax
__off_t v17; // rax
__off_t v18; // rax
__off_t v19; // rbx
int *v20; // rax

if ( !a3 )
{
fprintf(stderr, "read_chunk: NULL header for chunk ID 0x%08X\n", a2);
return 0LL;
}
v8 = a4;
v9 = a5;
if ( !a4 || !a5 )
{
v9 = 0LL;
v8 = 0LL;
}
if ( read(fd, a3, 8uLL) == 8 )
{
if ( *a3 != a2 )
{
v17 = lseek(fd, 0LL, 1);
fprintf(stderr, "Expected chunk ID = 0x%08x. Read chunk ID = 0x%08X. Offset = %d.\n", a2, *a3, v17);
return 0LL;
}
if ( v8 )
{
if ( !v9 )
_assert_fail("data_size > 0", "wavheader.c", 0x64u, "read_chunk");
if ( a3[1] < v9 )
{
v18 = lseek(fd, 0LL, 1);
fprintf(
stderr,
"Tried to read more data than the chunk contains for ID 0x%08X. Chunk data size = %d bytes, read size = %d byte"
"s. Offset = %d.\n",
a2,
a3[1],
v9,
v18);
return 0LL;
}
if ( read(fd, v8, v9) != v9 )
{
v19 = lseek(fd, 0LL, 1);
v20 = _errno_location();
fprintf(
stderr,
"Error while reading chunk data for chunk ID 0x%08X. Error = %d. Offset = %d.\n",
a2,
(unsigned int)*v20,
v19);
return 0LL;
}
v11 = a3[1] - v9;
}
else
{
v11 = a3[1];
}
result = 1LL;
if ( a6 )
{
v13 = lseek(fd, (a3[1] & 1) + v11, 1);
result = 1LL;
if ( v13 == -1 )
{
v14 = _errno_location();
fprintf(stderr, "Error while seeking to the end of chunk ID 0x%08X. Error = %d.\n", a2, (unsigned int)*v14);
result = 0LL;
}
}
}
else
{
v15 = lseek(fd, 0LL, 1);
v16 = _errno_location();
fprintf(
stderr,
"Error reading the header for chunk ID 0x%08X. Error = %d. Offset = %d.\n",
a2,
(unsigned int)*v16,
v15);
result = 0LL;
}
return result;
}

original_code:
static int read_chunk(int fd, unsigned int id, struct chunk_header *header, void *chunk_data, size_t data_size, int skip_data) {
   if (!header) {
      fprintf(stderr, "read_chunk: NULL header for chunk ID 0x%08X\n", id);
      return 0;
   }
   if (!chunk_data || !data_size) {
      chunk_data = ((void *)0);
      data_size = 0;
   }
   if (read(fd, header, sizeof(*header)) != sizeof(*header)) {
      fprintf(stderr,
              "Error reading the header for chunk ID 0x%08X. Error = %d. "
                 "Offset = %d.\n",
              id,
              (*__errno_location ()),
              lseek(fd, 0, 1));
      return 0;
   }
   if (header->id != id) {
      fprintf(stderr,
              "Expected chunk ID = 0x%08x. Read chunk ID = 0x%08X. Offset = "
                 "%d.\n",
              id,
              header->id,
              lseek(fd, 0, 1));
      return 0;
   }
   if (chunk_data) {
      ((void) sizeof ((data_size > 0) ? 1 : 0), __extension__ ({ if (data_size > 0) ; else __assert_fail ("data_size > 0", "wavheader.c", 100, __extension__ __PRETTY_FUNCTION__); }));
      if (data_size > header->size) {
         fprintf(stderr,
                 "Tried to read more data than the chunk contains for ID "
                    "0x%08X. Chunk data size = %d bytes, read size = %d "
                    "bytes. Offset = %d.\n",
                 id,
                 header->size,
                 data_size,
                 lseek(fd, 0, 1));
         return 0;
      }
      if (read(fd, chunk_data, data_size) != data_size) {
         fprintf(stderr,
                 "Error while reading chunk data for chunk ID 0x%08X. "
                    "Error = %d. Offset = %d.\n",
                 id,
                 (*__errno_location ()),
                 lseek(fd, 0, 1));
         return 0;
      }
      data_size = header->size - data_size;
   } else
     data_size = header->size;
   if (skip_data) {
      data_size += header->size % 2;
      if (lseek(fd, data_size, 1)==-1) {
         fprintf(stderr,
                 "Error while seeking to the end of chunk ID 0x%08X. Error = "
                    "%d.\n",
                 id,
                 (*__errno_location ()));
         return 0;
      }
   }
   return 1;
}

canonical_original_code:
static int read_chunk(int fd, unsigned int id, struct chunk_header *header, void *chunk_data, unsigned long data_size, int skip_data) {
   if (!header) {
      fprintf(stderr, "read_chunk: NULL header for chunk ID 0x%08X\n", id);
      return 0;
   }
   if (!chunk_data || !data_size) {
      chunk_data = ((void *)0);
      data_size = 0;
   }
   if (read(fd, header, sizeof(*header)) != sizeof(*header)) {
      fprintf(stderr,
              "Error reading the header for chunk ID 0x%08X. Error = %d. "
                 "Offset = %d.\n",
              id,
              (*__errno_location ()),
              lseek(fd, 0, 1));
      return 0;
   }
   if (header->id != id) {
      fprintf(stderr,
              "Expected chunk ID = 0x%08x. Read chunk ID = 0x%08X. Offset = "
                 "%d.\n",
              id,
              header->id,
              lseek(fd, 0, 1));
      return 0;
   }
   if (chunk_data) {
      ((void) sizeof ((data_size > 0) ? 1 : 0), __extension__ ({ if (data_size > 0) ; else __assert_fail ("data_size > 0", "wavheader.c", 100, __extension__ __PRETTY_FUNCTION__); }));
      if (data_size > header->size) {
         fprintf(stderr,
                 "Tried to read more data than the chunk contains for ID "
                    "0x%08X. Chunk data size = %d bytes, read size = %d "
                    "bytes. Offset = %d.\n",
                 id,
                 header->size,
                 data_size,
                 lseek(fd, 0, 1));
         return 0;
      }
      if (read(fd, chunk_data, data_size) != data_size) {
         fprintf(stderr,
                 "Error while reading chunk data for chunk ID 0x%08X. "
                    "Error = %d. Offset = %d.\n",
                 id,
                 (*__errno_location ()),
                 lseek(fd, 0, 1));
         return 0;
      }
      data_size = header->size - data_size;
   } else
     data_size = header->size;
   if (skip_data) {
      data_size += header->size % 2;
      if (lseek(fd, data_size, 1)==-1) {
         fprintf(stderr,
                 "Error while seeking to the end of chunk ID 0x%08X. Error = "
                    "%d.\n",
                 id,
                 (*__errno_location ()));
         return 0;
      }
   }
   return 1;
}

variable_types: fd int

variable_types:
id
unsigned int

variable_types: struct chunk_header { unsigned int id; unsigned int size; } *


decompiled_code:
int __fastcall print_riff_info(unsigned int *a1)
{
unsigned int v2; // [rsp+6h] [rbp-12h] BYREF
char v3; // [rsp+Ah] [rbp-Eh]
unsigned int v4; // [rsp+Bh] [rbp-Dh] BYREF
char v5; // [rsp+Fh] [rbp-9h]

v4 = *a1;
v5 = 0;
v2 = a1[2];
v3 = 0;
puts("-- Riff Header --");
printf(" Chunk ID: 0x%x (%s)\n", *a1, (const char *)&v4);
printf(" Size: %.2f KiB\n", (double)(int)a1[1] * 0.0009765625);
return printf(" Format: 0x%x (%s)\n", a1[2], (const char *)&v2);
}

canonical_decompiled_code:
int __fastcall func3(unsigned int *a1)
{
unsigned int v2; // [rsp+6h] [rbp-12h] BYREF
char v3; // [rsp+Ah] [rbp-Eh]
unsigned int v4; // [rsp+Bh] [rbp-Dh] BYREF
char v5; // [rsp+Fh] [rbp-9h]

v4 = *a1;
v5 = 0;
v2 = a1[2];
v3 = 0;
puts("-- Riff Header --");
printf(" Chunk ID: 0x%x (%s)\n", *a1, (const char *)&v4);
printf(" Size: %.2f KiB\n", (double)(int)a1[1] * 0.0009765625);
return printf(" Format: 0x%x (%s)\n", a1[2], (const char *)&v2);
}

original_code:
void print_riff_info(struct riff_header* h) {
  char id_string[5];
  char fmt_string[5];
  memcpy(id_string, &h->header.id, 4);
  id_string[4] = '\0';
  memcpy(fmt_string, &h->Format, 4);
  fmt_string[4] = '\0';
  printf("-- Riff Header --\n");
  printf("  Chunk ID: 0x%x (%s)\n", h->header.id, id_string);
  printf("  Size: %.2f KiB\n", h->header.size / 1024.0);
  printf("  Format: 0x%x (%s)\n", h->Format, fmt_string);
}

canonical_original_code:
void print_riff_info(struct riff_header *h) {
  char id_string[5];
  char fmt_string[5];
  memcpy(id_string, &h->header.id, 4);
  id_string[4] = '\0';
  memcpy(fmt_string, &h->Format, 4);
  fmt_string[4] = '\0';
  printf("-- Riff Header --\n");
  printf("  Chunk ID: 0x%x (%s)\n", h->header.id, id_string);
  printf("  Size: %.2f KiB\n", h->header.size / 1024.0);
  printf("  Format: 0x%x (%s)\n", h->Format, fmt_string);
}

variable_types: struct riff_header { struct chunk_header { unsigned int id; unsigned int size; } header; unsigned int Format; } *

user_defined_types: struct riff_header { struct chunk_header header; unsigned int Format; }


decompiled_code:
int __fastcall print_format_info(unsigned int *a1)
{
unsigned int v2; // [rsp+Bh] [rbp-Dh] BYREF
char v3; // [rsp+Fh] [rbp-9h]

v2 = *a1;
v3 = 0;
puts("-- Format Header --");
printf(" Chunk ID: 0x%x (%s)\n", *a1, (const char *)&v2);
printf(" Size: %i bytes\n", a1[1]);
printf(" Audio Format: %i (1=PCM)\n", (unsigned int)*((__int16 *)a1 + 4));
printf(" Num Channels: %i (2=Stereo)\n", (unsigned int)*((__int16 *)a1 + 5));
printf(" Sampling Rate: %i Hz\n", a1[3]);
printf(" ByteRate: %i Bps\n", a1[4]);
printf(" BlockAlign: %i\n", (unsigned int)*((__int16 *)a1 + 10));
return printf(" Bits/Sample: %i\n", (unsigned int)*((__int16 *)a1 + 11));
}

canonical_decompiled_code:
int __fastcall func4(unsigned int *a1)
{
unsigned int v2; // [rsp+Bh] [rbp-Dh] BYREF
char v3; // [rsp+Fh] [rbp-9h]

v2 = *a1;
v3 = 0;
puts("-- Format Header --");
printf(" Chunk ID: 0x%x (%s)\n", *a1, (const char *)&v2);
printf(" Size: %i bytes\n", a1[1]);
printf(" Audio Format: %i (1=PCM)\n", (unsigned int)*((__int16 *)a1 + 4));
printf(" Num Channels: %i (2=Stereo)\n", (unsigned int)*((__int16 *)a1 + 5));
printf(" Sampling Rate: %i Hz\n", a1[3]);
printf(" ByteRate: %i Bps\n", a1[4]);
printf(" BlockAlign: %i\n", (unsigned int)*((__int16 *)a1 + 10));
return printf(" Bits/Sample: %i\n", (unsigned int)*((__int16 *)a1 + 11));
}

original_code:
void print_format_info(struct fmt_header* h) {
  char id_string[5];
  memcpy(id_string, &h->header.id, 4);
  id_string[4] = '\0';
  printf("-- Format Header --\n");
  printf("  Chunk ID: 0x%x (%s)\n", h->header.id, id_string);
  printf("  Size: %i bytes\n", h->header.size);
  printf("  Audio Format: %i (1=PCM)\n", h->AudioFormat);
  printf("  Num Channels: %i (2=Stereo)\n", h->NumChannels);
  printf("  Sampling Rate: %i Hz\n", h->SampleRate);
  printf("  ByteRate: %i Bps\n", h->ByteRate);
  printf("  BlockAlign: %i\n", h->BlockAlign);
  printf("  Bits/Sample: %i\n", h->BitsPerSample);
}

canonical_original_code:
void print_format_info(struct fmt_header *h) {
  char id_string[5];
  memcpy(id_string, &h->header.id, 4);
  id_string[4] = '\0';
  printf("-- Format Header --\n");
  printf("  Chunk ID: 0x%x (%s)\n", h->header.id, id_string);
  printf("  Size: %i bytes\n", h->header.size);
  printf("  Audio Format: %i (1=PCM)\n", h->AudioFormat);
  printf("  Num Channels: %i (2=Stereo)\n", h->NumChannels);
  printf("  Sampling Rate: %i Hz\n", h->SampleRate);
  printf("  ByteRate: %i Bps\n", h->ByteRate);
  printf("  BlockAlign: %i\n", h->BlockAlign);
  printf("  Bits/Sample: %i\n", h->BitsPerSample);
}

variable_types: struct fmt_header { struct chunk_header { unsigned int id; unsigned int size; } header; short AudioFormat; short NumChannels; unsigned int SampleRate; unsigned int ByteRate; short BlockAlign; short BitsPerSample; } *

user_defined_types: struct fmt_header { struct chunk_header header; short AudioFormat; short NumChannels; unsigned int SampleRate; unsigned int ByteRate; short BlockAlign; short BitsPerSample; }


decompiled_code:
int __fastcall print_data_info(unsigned int *a1)
{
unsigned int v2; // [rsp+Bh] [rbp-Dh] BYREF
char v3; // [rsp+Fh] [rbp-9h]

v2 = *a1;
v3 = 0;
puts("-- Data Header --");
printf(" Chunk ID: 0x%x (%s)\n", *a1, (const char *)&v2);
return printf(" Size: %.2f KiB\n", (double)(int)a1[1] * 0.0009765625);
}

canonical_decompiled_code:
int __fastcall func5(unsigned int *a1)
{
unsigned int v2; // [rsp+Bh] [rbp-Dh] BYREF
char v3; // [rsp+Fh] [rbp-9h]

v2 = *a1;
v3 = 0;
puts("-- Data Header --");
printf(" Chunk ID: 0x%x (%s)\n", *a1, (const char *)&v2);
return printf(" Size: %.2f KiB\n", (double)(int)a1[1] * 0.0009765625);
}

original_code:
void print_data_info(struct chunk_header* h) {
  char id_string[5];
  memcpy(id_string, &h->id, 4);
  id_string[4] = '\0';
  printf("-- Data Header --\n");
  printf("  Chunk ID: 0x%x (%s)\n", h->id, id_string);
  printf("  Size: %.2f KiB\n", h->size / 1024.0);
}

canonical_original_code:
void print_data_info(struct chunk_header *h) {
  char id_string[5];
  memcpy(id_string, &h->id, 4);
  id_string[4] = '\0';
  printf("-- Data Header --\n");
  printf("  Chunk ID: 0x%x (%s)\n", h->id, id_string);
  printf("  Size: %.2f KiB\n", h->size / 1024.0);
}

user_defined_types: struct chunk_header { unsigned int id; unsigned int size; }


decompiled_code:
__int64 __fastcall process_headers(int fd, unsigned int *a2)
{
__int64 result; // rax

if ( a2 )
{
result = read_chunk(fd, 0x46464952u, a2, a2 + 2, 4uLL, 0);
if ( (_DWORD)result )
{
result = read_chunk(fd, 0x20746D66u, a2 + 3, a2 + 5, 0x10uLL, 1);
if ( (_DWORD)result )
result = (unsigned int)skip_chunks(fd, 0x61746164u, a2 + 9) != 0;
}
}
else
{
fwrite("process_headers: wav_file_header is NULL\n", 1uLL, 0x29uLL, stderr);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(int fd, unsigned int *a2)
{
__int64 result; // rax

if ( a2 )
{
result = func2(fd, 0x46464952u, a2, a2 + 2, 4uLL, 0);
if ( (_DWORD)result )
{
result = func2(fd, 0x20746D66u, a2 + 3, a2 + 5, 0x10uLL, 1);
if ( (_DWORD)result )
result = (unsigned int)func1(fd, 0x61746164u, a2 + 9) != 0;
}
}
else
{
fwrite("process_headers: wav_file_header is NULL\n", 1uLL, 0x29uLL, stderr);
result = 0LL;
}
return result;
}

original_code:
int process_headers(int fd, struct wav_file_headers *h) {
  if (!h) {
     fprintf(stderr, "process_headers: wav_file_header is NULL\n");
     return 0;
  }
  if (!read_chunk(fd, 0x46464952, &h->riff.header, (unsigned char *)&h->riff + sizeof(h->riff.header), sizeof(h->riff) - sizeof(h->riff.header), 0))
     return 0;
  if (!read_chunk(fd, 0x20746d66, &h->fmt.header, (unsigned char *)&h->fmt + sizeof(h->fmt.header), sizeof(h->fmt) - sizeof(h->fmt.header), 1))
     return 0;
  if (!skip_chunks(fd, 0x61746164, &h->data))
     return 0;
  return 1;
}

variable_types: struct wav_file_headers { struct riff_header { struct chunk_header { unsigned int id; unsigned int size; } header; unsigned int Format; } riff; struct fmt_header { struct chunk_header header; short AudioFormat; short NumChannels; unsigned int SampleRate; unsigned int ByteRate; short BlockAlign; short BitsPerSample; } fmt; struct chunk_header data; } *

user_defined_types: struct wav_file_headers { struct riff_header riff; struct fmt_header fmt; struct chunk_header data; }


decompiled_code:
__int64 __fastcall write_headers(int fd, void *buf)
{
__off_t v2; // rax
__off_t v3; // r12
int *v4; // rax
__int64 result; // rax
int *v6; // rax
int *v7; // rax
ssize_t v8; // rdx
int *v9; // rax

v2 = lseek(fd, 0LL, 1);
if ( v2 == -1 )
{
v6 = _errno_location();
fprintf(stderr, "write_headers: Could not get the file offset. Error = %d.\n", (unsigned int)*v6);
result = 0LL;
}
else
{
v3 = v2;
if ( buf )
{
if ( write(fd, buf, 0xCuLL) == 12 )
{
if ( write(fd, (char *)buf + 12, 0x18uLL) == 24 )
{
v8 = write(fd, (char *)buf + 36, 8uLL);
result = 1LL;
if ( v8 != 8 )
{
v9 = _errno_location();
fprintf(
stderr,
"write_headers: Error while writing data header. Start offset = %d. Error = %d.\n",
v3 + 36,
(unsigned int)*v9);
result = 0LL;
}
}
else
{
v7 = _errno_location();
fprintf(
stderr,
"write_headers: Error while writing format header. Start offset = %d. Error = %d.\n",
v3 + 12,
(unsigned int)*v7);
result = 0LL;
}
}
else
{
v4 = _errno_location();
fprintf(
stderr,
"write_headers: Error while writing RIFF header. Start offset = %d. Error = %d.\n",
v3,
(unsigned int)*v4);
result = 0LL;
}
}
else
{
fwrite("write_headers: wav_file_headers is NULL\n", 1uLL, 0x28uLL, stderr);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int fd, void *buf)
{
__off_t v2; // rax
__off_t v3; // r12
int *v4; // rax
__int64 result; // rax
int *v6; // rax
int *v7; // rax
ssize_t v8; // rdx
int *v9; // rax

v2 = lseek(fd, 0LL, 1);
if ( v2 == -1 )
{
v6 = _errno_location();
fprintf(stderr, "write_headers: Could not get the file offset. Error = %d.\n", (unsigned int)*v6);
result = 0LL;
}
else
{
v3 = v2;
if ( buf )
{
if ( write(fd, buf, 0xCuLL) == 12 )
{
if ( write(fd, (char *)buf + 12, 0x18uLL) == 24 )
{
v8 = write(fd, (char *)buf + 36, 8uLL);
result = 1LL;
if ( v8 != 8 )
{
v9 = _errno_location();
fprintf(
stderr,
"write_headers: Error while writing data header. Start offset = %d. Error = %d.\n",
v3 + 36,
(unsigned int)*v9);
result = 0LL;
}
}
else
{
v7 = _errno_location();
fprintf(
stderr,
"write_headers: Error while writing format header. Start offset = %d. Error = %d.\n",
v3 + 12,
(unsigned int)*v7);
result = 0LL;
}
}
else
{
v4 = _errno_location();
fprintf(
stderr,
"write_headers: Error while writing RIFF header. Start offset = %d. Error = %d.\n",
v3,
(unsigned int)*v4);
result = 0LL;
}
}
else
{
fwrite("write_headers: wav_file_headers is NULL\n", 1uLL, 0x28uLL, stderr);
result = 0LL;
}
}
return result;
}

original_code:
int write_headers(int fd, struct wav_file_headers* h) {
   off_t offset = lseek(fd, 0, 1);
   if (offset==-1) {
      fprintf(stderr,
              "write_headers: Could not get the file offset. Error = %d.\n",
              (*__errno_location ()));
      return 0;
   }
   if (!h) {
      fprintf(stderr, "write_headers: wav_file_headers is NULL\n");
      return 0;
   }
  if (write(fd, &h->riff, sizeof(h->riff)) != sizeof(h->riff)) {
     fprintf(stderr,
             "write_headers: Error while writing RIFF header. Start offset = "
                "%d. Error = %d.\n",
             offset,
             (*__errno_location ()));
     return 0;
   }
  offset += sizeof(h->riff);
  if (write(fd, &h->fmt, sizeof(h->fmt)) != sizeof(h->fmt)) {
     fprintf(stderr,
             "write_headers: Error while writing format header. Start offset = "
                "%d. Error = %d.\n",
             offset,
             (*__errno_location ()));
     return 0;
  }
  offset += sizeof(h->fmt);
  if (write(fd, &h->data, sizeof(h->data)) != sizeof(h->data)) {
     fprintf(stderr,
             "write_headers: Error while writing data header. Start offset = "
               "%d. Error = %d.\n",
             offset,
             (*__errno_location ()));
     return 0;
  }
  return 1;
}

canonical_original_code:
int write_headers(int fd, struct wav_file_headers *h) {
   long offset = lseek(fd, 0, 1);
   if (offset==-1) {
      fprintf(stderr,
              "write_headers: Could not get the file offset. Error = %d.\n",
              (*__errno_location ()));
      return 0;
   }
   if (!h) {
      fprintf(stderr, "write_headers: wav_file_headers is NULL\n");
      return 0;
   }
  if (write(fd, &h->riff, sizeof(h->riff)) != sizeof(h->riff)) {
     fprintf(stderr,
             "write_headers: Error while writing RIFF header. Start offset = "
                "%d. Error = %d.\n",
             offset,
             (*__errno_location ()));
     return 0;
   }
  offset += sizeof(h->riff);
  if (write(fd, &h->fmt, sizeof(h->fmt)) != sizeof(h->fmt)) {
     fprintf(stderr,
             "write_headers: Error while writing format header. Start offset = "
                "%d. Error = %d.\n",
             offset,
             (*__errno_location ()));
     return 0;
  }
  offset += sizeof(h->fmt);
  if (write(fd, &h->data, sizeof(h->data)) != sizeof(h->data)) {
     fprintf(stderr,
             "write_headers: Error while writing data header. Start offset = "
               "%d. Error = %d.\n",
             offset,
             (*__errno_location ()));
     return 0;
  }
  return 1;
}

variable_types: offset long


usage

repo: DQNEO/minigit

decompiled_code:
size_t usage()
{
fwrite("Usage:\n", 1uLL, 7uLL, stderr);
fwrite(" zlib -c in_file out_file\n", 1uLL, 0x1BuLL, stderr);
return fwrite(" zlib -d in_file out_file\n", 1uLL, 0x1BuLL, stderr);
}

canonical_decompiled_code:
size_t func9()
{
fwrite("Usage:\n", 1uLL, 7uLL, stderr);
fwrite(" zlib -c in_file out_file\n", 1uLL, 0x1BuLL, stderr);
return fwrite(" zlib -d in_file out_file\n", 1uLL, 0x1BuLL, stderr);
}

original_code:
void usage()
{
    fprintf(stderr, "Usage:\n");
    fprintf(stderr, "  zlib -c in_file out_file\n");
    fprintf(stderr, "  zlib -d in_file out_file\n");
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdx
int v4; // er13
FILE *v5; // r12
FILE *v6; // rax
FILE *v7; // rbp

if ( argc == 1 )
{
usage();
exit(0);
}
v3 = argv[1];
if ( !strcmp(v3, "-c") )
{
v4 = 1;
}
else
{
v4 = strcmp(v3, "-d");
if ( v4 )
{
usage();
exit(0);
}
}
v5 = fopen(argv[2], "r");
if ( !v5 )
{
fprintf(stderr, "Can't open %s\n", argv[2]);
exit(1);
}
v6 = fopen(argv[3], "w");
v7 = v6;
if ( !v6 )
{
fprintf(stderr, "Can't open %s\n", argv[3]);
exit(1);
}
if ( v4 )
compress(v5, v6);
else
decompress(v5, v6);
fclose(v5);
fclose(v7);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdx
int v4; // er13
FILE *v5; // r12
FILE *v6; // rax
FILE *v7; // rbp

if ( argc == 1 )
{
func9();
exit(0);
}
v3 = argv[1];
if ( !strcmp(v3, "-c") )
{
v4 = 1;
}
else
{
v4 = strcmp(v3, "-d");
if ( v4 )
{
func9();
exit(0);
}
}
v5 = fopen(argv[2], "r");
if ( !v5 )
{
fprintf(stderr, "Can't open %s\n", argv[2]);
exit(1);
}
v6 = fopen(argv[3], "w");
v7 = v6;
if ( !v6 )
{
fprintf(stderr, "Can't open %s\n", argv[3]);
exit(1);
}
if ( v4 )
func7(v5, v6);
else
func8(v5, v6);
fclose(v5);
fclose(v7);
return 0;
}

original_code:
int main(int argc, char **argv)
{
    char *filename;
    printf("[parsing packe file] %s\n", argv[1]);
    filename = argv[1];
    parse_file(filename);
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
filename
char *


original_code:
void calc_values()
{
    long long i;
    long long highest = 0;
    table[1] = 1;
    for (i = 2; i < 1000000; i++) {
        long long ans = 0;
        long long tmp = i;
        if (table[i] != 0)
            continue;
        while (tmp != 1) {
            if (tmp % 2 == 0) {
                tmp /= 2;
                ans++;
            } else {
                tmp = (tmp * 3 + 1);
                ans++;
            }
            if (tmp > highest)
                highest = tmp;
        }
        table[i] = ans;
        for (tmp = i; tmp < 1000000; tmp *= 2) {
            ans++;
            table[tmp] = ans;
        }
    }
}

variable_types:
i
long long

variable_types:
highest
long long

variable_types:
ans
long long

variable_types:
tmp
long long


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // esi
unsigned int v4; // eax
unsigned __int64 v5; // rcx
unsigned int v7; // [rsp+8h] [rbp-10h]
unsigned int v8; // [rsp+Ch] [rbp-Ch]

calc_values();
while ( (unsigned int)__isoc99_scanf() != -1 )
{
getchar();
if ( v8 > v7 )
{
v3 = v8;
v4 = v7;
}
else
{
v3 = v7;
v4 = v8;
}
v5 = 0LL;
do
{
if ( v5 < table[v4] )
v5 = table[v4];
++v4;
}
while ( v3 >= v4 );
printf("%d %d %d\n", v8, v7, v5);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // esi
unsigned int v4; // eax
unsigned __int64 v5; // rcx
unsigned int v7; // [rsp+8h] [rbp-10h]
unsigned int v8; // [rsp+Ch] [rbp-Ch]

func7();
while ( (unsigned int)__isoc99_scanf() != -1 )
{
getchar();
if ( v8 > v7 )
{
v3 = v8;
v4 = v7;
}
else
{
v3 = v7;
v4 = v8;
}
v5 = 0LL;
do
{
if ( v5 < table[v4] )
v5 = table[v4];
++v4;
}
while ( v3 >= v4 );
printf("%d %d %d\n", v8, v7, v5);
}
return 0;
}

original_code:
int main(int argc, char **argv)
{
    unsigned int low, high, cur;
    unsigned long long highest = 0;
    unsigned long long i;
    calc_values();
    while (scanf("%d %d", &low, &high) != (-1)) {
        unsigned int llow, lhigh;
        getchar();
        if (low > high) {
            llow = high;
            lhigh = low;
        } else {
            llow = low;
            lhigh = high;
        }
        highest = 0;
        for (cur = llow; cur <= lhigh; cur++) {
            register long long count = table[cur];
            if (count > highest)
                highest = count;
        }
        printf("%d %d %d\n", low, high, highest);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    unsigned int low; unsigned int high; unsigned int cur;
    unsigned long long highest = 0;
    unsigned long long i;
    calc_values();
    while (scanf("%d %d", &low, &high) != (-1)) {
        unsigned int llow; unsigned int lhigh;
        getchar();
        if (low > high) {
            llow = high;
            lhigh = low;
        } else {
            llow = low;
            lhigh = high;
        }
        highest = 0;
        for (cur = llow; cur <= lhigh; cur++) {
            register long long count = table[cur];
            if (count > highest)
                highest = count;
        }
        printf("%d %d %d\n", low, high, highest);
    }
    return 0;
}

variable_types:
low
unsigned int

variable_types:
high
unsigned int

variable_types:
cur
unsigned int

variable_types:
highest
unsigned long long

variable_types:
i
unsigned long long

variable_types:
llow
unsigned int

variable_types:
lhigh
unsigned int

variable_types:
count
long long


decompiled_code:
__int64 __fastcall check_spot(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 result; // rax
__int64 v4; // rdx
int v5; // ecx

result = a3;
v4 = a2 >> 32;
if ( a2 >= 0 && (int)a2 >= 0 && rows > (int)a2 && columns > SHIDWORD(a2) )
{
v5 = 0;
if ( map[100 * (int)a1 + (a1 >> 32)] > map[100 * (int)a2 + v4] )
v5 = largest_path[100 * (int)a2 + v4] + 1;
if ( (int)result < v5 )
result = (unsigned int)v5;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 result; // rax
__int64 v4; // rdx
int v5; // ecx

result = a3;
v4 = a2 >> 32;
if ( a2 >= 0 && (int)a2 >= 0 && rows > (int)a2 && columns > SHIDWORD(a2) )
{
v5 = 0;
if ( map[100 * (int)a1 + (a1 >> 32)] > map[100 * (int)a2 + v4] )
v5 = largest_path[100 * (int)a2 + v4] + 1;
if ( (int)result < v5 )
result = (unsigned int)v5;
}
return result;
}

original_code:
static int check_spot(struct pos p, struct pos target, int max)
{
    int new_max = 0;
    if (target.row >= 0 && target.column >= 0 && target.row < rows && target.column < columns) {
        int val_cur = map[p.row][p.column];
        int val_spot = map[target.row][target.column];
        if (val_spot < val_cur)
            new_max = largest_path[target.row][target.column] + 1;
        if (new_max > max)
            max = new_max;
    }
    return max;
}

variable_types:
p
struct pos { int row; int column; }

variable_types:
target
struct pos

variable_types: max int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int v4; // eax
int v5; // edx
__int64 v6; // rcx
__int64 j; // rbx
int i; // ebp
__int64 v9; // r12
unsigned __int64 v10; // r12
unsigned int v11; // ebx
unsigned int v12; // ebp
unsigned __int64 v13; // r14
unsigned int v14; // eax
unsigned int v15; // eax
unsigned int v16; // eax
int v17; // eax
int v18; // er15
__int64 v19; // [rsp+8h] [rbp-180h]
__int64 v20; // [rsp+10h] [rbp-178h]
__int64 v21; // [rsp+18h] [rbp-170h]
int *v22; // [rsp+20h] [rbp-168h]
int v23; // [rsp+2Ch] [rbp-15Ch]
int v24; // [rsp+3Ch] [rbp-14Ch] BYREF
char v25[268]; // [rsp+40h] [rbp-148h] BYREF
int v26[15]; // [rsp+14Ch] [rbp-3Ch] BYREF

v26[0] = 0;
result = scanf("%d", v26);
if ( v26[0] > 0 )
{
v23 = 0;
do
{
scanf("%s %d %d", v25, &rows, &columns);
memset(nums_by_height_count, 0, sizeof(nums_by_height_count));
memset(nums_by_height, 0, (size_t)&unk_7A1200);
memset(map, 0, sizeof(map));
memset(largest_path, 0, sizeof(largest_path));
v9 = 0LL;
for ( i = 0; rows > i; v9 += 100LL )
{
for ( j = 0LL; columns > (int)j; ++j )
{
scanf("%d", &v24);
v4 = v24;
map[j + v9] = v24;
v5 = nums_by_height_count[v4];
v6 = v5 + 10000LL * v4;
nums_by_height[2 * v6] = i;
dword_417904[2 * v6] = j;
nums_by_height_count[v4] = v5 + 1;
}
++i;
}
v22 = nums_by_height_count;
v21 = 0LL;
v18 = 0;
do
{
if ( *v22 > 0 )
{
v10 = 8 * v21;
v20 = 8 * (v21 + (unsigned int)(*v22 - 1) + 1);
do
{
v11 = nums_by_height[v10 / 4];
v12 = dword_417904[v10 / 4];
v13 = (unsigned __int64)v12 << 32;
v19 = v11;
v14 = check_spot(v13 | v11, v13 | (v11 - 1), 0);
v15 = check_spot(v13 | v11, v13 | (v11 + 1), v14);
v16 = check_spot(v13 | v19, v19 | ((unsigned __int64)(v12 + 1) << 32), v15);
v17 = check_spot(v13 | v19, v19 | ((unsigned __int64)(v12 - 1) << 32), v16);
largest_path[100 * v11 + v12] = v17;
if ( v18 < v17 )
v18 = v17;
v10 += 8LL;
}
while ( v20 != v10 );
}
++v22;
v21 += 10000LL;
}
while ( v21 != 1000000 );
printf("%s: %d\n", v25, (unsigned int)(v18 + 1));
result = ++v23;
}
while ( v26[0] > v23 );
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int v4; // eax
int v5; // edx
__int64 v6; // rcx
__int64 j; // rbx
int i; // ebp
__int64 v9; // r12
unsigned __int64 v10; // r12
unsigned int v11; // ebx
unsigned int v12; // ebp
unsigned __int64 v13; // r14
unsigned int v14; // eax
unsigned int v15; // eax
unsigned int v16; // eax
int v17; // eax
int v18; // er15
__int64 v19; // [rsp+8h] [rbp-180h]
__int64 v20; // [rsp+10h] [rbp-178h]
__int64 v21; // [rsp+18h] [rbp-170h]
int *v22; // [rsp+20h] [rbp-168h]
int v23; // [rsp+2Ch] [rbp-15Ch]
int v24; // [rsp+3Ch] [rbp-14Ch] BYREF
char v25[268]; // [rsp+40h] [rbp-148h] BYREF
int v26[15]; // [rsp+14Ch] [rbp-3Ch] BYREF

v26[0] = 0;
result = scanf("%d", v26);
if ( v26[0] > 0 )
{
v23 = 0;
do
{
scanf("%s %d %d", v25, &rows, &columns);
memset(nums_by_height_count, 0, sizeof(nums_by_height_count));
memset(nums_by_height, 0, (size_t)&unk_7A1200);
memset(map, 0, sizeof(map));
memset(largest_path, 0, sizeof(largest_path));
v9 = 0LL;
for ( i = 0; rows > i; v9 += 100LL )
{
for ( j = 0LL; columns > (int)j; ++j )
{
scanf("%d", &v24);
v4 = v24;
map[j + v9] = v24;
v5 = nums_by_height_count[v4];
v6 = v5 + 10000LL * v4;
nums_by_height[2 * v6] = i;
dword_417904[2 * v6] = j;
nums_by_height_count[v4] = v5 + 1;
}
++i;
}
v22 = nums_by_height_count;
v21 = 0LL;
v18 = 0;
do
{
if ( *v22 > 0 )
{
v10 = 8 * v21;
v20 = 8 * (v21 + (unsigned int)(*v22 - 1) + 1);
do
{
v11 = nums_by_height[v10 / 4];
v12 = dword_417904[v10 / 4];
v13 = (unsigned __int64)v12 << 32;
v19 = v11;
v14 = func7(v13 | v11, v13 | (v11 - 1), 0);
v15 = func7(v13 | v11, v13 | (v11 + 1), v14);
v16 = func7(v13 | v19, v19 | ((unsigned __int64)(v12 + 1) << 32), v15);
v17 = func7(v13 | v19, v19 | ((unsigned __int64)(v12 - 1) << 32), v16);
largest_path[100 * v11 + v12] = v17;
if ( v18 < v17 )
v18 = v17;
v10 += 8LL;
}
while ( v20 != v10 );
}
++v22;
v21 += 10000LL;
}
while ( v21 != 1000000 );
printf("%s: %d\n", v25, (unsigned int)(v18 + 1));
result = ++v23;
}
while ( v26[0] > v23 );
}
return result;
}

original_code:
int main()
{
    int problems = 0;
    scanf("%d", &problems);
    int i;
    for (i = 0; i < problems; i++) {
        char name[255];
        scanf("%s %d %d", name, &rows, &columns);
        memset(nums_by_height_count, 0, sizeof(nums_by_height_count));
        memset(nums_by_height, 0, sizeof(nums_by_height));
        memset(map, 0, sizeof(map));
        memset(largest_path, 0, sizeof(largest_path));
        int j, k;
        for (j = 0; j < rows; j++) {
            for (k = 0; k < columns; k++) {
                int val;
                scanf("%d", &val);
                map[j][k] = val;
                nums_by_height[val][nums_by_height_count[val]].row = j;
                nums_by_height[val][nums_by_height_count[val]].column = k;
                nums_by_height_count[val]++;
            }
        }
        int total_max = 0;
        for (j = 0; j < 100; j++) {
            for (k = 0; k < nums_by_height_count[j]; k++) {
                struct pos p = nums_by_height[j][k];
                int max = 0;
                max = check_spot(p, (struct pos) { p.row - 1, p.column }, max);
                max = check_spot(p, (struct pos) { p.row + 1, p.column }, max);
                max = check_spot(p, (struct pos) { p.row, p.column + 1 }, max);
                max = check_spot(p, (struct pos) { p.row, p.column - 1 }, max);
                largest_path[p.row][p.column] = max;
                if (max > total_max)
                    total_max = max;
            }
        }
        printf("%s: %d\n", name, total_max + 1);
    }
}

canonical_original_code:
int main()
{
    int problems = 0;
    scanf("%d", &problems);
    int i;
    for (i = 0; i < problems; i++) {
        char name[255];
        scanf("%s %d %d", name, &rows, &columns);
        memset(nums_by_height_count, 0, sizeof(nums_by_height_count));
        memset(nums_by_height, 0, sizeof(nums_by_height));
        memset(map, 0, sizeof(map));
        memset(largest_path, 0, sizeof(largest_path));
        int j; int k;
        for (j = 0; j < rows; j++) {
            for (k = 0; k < columns; k++) {
                int val;
                scanf("%d", &val);
                map[j][k] = val;
                nums_by_height[val][nums_by_height_count[val]].row = j;
                nums_by_height[val][nums_by_height_count[val]].column = k;
                nums_by_height_count[val]++;
            }
        }
        int total_max = 0;
        for (j = 0; j < 100; j++) {
            for (k = 0; k < nums_by_height_count[j]; k++) {
                struct pos p = nums_by_height[j][k];
                int max = 0;
                max = check_spot(p, (struct pos) { p.row - 1, p.column }, max);
                max = check_spot(p, (struct pos) { p.row + 1, p.column }, max);
                max = check_spot(p, (struct pos) { p.row, p.column + 1 }, max);
                max = check_spot(p, (struct pos) { p.row, p.column - 1 }, max);
                largest_path[p.row][p.column] = max;
                if (max > total_max)
                    total_max = max;
            }
        }
        printf("%s: %d\n", name, total_max + 1);
    }
}

variable_types: problems int

variable_types: i int

variable_types:
name
char [255]

variable_types: j int

variable_types: k int

variable_types: val int


decompiled_code:
__int64 __fastcall find_pos(int a1, _DWORD *a2, _DWORD *a3)
{
__int64 result; // rax
__int64 v6; // rdx
__int64 v7; // r8
int v8; // esi

result = (unsigned int)box_total;
if ( box_total > 0 )
{
v6 = 0LL;
v7 = 0LL;
while ( 1 )
{
v8 = box_count[v7];
result = 0LL;
if ( v8 > 0 )
break;
LABEL_6:
++v7;
v6 += 25LL;
if ( v7 == box_total )
return result;
}
while ( boxes[result + v6] != a1 )
{
if ( ++result == v8 )
goto LABEL_6;
}
*a2 = v7;
*a3 = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, _DWORD *a2, _DWORD *a3)
{
__int64 result; // rax
__int64 v6; // rdx
__int64 v7; // r8
int v8; // esi

result = (unsigned int)box_total;
if ( box_total > 0 )
{
v6 = 0LL;
v7 = 0LL;
while ( 1 )
{
v8 = box_count[v7];
result = 0LL;
if ( v8 > 0 )
break;
LABEL_6:
++v7;
v6 += 25LL;
if ( v7 == box_total )
return result;
}
while ( boxes[result + v6] != a1 )
{
if ( ++result == v8 )
goto LABEL_6;
}
*a2 = v7;
*a3 = result;
}
return result;
}

original_code:
void find_pos (int num, int *row, int *col) {
    int i, k;
    for (i = 0; i < box_total; i++) {
        for (k = 0; k < box_count[i]; k++) {
            if (boxes[i][k] == num) {
                *row = i;
                *col = k;
                return ;
            }
        }
    }
}

canonical_original_code:
void find_pos(int num, int *row, int *col) {
    int i; int k;
    for (i = 0; i < box_total; i++) {
        for (k = 0; k < box_count[i]; k++) {
            if (boxes[i][k] == num) {
                *row = i;
                *col = k;
                return ;
            }
        }
    }
}

variable_types: num int

variable_types:
row
int *

variable_types:
col
int *


decompiled_code:
__int64 __fastcall add_block(int a1, int a2)
{
int v2; // eax
__int64 result; // rax

v2 = box_count[a1];
boxes[25 * a1 + v2] = a2;
result = (unsigned int)(v2 + 1);
box_count[a1] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, int a2)
{
int v2; // eax
__int64 result; // rax

v2 = box_count[a1];
boxes[25 * a1 + v2] = a2;
result = (unsigned int)(v2 + 1);
box_count[a1] = result;
return result;
}

original_code:
void add_block (int row, int num) {
    boxes[row][box_count[row]] = num;
    box_count[row]++;
}

canonical_original_code:
void add_block(int row, int num) {
    boxes[row][box_count[row]] = num;
    box_count[row]++;
}

variable_types: row int


decompiled_code:
__int64 __fastcall remove_blocks(int a1, int a2)
{
int v2; // edx
__int64 result; // rax
__int64 v4; // rax
int *v5; // rbx
int *v6; // r12

v2 = box_count[a1];
result = (unsigned int)(v2 - 1);
if ( (int)result > a2 )
{
v4 = v2 + 25LL * a1;
v5 = (int *)((char *)&unk_40407C + 4 * v4);
v6 = (int *)((char *)&unk_404078 + 4 * (v4 - (unsigned int)(v2 - a2 - 2)));
do
{
result = add_block(*v5, *v5);
--box_count[a1];
--v5;
}
while ( v5 != v6 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, int a2)
{
int v2; // edx
__int64 result; // rax
__int64 v4; // rax
int *v5; // rbx
int *v6; // r12

v2 = box_count[a1];
result = (unsigned int)(v2 - 1);
if ( (int)result > a2 )
{
v4 = v2 + 25LL * a1;
v5 = (int *)((char *)&unk_40407C + 4 * v4);
v6 = (int *)((char *)&unk_404078 + 4 * (v4 - (unsigned int)(v2 - a2 - 2)));
do
{
result = func8(*v5, *v5);
--box_count[a1];
--v5;
}
while ( v5 != v6 );
}
return result;
}

original_code:
void remove_blocks(int row, int col) {
    int i;
    for (i = box_count[row] - 1; i > col; i--) {
        add_block(boxes[row][i], boxes[row][i]);
        box_count[row]--;
    }
}

variable_types: col int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ecx
int *v4; // rdx
__int64 v5; // rax
int v6; // er13
__int64 v7; // r12
int v8; // ebp
__int64 v9; // rbx
int v10; // eax
__int64 v11; // r12
__int64 i; // rbp
__int64 v13; // rbx
int v15; // [rsp+Ch] [rbp-4Ch] BYREF
int v16; // [rsp+10h] [rbp-48h] BYREF
int v17; // [rsp+14h] [rbp-44h] BYREF
int v18; // [rsp+18h] [rbp-40h] BYREF
int v19; // [rsp+1Ch] [rbp-3Ch]
int v20; // [rsp+20h] [rbp-38h]
char v21; // [rsp+27h] [rbp-31h]
char v22; // [rsp+2Bh] [rbp-2Dh]

__isoc99_scanf();
v3 = box_total;
if ( box_total > 0 )
{
v4 = boxes;
v5 = 0LL;
do
{
box_count[v5] = 1;
*v4 = v5++;
v4 += 25;
}
while ( v5 != v3 );
}
while ( (unsigned int)__isoc99_scanf() != -1 && v22 != 113 )
{
__isoc99_scanf();
find_pos(v19, &v15, &v16);
find_pos(v20, &v17, &v18);
v10 = v19;
if ( v19 != v20 && v15 != v17 )
{
if ( v19 < v20 )
v10 = v20;
if ( box_total >= v10 )
{
if ( v22 != 109 )
{
if ( v21 != 110 )
goto LABEL_7;
LABEL_17:
remove_blocks(v17, v18);
find_pos(v19, &v15, &v16);
find_pos(v20, &v17, &v18);
goto LABEL_18;
}
remove_blocks(v15, v16);
if ( v21 == 110 )
goto LABEL_17;
LABEL_18:
if ( v22 == 109 )
{
find_pos(v20, &v17, &v18);
add_block(v17, boxes[25 * v15 + v16]);
--box_count[v15];
}
else
{
LABEL_7:
v6 = v16;
v7 = v15;
v8 = box_count[v15] - v16;
v9 = v17;
memcpy(&boxes[25 * v17 + box_count[v17]], &boxes[25 * v15 + v16], 4LL * v8);
box_count[v7] = v6;
box_count[v9] += v8;
}
}
}
}
v11 = 0LL;
for ( i = 0LL; box_total > (int)i; v11 += 25LL )
{
printf("%d:", (unsigned int)i);
if ( box_count[i] > 0 )
{
v13 = 0LL;
do
printf(" %d", (unsigned int)boxes[v13++ + v11]);
while ( box_count[i] > (int)v13 );
}
putchar(10);
++i;
}
return 0;
}

original_code:
int main() {
    int i, k;
    char move[5], op[5];
    scanf("%d\n", &box_total);
    for (i = 0; i < box_total; i++) {
        box_count[i] = 1;
        boxes[i][0] = i;
    }
    while (scanf("%s", move) != (-1) && move[0] != 'q' ) {
        int p1r, p1c, p2r, p2c;
        int a, b;
        int box_c;
        scanf("%d %s %d\n", &a, op, &b);
        find_pos(a, &p1r, &p1c);
        find_pos(b, &p2r, &p2c);
        if (a == b)
            continue;
        if (p1r == p2r)
            continue;
        if (a > box_total || b > box_total)
            continue;
        if (move[0] == 'm')
            remove_blocks(p1r, p1c);
        if (op[1] == 'n') {
            remove_blocks(p2r, p2c);
            find_pos(a, &p1r, &p1c);
            find_pos(b, &p2r, &p2c);
        }
        if (move[0] == 'm') {
            find_pos(b, &p2r, &p2c);
            add_block(p2r, boxes[p1r][p1c]);
            box_count[p1r]--;
        } else {
            box_c = box_count[p1r] - p1c;
            memcpy(boxes[p2r] + box_count[p2r], boxes[p1r] + p1c, box_c * sizeof(int));
            box_count[p1r] -= box_c;
            box_count[p2r] += box_c;
        }
    }
    for (i = 0; i < box_total; i++) {
        printf("%d:", i);
        for (k = 0; k < box_count[i]; k++)
            printf(" %d", boxes[i][k]);
        putchar('\n');
    }
    return 0;
}

canonical_original_code:
int main() {
    int i; int k;
    char move[5]; char op[5];
    scanf("%d\n", &box_total);
    for (i = 0; i < box_total; i++) {
        box_count[i] = 1;
        boxes[i][0] = i;
    }
    while (scanf("%s", move) != (-1) && move[0] != 'q' ) {
        int p1r; int p1c; int p2r; int p2c;
        int a; int b;
        int box_c;
        scanf("%d %s %d\n", &a, op, &b);
        find_pos(a, &p1r, &p1c);
        find_pos(b, &p2r, &p2c);
        if (a == b)
            continue;
        if (p1r == p2r)
            continue;
        if (a > box_total || b > box_total)
            continue;
        if (move[0] == 'm')
            remove_blocks(p1r, p1c);
        if (op[1] == 'n') {
            remove_blocks(p2r, p2c);
            find_pos(a, &p1r, &p1c);
            find_pos(b, &p2r, &p2c);
        }
        if (move[0] == 'm') {
            find_pos(b, &p2r, &p2c);
            add_block(p2r, boxes[p1r][p1c]);
            box_count[p1r]--;
        } else {
            box_c = box_count[p1r] - p1c;
            memcpy(boxes[p2r] + box_count[p2r], boxes[p1r] + p1c, box_c * sizeof(int));
            box_count[p1r] -= box_c;
            box_count[p2r] += box_c;
        }
    }
    for (i = 0; i < box_total; i++) {
        printf("%d:", i);
        for (k = 0; k < box_count[i]; k++)
            printf(" %d", boxes[i][k]);
        putchar('\n');
    }
    return 0;
}

variable_types:
move
char [5]

variable_types:
op
char [5]

variable_types: a int

variable_types: b int


decompiled_code:
int __fastcall find_solutions(int a1, int a2)
{
__int64 v2; // rbp
__int64 v3; // r13
__int64 i; // rbx
int result; // eax

v2 = 4LL * a2 + 24;
v3 = 6LL * a2;
for ( i = 1LL; i != 6; ++i )
{
if ( *(int *)((char *)&is_connected + v2) && !*(int *)((char *)arr + v2) )
{
if ( a1 > 7 )
{
result = printf(
"%d%d%d%d%d%d%d%d%d\n",
(unsigned int)cur[0],
(unsigned int)dword_4041A4,
(unsigned int)dword_4041A8,
(unsigned int)dword_4041AC,
(unsigned int)dword_4041B0,
(unsigned int)dword_4041B4,
(unsigned int)dword_4041B8,
(unsigned int)dword_4041BC,
i);
}
else
{
cur[a1] = i;
*(int *)((char *)arr + v2) = 1;
arr[i + v3] = 1;
result = find_solutions((unsigned int)(a1 + 1), (unsigned int)i);
*(int *)((char *)arr + v2) = 0;
arr[i + v3] = 0;
}
}
v2 += 24LL;
}
return result;
}

canonical_decompiled_code:
int __fastcall func7(int a1, int a2)
{
__int64 v2; // rbp
__int64 v3; // r13
__int64 i; // rbx
int result; // eax

v2 = 4LL * a2 + 24;
v3 = 6LL * a2;
for ( i = 1LL; i != 6; ++i )
{
if ( *(int *)((char *)&is_connected + v2) && !*(int *)((char *)arr + v2) )
{
if ( a1 > 7 )
{
result = printf(
"%d%d%d%d%d%d%d%d%d\n",
(unsigned int)cur[0],
(unsigned int)dword_4041A4,
(unsigned int)dword_4041A8,
(unsigned int)dword_4041AC,
(unsigned int)dword_4041B0,
(unsigned int)dword_4041B4,
(unsigned int)dword_4041B8,
(unsigned int)dword_4041BC,
i);
}
else
{
cur[a1] = i;
*(int *)((char *)arr + v2) = 1;
arr[i + v3] = 1;
result = func7((unsigned int)(a1 + 1), (unsigned int)i);
*(int *)((char *)arr + v2) = 0;
arr[i + v3] = 0;
}
}
v2 += 24LL;
}
return result;
}

original_code:
void find_solutions(int deep, int last)
{
    int i;
    for (i = 1; i < 6; i++) {
        if (is_connected[i][last] && !arr[i][last]) {
            if (deep < 8) {
                cur[deep] = i;
                arr[i][last] = 1;
                arr[last][i] = 1;
                find_solutions(deep + 1, i);
                arr[i][last] = 0;
                arr[last][i] = 0;
            } else {
                printf("%d%d%d%d%d%d%d%d%d\n",
                        cur[0], cur[1],
                        cur[2], cur[3],
                        cur[4], cur[5],
                        cur[6], cur[7],
                        i);
            }
        }
    }
}

variable_types: deep int

variable_types: last int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
cur[0] = 1;
find_solutions(1, 1);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
cur[0] = 1;
func7(1, 1);
return 0;
}

original_code:
int main()
{
    cur[0] = 1;
    find_solutions(1, 1);
    return 0;
}


decompiled_code:
void __fastcall push_edges(__int64 a1, int a2)
{
__int64 i; // r8
__int64 v3; // rax
_QWORD *v4; // rcx

for ( i = a1 + 16LL * a2; i != a1; a1 += 16LL )
{
v3 = queueStart;
if ( queueStart )
{
v4 = 0LL;
while ( *(_DWORD *)(a1 + 8) >= *(_DWORD *)(v3 + 8) )
{
v4 = (_QWORD *)v3;
if ( !*(_QWORD *)v3 )
{
*(_QWORD *)v3 = a1;
goto LABEL_10;
}
v3 = *(_QWORD *)v3;
}
if ( v4 )
*v4 = a1;
else
queueStart = a1;
*(_QWORD *)a1 = v3;
}
else
{
queueStart = a1;
}
LABEL_10:
;
}
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, int a2)
{
__int64 i; // r8
__int64 v3; // rax
_QWORD *v4; // rcx

for ( i = a1 + 16LL * a2; i != a1; a1 += 16LL )
{
v3 = queueStart;
if ( queueStart )
{
v4 = 0LL;
while ( *(_DWORD *)(a1 + 8) >= *(_DWORD *)(v3 + 8) )
{
v4 = (_QWORD *)v3;
if ( !*(_QWORD *)v3 )
{
*(_QWORD *)v3 = a1;
goto LABEL_10;
}
v3 = *(_QWORD *)v3;
}
if ( v4 )
*v4 = a1;
else
queueStart = a1;
*(_QWORD *)a1 = v3;
}
else
{
queueStart = a1;
}
LABEL_10:
;
}
}

original_code:
void push_edges(edge *e, int ecount)
{
    edge *j, *p;
    edge *end = e + ecount;
    for (; e != end; e++) {
        for (j = queueStart, p = ((void *)0); j != ((void *)0); p = j, j = j->next) {
            if (e->len < j->len) {
                if (p)
                    p->next = e;
                else
                    queueStart = e;
                e->next = j;
                break;
            }
        }
        if (!j) {
            if (p)
                p->next = e;
            else
                queueStart = e;
        }
    }
}

canonical_original_code:
void push_edges(struct edge *e, int ecount)
{
    struct edge *j; struct edge *p;
    struct edge *end = e + ecount;
    for (; e != end; e++) {
        for (j = queueStart, p = ((void *)0); j != ((void *)0); p = j, j = j->next) {
            if (e->len < j->len) {
                if (p)
                    p->next = e;
                else
                    queueStart = e;
                e->next = j;
                break;
            }
        }
        if (!j) {
            if (p)
                p->next = e;
            else
                queueStart = e;
        }
    }
}

variable_types: struct edge { struct edge *next; int len; int target; } *

variable_types: ecount int


decompiled_code:
__int64 get_smallest_edge()
{
__int64 v0; // rdx
int v1; // ecx

do
{
v0 = queueStart;
queueStart = *(_QWORD *)queueStart;
v1 = *(_DWORD *)(v0 + 12);
}
while ( dword_404A68[434 * v1] );
dword_404A68[434 * v1] = 1;
return v0;
}

canonical_decompiled_code:
__int64 func8()
{
__int64 v0; // rdx
int v1; // ecx

do
{
v0 = queueStart;
queueStart = *(_QWORD *)queueStart;
v1 = *(_DWORD *)(v0 + 12);
}
while ( dword_404A68[434 * v1] );
dword_404A68[434 * v1] = 1;
return v0;
}

original_code:
edge *get_smallest_edge(void)
{
    edge *q;
    while (1) {
        q = queueStart;
        queueStart = queueStart->next;
        if (!ai[q->target].usedFlag) {
            ai[q->target].usedFlag = 1;
            return q;
        }
    }
}

canonical_original_code:
struct edge *get_smallest_edge()
{
    struct edge *q;
    while (1) {
        q = queueStart;
        queueStart = queueStart->next;
        if (!ai[q->target].usedFlag) {
            ai[q->target].usedFlag = 1;
            return q;
        }
    }
}

return_type: struct edge { struct edge *next; int len; int target; } *


decompiled_code:
__int64 find_steps()
{
int v0; // ebx
unsigned int v1; // ebp
int *v2; // rdi
__int64 v3; // rax

dword_404A68[0] = 1;
if ( alienCount <= 1 )
return 0;
v0 = 1;
v1 = 0;
v2 = &ai;
do
{
push_edges((__int64)(v2 + 30), v2[3]);
v3 = get_smallest_edge();
v1 += *(_DWORD *)(v3 + 8);
v2 = (int *)(1736LL * *(int *)(v3 + 12) + 4213344);
++v0;
}
while ( alienCount > v0 );
return v1;
}

original_code:
int find_steps(void)
{
    alien *current = ai;
    edge *e;
    int len = 0, i = 0;
    current->usedFlag = 1;
    for (i = 1; i < alienCount; i++) {
        push_edges(current->edges, current->edgeCount);
        e = get_smallest_edge();
        len += e->len;
        current = ai + e->target;
    }
    return len;
}

canonical_original_code:
int find_steps()
{
    struct alien *current = ai;
    struct edge *e;
    int len = 0; int i = 0;
    current->usedFlag = 1;
    for (i = 1; i < alienCount; i++) {
        push_edges(current->edges, current->edgeCount);
        e = get_smallest_edge();
        len += e->len;
        current = ai + e->target;
    }
    return len;
}

variable_types: struct alien { int r; int c; int usedFlag; int edgeCount; char bitmap[101]; struct edge { struct edge *next; int len; int target; } edges[101]; } *

variable_types: len int

variable_types: i int

user_defined_types: struct alien { int r; int c; int usedFlag; int edgeCount; char bitmap[101]; struct edge edges[101]; }


original_code:
int find_shortest_path(int r1, int c1, int r2, int c2)
{
    int queueSize = 0;
    int flag[50][50] = {{ 0 }};
    rc edgequeue[2501];
    rc *first = edgequeue;
    rc current = { r1, c1, 0 };
    do {
        if (current.r == r2 && current.c == c2)
            return current.len;
        if (grid[current.r + 1][current.c] != '#' && !flag[current.r + 1][current.c]) {
            edgequeue[queueSize++] = (rc) { current.r + 1, current.c, current.len + 1 };
            flag[current.r + 1][current.c] = 1;
        }
        if (grid[current.r - 1][current.c] != '#' && !flag[current.r - 1][current.c]) {
            edgequeue[queueSize++] = (rc) { current.r - 1, current.c, current.len + 1 };
            flag[current.r - 1][current.c] = 1;
        }
        if (grid[current.r][current.c + 1] != '#' && !flag[current.r][current.c + 1]) {
            edgequeue[queueSize++] = (rc) { current.r, current.c + 1, current.len + 1 };
            flag[current.r][current.c + 1] = 1;
        }
        if (grid[current.r][current.c - 1] != '#' && !flag[current.r][current.c - 1]) {
            edgequeue[queueSize++] = (rc) { current.r, current.c - 1, current.len + 1 };
            flag[current.r][current.c - 1] = 1;
        }
        current = *first;
        first++;
    } while ((first - edgequeue) <= queueSize);
    return 0;
}

canonical_original_code:
int find_shortest_path(int r1, int c1, int r2, int c2)
{
    int queueSize = 0;
    int flag[50][50] = {{ 0 }};
    struct rc edgequeue[2501];
    struct rc *first = edgequeue;
    struct rc current = { r1, c1, 0 };
    do {
        if (current.r == r2 && current.c == c2)
            return current.len;
        if (grid[current.r + 1][current.c] != '#' && !flag[current.r + 1][current.c]) {
            edgequeue[queueSize++] = (struct rc) { current.r + 1, current.c, current.len + 1 };
            flag[current.r + 1][current.c] = 1;
        }
        if (grid[current.r - 1][current.c] != '#' && !flag[current.r - 1][current.c]) {
            edgequeue[queueSize++] = (struct rc) { current.r - 1, current.c, current.len + 1 };
            flag[current.r - 1][current.c] = 1;
        }
        if (grid[current.r][current.c + 1] != '#' && !flag[current.r][current.c + 1]) {
            edgequeue[queueSize++] = (struct rc) { current.r, current.c + 1, current.len + 1 };
            flag[current.r][current.c + 1] = 1;
        }
        if (grid[current.r][current.c - 1] != '#' && !flag[current.r][current.c - 1]) {
            edgequeue[queueSize++] = (struct rc) { current.r, current.c - 1, current.len + 1 };
            flag[current.r][current.c - 1] = 1;
        }
        current = *first;
        first++;
    } while ((first - edgequeue) <= queueSize);
    return 0;
}

variable_types: queueSize int

variable_types: struct rc { int r; int c; int len; } [2501]

variable_types:
first
struct rc { int r; int c; int len; } *

variable_types:
current
struct rc { int r; int c; int len; }


original_code:
int main()
{
    char buf[52];
    int cases, n, r, c, a, a2;
    int nrows, ncols, result;
    scanf("%d\n", &cases);
    for (n = 0; n < cases; n++) {
        alienCount = 0;
        queueStart = ((void *)0);
        memset(grid, 0, 50 * 50);
        scanf("%d %d\n", &ncols, &nrows);
        for (r = 0; r < nrows; r++) {
            memset(buf, 0, 50);
            fgets(buf, 52, stdin);
            memcpy(grid[r], buf, 50);
        }
        for (r = 0; r < nrows; r++) {
            for (c = 0; c < ncols; c++) {
                if (grid[r][c] == 'S' || grid[r][c] == 'A') {
                    memset(ai + alienCount, 0, sizeof *ai);
                    ai[alienCount].r = r;
                    ai[alienCount].c = c;
                    ai[alienCount].edgeCount = 0;
                    alienCount++;
                }
            }
        }
        for (a = 0; a < alienCount; a++) {
            for (a2 = 0; a2 < alienCount; a2++) {
                if (a == a2 || ai[a].bitmap[a2])
                    continue;
                int e = ai[a].edgeCount;
                int len = find_shortest_path(ai[a].r, ai[a].c, ai[a2].r, ai[a2].c);
                ai[a].edges[e].target = a2;
                ai[a].edges[e].len = len;
                ai[a].edgeCount++;
                e = ai[a2].edgeCount;
                ai[a2].edges[e].target = a;
                ai[a2].edges[e].len = len;
                ai[a2].edgeCount++;
                ai[a].bitmap[a2] = 1;
                ai[a2].bitmap[a] = 1;
            }
        }
        result = find_steps();
        printf("%d\n", result);
    }
    return 0;
}

canonical_original_code:
int main()
{
    char buf[52];
    int cases; int n; int r; int c; int a; int a2;
    int nrows; int ncols; int result;
    scanf("%d\n", &cases);
    for (n = 0; n < cases; n++) {
        alienCount = 0;
        queueStart = ((void *)0);
        memset(grid, 0, 50 * 50);
        scanf("%d %d\n", &ncols, &nrows);
        for (r = 0; r < nrows; r++) {
            memset(buf, 0, 50);
            fgets(buf, 52, stdin);
            memcpy(grid[r], buf, 50);
        }
        for (r = 0; r < nrows; r++) {
            for (c = 0; c < ncols; c++) {
                if (grid[r][c] == 'S' || grid[r][c] == 'A') {
                    memset(ai + alienCount, 0, sizeof *ai);
                    ai[alienCount].r = r;
                    ai[alienCount].c = c;
                    ai[alienCount].edgeCount = 0;
                    alienCount++;
                }
            }
        }
        for (a = 0; a < alienCount; a++) {
            for (a2 = 0; a2 < alienCount; a2++) {
                if (a == a2 || ai[a].bitmap[a2])
                    continue;
                int e = ai[a].edgeCount;
                int len = find_shortest_path(ai[a].r, ai[a].c, ai[a2].r, ai[a2].c);
                ai[a].edges[e].target = a2;
                ai[a].edges[e].len = len;
                ai[a].edgeCount++;
                e = ai[a2].edgeCount;
                ai[a2].edges[e].target = a;
                ai[a2].edges[e].len = len;
                ai[a2].edgeCount++;
                ai[a].bitmap[a2] = 1;
                ai[a2].bitmap[a] = 1;
            }
        }
        result = find_steps();
        printf("%d\n", result);
    }
    return 0;
}

variable_types:
buf
char [52]

variable_types: cases int

variable_types: n int

variable_types: r int

variable_types: c int

variable_types: nrows int

variable_types: ncols int

variable_types: result int

variable_types: e int


decompiled_code:
__int64 __fastcall get_pcap_netmask(__int64 a1, _DWORD *a2, _DWORD *a3)
{
__int64 result; // rax

result = pcap_lookupnet();
if ( (_DWORD)result == -1 )
{
result = print_err("Can't get netmask for device");
*a2 = 0;
*a3 = 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, _DWORD *a2, _DWORD *a3)
{
__int64 result; // rax

result = pcap_lookupnet();
if ( (_DWORD)result == -1 )
{
result = func16("Can't get netmask for device");
*a2 = 0;
*a3 = 0;
}
return result;
}

original_code:
void get_pcap_netmask(char* device, bpf_u_int32* net, bpf_u_int32* mask) {
  char err[256];
  if (pcap_lookupnet(device, net, mask, err) == -1) {
    print_err("Can't get netmask for device");
    *net = 0;
    *mask = 0;
  }
}

canonical_original_code:
void get_pcap_netmask(char *device, unsigned int *net, unsigned int *mask) {
  char err[256];
  if (pcap_lookupnet(device, net, mask, err) == -1) {
    print_err("Can't get netmask for device");
    *net = 0;
    *mask = 0;
  }
}

variable_types:
device
char *

variable_types:
net
unsigned int *

variable_types:
mask
unsigned int *

variable_types:
err
char [256]


decompiled_code:
__int64 select_pcap_dev()
{
return pcap_lookupdev();
}

canonical_decompiled_code:
__int64 func9()
{
return pcap_lookupdev();
}

original_code:
char* select_pcap_dev(void) {
  char err[256];
  return pcap_lookupdev(err);
}

canonical_original_code:
char *select_pcap_dev() {
  char err[256];
  return pcap_lookupdev(err);
}

return_type: char *


original_code:
void print_pkt(const struct ethernet_hdr* eth, const struct ip_hdr* ip,
               u_int ip_hdr_sz, const void* packet) {
  printf("*********************CAPTURED PACKET*************************\n");
  printf("********************ETHERNET HEADER DATA*************************\n");
  printf("SRC ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n",
         eth->src_addr[0], eth->src_addr[1],
         eth->src_addr[2], eth->src_addr[3],
         eth->src_addr[4], eth->src_addr[5]);
  printf("DEST ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n",
         eth->dest_addr[0], eth->dest_addr[1],
         eth->dest_addr[2], eth->dest_addr[3],
         eth->dest_addr[4], eth->dest_addr[5]);
  printf("PROTOCOL TYPE: Internet Protocol\n");
  printf("*********************IP HEADER DATA****************************\n");
  printf("VERSION: %02d\n", (((ip)->ver_hdrlen) >> 4));
  printf("TYPE OF SERVICE: %d\n", ip->tos);
  printf("TOTAL LENGTH: %d\n", ip->len);
  printf("IDENTIFICATION: %d\n", ip->id);
  printf("REMAINING TTL: %d\n", ip->ttl);
  printf("SOURCE ADDRESS: %s\n", inet_ntoa(ip->src_ip));
  printf("DEST ADDRESS: %s\n", inet_ntoa(ip->dest_ip));
  switch(ip->prot) {
  case 6:
    {
      u_int tcp_hdr_sz;
      char* pkt_data;
      const struct tcp_hdr* tcp = (struct tcp_hdr*)(packet +
                                                    14 + ip_hdr_sz);
      tcp_hdr_sz = (((tcp)->off & 0xf0) >> 4)*4;
      if (tcp_hdr_sz < 20) {
        return;
      }
      pkt_data = (char*)(packet + 14 + ip_hdr_sz + tcp_hdr_sz);
      printf("*********************TCP HEADER DATA***************************\n");
      printf("SOURCE PORT: %d\n", tcp->src_prt);
      printf("DEST PORT: %d\n", tcp->dest_prt);
      printf("SEQUENCE #: %d\n", ntohl(tcp->seq));
      printf("ACK #: %d\n", ntohl(tcp->ack));
      printf("OFFSET: %d\n", (((tcp)->off & 0xf0) >> 4));
      printf("FLAGS: %d\n", tcp->flags);
      printf("WINDOW POSITION: %d\n",tcp->win);
      if(ip->len - tcp_hdr_sz - ip_hdr_sz > 0) {
        printf("*******************PACKET_PAYLOAD DATA*************************\n");
        printf("%s\n", pkt_data);
      }
    }
    break;
  case 17:
    {
      const struct udp_hdr* udp_header =
        (struct udp_hdr*)(packet + 14 + ip_hdr_sz);
      char* pkt_data = (char*)(packet + 14 + ip_hdr_sz + 8);
      printf("*********************UDP HEADER DATA***************************\n");
      printf("SOURCE PORT: %d\n", udp_header->src_prt);
      printf("DEST PORT: %d\n", udp_header->dest_prt);
      if(ip->len - 8 - ip_hdr_sz > 0) {
        printf("*******************PACKET_PAYLOAD DATA*************************\n");
        printf("%s\n", pkt_data);
      }
    }
    break;
  default:
    printf("Unsupported Packet Type.\n");
    break;
  }
  printf("\n\n");
}

canonical_original_code:
void print_pkt(struct ethernet_hdr *eth, struct ip_hdr *ip, unsigned int ip_hdr_sz, void *packet) {
  printf("*********************CAPTURED PACKET*************************\n");
  printf("********************ETHERNET HEADER DATA*************************\n");
  printf("SRC ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n",
         eth->src_addr[0], eth->src_addr[1],
         eth->src_addr[2], eth->src_addr[3],
         eth->src_addr[4], eth->src_addr[5]);
  printf("DEST ADDRESS: %02x:%02x:%02x:%02x:%02x:%02x\n",
         eth->dest_addr[0], eth->dest_addr[1],
         eth->dest_addr[2], eth->dest_addr[3],
         eth->dest_addr[4], eth->dest_addr[5]);
  printf("PROTOCOL TYPE: Internet Protocol\n");
  printf("*********************IP HEADER DATA****************************\n");
  printf("VERSION: %02d\n", (((ip)->ver_hdrlen) >> 4));
  printf("TYPE OF SERVICE: %d\n", ip->tos);
  printf("TOTAL LENGTH: %d\n", ip->len);
  printf("IDENTIFICATION: %d\n", ip->id);
  printf("REMAINING TTL: %d\n", ip->ttl);
  printf("SOURCE ADDRESS: %s\n", inet_ntoa(ip->src_ip));
  printf("DEST ADDRESS: %s\n", inet_ntoa(ip->dest_ip));
  switch(ip->prot) {
  case 6:
    {
      unsigned int tcp_hdr_sz;
      char *pkt_data;
      struct tcp_hdr *tcp = (struct tcp_hdr*)(packet +
                                                    14 + ip_hdr_sz);
      tcp_hdr_sz = (((tcp)->off & 0xf0) >> 4)*4;
      if (tcp_hdr_sz < 20) {
        return;
      }
      pkt_data = (char *)(packet + 14 + ip_hdr_sz + tcp_hdr_sz);
      printf("*********************TCP HEADER DATA***************************\n");
      printf("SOURCE PORT: %d\n", tcp->src_prt);
      printf("DEST PORT: %d\n", tcp->dest_prt);
      printf("SEQUENCE #: %d\n", ntohl(tcp->seq));
      printf("ACK #: %d\n", ntohl(tcp->ack));
      printf("OFFSET: %d\n", (((tcp)->off & 0xf0) >> 4));
      printf("FLAGS: %d\n", tcp->flags);
      printf("WINDOW POSITION: %d\n",tcp->win);
      if(ip->len - tcp_hdr_sz - ip_hdr_sz > 0) {
        printf("*******************PACKET_PAYLOAD DATA*************************\n");
        printf("%s\n", pkt_data);
      }
    }
    break;
  case 17:
    {
      struct udp_hdr *udp_header = (struct udp_hdr*)(packet + 14 + ip_hdr_sz);
      char *pkt_data = (char*)(packet + 14 + ip_hdr_sz + 8);
      printf("*********************UDP HEADER DATA***************************\n");
      printf("SOURCE PORT: %d\n", udp_header->src_prt);
      printf("DEST PORT: %d\n", udp_header->dest_prt);
      if(ip->len - 8 - ip_hdr_sz > 0) {
        printf("*******************PACKET_PAYLOAD DATA*************************\n");
        printf("%s\n", pkt_data);
      }
    }
    break;
  default:
    printf("Unsupported Packet Type.\n");
    break;
  }
  printf("\n\n");
}

variable_types: struct ethernet_hdr { unsigned char dest_addr[6]; unsigned char src_addr[6]; unsigned short ether_type; } *

variable_types: struct ip_hdr { unsigned char ver_hdrlen; unsigned char tos; unsigned short len; unsigned short id; unsigned short off; unsigned char ttl; unsigned char prot; unsigned short chksum; struct in_addr { unsigned int s_addr; } src_ip; } *

variable_types:
packet
void *

variable_types: struct tcp_hdr { unsigned short src_prt; unsigned short dest_prt; unsigned int seq; unsigned int ack; unsigned char off; unsigned char flags; unsigned short win; unsigned short chksum; unsigned short urg; } *

variable_types: struct udp_hdr { unsigned short src_prt; unsigned short dest_prt; unsigned short length; unsigned short chksum; } *

user_defined_types: struct ip_hdr { unsigned char ver_hdrlen; unsigned char tos; unsigned short len; unsigned short id; unsigned short off; unsigned char ttl; unsigned char prot; unsigned short chksum; struct in_addr src_ip; }


decompiled_code:
int __fastcall handle_pcap_pkt(__int64 a1, __int64 a2, unsigned __int8 *a3)
{
unsigned int v4; // edx
int result; // eax

v4 = 4 * (a3[14] & 0xF);
if ( v4 > 0x13 )
result = print_pkt(a3, (__int64)(a3 + 14), v4, (__int64)&a3[v4 + 14]);
return result;
}

canonical_decompiled_code:
int __fastcall func13(__int64 a1, __int64 a2, unsigned __int8 *a3)
{
unsigned int v4; // edx
int result; // eax

v4 = 4 * (a3[14] & 0xF);
if ( v4 > 0x13 )
result = func12(a3, (__int64)(a3 + 14), v4, (__int64)&a3[v4 + 14]);
return result;
}

original_code:
void handle_pcap_pkt(u_char* args, const struct pcap_pkthdr* header,
                     const u_char* packet) {
  const struct ethernet_hdr* eth_header;
  const struct ip_hdr* ip_header;
  u_int ip_hdr_sz;
  eth_header = (struct ethernet_hdr*)(packet);
  ip_header = (struct ip_hdr*)(packet + 14);
  ip_hdr_sz = (((ip_header)->ver_hdrlen) & 0x0f)*4;
  if (ip_hdr_sz < 20) {
    return;
  }
  print_pkt(eth_header, ip_header, ip_hdr_sz, ((void*)packet+14 +ip_hdr_sz));
}

canonical_original_code:
void handle_pcap_pkt(unsigned char *args, struct pcap_pkthdr *header, unsigned char *packet) {
  struct ethernet_hdr *eth_header;
  struct ip_hdr *ip_header;
  unsigned int ip_hdr_sz;
  eth_header = (struct ethernet_hdr *)(packet);
  ip_header = (struct ip_hdr *)(packet + 14);
  ip_hdr_sz = (((ip_header)->ver_hdrlen) & 0x0f)*4;
  if (ip_hdr_sz < 20) {
    return;
  }
  print_pkt(eth_header, ip_header, ip_hdr_sz, ((void *)packet+14 +ip_hdr_sz));
}

variable_types:
args
unsigned char *

variable_types: struct pcap_pkthdr { struct timeval { long tv_sec; long tv_usec; } ts; unsigned int caplen; unsigned int len; } *

variable_types:
packet
unsigned char *

user_defined_types: struct pcap_pkthdr { struct timeval ts; unsigned int caplen; unsigned int len; }

user_defined_types: struct ethernet_hdr { unsigned char dest_addr[6]; unsigned char src_addr[6]; unsigned short ether_type; }


original_code:
void format_data(char* data, u_short len) {
  register unsigned short i;
  for(i = 0; i < len; ++i) {
    if(!((*__ctype_b_loc ())[(int) ((data[i]))] & (unsigned short int) _ISprint)) {
      data[i] = '.';
    }
  }
}

canonical_original_code:
void format_data(char *data, unsigned short len) {
  register unsigned short i;
  for(i = 0; i < len; ++i) {
    if(!((*__ctype_b_loc ())[(int) ((data[i]))] & (unsigned short) _ISprint)) {
      data[i] = '.';
    }
  }
}

variable_types:
data
char *

variable_types:
len
unsigned short

variable_types:
i
unsigned short


decompiled_code:
void __fastcall __noreturn fatal_err(int status, const char *a2)
{
fprintf(stderr, "%s\n", a2);
exit(status);
}

canonical_decompiled_code:
void __fastcall __noreturn func15(int status, const char *a2)
{
fprintf(stderr, "%s\n", a2);
exit(status);
}

original_code:
void fatal_err(int error_code, char* error_msg) {
  fprintf(stderr, "%s\n", error_msg);
  exit(error_code);
}

canonical_original_code:
void fatal_err(int error_code, char *error_msg) {
  fprintf(stderr, "%s\n", error_msg);
  exit(error_code);
}


decompiled_code:
int __fastcall print_err(const char *a1)
{
return fprintf(stderr, "%s\n", a1);
}

canonical_decompiled_code:
int __fastcall func16(const char *a1)
{
return fprintf(stderr, "%s\n", a1);
}

original_code:
void print_err(char* error_msg) {
  fprintf(stderr, "%s\n", error_msg);
}

canonical_original_code:
void print_err(char *error_msg) {
  fprintf(stderr, "%s\n", error_msg);
}


decompiled_code:
int __fastcall print_pcap_err(const char *a1, const char *a2)
{
return fprintf(stderr, "%s %s\n", a1, a2);
}

canonical_decompiled_code:
int __fastcall func17(const char *a1, const char *a2)
{
return fprintf(stderr, "%s %s\n", a1, a2);
}

original_code:
void print_pcap_err(char* msg, char* pcap_msg) {
  fprintf(stderr, "%s %s\n", msg, pcap_msg);
}

canonical_original_code:
void print_pcap_err(char *msg, char *pcap_msg) {
  fprintf(stderr, "%s %s\n", msg, pcap_msg);
}

variable_types:
msg
char *


HexToInt

repo: DX-MON/libHTTP

decompiled_code:
__int64 __fastcall HexToInt(const char *a1)
{
unsigned int v1; // eax
unsigned int v2; // ebx
const char *v3; // rbp
__int64 v4; // r12
int v5; // eax
int v6; // edx

v1 = strlen(a1);
v2 = v1;
if ( v1 )
{
v3 = a1;
v4 = (__int64)&a1[v1];
v2 = 0;
do
{
v5 = toupper(*v3);
v6 = v5 - 48;
if ( v5 - 48 > 9 )
{
v6 = v5 - 55;
if ( (unsigned int)(v5 - 65) > 6 )
break;
}
v2 = v6 + 16 * v2;
++v3;
}
while ( v3 != (const char *)v4 );
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
unsigned int v1; // eax
unsigned int v2; // ebx
const char *v3; // rbp
__int64 v4; // r12
int v5; // eax
int v6; // edx

v1 = strlen(a1);
v2 = v1;
if ( v1 )
{
v3 = a1;
v4 = (__int64)&a1[v1];
v2 = 0;
do
{
v5 = toupper(*v3);
v6 = v5 - 48;
if ( v5 - 48 > 9 )
{
v6 = v5 - 55;
if ( (unsigned int)(v5 - 65) > 6 )
break;
}
v2 = v6 + 16 * v2;
++v3;
}
while ( v3 != (const char *)v4 );
}
return v2;
}

original_code:
uint32_t HexToInt(char *Hex)
{
 uint32_t i, len = strlen(Hex), ret = 0;
 for (i = 0; i < len; i++)
 {
  int t = toupper(Hex[i]) - 0x30;
  if (t >= 10)
  {
   t -= 0x07;
   if (t > 16 || t < 10)
    return ret;
  }
  ret *= 16;
  ret += t;
 }
 return ret;
}

canonical_original_code:
unsigned int HexToInt(char *Hex)
{
 unsigned int i; unsigned int len = strlen(Hex); unsigned int ret = 0;
 for (i = 0; i < len; i++)
 {
  int t = toupper(Hex[i]) - 0x30;
  if (t >= 10)
  {
   t -= 0x07;
   if (t > 16 || t < 10)
    return ret;
  }
  ret *= 16;
  ret += t;
 }
 return ret;
}

variable_types:
Hex
char *

variable_types:
i
unsigned int

variable_types:
len
unsigned int

variable_types:
ret
unsigned int

variable_types: t int

return_type: unsigned int


freeChunks

decompiled_code:
void __fastcall freeChunks(void *ptr)
{
unsigned int v1; // ebx

if ( *(_DWORD *)ptr )
{
v1 = 0;
do
free(*(void **)(*((_QWORD *)ptr + 2) + 8LL * v1++));
while ( *(_DWORD *)ptr > v1 );
}
free(*((void **)ptr + 2));
free(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
unsigned int v1; // ebx

if ( *(_DWORD *)ptr )
{
v1 = 0;
do
free(*(void **)(*((_QWORD *)ptr + 2) + 8LL * v1++));
while ( *(_DWORD *)ptr > v1 );
}
free(*((void **)ptr + 2));
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void freeChunks(ChunkedInternals *chunks)
{
 uint32_t i;
 for (i = 0; i < chunks->nChunks; i++)
  free(chunks->Chunks[i]);
 free(chunks->Chunks);
 free(chunks->lenChunks);
 free(chunks);
}

canonical_original_code:
void freeChunks(struct _ChunkedInternals *chunks)
{
 unsigned int i;
 for (i = 0; i < chunks->nChunks; i++)
  free(chunks->Chunks[i]);
 free(chunks->Chunks);
 free(chunks->lenChunks);
 free(chunks);
}

variable_types: struct _ChunkedInternals { unsigned int nChunks; unsigned int *lenChunks; char **Chunks; } *


HeaderResponseProtoError

decompiled_code:
__int64 __fastcall HeaderResponseProtoError(void *a1)
{
freeChunks(a1);
httpError = -10LL;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *a1)
{
func1(a1);
httpError = -10LL;
return 0LL;
}

original_code:
uint8_t HeaderResponseProtoError(ChunkedInternals *chunks)
{
 freeChunks(chunks);
 httpError = -10;
 return 0;
}

canonical_original_code:
unsigned char HeaderResponseProtoError(struct _ChunkedInternals *chunks)
{
 freeChunks(chunks);
 httpError = -10;
 return 0;
}

return_type: unsigned char

user_defined_types: struct _ChunkedInternals { unsigned int nChunks; unsigned int *lenChunks; char **Chunks; }


tryRecvContent

decompiled_code:
__int64 __fastcall tryRecvContent(__int64 a1, __int64 a2)
{
int v2; // edx
int v3; // ebx
__int64 result; // rax
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( *(_BYTE *)(a1 + 64) == 1 )
return tryRecvChunkedContent(a1, a2);
v2 = *(_DWORD *)(a1 + 48);
v3 = 0;
if ( v2 <= 0 )
{
LABEL_5:
*(_DWORD *)(a1 + 48) = v2 + 1;
result = 1LL;
}
else
{
while ( 1 )
{
result = tryRecv(*(unsigned int *)(a2 + 24), *(_QWORD *)(a1 + 40) + v3, (unsigned int)(v2 - v3), v5);
if ( !(_BYTE)result )
break;
v3 += v5[0];
v2 = *(_DWORD *)(a1 + 48);
if ( v2 <= v3 )
goto LABEL_5;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
int v2; // edx
int v3; // ebx
__int64 result; // rax
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

if ( *(_BYTE *)(a1 + 64) == 1 )
return func3(a1, a2);
v2 = *(_DWORD *)(a1 + 48);
v3 = 0;
if ( v2 <= 0 )
{
LABEL_5:
*(_DWORD *)(a1 + 48) = v2 + 1;
result = 1LL;
}
else
{
while ( 1 )
{
result = tryRecv(*(unsigned int *)(a2 + 24), *(_QWORD *)(a1 + 40) + v3, (unsigned int)(v2 - v3), v5);
if ( !(_BYTE)result )
break;
v3 += v5[0];
v2 = *(_DWORD *)(a1 + 48);
if ( v2 <= v3 )
goto LABEL_5;
}
}
return result;
}

original_code:
uint8_t tryRecvContent(Response *resp, Address *URL)
{
 if (resp->TransferEncodingFound == 1)
  return tryRecvChunkedContent(resp, URL);
 else
 {
  int i;
  uint32_t read;
  for (i = 0; i < resp->ContentLen; )
  {
   if (!tryRecv(URL->Socket, resp->Content + i, resp->ContentLen - i, &read))
    return 0;
   i += read;
  }
  resp->ContentLen++;
 }
 return 1;
}

canonical_original_code:
unsigned char tryRecvContent(struct _Response *resp, struct _Address *URL)
{
 if (resp->TransferEncodingFound == 1)
  return tryRecvChunkedContent(resp, URL);
 else
 {
  int i;
  unsigned int read;
  for (i = 0; i < resp->ContentLen; )
  {
   if (!tryRecv(URL->Socket, resp->Content + i, resp->ContentLen - i, &read))
    return 0;
   i += read;
  }
  resp->ContentLen++;
 }
 return 1;
}

variable_types: struct _Response { int HTTPMajorVer; char *ResponceText; char **Headers; int nHeaders; char *Content; int ContentLen; void *Private; unsigned char TransferEncodingFound; } *

variable_types: struct _Address { char *ServerName; int ServerIP; char *ItemLocation; int Socket; unsigned char Secure; char *Protocol; struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *Server; short Port; int method; } *

variable_types: i int

variable_types:
read
unsigned int

user_defined_types: struct _Address { char *ServerName; int ServerIP; char *ItemLocation; int Socket; unsigned char Secure; char *Protocol; struct hostent *Server; short Port; int method; }


httpContentThread

decompiled_code:
void *__fastcall httpContentThread(void *a1)
{
__int64 v1; // rbp
char v2; // bl
void *result; // rax

v1 = *((_QWORD *)a1 + 7);
pthread_mutex_lock((pthread_mutex_t *)v1);
v2 = tryRecvContent((__int64)a1, *(_QWORD *)(v1 + 48));
pthread_mutex_unlock((pthread_mutex_t *)v1);
result = 0LL;
if ( !v2 )
result = (void *)httpError;
return result;
}

canonical_decompiled_code:
void *__fastcall func5(void *a1)
{
__int64 v1; // rbp
char v2; // bl
void *result; // rax

v1 = *((_QWORD *)a1 + 7);
pthread_mutex_lock((pthread_mutex_t *)v1);
v2 = func4((__int64)a1, *(_QWORD *)(v1 + 48));
pthread_mutex_unlock((pthread_mutex_t *)v1);
result = 0LL;
if ( !v2 )
result = (void *)httpError;
return result;
}

original_code:
void *httpContentThread(void *p_resp)
{
 uint8_t res;
 Response *resp = (Response *)p_resp;
 ContentInternals *Intern = (ContentInternals *)resp->Private;
 pthread_mutex_lock(&Intern->mutex);
 res = tryRecvContent(resp,Intern->URL);
 pthread_mutex_unlock(&Intern->mutex);
 if (res)
  return 0;
 else
  return (void *)httpError;
}

canonical_original_code:
void *httpContentThread(void *p_resp)
{
 unsigned char res;
 struct _Response *resp = (Response *)p_resp;
 struct _ContentInternals *Intern = (ContentInternals *)resp->Private;
 pthread_mutex_lock(&Intern->mutex);
 res = tryRecvContent(resp,Intern->URL);
 pthread_mutex_unlock(&Intern->mutex);
 if (res)
  return 0;
 else
  return (void *)httpError;
}

variable_types:
res
unsigned char

variable_types: struct _ContentInternals { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; unsigned long thread; struct _Address { char *ServerName; int ServerIP; char *ItemLocation; int Socket; unsigned char Secure; char *Protocol; struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *Server; short Port; int method; } *URL; } *

return_type: void *

user_defined_types: struct _Response { int HTTPMajorVer; char *ResponceText; char **Headers; int nHeaders; char *Content; int ContentLen; void *Private; unsigned char TransferEncodingFound; }

user_defined_types: struct _ContentInternals { union pthread_mutex_t mutex; unsigned long thread; struct _Address *URL; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


httpGetContentAsync

decompiled_code:
void __fastcall httpGetContentAsync(__int64 a1, _QWORD *a2)
{
void *v3; // rdi
pthread_mutex_t *v4; // rbx
pthread_attr_t v5; // [rsp-3Ch] [rbp-58h] BYREF
pthread_mutexattr_t attr; // [rsp+0h] [rbp-1Ch] BYREF

if ( Initialised )
{
if ( a1 )
{
if ( a2 )
{
v3 = (void *)a2[7];
if ( v3 )
free(v3);
v4 = (pthread_mutex_t *)malloc(0x38uLL);
a2[7] = v4;
v4->__align = 0LL;
*(&v4->__align + 1) = 0LL;
*(&v4->__align + 2) = 0LL;
v4->__list.__prev = 0LL;
v4->__list.__next = 0LL;
v4[1].__align = 0LL;
*(&v4[1].__align + 1) = a1;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, 2);
pthread_mutex_init(v4, &attr);
pthread_mutexattr_destroy(&attr);
pthread_attr_init(&v5);
pthread_attr_setdetachstate(&v5, 1);
pthread_attr_setscope(&v5, 1);
pthread_create((pthread_t *)&v4[1], &v5, httpContentThread, a2);
pthread_attr_destroy(&v5);
sleep(0);
}
else
{
httpError = -11LL;
}
}
else
{
httpError = -6LL;
}
}
else
{
httpError = -2LL;
}
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, _QWORD *a2)
{
void *v3; // rdi
pthread_mutex_t *v4; // rbx
pthread_attr_t v5; // [rsp-3Ch] [rbp-58h] BYREF
pthread_mutexattr_t attr; // [rsp+0h] [rbp-1Ch] BYREF

if ( Initialised )
{
if ( a1 )
{
if ( a2 )
{
v3 = (void *)a2[7];
if ( v3 )
free(v3);
v4 = (pthread_mutex_t *)malloc(0x38uLL);
a2[7] = v4;
v4->__align = 0LL;
*(&v4->__align + 1) = 0LL;
*(&v4->__align + 2) = 0LL;
v4->__list.__prev = 0LL;
v4->__list.__next = 0LL;
v4[1].__align = 0LL;
*(&v4[1].__align + 1) = a1;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, 2);
pthread_mutex_init(v4, &attr);
pthread_mutexattr_destroy(&attr);
pthread_attr_init(&v5);
pthread_attr_setdetachstate(&v5, 1);
pthread_attr_setscope(&v5, 1);
pthread_create((pthread_t *)&v4[1], &v5, httpContentThread, a2);
pthread_attr_destroy(&v5);
sleep(0);
}
else
{
httpError = -11LL;
}
}
else
{
httpError = -6LL;
}
}
else
{
httpError = -2LL;
}
}

original_code:
void httpGetContentAsync(Address *URL, Response *resp)
{
 pthread_mutexattr_t mutex_attr;
 pthread_attr_t thread_attr;
 ContentInternals *Intern;
 if (Initialised == 0)
 {
  httpError = -2;
  return;
 }
 if (URL == ((void *)0))
 {
  httpError = -6;
  return;
 }
 if (resp == ((void *)0))
 {
  httpError = -11;
  return;
 }
 if (resp->Private != ((void *)0))
  free(resp->Private);
 resp->Private = malloc(sizeof(ContentInternals));
 memset(resp->Private, 0x00, sizeof(ContentInternals));
 Intern = (ContentInternals *)resp->Private;
 Intern->URL = URL;
 pthread_mutexattr_init(&mutex_attr);
 pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
 pthread_mutex_init(&Intern->mutex, &mutex_attr);
 pthread_mutexattr_destroy(&mutex_attr);
 pthread_attr_init(&thread_attr);
 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
 pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_PROCESS);
 pthread_create(&Intern->thread, &thread_attr, httpContentThread, (void *)resp);
 pthread_attr_destroy(&thread_attr);
 sleep(0);
}

canonical_original_code:
void httpGetContentAsync(struct _Address *URL, struct _Response *resp)
{
 union pthread_mutexattr_t mutex_attr;
 union pthread_attr_t thread_attr;
 struct _ContentInternals *Intern;
 if (Initialised == 0)
 {
  httpError = -2;
  return;
 }
 if (URL == ((void *)0))
 {
  httpError = -6;
  return;
 }
 if (resp == ((void *)0))
 {
  httpError = -11;
  return;
 }
 if (resp->Private != ((void *)0))
  free(resp->Private);
 resp->Private = malloc(sizeof(ContentInternals));
 memset(resp->Private, 0x00, sizeof(ContentInternals));
 Intern = (struct _ContentInternals *)resp->Private;
 Intern->URL = URL;
 pthread_mutexattr_init(&mutex_attr);
 pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
 pthread_mutex_init(&Intern->mutex, &mutex_attr);
 pthread_mutexattr_destroy(&mutex_attr);
 pthread_attr_init(&thread_attr);
 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
 pthread_attr_setscope(&thread_attr, PTHREAD_SCOPE_PROCESS);
 pthread_create(&Intern->thread, &thread_attr, httpContentThread, (void *)resp);
 pthread_attr_destroy(&thread_attr);
 sleep(0);
}

variable_types: union pthread_mutexattr_t { char __size[4]; int __align; }

variable_types: union pthread_attr_t { char __size[56]; long __align; }

user_defined_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }