decompiled_code:
int __fastcall mobi_header_print(unsigned __int16 *a1)
{
__int16 v1; // ax
const char *v2; // rax
unsigned int v3; // eax
int v4; // eax
__int64 v5; // rsi
__int64 v6; // rsi
__int64 v7; // rsi
__int64 v8; // rsi
__int64 i; // rbp
__int64 v10; // rdx
const char *v11; // rsi
__int64 v12; // rsi

puts("PalmDOC header");
printf(" Compression: %d", *a1);
v1 = *a1;
if ( *a1 == 2 )
{
printf(" (PalmDOC compression)");
}
else if ( v1 == 17480 )
{
printf(" (HUFF/CDC compression)");
}
else if ( v1 == 1 )
{
printf(" (no compression)");
}
else
{
printf(" (Unknown compression)");
}
putchar(10);
printf(" Uncompressed text length: %d\n", *((unsigned int *)a1 + 1));
printf(" Record count: %d\n", a1[4]);
printf(" Record size: %d\n", a1[5]);
printf(" Encryptin type: %d\n", a1[6]);
puts("MOBI header");
v2 = (const char *)id2string(*((unsigned int *)a1 + 4));
printf(" MOBI ID: %08x (%s)\n", *((unsigned int *)a1 + 4), v2);
printf(" MOBI header length: %d\n", *((unsigned int *)a1 + 5));
printf(" Type %08x ", *((unsigned int *)a1 + 6));
v3 = *((_DWORD *)a1 + 6);
if ( v3 > 0x103 )
{
switch ( v3 )
{
case 0x201u:
puts("(PICS)");
break;
case 0x202u:
puts("(WORD)");
break;
case 0x203u:
puts("(XLS)");
break;
case 0x204u:
puts("(PPT)");
break;
case 0x205u:
puts("(TEXT)");
break;
case 0x206u:
puts("(HTML)");
break;
default:
goto LABEL_43;
}
}
else if ( v3 > 0xE7 )
{
switch ( v3 )
{
case 0xE8u:
puts("(mobipocket generated by kindlegen1.2)");
break;
case 0xF8u:
puts("(KF8: generated by kindlegen2)");
break;
case 0x101u:
puts("(News)");
break;
case 0x102u:
puts("(News_Feed)");
break;
case 0x103u:
puts("(News_Magazine)");
break;
default:
goto LABEL_43;
}
}
else
{
switch ( v3 )
{
case 3u:
puts("(PalmDoc Book)");
break;
case 4u:
puts("(Audio)");
break;
case 2u:
puts("(Mobipocket Book)");
break;
default:
LABEL_43:
puts("(unknown)");
break;
}
}
printf(" Text encoding: %d", *((unsigned int *)a1 + 7));
v4 = *((_DWORD *)a1 + 7);
if ( v4 == 1252 )
{
puts(" (CP1252)");
}
else if ( v4 == 65001 )
{
puts(" (UTF-8)");
}
else
{
puts(" (unknown)");
}
printf(" UniqID: %08x\n", *((unsigned int *)a1 + 8));
printf(" File version: %d\n", *((unsigned int *)a1 + 9));
v5 = *((unsigned int *)a1 + 10);
if ( (_DWORD)v5 != -1 )
printf(" Ortographic index: %d\n", v5);
v6 = *((unsigned int *)a1 + 11);
if ( (_DWORD)v6 != -1 )
printf(" Inflection index: %d\n", v6);
v7 = *((unsigned int *)a1 + 12);
if ( (_DWORD)v7 != -1 )
printf(" Index names: %d\n", v7);
v8 = *((unsigned int *)a1 + 13);
if ( (_DWORD)v8 != -1 )
printf(" Index keys: %d\n", v8);
for ( i = 0LL; i != 6; ++i )
{
v10 = *(unsigned int *)&a1[2 * i + 28];
if ( (_DWORD)v10 != -1 )
printf(" Extra Index #%d: %d\n", (unsigned int)i, v10);
}
printf(" First non-book index: %d\n", *((unsigned int *)a1 + 20));
printf(" Full name offset: %d\n", *((unsigned int *)a1 + 21));
printf(" Full name length: %d\n", *((unsigned int *)a1 + 22));
printf(" Locale: %d\n", *((unsigned int *)a1 + 23));
printf(" Dict input length: %d\n", *((unsigned int *)a1 + 24));
printf(" Dict output length: %d\n", *((unsigned int *)a1 + 25));
printf(" Min version: %d\n", *((unsigned int *)a1 + 26));
printf(" First image index: %d\n", *((unsigned int *)a1 + 27));
printf(" Huffman record offset: %d\n", *((unsigned int *)a1 + 28));
printf(" Huffman record count: %d\n", *((unsigned int *)a1 + 29));
printf(" Huffman table offset: %d\n", *((unsigned int *)a1 + 30));
printf(" Huffman table count: %d\n", *((unsigned int *)a1 + 31));
v11 = "EXTH present";
if ( (*((_DWORD *)a1 + 32) & 0x40) == 0 )
v11 = "No EXTH";
printf(" EXTH flags: %s (0x%08x)\n", v11);
printf(" DRM offset: %d\n", *((unsigned int *)a1 + 33));
printf(" DRM count: %d\n", *((unsigned int *)a1 + 34));
printf(" DRM size: %d\n", *((unsigned int *)a1 + 35));
printf(" DRM flags: %08x\n", *((unsigned int *)a1 + 36));
printf(" First content record #: %d\n", *((unsigned int *)a1 + 37));
printf(" Last content record #: %d\n", *((unsigned int *)a1 + 38));
printf(" FCIS record #: %d\n", *((unsigned int *)a1 + 39));
printf(" FLIS record #: %d\n", *((unsigned int *)a1 + 40));
printf(" Extra record data flags:");
v12 = *((unsigned int *)a1 + 41);
if ( (v12 & 1) != 0 )
{
printf(" multibyte");
v12 = *((unsigned int *)a1 + 41);
}
if ( (v12 & 2) != 0 )
{
printf(" tbs_index");
v12 = *((unsigned int *)a1 + 41);
}
if ( (v12 & 4) != 0 )
{
printf(" uncrossable_breaks");
v12 = *((unsigned int *)a1 + 41);
}
printf(" (0x%08x)\n", v12);
return printf(" INDX record offset: %d\n", *((unsigned int *)a1 + 42));
}

canonical_decompiled_code:
int __fastcall func4(unsigned __int16 *a1)
{
__int16 v1; // ax
const char *v2; // rax
unsigned int v3; // eax
int v4; // eax
__int64 v5; // rsi
__int64 v6; // rsi
__int64 v7; // rsi
__int64 v8; // rsi
__int64 i; // rbp
__int64 v10; // rdx
const char *v11; // rsi
__int64 v12; // rsi

puts("PalmDOC header");
printf(" Compression: %d", *a1);
v1 = *a1;
if ( *a1 == 2 )
{
printf(" (PalmDOC compression)");
}
else if ( v1 == 17480 )
{
printf(" (HUFF/CDC compression)");
}
else if ( v1 == 1 )
{
printf(" (no compression)");
}
else
{
printf(" (Unknown compression)");
}
putchar(10);
printf(" Uncompressed text length: %d\n", *((unsigned int *)a1 + 1));
printf(" Record count: %d\n", a1[4]);
printf(" Record size: %d\n", a1[5]);
printf(" Encryptin type: %d\n", a1[6]);
puts("MOBI header");
v2 = (const char *)id2string(*((unsigned int *)a1 + 4));
printf(" MOBI ID: %08x (%s)\n", *((unsigned int *)a1 + 4), v2);
printf(" MOBI header length: %d\n", *((unsigned int *)a1 + 5));
printf(" Type %08x ", *((unsigned int *)a1 + 6));
v3 = *((_DWORD *)a1 + 6);
if ( v3 > 0x103 )
{
switch ( v3 )
{
case 0x201u:
puts("(PICS)");
break;
case 0x202u:
puts("(WORD)");
break;
case 0x203u:
puts("(XLS)");
break;
case 0x204u:
puts("(PPT)");
break;
case 0x205u:
puts("(TEXT)");
break;
case 0x206u:
puts("(HTML)");
break;
default:
goto LABEL_43;
}
}
else if ( v3 > 0xE7 )
{
switch ( v3 )
{
case 0xE8u:
puts("(mobipocket generated by kindlegen1.2)");
break;
case 0xF8u:
puts("(KF8: generated by kindlegen2)");
break;
case 0x101u:
puts("(News)");
break;
case 0x102u:
puts("(News_Feed)");
break;
case 0x103u:
puts("(News_Magazine)");
break;
default:
goto LABEL_43;
}
}
else
{
switch ( v3 )
{
case 3u:
puts("(PalmDoc Book)");
break;
case 4u:
puts("(Audio)");
break;
case 2u:
puts("(Mobipocket Book)");
break;
default:
LABEL_43:
puts("(unknown)");
break;
}
}
printf(" Text encoding: %d", *((unsigned int *)a1 + 7));
v4 = *((_DWORD *)a1 + 7);
if ( v4 == 1252 )
{
puts(" (CP1252)");
}
else if ( v4 == 65001 )
{
puts(" (UTF-8)");
}
else
{
puts(" (unknown)");
}
printf(" UniqID: %08x\n", *((unsigned int *)a1 + 8));
printf(" File version: %d\n", *((unsigned int *)a1 + 9));
v5 = *((unsigned int *)a1 + 10);
if ( (_DWORD)v5 != -1 )
printf(" Ortographic index: %d\n", v5);
v6 = *((unsigned int *)a1 + 11);
if ( (_DWORD)v6 != -1 )
printf(" Inflection index: %d\n", v6);
v7 = *((unsigned int *)a1 + 12);
if ( (_DWORD)v7 != -1 )
printf(" Index names: %d\n", v7);
v8 = *((unsigned int *)a1 + 13);
if ( (_DWORD)v8 != -1 )
printf(" Index keys: %d\n", v8);
for ( i = 0LL; i != 6; ++i )
{
v10 = *(unsigned int *)&a1[2 * i + 28];
if ( (_DWORD)v10 != -1 )
printf(" Extra Index #%d: %d\n", (unsigned int)i, v10);
}
printf(" First non-book index: %d\n", *((unsigned int *)a1 + 20));
printf(" Full name offset: %d\n", *((unsigned int *)a1 + 21));
printf(" Full name length: %d\n", *((unsigned int *)a1 + 22));
printf(" Locale: %d\n", *((unsigned int *)a1 + 23));
printf(" Dict input length: %d\n", *((unsigned int *)a1 + 24));
printf(" Dict output length: %d\n", *((unsigned int *)a1 + 25));
printf(" Min version: %d\n", *((unsigned int *)a1 + 26));
printf(" First image index: %d\n", *((unsigned int *)a1 + 27));
printf(" Huffman record offset: %d\n", *((unsigned int *)a1 + 28));
printf(" Huffman record count: %d\n", *((unsigned int *)a1 + 29));
printf(" Huffman table offset: %d\n", *((unsigned int *)a1 + 30));
printf(" Huffman table count: %d\n", *((unsigned int *)a1 + 31));
v11 = "EXTH present";
if ( (*((_DWORD *)a1 + 32) & 0x40) == 0 )
v11 = "No EXTH";
printf(" EXTH flags: %s (0x%08x)\n", v11);
printf(" DRM offset: %d\n", *((unsigned int *)a1 + 33));
printf(" DRM count: %d\n", *((unsigned int *)a1 + 34));
printf(" DRM size: %d\n", *((unsigned int *)a1 + 35));
printf(" DRM flags: %08x\n", *((unsigned int *)a1 + 36));
printf(" First content record #: %d\n", *((unsigned int *)a1 + 37));
printf(" Last content record #: %d\n", *((unsigned int *)a1 + 38));
printf(" FCIS record #: %d\n", *((unsigned int *)a1 + 39));
printf(" FLIS record #: %d\n", *((unsigned int *)a1 + 40));
printf(" Extra record data flags:");
v12 = *((unsigned int *)a1 + 41);
if ( (v12 & 1) != 0 )
{
printf(" multibyte");
v12 = *((unsigned int *)a1 + 41);
}
if ( (v12 & 2) != 0 )
{
printf(" tbs_index");
v12 = *((unsigned int *)a1 + 41);
}
if ( (v12 & 4) != 0 )
{
printf(" uncrossable_breaks");
v12 = *((unsigned int *)a1 + 41);
}
printf(" (0x%08x)\n", v12);
return printf(" INDX record offset: %d\n", *((unsigned int *)a1 + 42));
}

