decompiled_code:
int __fastcall parse_data(void *haystack, __int64 a2)
{
_BYTE *v2; // rbp
void *v3; // rbx
char *v4; // rax
char *v5; // rbx
char *v6; // rdi
char *v7; // rax
const char *v8; // r15
char *v9; // rax
char *v10; // rax
const char *v11; // r14
char *v12; // rax
char *v13; // rdi
char *v14; // rax
const char *v15; // r13
char *v16; // rax
void *v17; // rax
struct tm *v18; // rax
__int64 v19; // rcx
const char *v21; // [rsp+0h] [rbp-78h]
char *v22; // [rsp+8h] [rbp-70h]
time_t timer; // [rsp+18h] [rbp-60h] BYREF
char s[88]; // [rsp+20h] [rbp-58h] BYREF

v2 = haystack;
v3 = haystack;
while ( 1 )
{
v4 = find_section(v3, (__int64)v2, a2, 0);
v5 = v4;
if ( !v4 )
return (int)v4;
v6 = find_section(v4 + 14, (__int64)v2, a2, 1);
if ( !v6 )
{
v19 = 1LL;
LABEL_14:
LODWORD(v4) = fprintf(_bss_start, "%s: warning: couldn't find section %d\n", (const char *)prog, v19);
return (int)v4;
}
v7 = find_section(v6, (__int64)v2, a2, 2);
v8 = v7;
if ( !v7 )
{
v19 = 2LL;
goto LABEL_14;
}
v9 = find_section(v7, (__int64)v2, a2, 3);
if ( !v9 )
break;
*(v9 - 1) = 0;
v21 = v9 + 1;
v10 = find_section(v9 + 1, (__int64)v2, a2, 5);
v11 = v10;
if ( !v10 )
{
v19 = 5LL;
goto LABEL_14;
}
*(v10 - 1) = 0;
v12 = find_section(v10, (__int64)v2, a2, 6);
v13 = v12;
if ( !v12 )
{
v19 = 6LL;
goto LABEL_14;
}
*(v12 - 1) = 0;
timer = read_time(v12);
v14 = find_section(v13 + 6, (__int64)v2, a2, 7);
v15 = v14;
if ( !v14 )
{
v19 = 7LL;
goto LABEL_14;
}
v16 = find_section(v14, (__int64)v2, a2, 8);
if ( !v16 )
{
v19 = 8LL;
goto LABEL_14;
}
v3 = v5 + 1;
if ( v16 - v15 <= 50 )
{
v22 = v16;
v17 = memchr(v16, 0, a2 - (v16 - v2));
v3 = find_section(v17, (__int64)v2, a2, 9);
if ( !v3 )
{
v19 = 9LL;
goto LABEL_14;
}
v18 = localtime(&timer);
strftime(s, 0x14uLL, "%Y-%m-%d.%H:%M:%S", v18);
output_chat(s, v8, v21, v15, (const char *)v3, v11, v22);
}
}
LODWORD(v4) = fprintf(_bss_start, "%s: warning: couldn't find section %d\n", (const char *)prog, 3LL);
return (int)v4;
}

canonical_decompiled_code:
int __fastcall func11(void *haystack, __int64 a2)
{
_BYTE *v2; // rbp
void *v3; // rbx
char *v4; // rax
char *v5; // rbx
char *v6; // rdi
char *v7; // rax
const char *v8; // r15
char *v9; // rax
char *v10; // rax
const char *v11; // r14
char *v12; // rax
char *v13; // rdi
char *v14; // rax
const char *v15; // r13
char *v16; // rax
void *v17; // rax
struct tm *v18; // rax
__int64 v19; // rcx
const char *v21; // [rsp+0h] [rbp-78h]
char *v22; // [rsp+8h] [rbp-70h]
time_t timer; // [rsp+18h] [rbp-60h] BYREF
char s[88]; // [rsp+20h] [rbp-58h] BYREF

v2 = haystack;
v3 = haystack;
while ( 1 )
{
v4 = func9(v3, (__int64)v2, a2, 0);
v5 = v4;
if ( !v4 )
return (int)v4;
v6 = func9(v4 + 14, (__int64)v2, a2, 1);
if ( !v6 )
{
v19 = 1LL;
LABEL_14:
LODWORD(v4) = fprintf(_bss_start, "%s: warning: couldn't find section %d\n", (const char *)prog, v19);
return (int)v4;
}
v7 = func9(v6, (__int64)v2, a2, 2);
v8 = v7;
if ( !v7 )
{
v19 = 2LL;
goto LABEL_14;
}
v9 = func9(v7, (__int64)v2, a2, 3);
if ( !v9 )
break;
*(v9 - 1) = 0;
v21 = v9 + 1;
v10 = func9(v9 + 1, (__int64)v2, a2, 5);
v11 = v10;
if ( !v10 )
{
v19 = 5LL;
goto LABEL_14;
}
*(v10 - 1) = 0;
v12 = func9(v10, (__int64)v2, a2, 6);
v13 = v12;
if ( !v12 )
{
v19 = 6LL;
goto LABEL_14;
}
*(v12 - 1) = 0;
timer = func8(v12);
v14 = func9(v13 + 6, (__int64)v2, a2, 7);
v15 = v14;
if ( !v14 )
{
v19 = 7LL;
goto LABEL_14;
}
v16 = func9(v14, (__int64)v2, a2, 8);
if ( !v16 )
{
v19 = 8LL;
goto LABEL_14;
}
v3 = v5 + 1;
if ( v16 - v15 <= 50 )
{
v22 = v16;
v17 = memchr(v16, 0, a2 - (v16 - v2));
v3 = func9(v17, (__int64)v2, a2, 9);
if ( !v3 )
{
v19 = 9LL;
goto LABEL_14;
}
v18 = localtime(&timer);
strftime(s, 0x14uLL, "%Y-%m-%d.%H:%M:%S", v18);
func10(s, v8, v21, v15, (const char *)v3, v11, v22);
}
}
LODWORD(v4) = fprintf(_bss_start, "%s: warning: couldn't find section %d\n", (const char *)prog, 3LL);
return (int)v4;
}

original_code:
void parse_data(char *data, size_t len)
{
 char *ptr;
 time_t time;
 char timestr[20];
 ptr = data;
 do{
  char *caller, *recipients, *msg, *chatid, *sender, *sendername, *startsection;
  do{ ptr = find_section(ptr, data, len, SEC_START_REC); if(!ptr){ if(SEC_START_REC != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_START_REC); return; } }while(0);
  startsection = ptr;
  ptr += 14;
  do{ ptr = find_section(ptr, data, len, SEC_START_CHAT); if(!ptr){ if(SEC_START_CHAT != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_START_CHAT); return; } }while(0);
  do{ ptr = find_section(ptr, data, len, SEC_START_CALLER); if(!ptr){ if(SEC_START_CALLER != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_START_CALLER); return; } }while(0);
  caller = ptr;
  do{ ptr = find_section(ptr, data, len, SEC_MEM_SEP); if(!ptr){ if(SEC_MEM_SEP != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_MEM_SEP); return; } }while(0);
  ptr[-1] = '\0';
  ptr++;
  recipients = ptr;
  do{ ptr = find_section(ptr, data, len, SEC_END_MEMBS); if(!ptr){ if(SEC_END_MEMBS != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_END_MEMBS); return; } }while(0);
  ptr[-1] = '\0';
  chatid = ptr;
  do{ ptr = find_section(ptr, data, len, SEC_START_TIME); if(!ptr){ if(SEC_START_TIME != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_START_TIME); return; } }while(0);
  ptr[-1] = '\0';
  time = read_time(ptr);
  ptr += 6;
  do{ ptr = find_section(ptr, data, len, SEC_START_SENDER); if(!ptr){ if(SEC_START_SENDER != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_START_SENDER); return; } }while(0);
  sender = ptr;
  do{ ptr = find_section(ptr, data, len, SEC_START_MSG); if(!ptr){ if(SEC_START_MSG != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_START_MSG); return; } }while(0);
  if ((ptr-sender) > MAX_DIST_TO_MSG) {
   ptr = startsection + 1;
   continue;
  }
  msg = ptr;
  ptr = memchr(ptr, 0, len - (ptr - data));
  do{ ptr = find_section(ptr, data, len, SEC_START_SENDERNAME); if(!ptr){ if(SEC_START_SENDERNAME != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_START_SENDERNAME); return; } }while(0);
  sendername = ptr;
  strftime(timestr, sizeof timestr, "%Y-%m-%d.%H:%M:%S", localtime(&time));
  output_chat(timestr, caller, recipients, sender, sendername, chatid, msg);
 }while(1);
}

canonical_original_code:
void parse_data(char *data, unsigned long len)
{
 char *ptr;
 long time;
 char timestr[20];
 ptr = data;
 do{
  char *caller; char *recipients; char *msg; char *chatid; char *sender; char *sendername; char *startsection;
  do{ ptr = find_section(ptr, data, len, SEC_START_REC); if(!ptr){ if(SEC_START_REC != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_START_REC); return; } }while(0);
  startsection = ptr;
  ptr += 14;
  do{ ptr = find_section(ptr, data, len, SEC_START_CHAT); if(!ptr){ if(SEC_START_CHAT != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_START_CHAT); return; } }while(0);
  do{ ptr = find_section(ptr, data, len, SEC_START_CALLER); if(!ptr){ if(SEC_START_CALLER != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_START_CALLER); return; } }while(0);
  caller = ptr;
  do{ ptr = find_section(ptr, data, len, SEC_MEM_SEP); if(!ptr){ if(SEC_MEM_SEP != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_MEM_SEP); return; } }while(0);
  ptr[-1] = '\0';
  ptr++;
  recipients = ptr;
  do{ ptr = find_section(ptr, data, len, SEC_END_MEMBS); if(!ptr){ if(SEC_END_MEMBS != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_END_MEMBS); return; } }while(0);
  ptr[-1] = '\0';
  chatid = ptr;
  do{ ptr = find_section(ptr, data, len, SEC_START_TIME); if(!ptr){ if(SEC_START_TIME != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_START_TIME); return; } }while(0);
  ptr[-1] = '\0';
  time = read_time(ptr);
  ptr += 6;
  do{ ptr = find_section(ptr, data, len, SEC_START_SENDER); if(!ptr){ if(SEC_START_SENDER != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_START_SENDER); return; } }while(0);
  sender = ptr;
  do{ ptr = find_section(ptr, data, len, SEC_START_MSG); if(!ptr){ if(SEC_START_MSG != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_START_MSG); return; } }while(0);
  if ((ptr-sender) > MAX_DIST_TO_MSG) {
   ptr = startsection + 1;
   continue;
  }
  msg = ptr;
  ptr = memchr(ptr, 0, len - (ptr - data));
  do{ ptr = find_section(ptr, data, len, SEC_START_SENDERNAME); if(!ptr){ if(SEC_START_SENDERNAME != 0) fprintf(stderr, "%s: warning: couldn't find section %d\n", prog, SEC_START_SENDERNAME); return; } }while(0);
  sendername = ptr;
  strftime(timestr, sizeof timestr, "%Y-%m-%d.%H:%M:%S", localtime(&time));
  output_chat(timestr, caller, recipients, sender, sendername, chatid, msg);
 }while(1);
}

variable_types:
ptr
char *

variable_types: time long

variable_types:
timestr
char [20]

variable_types:
startsection
char *


process

repo: bobrippling/skype-chatlog-plaintext

decompiled_code:
int __fastcall process(const char *a1)
{
int v1; // eax
int v2; // ebp
__off_t v3; // r13
void *v4; // r12

v1 = open(a1, 0);
if ( v1 == -1 )
die("open \"%s\":", a1);
v2 = v1;
v3 = lseek(v1, 0LL, 2);
v4 = mmap(0LL, v3, 2, 2, v2, 0LL);
if ( v4 == (void *)-1LL )
die("mmap() on \"%s\":", a1);
close(v2);
parse_data(v4, v3);
return munmap(v4, v3);
}

canonical_decompiled_code:
int __fastcall func12(const char *a1)
{
int v1; // eax
int v2; // ebp
__off_t v3; // r13
void *v4; // r12

v1 = open(a1, 0);
if ( v1 == -1 )
func7("open \"%s\":", a1);
v2 = v1;
v3 = lseek(v1, 0LL, 2);
v4 = mmap(0LL, v3, 2, 2, v2, 0LL);
if ( v4 == (void *)-1LL )
func7("mmap() on \"%s\":", a1);
close(v2);
func11(v4, v3);
return munmap(v4, v3);
}

original_code:
void process(const char *fname)
{
 int fd;
 void *buf;
 off_t sz;
 fd = open(fname, 00);
 if(fd == -1)
  die("open \"%s\":", fname);
 sz = lseek(fd, 0, 2);
 buf = mmap(0, sz, 0x2, 0x02, fd, 0);
 if(buf == ((void *) -1))
  die("mmap() on \"%s\":", fname);
 close(fd);
 parse_data(buf, sz);
 munmap(buf, sz);
}

canonical_original_code:
void process(char *fname)
{
 int fd;
 void *buf;
 long sz;
 fd = open(fname, 00);
 if(fd == -1)
  die("open \"%s\":", fname);
 sz = lseek(fd, 0, 2);
 buf = mmap(0, sz, 0x2, 0x02, fd, 0);
 if(buf == ((void *) -1))
  die("mmap() on \"%s\":", fname);
 close(fd);
 parse_data(buf, sz);
 munmap(buf, sz);
}

variable_types:
fname
char *

variable_types: fd int

variable_types:
buf
void *

variable_types: sz long

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v3; // rbp
__int64 v4; // r12

v3 = malloc(0x460uLL);
check_help((unsigned int)argc, argv);
v4 = (unsigned int)init_array(v3);
sort_array(v3, v4);
search_illness(v3, (unsigned int)v4, argv);
free(v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
void *v3; // rbp
__int64 v4; // r12

v3 = malloc(0x460uLL);
func12((unsigned int)argc, argv);
v4 = (unsigned int)func9(v3);
func10(v3, v4);
func11(v3, (unsigned int)v4, argv);
free(v3);
return 0;
}

