decompiled_code:
__int64 __fastcall compare_index(const void *a1, const void *a2)
{
return (unsigned int)*((_QWORD *)a1 + 2) - *((_DWORD *)a2 + 4);
}

canonical_decompiled_code:
__int64 __fastcall func0(const void *a1, const void *a2)
{
return (unsigned int)*((_QWORD *)a1 + 2) - *((_DWORD *)a2 + 4);
}

original_code:
int compare_index(const idxrec_t* idx1, const idxrec_t* idx2)
{
 return(idx1->number - idx2->number);
}

canonical_original_code:
int compare_index(struct idxrec_t *idx1, struct idxrec_t *idx2)
{
 return(idx1->number - idx2->number);
}

variable_types: struct idxrec_t { unsigned short to; unsigned short from; unsigned short subj; unsigned short attr; unsigned long offset; unsigned long number; unsigned long time; } *


repo: ftnapps/pkg-sbbs

decompiled_code:
void unlock_msgbase(void)
{
unsigned int v0; // eax

if ( (unsigned int)smb_islocked(smb) )
{
v0 = smb_unlock(smb);
if ( v0 )
printf("smb_unlock returned %d: %s\n", v0, (const char *)(unsigned int)((_DWORD)smb + 240));
}
}

canonical_decompiled_code:
void func1(void)
{
unsigned int v0; // eax

if ( (unsigned int)smb_islocked(smb) )
{
v0 = smb_unlock(smb);
if ( v0 )
printf("smb_unlock returned %d: %s\n", v0, (const char *)(unsigned int)((_DWORD)smb + 240));
}
}

original_code:
void unlock_msgbase(void)
{
 int i;
 if(smb_islocked(&smb) && (i=smb_unlock(&smb))!=0)
  printf("smb_unlock returned %d: %s\n",i,smb.last_error);
}

canonical_original_code:
void unlock_msgbase()
{
 int i;
 if(smb_islocked(&smb) && (i=smb_unlock(&smb))!=0)
  printf("smb_unlock returned %d: %s\n",i,smb.last_error);
}

variable_types: i int

return_type: void


fixsmb

decompiled_code:
__int64 __fastcall fixsmb(char *src)
{
const char *v1; // rax
const char *v2; // rbx
unsigned int v3; // eax
unsigned int v4; // eax
unsigned int v5; // eax
unsigned int v6; // ebp
unsigned int v7; // eax
unsigned int v8; // eax
int v9; // eax
int v10; // eax
int v11; // eax
unsigned int v12; // eax
unsigned __int64 v13; // rbp
unsigned __int64 v14; // rbx
__int64 v15; // r15
float v16; // xmm1_4
unsigned int v17; // eax
unsigned int v18; // er12
__int64 v19; // rax
__int64 v20; // r13
__int64 v21; // r14
unsigned int v22; // eax
unsigned int v23; // eax
unsigned __int64 v24; // rbx
unsigned int v25; // eax
unsigned __int64 v26; // rbp
unsigned __int64 v27; // rbx
unsigned int v28; // eax
unsigned __int64 v29; // rsi
int v30; // eax
unsigned int v31; // eax
unsigned int v33; // eax
void *v34; // r8
__int16 v35; // dx
char v36; // di
float v37; // [rsp+4h] [rbp-224h]
unsigned int v38; // [rsp+4h] [rbp-224h]
unsigned int v39; // [rsp+4h] [rbp-224h]
unsigned int v40; // [rsp+4h] [rbp-224h]
void *v41; // [rsp+8h] [rbp-220h]
char v42; // [rsp+1Bh] [rbp-20Dh] BYREF
int ptr; // [rsp+1Ch] [rbp-20Ch] BYREF
char v44[8]; // [rsp+20h] [rbp-208h] BYREF
unsigned __int64 v45; // [rsp+28h] [rbp-200h]
__int16 v46; // [rsp+4Ah] [rbp-1DEh]
__int64 v47; // [rsp+54h] [rbp-1D4h]
__int64 v48; // [rsp+70h] [rbp-1B8h]
const char *v49; // [rsp+C8h] [rbp-160h]
__int64 v50; // [rsp+1B8h] [rbp-70h]

memset(smb, 0, sizeof(smb));
strncpy(smb, src, 0x7FuLL);
v1 = (const char *)getfext(smb);
if ( v1 )
{
v2 = v1;
if ( !strcasecmp(v1, ".shd") )
*v2 = 0;
}
printf("Opening %s\n", smb);
v3 = smb_open(smb);
if ( v3 )
{
printf("smb_open returned %d: %s\n", v3, (const char *)(unsigned int)((_DWORD)smb + 240));
exit(1);
}
v4 = smb_lock(smb);
if ( v4 )
{
printf("smb_lock returned %d: %s\n", v4, (const char *)(unsigned int)((_DWORD)smb + 240));
exit(1);
}
v5 = smb_locksmbhdr(smb);
if ( v5 )
{
v39 = v5;
smb_close(smb);
printf("smb_locksmbhdr returned %d: %s\n", v39, (const char *)(unsigned int)((_DWORD)smb + 240));
exit(1);
}
v6 = smb_getstatus(smb);
if ( v6 )
{
smb_unlocksmbhdr(smb);
smb_close(smb);
printf("smb_getstatus returned %d: %s\n", v6, (const char *)(unsigned int)((_DWORD)smb + 240));
exit(1);
}
if ( (smb[234] & 2) == 0 )
{
v7 = smb_open_fp(smb, (unsigned int)((_DWORD)smb + 160), 1LL);
if ( v7 )
{
v40 = v7;
smb_close(smb);
printf("smb_open_ha returned %d: %s\n", v40, (const char *)(unsigned int)((_DWORD)smb + 240));
exit(1);
}
v8 = smb_open_fp(smb, (unsigned int)((_DWORD)smb + 152), 1LL);
if ( v8 )
{
v38 = v8;
smb_close(smb);
printf("smb_open_da returned %d: %s\n", v38, (const char *)(unsigned int)((_DWORD)smb + 240));
exit(1);
}
rewind(*(FILE **)&smb[160]);
v9 = fileno(*(FILE **)&smb[160]);
ftruncate(v9, 0LL);
rewind(*(FILE **)&smb[152]);
v10 = fileno(*(FILE **)&smb[152]);
ftruncate(v10, 0LL);
}
rewind(*(FILE **)&smb[144]);
v11 = fileno(*(FILE **)&smb[144]);
ftruncate(v11, 0LL);
if ( (smb[234] & 2) != 0 )
{
v12 = fileno(*(FILE **)&smb[136]);
v13 = filelength(v12);
}
else
{
v24 = 0LL;
v25 = fileno(*(FILE **)&smb[128]);
ptr = 0;
v26 = filelength(v25);
if ( v26 )
{
do
{
v24 += 256LL;
fwrite(&ptr, 2uLL, 1uLL, *(FILE **)&smb[152]);
}
while ( v26 > v24 );
}
v27 = 0LL;
v28 = fileno(*(FILE **)&smb[136]);
v42 = 0;
v13 = filelength(v28);
if ( !v13 )
goto LABEL_65;
do
{
v27 += 256LL;
fwrite(&v42, 1uLL, 1uLL, *(FILE **)&smb[160]);
}
while ( v13 > v27 );
}
v14 = *(_QWORD *)&smb[208];
if ( v13 > v14 )
{
if ( (v13 & 0x8000000000000000LL) != 0LL )
v37 = (float)(int)(v13 & 1 | (v13 >> 1)) + (float)(int)(v13 & 1 | (v13 >> 1));
else
v37 = (float)(int)v13;
v15 = 0LL;
while ( 1 )
{
while ( 1 )
{
v16 = (v14 & 0x8000000000000000LL) == 0LL ? (float)(int)v14 : (float)(int)(v14 & 1 | (v14 >> 1))
+ (float)(int)(v14 & 1 | (v14 >> 1));
printf("\r%2lu%% ", (unsigned int)(int)(100.0 / (float)(v37 / v16)));
v45 = v14;
v17 = smb_lockmsghdr(smb, v44);
if ( !v17 )
break;
v29 = v14;
v14 += 256LL;
printf("\n(%06lX) smb_lockmsghdr returned %d:\n%s\n", v29, v17, (const char *)(unsigned int)((_DWORD)smb + 240));
if ( v13 <= v14 )
goto LABEL_43;
}
v18 = smb_getmsghdr(smb, v44);
smb_unlockmsghdr(smb, v44);
if ( v18 )
{
v21 = 256LL;
printf("\n(%06lX) smb_getmsghdr returned %d:\n%s\n", v14, v18, (const char *)(unsigned int)((_DWORD)smb + 240));
goto LABEL_34;
}
v19 = smb_getmsghdrlen(v44);
v20 = smb_hdrblocks(v19);
v21 = v20 << 8;
printf("#%-5lu (%06lX) %-25.25s ", v48, v14, v49);
if ( (v46 & 0x10) != 0 )
break;
v41 = (void *)smb_getmsgtxt(smb, v44, 4LL);
v33 = smb_hashmsg(smb, v44, v41, 1LL);
v34 = v41;
if ( v33 )
{
printf("!ERROR %d hashing message\n", v33);
v34 = v41;
}
if ( !v34 )
goto LABEL_24;
free(v34);
if ( (v46 & 0x10) == 0 )
{
LABEL_25:
if ( v48 )
{
v50 = v15;
if ( renumber )
v48 = v15 + 1;
if ( (v47 & 2) != 0 )
{
puts("Removing 'in transit' attribute");
v47 &= 0xFFFFFFFFFFFFFFFDLL;
}
v23 = smb_putmsg(smb, v44);
if ( v23 )
{
printf("\nsmb_putmsg returned %d: %s\n", v23, (const char *)(unsigned int)((_DWORD)smb + 240));
goto LABEL_34;
}
++v15;
}
else
{
puts("Not indexing invalid message number (0)!");
}
if ( (smb[234] & 2) != 0 )
goto LABEL_33;
LABEL_54:
fseek(*(FILE **)&smb[160], (v14 - *(_QWORD *)&smb[208]) >> 8, 0);
v35 = v46 & 0x10;
v36 = v35 == 0;
v42 = v36;
if ( (int)v20 > 0 )
{
while ( 1 )
{
++v18;
fputc(v36, *(FILE **)&smb[160]);
if ( v18 == (_DWORD)v20 )
break;
v36 = v42;
}
v35 = v46 & 0x10;
}
if ( !v35 )
smb_incmsg_dfields(smb, v44, 1LL);
goto LABEL_33;
}
LABEL_53:
puts("Not indexing deleted message");
if ( (smb[234] & 2) == 0 )
goto LABEL_54;
LABEL_33:
smb_freemsgmem(v44);
LABEL_34:
v14 += v21;
if ( v13 <= v14 )
goto LABEL_43;
}
v22 = smb_hashmsg(smb, v44, 0LL, 1LL);
if ( v22 )
printf("!ERROR %d hashing message\n", v22);
LABEL_24:
if ( (v46 & 0x10) == 0 )
goto LABEL_25;
goto LABEL_53;
}
LABEL_65:
v15 = 0LL;
LABEL_43:
printf("\r%79s\r100%%\n", (const char *)&unk_2CA6);
v30 = renumber;
*(_QWORD *)&smb[200] = v15;
if ( v30 )
*(_QWORD *)&smb[192] = v15;
else
sort_index((__int64)smb);
printf("Saving message base status (%lu total messages).\n", v15);
v31 = smb_putstatus(smb);
if ( v31 )
printf("\nsmb_putstatus returned %d: %s\n", v31, (const char *)(unsigned int)((_DWORD)smb + 240));
smb_unlocksmbhdr(smb);
puts("Closing message base.");
smb_close(smb);
unlock_msgbase();
puts("Done.");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *src)
{
const char *v1; // rax
const char *v2; // rbx
unsigned int v3; // eax
unsigned int v4; // eax
unsigned int v5; // eax
unsigned int v6; // ebp
unsigned int v7; // eax
unsigned int v8; // eax
int v9; // eax
int v10; // eax
int v11; // eax
unsigned int v12; // eax
unsigned __int64 v13; // rbp
unsigned __int64 v14; // rbx
__int64 v15; // r15
float v16; // xmm1_4
unsigned int v17; // eax
unsigned int v18; // er12
__int64 v19; // rax
__int64 v20; // r13
__int64 v21; // r14
unsigned int v22; // eax
unsigned int v23; // eax
unsigned __int64 v24; // rbx
unsigned int v25; // eax
unsigned __int64 v26; // rbp
unsigned __int64 v27; // rbx
unsigned int v28; // eax
unsigned __int64 v29; // rsi
int v30; // eax
unsigned int v31; // eax
unsigned int v33; // eax
void *v34; // r8
__int16 v35; // dx
char v36; // di
float v37; // [rsp+4h] [rbp-224h]
unsigned int v38; // [rsp+4h] [rbp-224h]
unsigned int v39; // [rsp+4h] [rbp-224h]
unsigned int v40; // [rsp+4h] [rbp-224h]
void *v41; // [rsp+8h] [rbp-220h]
char v42; // [rsp+1Bh] [rbp-20Dh] BYREF
int ptr; // [rsp+1Ch] [rbp-20Ch] BYREF
char v44[8]; // [rsp+20h] [rbp-208h] BYREF
unsigned __int64 v45; // [rsp+28h] [rbp-200h]
__int16 v46; // [rsp+4Ah] [rbp-1DEh]
__int64 v47; // [rsp+54h] [rbp-1D4h]
__int64 v48; // [rsp+70h] [rbp-1B8h]
const char *v49; // [rsp+C8h] [rbp-160h]
__int64 v50; // [rsp+1B8h] [rbp-70h]

memset(smb, 0, sizeof(smb));
strncpy(smb, src, 0x7FuLL);
v1 = (const char *)getfext(smb);
if ( v1 )
{
v2 = v1;
if ( !strcasecmp(v1, ".shd") )
*v2 = 0;
}
printf("Opening %s\n", smb);
v3 = smb_open(smb);
if ( v3 )
{
printf("smb_open returned %d: %s\n", v3, (const char *)(unsigned int)((_DWORD)smb + 240));
exit(1);
}
v4 = smb_lock(smb);
if ( v4 )
{
printf("smb_lock returned %d: %s\n", v4, (const char *)(unsigned int)((_DWORD)smb + 240));
exit(1);
}
v5 = smb_locksmbhdr(smb);
if ( v5 )
{
v39 = v5;
smb_close(smb);
printf("smb_locksmbhdr returned %d: %s\n", v39, (const char *)(unsigned int)((_DWORD)smb + 240));
exit(1);
}
v6 = smb_getstatus(smb);
if ( v6 )
{
smb_unlocksmbhdr(smb);
smb_close(smb);
printf("smb_getstatus returned %d: %s\n", v6, (const char *)(unsigned int)((_DWORD)smb + 240));
exit(1);
}
if ( (smb[234] & 2) == 0 )
{
v7 = smb_open_fp(smb, (unsigned int)((_DWORD)smb + 160), 1LL);
if ( v7 )
{
v40 = v7;
smb_close(smb);
printf("smb_open_ha returned %d: %s\n", v40, (const char *)(unsigned int)((_DWORD)smb + 240));
exit(1);
}
v8 = smb_open_fp(smb, (unsigned int)((_DWORD)smb + 152), 1LL);
if ( v8 )
{
v38 = v8;
smb_close(smb);
printf("smb_open_da returned %d: %s\n", v38, (const char *)(unsigned int)((_DWORD)smb + 240));
exit(1);
}
rewind(*(FILE **)&smb[160]);
v9 = fileno(*(FILE **)&smb[160]);
ftruncate(v9, 0LL);
rewind(*(FILE **)&smb[152]);
v10 = fileno(*(FILE **)&smb[152]);
ftruncate(v10, 0LL);
}
rewind(*(FILE **)&smb[144]);
v11 = fileno(*(FILE **)&smb[144]);
ftruncate(v11, 0LL);
if ( (smb[234] & 2) != 0 )
{
v12 = fileno(*(FILE **)&smb[136]);
v13 = filelength(v12);
}
else
{
v24 = 0LL;
v25 = fileno(*(FILE **)&smb[128]);
ptr = 0;
v26 = filelength(v25);
if ( v26 )
{
do
{
v24 += 256LL;
fwrite(&ptr, 2uLL, 1uLL, *(FILE **)&smb[152]);
}
while ( v26 > v24 );
}
v27 = 0LL;
v28 = fileno(*(FILE **)&smb[136]);
v42 = 0;
v13 = filelength(v28);
if ( !v13 )
goto LABEL_65;
do
{
v27 += 256LL;
fwrite(&v42, 1uLL, 1uLL, *(FILE **)&smb[160]);
}
while ( v13 > v27 );
}
v14 = *(_QWORD *)&smb[208];
if ( v13 > v14 )
{
if ( (v13 & 0x8000000000000000LL) != 0LL )
v37 = (float)(int)(v13 & 1 | (v13 >> 1)) + (float)(int)(v13 & 1 | (v13 >> 1));
else
v37 = (float)(int)v13;
v15 = 0LL;
while ( 1 )
{
while ( 1 )
{
v16 = (v14 & 0x8000000000000000LL) == 0LL ? (float)(int)v14 : (float)(int)(v14 & 1 | (v14 >> 1))
+ (float)(int)(v14 & 1 | (v14 >> 1));
printf("\r%2lu%% ", (unsigned int)(int)(100.0 / (float)(v37 / v16)));
v45 = v14;
v17 = smb_lockmsghdr(smb, v44);
if ( !v17 )
break;
v29 = v14;
v14 += 256LL;
printf("\n(%06lX) smb_lockmsghdr returned %d:\n%s\n", v29, v17, (const char *)(unsigned int)((_DWORD)smb + 240));
if ( v13 <= v14 )
goto LABEL_43;
}
v18 = smb_getmsghdr(smb, v44);
smb_unlockmsghdr(smb, v44);
if ( v18 )
{
v21 = 256LL;
printf("\n(%06lX) smb_getmsghdr returned %d:\n%s\n", v14, v18, (const char *)(unsigned int)((_DWORD)smb + 240));
goto LABEL_34;
}
v19 = smb_getmsghdrlen(v44);
v20 = smb_hdrblocks(v19);
v21 = v20 << 8;
printf("#%-5lu (%06lX) %-25.25s ", v48, v14, v49);
if ( (v46 & 0x10) != 0 )
break;
v41 = (void *)smb_getmsgtxt(smb, v44, 4LL);
v33 = smb_hashmsg(smb, v44, v41, 1LL);
v34 = v41;
if ( v33 )
{
printf("!ERROR %d hashing message\n", v33);
v34 = v41;
}
if ( !v34 )
goto LABEL_24;
free(v34);
if ( (v46 & 0x10) == 0 )
{
LABEL_25:
if ( v48 )
{
v50 = v15;
if ( renumber )
v48 = v15 + 1;
if ( (v47 & 2) != 0 )
{
puts("Removing 'in transit' attribute");
v47 &= 0xFFFFFFFFFFFFFFFDLL;
}
v23 = smb_putmsg(smb, v44);
if ( v23 )
{
printf("\nsmb_putmsg returned %d: %s\n", v23, (const char *)(unsigned int)((_DWORD)smb + 240));
goto LABEL_34;
}
++v15;
}
else
{
puts("Not indexing invalid message number (0)!");
}
if ( (smb[234] & 2) != 0 )
goto LABEL_33;
LABEL_54:
fseek(*(FILE **)&smb[160], (v14 - *(_QWORD *)&smb[208]) >> 8, 0);
v35 = v46 & 0x10;
v36 = v35 == 0;
v42 = v36;
if ( (int)v20 > 0 )
{
while ( 1 )
{
++v18;
fputc(v36, *(FILE **)&smb[160]);
if ( v18 == (_DWORD)v20 )
break;
v36 = v42;
}
v35 = v46 & 0x10;
}
if ( !v35 )
smb_incmsg_dfields(smb, v44, 1LL);
goto LABEL_33;
}
LABEL_53:
puts("Not indexing deleted message");
if ( (smb[234] & 2) == 0 )
goto LABEL_54;
LABEL_33:
smb_freemsgmem(v44);
LABEL_34:
v14 += v21;
if ( v13 <= v14 )
goto LABEL_43;
}
v22 = smb_hashmsg(smb, v44, 0LL, 1LL);
if ( v22 )
printf("!ERROR %d hashing message\n", v22);
LABEL_24:
if ( (v46 & 0x10) == 0 )
goto LABEL_25;
goto LABEL_53;
}
LABEL_65:
v15 = 0LL;
LABEL_43:
printf("\r%79s\r100%%\n", (const char *)&unk_2CA6);
v30 = renumber;
*(_QWORD *)&smb[200] = v15;
if ( v30 )
*(_QWORD *)&smb[192] = v15;
else
func2((__int64)smb);
printf("Saving message base status (%lu total messages).\n", v15);
v31 = smb_putstatus(smb);
if ( v31 )
printf("\nsmb_putstatus returned %d: %s\n", v31, (const char *)(unsigned int)((_DWORD)smb + 240));
smb_unlocksmbhdr(smb);
puts("Closing message base.");
smb_close(smb);
func1();
puts("Done.");
return 0LL;
}