original_code:
void
mobi_header_print(mobi_header_t* h)
{
    int i;
    printf("PalmDOC header\n");
    printf("  Compression: %d", h->mobi_compression);
    switch (h->mobi_compression) {
        case 1:
            printf(" (no compression)");
            break;
        case 2:
            printf(" (PalmDOC compression)");
            break;
        case 17480:
            printf(" (HUFF/CDC compression)");
            break;
        default:
            printf(" (Unknown compression)");
            break;
    }
    printf("\n");
    printf("  Uncompressed text length: %d\n", h->mobi_text_length);
    printf("  Record count: %d\n", h->mobi_record_count);
    printf("  Record size: %d\n", h->mobi_record_size);
    printf("  Encryptin type: %d\n", h->mobi_encryption_type);
    printf("MOBI header\n");
    printf("  MOBI ID: %08x (%s)\n", h->mobi_identifier, id2string(h->mobi_identifier));
    printf("  MOBI header length: %d\n", h->mobi_header_length);
    printf("  Type %08x ", h->mobi_type);
    switch (h->mobi_type) {
        case 2:
            printf("(Mobipocket Book)\n");
            break;
        case 3:
            printf("(PalmDoc Book)\n");
            break;
        case 4:
            printf("(Audio)\n");
            break;
        case 232:
            printf("(mobipocket generated by kindlegen1.2)\n");
            break;
        case 248:
            printf("(KF8: generated by kindlegen2)\n");
            break;
        case 257:
            printf("(News)\n");
            break;
        case 258:
            printf("(News_Feed)\n");
            break;
        case 259:
            printf("(News_Magazine)\n");
            break;
        case 513:
            printf("(PICS)\n");
            break;
        case 514:
            printf("(WORD)\n");
            break;
        case 515:
            printf("(XLS)\n");
            break;
        case 516:
            printf("(PPT)\n");
            break;
        case 517:
            printf("(TEXT)\n");
            break;
        case 518:
            printf("(HTML)\n");
            break;
        default:
            printf("(unknown)\n");
    }
    printf("  Text encoding: %d", h->mobi_text_encoding);
    if (h->mobi_text_encoding == 1252)
        printf(" (CP1252)\n");
    else if (h->mobi_text_encoding == 65001)
        printf(" (UTF-8)\n");
    else
        printf(" (unknown)\n");
    printf("  UniqID: %08x\n", h->mobi_uid);
    printf("  File version: %d\n", h->mobi_file_version);
    if (h->mobi_ortographic_index != 0xffffffffU)
        printf("  Ortographic index: %d\n", h->mobi_ortographic_index);
    if (h->mobi_inflection_index != 0xffffffffU)
        printf("  Inflection index: %d\n", h->mobi_inflection_index);
    if (h->mobi_index_names != 0xffffffffU)
        printf(" Index names:  %d\n", h->mobi_index_names);
    if (h->mobi_index_keys != 0xffffffffU)
        printf("  Index keys: %d\n", h->mobi_index_keys);
    for (i = 0; i < 6; i++) {
        if (h->mobi_extra_index[i] != 0xffffffffU)
            printf("  Extra Index #%d: %d\n", i, h->mobi_extra_index[i]);
    }
    printf("  First non-book index: %d\n", h->mobi_first_nonbook_index);
    printf("  Full name offset: %d\n", h->mobi_full_name_offset);
    printf("  Full name length: %d\n", h->mobi_full_name_length);
    printf("  Locale: %d\n", h->mobi_locale);
    printf("  Dict input length: %d\n", h->mobi_dict_input_lang);
    printf("  Dict output length: %d\n", h->mobi_dict_output_lang);
    printf("  Min version: %d\n", h->mobi_min_version);
    printf("  First image index: %d\n", h->mobi_first_image_rec);
    printf("  Huffman record offset: %d\n", h->mobi_huffman_record_offset);
    printf("  Huffman record count: %d\n", h->mobi_huffman_record_count);
    printf("  Huffman table offset: %d\n", h->mobi_huffman_table_offset);
    printf("  Huffman table count: %d\n", h->mobi_huffman_table_count);
    printf("  EXTH flags: %s (0x%08x)\n",
            ((h->mobi_exth_flags & 0x40) ? "EXTH present" : "No EXTH"),
            h->mobi_exth_flags);
    printf("  DRM offset: %d\n", h->mobi_drm_offset);
    printf("  DRM count: %d\n", h->mobi_drm_count);
    printf("  DRM size: %d\n", h->mobi_drm_size);
    printf("  DRM flags: %08x\n", h->mobi_drm_flags);
    printf("  First content record #: %d\n", h->mobi_first_content_rec);
    printf("  Last content record #: %d\n", h->mobi_last_content_rec);
    printf("  FCIS record #: %d\n", h->mobi_fcis_rec);
    printf("  FLIS record #: %d\n", h->mobi_flis_rec);
    printf("  Extra record data flags:");
    if (h->mobi_extra_record_data_flags & (1 << 0))
        printf(" multibyte");
    if (h->mobi_extra_record_data_flags & (1 << 1))
        printf(" tbs_index");
    if (h->mobi_extra_record_data_flags & (1 << 2))
        printf(" uncrossable_breaks");
    printf(" (0x%08x)\n", h->mobi_extra_record_data_flags);
    printf("  INDX record offset: %d\n", h->mobi_indx_record_offset);
}

canonical_original_code:
void mobi_header_print(struct mobi_header *h)
{
    int i;
    printf("PalmDOC header\n");
    printf("  Compression: %d", h->mobi_compression);
    switch (h->mobi_compression) {
        case 1:
            printf(" (no compression)");
            break;
        case 2:
            printf(" (PalmDOC compression)");
            break;
        case 17480:
            printf(" (HUFF/CDC compression)");
            break;
        default:
            printf(" (Unknown compression)");
            break;
    }
    printf("\n");
    printf("  Uncompressed text length: %d\n", h->mobi_text_length);
    printf("  Record count: %d\n", h->mobi_record_count);
    printf("  Record size: %d\n", h->mobi_record_size);
    printf("  Encryptin type: %d\n", h->mobi_encryption_type);
    printf("MOBI header\n");
    printf("  MOBI ID: %08x (%s)\n", h->mobi_identifier, id2string(h->mobi_identifier));
    printf("  MOBI header length: %d\n", h->mobi_header_length);
    printf("  Type %08x ", h->mobi_type);
    switch (h->mobi_type) {
        case 2:
            printf("(Mobipocket Book)\n");
            break;
        case 3:
            printf("(PalmDoc Book)\n");
            break;
        case 4:
            printf("(Audio)\n");
            break;
        case 232:
            printf("(mobipocket generated by kindlegen1.2)\n");
            break;
        case 248:
            printf("(KF8: generated by kindlegen2)\n");
            break;
        case 257:
            printf("(News)\n");
            break;
        case 258:
            printf("(News_Feed)\n");
            break;
        case 259:
            printf("(News_Magazine)\n");
            break;
        case 513:
            printf("(PICS)\n");
            break;
        case 514:
            printf("(WORD)\n");
            break;
        case 515:
            printf("(XLS)\n");
            break;
        case 516:
            printf("(PPT)\n");
            break;
        case 517:
            printf("(TEXT)\n");
            break;
        case 518:
            printf("(HTML)\n");
            break;
        default:
            printf("(unknown)\n");
    }
    printf("  Text encoding: %d", h->mobi_text_encoding);
    if (h->mobi_text_encoding == 1252)
        printf(" (CP1252)\n");
    else if (h->mobi_text_encoding == 65001)
        printf(" (UTF-8)\n");
    else
        printf(" (unknown)\n");
    printf("  UniqID: %08x\n", h->mobi_uid);
    printf("  File version: %d\n", h->mobi_file_version);
    if (h->mobi_ortographic_index != 0xffffffffU)
        printf("  Ortographic index: %d\n", h->mobi_ortographic_index);
    if (h->mobi_inflection_index != 0xffffffffU)
        printf("  Inflection index: %d\n", h->mobi_inflection_index);
    if (h->mobi_index_names != 0xffffffffU)
        printf(" Index names:  %d\n", h->mobi_index_names);
    if (h->mobi_index_keys != 0xffffffffU)
        printf("  Index keys: %d\n", h->mobi_index_keys);
    for (i = 0; i < 6; i++) {
        if (h->mobi_extra_index[i] != 0xffffffffU)
            printf("  Extra Index #%d: %d\n", i, h->mobi_extra_index[i]);
    }
    printf("  First non-book index: %d\n", h->mobi_first_nonbook_index);
    printf("  Full name offset: %d\n", h->mobi_full_name_offset);
    printf("  Full name length: %d\n", h->mobi_full_name_length);
    printf("  Locale: %d\n", h->mobi_locale);
    printf("  Dict input length: %d\n", h->mobi_dict_input_lang);
    printf("  Dict output length: %d\n", h->mobi_dict_output_lang);
    printf("  Min version: %d\n", h->mobi_min_version);
    printf("  First image index: %d\n", h->mobi_first_image_rec);
    printf("  Huffman record offset: %d\n", h->mobi_huffman_record_offset);
    printf("  Huffman record count: %d\n", h->mobi_huffman_record_count);
    printf("  Huffman table offset: %d\n", h->mobi_huffman_table_offset);
    printf("  Huffman table count: %d\n", h->mobi_huffman_table_count);
    printf("  EXTH flags: %s (0x%08x)\n",
            ((h->mobi_exth_flags & 0x40) ? "EXTH present" : "No EXTH"),
            h->mobi_exth_flags);
    printf("  DRM offset: %d\n", h->mobi_drm_offset);
    printf("  DRM count: %d\n", h->mobi_drm_count);
    printf("  DRM size: %d\n", h->mobi_drm_size);
    printf("  DRM flags: %08x\n", h->mobi_drm_flags);
    printf("  First content record #: %d\n", h->mobi_first_content_rec);
    printf("  Last content record #: %d\n", h->mobi_last_content_rec);
    printf("  FCIS record #: %d\n", h->mobi_fcis_rec);
    printf("  FLIS record #: %d\n", h->mobi_flis_rec);
    printf("  Extra record data flags:");
    if (h->mobi_extra_record_data_flags & (1 << 0))
        printf(" multibyte");
    if (h->mobi_extra_record_data_flags & (1 << 1))
        printf(" tbs_index");
    if (h->mobi_extra_record_data_flags & (1 << 2))
        printf(" uncrossable_breaks");
    printf(" (0x%08x)\n", h->mobi_extra_record_data_flags);
    printf("  INDX record offset: %d\n", h->mobi_indx_record_offset);
}


repo: gonzoua/readmobi

