getPartitionData

decompiled_code:
int *getPartitionData()
{
int *result; // rax
int v1; // er13
unsigned __int16 v2; // ax
char *v3; // rbp
char *v4; // rbx
__int64 v5; // r12
__int64 v6; // r12
unsigned int v7[2]; // [rsp-4h] [rbp-428h] BYREF
char s1[10]; // [rsp+4h] [rbp-420h] BYREF
unsigned __int16 v9; // [rsp+Eh] [rbp-416h]
char src; // [rsp+20h] [rbp-404h] BYREF

if ( isValid_1 != -1 )
{
result = &partitionData_0;
if ( !isValid_1 )
result = 0LL;
return result;
}
isValid_1 = 0;
read_disk(3u, 0LL, 2u, (__int64)v7);
v1 = memcmp(s1, "softw311", 8uLL);
if ( v1 )
{
puts("disk seems to not have allwinner partition table");
return 0LL;
}
partitionData_0 = _uint32_identity(v7[1]);
v2 = _uint16_identity(v9);
dword_A84 = v2;
if ( v2 > 0xFu )
{
printf("error: wrong partition count=%d\n", v2);
return 0LL;
}
v3 = dest;
v4 = &src;
if ( v2 )
{
do
{
v5 = (unsigned int)_uint32_identity(*((_DWORD *)v4 - 4));
*((_QWORD *)v3 - 2) = (unsigned int)_uint32_identity(*((_DWORD *)v4 - 3)) + (v5 << 32);
v6 = (unsigned int)_uint32_identity(*((_DWORD *)v4 - 2));
*((_QWORD *)v3 - 1) = (unsigned int)_uint32_identity(*((_DWORD *)v4 - 1)) + (v6 << 32);
v3[12] = 0;
strncpy(v3, v4, 0xCuLL);
v3[25] = 0;
strncpy(v3 + 13, v4 + 12, 0xCuLL);
*((_DWORD *)v3 + 7) = _uint32_identity(*((_DWORD *)v4 + 6));
*((_DWORD *)v3 + 8) = _uint32_identity(*((_DWORD *)v4 + 7));
++v1;
v3 += 56;
v4 += 64;
}
while ( *(&partitionData_0 + 1) > v1 );
}
isValid_1 = 1;
return &partitionData_0;
}

canonical_decompiled_code:
int *func7()
{
int *result; // rax
int v1; // er13
unsigned __int16 v2; // ax
char *v3; // rbp
char *v4; // rbx
__int64 v5; // r12
__int64 v6; // r12
unsigned int v7[2]; // [rsp-4h] [rbp-428h] BYREF
char s1[10]; // [rsp+4h] [rbp-420h] BYREF
unsigned __int16 v9; // [rsp+Eh] [rbp-416h]
char src; // [rsp+20h] [rbp-404h] BYREF

if ( isValid_1 != -1 )
{
result = &partitionData_0;
if ( !isValid_1 )
result = 0LL;
return result;
}
isValid_1 = 0;
func6(3u, 0LL, 2u, (__int64)v7);
v1 = memcmp(s1, "softw311", 8uLL);
if ( v1 )
{
puts("disk seems to not have allwinner partition table");
return 0LL;
}
partitionData_0 = func1(v7[1]);
v2 = func0(v9);
dword_A84 = v2;
if ( v2 > 0xFu )
{
printf("error: wrong partition count=%d\n", v2);
return 0LL;
}
v3 = dest;
v4 = &src;
if ( v2 )
{
do
{
v5 = (unsigned int)func1(*((_DWORD *)v4 - 4));
*((_QWORD *)v3 - 2) = (unsigned int)func1(*((_DWORD *)v4 - 3)) + (v5 << 32);
v6 = (unsigned int)func1(*((_DWORD *)v4 - 2));
*((_QWORD *)v3 - 1) = (unsigned int)func1(*((_DWORD *)v4 - 1)) + (v6 << 32);
v3[12] = 0;
strncpy(v3, v4, 0xCuLL);
v3[25] = 0;
strncpy(v3 + 13, v4 + 12, 0xCuLL);
*((_DWORD *)v3 + 7) = func1(*((_DWORD *)v4 + 6));
*((_DWORD *)v3 + 8) = func1(*((_DWORD *)v4 + 7));
++v1;
v3 += 56;
v4 += 64;
}
while ( *(&partitionData_0 + 1) > v1 );
}
isValid_1 = 1;
return &partitionData_0;
}

original_code:
static struct MbrPartitionData *getPartitionData(void)
{
    static struct MbrPartitionData partitionData;
    static int isValid = -1;
    if( isValid == -1 ) {
        MBR mbr;
        int i;
        isValid = 0;
        read_disk(FMAREA_LOGDISK, 0, 2, &mbr);
        if( memcmp(mbr.magic, MBR_MAGIC, 8) ) {
            printf("disk seems to not have allwinner partition table\n");
            return ((void *)0);
        }
        partitionData.version = __uint32_identity (mbr.version);
        partitionData.partitionCount = __uint16_identity (mbr.PartCount);
        if( partitionData.partitionCount < 0 ||
                partitionData.partitionCount > MBR_MAX_PART_COUNT )
        {
            printf("error: wrong partition count=%d\n",
                    partitionData.partitionCount);
            return ((void *)0);
        }
        for(i = 0; i < partitionData.partitionCount; ++i) {
            partitionData.partitions[i].firstSector =
                ((unsigned long long)__uint32_identity (mbr.array[i].addrhi) << 32) +
                __uint32_identity (mbr.array[i].addrlo);
            partitionData.partitions[i].sectorCount =
                ((unsigned long long)__uint32_identity (mbr.array[i].lenhi) << 32) +
                __uint32_identity (mbr.array[i].lenlo);
            memset(partitionData.partitions[i].classname, 0,
                    sizeof(partitionData.partitions[i].classname));
            strncpy(partitionData.partitions[i].classname,
                    mbr.array[i].classname,
                    sizeof(mbr.array[i].classname));
            memset(partitionData.partitions[i].name, 0,
                    sizeof(partitionData.partitions[i].name));
            strncpy(partitionData.partitions[i].name, mbr.array[i].name,
                    sizeof(mbr.array[i].name));
            partitionData.partitions[i].user_type =
                __uint32_identity (mbr.array[i].user_type);
            partitionData.partitions[i].ro = __uint32_identity (mbr.array[i].ro);
        }
        isValid = 1;
    }
    return isValid ? &partitionData : ((void *)0);
}

canonical_original_code:
static struct MbrPartitionData *getPartitionData()
{
    static struct MbrPartitionData partitionData;
    static int isValid = -1;
    if( isValid == -1 ) {
        struct tag_MBR mbr;
        int i;
        isValid = 0;
        read_disk(FMAREA_LOGDISK, 0, 2, &mbr);
        if( memcmp(mbr.magic, MBR_MAGIC, 8) ) {
            printf("disk seems to not have allwinner partition table\n");
            return ((void *)0);
        }
        partitionData.version = __uint32_identity (mbr.version);
        partitionData.partitionCount = __uint16_identity (mbr.PartCount);
        if( partitionData.partitionCount < 0 ||
                partitionData.partitionCount > MBR_MAX_PART_COUNT )
        {
            printf("error: wrong partition count=%d\n",
                    partitionData.partitionCount);
            return ((void *)0);
        }
        for(i = 0; i < partitionData.partitionCount; ++i) {
            partitionData.partitions[i].firstSector =
                ((unsigned long long)__uint32_identity (mbr.array[i].addrhi) << 32) +
                __uint32_identity (mbr.array[i].addrlo);
            partitionData.partitions[i].sectorCount =
                ((unsigned long long)__uint32_identity (mbr.array[i].lenhi) << 32) +
                __uint32_identity (mbr.array[i].lenlo);
            memset(partitionData.partitions[i].classname, 0,
                    sizeof(partitionData.partitions[i].classname));
            strncpy(partitionData.partitions[i].classname,
                    mbr.array[i].classname,
                    sizeof(mbr.array[i].classname));
            memset(partitionData.partitions[i].name, 0,
                    sizeof(partitionData.partitions[i].name));
            strncpy(partitionData.partitions[i].name, mbr.array[i].name,
                    sizeof(mbr.array[i].name));
            partitionData.partitions[i].user_type =
                __uint32_identity (mbr.array[i].user_type);
            partitionData.partitions[i].ro = __uint32_identity (mbr.array[i].ro);
        }
        isValid = 1;
    }
    return isValid ? &partitionData : ((void *)0);
}

variable_types: isValid int

variable_types: struct tag_MBR { unsigned int crc32; unsigned int version; char magic[8]; unsigned char copy; unsigned char index; unsigned short PartCount; struct tag_PARTITION { unsigned int addrhi; unsigned int addrlo; unsigned int lenhi; unsigned int lenlo; char classname[12]; char name[12]; unsigned int user_type; unsigned int ro; char res[16]; } array[-1]; char res[-1]; }

variable_types: i int

return_type: struct MbrPartitionData { int version; int partitionCount; struct MbrPartition { unsigned long long firstSector; unsigned long long sectorCount; char classname[13]; char name[13]; unsigned int user_type; unsigned int ro; } partitions[-1]; } *

user_defined_types: struct MbrPartitionData { int version; int partitionCount; struct MbrPartition partitions[-1]; }

user_defined_types: struct tag_MBR { unsigned int crc32; unsigned int version; char magic[8]; unsigned char copy; unsigned char index; unsigned short PartCount; struct tag_PARTITION array[-1]; char res[-1]; }


decompiled_code:
__int64 __fastcall write_disk(unsigned int a1, __int64 a2, unsigned int a3, __int64 a4)
{
return send_command(0x57u, a1, a2, a3, a4, a3 << 9);
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, __int64 a2, unsigned int a3, __int64 a4)
{
return func4(0x57u, a1, a2, a3, a4, a3 << 9);
}

original_code:
void write_disk(enum FlashMemoryArea fmarea,
        unsigned long long firstSector,
        unsigned long long count, const void *wrbuf)
{
    send_command(BCMD_DISKWRITE, fmarea, firstSector, count, wrbuf, count*512);
}

canonical_original_code:
void write_disk(enum FlashMemoryArea fmarea, unsigned long long firstSector, unsigned long long count, void *wrbuf)
{
    send_command(BCMD_DISKWRITE, fmarea, firstSector, count, wrbuf, count*512);
}

variable_types: enum FlashMemoryArea { FMAREA_BOOT0=0, FMAREA_BOOT1=1, FMAREA_PHYDISK=2, FMAREA_LOGDISK=3, FMAREA_COUNT=4 }

variable_types:
firstSector
unsigned long long

variable_types:
count
unsigned long long

variable_types:
wrbuf
void *

return_type: void


decompiled_code:
void *get_flashmem_props()
{
unsigned int *v1; // rbx

if ( is_initialized_3 )
return &props_2;
send_command(0x46u, 0, 0LL, 0, 0LL, 0);
get_response_data((__int64)&props_2, 0x48u);
v1 = (unsigned int *)&props_2;
do
{
*v1 = _uint32_identity(*v1);
v1[1] = _uint32_identity(v1[1]);
v1[2] = _uint32_identity(v1[2]);
v1 += 3;
}
while ( v1 != (unsigned int *)&dword_E10 );
dword_E10 = _uint32_identity(dword_E10);
dword_E14 = _uint32_identity(dword_E14);
dword_E18 = _uint32_identity(dword_E18);
dword_E1C = _uint32_identity(dword_E1C);
dword_E20 = _uint32_identity(dword_E20);
dword_E24 = _uint32_identity(dword_E24);
is_initialized_3 = 1;
return &props_2;
}

canonical_decompiled_code:
void *func9()
{
unsigned int *v1; // rbx

if ( is_initialized_3 )
return &props_2;
func4(0x46u, 0, 0LL, 0, 0LL, 0);
func5((__int64)&props_2, 0x48u);
v1 = (unsigned int *)&props_2;
do
{
*v1 = func1(*v1);
v1[1] = func1(v1[1]);
v1[2] = func1(v1[2]);
v1 += 3;
}
while ( v1 != (unsigned int *)&dword_E10 );
dword_E10 = func1(dword_E10);
dword_E14 = func1(dword_E14);
dword_E18 = func1(dword_E18);
dword_E1C = func1(dword_E1C);
dword_E20 = func1(dword_E20);
dword_E24 = func1(dword_E24);
is_initialized_3 = 1;
return &props_2;
}

original_code:
struct flashmem_properties *get_flashmem_props(void)
{
    static struct flashmem_properties props;
    static int is_initialized;
    if( ! is_initialized ) {
        int i;
        send_command(BCMD_FLASHMEM_PARAMS, 0, 0, 0, ((void *)0), 0);
        get_response_data(&props, sizeof(props));
        for(i = 0; i < FMAREA_COUNT; ++i) {
            props.areas[i].sectors_per_page =
                __uint32_identity (props.areas[i].sectors_per_page);
            props.areas[i].first_pageno =
                __uint32_identity (props.areas[i].first_pageno);
            props.areas[i].page_count =
                __uint32_identity (props.areas[i].page_count);
        }
        props.sectors_per_page = __uint32_identity (props.sectors_per_page);
        props.pages_per_block = __uint32_identity (props.pages_per_block);
        props.blocks_per_chip = __uint32_identity (props.blocks_per_chip);
        props.chip_cnt = __uint32_identity (props.chip_cnt);
        props.pagewithbadflag = __uint32_identity (props.pagewithbadflag);
        props.block_cnt_of_zone = __uint32_identity (props.block_cnt_of_zone);
        is_initialized = 1;
    }
    return &props;
}

canonical_original_code:
struct flashmem_properties *get_flashmem_props()
{
    static struct flashmem_properties props;
    static int is_initialized;
    if( ! is_initialized ) {
        int i;
        send_command(BCMD_FLASHMEM_PARAMS, 0, 0, 0, ((void *)0), 0);
        get_response_data(&props, sizeof(props));
        for(i = 0; i < FMAREA_COUNT; ++i) {
            props.areas[i].sectors_per_page =
                __uint32_identity (props.areas[i].sectors_per_page);
            props.areas[i].first_pageno =
                __uint32_identity (props.areas[i].first_pageno);
            props.areas[i].page_count =
                __uint32_identity (props.areas[i].page_count);
        }
        props.sectors_per_page = __uint32_identity (props.sectors_per_page);
        props.pages_per_block = __uint32_identity (props.pages_per_block);
        props.blocks_per_chip = __uint32_identity (props.blocks_per_chip);
        props.chip_cnt = __uint32_identity (props.chip_cnt);
        props.pagewithbadflag = __uint32_identity (props.pagewithbadflag);
        props.block_cnt_of_zone = __uint32_identity (props.block_cnt_of_zone);
        is_initialized = 1;
    }
    return &props;
}

return_type: struct flashmem_properties { struct flashmarea_properties { unsigned int sectors_per_page; unsigned int first_pageno; unsigned int page_count; } areas[-1]; unsigned int sectors_per_page; unsigned int pages_per_block; unsigned int blocks_per_chip; unsigned int chip_cnt; unsigned int pagewithbadflag; unsigned int block_cnt_of_zone; } *

