strrep

decompiled_code:
__int64 __fastcall strrep(char *haystack, char *needle, void *src)
{
unsigned int v3; // er14
size_t v5; // rax
int v6; // er15
char *v7; // rbx
size_t v8; // rbp
int v9; // er14
size_t v10; // rax
size_t v12; // rax

v3 = 0;
v5 = strlen(needle);
if ( v5 )
{
v6 = v5;
v7 = strstr(haystack, needle);
if ( v7 )
{
v8 = strlen((const char *)src);
v9 = v8 - v6;
do
{
if ( v9 <= 0 )
{
if ( v9 )
{
v12 = strlen(v7);
memmove(v7, &v7[-v9], v9 + v12 + 1);
}
}
else
{
v10 = strlen(v7);
memmove(&v7[v9], v7, v10 + 1);
}
memmove(v7, src, v8);
v7 = strstr(&v7[v8], needle);
}
while ( v7 );
v3 = 1;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *haystack, char *needle, void *src)
{
unsigned int v3; // er14
size_t v5; // rax
int v6; // er15
char *v7; // rbx
size_t v8; // rbp
int v9; // er14
size_t v10; // rax
size_t v12; // rax

v3 = 0;
v5 = strlen(needle);
if ( v5 )
{
v6 = v5;
v7 = strstr(haystack, needle);
if ( v7 )
{
v8 = strlen((const char *)src);
v9 = v8 - v6;
do
{
if ( v9 <= 0 )
{
if ( v9 )
{
v12 = strlen(v7);
memmove(v7, &v7[-v9], v9 + v12 + 1);
}
}
else
{
v10 = strlen(v7);
memmove(&v7[v9], v7, v10 + 1);
}
memmove(v7, src, v8);
v7 = strstr(&v7[v8], needle);
}
while ( v7 );
v3 = 1;
}
}
return v3;
}

original_code:
int strrep(char *buf, char *mae, char *ato)
{
    char *mitsuke, *findpos;
 size_t maelen, atolen;
 int shift;
 findpos = buf;
 maelen = strlen(mae);
 atolen = strlen(ato);
 shift = (int)(strlen(ato)-strlen(mae));
 if (maelen == 0 || strstr(findpos, mae) == ((void *)0)) return 0;
 while ((mitsuke = strstr(findpos, mae)) != ((void *)0)) {
  if (shift > 0) {
   memmove(mitsuke + shift, mitsuke, strlen(mitsuke) + 1);
  } else if (shift < 0) {
   memmove(mitsuke, mitsuke - shift, strlen(mitsuke) + shift + 1);
  }
  memmove(mitsuke, ato, atolen);
  findpos = mitsuke + atolen;
 }
 return 1;
}

canonical_original_code:
int strrep(char *buf, char *mae, char *ato)
{
    char *mitsuke; char *findpos;
 unsigned long maelen; unsigned long atolen;
 int shift;
 findpos = buf;
 maelen = strlen(mae);
 atolen = strlen(ato);
 shift = (int)(strlen(ato)-strlen(mae));
 if (maelen == 0 || strstr(findpos, mae) == ((void *)0)) return 0;
 while ((mitsuke = strstr(findpos, mae)) != ((void *)0)) {
  if (shift > 0) {
   memmove(mitsuke + shift, mitsuke, strlen(mitsuke) + 1);
  } else if (shift < 0) {
   memmove(mitsuke, mitsuke - shift, strlen(mitsuke) + shift + 1);
  }
  memmove(mitsuke, ato, atolen);
  findpos = mitsuke + atolen;
 }
 return 1;
}

variable_types:
buf
char *

variable_types:
mae
char *

variable_types:
ato
char *

variable_types:
mitsuke
char *

variable_types:
findpos
char *

variable_types:
maelen
unsigned long

variable_types:
atolen
unsigned long

variable_types: shift int


getBit

original_code:
int getBit(unsigned char *byte, int *pbit, int gbit) {
 int pbyte = *pbit / 8;
 unsigned char *fbyte = byte + pbyte;
 int cutbit = *pbit - (pbyte * 8);
 int lcutbit = 32 - (cutbit + gbit);
 unsigned char tbuf[4];
 unsigned int tnum;
 memcpy(tbuf, fbyte, sizeof(unsigned char) * 4);
 tbuf[0] = tbuf[0] << cutbit;
 tbuf[0] = tbuf[0] >> cutbit;
 tnum = tbuf[0] << 24 | tbuf[1] << 16 | tbuf[2] << 8 | tbuf[3];
 tnum = tnum >> lcutbit;
 *pbit += gbit;
 return tnum;
}

variable_types:
byte
unsigned char *

variable_types:
pbit
int *

variable_types: gbit int

variable_types: pbyte int

variable_types:
fbyte
unsigned char *

variable_types: cutbit int

variable_types: lcutbit int

variable_types:
tbuf
unsigned char [4]

variable_types:
tnum
unsigned int

return_type: int


getStr

decompiled_code:
__int64 __fastcall getStr(__int64 a1, __int64 a2, int *a3, unsigned int a4)
{
int v7; // ebp
int v8; // esi
void *v9; // rax
_BYTE v11[4136]; // [rsp+0h] [rbp-1028h] BYREF

v7 = *a3;
v8 = *a3 + 7;
memset(v11, 0, 0x1000uLL);
if ( v7 >= 0 )
v8 = v7;
v9 = memcpy(v11, (const void *)(a2 + (v8 >> 3)), (int)a4);
*a3 = v7 + 8 * a4;
return AribToString(a1, v9, a4);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, int *a3, unsigned int a4)
{
int v7; // ebp
int v8; // esi
void *v9; // rax
_BYTE v11[4136]; // [rsp+0h] [rbp-1028h] BYREF

v7 = *a3;
v8 = *a3 + 7;
memset(v11, 0, 0x1000uLL);
if ( v7 >= 0 )
v8 = v7;
v9 = memcpy(v11, (const void *)(a2 + (v8 >> 3)), (int)a4);
*a3 = v7 + 8 * a4;
return AribToString(a1, v9, a4);
}

original_code:
void getStr(char *tostr, unsigned char *byte, int *pbit, int len) {
 char str[4096];
 int pbyte = *pbit / 8;
 unsigned char *fbyte = byte + pbyte;
 memset(str, 0, sizeof(char) * 4096);
 memcpy(str, fbyte, len);
 *pbit += (len * 8);
 AribToString(tostr, str, len);
 return;
}

variable_types:
tostr
char *

variable_types: len int

variable_types:
str
char [4096]

return_type: void


parseOTHERdesc

decompiled_code:
__int64 __fastcall parseOTHERdesc(__int64 a1)
{
__int64 v1; // r10
int v3; // [rsp+Ch] [rbp-4h] BYREF

v3 = 0;
getBit(a1, &v3, 8);
return (unsigned int)getBit(v1, &v3, 8) + 2;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 v1; // r10
int v3; // [rsp+Ch] [rbp-4h] BYREF

v3 = 0;
func1(a1, &v3, 8);
return (unsigned int)func1(v1, &v3, 8) + 2;
}

original_code:
int parseOTHERdesc(unsigned char *data) {
 int boff = 0;
 int descriptor_tag;
 int descriptor_length;
 descriptor_tag = getBit(data, &boff, 8);
 descriptor_length = getBit(data, &boff, 8);
 return descriptor_length + 2;
}

variable_types:
data
unsigned char *

variable_types: boff int


xmlspecialchars

original_code:
void xmlspecialchars(char *str)
{
 strrep(str, "&", "&");
 strrep(str, "'", "'");
 strrep(str, "\"", """);
 strrep(str, "<", "<");
 strrep(str, ">", ">");
}

variable_types:
str
char *


main

canonical_name: main

repo: hachreak/scheduler-clockdriven

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

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


decompiled_code:
void __fastcall __noreturn sp_task_handler(void *a1)
{
while ( 1 )
{
if ( (unsigned int)excstate_get_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8)) == 1 )
excstate_set_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8), 4LL);
excstate_wait_running((unsigned int)((_DWORD)&executive_sp_frame_desc + 8));
excstate_set_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8), 1LL);
puts("[SP] [WAKE UP] SP frame");
puts("[SP] [EXEC SP TASK]");
SP_TASK();
puts("[SP] [END]");
}
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
while ( 1 )
{
if ( (unsigned int)excstate_get_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8)) == 1 )
excstate_set_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8), 4LL);
excstate_wait_running((unsigned int)((_DWORD)&executive_sp_frame_desc + 8));
excstate_set_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8), 1LL);
puts("[SP] [WAKE UP] SP frame");
puts("[SP] [EXEC SP TASK]");
SP_TASK();
puts("[SP] [END]");
}
}

original_code:
void *sp_task_handler(){
  while(1){
    if(excstate_get_state(&executive_sp_frame_desc.excstate) == WORKING){
      excstate_set_state(&executive_sp_frame_desc.excstate, COMPLETED);
    }
    excstate_wait_running(&executive_sp_frame_desc.excstate);
    excstate_set_state(&executive_sp_frame_desc.excstate, WORKING);
    printf("[SP] [WAKE UP] SP frame\n");
    printf("[SP] [EXEC SP TASK]\n");
    SP_TASK();
    printf("[SP] [END]\n");
  }
  return ((void *)0);
}

return_type: void *


decompiled_code:
void __fastcall __noreturn frame_handler(void *a1)
{
__int64 v1; // r13
unsigned int v2; // ebp
__int64 v3; // rbx
unsigned int v4; // er12
__int64 v5; // r14

v1 = *(int *)a1;
v2 = *(_DWORD *)a1;
v3 = 208 * v1;
while ( 1 )
{
do
{
excstate_set_state(v3 + frame_descs + 8, 0LL);
printf("[FRAME %d] [WAIT WAKE UP]\n", v2);
excstate_wait_running(v3 + frame_descs + 8);
excstate_set_state(v3 + frame_descs + 8, 1LL);
printf("[FRAME %d] [WAKE UP]\n", v2);
v4 = **((_DWORD **)&SCHEDULE + v1);
}
while ( v4 == -1 );
v5 = 4LL;
do
{
printf("[FRAME %d] [EXECUTE %d] [INIT]\n", v2, v4);
excstate_set_state(v3 + frame_descs + 112, v4);
(*(&P_TASKS + (int)v4))();
printf("[FRAME %d] [EXECUTE %d] [END]\n", v2, v4);
v4 = *(_DWORD *)(*((_QWORD *)&SCHEDULE + v1) + v5);
v5 += 4LL;
}
while ( v4 != -1 );
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1(void *a1)
{
__int64 v1; // r13
unsigned int v2; // ebp
__int64 v3; // rbx
unsigned int v4; // er12
__int64 v5; // r14

v1 = *(int *)a1;
v2 = *(_DWORD *)a1;
v3 = 208 * v1;
while ( 1 )
{
do
{
excstate_set_state(v3 + frame_descs + 8, 0LL);
printf("[FRAME %d] [WAIT WAKE UP]\n", v2);
excstate_wait_running(v3 + frame_descs + 8);
excstate_set_state(v3 + frame_descs + 8, 1LL);
printf("[FRAME %d] [WAKE UP]\n", v2);
v4 = **((_DWORD **)&SCHEDULE + v1);
}
while ( v4 == -1 );
v5 = 4LL;
do
{
printf("[FRAME %d] [EXECUTE %d] [INIT]\n", v2, v4);
excstate_set_state(v3 + frame_descs + 112, v4);
(*(&P_TASKS + (int)v4))();
printf("[FRAME %d] [EXECUTE %d] [END]\n", v2, v4);
v4 = *(_DWORD *)(*((_QWORD *)&SCHEDULE + v1) + v5);
v5 += 4LL;
}
while ( v4 != -1 );
}
}

original_code:
void *frame_handler(void *arg){
  int index = * (int *) arg;
  while(1){
    excstate_set_state(&frame_descs[index].excstate, IDLE);
    printf("[FRAME %d] [WAIT WAKE UP]\n", index);
    excstate_wait_running(&frame_descs[index].excstate);
    excstate_set_state(&frame_descs[index].excstate, WORKING);
    printf("[FRAME %d] [WAKE UP]\n", index);
    int task_index = 0, i = 0;
    while((task_index = SCHEDULE[index][i]) != -1){
      printf("[FRAME %d] [EXECUTE %d] [INIT]\n", index, task_index);
      excstate_set_state(&frame_descs[index].task_in_execution, task_index);
      P_TASKS[task_index]();
      printf("[FRAME %d] [EXECUTE %d] [END]\n", index, task_index);
      i++;
    }
  }
  return ((void *)0);
}

canonical_original_code:
void *frame_handler(void *arg){
  int index = * (int *) arg;
  while(1){
    excstate_set_state(&frame_descs[index].excstate, IDLE);
    printf("[FRAME %d] [WAIT WAKE UP]\n", index);
    excstate_wait_running(&frame_descs[index].excstate);
    excstate_set_state(&frame_descs[index].excstate, WORKING);
    printf("[FRAME %d] [WAKE UP]\n", index);
    int task_index = 0; int i = 0;
    while((task_index = SCHEDULE[index][i]) != -1){
      printf("[FRAME %d] [EXECUTE %d] [INIT]\n", index, task_index);
      excstate_set_state(&frame_descs[index].task_in_execution, task_index);
      P_TASKS[task_index]();
      printf("[FRAME %d] [EXECUTE %d] [END]\n", index, task_index);
      i++;
    }
  }
  return ((void *)0);
}

variable_types:
arg
void *

variable_types: index int

variable_types: i int


decompiled_code:
__int64 __fastcall executive_check_deadline_frame(unsigned int a1)
{
__int64 v1; // rbx
__int64 result; // rax
unsigned int v3; // eax

printf("[FRAME %d] [DEADLINE CHECK]\n", a1);
v1 = 208LL * (int)a1;
result = (unsigned int)excstate_get_state(v1 + frame_descs + 8) - 1;
if ( (unsigned int)result <= 1 )
{
v3 = excstate_get_state(frame_descs + v1 + 112);
printf("[FRAME %d] [DEADLINE MISS] [TASK %d]!\n", a1, v3);
exit(1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
__int64 v1; // rbx
__int64 result; // rax
unsigned int v3; // eax

printf("[FRAME %d] [DEADLINE CHECK]\n", a1);
v1 = 208LL * (int)a1;
result = (unsigned int)excstate_get_state(v1 + frame_descs + 8) - 1;
if ( (unsigned int)result <= 1 )
{
v3 = excstate_get_state(frame_descs + v1 + 112);
printf("[FRAME %d] [DEADLINE MISS] [TASK %d]!\n", a1, v3);
exit(1);
}
return result;
}

original_code:
void executive_check_deadline_frame(int index){
  printf("[FRAME %d] [DEADLINE CHECK]\n", index);
  int state = excstate_get_state(&frame_descs[index].excstate);
  if(state == WORKING || state == PENDING){
    int task = excstate_get_state(&frame_descs[index].task_in_execution);
    printf("[FRAME %d] [DEADLINE MISS] [TASK %d]!\n", index, task);
    exit(1);
  }
}

variable_types: state int

variable_types: task int


decompiled_code:
bool __fastcall executive_sp_check_static_deadline(int a1)
{
int v1; // er9
unsigned int v2; // ebx
__int64 v3; // r8
int v4; // er12
int v5; // er13
int v6; // er15
unsigned int v7; // ebp
__int64 v8; // rcx
__int64 v9; // rdx
__int64 v10; // rsi

v1 = H_PERIOD / NUM_FRAMES * (a1 + 1);
v2 = H_PERIOD / NUM_FRAMES + v1;
v3 = (unsigned int)(v1 + SP_DLINE);
if ( H_PERIOD / NUM_FRAMES > SP_DLINE )
{
v10 = 0LL;
v5 = 0;
}
else
{
v4 = H_PERIOD / NUM_FRAMES * (a1 + 1);
v5 = 0;
v6 = H_PERIOD / NUM_FRAMES;
v7 = 1;
do
{
v8 = v2;
v2 += v6;
v9 = *((unsigned int *)&SLACK + (a1 + v7) % NUM_FRAMES);
v5 += v9;
printf("SLACK[%d] = %d; CURMAX[%d] DEADLINE[%d]\n", (a1 + v7) % NUM_FRAMES, v9, v8, v3);
v10 = v7++;
v3 = (unsigned int)(SP_DLINE + v4);
}
while ( SP_DLINE >= (int)(v2 - v4) );
}
printf("[SP] [WAIT] %d frame\n", v10);
excstate_set_state(&executive_sp_count_frame, (unsigned int)v10);
return SP_WCET <= v5;
}

canonical_decompiled_code:
bool __fastcall func3(int a1)
{
int v1; // er9
unsigned int v2; // ebx
__int64 v3; // r8
int v4; // er12
int v5; // er13
int v6; // er15
unsigned int v7; // ebp
__int64 v8; // rcx
__int64 v9; // rdx
__int64 v10; // rsi

v1 = H_PERIOD / NUM_FRAMES * (a1 + 1);
v2 = H_PERIOD / NUM_FRAMES + v1;
v3 = (unsigned int)(v1 + SP_DLINE);
if ( H_PERIOD / NUM_FRAMES > SP_DLINE )
{
v10 = 0LL;
v5 = 0;
}
else
{
v4 = H_PERIOD / NUM_FRAMES * (a1 + 1);
v5 = 0;
v6 = H_PERIOD / NUM_FRAMES;
v7 = 1;
do
{
v8 = v2;
v2 += v6;
v9 = *((unsigned int *)&SLACK + (a1 + v7) % NUM_FRAMES);
v5 += v9;
printf("SLACK[%d] = %d; CURMAX[%d] DEADLINE[%d]\n", (a1 + v7) % NUM_FRAMES, v9, v8, v3);
v10 = v7++;
v3 = (unsigned int)(SP_DLINE + v4);
}
while ( SP_DLINE >= (int)(v2 - v4) );
}
printf("[SP] [WAIT] %d frame\n", v10);
excstate_set_state(&executive_sp_count_frame, (unsigned int)v10);
return SP_WCET <= v5;
}

original_code:
_Bool executive_sp_check_static_deadline(int frame_index){
  int index = frame_index;
  index++;
  int quant = H_PERIOD / NUM_FRAMES;
  int qi = index * quant;
  int y = 1;
  int i = 0;
  int count_slack = 0;
  while((i = qi + (y*quant)) <= (qi + SP_DLINE)){
    int slack = SLACK[(index+y-1)%NUM_FRAMES];
    count_slack += slack;
    printf("SLACK[%d] = %d; CURMAX[%d] DEADLINE[%d]\n", (index+y-1)%NUM_FRAMES, slack, i, (qi + SP_DLINE));
    y++;
  }
  printf("[SP] [WAIT] %d frame\n", --y);
  excstate_set_state(&executive_sp_count_frame, y);
  return (count_slack >= SP_WCET ? 1 : 0);
}

variable_types: quant int

variable_types: qi int

variable_types: y int

variable_types: slack int


decompiled_code:
int executive_sp_check_deadline()
{
int result; // eax
int v1; // ebx
__int64 v2; // rbp
unsigned int v3; // eax

result = excstate_get_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8));
v1 = result;
if ( result == 1 || result == 4 )
{
v2 = (unsigned int)excstate_get_state(&executive_sp_count_frame) - 1;
excstate_set_state(&executive_sp_count_frame, v2);
v3 = excstate_get_state(&executive_sp_count_frame);
result = printf("[SP] [WAIT] [%d FRAMES]\n", v3);
if ( (int)v2 < 0 )
{
puts("[SP] [DEADLINE CHECK]");
if ( (unsigned int)(v1 - 1) <= 1 )
{
puts("[SP] [ERROR] SP not respect deadline!");
exit(1);
}
result = excstate_set_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8), 0LL);
}
}
return result;
}

canonical_decompiled_code:
int func4()
{
int result; // eax
int v1; // ebx
__int64 v2; // rbp
unsigned int v3; // eax

result = excstate_get_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8));
v1 = result;
if ( result == 1 || result == 4 )
{
v2 = (unsigned int)excstate_get_state(&executive_sp_count_frame) - 1;
excstate_set_state(&executive_sp_count_frame, v2);
v3 = excstate_get_state(&executive_sp_count_frame);
result = printf("[SP] [WAIT] [%d FRAMES]\n", v3);
if ( (int)v2 < 0 )
{
puts("[SP] [DEADLINE CHECK]");
if ( (unsigned int)(v1 - 1) <= 1 )
{
puts("[SP] [ERROR] SP not respect deadline!");
exit(1);
}
result = excstate_set_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8), 0LL);
}
}
return result;
}

original_code:
void executive_sp_check_deadline(){
  int state = excstate_get_state(&executive_sp_frame_desc.excstate);
  if(state == WORKING || state == COMPLETED){
    int frames = excstate_get_state(&executive_sp_count_frame);
    excstate_set_state(&executive_sp_count_frame, --frames);
    printf("[SP] [WAIT] [%d FRAMES]\n", excstate_get_state(&executive_sp_count_frame));
    if(frames < 0){
      printf("[SP] [DEADLINE CHECK]\n");
      if(state == WORKING || state == PENDING){
        printf("[SP] [ERROR] SP not respect deadline!\n");
        exit(1);
      }
      excstate_set_state(&executive_sp_frame_desc.excstate, IDLE);
    }
  }
}

variable_types: frames int


executive

decompiled_code:
void __fastcall __noreturn executive(void *a1)
{
unsigned int v1; // er12
unsigned int v2; // ebx
int v3; // er13
__syscall_slong_t v4; // rcx
__syscall_slong_t v5; // rcx
struct sched_param param; // [rsp+Ch] [rbp-3Ch] BYREF
struct timeval tv; // [rsp+10h] [rbp-38h] BYREF

v1 = 0;
gettimeofday(&tv, 0LL);
v2 = NUM_FRAMES;
abstime.tv_sec = tv.tv_sec;
v3 = -3 - NUM_FRAMES;
abstime.tv_nsec = 1000 * tv.tv_usec;
while ( 1 )
{
printf("\n------[FRAME %d]-------\n", v1);
v5 = abstime.tv_nsec + time2wait;
abstime.tv_sec += v5 / 1000000000;
abstime.tv_nsec = v5 % 1000000000;
if ( (unsigned int)excstate_get_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8)) )
{
slacktime2wait = 100000000 * *((_DWORD *)&SLACK + (int)v1);
param.__sched_priority = sched_get_priority_max(1) - 2;
pthread_setschedparam(executive_sp_frame_desc, 1, &param);
printf("[SP] [SET PRIORITY %d] [HIGH]\n", (unsigned int)param.__sched_priority);
}
printf("[FRAME %d] [PENDING]!\n", v1);
excstate_set_state(frame_descs + 208LL * (int)v1 + 8, 2LL);
if ( (unsigned int)excstate_get_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8)) == 3 )
{
puts("[SP] [PENDING] sp thread!");
excstate_set_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8), 2LL);
}
if ( (unsigned int)excstate_get_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8)) )
{
v4 = abstime.tv_nsec + slacktime2wait;
sp_abstime.tv_sec += v4 / 1000000000;
sp_abstime.tv_nsec = v4 % 1000000000;
pthread_cond_timedwait(&efb_cond, &efb_mutex, &sp_abstime);
param.__sched_priority = v3 + sched_get_priority_max(1);
pthread_setschedparam(executive_sp_frame_desc, 1, &param);
printf("[SP] [SET PRIORITY %d] [LOW]\n", (unsigned int)param.__sched_priority);
}
pthread_cond_timedwait(&efb_cond, &efb_mutex, &abstime);
printf(
"------[END FRAME]------ wait %lld nsec - next %lld sec %li nsec\n\n",
time2wait,
abstime.tv_sec,
abstime.tv_nsec);
executive_check_deadline_frame(v1);
executive_sp_check_deadline();
v1 = (v1 + 1) % v2;
excstate_set_state(&executive_frame_index, v1);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func5(void *a1)
{
unsigned int v1; // er12
unsigned int v2; // ebx
int v3; // er13
__syscall_slong_t v4; // rcx
__syscall_slong_t v5; // rcx
struct sched_param param; // [rsp+Ch] [rbp-3Ch] BYREF
struct timeval tv; // [rsp+10h] [rbp-38h] BYREF

v1 = 0;
gettimeofday(&tv, 0LL);
v2 = NUM_FRAMES;
abstime.tv_sec = tv.tv_sec;
v3 = -3 - NUM_FRAMES;
abstime.tv_nsec = 1000 * tv.tv_usec;
while ( 1 )
{
printf("\n------[FRAME %d]-------\n", v1);
v5 = abstime.tv_nsec + time2wait;
abstime.tv_sec += v5 / 1000000000;
abstime.tv_nsec = v5 % 1000000000;
if ( (unsigned int)excstate_get_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8)) )
{
slacktime2wait = 100000000 * *((_DWORD *)&SLACK + (int)v1);
param.__sched_priority = sched_get_priority_max(1) - 2;
pthread_setschedparam(executive_sp_frame_desc, 1, &param);
printf("[SP] [SET PRIORITY %d] [HIGH]\n", (unsigned int)param.__sched_priority);
}
printf("[FRAME %d] [PENDING]!\n", v1);
excstate_set_state(frame_descs + 208LL * (int)v1 + 8, 2LL);
if ( (unsigned int)excstate_get_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8)) == 3 )
{
puts("[SP] [PENDING] sp thread!");
excstate_set_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8), 2LL);
}
if ( (unsigned int)excstate_get_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8)) )
{
v4 = abstime.tv_nsec + slacktime2wait;
sp_abstime.tv_sec += v4 / 1000000000;
sp_abstime.tv_nsec = v4 % 1000000000;
pthread_cond_timedwait(&efb_cond, &efb_mutex, &sp_abstime);
param.__sched_priority = v3 + sched_get_priority_max(1);
pthread_setschedparam(executive_sp_frame_desc, 1, &param);
printf("[SP] [SET PRIORITY %d] [LOW]\n", (unsigned int)param.__sched_priority);
}
pthread_cond_timedwait(&efb_cond, &efb_mutex, &abstime);
printf(
"------[END FRAME]------ wait %lld nsec - next %lld sec %li nsec\n\n",
time2wait,
abstime.tv_sec,
abstime.tv_nsec);
func2(v1);
func4();
v1 = (v1 + 1) % v2;
excstate_set_state(&executive_frame_index, v1);
}
}

original_code:
void *executive(){
  struct timeval utime;
  gettimeofday(&utime,((void *)0));
  abstime.tv_sec = utime.tv_sec;
  abstime.tv_nsec = utime.tv_usec * 1000;
  int index = 0;
  while(1){
    printf("\n------[FRAME %d]-------\n",index);
    abstime.tv_sec += ( abstime.tv_nsec + time2wait ) / 1000000000;
    abstime.tv_nsec = ( abstime.tv_nsec + time2wait ) % 1000000000;
    if(excstate_get_state(&executive_sp_frame_desc.excstate) != IDLE){
       slacktime2wait = SLACK[index] * 100 * 1000000;
       struct sched_param param;
       param.sched_priority = sched_get_priority_max(1) - 2;
       pthread_setschedparam(executive_sp_frame_desc.pchild, 1, &param);
       printf("[SP] [SET PRIORITY %d] [HIGH]\n", param.sched_priority);
    }
    printf("[FRAME %d] [PENDING]!\n", index);
    excstate_set_state(&frame_descs[index].excstate, PENDING);
    if(excstate_get_state(&executive_sp_frame_desc.excstate) == READY){
      printf("[SP] [PENDING] sp thread!\n");
      excstate_set_state(&executive_sp_frame_desc.excstate, PENDING);
    }
    int state = excstate_get_state(&executive_sp_frame_desc.excstate);
    if(state != IDLE){
       sp_abstime.tv_sec += ( abstime.tv_nsec + slacktime2wait ) / 1000000000;
       sp_abstime.tv_nsec = ( abstime.tv_nsec + slacktime2wait ) % 1000000000;
       pthread_cond_timedwait(&efb_cond, &efb_mutex, &sp_abstime);
       struct sched_param param;
       param.sched_priority = sched_get_priority_max(1) - NUM_FRAMES - 3;
       pthread_setschedparam(executive_sp_frame_desc.pchild, 1, &param);
       printf("[SP] [SET PRIORITY %d] [LOW]\n", param.sched_priority);
    }
    pthread_cond_timedwait(&efb_cond, &efb_mutex, &abstime);
    printf("------[END FRAME]------ wait %lld nsec - next %lld sec %li nsec\n\n", time2wait, (long long)abstime.tv_sec, abstime.tv_nsec);
    executive_check_deadline_frame(index);
    executive_sp_check_deadline();
    index++;
    index = index % NUM_FRAMES;
    excstate_set_state(&executive_frame_index, index);
  }
}

variable_types: struct timeval { long tv_sec; long tv_usec; }

variable_types: struct sched_param { int sched_priority; }


decompiled_code:
__int64 sp_task_request()
{
__int64 v0; // r12
unsigned int v1; // eax
unsigned int v2; // er12
__int64 result; // rax

if ( (unsigned int)excstate_get_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8)) )
{
puts("[SP] [ALREADY SCHEDULED] can't schedule new sporadic job");
result = 0LL;
}
else
{
v0 = (unsigned int)excstate_get_state(&executive_frame_index);
printf("[SP] [CHECK SP REQ] frame %d\n", v0);
LOBYTE(v1) = executive_sp_check_static_deadline(v0);
v2 = v1;
if ( !(_BYTE)v1 )
{
puts("[SP] [ERROR] Can't schedule SP task");
exit(1);
}
puts("[SP] [TASK SCHEDULED]");
puts("[SP] [READY] SP frame!");
excstate_set_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8), 3LL);
result = v2;
}
return result;
}

canonical_decompiled_code:
__int64 func6()
{
__int64 v0; // r12
unsigned int v1; // eax
unsigned int v2; // er12
__int64 result; // rax

if ( (unsigned int)excstate_get_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8)) )
{
puts("[SP] [ALREADY SCHEDULED] can't schedule new sporadic job");
result = 0LL;
}
else
{
v0 = (unsigned int)excstate_get_state(&executive_frame_index);
printf("[SP] [CHECK SP REQ] frame %d\n", v0);
LOBYTE(v1) = func3(v0);
v2 = v1;
if ( !(_BYTE)v1 )
{
puts("[SP] [ERROR] Can't schedule SP task");
exit(1);
}
puts("[SP] [TASK SCHEDULED]");
puts("[SP] [READY] SP frame!");
excstate_set_state((unsigned int)((_DWORD)&executive_sp_frame_desc + 8), 3LL);
result = v2;
}
return result;
}

original_code:
_Bool sp_task_request(){
  if(excstate_get_state(&executive_sp_frame_desc.excstate) != IDLE){
    printf("[SP] [ALREADY SCHEDULED] can't schedule new sporadic job\n");
    return 0;
  }
  int index = excstate_get_state(&executive_frame_index);
  printf("[SP] [CHECK SP REQ] frame %d\n", index);
  if(!executive_sp_check_static_deadline(index)){
    printf("[SP] [ERROR] Can't schedule SP task\n");
    exit(1);
  }
  printf("[SP] [TASK SCHEDULED]\n");
  printf("[SP] [READY] SP frame!\n");
  excstate_set_state(&executive_sp_frame_desc.excstate, READY);
  return 1;
}


decompiled_code:
int executive_init_frame()
{
float v0; // xmm0_4

v0 = (float)(H_PERIOD / NUM_FRAMES);
time2wait = (unsigned int)(int)(float)((float)(100.0 * v0) * 1000000.0);
return printf("[INIT TIME] [QUANT %f] [EXECUTIVE QUANT %d] [NANOSEC FRAME %lld]\n", v0, 100LL, time2wait);
}

canonical_decompiled_code:
int func7()
{
float v0; // xmm0_4

v0 = (float)(H_PERIOD / NUM_FRAMES);
time2wait = (unsigned int)(int)(float)((float)(100.0 * v0) * 1000000.0);
return printf("[INIT TIME] [QUANT %f] [EXECUTIVE QUANT %d] [NANOSEC FRAME %lld]\n", v0, 100LL, time2wait);
}

original_code:
void executive_init_frame(){
  float quant = H_PERIOD / NUM_FRAMES;
  time2wait = quant * 100 * 1000000;
  printf("[INIT TIME] [QUANT %f] [EXECUTIVE QUANT %d] [NANOSEC FRAME %lld]\n", quant, 100, time2wait);
}

variable_types: quant float


decompiled_code:
int __fastcall executive_new_pthread_attr(pthread_attr_t *attr, int a2)
{
int result; // eax
struct sched_param param; // [rsp+Ch] [rbp-1Ch] BYREF

if ( pthread_attr_init(attr) )
{
perror("error init attr\n");
exit(1);
}
if ( pthread_attr_setschedpolicy(attr, 1) )
{
perror("error init attr - set policy\n");
exit(1);
}
pthread_attr_setinheritsched(attr, 1);
param.__sched_priority = a2;
result = pthread_attr_setschedparam(attr, &param);
if ( result )
{
perror("error init attr - set param\n");
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(pthread_attr_t *attr, int a2)
{
int result; // eax
struct sched_param param; // [rsp+Ch] [rbp-1Ch] BYREF

if ( pthread_attr_init(attr) )
{
perror("error init attr\n");
exit(1);
}
if ( pthread_attr_setschedpolicy(attr, 1) )
{
perror("error init attr - set policy\n");
exit(1);
}
pthread_attr_setinheritsched(attr, 1);
param.__sched_priority = a2;
result = pthread_attr_setschedparam(attr, &param);
if ( result )
{
perror("error init attr - set param\n");
exit(1);
}
return result;
}

original_code:
void executive_new_pthread_attr(pthread_attr_t *attr, int priority){
  if(pthread_attr_init(attr)){
    perror("error init attr\n");
    exit(1);
  }
  if(pthread_attr_setschedpolicy(attr, 1)){
    perror("error init attr - set policy\n");
    exit(1);
  }
  pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED);
  struct sched_param p;
  p.sched_priority = priority;
  if(pthread_attr_setschedparam(attr, &p)){
    perror("error init attr - set param\n");
    exit(1);
  }
}

canonical_original_code:
void executive_new_pthread_attr(union pthread_attr_t *attr, int priority){
  if(pthread_attr_init(attr)){
    perror("error init attr\n");
    exit(1);
  }
  if(pthread_attr_setschedpolicy(attr, 1)){
    perror("error init attr - set policy\n");
    exit(1);
  }
  pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED);
  struct sched_param p;
  p.sched_priority = priority;
  if(pthread_attr_setschedparam(attr, &p)){
    perror("error init attr - set param\n");
    exit(1);
  }
}

variable_types: union pthread_attr_t { char __size[56]; long __align; } *

variable_types: priority int

user_defined_types: struct sched_param { int sched_priority; }


decompiled_code:
int executive_init()
{
int v0; // er13
int v1; // eax
__int64 v2; // r15
unsigned int v3; // ebx
__int64 v4; // rbp
int v5; // ebp
unsigned int v6; // ebp
int v8; // [rsp+8h] [rbp-C0h]
int v9; // [rsp+Ch] [rbp-BCh]
pthread_attr_t v10; // [rsp+10h] [rbp-B8h] BYREF
pthread_attr_t attr; // [rsp+50h] [rbp-78h] BYREF

task_init();
executive_init_frame();
excstate_init(&executive_frame_index, 0LL);
excstate_init(&executive_sp_count_frame, 0LL);
v0 = NUM_FRAMES;
frame_descs = (__int64)malloc(208LL * NUM_FRAMES);
v1 = sched_get_priority_max(1);
v9 = v1;
if ( v0 )
{
v2 = 0LL;
v3 = 0;
v8 = v1 - 3;
do
{
v4 = v2 + frame_descs;
v2 += 208LL;
excstate_init(v4 + 8, 0LL);
executive_new_pthread_attr(&attr, v8 - v3);
printf("[FRAME %d] [TASK INIT] priority %d\n", v3, v8 - v3);
*(_DWORD *)(v4 + 104) = v3++;
pthread_create((pthread_t *)v4, &attr, (void *(*)(void *))frame_handler, (void *)(v4 + 104));
}
while ( v0 != v3 );
v5 = v9 - v0;
}
else
{
v5 = v1;
}
v6 = v5 - 3;
excstate_init((unsigned int)((_DWORD)&executive_sp_frame_desc + 8), 0LL);
printf("[SP] [TASK INIT] priority %d\n", v6);
executive_new_pthread_attr(&v10, v6);
pthread_create(&executive_sp_frame_desc, &v10, (void *(*)(void *))sp_task_handler, 0LL);
printf("[EXECUTIVE] [TASK INIT] priority %d\n", (unsigned int)(v9 - 1));
executive_new_pthread_attr(&attr, v9 - 1);
pthread_create(&executive_exec_desc, &attr, (void *(*)(void *))executive, 0LL);
return pthread_join(executive_exec_desc, 0LL);
}

canonical_decompiled_code:
int func9()
{
int v0; // er13
int v1; // eax
__int64 v2; // r15
unsigned int v3; // ebx
__int64 v4; // rbp
int v5; // ebp
unsigned int v6; // ebp
int v8; // [rsp+8h] [rbp-C0h]
int v9; // [rsp+Ch] [rbp-BCh]
pthread_attr_t v10; // [rsp+10h] [rbp-B8h] BYREF
pthread_attr_t attr; // [rsp+50h] [rbp-78h] BYREF

task_init();
func7();
excstate_init(&executive_frame_index, 0LL);
excstate_init(&executive_sp_count_frame, 0LL);
v0 = NUM_FRAMES;
frame_descs = (__int64)malloc(208LL * NUM_FRAMES);
v1 = sched_get_priority_max(1);
v9 = v1;
if ( v0 )
{
v2 = 0LL;
v3 = 0;
v8 = v1 - 3;
do
{
v4 = v2 + frame_descs;
v2 += 208LL;
excstate_init(v4 + 8, 0LL);
func8(&attr, v8 - v3);
printf("[FRAME %d] [TASK INIT] priority %d\n", v3, v8 - v3);
*(_DWORD *)(v4 + 104) = v3++;
pthread_create((pthread_t *)v4, &attr, (void *(*)(void *))frame_handler, (void *)(v4 + 104));
}
while ( v0 != v3 );
v5 = v9 - v0;
}
else
{
v5 = v1;
}
v6 = v5 - 3;
excstate_init((unsigned int)((_DWORD)&executive_sp_frame_desc + 8), 0LL);
printf("[SP] [TASK INIT] priority %d\n", v6);
func8(&v10, v6);
pthread_create(&executive_sp_frame_desc, &v10, (void *(*)(void *))sp_task_handler, 0LL);
printf("[EXECUTIVE] [TASK INIT] priority %d\n", (unsigned int)(v9 - 1));
func8(&attr, v9 - 1);
pthread_create(&executive_exec_desc, &attr, (void *(*)(void *))executive, 0LL);
return pthread_join(executive_exec_desc, 0LL);
}

original_code:
void executive_init(){
  task_init();
  executive_init_frame();
  excstate_init(&executive_frame_index, 0);
  excstate_init(&executive_sp_count_frame, 0);
  frame_descs = (frame_descriptor *)malloc(sizeof(frame_descriptor) * NUM_FRAMES);
  int max_priority = sched_get_priority_max(1);
  int i = 1;
  for(i=0; i<NUM_FRAMES; i++){
    frame_descriptor *fd = &frame_descs[i];
    excstate_init(&fd->excstate, IDLE);
    pthread_attr_t attr;
    int frame_priority = max_priority - 3;
    frame_priority -= i;
    executive_new_pthread_attr(&attr, frame_priority);
    printf("[FRAME %d] [TASK INIT] priority %d\n", i, frame_priority);
    fd->index = i;
    pthread_create( &fd->pchild, &attr, frame_handler, &fd->index);
  }
  excstate_init(&executive_sp_frame_desc.excstate, IDLE);
  printf("[SP] [TASK INIT] priority %d\n", max_priority - i - 3);
  pthread_attr_t attr_sp;
  executive_new_pthread_attr(&attr_sp, max_priority - i - 3);
  pthread_create(&executive_sp_frame_desc.pchild, &attr_sp, &sp_task_handler, ((void *)0));
  printf("[EXECUTIVE] [TASK INIT] priority %d\n", max_priority - 1);
  pthread_attr_t attr_exec;
  executive_new_pthread_attr(&attr_exec, max_priority - 1);
  pthread_create(&executive_exec_desc.pchild, &attr_exec, &executive, ((void *)0));
  pthread_join(executive_exec_desc.pchild, ((void *)0));
}

canonical_original_code:
void executive_init(){
  task_init();
  executive_init_frame();
  excstate_init(&executive_frame_index, 0);
  excstate_init(&executive_sp_count_frame, 0);
  frame_descs = (struct frame_descriptor *)malloc(sizeof(frame_descriptor) * NUM_FRAMES);
  int max_priority = sched_get_priority_max(1);
  int i = 1;
  for(i=0; i<NUM_FRAMES; i++){
    struct frame_descriptor *fd = &frame_descs[i];
    excstate_init(&fd->excstate, IDLE);
    union pthread_attr_t attr;
    int frame_priority = max_priority - 3;
    frame_priority -= i;
    executive_new_pthread_attr(&attr, frame_priority);
    printf("[FRAME %d] [TASK INIT] priority %d\n", i, frame_priority);
    fd->index = i;
    pthread_create( &fd->pchild, &attr, frame_handler, &fd->index);
  }
  excstate_init(&executive_sp_frame_desc.excstate, IDLE);
  printf("[SP] [TASK INIT] priority %d\n", max_priority - i - 3);
  union pthread_attr_t attr_sp;
  executive_new_pthread_attr(&attr_sp, max_priority - i - 3);
  pthread_create(&executive_sp_frame_desc.pchild, &attr_sp, &sp_task_handler, ((void *)0));
  printf("[EXECUTIVE] [TASK INIT] priority %d\n", max_priority - 1);
  union pthread_attr_t attr_exec;
  executive_new_pthread_attr(&attr_exec, max_priority - 1);
  pthread_create(&executive_exec_desc.pchild, &attr_exec, &executive, ((void *)0));
  pthread_join(executive_exec_desc.pchild, ((void *)0));
}

variable_types: struct frame_descriptor { unsigned long pchild; struct excstate { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; unsigned int SIZE; unsigned int count; } excstate; int index; struct excstate task_in_execution; } *

variable_types: union pthread_attr_t { char __size[56]; long __align; }

user_defined_types: struct frame_descriptor { unsigned long pchild; struct excstate excstate; int index; struct excstate task_in_execution; }

user_defined_types: struct excstate { union pthread_mutex_t mutex; union pthread_cond_t cond; unsigned int SIZE; unsigned int count; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
void task_destroy()
{
free(SCHEDULE);
free(*(&SCHEDULE + 1));
free(*(&SCHEDULE + 2));
}

canonical_decompiled_code:
void func7()
{
free(SCHEDULE);
free(*(&SCHEDULE + 1));
free(*(&SCHEDULE + 2));
}

original_code:
void task_destroy()
  {
  unsigned int i;
  for ( i = 0; i < NUM_FRAMES; ++i )
    free( SCHEDULE[i] );
  }

variable_types:
i
unsigned int


decompiled_code:
int __fastcall busy_wait(unsigned int a1)
{
int result; // eax
int v2; // esi
int v3; // ecx
int v4; // edx
int v5; // [rsp+Ch] [rbp-Ch]

result = printf("[BUSY WAIT] [%d msec]\n", a1);
if ( a1 )
{
v2 = 0;
v3 = calib;
do
{
result = v2;
if ( calib )
{
do
{
v4 = result * v5;
++result;
v5 = v4;
}
while ( v3 != result );
}
++v2;
++v3;
}
while ( a1 != v2 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(unsigned int a1)
{
int result; // eax
int v2; // esi
int v3; // ecx
int v4; // edx
int v5; // [rsp+Ch] [rbp-Ch]

result = printf("[BUSY WAIT] [%d msec]\n", a1);
if ( a1 )
{
v2 = 0;
v3 = calib;
do
{
result = v2;
if ( calib )
{
do
{
v4 = result * v5;
++result;
v5 = v4;
}
while ( v3 != result );
}
++v2;
++v3;
}
while ( a1 != v2 );
}
return result;
}

original_code:
void busy_wait(unsigned int millisec){
  printf("[BUSY WAIT] [%d msec]\n", millisec);
  unsigned int i, j;
  volatile unsigned int value;
  for (i=0; i < millisec; ++i)
    for (j=0; j < calib; ++j)
      value *= i+j;
}

canonical_original_code:
void busy_wait(unsigned int millisec){
  printf("[BUSY WAIT] [%d msec]\n", millisec);
  unsigned int i; unsigned int j;
  unsigned int value;
  for (i=0; i < millisec; ++i)
    for (j=0; j < calib; ++j)
      value *= i+j;
}

variable_types:
millisec
unsigned int

variable_types:
j
unsigned int

variable_types:
value
unsigned int


decompiled_code:
__int64 busy_calib()
{
unsigned int v0; // ebx
__int64 result; // rax
struct timeval v2; // [rsp+0h] [rbp-28h] BYREF
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

gettimeofday(&v2, 0LL);
busy_wait(1u);
gettimeofday(&tv, 0LL);
v0 = LODWORD(tv.tv_usec) + 1000000 * (LODWORD(tv.tv_sec) - LODWORD(v2.tv_sec)) - LODWORD(v2.tv_usec);
printf("[BUSY WAIT] [DURATION 1 msec ] [%d nsec]\n", v0);
result = 1000 * (calib / v0);
calib = 1000 * (calib / v0);
return result;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v0; // ebx
__int64 result; // rax
struct timeval v2; // [rsp+0h] [rbp-28h] BYREF
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

gettimeofday(&v2, 0LL);
func0(1u);
gettimeofday(&tv, 0LL);
v0 = LODWORD(tv.tv_usec) + 1000000 * (LODWORD(tv.tv_sec) - LODWORD(v2.tv_sec)) - LODWORD(v2.tv_usec);
printf("[BUSY WAIT] [DURATION 1 msec ] [%d nsec]\n", v0);
result = 1000 * (calib / v0);
calib = 1000 * (calib / v0);
return result;
}

original_code:
void busy_calib(){
  struct timeval before;
  struct timeval after;
  unsigned int duration;
  gettimeofday(&before, ((void *)0));
  busy_wait(1);
  gettimeofday(&after, ((void *)0));
  duration = (after.tv_sec - before.tv_sec) * 1000000;
  duration += after.tv_usec;
  duration -= before.tv_usec;
  printf("[BUSY WAIT] [DURATION 1 msec ] [%d nsec]\n", duration);
  calib /= duration;
  calib *= 1000;
}

variable_types:
duration
unsigned int

user_defined_types: struct timeval { long tv_sec; long tv_usec; }


decompiled_code:
int __fastcall excstate_init(__int64 a1, int a2)
{
int result; // eax

pthread_mutex_init((pthread_mutex_t *)a1, 0LL);
result = pthread_cond_init((pthread_cond_t *)(a1 + 40), 0LL);
*(_DWORD *)(a1 + 92) = a2;
return result;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, int a2)
{
int result; // eax

pthread_mutex_init((pthread_mutex_t *)a1, 0LL);
result = pthread_cond_init((pthread_cond_t *)(a1 + 40), 0LL);
*(_DWORD *)(a1 + 92) = a2;
return result;
}

original_code:
void excstate_init( excstate * b, int state){
  pthread_mutex_init( &b->mutex, ((void *)0) );
  pthread_cond_init( &b->cond, ((void *)0) );
  b->count = state;
}

canonical_original_code:
void excstate_init(struct excstate *b, int state){
  pthread_mutex_init( &b->mutex, ((void *)0) );
  pthread_cond_init( &b->cond, ((void *)0) );
  b->count = state;
}

variable_types: struct excstate { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; unsigned int SIZE; unsigned int count; } *

variable_types: state int


decompiled_code:
int __fastcall excstate_destroy(__int64 a1)
{
pthread_mutex_destroy((pthread_mutex_t *)a1);
return pthread_cond_destroy((pthread_cond_t *)(a1 + 40));
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
pthread_mutex_destroy((pthread_mutex_t *)a1);
return pthread_cond_destroy((pthread_cond_t *)(a1 + 40));
}

original_code:
void excstate_destroy(excstate * b){
  pthread_mutex_destroy( &b->mutex );
  pthread_cond_destroy( &b->cond );
}

canonical_original_code:
void excstate_destroy(struct excstate *b){
  pthread_mutex_destroy( &b->mutex );
  pthread_cond_destroy( &b->cond );
}

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
int __fastcall excstate_set_state(pthread_mutex_t *mutex, unsigned int a2)
{
pthread_mutex_lock(mutex);
mutex[2].__nusers = a2;
pthread_cond_signal((pthread_cond_t *)&mutex[1]);
return pthread_mutex_unlock(mutex);
}

canonical_decompiled_code:
int __fastcall func2(pthread_mutex_t *mutex, unsigned int a2)
{
pthread_mutex_lock(mutex);
mutex[2].__nusers = a2;
pthread_cond_signal((pthread_cond_t *)&mutex[1]);
return pthread_mutex_unlock(mutex);
}

original_code:
void excstate_set_state(excstate *b, int state){
  pthread_mutex_lock(&b->mutex);
  b->count = state;
  pthread_cond_signal( &b->cond );
  pthread_mutex_unlock(&b->mutex);
}

canonical_original_code:
void excstate_set_state(struct excstate *b, int state){
  pthread_mutex_lock(&b->mutex);
  b->count = state;
  pthread_cond_signal( &b->cond );
  pthread_mutex_unlock(&b->mutex);
}


decompiled_code:
__int64 __fastcall excstate_get_state(pthread_mutex_t *mutex)
{
unsigned int v1; // er12

pthread_mutex_lock(mutex);
v1 = mutex[2].__nusers;
pthread_mutex_unlock(mutex);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func3(pthread_mutex_t *mutex)
{
unsigned int v1; // er12

pthread_mutex_lock(mutex);
v1 = mutex[2].__nusers;
pthread_mutex_unlock(mutex);
return v1;
}

original_code:
int excstate_get_state(excstate *b){
  pthread_mutex_lock(&b->mutex);
  int state = b->count;
  pthread_mutex_unlock(&b->mutex);
  return state;
}

canonical_original_code:
int excstate_get_state(struct excstate *b){
  pthread_mutex_lock(&b->mutex);
  int state = b->count;
  pthread_mutex_unlock(&b->mutex);
  return state;
}


decompiled_code:
int __fastcall excstate_wait_running(pthread_mutex_t *mutex)
{
pthread_cond_t *v1; // r12

v1 = (pthread_cond_t *)&mutex[1];
pthread_mutex_lock(mutex);
while ( mutex[2].__nusers != 2 )
pthread_cond_wait(v1, mutex);
pthread_cond_signal(v1);
return pthread_mutex_unlock(mutex);
}

canonical_decompiled_code:
int __fastcall func4(pthread_mutex_t *mutex)
{
pthread_cond_t *v1; // r12

v1 = (pthread_cond_t *)&mutex[1];
pthread_mutex_lock(mutex);
while ( mutex[2].__nusers != 2 )
pthread_cond_wait(v1, mutex);
pthread_cond_signal(v1);
return pthread_mutex_unlock(mutex);
}

original_code:
void excstate_wait_running( excstate * b ){
  pthread_mutex_lock(&b->mutex);
  while (b->count != PENDING){
    pthread_cond_wait(&b->cond,&b->mutex);
  }
  pthread_cond_signal( &b->cond );
  pthread_mutex_unlock(&b->mutex);
}

canonical_original_code:
void excstate_wait_running(struct excstate *b){
  pthread_mutex_lock(&b->mutex);
  while (b->count != PENDING){
    pthread_cond_wait(&b->cond,&b->mutex);
  }
  pthread_cond_signal( &b->cond );
  pthread_mutex_unlock(&b->mutex);
}


main

repo: hackman/ipparser

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
struct in_addr *v3; // rbp

v3 = (struct in_addr *)malloc(8uLL);
parse_ip("11.0.0.0/24", v3);
parse_ip("12.0.0.0/255.255.255.0", v3);
parse_ip("13.1.0.12", v3);
parse_file(v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
struct in_addr *v3; // rbp

v3 = (struct in_addr *)malloc(8uLL);
func9("11.0.0.0/24", v3);
func9("12.0.0.0/255.255.255.0", v3);
func9("13.1.0.12", v3);
func10(v3);
return 0;
}

original_code:
int main() {
 int i;
 char *p[] = { "11.0.0.0/24", "12.0.0.0/255.255.255.0", "13.1.0.12" };
 struct myip *return_ip;
 return_ip = malloc(sizeof(return_ip));
 for(i=0; i<3; i++) {
  parse_ip(p[i], return_ip);
 }
 parse_file(return_ip);
 return 0;
}

variable_types: i int

variable_types:
p
char *[]

variable_types: struct myip { struct in_addr { unsigned int s_addr; } net; struct in_addr mask; } *

user_defined_types: struct myip { struct in_addr net; struct in_addr mask; }


decompiled_code:
void *__fastcall int2bin(int a1)
{
_BYTE *v1; // rax
char v2; // dl
_BYTE *v3; // rdx

end = 0;
v1 = &unk_4040BF;
do
{
v2 = a1;
a1 >>= 1;
*v1 = (v2 & 1) + 48;
v3 = v1--;
}
while ( v3 != (_BYTE *)&ipbuf_0 );
return &ipbuf_0;
}

canonical_decompiled_code:
void *__fastcall func8(int a1)
{
_BYTE *v1; // rax
char v2; // dl
_BYTE *v3; // rdx

end = 0;
v1 = &unk_4040BF;
do
{
v2 = a1;
a1 >>= 1;
*v1 = (v2 & 1) + 48;
v3 = v1--;
}
while ( v3 != (_BYTE *)&ipbuf_0 );
return &ipbuf_0;
}

original_code:
char *int2bin(int a) {
 int i;
 static char ipbuf[32];
 ipbuf[32] = '\0';
    for (i = 32 - 1; i >= 0; i--) {
        ipbuf[i] = (a & 1) + '0';
        a >>= 1;
    }
    return ipbuf;
}

variable_types: a int

variable_types:
ipbuf
char [32]

return_type: char *


decompiled_code:
int __fastcall parse_ip(char *s, struct in_addr *inp)
{
char *v3; // rax
const char *v4; // r12
__int64 v5; // rsi
const char *v6; // rax
_BYTE *v7; // rsi
__int64 v8; // rax
char v9; // cl
char *v10; // r8
int result; // eax
const char *v12; // rax
int v13; // eax
in_addr_t v14; // eax

printf("Buf: %s\n", s);
v3 = strchr(s, 47);
if ( !v3 )
goto LABEL_6;
v4 = v3 + 1;
if ( strchr(v3 + 1, 46) )
{
if ( inet_aton(v4, inp + 1) )
{
v5 = inp[1].s_addr;
LABEL_5:
printf("Mask: %d\n", v5);
v6 = (const char *)int2bin(inp[1].s_addr);
printf("Mask(bin): %s\n", v6);
LABEL_6:
v7 = calloc(0x10uLL, 1uLL);
v8 = 0LL;
LABEL_7:
v9 = s[v8];
v10 = &v7[v8];
if ( v9 == 0 || v9 == 47 )
{
if ( inet_aton(&v10[-(int)v8], inp) )
{
LABEL_14:
printf("IP: %d\n", inp->s_addr);
v12 = (const char *)int2bin(inp->s_addr);
printf("Mask(bin): %s\n", v12);
return puts("--------------------------------");
}
}
else
{
while ( v8 != 16 )
{
if ( v9 == 46 || (unsigned __int8)(v9 - 48) <= 9u )
{
v7[v8++] = v9;
goto LABEL_7;
}
}
if ( inet_aton(v10 - 16, inp) )
goto LABEL_14;
}
puts("Unable to parse the IP");
return 4;
}
puts("Unable to parse the netmask");
result = 1;
}
else
{
v13 = atoi(v4);
if ( (unsigned int)(v13 - 1) <= 0x1F )
{
v14 = _bswap_32(0xFFFFFFFFLL << (32 - (unsigned __int8)v13));
inp[1].s_addr = v14;
v5 = v14;
goto LABEL_5;
}
puts("invalid mask in network/netmask");
result = 2;
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(char *s, struct in_addr *inp)
{
char *v3; // rax
const char *v4; // r12
__int64 v5; // rsi
const char *v6; // rax
_BYTE *v7; // rsi
__int64 v8; // rax
char v9; // cl
char *v10; // r8
int result; // eax
const char *v12; // rax
int v13; // eax
in_addr_t v14; // eax

printf("Buf: %s\n", s);
v3 = strchr(s, 47);
if ( !v3 )
goto LABEL_6;
v4 = v3 + 1;
if ( strchr(v3 + 1, 46) )
{
if ( inet_aton(v4, inp + 1) )
{
v5 = inp[1].s_addr;
LABEL_5:
printf("Mask: %d\n", v5);
v6 = (const char *)func8(inp[1].s_addr);
printf("Mask(bin): %s\n", v6);
LABEL_6:
v7 = calloc(0x10uLL, 1uLL);
v8 = 0LL;
LABEL_7:
v9 = s[v8];
v10 = &v7[v8];
if ( v9 == 0 || v9 == 47 )
{
if ( inet_aton(&v10[-(int)v8], inp) )
{
LABEL_14:
printf("IP: %d\n", inp->s_addr);
v12 = (const char *)func8(inp->s_addr);
printf("Mask(bin): %s\n", v12);
return puts("--------------------------------");
}
}
else
{
while ( v8 != 16 )
{
if ( v9 == 46 || (unsigned __int8)(v9 - 48) <= 9u )
{
v7[v8++] = v9;
goto LABEL_7;
}
}
if ( inet_aton(v10 - 16, inp) )
goto LABEL_14;
}
puts("Unable to parse the IP");
return 4;
}
puts("Unable to parse the netmask");
result = 1;
}
else
{
v13 = atoi(v4);
if ( (unsigned int)(v13 - 1) <= 0x1F )
{
v14 = func7(0xFFFFFFFFLL << (32 - (unsigned __int8)v13));
inp[1].s_addr = v14;
v5 = v14;
goto LABEL_5;
}
puts("invalid mask in network/netmask");
result = 2;
}
return result;
}

original_code:
int parse_ip(char *buf, struct myip *ip) {
  char *s = ((void *)0);
  int ipsize=0;
  printf("Buf: %s\n", buf);
  if ((s = strchr(buf, '/'))) {
   *s++;
   if (strchr(s, '.')) {
    if (inet_aton(s, &ip->mask) == 0) {
     printf("Unable to parse the netmask\n");
     return 1;
    }
   } else {
    int m = 0;
    m = atoi(s);
    if (m > 32 || m <= 0) {
     printf("invalid mask in network/netmask\n");
     return 2;
    }
    ip->mask.s_addr = 0xFFFFFFFFUL << (32 - m);
    ip->mask.s_addr = htonl(ip->mask.s_addr);
   }
   printf("Mask: %d\n", ip->mask.s_addr);
   printf("Mask(bin): %s\n", int2bin((int) ip->mask.s_addr));
  }
  s = ((void *)0);
  s = malloc(16);
  memset(s, '\0', 16);
  while (*buf) {
   if (*buf == '/' || *buf == '\0' || ipsize == 16)
    break;
   if (isdigit(*buf) || *buf == '.') {
    *s = *buf;
    buf++;
    s++;
    ipsize++;
   }
  }
  s = s - ipsize;
  if (inet_aton(s, &ip->net) == 0) {
   printf("Unable to parse the IP\n");
   return 4;
  }
  printf("IP: %d\n", ip->net.s_addr);
  printf("Mask(bin): %s\n", int2bin((int) ip->net.s_addr));
  printf("--------------------------------\n");
}

canonical_original_code:
int parse_ip(char *buf, struct myip *ip) {
  char *s = ((void *)0);
  int ipsize = 0;
  printf("Buf: %s\n", buf);
  if ((s = strchr(buf, '/'))) {
   *s++;
   if (strchr(s, '.')) {
    if (inet_aton(s, &ip->mask) == 0) {
     printf("Unable to parse the netmask\n");
     return 1;
    }
   } else {
    int m = 0;
    m = atoi(s);
    if (m > 32 || m <= 0) {
     printf("invalid mask in network/netmask\n");
     return 2;
    }
    ip->mask.s_addr = 0xFFFFFFFFUL << (32 - m);
    ip->mask.s_addr = htonl(ip->mask.s_addr);
   }
   printf("Mask: %d\n", ip->mask.s_addr);
   printf("Mask(bin): %s\n", int2bin((int) ip->mask.s_addr));
  }
  s = ((void *)0);
  s = malloc(16);
  memset(s, '\0', 16);
  while (*buf) {
   if (*buf == '/' || *buf == '\0' || ipsize == 16)
    break;
   if (isdigit(*buf) || *buf == '.') {
    *s = *buf;
    buf++;
    s++;
    ipsize++;
   }
  }
  s = s - ipsize;
  if (inet_aton(s, &ip->net) == 0) {
   printf("Unable to parse the IP\n");
   return 4;
  }
  printf("IP: %d\n", ip->net.s_addr);
  printf("Mask(bin): %s\n", int2bin((int) ip->net.s_addr));
  printf("--------------------------------\n");
}

variable_types:
buf
char *

variable_types:
s
char *

variable_types: ipsize int

variable_types: m int


repo: hagemt/insect

decompiled_code:
__int64 __fastcall file_type(const char *a1)
{
unsigned int v2[4]; // [rsp+8h] [rbp-10h] BYREF

_file_info(a1, v2, 1);
return v2[0];
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
unsigned int v2[4]; // [rsp+8h] [rbp-10h] BYREF

func0(a1, v2, 1);
return v2[0];
}

original_code:
FileType
file_type(FilePath path)
{
 FileInfo info; if (__file_info(path, &info, FILEIREQ_TYPE)) { };
 return info.file_type;
}

canonical_original_code:
enum file_type_t file_type(char *path)
{
 struct file_info_t info; if (__file_info(path, &info, FILEIREQ_TYPE)) { };
 return info.file_type;
}

variable_types:
path
char *

variable_types: struct file_info_t { enum file_type_t { FILETYPE_UNKNOWN=0, FILETYPE_REGULAR=1, FILETYPE_DIRECTORY=2, FILETYPE_MAX=3 } file_type; enum file_perm_t { FILEPERM_NONE=0, FILEPERM_R=1, FILEPERM_W=2, FILEPERM_X=4, FILEPERM_RW=expr, FILEPERM_RX=expr, FILEPERM_ALL=expr, FILEPERM_MAX=255 } file_perm; }

user_defined_types: struct file_info_t { enum file_type_t file_type; enum file_perm_t file_perm; }


decompiled_code:
int __fastcall file_info(const char *a1, _DWORD *a2)
{
return _file_info(a1, a2, 3);
}

canonical_decompiled_code:
int __fastcall func2(const char *a1, _DWORD *a2)
{
return func0(a1, a2, 3);
}

original_code:
int
file_info(FilePath path, FileInfo *info)
{
 return __file_info(path, info, FILEIREQ_MASK);
}

canonical_original_code:
int file_info(char *path, struct file_info_t *info)
{
 return __file_info(path, info, FILEIREQ_MASK);
}

variable_types: struct file_info_t { enum file_type_t { FILETYPE_UNKNOWN=0, FILETYPE_REGULAR=1, FILETYPE_DIRECTORY=2, FILETYPE_MAX=3 } file_type; enum file_perm_t { FILEPERM_NONE=0, FILEPERM_R=1, FILEPERM_W=2, FILEPERM_X=4, FILEPERM_RW=expr, FILEPERM_RX=expr, FILEPERM_ALL=expr, FILEPERM_MAX=255 } file_perm; } *


decompiled_code:
_BOOL8 __fastcall file_exists(const char *a1)
{
return _file_info(a1, 0LL, 0) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(const char *a1)
{
return func0(a1, 0LL, 0) == 0;
}

original_code:
int
file_exists(FilePath path)
{
 return __file_info(path, ((FileInfo *) 0), FILEIREQ_NONE) == 0;
}

canonical_original_code:
int file_exists(char *path)
{
 return __file_info(path, ((struct file_info_t *) 0), FILEIREQ_NONE) == 0;
}

user_defined_types: enum file_type_t { FILETYPE_UNKNOWN=0, FILETYPE_REGULAR=1, FILETYPE_DIRECTORY=2, FILETYPE_MAX=3 }

user_defined_types: enum file_perm_t { FILEPERM_NONE=0, FILEPERM_R=1, FILEPERM_W=2, FILEPERM_X=4, FILEPERM_RW=expr, FILEPERM_RX=expr, FILEPERM_ALL=expr, FILEPERM_MAX=255 }


decompiled_code:
__int64 __fastcall file_can_read(const char *a1)
{
int v2; // [rsp+8h] [rbp-10h] BYREF
int v3; // [rsp+Ch] [rbp-Ch]

_file_info(a1, &v2, 2);
return v3 & 1;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1)
{
int v2; // [rsp+8h] [rbp-10h] BYREF
int v3; // [rsp+Ch] [rbp-Ch]

func0(a1, &v2, 2);
return v3 & 1;
}

original_code:
int
file_can_read(FilePath path)
{
 FileInfo info; if (__file_info(path, &info, FILEIREQ_PERM)) { };
 return ((info.file_perm & FILEPERM_R) != FILEPERM_NONE);
}

canonical_original_code:
int file_can_read(char *path)
{
 struct file_info_t info; if (__file_info(path, &info, FILEIREQ_PERM)) { };
 return ((info.file_perm & FILEPERM_R) != FILEPERM_NONE);
}


decompiled_code:
__int64 __fastcall file_can_write(const char *a1)
{
_DWORD v2[4]; // [rsp+8h] [rbp-10h] BYREF

_file_info(a1, v2, 2);
return (v2[1] >> 1) & 1;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1)
{
_DWORD v2[4]; // [rsp+8h] [rbp-10h] BYREF

func0(a1, v2, 2);
return (v2[1] >> 1) & 1;
}

original_code:
int
file_can_write(FilePath path)
{
 FileInfo info; if (__file_info(path, &info, FILEIREQ_PERM)) { };
 return ((info.file_perm & FILEPERM_W) != FILEPERM_NONE);
}

canonical_original_code:
int file_can_write(char *path)
{
 struct file_info_t info; if (__file_info(path, &info, FILEIREQ_PERM)) { };
 return ((info.file_perm & FILEPERM_W) != FILEPERM_NONE);
}


decompiled_code:
__int64 __fastcall crawl_worker(__int64 a1)
{
__int64 v2; // rdi

if ( !a1 || (v2 = *(_QWORD *)(a1 + 16)) == 0 )
_assert_fail("thread && thread->input", "test/posix/main_pthread.c", 0x46u, "crawl_worker");
crawl(v2, &remember);
return *(_QWORD *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v2; // rdi

if ( !a1 || (v2 = *(_QWORD *)(a1 + 16)) == 0 )
_assert_fail("thread && thread->input", "test/posix/main_pthread.c", 0x46u, "crawl_worker");
crawl(v2, &remember);
return *(_QWORD *)(a1 + 16);
}

original_code:
static void *
crawl_worker(void *data)
{
 Thread *thread = (Thread *) data;
 ((void) sizeof ((thread && thread->input) ? 1 : 0), __extension__ ({ if (thread && thread->input) ; else __assert_fail ("thread && thread->input", "test/posix/main_pthread.c", 70, __extension__ __PRETTY_FUNCTION__); }));
 (void) crawl((Path) thread->input, &remember);
 return thread->input;
}

canonical_original_code:
static void *crawl_worker(void *data)
{
 struct thread_t *thread = (Thread *) data;
 ((void) sizeof ((thread && thread->input) ? 1 : 0), __extension__ ({ if (thread && thread->input) ; else __assert_fail ("thread && thread->input", "test/posix/main_pthread.c", 70, __extension__ __PRETTY_FUNCTION__); }));
 (void) crawl((char *) thread->input, &remember);
 return thread->input;
}

variable_types:
data
void *

variable_types: struct thread_t { unsigned long id; void *(*worker)(void *); void *input; struct thread_t *next; } *


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
pthread_t v3; // rax
__int64 v4; // rbx
int v5; // er13
_QWORD *v6; // rax
void *v7; // rbp
void *v8; // rbx
void *v9; // rdi

if ( argc <= 1 )
{
v5 = 1;
fprintf(stderr, "[%s] %s (%s)\n", "USAGE", *argv, "path/to/crawl *");
return v5;
}
v3 = pthread_self();
ptr = 0LL;
_threads = v3;
v4 = argc - 1;
reminisce(0LL);
do
{
while ( 1 )
{
if ( !(unsigned int)file_exists(argv[v4]) )
goto LABEL_4;
v6 = malloc(0x28uLL);
v7 = v6;
if ( !v6 )
goto LABEL_4;
v6[1] = crawl_worker;
v6[2] = argv[v4];
v6[4] = ptr;
if ( (unsigned int)spawn_worker_constprop_0(v6) )
break;
ptr = v7;
LABEL_4:
v5 = --v4;
if ( !(_DWORD)v4 )
goto LABEL_9;
}
--v4;
free(v7);
v5 = v4;
}
while ( (_DWORD)v4 );
LABEL_9:
v8 = ptr;
while ( v8 )
{
collect_worker_isra_0((__int64)v8);
v9 = v8;
v8 = (void *)*((_QWORD *)v8 + 4);
free(v9);
}
regurgitate(stderr, 0LL);
return v5;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
pthread_t v3; // rax
__int64 v4; // rbx
int v5; // er13
_QWORD *v6; // rax
void *v7; // rbp
void *v8; // rbx
void *v9; // rdi

if ( argc <= 1 )
{
v5 = 1;
fprintf(stderr, "[%s] %s (%s)\n", "USAGE", *argv, "path/to/crawl *");
return v5;
}
v3 = pthread_self();
ptr = 0LL;
_threads = v3;
v4 = argc - 1;
reminisce(0LL);
do
{
while ( 1 )
{
if ( !(unsigned int)file_exists(argv[v4]) )
goto LABEL_4;
v6 = malloc(0x28uLL);
v7 = v6;
if ( !v6 )
goto LABEL_4;
v6[1] = crawl_worker;
v6[2] = argv[v4];
v6[4] = ptr;
if ( (unsigned int)func1(v6) )
break;
ptr = v7;
LABEL_4:
v5 = --v4;
if ( !(_DWORD)v4 )
goto LABEL_9;
}
--v4;
free(v7);
v5 = v4;
}
while ( (_DWORD)v4 );
LABEL_9:
v8 = ptr;
while ( v8 )
{
func2((__int64)v8);
v9 = v8;
v8 = (void *)*((_QWORD *)v8 + 4);
free(v9);
}
regurgitate(stderr, 0LL);
return v5;
}

original_code:
int
main(int argc, char *argv[])
{
 Thread *current, *next;
 if (argc < 2) {
  (void) fprintf(stderr, "[%s] %s (%s)\n",
    "USAGE", *argv, "path/to/crawl *");
  return 1;
 }
 __threads.id = pthread_self();
 __threads.next = ((void *)0);
 (void) reminisce(((void *)0));
 while (--argc) {
  if (!file_exists(argv[argc])) continue;
  current = malloc(sizeof(Thread));
  if (!current) continue;
  current->worker = &crawl_worker;
  current->input = argv[argc];
  current->next = __threads.next;
  if (spawn_worker(current)) {
   free(current);
  } else {
   __threads.next = current;
  }
 }
 next = __threads.next;
 while ((current = next)) {
  if (collect_worker(current)) {
  }
  next = current->next;
  free(current);
 }
 (void) regurgitate(stderr, ((void *)0));
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 struct thread_t *current; struct thread_t *next;
 if (argc < 2) {
  (void) fprintf(stderr, "[%s] %s (%s)\n",
    "USAGE", *argv, "path/to/crawl *");
  return 1;
 }
 __threads.id = pthread_self();
 __threads.next = ((void *)0);
 (void) reminisce(((void *)0));
 while (--argc) {
  if (!file_exists(argv[argc])) continue;
  current = malloc(sizeof(Thread));
  if (!current) continue;
  current->worker = &crawl_worker;
  current->input = argv[argc];
  current->next = __threads.next;
  if (spawn_worker(current)) {
   free(current);
  } else {
   __threads.next = current;
  }
 }
 next = __threads.next;
 while ((current = next)) {
  if (collect_worker(current)) {
  }
  next = current->next;
  free(current);
 }
 (void) regurgitate(stderr, ((void *)0));
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

user_defined_types: struct thread_t { unsigned long id; void *(*worker)(void *); void *input; struct thread_t *next; }


crawl

decompiled_code:
__int64 __fastcall crawl(__int64 a1, __int64 (__fastcall *a2)(__int64))
{
int v2; // eax
__int64 result; // rax

if ( !a1 )
_assert_fail("path", "core/file_crawler.c", 0xDu, "crawl");
v2 = file_type();
if ( v2 == 1 )
{
if ( !a2 )
return 0LL;
result = a2(a1);
}
else
{
if ( v2 != 2 )
return 0LL;
result = _expand(a1, a2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 (__fastcall *a2)(__int64))
{
int v2; // eax
__int64 result; // rax

if ( !a1 )
_assert_fail("path", "core/file_crawler.c", 0xDu, "crawl");
v2 = file_type();
if ( v2 == 1 )
{
if ( !a2 )
return 0LL;
result = a2(a1);
}
else
{
if ( v2 != 2 )
return 0LL;
result = func1(a1, a2);
}
return result;
}

original_code:
int
crawl(Path path, Crawlback callback)
{
 ((void) sizeof ((path) ? 1 : 0), __extension__ ({ if (path) ; else __assert_fail ("path", "core/file_crawler.c", 13, __extension__ __PRETTY_FUNCTION__); }));
 switch (file_type(path)) {
 case FILETYPE_DIRECTORY:
  return __expand(path, callback);
 case FILETYPE_REGULAR:
  if (callback) return (*callback)(path);
 default:
  break;
 }
 return 0;
}

canonical_original_code:
int crawl(char *path, int (*callback)(char *))
{
 ((void) sizeof ((path) ? 1 : 0), __extension__ ({ if (path) ; else __assert_fail ("path", "core/file_crawler.c", 13, __extension__ __PRETTY_FUNCTION__); }));
 switch (file_type(path)) {
 case FILETYPE_DIRECTORY:
  return __expand(path, callback);
 case FILETYPE_REGULAR:
  if (callback) return (*callback)(path);
 default:
  break;
 }
 return 0;
}

variable_types:
callback
int (*)(char *)


main

repo: hagemt/teaching

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

original_code:
int main(void) {
 float fzero, x;
 fzero = (0.0f);
 x = (0.0f) / fzero;
 return __builtin_isnan (x);
}

canonical_original_code:
int main() {
 float fzero; float x;
 fzero = (0.0f);
 x = (0.0f) / fzero;
 return __builtin_isnan (x);
}

variable_types: fzero float

variable_types: x float


daemonize

repo: hahiserw/lhttp

decompiled_code:
int daemonize()
{
__pid_t v0; // eax
int v1; // edi
int result; // eax

v0 = fork();
if ( v0 < 0 )
{
fprintf(stderr, "%s: cannot fork\n", program_name);
LABEL_7:
v1 = 1;
LABEL_8:
exit(v1);
}
v1 = 0;
if ( v0 )
goto LABEL_8;
umask(0);
if ( setsid() < 0 )
{
fprintf(stderr, "%s: cannot set session\n", program_name);
goto LABEL_7;
}
result = chdir("/");
if ( result < 0 )
{
fprintf(stderr, "%s: cannot change working directory\n", program_name);
goto LABEL_7;
}
return result;
}

canonical_decompiled_code:
int func0()
{
__pid_t v0; // eax
int v1; // edi
int result; // eax

v0 = fork();
if ( v0 < 0 )
{
fprintf(stderr, "%s: cannot fork\n", program_name);
LABEL_7:
v1 = 1;
LABEL_8:
exit(v1);
}
v1 = 0;
if ( v0 )
goto LABEL_8;
umask(0);
if ( setsid() < 0 )
{
fprintf(stderr, "%s: cannot set session\n", program_name);
goto LABEL_7;
}
result = chdir("/");
if ( result < 0 )
{
fprintf(stderr, "%s: cannot change working directory\n", program_name);
goto LABEL_7;
}
return result;
}

original_code:
void daemonize()
{
 pid_t child, session;
 child = fork();
 if (child < 0) {
  fprintf(stderr, "%s: cannot fork\n", program_name);
  exit(1);
 }
 if (child > 0)
  exit(0);
 umask(0);
 session = setsid();
 if (session < 0) {
  fprintf(stderr, "%s: cannot set session\n", program_name);
  exit(1);
 }
 if (chdir("/") < 0) {
  fprintf(stderr, "%s: cannot change working directory\n",
   program_name);
  exit(1);
 }
}

canonical_original_code:
void daemonize()
{
 int child; int session;
 child = fork();
 if (child < 0) {
  fprintf(stderr, "%s: cannot fork\n", program_name);
  exit(1);
 }
 if (child > 0)
  exit(0);
 umask(0);
 session = setsid();
 if (session < 0) {
  fprintf(stderr, "%s: cannot set session\n", program_name);
  exit(1);
 }
 if (chdir("/") < 0) {
  fprintf(stderr, "%s: cannot change working directory\n",
   program_name);
  exit(1);
 }
}

variable_types: child int

variable_types: session int


decompiled_code:
int close_stds()
{
close(0);
close(1);
return close(2);
}

canonical_decompiled_code:
int func1()
{
close(0);
close(1);
return close(2);
}

original_code:
void close_stds() {
 close(0);
 close(1);
 close(2);
}


decompiled_code:
__sighandler_t prepare_signals()
{
signal(2, handle_signal);
return signal(15, handle_signal);
}

canonical_decompiled_code:
__sighandler_t func2()
{
signal(2, handle_signal);
return signal(15, handle_signal);
}

original_code:
void prepare_signals()
{
 signal(2, handle_signal);
 signal(15, handle_signal);
}


usage

decompiled_code:
void __noreturn usage()
{
fprintf(
stderr,
"Usage: %s [-h] [-v] [-f] [-t temp_dir] [-l log_file] [-d working_directory] [ip[:port]]\n",
(const char *)program_name);
fputc(10, stderr);
fwrite("Listen for http requests at ip[:port] and serve files from working_directory\n", 1uLL, 0x4DuLL, stderr);
fputc(10, stderr);
fwrite(" -h Display this helpful text and exit\n", 1uLL, 0x29uLL, stderr);
fwrite(" -v Verbosity level, more `v', more verbose\n", 1uLL, 0x2EuLL, stderr);
fwrite(" -f Stay in foreground (don't demonize, disables -l)\n", 1uLL, 0x37uLL, stderr);
fwrite(" -t Temporary directory for lhttpd files (default: /tmp)\n", 1uLL, 0x3BuLL, stderr);
fwrite(" -l File where all messages are written to (default: /tmp/lhttpd.log)\n", 1uLL, 0x48uLL, stderr);
fwrite(" -d Root directory for http files (default: .)\n", 1uLL, 0x31uLL, stderr);
fputc(10, stderr);
fwrite(" If no IP is specified 0.0.0.0 is used\n", 1uLL, 0x28uLL, stderr);
fwrite(" Default port is 80\n", 1uLL, 0x15uLL, stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func3()
{
fprintf(
stderr,
"Usage: %s [-h] [-v] [-f] [-t temp_dir] [-l log_file] [-d working_directory] [ip[:port]]\n",
(const char *)program_name);
fputc(10, stderr);
fwrite("Listen for http requests at ip[:port] and serve files from working_directory\n", 1uLL, 0x4DuLL, stderr);
fputc(10, stderr);
fwrite(" -h Display this helpful text and exit\n", 1uLL, 0x29uLL, stderr);
fwrite(" -v Verbosity level, more `v', more verbose\n", 1uLL, 0x2EuLL, stderr);
fwrite(" -f Stay in foreground (don't demonize, disables -l)\n", 1uLL, 0x37uLL, stderr);
fwrite(" -t Temporary directory for lhttpd files (default: /tmp)\n", 1uLL, 0x3BuLL, stderr);
fwrite(" -l File where all messages are written to (default: /tmp/lhttpd.log)\n", 1uLL, 0x48uLL, stderr);
fwrite(" -d Root directory for http files (default: .)\n", 1uLL, 0x31uLL, stderr);
fputc(10, stderr);
fwrite(" If no IP is specified 0.0.0.0 is used\n", 1uLL, 0x28uLL, stderr);
fwrite(" Default port is 80\n", 1uLL, 0x15uLL, stderr);
exit(1);
}

original_code:
void usage()
{
 fprintf(stderr, "Usage: %s [-h] [-v] [-f] [-t temp_dir] [-l log_file]"
  " [-d working_directory] [ip[:port]]\n", program_name);
 fprintf(stderr, "\n");
 fprintf(stderr, "Listen for http requests at ip[:port] and serve files"
  " from working_directory\n");
 fprintf(stderr, "\n");
 fprintf(stderr, "  -h  Display this helpful text and exit\n");
 fprintf(stderr, "  -v  Verbosity level, more `v', more verbose\n");
 fprintf(stderr, "  -f  Stay in foreground"
  " (don't demonize, disables -l)\n");
 fprintf(stderr, "  -t  Temporary directory for lhttpd files"
  " (default: /tmp)\n");
 fprintf(stderr, "  -l  File where all messages are written to"
  " (default: /tmp/lhttpd.log)\n");
 fprintf(stderr, "  -d  Root directory for http files (default: .)\n");
 fprintf(stderr, "\n");
 fprintf(stderr, "  If no IP is specified 0.0.0.0 is used\n");
 fprintf(stderr, "  Default port is 80\n");
 exit(1);
}


decompiled_code:
__int64 __fastcall parse_args(int argc, char **argv)
{
int v2; // eax
__int64 result; // rax
size_t v4; // rax

while ( 1 )
{
v2 = getopt(argc, argv, "hvft:l:d:3");
if ( v2 == -1 )
break;
if ( v2 == 51 )
{
leon = 1;
}
else
{
switch ( v2 )
{
case 'd':
base_path = optarg;
break;
case 'f':
foreground = 1;
break;
case 'l':
log_file = optarg;
break;
case 't':
temp_dir = optarg;
break;
case 'v':
++verbosity;
break;
default:
usage();
return result;
}
}
}
result = optind;
if ( optind < argc )
{
v4 = strlen(argv[optind]);
address = (char *)malloc(v4 + 1);
result = _isoc99_sscanf(argv[optind], "%[^:]:%s", address, &port);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(int argc, char **argv)
{
int v2; // eax
__int64 result; // rax
size_t v4; // rax

while ( 1 )
{
v2 = getopt(argc, argv, "hvft:l:d:3");
if ( v2 == -1 )
break;
if ( v2 == 51 )
{
leon = 1;
}
else
{
switch ( v2 )
{
case 'd':
base_path = optarg;
break;
case 'f':
foreground = 1;
break;
case 'l':
log_file = optarg;
break;
case 't':
temp_dir = optarg;
break;
case 'v':
++verbosity;
break;
default:
func3();
return result;
}
}
}
result = optind;
if ( optind < argc )
{
v4 = strlen(argv[optind]);
address = (char *)malloc(v4 + 1);
result = _isoc99_sscanf(argv[optind], "%[^:]:%s", address, &port);
}
return result;
}

original_code:
void parse_args(int argc, char *argv[])
{
 int option;
 while ((option = getopt(argc, argv, "hvft:l:d:3")) != -1) {
  switch (option) {
  case 'v':
   verbosity++;
   break;
  case 'f':
   foreground = 1;
   break;
  case 't':
   temp_dir = optarg;
   break;
  case 'l':
   log_file = optarg;
   break;
  case 'd':
   base_path = optarg;
   break;
  case '3':
   leon = 1;
   break;
  case 'h':
  default:
   usage();
  }
 }
 if (optind < argc) {
  address = (char *)malloc(strlen(argv[optind]) + 1);
  sscanf(argv[optind], "%[^:]:%s", address, port);
 }
}

variable_types: option int


decompiled_code:
void __fastcall handle_signal(unsigned int a1)
{
if ( a1 == 2 || a1 == 15 )
{
close(server);
log_message(1u, "* Server terminated");
exit(1);
}
log_message(1u, "Got signal nr %i, ignoring", a1);
}

canonical_decompiled_code:
void __fastcall func6(unsigned int a1)
{
if ( a1 == 2 || a1 == 15 )
{
close(server);
func5(1u, "* Server terminated");
exit(1);
}
func5(1u, "Got signal nr %i, ignoring", a1);
}

original_code:
void handle_signal(int number)
{
 switch (number) {
 case 2:
 case 15:
  close(server);
  log_message(WARN, "* Server terminated");
  exit(1);
 default:
  log_message(WARN, "Got signal nr %i, ignoring", number);
 }
}

variable_types: number int


die

decompiled_code:
void __fastcall __noreturn die(const char *a1)
{
unsigned int *v1; // rax
unsigned int v2; // ebp
unsigned int *v3; // rbx

v1 = (unsigned int *)_errno_location();
v2 = *v1;
v3 = v1;
log_message(0, "* %s (errno: %i) Exiting", a1, *v1);
*v3 = v2;
if ( foreground )
perror("die");
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(const char *a1)
{
unsigned int *v1; // rax
unsigned int v2; // ebp
unsigned int *v3; // rbx

v1 = (unsigned int *)_errno_location();
v2 = *v1;
v3 = v1;
func5(0, "* %s (errno: %i) Exiting", a1, *v1);
*v3 = v2;
if ( foreground )
perror("die");
exit(1);
}

original_code:
void die(const char *reason)
{
 int errno_temp = (*__errno_location ());
 log_message(ERROR, "* %s (errno: %i) Exiting", reason, (*__errno_location ()));
 (*__errno_location ()) = errno_temp;
 if (foreground)
  perror("die");
 exit(1);
}

canonical_original_code:
void die(char *reason)
{
 int errno_temp = (*__errno_location ());
 log_message(ERROR, "* %s (errno: %i) Exiting", reason, (*__errno_location ()));
 (*__errno_location ()) = errno_temp;
 if (foreground)
  perror("die");
 exit(1);
}

variable_types:
reason
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
program_name = (__int64)*argv;
parse_args(argc, (char **)argv);
if ( !foreground )
daemonize();
log_message(2u, "*");
log_message(2u, "* lhttpd started");
if ( !foreground )
close_stds();
prepare_signals();
log_message(5u, "Opening socket");
create_server(address, &port);
accept_connections();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
program_name = (__int64)*argv;
func4(argc, (char **)argv);
if ( !foreground )
func0();
func5(2u, "*");
func5(2u, "* lhttpd started");
if ( !foreground )
func1();
func2();
func5(5u, "Opening socket");
create_server(address, &port);
accept_connections();
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 program_name = argv[0];
 parse_args(argc, argv);
 if (!foreground)
  daemonize();
 log_message(NOTICE, "*");
 log_message(NOTICE, "* lhttpd started");
 if (!foreground)
  close_stds();
 prepare_signals();
 log_message(DEBUG1, "Opening socket");
 create_server(address, port);
 accept_connections();
}


decompiled_code:
int __fastcall create_server(const char *a1, const char *a2)
{
int v2; // edi
int optval; // [rsp+4h] [rbp-24h] BYREF
struct addrinfo *pai; // [rsp+8h] [rbp-20h] BYREF

if ( getaddrinfo(a1, a2, 0LL, &pai) )
die("Wrong ip address");
log_message(5LL, "Server listening on %s:%s", a1, a2);
server = socket(pai->ai_family, pai->ai_socktype, pai->ai_protocol);
v2 = server;
if ( server < 0 )
{
die("Error opening socket");
v2 = server;
}
optval = 1;
setsockopt(v2, 1, 2, &optval, 4u);
if ( bind(server, pai->ai_addr, pai->ai_addrlen) < 0 )
die("Error binding server to address");
return listen(server, 5);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1, const char *a2)
{
int v2; // edi
int optval; // [rsp+4h] [rbp-24h] BYREF
struct addrinfo *pai; // [rsp+8h] [rbp-20h] BYREF

if ( getaddrinfo(a1, a2, 0LL, &pai) )
die("Wrong ip address");
log_message(5LL, "Server listening on %s:%s", a1, a2);
server = socket(pai->ai_family, pai->ai_socktype, pai->ai_protocol);
v2 = server;
if ( server < 0 )
{
die("Error opening socket");
v2 = server;
}
optval = 1;
setsockopt(v2, 1, 2, &optval, 4u);
if ( bind(server, pai->ai_addr, pai->ai_addrlen) < 0 )
die("Error binding server to address");
return listen(server, 5);
}

original_code:
void create_server(char *address_string, char *port)
{
 struct addrinfo *addr;
 if (getaddrinfo(address_string, port, ((void *)0), &addr))
  die("Wrong ip address");
 log_message(DEBUG1, "Server listening on %s:%s", address_string, port);
 server = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
 if (server < 0)
  die("Error opening socket");
  int sockoptval = 1;
  setsockopt(server, 1, 2, &sockoptval,
   sizeof(int));
 if (bind(server, addr->ai_addr, addr->ai_addrlen) < 0)
  die("Error binding server to address");
 listen(server, 5);
}

variable_types:
port
char *

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

variable_types: sockoptval int

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


decompiled_code:
void __noreturn accept_connections()
{
socklen_t addr_len; // [rsp+4h] [rbp-34h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-30h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF
char buf[24]; // [rsp+20h] [rbp-18h] BYREF

addr_len = 16;
while ( 1 )
{
do
client = accept(server, &addr, &addr_len);
while ( client < 0 );
if ( !(unsigned int)inet_ntop(addr.sa_family, &addr.sa_data[2], buf, 0x10u) )
strcpy(buf, "[ip error]");
log_message(4LL, "Client %s connected", buf);
if ( pthread_create(&newthread, 0LL, handle_connection, 0LL) )
die("Error creating thread");
}
}

canonical_decompiled_code:
void __noreturn func1()
{
socklen_t addr_len; // [rsp+4h] [rbp-34h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-30h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF
char buf[24]; // [rsp+20h] [rbp-18h] BYREF

addr_len = 16;
while ( 1 )
{
do
client = accept(server, &addr, &addr_len);
while ( client < 0 );
if ( !(unsigned int)inet_ntop(addr.sa_family, &addr.sa_data[2], buf, 0x10u) )
strcpy(buf, "[ip error]");
log_message(4LL, "Client %s connected", buf);
if ( pthread_create(&newthread, 0LL, handle_connection, 0LL) )
die("Error creating thread");
}
}

original_code:
void accept_connections()
{
 pthread_t client_thread;
 struct sockaddr_in client_addr;
 socklen_t client_addr_length = sizeof(client_addr);
 char client_ip[16];
 while (1) {
  client = accept(server,
   (struct sockaddr *)&client_addr, &client_addr_length);
  if (client < 0) {
   continue;
  }
  if (!inet_ntop(client_addr.sin_family,
   &client_addr.sin_addr,
   client_ip, 16))
   strcpy(client_ip, "[ip error]");
  log_message(CONNECTION, "Client %s connected", client_ip);
  if (pthread_create(&client_thread, ((void *)0),
   (void *)handle_connection, ((void *)0) ))
   die("Error creating thread");
 }
}

canonical_original_code:
void accept_connections()
{
 unsigned long client_thread;
 struct sockaddr_in client_addr;
 unsigned int client_addr_length = sizeof(client_addr);
 char client_ip[16];
 while (1) {
  client = accept(server,
   (struct sockaddr *)&client_addr, &client_addr_length);
  if (client < 0) {
   continue;
  }
  if (!inet_ntop(client_addr.sin_family,
   &client_addr.sin_addr,
   client_ip, 16))
   strcpy(client_ip, "[ip error]");
  log_message(CONNECTION, "Client %s connected", client_ip);
  if (pthread_create(&client_thread, ((void *)0),
   (void *)handle_connection, ((void *)0) ))
   die("Error creating thread");
 }
}

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

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

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


decompiled_code:
__int64 __fastcall another_line_from_buffer(_BYTE *a1, char **a2)
{
char *v2; // rax
unsigned int v3; // er8
char v4; // dl

v2 = *a2;
v3 = 0;
v4 = **a2;
if ( !v4 )
return 0xFFFFFFFFLL;
while ( v4 == 13 )
{
if ( v2[1] == 10 )
{
*a2 = v2 + 2;
goto LABEL_10;
}
LABEL_4:
++a1;
++v3;
*a2 = v2 + 1;
*(a1 - 1) = *v2;
v2 = *a2;
v4 = **a2;
if ( !v4 )
return 0xFFFFFFFFLL;
}
if ( v4 != 10 )
goto LABEL_4;
*a2 = v2 + 1;
LABEL_10:
*a1 = 0;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(_BYTE *a1, char **a2)
{
char *v2; // rax
unsigned int v3; // er8
char v4; // dl

v2 = *a2;
v3 = 0;
v4 = **a2;
if ( !v4 )
return 0xFFFFFFFFLL;
while ( v4 == 13 )
{
if ( v2[1] == 10 )
{
*a2 = v2 + 2;
goto LABEL_10;
}
LABEL_4:
++a1;
++v3;
*a2 = v2 + 1;
*(a1 - 1) = *v2;
v2 = *a2;
v4 = **a2;
if ( !v4 )
return 0xFFFFFFFFLL;
}
if ( v4 != 10 )
goto LABEL_4;
*a2 = v2 + 1;
LABEL_10:
*a1 = 0;
return v3;
}

original_code:
int another_line_from_buffer(char *line, char **pointer)
{
 int chars = 0;
 while (1) {
  if (**pointer == '\0') {
   return -1;
  } else if (**pointer == '\r' && *(*pointer+1) == '\n') {
   *pointer += 2;
   break;
  } else if (**pointer == '\n') {
   ++*pointer;
   break;
  }
  *line++ = *(*pointer)++;
  ++chars;
 }
 *line = '\0';
 return chars;
}

variable_types:
line
char *

variable_types:
pointer
char **

variable_types: chars int


decompiled_code:
__int64 __fastcall parse_line(_DWORD *a1, const char *a2, __int64 a3)
{
size_t v5; // rdi
signed __int64 v6; // rax
void *v7; // rsp
void *v8; // rsp
__int64 result; // rax
void *v10; // rax
__int64 v11; // [rsp+0h] [rbp-30h]
__int64 v12; // [rsp+0h] [rbp-30h]
char v13[32]; // [rsp+10h] [rbp-20h] BYREF

v5 = strlen(a2);
v6 = 16 * ((unsigned __int64)((int)v5 + 1 + 15LL) >> 4);
v7 = alloca(v6);
v8 = alloca(v6);
result = (unsigned int)*a1;
if ( (_DWORD)result )
{
if ( (_DWORD)result == 1 )
{
if ( *a2 )
{
if ( (unsigned int)_isoc99_sscanf(a2, "%[^:]: %[^\r\n]", v13, v13) == 2 )
result = log_message(6LL, "Header: %s: %s", v13, v13);
else
result = log_message(5LL, "Wrong header?\n> %s", a2);
}
else
{
*a1 = 2;
}
}
}
else
{
v10 = malloc(v5 + 1);
*(_QWORD *)(a3 + 16) = v10;
_isoc99_sscanf(a2, "%s %s %[^/]/%i.%i", a3, v10, a3 + 24, a3 + 36, a3 + 40);
LODWORD(v11) = *(_DWORD *)(a3 + 40);
log_message(
4LL,
"%s %s %s/%i.%i",
(const char *)a3,
*(const char **)(a3 + 16),
(const char *)(a3 + 24),
*(unsigned int *)(a3 + 36),
v11);
result = v12;
++*a1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_DWORD *a1, const char *a2, __int64 a3)
{
size_t v5; // rdi
signed __int64 v6; // rax
void *v7; // rsp
void *v8; // rsp
__int64 result; // rax
void *v10; // rax
__int64 v11; // [rsp+0h] [rbp-30h]
__int64 v12; // [rsp+0h] [rbp-30h]
char v13[32]; // [rsp+10h] [rbp-20h] BYREF

v5 = strlen(a2);
v6 = 16 * ((unsigned __int64)((int)v5 + 1 + 15LL) >> 4);
v7 = alloca(v6);
v8 = alloca(v6);
result = (unsigned int)*a1;
if ( (_DWORD)result )
{
if ( (_DWORD)result == 1 )
{
if ( *a2 )
{
if ( (unsigned int)_isoc99_sscanf(a2, "%[^:]: %[^\r\n]", v13, v13) == 2 )
result = log_message(6LL, "Header: %s: %s", v13, v13);
else
result = log_message(5LL, "Wrong header?\n> %s", a2);
}
else
{
*a1 = 2;
}
}
}
else
{
v10 = malloc(v5 + 1);
*(_QWORD *)(a3 + 16) = v10;
_isoc99_sscanf(a2, "%s %s %[^/]/%i.%i", a3, v10, a3 + 24, a3 + 36, a3 + 40);
LODWORD(v11) = *(_DWORD *)(a3 + 40);
log_message(
4LL,
"%s %s %s/%i.%i",
(const char *)a3,
*(const char **)(a3 + 16),
(const char *)(a3 + 24),
*(unsigned int *)(a3 + 36),
v11);
result = v12;
++*a1;
}
return result;
}

original_code:
int parse_line(enum line_type *type, char *line, struct request_data *request)
{
 int size_temp = strlen(line) + 1;
 char temp[size_temp];
 char temp2[size_temp];
 switch (*type) {
 case REQUEST:
  request->url = (char *)malloc(strlen(line) + 1);
  sscanf(line, "%s %s %[^/]/%i.%i",
   &request->method, request->url, &request->magic,
   &request->major, &request->minor);
  log_message(CONNECTION, "%s %s %s/%i.%i",
   request->method, request->url, request->magic,
   request->major, request->minor);
  ++*type;
  break;
 case HEADER:
  if (strlen(line) == 0)
   ++*type;
  else if (sscanf(line, "%[^:]: %[^\r\n]", &temp, &temp2) == 2)
   log_message(DEBUG2, "Header: %s: %s", temp, temp2);
  else
   log_message(DEBUG1, "Wrong header?\n> %s", line);
  break;
 case BODY:
 case END:
  break;
 }
}

canonical_original_code:
int parse_line(enum line_type *type, char *line, struct request_data *request)
{
 int size_temp = strlen(line) + 1;
 char temp[-1];
 char temp2[-1];
 switch (*type) {
 case REQUEST:
  request->url = (char *)malloc(strlen(line) + 1);
  sscanf(line, "%s %s %[^/]/%i.%i",
   &request->method, request->url, &request->magic,
   &request->major, &request->minor);
  log_message(CONNECTION, "%s %s %s/%i.%i",
   request->method, request->url, request->magic,
   request->major, request->minor);
  ++*type;
  break;
 case HEADER:
  if (strlen(line) == 0)
   ++*type;
  else if (sscanf(line, "%[^:]: %[^\r\n]", &temp, &temp2) == 2)
   log_message(DEBUG2, "Header: %s: %s", temp, temp2);
  else
   log_message(DEBUG1, "Wrong header?\n> %s", line);
  break;
 case BODY:
 case END:
  break;
 }
}

variable_types: enum line_type { REQUEST=0, HEADER=1, BODY=2, END=3 } *

variable_types: struct request_data { char method[10]; char *url; char magic[10]; int major; } *

variable_types:
temp
char [-1]


decompiled_code:
__int64 __fastcall clear_buffer_to_eol(char *a1)
{
char *v1; // rax
char *v2; // rbx
char *v3; // rbp
char v4; // cl
char *v5; // rax
_BYTE *v6; // rdx
int v7; // esi
unsigned int v8; // er8
char v9; // cl

v1 = 0LL;
v2 = a1;
while ( 1 )
{
v3 = v1;
v1 = strchr(a1 + 1, 10);
if ( !v1 )
break;
a1 = v1;
}
if ( !v3 )
return (unsigned int)-1;
v4 = v3[1];
v5 = v3 + 2;
v6 = v2 + 1;
*v2 = v4;
if ( v4 )
{
v7 = 1 - (_DWORD)v5;
do
{
v8 = v7 + (_DWORD)v5;
v9 = *v5++;
*v6++ = v9;
}
while ( v9 );
}
else
{
v8 = 0;
}
*v6 = 0;
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1)
{
char *v1; // rax
char *v2; // rbx
char *v3; // rbp
char v4; // cl
char *v5; // rax
_BYTE *v6; // rdx
int v7; // esi
unsigned int v8; // er8
char v9; // cl

v1 = 0LL;
v2 = a1;
while ( 1 )
{
v3 = v1;
v1 = strchr(a1 + 1, 10);
if ( !v1 )
break;
a1 = v1;
}
if ( !v3 )
return (unsigned int)-1;
v4 = v3[1];
v5 = v3 + 2;
v6 = v2 + 1;
*v2 = v4;
if ( v4 )
{
v7 = 1 - (_DWORD)v5;
do
{
v8 = v7 + (_DWORD)v5;
v9 = *v5++;
*v6++ = v9;
}
while ( v9 );
}
else
{
v8 = 0;
}
*v6 = 0;
return v8;
}

original_code:
int clear_buffer_to_eol(char *buffer)
{
 int characters = 0;
 char *data = buffer;
 char *rest_start = ((void *)0);
 char *temp = data;
 while (temp = strstr(++temp, "\n")) {
  rest_start = temp;
 }
 if (rest_start == ((void *)0))
  return -1;
 ++rest_start;
 while (*data++ = *rest_start++)
  ++characters;
 *data = '\0';
 return characters;
}

variable_types:
buffer
char *

variable_types: characters int

variable_types:
data
char *

variable_types:
temp
char *


decompiled_code:
void __fastcall handle_connection(void *a1)
{
unsigned int v1; // er13
char *v2; // rax
int v3; // edi
char *v4; // r12
int v5; // ebx
size_t v6; // rax
char *v7; // rax
char *v8; // rbp
char *i; // rbp
int v10; // [rsp+4h] [rbp-104h] BYREF
char *v11; // [rsp+8h] [rbp-100h] BYREF
char v12[16]; // [rsp+10h] [rbp-F8h] BYREF
void *ptr; // [rsp+20h] [rbp-E8h]
char buf[200]; // [rsp+40h] [rbp-C8h] BYREF

v1 = 0;
v2 = (char *)malloc(0x5DCuLL);
v3 = client;
*v2 = 0;
v4 = v2;
v11 = v2;
v10 = 0;
v5 = read(v3, buf, 0x96uLL);
while ( 1 )
{
++v1;
v6 = strlen(v4);
v7 = stpcpy(&v4[v6], buf);
v11 = v4;
v8 = v7;
if ( strchr(v4, 10) )
break;
if ( v5 != 150 )
goto LABEL_11;
LABEL_4:
v5 = read(client, buf, 0x96uLL);
if ( (int)(150 * v1) > 1500 )
{
log_message(5LL, "Reallocating buffer for %i*%i bytes", v1, 150LL);
if ( !realloc(v4, 150LL * (int)v1) )
connection_die(500LL, "Cannot realloc. Too long request line?");
}
}
for ( i = (char *)malloc(v8 - v4 + 1); (int)another_line_from_buffer(i, &v11) >= 0; parse_line(&v10, i, (__int64)v12) )
;
v1 = 0;
clear_buffer_to_eol(v4);
free(i);
if ( v5 == 150 )
goto LABEL_4;
LABEL_11:
log_message(5LL, "End of request");
free(v4);
parse_request(v12);
close(client);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func5(void *a1)
{
unsigned int v1; // er13
char *v2; // rax
int v3; // edi
char *v4; // r12
int v5; // ebx
size_t v6; // rax
char *v7; // rax
char *v8; // rbp
char *i; // rbp
int v10; // [rsp+4h] [rbp-104h] BYREF
char *v11; // [rsp+8h] [rbp-100h] BYREF
char v12[16]; // [rsp+10h] [rbp-F8h] BYREF
void *ptr; // [rsp+20h] [rbp-E8h]
char buf[200]; // [rsp+40h] [rbp-C8h] BYREF

v1 = 0;
v2 = (char *)malloc(0x5DCuLL);
v3 = client;
*v2 = 0;
v4 = v2;
v11 = v2;
v10 = 0;
v5 = read(v3, buf, 0x96uLL);
while ( 1 )
{
++v1;
v6 = strlen(v4);
v7 = stpcpy(&v4[v6], buf);
v11 = v4;
v8 = v7;
if ( strchr(v4, 10) )
break;
if ( v5 != 150 )
goto LABEL_11;
LABEL_4:
v5 = read(client, buf, 0x96uLL);
if ( (int)(150 * v1) > 1500 )
{
log_message(5LL, "Reallocating buffer for %i*%i bytes", v1, 150LL);
if ( !realloc(v4, 150LL * (int)v1) )
connection_die(500LL, "Cannot realloc. Too long request line?");
}
}
for ( i = (char *)malloc(v8 - v4 + 1); (int)func2(i, &v11) >= 0; func3(&v10, i, (__int64)v12) )
;
v1 = 0;
func4(v4);
free(i);
if ( v5 == 150 )
goto LABEL_4;
LABEL_11:
log_message(5LL, "End of request");
free(v4);
parse_request(v12);
close(client);
free(ptr);
}

original_code:
void handle_connection()
{
 int buffer_size = 1500;
 int chunk_size = 150;
 char *buffer = (char *)malloc(sizeof(char) * buffer_size);
 *buffer = 0;
 char chunk[chunk_size];
 int chunks = 0;
 int read_length = 0;
 enum line_type type = REQUEST;
 struct request_data request;
 char *line_start = buffer;
 int chars;
 char *line;
 while (1) {
  read_length = read(client,
   chunk, chunk_size);
  if (chunks * chunk_size > buffer_size) {
   log_message(DEBUG1,
    "Reallocating buffer for %i*%i bytes",
    chunks, chunk_size);
   if (realloc(buffer,
    sizeof(char) * chunks * chunk_size) == ((void *)0))
    connection_die(500, "Cannot realloc."
     " Too long request line?");
  }
  strcat(buffer, chunk);
  ++chunks;
  line_start = buffer;
  if (strstr(buffer, "\n")) {
   line = (char *)malloc(strlen(buffer) + 1);
   while (1) {
    chars = another_line_from_buffer(
     line, &line_start);
    if (chars < 0)
     break;
    parse_line(&type, line, &request);
   }
   clear_buffer_to_eol(buffer);
   chunks = 0;
   free(line);
  }
  if (read_length != chunk_size)
   break;
 }
 log_message(DEBUG1, "End of request");
 free(buffer);
 parse_request(&request);
 close(client);
 free(request.url);
}

canonical_original_code:
void handle_connection()
{
 int buffer_size = 1500;
 int chunk_size = 150;
 char *buffer = (char *)malloc(sizeof(char) * buffer_size);
 *buffer = 0;
 char chunk[-1];
 int chunks = 0;
 int read_length = 0;
 enum line_type type = REQUEST;
 struct request_data request;
 char *line_start = buffer;
 int chars;
 char *line;
 while (1) {
  read_length = read(client,
   chunk, chunk_size);
  if (chunks * chunk_size > buffer_size) {
   log_message(DEBUG1,
    "Reallocating buffer for %i*%i bytes",
    chunks, chunk_size);
   if (realloc(buffer,
    sizeof(char) * chunks * chunk_size) == ((void *)0))
    connection_die(500, "Cannot realloc."
     " Too long request line?");
  }
  strcat(buffer, chunk);
  ++chunks;
  line_start = buffer;
  if (strstr(buffer, "\n")) {
   line = (char *)malloc(strlen(buffer) + 1);
   while (1) {
    chars = another_line_from_buffer(
     line, &line_start);
    if (chars < 0)
     break;
    parse_line(&type, line, &request);
   }
   clear_buffer_to_eol(buffer);
   chunks = 0;
   free(line);
  }
  if (read_length != chunk_size)
   break;
 }
 log_message(DEBUG1, "End of request");
 free(buffer);
 parse_request(&request);
 close(client);
 free(request.url);
}

variable_types:
chunk
char [-1]

variable_types: chunks int

variable_types: enum line_type { REQUEST=0, HEADER=1, BODY=2, END=3 }

variable_types: struct request_data { char method[10]; char *url; char magic[10]; int major; }


original_code:
void remove_dots(char *url)
{
 char *curr = url + 2;
 while (*curr) {
  while (*(curr - 2) == '.' && *(curr - 1) == '.' && *curr == '/') {
   char *curr_to = curr - 2;
   char *curr_from = curr + 1;
   while (*curr_to++ = *curr_from++);
   *curr_to = '\0';
  }
  ++curr;
 }
}

variable_types:
url
char *

variable_types:
curr
char *


unescape

decompiled_code:
__int64 __fastcall unescape(unsigned __int8 *a1)
{
_BYTE *v1; // rdx
unsigned __int8 *v2; // rbx
__int64 result; // rax
_BYTE *i; // rbp
char v5; // al
int v6; // eax
bool v7; // zf
char nptr[29]; // [rsp+Bh] [rbp-1Dh] BYREF

v1 = a1;
v2 = a1;
result = *a1;
strcpy(nptr, "0x00");
if ( (_BYTE)result )
{
for ( i = a1; ; ++i )
{
while ( (_BYTE)result == 37 )
{
v5 = v2[1];
v2 += 3;
nptr[2] = v5;
nptr[3] = *(v2 - 1);
v6 = strtol(nptr, 0LL, 16);
if ( v6 > 31 && v6 != 127 )
*i++ = v6;
result = *v2;
}
v7 = i[1] == 0;
*i = result;
v1 = i + 1;
if ( v7 )
break;
result = *++v2;
}
}
*v1 = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int8 *a1)
{
_BYTE *v1; // rdx
unsigned __int8 *v2; // rbx
__int64 result; // rax
_BYTE *i; // rbp
char v5; // al
int v6; // eax
bool v7; // zf
char nptr[29]; // [rsp+Bh] [rbp-1Dh] BYREF

v1 = a1;
v2 = a1;
result = *a1;
strcpy(nptr, "0x00");
if ( (_BYTE)result )
{
for ( i = a1; ; ++i )
{
while ( (_BYTE)result == 37 )
{
v5 = v2[1];
v2 += 3;
nptr[2] = v5;
nptr[3] = *(v2 - 1);
v6 = strtol(nptr, 0LL, 16);
if ( v6 > 31 && v6 != 127 )
*i++ = v6;
result = *v2;
}
v7 = i[1] == 0;
*i = result;
v1 = i + 1;
if ( v7 )
break;
result = *++v2;
}
}
*v1 = 0;
return result;
}

original_code:
void unescape(char *url)
{
 char *src = url;
 char *dest = url;
 char buffer[] = "0x00";
 int value;
 while (*dest) {
  while (*src == '%') {
   ++src;
   buffer[2] = *src++;
   buffer[3] = *src++;
   value = strtol(buffer, ((void *)0), 16);
   if (value > 31 && value != 127)
    *dest++ = value;
  }
  *dest++ = *src++;
 }
 *dest = '\0';
}

variable_types:
src
char *

variable_types:
dest
char *

variable_types:
buffer
char []

variable_types: value int


decompiled_code:
char *__fastcall decode_url(char *s)
{
char *result; // rax

if ( strstr(s, "../") )
remove_dots(s);
if ( strchr(s, 37) )
unescape((unsigned __int8 *)s);
result = strchr(s, 63);
if ( result )
*result = 0;
return result;
}

canonical_decompiled_code:
char *__fastcall func2(char *s)
{
char *result; // rax

if ( strstr(s, "../") )
func0(s);
if ( strchr(s, 37) )
func1((unsigned __int8 *)s);
result = strchr(s, 63);
if ( result )
*result = 0;
return result;
}

original_code:
void decode_url(char *url)
{
 if (strstr(url, "../"))
  remove_dots(url);
 if (strstr(url, "%"))
  unescape(url);
 char *character = strstr(url, "?");
 if (character)
  *character = '\0';
}

variable_types:
character
char *


decompiled_code:
__int64 __fastcall parse_request(__int64 a1)
{
char v1; // al
__int64 result; // rax

v1 = *(_BYTE *)a1;
if ( *(_BYTE *)a1 == 103 )
goto LABEL_6;
if ( v1 > 103 )
{
if ( v1 == 104 )
goto LABEL_5;
}
else
{
if ( v1 == 71 )
{
LABEL_6:
decode_url(*(char **)(a1 + 16));
result = serve(*(_QWORD *)(a1 + 16));
head_request = 0;
return result;
}
if ( v1 == 72 )
{
LABEL_5:
head_request = 1;
goto LABEL_6;
}
}
return connection_die(501LL, "It's just for zaliczenie, don't expect too much");
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
char v1; // al
__int64 result; // rax

v1 = *(_BYTE *)a1;
if ( *(_BYTE *)a1 == 103 )
goto LABEL_6;
if ( v1 > 103 )
{
if ( v1 == 104 )
goto LABEL_5;
}
else
{
if ( v1 == 71 )
{
LABEL_6:
func2(*(char **)(a1 + 16));
result = serve(*(_QWORD *)(a1 + 16));
head_request = 0;
return result;
}
if ( v1 == 72 )
{
LABEL_5:
head_request = 1;
goto LABEL_6;
}
}
return connection_die(501LL, "It's just for zaliczenie, don't expect too much");
}

original_code:
void parse_request(struct request_data *data)
{
 switch (*(data->method)) {
 case 'H':
 case 'h':
  head_request = 1;
 case 'G':
 case 'g':
  decode_url(data->url);
  serve(data->url);
  head_request = 0;
  break;
 default:
  connection_die(501, "It's just for zaliczenie,"
   " don't expect too much");
 }
}

variable_types: struct request_data { char method[10]; char *url; char magic[10]; int major; } *


repo: hahong/pymworks

decompiled_code:
__int64 filestr_bind()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
return 0LL;
}

original_code:
static ScarabStream *
filestr_bind(ScarabSession * session, const char *uri)
{
 return ((void *)0);
}

canonical_original_code:
static struct __ScarabStream *filestr_bind(struct __ScarabSession *session, char *uri)
{
 return ((void *)0);
}

variable_types:
session
struct __ScarabSession *

variable_types:
uri
char *

return_type: struct __ScarabStream { struct __ScarabStream *next; struct __ScarabSession { struct __ScarabEncoderEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); int (*session_accept)(struct __ScarabSession *s); int (*session_connect)(struct __ScarabSession *s); int (*session_close)(struct __ScarabSession *s); int (*write_null)(struct __ScarabSession *s); int (*write_integer)(struct __ScarabSession *s, long long value); int (*write_float)(struct __ScarabSession *s, double value); int (*write_float_inf)(struct __ScarabSession *s); int (*write_float_nan)(struct __ScarabSession *s); int (*write_opaque)(struct __ScarabSession *s, char *value, int len); int (*write_list)(struct __ScarabSession *s, struct __ScarabList { int size; struct __ScarabDatum { union { long long integer; double floatp; struct __ScarabOpaque { int size; unsigned char *data; } opaque; struct __ScarabDict { int size; int tablesize; struct __ScarabDatum **keys; struct __ScarabDatum **values; int (*expansion_function)(struct __ScarabDict *dict); } *dict; struct __ScarabList *list; void *p; } data; struct __ScarabDatum *attributes; int type; int ref_count; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } *mutex; } **values; } *list); int (*write_dict)(struct __ScarabSession *s, struct __ScarabDict *dict); struct __ScarabDatum *(*read)(struct __ScarabSession *s); int (*seek)(struct __ScarabSession *s, long offset, int origin); long (*tell)(struct __ScarabSession *s); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; } *encoder_engine; void *encoder; char *url; struct __ScarabStream *stream_head; int errcode; int os_errno; int force_buffering; int send_recursion_counter; int send_buffer_position; unsigned char send_buffer[100000000]; unsigned char read_buffer[100000000]; int read_buffer_fill_position; int read_buffer_read_position; } *session; struct __ScarabStreamEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); struct __ScarabStream *(*stream_listen)(struct __ScarabSession *s, char *uri); struct __ScarabStream *(*stream_accept)(struct __ScarabSession *session, char *uri, struct __ScarabStream *binding); struct __ScarabStream *(*stream_connect)(struct __ScarabSession *session, char *uri); int (*stream_close)(struct __ScarabStream *self); int (*write)(struct __ScarabStream *self, void *data, int len); int (*send)(struct __ScarabStream *self); int (*flush)(struct __ScarabStream *self); int (*read)(struct __ScarabStream *self, void *data, int len); int (*seek)(struct __ScarabStream *self, long offset, int origin); long (*tell)(struct __ScarabStream *); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; int (*local_port)(struct __ScarabStream *self); char *(*local_address)(struct __ScarabStream *self); int (*foreign_port)(struct __ScarabStream *self); char *(*foreign_address)(struct __ScarabStream *self); } *engine; } *

user_defined_types: struct __ScarabStream { struct __ScarabStream *next; struct __ScarabSession *session; struct __ScarabStreamEngine *engine; }

user_defined_types: struct __ScarabSession { struct __ScarabEncoderEngine *encoder_engine; void *encoder; char *url; struct __ScarabStream *stream_head; int errcode; int os_errno; int force_buffering; int send_recursion_counter; int send_buffer_position; unsigned char send_buffer[100000000]; unsigned char read_buffer[100000000]; int read_buffer_fill_position; int read_buffer_read_position; }

user_defined_types: struct __ScarabEncoderEngine { int api_version; int err_code_base; int nerr_code; int _pad1; int (*lib_init)(); int (*session_accept)(struct __ScarabSession *s); int (*session_connect)(struct __ScarabSession *s); int (*session_close)(struct __ScarabSession *s); int (*write_null)(struct __ScarabSession *s); int (*write_integer)(struct __ScarabSession *s, long long value); int (*write_float)(struct __ScarabSession *s, double value); int (*write_float_inf)(struct __ScarabSession *s); int (*write_float_nan)(struct __ScarabSession *s); int (*write_opaque)(struct __ScarabSession *s, char *value, int len); int (*write_list)(struct __ScarabSession *s, struct __ScarabList *list); int (*write_dict)(struct __ScarabSession *s, struct __ScarabDict *dict); struct __ScarabDatum *(*read)(struct __ScarabSession *s); int (*seek)(struct __ScarabSession *s, long offset, int origin); long (*tell)(struct __ScarabSession *s); char *(*strerr)(int code); char uri_scheme[64]; char engine_author[64]; char engine_name[64]; }

user_defined_types: struct __ScarabList { int size; struct __ScarabDatum **values; }

user_defined_types: struct __ScarabDatum { union { long long integer; double floatp; struct __ScarabOpaque opaque; struct __ScarabDict *dict; struct __ScarabList *list; void *p; } data; struct __ScarabDatum *attributes; int type; int ref_count; union pthread_mutex_t *mutex; }