original_code:
int fixsmb(char* sub)
{
 char* p;
 char* text;
 char c;
 int i,w;
 ulong l,length,size,n;
 smbmsg_t msg;
 memset(&smb,0,sizeof(smb));
 (strncpy(smb.file,sub,sizeof(smb.file)), smb.file[sizeof(smb.file)-1]=0);
 if((p=getfext(smb.file))!=((void *)0) && strcasecmp(p,".shd")==0)
  *p=0;
 printf("Opening %s\n",smb.file);
 if((i=smb_open(&smb))!=0) {
  printf("smb_open returned %d: %s\n",i,smb.last_error);
  exit(1);
 }
 if((i=smb_lock(&smb))!=0) {
  printf("smb_lock returned %d: %s\n",i,smb.last_error);
  exit(1);
 }
 if((i=smb_locksmbhdr(&smb))!=0) {
  smb_close(&smb);
  printf("smb_locksmbhdr returned %d: %s\n",i,smb.last_error);
  exit(1);
 }
 if((i=smb_getstatus(&smb))!=0) {
  smb_unlocksmbhdr(&smb);
  smb_close(&smb);
  printf("smb_getstatus returned %d: %s\n",i,smb.last_error);
  exit(1);
 }
 if(!(smb.status.attr&2)) {
  if((i=smb_open_fp(&smb,&(&smb)->sha_fp,1))!=0) {
   smb_close(&smb);
   printf("smb_open_ha returned %d: %s\n",i,smb.last_error);
   exit(1);
  }
  if((i=smb_open_fp(&smb,&(&smb)->sda_fp,1))!=0) {
   smb_close(&smb);
   printf("smb_open_da returned %d: %s\n",i,smb.last_error);
   exit(1);
  }
  rewind(smb.sha_fp);
  ftruncate(fileno(smb.sha_fp),0L);
  rewind(smb.sda_fp);
  ftruncate(fileno(smb.sda_fp),0L);
 }
 rewind(smb.sid_fp);
 ftruncate(fileno(smb.sid_fp),0L);
 if(!(smb.status.attr&2)) {
  length=filelength(fileno(smb.sdt_fp));
  w=0;
  for(l=0;l<length;l+=256)
   fwrite(&w,2,1,smb.sda_fp);
  length=filelength(fileno(smb.shd_fp));
  c=0;
  for(l=0;l<length;l+=256)
   fwrite(&c,1,1,smb.sha_fp);
 } else
  length=filelength(fileno(smb.shd_fp));
 n=0;
 for(l=smb.status.header_offset;l<length;l+=size) {
  size=256;
  printf("\r%2lu%%  ",(long)(100.0/((float)length/l)));
  msg.idx.offset=l;
  if((i=smb_lockmsghdr(&smb,&msg))!=0) {
   printf("\n(%06lX) smb_lockmsghdr returned %d:\n%s\n",l,i,smb.last_error);
   continue;
  }
  i=smb_getmsghdr(&smb,&msg);
  smb_unlockmsghdr(&smb,&msg);
  if(i!=0) {
   printf("\n(%06lX) smb_getmsghdr returned %d:\n%s\n",l,i,smb.last_error);
   continue;
  }
  size=smb_hdrblocks(smb_getmsghdrlen(&msg))*256;
  printf("#%-5lu (%06lX) %-25.25s ",msg.hdr.number,l,msg.from);
  if(msg.hdr.attr&(1<<4))
   text=((void *)0);
  else
   text=smb_getmsgtxt(&smb,&msg,(1<<2));
  i=smb_hashmsg(&smb,&msg,text,1 );
  if(i!=0)
   printf("!ERROR %d hashing message\n", i);
  if(text!=((void *)0))
   free(text);
  if(msg.hdr.attr&(1<<4))
   printf("Not indexing deleted message\n");
  else if(msg.hdr.number==0)
   printf("Not indexing invalid message number (0)!\n");
  else {
   msg.offset=n;
   if(renumber)
    msg.hdr.number=n+1;
   if(msg.hdr.netattr&(1<<1)) {
    printf("Removing 'in transit' attribute\n");
    msg.hdr.netattr&=~(1<<1);
   }
   if((i=smb_putmsg(&smb,&msg))!=0) {
    printf("\nsmb_putmsg returned %d: %s\n",i,smb.last_error);
    continue;
   }
   n++;
  }
  if(!(smb.status.attr&2)) {
   fseek(smb.sha_fp,(l-smb.status.header_offset)/256,0);
   if(msg.hdr.attr&(1<<4)) c=0;
   else c=1;
   for(i=0;i<(int)(size/256);i++)
    fputc(c,smb.sha_fp);
   if(!(msg.hdr.attr&(1<<4)))
    smb_incmsg_dfields(&smb,&msg,1);
  }
  smb_freemsgmem(&msg);
 }
 printf("\r%79s\r100%%\n","");
 smb.status.total_msgs=n;
 if(renumber)
  smb.status.last_msg=n;
 else
  sort_index(&smb);
 printf("Saving message base status (%lu total messages).\n",n);
 if((i=smb_putstatus(&smb))!=0)
  printf("\nsmb_putstatus returned %d: %s\n",i,smb.last_error);
 smb_unlocksmbhdr(&smb);
 printf("Closing message base.\n");
 smb_close(&smb);
 unlock_msgbase();
 printf("Done.\n");
 return(0);
}