user_defined_types: struct flashmem_properties { struct flashmarea_properties areas[-1]; unsigned int sectors_per_page; unsigned int pages_per_block; unsigned int blocks_per_chip; unsigned int chip_cnt; unsigned int pagewithbadflag; unsigned int block_cnt_of_zone; }


getMbrVersion

decompiled_code:
__int64 getMbrVersion()
{
int *v0; // rax
__int64 result; // rax

v0 = getPartitionData();
if ( v0 )
result = (unsigned int)*v0;
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 func10()
{
int *v0; // rax
__int64 result; // rax

v0 = func7();
if ( v0 )
result = (unsigned int)*v0;
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int getMbrVersion(void)
{
    struct MbrPartitionData *pdata = getPartitionData();
    return pdata == ((void *)0) ? -1 : pdata->version;
}

canonical_original_code:
int getMbrVersion()
{
    struct MbrPartitionData *pdata = getPartitionData();
    return pdata == ((void *)0) ? -1 : pdata->version;
}

variable_types: struct MbrPartitionData { int version; int partitionCount; struct MbrPartition { unsigned long long firstSector; unsigned long long sectorCount; char classname[13]; char name[13]; unsigned int user_type; unsigned int ro; } partitions[-1]; } *

return_type: int


getPartitionCount

decompiled_code:
__int64 getPartitionCount()
{
int *v0; // rax
__int64 result; // rax

v0 = getPartitionData();
if ( v0 )
result = (unsigned int)v0[1];
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 func11()
{
int *v0; // rax
__int64 result; // rax

v0 = func7();
if ( v0 )
result = (unsigned int)v0[1];
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int getPartitionCount(void)
{
    struct MbrPartitionData *pdata = getPartitionData();
    return pdata == ((void *)0) ? -1 : pdata->partitionCount;
}

canonical_original_code:
int getPartitionCount()
{
    struct MbrPartitionData *pdata = getPartitionData();
    return pdata == ((void *)0) ? -1 : pdata->partitionCount;
}

user_defined_types: struct MbrPartition { unsigned long long firstSector; unsigned long long sectorCount; char classname[13]; char name[13]; unsigned int user_type; unsigned int ro; }


getPartitionNoParams

decompiled_code:
int *__fastcall getPartitionNoParams(int a1)
{
int *result; // rax

result = getPartitionData();
if ( result )
{
if ( result[1] <= a1 )
result = 0LL;
else
result += 14 * a1 + 2;
}
return result;
}

canonical_decompiled_code:
int *__fastcall func12(int a1)
{
int *result; // rax

result = func7();
if ( result )
{
if ( result[1] <= a1 )
result = 0LL;
else
result += 14 * a1 + 2;
}
return result;
}

original_code:
struct MbrPartition *getPartitionNoParams(int partNo)
{
    struct MbrPartitionData *pdata = getPartitionData();
    return pdata == ((void *)0) || partNo >= pdata->partitionCount ? ((void *)0) :
        pdata->partitions + partNo;
}

variable_types: partNo int

return_type: struct MbrPartition { unsigned long long firstSector; unsigned long long sectorCount; char classname[13]; char name[13]; unsigned int user_type; unsigned int ro; } *


getParitionParams

decompiled_code:
__int64 __fastcall getParitionParams(char *nptr, _DWORD *a2, unsigned __int64 *a3, _QWORD *a4)
{
__int64 result; // rax
int v6; // er12
__int64 v7; // rdx
__int64 v8; // rax
unsigned __int64 v9; // rcx
unsigned __int64 v10; // rsi
int v11; // er15
const char *v12; // r13
int v13; // eax
unsigned int *v14; // [rsp+8h] [rbp-50h]
int *v16; // [rsp+18h] [rbp-40h]

v14 = (unsigned int *)get_flashmem_props();
*a2 = 4;
if ( !strcmp(nptr, "boot0") )
{
*a2 = 0;
LABEL_3:
*a3 = 0LL;
*a4 = v14[3 * *a2 + 2] * (unsigned __int64)v14[3 * *a2];
return 1LL;
}
if ( !strcmp(nptr, "boot1") )
{
*a2 = 1;
goto LABEL_3;
}
if ( !strcmp(nptr, "disk-logic") )
{
*a2 = 3;
goto LABEL_3;
}
v16 = getPartitionData();
if ( !v16 )
return 0LL;
v11 = v16[1];
if ( v11 <= 0 )
{
v6 = 0;
}
else
{
v12 = (char *)v16 + 37;
v6 = 0;
while ( strcmp(nptr, v12) )
{
++v6;
v12 += 56;
if ( v6 == v11 )
goto LABEL_21;
}
}
if ( v11 == v6 )
{
LABEL_21:
if ( ((*_ctype_b_loc())[*nptr] & 0x800) == 0 )
{
printf("partition with name %s not found\n", nptr);
return 0LL;
}
v13 = strtoul(nptr, 0LL, 0);
v6 = v13;
if ( v13 < 0 || v16[1] <= v13 )
{
printf("wrong prtition number %s\n", nptr);
return 0LL;
}
}
*a2 = 3;
v7 = 14LL * v6;
*a3 = *(_QWORD *)&v16[v7 + 2];
v8 = *(_QWORD *)&v16[v7 + 4];
*a4 = v8;
v9 = v14[11] * (unsigned __int64)v14[9];
if ( *a3 >= v9 || (v10 = v8 + *a3, result = 1LL, v10 > v9) )
{
printf("ERROR: corrupted MBR, partition %s is beyond disk size\n", (const char *)&v16[v7 + 9] + 1);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(char *nptr, _DWORD *a2, unsigned __int64 *a3, _QWORD *a4)
{
__int64 result; // rax
int v6; // er12
__int64 v7; // rdx
__int64 v8; // rax
unsigned __int64 v9; // rcx
unsigned __int64 v10; // rsi
int v11; // er15
const char *v12; // r13
int v13; // eax
unsigned int *v14; // [rsp+8h] [rbp-50h]
int *v16; // [rsp+18h] [rbp-40h]

v14 = (unsigned int *)func9();
*a2 = 4;
if ( !strcmp(nptr, "boot0") )
{
*a2 = 0;
LABEL_3:
*a3 = 0LL;
*a4 = v14[3 * *a2 + 2] * (unsigned __int64)v14[3 * *a2];
return 1LL;
}
if ( !strcmp(nptr, "boot1") )
{
*a2 = 1;
goto LABEL_3;
}
if ( !strcmp(nptr, "disk-logic") )
{
*a2 = 3;
goto LABEL_3;
}
v16 = func7();
if ( !v16 )
return 0LL;
v11 = v16[1];
if ( v11 <= 0 )
{
v6 = 0;
}
else
{
v12 = (char *)v16 + 37;
v6 = 0;
while ( strcmp(nptr, v12) )
{
++v6;
v12 += 56;
if ( v6 == v11 )
goto LABEL_21;
}
}
if ( v11 == v6 )
{
LABEL_21:
if ( ((*_ctype_b_loc())[*nptr] & 0x800) == 0 )
{
printf("partition with name %s not found\n", nptr);
return 0LL;
}
v13 = strtoul(nptr, 0LL, 0);
v6 = v13;
if ( v13 < 0 || v16[1] <= v13 )
{
printf("wrong prtition number %s\n", nptr);
return 0LL;
}
}
*a2 = 3;
v7 = 14LL * v6;
*a3 = *(_QWORD *)&v16[v7 + 2];
v8 = *(_QWORD *)&v16[v7 + 4];
*a4 = v8;
v9 = v14[11] * (unsigned __int64)v14[9];
if ( *a3 >= v9 || (v10 = v8 + *a3, result = 1LL, v10 > v9) )
{
printf("ERROR: corrupted MBR, partition %s is beyond disk size\n", (const char *)&v16[v7 + 9] + 1);
result = 0LL;
}
return result;
}

original_code:
int getParitionParams(const char *partName,
        enum FlashMemoryArea *pFMArea,
        unsigned long long *pFirstSector,
        unsigned long long *pSectorCount)
{
    struct flashmem_properties *props = get_flashmem_props();
    struct MbrPartitionData *pdata;
    *pFMArea = FMAREA_COUNT;
    if( ! strcmp(partName, "boot0") ) {
        *pFMArea = FMAREA_BOOT0;
    }else if( ! strcmp(partName, "boot1") ) {
        *pFMArea = FMAREA_BOOT1;
    }else if( ! strcmp(partName, "disk-logic") ) {
        *pFMArea = FMAREA_LOGDISK;
    }
    if( *pFMArea != FMAREA_COUNT ) {
        *pFirstSector = 0LL;
        *pSectorCount = 1ULL * props->areas[*pFMArea].sectors_per_page *
            props->areas[*pFMArea].page_count;
    }else{
        int i;
        unsigned long long diskSize;
        if( (pdata = getPartitionData()) == ((void *)0) )
            return 0;
        for( i = 0; i < pdata->partitionCount; ++i ) {
            if( ! strcmp(partName, pdata->partitions[i].name) )
                break;
        }
        if( i == pdata->partitionCount ) {
            if( ((*__ctype_b_loc ())[(int) ((partName[0]))] & (unsigned short int) _ISdigit) ) {
                i = strtoul(partName, ((void *)0), 0);
                if( i < 0 || i >= pdata->partitionCount ) {
                    printf("wrong prtition number %s\n", partName);
                    return 0;
                }
            }else{
                printf("partition with name %s not found\n", partName);
                return 0;
            }
        }
        *pFMArea = FMAREA_LOGDISK;
        *pFirstSector = pdata->partitions[i].firstSector;
        *pSectorCount = pdata->partitions[i].sectorCount;
        diskSize = 1ULL * props->areas[FMAREA_LOGDISK].sectors_per_page *
            props->areas[FMAREA_LOGDISK].page_count;
        if( *pFirstSector >= diskSize ||
                *pFirstSector + *pSectorCount > diskSize)
        {
            printf("ERROR: corrupted MBR, partition %s is beyond disk size\n",
                    pdata->partitions[i].name);
            return 0;
        }
    }
    return 1;
}

canonical_original_code:
int getParitionParams(char *partName, enum FlashMemoryArea *pFMArea, unsigned long long *pFirstSector, unsigned long long *pSectorCount)
{
    struct flashmem_properties *props = get_flashmem_props();
    struct MbrPartitionData *pdata;
    *pFMArea = FMAREA_COUNT;
    if( ! strcmp(partName, "boot0") ) {
        *pFMArea = FMAREA_BOOT0;
    }else if( ! strcmp(partName, "boot1") ) {
        *pFMArea = FMAREA_BOOT1;
    }else if( ! strcmp(partName, "disk-logic") ) {
        *pFMArea = FMAREA_LOGDISK;
    }
    if( *pFMArea != FMAREA_COUNT ) {
        *pFirstSector = 0LL;
        *pSectorCount = 1ULL * props->areas[*pFMArea].sectors_per_page *
            props->areas[*pFMArea].page_count;
    }else{
        int i;
        unsigned long long diskSize;
        if( (pdata = getPartitionData()) == ((void *)0) )
            return 0;
        for( i = 0; i < pdata->partitionCount; ++i ) {
            if( ! strcmp(partName, pdata->partitions[i].name) )
                break;
        }
        if( i == pdata->partitionCount ) {
            if( ((*__ctype_b_loc ())[(int) ((partName[0]))] & (unsigned short) _ISdigit) ) {
                i = strtoul(partName, ((void *)0), 0);
                if( i < 0 || i >= pdata->partitionCount ) {
                    printf("wrong prtition number %s\n", partName);
                    return 0;
                }
            }else{
                printf("partition with name %s not found\n", partName);
                return 0;
            }
        }
        *pFMArea = FMAREA_LOGDISK;
        *pFirstSector = pdata->partitions[i].firstSector;
        *pSectorCount = pdata->partitions[i].sectorCount;
        diskSize = 1ULL * props->areas[FMAREA_LOGDISK].sectors_per_page *
            props->areas[FMAREA_LOGDISK].page_count;
        if( *pFirstSector >= diskSize ||
                *pFirstSector + *pSectorCount > diskSize)
        {
            printf("ERROR: corrupted MBR, partition %s is beyond disk size\n",
                    pdata->partitions[i].name);
            return 0;
        }
    }
    return 1;
}

variable_types:
partName
char *

variable_types: enum FlashMemoryArea { FMAREA_BOOT0=0, FMAREA_BOOT1=1, FMAREA_PHYDISK=2, FMAREA_LOGDISK=3, FMAREA_COUNT=4 } *

variable_types:
pFirstSector
unsigned long long *

variable_types:
pSectorCount
unsigned long long *

variable_types: struct flashmem_properties { struct flashmarea_properties { unsigned int sectors_per_page; unsigned int first_pageno; unsigned int page_count; } areas[-1]; unsigned int sectors_per_page; unsigned int pages_per_block; unsigned int blocks_per_chip; unsigned int chip_cnt; unsigned int pagewithbadflag; unsigned int block_cnt_of_zone; } *

variable_types:
diskSize
unsigned long long


decompiled_code:
__int64 __fastcall get_mountstatus(unsigned int *a1)
{
__int64 result; // rax

send_command(0x6Du, 0, 0LL, 0, 0LL, 0);
get_response_data((__int64)a1, 0xCu);
*a1 = _uint32_identity(*a1);
a1[1] = _uint32_identity(a1[1]);
result = _uint32_identity(a1[2]);
a1[2] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned int *a1)
{
__int64 result; // rax

func4(0x6Du, 0, 0LL, 0, 0LL, 0);
func5((__int64)a1, 0xCu);
*a1 = func1(*a1);
a1[1] = func1(a1[1]);
result = func1(a1[2]);
a1[2] = result;
return result;
}

original_code:
void get_mountstatus(struct diskmount_status *stat)
{
    send_command(BCMD_GETMOUNT_STATUS, 0, 0, 0, ((void *)0), 0);
    get_response_data(stat, sizeof(*stat));
    stat->mountMode = __uint32_identity (stat->mountMode);
    stat->firstSector = __uint32_identity (stat->firstSector);
    stat->sectorCount = __uint32_identity (stat->sectorCount);
}

variable_types: struct diskmount_status { unsigned int mountMode; unsigned int firstSector; unsigned int sectorCount; } *


decompiled_code:
__int64 usbcomm_init()
{
unsigned int v0; // ebp
int v2; // ebx
__int64 v3; // rax

if ( (unsigned int)libusb_init(0LL) )
{
puts("libusb_init fail");
v0 = -1;
}
else
{
v0 = 0;
usb = libusb_open_device_with_vid_pid(0LL, 2996LL, 4095LL);
if ( usb )
goto LABEL_3;
printf("open device failed, trying to fel...");
fflush(stdout);
v2 = fel_ainol();
if ( v2 )
{
fwrite("\nfailed to open A10 USB FEL device\n", 1uLL, 0x23uLL, stderr);
return (unsigned int)-1;
}
if ( !usb )
{
do
{
putchar(46);
fflush(stdout);
usleep(0x7A120u);
v3 = libusb_open_device_with_vid_pid(0LL, 2996LL, 4095LL);
usb = v3;
++v2;
}
while ( v2 <= 9 && !v3 );
}
putchar(10);
if ( usb )
{
LABEL_3:
libusb_claim_interface(usb, 0LL);
}
else
{
puts("open device failed");
v0 = -1;
}
}
return v0;
}

canonical_decompiled_code:
__int64 func15()
{
unsigned int v0; // ebp
int v2; // ebx
__int64 v3; // rax

if ( (unsigned int)libusb_init(0LL) )
{
puts("libusb_init fail");
v0 = -1;
}
else
{
v0 = 0;
usb = libusb_open_device_with_vid_pid(0LL, 2996LL, 4095LL);
if ( usb )
goto LABEL_3;
printf("open device failed, trying to fel...");
fflush(stdout);
v2 = fel_ainol();
if ( v2 )
{
fwrite("\nfailed to open A10 USB FEL device\n", 1uLL, 0x23uLL, stderr);
return (unsigned int)-1;
}
if ( !usb )
{
do
{
putchar(46);
fflush(stdout);
usleep(0x7A120u);
v3 = libusb_open_device_with_vid_pid(0LL, 2996LL, 4095LL);
usb = v3;
++v2;
}
while ( v2 <= 9 && !v3 );
}
putchar(10);
if ( usb )
{
LABEL_3:
libusb_claim_interface(usb, 0LL);
}
else
{
puts("open device failed");
v0 = -1;
}
}
return v0;
}

original_code:
int usbcomm_init(void)
{
    int i;
    if( libusb_init(((void *)0)) != 0 ) {
        printf("libusb_init fail\n");
        return -1;
    }
    if( (usb = libusb_open_device_with_vid_pid(((void *)0), 0xbb4, 0xfff)) == ((void *)0) )
    {
        printf("open device failed, trying to fel...");
        fflush(stdout);
        if( fel_ainol() != 0 ) {
            fprintf(stderr, "\nfailed to open A10 USB FEL device\n");
            return -1;
        }
        for(i = 0; usb == ((void *)0) && i < 10; ++i) {
            printf(".");
            fflush(stdout);
            usleep(500000);
            usb = libusb_open_device_with_vid_pid(((void *)0), 0xbb4, 0xfff);
        }
        printf("\n");
        if( usb == ((void *)0) ) {
            printf("open device failed\n");
            return -1;
        }
    }
    libusb_claim_interface(usb, 0);
    return 0;
}

canonical_original_code:
int usbcomm_init()
{
    int i;
    if( libusb_init(((void *)0)) != 0 ) {
        printf("libusb_init fail\n");
        return -1;
    }
    if( (usb = libusb_open_device_with_vid_pid(((void *)0), 0xbb4, 0xfff)) == ((void *)0) )
    {
        printf("open device failed, trying to fel...");
        fflush(stdout);
        if( fel_ainol() != 0 ) {
            fprintf(stderr, "\nfailed to open A10 USB FEL device\n");
            return -1;
        }
        for(i = 0; usb == ((void *)0) && i < 10; ++i) {
            printf(".");
            fflush(stdout);
            usleep(500000);
            usb = libusb_open_device_with_vid_pid(((void *)0), 0xbb4, 0xfff);
        }
        printf("\n");
        if( usb == ((void *)0) ) {
            printf("open device failed\n");
            return -1;
        }
    }
    libusb_claim_interface(usb, 0);
    return 0;
}


decompiled_code:
__int64 usbcomm_exit()
{
libusb_release_interface(usb, 0LL);
libusb_close(usb);
return libusb_exit(0LL);
}

canonical_decompiled_code:
__int64 func16()
{
libusb_release_interface(usb, 0LL);
libusb_close(usb);
return libusb_exit(0LL);
}

original_code:
void usbcomm_exit(void)
{
    libusb_release_interface(usb, 0);
    libusb_close(usb);
    libusb_exit(((void *)0));
}

canonical_original_code:
void usbcomm_exit()
{
    libusb_release_interface(usb, 0);
    libusb_close(usb);
    libusb_exit(((void *)0));
}


decompiled_code:
void __fastcall extract_img(const char *a1, const char *a2, int a3, unsigned int a4, unsigned int a5)
{
unsigned int v6; // ebp
unsigned int v7; // edx
int v8; // ebx
int *v9; // rax
char *v10; // rax
int v11; // er13
unsigned int v12; // edx
int *v13; // rax
char *v14; // rax
int *v15; // rax
char *v16; // rax
int *v17; // rax
char *v18; // rax
_BYTE v19[32768]; // [rsp-8000h] [rbp-8428h] BYREF
char s[1064]; // [rsp+0h] [rbp-428h] BYREF

if ( a5 )
{
v6 = a5;
if ( (int)lseek(a3, a4, 0) < 0 )
{
v9 = __errno_location();
v10 = strerror(*v9);
fprintf(stderr, "lseek: %s\n", v10);
return;
}
v7 = 0x8000;
if ( v6 <= 0x8000 )
v7 = v6;
v8 = read(a3, v19, v7);
if ( v8 > 0 )
{
snprintf(s, 0x400uLL, "%s_%s", a1, a2);
printf("extracting %s into %s\n", a1, s);
v11 = open(s, 65, 436LL);
if ( v11 < 0 )
{
v13 = __errno_location();
v14 = strerror(*v13);
fprintf(stderr, "unable to create %s: %s\n", s, v14);
return;
}
while ( 1 )
{
if ( (unsigned int)write(v11, v19, (unsigned int)v8) != v8 )
{
v15 = __errno_location();
v16 = strerror(*v15);
fprintf(stderr, "write: %s\n", v16);
close(v11);
return;
}
v6 -= v8;
if ( !v6 )
break;
v12 = 0x8000;
if ( v6 <= 0x8000 )
v12 = v6;
v8 = read(a3, v19, v12);
if ( v8 <= 0 )
goto LABEL_20;
}
v8 = 0;
LABEL_20:
close(v11);
}
if ( v8 < 0 )
{
v17 = __errno_location();
v18 = strerror(*v17);
fprintf(stderr, "read: %s\n", v18);
}
}
}

canonical_decompiled_code:
void __fastcall func7(const char *a1, const char *a2, int a3, unsigned int a4, unsigned int a5)
{
unsigned int v6; // ebp
unsigned int v7; // edx
int v8; // ebx
int *v9; // rax
char *v10; // rax
int v11; // er13
unsigned int v12; // edx
int *v13; // rax
char *v14; // rax
int *v15; // rax
char *v16; // rax
int *v17; // rax
char *v18; // rax
_BYTE v19[32768]; // [rsp-8000h] [rbp-8428h] BYREF
char s[1064]; // [rsp+0h] [rbp-428h] BYREF

if ( a5 )
{
v6 = a5;
if ( (int)lseek(a3, a4, 0) < 0 )
{
v9 = __errno_location();
v10 = strerror(*v9);
fprintf(stderr, "lseek: %s\n", v10);
return;
}
v7 = 0x8000;
if ( v6 <= 0x8000 )
v7 = v6;
v8 = read(a3, v19, v7);
if ( v8 > 0 )
{
snprintf(s, 0x400uLL, "%s_%s", a1, a2);
printf("extracting %s into %s\n", a1, s);
v11 = open(s, 65, 436LL);
if ( v11 < 0 )
{
v13 = __errno_location();
v14 = strerror(*v13);
fprintf(stderr, "unable to create %s: %s\n", s, v14);
return;
}
while ( 1 )
{
if ( (unsigned int)write(v11, v19, (unsigned int)v8) != v8 )
{
v15 = __errno_location();
v16 = strerror(*v15);
fprintf(stderr, "write: %s\n", v16);
close(v11);
return;
}
v6 -= v8;
if ( !v6 )
break;
v12 = 0x8000;
if ( v6 <= 0x8000 )
v12 = v6;
v8 = read(a3, v19, v12);
if ( v8 <= 0 )
goto LABEL_20;
}
v8 = 0;
LABEL_20:
close(v11);
}
if ( v8 < 0 )
{
v17 = __errno_location();
v18 = strerror(*v17);
fprintf(stderr, "read: %s\n", v18);
}
}
}

original_code:
static void extract_img(const char *image_name, const char *fname,
        int fd, unsigned offset, unsigned size)
{
    char out_fname[1024], buf[32768];
    int fdOut, rd;
    if( size == 0 )
        return;
    if( lseek(fd, offset, 0) < 0 ) {
        fprintf(stderr, "lseek: %s\n", strerror((*__errno_location ())));
        return;
    }
    rd = read(fd, buf, sizeof(buf) > size ? size : sizeof(buf));
    if( rd > 0 ) {
        snprintf(out_fname, sizeof(out_fname), "%s_%s", image_name, fname);
        printf("extracting %s into %s\n", image_name, out_fname);
        if( (fdOut = open(out_fname, 01 | 0100, 0664)) < 0 ) {
            fprintf(stderr, "unable to create %s: %s\n", out_fname,
                    strerror((*__errno_location ())));
            return;
        }
        while( rd > 0 ) {
            if( write(fdOut, buf, rd) != rd ) {
                fprintf(stderr, "write: %s\n", strerror((*__errno_location ())));
                close(fdOut);
                return;
            }
            size -= rd;
            if( size > 0 ) {
                rd = read(fd, buf, sizeof(buf) > size ? size : sizeof(buf));
            }else{
                rd = 0;
            }
        }
        close(fdOut);
    }
    if( rd < 0 ) {
        fprintf(stderr, "read: %s\n", strerror((*__errno_location ())));
    }
}

canonical_original_code:
static void extract_img(char *image_name, char *fname, int fd, unsigned int offset, unsigned int size)
{
    char out_fname[1024]; char buf[32768];
    int fdOut; int rd;
    if( size == 0 )
        return;
    if( lseek(fd, offset, 0) < 0 ) {
        fprintf(stderr, "lseek: %s\n", strerror((*__errno_location ())));
        return;
    }
    rd = read(fd, buf, sizeof(buf) > size ? size : sizeof(buf));
    if( rd > 0 ) {
        snprintf(out_fname, sizeof(out_fname), "%s_%s", image_name, fname);
        printf("extracting %s into %s\n", image_name, out_fname);
        if( (fdOut = open(out_fname, 01 | 0100, 0664)) < 0 ) {
            fprintf(stderr, "unable to create %s: %s\n", out_fname,
                    strerror((*__errno_location ())));
            return;
        }
        while( rd > 0 ) {
            if( write(fdOut, buf, rd) != rd ) {
                fprintf(stderr, "write: %s\n", strerror((*__errno_location ())));
                close(fdOut);
                return;
            }
            size -= rd;
            if( size > 0 ) {
                rd = read(fd, buf, sizeof(buf) > size ? size : sizeof(buf));
            }else{
                rd = 0;
            }
        }
        close(fdOut);
    }
    if( rd < 0 ) {
        fprintf(stderr, "read: %s\n", strerror((*__errno_location ())));
    }
}

variable_types:
fname
char *

variable_types: fd int

variable_types:
offset
unsigned int

variable_types:
size
unsigned int

variable_types:
buf
char [32768]

variable_types: fdOut int

variable_types: rd int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
bool v4; // zf
int v5; // ebx
int v6; // ebp
int v7; // eax
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
int v13[158]; // [rsp+0h] [rbp-278h] BYREF

if ( argc == 1 )
{
printf("usage: %s [-e] <image file>\n", *argv);
puts(" -e - extract kernel and initrd");
v5 = 0;
}
else
{
v3 = argv[1];
v4 = strcmp(v3, "-e") == 0;
v5 = !v4;
if ( v4 )
{
if ( argc <= 2 )
{
puts("file name?");
return 1;
}
v3 = argv[2];
}
v6 = open(v3, 0, argv);
if ( v6 < 0 )
{
v8 = __errno_location();
v9 = strerror(*v8);
fprintf(stderr, "failed to open %s: %s\n", v3, v9);
v5 = 1;
}
else
{
memset(v13, 0, 0x260uLL);
v7 = read(v6, v13, 0x260uLL);
if ( v7 < 0 )
{
v10 = __errno_location();
v11 = strerror(*v10);
fprintf(stderr, "fatal: read failed: %s\n", v11);
v5 = 1;
}
else
{
if ( (unsigned int)v7 <= 0x25F )
printf("warn: header is truncated, file size=%d, header size=%d\n", (unsigned int)v7, 608LL);
if ( memcmp(v13, "ANDROID!", 8uLL) )
puts("warn: file doest not seem to be a fastboot image");
printf(" kernel size (bytes): %u\n", (unsigned int)v13[2]);
printf(" kernel addr: 0x%x\n", (unsigned int)v13[3]);
printf(" ramdisk size (bytes): %u\n", (unsigned int)v13[4]);
printf(" ramdisk addr: 0x%x\n", (unsigned int)v13[5]);
printf(" second size: %u\n", (unsigned int)v13[6]);
printf(" second addr: 0x%x\n", (unsigned int)v13[7]);
printf(" tags addr: 0x%x\n", (unsigned int)v13[8]);
printf(" page size: %u\n", (unsigned int)v13[9]);
printf(" unused1: %u\n", (unsigned int)v13[10]);
printf(" unused2: %u\n", (unsigned int)v13[11]);
printf(" product name: %.*s\n", 16, (const char *)&v13[12]);
printf(" cmdline: %.*s\n", 512, (const char *)&v13[16]);
putchar(10);
if ( v5 )
{
v5 = 0;
}
else if ( v13[9] )
{
extract_img("bImage", v3, v6, v13[9], v13[2]);
extract_img("initrd", v3, v6, v13[2] + 2 * v13[9] - v13[2] % (unsigned int)v13[9], v13[4]);
puts("done.\n");
}
}
}
}
return v5;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
bool v4; // zf
int v5; // ebx
int v6; // ebp
int v7; // eax
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
int v13[158]; // [rsp+0h] [rbp-278h] BYREF

if ( argc == 1 )
{
printf("usage: %s [-e] <image file>\n", *argv);
puts(" -e - extract kernel and initrd");
v5 = 0;
}
else
{
v3 = argv[1];
v4 = strcmp(v3, "-e") == 0;
v5 = !v4;
if ( v4 )
{
if ( argc <= 2 )
{
puts("file name?");
return 1;
}
v3 = argv[2];
}
v6 = open(v3, 0, argv);
if ( v6 < 0 )
{
v8 = __errno_location();
v9 = strerror(*v8);
fprintf(stderr, "failed to open %s: %s\n", v3, v9);
v5 = 1;
}
else
{
memset(v13, 0, 0x260uLL);
v7 = read(v6, v13, 0x260uLL);
if ( v7 < 0 )
{
v10 = __errno_location();
v11 = strerror(*v10);
fprintf(stderr, "fatal: read failed: %s\n", v11);
v5 = 1;
}
else
{
if ( (unsigned int)v7 <= 0x25F )
printf("warn: header is truncated, file size=%d, header size=%d\n", (unsigned int)v7, 608LL);
if ( memcmp(v13, "ANDROID!", 8uLL) )
puts("warn: file doest not seem to be a fastboot image");
printf(" kernel size (bytes): %u\n", (unsigned int)v13[2]);
printf(" kernel addr: 0x%x\n", (unsigned int)v13[3]);
printf(" ramdisk size (bytes): %u\n", (unsigned int)v13[4]);
printf(" ramdisk addr: 0x%x\n", (unsigned int)v13[5]);
printf(" second size: %u\n", (unsigned int)v13[6]);
printf(" second addr: 0x%x\n", (unsigned int)v13[7]);
printf(" tags addr: 0x%x\n", (unsigned int)v13[8]);
printf(" page size: %u\n", (unsigned int)v13[9]);
printf(" unused1: %u\n", (unsigned int)v13[10]);
printf(" unused2: %u\n", (unsigned int)v13[11]);
printf(" product name: %.*s\n", 16, (const char *)&v13[12]);
printf(" cmdline: %.*s\n", 512, (const char *)&v13[16]);
putchar(10);
if ( v5 )
{
v5 = 0;
}
else if ( v13[9] )
{
func7("bImage", v3, v6, v13[9], v13[2]);
func7("initrd", v3, v6, v13[2] + 2 * v13[9] - v13[2] % (unsigned int)v13[9], v13[4]);
puts("done.\n");
}
}
}
}
return v5;
}

original_code:
int main(int argc, char *argv[])
{
    int fd, rd;
    struct fastboot_boot_img_hdr hdr;
    int extract;
    const char *fname;
    if( argc == 1 ) {
        printf("usage: %s [-e] <image file>\n", argv[0]);
        printf("    -e      - extract kernel and initrd\n");
        return 0;
    }
    fname = argv[1];
    if( extract = !strcmp(argv[1], "-e") ) {
        if( argc < 3 ) {
            printf("file name?\n");
            return 1;
        }
        fname = argv[2];
    }
    if( (fd = open(fname, 00)) < 0 ) {
        fprintf(stderr, "failed to open %s: %s\n", fname, strerror((*__errno_location ())));
        return 1;
    }
    memset(&hdr, 0, sizeof(hdr));
    if( (rd = read(fd, &hdr, sizeof(hdr))) < 0 ) {
        fprintf(stderr, "fatal: read failed: %s\n", strerror((*__errno_location ())));
        return 1;
    }
    if( rd < sizeof(hdr) ) {
        printf("warn: header is truncated, file size=%d, header size=%d\n",
                rd, sizeof(hdr));
    }
    if( memcmp(hdr.magic, "ANDROID!", 8) ) {
        printf("warn: file doest not seem to be a fastboot image\n");
    }
 printf("    kernel size (bytes):  %u\n", hdr.kernel_size);
 printf("    kernel addr:          0x%x\n", hdr.kernel_addr);
 printf("    ramdisk size (bytes): %u\n", hdr.ramdisk_size);
 printf("    ramdisk addr:         0x%x\n", hdr.ramdisk_addr);
 printf("    second size:          %u\n", hdr.second_size);
 printf("    second addr:          0x%x\n", hdr.second_addr);
 printf("    tags addr:            0x%x\n", hdr.tags_addr);
 printf("    page size:            %u\n", hdr.page_size);
 printf("    unused1:              %u\n", hdr.unused[0]);
 printf("    unused2:              %u\n", hdr.unused[1]);
 printf("    product name:         %.*s\n", 16, hdr.name);
 printf("    cmdline:              %.*s\n", 512,
            hdr.cmdline);
    printf("\n");
    if( extract && hdr.page_size > 0 ) {
        extract_img("bImage", fname, fd, hdr.page_size, hdr.kernel_size);
        extract_img("initrd", fname, fd, 2 * hdr.page_size + hdr.kernel_size -
                hdr.kernel_size % hdr.page_size, hdr.ramdisk_size);
        printf("done.\n\n");
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int fd; int rd;
    struct fastboot_boot_img_hdr hdr;
    int extract;
    char *fname;
    if( argc == 1 ) {
        printf("usage: %s [-e] <image file>\n", argv[0]);
        printf("    -e      - extract kernel and initrd\n");
        return 0;
    }
    fname = argv[1];
    if( extract = !strcmp(argv[1], "-e") ) {
        if( argc < 3 ) {
            printf("file name?\n");
            return 1;
        }
        fname = argv[2];
    }
    if( (fd = open(fname, 00)) < 0 ) {
        fprintf(stderr, "failed to open %s: %s\n", fname, strerror((*__errno_location ())));
        return 1;
    }
    memset(&hdr, 0, sizeof(hdr));
    if( (rd = read(fd, &hdr, sizeof(hdr))) < 0 ) {
        fprintf(stderr, "fatal: read failed: %s\n", strerror((*__errno_location ())));
        return 1;
    }
    if( rd < sizeof(hdr) ) {
        printf("warn: header is truncated, file size=%d, header size=%d\n",
                rd, sizeof(hdr));
    }
    if( memcmp(hdr.magic, "ANDROID!", 8) ) {
        printf("warn: file doest not seem to be a fastboot image\n");
    }
 printf("    kernel size (bytes):  %u\n", hdr.kernel_size);
 printf("    kernel addr:          0x%x\n", hdr.kernel_addr);
 printf("    ramdisk size (bytes): %u\n", hdr.ramdisk_size);
 printf("    ramdisk addr:         0x%x\n", hdr.ramdisk_addr);
 printf("    second size:          %u\n", hdr.second_size);
 printf("    second addr:          0x%x\n", hdr.second_addr);
 printf("    tags addr:            0x%x\n", hdr.tags_addr);
 printf("    page size:            %u\n", hdr.page_size);
 printf("    unused1:              %u\n", hdr.unused[0]);
 printf("    unused2:              %u\n", hdr.unused[1]);
 printf("    product name:         %.*s\n", 16, hdr.name);
 printf("    cmdline:              %.*s\n", 512,
            hdr.cmdline);
    printf("\n");
    if( extract && hdr.page_size > 0 ) {
        extract_img("bImage", fname, fd, hdr.page_size, hdr.kernel_size);
        extract_img("initrd", fname, fd, 2 * hdr.page_size + hdr.kernel_size -
                hdr.kernel_size % hdr.page_size, hdr.ramdisk_size);
        printf("done.\n\n");
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct fastboot_boot_img_hdr { unsigned char magic[8]; unsigned int kernel_size; unsigned int kernel_addr; unsigned int ramdisk_size; unsigned int ramdisk_addr; unsigned int second_size; unsigned int second_addr; unsigned int tags_addr; unsigned int page_size; unsigned int unused[2]; unsigned char name[16]; unsigned char cmdline[512]; unsigned int id[8]; }

variable_types: extract int


decompiled_code:
int __fastcall disp_getbrightness(int a1)
{
__int64 v2[5]; // [rsp+0h] [rbp-28h] BYREF

v2[0] = 0LL;
return ioctl(a1, 0x143uLL, v2);
}

canonical_decompiled_code:
int __fastcall func8(int a1)
{
__int64 v2[5]; // [rsp+0h] [rbp-28h] BYREF

v2[0] = 0LL;
return ioctl(a1, 0x143uLL, v2);
}

original_code:
static int disp_getbrightness(int disp_fd)
{
    unsigned long arg[4];
    arg[0] = 0;
    return ioctl(disp_fd, DISP_CMD_LCD_GET_BRIGHTNESS, (unsigned long)arg);
}

variable_types:
arg
unsigned long [4]


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
bool v3; // zf
int v4; // ebx
int v5; // eax
int v6; // ebp
int v7; // er12
int v8; // eax
char v9; // dl
int v10; // esi
int v12; // eax
unsigned int v13; // eax

if ( argc <= 1 )
{
v12 = open("/dev/disp", 2, envp);
v6 = v12;
if ( v12 >= 0 )
{
disp_announceversion(v12);
v13 = disp_getbrightness(v6);
v7 = v13;
if ( argc == 1 )
{
printf("brightness: %d\n", v13);
return 0;
}
goto LABEL_5;
}
LABEL_19:
puts("/dev/disp open fail");
return 1;
}
v3 = strcmp(argv[1], "-h") == 0;
v4 = !v3;
if ( v3 )
{
puts("usage: sunxi-disp-brightness [[+|-]<number>]");
return v4;
}
v5 = open("/dev/disp", 2, envp);
v6 = v5;
if ( v5 < 0 )
goto LABEL_19;
disp_announceversion(v5);
v7 = disp_getbrightness(v6);
LABEL_5:
v8 = strtoul(&argv[1][((*argv[1] - 43) & 0xFD) == 0], 0LL, 10);
v9 = *argv[1];
if ( v9 == 43 )
{
v8 += v7;
}
else if ( v9 == 45 )
{
v8 = v7 - v8;
}
if ( v8 > 255 )
v8 = 255;
v10 = 0;
if ( v8 >= 0 )
v10 = v8;
v4 = 0;
if ( v10 != v7 )
disp_setbrightness(v6, v10);
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
bool v3; // zf
int v4; // ebx
int v5; // eax
int v6; // ebp
int v7; // er12
int v8; // eax
char v9; // dl
int v10; // esi
int v12; // eax
unsigned int v13; // eax

if ( argc <= 1 )
{
v12 = open("/dev/disp", 2, envp);
v6 = v12;
if ( v12 >= 0 )
{
func7(v12);
v13 = func8(v6);
v7 = v13;
if ( argc == 1 )
{
printf("brightness: %d\n", v13);
return 0;
}
goto LABEL_5;
}
LABEL_19:
puts("/dev/disp open fail");
return 1;
}
v3 = strcmp(argv[1], "-h") == 0;
v4 = !v3;
if ( v3 )
{
puts("usage: sunxi-disp-brightness [[+|-]<number>]");
return v4;
}
v5 = open("/dev/disp", 2, envp);
v6 = v5;
if ( v5 < 0 )
goto LABEL_19;
func7(v5);
v7 = func8(v6);
LABEL_5:
v8 = strtoul(&argv[1][((*argv[1] - 43) & 0xFD) == 0], 0LL, 10);
v9 = *argv[1];
if ( v9 == 43 )
{
v8 += v7;
}
else if ( v9 == 45 )
{
v8 = v7 - v8;
}
if ( v8 > 255 )
v8 = 255;
v10 = 0;
if ( v8 >= 0 )
v10 = v8;
v4 = 0;
if ( v10 != v7 )
func9(v6, v10);
return v4;
}

original_code:
int main(int argc, char *argv[])
{
    int disp_fd, cur_brightness, new_brightness;
    char *arg;
    if( argc > 1 && !strcmp(argv[1], "-h") ) {
        printf("usage: sunxi-disp-brightness [[+|-]<number>]\n");
        return 0;
    }
    if( (disp_fd = open("/dev/disp", 02)) < 0 ) {
        printf("/dev/disp open fail\n");
        return 1;
    }
    disp_announceversion(disp_fd);
    cur_brightness = disp_getbrightness(disp_fd);
    if(argc == 1 ) {
        printf("brightness: %d\n", cur_brightness);
    }else{
        arg = argv[1];
        if( *arg == '+' || *arg == '-' )
            ++arg;
        new_brightness = strtoul(arg, ((void *)0), 10);
        if( *argv[1] == '+' )
            new_brightness += cur_brightness;
        else if( *argv[1] == '-' )
            new_brightness = cur_brightness - new_brightness;
        if( new_brightness < 0 )
            new_brightness = 0;
        if( new_brightness > 255 )
            new_brightness = 255;
        if( new_brightness != cur_brightness )
            disp_setbrightness(disp_fd, new_brightness);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int disp_fd; int cur_brightness; int new_brightness;
    char *arg;
    if( argc > 1 && !strcmp(argv[1], "-h") ) {
        printf("usage: sunxi-disp-brightness [[+|-]<number>]\n");
        return 0;
    }
    if( (disp_fd = open("/dev/disp", 02)) < 0 ) {
        printf("/dev/disp open fail\n");
        return 1;
    }
    disp_announceversion(disp_fd);
    cur_brightness = disp_getbrightness(disp_fd);
    if(argc == 1 ) {
        printf("brightness: %d\n", cur_brightness);
    }else{
        arg = argv[1];
        if( *arg == '+' || *arg == '-' )
            ++arg;
        new_brightness = strtoul(arg, ((void *)0), 10);
        if( *argv[1] == '+' )
            new_brightness += cur_brightness;
        else if( *argv[1] == '-' )
            new_brightness = cur_brightness - new_brightness;
        if( new_brightness < 0 )
            new_brightness = 0;
        if( new_brightness > 255 )
            new_brightness = 255;
        if( new_brightness != cur_brightness )
            disp_setbrightness(disp_fd, new_brightness);
    }
    return 0;
}

variable_types:
arg
char *


ThreadFun

decompiled_code:
void *__fastcall ThreadFun(void *a1)
{
int v2; // [rsp+Ch] [rbp-3FCh] BYREF
char v3[1016]; // [rsp+10h] [rbp-3F8h] BYREF

while ( !(unsigned int)libusb_bulk_transfer(usb, 129LL, v3, 1000LL, &v2, 0LL) )
printf("response: %.*s\n", v2, v3);
if ( !do_finish )
puts("read error");
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
int v2; // [rsp+Ch] [rbp-3FCh] BYREF
char v3[1016]; // [rsp+10h] [rbp-3F8h] BYREF

while ( !(unsigned int)libusb_bulk_transfer(usb, 129LL, v3, 1000LL, &v2, 0LL) )
printf("response: %.*s\n", v2, v3);
if ( !do_finish )
puts("read error");
return 0LL;
}

original_code:
static void *ThreadFun(void *par)
{
    char buf[1000];
    int transferred;
    while( libusb_bulk_transfer(usb, 1 | LIBUSB_ENDPOINT_IN, buf,
                sizeof(buf), &transferred, 0) == 0)
    {
        printf("response: %.*s\n", transferred, buf);
    }
    if( ! do_finish )
        printf("read error\n");
    return ((void *)0);
}

variable_types:
par
void *

variable_types:
buf
char [1000]

variable_types: transferred int

return_type: void *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
__int64 v4; // rdi
size_t v5; // rax
pthread_t newthread; // [rsp+8h] [rbp-400h] BYREF
char s[1004]; // [rsp+10h] [rbp-3F8h] BYREF
char v9[12]; // [rsp+3FCh] [rbp-Ch] BYREF

if ( (unsigned int)libusb_init(0LL, argv, envp) )
{
puts("libusb_init fail");
return 1;
}
v3 = 0;
v4 = libusb_open_device_with_vid_pid(0LL, 2996LL, 4095LL);
usb = v4;
if ( !v4 )
{
puts("open device failed");
return 1;
}
libusb_claim_interface(v4, 0LL);
pthread_create(&newthread, 0LL, ThreadFun, 0LL);
while ( (unsigned int)gets(s) )
{
v5 = strlen(s);
if ( (unsigned int)libusb_bulk_transfer(usb, 2LL, s, v5, v9, 0LL) )
{
puts("write error");
break;
}
}
do_finish = 1;
libusb_release_interface(usb, 0LL);
libusb_close(usb);
libusb_exit(0LL);
return v3;
}

original_code:
int main(int argc, char *argv[])
{
    int transferred;
    char buf[1000];
    pthread_t thr;
    if( libusb_init(((void *)0)) != 0 ) {
        printf("libusb_init fail\n");
        return 1;
    }
    if( (usb = libusb_open_device_with_vid_pid(((void *)0), 0xbb4, 0xfff)) == ((void *)0) ) {
        printf("open device failed\n");
        return 1;
    }
    libusb_claim_interface(usb, 0);
    pthread_create(&thr, ((void *)0), ThreadFun, ((void *)0));
    while( gets(buf) != ((void *)0) ) {
        if( libusb_bulk_transfer(usb, 2 | LIBUSB_ENDPOINT_OUT,
                    buf, strlen(buf), &transferred, 0) != 0 )
        {
            printf("write error\n");
            break;
        }
    }
    do_finish = 1;
    libusb_release_interface(usb, 0);
    libusb_close(usb);
    libusb_exit(((void *)0));
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int transferred;
    char buf[1000];
    unsigned long thr;
    if( libusb_init(((void *)0)) != 0 ) {
        printf("libusb_init fail\n");
        return 1;
    }
    if( (usb = libusb_open_device_with_vid_pid(((void *)0), 0xbb4, 0xfff)) == ((void *)0) ) {
        printf("open device failed\n");
        return 1;
    }
    libusb_claim_interface(usb, 0);
    pthread_create(&thr, ((void *)0), ThreadFun, ((void *)0));
    while( gets(buf) != ((void *)0) ) {
        if( libusb_bulk_transfer(usb, 2 | LIBUSB_ENDPOINT_OUT,
                    buf, strlen(buf), &transferred, 0) != 0 )
        {
            printf("write error\n");
            break;
        }
    }
    do_finish = 1;
    libusb_release_interface(usb, 0);
    libusb_close(usb);
    libusb_exit(((void *)0));
}

variable_types:
thr
unsigned long


decompiled_code:
__int64 __fastcall round_up(unsigned int a1, unsigned int a2)
{
int v2; // edx
int v3; // esi

v2 = a1 % a2;
v3 = a1 + a2 - a1 % a2;
if ( v2 )
a1 = v3;
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, unsigned int a2)
{
int v2; // edx
int v3; // esi

v2 = a1 % a2;
v3 = a1 + a2 - a1 % a2;
if ( v2 )
a1 = v3;
return a1;
}

original_code:
static unsigned round_up(unsigned val, unsigned round)
{
    if( val % round )
        val += round - val % round;
    return val;
}

canonical_original_code:
static unsigned int round_up(unsigned int val, unsigned int round)
{
    if( val % round )
        val += round - val % round;
    return val;
}

variable_types:
val
unsigned int

variable_types:
round
unsigned int

return_type: unsigned int


setBootImgChecksum

decompiled_code:
__int64 __fastcall setBootImgChecksum(unsigned int *a1, int a2)
{
unsigned int *v2; // rbx
unsigned int v3; // ebp
__int64 result; // rax

a1[3] = _uint32_identity(0x5F0A6C39u);
if ( a2 <= 0 )
{
v3 = 0;
}
else
{
v2 = a1;
v3 = 0;
do
v3 += _uint32_identity(*v2++);
while ( (char *)v2 - (char *)a1 < a2 );
}
result = _uint32_identity(v3);
a1[3] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int *a1, int a2)
{
unsigned int *v2; // rbx
unsigned int v3; // ebp
__int64 result; // rax

a1[3] = func0(0x5F0A6C39u);
if ( a2 <= 0 )
{
v3 = 0;
}
else
{
v2 = a1;
v3 = 0;
do
v3 += func0(*v2++);
while ( (char *)v2 - (char *)a1 < a2 );
}
result = func0(v3);
a1[3] = result;
return result;
}

original_code:
static void setBootImgChecksum(char *image, int imageSize)
{
    uint32_t val = __uint32_identity (0x5F0A6C39);
    unsigned checksum = 0;
    char *addr = image;
    memcpy(image+12, &val, 4);
    while(addr - image < imageSize) {
        memcpy(&val, addr, 4);
        checksum += __uint32_identity (val);
        addr += 4;
    }
    val = __uint32_identity (checksum);
    memcpy(image+12, &val, 4);
}

canonical_original_code:
static void setBootImgChecksum(char *image, int imageSize)
{
    unsigned int val = __uint32_identity (0x5F0A6C39);
    unsigned int checksum = 0;
    char *addr = image;
    memcpy(image+12, &val, 4);
    while(addr - image < imageSize) {
        memcpy(&val, addr, 4);
        checksum += __uint32_identity (val);
        addr += 4;
    }
    val = __uint32_identity (checksum);
    memcpy(image+12, &val, 4);
}

variable_types:
image
char *

variable_types: imageSize int

variable_types:
checksum
unsigned int

variable_types:
addr
char *


decompiled_code:
void __fastcall egon0_embed(void *src, size_t n)
{
int *v2; // rax
int v3; // er12
_DWORD *v4; // rbx
size_t v5; // r14
unsigned int v6; // er13
int v7; // er14
unsigned int v8; // er12
int v9; // ebp
unsigned int size; // [rsp+8h] [rbp-40h]
int size_4; // [rsp+Ch] [rbp-3Ch]

v2 = (int *)get_flashmem_props();
v3 = *v2;
size_4 = v2[2] * *v2;
size = size_4 << 9;
if ( size_4 << 9 < (unsigned int)n )
{
puts("error: boot0 image file too big");
}
else
{
v4 = malloc(size);
memcpy(v4, src, (unsigned int)n);
v5 = (unsigned int)round_up(n, 0x400u);
memset((char *)v4 + (unsigned int)n, -1, v5);
v4[4] = _uint32_identity(v5);
setBootImgChecksum(v4, v5);
v6 = round_up(v5, v3 << 15);
memset((char *)v4 + v5, -1, v6);
v7 = size / v6;
if ( size / v6 > 1 )
{
v8 = v6;
v9 = 1;
do
{
memcpy((char *)v4 + v8, v4, v6);
++v9;
v8 += v6;
}
while ( v7 != v9 );
}
memset((char *)v4 + v7 * v6, -1, size % v6);
write_disk(0LL, 0LL, size_4 & 0x7FFFFF, v4);
free(v4);
}
}

canonical_decompiled_code:
void __fastcall func3(void *src, size_t n)
{
int *v2; // rax
int v3; // er12
_DWORD *v4; // rbx
size_t v5; // r14
unsigned int v6; // er13
int v7; // er14
unsigned int v8; // er12
int v9; // ebp
unsigned int size; // [rsp+8h] [rbp-40h]
int size_4; // [rsp+Ch] [rbp-3Ch]

v2 = (int *)get_flashmem_props();
v3 = *v2;
size_4 = v2[2] * *v2;
size = size_4 << 9;
if ( size_4 << 9 < (unsigned int)n )
{
puts("error: boot0 image file too big");
}
else
{
v4 = malloc(size);
memcpy(v4, src, (unsigned int)n);
v5 = (unsigned int)func1(n, 0x400u);
memset((char *)v4 + (unsigned int)n, -1, v5);
v4[4] = func0(v5);
func2(v4, v5);
v6 = func1(v5, v3 << 15);
memset((char *)v4 + v5, -1, v6);
v7 = size / v6;
if ( size / v6 > 1 )
{
v8 = v6;
v9 = 1;
do
{
memcpy((char *)v4 + v8, v4, v6);
++v9;
v8 += v6;
}
while ( v7 != v9 );
}
memset((char *)v4 + v7 * v6, -1, size % v6);
write_disk(0LL, 0LL, size_4 & 0x7FFFFF, v4);
free(v4);
}
}

original_code:
static void egon0_embed(const char *image, unsigned imageSize)
{
    struct flashmem_properties *props = get_flashmem_props();
    unsigned pageSize = props->areas[FMAREA_BOOT0].sectors_per_page * 512;
    unsigned areaSize = props->areas[FMAREA_BOOT0].page_count * pageSize;
    unsigned i, repCount;
    char *buf;
    uint32_t imgSizeLe;
    if( imageSize > areaSize ) {
        printf("error: boot0 image file too big\n");
        return;
    }
    buf = malloc(areaSize);
    memcpy(buf, image, imageSize);
    memset(buf + imageSize, '\xff', round_up(imageSize, 1024));
    imageSize = round_up(imageSize, 1024);
    imgSizeLe = __uint32_identity (imageSize);
    memcpy(buf + 16, &imgSizeLe, 4);
    setBootImgChecksum(buf, imageSize);
    memset(buf + imageSize, '\xff', round_up(imageSize, 64 * pageSize));
    imageSize = round_up(imageSize, 64 * pageSize);
    repCount = areaSize / imageSize;
    for(i = 1; i < repCount; ++i) {
        memcpy(buf + i * imageSize, buf, imageSize);
    }
    memset(buf + repCount * imageSize, '\xff', areaSize - repCount * imageSize);
    write_disk(FMAREA_BOOT0, 0, areaSize / 512, buf);
    free(buf);
}

canonical_original_code:
static void egon0_embed(char *image, unsigned int imageSize)
{
    struct flashmem_properties *props = get_flashmem_props();
    unsigned int pageSize = props->areas[FMAREA_BOOT0].sectors_per_page * 512;
    unsigned int areaSize = props->areas[FMAREA_BOOT0].page_count * pageSize;
    unsigned int i; unsigned int repCount;
    char *buf;
    unsigned int imgSizeLe;
    if( imageSize > areaSize ) {
        printf("error: boot0 image file too big\n");
        return;
    }
    buf = malloc(areaSize);
    memcpy(buf, image, imageSize);
    memset(buf + imageSize, '\xff', round_up(imageSize, 1024));
    imageSize = round_up(imageSize, 1024);
    imgSizeLe = __uint32_identity (imageSize);
    memcpy(buf + 16, &imgSizeLe, 4);
    setBootImgChecksum(buf, imageSize);
    memset(buf + imageSize, '\xff', round_up(imageSize, 64 * pageSize));
    imageSize = round_up(imageSize, 64 * pageSize);
    repCount = areaSize / imageSize;
    for(i = 1; i < repCount; ++i) {
        memcpy(buf + i * imageSize, buf, imageSize);
    }
    memset(buf + repCount * imageSize, '\xff', areaSize - repCount * imageSize);
    write_disk(FMAREA_BOOT0, 0, areaSize / 512, buf);
    free(buf);
}

variable_types:
imageSize
unsigned int

variable_types: struct flashmem_properties { struct flashmarea_properties { unsigned int sectors_per_page; unsigned int first_pageno; unsigned int page_count; } areas[-1]; unsigned int sectors_per_page; unsigned int pages_per_block; unsigned int blocks_per_chip; unsigned int chip_cnt; unsigned int pagewithbadflag; unsigned int block_cnt_of_zone; } *

variable_types:
pageSize
unsigned int

variable_types:
areaSize
unsigned int

variable_types:
i
unsigned int

variable_types:
repCount
unsigned int

variable_types:
buf
char *

variable_types:
imgSizeLe
unsigned int


strstrip

decompiled_code:
const char *__fastcall strstrip(const char *a1)
{
size_t v1; // rax
const char *i; // rax

v1 = strlen(a1);
if ( v1 )
{
for ( i = &a1[v1 - 1]; a1 != i; --i )
{
if ( (unsigned __int8)(*i - 9) > 4u && *i != 32 )
break;
}
*((_BYTE *)i + 1) = 0;
}
return a1;
}

canonical_decompiled_code:
const char *__fastcall func2(const char *a1)
{
size_t v1; // rax
const char *i; // rax

v1 = strlen(a1);
if ( v1 )
{
for ( i = &a1[v1 - 1]; a1 != i; --i )
{
if ( (unsigned __int8)(*i - 9) > 4u && *i != 32 )
break;
}
*((_BYTE *)i + 1) = 0;
}
return a1;
}

canonical_original_code:
static char *strstrip(char *str)
{
 unsigned long len = strlen(str);
 if (len) {
  char *end = str + len - 1;
  while ((end != str) && (((*end)==' ')||((*end)=='\n')||((*end)=='\t')||((*end)=='\v')||((*end)=='\r')||((*end)=='\f')))
   end--;
  *(end + 1) = '\0';
 }
 return str;
}

variable_types:
str
char *

variable_types:
len
unsigned long

variable_types:
end
char *

return_type: char *


repo: rafkhan/etchy

decompiled_code:
chtype inch_without_color()
{
chtype result; // rax

result = winch(stdscr__NCURSES6_TINFO_5_0_19991023) & 0x7F;
if ( (_DWORD)result == 127 )
result = 32LL;
return result;
}

canonical_decompiled_code:
chtype func7()
{
chtype result; // rax

result = winch(stdscr__NCURSES6_TINFO_5_0_19991023) & 0x7F;
if ( (_DWORD)result == 127 )
result = 32LL;
return result;
}

original_code:
int inch_without_color(void) {
 int ch;
 ch = winch(stdscr) & 127;
 if (ch == 127)
  ch = 32;
 return ch;
}

canonical_original_code:
int inch_without_color() {
 int ch;
 ch = winch(stdscr) & 127;
 if (ch == 127)
  ch = 32;
 return ch;
}

variable_types: ch int


decompiled_code:
int __fastcall draw_color_with_character(__int16 a1, int a2)
{
int v2; // eax

v2 = a2;
if ( !a2 )
v2 = inch_without_color();
return waddch(stdscr__NCURSES6_TINFO_5_0_19991023, v2 | (unsigned int)(unsigned __int16)(a1 << 8));
}

canonical_decompiled_code:
int __fastcall func8(__int16 a1, int a2)
{
int v2; // eax

v2 = a2;
if ( !a2 )
v2 = func7();
return waddch(stdscr__NCURSES6_TINFO_5_0_19991023, v2 | (unsigned int)(unsigned __int16)(a1 << 8));
}

original_code:
void draw_color_with_character(int cpair, int ch) {
 if (ch == 0)
  ch = inch_without_color();
 waddch(stdscr,(ch | (((chtype)(((cpair))) << ((0) + 8)) & ((chtype)((((1U) << 8) - 1U)) << ((0) + 8)))));
}

variable_types: cpair int


init

decompiled_code:
int init()
{
initscr();
keypad(stdscr__NCURSES6_TINFO_5_0_19991023, 1);
cbreak();
noecho();
start_color();
init_pair(1, 7, 0);
init_pair(2, 7, 1);
init_pair(3, 7, 4);
init_pair(4, 7, 2);
init_pair(5, 0, 3);
init_pair(6, 0, 5);
init_pair(7, 0, 6);
return init_pair(8, 0, 7);
}

canonical_decompiled_code:
int func9()
{
initscr();
keypad(stdscr__NCURSES6_TINFO_5_0_19991023, 1);
cbreak();
noecho();
start_color();
init_pair(1, 7, 0);
init_pair(2, 7, 1);
init_pair(3, 7, 4);
init_pair(4, 7, 2);
init_pair(5, 0, 3);
init_pair(6, 0, 5);
init_pair(7, 0, 6);
return init_pair(8, 0, 7);
}

original_code:
void init(void) {
 initscr();
 keypad(stdscr, 1);
 cbreak();
 noecho();
 start_color();
 init_pair(1, 7, 0);
 init_pair(2, 7, 1);
 init_pair(3, 7, 4);
 init_pair(4, 7, 2);
 init_pair(5, 0, 3);
 init_pair(6, 0, 5);
 init_pair(7, 0, 6);
 init_pair(8, 0, 7);
}

canonical_original_code:
void init() {
 initscr();
 keypad(stdscr, 1);
 cbreak();
 noecho();
 start_color();
 init_pair(1, 7, 0);
 init_pair(2, 7, 1);
 init_pair(3, 7, 4);
 init_pair(4, 7, 2);
 init_pair(5, 0, 3);
 init_pair(6, 0, 5);
 init_pair(7, 0, 6);
 init_pair(8, 0, 7);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
int v4; // er13
__int16 v5; // bx
int v6; // er12
int v7; // ebp
int v8; // er13
int v9; // esi

init();
if ( stdscr__NCURSES6_TINFO_5_0_19991023 )
{
v3 = stdscr__NCURSES6_TINFO_5_0_19991023->_maxy + 1;
v4 = stdscr__NCURSES6_TINFO_5_0_19991023->_maxx + 1;
}
else
{
v3 = -1;
v4 = -1;
}
v5 = 1;
v6 = 0;
v7 = 0;
v8 = v4 - 1;
while ( 2 )
{
v9 = wgetch(stdscr__NCURSES6_TINFO_5_0_19991023);
switch ( v9 )
{
case 258:
if ( v3 - 1 > v6 )
{
draw_color_with_character(v5, 0);
wmove(stdscr__NCURSES6_TINFO_5_0_19991023, ++v6, v7);
}
goto LABEL_15;
case 259:
if ( v6 > 0 )
{
draw_color_with_character(v5, 0);
wmove(stdscr__NCURSES6_TINFO_5_0_19991023, --v6, v7);
}
goto LABEL_15;
case 260:
if ( v7 > 0 )
{
draw_color_with_character(v5, 0);
wmove(stdscr__NCURSES6_TINFO_5_0_19991023, v6, --v7);
}
goto LABEL_15;
case 261:
if ( v8 > v7 )
{
draw_color_with_character(v5, 0);
wmove(stdscr__NCURSES6_TINFO_5_0_19991023, v6, ++v7);
}
goto LABEL_15;
case 265:
v5 = 1;
goto LABEL_15;
case 266:
v5 = 2;
goto LABEL_15;
case 267:
v5 = 3;
goto LABEL_15;
case 268:
v5 = 4;
goto LABEL_15;
case 269:
v5 = 5;
goto LABEL_15;
case 270:
v5 = 6;
goto LABEL_15;
case 271:
v5 = 7;
goto LABEL_15;
case 272:
v5 = 8;
goto LABEL_15;
default:
if ( (unsigned int)(v9 - 32) <= 0x5E )
{
if ( v8 > v7 )
{
draw_color_with_character(v5, v9);
wmove(stdscr__NCURSES6_TINFO_5_0_19991023, v6, ++v7);
}
else if ( v3 - 1 > v6 )
{
draw_color_with_character(v5, v9);
wmove(stdscr__NCURSES6_TINFO_5_0_19991023, ++v6, 0);
v7 = 0;
}
goto LABEL_15;
}
if ( v9 == 127 )
{
draw_color_with_character(v5, 32);
goto LABEL_15;
}
if ( v9 != 27 )
{
if ( v9 == 10 )
{
wmove(stdscr__NCURSES6_TINFO_5_0_19991023, ++v6, 0);
v7 = 0;
}
LABEL_15:
wrefresh(stdscr__NCURSES6_TINFO_5_0_19991023);
continue;
}
endwin();
return 0;
}
}
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
int v4; // er13
__int16 v5; // bx
int v6; // er12
int v7; // ebp
int v8; // er13
int v9; // esi

func9();
if ( stdscr__NCURSES6_TINFO_5_0_19991023 )
{
v3 = stdscr__NCURSES6_TINFO_5_0_19991023->_maxy + 1;
v4 = stdscr__NCURSES6_TINFO_5_0_19991023->_maxx + 1;
}
else
{
v3 = -1;
v4 = -1;
}
v5 = 1;
v6 = 0;
v7 = 0;
v8 = v4 - 1;
while ( 2 )
{
v9 = wgetch(stdscr__NCURSES6_TINFO_5_0_19991023);
switch ( v9 )
{
case 258:
if ( v3 - 1 > v6 )
{
func8(v5, 0);
wmove(stdscr__NCURSES6_TINFO_5_0_19991023, ++v6, v7);
}
goto LABEL_15;
case 259:
if ( v6 > 0 )
{
func8(v5, 0);
wmove(stdscr__NCURSES6_TINFO_5_0_19991023, --v6, v7);
}
goto LABEL_15;
case 260:
if ( v7 > 0 )
{
func8(v5, 0);
wmove(stdscr__NCURSES6_TINFO_5_0_19991023, v6, --v7);
}
goto LABEL_15;
case 261:
if ( v8 > v7 )
{
func8(v5, 0);
wmove(stdscr__NCURSES6_TINFO_5_0_19991023, v6, ++v7);
}
goto LABEL_15;
case 265:
v5 = 1;
goto LABEL_15;
case 266:
v5 = 2;
goto LABEL_15;
case 267:
v5 = 3;
goto LABEL_15;
case 268:
v5 = 4;
goto LABEL_15;
case 269:
v5 = 5;
goto LABEL_15;
case 270:
v5 = 6;
goto LABEL_15;
case 271:
v5 = 7;
goto LABEL_15;
case 272:
v5 = 8;
goto LABEL_15;
default:
if ( (unsigned int)(v9 - 32) <= 0x5E )
{
if ( v8 > v7 )
{
func8(v5, v9);
wmove(stdscr__NCURSES6_TINFO_5_0_19991023, v6, ++v7);
}
else if ( v3 - 1 > v6 )
{
func8(v5, v9);
wmove(stdscr__NCURSES6_TINFO_5_0_19991023, ++v6, 0);
v7 = 0;
}
goto LABEL_15;
}
if ( v9 == 127 )
{
func8(v5, 32);
goto LABEL_15;
}
if ( v9 != 27 )
{
if ( v9 == 10 )
{
wmove(stdscr__NCURSES6_TINFO_5_0_19991023, ++v6, 0);
v7 = 0;
}
LABEL_15:
wrefresh(stdscr__NCURSES6_TINFO_5_0_19991023);
continue;
}
endwin();
return 0;
}
}
}

original_code:
int main(void) {
 int ch, xc, yc, max_y, max_x, color;
 color = 1;
 init();
 (max_y = ((0 != (const void *)((stdscr))) ? ((stdscr)->_maxy + 1) : (-1)), max_x = ((0 != (const void *)((stdscr))) ? ((stdscr)->_maxx + 1) : (-1)));
 xc = yc = 0;
 while(1) {
  ch = wgetch(stdscr);
  switch(ch) {
   case 0403:
    if(yc > 0) {
     draw_color_with_character(color, 0);
     wmove(stdscr,(--yc),(xc));
    }
    break;
   case 0402:
    if(yc < max_y - 1) {
     draw_color_with_character(color, 0);
     wmove(stdscr,(++yc),(xc));
    }
    break;
   case 0404:
    if(xc > 0) {
     draw_color_with_character(color, 0);
     wmove(stdscr,(yc),(--xc));
    }
    break;
   case 0405:
    if(xc < max_x - 1) {
     draw_color_with_character(color, 0);
     wmove(stdscr,(yc),(++xc));
    }
    break;
   case (0410 +(1)):color = 1;break;;
   case (0410 +(2)):color = 2;break;;
   case (0410 +(3)):color = 3;break;;
   case (0410 +(4)):color = 4;break;;
   case (0410 +(5)):color = 5;break;;
   case (0410 +(6)):color = 6;break;;
   case (0410 +(7)):color = 7;break;;
   case (0410 +(8)):color = 8;break;;
   default:
    if(ch >= ' ' && ch <= '~') {
     if(xc < max_x - 1) {
      draw_color_with_character(color, ch);
      wmove(stdscr,(yc),(++xc));
     } else if(yc < max_y - 1) {
      draw_color_with_character(color, ch);
      xc = 0;
      wmove(stdscr,(++yc),(xc));
     }
    } else if (ch == 127) {
     draw_color_with_character(color, ' ');
    } else if (ch == 27) {
     endwin();
     return 0;
    } else if (ch == 10) {
     xc = 0;
     wmove(stdscr,(++yc),(xc));
    }
  }
  wrefresh(stdscr);
 }
}

canonical_original_code:
int main() {
 int ch; int xc; int yc; int max_y; int max_x; int color;
 color = 1;
 init();
 (max_y = ((0 != (void *)((stdscr))) ? ((stdscr)->_maxy + 1) : (-1)), max_x = ((0 != (void *)((stdscr))) ? ((stdscr)->_maxx + 1) : (-1)));
 xc = yc = 0;
 while(1) {
  ch = wgetch(stdscr);
  switch(ch) {
   case 0403:
    if(yc > 0) {
     draw_color_with_character(color, 0);
     wmove(stdscr,(--yc),(xc));
    }
    break;
   case 0402:
    if(yc < max_y - 1) {
     draw_color_with_character(color, 0);
     wmove(stdscr,(++yc),(xc));
    }
    break;
   case 0404:
    if(xc > 0) {
     draw_color_with_character(color, 0);
     wmove(stdscr,(yc),(--xc));
    }
    break;
   case 0405:
    if(xc < max_x - 1) {
     draw_color_with_character(color, 0);
     wmove(stdscr,(yc),(++xc));
    }
    break;
   case (0410 +(1)):color = 1;break;;
   case (0410 +(2)):color = 2;break;;
   case (0410 +(3)):color = 3;break;;
   case (0410 +(4)):color = 4;break;;
   case (0410 +(5)):color = 5;break;;
   case (0410 +(6)):color = 6;break;;
   case (0410 +(7)):color = 7;break;;
   case (0410 +(8)):color = 8;break;;
   default:
    if(ch >= ' ' && ch <= '~') {
     if(xc < max_x - 1) {
      draw_color_with_character(color, ch);
      wmove(stdscr,(yc),(++xc));
     } else if(yc < max_y - 1) {
      draw_color_with_character(color, ch);
      xc = 0;
      wmove(stdscr,(++yc),(xc));
     }
    } else if (ch == 127) {
     draw_color_with_character(color, ' ');
    } else if (ch == 27) {
     endwin();
     return 0;
    } else if (ch == 10) {
     xc = 0;
     wmove(stdscr,(++yc),(xc));
    }
  }
  wrefresh(stdscr);
 }
}

variable_types: xc int

variable_types: yc int

variable_types: color int


repo: rahpaere/channel

decompiled_code:
void unload_modules()
{
void **v0; // rbx

v0 = (void **)modules;
if ( modules )
{
do
{
dlclose(v0[2]);
v0 = *(void ***)modules;
free(modules);
modules = v0;
}
while ( v0 );
}
}

canonical_decompiled_code:
void func7()
{
void **v0; // rbx

v0 = (void **)modules;
if ( modules )
{
do
{
dlclose(v0[2]);
v0 = *(void ***)modules;
free(modules);
modules = v0;
}
while ( v0 );
}
}

original_code:
static void unload_modules(void)
{
 struct module *next;
 while (modules) {
  dlclose(modules->handle);
  next = modules->next;
  free(modules);
  modules = next;
 }
}

canonical_original_code:
static void unload_modules()
{
 struct module *next;
 while (modules) {
  dlclose(modules->handle);
  next = modules->next;
  free(modules);
  modules = next;
 }
}

variable_types: struct module { struct module *next; char *name; void *handle; struct channel { void (*send)(struct channel *channel, void *message); } *channel; } *

user_defined_types: struct module { struct module *next; char *name; void *handle; struct channel *channel; }


decompiled_code:
void *__fastcall channel_lookup(char *s)
{
const char *v1; // rbx
void *v2; // rbx
__int64 (*v3)(void); // r13
_QWORD *v4; // r12
int *v6; // rax
char *v7; // rax
char v8[296]; // [rsp+0h] [rbp-128h] BYREF

v1 = (const char *)modules;
if ( modules )
{
while ( strcmp(*((const char **)v1 + 1), s) )
{
v1 = *(const char **)v1;
if ( !v1 )
goto LABEL_4;
}
v2 = (void *)*((_QWORD *)v1 + 3);
}
else
{
LABEL_4:
snprintf(v8, 0x100uLL, "%s.so", s);
v2 = dlopen(v8, 1);
if ( v2 )
{
snprintf(v8, 0x100uLL, "%s_setup", s);
v3 = (__int64 (*)(void))dlsym(v2, v8);
if ( v3 )
{
v4 = malloc(0x20uLL);
if ( v4 )
{
*v4 = modules;
v4[1] = strdup(s);
v4[2] = v2;
v2 = (void *)v3();
v4[3] = v2;
modules = v4;
}
else
{
v6 = __errno_location();
v7 = strerror(*v6);
syslog(4, "%s", v7);
dlclose(v2);
v2 = 0LL;
}
}
else
{
syslog(4, "module %s has no setup function", s);
dlclose(v2);
v2 = 0LL;
}
}
else
{
syslog(4, "unable to load module %s", s);
}
}
return v2;
}

canonical_decompiled_code:
void *__fastcall func8(char *s)
{
const char *v1; // rbx
void *v2; // rbx
__int64 (*v3)(void); // r13
_QWORD *v4; // r12
int *v6; // rax
char *v7; // rax
char v8[296]; // [rsp+0h] [rbp-128h] BYREF

v1 = (const char *)modules;
if ( modules )
{
while ( strcmp(*((const char **)v1 + 1), s) )
{
v1 = *(const char **)v1;
if ( !v1 )
goto LABEL_4;
}
v2 = (void *)*((_QWORD *)v1 + 3);
}
else
{
LABEL_4:
snprintf(v8, 0x100uLL, "%s.so", s);
v2 = dlopen(v8, 1);
if ( v2 )
{
snprintf(v8, 0x100uLL, "%s_setup", s);
v3 = (__int64 (*)(void))dlsym(v2, v8);
if ( v3 )
{
v4 = malloc(0x20uLL);
if ( v4 )
{
*v4 = modules;
v4[1] = strdup(s);
v4[2] = v2;
v2 = (void *)v3();
v4[3] = v2;
modules = v4;
}
else
{
v6 = __errno_location();
v7 = strerror(*v6);
syslog(4, "%s", v7);
dlclose(v2);
v2 = 0LL;
}
}
else
{
syslog(4, "module %s has no setup function", s);
dlclose(v2);
v2 = 0LL;
}
}
else
{
syslog(4, "unable to load module %s", s);
}
}
return v2;
}

original_code:
struct channel *channel_lookup(const char *name)
{
 struct module *module;
 struct channel *(*setup)(void);
 void *handle;
 char str[256];
 for (module = modules; module; module = module->next)
  if (!strcmp(module->name, name))
   return module->channel;
 snprintf(str, sizeof(str), "%s.so", name);
 handle = dlopen(str, 0x00001);
 if (!handle) {
  syslog(4, "unable to load module %s", name);
  return ((void *)0);
 }
 snprintf(str, sizeof(str), "%s_setup", name);
 setup = dlsym(handle, str);
 if (!setup) {
  syslog(4, "module %s has no setup function", name);
  dlclose(handle);
  return ((void *)0);
 }
 module = malloc(sizeof(*module));
 if (!module) {
  syslog(4, "%s", strerror((*__errno_location ())));
  dlclose(handle);
  return ((void *)0);
 }
 module->next = modules;
 module->name = strdup(name);
 module->handle = handle;
 module->channel = setup();
 modules = module;
 return module->channel;
}

canonical_original_code:
struct channel *channel_lookup(char *name)
{
 struct module *module;
 struct channel *(*setup)();
 void *handle;
 char str[256];
 for (module = modules; module; module = module->next)
  if (!strcmp(module->name, name))
   return module->channel;
 snprintf(str, sizeof(str), "%s.so", name);
 handle = dlopen(str, 0x00001);
 if (!handle) {
  syslog(4, "unable to load module %s", name);
  return ((void *)0);
 }
 snprintf(str, sizeof(str), "%s_setup", name);
 setup = dlsym(handle, str);
 if (!setup) {
  syslog(4, "module %s has no setup function", name);
  dlclose(handle);
  return ((void *)0);
 }
 module = malloc(sizeof(*module));
 if (!module) {
  syslog(4, "%s", strerror((*__errno_location ())));
  dlclose(handle);
  return ((void *)0);
 }
 module->next = modules;
 module->name = strdup(name);
 module->handle = handle;
 module->channel = setup();
 modules = module;
 return module->channel;
}

variable_types:
name
char *

variable_types: struct channel { void (*send)(struct channel *channel, void *message); } *(*)()

variable_types:
handle
void *

variable_types:
str
char [256]


decompiled_code:
void null_send()
{
syslog(7, "ignored message on null channel");
}

canonical_decompiled_code:
void func6()
{
syslog(7, "ignored message on null channel");
}

original_code:
static void null_send(struct channel *channel, void *message)
{
 (void)channel;
 (void)message;
 syslog(7, "ignored message on null channel");
}

variable_types: struct channel { void (*send)(struct channel *channel, void *message); } *

variable_types:
message
void *


decompiled_code:
__int64 null_test_setup()
{
void (__fastcall **v0)(_QWORD, _QWORD); // rax

v0 = (void (__fastcall **)(_QWORD, _QWORD))channel_lookup();
(*v0)(v0, 0LL);
syslog(6, "loaded null_test module");
return 0LL;
}

canonical_decompiled_code:
__int64 func6()
{
void (__fastcall **v0)(_QWORD, _QWORD); // rax

v0 = (void (__fastcall **)(_QWORD, _QWORD))channel_lookup();
(*v0)(v0, 0LL);
syslog(6, "loaded null_test module");
return 0LL;
}

original_code:
struct channel *null_test_setup(void)
{
 struct channel *channel;
 channel = channel_lookup("null");
 channel->send(channel, ((void *)0));
 syslog(6, "loaded null_test module");
 return ((void *)0);
}

canonical_original_code:
struct channel *null_test_setup()
{
 struct channel *channel;
 channel = channel_lookup("null");
 channel->send(channel, ((void *)0));
 syslog(6, "loaded null_test module");
 return ((void *)0);
}

return_type: struct channel { void (*send)(struct channel *channel, void *message); } *


repo: rahpaere/dc

decompiled_code:
__int64 __fastcall log_next(const char **a1)
{
int v2; // edi
const char *v3; // rax
unsigned int v4; // er12
unsigned __int64 v5; // rcx
const char *v6; // r15
int v7; // er13
char *v8; // rax
char *v9; // rbp
int v10; // eax
int v11; // eax
__int64 result; // rax
char *v13; // rax
char *v14; // r13

v2 = *((_DWORD *)a1 + 10);
if ( v2 > 0 )
{
close(v2);
*((_DWORD *)a1 + 10) = -1;
}
if ( a1[1] )
{
v3 = a1[4];
v4 = (unsigned int)v3;
v5 = (unsigned __int64)a1[2];
if ( v5 )
v4 = (int)v3 % v5;
v6 = *a1;
v7 = strlen(*a1) + 10;
v8 = (char *)malloc(v7);
v9 = v8;
if ( v8 )
{
v10 = snprintf(v8, v7, "%s%d", v6, v4);
if ( v7 <= v10 )
{
v13 = (char *)realloc(v9, v10 + 1);
v14 = v13;
if ( !v13 )
{
LABEL_12:
free(v9);
return 0xFFFFFFFFLL;
}
sprintf(v13, "%s%d", *a1, v4);
v9 = v14;
}
LABEL_8:
unlink(v9);
v11 = creat(v9, 0x180u);
*((_DWORD *)a1 + 10) = v11;
if ( v11 >= 0 )
{
free(v9);
++a1[4];
a1[3] = 0LL;
return 0LL;
}
goto LABEL_12;
}
result = 0xFFFFFFFFLL;
}
else
{
v9 = strdup(*a1);
if ( v9 )
goto LABEL_8;
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char **a1)
{
int v2; // edi
const char *v3; // rax
unsigned int v4; // er12
unsigned __int64 v5; // rcx
const char *v6; // r15
int v7; // er13
char *v8; // rax
char *v9; // rbp
int v10; // eax
int v11; // eax
__int64 result; // rax
char *v13; // rax
char *v14; // r13

v2 = *((_DWORD *)a1 + 10);
if ( v2 > 0 )
{
close(v2);
*((_DWORD *)a1 + 10) = -1;
}
if ( a1[1] )
{
v3 = a1[4];
v4 = (unsigned int)v3;
v5 = (unsigned __int64)a1[2];
if ( v5 )
v4 = (int)v3 % v5;
v6 = *a1;
v7 = strlen(*a1) + 10;
v8 = (char *)malloc(v7);
v9 = v8;
if ( v8 )
{
v10 = snprintf(v8, v7, "%s%d", v6, v4);
if ( v7 <= v10 )
{
v13 = (char *)realloc(v9, v10 + 1);
v14 = v13;
if ( !v13 )
{
LABEL_12:
free(v9);
return 0xFFFFFFFFLL;
}
sprintf(v13, "%s%d", *a1, v4);
v9 = v14;
}
LABEL_8:
unlink(v9);
v11 = creat(v9, 0x180u);
*((_DWORD *)a1 + 10) = v11;
if ( v11 >= 0 )
{
free(v9);
++a1[4];
a1[3] = 0LL;
return 0LL;
}
goto LABEL_12;
}
result = 0xFFFFFFFFLL;
}
else
{
v9 = strdup(*a1);
if ( v9 )
goto LABEL_8;
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int log_next(struct log *log)
{
 char *filename;
 int olderr;
 if (log->fd > 0) {
  close(log->fd);
  log->fd = -1;
 }
 if (log->maxbytes > 0) {
  char *s;
  int guess;
  int length;
  int index;
  index = log->count;
  if (log->maxcount > 0)
   index %= log->maxcount;
  guess = strlen(log->prefix) + 10;
  s = malloc(guess);
  if (!s)
   return -1;
  length = snprintf(s, guess, "%s%d", log->prefix, index);
  if (length >= guess) {
   filename = realloc(s, length + 1);
   if (!filename) {
    free(s);
    return -1;
   }
   sprintf(filename, "%s%d", log->prefix, index);
  } else {
   filename = s;
  }
 } else {
  filename = strdup(log->prefix);
  if (!filename)
   return -1;
 }
 unlink(filename);
 log->fd = creat(filename, 0600);
 if (log->fd < 0) {
  olderr = (*__errno_location ());
  free(filename);
  (*__errno_location ()) = olderr;
  return -1;
 }
 free(filename);
 log->count++;
 log->bytes = 0;
 return 0;
}

variable_types: struct log { char *prefix; unsigned long maxbytes; unsigned long maxcount; unsigned long bytes; unsigned long count; int fd; } *

variable_types:
filename
char *

variable_types: olderr int

variable_types:
s
char *

variable_types: guess int

variable_types: length int

variable_types: index int


decompiled_code:
unsigned __int64 __fastcall log_write(__int64 a1, __int64 a2, unsigned __int64 a3)
{
unsigned __int64 v4; // rbp
size_t v5; // rdx
size_t v6; // rax
ssize_t v7; // rax
unsigned __int64 v8; // rax
unsigned __int64 v9; // rdx

if ( !a3 )
return 0LL;
v4 = 0LL;
do
{
v5 = a3 - v4;
v6 = *(_QWORD *)(a1 + 8);
if ( v6 && v6 < *(_QWORD *)(a1 + 24) + v5 )
v5 = v6 - *(_QWORD *)(a1 + 24);
v7 = write(*(_DWORD *)(a1 + 40), (const void *)(a2 + v4), v5);
if ( v7 <= 0 )
break;
v4 += v7;
v8 = *(_QWORD *)(a1 + 24) + v7;
*(_QWORD *)(a1 + 24) = v8;
v9 = *(_QWORD *)(a1 + 8);
if ( v8 >= v9 && v9 && (int)log_next((const char **)a1) < 0 )
break;
}
while ( a3 > v4 );
return v4;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(__int64 a1, __int64 a2, unsigned __int64 a3)
{
unsigned __int64 v4; // rbp
size_t v5; // rdx
size_t v6; // rax
ssize_t v7; // rax
unsigned __int64 v8; // rax
unsigned __int64 v9; // rdx

if ( !a3 )
return 0LL;
v4 = 0LL;
do
{
v5 = a3 - v4;
v6 = *(_QWORD *)(a1 + 8);
if ( v6 && v6 < *(_QWORD *)(a1 + 24) + v5 )
v5 = v6 - *(_QWORD *)(a1 + 24);
v7 = write(*(_DWORD *)(a1 + 40), (const void *)(a2 + v4), v5);
if ( v7 <= 0 )
break;
v4 += v7;
v8 = *(_QWORD *)(a1 + 24) + v7;
*(_QWORD *)(a1 + 24) = v8;
v9 = *(_QWORD *)(a1 + 8);
if ( v8 >= v9 && v9 && (int)func0((const char **)a1) < 0 )
break;
}
while ( a3 > v4 );
return v4;
}

original_code:
size_t log_write(struct log *log, char *data, size_t size)
{
 size_t n;
 size_t total = 0;
 ssize_t bytes;
 while (total < size) {
  n = size - total;
  if (log->maxbytes > 0 && log->bytes + n > log->maxbytes)
   n = log->maxbytes - log->bytes;
  bytes = write(log->fd, &data[total], n);
  if (bytes <= 0)
   break;
  total += bytes;
  log->bytes += bytes;
  if (log->maxbytes > 0 && log->bytes >= log->maxbytes) {
   if (log_next(log) < 0)
    break;
  }
 }
 return total;
}

canonical_original_code:
unsigned long log_write(struct log *log, char *data, unsigned long size)
{
 unsigned long n;
 unsigned long total = 0;
 long bytes;
 while (total < size) {
  n = size - total;
  if (log->maxbytes > 0 && log->bytes + n > log->maxbytes)
   n = log->maxbytes - log->bytes;
  bytes = write(log->fd, &data[total], n);
  if (bytes <= 0)
   break;
  total += bytes;
  log->bytes += bytes;
  if (log->maxbytes > 0 && log->bytes >= log->maxbytes) {
   if (log_next(log) < 0)
    break;
  }
 }
 return total;
}

variable_types:
data
char *

variable_types:
size
unsigned long

variable_types:
n
unsigned long

variable_types:
total
unsigned long

variable_types: bytes long

return_type: unsigned long

user_defined_types: struct log { char *prefix; unsigned long maxbytes; unsigned long maxcount; unsigned long bytes; unsigned long count; int fd; }


decompiled_code:
void __fastcall log_stop(void *ptr)
{
int v2; // edi

v2 = *((_DWORD *)ptr + 10);
if ( v2 > 0 )
close(v2);
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
int v2; // edi

v2 = *((_DWORD *)ptr + 10);
if ( v2 > 0 )
close(v2);
free(*(void **)ptr);
free(ptr);
}

original_code:
void log_stop(struct log *log)
{
 if (log->fd > 0)
  close(log->fd);
 free(log->prefix);
 free(log);
}


decompiled_code:
void __fastcall do_copy(int fd, int a2)
{
int v2; // eax
int v3; // ebp
int v4; // ebx
_BYTE v5[1064]; // [rsp+0h] [rbp-428h] BYREF

while ( 1 )
{
v2 = read(fd, v5, 0x400uLL);
v3 = v2;
if ( v2 <= 0 )
break;
do
{
v4 = write(a2, v5, v3);
if ( v4 <= 0 )
perror("do_copy: write");
v3 -= v4;
}
while ( v3 > 0 );
}
if ( v2 < 0 )
perror("do_copy: read");
}

canonical_decompiled_code:
void __fastcall func8(int fd, int a2)
{
int v2; // eax
int v3; // ebp
int v4; // ebx
_BYTE v5[1064]; // [rsp+0h] [rbp-428h] BYREF

while ( 1 )
{
v2 = read(fd, v5, 0x400uLL);
v3 = v2;
if ( v2 <= 0 )
break;
do
{
v4 = write(a2, v5, v3);
if ( v4 <= 0 )
perror("do_copy: write");
v3 -= v4;
}
while ( v3 > 0 );
}
if ( v2 < 0 )
perror("do_copy: read");
}

original_code:
void do_copy(int input, int output){
 char buf[1024];
 int n;
 while ((n = read(input, buf, sizeof(buf))) > 0) {
  char *p = buf;
  while (n > 0) {
   int m = write(output, buf, n);
   if (m <= 0) {
    perror("do_copy: write");
   }
   p += m;
   n -= m;
  }
 }
 if (n < 0) {
  perror("do_copy: read");
 }
}

variable_types: input int

variable_types: output int

variable_types:
buf
char [1024]

variable_types: n int

variable_types:
p
char *

variable_types: m int


decompiled_code:
__int64 __fastcall do_connect(char *s, __int16 a2)
{
struct hostent *v2; // rax
int v3; // eax
unsigned int v4; // ebx
struct sockaddr v6; // [rsp+0h] [rbp-28h] BYREF

v2 = gethostbyname(s);
if ( !v2 )
{
fprintf(stderr, "can't lookup %s\n", s);
exit(1);
}
v6.sa_family = v2->h_addrtype;
*(_DWORD *)&v6.sa_data[2] = **(_DWORD **)v2->h_addr_list;
*(_WORD *)v6.sa_data = _bswap_16(a2);
v3 = socket(2, 1, 0);
v4 = v3;
if ( v3 < 0 )
{
perror("socket");
exit(1);
}
if ( connect(v3, &v6, 0x10u) < 0 )
{
perror(s);
exit(1);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func9(char *s, __int16 a2)
{
struct hostent *v2; // rax
int v3; // eax
unsigned int v4; // ebx
struct sockaddr v6; // [rsp+0h] [rbp-28h] BYREF

v2 = gethostbyname(s);
if ( !v2 )
{
fprintf(stderr, "can't lookup %s\n", s);
exit(1);
}
v6.sa_family = v2->h_addrtype;
*(_DWORD *)&v6.sa_data[2] = **(_DWORD **)v2->h_addr_list;
*(_WORD *)v6.sa_data = func7(a2);
v3 = socket(2, 1, 0);
v4 = v3;
if ( v3 < 0 )
{
perror("socket");
exit(1);
}
if ( connect(v3, &v6, 0x10u) < 0 )
{
perror(s);
exit(1);
}
return v4;
}

original_code:
int do_connect(char *peer, int port){
 struct hostent *h;
 if ((h = gethostbyname(peer)) == 0) {
  fprintf(stderr, "can't lookup %s\n", peer);
  exit(1);
 }
 struct sockaddr_in addr;
 addr.sin_family = 2;
 addr.sin_family = h->h_addrtype;
 addr.sin_addr = * (struct in_addr *) *h->h_addr_list;
 addr.sin_port = htons(port);
 int s;
 if ((s = socket(2, SOCK_STREAM, 0)) < 0) {
  perror("socket");
  exit(1);
 }
 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  perror(peer);
  exit(1);
 }
 return s;
}

variable_types:
peer
char *

variable_types: port int

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

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types: s int

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

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


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v3; // r12
__int16 v4; // bp
int v5; // ebx
int v6; // eax

v3 = atoi(argv[1]);
v4 = atoi(argv[2]);
v5 = do_connect("localhost", v3);
v6 = do_connect("localhost", v4);
do_copy(v5, v6);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v3; // r12
__int16 v4; // bp
int v5; // ebx
int v6; // eax

v3 = atoi(argv[1]);
v4 = atoi(argv[2]);
v5 = func9("localhost", v3);
v6 = func9("localhost", v4);
func8(v5, v6);
return 0;
}

original_code:
int main(int argc, char *argv[]){
 int inport = atoi(argv[1]);
 int outport = atoi(argv[2]);
 int input = do_connect("localhost", inport);
 int output = do_connect("localhost", outport);
 (void)argc;
 do_copy(input, output);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: inport int

variable_types: outport int


decompiled_code:
char *__fastcall parse_arguments(__int64 a1, int a2, char *const *a3)
{
int v4; // eax
int v5; // eax
int v6; // eax
int v7; // eax
__int64 v8; // rdx
char *result; // rax

*(char **)a1 = *a3;
while ( 1 )
{
v4 = getopt(a2, a3, "l:n:s:");
if ( v4 == -1 )
break;
switch ( v4 )
{
case 'n':
v6 = atoi(optarg);
*(_DWORD *)(a1 + 20) = v6;
if ( v6 <= 0 )
usage((const char **)a1);
break;
case 's':
v5 = atoi(optarg);
*(_DWORD *)(a1 + 16) = v5;
if ( v5 <= 0 )
usage((const char **)a1);
break;
case 'l':
*(_QWORD *)(a1 + 8) = optarg;
break;
default:
usage((const char **)a1);
}
}
v7 = optind;
if ( a2 - optind != 5 )
usage((const char **)a1);
++optind;
v8 = v7;
*(char **)(a1 + 24) = a3[v8];
optind = v7 + 2;
*(char **)(a1 + 32) = a3[v8 + 1];
optind = v7 + 3;
*(char **)(a1 + 56) = a3[v8 + 2];
optind = v7 + 4;
*(char **)(a1 + 40) = a3[v8 + 3];
optind = v7 + 5;
result = a3[v8 + 4];
*(_QWORD *)(a1 + 48) = result;
return result;
}

canonical_decompiled_code:
char *__fastcall func1(__int64 a1, int a2, char *const *a3)
{
int v4; // eax
int v5; // eax
int v6; // eax
int v7; // eax
__int64 v8; // rdx
char *result; // rax

*(char **)a1 = *a3;
while ( 1 )
{
v4 = getopt(a2, a3, "l:n:s:");
if ( v4 == -1 )
break;
switch ( v4 )
{
case 'n':
v6 = atoi(optarg);
*(_DWORD *)(a1 + 20) = v6;
if ( v6 <= 0 )
func0((const char **)a1);
break;
case 's':
v5 = atoi(optarg);
*(_DWORD *)(a1 + 16) = v5;
if ( v5 <= 0 )
func0((const char **)a1);
break;
case 'l':
*(_QWORD *)(a1 + 8) = optarg;
break;
default:
func0((const char **)a1);
}
}
v7 = optind;
if ( a2 - optind != 5 )
func0((const char **)a1);
++optind;
v8 = v7;
*(char **)(a1 + 24) = a3[v8];
optind = v7 + 2;
*(char **)(a1 + 32) = a3[v8 + 1];
optind = v7 + 3;
*(char **)(a1 + 56) = a3[v8 + 2];
optind = v7 + 4;
*(char **)(a1 + 40) = a3[v8 + 3];
optind = v7 + 5;
result = a3[v8 + 4];
*(_QWORD *)(a1 + 48) = result;
return result;
}

original_code:
static void parse_arguments(struct arguments *args, int argc, char **argv)
{
 int c;
 args->name = argv[0];
 while ((c = getopt(argc, argv, "l:n:s:")) != -1)
  switch (c) {
  case 'l':
   args->logprefix = optarg;
   break;
  case 's':
   args->logbytes = atoi(optarg);
   if (args->logbytes <= 0)
    usage(args);
   break;
  case 'n':
   args->logcount = atoi(optarg);
   if (args->logcount <= 0)
    usage(args);
   break;
  default:
   usage(args);
  }
 if (argc - optind != 5)
  usage(args);
 args->pullhost = argv[optind++];
 args->pullport = argv[optind++];
 args->id = argv[optind++];
 args->pushhost = argv[optind++];
 args->pushport = argv[optind++];
}

variable_types: struct arguments { char *name; char *logprefix; int logbytes; int logcount; char *pullhost; char *pullport; char *pushhost; char *pushport; char *id; } *

variable_types:
argv
char **

variable_types: c int


decompiled_code:
__int64 __fastcall resolve_address(void *dest, char *name, char *service)
{
unsigned int v3; // ebx
struct addrinfo *v5; // r12
struct addrinfo *pai; // [rsp+8h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-48h] BYREF

*(_QWORD *)&req.ai_flags = 0x200000000LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
req.ai_socktype = 1;
req.ai_protocol = 6;
v3 = getaddrinfo(name, service, &req, &pai);
if ( !v3 )
{
v5 = pai;
memcpy(dest, pai->ai_addr, pai->ai_addrlen);
freeaddrinfo(v5);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *dest, char *name, char *service)
{
unsigned int v3; // ebx
struct addrinfo *v5; // r12
struct addrinfo *pai; // [rsp+8h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+10h] [rbp-48h] BYREF

*(_QWORD *)&req.ai_flags = 0x200000000LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
req.ai_socktype = 1;
req.ai_protocol = 6;
v3 = getaddrinfo(name, service, &req, &pai);
if ( !v3 )
{
v5 = pai;
memcpy(dest, pai->ai_addr, pai->ai_addrlen);
freeaddrinfo(v5);
}
return v3;
}

original_code:
static int resolve_address(struct sockaddr_in *addr, const char *host,
      const char *port)
{
 struct addrinfo hints;
 struct addrinfo *ai;
 int err;
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = 2;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_protocol = IPPROTO_TCP;
 err = getaddrinfo(host, port, &hints, &ai);
 if (err)
  return err;
 memcpy(addr, ai->ai_addr, ai->ai_addrlen);
 freeaddrinfo(ai);
 return 0;
}

canonical_original_code:
static int resolve_address(struct sockaddr_in *addr, char *host, char *port)
{
 struct addrinfo hints;
 struct addrinfo *ai;
 int err;
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = 2;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_protocol = IPPROTO_TCP;
 err = getaddrinfo(host, port, &hints, &ai);
 if (err)
  return err;
 memcpy(addr, ai->ai_addr, ai->ai_addrlen);
 freeaddrinfo(ai);
 return 0;
}

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *

variable_types:
host
char *

variable_types:
port
char *

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types: err int

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


decompiled_code:
__int64 __fastcall connect_to_peer(struct sockaddr *addr, __int16 a2)
{
int v2; // eax
unsigned int v3; // ebx
struct sockaddr v5; // [rsp+0h] [rbp-38h] BYREF
int optval[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

optval[0] = 1;
v2 = socket(2, 1, 6);
if ( v2 < 0 )
return (unsigned int)-1;
v3 = v2;
if ( a2
&& (v5.sa_family = 2,
*(_DWORD *)&v5.sa_data[2] = _bswap_32(0),
*(_WORD *)v5.sa_data = _bswap_16(a2),
setsockopt(v3, 1, 2, optval, 4u),
bind(v3, &v5, 0x10u) < 0)
|| connect(v3, addr, 0x10u) < 0 )
{
close(v3);
v3 = -1;
}
else
{
setsockopt(v3, 6, 1, optval, 4u);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(struct sockaddr *addr, __int16 a2)
{
int v2; // eax
unsigned int v3; // ebx
struct sockaddr v5; // [rsp+0h] [rbp-38h] BYREF
int optval[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

optval[0] = 1;
v2 = socket(2, 1, 6);
if ( v2 < 0 )
return (unsigned int)-1;
v3 = v2;
if ( a2
&& (v5.sa_family = 2,
*(_DWORD *)&v5.sa_data[2] = func3(0),
*(_WORD *)v5.sa_data = func4(a2),
setsockopt(v3, 1, 2, optval, 4u),
bind(v3, &v5, 0x10u) < 0)
|| connect(v3, addr, 0x10u) < 0 )
{
close(v3);
v3 = -1;
}
else
{
setsockopt(v3, 6, 1, optval, 4u);
}
return v3;
}

original_code:
static int connect_to_peer(struct sockaddr_in *peeraddr, uint16_t bindport)
{
 int s;
 int yes = 1;
 struct sockaddr_in self;
 s = socket(2, SOCK_STREAM, IPPROTO_TCP);
 if (s < 0)
  return -1;
 if (bindport) {
  self.sin_family = 2;
  self.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
  self.sin_port = htons(bindport);
  setsockopt(s, 1, 2, &yes, sizeof(yes));
  if (bind(s, (struct sockaddr *)&self, sizeof(self)) < 0) {
   close(s);
   return -1;
  }
 }
 if (connect(s, (struct sockaddr *)peeraddr, sizeof(*peeraddr)) < 0) {
  close(s);
  return -1;
 }
 setsockopt(s, IPPROTO_TCP, 1, &yes, sizeof(yes));
 return s;
}

canonical_original_code:
static int connect_to_peer(struct sockaddr_in *peeraddr, unsigned short bindport)
{
 int s;
 int yes = 1;
 struct sockaddr_in self;
 s = socket(2, SOCK_STREAM, IPPROTO_TCP);
 if (s < 0)
  return -1;
 if (bindport) {
  self.sin_family = 2;
  self.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
  self.sin_port = htons(bindport);
  setsockopt(s, 1, 2, &yes, sizeof(yes));
  if (bind(s, (struct sockaddr *)&self, sizeof(self)) < 0) {
   close(s);
   return -1;
  }
 }
 if (connect(s, (struct sockaddr *)peeraddr, sizeof(*peeraddr)) < 0) {
  close(s);
  return -1;
 }
 setsockopt(s, IPPROTO_TCP, 1, &yes, sizeof(yes));
 return s;
}

variable_types:
bindport
unsigned short

variable_types: yes int


decompiled_code:
int __fastcall get_args(int argc, char **argv)
{
int result; // eax
__int64 v3; // rax

prog_args = (__int64)*argv;
while ( 1 )
{
result = getopt(argc, argv, "p:");
if ( result == -1 )
break;
if ( result != 112 )
usage();
if ( *(&prog_args + 1) )
{
fprintf(stderr, "%s: can specify only one port\n", (const char *)prog_args);
exit(1);
}
v3 = optarg;
*(&prog_args + 1) = optarg;
if ( !v3 )
{
fprintf(stderr, "%s: -p takes a port argument\n", 0LL);
exit(1);
}
}
if ( optind != argc )
usage();
return result;
}

canonical_decompiled_code:
int __fastcall func8(int argc, char **argv)
{
int result; // eax
__int64 v3; // rax

prog_args = (__int64)*argv;
while ( 1 )
{
result = getopt(argc, argv, "p:");
if ( result == -1 )
break;
if ( result != 112 )
func7();
if ( *(&prog_args + 1) )
{
fprintf(stderr, "%s: can specify only one port\n", (const char *)prog_args);
exit(1);
}
v3 = optarg;
*(&prog_args + 1) = optarg;
if ( !v3 )
{
fprintf(stderr, "%s: -p takes a port argument\n", 0LL);
exit(1);
}
}
if ( optind != argc )
func7();
return result;
}

original_code:
static void get_args(int argc, char *argv[]){
 prog_args.name = argv[0];
 int c;
 while ((c = getopt(argc, argv, "p:")) != -1) {
  switch (c) {
   case 'p':
    if (prog_args.port != 0) {
     fprintf(stderr, "%s: can specify only one port\n", prog_args.name);
     exit(1);
    }
    if ((prog_args.port = optarg) == 0) {
     fprintf(stderr, "%s: -p takes a port argument\n", prog_args.port);
     exit(1);
    }
    break;
   case '?':
   default:
    usage();
  }
 }
 if (argc - optind != 0) {
  usage();
 }
}


decompiled_code:
__int64 __fastcall do_copy(int a1)
{
FILE *v1; // rbp
int v2; // eax
__int64 v3; // rax
void *v4; // rbx
_BYTE v6[72]; // [rsp+0h] [rbp-48h] BYREF

v1 = fdopen(a1, "r");
if ( !v1 )
{
fprintf(stderr, "%s: fdopen failed\n", (const char *)prog_args);
exit(1);
}
while ( !feof(v1) )
{
v2 = fread(v6, 0x2AuLL, 1uLL, v1);
if ( !v2 )
break;
if ( v2 < 0 )
{
perror("do_copy: fread");
exit(1);
}
v3 = get_c37_packet(v6);
v4 = (void *)v3;
if ( !v3 )
{
fprintf(stderr, "%s: do_copy: bad packet\n", (const char *)prog_args);
exit(1);
}
if ( *(_WORD *)(v3 + 2) != 42 )
{
fprintf(stderr, "%s: do_copy: bad frame size\n", (const char *)prog_args);
exit(1);
}
write_c37_packet_readable(_bss_start);
free(v4);
}
fclose(v1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1)
{
FILE *v1; // rbp
int v2; // eax
__int64 v3; // rax
void *v4; // rbx
_BYTE v6[72]; // [rsp+0h] [rbp-48h] BYREF

v1 = fdopen(a1, "r");
if ( !v1 )
{
fprintf(stderr, "%s: fdopen failed\n", (const char *)prog_args);
exit(1);
}
while ( !feof(v1) )
{
v2 = fread(v6, 0x2AuLL, 1uLL, v1);
if ( !v2 )
break;
if ( v2 < 0 )
{
perror("do_copy: fread");
exit(1);
}
v3 = func14(v6);
v4 = (void *)v3;
if ( !v3 )
{
fprintf(stderr, "%s: do_copy: bad packet\n", (const char *)prog_args);
exit(1);
}
if ( *(_WORD *)(v3 + 2) != 42 )
{
fprintf(stderr, "%s: do_copy: bad frame size\n", (const char *)prog_args);
exit(1);
}
func18(_bss_start);
free(v4);
}
fclose(v1);
return 1LL;
}

canonical_original_code:
void do_copy(int input, int output){
 char buf[1024];
 int n;
 while ((n = read(input, buf, sizeof(buf))) > 0) {
  char *p = buf;
  while (n > 0) {
   int m = write(output, buf, n);
   if (m <= 0) {
    perror("do_copy: write");
   }
   p += m;
   n -= m;
  }
 }
 if (n < 0) {
  perror("do_copy: read");
 }
}

variable_types: input int

variable_types: output int

variable_types:
buf
char [1024]

variable_types: n int

variable_types:
p
char *

variable_types: m int