original_code:
int main (int argc, char **argv)
{
    int array_size;
    struct kindergarden *children;
    children = (struct kindergarden*)malloc(20*sizeof(struct kindergarden));
    check_help(argc, argv);
    array_size = init_array(children);
    sort_array(children, array_size);
    search_illness(children, array_size, argv);
    free(children);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int array_size;
    struct kindergarden *children;
    children = (struct kindergarden *)malloc(20*sizeof(struct kindergarden));
    check_help(argc, argv);
    array_size = init_array(children);
    sort_array(children, array_size);
    search_illness(children, array_size, argv);
    free(children);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct kindergarden { char *surname; int hosp_identify; union { struct { char *illness; } nonhosp; struct { char *illness; } hosp; } hospital; } *

return_type: int


decompiled_code:
unsigned int atoi_change()
{
unsigned int result; // eax
char v1[136]; // [rsp+0h] [rbp-88h] BYREF

while ( 1 )
{
printf("(yes - 1/no - 0) ");
fgets(v1, 128, stdin);
result = atoi(v1);
if ( result <= 1 )
break;
puts("It's not correct. Please, input data again.");
}
return result;
}

canonical_decompiled_code:
unsigned int func7()
{
unsigned int result; // eax
char v1[136]; // [rsp+0h] [rbp-88h] BYREF

while ( 1 )
{
printf("(yes - 1/no - 0) ");
fgets(v1, 128, stdin);
result = atoi(v1);
if ( result <= 1 )
break;
puts("It's not correct. Please, input data again.");
}
return result;
}

original_code:
int atoi_change()
{
    int identifier;
    char string[128];
    while(1)
    {
 printf("(yes - 1/no - 0) ");
 fgets(string, 128, stdin);
 identifier = atoi(string);
 if(identifier == 1 || identifier == 0) { break; }
 puts("It's not correct. Please, input data again.");
    }
    return identifier;
}

variable_types: identifier int

variable_types:
string
char [128]


decompiled_code:
void *__fastcall check_string(const char *a1)
{
void *v1; // r12

v1 = malloc(0x1EuLL);
printf("%s", a1);
__fpurge(stdin);
gets();
return v1;
}

canonical_decompiled_code:
void *__fastcall func8(const char *a1)
{
void *v1; // r12

v1 = malloc(0x1EuLL);
printf("%s", a1);
__fpurge(stdin);
gets();
return v1;
}

original_code:
char* check_string(char* comment)
{
    char *text;
    text = (char*)malloc(30*sizeof(char));
    do
    {
 printf("%s", comment);
        __fpurge(stdin);
        gets(text);
        if(text[0] == '\n') { continue; }
    } while (0);
    return text;
}

canonical_original_code:
char *check_string(char *comment)
{
    char *text;
    text = (char *)malloc(30*sizeof(char));
    do
    {
 printf("%s", comment);
        __fpurge(stdin);
        gets(text);
        if(text[0] == '\n') { continue; }
    } while (0);
    return text;
}

variable_types:
comment
char *

variable_types:
text
char *

return_type: char *


decompiled_code:
__int64 __fastcall init_array(__int64 a1)
{
unsigned int i; // er12

for ( i = 0; ; ++i )
{
*(_QWORD *)a1 = check_string("Enter a child's surname: ");
*(_QWORD *)(a1 + 8) = check_string("Enter a child's name: ");
printf("Was a child hospitalized?");
if ( atoi_change() )
{
*(_DWORD *)(a1 + 16) = 1;
*(_QWORD *)(a1 + 24) = check_string("Enter an illness: ");
*(_QWORD *)(a1 + 32) = check_string("Enter the number of hospital: ");
*(_QWORD *)(a1 + 40) = check_string("Enter the situation of hospital: ");
*(_QWORD *)(a1 + 48) = check_string("Enter a hospital doc's surname: ");
}
else
{
*(_DWORD *)(a1 + 16) = 0;
*(_QWORD *)(a1 + 24) = check_string("Enter an illness: ");
*(_QWORD *)(a1 + 32) = check_string("Enter a doc's surname: ");
}
if ( i == 19 )
break;
a1 += 56LL;
printf("Want to continue?");
if ( !atoi_change() )
return i;
}
return 20;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
unsigned int i; // er12

for ( i = 0; ; ++i )
{
*(_QWORD *)a1 = func8("Enter a child's surname: ");
*(_QWORD *)(a1 + 8) = func8("Enter a child's name: ");
printf("Was a child hospitalized?");
if ( func7() )
{
*(_DWORD *)(a1 + 16) = 1;
*(_QWORD *)(a1 + 24) = func8("Enter an illness: ");
*(_QWORD *)(a1 + 32) = func8("Enter the number of hospital: ");
*(_QWORD *)(a1 + 40) = func8("Enter the situation of hospital: ");
*(_QWORD *)(a1 + 48) = func8("Enter a hospital doc's surname: ");
}
else
{
*(_DWORD *)(a1 + 16) = 0;
*(_QWORD *)(a1 + 24) = func8("Enter an illness: ");
*(_QWORD *)(a1 + 32) = func8("Enter a doc's surname: ");
}
if ( i == 19 )
break;
a1 += 56LL;
printf("Want to continue?");
if ( !func7() )
return i;
}
return 20;
}

original_code:
int init_array(struct kindergarden* new_kids_base)
{
    int n=0, identifier;
    do
    {
 new_kids_base[n].surname = check_string("Enter a child's surname: ");
 new_kids_base[n].name = check_string("Enter a child's name: ");
 printf("Was a child hospitalized?");
 identifier = atoi_change();
 if(identifier)
 {
     new_kids_base[n].hosp_identify = 1;
     new_kids_base[n].hospital.hosp.illness = check_string("Enter an illness: ");
     new_kids_base[n].hospital.hosp.hospnum = check_string("Enter the number of hospital: ");
     new_kids_base[n].hospital.hosp.hospplace = check_string("Enter the situation of hospital: ");
     new_kids_base[n].hospital.hosp.hospdoc = check_string("Enter a hospital doc's surname: ");
 }
 else
 {
     new_kids_base[n].hosp_identify = 0;
     new_kids_base[n].hospital.nonhosp.illness = check_string("Enter an illness: ");
     new_kids_base[n].hospital.nonhosp.doc = check_string("Enter a doc's surname: ");
 };
 identifier = 0;
 if(n<20 -1)
 {
     printf("Want to continue?");
     identifier = atoi_change();
     if (!identifier) { break; }
 }
    n++;
    } while (n<20);
    return n;
}

canonical_original_code:
int init_array(struct kindergarden *new_kids_base)
{
    int n = 0; int identifier;
    do
    {
 new_kids_base[n].surname = check_string("Enter a child's surname: ");
 new_kids_base[n].name = check_string("Enter a child's name: ");
 printf("Was a child hospitalized?");
 identifier = atoi_change();
 if(identifier)
 {
     new_kids_base[n].hosp_identify = 1;
     new_kids_base[n].hospital.hosp.illness = check_string("Enter an illness: ");
     new_kids_base[n].hospital.hosp.hospnum = check_string("Enter the number of hospital: ");
     new_kids_base[n].hospital.hosp.hospplace = check_string("Enter the situation of hospital: ");
     new_kids_base[n].hospital.hosp.hospdoc = check_string("Enter a hospital doc's surname: ");
 }
 else
 {
     new_kids_base[n].hosp_identify = 0;
     new_kids_base[n].hospital.nonhosp.illness = check_string("Enter an illness: ");
     new_kids_base[n].hospital.nonhosp.doc = check_string("Enter a doc's surname: ");
 };
 identifier = 0;
 if(n<20 -1)
 {
     printf("Want to continue?");
     identifier = atoi_change();
     if (!identifier) { break; }
 }
    n++;
    } while (n<20);
    return n;
}

variable_types: n int

user_defined_types: struct kindergarden { char *surname; int hosp_identify; union { struct { char *illness; } nonhosp; struct { char *illness; } hosp; } hospital; }


original_code:
void sort_array(struct kindergarden* kids_base, int array_size)
{
    struct kindergarden temp;
    int i, j;
    for(i=0; i<array_size-1; i++)
 for(j=i+1; j<array_size; j++)
     if(strcmp(kids_base[i].surname, kids_base[j].surname)>0)
     {
  temp = kids_base[i];
  kids_base[i] = kids_base[j];
  kids_base[j] = temp;
     }
}

canonical_original_code:
void sort_array(struct kindergarden *kids_base, int array_size)
{
    struct kindergarden temp;
    int i; int j;
    for(i=0; i<array_size-1; i++)
 for(j=i+1; j<array_size; j++)
     if(strcmp(kids_base[i].surname, kids_base[j].surname)>0)
     {
  temp = kids_base[i];
  kids_base[i] = kids_base[j];
  kids_base[j] = temp;
     }
}

variable_types: struct kindergarden { char *surname; int hosp_identify; union { struct { char *illness; } nonhosp; struct { char *illness; } hosp; } hospital; }

variable_types: i int

variable_types: j int


decompiled_code:
int __fastcall search_illness(__int64 a1, int a2, __int64 a3)
{
int result; // eax
int v6; // ebp
const char *v7; // rcx
const char *v8; // rdx
const char *v9; // r9
const char *v10; // r8

result = printf("This is the list of kids illed with %s:\n", *(const char **)(a3 + 8));
if ( a2 >= 0 )
{
v6 = 0;
while ( 1 )
{
result = strcmp(*(const char **)(a1 + 24), *(const char **)(a3 + 8));
if ( !*(_DWORD *)(a1 + 16) )
break;
if ( result )
{
LABEL_5:
++v6;
a1 += 56LL;
if ( a2 < v6 )
return result;
}
else
{
v7 = *(const char **)(a1 + 32);
v8 = *(const char **)(a1 + 8);
++v6;
v9 = *(const char **)(a1 + 48);
v10 = *(const char **)(a1 + 40);
a1 += 56LL;
result = printf(
"The child %s %s was hospitalized in Hospital %s in %s, where was searched by Doc.%s\n",
*(const char **)(a1 - 56),
v8,
v7,
v10,
v9);
if ( a2 < v6 )
return result;
}
}
if ( !result )
result = printf(
"The child %s %s was searched by Doc.%s\n",
*(const char **)a1,
*(const char **)(a1 + 8),
*(const char **)(a1 + 32));
goto LABEL_5;
}
return result;
}

canonical_decompiled_code:
int __fastcall func11(__int64 a1, int a2, __int64 a3)
{
int result; // eax
int v6; // ebp
const char *v7; // rcx
const char *v8; // rdx
const char *v9; // r9
const char *v10; // r8

result = printf("This is the list of kids illed with %s:\n", *(const char **)(a3 + 8));
if ( a2 >= 0 )
{
v6 = 0;
while ( 1 )
{
result = strcmp(*(const char **)(a1 + 24), *(const char **)(a3 + 8));
if ( !*(_DWORD *)(a1 + 16) )
break;
if ( result )
{
LABEL_5:
++v6;
a1 += 56LL;
if ( a2 < v6 )
return result;
}
else
{
v7 = *(const char **)(a1 + 32);
v8 = *(const char **)(a1 + 8);
++v6;
v9 = *(const char **)(a1 + 48);
v10 = *(const char **)(a1 + 40);
a1 += 56LL;
result = printf(
"The child %s %s was hospitalized in Hospital %s in %s, where was searched by Doc.%s\n",
*(const char **)(a1 - 56),
v8,
v7,
v10,
v9);
if ( a2 < v6 )
return result;
}
}
if ( !result )
result = printf(
"The child %s %s was searched by Doc.%s\n",
*(const char **)a1,
*(const char **)(a1 + 8),
*(const char **)(a1 + 32));
goto LABEL_5;
}
return result;
}

original_code:
void search_illness(struct kindergarden* kids_base, int array_size, char **argv)
{
    int i;
    printf("This is the list of kids illed with %s:\n", argv[1]);
    for(i=0; i<=array_size; i++)
    {
 if (kids_base[i].hosp_identify == 0)
 {
     if(!strcmp(kids_base[i].hospital.nonhosp.illness, argv[1]))
     {
  printf("The child %s %s was searched by Doc.%s\n", kids_base[i].surname, kids_base[i].name, kids_base[i].hospital.nonhosp.doc);
     }
 }
 else if(!strcmp(kids_base[i].hospital.hosp.illness, argv[1]))
 {
     printf("The child %s %s was hospitalized  in Hospital %s in %s, where was searched by Doc.%s\n", kids_base[i].surname, kids_base[i].name, kids_base[i].hospital.hosp.hospnum, kids_base[i].hospital.hosp.hospplace, kids_base[i].hospital.hosp.hospdoc);
 }
    }
}

canonical_original_code:
void search_illness(struct kindergarden *kids_base, int array_size, char **argv)
{
    int i;
    printf("This is the list of kids illed with %s:\n", argv[1]);
    for(i=0; i<=array_size; i++)
    {
 if (kids_base[i].hosp_identify == 0)
 {
     if(!strcmp(kids_base[i].hospital.nonhosp.illness, argv[1]))
     {
  printf("The child %s %s was searched by Doc.%s\n", kids_base[i].surname, kids_base[i].name, kids_base[i].hospital.nonhosp.doc);
     }
 }
 else if(!strcmp(kids_base[i].hospital.hosp.illness, argv[1]))
 {
     printf("The child %s %s was hospitalized  in Hospital %s in %s, where was searched by Doc.%s\n", kids_base[i].surname, kids_base[i].name, kids_base[i].hospital.hosp.hospnum, kids_base[i].hospital.hosp.hospplace, kids_base[i].hospital.hosp.hospdoc);
 }
    }
}


decompiled_code:
void __fastcall check_help(int a1, __int64 a2)
{
_BYTE *v2; // rax

if ( a1 > 1 )
{
v2 = *(_BYTE **)(a2 + 8);
if ( *v2 == 45 && v2[1] == 104 && !v2[2] )
{
puts(
"\n"
"=============================================\n"
"Manual\n"
"\n"
"1) send an interested illness by command line\n"
"2) A: Enter an information about children:\n"
"- surname\n"
"- name\n"
"B: Choose if he/she was hospitalized and then put the information about illness, hospital and doctor- illness\n"
"- doctor\n"
"- hospital number\n"
"- hospital situation\n"
"3) Output data about interested illness\n"
"\n"
"=================================================\n");
exit(0);
}
}
}

canonical_decompiled_code:
void __fastcall func12(int a1, __int64 a2)
{
_BYTE *v2; // rax

if ( a1 > 1 )
{
v2 = *(_BYTE **)(a2 + 8);
if ( *v2 == 45 && v2[1] == 104 && !v2[2] )
{
puts(
"\n"
"=============================================\n"
"Manual\n"
"\n"
"1) send an interested illness by command line\n"
"2) A: Enter an information about children:\n"
"- surname\n"
"- name\n"
"B: Choose if he/she was hospitalized and then put the information about illness, hospital and doctor- illness\n"
"- doctor\n"
"- hospital number\n"
"- hospital situation\n"
"3) Output data about interested illness\n"
"\n"
"=================================================\n");
exit(0);
}
}
}

original_code:
void check_help(int argc, char **argv)
{
    if(argc > 1 && !strcmp(argv[1], "-h"))
    {
 puts("\n=============================================\n"
 "Manual\n\n"
 "1) send an interested illness by command line\n"
 "2) A: Enter an information about children:\n"
 "- surname\n"
 "- name\n"
 "B: Choose if he/she was hospitalized and then put the information about illness, hospital and doctor"
 "- illness\n"
 "- doctor\n"
 "- hospital number\n"
 "- hospital situation\n"
 "3) Output data about interested illness\n\n"
 "=================================================\n");
 exit(0);
    }
}


InitSqList

repo: buaazp/datastruct

decompiled_code:
__int64 __fastcall InitSqList(_QWORD *a1)
{
void *v1; // rax
__int64 result; // rax

v1 = malloc(0x190uLL);
*a1 = v1;
if ( !v1 )
exit(-1);
result = 0x6400000000LL;
a1[1] = 0x6400000000LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_QWORD *a1)
{
void *v1; // rax
__int64 result; // rax

v1 = malloc(0x190uLL);
*a1 = v1;
if ( !v1 )
exit(-1);
result = 0x6400000000LL;
a1[1] = 0x6400000000LL;
return result;
}

original_code:
void InitSqList(SqList *L)
{
    L->elem = (ElemType *)malloc(100*sizeof(ElemType));
    if(!L->elem)
        exit(-1);
    L->length = 0;
    L->listsize = 100;
}

canonical_original_code:
void InitSqList(struct SqList *L)
{
    L->elem = (int *)malloc(100*sizeof(ElemType));
    if(!L->elem)
        exit(-1);
    L->length = 0;
    L->listsize = 100;
}

variable_types: struct SqList { int *elem; int length; int listsize; } *


CreateSqList

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

if ( a2 > 0 )
{
v2 = 0LL;
do
{
v2 += 4LL;
result = __isoc99_scanf();
++*(_DWORD *)(a1 + 8);
}
while ( v2 != 4LL * a2 );
}
return result;
}

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