canonical_original_code:
int fixsmb(char *sub)
{
 char *p;
 char *text;
 char c;
 int i; int w;
 unsigned long l; unsigned long length; unsigned long size; unsigned long n;
 struct smbmsg_t msg;
 memset(&smb,0,sizeof(smb));
 (strncpy(smb.file,sub,sizeof(smb.file)), smb.file[sizeof(smb.file)-1]=0);
 if((p=getfext(smb.file))!=((void *)0) && strcasecmp(p,".shd")==0)
  *p=0;
 printf("Opening %s\n",smb.file);
 if((i=smb_open(&smb))!=0) {
  printf("smb_open returned %d: %s\n",i,smb.last_error);
  exit(1);
 }
 if((i=smb_lock(&smb))!=0) {
  printf("smb_lock returned %d: %s\n",i,smb.last_error);
  exit(1);
 }
 if((i=smb_locksmbhdr(&smb))!=0) {
  smb_close(&smb);
  printf("smb_locksmbhdr returned %d: %s\n",i,smb.last_error);
  exit(1);
 }
 if((i=smb_getstatus(&smb))!=0) {
  smb_unlocksmbhdr(&smb);
  smb_close(&smb);
  printf("smb_getstatus returned %d: %s\n",i,smb.last_error);
  exit(1);
 }
 if(!(smb.status.attr&2)) {
  if((i=smb_open_fp(&smb,&(&smb)->sha_fp,1))!=0) {
   smb_close(&smb);
   printf("smb_open_ha returned %d: %s\n",i,smb.last_error);
   exit(1);
  }
  if((i=smb_open_fp(&smb,&(&smb)->sda_fp,1))!=0) {
   smb_close(&smb);
   printf("smb_open_da returned %d: %s\n",i,smb.last_error);
   exit(1);
  }
  rewind(smb.sha_fp);
  ftruncate(fileno(smb.sha_fp),0L);
  rewind(smb.sda_fp);
  ftruncate(fileno(smb.sda_fp),0L);
 }
 rewind(smb.sid_fp);
 ftruncate(fileno(smb.sid_fp),0L);
 if(!(smb.status.attr&2)) {
  length=filelength(fileno(smb.sdt_fp));
  w=0;
  for(l=0;l<length;l+=256)
   fwrite(&w,2,1,smb.sda_fp);
  length=filelength(fileno(smb.shd_fp));
  c=0;
  for(l=0;l<length;l+=256)
   fwrite(&c,1,1,smb.sha_fp);
 } else
  length=filelength(fileno(smb.shd_fp));
 n=0;
 for(l=smb.status.header_offset;l<length;l+=size) {
  size=256;
  printf("\r%2lu%%  ",(long)(100.0/((float)length/l)));
  msg.idx.offset=l;
  if((i=smb_lockmsghdr(&smb,&msg))!=0) {
   printf("\n(%06lX) smb_lockmsghdr returned %d:\n%s\n",l,i,smb.last_error);
   continue;
  }
  i=smb_getmsghdr(&smb,&msg);
  smb_unlockmsghdr(&smb,&msg);
  if(i!=0) {
   printf("\n(%06lX) smb_getmsghdr returned %d:\n%s\n",l,i,smb.last_error);
   continue;
  }
  size=smb_hdrblocks(smb_getmsghdrlen(&msg))*256;
  printf("#%-5lu (%06lX) %-25.25s ",msg.hdr.number,l,msg.from);
  if(msg.hdr.attr&(1<<4))
   text=((void *)0);
  else
   text=smb_getmsgtxt(&smb,&msg,(1<<2));
  i=smb_hashmsg(&smb,&msg,text,1 );
  if(i!=0)
   printf("!ERROR %d hashing message\n", i);
  if(text!=((void *)0))
   free(text);
  if(msg.hdr.attr&(1<<4))
   printf("Not indexing deleted message\n");
  else if(msg.hdr.number==0)
   printf("Not indexing invalid message number (0)!\n");
  else {
   msg.offset=n;
   if(renumber)
    msg.hdr.number=n+1;
   if(msg.hdr.netattr&(1<<1)) {
    printf("Removing 'in transit' attribute\n");
    msg.hdr.netattr&=~(1<<1);
   }
   if((i=smb_putmsg(&smb,&msg))!=0) {
    printf("\nsmb_putmsg returned %d: %s\n",i,smb.last_error);
    continue;
   }
   n++;
  }
  if(!(smb.status.attr&2)) {
   fseek(smb.sha_fp,(l-smb.status.header_offset)/256,0);
   if(msg.hdr.attr&(1<<4)) c=0;
   else c=1;
   for(i=0;i<(int)(size/256);i++)
    fputc(c,smb.sha_fp);
   if(!(msg.hdr.attr&(1<<4)))
    smb_incmsg_dfields(&smb,&msg,1);
  }
  smb_freemsgmem(&msg);
 }
 printf("\r%79s\r100%%\n","");
 smb.status.total_msgs=n;
 if(renumber)
  smb.status.last_msg=n;
 else
  sort_index(&smb);
 printf("Saving message base status (%lu total messages).\n",n);
 if((i=smb_putstatus(&smb))!=0)
  printf("\nsmb_putstatus returned %d: %s\n",i,smb.last_error);
 smb_unlocksmbhdr(&smb);
 printf("Closing message base.\n");
 smb_close(&smb);
 unlock_msgbase();
 printf("Done.\n");
 return(0);
}

variable_types:
sub
char *

variable_types:
p
char *

variable_types:
text
char *

variable_types: c char

variable_types: w int

variable_types:
l
unsigned long

variable_types:
length
unsigned long

variable_types:
size
unsigned long

variable_types:
n
unsigned long

variable_types: struct smbmsg_t { struct idxrec_t { unsigned short to; unsigned short from; unsigned short subj; unsigned short attr; unsigned long offset; unsigned long number; unsigned long time; } idx; struct msghdr_t { unsigned char id[4]; unsigned short type; unsigned short version; unsigned short length; unsigned short attr; unsigned long auxattr; unsigned long netattr; struct when_t { unsigned long time; short zone; } when_written; struct when_t when_imported; unsigned long number; unsigned long thread_back; unsigned long thread_next; unsigned long thread_first; unsigned short delivery_attempts; unsigned long times_downloaded; unsigned long last_downloaded; unsigned char reserved[6]; unsigned long offset; unsigned short total_dfields; } hdr; char *to; char *summary; char *subj; unsigned short to_agent; struct net_t { unsigned short type; void *addr; } to_net; unsigned short total_hfields; struct hfield_t { unsigned short type; unsigned short length; } *hfield; void **hfield_dat; struct dfield_t { unsigned short type; unsigned long offset; unsigned long length; } *dfield; long offset; int forwarded; unsigned long expiration; unsigned long priority; unsigned long cost; unsigned long flags; }

return_type: int

user_defined_types: struct smbmsg_t { struct idxrec_t idx; struct msghdr_t hdr; char *to; char *summary; char *subj; unsigned short to_agent; struct net_t to_net; unsigned short total_hfields; struct hfield_t *hfield; void **hfield_dat; struct dfield_t *dfield; long offset; int forwarded; unsigned long expiration; unsigned long priority; unsigned long cost; unsigned long flags; }