original_code:
off_t
mobi_header_read(mobi_header_t* h, unsigned char *ptr, off_t size)
{
    int i;
    if (size < (16 + 0xf8))
        return (-1);
    do { (h->mobi_compression) = bs_read_2((ptr)); (ptr) += 2; } while (0);;
    ptr += 2;
    do { (h->mobi_text_length) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_record_count) = bs_read_2((ptr)); (ptr) += 2; } while (0);;
    do { (h->mobi_record_size) = bs_read_2((ptr)); (ptr) += 2; } while (0);;
    do { (h->mobi_encryption_type) = bs_read_2((ptr)); (ptr) += 2; } while (0);;
    ptr += 2;
    do { (h->mobi_identifier) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    if (h->mobi_identifier != 0x4d4f4249) {
        return (-1);
    }
    do { (h->mobi_header_length) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_type) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_text_encoding) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_uid) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_file_version) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_ortographic_index) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_inflection_index) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_index_names) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_index_keys) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    for (i = 0; i < 6; i++) {
        do { (h->mobi_extra_index[i]) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    }
    do { (h->mobi_first_nonbook_index) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_full_name_offset) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_full_name_length) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_locale) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_dict_input_lang) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_dict_output_lang) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_min_version) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_first_image_rec) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_huffman_record_offset) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_huffman_record_count) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_huffman_table_offset) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_huffman_table_count) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_exth_flags) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    ptr += 32;
    do { (h->mobi_drm_offset) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_drm_count) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_drm_size) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_drm_flags) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    ptr += 12;
    do { (h->mobi_first_content_rec) = bs_read_2((ptr)); (ptr) += 2; } while (0);;
    do { (h->mobi_last_content_rec) = bs_read_2((ptr)); (ptr) += 2; } while (0);;
    ptr += 4;
    do { (h->mobi_fcis_rec) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    ptr += 4;
    do { (h->mobi_flis_rec) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    ptr += 4;
    ptr += 24;
    do { (h->mobi_extra_record_data_flags) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    if (h->mobi_header_length == 0xe8) {
        do { (h->mobi_indx_record_offset) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    } else
        h->mobi_indx_record_offset = 0xffffffff;
    return h->mobi_header_length + 16;
}

canonical_original_code:
long mobi_header_read(struct mobi_header *h, unsigned char *ptr, long size)
{
    int i;
    if (size < (16 + 0xf8))
        return (-1);
    do { (h->mobi_compression) = bs_read_2((ptr)); (ptr) += 2; } while (0);;
    ptr += 2;
    do { (h->mobi_text_length) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_record_count) = bs_read_2((ptr)); (ptr) += 2; } while (0);;
    do { (h->mobi_record_size) = bs_read_2((ptr)); (ptr) += 2; } while (0);;
    do { (h->mobi_encryption_type) = bs_read_2((ptr)); (ptr) += 2; } while (0);;
    ptr += 2;
    do { (h->mobi_identifier) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    if (h->mobi_identifier != 0x4d4f4249) {
        return (-1);
    }
    do { (h->mobi_header_length) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_type) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_text_encoding) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_uid) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_file_version) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_ortographic_index) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_inflection_index) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_index_names) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_index_keys) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    for (i = 0; i < 6; i++) {
        do { (h->mobi_extra_index[i]) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    }
    do { (h->mobi_first_nonbook_index) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_full_name_offset) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_full_name_length) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_locale) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_dict_input_lang) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_dict_output_lang) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_min_version) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_first_image_rec) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_huffman_record_offset) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_huffman_record_count) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_huffman_table_offset) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_huffman_table_count) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_exth_flags) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    ptr += 32;
    do { (h->mobi_drm_offset) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_drm_count) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_drm_size) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    do { (h->mobi_drm_flags) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    ptr += 12;
    do { (h->mobi_first_content_rec) = bs_read_2((ptr)); (ptr) += 2; } while (0);;
    do { (h->mobi_last_content_rec) = bs_read_2((ptr)); (ptr) += 2; } while (0);;
    ptr += 4;
    do { (h->mobi_fcis_rec) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    ptr += 4;
    do { (h->mobi_flis_rec) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    ptr += 4;
    ptr += 24;
    do { (h->mobi_extra_record_data_flags) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    if (h->mobi_header_length == 0xe8) {
        do { (h->mobi_indx_record_offset) = bs_read_4((ptr)); (ptr) += 4; } while (0);;
    } else
        h->mobi_indx_record_offset = 0xffffffff;
    return h->mobi_header_length + 16;
}

variable_types: struct mobi_header { unsigned short mobi_compression; unsigned int mobi_text_length; unsigned short mobi_record_count; unsigned short mobi_record_size; unsigned short mobi_encryption_type; unsigned int mobi_identifier; unsigned int mobi_header_length; unsigned int mobi_type; unsigned int mobi_text_encoding; unsigned int mobi_uid; unsigned int mobi_file_version; unsigned int mobi_ortographic_index; unsigned int mobi_inflection_index; unsigned int mobi_index_names; unsigned int mobi_index_keys; unsigned int mobi_extra_index[6]; unsigned int mobi_first_nonbook_index; unsigned int mobi_full_name_offset; unsigned int mobi_full_name_length; unsigned int mobi_locale; unsigned int mobi_dict_input_lang; unsigned int mobi_dict_output_lang; unsigned int mobi_min_version; unsigned int mobi_first_image_rec; unsigned int mobi_huffman_record_offset; unsigned int mobi_huffman_record_count; unsigned int mobi_huffman_table_offset; unsigned int mobi_huffman_table_count; unsigned int mobi_exth_flags; unsigned int mobi_drm_offset; unsigned int mobi_drm_count; unsigned int mobi_drm_size; unsigned int mobi_drm_flags; unsigned int mobi_first_content_rec; unsigned int mobi_last_content_rec; unsigned int mobi_fcis_rec; unsigned int mobi_flis_rec; unsigned int mobi_extra_record_data_flags; unsigned int mobi_indx_record_offset; } *

variable_types:
ptr
unsigned char *

variable_types: size long

variable_types: i int

return_type: long


usage

decompiled_code:
size_t usage()
{
fwrite("Usage: readmobi {-v | -adDeEm | -r id | -t file.mhtml} [file.mobi]\n", 1uLL, 0x43uLL, stderr);
fwrite("\t-a\t\t\tprint all headers/records\n", 1uLL, 0x20uLL, stderr);
fwrite("\t-d\t\t\tprint PDB headers\n", 1uLL, 0x18uLL, stderr);
fwrite("\t-D\t\t\tprint PDB records\n", 1uLL, 0x18uLL, stderr);
fwrite("\t-e\t\t\tprint EXTH header\n", 1uLL, 0x18uLL, stderr);
fwrite("\t-E\t\t\tprint EXTH records\n", 1uLL, 0x19uLL, stderr);
fwrite("\t-m\t\t\tprint MOBI headers\n", 1uLL, 0x19uLL, stderr);
fwrite("\t-r record_id\t\tDump PDB record\n", 1uLL, 0x1FuLL, stderr);
fwrite("\t-t out_file\t\tDump text to file (\"-\" for stdout)\n", 1uLL, 0x31uLL, stderr);
return fwrite("\t-v\t\t\tprint version and exit\n", 1uLL, 0x1DuLL, stderr);
}

canonical_decompiled_code:
size_t func0()
{
fwrite("Usage: readmobi {-v | -adDeEm | -r id | -t file.mhtml} [file.mobi]\n", 1uLL, 0x43uLL, stderr);
fwrite("\t-a\t\t\tprint all headers/records\n", 1uLL, 0x20uLL, stderr);
fwrite("\t-d\t\t\tprint PDB headers\n", 1uLL, 0x18uLL, stderr);
fwrite("\t-D\t\t\tprint PDB records\n", 1uLL, 0x18uLL, stderr);
fwrite("\t-e\t\t\tprint EXTH header\n", 1uLL, 0x18uLL, stderr);
fwrite("\t-E\t\t\tprint EXTH records\n", 1uLL, 0x19uLL, stderr);
fwrite("\t-m\t\t\tprint MOBI headers\n", 1uLL, 0x19uLL, stderr);
fwrite("\t-r record_id\t\tDump PDB record\n", 1uLL, 0x1FuLL, stderr);
fwrite("\t-t out_file\t\tDump text to file (\"-\" for stdout)\n", 1uLL, 0x31uLL, stderr);
return fwrite("\t-v\t\t\tprint version and exit\n", 1uLL, 0x1DuLL, stderr);
}

original_code:
static void
usage(void)
{
    fprintf(stderr, "Usage: readmobi {-v | -adDeEm | -r id | -t file.mhtml} [file.mobi]\n");
    fprintf(stderr, "\t-a\t\t\tprint all headers/records\n");
    fprintf(stderr, "\t-d\t\t\tprint PDB headers\n");
    fprintf(stderr, "\t-D\t\t\tprint PDB records\n");
    fprintf(stderr, "\t-e\t\t\tprint EXTH header\n");
    fprintf(stderr, "\t-E\t\t\tprint EXTH records\n");
    fprintf(stderr, "\t-m\t\t\tprint MOBI headers\n");
    fprintf(stderr, "\t-r record_id\t\tDump PDB record\n");
    fprintf(stderr, "\t-t out_file\t\tDump text to file (\"-\" for stdout)\n");
    fprintf(stderr, "\t-v\t\t\tprint version and exit\n");
}

canonical_original_code:
static void usage()
{
    fprintf(stderr, "Usage: readmobi {-v | -adDeEm | -r id | -t file.mhtml} [file.mobi]\n");
    fprintf(stderr, "\t-a\t\t\tprint all headers/records\n");
    fprintf(stderr, "\t-d\t\t\tprint PDB headers\n");
    fprintf(stderr, "\t-D\t\t\tprint PDB records\n");
    fprintf(stderr, "\t-e\t\t\tprint EXTH header\n");
    fprintf(stderr, "\t-E\t\t\tprint EXTH records\n");
    fprintf(stderr, "\t-m\t\t\tprint MOBI headers\n");
    fprintf(stderr, "\t-r record_id\t\tDump PDB record\n");
    fprintf(stderr, "\t-t out_file\t\tDump text to file (\"-\" for stdout)\n");
    fprintf(stderr, "\t-v\t\t\tprint version and exit\n");
}

return_type: void


original_code:
int
palmdoc_decompress(unsigned char *ptr_in, size_t size_in,
        unsigned char* ptr_out, size_t size_out)
{
    size_t pos_in = 0;
    size_t pos_out = 0;
    unsigned char cmd;
    uint8_t length;
    uint16_t distance;
    int i;
    while ((pos_in < size_in) && (pos_out < size_out) ) {
        cmd = ptr_in[pos_in++];
        if ((cmd == 0) || (cmd > 8 && cmd < 0x80)) {
            ptr_out[pos_out++] = cmd;
        }
        if ((cmd > 0 && cmd < 9)) {
            memcpy(ptr_out + pos_out, ptr_in + pos_in, cmd);
            pos_in += cmd;
            pos_out += cmd;
        }
        if ((cmd >= 0x80 && cmd < 0xc0)) {
            distance = cmd & 0x3f;
            distance <<= 5;
            distance |= (ptr_in[pos_in] >> 3) & 0x1f;
            length = (ptr_in[pos_in] & 7) + 3;
            pos_in++;
            for (i = 0; i < length; i++) {
                ptr_out[pos_out] = ptr_out[pos_out - distance];
                pos_out++;
            }
        }
        if (cmd >= 0xc0) {
            ptr_out[pos_out++] = ' ';
            ptr_out[pos_out++] = cmd ^ 0x80;
        }
    }
    return (pos_out);
}

canonical_original_code:
int palmdoc_decompress(unsigned char *ptr_in, unsigned long size_in, unsigned char *ptr_out, unsigned long size_out)
{
    unsigned long pos_in = 0;
    unsigned long pos_out = 0;
    unsigned char cmd;
    unsigned char length;
    unsigned short distance;
    int i;
    while ((pos_in < size_in) && (pos_out < size_out) ) {
        cmd = ptr_in[pos_in++];
        if ((cmd == 0) || (cmd > 8 && cmd < 0x80)) {
            ptr_out[pos_out++] = cmd;
        }
        if ((cmd > 0 && cmd < 9)) {
            memcpy(ptr_out + pos_out, ptr_in + pos_in, cmd);
            pos_in += cmd;
            pos_out += cmd;
        }
        if ((cmd >= 0x80 && cmd < 0xc0)) {
            distance = cmd & 0x3f;
            distance <<= 5;
            distance |= (ptr_in[pos_in] >> 3) & 0x1f;
            length = (ptr_in[pos_in] & 7) + 3;
            pos_in++;
            for (i = 0; i < length; i++) {
                ptr_out[pos_out] = ptr_out[pos_out - distance];
                pos_out++;
            }
        }
        if (cmd >= 0xc0) {
            ptr_out[pos_out++] = ' ';
            ptr_out[pos_out++] = cmd ^ 0x80;
        }
    }
    return (pos_out);
}

variable_types:
cmd
unsigned char

variable_types:
length
unsigned char

variable_types:
distance
unsigned short

return_type: int


decompiled_code:
__int16 __fastcall bs_read_2(_WORD *a1)
{
return __ROL2__(*a1, 8);
}

canonical_decompiled_code:
__int16 __fastcall func0(_WORD *a1)
{
return __ROL2__(*a1, 8);
}

original_code:
static inline uint16_t
bs_read_2(unsigned char *ptr)
{
    return (ptr[0] << 8) | ptr[1];
}

canonical_original_code:
static inline unsigned short bs_read_2(unsigned char *ptr)
{
    return (ptr[0] << 8) | ptr[1];
}

return_type: unsigned short


decompiled_code:
__int64 __fastcall bs_read_4(unsigned int *a1)
{
return _byteswap_ulong(*a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int *a1)
{
return _byteswap_ulong(*a1);
}

original_code:
static inline uint32_t
bs_read_4(unsigned char *ptr)
{
    return (ptr[0] << 24) |
        (ptr[1] << 16) |
        (ptr[2] << 8) |
        ptr[3];
}

canonical_original_code:
static inline unsigned int bs_read_4(unsigned char *ptr)
{
    return (ptr[0] << 24) |
        (ptr[1] << 16) |
        (ptr[2] << 8) |
        ptr[3];
}

return_type: unsigned int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
printword(argc, argv, envp);
return 0;
}

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

original_code:
int main() {
  printword();
  return 0;
}

canonical_original_code:
int main() {
  printword();
  return 0;
}


printword

decompiled_code:
int printword()
{
return puts("hello world");
}

canonical_decompiled_code:
int func7()
{
return puts("hello world");
}

original_code:
void printword() {
  printf("hello world\n");
}


repo: google/vpn-reverse-tether

decompiled_code:
__int64 __fastcall get_interface(char *src)
{
unsigned int v1; // er12
char v3[16]; // [rsp+0h] [rbp-48h] BYREF
__int128 v4; // [rsp+10h] [rbp-38h]
__int64 v5; // [rsp+20h] [rbp-28h]

v1 = open("/dev/net/tun", 2050);
v4 = 0LL;
v5 = 0LL;
LOWORD(v4) = 4097;
strncpy(v3, src, 0x10uLL);
if ( ioctl(v1, 0x400454CAuLL, v3) )
{
v1 = -1;
perror("Cannot get TUN interface");
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *src)
{
unsigned int v1; // er12
char v3[16]; // [rsp+0h] [rbp-48h] BYREF
__int128 v4; // [rsp+10h] [rbp-38h]
__int64 v5; // [rsp+20h] [rbp-28h]

v1 = open("/dev/net/tun", 2050);
v4 = 0LL;
v5 = 0LL;
LOWORD(v4) = 4097;
strncpy(v3, src, 0x10uLL);
if ( ioctl(v1, 0x400454CAuLL, v3) )
{
v1 = -1;
perror("Cannot get TUN interface");
}
return v1;
}

original_code:
int get_interface(char* name) {
  int interface = open("/dev/net/tun", 02 | 04000);
  struct ifreq ifr;
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_ifru.ifru_flags = 0x0001 | 0x1000;
  strncpy(ifr.ifr_ifrn.ifrn_name, name, sizeof(ifr.ifr_ifrn.ifrn_name));
  if (ioctl(interface, (((1U) << (((0 +8)+8)+14)) | ((('T')) << (0 +8)) | (((202)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &ifr)) {
    perror("Cannot get TUN interface");
    return -1;
  }
  return interface;
}

canonical_original_code:
int get_interface(char *name) {
  int interface = open("/dev/net/tun", 02 | 04000);
  struct ifreq ifr;
  memset(&ifr, 0, sizeof(ifr));
  ifr.ifr_ifru.ifru_flags = 0x0001 | 0x1000;
  strncpy(ifr.ifr_ifrn.ifrn_name, name, sizeof(ifr.ifr_ifrn.ifrn_name));
  if (ioctl(interface, (((1U) << (((0 +8)+8)+14)) | ((('T')) << (0 +8)) | (((202)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), &ifr)) {
    perror("Cannot get TUN interface");
    return -1;
  }
  return interface;
}

variable_types:
name
char *

variable_types: interface int

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

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

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


decompiled_code:
__int64 __fastcall get_tunnel(__int16 a1)
{
unsigned int v1; // er12
socklen_t v2; // edx
struct sockaddr v4; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 1, 6);
v4 = 0LL;
v4.sa_family = 2;
*(_WORD *)v4.sa_data = _bswap_16(a1);
if ( connect(v1, &v4, v2) )
{
v1 = -1;
perror("Connect to tunnel");
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int16 a1)
{
unsigned int v1; // er12
socklen_t v2; // edx
struct sockaddr v4; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 1, 6);
v4 = 0LL;
v4.sa_family = 2;
*(_WORD *)v4.sa_data = func0(a1);
if ( connect(v1, &v4, v2) )
{
v1 = -1;
perror("Connect to tunnel");
}
return v1;
}

original_code:
int get_tunnel(int port) {
  int tunnel = socket(2, SOCK_STREAM, IPPROTO_TCP);
  struct sockaddr_in addr = {};
  addr.sin_family = 2;
  addr.sin_port = htons(port);
  if (connect(tunnel, (struct sockaddr*) &addr, sizeof(addr))) {
    perror("Connect to tunnel");
    return -1;
  }
  return tunnel;
}

canonical_original_code:
int get_tunnel(int port) {
  int tunnel = socket(2, SOCK_STREAM, IPPROTO_TCP);
  struct sockaddr_in addr = {};
  addr.sin_family = 2;
  addr.sin_port = htons(port);
  if (connect(tunnel, (struct sockaddr *) &addr, sizeof(addr))) {
    perror("Connect to tunnel");
    return -1;
  }
  return tunnel;
}

variable_types: port int

variable_types: tunnel int

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]; }

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:
__int64 __fastcall get_local_tunnel(char *src)
{
unsigned int v1; // er12
struct sockaddr v3[6]; // [rsp+0h] [rbp-88h] BYREF
char v4; // [rsp+6Dh] [rbp-1Bh]

v1 = socket(1, 1, 0);
v3[0].sa_family = 1;
strncpy(v3[0].sa_data, src, 0x6BuLL);
v4 = 0;
if ( connect(v1, v3, 0x6Eu) )
{
v1 = -1;
perror("Connect to tunnel");
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *src)
{
unsigned int v1; // er12
struct sockaddr v3[6]; // [rsp+0h] [rbp-88h] BYREF
char v4; // [rsp+6Dh] [rbp-1Bh]

v1 = socket(1, 1, 0);
v3[0].sa_family = 1;
strncpy(v3[0].sa_data, src, 0x6BuLL);
v4 = 0;
if ( connect(v1, v3, 0x6Eu) )
{
v1 = -1;
perror("Connect to tunnel");
}
return v1;
}

original_code:
int get_local_tunnel(char* name) {
  int tunnel = socket(1, SOCK_STREAM, 0);
  struct sockaddr_un addr = {};
  addr.sun_family = 1;
  strncpy(addr.sun_path, name, sizeof(addr.sun_path));
  addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
  if (connect(tunnel, (struct sockaddr*) &addr, sizeof(addr))) {
    perror("Connect to tunnel");
    return -1;
  }
  return tunnel;
}

canonical_original_code:
int get_local_tunnel(char *name) {
  int tunnel = socket(1, SOCK_STREAM, 0);
  struct sockaddr_un addr = {};
  addr.sun_family = 1;
  strncpy(addr.sun_path, name, sizeof(addr.sun_path));
  addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
  if (connect(tunnel, (struct sockaddr *) &addr, sizeof(addr))) {
    perror("Connect to tunnel");
    return -1;
  }
  return tunnel;
}

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }


decompiled_code:
__int64 __fastcall build_parameters(__int64 a1, int a2, int a3, __int64 a4)
{
__int64 v6; // r15
unsigned int v7; // er12
signed int v8; // edi
const char *v9; // rbp
int v10; // eax
char v11; // di
size_t v12; // rdx
unsigned int v13; // eax
__int64 v15; // [rsp+8h] [rbp-40h]

if ( a3 <= 0 )
return 0;
v15 = a3;
v6 = 0LL;
v7 = 0;
while ( 1 )
{
v9 = *(const char **)(a4 + 8 * v6);
v10 = strlen(v9);
v11 = 44;
v12 = v10;
if ( v10 == 2 && *v9 == 45 )
break;
v13 = v10 + v7;
if ( (int)(v12 + v7 + 1) >= a2 )
goto LABEL_11;
LABEL_7:
if ( v6 )
{
*(_BYTE *)(a1 + (int)v7) = v11;
v8 = v7 + 1;
v7 += v12 + 1;
}
else
{
v8 = v7;
v7 = v13;
}
++v6;
memcpy((void *)(a1 + v8), v9, v12);
if ( v15 == v6 )
return v7;
}
v12 = 1LL;
++v9;
v11 = 32;
v13 = v7 + 1;
if ( (int)(v7 + 2) < a2 )
goto LABEL_7;
LABEL_11:
v7 = -1;
puts("Parameters are too large!");
return v7;
}

original_code:
int build_parameters(char* parameters, int size, int argc, char* argv[]) {
  int offset = 0;
  for (int i = 0; i < argc; ++i) {
    char *parameter = argv[i];
    int length = strlen(parameter);
    char delimiter = ',';
    if (length == 2 && parameter[0] == '-') {
      ++parameter;
      --length;
      delimiter = ' ';
    }
    if (offset + length + 1 >= size) {
      puts("Parameters are too large!");
      return -1;
    }
    if (i > 0) {
      parameters[offset] = delimiter;
      ++offset;
    }
    memcpy(&parameters[offset], parameter, length);
    offset += length;
  }
  return offset;
}

canonical_original_code:
int build_parameters(char *parameters, int size, int argc, char *argv[]) {
  int offset = 0;
  for (int i = 0; i < argc; ++i) {
    char *parameter = argv[i];
    int length = strlen(parameter);
    char delimiter = ',';
    if (length == 2 && parameter[0] == '-') {
      ++parameter;
      --length;
      delimiter = ' ';
    }
    if (offset + length + 1 >= size) {
      puts("Parameters are too large!");
      return -1;
    }
    if (i > 0) {
      parameters[offset] = delimiter;
      ++offset;
    }
    memcpy(&parameters[offset], parameter, length);
    offset += length;
  }
  return offset;
}

variable_types:
parameters
char *

variable_types: size int

variable_types: argc int

variable_types:
argv
char *[]

variable_types: offset int

variable_types:
parameter
char *

variable_types: length int

variable_types: delimiter char


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v3; // bp
int result; // eax
int v5; // er12
char *v6; // r13
int v7; // eax
int v8; // er13
size_t v9; // rdx
int v10; // ecx
__int16 buf; // [rsp+Eh] [rbp-42Ah] BYREF
char v12[1064]; // [rsp+10h] [rbp-428h] BYREF

if ( argc <= 3 )
{
printf(
"Usage: %s <tunN> <port|path> options...\n"
"\n"
"Options:\n"
" -s <session name>\n"
" -m <MTU> for the maximum transmission unit\n"
" -a <address> <prefix-length> for the private address\n"
" -r <address> <prefix-length> for the forwarding route\n"
" -d <address> for the domain name server\n"
" -s <domain> for the search domain\n",
*argv);
result = 2;
}
else
{
v3 = build_parameters((__int64)v12, 1024, argc - 3, (__int64)(argv + 3));
result = 2;
if ( v3 >= 0 )
{
v5 = get_interface((char *)argv[1]);
if ( v5 >= 0 )
{
v6 = (char *)argv[2];
v7 = atoi(v6);
if ( v7 )
v8 = get_tunnel(v7);
else
v8 = get_local_tunnel(v6);
if ( v8 >= 0 )
{
puts("Connected.");
buf = _bswap_16(v3);
send(v8, &buf, v9, v10);
send(v8, v12, v3, 0);
puts("Forwarding. Press Enter to stop.");
forward(0LL, (unsigned int)v8, (unsigned int)v5);
perror("Finished");
}
}
result = 1;
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v3; // bp
int result; // eax
int v5; // er12
char *v6; // r13
int v7; // eax
int v8; // er13
size_t v9; // rdx
int v10; // ecx
__int16 buf; // [rsp+Eh] [rbp-42Ah] BYREF
char v12[1064]; // [rsp+10h] [rbp-428h] BYREF

if ( argc <= 3 )
{
printf(
"Usage: %s <tunN> <port|path> options...\n"
"\n"
"Options:\n"
" -s <session name>\n"
" -m <MTU> for the maximum transmission unit\n"
" -a <address> <prefix-length> for the private address\n"
" -r <address> <prefix-length> for the forwarding route\n"
" -d <address> for the domain name server\n"
" -s <domain> for the search domain\n",
*argv);
result = 2;
}
else
{
v3 = func4((__int64)v12, 1024, argc - 3, (__int64)(argv + 3));
result = 2;
if ( v3 >= 0 )
{
v5 = func1((char *)argv[1]);
if ( v5 >= 0 )
{
v6 = (char *)argv[2];
v7 = atoi(v6);
if ( v7 )
v8 = func2(v7);
else
v8 = func3(v6);
if ( v8 >= 0 )
{
puts("Connected.");
buf = func0(v3);
send(v8, &buf, v9, v10);
send(v8, v12, v3, 0);
puts("Forwarding. Press Enter to stop.");
forward(0LL, (unsigned int)v8, (unsigned int)v5);
perror("Finished");
}
}
result = 1;
}
}
return result;
}

original_code:
int main(int argc, char **argv) {
  if (argc < 4) {
    printf("Usage: %s <tunN> <port|path> options...\n"
        "\n"
        "Options:\n"
        "  -s <session name>\n"
        "  -m <MTU> for the maximum transmission unit\n"
        "  -a <address> <prefix-length> for the private address\n"
        "  -r <address> <prefix-length> for the forwarding route\n"
        "  -d <address> for the domain name server\n"
        "  -s <domain> for the search domain\n", argv[0]);
    return 2;
  }
  char parameters[1024];
  int16_t param_length = build_parameters(parameters, sizeof(parameters), argc - 3, argv + 3);
  if (param_length < 0)
    return 2;
  int interface = get_interface(argv[1]);
  if (interface < 0)
    return 1;
  int tunnel = -1;
  int port = atoi(argv[2]);
  if (port) {
    tunnel = get_tunnel(port);
  } else {
    tunnel = get_local_tunnel(argv[2]);
  }
  if (tunnel < 0)
    return 1;
  puts("Connected.");
  int16_t param_length_be = htons(param_length);
  send(tunnel, &param_length_be, sizeof(param_length_be), 0);
  send(tunnel, parameters, param_length, 0);
  puts("Forwarding. Press Enter to stop.");
  forward(0, tunnel, interface);
  perror("Finished");
  return 1;
}

canonical_original_code:
int main(int argc, char **argv) {
  if (argc < 4) {
    printf("Usage: %s <tunN> <port|path> options...\n"
        "\n"
        "Options:\n"
        "  -s <session name>\n"
        "  -m <MTU> for the maximum transmission unit\n"
        "  -a <address> <prefix-length> for the private address\n"
        "  -r <address> <prefix-length> for the forwarding route\n"
        "  -d <address> for the domain name server\n"
        "  -s <domain> for the search domain\n", argv[0]);
    return 2;
  }
  char parameters[1024];
  short param_length = build_parameters(parameters, sizeof(parameters), argc - 3, argv + 3);
  if (param_length < 0)
    return 2;
  int interface = get_interface(argv[1]);
  if (interface < 0)
    return 1;
  int tunnel = -1;
  int port = atoi(argv[2]);
  if (port) {
    tunnel = get_tunnel(port);
  } else {
    tunnel = get_local_tunnel(argv[2]);
  }
  if (tunnel < 0)
    return 1;
  puts("Connected.");
  short param_length_be = htons(param_length);
  send(tunnel, &param_length_be, sizeof(param_length_be), 0);
  send(tunnel, parameters, param_length, 0);
  puts("Forwarding. Press Enter to stop.");
  forward(0, tunnel, interface);
  perror("Finished");
  return 1;
}

variable_types:
argv
char **

variable_types:
parameters
char [1024]


decompiled_code:
__int64 __fastcall read_stream(int fd, __int64 a2)
{
unsigned __int64 v2; // rdx
unsigned __int64 v4; // rsi
unsigned __int64 v5; // rax
__int64 v6; // r12
__int64 result; // rax
int v8; // eax
int v9; // eax

v2 = *(_QWORD *)a2;
v4 = *(_QWORD *)(a2 + 8);
v5 = v2;
if ( v2 )
goto LABEL_2;
v6 = a2 + 16;
v8 = read(fd, (void *)(a2 + 16 + v4), 2 - v4);
if ( v8 < 0 )
goto LABEL_13;
v4 = *(_QWORD *)(a2 + 8) + v8;
*(_QWORD *)(a2 + 8) = v4;
if ( v4 <= 1 )
{
v5 = *(_QWORD *)a2;
v2 = *(_QWORD *)a2;
if ( !*(_QWORD *)a2 )
return v4 == v2;
LABEL_2:
v6 = a2 + 16;
if ( v5 == v4 )
{
v2 = v4;
return v4 == v2;
}
goto LABEL_9;
}
*(_QWORD *)(a2 + 8) = 0LL;
v4 = 0LL;
v2 = (unsigned __int16)_bswap_16(*(_WORD *)(a2 + 16));
result = 0xFFFFFFFFLL;
*(_QWORD *)a2 = v2;
if ( v2 )
{
LABEL_9:
v9 = read(fd, (void *)(v6 + v4), v2 - v4);
if ( v9 >= 0 )
{
v4 = *(_QWORD *)(a2 + 8) + v9;
v2 = *(_QWORD *)a2;
*(_QWORD *)(a2 + 8) = v4;
return v4 == v2;
}
LABEL_13:
result = (unsigned int)-(*_errno_location() != 11);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, __int64 a2)
{
unsigned __int64 v2; // rdx
unsigned __int64 v4; // rsi
unsigned __int64 v5; // rax
__int64 v6; // r12
__int64 result; // rax
int v8; // eax
int v9; // eax

v2 = *(_QWORD *)a2;
v4 = *(_QWORD *)(a2 + 8);
v5 = v2;
if ( v2 )
goto LABEL_2;
v6 = a2 + 16;
v8 = read(fd, (void *)(a2 + 16 + v4), 2 - v4);
if ( v8 < 0 )
goto LABEL_13;
v4 = *(_QWORD *)(a2 + 8) + v8;
*(_QWORD *)(a2 + 8) = v4;
if ( v4 <= 1 )
{
v5 = *(_QWORD *)a2;
v2 = *(_QWORD *)a2;
if ( !*(_QWORD *)a2 )
return v4 == v2;
LABEL_2:
v6 = a2 + 16;
if ( v5 == v4 )
{
v2 = v4;
return v4 == v2;
}
goto LABEL_9;
}
*(_QWORD *)(a2 + 8) = 0LL;
v4 = 0LL;
v2 = (unsigned __int16)func0(*(_WORD *)(a2 + 16));
result = 0xFFFFFFFFLL;
*(_QWORD *)a2 = v2;
if ( v2 )
{
LABEL_9:
v9 = read(fd, (void *)(v6 + v4), v2 - v4);
if ( v9 >= 0 )
{
v4 = *(_QWORD *)(a2 + 8) + v9;
v2 = *(_QWORD *)a2;
*(_QWORD *)(a2 + 8) = v4;
return v4 == v2;
}
LABEL_13:
result = (unsigned int)-(*_errno_location() != 11);
}
return result;
}

original_code:
int read_stream(int fd, struct buffer* buf) {
  if (!buf->size) {
    int num_read = read(fd, buf->data + buf->progress,
                        sizeof(int16_t) - buf->progress);
    if (num_read < 0)
      return ((*__errno_location ()) == 11) ? 0 : -1;
    buf->progress += num_read;
    if (buf->progress >= sizeof(int16_t)) {
      buf->progress = 0;
      buf->size = ntohs(*(int16_t*)(buf->data));
      if (buf->size == 0)
        return -1;
    }
  }
  if (buf->size && (buf->size - buf->progress) > 0) {
    int num_read = read(fd, buf->data + buf->progress,
                        buf->size - buf->progress);
    if (num_read < 0)
      return ((*__errno_location ()) == 11) ? 0 : -1;
    buf->progress += num_read;
  }
  return buf->progress == buf->size;
}

canonical_original_code:
int read_stream(int fd, struct buffer *buf) {
  if (!buf->size) {
    int num_read = read(fd, buf->data + buf->progress,
                        sizeof(int16_t) - buf->progress);
    if (num_read < 0)
      return ((*__errno_location ()) == 11) ? 0 : -1;
    buf->progress += num_read;
    if (buf->progress >= sizeof(short)) {
      buf->progress = 0;
      buf->size = ntohs(*(short *)(buf->data));
      if (buf->size == 0)
        return -1;
    }
  }
  if (buf->size && (buf->size - buf->progress) > 0) {
    int num_read = read(fd, buf->data + buf->progress,
                        buf->size - buf->progress);
    if (num_read < 0)
      return ((*__errno_location ()) == 11) ? 0 : -1;
    buf->progress += num_read;
  }
  return buf->progress == buf->size;
}

variable_types: fd int

variable_types: struct buffer { unsigned long size; unsigned long progress; char data[4096]; } *


decompiled_code:
__int64 __fastcall write_stream(int a1, unsigned __int64 *a2)
{
unsigned __int64 v2; // rax
unsigned __int64 v3; // rdx
int v5; // eax
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rax

v2 = a2[1];
v3 = *a2;
if ( v2 >= *a2 )
return v2 == v3;
v5 = write(a1, (char *)a2 + v2 + 16, v3 - v2);
if ( v5 < 0 )
return (unsigned int)-(*_errno_location() != 11);
v6 = *a2;
v7 = a2[1] + v5;
a2[1] = v7;
return v7 == v6;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, unsigned __int64 *a2)
{
unsigned __int64 v2; // rax
unsigned __int64 v3; // rdx
int v5; // eax
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rax

v2 = a2[1];
v3 = *a2;
if ( v2 >= *a2 )
return v2 == v3;
v5 = write(a1, (char *)a2 + v2 + 16, v3 - v2);
if ( v5 < 0 )
return (unsigned int)-(*_errno_location() != 11);
v6 = *a2;
v7 = a2[1] + v5;
a2[1] = v7;
return v7 == v6;
}

original_code:
int write_stream(int fd, struct buffer* buf) {
  if (buf->progress < buf->size) {
    int num_written = write(fd, buf->data + buf->progress,
                            buf->size - buf->progress);
    if (num_written < 0)
      return ((*__errno_location ()) == 11) ? 0 : -1;
    buf->progress += num_written;
  }
  return buf->progress == buf->size;
}

canonical_original_code:
int write_stream(int fd, struct buffer *buf) {
  if (buf->progress < buf->size) {
    int num_written = write(fd, buf->data + buf->progress,
                            buf->size - buf->progress);
    if (num_written < 0)
      return ((*__errno_location ()) == 11) ? 0 : -1;
    buf->progress += num_written;
  }
  return buf->progress == buf->size;
}

user_defined_types: struct buffer { unsigned long size; unsigned long progress; char data[4096]; }


decompiled_code:
__int64 __fastcall read_datagram(int a1, __int64 a2)
{
int v2; // eax
__int16 v3; // ax
int v4; // edx
__int64 result; // rax

v2 = read(a1, (void *)(a2 + 18), 0xFFEuLL);
if ( v2 < 0 )
return (unsigned int)-(*_errno_location() != 11);
v3 = _bswap_16(v2);
*(_QWORD *)(a2 + 8) = 0LL;
*(_WORD *)(a2 + 16) = v3;
result = 1LL;
*(_QWORD *)a2 = v4 + 2LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, __int64 a2)
{
int v2; // eax
__int16 v3; // ax
int v4; // edx
__int64 result; // rax

v2 = read(a1, (void *)(a2 + 18), 0xFFEuLL);
if ( v2 < 0 )
return (unsigned int)-(*_errno_location() != 11);
v3 = func0(v2);
*(_QWORD *)(a2 + 8) = 0LL;
*(_WORD *)(a2 + 16) = v3;
result = 1LL;
*(_QWORD *)a2 = v4 + 2LL;
return result;
}

original_code:
int read_datagram(int fd, struct buffer* buf) {
  int num_read = read(fd, buf->data + sizeof(int16_t),
                      sizeof(buf->data) - sizeof(int16_t));
  if (num_read < 0)
    return ((*__errno_location ()) == 11) ? 0 : -1;
  *(int16_t*)(buf->data) = htons(num_read);
  buf->size = num_read + sizeof(int16_t);
  buf->progress = 0;
  return 1;
}

canonical_original_code:
int read_datagram(int fd, struct buffer *buf) {
  int num_read = read(fd, buf->data + sizeof(int16_t),
                      sizeof(buf->data) - sizeof(int16_t));
  if (num_read < 0)
    return ((*__errno_location ()) == 11) ? 0 : -1;
  *(short *)(buf->data) = htons(num_read);
  buf->size = num_read + sizeof(short);
  buf->progress = 0;
  return 1;
}


decompiled_code:
__int64 __fastcall write_datagram(int a1, size_t *a2)
{
__int64 result; // rax

if ( (int)write(a1, a2 + 2, *a2) < 0 )
return (unsigned int)-(*_errno_location() != 11);
*a2 = 0LL;
result = 1LL;
a2[1] = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, size_t *a2)
{
__int64 result; // rax

if ( (int)write(a1, a2 + 2, *a2) < 0 )
return (unsigned int)-(*_errno_location() != 11);
*a2 = 0LL;
result = 1LL;
a2[1] = 0LL;
return result;
}

original_code:
int write_datagram(int fd, struct buffer* buf) {
  int num_written = write(fd, buf->data, buf->size);
  if (num_written < 0)
    return ((*__errno_location ()) == 11) ? 0 : -1;
  buf->progress = buf->size = 0;
  return 1;
}

canonical_original_code:
int write_datagram(int fd, struct buffer *buf) {
  int num_written = write(fd, buf->data, buf->size);
  if (num_written < 0)
    return ((*__errno_location ()) == 11) ? 0 : -1;
  buf->progress = buf->size = 0;
  return 1;
}


forward

original_code:
void forward(int control_fd, int stream_fd, int datagram_fd) {
  int fds[] = { control_fd, stream_fd, datagram_fd };
  int nfds = 0;
  for (int i = 0; i < 3; ++i) {
    if (fcntl(fds[i], 4, fcntl(fds[i], 3, 0) | 04000) != 0)
      goto exit;
    if (fds[i] > nfds)
      nfds = fds[i];
  }
  nfds += 1;
  struct buffer stream_buf;
  struct buffer datagram_buf;
  int have_stream = 0;
  int have_datagram = 0;
  fd_set fds_read;
  fd_set fds_write;
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_read)->__fds_bits)[0]) : "memory"); } while (0);
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_write)->__fds_bits)[0]) : "memory"); } while (0);
  while (1) {
    ((void) (((&fds_read)->__fds_bits)[((control_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((control_fd) % (8 * (int) sizeof (__fd_mask)))))));
    if (have_stream) {
      ((void) (((&fds_read)->__fds_bits)[((stream_fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((stream_fd) % (8 * (int) sizeof (__fd_mask)))))));
      ((void) (((&fds_write)->__fds_bits)[((datagram_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((datagram_fd) % (8 * (int) sizeof (__fd_mask)))))));
    } else {
      ((void) (((&fds_read)->__fds_bits)[((stream_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((stream_fd) % (8 * (int) sizeof (__fd_mask)))))));
      ((void) (((&fds_write)->__fds_bits)[((datagram_fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((datagram_fd) % (8 * (int) sizeof (__fd_mask)))))));
    }
    if (have_datagram) {
      ((void) (((&fds_read)->__fds_bits)[((datagram_fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((datagram_fd) % (8 * (int) sizeof (__fd_mask)))))));
      ((void) (((&fds_write)->__fds_bits)[((stream_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((stream_fd) % (8 * (int) sizeof (__fd_mask)))))));
    } else {
      ((void) (((&fds_read)->__fds_bits)[((datagram_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((datagram_fd) % (8 * (int) sizeof (__fd_mask)))))));
      ((void) (((&fds_write)->__fds_bits)[((stream_fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((stream_fd) % (8 * (int) sizeof (__fd_mask)))))));
    }
    int n = select(nfds, &fds_read, &fds_write, ((void *)0), ((void *)0));
    if (((((&fds_read)->__fds_bits)[((control_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((control_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      goto exit;
    }
    if (((((&fds_read)->__fds_bits)[((stream_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((stream_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      int rv = read_stream(stream_fd, &stream_buf);
      if (rv < 0)
        goto exit;
      if (rv > 0)
        have_stream = 1;
    }
    if (((((&fds_write)->__fds_bits)[((stream_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((stream_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      int rv = write_stream(stream_fd, &datagram_buf);
      if (rv < 0)
        goto exit;
      if (rv > 0)
        have_datagram = 0;
    }
    if (((((&fds_read)->__fds_bits)[((datagram_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((datagram_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      int rv = read_datagram(datagram_fd, &datagram_buf);
      if (rv < 0)
        goto exit;
      if (rv > 0)
        have_datagram = 1;
    }
    if (((((&fds_write)->__fds_bits)[((datagram_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((datagram_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      int rv = write_datagram(datagram_fd, &stream_buf);
      if (rv < 0)
        goto exit;
      if (rv > 0)
        have_stream = 0;
    }
  }
exit:
  close(stream_fd);
  close(datagram_fd);
  close(control_fd);
}

canonical_original_code:
void forward(int control_fd, int stream_fd, int datagram_fd) {
  int fds[] = { control_fd, stream_fd, datagram_fd };
  int nfds = 0;
  for (int i = 0; i < 3; ++i) {
    if (fcntl(fds[i], 4, fcntl(fds[i], 3, 0) | 04000) != 0)
      goto exit;
    if (fds[i] > nfds)
      nfds = fds[i];
  }
  nfds += 1;
  struct buffer stream_buf;
  struct buffer datagram_buf;
  int have_stream = 0;
  int have_datagram = 0;
  struct fd_set fds_read;
  struct fd_set fds_write;
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_read)->__fds_bits)[0]) : "memory"); } while (0);
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_write)->__fds_bits)[0]) : "memory"); } while (0);
  while (1) {
    ((void) (((&fds_read)->__fds_bits)[((control_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((control_fd) % (8 * (int) sizeof (__fd_mask)))))));
    if (have_stream) {
      ((void) (((&fds_read)->__fds_bits)[((stream_fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((stream_fd) % (8 * (int) sizeof (__fd_mask)))))));
      ((void) (((&fds_write)->__fds_bits)[((datagram_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((datagram_fd) % (8 * (int) sizeof (__fd_mask)))))));
    } else {
      ((void) (((&fds_read)->__fds_bits)[((stream_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((stream_fd) % (8 * (int) sizeof (__fd_mask)))))));
      ((void) (((&fds_write)->__fds_bits)[((datagram_fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((datagram_fd) % (8 * (int) sizeof (__fd_mask)))))));
    }
    if (have_datagram) {
      ((void) (((&fds_read)->__fds_bits)[((datagram_fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((datagram_fd) % (8 * (int) sizeof (__fd_mask)))))));
      ((void) (((&fds_write)->__fds_bits)[((stream_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((stream_fd) % (8 * (int) sizeof (__fd_mask)))))));
    } else {
      ((void) (((&fds_read)->__fds_bits)[((datagram_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((datagram_fd) % (8 * (int) sizeof (__fd_mask)))))));
      ((void) (((&fds_write)->__fds_bits)[((stream_fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((stream_fd) % (8 * (int) sizeof (__fd_mask)))))));
    }
    int n = select(nfds, &fds_read, &fds_write, ((void *)0), ((void *)0));
    if (((((&fds_read)->__fds_bits)[((control_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((control_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      goto exit;
    }
    if (((((&fds_read)->__fds_bits)[((stream_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((stream_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      int rv = read_stream(stream_fd, &stream_buf);
      if (rv < 0)
        goto exit;
      if (rv > 0)
        have_stream = 1;
    }
    if (((((&fds_write)->__fds_bits)[((stream_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((stream_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      int rv = write_stream(stream_fd, &datagram_buf);
      if (rv < 0)
        goto exit;
      if (rv > 0)
        have_datagram = 0;
    }
    if (((((&fds_read)->__fds_bits)[((datagram_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((datagram_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      int rv = read_datagram(datagram_fd, &datagram_buf);
      if (rv < 0)
        goto exit;
      if (rv > 0)
        have_datagram = 1;
    }
    if (((((&fds_write)->__fds_bits)[((datagram_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((datagram_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
      int rv = write_datagram(datagram_fd, &stream_buf);
      if (rv < 0)
        goto exit;
      if (rv > 0)
        have_stream = 0;
    }
  }
exit:
  close(stream_fd);
  close(datagram_fd);
  close(control_fd);
}

variable_types:
fds
int []

variable_types: nfds int

variable_types: i int

variable_types: struct buffer { unsigned long size; unsigned long progress; char data[4096]; }

variable_types: n int

variable_types: rv int


main

repo: gordio/tmux-sb

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__pid_t v3; // eax
struct stat v5; // [rsp+0h] [rbp-98h] BYREF

if ( chdir("/tmp/") )
errx(1, "Can't change directory to /tmp");
if ( stat("/tmp/tmux-sb.socket", &v5) )
{
v3 = fork();
if ( v3 < 0 )
errx(1, "Fork error.");
if ( !v3 )
start_server("tmux-sb.socket");
}
client();
return 0;
}

original_code:
int main(int argc, const char *argv[], const char *envp[])
{
 _Bool server_runned = 0;
 pid_t pid = -1;
 if (chdir("/tmp/")) {
  errx(1, "Can't change directory to /tmp");
 }
 struct stat file_stat;
 if (stat("/tmp/" "tmux-sb.socket", &file_stat) == 0) {
  server_runned = 1;
 }
 if (!server_runned) {
  pid = fork();
  if (pid < 0) {
   errx(1, "Fork error.");
  }
  if (pid == 0) {
   start_server("tmux-sb.socket");
  }
 }
 client();
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[], char *envp[])
{
 _Bool server_runned = 0;
 int pid = -1;
 if (chdir("/tmp/")) {
  errx(1, "Can't change directory to /tmp");
 }
 struct stat file_stat;
 if (stat("/tmp/" "tmux-sb.socket", &file_stat) == 0) {
  server_runned = 1;
 }
 if (!server_runned) {
  pid = fork();
  if (pid < 0) {
   errx(1, "Fork error.");
  }
  if (pid == 0) {
   start_server("tmux-sb.socket");
  }
 }
 client();
 return 0;
}

variable_types:
envp
char *[]

variable_types: pid int

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

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


client

decompiled_code:
int client()
{
_BYTE *v0; // rax
_BYTE *v1; // r12
int v2; // ebp
int v3; // ebx
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
__int16 v11; // [rsp+0h] [rbp-88h] BYREF
__m128i v12; // [rsp+2h] [rbp-86h]
__int128 v13; // [rsp+12h] [rbp-76h]
__int128 v14; // [rsp+22h] [rbp-66h]
__int128 v15; // [rsp+32h] [rbp-56h]
__int128 v16; // [rsp+42h] [rbp-46h]
__int128 v17; // [rsp+52h] [rbp-36h]
__int64 v18; // [rsp+62h] [rbp-26h]
__int16 v19; // [rsp+6Ah] [rbp-1Eh]
char v20; // [rsp+6Ch] [rbp-1Ch]

v0 = malloc(0x400uLL);
if ( !v0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
errx(-2, "Can't alloc buffer: %s", v10);
}
v1 = v0;
v2 = socket(1, 1, 0);
if ( v2 == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
errx(-2, "Can't create socket: %s", v8);
}
v20 = 0;
v3 = 0;
v11 = 1;
v18 = 0LL;
v19 = 0;
v12 = _mm_load_si128((const __m128i *)&LC5);
v13 = 0LL;
v14 = 0LL;
v15 = 0LL;
v16 = 0LL;
v17 = 0LL;
while ( connect(v2, (const struct sockaddr *)&v11, 0x6Eu) == -1 )
{
if ( v3 == 2 )
{
v5 = _errno_location();
v6 = strerror(*v5);
errx(1, "Fatal. Can't connect: %s (Try remove '%s')", v6, "tmux-sb.socket");
}
fprintf(stderr, "Can't connect. Retry %i/%i\n", (unsigned int)++v3, 3LL);
usleep(0x2710u);
}
usleep(0x2710u);
recv(v2, v1, 0x3FFuLL, 0);
close(v2);
v1[1023] = 0;
return puts(v1);
}

canonical_decompiled_code:
int func0()
{
_BYTE *v0; // rax
_BYTE *v1; // r12
int v2; // ebp
int v3; // ebx
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
__int16 v11; // [rsp+0h] [rbp-88h] BYREF
__m128i v12; // [rsp+2h] [rbp-86h]
__int128 v13; // [rsp+12h] [rbp-76h]
__int128 v14; // [rsp+22h] [rbp-66h]
__int128 v15; // [rsp+32h] [rbp-56h]
__int128 v16; // [rsp+42h] [rbp-46h]
__int128 v17; // [rsp+52h] [rbp-36h]
__int64 v18; // [rsp+62h] [rbp-26h]
__int16 v19; // [rsp+6Ah] [rbp-1Eh]
char v20; // [rsp+6Ch] [rbp-1Ch]

v0 = malloc(0x400uLL);
if ( !v0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
errx(-2, "Can't alloc buffer: %s", v10);
}
v1 = v0;
v2 = socket(1, 1, 0);
if ( v2 == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
errx(-2, "Can't create socket: %s", v8);
}
v20 = 0;
v3 = 0;
v11 = 1;
v18 = 0LL;
v19 = 0;
v12 = _mm_load_si128((const __m128i *)&LC5);
v13 = 0LL;
v14 = 0LL;
v15 = 0LL;
v16 = 0LL;
v17 = 0LL;
while ( connect(v2, (const struct sockaddr *)&v11, 0x6Eu) == -1 )
{
if ( v3 == 2 )
{
v5 = _errno_location();
v6 = strerror(*v5);
errx(1, "Fatal. Can't connect: %s (Try remove '%s')", v6, "tmux-sb.socket");
}
fprintf(stderr, "Can't connect. Retry %i/%i\n", (unsigned int)++v3, 3LL);
usleep(0x2710u);
}
usleep(0x2710u);
recv(v2, v1, 0x3FFuLL, 0);
close(v2);
v1[1023] = 0;
return puts(v1);
}

original_code:
void
client(void)
{
 int sock;
 struct sockaddr_un addr;
 char *buf;
 if (!(buf = malloc(1024))) {
  errx(-2, "Can't alloc buffer: %s", strerror((*__errno_location ())));
 }
 if (-1 == (sock = socket(1, SOCK_STREAM, 0))) {
  errx(-2, "Can't create socket: %s", strerror((*__errno_location ())));
 }
 addr.sun_family = 1;
 strncpy(addr.sun_path, "tmux-sb.socket", (sizeof addr.sun_path) - 1);
 int cur_retr = 0;
 while (cur_retr < 3) {
  if (-1 == connect(sock, (struct sockaddr *) &addr, sizeof addr)) {
   if (cur_retr + 1 < 3) {
    fprintf(stderr, "Can't connect. Retry %i/%i\n", cur_retr + 1, 3);
   } else {
    errx(1, "Fatal. Can't connect: %s (Try remove '%s')", strerror((*__errno_location ())), "tmux-sb.socket");
   }
  } else {
   cur_retr = 3;
  }
  usleep(10000);
  cur_retr++;
 }
 int len;
 len = recv(sock, buf, 1024 - 1, 0);
 close(sock);
 buf[1024 - 1] = '\0';
 printf("%s\n", buf);
}

canonical_original_code:
void client()
{
 int sock;
 struct sockaddr_un addr;
 char *buf;
 if (!(buf = malloc(1024))) {
  errx(-2, "Can't alloc buffer: %s", strerror((*__errno_location ())));
 }
 if (-1 == (sock = socket(1, SOCK_STREAM, 0))) {
  errx(-2, "Can't create socket: %s", strerror((*__errno_location ())));
 }
 addr.sun_family = 1;
 strncpy(addr.sun_path, "tmux-sb.socket", (sizeof addr.sun_path) - 1);
 int cur_retr = 0;
 while (cur_retr < 3) {
  if (-1 == connect(sock, (struct sockaddr *) &addr, sizeof addr)) {
   if (cur_retr + 1 < 3) {
    fprintf(stderr, "Can't connect. Retry %i/%i\n", cur_retr + 1, 3);
   } else {
    errx(1, "Fatal. Can't connect: %s (Try remove '%s')", strerror((*__errno_location ())), "tmux-sb.socket");
   }
  } else {
   cur_retr = 3;
  }
  usleep(10000);
  cur_retr++;
 }
 int len;
 len = recv(sock, buf, 1024 - 1, 0);
 close(sock);
 buf[1024 - 1] = '\0';
 printf("%s\n", buf);
}

variable_types: sock int

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

variable_types:
buf
char *

variable_types: len int


decompiled_code:
void __fastcall signal_handler(int a1)
{
if ( a1 != 1 )
deinit_sock_constprop_0(sock);
}

canonical_decompiled_code:
void __fastcall func1(int a1)
{
if ( a1 != 1 )
func0(sock);
}

original_code:
static void
signal_handler(int signal)
{
 switch (signal) {
  case 1:
   break;
  default:
   deinit_sock(sock, &addr);
   break;
 }
}

variable_types: signal int


repo: gosour/Yet-another-fortune

decompiled_code:
__int64 random_number()
{
time_t timer; // [rsp+8h] [rbp-10h] BYREF

time(&timer);
srand(timer);
return random();
}

canonical_decompiled_code:
__int64 func0()
{
time_t timer; // [rsp+8h] [rbp-10h] BYREF

time(&timer);
srand(timer);
return random();
}

original_code:
int random_number()
{
 time_t seconds;
 time(&seconds);
 srand((unsigned int)seconds);
 return random();
}

canonical_original_code:
int random_number()
{
 long seconds;
 time(&seconds);
 srand((unsigned int)seconds);
 return random();
}

variable_types: seconds long


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

rand = random_number();
result = (unsigned int)(rand % a1 + 1);
value = rand % a1 + 1;
return result;
}

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

rand = func0();
result = (unsigned int)(rand % a1 + 1);
value = rand % a1 + 1;
return result;
}

original_code:
int random_less_than(int max)
{
 rand=random_number();
 value=rand%max+1;
 return value;
}

variable_types: max int


decompiled_code:
__int64 __fastcall random_in_between(unsigned int a1, int a2)
{
__int64 result; // rax
unsigned int v3; // ebx
unsigned int v4; // ebp

result = a1;
if ( a1 != a2 )
{
v3 = a1;
v4 = a2;
if ( (int)a1 > a2 )
{
v3 = a2;
v4 = a1;
}
rand = random_number();
result = v3 + rand % (int)(v3 - v4 - 1) + 1;
value = v3 + rand % (int)(v3 - v4 - 1) + 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, int a2)
{
__int64 result; // rax
unsigned int v3; // ebx
unsigned int v4; // ebp

result = a1;
if ( a1 != a2 )
{
v3 = a1;
v4 = a2;
if ( (int)a1 > a2 )
{
v3 = a2;
v4 = a1;
}
rand = func0();
result = v3 + rand % (int)(v3 - v4 - 1) + 1;
value = v3 + rand % (int)(v3 - v4 - 1) + 1;
}
return result;
}

original_code:
int random_in_between(int bound1, int bound2)
{
 if(bound1==bound2)
  return bound1;
 if(bound1>bound2)
 {
  bound1=bound1+bound2;
  bound2=bound1-bound2;
  bound1=bound1-bound2;
 }
 rand=random_number();
 value=rand%(bound1-bound2-1) + bound1+1;
 return value;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
bool v6; // cf
bool v7; // zf
__int64 v8; // rcx
const char *v9; // rdi
const char *v10; // rsi
bool v11; // cf
bool v12; // zf
__int64 v13; // rcx
const char *v14; // rdi
const char *v15; // rsi
char v16; // al
int result; // eax

if ( argc == 1 )
{
if ( (unsigned int)file_open_routine(argc, argv, envp) )
{
printf("There has been an exception, quiting");
return 1;
}
result = file_print() != 0;
}
else
{
v3 = i;
v6 = argc < (unsigned int)i;
v7 = argc == i;
if ( argc <= i )
return 1;
while ( 1 )
{
while ( 1 )
{
v13 = 8LL;
v14 = "version";
v15 = argv[v3];
do
{
if ( !v13 )
break;
v6 = *v15 < (unsigned int)*v14;
v7 = *v15++ == *v14++;
--v13;
}
while ( v7 );
v16 = (!v6 && !v7) - v6;
v11 = 0;
v12 = v16 == 0;
if ( !v16 )
printf("Fortune: %d\nSourav Goswami\nThis is a free software\n", v15);
v8 = 6LL;
v9 = "reset";
v10 = argv[i];
do
{
if ( !v8 )
break;
v11 = *v10 < (unsigned int)*v9;
v12 = *v10++ == *v9++;
--v8;
}
while ( v12 );
if ( (!v11 && !v12) == v11 )
break;
v3 = i + 1;
i = v3;
v6 = v3 < (unsigned int)argc;
v7 = v3 == argc;
if ( v3 >= argc )
return 1;
}
if ( !(unsigned int)file_create_default(v9, v10) )
break;
v3 = i + 1;
i = v3;
v6 = v3 < (unsigned int)argc;
v7 = v3 == argc;
if ( v3 >= argc )
return 1;
}
printf("Unknown Error");
result = 1;
}
return result;
}

original_code:
int main(int argc, char *argv[])
{
 if(argc-1)
 {
  while(i<argc)
  {
   if(!strcmp(argv[i],"version"))
    printf("Fortune: %d\nSourav Goswami\nThis is a free software\n",1.0);
   if(!strcmp(argv[i],"reset"))
   {
    if(!file_create_default())
     {
     printf("Unknown Error");
     return 1;
     }
   }
   i++;
  }
 }
if(argc==1)
{
 if(file_open_routine())
 {
  printf("There has been an exception, quiting");
  return 1;
 }
 if(!file_print())
  return 0;
 else
  return 1;
}
 return 1;
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
__int64 file_open_routine()
{
fp = fopen("data/list", "r");
if ( fp )
return 0LL;
printf("There has been an exception, restart application");
return 1LL;
}

canonical_decompiled_code:
__int64 func0()
{
fp = fopen("data/list", "r");
if ( fp )
return 0LL;
printf("There has been an exception, restart application");
return 1LL;
}

original_code:
int file_open_routine()
{
 if((fp=fopen("data/list","r"))==((void *)0))
 {
  printf("There has been an exception, restart application");
  return 1;
 }
 else
  return 0;
}


decompiled_code:
__int64 __fastcall file_print(__int64 a1, __int64 a2)
{
unsigned int v2; // ebp
char v3; // dl
unsigned int v4; // eax
int v5; // ebp
int v6; // ebx
int v7; // ebp
__int64 result; // rax
char v9[136]; // [rsp+0h] [rbp-88h] BYREF

v2 = 0;
v3 = getc(fp);
if ( v3 == -1 )
{
printf("The data file seems to be corrupted, please rerun with \"reset\" as argument");
result = 1LL;
}
else
{
do
{
v2 += v3 == 10;
v4 = getc(fp);
v3 = v4;
}
while ( (_BYTE)v4 != 0xFF );
v5 = random_less_than(v2, a2, v4);
rewind(fp);
if ( v5 != 1 )
{
v6 = 0;
v7 = v5 - 1;
if ( v7 )
{
do
{
++v6;
fgets(v9, 99, fp);
}
while ( v7 != v6 );
}
}
fgets(v9, 99, fp);
fputs(v9, stdout);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
unsigned int v2; // ebp
char v3; // dl
unsigned int v4; // eax
int v5; // ebp
int v6; // ebx
int v7; // ebp
__int64 result; // rax
char v9[136]; // [rsp+0h] [rbp-88h] BYREF

v2 = 0;
v3 = getc(fp);
if ( v3 == -1 )
{
printf("The data file seems to be corrupted, please rerun with \"reset\" as argument");
result = 1LL;
}
else
{
do
{
v2 += v3 == 10;
v4 = getc(fp);
v3 = v4;
}
while ( (_BYTE)v4 != 0xFF );
v5 = random_less_than(v2, a2, v4);
rewind(fp);
if ( v5 != 1 )
{
v6 = 0;
v7 = v5 - 1;
if ( v7 )
{
do
{
++v6;
fgets(v9, 99, fp);
}
while ( v7 != v6 );
}
}
fgets(v9, 99, fp);
fputs(v9, stdout);
result = 1LL;
}
return result;
}

original_code:
int file_print()
{
 int count_lines=0;
 int random_line;
 char ch_traverse;
 int count=0;
 char str[100];
 ch_traverse=getc(fp);
 if(ch_traverse==(-1))
 {
  printf("The data file seems to be corrupted, please rerun with \"reset\" as argument");
  return 1;
 }
 while(ch_traverse!=(-1))
 {
  if(ch_traverse=='\n')
   count_lines++;
  ch_traverse=getc(fp);
 }
 random_line=random_less_than(count_lines);
 rewind(fp);
 if(random_line==1)
  fgets(str,99,fp);
 else
 {
  while(count!=random_line-1)
  {
   fgets(str,99,fp);
   count++;
  }
 fgets(str,99,fp);
 }
 fputs(str,stdout);
 return 1;
}

canonical_original_code:
int file_print()
{
 int count_lines = 0;
 int random_line;
 char ch_traverse;
 int count = 0;
 char str[100];
 ch_traverse=getc(fp);
 if(ch_traverse==(-1))
 {
  printf("The data file seems to be corrupted, please rerun with \"reset\" as argument");
  return 1;
 }
 while(ch_traverse!=(-1))
 {
  if(ch_traverse=='\n')
   count_lines++;
  ch_traverse=getc(fp);
 }
 random_line=random_less_than(count_lines);
 rewind(fp);
 if(random_line==1)
  fgets(str,99,fp);
 else
 {
  while(count!=random_line-1)
  {
   fgets(str,99,fp);
   count++;
  }
 fgets(str,99,fp);
 }
 fputs(str,stdout);
 return 1;
}

variable_types: count int

variable_types:
str
char [100]


decompiled_code:
__int64 file_create_default()
{
FILE *v0; // rax
__int64 result; // rax

v0 = fopen("data/list", "w+");
fp = v0;
if ( v0 )
{
fwrite(
"You sir are a collosal faggot\n"
"Do not argue with an idiot.He will drag you down to his level and beat you with his experience\n"
"Make it correct,make it clear,make it concise,make it fast.In that order.\n"
"\"Somewhere something incredible is waiting to be known\"- Carl Sagan\n"
"\"Charmender is Red, Squirtle is Blue, If you are a pokemon I would choose you\"\n"
"\"I would walk into Mordor for you\"",
1uLL,
0x17CuLL,
v0);
result = 1LL;
}
else
{
printf("There has been an exception, restart application");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
FILE *v0; // rax
__int64 result; // rax

v0 = fopen("data/list", "w+");
fp = v0;
if ( v0 )
{
fwrite(
"You sir are a collosal faggot\n"
"Do not argue with an idiot.He will drag you down to his level and beat you with his experience\n"
"Make it correct,make it clear,make it concise,make it fast.In that order.\n"
"\"Somewhere something incredible is waiting to be known\"- Carl Sagan\n"
"\"Charmender is Red, Squirtle is Blue, If you are a pokemon I would choose you\"\n"
"\"I would walk into Mordor for you\"",
1uLL,
0x17CuLL,
v0);
result = 1LL;
}
else
{
printf("There has been an exception, restart application");
result = 0LL;
}
return result;
}

original_code:
int file_create_default()
{
 if((fp=fopen("data/list","w+"))==((void *)0))
 {
  printf("There has been an exception, restart application");
  return 0;
 }
 fputs("You sir are a collosal faggot\nDo not argue with an idiot.He will drag you down to his level and beat you with his experience\nMake it correct,make it clear,make it concise,make it fast.In that order.\n\"Somewhere something incredible is waiting to be known\"- Carl Sagan\n\"Charmender is Red, Squirtle is Blue, If you are a pokemon I would choose you\"\n\"I would walk into Mordor for you\"",fp);
 return 1;
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
__int64 v4; // rbx
int v5; // er15
void *v6; // r13
char *v7; // r12
_QWORD *v8; // rax
unsigned int v9; // er15
unsigned int v10; // ebx
bool v11; // cf
int result; // eax
int v13; // [rsp+Ch] [rbp-94h]
_QWORD *v14; // [rsp+18h] [rbp-88h] BYREF
void *ptr; // [rsp+20h] [rbp-80h] BYREF
void *v16; // [rsp+28h] [rbp-78h] BYREF
char s[32]; // [rsp+30h] [rbp-70h] BYREF
char v18[80]; // [rsp+50h] [rbp-50h] BYREF

if ( argc == 2 )
{
v3 = atoi(argv[1]);
v13 = v3 - 1;
if ( (unsigned int)(v3 - 1) > 0x1869F )
{
v13 = 99999;
v3 = 100000;
}
v4 = 0LL;
v5 = 2 * v3;
v6 = (void *)hash_create(hash_fn, hash_strcmp);
puts("\nInsert phase:");
ptr = 0LL;
v16 = 0LL;
do
{
while ( 1 )
{
v7 = (char *)malloc(0x20uLL);
snprintf(v7, 0x20uLL, "String %d", (unsigned int)((int)v4 % v3));
v8 = malloc(8uLL);
*v8 = v4;
v14 = v8;
hash_insert(v6, v7, v8, &ptr, &v16);
if ( !v16 )
break;
++v4;
printf("Replaced (%s, %li) while inserting (%s, %li)\n", (const char *)ptr, *(_QWORD *)v16, v7, *v14);
free(ptr);
free(v16);
if ( v4 == v5 )
goto LABEL_8;
}
++v4;
printf("Inserted (%s, %li)\n", v7, *v14);
}
while ( v4 != v5 );
LABEL_8:
puts("\nLookup phase:");
v9 = v13;
v10 = v13;
do
{
while ( 1 )
{
snprintf(s, 0x20uLL, "String %d", v10);
if ( (unsigned __int8)hash_lookup(v6, s, &v14) )
break;
printf("Entry for %s not found\n", s);
v11 = v10-- == 0;
if ( v11 )
goto LABEL_12;
}
printf("%s -> %li\n", s, *v14);
v11 = v10-- == 0;
}
while ( !v11 );
LABEL_12:
if ( (unsigned __int8)hash_lookup(v6, "not-found key", &v14) )
printf("%s -> %li (unexpected!)\n", "not-found key", *v14);
else
printf("Lookup of \"%s\" failed (as expected)\n", "not-found key");
hash_remove(v6, "String 22", &ptr, &v16);
free(ptr);
free(v16);
hash_remove(v6, "String 69", &ptr, &v16);
free(ptr);
free(v16);
puts("\nLookup phase:");
do
{
while ( 1 )
{
snprintf(v18, 0x20uLL, "String %d", v9);
if ( (unsigned __int8)hash_lookup(v6, v18, &v14) )
break;
printf("Entry for %s not found\n", v18);
if ( --v9 == -1 )
goto LABEL_18;
}
printf("%s -> %li\n", v18, *v14);
--v9;
}
while ( v9 != -1 );
LABEL_18:
hash_destroy(v6);
result = 0;
}
else
{
printf("Usage: %s <N>\nRun test inserting a total of N items\n", *argv);
result = 1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er14
__int64 v4; // rbx
int v5; // er15
void *v6; // r13
char *v7; // r12
_QWORD *v8; // rax
unsigned int v9; // er15
unsigned int v10; // ebx
bool v11; // cf
int result; // eax
int v13; // [rsp+Ch] [rbp-94h]
_QWORD *v14; // [rsp+18h] [rbp-88h] BYREF
void *ptr; // [rsp+20h] [rbp-80h] BYREF
void *v16; // [rsp+28h] [rbp-78h] BYREF
char s[32]; // [rsp+30h] [rbp-70h] BYREF
char v18[80]; // [rsp+50h] [rbp-50h] BYREF

if ( argc == 2 )
{
v3 = atoi(argv[1]);
v13 = v3 - 1;
if ( (unsigned int)(v3 - 1) > 0x1869F )
{
v13 = 99999;
v3 = 100000;
}
v4 = 0LL;
v5 = 2 * v3;
v6 = (void *)hash_create(hash_fn, hash_strcmp);
puts("\nInsert phase:");
ptr = 0LL;
v16 = 0LL;
do
{
while ( 1 )
{
v7 = (char *)malloc(0x20uLL);
snprintf(v7, 0x20uLL, "String %d", (unsigned int)((int)v4 % v3));
v8 = malloc(8uLL);
*v8 = v4;
v14 = v8;
hash_insert(v6, v7, v8, &ptr, &v16);
if ( !v16 )
break;
++v4;
printf("Replaced (%s, %li) while inserting (%s, %li)\n", (const char *)ptr, *(_QWORD *)v16, v7, *v14);
free(ptr);
free(v16);
if ( v4 == v5 )
goto LABEL_8;
}
++v4;
printf("Inserted (%s, %li)\n", v7, *v14);
}
while ( v4 != v5 );
LABEL_8:
puts("\nLookup phase:");
v9 = v13;
v10 = v13;
do
{
while ( 1 )
{
snprintf(s, 0x20uLL, "String %d", v10);
if ( (unsigned __int8)func9(v6, s, &v14) )
break;
printf("Entry for %s not found\n", s);
v11 = v10-- == 0;
if ( v11 )
goto LABEL_12;
}
printf("%s -> %li\n", s, *v14);
v11 = v10-- == 0;
}
while ( !v11 );
LABEL_12:
if ( (unsigned __int8)func9(v6, "not-found key", &v14) )
printf("%s -> %li (unexpected!)\n", "not-found key", *v14);
else
printf("Lookup of \"%s\" failed (as expected)\n", "not-found key");
func13(v6, "String 22", &ptr, &v16);
free(ptr);
free(v16);
func13(v6, "String 69", &ptr, &v16);
free(ptr);
free(v16);
puts("\nLookup phase:");
do
{
while ( 1 )
{
snprintf(v18, 0x20uLL, "String %d", v9);
if ( (unsigned __int8)func9(v6, v18, &v14) )
break;
printf("Entry for %s not found\n", v18);
if ( --v9 == -1 )
goto LABEL_18;
}
printf("%s -> %li\n", v18, *v14);
--v9;
}
while ( v9 != -1 );
LABEL_18:
func11(v6);
result = 0;
}
else
{
printf("Usage: %s <N>\nRun test inserting a total of N items\n", *argv);
result = 1;
}
return result;
}

original_code:
int main(int argc, char * argv[], char * envp[]){
  int exit_stat = 0;
  while(1){
    char* input = readline("CSE451Shell\% ");
    if(!strcmp(input, "")){
      free(input);
      continue;
    }
    exit_stat = exec_command(input, exit_stat);
    free(input);
  }
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[], char *envp[]){
  int exit_stat = 0;
  while(1){
    char *input = readline("CSE451Shell\% ");
    if(!strcmp(input, "")){
      free(input);
      continue;
    }
    exit_stat = exec_command(input, exit_stat);
    free(input);
  }
  return 0;
}

variable_types:
input
char *


original_code:
static uint64_t hash_fn(const void* k) {
  uint64_t hash_val = 0;
  uint64_t coefficient = 1;
  for (const char* p = (const char*) k; *p != '\0'; p++) {
    hash_val += coefficient * (*p);
    coefficient *= 37;
  }
  return hash_val;
}

canonical_original_code:
static unsigned long hash_fn(void *k) {
  unsigned long hash_val = 0;
  unsigned long coefficient = 1;
  for (char *p = (const char*) k; *p != '\0'; p++) {
    hash_val += coefficient * (*p);
    coefficient *= 37;
  }
  return hash_val;
}

variable_types:
k
void *

variable_types:
coefficient
unsigned long

variable_types:
p
char *

return_type: unsigned long


decompiled_code:
// attributes: thunk
int hash_strcmp(const char *s1, const char *s2)
{
return strcmp(s1, s2);
}

canonical_decompiled_code:
int func8(const char *s1, const char *s2)
{
return strcmp(s1, s2);
}

original_code:
static int hash_strcmp(const void* k1, const void* k2) {
  return strcmp((const char*) k1, (const char*) k2);
}

canonical_original_code:
static int hash_strcmp(void *k1, void *k2) {
  return strcmp((char *) k1, (char *) k2);
}


original_code:
_Bool hash_lookup(hash_table* ht, const void* key, void** value_ptr) {
  uint64_t idx = ht->hasher(key);
  while (ht->buckets[idx % ht->num_buckets]) {
    hash_entry *cur = ht->buckets[idx % ht->num_buckets];
    if (!ht->hc(cur->key, key)) {
      *value_ptr = cur->value;
      return 1;
    }
    idx++;
  }
  return 0;
}

canonical_original_code:
_Bool hash_lookup(struct _hash_table *ht, void *key, void **value_ptr) {
  unsigned long idx = ht->hasher(key);
  while (ht->buckets[idx % ht->num_buckets]) {
    struct _hash_entry *cur = ht->buckets[idx % ht->num_buckets];
    if (!ht->hc(cur->key, key)) {
      *value_ptr = cur->value;
      return 1;
    }
    idx++;
  }
  return 0;
}

variable_types: struct _hash_table { unsigned long num_buckets; unsigned long num_elements; struct _hash_entry { void *key; void *value; } **buckets; unsigned long (*hasher)(void *); int (*hc)(void *, void *); } *

variable_types:
key
void *

variable_types:
idx
unsigned long

user_defined_types: struct _hash_table { unsigned long num_buckets; unsigned long num_elements; struct _hash_entry **buckets; unsigned long (*hasher)(void *); int (*hc)(void *, void *); }


decompiled_code:
__int64 __fastcall hash_is_present(__int64 a1, __int64 a2)
{
__int64 v3; // [rsp+8h] [rbp-10h] BYREF

return hash_lookup(a1, a2, &v3);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2)
{
__int64 v3; // [rsp+8h] [rbp-10h] BYREF

return func9(a1, a2, &v3);
}

original_code:
_Bool hash_is_present(hash_table* ht, const void* key) {
  void *dummy;
  return hash_lookup(ht, key, &dummy);
}

canonical_original_code:
_Bool hash_is_present(struct _hash_table *ht, void *key) {
  void *dummy;
  return hash_lookup(ht, key, &dummy);
}

variable_types:
dummy
void *

user_defined_types: struct _hash_entry { void *key; void *value; }


decompiled_code:
void __fastcall hash_destroy(void *ptr, char a2, char a3)
{
bool v4; // zf
_QWORD *v5; // rdi
unsigned __int64 i; // rbx
void **v8; // rbp

v4 = *(_QWORD *)ptr == 0LL;
v5 = (_QWORD *)*((_QWORD *)ptr + 2);
if ( !v4 )
{
for ( i = 0LL; *(_QWORD *)ptr > i; ++i )
{
v8 = (void **)v5[i];
if ( !v8 )
continue;
if ( a2 )
{
free(*v8);
if ( a3 )
{
LABEL_9:
free(v8[1]);
goto LABEL_4;
}
}
else if ( a3 )
{
goto LABEL_9;
}
LABEL_4:
free(v8);
v5 = (_QWORD *)*((_QWORD *)ptr + 2);
}
}
free(v5);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func11(void *ptr, char a2, char a3)
{
bool v4; // zf
_QWORD *v5; // rdi
unsigned __int64 i; // rbx
void **v8; // rbp

v4 = *(_QWORD *)ptr == 0LL;
v5 = (_QWORD *)*((_QWORD *)ptr + 2);
if ( !v4 )
{
for ( i = 0LL; *(_QWORD *)ptr > i; ++i )
{
v8 = (void **)v5[i];
if ( !v8 )
continue;
if ( a2 )
{
free(*v8);
if ( a3 )
{
LABEL_9:
free(v8[1]);
goto LABEL_4;
}
}
else if ( a3 )
{
goto LABEL_9;
}
LABEL_4:
free(v8);
v5 = (_QWORD *)*((_QWORD *)ptr + 2);
}
}
free(v5);
free(ptr);
}

original_code:
void hash_destroy(hash_table* ht, _Bool free_keys, _Bool free_values) {
  for (int i = 0; i < ht->num_buckets; i++) {
    hash_entry *cur = ht->buckets[i];
    if (cur) {
      if (free_keys) free(cur->key);
      if (free_values) free(cur->value);
      free(cur);
    }
  }
  free(ht->buckets);
  free(ht);
}

canonical_original_code:
void hash_destroy(struct _hash_table *ht, _Bool free_keys, _Bool free_values) {
  for (int i = 0; i < ht->num_buckets; i++) {
    struct _hash_entry *cur = ht->buckets[i];
    if (cur) {
      if (free_keys) free(cur->key);
      if (free_values) free(cur->value);
      free(cur);
    }
  }
  free(ht->buckets);
  free(ht);
}

variable_types: i int

variable_types: struct _hash_entry { void *key; void *value; } *


rehash

original_code:
void rehash(hash_table* ht) {
  hash_entry **oldbuckets = ht->buckets;
  ht->num_buckets = ht->num_buckets * 2;
  ht->num_elements = 0;
  ht->buckets = (hash_entry **) calloc(ht->num_buckets,
                                       sizeof(oldbuckets));
  void *dummykey, *dummyval;
  for (int i = 0; i < ht->num_buckets / 2; i++) {
    hash_entry *cur = oldbuckets[i];
    if (cur) {
      hash_insert(ht, cur->key, cur->value, &dummykey, &dummyval);
      free(cur);
    }
  }
  free(oldbuckets);
}

canonical_original_code:
void rehash(struct _hash_table *ht) {
  struct _hash_entry **oldbuckets = ht->buckets;
  ht->num_buckets = ht->num_buckets * 2;
  ht->num_elements = 0;
  ht->buckets = (struct _hash_entry **) calloc(ht->num_buckets,
                                       sizeof(oldbuckets));
  void *dummykey; void *dummyval;
  for (int i = 0; i < ht->num_buckets / 2; i++) {
    struct _hash_entry *cur = oldbuckets[i];
    if (cur) {
      hash_insert(ht, cur->key, cur->value, &dummykey, &dummyval);
      free(cur);
    }
  }
  free(oldbuckets);
}

variable_types: struct _hash_entry { void *key; void *value; } **

variable_types:
dummykey
void *

variable_types:
dummyval
void *


original_code:
_Bool hash_remove(hash_table* ht, const void* key,
                 void** removed_key_ptr, void** removed_value_ptr) {
  uint64_t idx = ht->hasher(key);
  while (ht->buckets[idx % ht->num_buckets]) {
    hash_entry *cur = ht->buckets[idx % ht->num_buckets];
    if (!ht->hc(cur->key, key)) {
      *removed_key_ptr = cur->key;
      *removed_value_ptr = cur->value;
      free(cur);
      ht->buckets[idx % ht->num_buckets] = ((void *)0);
      ht->num_elements--;
      idx++;
      void *dummykey, *dummyval;
      while (ht->buckets[idx % ht->num_buckets]) {
        ht->num_elements--;
        cur = ht->buckets[idx % ht->num_buckets];
        ht->buckets[idx % ht->num_buckets] = ((void *)0);
        hash_insert(ht, cur->key, cur->value, &dummykey, &dummyval);
        free(cur);
        idx++;
      }
      return 1;
    }
    idx++;
  }
  return 0;
}

canonical_original_code:
_Bool hash_remove(struct _hash_table *ht, void *key, void **removed_key_ptr, void **removed_value_ptr) {
  unsigned long idx = ht->hasher(key);
  while (ht->buckets[idx % ht->num_buckets]) {
    struct _hash_entry *cur = ht->buckets[idx % ht->num_buckets];
    if (!ht->hc(cur->key, key)) {
      *removed_key_ptr = cur->key;
      *removed_value_ptr = cur->value;
      free(cur);
      ht->buckets[idx % ht->num_buckets] = ((void *)0);
      ht->num_elements--;
      idx++;
      void *dummykey; void *dummyval;
      while (ht->buckets[idx % ht->num_buckets]) {
        ht->num_elements--;
        cur = ht->buckets[idx % ht->num_buckets];
        ht->buckets[idx % ht->num_buckets] = ((void *)0);
        hash_insert(ht, cur->key, cur->value, &dummykey, &dummyval);
        free(cur);
        idx++;
      }
      return 1;
    }
    idx++;
  }
  return 0;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v3; // rbp
__int64 v4; // rax
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax
__int64 v8; // rax
int v10; // [rsp+4h] [rbp-24h] BYREF
int v11; // [rsp+8h] [rbp-20h] BYREF
int v12; // [rsp+Ch] [rbp-1Ch] BYREF
int v13; // [rsp+10h] [rbp-18h] BYREF
int v14; // [rsp+14h] [rbp-14h] BYREF
char v15[16]; // [rsp+18h] [rbp-10h] BYREF

v10 = 4;
v3 = (void *)queue_create(argc, argv, envp);
v11 = 3;
v12 = 4;
v13 = 3;
queue_append(v3, &v10);
queue_append(v3, &v11);
queue_append(v3, &v12);
queue_append(v3, &v13);
v4 = queue_size(v3);
printf("Queue size is %zu\n", v4);
v14 = 0;
queue_apply(v3, show_one, &v14);
queue_reverse(v3);
v5 = queue_size(v3);
printf("Queue size is %zu\n", v5);
v14 = 0;
queue_apply(v3, show_one, &v14);
queue_sort(v3, compare_two);
v6 = queue_size(v3);
printf("Queue size is %zu\n", v6);
v14 = 0;
queue_apply(v3, show_one, &v14);
queue_remove(v3, v15);
v7 = queue_size(v3);
printf("Queue size is %zu\n", v7);
v14 = 0;
queue_apply(v3, show_one, &v14);
while ( !(unsigned __int8)queue_is_empty(v3) )
queue_remove(v3, v15);
v8 = queue_size(v3);
printf("Queue size is %zu\n", v8);
free(v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v3; // rbp
__int64 v4; // rax
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax
__int64 v8; // rax
int v10; // [rsp+4h] [rbp-24h] BYREF
int v11; // [rsp+8h] [rbp-20h] BYREF
int v12; // [rsp+Ch] [rbp-1Ch] BYREF
int v13; // [rsp+10h] [rbp-18h] BYREF
int v14; // [rsp+14h] [rbp-14h] BYREF
char v15[16]; // [rsp+18h] [rbp-10h] BYREF

v10 = 4;
v3 = (void *)func10(argc, argv, envp);
v11 = 3;
v12 = 4;
v13 = 3;
queue_append(v3, &v10);
queue_append(v3, &v11);
queue_append(v3, &v12);
queue_append(v3, &v13);
v4 = func14(v3);
printf("Queue size is %zu\n", v4);
v14 = 0;
func13(v3, show_one, &v14);
func15(v3);
v5 = func14(v3);
printf("Queue size is %zu\n", v5);
v14 = 0;
func13(v3, show_one, &v14);
func16(v3, compare_two);
v6 = func14(v3);
printf("Queue size is %zu\n", v6);
v14 = 0;
func13(v3, show_one, &v14);
func12(v3, v15);
v7 = func14(v3);
printf("Queue size is %zu\n", v7);
v14 = 0;
func13(v3, show_one, &v14);
while ( !(unsigned __int8)func11(v3) )
func12(v3, v15);
v8 = func14(v3);
printf("Queue size is %zu\n", v8);
free(v3);
return 0;
}

variable_types: argc int

variable_types:
envp
char *[]


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

result = 0xFFFFFFFFLL;
if ( *a1 >= *a2 )
result = *a1 != *a2;
return result;
}

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

result = 0xFFFFFFFFLL;
if ( *a1 >= *a2 )
result = *a1 != *a2;
return result;
}

original_code:
int compare_two(queue_element* e1, queue_element* e2) {
  int x = *(int*) e1;
  int y = *(int*) e2;
  if (x < y)
    return -1;
  else if (x == y)
    return 0;
  else
    return 1;
}

canonical_original_code:
int compare_two(void *e1, void *e2) {
  int x = *(int*) e1;
  int y = *(int*) e2;
  if (x < y)
    return -1;
  else if (x == y)
    return 0;
  else
    return 1;
}

variable_types: x int

variable_types: y int


decompiled_code:
__int64 __fastcall show_one(unsigned int *a1, unsigned int *a2)
{
printf("Item %d == %d\n", *a2, *a1);
++*a2;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int *a1, unsigned int *a2)
{
printf("Item %d == %d\n", *a2, *a1);
++*a2;
return 1LL;
}

original_code:
_Bool show_one(queue_element* elem, queue_function_args* args) {
  printf("Item %d == %d\n", *(int*) args, *(int*) elem);
  *(int*) args = *(int*) args + 1;
  return 1;
}

canonical_original_code:
_Bool show_one(void *elem, void *args) {
  printf("Item %d == %d\n", *(int *) args, *(int *) elem);
  *(int *) args = *(int *) args + 1;
  return 1;
}

variable_types:
elem
void *

variable_types:
args
void *


decompiled_code:
__int64 __fastcall queue_count_one(__int64 a1, _QWORD *a2)
{
++*a2;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, _QWORD *a2)
{
++*a2;
return 1LL;
}

original_code:
static _Bool queue_count_one(queue_element* elem, queue_function_args* args) {
  size_t* count = (size_t*) args;
  *count = *count + 1;
  return 1;
}

canonical_original_code:
static _Bool queue_count_one(void *elem, void *args) {
  unsigned long *count = (size_t*) args;
  *count = *count + 1;
  return 1;
}

variable_types:
count
unsigned long *