if ( a2 > 0 )
{
v2 = 0LL;
do
{
v2 += 4LL;
result = __isoc99_scanf();
++*(_DWORD *)(a1 + 8);
}
while ( v2 != 4LL * a2 );
}
return result;
}

original_code:
void CreateSqList(SqList *L, int n)
{
    int i;
    for(i = 0;i<n;i++)
    {
        scanf("%d", &L->elem[i]);
        L->length++;
    }
}

canonical_original_code:
void CreateSqList(struct SqList *L, int n)
{
    int i;
    for(i = 0;i<n;i++)
    {
        scanf("%d", &L->elem[i]);
        L->length++;
    }
}

user_defined_types: struct SqList { int *elem; int length; int listsize; }


PrintSqList

decompiled_code:
int __fastcall PrintSqList(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // rsi

if ( *(int *)(a1 + 8) > 0 )
{
v1 = 0LL;
do
{
v2 = *(unsigned int *)(*(_QWORD *)a1 + 4 * v1++);
printf("%d ", v2);
}
while ( *(_DWORD *)(a1 + 8) > (int)v1 );
}
return putchar(10);
}

original_code:
void PrintSqList(SqList *L)
{
    int i;
    for(i=0; i<L->length; i++)
        printf("%d ", L->elem[i]);
    printf("\n");
}

canonical_original_code:
void PrintSqList(struct SqList *L)
{
    int i;
    for(i=0; i<L->length; i++)
        printf("%d ", L->elem[i]);
    printf("\n");
}


DestroySqList

decompiled_code:
void __fastcall DestroySqList(__int64 a1)
{
void *v2; // rdi

v2 = *(void **)a1;
if ( v2 )
{
free(v2);
*(_QWORD *)a1 = 0LL;
}
*(_DWORD *)(a1 + 8) = 0;
}

original_code:
void DestroySqList(SqList *L)
{
    if(L->elem != ((void *)0))
    {
        free(L->elem);
        L->elem = ((void *)0);
    }
    L->length = 0;
}

canonical_original_code:
void DestroySqList(struct SqList *L)
{
    if(L->elem != ((void *)0))
    {
        free(L->elem);
        L->elem = ((void *)0);
    }
    L->length = 0;
}


SqListInsert

original_code:
int SqListInsert(SqList *L, int i, ElemType x)
{
    int j;
    ElemType *p;
    if(i<1||i>L->length+1)
        return 0;
    if(L->length >= L->listsize)
    {
        p = (ElemType *)realloc(L->elem, (L->listsize + 10)*sizeof(ElemType));
        if(!p)
            exit(-1);
        L->elem = p;
        L->listsize = L->listsize + 10;
    }
    for(j = L->length; j >= i; j--)
        L->elem[j] = L->elem[j-1];
    L->elem[j] = x;
    ++L->length;
    return 1;
}

canonical_original_code:
int SqListInsert(struct SqList *L, int i, int x)
{
    int j;
    int *p;
    if(i<1||i>L->length+1)
        return 0;
    if(L->length >= L->listsize)
    {
        p = (int *)realloc(L->elem, (L->listsize + 10)*sizeof(ElemType));
        if(!p)
            exit(-1);
        L->elem = p;
        L->listsize = L->listsize + 10;
    }
    for(j = L->length; j >= i; j--)
        L->elem[j] = L->elem[j-1];
    L->elem[j] = x;
    ++L->length;
    return 1;
}

variable_types: x int

variable_types:
p
int *


SqListDelete

original_code:
int SqListDelete(SqList *L, int i)
{
    int j;
    if(i<1 || i>L->length)
        return 0;
    for(j=i-1; j<L->length-1; j++)
        L->elem[j] = L->elem[j+1];
    L->length--;
    return 1;
}

canonical_original_code:
int SqListDelete(struct SqList *L, int i)
{
    int j;
    if(i<1 || i>L->length)
        return 0;
    for(j=i-1; j<L->length-1; j++)
        L->elem[j] = L->elem[j+1];
    L->length--;
    return 1;
}


LocateElem

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

v2 = *(_DWORD *)(a1 + 8);
if ( v2 <= 0 )
return 0LL;
result = 1LL;
while ( *(_DWORD *)(*(_QWORD *)a1 + 4 * result - 4) != a2 )
{
if ( v2 < (int)++result )
return 0LL;
}
return (unsigned int)result;
}

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

v2 = *(_DWORD *)(a1 + 8);
if ( v2 <= 0 )
return 0LL;
result = 1LL;
while ( *(_DWORD *)(*(_QWORD *)a1 + 4 * result - 4) != a2 )
{
if ( v2 < (int)++result )
return 0LL;
}
return (unsigned int)result;
}

original_code:
int LocateElem(SqList *L, ElemType x)
{
    int i=1;
    while(i<=L->length&&L->elem[i-1]!=x){
        i++;
    }
    if(i<=L->length)
        return i;
    else
        return 0;
}

canonical_original_code:
int LocateElem(struct SqList *L, int x)
{
    int i = 1;
    while(i<=L->length&&L->elem[i-1]!=x){
        i++;
    }
    if(i<=L->length)
        return i;
    else
        return 0;
}


ReserveSqList

original_code:
int ReserveSqList(SqList *L)
{
    int i, length = L->length;
    ElemType temp;
    for(i=0; i<=length/2; i++)
    {
        temp = L->elem[length-i-1];
        L->elem[length-i-1] = L->elem[i];
        L->elem[i] = temp;
    }
    return 1;
}

canonical_original_code:
int ReserveSqList(struct SqList *L)
{
    int i; int length = L->length;
    int temp;
    for(i=0; i<=length/2; i++)
    {
        temp = L->elem[length-i-1];
        L->elem[length-i-1] = L->elem[i];
        L->elem[i] = temp;
    }
    return 1;
}

variable_types: length int

variable_types: temp int


repo: cantora/ncte

decompiled_code:
char *__fastcall opt_init(__int64 a1)
{
char *result; // rax

*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
result = getenv("SHELL");
*(_DWORD *)(a1 + 24) = 1;
*(_QWORD *)(a1 + 32) = &default_argv;
if ( result )
{
default_argv = result;
qword_508 = 0LL;
}
return result;
}

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

*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
result = getenv("SHELL");
*(_DWORD *)(a1 + 24) = 1;
*(_QWORD *)(a1 + 32) = &default_argv;
if ( result )
{
default_argv = result;
qword_508 = 0LL;
}
return result;
}

original_code:
void opt_init(struct ncte_conf *conf) {
 const char *shell;
 conf->term = ncte_options[0].default_val;
 conf->ncterm = ncte_options[1].default_val;
 conf->debug_file = ncte_options[2].default_val;
 shell = getenv("SHELL");
 if(shell != ((void *)0)) {
  default_argv[0] = shell;
  default_argv[1] = ((void *)0);
  conf->cmd_argc = 1;
 }
 else {
  conf->cmd_argc = default_argc;
 }
 conf->cmd_argv = default_argv;
}

canonical_original_code:
void opt_init(struct ncte_conf *conf) {
 char *shell;
 conf->term = ncte_options[0].default_val;
 conf->ncterm = ncte_options[1].default_val;
 conf->debug_file = ncte_options[2].default_val;
 shell = getenv("SHELL");
 if(shell != ((void *)0)) {
  default_argv[0] = shell;
  default_argv[1] = ((void *)0);
  conf->cmd_argc = 1;
 }
 else {
  conf->cmd_argc = default_argc;
 }
 conf->cmd_argv = default_argv;
}

variable_types: struct ncte_conf { char *ncterm; char *term; char *debug_file; int cmd_argc; char **cmd_argv; } *

variable_types:
shell
char *


decompiled_code:
int __fastcall opt_print_usage(int a1, const char **a2)
{
const char *v2; // rdx

v2 = (const char *)&unk_572;
if ( a1 > 0 )
v2 = *a2;
return fprintf(stdout, "usage: %s [OPTIONS] [CMD [ARG1, ...]]\n", v2);
}

canonical_decompiled_code:
int __fastcall func2(int a1, const char **a2)
{
const char *v2; // rdx

v2 = (const char *)&unk_572;
if ( a1 > 0 )
v2 = *a2;
return fprintf(stdout, "usage: %s [OPTIONS] [CMD [ARG1, ...]]\n", v2);
}

original_code:
void opt_print_usage(int argc, const char *const argv[]) {
 fprintf(stdout, "usage: %s [OPTIONS] [CMD [ARG1, ...]]\n", (argc > 0)? argv[0] : "");
}