user_defined_types: struct msghdr_t { unsigned char id[4]; unsigned short type; unsigned short version; unsigned short length; unsigned short attr; unsigned long auxattr; unsigned long netattr; struct when_t when_written; struct when_t when_imported; unsigned long number; unsigned long thread_back; unsigned long thread_next; unsigned long thread_first; unsigned short delivery_attempts; unsigned long times_downloaded; unsigned long last_downloaded; unsigned char reserved[6]; unsigned long offset; unsigned short total_dfields; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rax
const char **v4; // rbx
const char **v5; // rbp
const char *v6; // rsi
char *v7; // rdi
__int64 v8; // rbx
char **v10; // [rsp+8h] [rbp-30h] BYREF
char v11[40]; // [rsp+10h] [rbp-28h] BYREF

_isoc99_sscanf("$Revision: 1.30 $", "%*s %s", v11);
v3 = (const char *)smb_lib_ver();
printf("\nFIXSMB v2.10-%s (rev %s) SMBLIB %s - Rebuild Synchronet Message Base\n\n", "Linux", v11, v3);
v10 = (char **)strListInit();
if ( argc > 1 )
{
v4 = argv + 1;
v5 = &argv[(unsigned int)(argc - 2) + 2];
do
{
while ( **v4 != 45 )
{
v6 = *v4++;
strListAppend(&v10, v6, -1LL);
if ( v5 == v4 )
goto LABEL_8;
}
if ( !strcasecmp(*v4, "-renumber") )
renumber = 1;
++v4;
}
while ( v5 != v4 );
}
LABEL_8:
if ( !strListCount(v10) )
{
printf(usage);
exit(1);
}
atexit(unlock_msgbase);
v7 = *v10;
if ( *v10 )
{
v8 = 1LL;
do
{
fixsmb(v7);
v7 = v10[v8++];
}
while ( v7 );
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rax
const char **v4; // rbx
const char **v5; // rbp
const char *v6; // rsi
char *v7; // rdi
__int64 v8; // rbx
char **v10; // [rsp+8h] [rbp-30h] BYREF
char v11[40]; // [rsp+10h] [rbp-28h] BYREF

_isoc99_sscanf("$Revision: 1.30 $", "%*s %s", v11);
v3 = (const char *)smb_lib_ver();
printf("\nFIXSMB v2.10-%s (rev %s) SMBLIB %s - Rebuild Synchronet Message Base\n\n", "Linux", v11, v3);
v10 = (char **)strListInit();
if ( argc > 1 )
{
v4 = argv + 1;
v5 = &argv[(unsigned int)(argc - 2) + 2];
do
{
while ( **v4 != 45 )
{
v6 = *v4++;
strListAppend(&v10, v6, -1LL);
if ( v5 == v4 )
goto LABEL_8;
}
if ( !strcasecmp(*v4, "-renumber") )
renumber = 1;
++v4;
}
while ( v5 != v4 );
}
LABEL_8:
if ( !strListCount(v10) )
{
printf(usage);
exit(1);
}
atexit(unlock_msgbase);
v7 = *v10;
if ( *v10 )
{
v8 = 1LL;
do
{
func3(v7);
v7 = v10[v8++];
}
while ( v7 );
}
return 0;
}

original_code:
int main(int argc, char **argv)
{
 char revision[16];
 int i;
 str_list_t list;
 sscanf("$Revision: 1.30 $", "%*s %s", revision);
 printf("\nFIXSMB v2.10-%s (rev %s) SMBLIB %s - Rebuild Synchronet Message Base\n\n"
  ,"Linux",revision,smb_lib_ver());
 list=strListInit();
 for(i=1;i<argc;i++) {
  if(argv[i][0]=='-') {
   if(!strcasecmp(argv[i],"-renumber"))
    renumber=1;
  } else
   strListAppend(&list, argv[i], (~0));
 }
 if(!strListCount(list)) {
  printf(usage);
  exit(1);
 }
 atexit(unlock_msgbase);
 for(i=0;list[i]!=((void *)0);i++)
  fixsmb(list[i]);
 return(0);
}

canonical_original_code:
int main(int argc, char **argv)
{
 char revision[16];
 int i;
 char **list;
 sscanf("$Revision: 1.30 $", "%*s %s", revision);
 printf("\nFIXSMB v2.10-%s (rev %s) SMBLIB %s - Rebuild Synchronet Message Base\n\n"
  ,"Linux",revision,smb_lib_ver());
 list=strListInit();
 for(i=1;i<argc;i++) {
  if(argv[i][0]=='-') {
   if(!strcasecmp(argv[i],"-renumber"))
    renumber=1;
  } else
   strListAppend(&list, argv[i], (~0));
 }
 if(!strListCount(list)) {
  printf(usage);
  exit(1);
 }
 atexit(unlock_msgbase);
 for(i=0;list[i]!=((void *)0);i++)
  fixsmb(list[i]);
 return(0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
revision
char [16]

variable_types:
list
char **


decompiled_code:
__int64 __fastcall sem_trywait_block(__int64 a1, __int64 a2)
{
unsigned int v2; // er12
int *v3; // rax
__int64 v5[2]; // [rsp+0h] [rbp-38h] BYREF
struct timeval tv; // [rsp+10h] [rbp-28h] BYREF

gettimeofday(&tv, 0LL);
v5[0] = tv.tv_sec + (a2 + tv.tv_usec / 1000) / 0x3E8uLL;
v5[1] = (1000000 * a2 + 1000 * tv.tv_usec) % 0x3B9ACA00uLL;
v2 = xp_sem_timedwait(a1, v5);
if ( !v2 )
return v2;
v3 = _errno_location();
if ( *v3 != 110 )
return v2;
*v3 = 11;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
unsigned int v2; // er12
int *v3; // rax
__int64 v5[2]; // [rsp+0h] [rbp-38h] BYREF
struct timeval tv; // [rsp+10h] [rbp-28h] BYREF

gettimeofday(&tv, 0LL);
v5[0] = tv.tv_sec + (a2 + tv.tv_usec / 1000) / 0x3E8uLL;
v5[1] = (1000000 * a2 + 1000 * tv.tv_usec) % 0x3B9ACA00uLL;
v2 = xp_sem_timedwait(a1, v5);
if ( !v2 )
return v2;
v3 = _errno_location();
if ( *v3 != 110 )
return v2;
*v3 = 11;
return v2;
}

original_code:
int
sem_trywait_block(xp_sem_t *sem, unsigned long timeout)
{
 int retval;
 struct timespec abstime;
 struct timeval currtime;
 gettimeofday(&currtime,((void *)0));
 abstime.tv_sec=currtime.tv_sec + (currtime.tv_usec/1000 + timeout)/1000;
 abstime.tv_nsec=(currtime.tv_usec*1000 + timeout*1000000)%1000000000;
 retval=xp_sem_timedwait(sem,&abstime);
 if(retval && (*__errno_location ())==110)
  (*__errno_location ())=11;
 return retval;
}

canonical_original_code:
int sem_trywait_block(struct xp_sem **sem, unsigned long timeout)
{
 int retval;
 struct timespec abstime;
 struct timeval currtime;
 gettimeofday(&currtime,((void *)0));
 abstime.tv_sec=currtime.tv_sec + (currtime.tv_usec/1000 + timeout)/1000;
 abstime.tv_nsec=(currtime.tv_usec*1000 + timeout*1000000)%1000000000;
 retval=xp_sem_timedwait(sem,&abstime);
 if(retval && (*__errno_location ())==110)
  (*__errno_location ())=11;
 return retval;
}

variable_types: struct xp_sem { unsigned int magic; 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; } lock; 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; } gtzero; unsigned int count; unsigned int nwaiters; } **

variable_types:
timeout
unsigned long

variable_types: retval int

variable_types: struct timespec { long tv_sec; long tv_nsec; }

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

user_defined_types: struct xp_sem { unsigned int magic; union pthread_mutex_t lock; union pthread_cond_t gtzero; unsigned int count; unsigned int nwaiters; }

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


usage

repo: fugalh/jhat

decompiled_code:
void __fastcall __noreturn usage(const char *a1)
{
fprintf(stderr, "usage: %s <joystick device path> <x axis> <z axis>\n\n", a1);
fwrite("See /proc/bus/input/devices for the path.\n", 1uLL, 0x2AuLL, stderr);
fwrite("Use jstest to figure out the axes.\n", 1uLL, 0x23uLL, stderr);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(const char *a1)
{
fprintf(stderr, "usage: %s <joystick device path> <x axis> <z axis>\n\n", a1);
fwrite("See /proc/bus/input/devices for the path.\n", 1uLL, 0x2AuLL, stderr);
fwrite("Use jstest to figure out the axes.\n", 1uLL, 0x23uLL, stderr);
exit(1);
}

original_code:
void usage(char *argv0)
{
    fprintf(stderr, "usage: %s <joystick device path> <x axis> <z axis>\n\n", argv0);
    fprintf(stderr, "See /proc/bus/input/devices for the path.\n");
    fprintf(stderr, "Use jstest to figure out the axes.\n");
    exit(1);
}


keyup

decompiled_code:
ssize_t __fastcall keyup(int fd, void *buf, _DWORD *a3)
{
ssize_t result; // rax
size_t v5; // rax

result = (unsigned int)*a3;
if ( (_DWORD)result )
{
write(fd, "keyup ", 6uLL);
v5 = strlen((const char *)buf);
write(fd, buf, v5);
result = write(fd, "\n", 1uLL);
*a3 = 0;
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func8(int fd, void *buf, _DWORD *a3)
{
ssize_t result; // rax
size_t v5; // rax

result = (unsigned int)*a3;
if ( (_DWORD)result )
{
write(fd, "keyup ", 6uLL);
v5 = strlen((const char *)buf);
write(fd, buf, v5);
result = write(fd, "\n", 1uLL);
*a3 = 0;
}
return result;
}

original_code:
void keyup(int xte_fd, char *key, int *state)
{
    if (*state)
    {
 write(xte_fd, "keyup ", 6);
 write(xte_fd, key, strlen(key));
 write(xte_fd, "\n", 1);
 *state = 0;
    }
}

variable_types:
key
char *

variable_types:
state
int *


keydown

decompiled_code:
ssize_t __fastcall keydown(int fd, void *buf, _DWORD *a3)
{
ssize_t result; // rax
size_t v5; // rax

result = (unsigned int)*a3;
if ( !(_DWORD)result )
{
write(fd, "keydown ", 8uLL);
v5 = strlen((const char *)buf);
write(fd, buf, v5);
result = write(fd, "\n", 1uLL);
*a3 = 1;
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func9(int fd, void *buf, _DWORD *a3)
{
ssize_t result; // rax
size_t v5; // rax

result = (unsigned int)*a3;
if ( !(_DWORD)result )
{
write(fd, "keydown ", 8uLL);
v5 = strlen((const char *)buf);
write(fd, buf, v5);
result = write(fd, "\n", 1uLL);
*a3 = 1;
}
return result;
}

original_code:
void keydown(int xte_fd, char *key, int *state)
{
    if (!*state)
    {
 write(xte_fd, "keydown ", 8);
 write(xte_fd, key, strlen(key));
 write(xte_fd, "\n", 1);
 *state = 1;
    }
}


keyboard

repo: fujiruki/Harvester

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

if ( a1 == 27 )
{
EXIT = 0;
}
else
{
switch ( a1 )
{
case 'a':
if ( L_BTN )
L_BTN = 0;
break;
case 'd':
result = (unsigned int)R_BTN;
if ( R_BTN )
R_BTN = 0;
break;
case 'j':
if ( BTN1 )
BTN1 = 0;
break;
case 'k':
if ( BTN2 )
BTN2 = 0;
break;
case 's':
if ( D_BTN )
D_BTN = 0;
break;
case 'w':
if ( U_BTN )
U_BTN = 0;
break;
default:
return result;
}
}
return result;
}

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

if ( a1 == 27 )
{
EXIT = 0;
}
else
{
switch ( a1 )
{
case 'a':
if ( L_BTN )
L_BTN = 0;
break;
case 'd':
result = (unsigned int)R_BTN;
if ( R_BTN )
R_BTN = 0;
break;
case 'j':
if ( BTN1 )
BTN1 = 0;
break;
case 'k':
if ( BTN2 )
BTN2 = 0;
break;
case 's':
if ( D_BTN )
D_BTN = 0;
break;
case 'w':
if ( U_BTN )
U_BTN = 0;
break;
default:
return result;
}
}
return result;
}

original_code:
void keyboard(unsigned char key, int x, int y)
{
 switch (key)
 {
  case '\033':
   if ('\033' != 0) {
    EXIT = 0;
   }
   break;
  case 'j':
   if (BTN1 != 0) {
    BTN1 = 0;
   }
   break;
  case 'k':
   if (BTN2 != 0) {
    BTN2 = 0;
   }
   break;
  case 'w':
   if (U_BTN != 0) {
    U_BTN = 0;
   }
   break;
  case 's':
   if (D_BTN != 0) {
    D_BTN = 0;
   }
   break;
  case 'a':
   if (L_BTN != 0) {
    L_BTN = 0;
   }
   break;
  case 'd':
   if (R_BTN != 0) {
    R_BTN = 0;
   }
   break;
  default:
   break;
 }
}

variable_types:
key
unsigned char

variable_types: x int

variable_types: y int


specialkey

decompiled_code:
void specialkey()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void specialkey(int key, int x, int y)
{
 switch (key) {
 }
}

variable_types: key int


mouse

decompiled_code:
void mouse()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

original_code:
void mouse(int button, int state, int x, int y)
{
 switch (button) {
  case 0x0000:
   break;
  case 0x0001:
   break;
  case 0x0002:
   break;
  default:
   break;
 }
}

variable_types: button int

variable_types: state int


decompiled_code:
void __fastcall keyboard_up(char a1)
{
if ( a1 == 27 )
{
EXIT = 1;
exit(0);
}
switch ( a1 )
{
case 'a':
if ( L_BTN != 1 )
L_BTN = 1;
break;
case 'd':
if ( R_BTN != 1 )
R_BTN = 1;
break;
case 'j':
if ( BTN1 != 1 )
BTN1 = 1;
break;
case 'k':
if ( BTN2 != 1 )
BTN2 = 1;
break;
case 's':
if ( D_BTN != 1 )
D_BTN = 1;
break;
case 'w':
if ( U_BTN != 1 )
U_BTN = 1;
break;
default:
return;
}
}

canonical_decompiled_code:
void __fastcall func3(char a1)
{
if ( a1 == 27 )
{
EXIT = 1;
exit(0);
}
switch ( a1 )
{
case 'a':
if ( L_BTN != 1 )
L_BTN = 1;
break;
case 'd':
if ( R_BTN != 1 )
R_BTN = 1;
break;
case 'j':
if ( BTN1 != 1 )
BTN1 = 1;
break;
case 'k':
if ( BTN2 != 1 )
BTN2 = 1;
break;
case 's':
if ( D_BTN != 1 )
D_BTN = 1;
break;
case 'w':
if ( U_BTN != 1 )
U_BTN = 1;
break;
default:
return;
}
}

original_code:
void keyboard_up(unsigned char key, int x, int y)
{
 switch (key)
 {
  case '\033':
   if ('\033' != 1) {
    EXIT = 1;
    exit(0);
   }
  case 'j':
   if (BTN1 != 1) {
    BTN1 = 1;
   }
   break;
  case 'k':
   if (BTN2 != 1) {
    BTN2 = 1;
   }
   break;
  case 'w':
   if (U_BTN != 1) {
    U_BTN = 1;
   }
   break;
  case 's':
   if (D_BTN != 1) {
    D_BTN = 1;
   }
   break;
  case 'a':
   if (L_BTN != 1) {
    L_BTN = 1;
   }
   break;
  case 'd':
   if (R_BTN != 1) {
    R_BTN = 1;
   }
   break;
  default:
   break;
 }
}


decompiled_code:
void specialkey_up()
{
;
}

canonical_decompiled_code:
void func5()
{
;
}

original_code:
void specialkey_up(int key, int x, int y)
{
 switch (key) {
 }
}


decompiled_code:
__int64 __fastcall wait_ms(int a1)
{
int v1; // ebx
clock_t v2; // rax

v1 = clock();
do
{
v2 = clock();
if ( 1000.0 * (double)a1 <= (double)((int)v2 - v1) )
return 0LL;
}
while ( v2 != -1 );
fwrite(&unk_7B10, 1uLL, 0x1FuLL, stderr);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
int v1; // ebx
clock_t v2; // rax

v1 = clock();
do
{
v2 = clock();
if ( 1000.0 * (double)a1 <= (double)((int)v2 - v1) )
return 0LL;
}
while ( v2 != -1 );
fwrite(&unk_7B10, 1uLL, 0x1FuLL, stderr);
return 1LL;
}

original_code:
int wait_ms(long t)
{
 clock_t s_c, e_c;
 s_c = clock();
 while ((e_c=clock())-s_c < ((__clock_t) 1000000)/1000.0*t)
 {
  if (e_c == (clock_t)-1) {
   fprintf(stderr, "clock()が異常な戻り値。");
   return 1;
  }
 }
 return 0;
}

canonical_original_code:
int wait_ms(long t)
{
 long s_c; long e_c;
 s_c = clock();
 while ((e_c=clock())-s_c < ((long) 1000000)/1000.0*t)
 {
  if (e_c == (clock_t)-1) {
   fprintf(stderr, "clock()が異常な戻り値。");
   return 1;
  }
 }
 return 0;
}

variable_types: t long


original_code:
void disp_frame()
{
 int x, y;
 for (x=0; x<60; x++) {
  for (y=0; y<60; y++) {
   emu_disp_pat[y][x] = pat[y][x];
  }
 }
 wait_ms(40);
}

canonical_original_code:
void disp_frame()
{
 int x; int y;
 for (x=0; x<60; x++) {
  for (y=0; y<60; y++) {
   emu_disp_pat[y][x] = pat[y][x];
  }
 }
 wait_ms(40);
}


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

for ( result = (void *)game_init(a1); !EXIT; result = (void *)disp_frame() )
game_main();
return result;
}

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

for ( result = (void *)game_init(a1); !EXIT; result = (void *)func7() )
game_main();
return result;
}

original_code:
void *emu_loop(void* arg)
{
 game_init();
 while (EXIT == 0) {
  game_main();
  disp_frame();
 }
}

canonical_original_code:
void *emu_loop(void *arg)
{
 game_init();
 while (EXIT == 0) {
  game_main();
  disp_frame();
 }
}

variable_types:
arg
void *

return_type: void *


decompiled_code:
__int64 __fastcall pat2rgb(int a1, double *a2, double *a3, double *a4)
{
__int64 result; // rax

result = (a1 >> 4) & 3;
*a4 = (double)((a1 >> 2) & 3) / 3.0;
*a2 = (double)(int)result / 3.0;
*a3 = (double)((a1 >> 6) & 3) / 3.0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, double *a2, double *a3, double *a4)
{
__int64 result; // rax

result = (a1 >> 4) & 3;
*a4 = (double)((a1 >> 2) & 3) / 3.0;
*a2 = (double)(int)result / 3.0;
*a3 = (double)((a1 >> 6) & 3) / 3.0;
return result;
}

original_code:
void pat2rgb(int draw_pat, double *r, double *g, double *b)
{
 int pat_color = draw_pat >> 2;
 *b = (pat_color & 3) / 3.0;
 pat_color >>= 2;
 *r = (pat_color & 3) / 3.0;
 pat_color >>= 2;
 *g = (pat_color & 3) / 3.0;
 return;
}

variable_types:
r
double *

variable_types:
g
double *

variable_types:
b
double *


decompiled_code:
__int64 __fastcall Emu_draw_rect(char a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8)
{
glColor3d(a6, a7, a8);
if ( a1 <= 0 )
glBegin(2LL);
else
glBegin(9LL);
glVertex2d(a2, a3);
glVertex2d(a4 + a2, a3);
glVertex2d(a4 + a2, a3 + a5);
glVertex2d(a2, a3 + a5);
return glEnd();
}

canonical_decompiled_code:
__int64 __fastcall func10(char a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8)
{
glColor3d(a6, a7, a8);
if ( a1 <= 0 )
glBegin(2LL);
else
glBegin(9LL);
glVertex2d(a2, a3);
glVertex2d(a4 + a2, a3);
glVertex2d(a4 + a2, a3 + a5);
glVertex2d(a2, a3 + a5);
return glEnd();
}

original_code:
void Emu_draw_rect(double x, double y, double w, double h, double r, double g, double b, char fill)
{
 glColor3d(r, g, b);
 if (fill>0) {
  glBegin(0x0009);
 } else {
  glBegin(0x0002);
 }
 glVertex2d(x, y);
 glVertex2d(x+w, y);
 glVertex2d(x+w, y+h);
 glVertex2d(x, y+h);
 glEnd();
}

variable_types: x double

variable_types: y double

variable_types: w double

variable_types: h double

variable_types: r double

variable_types: g double

variable_types: b double

variable_types: fill char


decompiled_code:
__int64 emu_disp_cells()
{
int *v0; // r12
int v1; // ebp
int *v2; // r13
int i; // ebx
int v4; // edi
double v5; // xmm1_8
__int64 result; // rax
double v7; // [rsp+0h] [rbp-58h]
double v8; // [rsp+8h] [rbp-50h]
double v9; // [rsp+18h] [rbp-40h] BYREF
double v10; // [rsp+20h] [rbp-38h] BYREF
double v11[6]; // [rsp+28h] [rbp-30h] BYREF

v0 = &emu_disp_pat;
v1 = 0;
v7 = *(double *)&disp_ortho;
v8 = *((double *)&disp_ortho + 1);
do
{
v2 = v0;
for ( i = 0; i != 60; ++i )
{
v4 = *v2;
v2 += 60;
pat2rgb(v4, &v9, &v10, v11);
v5 = (double)i;
result = Emu_draw_rect(
1,
(double)v1 * *(double *)&cell_box + v7 + *(double *)&border_w,
v5 * *((double *)&cell_box + 1) + v8 + *(double *)&border_w,
*(double *)&cell_size,
*((double *)&cell_size + 1),
v9,
v10,
v11[0]);
}
++v1;
++v0;
}
while ( v1 != 60 );
return result;
}

canonical_decompiled_code:
__int64 func11()
{
int *v0; // r12
int v1; // ebp
int *v2; // r13
int i; // ebx
int v4; // edi
double v5; // xmm1_8
__int64 result; // rax
double v7; // [rsp+0h] [rbp-58h]
double v8; // [rsp+8h] [rbp-50h]
double v9; // [rsp+18h] [rbp-40h] BYREF
double v10; // [rsp+20h] [rbp-38h] BYREF
double v11[6]; // [rsp+28h] [rbp-30h] BYREF

v0 = &emu_disp_pat;
v1 = 0;
v7 = *(double *)&disp_ortho;
v8 = *((double *)&disp_ortho + 1);
do
{
v2 = v0;
for ( i = 0; i != 60; ++i )
{
v4 = *v2;
v2 += 60;
func9(v4, &v9, &v10, v11);
v5 = (double)i;
result = func10(
1,
(double)v1 * *(double *)&cell_box + v7 + *(double *)&border_w,
v5 * *((double *)&cell_box + 1) + v8 + *(double *)&border_w,
*(double *)&cell_size,
*((double *)&cell_size + 1),
v9,
v10,
v11[0]);
}
++v1;
++v0;
}
while ( v1 != 60 );
return result;
}

original_code:
void emu_disp_cells()
{
 int x, y;
 double r, g, b;
 posd offset;
 offset.x = disp_ortho.x;
 offset.y = disp_ortho.y;
 for (x=0; x<60; x++) {
  for (y=0; y<60; y++) {
   pat2rgb(emu_disp_pat[y][x], &r, &g, &b);
   Emu_draw_rect(offset.x + x*cell_box.w +border_w,
     offset.y + y*cell_box.h +border_w, cell_size.w, cell_size.h, r, g, b, 1);
  }
 }
}

canonical_original_code:
void emu_disp_cells()
{
 int x; int y;
 double r; double g; double b;
 struct posd offset;
 offset.x = disp_ortho.x;
 offset.y = disp_ortho.y;
 for (x=0; x<60; x++) {
  for (y=0; y<60; y++) {
   pat2rgb(emu_disp_pat[y][x], &r, &g, &b);
   Emu_draw_rect(offset.x + x*cell_box.w +border_w,
     offset.y + y*cell_box.h +border_w, cell_size.w, cell_size.h, r, g, b, 1);
  }
 }
}

variable_types: x int

variable_types: y int

variable_types:
offset
struct posd { double x; }


display

decompiled_code:
__int64 display()
{
glClear(0x4000LL);
emu_disp_cells();
return glutSwapBuffers();
}

canonical_decompiled_code:
__int64 func12()
{
glClear(0x4000LL);
func11();
return glutSwapBuffers();
}

original_code:
void display(void)
{
 int x, y;
 double r,g,b;
 glClear(0x00004000);
 emu_disp_cells();
 glutSwapBuffers();
}

canonical_original_code:
void display()
{
 int x; int y;
 double r; double g; double b;
 glClear(0x00004000);
 emu_disp_cells();
 glutSwapBuffers();
}


decompiled_code:
void set_cell_size()
{
double v0; // xmm3_8
double v1; // xmm0_8
double v2; // xmm1_8
double v3; // xmm2_8

v0 = 61.0 * *(double *)&border_rate + 60.0;
v1 = *((double *)&disp_ortho + 2) / v0;
v2 = *((double *)&disp_ortho + 3) / v0;
v3 = *(double *)&border_rate * v1;
*(double *)&cell_size = v1;
*(double *)&border_w = *(double *)&border_rate * v1;
*(double *)&cell_box = v1 + *(double *)&border_rate * v1;
*((double *)&cell_size + 1) = v2;
*((double *)&cell_box + 1) = v2 + v3;
}

canonical_decompiled_code:
void func13()
{
double v0; // xmm3_8
double v1; // xmm0_8
double v2; // xmm1_8
double v3; // xmm2_8

v0 = 61.0 * *(double *)&border_rate + 60.0;
v1 = *((double *)&disp_ortho + 2) / v0;
v2 = *((double *)&disp_ortho + 3) / v0;
v3 = *(double *)&border_rate * v1;
*(double *)&cell_size = v1;
*(double *)&border_w = *(double *)&border_rate * v1;
*(double *)&cell_box = v1 + *(double *)&border_rate * v1;
*((double *)&cell_size + 1) = v2;
*((double *)&cell_box + 1) = v2 + v3;
}

original_code:
void set_cell_size()
{
 cell_size.w = disp_ortho.w/( 60 + (60 +1)*border_rate );
 cell_size.h = disp_ortho.h/( 60 + (60 +1)*border_rate );
 border_w = border_rate * cell_size.w;
 cell_box.w = border_w + cell_size.w;
 cell_box.h = border_w + cell_size.h;
}


resize

decompiled_code:
__int64 __fastcall resize(int a1, int a2)
{
double v2; // xmm0_8
double v3; // xmm1_8

bdisp[2] = (double)a1;
bdisp[3] = (double)a2;
glViewport(0LL, 0LL, (unsigned int)a1, (unsigned int)a2);
glLoadIdentity();
v2 = (double)a1 / 200.0 + (double)a1 / 200.0;
*((double *)&disp_ortho + 2) = v2;
*(double *)&disp_ortho = COERCE_DOUBLE(*(_QWORD *)&v2 ^ LC6) * 0.5;
v3 = (double)a2 / 200.0 + (double)a2 / 200.0;
*((double *)&disp_ortho + 3) = v3;
*((double *)&disp_ortho + 1) = COERCE_DOUBLE(*(_QWORD *)&v3 ^ LC6) * 0.5;
set_cell_size();
return glOrtho(
*(double *)&disp_ortho,
*((double *)&disp_ortho + 2) + *(double *)&disp_ortho,
*((double *)&disp_ortho + 1) + *((double *)&disp_ortho + 3),
*((double *)&disp_ortho + 1),
-1.0,
1.0);
}

canonical_decompiled_code:
__int64 __fastcall func14(int a1, int a2)
{
double v2; // xmm0_8
double v3; // xmm1_8

bdisp[2] = (double)a1;
bdisp[3] = (double)a2;
glViewport(0LL, 0LL, (unsigned int)a1, (unsigned int)a2);
glLoadIdentity();
v2 = (double)a1 / 200.0 + (double)a1 / 200.0;
*((double *)&disp_ortho + 2) = v2;
*(double *)&disp_ortho = COERCE_DOUBLE(*(_QWORD *)&v2 ^ LC6) * 0.5;
v3 = (double)a2 / 200.0 + (double)a2 / 200.0;
*((double *)&disp_ortho + 3) = v3;
*((double *)&disp_ortho + 1) = COERCE_DOUBLE(*(_QWORD *)&v3 ^ LC6) * 0.5;
func13();
return glOrtho(
*(double *)&disp_ortho,
*((double *)&disp_ortho + 2) + *(double *)&disp_ortho,
*((double *)&disp_ortho + 1) + *((double *)&disp_ortho + 3),
*((double *)&disp_ortho + 1),
-1.0,
1.0);
}

original_code:
void resize(int w, int h)
{
 bdisp.w = w;
 bdisp.h = h;
 glViewport(0, 0, bdisp.w, bdisp.h);
 glLoadIdentity();
 disp_ortho.w = w/200.0*2;
 disp_ortho.h = h/200.0*2;
 disp_ortho.y = -disp_ortho.h/2.0;
 disp_ortho.x = -disp_ortho.w/2.0;
 set_cell_size();
 glOrtho(disp_ortho.x, disp_ortho.x+disp_ortho.w,
   disp_ortho.y+disp_ortho.h, disp_ortho.y, -1.0, 1.0);
}

variable_types: w int

variable_types: h int


decompiled_code:
// attributes: thunk
__int64 display_loop()
{
return glutPostRedisplay();
}

canonical_decompiled_code:
__int64 func15()
{
return glutPostRedisplay();
}

original_code:
void display_loop()
{
 glutPostRedisplay();
}


decompiled_code:
__int64 timer_display()
{
display_loop();
return glutTimerFunc(16LL, timer_display, 0LL);
}

canonical_decompiled_code:
__int64 func16()
{
func15();
return glutTimerFunc(16LL, timer_display, 0LL);
}

original_code:
void timer_display(int value)
{
 display_loop();
 glutTimerFunc(16, timer_display, 0);
}

variable_types: value int


decompiled_code:
int __fastcall print_bit(int a1)
{
int v1; // ebx
int v2; // esi
int result; // eax

v1 = 0;
printf("%d : ", (unsigned int)a1);
do
{
v2 = a1 >> v1++;
result = printf("%d", (((a1 < 0) + (_BYTE)v2) & 1) - ((unsigned int)a1 >> 31));
}
while ( v1 != 9 );
return result;
}

canonical_decompiled_code:
int __fastcall func18(int a1)
{
int v1; // ebx
int v2; // esi
int result; // eax

v1 = 0;
printf("%d : ", (unsigned int)a1);
do
{
v2 = a1 >> v1++;
result = printf("%d", (((a1 < 0) + (_BYTE)v2) & 1) - ((unsigned int)a1 >> 31));
}
while ( v1 != 9 );
return result;
}

original_code:
void print_bit(int n)
{
 int i, bit;
 printf("%d : ", n);
 for (i=0; i<=8; i++) {
  printf("%d",(n >> i) % 2);
 }
}

canonical_original_code:
void print_bit(int n)
{
 int i; int bit;
 printf("%d : ", n);
 for (i=0; i<=8; i++) {
  printf("%d",(n >> i) % 2);
 }
}

variable_types: n int

variable_types: i int

variable_types: bit int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4[3]; // [rsp+Ch] [rbp-1Ch] BYREF
pthread_t newthread[2]; // [rsp+18h] [rbp-10h] BYREF

v4[0] = argc;
init_emu((__int64)v4, argv);
if ( pthread_create(newthread, 0LL, emu_loop, 0LL) )
{
fwrite(&unk_7B30, 1uLL, 0x16uLL, stderr);
exit(-1);
}
glutMainLoop();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4[3]; // [rsp+Ch] [rbp-1Ch] BYREF
pthread_t newthread[2]; // [rsp+18h] [rbp-10h] BYREF

v4[0] = argc;
func17((__int64)v4, argv);
if ( pthread_create(newthread, 0LL, emu_loop, 0LL) )
{
fwrite(&unk_7B30, 1uLL, 0x16uLL, stderr);
exit(-1);
}
glutMainLoop();
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 pthread_t game_thread;
 int game_thread_status;
 init_emu(&argc, argv);
 game_thread_status = pthread_create(&game_thread, ((void *)0), emu_loop, 0);
 if (game_thread_status!=0) {
  fprintf(stderr, "pthreadでのエラー");
  exit(-1);
 }
 glutMainLoop();
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 unsigned long game_thread;
 int game_thread_status;
 init_emu(&argc, argv);
 game_thread_status = pthread_create(&game_thread, ((void *)0), emu_loop, 0);
 if (game_thread_status!=0) {
  fprintf(stderr, "pthreadでのエラー");
  exit(-1);
 }
 glutMainLoop();
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


plot

decompiled_code:
__int64 __fastcall plot(unsigned int a1, unsigned int a2, int a3)
{
__int64 result; // rax

if ( a1 <= 0x3B && a2 <= 0x3B )
{
result = (int)a1 + 60LL * (int)a2;
*((_DWORD *)&pat + result) = a3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, unsigned int a2, int a3)
{
__int64 result; // rax

if ( a1 <= 0x3B && a2 <= 0x3B )
{
result = (int)a1 + 60LL * (int)a2;
*((_DWORD *)&pat + result) = a3;
}
return result;
}

original_code:
void plot(int x, int y, int color)
{
 if (0 <= x && x < 60 && 0<=y && y < 60)
 {
  pat[y][x] = color;
 }
}

variable_types: color int


decompiled_code:
__int64 __fastcall draw_line(signed int a1, signed int a2, int a3, int a4, int a5)
{
unsigned int v5; // er10
unsigned int v6; // er9
int v7; // ebx
int v8; // er13
int v9; // er12
int v10; // ebp
int v11; // ebx
__int64 result; // rax
int v13; // er11
int v14; // ecx
int v15; // ebp
int v16; // er11
int v17; // ecx

v5 = a1;
v6 = a2;
if ( a3 <= a1 )
{
v7 = a1 - a3;
v8 = -1;
if ( a4 > a2 )
{
v9 = 1;
v10 = a4 - a2;
}
else
{
v9 = -1;
v10 = a2 - a4;
}
}
else
{
v7 = a3 - a1;
if ( a4 > a2 )
{
v8 = 1;
v9 = 1;
v10 = a4 - a2;
if ( v7 <= a4 - a2 )
goto LABEL_4;
goto LABEL_12;
}
v8 = 1;
v9 = -1;
v10 = a2 - a4;
}
if ( v7 <= v10 )
{
LABEL_4:
v11 = 2 * v7;
do
{
result = plot(v5, v6, a5);
if ( v11 + v14 >= 0 )
v5 += v8;
}
while ( v13 + 1 <= v10 );
return result;
}
LABEL_12:
v15 = 2 * v10;
do
{
result = plot(v5, v6, a5);
if ( v15 + v17 >= 0 )
v6 += v9;
}
while ( v7 >= v16 + 1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(signed int a1, signed int a2, int a3, int a4, int a5)
{
unsigned int v5; // er10
unsigned int v6; // er9
int v7; // ebx
int v8; // er13
int v9; // er12
int v10; // ebp
int v11; // ebx
__int64 result; // rax
int v13; // er11
int v14; // ecx
int v15; // ebp
int v16; // er11
int v17; // ecx

v5 = a1;
v6 = a2;
if ( a3 <= a1 )
{
v7 = a1 - a3;
v8 = -1;
if ( a4 > a2 )
{
v9 = 1;
v10 = a4 - a2;
}
else
{
v9 = -1;
v10 = a2 - a4;
}
}
else
{
v7 = a3 - a1;
if ( a4 > a2 )
{
v8 = 1;
v9 = 1;
v10 = a4 - a2;
if ( v7 <= a4 - a2 )
goto LABEL_4;
goto LABEL_12;
}
v8 = 1;
v9 = -1;
v10 = a2 - a4;
}
if ( v7 <= v10 )
{
LABEL_4:
v11 = 2 * v7;
do
{
result = func0(v5, v6, a5);
if ( v11 + v14 >= 0 )
v5 += v8;
}
while ( v13 + 1 <= v10 );
return result;
}
LABEL_12:
v15 = 2 * v10;
do
{
result = func0(v5, v6, a5);
if ( v15 + v17 >= 0 )
v6 += v9;
}
while ( v7 >= v16 + 1 );
return result;
}

original_code:
void draw_line(int x1, int y1, int x2, int y2, int color)
{
 int i;
 Pos dist, direc;
 int E;
 dist.x = (x2 > x1) ? (x2-x1):(x1-x2);
 dist.y = (y2 > y1) ? (y2-y1):(y1-y2);
 direc.x = (x2 > x1) ? 1 : -1;
 direc.y = (y2 > y1) ? 1 : -1;
 if ( dist.x > dist.y )
 {
  E = -dist.x;
  for ( i = 0 ; i <= dist.x ; i++ ) {
   plot( x1, y1, color );
   x1 += direc.x;
   E += 2 * dist.y;
   if ( E >= 0 ) {
    y1 += direc.y;
    E -= 2 * dist.x;
   }
  }
 } else {
  E = -dist.y;
  for ( i = 0 ; i <= dist.y ; i++ ) {
   plot( x1, y1, color );
   y1 += direc.y;
   E += 2 * dist.x;
   if ( E >= 0 ) {
    x1 += direc.x;
    E -= 2 * dist.y;
   }
  }
 }
}

canonical_original_code:
void draw_line(int x1, int y1, int x2, int y2, int color)
{
 int i;
 struct Pos dist; struct Pos direc;
 int E;
 dist.x = (x2 > x1) ? (x2-x1):(x1-x2);
 dist.y = (y2 > y1) ? (y2-y1):(y1-y2);
 direc.x = (x2 > x1) ? 1 : -1;
 direc.y = (y2 > y1) ? 1 : -1;
 if ( dist.x > dist.y )
 {
  E = -dist.x;
  for ( i = 0 ; i <= dist.x ; i++ ) {
   plot( x1, y1, color );
   x1 += direc.x;
   E += 2 * dist.y;
   if ( E >= 0 ) {
    y1 += direc.y;
    E -= 2 * dist.x;
   }
  }
 } else {
  E = -dist.y;
  for ( i = 0 ; i <= dist.y ; i++ ) {
   plot( x1, y1, color );
   y1 += direc.y;
   E += 2 * dist.x;
   if ( E >= 0 ) {
    x1 += direc.x;
    E -= 2 * dist.y;
   }
  }
 }
}

variable_types:
dist
struct Pos { int x; }

variable_types:
direc
struct Pos { int x; }

variable_types: E int


fillBox

decompiled_code:
__int64 __fastcall fillBox(int a1, int a2, int a3, int a4, int a5)
{
__int64 result; // rax
int v8; // esi
int v9; // ecx
__int64 v10; // rdx
unsigned __int64 v11; // rdi

result = (unsigned int)a4;
if ( a1 <= 59 && a2 <= 59 && a3 >= 0 && a4 >= 0 )
{
v8 = 0;
if ( a2 >= 0 && a1 >= 0 )
v8 = a1;
v9 = a3;
if ( a3 > 60 )
v9 = 60;
if ( (int)result > 60 )
result = 60LL;
if ( a2 < (int)result )
{
v10 = 240LL * a2;
v11 = 240 * (a2 + (unsigned __int64)(unsigned int)(result - 1 - a2) + 1);
do
{
result = v8;
if ( v8 < v9 )
{
do
*(_DWORD *)((char *)&pat + 4 * result++ + v10) = a5;
while ( v9 > (int)result );
}
v10 += 240LL;
}
while ( v10 != v11 );
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2, int a3, int a4, int a5)
{
__int64 result; // rax
int v8; // esi
int v9; // ecx
__int64 v10; // rdx
unsigned __int64 v11; // rdi

result = (unsigned int)a4;
if ( a1 <= 59 && a2 <= 59 && a3 >= 0 && a4 >= 0 )
{
v8 = 0;
if ( a2 >= 0 && a1 >= 0 )
v8 = a1;
v9 = a3;
if ( a3 > 60 )
v9 = 60;
if ( (int)result > 60 )
result = 60LL;
if ( a2 < (int)result )
{
v10 = 240LL * a2;
v11 = 240 * (a2 + (unsigned __int64)(unsigned int)(result - 1 - a2) + 1);
do
{
result = v8;
if ( v8 < v9 )
{
do
*(_DWORD *)((char *)&pat + 4 * result++ + v10) = a5;
while ( v9 > (int)result );
}
v10 += 240LL;
}
while ( v10 != v11 );
}
}
return result;
}

variable_types: j int


original_code:
void simu_chara_spring(Chara *obj)
{
 float *vec, *vec_prev, f;
 float y_tmp, vy;
 int i;
 for (i=0; i<2; i++)
 {
  if (i==0) {
   vec = &(obj->p.y);
   vec_prev = &(obj->p_prev.y);
   f = obj->f.y = 0.02*(*vec - jiki.p.y);
  } else {
   vec = &(obj->p.x);
   vec_prev = &(obj->p_prev.x);
   f = obj->f.x = 0.02*(*vec - jiki.p.x);
  }
  y_tmp = *vec;
  vy = (-(*vec - *vec_prev) + f);
  if (vy < -2 || vy > 2) {
   vy = (*vec < *vec_prev) ? 2 : -2;
  }
  vy *= 0.98;
  *vec -= vy;
  *vec_prev = y_tmp;
 }
}

canonical_original_code:
void simu_chara_spring(struct Chara *obj)
{
 float *vec; float *vec_prev; float f;
 float y_tmp; float vy;
 int i;
 for (i=0; i<2; i++)
 {
  if (i==0) {
   vec = &(obj->p.y);
   vec_prev = &(obj->p_prev.y);
   f = obj->f.y = 0.02*(*vec - jiki.p.y);
  } else {
   vec = &(obj->p.x);
   vec_prev = &(obj->p_prev.x);
   f = obj->f.x = 0.02*(*vec - jiki.p.x);
  }
  y_tmp = *vec;
  vy = (-(*vec - *vec_prev) + f);
  if (vy < -2 || vy > 2) {
   vy = (*vec < *vec_prev) ? 2 : -2;
  }
  vy *= 0.98;
  *vec -= vy;
  *vec_prev = y_tmp;
 }
}

variable_types: struct Chara { struct FPos { float x; } p; struct FPos p_prev; struct FPos f; char isflying; } *

variable_types:
vec
float *

variable_types: f float

variable_types: vy float

user_defined_types: struct Chara { struct FPos p; struct FPos p_prev; struct FPos f; char isflying; }


decompiled_code:
float *move_tail()
{
return simu_chara_spring((float *)tail);
}

canonical_decompiled_code:
float *func4()
{
return func3((float *)tail);
}

original_code:
void move_tail()
{
 simu_chara_spring(&tail);
}


decompiled_code:
float *move_jiki()
{
float v0; // xmm0_4
float v1; // xmm0_4
float v2; // xmm0_4
float v3; // xmm0_4

if ( !L_BTN )
{
v0 = *(float *)&jiki - 0.7;
*(float *)&jiki = v0;
}
if ( !R_BTN )
{
v1 = *(float *)&jiki + 0.7;
*(float *)&jiki = v1;
}
if ( !U_BTN )
{
v2 = *((float *)&jiki + 1) - 0.7;
*((float *)&jiki + 1) = v2;
}
if ( !D_BTN )
{
v3 = *((float *)&jiki + 1) + 0.7;
*((float *)&jiki + 1) = v3;
}
return move_tail();
}

canonical_decompiled_code:
float *func5()
{
float v0; // xmm0_4
float v1; // xmm0_4
float v2; // xmm0_4
float v3; // xmm0_4

if ( !L_BTN )
{
v0 = *(float *)&jiki - 0.7;
*(float *)&jiki = v0;
}
if ( !R_BTN )
{
v1 = *(float *)&jiki + 0.7;
*(float *)&jiki = v1;
}
if ( !U_BTN )
{
v2 = *((float *)&jiki + 1) - 0.7;
*((float *)&jiki + 1) = v2;
}
if ( !D_BTN )
{
v3 = *((float *)&jiki + 1) + 0.7;
*((float *)&jiki + 1) = v3;
}
return func4();
}

original_code:
void move_jiki()
{
 float y_tmp, vy;
 if (!L_BTN) { jiki.p.x -= 0.7; }
 if (!R_BTN) { jiki.p.x += 0.7; }
 if (!U_BTN) { jiki.p.y -= 0.7; }
 if (!D_BTN) { jiki.p.y += 0.7; }
 move_tail();
}

canonical_original_code:
void move_jiki()
{
 float y_tmp; float vy;
 if (!L_BTN) { jiki.p.x -= 0.7; }
 if (!R_BTN) { jiki.p.x += 0.7; }
 if (!U_BTN) { jiki.p.y -= 0.7; }
 if (!D_BTN) { jiki.p.y += 0.7; }
 move_tail();
}


decompiled_code:
__int64 draw_jiki()
{
draw_line((int)*(float *)&jiki, (int)*((float *)&jiki + 1), (int)tail[0], (int)tail[1], 0);
fillBox(
(int)*(float *)&jiki,
(int)*((float *)&jiki + 1),
(int)(float)(*(float *)&jiki + 1.0),
(int)(float)(*((float *)&jiki + 1) + 1.0),
48);
return fillBox((int)tail[0], (int)tail[1], (int)(float)(tail[0] + 1.0), (int)(float)(tail[1] + 1.0), 12);
}

canonical_decompiled_code:
__int64 func6()
{
func1((int)*(float *)&jiki, (int)*((float *)&jiki + 1), (int)tail[0], (int)tail[1], 0);
func2(
(int)*(float *)&jiki,
(int)*((float *)&jiki + 1),
(int)(float)(*(float *)&jiki + 1.0),
(int)(float)(*((float *)&jiki + 1) + 1.0),
48);
return func2((int)tail[0], (int)tail[1], (int)(float)(tail[0] + 1.0), (int)(float)(tail[1] + 1.0), 12);
}

original_code:
void draw_jiki()
{
 draw_line(jiki.p.x, jiki.p.y, tail.p.x,tail.p.y, 0x00);
 fillBox(jiki.p.x, jiki.p.y, jiki.p.x+1, jiki.p.y+1, 0x30);
 fillBox(tail.p.x, tail.p.y, tail.p.x+1, tail.p.y+1, 0x0c);
}


original_code:
void draw_back()
{
 fillBox(0, 0, 60, 60, 0xff);
}


original_code:
void game_init()
{
 jiki.p.x = 10;
 jiki.p.y = (60 >> 1);
 jiki.p_prev.x = jiki.p.x;
 jiki.p_prev.y = jiki.p.y;
 tail.p_prev.x = jiki.p.x - 6;
 tail.p_prev.y = jiki.p.y;
 tail.p.x = tail.p_prev.x;
 tail.p.y = tail.p_prev.y + 1;
}


decompiled_code:
__int64 game_main()
{
move_jiki();
draw_back();
return draw_jiki();
}

canonical_decompiled_code:
__int64 func9()
{
func5();
func7();
return func6();
}

original_code:
void game_main()
{
 move_jiki();
 draw_back();
 draw_jiki();
}


repo: fumin/apiserv

decompiled_code:
__int64 __fastcall heap_node_free(_QWORD *a1)
{
_QWORD *v1; // rbx
_QWORD *v2; // rbp
__int64 result; // rax

if ( a1 )
{
v1 = a1;
do
{
v2 = v1;
v1 = (_QWORD *)v1[3];
free(v2);
result = heap_node_free(v2[1]);
}
while ( v1 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1)
{
_QWORD *v1; // rbx
_QWORD *v2; // rbp
__int64 result; // rax

if ( a1 )
{
v1 = a1;
do
{
v2 = v1;
v1 = (_QWORD *)v1[3];
free(v2);
result = func1(v2[1]);
}
while ( v1 );
}
return result;
}

original_code:
void heap_node_free(heap_node *h) {
    heap_node * fc;
    if (!h)
        return;
    fc = h->first_child;
    free(h);
    heap_node_free(h->next_sibling);
    return heap_node_free(fc);
}

canonical_original_code:
void heap_node_free(struct heap_node *h) {
    struct heap_node *fc;
    if (!h)
        return;
    fc = h->first_child;
    free(h);
    heap_node_free(h->next_sibling);
    return heap_node_free(fc);
}

variable_types: struct heap_node { struct heap_node *parent; struct heap_node *next_sibling; struct heap_node *previous_sibling; struct heap_node *first_child; struct heap_node *last_child; int n_subtrees; struct connection_t { int state; int fd; struct sockaddr { unsigned short sa_family; char sa_data[14]; } client_addr; unsigned int events; long timeout; struct heap_node *pheap_node; char req_buf[512]; char *preq_buf; int content_length; char *p_newline; char *p_end_of_init; int http_resp_code; char resp_header_buf[256]; int resp_header_len; char resp_buf[1024]; long resp_content_length; long resp_header_write_offset; long resp_body_write_offset; int static_content_fd; struct conn_writev_t { struct iovec { void *iov_base; unsigned long iov_len; } iovs[2]; struct iovec *iov; int iovcnt; } writev_s; unsigned int tcp_cork; } value; } *

user_defined_types: struct heap_node { struct heap_node *parent; struct heap_node *next_sibling; struct heap_node *previous_sibling; struct heap_node *first_child; struct heap_node *last_child; int n_subtrees; struct connection_t value; }

user_defined_types: struct connection_t { int state; int fd; struct sockaddr client_addr; unsigned int events; long timeout; struct heap_node *pheap_node; char req_buf[512]; char *preq_buf; int content_length; char *p_newline; char *p_end_of_init; int http_resp_code; char resp_header_buf[256]; int resp_header_len; char resp_buf[1024]; long resp_content_length; long resp_header_write_offset; long resp_body_write_offset; int static_content_fd; struct conn_writev_t writev_s; unsigned int tcp_cork; }

user_defined_types: struct conn_writev_t { struct iovec iovs[2]; struct iovec *iov; int iovcnt; }


decompiled_code:
void __fastcall heap_free(void *ptr)
{
_QWORD *v2; // rdi

v2 = *(_QWORD **)ptr;
g_heap_num_ele = 0;
heap_node_free(v2);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
_QWORD *v2; // rdi

v2 = *(_QWORD **)ptr;
g_heap_num_ele = 0;
func1(v2);
free(ptr);
}

original_code:
void heap_free(heap *hp) {g_heap_num_ele = 0;
    heap_node_free(hp->root);
    free(hp);
}

canonical_original_code:
void heap_free(struct heap *hp) {g_heap_num_ele = 0;
    heap_node_free(hp->root);
    free(hp);
}

variable_types: struct heap { struct heap_node { struct heap_node *parent; struct heap_node *next_sibling; struct heap_node *previous_sibling; struct heap_node *first_child; struct heap_node *last_child; int n_subtrees; struct connection_t { int state; int fd; struct sockaddr { unsigned short sa_family; char sa_data[14]; } client_addr; unsigned int events; long timeout; struct heap_node *pheap_node; char req_buf[512]; char *preq_buf; int content_length; char *p_newline; char *p_end_of_init; int http_resp_code; char resp_header_buf[256]; int resp_header_len; char resp_buf[1024]; long resp_content_length; long resp_header_write_offset; long resp_body_write_offset; int static_content_fd; struct conn_writev_t { struct iovec { void *iov_base; unsigned long iov_len; } iovs[2]; struct iovec *iov; int iovcnt; } writev_s; unsigned int tcp_cork; } value; } *root; } *


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

result = *a1;
if ( *a1 )
result = *a1 + 48;
return result;
}

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

result = *a1;
if ( *a1 )
result = *a1 + 48;
return result;
}

original_code:
struct connection_t* heap_min(heap *h){
 if (h->root == ((void *)0)) return ((void *)0);
 return &h->root->value;
}

canonical_original_code:
struct connection_t *heap_min(struct heap *h){
 if (h->root == ((void *)0)) return ((void *)0);
 return &h->root->value;
}

variable_types: struct heap { struct heap_node *root; } *

return_type: struct connection_t { int state; int fd; struct sockaddr { unsigned short sa_family; char sa_data[14]; } client_addr; unsigned int events; long timeout; struct heap_node { struct heap_node *parent; struct heap_node *next_sibling; struct heap_node *previous_sibling; struct heap_node *first_child; struct heap_node *last_child; int n_subtrees; struct connection_t value; } *pheap_node; char req_buf[512]; char *preq_buf; int content_length; char *p_newline; char *p_end_of_init; int http_resp_code; char resp_header_buf[256]; int resp_header_len; char resp_buf[1024]; long resp_content_length; long resp_header_write_offset; long resp_body_write_offset; int static_content_fd; struct conn_writev_t { struct iovec { void *iov_base; unsigned long iov_len; } iovs[2]; struct iovec *iov; int iovcnt; } writev_s; unsigned int tcp_cork; } *


decompiled_code:
void __fastcall heap_delete_min(_QWORD **a1)
{
_QWORD *v1; // r8
__int64 *v2; // r9
__int64 *v3; // rsi
_QWORD *v4; // r8
_QWORD *v5; // r9
_QWORD *v6; // r9
_QWORD *v7; // rax
_QWORD *v8; // r8
__int64 *v9; // r10
__int64 *v10; // r9
_QWORD *v11; // r9
_QWORD *v12; // rax
_QWORD *v13; // r8
__int64 *v14; // rsi
_QWORD *v15; // rbp

v1 = *a1;
--g_heap_num_ele;
v2 = (__int64 *)v1[3];
if ( !v2 )
goto LABEL_13;
while ( 1 )
{
v3 = (__int64 *)v2[1];
heap_node_isolate(v2);
if ( !v3 )
break;
heap_node_isolate(v3);
v7 = heap_node_meld(v6, v3);
v1 = heap_node_meld(v8, v7);
if ( !v9 )
goto LABEL_6;
v2 = v9;
}
v1 = heap_node_meld(v4, v5);
LABEL_6:
v10 = (__int64 *)v1[3];
if ( v10 )
{
do
{
v14 = (__int64 *)v10[1];
if ( !v14 )
break;
heap_node_isolate(v10);
heap_node_isolate(v14);
v12 = heap_node_meld(v11, v14);
v1 = heap_node_meld(v13, v12);
}
while ( v10 );
v15 = (_QWORD *)v1[3];
if ( v15 )
*v15 = 0LL;
}
else
{
LABEL_13:
v15 = 0LL;
}
free(v1);
*a1 = v15;
}

original_code:
void heap_delete_min(heap *hp) { g_heap_num_ele--;
    heap_node *a,*b,*next_tmp,*new_root=((void *)0);
    heap_node * h = hp->root;
    a = h->first_child;
    while(a!=((void *)0))
    {
        next_tmp=((void *)0);
        b = a->next_sibling;
        heap_node_isolate(a);
        if (b) {
            next_tmp=b->next_sibling;
            heap_node_isolate(b);
            a = heap_node_meld(a,b);
        }
        h = heap_node_meld(h,a);
        a = next_tmp;
    }
    a = h->first_child;
    while(a!=((void *)0))
    {
        b = a->next_sibling;
        if (!b) break;
        heap_node_isolate(a);
        heap_node_isolate(b);
        a = heap_node_meld(a,b);
        h = heap_node_meld(h,a);
    }
    if (h->first_child) {
        new_root = h->first_child;
        h->first_child->parent=((void *)0);
    }
    free(h);
    hp->root = new_root;
}

canonical_original_code:
void heap_delete_min(struct heap *hp) { g_heap_num_ele--;
    struct heap_node *a; struct heap_node *b; struct heap_node *next_tmp; struct heap_node *new_root = ((void *)0);
    struct heap_node *h = hp->root;
    a = h->first_child;
    while(a!=((void *)0))
    {
        next_tmp=((void *)0);
        b = a->next_sibling;
        heap_node_isolate(a);
        if (b) {
            next_tmp=b->next_sibling;
            heap_node_isolate(b);
            a = heap_node_meld(a,b);
        }
        h = heap_node_meld(h,a);
        a = next_tmp;
    }
    a = h->first_child;
    while(a!=((void *)0))
    {
        b = a->next_sibling;
        if (!b) break;
        heap_node_isolate(a);
        heap_node_isolate(b);
        a = heap_node_meld(a,b);
        h = heap_node_meld(h,a);
    }
    if (h->first_child) {
        new_root = h->first_child;
        h->first_child->parent=((void *)0);
    }
    free(h);
    hp->root = new_root;
}

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

user_defined_types: struct iovec { void *iov_base; unsigned long iov_len; }


decompiled_code:
_QWORD *heap_new()
{
_QWORD *result; // rax

g_heap_num_ele = 0;
result = malloc(8uLL);
*result = 0LL;
return result;
}

canonical_decompiled_code:
_QWORD *func7()
{
_QWORD *result; // rax

g_heap_num_ele = 0;
result = malloc(8uLL);
*result = 0LL;
return result;
}

original_code:
heap * heap_new(void) {g_heap_num_ele=0;
    heap *h;
    h = (heap*)malloc(sizeof(heap));
    h->root = ((void *)0);
    return h;
}

canonical_original_code:
struct heap *heap_new() {g_heap_num_ele=0;
    struct heap *h;
    h = (struct heap *)malloc(sizeof(heap));
    h->root = ((void *)0);
    return h;
}

return_type: struct heap { struct heap_node { struct heap_node *parent; struct heap_node *next_sibling; struct heap_node *previous_sibling; struct heap_node *first_child; struct heap_node *last_child; int n_subtrees; struct connection_t { int state; int fd; struct sockaddr { unsigned short sa_family; char sa_data[14]; } client_addr; unsigned int events; long timeout; struct heap_node *pheap_node; char req_buf[512]; char *preq_buf; int content_length; char *p_newline; char *p_end_of_init; int http_resp_code; char resp_header_buf[256]; int resp_header_len; char resp_buf[1024]; long resp_content_length; long resp_header_write_offset; long resp_body_write_offset; int static_content_fd; struct conn_writev_t { struct iovec { void *iov_base; unsigned long iov_len; } iovs[2]; struct iovec *iov; int iovcnt; } writev_s; unsigned int tcp_cork; } value; } *root; } *


decompiled_code:
time_t log_alarm_signal_handler()
{
time_t *v0; // rbx
time_t result; // rax

v0 = gp_current_timestamp;
result = time(0LL);
*v0 = result;
return result;
}

canonical_decompiled_code:
time_t func0()
{
time_t *v0; // rbx
time_t result; // rax

v0 = gp_current_timestamp;
result = time(0LL);
*v0 = result;
return result;
}

original_code:
static void log_alarm_signal_handler(int sig) {
 *gp_current_timestamp = time(((void *)0));
}

variable_types: sig int


repo: funglaub/devd-notifier

decompiled_code:
void __noreturn display_usage()
{
puts("Usage: devd-notifier [OPTION]");
puts("Options:");
printf("%-10s %-s\n", "-f", "don't fork to background");
printf("%-10s %-s\n", "-h", "show this help");
exit(1);
}

canonical_decompiled_code:
void __noreturn func7()
{
puts("Usage: devd-notifier [OPTION]");
puts("Options:");
printf("%-10s %-s\n", "-f", "don't fork to background");
printf("%-10s %-s\n", "-h", "show this help");
exit(1);
}

original_code:
void display_usage() {
  printf("Usage: devd-notifier [OPTION]\n");
  printf("Options:\n");
  printf("%-10s %-s\n", "-f", "don't fork to background");
  printf("%-10s %-s\n", "-h", "show this help");
  exit(1);
}


cleanup

decompiled_code:
void __noreturn cleanup()
{
closelog();
regfree(&regex);
notify_uninit();
exit(0);
}

canonical_decompiled_code:
void __noreturn func8()
{
closelog();
regfree(&regex);
notify_uninit();
exit(0);
}

original_code:
void cleanup() {
  closelog();
  regfree(&regex);
  notify_uninit();
  exit(0);
}


decompiled_code:
void __fastcall signal_handler(unsigned int a1)
{
if ( a1 == 15 || a1 == 3 )
{
syslog(5, "Received signal %i. Terminating", a1);
cleanup();
}
}

canonical_decompiled_code:
void __fastcall func9(unsigned int a1)
{
if ( a1 == 15 || a1 == 3 )
{
syslog(5, "Received signal %i. Terminating", a1);
func8();
}
}

original_code:
void signal_handler(int sig) {
  if(sig == 15 || sig == 3) {
    syslog(5, "Received signal %i. Terminating", sig);
    cleanup();
  }
}


daemonize

decompiled_code:
int daemonize()
{
__pid_t v0; // eax
char *v1; // rax
int result; // eax

v0 = fork();
if ( v0 < 0 )
{
perror("Error");
exit(1);
}
if ( v0 )
{
syslog(5, "forked to background with PID %i", (unsigned int)v0);
exit(0);
}
umask(0);
if ( setsid() < 0 )
{
perror("Error retrieving a new SID");
exit(1);
}
v1 = getenv("HOME");
result = chdir(v1);
if ( result < 0 )
{
perror("Error changing working directory");
exit(1);
}
return result;
}

canonical_decompiled_code:
int func10()
{
__pid_t v0; // eax
char *v1; // rax
int result; // eax

v0 = fork();
if ( v0 < 0 )
{
perror("Error");
exit(1);
}
if ( v0 )
{
syslog(5, "forked to background with PID %i", (unsigned int)v0);
exit(0);
}
umask(0);
if ( setsid() < 0 )
{
perror("Error retrieving a new SID");
exit(1);
}
v1 = getenv("HOME");
result = chdir(v1);
if ( result < 0 )
{
perror("Error changing working directory");
exit(1);
}
return result;
}

original_code:
void daemonize() {
  pid_t pid, sid;
  pid = fork();
  if (pid < 0) {
    perror("Error");
    exit(1);
  }
  if (pid > 0) {
    syslog(5, "forked to background with PID %i", pid);
    exit(0);
  }
  umask(0);
  sid = setsid();
  if (sid < 0) {
    perror("Error retrieving a new SID");
    exit(1);
  }
  if ((chdir(getenv("HOME"))) < 0) {
    perror("Error changing working directory");
    exit(1);
  }
}

canonical_original_code:
void daemonize() {
  int pid; int sid;
  pid = fork();
  if (pid < 0) {
    perror("Error");
    exit(1);
  }
  if (pid > 0) {
    syslog(5, "forked to background with PID %i", pid);
    exit(0);
  }
  umask(0);
  sid = setsid();
  if (sid < 0) {
    perror("Error retrieving a new SID");
    exit(1);
  }
  if ((chdir(getenv("HOME"))) < 0) {
    perror("Error changing working directory");
    exit(1);
  }
}

variable_types: pid int

variable_types: sid int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
char *v4; // rax
struct tm *v6; // rax
time_t timer; // [rsp+8h] [rbp-140h] BYREF
struct timeval tv; // [rsp+10h] [rbp-138h] BYREF
struct timeval v9; // [rsp+20h] [rbp-128h] BYREF
char buf[280]; // [rsp+30h] [rbp-118h] BYREF

while ( (int)FCGI_Accept() >= 0 )
{
while ( 1 )
{
gettimeofday(&tv, 0LL);
v3 = getenv("REMOTE_ADDR");
v4 = getenv("REMOTE_PORT");
if ( !v3 || !v4 )
{
FCGI_printf();
exit(1);
}
if ( !(unsigned int)query_identd(buf, v3, v4) )
break;
FCGI_printf();
FCGI_printf();
if ( (int)FCGI_Accept() < 0 )
return 0;
}
gettimeofday(&v9, 0LL);
timer = v9.tv_sec;
v6 = gmtime(&timer);
asctime(v6);
FCGI_printf();
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbp
char *v4; // rax
struct tm *v6; // rax
time_t timer; // [rsp+8h] [rbp-140h] BYREF
struct timeval tv; // [rsp+10h] [rbp-138h] BYREF
struct timeval v9; // [rsp+20h] [rbp-128h] BYREF
char buf[280]; // [rsp+30h] [rbp-118h] BYREF

while ( (int)FCGI_Accept() >= 0 )
{
while ( 1 )
{
gettimeofday(&tv, 0LL);
v3 = getenv("REMOTE_ADDR");
v4 = getenv("REMOTE_PORT");
if ( !v3 || !v4 )
{
FCGI_printf();
exit(1);
}
if ( !(unsigned int)func8(buf, v3, v4) )
break;
FCGI_printf();
FCGI_printf();
if ( (int)FCGI_Accept() < 0 )
return 0;
}
gettimeofday(&v9, 0LL);
timer = v9.tv_sec;
v6 = gmtime(&timer);
asctime(v6);
FCGI_printf();
}
return 0;
}

original_code:
int main(int argc, char *argv[])
{
  char buffer[256];
  time_t curtime;
  struct timeval start, stop;
  long elapsed;
  char *remote_addr;
  char *remote_port;
  while( FCGI_Accept() >= 0) {
    gettimeofday(&start, ((void *)0));
    remote_addr = getenv("REMOTE_ADDR");
    remote_port = getenv("REMOTE_PORT");
    if (remote_addr == ((void *)0) || remote_port == ((void *)0)) {
      FCGI_printf("No remote address or port received. Terminating.\n");
      exit(1);
    }
    if (query_identd(&buffer[0], remote_addr, remote_port) != 0) {
      FCGI_printf("Content-type: text/plain\r\n");
      FCGI_printf("\r\nCan't connect to identd server at %s\n", remote_addr);
    } else {
      gettimeofday(&stop, ((void *)0));
      curtime = stop.tv_sec;
      elapsed = (stop.tv_sec-start.tv_sec)*1000000 + stop.tv_usec-start.tv_usec;
      FCGI_printf("Content-type: text/plain\r\n"
             "\r\n"
             "Hello %s %s.\n"
             "Ident response: %s\n"
             "Request took %f seconds\n"
             "Time (UTC): %s\n", remote_addr, remote_port, buffer, (double)elapsed/1e6, asctime(gmtime(&curtime)));
    }
  }
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  char buffer[256];
  long curtime;
  struct timeval start; struct timeval stop;
  long elapsed;
  char *remote_addr;
  char *remote_port;
  while( FCGI_Accept() >= 0) {
    gettimeofday(&start, ((void *)0));
    remote_addr = getenv("REMOTE_ADDR");
    remote_port = getenv("REMOTE_PORT");
    if (remote_addr == ((void *)0) || remote_port == ((void *)0)) {
      FCGI_printf("No remote address or port received. Terminating.\n");
      exit(1);
    }
    if (query_identd(&buffer[0], remote_addr, remote_port) != 0) {
      FCGI_printf("Content-type: text/plain\r\n");
      FCGI_printf("\r\nCan't connect to identd server at %s\n", remote_addr);
    } else {
      gettimeofday(&stop, ((void *)0));
      curtime = stop.tv_sec;
      elapsed = (stop.tv_sec-start.tv_sec)*1000000 + stop.tv_usec-start.tv_usec;
      FCGI_printf("Content-type: text/plain\r\n"
             "\r\n"
             "Hello %s %s.\n"
             "Ident response: %s\n"
             "Request took %f seconds\n"
             "Time (UTC): %s\n", remote_addr, remote_port, buffer, (double)elapsed/1e6, asctime(gmtime(&curtime)));
    }
  }
  return 0;
}

variable_types: argc int

variable_types:
buffer
char [256]

variable_types: curtime long

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

variable_types: elapsed long


decompiled_code:
__int64 __fastcall query_identd(void *buf, char *name, char *s)
{
int v4; // eax
unsigned int v5; // er12
struct hostent *v6; // rax
socklen_t v7; // edx
size_t v8; // rax
char *v9; // rbp
size_t v10; // rax
__int64 result; // rax
struct sockaddr v12[3]; // [rsp+0h] [rbp-38h] BYREF

v4 = socket(2, 1, 0);
if ( v4 >= 0 && (v5 = v4, (v6 = gethostbyname(name)) != 0LL) )
{
v12[0] = 0LL;
v12[0].sa_family = 2;
memcpy(&v12[0].sa_data[2], *(const void **)v6->h_addr_list, v6->h_length);
*(_WORD *)v12[0].sa_data = _bswap_16_constprop_0(v5, v12, 16LL);
if ( connect(v5, v12, v7) < 0 )
{
FCGI_perror();
result = 0xFFFFFFFFLL;
}
else
{
v8 = strlen(s);
v9 = (char *)malloc(v8 + 6);
snprintf(v9, 8uLL, "%s", s);
strcpy(v9, ",80\n");
FCGI_printf();
v10 = strlen(v9);
if ( (int)write(v5, v9, v10) < 0
|| (*(_QWORD *)buf = 0LL,
*((_QWORD *)buf + 31) = 0LL,
memset(
(void *)(((unsigned __int64)buf + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)buf - (((_DWORD)buf + 8) & 0xFFFFFFF8) + 256) >> 3)),
(int)read(v5, buf, 0xFFuLL) < 0) )
{
FCGI_perror();
free(v9);
result = 0xFFFFFFFFLL;
}
else
{
close(v5);
free(v9);
result = 0LL;
}
}
}
else
{
FCGI_perror();
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(void *buf, char *name, char *s)
{
int v4; // eax
unsigned int v5; // er12
struct hostent *v6; // rax
socklen_t v7; // edx
size_t v8; // rax
char *v9; // rbp
size_t v10; // rax
__int64 result; // rax
struct sockaddr v12[3]; // [rsp+0h] [rbp-38h] BYREF

v4 = socket(2, 1, 0);
if ( v4 >= 0 && (v5 = v4, (v6 = gethostbyname(name)) != 0LL) )
{
v12[0] = 0LL;
v12[0].sa_family = 2;
memcpy(&v12[0].sa_data[2], *(const void **)v6->h_addr_list, v6->h_length);
*(_WORD *)v12[0].sa_data = func7(v5, v12, 16LL);
if ( connect(v5, v12, v7) < 0 )
{
FCGI_perror();
result = 0xFFFFFFFFLL;
}
else
{
v8 = strlen(s);
v9 = (char *)malloc(v8 + 6);
snprintf(v9, 8uLL, "%s", s);
strcpy(v9, ",80\n");
FCGI_printf();
v10 = strlen(v9);
if ( (int)write(v5, v9, v10) < 0
|| (*(_QWORD *)buf = 0LL,
*((_QWORD *)buf + 31) = 0LL,
memset(
(void *)(((unsigned __int64)buf + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)buf - (((_DWORD)buf + 8) & 0xFFFFFFF8) + 256) >> 3)),
(int)read(v5, buf, 0xFFuLL) < 0) )
{
FCGI_perror();
free(v9);
result = 0xFFFFFFFFLL;
}
else
{
close(v5);
free(v9);
result = 0LL;
}
}
}
else
{
FCGI_perror();
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int query_identd(char *buffer, char *remote_addr, char *remote_port) {
  int sockfd, n;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  static const char *www_port = ",80\n";
  char *request;
  sockfd = socket(2, SOCK_STREAM, 0);
  if (sockfd < 0) {
    FCGI_perror("ERROR opening socket");
    return(-1);
  }
  server = gethostbyname(remote_addr);
  if (server == ((void *)0)) {
    FCGI_perror("no such host\n");
    return(-1);
  }
  memset((char *) &serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = 2;
  memcpy((char *)&serv_addr.sin_addr.s_addr,
         (char *)server->h_addr_list[0],
         server->h_length);
  serv_addr.sin_port = htons(113);
  if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
    FCGI_perror("ERROR connecting");
    return(-1);
  }
  request = malloc(strlen(remote_port)+strlen(www_port)+2);
  snprintf(request, sizeof request, "%s", remote_port);
  snprintf(request, sizeof request, "%s", www_port);
  FCGI_printf("%s\n", request);
  n = write(sockfd,request,strlen(request));
  if (n < 0) {
    FCGI_perror("ERROR writing to socket");
    free(request);
    return(-1);
  }
  memset(buffer, 0, 256);
  n = read(sockfd,buffer,255);
  if (n < 0) {
    FCGI_perror("ERROR reading from socket");
    free(request);
    return(-1);
  }
  close(sockfd);
  free(request);
  return 0;
}

canonical_original_code:
int query_identd(char *buffer, char *remote_addr, char *remote_port) {
  int sockfd; int n;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  static char *www_port = ",80\n";
  char *request;
  sockfd = socket(2, SOCK_STREAM, 0);
  if (sockfd < 0) {
    FCGI_perror("ERROR opening socket");
    return(-1);
  }
  server = gethostbyname(remote_addr);
  if (server == ((void *)0)) {
    FCGI_perror("no such host\n");
    return(-1);
  }
  memset((char *) &serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = 2;
  memcpy((char *)&serv_addr.sin_addr.s_addr,
         (char *)server->h_addr_list[0],
         server->h_length);
  serv_addr.sin_port = htons(113);
  if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
    FCGI_perror("ERROR connecting");
    return(-1);
  }
  request = malloc(strlen(remote_port)+strlen(www_port)+2);
  snprintf(request, sizeof request, "%s", remote_port);
  snprintf(request, sizeof request, "%s", www_port);
  FCGI_printf("%s\n", request);
  n = write(sockfd,request,strlen(request));
  if (n < 0) {
    FCGI_perror("ERROR writing to socket");
    free(request);
    return(-1);
  }
  memset(buffer, 0, 256);
  n = read(sockfd,buffer,255);
  if (n < 0) {
    FCGI_perror("ERROR reading from socket");
    free(request);
    return(-1);
  }
  close(sockfd);
  free(request);
  return 0;
}

variable_types:
buffer
char *

variable_types: sockfd int

variable_types: n int

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

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

variable_types:
request
char *

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


umalloc

repo: funglaub/kqueue-notify

decompiled_code:
void *__fastcall umalloc(size_t a1)
{
void *result; // rax

result = calloc(a1, 1uLL);
if ( !result )
err(71, 0LL);
return result;
}

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

result = calloc(a1, 1uLL);
if ( !result )
err(71, 0LL);
return result;
}

original_code:
void *
umalloc(size_t l)
{
  void *p;
  p = malloc(l);
  if (p == ((void *)0)) err(71, ((void *)0));
  memset(p, 0, l);
  return p;
}

canonical_original_code:
void *umalloc(unsigned long l)
{
  void *p;
  p = malloc(l);
  if (p == ((void *)0)) err(71, ((void *)0));
  memset(p, 0, l);
  return p;
}

variable_types:
l
unsigned long

variable_types:
p
void *


urealloc

decompiled_code:
void *__fastcall urealloc(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
err(71, 0LL);
return result;
}

canonical_decompiled_code:
void *__fastcall func1(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
err(71, 0LL);
return result;
}

original_code:
void *
urealloc(void *p, size_t l)
{
  void *r;
  r = realloc(p, l);
  if(r == ((void *)0)) err(71, ((void *)0));
  return r;
}

canonical_original_code:
void *urealloc(void *p, unsigned long l)
{
  void *r;
  r = realloc(p, l);
  if(r == ((void *)0)) err(71, ((void *)0));
  return r;
}

variable_types:
r
void *


ustrdup

decompiled_code:
char *__fastcall ustrdup(char *src)
{
size_t v1; // r12
char *v2; // rax

v1 = strlen(src) + 1;
v2 = (char *)umalloc(v1);
return strncpy(v2, src, v1);
}

canonical_decompiled_code:
char *__fastcall func2(char *src)
{
size_t v1; // r12
char *v2; // rax

v1 = strlen(src) + 1;
v2 = (char *)func0(v1);
return strncpy(v2, src, v1);
}

original_code:
char *
ustrdup(char *s) {
  size_t len;
  char *new;
  len = strlen(s) + 1;
  new = (char *)umalloc(sizeof(char) * len);
  strncpy(new, s, len);
  return new;
}

canonical_original_code:
char *ustrdup(char *s) {
  unsigned long len;
  char *new;
  len = strlen(s) + 1;
  new = (char *)umalloc(sizeof(char) * len);
  strncpy(new, s, len);
  return new;
}

variable_types:
s
char *

variable_types:
len
unsigned long

variable_types:
new
char *

return_type: char *


repo: fuzxxl/fdup

decompiled_code:
__int64 __fastcall adjust_suffix(__int64 a1, char a2)
{
__int64 result; // rax

result = a1;
switch ( a2 )
{
case 'E':
result = a1 << 60;
break;
case 'G':
result = a1 << 30;
break;
case 'K':
result = a1 << 10;
break;
case 'M':
result = a1 << 20;
break;
case 'P':
result = a1 << 50;
break;
case 'T':
result = a1 << 40;
break;
default:
return result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, char a2)
{
__int64 result; // rax

result = a1;
switch ( a2 )
{
case 'E':
result = a1 << 60;
break;
case 'G':
result = a1 << 30;
break;
case 'K':
result = a1 << 10;
break;
case 'M':
result = a1 << 20;
break;
case 'P':
result = a1 << 50;
break;
case 'T':
result = a1 << 40;
break;
default:
return result;
}
return result;
}

original_code:
static off_t adjust_suffix(off_t n, char suffix) {
 switch (suffix) {
 case 'K': return n << 10;
 case 'M': return n << 20;
 case 'G': return n << 30;
 case 'T': return n << 40;
 case 'P': return n << 50;
 case 'E': return n << 60;
 default: return n;
 }
}

canonical_original_code:
static long adjust_suffix(long n, char suffix) {
 switch (suffix) {
 case 'K': return n << 10;
 case 'M': return n << 20;
 case 'G': return n << 30;
 case 'T': return n << 40;
 case 'P': return n << 50;
 case 'E': return n << 60;
 default: return n;
 }
}

variable_types: n long

variable_types: suffix char

return_type: long


walker

decompiled_code:
__int64 __fastcall walker(const char *filename, const struct stat64 *status, int flag, struct FTW *info)
{
__off_t v4; // rax
unsigned int v5; // er12
unsigned int v6; // eax

if ( (status->st_mode & 0xF000) != 0x8000 )
return 0;
v4 = status->st_size;
if ( v4 < bounds || dword_2B0 && v4 > qword_2A8 )
return 0;
v5 = register_file(matcher, filename, status, info);
if ( v5 )
return 1;
if ( !verbose )
return 0;
v6 = get_file_count(matcher);
fprintf(stderr, "\r%9d files", v6);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *filename, const struct stat64 *status, int flag, struct FTW *info)
{
__off_t v4; // rax
unsigned int v5; // er12
unsigned int v6; // eax

if ( (status->st_mode & 0xF000) != 0x8000 )
return 0;
v4 = status->st_size;
if ( v4 < bounds || dword_2B0 && v4 > qword_2A8 )
return 0;
v5 = register_file(matcher, filename, status, info);
if ( v5 )
return 1;
if ( !verbose )
return 0;
v6 = get_file_count(matcher);
fprintf(stderr, "\r%9d files", v6);
return v5;
}

original_code:
static int walker(const char *fpath,const struct stat *sb,int tf,struct FTW *ftwbuf) {
 (void)tf;
 (void)ftwbuf;
 if (!((((sb->st_mode)) & 0170000) == (0100000))) return 0;
 if (sb->st_size < bounds.lower) return 0;
 if (bounds.has_upper && sb->st_size > bounds.upper) return 0;
 if (register_file(matcher,fpath,sb)) return 1;
 if (verbose) fprintf(stderr,"\r%9d files",get_file_count(matcher));
 return 0;
}

canonical_original_code:
static int walker(char *fpath, struct stat *sb, int tf, struct FTW *ftwbuf) {
 (void)tf;
 (void)ftwbuf;
 if (!((((sb->st_mode)) & 0170000) == (0100000))) return 0;
 if (sb->st_size < bounds.lower) return 0;
 if (bounds.has_upper && sb->st_size > bounds.upper) return 0;
 if (register_file(matcher,fpath,sb)) return 1;
 if (verbose) fprintf(stderr,"\r%9d files",get_file_count(matcher));
 return 0;
}

variable_types:
fpath
char *

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

variable_types: tf int

variable_types:
ftwbuf
struct FTW { int base; int level; } *

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


help

decompiled_code:
int __fastcall help(const char *a1)
{
return printf("Usage: %s [-B | -H | -L | -S] [-hpvx] [-b cdglmpu] [-s n[,m]] directory...\n", a1);
}

canonical_decompiled_code:
int __fastcall func2(const char *a1)
{
return printf("Usage: %s [-B | -H | -L | -S] [-hpvx] [-b cdglmpu] [-s n[,m]] directory...\n", a1);
}

original_code:
static void help(const char *program) {
 printf("Usage: %s [-B | -H | -L | -S] [-hpvx] [-b cdglmpu] [-s n[,m]] directory...\n",program);
}

canonical_original_code:
static void help(char *program) {
 printf("Usage: %s [-B | -H | -L | -S] [-hpvx] [-b cdglmpu] [-s n[,m]] directory...\n",program);
}

variable_types:
program
char *