decompiled_code:
int __fastcall get_client_ip(int a1, char *a2)
{
unsigned __int16 v2; // bx
char *v3; // rdx
socklen_t v4; // ecx
socklen_t len; // [rsp+Ch] [rbp-ACh] BYREF
char v7[16]; // [rsp+10h] [rbp-A8h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-98h] BYREF

len = 128;
getpeername(a1, &addr, &len);
v2 = _bswap_16(*(__int16 *)addr.sa_data);
inet_ntop(2, &addr.sa_data[2], v3, v4);
return snprintf(a2, 0x15uLL, "%s:%i", v7, v2);
}

canonical_decompiled_code:
int __fastcall func5(int a1, char *a2)
{
unsigned __int16 v2; // bx
char *v3; // rdx
socklen_t v4; // ecx
socklen_t len; // [rsp+Ch] [rbp-ACh] BYREF
char v7[16]; // [rsp+10h] [rbp-A8h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-98h] BYREF

len = 128;
getpeername(a1, &addr, &len);
v2 = func0(*(__int16 *)addr.sa_data);
inet_ntop(2, &addr.sa_data[2], v3, v4);
return snprintf(a2, 0x15uLL, "%s:%i", v7, v2);
}

original_code:
void get_client_ip(int clientSock, char *buf){
 socklen_t len;
 struct sockaddr_storage addr;
 char ip[16];
 int port;
 len = sizeof addr;
 getpeername(clientSock, (struct sockaddr*)&addr, &len);
 struct sockaddr_in *s = (struct sockaddr_in *)&addr;
    port = ntohs(s->sin_port);
    inet_ntop(2, &s->sin_addr, ip, sizeof(ip));
    snprintf(buf, sizeof(ip)+5, "%s:%i", ip, port);
}

canonical_original_code:
void get_client_ip(int clientSock, char *buf){
 unsigned int len;
 struct sockaddr_storage addr;
 char ip[16];
 int port;
 len = sizeof addr;
 getpeername(clientSock, (struct sockaddr *)&addr, &len);
 struct sockaddr_in *s = (struct sockaddr_in *)&addr;
    port = ntohs(s->sin_port);
    inet_ntop(2, &s->sin_addr, ip, sizeof(ip));
    snprintf(buf, sizeof(ip)+5, "%s:%i", ip, port);
}

variable_types: clientSock int

variable_types:
buf
char *

variable_types:
len
unsigned int

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }

variable_types:
ip
char [16]

variable_types: port 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]; } *


repo: pstuifzand/meta-ii-parser

original_code:
void generate_label(int lbl)
{
    int c = lbl % 10;
    lbl -= c;
    lbl /= 10;
    int b = lbl % 10;
    lbl -= b;
    lbl /= 10;
    int a = lbl % 26;
    printf("%c%c%c\n", a+'A', b+'0', c+'0');
}

variable_types: lbl int

variable_types: c int

variable_types: b int

variable_types: a int

return_type: void


label

decompiled_code:
// attributes: thunk
int label(const char *s)
{
return puts(s);
}

canonical_decompiled_code:
int func8(const char *s)
{
return puts(s);
}

original_code:
void label(char* s)
{
    printf("%s\n",s);
}

canonical_original_code:
void label(char *s)
{
    printf("%s\n",s);
}

variable_types:
s
char *


decompiled_code:
int __fastcall out1(const char *a1)
{
return printf(" %-3s\n", a1);
}

canonical_decompiled_code:
int __fastcall func9(const char *a1)
{
return printf(" %-3s\n", a1);
}

original_code:
void out1(char* p)
{
    printf("        %-3s\n",p);
}

canonical_original_code:
void out1(char *p)
{
    printf("        %-3s\n",p);
}

variable_types:
p
char *