canonical_original_code:
void opt_print_usage(int argc, char *argv[]) {
 fprintf(stdout, "usage: %s [OPTIONS] [CMD [ARG1, ...]]\n", (argc > 0)? argv[0] : "");
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
int __fastcall opt_print_help(int a1, const char **a2)
{
const char *v2; // r12
unsigned __int64 v3; // rbp
const char *v4; // rcx
int v5; // eax
int v6; // ebx
__int64 v7; // rcx
const char *v8; // rcx
__int64 i; // rbx
const char *v10; // rdx
char v12[88]; // [rsp+0h] [rbp-58h] BYREF

v2 = "sets the TERM environment variable to TERMNAME";
v3 = 0LL;
opt_print_usage(a1, a2);
fprintf(stdout, "\n%-26s%s\n", " CMD", "run CMD with arguments instead of the default");
fprintf(stdout, "%-26s\tdefault: the value of SHELL in the environment\n", (const char *)&unk_572);
fprintf(stdout, "%-26s\tor if SHELL undefined: ", (const char *)&unk_572);
fprintf(stdout, "%s ", default_argv);
fwrite("\nOPTIONS:\n", 1uLL, 0xAuLL, stdout);
v4 = "term";
while ( 1 )
{
v5 = snprintf(v12, 0x20uLL, " --%s", v4);
v6 = v5;
if ( v5 <= 31 )
{
v7 = (unsigned int)dword_848[v3 / 4];
if ( (unsigned int)v7 > 0xFF || (v6 = v5 + 3, snprintf(&v12[v5], 32 - v5, "|-%c", v7), v6 <= 31) )
{
if ( dword_838[v3 / 4] )
{
v8 = off_808[v3 / 8];
if ( v8 )
snprintf(&v12[v6], 32 - v6, "%s", v8);
}
}
}
for ( i = 0LL; v2; v2 = (&off_810[i])[v3 / 8] )
{
v10 = (const char *)&unk_572;
if ( !i )
v10 = v12;
++i;
fprintf(stdout, "%-26s%s\n", v10, v2);
}
v3 += 80LL;
if ( v3 == 320 )
break;
v4 = ncte_options[v3 / 8];
v2 = off_810[v3 / 8];
}
return fputc(10, stdout);
}

canonical_decompiled_code:
int __fastcall func3(int a1, const char **a2)
{
const char *v2; // r12
unsigned __int64 v3; // rbp
const char *v4; // rcx
int v5; // eax
int v6; // ebx
__int64 v7; // rcx
const char *v8; // rcx
__int64 i; // rbx
const char *v10; // rdx
char v12[88]; // [rsp+0h] [rbp-58h] BYREF

v2 = "sets the TERM environment variable to TERMNAME";
v3 = 0LL;
func2(a1, a2);
fprintf(stdout, "\n%-26s%s\n", " CMD", "run CMD with arguments instead of the default");
fprintf(stdout, "%-26s\tdefault: the value of SHELL in the environment\n", (const char *)&unk_572);
fprintf(stdout, "%-26s\tor if SHELL undefined: ", (const char *)&unk_572);
fprintf(stdout, "%s ", default_argv);
fwrite("\nOPTIONS:\n", 1uLL, 0xAuLL, stdout);
v4 = "term";
while ( 1 )
{
v5 = snprintf(v12, 0x20uLL, " --%s", v4);
v6 = v5;
if ( v5 <= 31 )
{
v7 = (unsigned int)dword_848[v3 / 4];
if ( (unsigned int)v7 > 0xFF || (v6 = v5 + 3, snprintf(&v12[v5], 32 - v5, "|-%c", v7), v6 <= 31) )
{
if ( dword_838[v3 / 4] )
{
v8 = off_808[v3 / 8];
if ( v8 )
snprintf(&v12[v6], 32 - v6, "%s", v8);
}
}
}
for ( i = 0LL; v2; v2 = (&off_810[i])[v3 / 8] )
{
v10 = (const char *)&unk_572;
if ( !i )
v10 = v12;
++i;
fprintf(stdout, "%-26s%s\n", v10, v2);
}
v3 += 80LL;
if ( v3 == 320 )
break;
v4 = ncte_options[v3 / 8];
v2 = off_810[v3 / 8];
}
return fputc(10, stdout);
}

original_code:
void opt_print_help(int argc, const char *const argv[]) {
 int i, k, f1_amt;
 char f1[32];
 opt_print_usage(argc, argv);
 fprintf(stdout, "\n%-" "26" "s%s\n", "  CMD", "run CMD with arguments instead of the default");
 fprintf(stdout, "%-" "26" "s\tdefault: the value of SHELL in the environment\n", "");
 fprintf(stdout, "%-" "26" "s\tor if SHELL undefined: ", "");
 for(k = 0; k < default_argc; k++)
  fprintf(stdout, "%s ", default_argv[k]);
 fprintf(stdout, "\nOPTIONS:\n");
 for(i = 0; i < 4; i++) {
  f1_amt = 0;
  f1_amt += snprintf(f1, 32, "  --%s", ncte_options[i].name);
  if(f1_amt < 32 && ncte_options[i].lopt.val >= 0 && ncte_options[i].lopt.val < 256)
   f1_amt += snprintf(f1+f1_amt, 32 -f1_amt, "|-%c", ncte_options[i].lopt.val);
  if(f1_amt < 32 && ncte_options[i].lopt.has_arg && ncte_options[i].usage != ((void *)0))
   snprintf(f1+f1_amt, 32 -f1_amt, "%s", ncte_options[i].usage);
  for(k = 0; ncte_options[i].desc[k] != ((void *)0); k++)
   fprintf(stdout, "%-" "26" "s%s\n", (k == 0? f1 : ""), ncte_options[i].desc[k]);
 }
 fputc('\n', stdout);
}

canonical_original_code:
void opt_print_help(int argc, char *argv[]) {
 int i; int k; int f1_amt;
 char f1[32];
 opt_print_usage(argc, argv);
 fprintf(stdout, "\n%-" "26" "s%s\n", "  CMD", "run CMD with arguments instead of the default");
 fprintf(stdout, "%-" "26" "s\tdefault: the value of SHELL in the environment\n", "");
 fprintf(stdout, "%-" "26" "s\tor if SHELL undefined: ", "");
 for(k = 0; k < default_argc; k++)
  fprintf(stdout, "%s ", default_argv[k]);
 fprintf(stdout, "\nOPTIONS:\n");
 for(i = 0; i < 4; i++) {
  f1_amt = 0;
  f1_amt += snprintf(f1, 32, "  --%s", ncte_options[i].name);
  if(f1_amt < 32 && ncte_options[i].lopt.val >= 0 && ncte_options[i].lopt.val < 256)
   f1_amt += snprintf(f1+f1_amt, 32 -f1_amt, "|-%c", ncte_options[i].lopt.val);
  if(f1_amt < 32 && ncte_options[i].lopt.has_arg && ncte_options[i].usage != ((void *)0))
   snprintf(f1+f1_amt, 32 -f1_amt, "%s", ncte_options[i].usage);
  for(k = 0; ncte_options[i].desc[k] != ((void *)0); k++)
   fprintf(stdout, "%-" "26" "s%s\n", (k == 0? f1 : ""), ncte_options[i].desc[k]);
 }
 fputc('\n', stdout);
}

variable_types: k int


decompiled_code:
__int64 __fastcall opt_parse(int argc, char **argv, __int64 a3)
{
int v4; // eax
const char *v5; // rcx
__int64 result; // rax
__int64 v7; // rdx
int longind; // [rsp+Ch] [rbp-FCh] BYREF
char shortopts[64]; // [rsp+10h] [rbp-F8h] BYREF
struct option longopts; // [rsp+50h] [rbp-B8h] BYREF

opterr = 0;
init_long_options((__m128i *)&longopts, shortopts);
longind = 0;
while ( 1 )
{
v4 = getopt_long(argc, argv, shortopts, &longopts, &longind);
if ( v4 == -1 )
break;
if ( optarg && *optarg == 45 )
{
v5 = argv[optind - 2];
LABEL_14:
snprintf(opt_err_msg, 0x40uLL, "option '%s' missing argument", v5);
*_errno_location() = 2;
return 0xFFFFFFFFLL;
}
if ( v4 == 104 )
{
*_errno_location() = 4;
return 0xFFFFFFFFLL;
}
if ( v4 > 104 )
{
if ( v4 == 2048 )
{
*(_QWORD *)(a3 + 8) = optarg;
}
else
{
if ( v4 != 2049 )
goto LABEL_23;
*(_QWORD *)a3 = optarg;
}
}
else
{
if ( v4 == 63 )
{
snprintf(opt_err_msg, 0x40uLL, "unknown option '%s'", argv[optind - 1]);
*_errno_location() = 1;
return 0xFFFFFFFFLL;
}
if ( v4 != 100 )
{
if ( v4 != 58 )
{
LABEL_23:
printf("getopt returned char '%c' (%d)\n", (unsigned int)v4, (unsigned int)v4);
_assert_fail("0", "src/opt.c", 0xE5u, "opt_parse");
}
v5 = argv[optind - 1];
goto LABEL_14;
}
*(_QWORD *)(a3 + 16) = optarg;
}
}
v7 = optind;
result = 0LL;
if ( optind < argc )
{
*(_DWORD *)(a3 + 24) = argc - optind;
*(_QWORD *)(a3 + 32) = &argv[v7];
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(int argc, char **argv, __int64 a3)
{
int v4; // eax
const char *v5; // rcx
__int64 result; // rax
__int64 v7; // rdx
int longind; // [rsp+Ch] [rbp-FCh] BYREF
char shortopts[64]; // [rsp+10h] [rbp-F8h] BYREF
struct option longopts; // [rsp+50h] [rbp-B8h] BYREF

opterr = 0;
func0((__m128i *)&longopts, shortopts);
longind = 0;
while ( 1 )
{
v4 = getopt_long(argc, argv, shortopts, &longopts, &longind);
if ( v4 == -1 )
break;
if ( optarg && *optarg == 45 )
{
v5 = argv[optind - 2];
LABEL_14:
snprintf(opt_err_msg, 0x40uLL, "option '%s' missing argument", v5);
*_errno_location() = 2;
return 0xFFFFFFFFLL;
}
if ( v4 == 104 )
{
*_errno_location() = 4;
return 0xFFFFFFFFLL;
}
if ( v4 > 104 )
{
if ( v4 == 2048 )
{
*(_QWORD *)(a3 + 8) = optarg;
}
else
{
if ( v4 != 2049 )
goto LABEL_23;
*(_QWORD *)a3 = optarg;
}
}
else
{
if ( v4 == 63 )
{
snprintf(opt_err_msg, 0x40uLL, "unknown option '%s'", argv[optind - 1]);
*_errno_location() = 1;
return 0xFFFFFFFFLL;
}
if ( v4 != 100 )
{
if ( v4 != 58 )
{
LABEL_23:
printf("getopt returned char '%c' (%d)\n", (unsigned int)v4, (unsigned int)v4);
_assert_fail("0", "src/opt.c", 0xE5u, "opt_parse");
}
v5 = argv[optind - 1];
goto LABEL_14;
}
*(_QWORD *)(a3 + 16) = optarg;
}
}
v7 = optind;
result = 0LL;
if ( optind < argc )
{
*(_DWORD *)(a3 + 24) = argc - optind;
*(_QWORD *)(a3 + 32) = &argv[v7];
}
return result;
}

original_code:
int opt_parse(int argc, char *const argv[], struct ncte_conf *conf) {
 int index, c;
 char optstring[64];
 struct option long_options[4 +1];
 opterr = 0;
 init_long_options(long_options, optstring);
 index = 0;
 while( (c = getopt_long(argc, argv, optstring, long_options, &index)) != -1 ) {
  if(optarg && optarg[0] == '-') {
   snprintf(opt_err_msg, 64, "option '%s' missing argument", argv[optind-2]);
   (*__errno_location ()) = OPT_ERR_MISSING_ARG;
   goto fail;
  }
  switch (c) {
  case 'd':
   conf->debug_file = optarg;
   break;
  case ((1 << 11) + 0):
   conf->term = optarg;
   break;
  case ((1 << 11) + 1):
   conf->ncterm = optarg;
   break;
  case 'h':
   (*__errno_location ()) = OPT_ERR_HELP;
   goto fail;
   break;
  case '?':
   snprintf(opt_err_msg, 64, "unknown option '%s'", argv[optind-1]);
   (*__errno_location ()) = OPT_ERR_UNKNOWN_OPTION;
   goto fail;
   break;
  case ':':
   snprintf(opt_err_msg, 64, "option '%s' missing argument", argv[optind-1]);
   (*__errno_location ()) = OPT_ERR_MISSING_ARG;
   goto fail;
   break;
  default:
   printf("getopt returned char '%c' (%d)\n", c, c);
   ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "src/opt.c", 229, __extension__ __PRETTY_FUNCTION__); }));
  }
 }
 if(optind < argc) {
  conf->cmd_argc = argc - optind;
  conf->cmd_argv = (const char *const *) &argv[optind];
 }
 return 0;
fail:
 return -1;
}

canonical_original_code:
int opt_parse(int argc, char *argv[], struct ncte_conf *conf) {
 int index; int c;
 char optstring[64];
 struct option long_options[-1];
 opterr = 0;
 init_long_options(long_options, optstring);
 index = 0;
 while( (c = getopt_long(argc, argv, optstring, long_options, &index)) != -1 ) {
  if(optarg && optarg[0] == '-') {
   snprintf(opt_err_msg, 64, "option '%s' missing argument", argv[optind-2]);
   (*__errno_location ()) = OPT_ERR_MISSING_ARG;
   goto fail;
  }
  switch (c) {
  case 'd':
   conf->debug_file = optarg;
   break;
  case ((1 << 11) + 0):
   conf->term = optarg;
   break;
  case ((1 << 11) + 1):
   conf->ncterm = optarg;
   break;
  case 'h':
   (*__errno_location ()) = OPT_ERR_HELP;
   goto fail;
   break;
  case '?':
   snprintf(opt_err_msg, 64, "unknown option '%s'", argv[optind-1]);
   (*__errno_location ()) = OPT_ERR_UNKNOWN_OPTION;
   goto fail;
   break;
  case ':':
   snprintf(opt_err_msg, 64, "option '%s' missing argument", argv[optind-1]);
   (*__errno_location ()) = OPT_ERR_MISSING_ARG;
   goto fail;
   break;
  default:
   printf("getopt returned char '%c' (%d)\n", c, c);
   ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "src/opt.c", 229, __extension__ __PRETTY_FUNCTION__); }));
  }
 }
 if(optind < argc) {
  conf->cmd_argc = argc - optind;
  conf->cmd_argv = (char **) &argv[optind];
 }
 return 0;
fail:
 return -1;
}

variable_types: index int

variable_types: c int

variable_types:
optstring
char [64]

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

user_defined_types: struct ncte_conf { char *ncterm; char *term; char *debug_file; int cmd_argc; char **cmd_argv; }


FuseChecksum

repo: daveho/GALasm

decompiled_code:
__int64 __fastcall FuseChecksum(int a1)
{
int v2; // edx
unsigned __int64 v3; // r11
int v4; // eax
unsigned __int64 v5; // r10
unsigned __int64 v6; // rdi
__int16 v7; // r9
int v8; // esi

v2 = 0;
v3 = (unsigned int)((_DWORD)Jedec + 5956);
v4 = 0;
v5 = (unsigned int)((_DWORD)Jedec + 5808);
v6 = (unsigned int)((_DWORD)Jedec - 1);
v7 = 0;
while ( 1 )
{
if ( a1 == 1 )
{
switch ( v4 )
{
case 2048:
v6 = (unsigned int)((_DWORD)Jedec + 5808);
break;
case 2056:
v6 = (unsigned int)((_DWORD)Jedec + 5818);
break;
case 2194:
return (unsigned __int16)(v7 + (unsigned __int8)v2);
default:
++v6;
break;
}
LABEL_15:
v8 = *(char *)v6;
goto LABEL_7;
}
if ( a1 != 2 )
break;
switch ( v4 )
{
case 2560:
v8 = Jedec[5808];
v6 = (unsigned int)((_DWORD)Jedec + 5808);
break;
case 2568:
v8 = Jedec[5818];
v6 = (unsigned int)((_DWORD)Jedec + 5818);
break;
case 2706:
return (unsigned __int16)(v7 + (unsigned __int8)v2);
default:
goto LABEL_19;
}
LABEL_7:
v2 |= v8 << (v4 & 7);
if ( (((_BYTE)v4 + 9) & 7) == 0 )
{
v7 += (unsigned __int8)v2;
v2 = 0;
}
LABEL_9:
++v4;
}
if ( a1 != 3 )
{
if ( a1 != 4 )
goto LABEL_15;
if ( v4 == 3200 )
{
v2 |= Jedec[5808];
v6 = (unsigned int)((_DWORD)Jedec + 5808);
goto LABEL_9;
}
if ( v4 == 3274 )
return (unsigned __int16)(v7 + (unsigned __int8)v2);
goto LABEL_19;
}
if ( (unsigned int)(v4 - 5808) <= 0x13 )
{
if ( (v4 & 1) != 0 )
{
v8 = *(char *)v3;
v6 = v3++;
}
else
{
v8 = *(char *)v5;
v6 = v5++;
}
goto LABEL_7;
}
if ( v4 == 5828 )
{
v6 = (unsigned int)((_DWORD)Jedec + 5817);
goto LABEL_19;
}
if ( v4 != 5892 )
{
LABEL_19:
v8 = *(char *)++v6;
goto LABEL_7;
}
return (unsigned __int16)(v7 + (unsigned __int8)v2);
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
int v2; // edx
unsigned __int64 v3; // r11
int v4; // eax
unsigned __int64 v5; // r10
unsigned __int64 v6; // rdi
__int16 v7; // r9
int v8; // esi

v2 = 0;
v3 = (unsigned int)((_DWORD)Jedec + 5956);
v4 = 0;
v5 = (unsigned int)((_DWORD)Jedec + 5808);
v6 = (unsigned int)((_DWORD)Jedec - 1);
v7 = 0;
while ( 1 )
{
if ( a1 == 1 )
{
switch ( v4 )
{
case 2048:
v6 = (unsigned int)((_DWORD)Jedec + 5808);
break;
case 2056:
v6 = (unsigned int)((_DWORD)Jedec + 5818);
break;
case 2194:
return (unsigned __int16)(v7 + (unsigned __int8)v2);
default:
++v6;
break;
}
LABEL_15:
v8 = *(char *)v6;
goto LABEL_7;
}
if ( a1 != 2 )
break;
switch ( v4 )
{
case 2560:
v8 = Jedec[5808];
v6 = (unsigned int)((_DWORD)Jedec + 5808);
break;
case 2568:
v8 = Jedec[5818];
v6 = (unsigned int)((_DWORD)Jedec + 5818);
break;
case 2706:
return (unsigned __int16)(v7 + (unsigned __int8)v2);
default:
goto LABEL_19;
}
LABEL_7:
v2 |= v8 << (v4 & 7);
if ( (((_BYTE)v4 + 9) & 7) == 0 )
{
v7 += (unsigned __int8)v2;
v2 = 0;
}
LABEL_9:
++v4;
}
if ( a1 != 3 )
{
if ( a1 != 4 )
goto LABEL_15;
if ( v4 == 3200 )
{
v2 |= Jedec[5808];
v6 = (unsigned int)((_DWORD)Jedec + 5808);
goto LABEL_9;
}
if ( v4 == 3274 )
return (unsigned __int16)(v7 + (unsigned __int8)v2);
goto LABEL_19;
}
if ( (unsigned int)(v4 - 5808) <= 0x13 )
{
if ( (v4 & 1) != 0 )
{
v8 = *(char *)v3;
v6 = v3++;
}
else
{
v8 = *(char *)v5;
v6 = v5++;
}
goto LABEL_7;
}
if ( v4 == 5828 )
{
v6 = (unsigned int)((_DWORD)Jedec + 5817);
goto LABEL_19;
}
if ( v4 != 5892 )
{
LABEL_19:
v8 = *(char *)++v6;
goto LABEL_7;
}
return (unsigned __int16)(v7 + (unsigned __int8)v2);
}

