decompiled_code:
__int64 lookup_bfd_init()
{
size_t v0; // rax
char *v1; // rbp
__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; // rbp
__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:
__int64 __fastcall store_write(unsigned int a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
unsigned __int64 v4; // rcx
__int64 i; // r8

result = a1;
v4 = a3 + a1;
for ( i = a1; a1 < v4; result = a1 )
{
*((_BYTE *)&store + result) &= *(_BYTE *)(a2 + result - i);
++a1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, __int64 a2, __int64 a3)
{
__int64 result; // rax
unsigned __int64 v4; // rcx
__int64 i; // r8

result = a1;
v4 = a3 + a1;
for ( i = a1; a1 < v4; result = a1 )
{
*((_BYTE *)&store + result) &= *(_BYTE *)(a2 + result - i);
++a1;
}
return result;
}

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 v1; // rdx
char *v2; // rcx
__int64 result; // rax
void *v4; // rdi
__int64 v5; // rcx
__int64 v6; // rdx

v1 = a1 << 7;
v2 = (char *)&store + (int)v1;
result = (int)v1 + 128 < (unsigned int)(v1 + 1) ? 1 : 128;
if ( (unsigned int)result >= 8 )
{
*(__int64 *)((char *)&store + v1) = -1LL;
*(_QWORD *)&v2[(unsigned int)result - 8] = -1LL;
v4 = (void *)((unsigned __int64)(v2 + 8) & 0xFFFFFFFFFFFFFFF8LL);
v5 = (unsigned int)((_DWORD)v2 - (_DWORD)v4 + result) >> 3;
result = -1LL;
memset(v4, 0xFFu, 8 * v5);
}
else if ( (((int)v1 + 128 < (unsigned int)(v1 + 1) ? 1 : 0x80) & 4) != 0 )
{
*(_DWORD *)((char *)&store + v1) = -1;
*(_DWORD *)&v2[(unsigned int)result - 4] = -1;
}
else if ( ((int)v1 + 128 < (unsigned int)(v1 + 1) ? 0xFFFFFF81 : 0) != -128 )
{
*((_BYTE *)&store + v1) = -1;
if ( (((int)v1 + 128 < (unsigned int)(v1 + 1) ? 1 : 0x80) & 2) != 0 )
{
v6 = (unsigned int)result;
result = 0xFFFFFFFFLL;
*(_WORD *)&v2[v6 - 2] = -1;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int16 a1)
{
__int64 v1; // rdx
char *v2; // rcx
__int64 result; // rax
void *v4; // rdi
__int64 v5; // rcx
__int64 v6; // rdx

v1 = a1 << 7;
v2 = (char *)&store + (int)v1;
result = (int)v1 + 128 < (unsigned int)(v1 + 1) ? 1 : 128;
if ( (unsigned int)result >= 8 )
{
*(__int64 *)((char *)&store + v1) = -1LL;
*(_QWORD *)&v2[(unsigned int)result - 8] = -1LL;
v4 = (void *)((unsigned __int64)(v2 + 8) & 0xFFFFFFFFFFFFFFF8LL);
v5 = (unsigned int)((_DWORD)v2 - (_DWORD)v4 + result) >> 3;
result = -1LL;
memset(v4, 0xFFu, 8 * v5);
}
else if ( (((int)v1 + 128 < (unsigned int)(v1 + 1) ? 1 : 0x80) & 4) != 0 )
{
*(_DWORD *)((char *)&store + v1) = -1;
*(_DWORD *)&v2[(unsigned int)result - 4] = -1;
}
else if ( ((int)v1 + 128 < (unsigned int)(v1 + 1) ? 0xFFFFFF81 : 0) != -128 )
{
*((_BYTE *)&store + v1) = -1;
if ( (((int)v1 + 128 < (unsigned int)(v1 + 1) ? 1 : 0x80) & 2) != 0 )
{
v6 = (unsigned int)result;
result = 0xFFFFFFFFLL;
*(_WORD *)&v2[v6 - 2] = -1;
}
}
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:
__int64 __fastcall store_read(int a1, __int64 a2, __int64 a3)
{
__int64 v3; // rdx
int v4; // edi
__int64 v5; // rax
__int64 result; // rax

if ( a3 )
{
v3 = a2 + a3;
v4 = a1 - a2;
do
{
v5 = (unsigned int)(v4 + a2++);
result = *((unsigned __int8 *)&store + v5);
*(_BYTE *)(a2 - 1) = result;
}
while ( a2 != v3 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, __int64 a2, __int64 a3)
{
__int64 v3; // rdx
int v4; // edi
__int64 v5; // rax
__int64 result; // rax

if ( a3 )
{
v3 = a2 + a3;
v4 = a1 - a2;
do
{
v5 = (unsigned int)(v4 + a2++);
result = *((unsigned __int8 *)&store + v5);
*(_BYTE *)(a2 - 1) = result;
}
while ( a2 != v3 );
}
return result;
}

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:
__int64 flash_init()
{
__int64 result; // rax

result = -1LL;
is_off = 0;
fail_after = 0;
memset(&store, 0xFFu, 0x2000uLL);
write_count = 0;
return result;
}

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

result = -1LL;
is_off = 0;
fail_after = 0;
memset(&store, 0xFFu, 0x2000uLL);
write_count = 0;
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)
{
__int64 result; // rax
unsigned int v4; // er9

if ( a1 > 0x1FFF )
_assert_fail("addr < FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 0x57u, "flash_write");
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;
result = 0LL;
write_count = fail_after + 1;
}
else
{
++write_count;
result = 0LL;
if ( !is_off )
{
store_write(a1, a2, a3);
result = v4;
}
}
return result;
}

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

if ( a1 > 0x1FFF )
_assert_fail("addr < FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 0x57u, "flash_write");
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;
result = 0LL;
write_count = fail_after + 1;
}
else
{
++write_count;
result = 0LL;
if ( !is_off )
{
func0(a1, a2, a3);
result = v4;
}
}
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;
}

return_type: int


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

if ( a1 > 0x1FFF )
_assert_fail("addr < FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 0x6Eu, "flash_read");
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, __int64 a2, __int64 a3)
{
unsigned int v3; // ecx

if ( a1 > 0x1FFF )
_assert_fail("addr < FLASH_CHIP_SIZE", "Test/flash_port_mock.c", 0x6Eu, "flash_read");
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)
{
ssize_t v10; // rax
__int64 result; // rax
__off_t v12; // rbx
int *v13; // rax
__off_t v14; // rax
__off_t v15; // rax
int *v16; // rax
__off_t v17; // rbx
int *v18; // rax

if ( read(fd, a3, 8uLL) == 8 )
{
if ( *a3 == a2 )
{
if ( a3[1] < a5 )
{
v15 = 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],
a5,
v15);
result = 0LL;
}
else
{
v10 = read(fd, a4, a5);
if ( v10 == a5 )
{
if ( a6 && lseek(fd, a3[1] - v10 + (a3[1] & 1), 1) == -1 )
{
v16 = __errno_location();
fprintf(stderr, "Error while seeking to the end of chunk ID 0x%08X. Error = %d.\n", a2, (unsigned int)*v16);
result = 0LL;
}
else
{
result = 1LL;
}
}
else
{
v17 = lseek(fd, 0LL, 1);
v18 = __errno_location();
fprintf(
stderr,
"Error while reading chunk data for chunk ID 0x%08X. Error = %d. Offset = %d.\n",
a2,
(unsigned int)*v18,
v17);
result = 0LL;
}
}
}
else
{
v14 = lseek(fd, 0LL, 1);
fprintf(stderr, "Expected chunk ID = 0x%08x. Read chunk ID = 0x%08X. Offset = %d.\n", a2, *a3, v14);
result = 0LL;
}
}
else
{
v12 = lseek(fd, 0LL, 1);
v13 = __errno_location();
fprintf(
stderr,
"Error reading the header for chunk ID 0x%08X. Error = %d. Offset = %d.\n",
a2,
(unsigned int)*v13,
v12);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(int fd, unsigned int a2, unsigned int *a3, void *a4, size_t a5, int a6)
{
ssize_t v10; // rax
__int64 result; // rax
__off_t v12; // rbx
int *v13; // rax
__off_t v14; // rax
__off_t v15; // rax
int *v16; // rax
__off_t v17; // rbx
int *v18; // rax

if ( read(fd, a3, 8uLL) == 8 )
{
if ( *a3 == a2 )
{
if ( a3[1] < a5 )
{
v15 = 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],
a5,
v15);
result = 0LL;
}
else
{
v10 = read(fd, a4, a5);
if ( v10 == a5 )
{
if ( a6 && lseek(fd, a3[1] - v10 + (a3[1] & 1), 1) == -1 )
{
v16 = __errno_location();
fprintf(stderr, "Error while seeking to the end of chunk ID 0x%08X. Error = %d.\n", a2, (unsigned int)*v16);
result = 0LL;
}
else
{
result = 1LL;
}
}
else
{
v17 = lseek(fd, 0LL, 1);
v18 = __errno_location();
fprintf(
stderr,
"Error while reading chunk data for chunk ID 0x%08X. Error = %d. Offset = %d.\n",
a2,
(unsigned int)*v18,
v17);
result = 0LL;
}
}
}
else
{
v14 = lseek(fd, 0LL, 1);
fprintf(stderr, "Expected chunk ID = 0x%08x. Read chunk ID = 0x%08X. Offset = %d.\n", a2, *a3, v14);
result = 0LL;
}
}
else
{
v12 = lseek(fd, 0LL, 1);
v13 = __errno_location();
fprintf(
stderr,
"Error reading the header for chunk ID 0x%08X. Error = %d. Offset = %d.\n",
a2,
(unsigned int)*v13,
v12);
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(int *a1)
{
int v1; // eax
int v2; // eax
int v4; // [rsp+6h] [rbp-12h] BYREF
char v5; // [rsp+Ah] [rbp-Eh]
int v6; // [rsp+Bh] [rbp-Dh] BYREF
char v7; // [rsp+Fh] [rbp-9h]

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

canonical_decompiled_code:
int __fastcall func14(int *a1)
{
int v1; // eax
int v2; // eax
int v4; // [rsp+6h] [rbp-12h] BYREF
char v5; // [rsp+Ah] [rbp-Eh]
int v6; // [rsp+Bh] [rbp-Dh] BYREF
char v7; // [rsp+Fh] [rbp-9h]

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

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(int *a1)
{
int v1; // eax
int v3; // [rsp+Bh] [rbp-Dh] BYREF
char v4; // [rsp+Fh] [rbp-9h]

v1 = *a1;
v4 = 0;
v3 = v1;
puts("-- Format Header --");
printf(" Chunk ID: 0x%x (%s)\n", (unsigned int)*a1, (const char *)&v3);
printf(" Size: %i bytes\n", (unsigned int)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", (unsigned int)a1[3]);
printf(" ByteRate: %i Bps\n", (unsigned int)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 func15(int *a1)
{
int v1; // eax
int v3; // [rsp+Bh] [rbp-Dh] BYREF
char v4; // [rsp+Fh] [rbp-9h]

v1 = *a1;
v4 = 0;
v3 = v1;
puts("-- Format Header --");
printf(" Chunk ID: 0x%x (%s)\n", (unsigned int)*a1, (const char *)&v3);
printf(" Size: %i bytes\n", (unsigned int)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", (unsigned int)a1[3]);
printf(" ByteRate: %i Bps\n", (unsigned int)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(int *a1)
{
int v1; // eax
int v3; // [rsp+Bh] [rbp-Dh] BYREF
char v4; // [rsp+Fh] [rbp-9h]

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

canonical_decompiled_code:
int __fastcall func16(int *a1)
{
int v1; // eax
int v3; // [rsp+Bh] [rbp-Dh] BYREF
char v4; // [rsp+Fh] [rbp-9h]

v1 = *a1;
v4 = 0;
v3 = v1;
puts("-- Data Header --");
printf(" Chunk ID: 0x%x (%s)\n", (unsigned int)*a1, (const char *)&v3);
return printf(" Size: %.2f KiB\n", (double)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:
_BOOL8 __fastcall process_headers(int fd, unsigned int *a2)
{
_BOOL8 result; // rax

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

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

if ( a2 )
{
if ( (unsigned int)func11(fd, 0x46464952u, a2, a2 + 2, 4uLL, 0)
&& (unsigned int)func11(fd, 0x20746D66u, a2 + 3, a2 + 5, 0x10uLL, 1) )
{
result = (unsigned int)func13(fd, a2 + 9) != 0;
}
else
{
result = 0LL;
}
}
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
ssize_t v6; // r8
int *v7; // rax
int *v8; // rax
int *v9; // rax

v2 = lseek(fd, 0LL, 1);
if ( v2 == -1 )
{
v9 = __errno_location();
fprintf(stderr, "write_headers: Could not get the file offset. Error = %d.\n", (unsigned int)*v9);
result = 0LL;
}
else if ( buf )
{
v3 = v2;
if ( write(fd, buf, 0xCuLL) == 12 )
{
if ( write(fd, (char *)buf + 12, 0x18uLL) == 24 )
{
v6 = write(fd, (char *)buf + 36, 8uLL);
result = 1LL;
if ( v6 != 8 )
{
v7 = __errno_location();
fprintf(
stderr,
"write_headers: Error while writing data header. Start offset = %d. Error = %d.\n",
v3 + 36,
(unsigned int)*v7);
result = 0LL;
}
}
else
{
v8 = __errno_location();
fprintf(
stderr,
"write_headers: Error while writing format header. Start offset = %d. Error = %d.\n",
v3 + 12,
(unsigned int)*v8);
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 func18(int fd, void *buf)
{
__off_t v2; // rax
__off_t v3; // r12
int *v4; // rax
__int64 result; // rax
ssize_t v6; // r8
int *v7; // rax
int *v8; // rax
int *v9; // rax

v2 = lseek(fd, 0LL, 1);
if ( v2 == -1 )
{
v9 = __errno_location();
fprintf(stderr, "write_headers: Could not get the file offset. Error = %d.\n", (unsigned int)*v9);
result = 0LL;
}
else if ( buf )
{
v3 = v2;
if ( write(fd, buf, 0xCuLL) == 12 )
{
if ( write(fd, (char *)buf + 12, 0x18uLL) == 24 )
{
v6 = write(fd, (char *)buf + 36, 8uLL);
result = 1LL;
if ( v6 != 8 )
{
v7 = __errno_location();
fprintf(
stderr,
"write_headers: Error while writing data header. Start offset = %d. Error = %d.\n",
v3 + 36,
(unsigned int)*v7);
result = 0LL;
}
}
else
{
v8 = __errno_location();
fprintf(
stderr,
"write_headers: Error while writing format header. Start offset = %d. Error = %d.\n",
v3 + 12,
(unsigned int)*v8);
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


main

canonical_name: main

repo: DQNEO/minigit

decompiled_code:
// local variable allocation has failed, the output may be wrong!
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbx
int v4; // er13
FILE *v5; // r12
FILE *v6; // rax
FILE *v7; // rbp
const char *v9; // rdx

if ( argc == 1
|| ((*(_QWORD *)&argc = argv[1], v3 = argv, **(_WORD **)&argc != 25389)
|| (v4 = 1, *(_BYTE *)(*(_QWORD *)&argc + 2LL)))
&& (argv = (const char **)"-d", (v4 = strcmp(*(const char **)&argc, "-d")) != 0) )
{
usage(*(_QWORD *)&argc, argv, envp);
exit(0);
}
v5 = fopen(v3[2], "r");
if ( !v5 )
{
v9 = v3[2];
goto LABEL_14;
}
v6 = fopen(v3[3], "w");
v7 = v6;
if ( !v6 )
{
v9 = v3[3];
LABEL_14:
fprintf(stderr, "Can't open %s\n", v9);
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; // rbx
int v4; // er13
FILE *v5; // r12
FILE *v6; // rax
FILE *v7; // rbp
const char *v9; // rdx

if ( argc == 1
|| ((*(_QWORD *)&argc = argv[1], v3 = argv, **(_WORD **)&argc != 25389)
|| (v4 = 1, *(_BYTE *)(*(_QWORD *)&argc + 2LL)))
&& (argv = (const char **)"-d", (v4 = strcmp(*(const char **)&argc, "-d")) != 0) )
{
func9(*(_QWORD *)&argc, argv, envp);
exit(0);
}
v5 = fopen(v3[2], "r");
if ( !v5 )
{
v9 = v3[2];
goto LABEL_14;
}
v6 = fopen(v3[3], "w");
v7 = v6;
if ( !v6 )
{
v9 = v3[3];
LABEL_14:
fprintf(stderr, "Can't open %s\n", v9);
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 *


usage

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");
}


decompiled_code:
__int64 calc_values()
{
__int64 i; // rcx
__int64 result; // rax
__int64 v2; // rdx
__int64 v3; // rdx

qword_404068 = 1LL;
for ( i = 2LL; i != 1000000; ++i )
{
while ( 1 )
{
result = table[i];
if ( !result )
break;
if ( ++i == 1000000 )
return result;
}
v2 = i;
do
{
while ( 1 )
{
++result;
if ( (v2 & 1) == 0 )
break;
v2 = 3 * v2 + 1;
}
v2 >>= 1;
}
while ( v2 != 1 );
table[i] = result;
v3 = i;
do
{
table[v3] = ++result;
v3 *= 2LL;
}
while ( v3 <= 999999 );
}
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 i; // rcx
__int64 result; // rax
__int64 v2; // rdx
__int64 v3; // rdx

qword_404068 = 1LL;
for ( i = 2LL; i != 1000000; ++i )
{
while ( 1 )
{
result = table[i];
if ( !result )
break;
if ( ++i == 1000000 )
return result;
}
v2 = i;
do
{
while ( 1 )
{
++result;
if ( (v2 & 1) == 0 )
break;
v2 = 3 * v2 + 1;
}
v2 >>= 1;
}
while ( v2 != 1 );
table[i] = result;
v3 = i;
do
{
table[v3] = ++result;
v3 *= 2LL;
}
while ( v3 <= 999999 );
}
return result;
}

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


main

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(argc, argv, envp);
while ( (unsigned int)__isoc99_scanf() != -1 )
{
getchar();
if ( v7 <= v8 )
{
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", v7, v8, 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(argc, argv, envp);
while ( (unsigned int)__isoc99_scanf() != -1 )
{
getchar();
if ( v7 <= v8 )
{
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", v7, v8, 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; // rcx
int v5; // edx

result = a3;
if ( (int)a2 >= 0 && a2 >= 0 && rows > (int)a2 && columns > SHIDWORD(a2) )
{
v4 = (a2 >> 32) + 100LL * (int)a2;
v5 = 0;
if ( map[100 * (int)a1 + (a1 >> 32)] > map[v4] )
v5 = largest_path[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; // rcx
int v5; // edx

result = a3;
if ( (int)a2 >= 0 && a2 >= 0 && rows > (int)a2 && columns > SHIDWORD(a2) )
{
v4 = (a2 >> 32) + 100LL * (int)a2;
v5 = 0;
if ( map[100 * (int)a1 + (a1 >> 32)] > map[v4] )
v5 = largest_path[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


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; // rsi
__int64 v8; // rcx

result = (unsigned int)box_total;
if ( box_total > 0 )
{
v6 = 0LL;
v7 = 0LL;
while ( 1 )
{
v8 = box_count[v7];
result = 0LL;
if ( (int)v8 > 0 )
break;
LABEL_8:
++v7;
v6 += 25LL;
if ( v7 == box_total )
return result;
}
while ( boxes[result + v6] != a1 )
{
if ( ++result == v8 )
goto LABEL_8;
}
*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; // rsi
__int64 v8; // rcx

result = (unsigned int)box_total;
if ( box_total > 0 )
{
v6 = 0LL;
v7 = 0LL;
while ( 1 )
{
v8 = box_count[v7];
result = 0LL;
if ( (int)v8 > 0 )
break;
LABEL_8:
++v7;
v6 += 25LL;
if ( v7 == box_total )
return result;
}
while ( boxes[result + v6] != a1 )
{
if ( ++result == v8 )
goto LABEL_8;
}
*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)
{
__int64 result; // rax

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

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

result = (unsigned int)(box_count[a1] + 1);
boxes[25 * a1 + box_count[a1]] = a2;
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
int *v4; // r9
__int64 v5; // r10
int *v6; // r11

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

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

v2 = box_count[a1];
result = (unsigned int)(v2 - 1);
if ( (int)result > a2 )
{
v4 = (int *)((char *)&unk_40407C + 100 * a1 + 4 * v2);
do
{
result = func8(*v4, *v4);
--box_count[v5];
}
while ( v4 != 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)
{
__int64 v3; // rcx
int *v4; // rdx
__int64 v5; // rax
int v6; // eax
unsigned int v7; // er8
unsigned int v8; // edi
__int64 v9; // rbp
int v10; // er13
signed int v11; // er12
__int64 v12; // r12
__int64 i; // rbp
__int64 v14; // rbx
__int64 v15; // rsi
unsigned int v17; // [rsp+Ch] [rbp-4Ch] BYREF
unsigned int v18; // [rsp+10h] [rbp-48h] BYREF
unsigned int v19; // [rsp+14h] [rbp-44h] BYREF
unsigned int v20; // [rsp+18h] [rbp-40h] BYREF
unsigned int v21; // [rsp+1Ch] [rbp-3Ch]
unsigned int v22; // [rsp+20h] [rbp-38h]
char v23; // [rsp+26h] [rbp-32h]
char v24; // [rsp+2Ch] [rbp-2Ch]

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

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rcx
int *v4; // rdx
__int64 v5; // rax
int v6; // eax
unsigned int v7; // er8
unsigned int v8; // edi
__int64 v9; // rbp
int v10; // er13
signed int v11; // er12
__int64 v12; // r12
__int64 i; // rbp
__int64 v14; // rbx
__int64 v15; // rsi
unsigned int v17; // [rsp+Ch] [rbp-4Ch] BYREF
unsigned int v18; // [rsp+10h] [rbp-48h] BYREF
unsigned int v19; // [rsp+14h] [rbp-44h] BYREF
unsigned int v20; // [rsp+18h] [rbp-40h] BYREF
unsigned int v21; // [rsp+1Ch] [rbp-3Ch]
unsigned int v22; // [rsp+20h] [rbp-38h]
char v23; // [rsp+26h] [rbp-32h]
char v24; // [rsp+2Ch] [rbp-2Ch]

__isoc99_scanf();
v3 = box_total;
if ( box_total > 0 )
{
v4 = boxes;
v5 = 0LL;
do
{
*v4 = v5;
v4 += 25;
box_count[v5++] = 1;
}
while ( v3 != v5 );
}
while ( (unsigned int)__isoc99_scanf() != -1 && v23 != 113 )
{
__isoc99_scanf();
func7(v21, &v17, &v18);
func7(v22, &v19, &v20);
v6 = v21;
if ( v21 != v22 )
{
v7 = v19;
v8 = v17;
v9 = (int)v19;
if ( v17 != v19 )
{
if ( (int)v21 < (int)v22 )
v6 = v22;
if ( box_total >= v6 )
{
if ( v23 != 109 )
{
if ( v24 != 110 )
goto LABEL_13;
goto LABEL_23;
}
func9(v17, v18);
if ( v24 == 110 )
{
v7 = v19;
LABEL_23:
func9(v7, v20);
func7(v21, &v17, &v18);
func7(v22, &v19, &v20);
}
if ( v23 == 109 )
{
func7(v22, &v19, &v20);
func8(v19, (unsigned int)boxes[25 * v17 + v18]);
--box_count[v17];
}
else
{
v8 = v17;
v9 = (int)v19;
LABEL_13:
v10 = v18;
v11 = box_count[v8] - v18;
memcpy(&boxes[25 * v9 + box_count[v9]], &boxes[25 * v8 + v18], 4LL * v11);
box_count[v8] = v10;
box_count[v9] += v11;
}
}
}
}
}
v12 = 0LL;
for ( i = 0LL; box_total > (int)i; ++i )
{
v14 = 0LL;
printf("%d:", (unsigned int)i);
if ( box_count[i] > 0 )
{
do
{
v15 = (unsigned int)boxes[v14++ + v12];
printf(" %d", v15);
}
while ( box_count[i] > (int)v14 );
}
v12 += 25LL;
putchar(10);
}
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; // r13
__int64 v3; // rbp
__int64 v4; // rbx
__int64 v5; // rdx
int result; // eax

v2 = 6LL * a2;
v3 = 1LL;
v4 = 4LL * a2 + 24;
do
{
v5 = *(unsigned int *)((char *)&is_connected + v4);
if ( (_DWORD)v5 )
{
result = *(int *)((char *)arr + v4);
if ( !result )
{
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,
v3);
}
else
{
*(int *)((char *)arr + v4) = 1;
cur[a1] = v3;
arr[v3 + v2] = 1;
result = find_solutions((unsigned int)(a1 + 1), (unsigned int)v3, v5);
*(int *)((char *)arr + v4) = 0;
arr[v3 + v2] = 0;
}
}
}
++v3;
v4 += 24LL;
}
while ( v3 != 6 );
return result;
}

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

v2 = 6LL * a2;
v3 = 1LL;
v4 = 4LL * a2 + 24;
do
{
v5 = *(unsigned int *)((char *)&is_connected + v4);
if ( (_DWORD)v5 )
{
result = *(int *)((char *)arr + v4);
if ( !result )
{
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,
v3);
}
else
{
*(int *)((char *)arr + v4) = 1;
cur[a1] = v3;
arr[v3 + v2] = 1;
result = func7((unsigned int)(a1 + 1), (unsigned int)v3, v5);
*(int *)((char *)arr + v4) = 0;
arr[v3 + v2] = 0;
}
}
}
++v3;
v4 += 24LL;
}
while ( v3 != 6 );
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 = 1;
find_solutions(1LL, 1LL, envp);
return 0;
}

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

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


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:
_QWORD *get_smallest_edge()
{
_QWORD *v0; // rdx
_QWORD *v1; // r8
_QWORD *result; // rax

v0 = (_QWORD *)queueStart;
do
{
v1 = v0;
v0 = (_QWORD *)*v0;
}
while ( dword_404A68[434 * *((int *)v1 + 3)] );
dword_404A68[434 * *((int *)v1 + 3)] = 1;
result = v1;
queueStart = (__int64)v0;
return result;
}

canonical_decompiled_code:
_QWORD *func8()
{
_QWORD *v0; // rdx
_QWORD *v1; // r8
_QWORD *result; // rax

v0 = (_QWORD *)queueStart;
do
{
v1 = v0;
v0 = (_QWORD *)*v0;
}
while ( dword_404A68[434 * *((int *)v1 + 3)] );
dword_404A68[434 * *((int *)v1 + 3)] = 1;
result = v1;
queueStart = (__int64)v0;
return result;
}

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()
{
unsigned int v0; // er12
int *v1; // rdi
_QWORD *v2; // rax
int v3; // er11

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

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)
{
signed int v4; // edx
int result; // eax

v4 = 4 * (a3[14] & 0xF);
if ( v4 > 19 )
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)
{
signed int v4; // edx
int result; // eax

v4 = 4 * (a3[14] & 0xF);
if ( v4 > 19 )
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)
{
const char *v1; // rbx
unsigned int v2; // eax
unsigned int v3; // er12
__int64 v4; // rbp
int v5; // eax
int v6; // edx

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

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

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

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
__int64 v2; // rdx

if ( *(_DWORD *)ptr )
{
v1 = 0;
do
{
v2 = v1++;
free(*(void **)(*((_QWORD *)ptr + 2) + 8 * v2));
}
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
__int64 v2; // rdx

if ( *(_DWORD *)ptr )
{
v1 = 0;
do
{
v2 = v1++;
free(*(void **)(*((_QWORD *)ptr + 2) + 8 * v2));
}
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)
{
__int64 result; // rax

freeChunks(a1);
result = 0LL;
httpError = -10LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *a1)
{
__int64 result; // rax

func1(a1);
result = 0LL;
httpError = -10LL;
return result;
}

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 )
{
while ( 1 )
{
result = tryRecv(*(unsigned int *)(a2 + 24), *(_QWORD *)(a1 + 40) + v3, (unsigned int)(v2 - v3), v5);
if ( !(_BYTE)result )
break;
v2 = *(_DWORD *)(a1 + 48);
v3 += v5[0];
if ( v2 <= v3 )
goto LABEL_7;
}
}
else
{
LABEL_7:
result = 1LL;
*(_DWORD *)(a1 + 48) = v2 + 1;
}
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 )
{
while ( 1 )
{
result = tryRecv(*(unsigned int *)(a2 + 24), *(_QWORD *)(a1 + 40) + v3, (unsigned int)(v2 - v3), v5);
if ( !(_BYTE)result )
break;
v2 = *(_DWORD *)(a1 + 48);
v3 += v5[0];
if ( v2 <= v3 )
goto LABEL_7;
}
}
else
{
LABEL_7:
result = 1LL;
*(_DWORD *)(a1 + 48) = v2 + 1;
}
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; // r12
char v2; // bp
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; // r12
char v2; // bp
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; // rax
pthread_mutex_t *v5; // rbp
pthread_mutexattr_t attr; // [rsp+0h] [rbp-5Ch] BYREF
pthread_attr_t v7; // [rsp+4h] [rbp-58h] BYREF

if ( Initialised )
{
if ( a1 )
{
if ( a2 )
{
v3 = (void *)a2[7];
if ( v3 )
free(v3);
v4 = (pthread_mutex_t *)calloc(0x38uLL, 1uLL);
a2[7] = v4;
v5 = v4;
*(&v4[1].__align + 1) = a1;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, 2);
pthread_mutex_init(v5, &attr);
pthread_mutexattr_destroy(&attr);
pthread_attr_init(&v7);
pthread_attr_setdetachstate(&v7, 1);
pthread_attr_setscope(&v7, 1);
pthread_create((pthread_t *)&v5[1], &v7, httpContentThread, a2);
pthread_attr_destroy(&v7);
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; // rax
pthread_mutex_t *v5; // rbp
pthread_mutexattr_t attr; // [rsp+0h] [rbp-5Ch] BYREF
pthread_attr_t v7; // [rsp+4h] [rbp-58h] BYREF

if ( Initialised )
{
if ( a1 )
{
if ( a2 )
{
v3 = (void *)a2[7];
if ( v3 )
free(v3);
v4 = (pthread_mutex_t *)calloc(0x38uLL, 1uLL);
a2[7] = v4;
v5 = v4;
*(&v4[1].__align + 1) = a1;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, 2);
pthread_mutex_init(v5, &attr);
pthread_mutexattr_destroy(&attr);
pthread_attr_init(&v7);
pthread_attr_setdetachstate(&v7, 1);
pthread_attr_setscope(&v7, 1);
pthread_create((pthread_t *)&v5[1], &v7, httpContentThread, a2);
pthread_attr_destroy(&v7);
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; }


httpWaitForContent

decompiled_code:
void __fastcall httpWaitForContent(__int64 a1)
{
pthread_mutex_t *v1; // rbp

if ( Initialised )
{
if ( a1 )
{
v1 = *(pthread_mutex_t **)(a1 + 56);
pthread_mutex_lock(v1);
pthread_mutex_unlock(v1);
free(v1);
*(_QWORD *)(a1 + 56) = 0LL;
}
else
{
httpError = -11LL;
}
}
else
{
httpError = -2LL;
}
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1)
{
pthread_mutex_t *v1; // rbp

if ( Initialised )
{
if ( a1 )
{
v1 = *(pthread_mutex_t **)(a1 + 56);
pthread_mutex_lock(v1);
pthread_mutex_unlock(v1);
free(v1);
*(_QWORD *)(a1 + 56) = 0LL;
}
else
{
httpError = -11LL;
}
}
else
{
httpError = -2LL;
}
}

original_code:
void httpWaitForContent(Response *resp)
{
 ContentInternals *Intern;
 if (Initialised == 0)
 {
  httpError = -2;
  return;
 }
 if (resp == ((void *)0))
 {
  httpError = -11;
  return;
 }
 Intern = (ContentInternals *)resp->Private;
 pthread_mutex_lock(&Intern->mutex);
 pthread_mutex_unlock(&Intern->mutex);
 free(Intern);
 resp->Private = ((void *)0);
}

canonical_original_code:
void httpWaitForContent(struct _Response *resp)
{
 struct _ContentInternals *Intern;
 if (Initialised == 0)
 {
  httpError = -2;
  return;
 }
 if (resp == ((void *)0))
 {
  httpError = -11;
  return;
 }
 Intern = (struct _ContentInternals *)resp->Private;
 pthread_mutex_lock(&Intern->mutex);
 pthread_mutex_unlock(&Intern->mutex);
 free(Intern);
 resp->Private = ((void *)0);
}


httpGetError

decompiled_code:
__int64 httpGetError()
{
return httpError;
}

canonical_decompiled_code:
__int64 func0()
{
return httpError;
}

original_code:
int httpGetError()
{
 int ret = httpError;
 return ret;
}

variable_types: ret int


httpResetError

decompiled_code:
void httpResetError()
{
httpError = 0LL;
}

canonical_decompiled_code:
void func1()
{
httpError = 0LL;
}

original_code:
void httpResetError()
{
 httpError = 0;
}