decompiled_code:
int __fastcall out2(const char *a1, const char *a2)
{
printf(" %-3s ", a1);
if ( a2 )
printf("%s", a2);
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func10(const char *a1, const char *a2)
{
printf(" %-3s ", a1);
if ( a2 )
printf("%s", a2);
return putchar(10);
}

original_code:
void out2(char* p, char* s)
{
    printf("        %-3s ",p);
    if (s) {
        printf("%s", s);
    }
    printf("\n");
}

canonical_original_code:
void out2(char *p, char *s)
{
    printf("        %-3s ",p);
    if (s) {
        printf("%s", s);
    }
    printf("\n");
}


decompiled_code:
int __fastcall out3(const char *a1, int a2)
{
printf(" %-3s ", a1);
return generate_label(a2);
}

canonical_decompiled_code:
int __fastcall func11(const char *a1, int a2)
{
printf(" %-3s ", a1);
return func7(a2);
}

original_code:
void out3(char* p, int lbl)
{
    printf("        %-3s ",p);
    generate_label(lbl);
}

canonical_original_code:
void out3(char *p, int lbl)
{
    printf("        %-3s ",p);
    generate_label(lbl);
}


repo: ptarcher/FIT-decode

decompiled_code:
__int64 __fastcall check_header(unsigned __int8 *a1)
{
if ( *a1 != 12 )
{
fprintf(stderr, "Not a .FIT header (%d != header_length = %ld)\n", *a1, 12LL);
exit(1);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1)
{
if ( *a1 != 12 )
{
fprintf(stderr, "Not a .FIT header (%d != header_length = %ld)\n", *a1, 12LL);
exit(1);
}
return 0LL;
}

original_code:
int check_header(struct fit_header *hdr) {
    if (hdr->h_len != sizeof(*hdr)) {
        fprintf(stderr,
            "Not a .FIT header (%d != header_length = %ld)\n",
            hdr->h_len,
            sizeof(*hdr));
        exit(1);
    } else if (0) {
        fprintf(stderr,
            "Not a .FIT header (%d != signature '.FIT')\n",
            hdr->h_len);
        exit(1);
    }
    return 0;
}

variable_types: struct fit_header { unsigned char h_len; unsigned char protocol_version; unsigned char profile_version; unsigned char xxx2; unsigned int file_size; unsigned char sig[4]; } *

return_type: int


decompiled_code:
__int64 __fastcall fetch_record_definition(char a1)
{
_QWORD *v2; // rbp
__int64 v3; // rax
__int64 v4; // rbx
char v5; // r13
char *v6; // rax
char *v7; // rdi
__int64 v8; // r13

v2 = calloc(1uLL, 0x18uLL);
if ( (unsigned int)fread((char *)v2 + 1, 5uLL, 1uLL, fp) != 1 )
{
fwrite("Unable to read fit_record_desc\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
v3 = *((unsigned __int8 *)v2 + 5);
nbytes += 5;
v4 = 0LL;
v5 = v3;
v6 = (char *)calloc(1uLL, 3 * v3);
v2[1] = v6;
v7 = v6;
if ( v5 )
{
do
{
v8 = 3 * v4;
if ( (unsigned int)fread(&v7[3 * v4], 3uLL, 1uLL, fp) != 1 )
{
fwrite("Unable to read fit_field_desc\n", 1uLL, 0x1EuLL, stderr);
exit(1);
}
v7 = (char *)v2[1];
nbytes += 3;
++v4;
v2[2] += (unsigned __int8)v7[v8 + 1];
}
while ( *((unsigned __int8 *)v2 + 5) > (int)v4 );
}
*(_BYTE *)v2 = a1;
return insert_definition(v2);
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1)
{
_QWORD *v2; // rbp
__int64 v3; // rax
__int64 v4; // rbx
char v5; // r13
char *v6; // rax
char *v7; // rdi
__int64 v8; // r13

v2 = calloc(1uLL, 0x18uLL);
if ( (unsigned int)fread((char *)v2 + 1, 5uLL, 1uLL, fp) != 1 )
{
fwrite("Unable to read fit_record_desc\n", 1uLL, 0x1FuLL, stderr);
exit(1);
}
v3 = *((unsigned __int8 *)v2 + 5);
nbytes += 5;
v4 = 0LL;
v5 = v3;
v6 = (char *)calloc(1uLL, 3 * v3);
v2[1] = v6;
v7 = v6;
if ( v5 )
{
do
{
v8 = 3 * v4;
if ( (unsigned int)fread(&v7[3 * v4], 3uLL, 1uLL, fp) != 1 )
{
fwrite("Unable to read fit_field_desc\n", 1uLL, 0x1EuLL, stderr);
exit(1);
}
v7 = (char *)v2[1];
nbytes += 3;
++v4;
v2[2] += (unsigned __int8)v7[v8 + 1];
}
while ( *((unsigned __int8 *)v2 + 5) > (int)v4 );
}
*(_BYTE *)v2 = a1;
return insert_definition(v2);
}

original_code:
void fetch_record_definition(uint8_t record_num) {
    int nelms;
    struct fit_definition *def;
    def = calloc(1, sizeof(*def));
    if ((nelms = fread(&(def->record), sizeof(def->record), 1, fp)) != 1) {
        fprintf(stderr, "Unable to read fit_record_desc\n");
        exit(1);
    }
    nbytes += sizeof(def->record);
    def->fields = calloc(1, sizeof(*def->fields)*def->record.nfields);
    int i;
    for (i = 0; i < def->record.nfields; i++) {
        if ((nelms = fread(&(def->fields[i]), sizeof(*(def->fields)), 1, fp)) != 1) {
            fprintf(stderr, "Unable to read fit_field_desc\n");
            exit(1);
        }
        nbytes += sizeof(*(def->fields));
        def->record_size += def->fields[i].size;
    }
    def->record_num = record_num;
    insert_definition(def);
}

canonical_original_code:
void fetch_record_definition(unsigned char record_num) {
    int nelms;
    struct fit_definition *def;
    def = calloc(1, sizeof(*def));
    if ((nelms = fread(&(def->record), sizeof(def->record), 1, fp)) != 1) {
        fprintf(stderr, "Unable to read fit_record_desc\n");
        exit(1);
    }
    nbytes += sizeof(def->record);
    def->fields = calloc(1, sizeof(*def->fields)*def->record.nfields);
    int i;
    for (i = 0; i < def->record.nfields; i++) {
        if ((nelms = fread(&(def->fields[i]), sizeof(*(def->fields)), 1, fp)) != 1) {
            fprintf(stderr, "Unable to read fit_field_desc\n");
            exit(1);
        }
        nbytes += sizeof(*(def->fields));
        def->record_size += def->fields[i].size;
    }
    def->record_num = record_num;
    insert_definition(def);
}

variable_types: nelms int

variable_types: struct fit_definition { unsigned char record_num; struct fit_record_definition { unsigned char reserved; unsigned char endian; unsigned short msg_num; unsigned char nfields; } record; struct fit_field_desc { unsigned char index; unsigned char size; unsigned char type; } *fields; unsigned long record_size; } *

variable_types: i int

user_defined_types: struct fit_definition { unsigned char record_num; struct fit_record_definition record; struct fit_field_desc *fields; unsigned long record_size; }


original_code:
int read_data(void *data, size_t len, struct fit_definition *def, struct fit_record_data *record_data, int record_data_nelms) {
    int i, j;
    int nelms;
    size_t offset;
    uint8_t *buf = data;
    for (i = 0, offset = 0; i < def->record.nfields; i++) {
        if (record_data) {
            for (j = 0; j < record_data_nelms; j++) {
                if (record_data[j].index == def->fields[i].index) {
                    offset = record_data[j].offset;
                    break;
                }
            }
        }
        if (record_data == ((void *)0) || j == record_data_nelms) {
            uint8_t tmp[def->fields[i].size];
            if ((nelms = fread(tmp, sizeof(uint8_t), def->fields[i].size,fp)) != def->fields[i].size) {
                fprintf(stdout, "Unable to read unknown index\n");
            }
            nbytes += sizeof(uint8_t) * def->fields[i].size;
            continue;
        }
        if ((nelms = fread(&(buf[offset]), sizeof(uint8_t), def->fields[i].size,fp)) != def->fields[i].size) {
            fprintf(stdout, "Unable to read data at byte %d\n", nbytes);
            exit(1);
        }
        nbytes += sizeof(uint8_t) * def->fields[i].size;
    }
    return 0;
}

canonical_original_code:
int read_data(void *data, unsigned long len, struct fit_definition *def, struct fit_record_data *record_data, int record_data_nelms) {
    int i; int j;
    int nelms;
    unsigned long offset;
    unsigned char *buf = data;
    for (i = 0, offset = 0; i < def->record.nfields; i++) {
        if (record_data) {
            for (j = 0; j < record_data_nelms; j++) {
                if (record_data[j].index == def->fields[i].index) {
                    offset = record_data[j].offset;
                    break;
                }
            }
        }
        if (record_data == ((void *)0) || j == record_data_nelms) {
            unsigned char tmp[-1];
            if ((nelms = fread(tmp, sizeof(unsigned char), def->fields[i].size,fp)) != def->fields[i].size) {
                fprintf(stdout, "Unable to read unknown index\n");
            }
            nbytes += sizeof(unsigned char) * def->fields[i].size;
            continue;
        }
        if ((nelms = fread(&(buf[offset]), sizeof(unsigned char), def->fields[i].size,fp)) != def->fields[i].size) {
            fprintf(stdout, "Unable to read data at byte %d\n", nbytes);
            exit(1);
        }
        nbytes += sizeof(unsigned char) * def->fields[i].size;
    }
    return 0;
}

variable_types:
data
void *

variable_types:
len
unsigned long

variable_types: struct fit_record_data { int index; char *label; int type; int offset; double scale; char *units; } *

variable_types: j int

variable_types:
offset
unsigned long

variable_types:
buf
unsigned char *

variable_types:
tmp
unsigned char [-1]

user_defined_types: struct fit_record_definition { unsigned char reserved; unsigned char endian; unsigned short msg_num; unsigned char nfields; }

user_defined_types: struct fit_field_desc { unsigned char index; unsigned char size; unsigned char type; }


decompiled_code:
void *__fastcall search_for_data(int a1, _DWORD *a2)
{
void *result; // rax

switch ( a1 )
{
case 0:
*a2 = 6;
result = &file_info_data;
break;
case 1:
*a2 = 2;
result = &file_creator_data;
break;
case 2:
*a2 = 5;
result = &event_data;
break;
case 3:
*a2 = 12;
result = &device_info_data;
break;
case 5:
*a2 = 13;
result = &record_data;
break;
case 6:
*a2 = 32;
result = &lap_data;
break;
case 7:
*a2 = 31;
result = &session_data;
break;
case 8:
*a2 = 6;
result = &activity_data;
break;
default:
result = 0LL;
break;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func3(int a1, _DWORD *a2)
{
void *result; // rax

switch ( a1 )
{
case 0:
*a2 = 6;
result = &file_info_data;
break;
case 1:
*a2 = 2;
result = &file_creator_data;
break;
case 2:
*a2 = 5;
result = &event_data;
break;
case 3:
*a2 = 12;
result = &device_info_data;
break;
case 5:
*a2 = 13;
result = &record_data;
break;
case 6:
*a2 = 32;
result = &lap_data;
break;
case 7:
*a2 = 31;
result = &session_data;
break;
case 8:
*a2 = 6;
result = &activity_data;
break;
default:
result = 0LL;
break;
}
return result;
}

original_code:
struct fit_record_data *search_for_data(int record_num, int *data_nelms) {
    switch(record_num) {
        case FITFILE_MSGTYPE_FILE_ID:
            *data_nelms = (sizeof(file_info_data)/sizeof(file_info_data[0]));
            return file_info_data;
        case FITFILE_MSGTYPE_FILE_CREATOR:
            *data_nelms = (sizeof(file_creator_data)/sizeof(file_creator_data[0]));
            return file_creator_data;
        case FITFILE_MSGTYPE_EVENT:
            *data_nelms = (sizeof(event_data)/sizeof(event_data[0]));
            return event_data;
        case FITFILE_MSGTYPE_DEVICE_INFO:
            *data_nelms = (sizeof(device_info_data)/sizeof(device_info_data[0]));
            return device_info_data;
        case FITFILE_MSGTYPE_RECORD:
            *data_nelms = (sizeof(record_data)/sizeof(record_data[0]));
            return record_data;
        case FITFILE_MSGTYPE_LAP:
            *data_nelms = (sizeof(lap_data)/sizeof(lap_data[0]));
            return lap_data;
        case FITFILE_MSGTYPE_SESSION:
            *data_nelms = (sizeof(session_data)/sizeof(session_data[0]));
            return session_data;
        case FITFILE_MSGTYPE_ACTIVITY:
            *data_nelms = (sizeof(activity_data)/sizeof(activity_data[0]));
            return activity_data;
        default:
            return ((void *)0);
    }
}

return_type: struct fit_record_data { int index; char *label; int type; int offset; double scale; char *units; } *


decompiled_code:
__int64 fit_process_file()
{
_DWORD *v0; // rax
__int64 v1; // rdx
signed __int64 v2; // rsi
void *v3; // rsp
char v4; // al
__int64 v5; // rdi
_BYTE v7[15]; // [rsp+0h] [rbp-30h] BYREF
char v8; // [rsp+Fh] [rbp-21h] BYREF
int v9; // [rsp+10h] [rbp-20h] BYREF
unsigned __int8 ptr[4]; // [rsp+14h] [rbp-1Ch] BYREF
unsigned int v11; // [rsp+18h] [rbp-18h]

if ( (unsigned int)fread(ptr, 0xCuLL, 1uLL, fp) != 1 || (unsigned int)check_header(ptr) )
goto LABEL_20;
print_xml_header();
if ( nbytes < v11 )
{
while ( 1 )
{
while ( 1 )
{
fread(&v8, 1uLL, 1uLL, fp);
++nbytes;
v5 = v8 & 0xF;
if ( (v8 & 0x40) == 0 )
break;
fetch_record_definition(v5);
if ( v11 <= nbytes )
goto LABEL_12;
}
if ( !search_for_def(v5) )
break;
v0 = search_for_data(v8 & 0xF, &v9);
v2 = *(_QWORD *)(v1 + 16);
v3 = alloca(v2);
read_data((__int64)v7, v2, v1, v0, v9);
v4 = v8 & 0xF;
if ( (v8 & 0xF) == 6 )
{
if ( !display )
{
print_xml_open_tag("lap", stdout, 1LL);
putchar(10);
print_xml_record_data(v7, &lap_data, 32LL, stdout, 2LL);
print_xml_close_tag("lap", stdout, 1LL);
putchar(10);
}
}
else if ( v4 == 7 )
{
if ( display == 1 )
{
print_xml_open_tag("session", stdout, 1LL);
putchar(10);
print_xml_record_data(v7, &session_data, 31LL, stdout, 2LL);
print_xml_close_tag("session", stdout, 1LL);
putchar(10);
}
}
else if ( v4 == 5 && display == 2 )
{
print_xml_open_tag("record", stdout, 1LL);
putchar(10);
print_xml_record_data(v7, &record_data, 13LL, stdout, 2LL);
print_xml_close_tag("record", stdout, 1LL);
putchar(10);
}
if ( v11 <= nbytes )
goto LABEL_12;
}
fwrite("Unable to find definition for message type\n", 1uLL, 0x2BuLL, stdout);
LABEL_20:
exit(1);
}
LABEL_12:
fclose(fp);
print_xml_footer();
return 0LL;
}

canonical_decompiled_code:
__int64 func4()
{
_DWORD *v0; // rax
__int64 v1; // rdx
signed __int64 v2; // rsi
void *v3; // rsp
char v4; // al
__int64 v5; // rdi
_BYTE v7[15]; // [rsp+0h] [rbp-30h] BYREF
char v8; // [rsp+Fh] [rbp-21h] BYREF
int v9; // [rsp+10h] [rbp-20h] BYREF
unsigned __int8 ptr[4]; // [rsp+14h] [rbp-1Ch] BYREF
unsigned int v11; // [rsp+18h] [rbp-18h]

if ( (unsigned int)fread(ptr, 0xCuLL, 1uLL, fp) != 1 || (unsigned int)func0(ptr) )
goto LABEL_20;
print_xml_header();
if ( nbytes < v11 )
{
while ( 1 )
{
while ( 1 )
{
fread(&v8, 1uLL, 1uLL, fp);
++nbytes;
v5 = v8 & 0xF;
if ( (v8 & 0x40) == 0 )
break;
func1(v5);
if ( v11 <= nbytes )
goto LABEL_12;
}
if ( !search_for_def(v5) )
break;
v0 = func3(v8 & 0xF, &v9);
v2 = *(_QWORD *)(v1 + 16);
v3 = alloca(v2);
func2((__int64)v7, v2, v1, v0, v9);
v4 = v8 & 0xF;
if ( (v8 & 0xF) == 6 )
{
if ( !display )
{
print_xml_open_tag("lap", stdout, 1LL);
putchar(10);
print_xml_record_data(v7, &lap_data, 32LL, stdout, 2LL);
print_xml_close_tag("lap", stdout, 1LL);
putchar(10);
}
}
else if ( v4 == 7 )
{
if ( display == 1 )
{
print_xml_open_tag("session", stdout, 1LL);
putchar(10);
print_xml_record_data(v7, &session_data, 31LL, stdout, 2LL);
print_xml_close_tag("session", stdout, 1LL);
putchar(10);
}
}
else if ( v4 == 5 && display == 2 )
{
print_xml_open_tag("record", stdout, 1LL);
putchar(10);
print_xml_record_data(v7, &record_data, 13LL, stdout, 2LL);
print_xml_close_tag("record", stdout, 1LL);
putchar(10);
}
if ( v11 <= nbytes )
goto LABEL_12;
}
fwrite("Unable to find definition for message type\n", 1uLL, 0x2BuLL, stdout);
LABEL_20:
exit(1);
}
LABEL_12:
fclose(fp);
print_xml_footer();
return 0LL;
}

original_code:
int fit_process_file(void) {
    int nelms;
    struct fit_header hdr;
    if ((nelms = fread(&hdr, sizeof(hdr), 1, fp)) != 1) {
        exit(1);
    }
    if (check_header(&hdr)) {
        exit(1);
    }
    print_xml_header();
    while (nbytes < hdr.file_size) {
        uint8_t record_hdr;
        if ((nelms = fread(&record_hdr, sizeof(record_hdr), 1, fp)) != 1) {
        }
        nbytes += sizeof(record_hdr);
        if (record_hdr & (1 << 6)) {
            fetch_record_definition(record_hdr & ((1 << 4) - 1));
            continue;
        }
        struct fit_definition *def;
        def = search_for_def(record_hdr & ((1 << 4) - 1));
        if (!def) {
            fprintf(stdout, "Unable to find definition for message type\n");
            exit(1);
        }
        struct fit_record_data *data;
        int data_len;
        data = search_for_data(record_hdr & ((1 << 4) - 1), &data_len);
        uint8_t array[def->record_size];
        read_data(array, sizeof(array), def, data, data_len);
        switch(record_hdr & ((1 << 4) - 1)) {
            case FITFILE_MSGTYPE_RECORD:
            {
                if (display == DISPLAY_RECORDS) {
                    print_xml_open_tag("record", stdout, 1);
                    printf("\n");
                    print_xml_record_data(array, record_data,
                            (sizeof(record_data)/sizeof(record_data[0])), stdout, 2);
                    print_xml_close_tag("record", stdout, 1);
                    printf("\n");
                }
            }
            break;
            case FITFILE_MSGTYPE_LAP:
            {
                if (display == DISPLAY_LAPS) {
                    print_xml_open_tag("lap", stdout, 1);
                    printf("\n");
                    print_xml_record_data(array, lap_data,
                            (sizeof(lap_data)/sizeof(lap_data[0])), stdout, 2);
                    print_xml_close_tag("lap", stdout, 1);
                    printf("\n");
                }
            }
            break;
            case FITFILE_MSGTYPE_SESSION:
            {
                if (display == DISPLAY_SESSIONS) {
                    print_xml_open_tag("session", stdout, 1);
                    printf("\n");
                    print_xml_record_data(array, session_data,
                            (sizeof(session_data)/sizeof(session_data[0])), stdout, 2);
                    print_xml_close_tag("session", stdout, 1);
                    printf("\n");
                }
            }
            break;
            default:
                break;
        }
    }
    fclose(fp);
    print_xml_footer();
    return 0;
}

canonical_original_code:
int fit_process_file() {
    int nelms;
    struct fit_header hdr;
    if ((nelms = fread(&hdr, sizeof(hdr), 1, fp)) != 1) {
        exit(1);
    }
    if (check_header(&hdr)) {
        exit(1);
    }
    print_xml_header();
    while (nbytes < hdr.file_size) {
        unsigned char record_hdr;
        if ((nelms = fread(&record_hdr, sizeof(record_hdr), 1, fp)) != 1) {
        }
        nbytes += sizeof(record_hdr);
        if (record_hdr & (1 << 6)) {
            fetch_record_definition(record_hdr & ((1 << 4) - 1));
            continue;
        }
        struct fit_definition *def;
        def = search_for_def(record_hdr & ((1 << 4) - 1));
        if (!def) {
            fprintf(stdout, "Unable to find definition for message type\n");
            exit(1);
        }
        struct fit_record_data *data;
        int data_len;
        data = search_for_data(record_hdr & ((1 << 4) - 1), &data_len);
        unsigned char array[-1];
        read_data(array, sizeof(array), def, data, data_len);
        switch(record_hdr & ((1 << 4) - 1)) {
            case FITFILE_MSGTYPE_RECORD:
            {
                if (display == DISPLAY_RECORDS) {
                    print_xml_open_tag("record", stdout, 1);
                    printf("\n");
                    print_xml_record_data(array, record_data,
                            (sizeof(record_data)/sizeof(record_data[0])), stdout, 2);
                    print_xml_close_tag("record", stdout, 1);
                    printf("\n");
                }
            }
            break;
            case FITFILE_MSGTYPE_LAP:
            {
                if (display == DISPLAY_LAPS) {
                    print_xml_open_tag("lap", stdout, 1);
                    printf("\n");
                    print_xml_record_data(array, lap_data,
                            (sizeof(lap_data)/sizeof(lap_data[0])), stdout, 2);
                    print_xml_close_tag("lap", stdout, 1);
                    printf("\n");
                }
            }
            break;
            case FITFILE_MSGTYPE_SESSION:
            {
                if (display == DISPLAY_SESSIONS) {
                    print_xml_open_tag("session", stdout, 1);
                    printf("\n");
                    print_xml_record_data(array, session_data,
                            (sizeof(session_data)/sizeof(session_data[0])), stdout, 2);
                    print_xml_close_tag("session", stdout, 1);
                    printf("\n");
                }
            }
            break;
            default:
                break;
        }
    }
    fclose(fp);
    print_xml_footer();
    return 0;
}

variable_types: struct fit_header { unsigned char h_len; unsigned char protocol_version; unsigned char profile_version; unsigned char xxx2; unsigned int file_size; unsigned char sig[4]; }

variable_types:
array
unsigned char [-1]

user_defined_types: struct fit_record_data { int index; char *label; int type; int offset; double scale; char *units; }


printUsage

decompiled_code:
void __noreturn printUsage()
{
fprintf(
stdout,
"Usage: %s [options] filename\n"
" -l --laps\n"
" -s --session\n"
" -r --records\n"
" -v --verbose\n"
" -h --help\n"
"\n"
"\n"
"%s version %s. \n"
"Author: %s\n"
"Report bugs to %s",
"fitdecode",
"fitdecode",
"0.1",
"Paul Archer",
"ptarcher@gmail.com");
exit(1);
}

canonical_decompiled_code:
void __noreturn func0()
{
fprintf(
stdout,
"Usage: %s [options] filename\n"
" -l --laps\n"
" -s --session\n"
" -r --records\n"
" -v --verbose\n"
" -h --help\n"
"\n"
"\n"
"%s version %s. \n"
"Author: %s\n"
"Report bugs to %s",
"fitdecode",
"fitdecode",
"0.1",
"Paul Archer",
"ptarcher@gmail.com");
exit(1);
}

original_code:
void printUsage(void) {
    fprintf(stdout, "Usage: %s [options] filename\n"
                    "       -l --laps\n"
                    "       -s --session\n"
                    "       -r --records\n"
                    "       -v --verbose\n"
                    "       -h --help\n"
                    "\n\n"
                    "%s version %s. \n"
                    "Author: %s\n"
                    "Report bugs to %s",
                    "fitdecode",
                    "fitdecode",
                    "0.1",
                    "Paul Archer",
                    "ptarcher@gmail.com");
    exit(1);
}

canonical_original_code:
void printUsage() {
    fprintf(stdout, "Usage: %s [options] filename\n"
                    "       -l --laps\n"
                    "       -s --session\n"
                    "       -r --records\n"
                    "       -v --verbose\n"
                    "       -h --help\n"
                    "\n\n"
                    "%s version %s. \n"
                    "Author: %s\n"
                    "Report bugs to %s",
                    "fitdecode",
                    "fitdecode",
                    "0.1",
                    "Paul Archer",
                    "ptarcher@gmail.com");
    exit(1);
}


parseArgs

decompiled_code:
__int64 __fastcall parseArgs(int argc, char **argv)
{
int v2; // eax
FILE *v3; // rax
const char **v4; // r12
char **v5; // rbx
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

fp = (__int64)stdin;
while ( 1 )
{
v2 = getopt_long(argc, argv, "lsrhv", &long_options_0, longind);
if ( v2 == -1 )
break;
if ( v2 == 114 )
{
if ( display )
goto LABEL_11;
display = 2;
}
else if ( v2 > 114 )
{
if ( v2 == 115 )
{
if ( display )
goto LABEL_11;
display = 1;
}
else
{
if ( v2 != 118 )
LABEL_26:
printUsage();
flag_verbose = 1;
}
}
else if ( v2 )
{
if ( v2 != 108 )
goto LABEL_26;
if ( display )
{
LABEL_11:
fwrite("Cannot have two modes set\n", 1uLL, 0x1AuLL, stderr);
exit(1);
}
}
else
{
printf("long option %s", *(&long_options_0.name + 4 * longind[0]));
if ( optarg )
printf(" with arg %s", optarg);
putchar(10);
}
}
if ( argc > optind )
{
v3 = (FILE *)fp;
v4 = (const char **)&argv[optind];
v5 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
while ( 1 )
{
if ( stdin != v3 )
{
fprintf(stderr, "more then one filename given %s\n", *v4);
exit(1);
}
v3 = fopen(*v4, "r");
fp = (__int64)v3;
if ( !v3 )
break;
if ( v5 == (char **)++v4 )
return 0LL;
}
perror(*v4);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int argc, char **argv)
{
int v2; // eax
FILE *v3; // rax
const char **v4; // r12
char **v5; // rbx
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

fp = (__int64)stdin;
while ( 1 )
{
v2 = getopt_long(argc, argv, "lsrhv", &long_options_0, longind);
if ( v2 == -1 )
break;
if ( v2 == 114 )
{
if ( display )
goto LABEL_11;
display = 2;
}
else if ( v2 > 114 )
{
if ( v2 == 115 )
{
if ( display )
goto LABEL_11;
display = 1;
}
else
{
if ( v2 != 118 )
LABEL_26:
func0();
flag_verbose = 1;
}
}
else if ( v2 )
{
if ( v2 != 108 )
goto LABEL_26;
if ( display )
{
LABEL_11:
fwrite("Cannot have two modes set\n", 1uLL, 0x1AuLL, stderr);
exit(1);
}
}
else
{
printf("long option %s", *(&long_options_0.name + 4 * longind[0]));
if ( optarg )
printf(" with arg %s", optarg);
putchar(10);
}
}
if ( argc > optind )
{
v3 = (FILE *)fp;
v4 = (const char **)&argv[optind];
v5 = &(&argv[optind])[(unsigned __int64)(unsigned int)(argc + ~optind) + 1];
while ( 1 )
{
if ( stdin != v3 )
{
fprintf(stderr, "more then one filename given %s\n", *v4);
exit(1);
}
v3 = fopen(*v4, "r");
fp = (__int64)v3;
if ( !v3 )
break;
if ( v5 == (char **)++v4 )
return 0LL;
}
perror(*v4);
}
return 0LL;
}

original_code:
int parseArgs(int argc, char *argv[]) {
    int c;
    int i;
    fp = stdin;
    while (1) {
        int this_option_optind;
        int option_index;
        static struct option long_options[] = {
            {"laps", 0, 0, 'l'},
            {"session", 0, 0, 's'},
            {"records", 0, 0, 'r'},
            {0, 0, 0, 0},
        };
        this_option_optind = optind ? optind : 1;
        c = getopt_long(argc, argv, "lsrhv", long_options, &option_index);
        if (c == -1) {
            break;
        }
        switch(c) {
            case 0:
                printf("long option %s", long_options[option_index].name);
                if (optarg) {
                    printf(" with arg %s", optarg);
                }
                printf("\n");
                break;
            case 'l':
                if (display) {
                    fprintf(stderr, "Cannot have two modes set\n");
                    exit(1);
                }
                display = DISPLAY_LAPS;
                break;
            case 's':
                if (display) {
                    fprintf(stderr, "Cannot have two modes set\n");
                    exit(1);
                }
                display = DISPLAY_SESSIONS;
                break;
            case 'r':
                if (display) {
                    fprintf(stderr, "Cannot have two modes set\n");
                    exit(1);
                }
                display = DISPLAY_RECORDS;
                break;
            case 'h':
                printUsage();
                break;
            case 'v':
                flag_verbose = 1;
                break;
            default:
                printUsage();
                break;
        }
    }
    for (i = optind; i < argc; i++) {
        if (fp != stdin) {
            fprintf(stderr, "more then one filename given %s\n", argv[i]);
            exit(1);
        }
        fp = fopen(argv[i], "r");
        if (fp == ((void *)0)) {
            perror(argv[i]);
            return 0;
        }
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: c int

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []


main

canonical_name: main

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

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

original_code:
int main(int argc, char *argv[])
{
    init_definitions();
    parseArgs(argc, argv);
    fit_process_file();
    return 0;
}


decompiled_code:
int print_xml_header()
{
int result; // eax

if ( display == 1 )
{
print_xml_open_tag("sessions", stdout, 0);
result = putchar(10);
}
else if ( display == 2 )
{
print_xml_open_tag("records", stdout, 0);
result = putchar(10);
}
else
{
if ( !display )
print_xml_open_tag("laps", stdout, 0);
result = putchar(10);
}
return result;
}

canonical_decompiled_code:
int func2()
{
int result; // eax

if ( display == 1 )
{
func1("sessions", stdout, 0);
result = putchar(10);
}
else if ( display == 2 )
{
func1("records", stdout, 0);
result = putchar(10);
}
else
{
if ( !display )
func1("laps", stdout, 0);
result = putchar(10);
}
return result;
}

original_code:
void print_xml_header(void) {
    switch(display) {
        case DISPLAY_LAPS:
            print_xml_open_tag("laps", stdout, 0);
            break;
        case DISPLAY_SESSIONS:
            print_xml_open_tag("sessions", stdout, 0);
            break;
        case DISPLAY_RECORDS:
            print_xml_open_tag("records", stdout, 0);
            break;
    }
    printf("\n");
}

canonical_original_code:
void print_xml_header() {
    switch(display) {
        case DISPLAY_LAPS:
            print_xml_open_tag("laps", stdout, 0);
            break;
        case DISPLAY_SESSIONS:
            print_xml_open_tag("sessions", stdout, 0);
            break;
        case DISPLAY_RECORDS:
            print_xml_open_tag("records", stdout, 0);
            break;
    }
    printf("\n");
}


decompiled_code:
int print_xml_footer()
{
int result; // eax

if ( display == 1 )
{
print_xml_close_tag("sessions", stdout, 0);
result = putchar(10);
}
else if ( display == 2 )
{
print_xml_close_tag("records", stdout, 0);
result = putchar(10);
}
else
{
if ( !display )
print_xml_close_tag("laps", stdout, 0);
result = putchar(10);
}
return result;
}

canonical_decompiled_code:
int func5()
{
int result; // eax

if ( display == 1 )
{
func4("sessions", stdout, 0);
result = putchar(10);
}
else if ( display == 2 )
{
func4("records", stdout, 0);
result = putchar(10);
}
else
{
if ( !display )
func4("laps", stdout, 0);
result = putchar(10);
}
return result;
}

original_code:
void print_xml_footer(void) {
    switch(display) {
        case DISPLAY_LAPS:
            print_xml_close_tag("laps", stdout, 0);
            break;
        case DISPLAY_SESSIONS:
            print_xml_close_tag("sessions", stdout, 0);
            break;
        case DISPLAY_RECORDS:
            print_xml_close_tag("records", stdout, 0);
            break;
    }
    printf("\n");
}

canonical_original_code:
void print_xml_footer() {
    switch(display) {
        case DISPLAY_LAPS:
            print_xml_close_tag("laps", stdout, 0);
            break;
        case DISPLAY_SESSIONS:
            print_xml_close_tag("sessions", stdout, 0);
            break;
        case DISPLAY_RECORDS:
            print_xml_close_tag("records", stdout, 0);
            break;
    }
    printf("\n");
}


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

defs = malloc(8uLL);
result = 0x100000000LL;
*(&defs + 1) = (void *)0x100000000LL;
return result;
}

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

defs = malloc(8uLL);
result = 0x100000000LL;
*(&defs + 1) = (void *)0x100000000LL;
return result;
}

original_code:
void init_definitions(void) {
    defs.nelms = 0;
    defs.defs = malloc(sizeof(*(defs.defs)));
    defs.size = 1;
}

canonical_original_code:
void init_definitions() {
    defs.nelms = 0;
    defs.defs = malloc(sizeof(*(defs.defs)));
    defs.size = 1;
}


decompiled_code:
void *__fastcall insert_definition(__int64 a1)
{
void *result; // rax
_QWORD *v3; // rdi
int v4; // edx
int v5; // ebx

result = (void *)*((int *)&defs + 2);
v3 = defs;
v4 = (_DWORD)result + 1;
if ( *((_DWORD *)&defs + 3) != (_DWORD)result + 1 )
goto LABEL_2;
v5 = 2 * v4;
result = realloc(defs, 16LL * v4);
v3 = result;
if ( result )
{
defs = result;
result = (void *)*((int *)&defs + 2);
*((_DWORD *)&defs + 3) = v5;
v4 = (_DWORD)result + 1;
LABEL_2:
*((_DWORD *)&defs + 2) = v4;
v3[(_QWORD)result] = a1;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func1(__int64 a1)
{
void *result; // rax
_QWORD *v3; // rdi
int v4; // edx
int v5; // ebx

result = (void *)*((int *)&defs + 2);
v3 = defs;
v4 = (_DWORD)result + 1;
if ( *((_DWORD *)&defs + 3) != (_DWORD)result + 1 )
goto LABEL_2;
v5 = 2 * v4;
result = realloc(defs, 16LL * v4);
v3 = result;
if ( result )
{
defs = result;
result = (void *)*((int *)&defs + 2);
*((_DWORD *)&defs + 3) = v5;
v4 = (_DWORD)result + 1;
LABEL_2:
*((_DWORD *)&defs + 2) = v4;
v3[(_QWORD)result] = a1;
}
return result;
}

original_code:
void insert_definition(struct fit_definition *def) {
    if (defs.size == (defs.nelms + 1)) {
        int new_size;
        struct fit_definition **new_defs;
        new_size = defs.size * 2;
        new_defs = realloc(defs.defs, sizeof(*new_defs)*new_size);
        if (new_defs == ((void *)0)) {
            return;
        }
        defs.defs = new_defs;
        defs.size = new_size;
    }
    defs.defs[defs.nelms++] = def;
}

variable_types: struct fit_definition { unsigned char record_num; struct fit_record_definition { unsigned char reserved; unsigned char endian; unsigned short msg_num; unsigned char nfields; } record; struct fit_field_desc { unsigned char index; unsigned char size; unsigned char type; } *fields; unsigned long record_size; } **


decompiled_code:
void print_csv_header()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void print_csv_header(csv_display_t display) {
    int i;
}

canonical_original_code:
void print_csv_header(enum csv_display_t display) {
    int i;
}

variable_types: enum csv_display_t { DISPLAY_LAPS=0, DISPLAY_SESSIONS=1, DISPLAY_RECORDS=2 }


strrindex

repo: ptdecker/cbasics

decompiled_code:
__int64 __fastcall strrindex(const char *a1, const char *a2)
{
size_t v3; // r12
size_t v4; // rax
size_t v5; // rsi
const char *v6; // rcx
int v7; // er8
char v8; // dl
size_t v9; // rax

v3 = strlen(a1);
v4 = strlen(a2);
v5 = v3 + 1 - v4;
if ( v3 + 1 != v4 )
{
v6 = &a2[v4 - (v3 + 1)];
do
{
v7 = v5--;
v8 = *a2;
v9 = v5;
if ( *a2 )
{
while ( a1[v9] == v8 )
{
v8 = v6[++v9 + 1];
if ( !v8 )
return (unsigned int)(v7 - 1);
}
}
++v6;
}
while ( v5 );
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, const char *a2)
{
size_t v3; // r12
size_t v4; // rax
size_t v5; // rsi
const char *v6; // rcx
int v7; // er8
char v8; // dl
size_t v9; // rax

v3 = strlen(a1);
v4 = strlen(a2);
v5 = v3 + 1 - v4;
if ( v3 + 1 != v4 )
{
v6 = &a2[v4 - (v3 + 1)];
do
{
v7 = v5--;
v8 = *a2;
v9 = v5;
if ( *a2 )
{
while ( a1[v9] == v8 )
{
v8 = v6[++v9 + 1];
if ( !v8 )
return (unsigned int)(v7 - 1);
}
}
++v6;
}
while ( v5 );
}
return 0xFFFFFFFFLL;
}

original_code:
static int strrindex(char s[], char t[]) {
    size_t i;
    size_t j;
    size_t k;
    for (i = (strlen(s) - strlen(t) + 1); i > 0; i--) {
        for (j = (i - 1), k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++)
            ;
        if (k > 0 && t[k] == '\0')
            return (int)(i - 1);
    }
    return -1;
}

canonical_original_code:
static int strrindex(char s[], char t[]) {
    unsigned long i;
    unsigned long j;
    unsigned long k;
    for (i = (strlen(s) - strlen(t) + 1); i > 0; i--) {
        for (j = (i - 1), k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++)
            ;
        if (k > 0 && t[k] == '\0')
            return (int)(i - 1);
    }
    return -1;
}

variable_types:
s
char []

variable_types:
t
char []

variable_types:
i
unsigned long

variable_types:
j
unsigned long

variable_types:
k
unsigned long


strindex

decompiled_code:
__int64 __fastcall strindex(_BYTE *a1, char *a2)
{
char v2; // r8
__int64 v3; // rcx
__int64 v4; // rax
char v5; // dl

if ( *a1 )
{
v2 = *a2;
v3 = 0LL;
do
{
v4 = v3;
v5 = v2;
if ( v2 )
{
while ( a1[v4] == v5 )
{
v5 = a2[++v4];
if ( !v5 )
return (unsigned int)v3;
}
}
++v3;
--a2;
}
while ( a1[v3] );
}
LODWORD(v3) = -1;
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, char *a2)
{
char v2; // r8
__int64 v3; // rcx
__int64 v4; // rax
char v5; // dl

if ( *a1 )
{
v2 = *a2;
v3 = 0LL;
do
{
v4 = v3;
v5 = v2;
if ( v2 )
{
while ( a1[v4] == v5 )
{
v5 = a2[++v4];
if ( !v5 )
return (unsigned int)v3;
}
}
++v3;
--a2;
}
while ( a1[v3] );
}
LODWORD(v3) = -1;
return (unsigned int)v3;
}

original_code:
static int strindex(char s[], char t[]) {
    size_t i;
    size_t j;
    size_t k;
    for (i = 0; s[i] != '\0'; i++) {
        for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++)
            ;
        if (k > 0 && t[k] == '\0')
            return (int)i;
    }
    return -1;
}

canonical_original_code:
static int strindex(char s[], char t[]) {
    unsigned long i;
    unsigned long j;
    unsigned long k;
    for (i = 0; s[i] != '\0'; i++) {
        for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++)
            ;
        if (k > 0 && t[k] == '\0')
            return (int)i;
    }
    return -1;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
unsigned int v4; // eax
unsigned int v5; // eax
unsigned int v6; // eax
unsigned int v7; // eax
unsigned int v8; // eax
unsigned int v9; // eax
unsigned int v10; // eax
unsigned int v11; // eax
unsigned int v12; // eax
__int128 v14[3]; // [rsp+0h] [rbp-B8h] BYREF
char v15[16]; // [rsp+30h] [rbp-88h] BYREF
__int128 v16[6]; // [rsp+40h] [rbp-78h] BYREF
int v17; // [rsp+A0h] [rbp-18h]

strcpy(v15, "st string.");
v14[0] = (__int128)_mm_load_si128((const __m128i *)&LC3);
v14[1] = (__int128)_mm_load_si128((const __m128i *)&LC4);
v14[2] = (__int128)_mm_load_si128((const __m128i *)&LC5);
v16[0] = 0LL;
v16[1] = 0LL;
v16[2] = 0LL;
v16[3] = 0LL;
v16[4] = 0LL;
v16[5] = 0LL;
v17 = 0;
LODWORD(v16[0]) = 1953719668;
printf("In the string: \"%s\"\n", (const char *)v14);
v3 = strindex(v14, (char *)v16);
printf("The leftmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v3);
v4 = strrindex((const char *)v14, (const char *)v16);
printf("The rightmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v4);
putchar(10);
strcpy((char *)v16, "string");
printf("In the string: \"%s\"\n", (const char *)v14);
v5 = strindex(v14, (char *)v16);
printf("The leftmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v5);
v6 = strrindex((const char *)v14, (const char *)v16);
printf("The rightmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v6);
putchar(10);
strcpy((char *)v16, "This");
printf("In the string: \"%s\"\n", (const char *)v14);
v7 = strindex(v14, (char *)v16);
printf("The leftmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v7);
v8 = strrindex((const char *)v14, (const char *)v16);
printf("The rightmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v8);
putchar(10);
LOWORD(v16[0]) = 46;
printf("In the string: \"%s\"\n", (const char *)v14);
v9 = strindex(v14, (char *)v16);
printf("The leftmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v9);
v10 = strrindex((const char *)v14, (const char *)v16);
printf("The rightmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v10);
putchar(10);
strcpy((char *)v16, "not in string");
printf("In the string: \"%s\"\n", (const char *)v14);
v11 = strindex(v14, (char *)v16);
printf("The leftmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v11);
v12 = strrindex((const char *)v14, (const char *)v16);
printf("The rightmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v12);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
unsigned int v4; // eax
unsigned int v5; // eax
unsigned int v6; // eax
unsigned int v7; // eax
unsigned int v8; // eax
unsigned int v9; // eax
unsigned int v10; // eax
unsigned int v11; // eax
unsigned int v12; // eax
__int128 v14[3]; // [rsp+0h] [rbp-B8h] BYREF
char v15[16]; // [rsp+30h] [rbp-88h] BYREF
__int128 v16[6]; // [rsp+40h] [rbp-78h] BYREF
int v17; // [rsp+A0h] [rbp-18h]

strcpy(v15, "st string.");
v14[0] = (__int128)_mm_load_si128((const __m128i *)&LC3);
v14[1] = (__int128)_mm_load_si128((const __m128i *)&LC4);
v14[2] = (__int128)_mm_load_si128((const __m128i *)&LC5);
v16[0] = 0LL;
v16[1] = 0LL;
v16[2] = 0LL;
v16[3] = 0LL;
v16[4] = 0LL;
v16[5] = 0LL;
v17 = 0;
LODWORD(v16[0]) = 1953719668;
printf("In the string: \"%s\"\n", (const char *)v14);
v3 = func0(v14, (char *)v16);
printf("The leftmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v3);
v4 = func1((const char *)v14, (const char *)v16);
printf("The rightmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v4);
putchar(10);
strcpy((char *)v16, "string");
printf("In the string: \"%s\"\n", (const char *)v14);
v5 = func0(v14, (char *)v16);
printf("The leftmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v5);
v6 = func1((const char *)v14, (const char *)v16);
printf("The rightmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v6);
putchar(10);
strcpy((char *)v16, "This");
printf("In the string: \"%s\"\n", (const char *)v14);
v7 = func0(v14, (char *)v16);
printf("The leftmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v7);
v8 = func1((const char *)v14, (const char *)v16);
printf("The rightmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v8);
putchar(10);
LOWORD(v16[0]) = 46;
printf("In the string: \"%s\"\n", (const char *)v14);
v9 = func0(v14, (char *)v16);
printf("The leftmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v9);
v10 = func1((const char *)v14, (const char *)v16);
printf("The rightmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v10);
putchar(10);
strcpy((char *)v16, "not in string");
printf("In the string: \"%s\"\n", (const char *)v14);
v11 = func0(v14, (char *)v16);
printf("The leftmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v11);
v12 = func1((const char *)v14, (const char *)v16);
printf("The rightmost occurrence of \"%s\" is at position %d.\n", (const char *)v16, v12);
return 0;
}

original_code:
int main(void) {
    char s[] = "This is my test string, but its not much of a test string.";
    char t[100] = "";
    (void)strcpy(t, "test");
    printf("In the string: \"%s\"\n", s);
    printf("The leftmost  occurrence of \"%s\" is at position %d.\n", t, strindex(s, t));
    printf("The rightmost occurrence of \"%s\" is at position %d.\n", t, strrindex(s, t));
    (void)putchar('\n');
    (void)strcpy(t, "string");
    printf("In the string: \"%s\"\n", s);
    printf("The leftmost  occurrence of \"%s\" is at position %d.\n", t, strindex(s, t));
    printf("The rightmost occurrence of \"%s\" is at position %d.\n", t, strrindex(s, t));
    (void)putchar('\n');
    (void)strcpy(t, "This");
    printf("In the string: \"%s\"\n", s);
    printf("The leftmost  occurrence of \"%s\" is at position %d.\n", t, strindex(s, t));
    printf("The rightmost occurrence of \"%s\" is at position %d.\n", t, strrindex(s, t));
    (void)putchar('\n');
    (void)strcpy(t, ".");
    printf("In the string: \"%s\"\n", s);
    printf("The leftmost  occurrence of \"%s\" is at position %d.\n", t, strindex(s, t));
    printf("The rightmost occurrence of \"%s\" is at position %d.\n", t, strrindex(s, t));
    (void)putchar('\n');
    (void)strcpy(t, "not in string");
    printf("In the string: \"%s\"\n", s);
    printf("The leftmost  occurrence of \"%s\" is at position %d.\n", t, strindex(s, t));
    printf("The rightmost occurrence of \"%s\" is at position %d.\n", t, strrindex(s, t));
    return 0;
}

canonical_original_code:
int main() {
    char s[] = "This is my test string, but its not much of a test string.";
    char t[100] = "";
    (void)strcpy(t, "test");
    printf("In the string: \"%s\"\n", s);
    printf("The leftmost  occurrence of \"%s\" is at position %d.\n", t, strindex(s, t));
    printf("The rightmost occurrence of \"%s\" is at position %d.\n", t, strrindex(s, t));
    (void)putchar('\n');
    (void)strcpy(t, "string");
    printf("In the string: \"%s\"\n", s);
    printf("The leftmost  occurrence of \"%s\" is at position %d.\n", t, strindex(s, t));
    printf("The rightmost occurrence of \"%s\" is at position %d.\n", t, strrindex(s, t));
    (void)putchar('\n');
    (void)strcpy(t, "This");
    printf("In the string: \"%s\"\n", s);
    printf("The leftmost  occurrence of \"%s\" is at position %d.\n", t, strindex(s, t));
    printf("The rightmost occurrence of \"%s\" is at position %d.\n", t, strrindex(s, t));
    (void)putchar('\n');
    (void)strcpy(t, ".");
    printf("In the string: \"%s\"\n", s);
    printf("The leftmost  occurrence of \"%s\" is at position %d.\n", t, strindex(s, t));
    printf("The rightmost occurrence of \"%s\" is at position %d.\n", t, strrindex(s, t));
    (void)putchar('\n');
    (void)strcpy(t, "not in string");
    printf("In the string: \"%s\"\n", s);
    printf("The leftmost  occurrence of \"%s\" is at position %d.\n", t, strindex(s, t));
    printf("The rightmost occurrence of \"%s\" is at position %d.\n", t, strrindex(s, t));
    return 0;
}

variable_types:
t
char [100]


lowerstr

decompiled_code:
void __fastcall lowerstr(char *a1)
{
__int64 v1; // rbx
char *v2; // rbp
const __int32_t **v3; // rdx

v1 = *a1;
if ( (_BYTE)v1 )
{
v2 = a1;
v3 = _ctype_tolower_loc();
do
{
*v2++ = (*v3)[v1];
v1 = *v2;
}
while ( (_BYTE)v1 );
}
}

canonical_decompiled_code:
void __fastcall func1(char *a1)
{
__int64 v1; // rbx
char *v2; // rbp
const __int32_t **v3; // rdx

v1 = *a1;
if ( (_BYTE)v1 )
{
v2 = a1;
v3 = _ctype_tolower_loc();
do
{
*v2++ = (*v3)[v1];
v1 = *v2;
}
while ( (_BYTE)v1 );
}
}

original_code:
static void lowerstr(char *s) {
    for ( ; *s != '\0'; s++)
        *s = tolower(*s);
}

variable_types:
s
char *


listprint

decompiled_code:
int listprint()
{
int result; // eax
__int64 v1; // rbx
__int64 v2; // rax

result = listcount;
if ( listcount > 0 )
{
v1 = 0LL;
do
{
v2 = list[v1++];
result = printf("%5d %s\n", *(unsigned int *)(v2 + 8), *(const char **)v2);
}
while ( listcount > (int)v1 );
}
return result;
}

canonical_decompiled_code:
int func2()
{
int result; // eax
__int64 v1; // rbx
__int64 v2; // rax

result = listcount;
if ( listcount > 0 )
{
v1 = 0LL;
do
{
v2 = list[v1++];
result = printf("%5d %s\n", *(unsigned int *)(v2 + 8), *(const char **)v2);
}
while ( listcount > (int)v1 );
}
return result;
}

original_code:
static void listprint(void) {
    int i;
    for (i = 0; i < listcount; i++)
        printf("%5d %s\n", list[i]->count, list[i]->word);
}

canonical_original_code:
static void listprint() {
    int i;
    for (i = 0; i < listcount; i++)
        printf("%5d %s\n", list[i]->count, list[i]->word);
}

variable_types: i int


talloc

decompiled_code:
void *talloc()
{
void *result; // rax

result = malloc(0x20uLL);
if ( !result )
{
printf("Error: ran out of memory");
exit(1);
}
return result;
}

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

result = malloc(0x20uLL);
if ( !result )
{
printf("Error: ran out of memory");
exit(1);
}
return result;
}

original_code:
static struct tnode *talloc(void) {
    struct tnode *p = (struct tnode *)malloc(sizeof(struct tnode));
    if (p == ((void *)0)) {
        printf("Error: ran out of memory");
        exit(1);
    }
    return p;
}

canonical_original_code:
static struct tnode *talloc() {
    struct tnode *p = (struct tnode *)malloc(sizeof(struct tnode));
    if (p == ((void *)0)) {
        printf("Error: ran out of memory");
        exit(1);
    }
    return p;
}

variable_types: struct tnode { char *word; int count; struct tnode *left; struct tnode *right; } *


mystrdup

decompiled_code:
void *__fastcall mystrdup(void *src)
{
size_t v1; // r12
void *v2; // rax

v1 = strlen((const char *)src) + 1;
v2 = malloc(v1);
if ( !v2 )
{
printf("Error: ran out of memory");
exit(1);
}
return memcpy(v2, src, v1);
}

canonical_decompiled_code:
void *__fastcall func4(void *src)
{
size_t v1; // r12
void *v2; // rax

v1 = strlen((const char *)src) + 1;
v2 = malloc(v1);
if ( !v2 )
{
printf("Error: ran out of memory");
exit(1);
}
return memcpy(v2, src, v1);
}

original_code:
static char *mystrdup(char *s) {
    char *p = (char *)malloc(strlen(s) + 1);
    if (p == ((void *)0)) {
        printf("Error: ran out of memory");
        exit(1);
    }
    strcpy(p, s);
    return p;
}

variable_types:
p
char *

return_type: char *


liststore

decompiled_code:
void __fastcall liststore(__int64 a1)
{
__int64 v1; // rbx
int v2; // eax

if ( a1 )
{
v1 = a1;
do
{
liststore(*(_QWORD *)(v1 + 16));
v2 = listcount;
if ( listcount > (int)&unk_1869F )
{
puts("error: too many distinct words");
exit(1);
}
list[listcount] = v1;
v1 = *(_QWORD *)(v1 + 24);
listcount = v2 + 1;
}
while ( v1 );
}
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1)
{
__int64 v1; // rbx
int v2; // eax

if ( a1 )
{
v1 = a1;
do
{
func7(*(_QWORD *)(v1 + 16));
v2 = listcount;
if ( listcount > (int)&unk_1869F )
{
puts("error: too many distinct words");
exit(1);
}
list[listcount] = v1;
v1 = *(_QWORD *)(v1 + 24);
listcount = v2 + 1;
}
while ( v1 );
}
}

original_code:
static void liststore(struct tnode *p) {
    if (p != ((void *)0)) {
        liststore(p->left);
        if (listcount < 100000)
            list[listcount++] = p;
        else {
            printf("error: too many distinct words\n");
            exit(1);
        }
        liststore(p->right);
    }
}

user_defined_types: struct tnode { char *word; int count; struct tnode *left; struct tnode *right; }


hash

decompiled_code:
__int64 __fastcall hash(_BYTE *a1)
{
int v1; // eax
unsigned int v2; // ecx

v1 = (char)*a1;
if ( !*a1 )
return 0LL;
v2 = 0;
do
{
++a1;
v2 = v1 + 31 * v2;
v1 = (char)*a1;
}
while ( *a1 );
return v2 % 0x65;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1)
{
int v1; // eax
unsigned int v2; // ecx

v1 = (char)*a1;
if ( !*a1 )
return 0LL;
v2 = 0;
do
{
++a1;
v2 = v1 + 31 * v2;
v1 = (char)*a1;
}
while ( *a1 );
return v2 % 0x65;
}

original_code:
static unsigned hash(char *s) {
    unsigned hashval;
    for (hashval = 0; *s != '\0'; s++)
        hashval = *s + 31 * hashval;
    return hashval % 101;
}

canonical_original_code:
static unsigned int hash(char *s) {
    unsigned int hashval;
    for (hashval = 0; *s != '\0'; s++)
        hashval = *s + 31 * hashval;
    return hashval % 101;
}

variable_types:
hashval
unsigned int

return_type: unsigned int


lookup

decompiled_code:
__int64 __fastcall lookup(char *s1)
{
__int64 i; // r12

for ( i = hashtab[(unsigned int)hash(s1)]; i; i = *(_QWORD *)i )
{
if ( !strcmp(s1, *(const char **)(i + 8)) )
break;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s1)
{
__int64 i; // r12

for ( i = hashtab[(unsigned int)func0(s1)]; i; i = *(_QWORD *)i )
{
if ( !strcmp(s1, *(const char **)(i + 8)) )
break;
}
return i;
}

original_code:
static struct nlist *lookup(char *s) {
    struct nlist *np;
    for (np = hashtab[hash(s)]; np != ((void *)0); np = np->next)
        if (strcmp(s, np->name) == 0)
            return np;
    return ((void *)0);
}

variable_types: struct nlist { struct nlist *next; char *name; char *defn; } *


undef

decompiled_code:
void __fastcall undef(char *s1)
{
__int64 v1; // r15
void **v2; // rbx
void **v3; // r14
char *v4; // r12
void **v5; // rbp
int v6; // eax

v1 = (unsigned int)hash(s1);
v2 = (void **)hashtab[v1];
if ( v2 )
{
v3 = 0LL;
while ( 1 )
{
v4 = (char *)v2[1];
v5 = v2;
v6 = strcmp(s1, v4);
v2 = (void **)*v2;
if ( !v6 )
break;
v3 = v5;
if ( !v2 )
return;
}
if ( v3 )
*v3 = v2;
else
hashtab[v1] = (__int64)v2;
free(v4);
free(v5[2]);
free(v5);
}
}

canonical_decompiled_code:
void __fastcall func3(char *s1)
{
__int64 v1; // r15
void **v2; // rbx
void **v3; // r14
char *v4; // r12
void **v5; // rbp
int v6; // eax

v1 = (unsigned int)func0(s1);
v2 = (void **)hashtab[v1];
if ( v2 )
{
v3 = 0LL;
while ( 1 )
{
v4 = (char *)v2[1];
v5 = v2;
v6 = strcmp(s1, v4);
v2 = (void **)*v2;
if ( !v6 )
break;
v3 = v5;
if ( !v2 )
return;
}
if ( v3 )
*v3 = v2;
else
hashtab[v1] = (__int64)v2;
free(v4);
free(v5[2]);
free(v5);
}
}

original_code:
static void undef(char *s) {
    unsigned h;
    struct nlist *prior;
    struct nlist *np;
    prior = ((void *)0);
    h = hash(s);
    for (np = hashtab[h]; np != ((void *)0); np = np->next) {
        if (strcmp(s, np->name) == 0)
            break;
        prior = np;
    }
    if (np != ((void *)0)) {
        if (prior == ((void *)0))
            hashtab[h] = np->next;
        else
            prior->next = np->next;
        free((void *)np->name);
        free((void *)np->defn);
        free((void *)np);
    }
}

canonical_original_code:
static void undef(char *s) {
    unsigned int h;
    struct nlist *prior;
    struct nlist *np;
    prior = ((void *)0);
    h = hash(s);
    for (np = hashtab[h]; np != ((void *)0); np = np->next) {
        if (strcmp(s, np->name) == 0)
            break;
        prior = np;
    }
    if (np != ((void *)0)) {
        if (prior == ((void *)0))
            hashtab[h] = np->next;
        else
            prior->next = np->next;
        free((void *)np->name);
        free((void *)np->defn);
        free((void *)np);
    }
}

variable_types:
h
unsigned int

user_defined_types: struct nlist { struct nlist *next; char *name; char *defn; }


mystrdup

canonical_original_code:
static char *mystrdup(char *s) {
    char *p = (char *)malloc(strlen(s) + 1);
    if (p == ((void *)0)) {
        printf("Error: ran out of memory");
        exit(1);
    }
    strcpy(p, s);
    return p;
}


skipblanks

decompiled_code:
__int64 skipblanks()
{
char v0; // al

do
{
do
v0 = getch();
while ( v0 == 9 );
}
while ( v0 == 32 );
return ungetch(v0);
}

canonical_decompiled_code:
__int64 func6()
{
char v0; // al

do
{
do
v0 = func2();
while ( v0 == 9 );
}
while ( v0 == 32 );
return func5(v0);
}

original_code:
static void skipblanks(void) {
    int c;
    while ((c = getch()) == ' ' || c == '\t')
        ;
    ungetch(c);
}

canonical_original_code:
static void skipblanks() {
    int c;
    while ((c = getch()) == ' ' || c == '\t')
        ;
    ungetch(c);
}

variable_types: c int


peekch

decompiled_code:
__int64 peekch()
{
char v0; // al
unsigned int v1; // er12

v0 = getch();
v1 = v0;
ungetch(v0);
return v1;
}

canonical_decompiled_code:
__int64 func8()
{
char v0; // al
unsigned int v1; // er12

v0 = func2();
v1 = v0;
func5(v0);
return v1;
}

original_code:
static char peekch() {
    char c;
    ungetch(c = getch());
    return c;
}

variable_types: c char

return_type: char


getdef

original_code:
static void getdef(void) {
    char def[100] = "";
    char dir[100] = "";
    char name[100] = "";
    int i;
    skipblanks();
    if (!((*__ctype_b_loc ())[(int) ((getword(dir, 100)))] & (unsigned short int) _ISalpha))
        error(dir[0], "getdef: expecting a directive after '#'--got '%s'");
    else if (strcmp(dir, "define") == 0) {
        skipblanks();
        if (!((*__ctype_b_loc ())[(int) ((getword(name, 100)))] & (unsigned short int) _ISalpha))
            error(name[0], "getdef: non-alpha name expected");
        else {
            skipblanks();
            for (i = 0; i < 100 - 1; i++)
                if ((def[i] = getch()) == (-1) || def[i] == '\n')
                    break;
            def[i] = '\0';
            if (i <= 0)
                error('\n', "getdef; incomplete define");
            else
                (void)install(name, def);
        }
    } else if (strcmp(dir, "undef") == 0) {
        skipblanks();
        if (!((*__ctype_b_loc ())[(int) ((getword(name, 100)))] & (unsigned short int) _ISalpha))
            error(name[0], "getdef: non-alpha in undef");
        else
            undef(name);
    } else if (strcmp(dir, "include") == 0) {
        printf("#include");
    } else
        error(dir[0], "getdef: expecting a directive after #");
}

canonical_original_code:
static void getdef() {
    char def[100] = "";
    char dir[100] = "";
    char name[100] = "";
    int i;
    skipblanks();
    if (!((*__ctype_b_loc ())[(int) ((getword(dir, 100)))] & (unsigned short) _ISalpha))
        error(dir[0], "getdef: expecting a directive after '#'--got '%s'");
    else if (strcmp(dir, "define") == 0) {
        skipblanks();
        if (!((*__ctype_b_loc ())[(int) ((getword(name, 100)))] & (unsigned short) _ISalpha))
            error(name[0], "getdef: non-alpha name expected");
        else {
            skipblanks();
            for (i = 0; i < 100 - 1; i++)
                if ((def[i] = getch()) == (-1) || def[i] == '\n')
                    break;
            def[i] = '\0';
            if (i <= 0)
                error('\n', "getdef; incomplete define");
            else
                (void)install(name, def);
        }
    } else if (strcmp(dir, "undef") == 0) {
        skipblanks();
        if (!((*__ctype_b_loc ())[(int) ((getword(name, 100)))] & (unsigned short) _ISalpha))
            error(name[0], "getdef: non-alpha in undef");
        else
            undef(name);
    } else if (strcmp(dir, "include") == 0) {
        printf("#include");
    } else
        error(dir[0], "getdef: expecting a directive after #");
}

variable_types:
def
char [100]

variable_types:
dir
char [100]

variable_types:
name
char [100]


ungets

decompiled_code:
size_t __fastcall ungets(const char *a1)
{
size_t result; // rax
size_t v2; // rbx

result = strlen(a1);
if ( result )
{
v2 = result;
do
result = ungetch(a1[--v2]);
while ( v2 );
}
return result;
}

canonical_decompiled_code:
size_t __fastcall func7(const char *a1)
{
size_t result; // rax
size_t v2; // rbx

result = strlen(a1);
if ( result )
{
v2 = result;
do
result = func5(a1[--v2]);
while ( v2 );
}
return result;
}

original_code:
static void ungets(char s[]) {
    size_t slen = strlen(s);
    while (slen > 0)
        ungetch(s[--slen]);
}

canonical_original_code:
static void ungets(char s[]) {
    unsigned long slen = strlen(s);
    while (slen > 0)
        ungetch(s[--slen]);
}

variable_types:
s
char []

variable_types:
slen
unsigned long


error

decompiled_code:
int __fastcall error(int a1, const char *a2)
{
int result; // eax

result = puts(a2);
if ( a1 != -1 && a1 != 10 )
{
do
result = getch();
while ( (_BYTE)result != 0xFF && (char)result != 10 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func10(int a1, const char *a2)
{
int result; // eax

result = puts(a2);
if ( a1 != -1 && a1 != 10 )
{
do
result = func2();
while ( (_BYTE)result != 0xFF && (char)result != 10 );
}
return result;
}

original_code:
static void error(int c, char *s) {
    printf("%s\n", s);
    while (c != (-1) && c != '\n')
        c = getch();
}


dapeek

decompiled_code:
double dapeek()
{
if ( dstackptr )
return *(double *)(8 * dstackptr + 0x1D8);
puts("Empty stack");
return 0.0;
}

canonical_decompiled_code:
double func2()
{
if ( dstackptr )
return *(double *)(8 * dstackptr + 0x1D8);
puts("Empty stack");
return 0.0;
}

original_code:
double dapeek(void) {
    if (dstackptr > 0)
        return dstack[(dstackptr - 1)];
    printf("Empty stack\n");
    return 0.0L;
}

canonical_original_code:
double dapeek() {
    if (dstackptr > 0)
        return dstack[(dstackptr - 1)];
    printf("Empty stack\n");
    return 0.0L;
}

return_type: double


dadup

decompiled_code:
int dadup()
{
__int64 v0; // rax
double v1; // xmm0_8

v0 = dstackptr;
if ( dstackptr )
{
if ( dstackptr == 100 )
{
LODWORD(v0) = puts("Stack is full");
}
else
{
v1 = *(double *)(8 * dstackptr++ + 0x1D8);
dstack[v0] = v1;
}
}
else
{
LODWORD(v0) = puts("Empty stack");
}
return v0;
}

canonical_decompiled_code:
int func3()
{
__int64 v0; // rax
double v1; // xmm0_8

v0 = dstackptr;
if ( dstackptr )
{
if ( dstackptr == 100 )
{
LODWORD(v0) = puts("Stack is full");
}
else
{
v1 = *(double *)(8 * dstackptr++ + 0x1D8);
dstack[v0] = v1;
}
}
else
{
LODWORD(v0) = puts("Empty stack");
}
return v0;
}

original_code:
void dadup(void) {
    if (dstackptr == 0)
        printf("Empty stack\n");
    else if (dstackptr == 100)
        printf("Stack is full\n");
    else {
        double temp = dstack[dstackptr - 1];
        dstack[dstackptr++] = temp;
    }
}

canonical_original_code:
void dadup() {
    if (dstackptr == 0)
        printf("Empty stack\n");
    else if (dstackptr == 100)
        printf("Stack is full\n");
    else {
        double temp = dstack[dstackptr - 1];
        dstack[dstackptr++] = temp;
    }
}

variable_types: temp double


daswap

decompiled_code:
int daswap()
{
__int64 v0; // rax
double v1; // xmm0_8

if ( (unsigned __int64)dstackptr <= 1 )
{
LODWORD(v0) = puts("Less than two items are on the stack");
}
else
{
v0 = dstackptr - 2;
v1 = dstack[dstackptr - 1];
dstack[dstackptr - 1] = dstack[dstackptr - 2];
dstack[v0] = v1;
}
return v0;
}

canonical_decompiled_code:
int func4()
{
__int64 v0; // rax
double v1; // xmm0_8

if ( (unsigned __int64)dstackptr <= 1 )
{
LODWORD(v0) = puts("Less than two items are on the stack");
}
else
{
v0 = dstackptr - 2;
v1 = dstack[dstackptr - 1];
dstack[dstackptr - 1] = dstack[dstackptr - 2];
dstack[v0] = v1;
}
return v0;
}

original_code:
void daswap(void) {
    if (dstackptr < 2)
        printf("Less than two items are on the stack\n");
    else {
        double temp;
        temp = dstack[dstackptr - 1];
        dstack[dstackptr - 1] = dstack[dstackptr - 2];
        dstack[dstackptr - 2] = temp;
    }
}

canonical_original_code:
void daswap() {
    if (dstackptr < 2)
        printf("Less than two items are on the stack\n");
    else {
        double temp;
        temp = dstack[dstackptr - 1];
        dstack[dstackptr - 1] = dstack[dstackptr - 2];
        dstack[dstackptr - 2] = temp;
    }
}


dalist

decompiled_code:
int dalist()
{
unsigned __int64 v0; // rbx
double v1; // xmm0_8
int result; // eax

if ( dstackptr )
{
v0 = 0LL;
do
{
v1 = dstack[v0++];
result = printf("%.8g\n", v1);
}
while ( dstackptr > v0 );
}
return result;
}

canonical_decompiled_code:
int func5()
{
unsigned __int64 v0; // rbx
double v1; // xmm0_8
int result; // eax

if ( dstackptr )
{
v0 = 0LL;
do
{
v1 = dstack[v0++];
result = printf("%.8g\n", v1);
}
while ( dstackptr > v0 );
}
return result;
}

original_code:
void dalist(void) {
    size_t i;
    for (i = 0; i < dstackptr; i++)
        printf("%.8g\n", dstack[i]);
}

canonical_original_code:
void dalist() {
    unsigned long i;
    for (i = 0; i < dstackptr; i++)
        printf("%.8g\n", dstack[i]);
}

variable_types:
i
unsigned long


daclear

decompiled_code:
void daclear()
{
dstackptr = 0LL;
}

canonical_decompiled_code:
void func6()
{
dstackptr = 0LL;
}

original_code:
void daclear(void) {
    dstackptr = 0;
}

canonical_original_code:
void daclear() {
    dstackptr = 0;
}


dasize

decompiled_code:
__int64 dasize()
{
return dstackptr;
}

canonical_decompiled_code:
__int64 func7()
{
return dstackptr;
}

original_code:
size_t dasize(void) {
    return dstackptr;
}

canonical_original_code:
unsigned long dasize() {
    return dstackptr;
}

return_type: unsigned long


fseek

decompiled_code:
__int64 __fastcall fseek(int *a1, __off_t a2, int a3)
{
__off_t v4; // rbp
int v5; // eax
__off_t v6; // rax
__int64 result; // rax
const void *v8; // rsi
__int64 v9; // r13
ssize_t v10; // r8

v4 = a2;
v5 = a1[6];
if ( (v5 & 1) != 0 )
{
if ( a3 == 1 )
v4 = a2 - *a1;
v6 = lseek(a1[7], v4, a3);
*a1 = 0;
return (unsigned int)-(v6 == -1);
}
result = v5 & 2;
if ( (_DWORD)result )
{
v8 = (const void *)*((_QWORD *)a1 + 2);
v9 = *((_QWORD *)a1 + 1) - (_QWORD)v8;
if ( (unsigned int)*((_QWORD *)a1 + 1) == (_DWORD)v8
|| (v10 = write(a1[7], v8, (unsigned int)v9), result = 0xFFFFFFFFLL, v10 == (unsigned int)v9) )
{
v6 = lseek(a1[7], v4, a3);
return (unsigned int)-(v6 == -1);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(int *a1, __off_t a2, int a3)
{
__off_t v4; // rbp
int v5; // eax
__off_t v6; // rax
__int64 result; // rax
const void *v8; // rsi
__int64 v9; // r13
ssize_t v10; // r8

v4 = a2;
v5 = a1[6];
if ( (v5 & 1) != 0 )
{
if ( a3 == 1 )
v4 = a2 - *a1;
v6 = lseek(a1[7], v4, a3);
*a1 = 0;
return (unsigned int)-(v6 == -1);
}
result = v5 & 2;
if ( (_DWORD)result )
{
v8 = (const void *)*((_QWORD *)a1 + 2);
v9 = *((_QWORD *)a1 + 1) - (_QWORD)v8;
if ( (unsigned int)*((_QWORD *)a1 + 1) == (_DWORD)v8
|| (v10 = write(a1[7], v8, (unsigned int)v9), result = 0xFFFFFFFFLL, v10 == (unsigned int)v9) )
{
v6 = lseek(a1[7], v4, a3);
return (unsigned int)-(v6 == -1);
}
}
return result;
}

original_code:
int fseek(FILE *fp, long offset, int origin) {
    unsigned nc;
    long rc = 0;
    if (fp->flag & _READ) {
        if (origin == 1)
            offset -= fp->cnt;
        rc = lseek(fp->fd, offset, origin);
        fp->cnt = 0;
    } else if (fp->flag & _WRITE) {
        nc = (fp->ptr - fp->base);
        if (nc > 0)
            if (write(fp->fd, fp->base, nc) != nc)
                rc = -1;
        if (rc != -1)
            rc = lseek(fp->fd, offset, origin);
    }
    return (rc == -1) ? -1 : 0;
}

canonical_original_code:
int fseek(struct _iobuf *fp, long offset, int origin) {
    unsigned int nc;
    long rc = 0;
    if (fp->flag & _READ) {
        if (origin == 1)
            offset -= fp->cnt;
        rc = lseek(fp->fd, offset, origin);
        fp->cnt = 0;
    } else if (fp->flag & _WRITE) {
        nc = (fp->ptr - fp->base);
        if (nc > 0)
            if (write(fp->fd, fp->base, nc) != nc)
                rc = -1;
        if (rc != -1)
            rc = lseek(fp->fd, offset, origin);
    }
    return (rc == -1) ? -1 : 0;
}

variable_types: struct _iobuf { int cnt; char *ptr; char *base; int flag; int fd; } *

variable_types: offset long

variable_types: origin int

variable_types:
nc
unsigned int

variable_types: rc long


casize

decompiled_code:
__int64 casize()
{
return cstackptr;
}

canonical_decompiled_code:
__int64 func0()
{
return cstackptr;
}

original_code:
static size_t casize(void) {
    return cstackptr;
}

canonical_original_code:
static unsigned long casize() {
    return cstackptr;
}


capop

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

if ( cstackptr )
{
result = *(unsigned __int8 *)(cstackptr + 159);
--cstackptr;
}
else
{
puts("Error: Stack is empty--nothing to remove (pop) from it");
result = 0LL;
}
return result;
}

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

if ( cstackptr )
{
result = *(unsigned __int8 *)(cstackptr + 159);
--cstackptr;
}
else
{
puts("Error: Stack is empty--nothing to remove (pop) from it");
result = 0LL;
}
return result;
}

original_code:
char capop(void) {
    if (cstackptr > 0)
        return cstack[--cstackptr];
    printf("Error: Stack is empty--nothing to remove (pop) from it\n");
    return '\0';
}

canonical_original_code:
char capop() {
    if (cstackptr > 0)
        return cstack[--cstackptr];
    printf("Error: Stack is empty--nothing to remove (pop) from it\n");
    return '\0';
}


caempty

decompiled_code:
bool caempty()
{
return casize() == 0;
}

canonical_decompiled_code:
bool func3()
{
return func0() == 0;
}

original_code:
_Bool caempty(void) {
    return (casize() == 0);
}

canonical_original_code:
_Bool caempty() {
    return (casize() == 0);
}


isleap

original_code:
static _Bool isleap(int year) {
    return ((year % 4 == 0) && (year % 100) != 0) || (year % 400 == 0);
}

variable_types: year int


decompiled_code:
const char *__fastcall month_name(int a1)
{
const char *result; // rax

result = "Illegal month";
if ( (unsigned int)(a1 - 1) <= 0xB )
result = name_0[a1];
return result;
}

canonical_decompiled_code:
const char *__fastcall func3(int a1)
{
const char *result; // rax

result = "Illegal month";
if ( (unsigned int)(a1 - 1) <= 0xB )
result = name_0[a1];
return result;
}

original_code:
static void *month_name(int month) {
    static char *name[] = {
        "Illegal month",
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
    };
    return (month < 1 || month > 12) ? name[0] : name[month];
}

variable_types: month int

variable_types:
name
char *[]

return_type: void *


compare

decompiled_code:
__int64 __fastcall compare(char *a1, __int64 a2, int a3, _BYTE *a4)
{
_BYTE *v4; // rax
char v7; // dl
int v9; // ecx
int v10; // esi
__int64 result; // rax

v4 = *(_BYTE **)a2;
v7 = *a1;
if ( **(_BYTE **)a2 == *a1 )
{
v9 = 1 - (_DWORD)a1;
while ( v7 )
{
v10 = v9 + (_DWORD)a1;
++v4;
v7 = *++a1;
if ( *a1 != *v4 )
goto LABEL_7;
}
result = 0LL;
}
else
{
v10 = 0;
LABEL_7:
if ( a3 <= v10 )
{
*a4 = 1;
*(_BYTE *)(a2 + 8) = 1;
}
result = (unsigned int)(*a1 - (char)*v4);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1, __int64 a2, int a3, _BYTE *a4)
{
_BYTE *v4; // rax
char v7; // dl
int v9; // ecx
int v10; // esi
__int64 result; // rax

v4 = *(_BYTE **)a2;
v7 = *a1;
if ( **(_BYTE **)a2 == *a1 )
{
v9 = 1 - (_DWORD)a1;
while ( v7 )
{
v10 = v9 + (_DWORD)a1;
++v4;
v7 = *++a1;
if ( *a1 != *v4 )
goto LABEL_7;
}
result = 0LL;
}
else
{
v10 = 0;
LABEL_7:
if ( a3 <= v10 )
{
*a4 = 1;
*(_BYTE *)(a2 + 8) = 1;
}
result = (unsigned int)(*a1 - (char)*v4);
}
return result;
}

original_code:
static int compare(char *s, struct tnode *p, int num, _Bool *found) {
    int i;
    char *t = p->word;
    for (i = 0; *s == *t; i++, s++, t++)
        if (*s == '\0')
            return 0;
    if (i >= num) {
        *found = 1;
        p->match = 1;
    }
    return *s - *t;
}

variable_types:
s
char *

variable_types: struct tnode { char *word; _Bool match; struct tnode *left; struct tnode *right; } *

variable_types: num int

variable_types:
found
_Bool *

variable_types: i int

variable_types:
t
char *


talloc

decompiled_code:
void *talloc()
{
void *result; // rax

result = malloc(0x20uLL);
if ( !result )
{
printf("Error: ran out of memory");
exit(1);
}
return result;
}

canonical_decompiled_code:
void *func5()
{
void *result; // rax

result = malloc(0x20uLL);
if ( !result )
{
printf("Error: ran out of memory");
exit(1);
}
return result;
}

original_code:
static struct tnode *talloc(void) {
    struct tnode *p = (struct tnode *)malloc(sizeof(struct tnode));
    if (p == ((void *)0)) {
        printf("Error: ran out of memory");
        exit(1);
    }
    return p;
}

canonical_original_code:
static struct tnode *talloc() {
    struct tnode *p = (struct tnode *)malloc(sizeof(struct tnode));
    if (p == ((void *)0)) {
        printf("Error: ran out of memory");
        exit(1);
    }
    return p;
}

variable_types: struct tnode { char *word; int count; struct tnode *left; struct tnode *right; } *


mystrdup

decompiled_code:
void *__fastcall mystrdup(void *src)
{
size_t v1; // r12
void *v2; // rax

v1 = strlen((const char *)src) + 1;
v2 = malloc(v1);
if ( !v2 )
{
printf("Error: ran out of memory");
exit(1);
}
return memcpy(v2, src, v1);
}

canonical_decompiled_code:
void *__fastcall func6(void *src)
{
size_t v1; // r12
void *v2; // rax

v1 = strlen((const char *)src) + 1;
v2 = malloc(v1);
if ( !v2 )
{
printf("Error: ran out of memory");
exit(1);
}
return memcpy(v2, src, v1);
}

original_code:
static char *mystrdup(char *s) {
    char *p = (char *)malloc(strlen(s) + 1);
    if (p == ((void *)0)) {
        printf("Error: ran out of memory");
        exit(1);
    }
    strcpy(p, s);
    return p;
}

variable_types:
p
char *


wordlength

decompiled_code:
__int64 wordlength()
{
return 32LL;
}

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

original_code:
static unsigned wordlength(void) {
    unsigned i;
    unsigned v = (unsigned)~0x00;
    for(i = 1; (v = v >> 1) > 0; i++)
        ;
    return i;
}

canonical_original_code:
static unsigned int wordlength() {
    unsigned int i;
    unsigned int v = (unsigned)~0x00;
    for(i = 1; (v = v >> 1) > 0; i++)
        ;
    return i;
}

variable_types:
i
unsigned int

variable_types:
v
unsigned int


rightrot

decompiled_code:
__int64 __fastcall rightrot(unsigned int a1, int a2)
{
char v2; // al
int v4; // esi

v2 = wordlength();
v4 = a2 - 1;
do
a1 = ((a1 & 1) << (v2 - 1)) | (a1 >> 1);
while ( v4-- != 0 );
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, int a2)
{
char v2; // al
int v4; // esi

v2 = func0();
v4 = a2 - 1;
do
a1 = ((a1 & 1) << (v2 - 1)) | (a1 >> 1);
while ( v4-- != 0 );
return a1;
}

original_code:
static int rightrot(int x, unsigned n) {
    unsigned rbit;
    while (n-- > 0) {
        rbit = ((unsigned)x & 0x01) << (wordlength() - 1);
        x = (int)((unsigned)x >> 1);
        x = (int)((unsigned)x | rbit);
    }
    return x;
}

canonical_original_code:
static int rightrot(int x, unsigned int n) {
    unsigned int rbit;
    while (n-- > 0) {
        rbit = ((unsigned int)x & 0x01) << (wordlength() - 1);
        x = (int)((unsigned int)x >> 1);
        x = (int)((unsigned int)x | rbit);
    }
    return x;
}

variable_types: x int

variable_types:
n
unsigned int

variable_types:
rbit
unsigned int


printbits

decompiled_code:
int __fastcall printbits(int a1)
{
int v1; // ebx
int result; // eax

v1 = 32;
printf("0b");
do
{
while ( (a1 & (1 << --v1)) != 0 )
{
result = putchar(49);
if ( !v1 )
return result;
}
result = putchar(48);
}
while ( v1 );
return result;
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
int v1; // ebx
int result; // eax

v1 = 32;
printf("0b");
do
{
while ( (a1 & (1 << --v1)) != 0 )
{
result = putchar(49);
if ( !v1 )
return result;
}
result = putchar(48);
}
while ( v1 );
return result;
}

original_code:
static void printbits(unsigned x) {
    unsigned i;
    printf("0b");
    for(i = (unsigned)(8 * sizeof(int)); i > 0; i--)
        (_Bool)(x & (1 << (i - 1))) ? putchar('1') : putchar('0');
}

canonical_original_code:
static void printbits(unsigned int x) {
    unsigned int i;
    printf("0b");
    for(i = (unsigned int)(8 * sizeof(int)); i > 0; i--)
        (_Bool)(x & (1 << (i - 1))) ? putchar('1') : putchar('0');
}

variable_types:
x
unsigned int


errmsg

decompiled_code:
int __fastcall errmsg(const char *a1)
{
int result; // eax

result = printf("%s", a1);
prevtoken = 1;
return result;
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
int result; // eax

result = printf("%s", a1);
prevtoken = 1;
return result;
}

original_code:
static void errmsg(char *msg) {
    printf("%s", msg);
    prevtoken = 1;
}

variable_types:
msg
char *


typespec

decompiled_code:
bool typespec()
{
char *key; // [rsp+8h] [rbp-10h] BYREF

key = token;
return bsearch(&key, &types_1, 9uLL, 8uLL, scompare) != 0LL;
}

canonical_decompiled_code:
bool func2()
{
char *key; // [rsp+8h] [rbp-10h] BYREF

key = token;
return bsearch(&key, &types_1, 9uLL, 8uLL, scompare) != 0LL;
}

original_code:
static _Bool typespec(void) {
    static char *types[] = {
        "char",
        "double",
        "float",
        "int",
        "long",
        "short",
        "signed",
        "unsigned",
        "void"
    };
    char *pt = token;
    char **result;
    result = (char **)bsearch( (char *) &pt, (char *)types, sizeof(types)/sizeof(types[0]),
                              sizeof( char * ), (int (*)(const void*, const void*))scompare);
    return (result != ((void *)0));}

canonical_original_code:
static _Bool typespec() {
    static char *types[] = {
        "char",
        "double",
        "float",
        "int",
        "long",
        "short",
        "signed",
        "unsigned",
        "void"
    };
    char *pt = token;
    char **result;
    result = (char **)bsearch( (char *) &pt, (char *)types, sizeof(types)/sizeof(types[0]),
                              sizeof( char * ), (int (*)(void *, void *))scompare);
    return (result != ((void *)0));}

variable_types:
types
char *[]

variable_types:
pt
char *

variable_types:
result
char **


typequal

decompiled_code:
bool typequal()
{
char *key; // [rsp+8h] [rbp-10h] BYREF

key = token;
return bsearch(&key, typeq_0, 2uLL, 8uLL, scompare) != 0LL;
}

canonical_decompiled_code:
bool func3()
{
char *key; // [rsp+8h] [rbp-10h] BYREF

key = token;
return bsearch(&key, typeq_0, 2uLL, 8uLL, scompare) != 0LL;
}

original_code:
static _Bool typequal(void) {
    static char *typeq[] = {
        "const",
        "volatile"
    };
    char *pt = token;
    char **result;
    result = (char **)bsearch( (char *) &pt, (char *)typeq, sizeof(typeq)/sizeof(typeq[0]),
                              sizeof( char * ), (int (*)(const void*, const void*))scompare);
    return (result != ((void *)0));
}

canonical_original_code:
static _Bool typequal() {
    static char *typeq[] = {
        "const",
        "volatile"
    };
    char *pt = token;
    char **result;
    result = (char **)bsearch( (char *) &pt, (char *)typeq, sizeof(typeq)/sizeof(typeq[0]),
                              sizeof( char * ), (int (*)(void *, void *))scompare);
    return (result != ((void *)0));
}

variable_types:
typeq
char *[]


scompare

decompiled_code:
int __fastcall scompare(const void *a1, const void *a2)
{
return strcmp(*(const char **)a1, *(const char **)a2);
}

canonical_decompiled_code:
int __fastcall func4(const void *a1, const void *a2)
{
return strcmp(*(const char **)a1, *(const char **)a2);
}

original_code:
static int scompare(char **s, char **t) {
    return strcmp(*s, *t);
}

variable_types:
s
char **

variable_types:
t
char **


gettoken

decompiled_code:
__int64 gettoken()
{
int v0; // eax
unsigned int v1; // er12
const unsigned __int16 **v2; // rbx
__int64 result; // rax
char *v4; // rbx
char v5; // al
char *i; // r12
const unsigned __int16 *v7; // rbp
int v8; // eax
int v9; // eax

if ( prevtoken )
{
prevtoken = 0;
result = (unsigned int)tokentype;
}
else
{
do
{
do
{
v0 = getch();
v1 = v0;
}
while ( v0 == 32 );
}
while ( v0 == 9 );
if ( v0 == 40 )
{
v9 = getch();
if ( v9 == 41 )
{
v1 = 1;
strcpy(token, "()");
tokentype = 1;
}
else
{
ungetch(v9);
tokentype = 40;
}
return v1;
}
if ( v0 == 91 )
{
token[0] = 91;
v4 = &token[1];
do
{
v5 = getch();
*v4++ = v5;
}
while ( v5 != 93 );
*v4 = 0;
v1 = 2;
tokentype = 2;
return v1;
}
v2 = _ctype_b_loc();
if ( ((*v2)[v1] & 0x400) == 0 )
{
tokentype = v1;
return v1;
}
token[0] = v1;
for ( i = &token[1]; ; ++i )
{
v7 = *v2;
v8 = getch();
if ( (v7[v8] & 8) == 0 )
break;
*i = v8;
}
*i = 0;
ungetch(v8);
result = 0LL;
tokentype = 0;
}
return result;
}

canonical_decompiled_code:
__int64 func6()
{
int v0; // eax
unsigned int v1; // er12
const unsigned __int16 **v2; // rbx
__int64 result; // rax
char *v4; // rbx
char v5; // al
char *i; // r12
const unsigned __int16 *v7; // rbp
int v8; // eax
int v9; // eax

if ( prevtoken )
{
prevtoken = 0;
result = (unsigned int)tokentype;
}
else
{
do
{
do
{
v0 = func5();
v1 = v0;
}
while ( v0 == 32 );
}
while ( v0 == 9 );
if ( v0 == 40 )
{
v9 = func5();
if ( v9 == 41 )
{
v1 = 1;
strcpy(token, "()");
tokentype = 1;
}
else
{
func0(v9);
tokentype = 40;
}
return v1;
}
if ( v0 == 91 )
{
token[0] = 91;
v4 = &token[1];
do
{
v5 = func5();
*v4++ = v5;
}
while ( v5 != 93 );
*v4 = 0;
v1 = 2;
tokentype = 2;
return v1;
}
v2 = _ctype_b_loc();
if ( ((*v2)[v1] & 0x400) == 0 )
{
tokentype = v1;
return v1;
}
token[0] = v1;
for ( i = &token[1]; ; ++i )
{
v7 = *v2;
v8 = func5();
if ( (v7[v8] & 8) == 0 )
break;
*i = v8;
}
*i = 0;
func0(v8);
result = 0LL;
tokentype = 0;
}
return result;
}

original_code:
static int gettoken(void) {
    int c;
    char *p = token;
    if (prevtoken) {
        prevtoken = 0;
        return tokentype;
    }
    while ((c = getch()) == ' ' || c == '\t')
        ;
    if (c == '(') {
        if ((c = getch()) == ')') {
            strcpy(token, "()");
            return (tokentype = PARENS);
        }
        ungetch(c);
        return (tokentype = '(');
    }
    if (c == '[') {
        for (*p++ = c; (*p++ = getch()) != ']'; )
            ;
        *p = '\0';
        return (tokentype = BRACKETS);
    }
    if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISalpha)) {
        for (*p++ = c; ((*__ctype_b_loc ())[(int) ((c = getch()))] & (unsigned short int) _ISalnum); )
            *p++ = c;
        *p = '\0';
        ungetch(c);
        return (tokentype = NAME);
    }
    return (tokentype = c);
}

canonical_original_code:
static int gettoken() {
    int c;
    char *p = token;
    if (prevtoken) {
        prevtoken = 0;
        return tokentype;
    }
    while ((c = getch()) == ' ' || c == '\t')
        ;
    if (c == '(') {
        if ((c = getch()) == ')') {
            strcpy(token, "()");
            return (tokentype = PARENS);
        }
        ungetch(c);
        return (tokentype = '(');
    }
    if (c == '[') {
        for (*p++ = c; (*p++ = getch()) != ']'; )
            ;
        *p = '\0';
        return (tokentype = BRACKETS);
    }
    if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISalpha)) {
        for (*p++ = c; ((*__ctype_b_loc ())[(int) ((c = getch()))] & (unsigned short) _ISalnum); )
            *p++ = c;
        *p = '\0';
        ungetch(c);
        return (tokentype = NAME);
    }
    return (tokentype = c);
}

variable_types: c int


dcl

decompiled_code:
size_t dcl()
{
int v0; // ebp
size_t result; // rax
int v2; // ebx

v0 = 0;
while ( (unsigned int)gettoken() == 42 )
++v0;
result = dirdcl();
v2 = v0 - 1;
if ( v0 )
{
do
{
result = strlen(out);
*(_QWORD *)&out[result] = 0x7265746E696F7020LL;
*(_DWORD *)&out[result + 8] = 7304224;
}
while ( v2-- != 0 );
}
return result;
}

canonical_decompiled_code:
size_t func8()
{
int v0; // ebp
size_t result; // rax
int v2; // ebx

v0 = 0;
while ( (unsigned int)func6() == 42 )
++v0;
result = func7();
v2 = v0 - 1;
if ( v0 )
{
do
{
result = strlen(out);
*(_QWORD *)&out[result] = 0x7265746E696F7020LL;
*(_DWORD *)&out[result + 8] = 7304224;
}
while ( v2-- != 0 );
}
return result;
}

original_code:
static void dcl(void) {
    int ns;
    for (ns = 0; gettoken() == '*'; )
        ns++;
    dirdcl();
    while (ns-- > 0)
        strcat(out, " pointer to");
}

canonical_original_code:
static void dcl() {
    int ns;
    for (ns = 0; gettoken() == '*'; )
        ns++;
    dirdcl();
    while (ns-- > 0)
        strcat(out, " pointer to");
}

variable_types: ns int


dclspec

decompiled_code:
size_t __fastcall dclspec(__int64 a1)
{
int v1; // ebx
size_t v2; // rax
char *v3; // rdi
size_t result; // rax
char v5[120]; // [rsp+0h] [rbp-78h] BYREF

v5[0] = 0;
gettoken(a1);
v1 = tokentype;
do
{
if ( v1 )
{
prevtoken = 1;
dcl();
goto LABEL_3;
}
if ( !typespec() && !typequal() )
{
errmsg("unknown type in parameter list\n");
LABEL_3:
v1 = tokentype;
if ( tokentype == 44 )
break;
continue;
}
v2 = strlen(v5);
v3 = &v5[v2 + 1];
v5[v2] = 32;
strcpy(&v5[v2 + 1], token);
gettoken(v3);
v1 = tokentype;
if ( tokentype == 44 )
break;
}
while ( v1 != 41 );
result = (size_t)strcat(out, v5);
if ( v1 == 44 )
{
result = strlen(out);
*(_WORD *)&out[result] = 44;
}
return result;
}

canonical_decompiled_code:
size_t __fastcall func9(__int64 a1)
{
int v1; // ebx
size_t v2; // rax
char *v3; // rdi
size_t result; // rax
char v5[120]; // [rsp+0h] [rbp-78h] BYREF

v5[0] = 0;
func6(a1);
v1 = tokentype;
do
{
if ( v1 )
{
prevtoken = 1;
func8();
goto LABEL_3;
}
if ( !func2() && !func3() )
{
func1("unknown type in parameter list\n");
LABEL_3:
v1 = tokentype;
if ( tokentype == 44 )
break;
continue;
}
v2 = strlen(v5);
v3 = &v5[v2 + 1];
v5[v2] = 32;
strcpy(&v5[v2 + 1], token);
func6(v3);
v1 = tokentype;
if ( tokentype == 44 )
break;
}
while ( v1 != 41 );
result = (size_t)strcat(out, v5);
if ( v1 == 44 )
{
result = strlen(out);
*(_WORD *)&out[result] = 44;
}
return result;
}

original_code:
static void dclspec(void) {
    char temp[100];
    temp[0] = '\0';
    (void)gettoken();
    do {
        if (tokentype != NAME) {
            prevtoken = 1;
            dcl();
        } else if (typespec() || typequal()) {
            strcat(temp, " ");
            strcat(temp, token);
            (void)gettoken();
        } else
            errmsg("unknown type in parameter list\n");
    } while (tokentype != ',' && tokentype != ')');
    strcat(out, temp);
    if (tokentype == ',')
        strcat(out, ",");
}

canonical_original_code:
static void dclspec() {
    char temp[100];
    temp[0] = '\0';
    (void)gettoken();
    do {
        if (tokentype != NAME) {
            prevtoken = 1;
            dcl();
        } else if (typespec() || typequal()) {
            strcat(temp, " ");
            strcat(temp, token);
            (void)gettoken();
        } else
            errmsg("unknown type in parameter list\n");
    } while (tokentype != ',' && tokentype != ')');
    strcat(out, temp);
    if (tokentype == ',')
        strcat(out, ",");
}

variable_types:
temp
char [100]


parmdcl

decompiled_code:
int __fastcall parmdcl(__int64 a1)
{
int result; // eax

do
{
dclspec(a1);
result = tokentype;
}
while ( tokentype == 44 );
if ( tokentype != 41 )
result = errmsg("missing ')' in parameter declaration\n");
return result;
}

canonical_decompiled_code:
int __fastcall func10(__int64 a1)
{
int result; // eax

do
{
func9(a1);
result = tokentype;
}
while ( tokentype == 44 );
if ( tokentype != 41 )
result = func1("missing ')' in parameter declaration\n");
return result;
}

original_code:
static void parmdcl(void) {
    do {
        dclspec();
    } while (tokentype == ',');
    if (tokentype != ')')
        errmsg("missing ')' in parameter declaration\n");
}

canonical_original_code:
static void parmdcl() {
    do {
        dclspec();
    } while (tokentype == ',');
    if (tokentype != ')')
        errmsg("missing ')' in parameter declaration\n");
}


readargs

decompiled_code:
__int64 __fastcall readargs(int a1, const char **a2)
{
int v2; // er12
const char *v4; // rbp
int v5; // eax
const char *v6; // rdi
__int64 v7; // rsi
char v8; // al
char v9; // r8
char v10; // r10
char v11; // di
char v12; // cl
char v13; // r13
char v14; // r9
char v15; // bp
char v16; // r11
char *v17; // rdx
__int64 result; // rax

v2 = a1 - 1;
if ( a1 - 1 > 0 )
{
while ( 1 )
{
v4 = a2[1];
++a2;
v5 = *v4;
if ( v5 != 45 )
break;
v6 = v4 + 1;
if ( ((*_ctype_b_loc())[v4[1]] & 0x800) != 0 )
{
pos2 = atoi(v6);
if ( !--v2 )
goto LABEL_9;
}
else
{
*a2 = v6;
v7 = (unsigned int)v4[1];
v8 = v4[1];
if ( v8 )
{
v9 = descend;
v10 = 0;
v11 = 0;
v12 = 0;
v13 = numeric;
v14 = fold;
v15 = 0;
v16 = dir;
do
{
if ( v8 == 110 )
{
v15 = 1;
v13 = 1;
}
else if ( v8 > 110 )
{
if ( v8 != 114 )
{
LABEL_33:
if ( v15 )
numeric = v13;
if ( v12 )
descend = v9;
if ( v11 )
fold = v14;
if ( v10 )
dir = v16;
printf("sort: illegal option %c\n", v7);
exit(1);
}
v12 = 1;
v9 = 1;
}
else if ( v8 == 100 )
{
v10 = 1;
v16 = 1;
}
else
{
if ( v8 != 102 )
goto LABEL_33;
v11 = 1;
v14 = 1;
}
v17 = (char *)(*a2 + 1);
*a2 = v17;
v7 = (unsigned int)*v17;
v8 = *v17;
}
while ( *v17 );
if ( v15 )
numeric = v13;
if ( v12 )
descend = v9;
if ( v11 )
fold = v14;
if ( v10 )
dir = v16;
}
LABEL_5:
if ( !--v2 )
goto LABEL_9;
}
}
if ( v5 != 43 )
goto LABEL_31;
pos1 = atoi(v4 + 1);
goto LABEL_5;
}
if ( a1 == 1 )
goto LABEL_9;
LABEL_31:
result = pos2;
if ( pos1 > (unsigned __int64)pos2 )
LABEL_9:
error("usage: sort -dnfr [+pos1] [-pos2] [FILE]");
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, const char **a2)
{
int v2; // er12
const char *v4; // rbp
int v5; // eax
const char *v6; // rdi
__int64 v7; // rsi
char v8; // al
char v9; // r8
char v10; // r10
char v11; // di
char v12; // cl
char v13; // r13
char v14; // r9
char v15; // bp
char v16; // r11
char *v17; // rdx
__int64 result; // rax

v2 = a1 - 1;
if ( a1 - 1 > 0 )
{
while ( 1 )
{
v4 = a2[1];
++a2;
v5 = *v4;
if ( v5 != 45 )
break;
v6 = v4 + 1;
if ( ((*_ctype_b_loc())[v4[1]] & 0x800) != 0 )
{
pos2 = atoi(v6);
if ( !--v2 )
goto LABEL_9;
}
else
{
*a2 = v6;
v7 = (unsigned int)v4[1];
v8 = v4[1];
if ( v8 )
{
v9 = descend;
v10 = 0;
v11 = 0;
v12 = 0;
v13 = numeric;
v14 = fold;
v15 = 0;
v16 = dir;
do
{
if ( v8 == 110 )
{
v15 = 1;
v13 = 1;
}
else if ( v8 > 110 )
{
if ( v8 != 114 )
{
LABEL_33:
if ( v15 )
numeric = v13;
if ( v12 )
descend = v9;
if ( v11 )
fold = v14;
if ( v10 )
dir = v16;
printf("sort: illegal option %c\n", v7);
exit(1);
}
v12 = 1;
v9 = 1;
}
else if ( v8 == 100 )
{
v10 = 1;
v16 = 1;
}
else
{
if ( v8 != 102 )
goto LABEL_33;
v11 = 1;
v14 = 1;
}
v17 = (char *)(*a2 + 1);
*a2 = v17;
v7 = (unsigned int)*v17;
v8 = *v17;
}
while ( *v17 );
if ( v15 )
numeric = v13;
if ( v12 )
descend = v9;
if ( v11 )
fold = v14;
if ( v10 )
dir = v16;
}
LABEL_5:
if ( !--v2 )
goto LABEL_9;
}
}
if ( v5 != 43 )
goto LABEL_31;
pos1 = atoi(v4 + 1);
goto LABEL_5;
}
if ( a1 == 1 )
goto LABEL_9;
LABEL_31:
result = pos2;
if ( pos1 > (unsigned __int64)pos2 )
LABEL_9:
func1("usage: sort -dnfr [+pos1] [-pos2] [FILE]");
return result;
}

original_code:
static void readargs(int argc, char *argv[]) {
    int c;
    while (--argc > 0 && ((c = (*++argv)[0]) == '-' || c == '+'))
        if (c == '-' && !((*__ctype_b_loc ())[(int) ((*(argv[0]+1)))] & (unsigned short int) _ISdigit))
            while ((c = *++argv[0]) != '\0')
                switch (c) {
                    case 'n':
                        numeric = 1;
                        break;
                    case 'r':
                        descend = 1;
                        break;
                    case 'f':
                        fold = 1;
                        break;
                    case 'd':
                        dir = 1;
                        break;
                    default:
                        printf("sort: illegal option %c\n", c);
                        exit(1);
                }
        else if (c == '-')
            pos2 = (size_t)atoi(argv[0]+1);
        else
            pos1 = (size_t)atoi(argv[0]+1);
    if ((argc == 0) || (pos1 > pos2))
        error("usage: sort -dnfr [+pos1] [-pos2] [FILE]");
}

canonical_original_code:
static void readargs(int argc, char *argv[]) {
    int c;
    while (--argc > 0 && ((c = (*++argv)[0]) == '-' || c == '+'))
        if (c == '-' && !((*__ctype_b_loc ())[(int) ((*(argv[0]+1)))] & (unsigned short) _ISdigit))
            while ((c = *++argv[0]) != '\0')
                switch (c) {
                    case 'n':
                        numeric = 1;
                        break;
                    case 'r':
                        descend = 1;
                        break;
                    case 'f':
                        fold = 1;
                        break;
                    case 'd':
                        dir = 1;
                        break;
                    default:
                        printf("sort: illegal option %c\n", c);
                        exit(1);
                }
        else if (c == '-')
            pos2 = (unsigned long)atoi(argv[0]+1);
        else
            pos1 = (unsigned long)atoi(argv[0]+1);
    if ((argc == 0) || (pos1 > pos2))
        error("usage: sort -dnfr [+pos1] [-pos2] [FILE]");
}

variable_types: argc int

variable_types:
argv
char *[]


alloc

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

if ( "substr: string is too short" - (_BYTE *)allocp < a1 )
return 0LL;
result = allocp;
allocp = (char *)allocp + a1;
return result;
}

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

if ( "substr: string is too short" - (_BYTE *)allocp < a1 )
return 0LL;
result = allocp;
allocp = (char *)allocp + a1;
return result;
}

original_code:
static char *alloc(int n) {
    if (allocbuf + 10000000 - allocp >= n) {
        allocp += n;
        return allocp - n;
    }
    return 0;
}

variable_types: n int


error

decompiled_code:
void __fastcall __noreturn error(const char *a1)
{
puts(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(const char *a1)
{
puts(a1);
exit(1);
}

original_code:
static void error(char *s) {
    printf("%s\n", s);
    exit(1);
}

variable_types:
s
char *


dircmp

decompiled_code:
__int64 __fastcall dircmp(const char *a1, const char *a2)
{
size_t v3; // rbp
unsigned __int64 v4; // r15
size_t v5; // rax
unsigned __int64 v6; // rbx
int v7; // er14
int v8; // eax
const unsigned __int16 *v9; // rsi
__int64 v10; // rcx
unsigned __int64 v11; // rdx
unsigned __int64 v12; // rdx

v3 = pos2;
v4 = pos1;
if ( !pos2 )
{
v3 = strlen(a1);
v5 = strlen(a2);
if ( v5 <= v3 )
v3 = v5;
}
v6 = v4;
while ( 1 )
{
v7 = a1[v4];
if ( v4 >= v3 || ((*_ctype_b_loc())[(char)v7] & 8) != 0 || (v7 & 0xDF) == 0 )
{
v8 = a2[v6];
if ( v6 >= v3 )
{
v11 = v6;
}
else
{
v9 = *_ctype_b_loc();
while ( 1 )
{
v10 = a2[v6];
v11 = v6++;
v8 = v10;
if ( (v9[v10] & 8) != 0 || (v10 & 0xDF) == 0 )
break;
if ( v3 <= v6 )
{
v8 = a2[v6++];
if ( (unsigned __int8)v7 | (unsigned __int8)v8 )
goto LABEL_14;
return 0;
}
}
}
v6 = v11 + 1;
if ( !((unsigned __int8)v7 | (unsigned __int8)v8) )
return 0;
LABEL_14:
v12 = v4 + 1;
if ( v6 >= v4 + 1 )
v12 = v6;
if ( v3 <= v12 || (_BYTE)v8 != (_BYTE)v7 )
return (unsigned int)(v7 - v8);
}
++v4;
}
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, const char *a2)
{
size_t v3; // rbp
unsigned __int64 v4; // r15
size_t v5; // rax
unsigned __int64 v6; // rbx
int v7; // er14
int v8; // eax
const unsigned __int16 *v9; // rsi
__int64 v10; // rcx
unsigned __int64 v11; // rdx
unsigned __int64 v12; // rdx

v3 = pos2;
v4 = pos1;
if ( !pos2 )
{
v3 = strlen(a1);
v5 = strlen(a2);
if ( v5 <= v3 )
v3 = v5;
}
v6 = v4;
while ( 1 )
{
v7 = a1[v4];
if ( v4 >= v3 || ((*_ctype_b_loc())[(char)v7] & 8) != 0 || (v7 & 0xDF) == 0 )
{
v8 = a2[v6];
if ( v6 >= v3 )
{
v11 = v6;
}
else
{
v9 = *_ctype_b_loc();
while ( 1 )
{
v10 = a2[v6];
v11 = v6++;
v8 = v10;
if ( (v9[v10] & 8) != 0 || (v10 & 0xDF) == 0 )
break;
if ( v3 <= v6 )
{
v8 = a2[v6++];
if ( (unsigned __int8)v7 | (unsigned __int8)v8 )
goto LABEL_14;
return 0;
}
}
}
v6 = v11 + 1;
if ( !((unsigned __int8)v7 | (unsigned __int8)v8) )
return 0;
LABEL_14:
v12 = v4 + 1;
if ( v6 >= v4 + 1 )
v12 = v6;
if ( v3 <= v12 || (_BYTE)v8 != (_BYTE)v7 )
return (unsigned int)(v7 - v8);
}
++v4;
}
}

original_code:
static int dircmp(const char *s, const char *t) {
    char a;
    char b;
    size_t i = pos1;
    size_t j = pos1;
    size_t endpos;
    if (pos2 > 0)
        endpos = pos2;
    else if ((endpos = strlen(s)) > strlen(t))
        endpos = strlen(t);
    do {
        while (i < endpos && !((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short int) _ISalnum) && s[i] != ' ' && s[i] != '\0')
            i++;
        while (j < endpos && !((*__ctype_b_loc ())[(int) ((t[j]))] & (unsigned short int) _ISalnum) && t[j] != ' ' && t[j] != '\0')
            j++;
        a = s[i++];
        b = t[j++];
        if (a == '\0' && b == '\0')
            return 0;
    } while (a == b && i < endpos && j < endpos);
    return a - b;
}

canonical_original_code:
static int dircmp(char *s, char *t) {
    char a;
    char b;
    unsigned long i = pos1;
    unsigned long j = pos1;
    unsigned long endpos;
    if (pos2 > 0)
        endpos = pos2;
    else if ((endpos = strlen(s)) > strlen(t))
        endpos = strlen(t);
    do {
        while (i < endpos && !((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short) _ISalnum) && s[i] != ' ' && s[i] != '\0')
            i++;
        while (j < endpos && !((*__ctype_b_loc ())[(int) ((t[j]))] & (unsigned short) _ISalnum) && t[j] != ' ' && t[j] != '\0')
            j++;
        a = s[i++];
        b = t[j++];
        if (a == '\0' && b == '\0')
            return 0;
    } while (a == b && i < endpos && j < endpos);
    return a - b;
}

variable_types:
t
char *

variable_types: a char

variable_types: b char

variable_types:
i
unsigned long

variable_types:
j
unsigned long

variable_types:
endpos
unsigned long