original_code:
unsigned FuseChecksum(int galtype)
{
    int n;
    unsigned checksum;
    UBYTE byte;
    BYTE *ptr, *ptrXOR, *ptrS1;
    ptr = &Jedec.GALLogic[0] - 1L;
    ptrXOR = &Jedec.GALXOR[0];
    ptrS1 = &Jedec.GALS1[0];
    n = 0;
    checksum = 0;
    byte = 0;
    for (;;)
    {
        if (galtype == 1)
        {
            if (n == 2048)
            {
                ptr = &Jedec.GALXOR[0];
            }
            else
            {
                if (n == 2048 +8)
                {
                    ptr = &Jedec.GALSig[0];
                }
                else
                {
                    if (n == 2194)
                        break;
                    else
                        ptr++;
                }
            }
        }
        if (galtype == 2)
        {
            if (n == 2560)
            {
                ptr = &Jedec.GALXOR[0];
            }
            else
            {
                if (n == 2560 +8)
                {
                    ptr = &Jedec.GALSig[0];
                }
                else
                {
                    if (n == 2706)
                        break;
                    else
                        ptr++;
                }
            }
        }
        if (galtype == 3)
        {
            if (n >= 5808 && n < 5808 + 20)
            {
                if (!(n % 2))
                    ptr = ptrXOR++;
                else
                    ptr = ptrS1++;
            }
            else
            {
                if (n == 5828)
                    ptr = &Jedec.GALSig[0] - 1L;
                if (n == 5828 + 64)
                    break;
                else
                    ptr++;
            }
        }
        if (galtype == 4)
        {
            if (n == 3200)
            {
                ptr = &Jedec.GALXOR[0];
            }
            else
            {
                if (n == 3210 + 64)
                    break;
                else
                    ptr++;
            }
        }
        byte |= (*ptr << (n+8) % 8);
        if (!((n + 9)%8))
        {
            checksum = (checksum + byte) % 0x10000;
            byte = 0;
        }
        n++;
    }
    checksum = (checksum + byte) % 0x10000;
    return(checksum);
}

canonical_original_code:
unsigned int FuseChecksum(int galtype)
{
    int n;
    unsigned int checksum;
    unsigned char byte;
    char *ptr; char *ptrXOR; char *ptrS1;
    ptr = &Jedec.GALLogic[0] - 1L;
    ptrXOR = &Jedec.GALXOR[0];
    ptrS1 = &Jedec.GALS1[0];
    n = 0;
    checksum = 0;
    byte = 0;
    for (;;)
    {
        if (galtype == 1)
        {
            if (n == 2048)
            {
                ptr = &Jedec.GALXOR[0];
            }
            else
            {
                if (n == 2048 +8)
                {
                    ptr = &Jedec.GALSig[0];
                }
                else
                {
                    if (n == 2194)
                        break;
                    else
                        ptr++;
                }
            }
        }
        if (galtype == 2)
        {
            if (n == 2560)
            {
                ptr = &Jedec.GALXOR[0];
            }
            else
            {
                if (n == 2560 +8)
                {
                    ptr = &Jedec.GALSig[0];
                }
                else
                {
                    if (n == 2706)
                        break;
                    else
                        ptr++;
                }
            }
        }
        if (galtype == 3)
        {
            if (n >= 5808 && n < 5808 + 20)
            {
                if (!(n % 2))
                    ptr = ptrXOR++;
                else
                    ptr = ptrS1++;
            }
            else
            {
                if (n == 5828)
                    ptr = &Jedec.GALSig[0] - 1L;
                if (n == 5828 + 64)
                    break;
                else
                    ptr++;
            }
        }
        if (galtype == 4)
        {
            if (n == 3200)
            {
                ptr = &Jedec.GALXOR[0];
            }
            else
            {
                if (n == 3210 + 64)
                    break;
                else
                    ptr++;
            }
        }
        byte |= (*ptr << (n+8) % 8);
        if (!((n + 9)%8))
        {
            checksum = (checksum + byte) % 0x10000;
            byte = 0;
        }
        n++;
    }
    checksum = (checksum + byte) % 0x10000;
    return(checksum);
}

variable_types: galtype int

variable_types: n int

variable_types:
checksum
unsigned int

variable_types:
byte
unsigned char

variable_types:
ptr
char *

variable_types:
ptrXOR
char *

return_type: unsigned int


MakeJedecBuff

decompiled_code:
__int64 __fastcall MakeJedecBuff(int a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char a7)
{
__int64 v7; // rax
int v8; // ebx
int v9; // ebp
int v10; // er13
unsigned int v11; // er15
__int64 v12; // rax
unsigned int v13; // er12
int v14; // eax
unsigned int v15; // er14
unsigned int v16; // er15
__int64 v17; // rbp
char *v18; // rbp
unsigned int v19; // er13
__int64 result; // rax
__int64 v21; // r14
__int64 v22; // rbx
unsigned int v23; // eax
__int64 v24; // rbp
unsigned int v25; // er13
unsigned int v26; // ebx
unsigned __int64 v27; // r13
int v28; // [rsp+8h] [rbp-70h]
__int64 v29; // [rsp+20h] [rbp-58h]
int v30; // [rsp+28h] [rbp-50h]
unsigned int v31; // [rsp+2Ch] [rbp-4Ch]
char v32[72]; // [rsp+30h] [rbp-48h] BYREF

v7 = (unsigned int)(a1 - 1);
v31 = v7;
if ( (unsigned int)v7 > 3 )
{
v30 = 0;
v8 = 0;
v28 = 0;
}
else
{
v8 = CSWTCH_13[v7];
v28 = CSWTCH_14[v7];
v30 = CSWTCH_15[v7];
}
if ( !*(_WORD *)(a2 + 8) && (unsigned int)AddString(&a7, &unk_C7B)
|| (unsigned int)AddString(&a7, "Used Program: GALasm 2.1\n")
|| (unsigned int)AddString(&a7, "GAL-Assembler: GALasm 2.1\n") )
{
return 0xFFFFFFFFLL;
}
switch ( a1 )
{
case 1:
if ( (unsigned int)AddString(&a7, "Device: GAL16V8\n\n") )
return 0xFFFFFFFFLL;
break;
case 2:
if ( (unsigned int)AddString(&a7, "Device: GAL20V8\n\n") )
return 0xFFFFFFFFLL;
break;
case 4:
if ( (unsigned int)AddString(&a7, "Device: GAL20RA10\n\n") )
return 0xFFFFFFFFLL;
break;
default:
if ( a1 == 3 && (unsigned int)AddString(&a7, "Device: GAL22V10\n\n") )
return 0xFFFFFFFFLL;
break;
}
if ( !(unsigned int)AddString(&a7, "*F0\n") )
{
if ( *(_WORD *)(a2 + 6) )
{
if ( (unsigned int)AddString(&a7, "*G1\n") )
return 0xFFFFFFFFLL;
if ( a1 != 1 )
goto LABEL_16;
}
else
{
if ( (unsigned int)AddString(&a7, "*G0\n") )
return 0xFFFFFFFFLL;
if ( a1 != 1 )
{
LABEL_16:
if ( a1 == 2 )
{
if ( (unsigned int)AddString(&a7, "*QF2706\n") )
return 0xFFFFFFFFLL;
}
else if ( a1 == 4 )
{
if ( (unsigned int)AddString(&a7, "*QF3274\n") )
return 0xFFFFFFFFLL;
}
else if ( a1 == 3 && (unsigned int)AddString(&a7, "*QF5892\n") )
{
return 0xFFFFFFFFLL;
}
LABEL_20:
if ( v28 )
{
v9 = 0;
v10 = v8 + 1;
v29 = v8 + 2;
v11 = 0;
while ( 2 )
{
v12 = (int)v11;
do
{
v13 = v12;
if ( Jedec[v12] )
{
sprintf(v32, "*L%04d ", v11);
if ( !(unsigned int)AddString(&a7, v32) )
{
v21 = v29 + (int)v11;
v22 = (int)(v11 + 1);
while ( !(unsigned int)AddByte(&a7, (unsigned __int8)(Jedec[v22 - 1] + 48)) )
{
v11 = v22++;
if ( v22 == v21 )
{
if ( (unsigned int)AddByte(&a7, 10LL) )
return 0xFFFFFFFFLL;
v14 = 1;
goto LABEL_26;
}
}
}
return 0xFFFFFFFFLL;
}
v13 = ++v12;
}
while ( v12 != v10 + (__int64)(int)v11 );
v14 = 0;
v11 = v13;
LABEL_26:
if ( v28 > ++v9 )
continue;
break;
}
v15 = v11;
v16 = v13;
if ( !v14 )
goto LABEL_57;
}
else
{
v16 = 0;
LABEL_57:
v15 = v16;
}
sprintf(v32, "*L%04d ", v15);
if ( !(unsigned int)AddString(&a7, v32) )
{
if ( v30 )
{
v17 = 0LL;
while ( !(unsigned int)AddByte(&a7, (unsigned __int8)(Jedec[v17 + 5808] + 48)) )
{
if ( a1 == 3 )
{
if ( (unsigned int)AddByte(&a7, (unsigned __int8)(Jedec[v17 + 5956] + 48)) )
return 0xFFFFFFFFLL;
++v17;
v15 += 2;
if ( v17 == v30 )
goto LABEL_36;
}
else
{
++v17;
++v15;
if ( v17 == v30 )
goto LABEL_36;
}
}
}
else
{
LABEL_36:
if ( !(unsigned int)AddByte(&a7, 10LL) )
{
sprintf(v32, "*L%04d ", v15);
if ( !(unsigned int)AddString(&a7, v32) )
{
v18 = Jedec;
while ( !(unsigned int)AddByte(&a7, (unsigned __int8)(v18[5818] + 48)) )
{
v19 = v15 - (unsigned int)Jedec + 1 + (_DWORD)v18++;
if ( v18 == &Jedec[64] )
{
if ( (unsigned int)AddByte(&a7, 10LL) )
return 0xFFFFFFFFLL;
if ( v31 <= 1 )
{
sprintf(v32, "*L%04d ", v19);
if ( !(unsigned int)AddString(&a7, v32) )
{
v24 = 0LL;
v25 = v19 + 1;
while ( !(unsigned int)AddByte(&a7, (unsigned __int8)(Jedec[v24 + 5882] + 48)) )
{
v26 = v25 + v24++;
if ( v24 == 8 )
{
if ( !(unsigned int)AddByte(&a7, 10LL) )
{
sprintf(v32, "*L%04d ", v26);
if ( !(unsigned int)AddString(&a7, v32) )
{
v27 = (unsigned int)((_DWORD)Jedec + 5890);
while ( !(unsigned int)AddByte(&a7, (unsigned __int8)(*(_BYTE *)v27 + 48)) )
{
if ( (_DWORD)Jedec + 5954 == ++v27 )
{
if ( !(unsigned int)AddByte(&a7, 10LL) )
{
sprintf(v32, "*L%04d ", v26 + 1);
if ( !(unsigned int)AddString(&a7, v32)
&& !(unsigned int)AddByte(&a7, (unsigned __int8)(Jedec[5954] + 48))
&& !(unsigned int)AddByte(&a7, 10LL) )
{
sprintf(v32, "*L%04d ", v26 + 2);
if ( !(unsigned int)AddString(&a7, v32)
&& !(unsigned int)AddByte(&a7, (unsigned __int8)(Jedec[5955] + 48))
&& !(unsigned int)AddByte(&a7, 10LL) )
{
goto LABEL_68;
}
}
}
return 0xFFFFFFFFLL;
}
++v26;
}
}
}
return 0xFFFFFFFFLL;
}
}
}
}
else
{
LABEL_68:
v23 = FuseChecksum(a1);
sprintf(v32, "*C%04x\n", v23);
if ( !(unsigned int)AddString(&a7, v32) )
{
result = AddString(&a7, "*\n");
if ( !(_DWORD)result )
{
if ( !*(_WORD *)(a2 + 8) )
result = (unsigned int)-((unsigned int)AddByte(&a7, 3LL) != 0);
return result;
}
}
}
return 0xFFFFFFFFLL;
}
}
}
}
}
}
return 0xFFFFFFFFLL;
}
}
if ( (unsigned int)AddString(&a7, "*QF2194\n") )
return 0xFFFFFFFFLL;
goto LABEL_20;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char a7)
{
__int64 v7; // rax
int v8; // ebx
int v9; // ebp
int v10; // er13
unsigned int v11; // er15
__int64 v12; // rax
unsigned int v13; // er12
int v14; // eax
unsigned int v15; // er14
unsigned int v16; // er15
__int64 v17; // rbp
char *v18; // rbp
unsigned int v19; // er13
__int64 result; // rax
__int64 v21; // r14
__int64 v22; // rbx
unsigned int v23; // eax
__int64 v24; // rbp
unsigned int v25; // er13
unsigned int v26; // ebx
unsigned __int64 v27; // r13
int v28; // [rsp+8h] [rbp-70h]
__int64 v29; // [rsp+20h] [rbp-58h]
int v30; // [rsp+28h] [rbp-50h]
unsigned int v31; // [rsp+2Ch] [rbp-4Ch]
char v32[72]; // [rsp+30h] [rbp-48h] BYREF

v7 = (unsigned int)(a1 - 1);
v31 = v7;
if ( (unsigned int)v7 > 3 )
{
v30 = 0;
v8 = 0;
v28 = 0;
}
else
{
v8 = CSWTCH_13[v7];
v28 = CSWTCH_14[v7];
v30 = CSWTCH_15[v7];
}
if ( !*(_WORD *)(a2 + 8) && (unsigned int)AddString(&a7, &unk_C7B)
|| (unsigned int)AddString(&a7, "Used Program: GALasm 2.1\n")
|| (unsigned int)AddString(&a7, "GAL-Assembler: GALasm 2.1\n") )
{
return 0xFFFFFFFFLL;
}
switch ( a1 )
{
case 1:
if ( (unsigned int)AddString(&a7, "Device: GAL16V8\n\n") )
return 0xFFFFFFFFLL;
break;
case 2:
if ( (unsigned int)AddString(&a7, "Device: GAL20V8\n\n") )
return 0xFFFFFFFFLL;
break;
case 4:
if ( (unsigned int)AddString(&a7, "Device: GAL20RA10\n\n") )
return 0xFFFFFFFFLL;
break;
default:
if ( a1 == 3 && (unsigned int)AddString(&a7, "Device: GAL22V10\n\n") )
return 0xFFFFFFFFLL;
break;
}
if ( !(unsigned int)AddString(&a7, "*F0\n") )
{
if ( *(_WORD *)(a2 + 6) )
{
if ( (unsigned int)AddString(&a7, "*G1\n") )
return 0xFFFFFFFFLL;
if ( a1 != 1 )
goto LABEL_16;
}
else
{
if ( (unsigned int)AddString(&a7, "*G0\n") )
return 0xFFFFFFFFLL;
if ( a1 != 1 )
{
LABEL_16:
if ( a1 == 2 )
{
if ( (unsigned int)AddString(&a7, "*QF2706\n") )
return 0xFFFFFFFFLL;
}
else if ( a1 == 4 )
{
if ( (unsigned int)AddString(&a7, "*QF3274\n") )
return 0xFFFFFFFFLL;
}
else if ( a1 == 3 && (unsigned int)AddString(&a7, "*QF5892\n") )
{
return 0xFFFFFFFFLL;
}
LABEL_20:
if ( v28 )
{
v9 = 0;
v10 = v8 + 1;
v29 = v8 + 2;
v11 = 0;
while ( 2 )
{
v12 = (int)v11;
do
{
v13 = v12;
if ( Jedec[v12] )
{
sprintf(v32, "*L%04d ", v11);
if ( !(unsigned int)AddString(&a7, v32) )
{
v21 = v29 + (int)v11;
v22 = (int)(v11 + 1);
while ( !(unsigned int)AddByte(&a7, (unsigned __int8)(Jedec[v22 - 1] + 48)) )
{
v11 = v22++;
if ( v22 == v21 )
{
if ( (unsigned int)AddByte(&a7, 10LL) )
return 0xFFFFFFFFLL;
v14 = 1;
goto LABEL_26;
}
}
}
return 0xFFFFFFFFLL;
}
v13 = ++v12;
}
while ( v12 != v10 + (__int64)(int)v11 );
v14 = 0;
v11 = v13;
LABEL_26:
if ( v28 > ++v9 )
continue;
break;
}
v15 = v11;
v16 = v13;
if ( !v14 )
goto LABEL_57;
}
else
{
v16 = 0;
LABEL_57:
v15 = v16;
}
sprintf(v32, "*L%04d ", v15);
if ( !(unsigned int)AddString(&a7, v32) )
{
if ( v30 )
{
v17 = 0LL;
while ( !(unsigned int)AddByte(&a7, (unsigned __int8)(Jedec[v17 + 5808] + 48)) )
{
if ( a1 == 3 )
{
if ( (unsigned int)AddByte(&a7, (unsigned __int8)(Jedec[v17 + 5956] + 48)) )
return 0xFFFFFFFFLL;
++v17;
v15 += 2;
if ( v17 == v30 )
goto LABEL_36;
}
else
{
++v17;
++v15;
if ( v17 == v30 )
goto LABEL_36;
}
}
}
else
{
LABEL_36:
if ( !(unsigned int)AddByte(&a7, 10LL) )
{
sprintf(v32, "*L%04d ", v15);
if ( !(unsigned int)AddString(&a7, v32) )
{
v18 = Jedec;
while ( !(unsigned int)AddByte(&a7, (unsigned __int8)(v18[5818] + 48)) )
{
v19 = v15 - (unsigned int)Jedec + 1 + (_DWORD)v18++;
if ( v18 == &Jedec[64] )
{
if ( (unsigned int)AddByte(&a7, 10LL) )
return 0xFFFFFFFFLL;
if ( v31 <= 1 )
{
sprintf(v32, "*L%04d ", v19);
if ( !(unsigned int)AddString(&a7, v32) )
{
v24 = 0LL;
v25 = v19 + 1;
while ( !(unsigned int)AddByte(&a7, (unsigned __int8)(Jedec[v24 + 5882] + 48)) )
{
v26 = v25 + v24++;
if ( v24 == 8 )
{
if ( !(unsigned int)AddByte(&a7, 10LL) )
{
sprintf(v32, "*L%04d ", v26);
if ( !(unsigned int)AddString(&a7, v32) )
{
v27 = (unsigned int)((_DWORD)Jedec + 5890);
while ( !(unsigned int)AddByte(&a7, (unsigned __int8)(*(_BYTE *)v27 + 48)) )
{
if ( (_DWORD)Jedec + 5954 == ++v27 )
{
if ( !(unsigned int)AddByte(&a7, 10LL) )
{
sprintf(v32, "*L%04d ", v26 + 1);
if ( !(unsigned int)AddString(&a7, v32)
&& !(unsigned int)AddByte(&a7, (unsigned __int8)(Jedec[5954] + 48))
&& !(unsigned int)AddByte(&a7, 10LL) )
{
sprintf(v32, "*L%04d ", v26 + 2);
if ( !(unsigned int)AddString(&a7, v32)
&& !(unsigned int)AddByte(&a7, (unsigned __int8)(Jedec[5955] + 48))
&& !(unsigned int)AddByte(&a7, 10LL) )
{
goto LABEL_68;
}
}
}
return 0xFFFFFFFFLL;
}
++v26;
}
}
}
return 0xFFFFFFFFLL;
}
}
}
}
else
{
LABEL_68:
v23 = func3(a1);
sprintf(v32, "*C%04x\n", v23);
if ( !(unsigned int)AddString(&a7, v32) )
{
result = AddString(&a7, "*\n");
if ( !(_DWORD)result )
{
if ( !*(_WORD *)(a2 + 8) )
result = (unsigned int)-((unsigned int)AddByte(&a7, 3LL) != 0);
return result;
}
}
}
return 0xFFFFFFFFLL;
}
}
}
}
}
}
return 0xFFFFFFFFLL;
}
}
if ( (unsigned int)AddString(&a7, "*QF2194\n") )
return 0xFFFFFFFFLL;
goto LABEL_20;
}
return 0xFFFFFFFFLL;
}

original_code:
int MakeJedecBuff(struct ActBuffer buff, int galtype, struct Config *cfg)
{
    UBYTE mystrng[16];
    int n, m, bitnum, bitnum2, flag;
    int MaxFuseAdr = 0, RowSize = 0, XORSize = 0;
    switch (galtype)
    {
         case 1:
                 MaxFuseAdr = 31;
                 RowSize = 64;
                 XORSize = 8;
                 break;
         case 2:
                 MaxFuseAdr = 39;
                 RowSize = 64;
                 XORSize = 8;
                 break;
         case 3:
                 MaxFuseAdr = 43;
                 RowSize = 132;
                 XORSize = 10;
                 break;
         case 4:
                 MaxFuseAdr = 39;
                 RowSize = 80;
                 XORSize = 10;
                 break;
    }
    if (!cfg->JedecFuseChk)
        if (AddString(&buff, (UBYTE *)"\2\n"))
            return(-1);
    if (AddString(&buff, (UBYTE *)"Used Program:   GALasm 2.1\n"))
        return(-1);
    if (AddString(&buff, (UBYTE *)"GAL-Assembler:  GALasm 2.1\n"))
        return(-1);
    if (galtype == 1)
    {
        if (AddString(&buff, (UBYTE *)"Device:         GAL16V8\n\n"))
            return(-1);
    }
    if (galtype == 2)
    {
        if (AddString(&buff, (UBYTE *)"Device:         GAL20V8\n\n"))
            return(-1);
    }
    if (galtype == 4)
    {
        if (AddString(&buff, (UBYTE *)"Device:         GAL20RA10\n\n"))
            return(-1);
    }
    if (galtype == 3)
    {
        if (AddString(&buff, (UBYTE *)"Device:         GAL22V10\n\n"))
            return(-1);
    }
    if (AddString(&buff, (UBYTE *)"*F0\n"))
        return(-1);
    if (cfg->JedecSecBit)
    {
        if (AddString(&buff, (UBYTE *)"*G1\n"))
            return(-1);
    }
    else
        if (AddString(&buff, (UBYTE *)"*G0\n"))
            return(-1);
    if (galtype == 1)
        if (AddString(&buff, (UBYTE *)"*QF2194\n"))
            return(-1);
    if (galtype == 2)
        if (AddString(&buff, (UBYTE *)"*QF2706\n"))
            return(-1);
    if (galtype == 4)
        if (AddString(&buff, (UBYTE *)"*QF3274\n"))
            return(-1);
    if (galtype == 3)
        if (AddString(&buff, (UBYTE *)"*QF5892\n"))
            return(-1);
    bitnum = bitnum2 = flag = 0;
    for (m = 0; m < RowSize; m++)
    {
        flag = 0;
        bitnum2 = bitnum;
        for (n = 0; n <= MaxFuseAdr; n++)
        {
            if (Jedec.GALLogic[bitnum2])
            {
                flag = 1;
                break;
            }
            bitnum2++;
        }
        if (flag)
        {
            sprintf((char *)&mystrng[0], "*L%04d ", bitnum);
            if (AddString(&buff, (UBYTE *)&mystrng[0]))
                return(-1);
            for (n=0; n<=MaxFuseAdr; n++)
            {
                if (AddByte(&buff, (UBYTE)(Jedec.GALLogic[bitnum] + '0')))
                    return(-1);
                bitnum++;
            }
            if (AddByte(&buff, (UBYTE)'\n'))
                return(-1);
        }
        else
            bitnum = bitnum2;
    }
    if (!flag)
        bitnum = bitnum2;
    sprintf((char *)&mystrng[0], "*L%04d ", bitnum);
    if (AddString(&buff, (UBYTE *)&mystrng[0]))
        return(-1);
    for (n = 0; n < XORSize; n++)
    {
        if (AddByte(&buff, (UBYTE)(Jedec.GALXOR[n] + '0')))
            return(-1);
        bitnum++;
        if (galtype == 3)
        {
            if (AddByte(&buff, (UBYTE)(Jedec.GALS1[n] + '0')))
                return(-1);
            bitnum++;
        }
    }
    if (AddByte(&buff, (UBYTE)'\n'))
        return(-1);
    sprintf((char *)&mystrng[0], "*L%04d ", bitnum);
    if (AddString(&buff, (UBYTE *)&mystrng[0]))
        return(-1);
    for (n = 0; n < 64; n++)
    {
        if (AddByte(&buff, (UBYTE)(Jedec.GALSig[n] + '0')))
            return(-1);
        bitnum++;
    }
    if (AddByte(&buff, (UBYTE)'\n'))
        return(-1);
    if ((galtype == 1) || (galtype == 2))
    {
        sprintf((char *)&mystrng[0], "*L%04d ", bitnum);
        if (AddString(&buff, (UBYTE *)&mystrng[0]))
            return(-1);
        for (n = 0; n < 8; n++)
        {
            if (AddByte(&buff, (UBYTE)(Jedec.GALAC1[n] + '0')))
                return(-1);
            bitnum++;
        }
        if (AddByte(&buff, (UBYTE)'\n'))
            return(-1);
        sprintf((char *)&mystrng[0], "*L%04d ", bitnum);
        if (AddString(&buff, (UBYTE *)&mystrng[0]))
            return(-1);
        for (n = 0; n < 64; n++)
        {
            if (AddByte(&buff, (UBYTE)(Jedec.GALPT[n] + '0')))
                return(-1);
            bitnum++;
        }
        if (AddByte(&buff, (UBYTE)'\n'))
            return(-1);
        sprintf((char *)&mystrng[0], "*L%04d ", bitnum);
        if (AddString(&buff, (UBYTE *)&mystrng[0]))
            return(-1);
        if (AddByte(&buff, (UBYTE)(Jedec.GALSYN + '0')))
            return(-1);
        if (AddByte(&buff, (UBYTE)'\n'))
            return(-1);
        bitnum++;
        sprintf((char *)&mystrng[0], "*L%04d ", bitnum);
        if (AddString(&buff, (UBYTE *)&mystrng[0]))
            return(-1);
        if (AddByte(&buff, (UBYTE)(Jedec.GALAC0 + '0')))
            return(-1);
        if (AddByte(&buff, (UBYTE)'\n'))
            return(-1);
    }
        sprintf((char *)&mystrng[0], "*C%04x\n", FuseChecksum(galtype));
        if (AddString(&buff, (UBYTE *)&mystrng[0]))
            return(-1);
    if (AddString(&buff, (UBYTE *)"*\n"))
        return(-1);
    if(!cfg->JedecFuseChk)
    {
        if (AddByte(&buff, (UBYTE)0x3))
            return(-1);
    }
    return(0);
}

canonical_original_code:
int MakeJedecBuff(struct ActBuffer buff, int galtype, struct Config *cfg)
{
    unsigned char mystrng[16];
    int n; int m; int bitnum; int bitnum2; int flag;
    int MaxFuseAdr = 0; int RowSize = 0; int XORSize = 0;
    switch (galtype)
    {
         case 1:
                 MaxFuseAdr = 31;
                 RowSize = 64;
                 XORSize = 8;
                 break;
         case 2:
                 MaxFuseAdr = 39;
                 RowSize = 64;
                 XORSize = 8;
                 break;
         case 3:
                 MaxFuseAdr = 43;
                 RowSize = 132;
                 XORSize = 10;
                 break;
         case 4:
                 MaxFuseAdr = 39;
                 RowSize = 80;
                 XORSize = 10;
                 break;
    }
    if (!cfg->JedecFuseChk)
        if (AddString(&buff, (unsigned char *)"\2\n"))
            return(-1);
    if (AddString(&buff, (unsigned char *)"Used Program:   GALasm 2.1\n"))
        return(-1);
    if (AddString(&buff, (unsigned char *)"GAL-Assembler:  GALasm 2.1\n"))
        return(-1);
    if (galtype == 1)
    {
        if (AddString(&buff, (unsigned char *)"Device:         GAL16V8\n\n"))
            return(-1);
    }
    if (galtype == 2)
    {
        if (AddString(&buff, (unsigned char *)"Device:         GAL20V8\n\n"))
            return(-1);
    }
    if (galtype == 4)
    {
        if (AddString(&buff, (unsigned char *)"Device:         GAL20RA10\n\n"))
            return(-1);
    }
    if (galtype == 3)
    {
        if (AddString(&buff, (unsigned char *)"Device:         GAL22V10\n\n"))
            return(-1);
    }
    if (AddString(&buff, (unsigned char *)"*F0\n"))
        return(-1);
    if (cfg->JedecSecBit)
    {
        if (AddString(&buff, (unsigned char *)"*G1\n"))
            return(-1);
    }
    else
        if (AddString(&buff, (unsigned char *)"*G0\n"))
            return(-1);
    if (galtype == 1)
        if (AddString(&buff, (unsigned char *)"*QF2194\n"))
            return(-1);
    if (galtype == 2)
        if (AddString(&buff, (unsigned char *)"*QF2706\n"))
            return(-1);
    if (galtype == 4)
        if (AddString(&buff, (unsigned char *)"*QF3274\n"))
            return(-1);
    if (galtype == 3)
        if (AddString(&buff, (unsigned char *)"*QF5892\n"))
            return(-1);
    bitnum = bitnum2 = flag = 0;
    for (m = 0; m < RowSize; m++)
    {
        flag = 0;
        bitnum2 = bitnum;
        for (n = 0; n <= MaxFuseAdr; n++)
        {
            if (Jedec.GALLogic[bitnum2])
            {
                flag = 1;
                break;
            }
            bitnum2++;
        }
        if (flag)
        {
            sprintf((char *)&mystrng[0], "*L%04d ", bitnum);
            if (AddString(&buff, (unsigned char *)&mystrng[0]))
                return(-1);
            for (n=0; n<=MaxFuseAdr; n++)
            {
                if (AddByte(&buff, (UBYTE)(Jedec.GALLogic[bitnum] + '0')))
                    return(-1);
                bitnum++;
            }
            if (AddByte(&buff, (unsigned char)'\n'))
                return(-1);
        }
        else
            bitnum = bitnum2;
    }
    if (!flag)
        bitnum = bitnum2;
    sprintf((char *)&mystrng[0], "*L%04d ", bitnum);
    if (AddString(&buff, (unsigned char *)&mystrng[0]))
        return(-1);
    for (n = 0; n < XORSize; n++)
    {
        if (AddByte(&buff, (UBYTE)(Jedec.GALXOR[n] + '0')))
            return(-1);
        bitnum++;
        if (galtype == 3)
        {
            if (AddByte(&buff, (UBYTE)(Jedec.GALS1[n] + '0')))
                return(-1);
            bitnum++;
        }
    }
    if (AddByte(&buff, (unsigned char)'\n'))
        return(-1);
    sprintf((char *)&mystrng[0], "*L%04d ", bitnum);
    if (AddString(&buff, (unsigned char *)&mystrng[0]))
        return(-1);
    for (n = 0; n < 64; n++)
    {
        if (AddByte(&buff, (UBYTE)(Jedec.GALSig[n] + '0')))
            return(-1);
        bitnum++;
    }
    if (AddByte(&buff, (unsigned char)'\n'))
        return(-1);
    if ((galtype == 1) || (galtype == 2))
    {
        sprintf((char *)&mystrng[0], "*L%04d ", bitnum);
        if (AddString(&buff, (unsigned char *)&mystrng[0]))
            return(-1);
        for (n = 0; n < 8; n++)
        {
            if (AddByte(&buff, (UBYTE)(Jedec.GALAC1[n] + '0')))
                return(-1);
            bitnum++;
        }
        if (AddByte(&buff, (unsigned char)'\n'))
            return(-1);
        sprintf((char *)&mystrng[0], "*L%04d ", bitnum);
        if (AddString(&buff, (unsigned char *)&mystrng[0]))
            return(-1);
        for (n = 0; n < 64; n++)
        {
            if (AddByte(&buff, (UBYTE)(Jedec.GALPT[n] + '0')))
                return(-1);
            bitnum++;
        }
        if (AddByte(&buff, (unsigned char)'\n'))
            return(-1);
        sprintf((char *)&mystrng[0], "*L%04d ", bitnum);
        if (AddString(&buff, (unsigned char *)&mystrng[0]))
            return(-1);
        if (AddByte(&buff, (UBYTE)(Jedec.GALSYN + '0')))
            return(-1);
        if (AddByte(&buff, (unsigned char)'\n'))
            return(-1);
        bitnum++;
        sprintf((char *)&mystrng[0], "*L%04d ", bitnum);
        if (AddString(&buff, (unsigned char *)&mystrng[0]))
            return(-1);
        if (AddByte(&buff, (UBYTE)(Jedec.GALAC0 + '0')))
            return(-1);
        if (AddByte(&buff, (unsigned char)'\n'))
            return(-1);
    }
        sprintf((char *)&mystrng[0], "*C%04x\n", FuseChecksum(galtype));
        if (AddString(&buff, (unsigned char *)&mystrng[0]))
            return(-1);
    if (AddString(&buff, (unsigned char *)"*\n"))
        return(-1);
    if(!cfg->JedecFuseChk)
    {
        if (AddByte(&buff, (unsigned char)0x3))
            return(-1);
    }
    return(0);
}

variable_types: struct ActBuffer { struct Buffer { struct Buffer *Next; struct Buffer *Prev; unsigned char Entries[256]; } *ThisBuff; unsigned char *Entry; unsigned char *BuffEnd; }

variable_types: struct Config { short GenFuse; short GenChip; short GenPin; short JedecSecBit; short JedecFuseChk; short ForceCRLF; short Verbose; } *

variable_types:
mystrng
unsigned char [16]

variable_types: m int

variable_types: bitnum int

variable_types: flag int

variable_types: MaxFuseAdr int

variable_types: RowSize int

variable_types: XORSize int

user_defined_types: struct ActBuffer { struct Buffer *ThisBuff; unsigned char *Entry; unsigned char *BuffEnd; }


socketSend

repo: dextero/tm-projekt

decompiled_code:
__int64 __fastcall socketSend(__int64 a1, __int64 a2, __int64 a3)
{
return tcpSend(a1, 24LL, a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3)
{
return tcpSend(a1, 24LL, a2, a3);
}

original_code:
int socketSend(tcpIp6Socket *sock,
               void *data,
               size_t data_size) {
    return tcpSend(sock, TCP_FLAG_ACK | TCP_FLAG_PSH, data, data_size);
}

canonical_original_code:
int socketSend(struct tcpIp6Socket *sock, void *data, unsigned long data_size) {
    return tcpSend(sock, TCP_FLAG_ACK | TCP_FLAG_PSH, data, data_size);
}

variable_types: struct tcpIp6Socket { struct eth_socket { struct mac_address { unsigned char bytes[6]; } mac; int raw_socket_fd; } ethSocket; enum tcpSocketState { SOCK_STATE_CLOSED=0, SOCK_STATE_LISTEN=1, SOCK_STATE_SYN_SENT=2, SOCK_STATE_SYN_RECEIVED=3, SOCK_STATE_ESTABLISHED=4, SOCK_STATE_FIN_WAIT_1=5, SOCK_STATE_FIN_WAIT_2=6, SOCK_STATE_CLOSE_WAIT=7, SOCK_STATE_CLOSING=8, SOCK_STATE_LAST_ACK=9, SOCK_STATE_TIME_WAIT=10 } state; unsigned short localAddress[8]; unsigned short remoteAddress[8]; unsigned short remotePort; unsigned short localPort; unsigned int sequenceNumber; unsigned int currPacketDataAlreadyRead; unsigned int lastAcknowledgedSeqNumber; unsigned int lastWindowWidthBytes; unsigned int lastReceivedAckNumber; unsigned int nextUnreadSeqNumber; struct tcpStream { void **packets; unsigned int currPacketSeqNumber; unsigned int currPacketDataAlreadyRead; unsigned int nextContiniousSeqNumber; } stream; void **unacknowledgedPackets; } *

variable_types:
data
void *

user_defined_types: struct tcpIp6Socket { struct eth_socket ethSocket; enum tcpSocketState state; unsigned short localAddress[8]; unsigned short remoteAddress[8]; unsigned short remotePort; unsigned short localPort; unsigned int sequenceNumber; unsigned int currPacketDataAlreadyRead; unsigned int lastAcknowledgedSeqNumber; unsigned int lastWindowWidthBytes; unsigned int lastReceivedAckNumber; unsigned int nextUnreadSeqNumber; struct tcpStream stream; void **unacknowledgedPackets; }

user_defined_types: struct eth_socket { struct mac_address mac; int raw_socket_fd; }


socketRelease

decompiled_code:
__int64 __fastcall socketRelease(__int64 a1)
{
__int64 result; // rax
__int64 *v2; // r8

result = allTcpSockets;
if ( allTcpSockets )
{
v2 = &allTcpSockets;
while ( a1 != result )
{
v2 = (__int64 *)(result - 8);
result = *(_QWORD *)(result - 8);
if ( !result )
return result;
}
list_erase__(v2);
result = logInfo("socket erased");
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 result; // rax
__int64 *v2; // r8

result = allTcpSockets;
if ( allTcpSockets )
{
v2 = &allTcpSockets;
while ( a1 != result )
{
v2 = (__int64 *)(result - 8);
result = *(_QWORD *)(result - 8);
if ( !result )
return result;
}
list_erase__(v2);
result = logInfo("socket erased");
}
return result;
}

original_code:
void socketRelease(tcpIp6Socket *sock) {
    tcpIp6Socket **sockPtr;
    for ((sockPtr) = (&allTcpSockets); *(sockPtr); (sockPtr) = ((typeof(*(sockPtr))*)(((char*)(*(sockPtr))) - sizeof(union extra_space_union__)))) {
        if (*sockPtr == sock) {
            list_erase__((void**)sockPtr);
            logInfo("socket erased");
            return;
        }
    }
}

canonical_original_code:
void socketRelease(struct tcpIp6Socket *sock) {
    struct tcpIp6Socket **sockPtr;
    for ((sockPtr) = (&allTcpSockets); *(sockPtr); (sockPtr) = ((typeof(*(sockPtr))*)(((char *)(*(sockPtr))) - sizeof(union extra_space_union__)))) {
        if (*sockPtr == sock) {
            list_erase__((void **)sockPtr);
            logInfo("socket erased");
            return;
        }
    }
}

variable_types: struct tcpIp6Socket { struct eth_socket { struct mac_address { unsigned char bytes[6]; } mac; int raw_socket_fd; } ethSocket; enum tcpSocketState { SOCK_STATE_CLOSED=0, SOCK_STATE_LISTEN=1, SOCK_STATE_SYN_SENT=2, SOCK_STATE_SYN_RECEIVED=3, SOCK_STATE_ESTABLISHED=4, SOCK_STATE_FIN_WAIT_1=5, SOCK_STATE_FIN_WAIT_2=6, SOCK_STATE_CLOSE_WAIT=7, SOCK_STATE_CLOSING=8, SOCK_STATE_LAST_ACK=9, SOCK_STATE_TIME_WAIT=10 } state; unsigned short localAddress[8]; unsigned short remoteAddress[8]; unsigned short remotePort; unsigned short localPort; unsigned int sequenceNumber; unsigned int currPacketDataAlreadyRead; unsigned int lastAcknowledgedSeqNumber; unsigned int lastWindowWidthBytes; unsigned int lastReceivedAckNumber; unsigned int nextUnreadSeqNumber; struct tcpStream { void **packets; unsigned int currPacketSeqNumber; unsigned int currPacketDataAlreadyRead; unsigned int nextContiniousSeqNumber; } stream; void **unacknowledgedPackets; } **

user_defined_types: union extra_space_union__ { void *voidPtr; long intMax; }


socketAccept

decompiled_code:
__int64 __fastcall socketAccept(__int64 a1, __int64 a2, __int16 a3)
{
__int64 result; // rax

eth_socket_init();
*(_DWORD *)(a1 + 12) = 1;
*(_WORD *)(a1 + 50) = a3;
if ( (unsigned int)ip6AddressForInterface(a2, a1 + 16) )
{
logInfo("ip6AddressForInterface failed");
result = 0xFFFFFFFFLL;
}
else
{
do
{
if ( (unsigned int)tcpProcessNextPacket(a1) )
goto LABEL_5;
}
while ( *(_DWORD *)(a1 + 12) == 1 );
if ( !(unsigned int)tcpSend(a1, 18LL, 0LL, 0LL) )
{
while ( 1 )
{
result = tcpProcessNextPacket(a1);
if ( (_DWORD)result )
break;
if ( *(_DWORD *)(a1 + 12) == 4 )
return result;
}
LABEL_5:
logInfo("processPacket failed");
return 0xFFFFFFFFLL;
}
logInfo("tcpSend failed");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, __int16 a3)
{
__int64 result; // rax

eth_socket_init();
*(_DWORD *)(a1 + 12) = 1;
*(_WORD *)(a1 + 50) = a3;
if ( (unsigned int)ip6AddressForInterface(a2, a1 + 16) )
{
logInfo("ip6AddressForInterface failed");
result = 0xFFFFFFFFLL;
}
else
{
do
{
if ( (unsigned int)tcpProcessNextPacket(a1) )
goto LABEL_5;
}
while ( *(_DWORD *)(a1 + 12) == 1 );
if ( !(unsigned int)tcpSend(a1, 18LL, 0LL, 0LL) )
{
while ( 1 )
{
result = tcpProcessNextPacket(a1);
if ( (_DWORD)result )
break;
if ( *(_DWORD *)(a1 + 12) == 4 )
return result;
}
LABEL_5:
logInfo("processPacket failed");
return 0xFFFFFFFFLL;
}
logInfo("tcpSend failed");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int socketAccept(tcpIp6Socket *sock,
                 const char *interface,
                 uint16_t port) {
    eth_socket_init(&sock->ethSocket, interface);
    sock->state = SOCK_STATE_LISTEN;
    sock->localPort = port;
    if (ip6AddressForInterface(interface, &sock->localAddress)) {
        logInfo("ip6AddressForInterface failed");
        return -1;
    }
    do {
        if (tcpProcessNextPacket(sock)) {
            logInfo("processPacket failed");
            return -1;
        }
    } while (sock->state == SOCK_STATE_LISTEN);
    if (tcpSend(sock, TCP_FLAG_SYN | TCP_FLAG_ACK, ((void *)0), 0)) {
        logInfo("tcpSend failed");
        return -1;
    }
    do {
        if (tcpProcessNextPacket(sock)) {
            logInfo("processPacket failed");
            return -1;
        }
    } while (sock->state != SOCK_STATE_ESTABLISHED);
    return 0;
}

canonical_original_code:
int socketAccept(struct tcpIp6Socket *sock, char *interface, unsigned short port) {
    eth_socket_init(&sock->ethSocket, interface);
    sock->state = SOCK_STATE_LISTEN;
    sock->localPort = port;
    if (ip6AddressForInterface(interface, &sock->localAddress)) {
        logInfo("ip6AddressForInterface failed");
        return -1;
    }
    do {
        if (tcpProcessNextPacket(sock)) {
            logInfo("processPacket failed");
            return -1;
        }
    } while (sock->state == SOCK_STATE_LISTEN);
    if (tcpSend(sock, TCP_FLAG_SYN | TCP_FLAG_ACK, ((void *)0), 0)) {
        logInfo("tcpSend failed");
        return -1;
    }
    do {
        if (tcpProcessNextPacket(sock)) {
            logInfo("processPacket failed");
            return -1;
        }
    } while (sock->state != SOCK_STATE_ESTABLISHED);
    return 0;
}

variable_types:
interface
char *

variable_types:
port
unsigned short

user_defined_types: enum tcpSocketState { SOCK_STATE_CLOSED=0, SOCK_STATE_LISTEN=1, SOCK_STATE_SYN_SENT=2, SOCK_STATE_SYN_RECEIVED=3, SOCK_STATE_ESTABLISHED=4, SOCK_STATE_FIN_WAIT_1=5, SOCK_STATE_FIN_WAIT_2=6, SOCK_STATE_CLOSE_WAIT=7, SOCK_STATE_CLOSING=8, SOCK_STATE_LAST_ACK=9, SOCK_STATE_TIME_WAIT=10 }

user_defined_types: struct tcpStream { void **packets; unsigned int currPacketSeqNumber; unsigned int currPacketDataAlreadyRead; unsigned int nextContiniousSeqNumber; }

user_defined_types: struct mac_address { unsigned char bytes[6]; }


socketReset

decompiled_code:
__int64 __fastcall socketReset(__int64 a1)
{
__int64 result; // rax
void **i; // rax

result = allTcpSockets;
if ( allTcpSockets )
{
while ( a1 != result )
{
result = *(_QWORD *)(result - 8);
if ( !result )
goto LABEL_8;
}
for ( i = *(void ***)(a1 + 80); i; i = *(void ***)(a1 + 80) )
{
free(*i);
list_erase__(a1 + 80);
}
result = logInfo("remaining packets freed");
}
else
{
LABEL_8:
*(_DWORD *)(a1 + 12) = 0;
}
return result;
}

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

result = allTcpSockets;
if ( allTcpSockets )
{
while ( a1 != result )
{
result = *(_QWORD *)(result - 8);
if ( !result )
goto LABEL_8;
}
for ( i = *(void ***)(a1 + 80); i; i = *(void ***)(a1 + 80) )
{
free(*i);
list_erase__(a1 + 80);
}
result = logInfo("remaining packets freed");
}
else
{
LABEL_8:
*(_DWORD *)(a1 + 12) = 0;
}
return result;
}

original_code:
void socketReset(tcpIp6Socket *sock) {
    tcpIp6Socket **curr;
    for ((curr) = (&allTcpSockets); *(curr); (curr) = ((typeof(*(curr))*)(((char*)(*(curr))) - sizeof(union extra_space_union__)))) {
        if (*curr == sock) {
            for (; *(&sock->stream.packets); list_erase__((void**)&sock->stream.packets)) {
                free(*sock->stream.packets);
            }
            logInfo("remaining packets freed");
            return;
        }
    }
    sock->state = SOCK_STATE_CLOSED;
}

canonical_original_code:
void socketReset(struct tcpIp6Socket *sock) {
    struct tcpIp6Socket **curr;
    for ((curr) = (&allTcpSockets); *(curr); (curr) = ((typeof(*(curr))*)(((char *)(*(curr))) - sizeof(union extra_space_union__)))) {
        if (*curr == sock) {
            for (; *(&sock->stream.packets); list_erase__((void **)&sock->stream.packets)) {
                free(*sock->stream.packets);
            }
            logInfo("remaining packets freed");
            return;
        }
    }
    sock->state = SOCK_STATE_CLOSED;
}


socketClose

decompiled_code:
__int64 __fastcall socketClose(__int64 a1)
{
closeConnection_isra_0(a1);
return socketReset(a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
func0(a1);
return func6(a1);
}

original_code:
void socketClose(tcpIp6Socket *sock) {
    closeConnection(sock);
    socketReset(sock);
}

canonical_original_code:
void socketClose(struct tcpIp6Socket *sock) {
    closeConnection(sock);
    socketReset(sock);
}


socketRecv

decompiled_code:
__int64 __fastcall socketRecv(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v4; // rbx
__int64 v5; // rsi
__int64 v6; // rax

if ( !a3 )
return 0LL;
v4 = a3;
while ( 1 )
{
v5 = *(unsigned int *)(a1 + 12);
if ( (_DWORD)v5 != 4 )
break;
v6 = tcpStreamReadNextPacket(a1 + 80, a2, v4);
if ( v6 == -2 )
{
if ( (unsigned int)tcpProcessNextPacket(a1) )
{
logInfo("processPacket failed");
return 0xFFFFFFFFLL;
}
}
else
{
if ( v6 == -1 )
return 0xFFFFFFFFLL;
a2 += v6;
v4 -= v6;
if ( !v4 )
return 0LL;
}
}
logInfo("invalid socket state: %d", v5);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, __int64 a3)
{
__int64 v4; // rbx
__int64 v5; // rsi
__int64 v6; // rax

if ( !a3 )
return 0LL;
v4 = a3;
while ( 1 )
{
v5 = *(unsigned int *)(a1 + 12);
if ( (_DWORD)v5 != 4 )
break;
v6 = tcpStreamReadNextPacket(a1 + 80, a2, v4);
if ( v6 == -2 )
{
if ( (unsigned int)tcpProcessNextPacket(a1) )
{
logInfo("processPacket failed");
return 0xFFFFFFFFLL;
}
}
else
{
if ( v6 == -1 )
return 0xFFFFFFFFLL;
a2 += v6;
v4 -= v6;
if ( !v4 )
return 0LL;
}
}
logInfo("invalid socket state: %d", v5);
return 0xFFFFFFFFLL;
}

original_code:
int socketRecv(tcpIp6Socket *sock,
               void *buffer,
               size_t bufferSize) {
    while (bufferSize > 0) {
        ssize_t bytesRead;
        if (sock->state != SOCK_STATE_ESTABLISHED) {
            logInfo("invalid socket state: %d", sock->state);
            return -1;
        }
        bytesRead = tcpStreamReadNextPacket(&sock->stream, buffer, bufferSize);
        switch (bytesRead) {
        case (-1):
            return -1;
        case (-2):
            if (tcpProcessNextPacket(sock)) {
                logInfo("processPacket failed");
                return -1;
            }
            break;
        default:
            bufferSize -= bytesRead;
            buffer = (char*)buffer + bytesRead;
            break;
        }
    }
    return 0;
}

canonical_original_code:
int socketRecv(struct tcpIp6Socket *sock, void *buffer, unsigned long bufferSize) {
    while (bufferSize > 0) {
        long bytesRead;
        if (sock->state != SOCK_STATE_ESTABLISHED) {
            logInfo("invalid socket state: %d", sock->state);
            return -1;
        }
        bytesRead = tcpStreamReadNextPacket(&sock->stream, buffer, bufferSize);
        switch (bytesRead) {
        case (-1):
            return -1;
        case (-2):
            if (tcpProcessNextPacket(sock)) {
                logInfo("processPacket failed");
                return -1;
            }
            break;
        default:
            bufferSize -= bytesRead;
            buffer = (char *)buffer + bytesRead;
            break;
        }
    }
    return 0;
}

variable_types:
buffer
void *

variable_types:
bufferSize
unsigned long

variable_types: bytesRead long


socketRecvLine

decompiled_code:
__int64 __fastcall socketRecvLine(__int64 a1, _QWORD *a2, _QWORD *a3)
{
__int64 v5; // rsi
int v6; // eax

*a2 = 0LL;
*a3 = 0LL;
while ( 1 )
{
v5 = *(unsigned int *)(a1 + 12);
if ( (_DWORD)v5 != 4 )
break;
v6 = tcpStreamReadNextLine(a1 + 80, a2, a3);
if ( v6 != -2 )
return (unsigned int)-(v6 == -1);
if ( (unsigned int)tcpProcessNextPacket(a1) )
{
logInfo("processPacket failed");
return 0xFFFFFFFFLL;
}
}
logInfo("invalid socket state: %d", v5);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, _QWORD *a2, _QWORD *a3)
{
__int64 v5; // rsi
int v6; // eax

*a2 = 0LL;
*a3 = 0LL;
while ( 1 )
{
v5 = *(unsigned int *)(a1 + 12);
if ( (_DWORD)v5 != 4 )
break;
v6 = tcpStreamReadNextLine(a1 + 80, a2, a3);
if ( v6 != -2 )
return (unsigned int)-(v6 == -1);
if ( (unsigned int)tcpProcessNextPacket(a1) )
{
logInfo("processPacket failed");
return 0xFFFFFFFFLL;
}
}
logInfo("invalid socket state: %d", v5);
return 0xFFFFFFFFLL;
}

original_code:
int socketRecvLine(tcpIp6Socket *sock,
                   char **outLine,
                   size_t *outSize) {
    *outLine = ((void *)0);
    *outSize = 0;
    while (1) {
        ssize_t bytesRead;
        if (sock->state != SOCK_STATE_ESTABLISHED) {
            logInfo("invalid socket state: %d", sock->state);
            return -1;
        }
        bytesRead = tcpStreamReadNextLine(&sock->stream, outLine, outSize);
        switch (bytesRead) {
        case (-1):
            return -1;
        case (-2):
            if (tcpProcessNextPacket(sock)) {
                logInfo("processPacket failed");
                return -1;
            }
            break;
        default:
            return 0;
        }
    }
    return -1;
}

canonical_original_code:
int socketRecvLine(struct tcpIp6Socket *sock, char **outLine, unsigned long *outSize) {
    *outLine = ((void *)0);
    *outSize = 0;
    while (1) {
        long bytesRead;
        if (sock->state != SOCK_STATE_ESTABLISHED) {
            logInfo("invalid socket state: %d", sock->state);
            return -1;
        }
        bytesRead = tcpStreamReadNextLine(&sock->stream, outLine, outSize);
        switch (bytesRead) {
        case (-1):
            return -1;
        case (-2):
            if (tcpProcessNextPacket(sock)) {
                logInfo("processPacket failed");
                return -1;
            }
            break;
        default:
            return 0;
        }
    }
    return -1;
}

variable_types:
outLine
char **

variable_types:
outSize
unsigned long *


repo: dgdunix/qemacs-tmp

decompiled_code:
__int64 makefile_init()
{
qword_4D0 = (__int64)makefile_mode_probe;
makefile_mode = (__int128)_mm_loadu_si128(text_mode);
xmmword_4E0 = (__int128)_mm_loadu_si128(&text_mode[2]);
xmmword_4F0 = (__int128)_mm_loadu_si128(&text_mode[3]);
xmmword_500 = (__int128)_mm_loadu_si128(&text_mode[4]);
xmmword_510 = (__int128)_mm_loadu_si128(&text_mode[5]);
xmmword_520 = (__int128)_mm_loadu_si128(&text_mode[6]);
xmmword_530 = (__int128)_mm_loadu_si128(&text_mode[7]);
xmmword_540 = (__int128)_mm_loadu_si128(&text_mode[8]);
xmmword_550 = (__int128)_mm_loadu_si128(&text_mode[9]);
xmmword_560 = (__int128)_mm_loadu_si128(&text_mode[10]);
xmmword_570 = (__int128)_mm_loadu_si128(&text_mode[11]);
*(_QWORD *)&makefile_mode = "Makefile";
qword_4D8 = (__int64)makefile_mode_init;
qe_register_mode(&makefile_mode);
qe_register_cmd_table(&makefile_commands, &makefile_mode);
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
qword_4D0 = (__int64)makefile_mode_probe;
makefile_mode = (__int128)_mm_loadu_si128(text_mode);
xmmword_4E0 = (__int128)_mm_loadu_si128(&text_mode[2]);
xmmword_4F0 = (__int128)_mm_loadu_si128(&text_mode[3]);
xmmword_500 = (__int128)_mm_loadu_si128(&text_mode[4]);
xmmword_510 = (__int128)_mm_loadu_si128(&text_mode[5]);
xmmword_520 = (__int128)_mm_loadu_si128(&text_mode[6]);
xmmword_530 = (__int128)_mm_loadu_si128(&text_mode[7]);
xmmword_540 = (__int128)_mm_loadu_si128(&text_mode[8]);
xmmword_550 = (__int128)_mm_loadu_si128(&text_mode[9]);
xmmword_560 = (__int128)_mm_loadu_si128(&text_mode[10]);
xmmword_570 = (__int128)_mm_loadu_si128(&text_mode[11]);
*(_QWORD *)&makefile_mode = "Makefile";
qword_4D8 = (__int64)makefile_mode_init;
qe_register_mode(&makefile_mode);
qe_register_cmd_table(&makefile_commands, &makefile_mode);
return 0LL;
}

original_code:
static int makefile_init(void)
{
    memcpy(&makefile_mode, &text_mode, sizeof(ModeDef));
    makefile_mode.name = "Makefile";
    makefile_mode.mode_probe = makefile_mode_probe;
    makefile_mode.mode_init = makefile_mode_init;
    qe_register_mode(&makefile_mode);
    qe_register_cmd_table(makefile_commands, &makefile_mode);
    return 0;
}

canonical_original_code:
static int makefile_init()
{
    memcpy(&makefile_mode, &text_mode, sizeof(ModeDef));
    makefile_mode.name = "Makefile";
    makefile_mode.mode_probe = makefile_mode_probe;
    makefile_mode.mode_init = makefile_mode_init;
    qe_register_mode(&makefile_mode);
    qe_register_cmd_table(makefile_commands, &makefile_mode);
    return 0;
}