decompiled_code:
void __noreturn handle_sigint()
{
int v0; // er12
void *v1; // rbp
double v2; // r14
const char *v3; // rsi
void *v4; // rbx

signal(2, handle_sigint);
v0 = sigint_count_0;
if ( !sigint_count_0 )
{
sigint_count_0 = 1;
puts("\nBeginning shutdown... Press C-c again to force quit");
v1 = last_worker;
if ( last_worker )
{
v2 = 0.0;
while ( 1 )
{
if ( file_worker_join((pthread_t *)v1) )
{
v3 = (const char *)*((_QWORD *)v1 + 2);
if ( !v3 )
v3 = "unknown";
printf("Error joining worker for \"%s\"\n", v3);
}
else
{
++v0;
v2 = v2 + *((double *)v1 + 1);
}
v4 = (void *)*((_QWORD *)v1 + 4);
file_worker_free(v1);
if ( !v4 )
break;
v1 = v4;
}
}
else
{
v2 = 0.0;
}
print_server_stats(v0, v2);
free(input_buffer);
exit(0);
}
puts("\n");
free(input_buffer);
exit(0);
}

canonical_decompiled_code:
void __noreturn func14()
{
int v0; // er12
void *v1; // rbp
double v2; // r14
const char *v3; // rsi
void *v4; // rbx

signal(2, handle_sigint);
v0 = sigint_count_0;
if ( !sigint_count_0 )
{
sigint_count_0 = 1;
puts("\nBeginning shutdown... Press C-c again to force quit");
v1 = last_worker;
if ( last_worker )
{
v2 = 0.0;
while ( 1 )
{
if ( func10((pthread_t *)v1) )
{
v3 = (const char *)*((_QWORD *)v1 + 2);
if ( !v3 )
v3 = "unknown";
printf("Error joining worker for \"%s\"\n", v3);
}
else
{
++v0;
v2 = v2 + *((double *)v1 + 1);
}
v4 = (void *)*((_QWORD *)v1 + 4);
func8(v1);
if ( !v4 )
break;
v1 = v4;
}
}
else
{
v2 = 0.0;
}
func13(v0, v2);
free(input_buffer);
exit(0);
}
puts("\n");
free(input_buffer);
exit(0);
}

original_code:
void
handle_sigint(int sig)
{
  signal(2, handle_sigint);
  static int sigint_count = 0;
  switch(sigint_count)
    {
    case 0: {
      sigint_count++;
      printf("\nBeginning shutdown... Press C-c again to force quit\n");
      file_worker_t* worker = last_worker;
      int worker_count = 0;
      double total_time_taken = 0;
      while (worker)
 {
   if(file_worker_join(worker) != 0)
     {
       printf("Error joining worker for \"%s\"\n",
       (worker && worker->filename) ? worker->filename : "unknown");
     }
   else
     {
       worker_count++;
       total_time_taken += worker->time_taken;
     }
   file_worker_t* current = worker;
   worker = worker->peer;
   file_worker_free(current);
 }
      print_server_stats(worker_count, total_time_taken);
      free(input_buffer);
      exit(0);
    }
    default:
      {
 puts("\n");
 free(input_buffer);
 exit(0);
      }
    }
}

canonical_original_code:
void handle_sigint(int sig)
{
  signal(2, handle_sigint);
  static int sigint_count = 0;
  switch(sigint_count)
    {
    case 0: {
      sigint_count++;
      printf("\nBeginning shutdown... Press C-c again to force quit\n");
      struct _file_worker_t *worker = last_worker;
      int worker_count = 0;
      double total_time_taken = 0;
      while (worker)
 {
   if(file_worker_join(worker) != 0)
     {
       printf("Error joining worker for \"%s\"\n",
       (worker && worker->filename) ? worker->filename : "unknown");
     }
   else
     {
       worker_count++;
       total_time_taken += worker->time_taken;
     }
   struct _file_worker_t *current = worker;
   worker = worker->peer;
   file_worker_free(current);
 }
      print_server_stats(worker_count, total_time_taken);
      free(input_buffer);
      exit(0);
    }
    default:
      {
 puts("\n");
 free(input_buffer);
 exit(0);
      }
    }
}

variable_types: sig int


WriteToLog

repo: maciejpatucha/GoRaspberry

decompiled_code:
void __fastcall WriteToLog(int a1, const char *a2)
{
openlog("GoHipsterd", 1, 8);
if ( a1 )
{
if ( a1 == 1 )
syslog(3, "%s", a2);
}
else
{
syslog(6, "%s", a2);
}
closelog();
}

canonical_decompiled_code:
void __fastcall func0(int a1, const char *a2)
{
openlog("GoHipsterd", 1, 8);
if ( a1 )
{
if ( a1 == 1 )
syslog(3, "%s", a2);
}
else
{
syslog(6, "%s", a2);
}
closelog();
}

original_code:
void WriteToLog(int level, char *message)
{
 openlog("GoHipsterd", 0x01, (1<<3));
 switch (level)
 {
  case 0:
   syslog(6, "%s", message);
   break;
  case 1:
   syslog(3, "%s", message);
   break;
 };
 closelog();
}

variable_types: level int

variable_types:
message
char *

return_type: void


main

canonical_name: main

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

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

variable_types: argc int

variable_types:
argv
char *[]

return_type: int


GetFreeSpace

decompiled_code:
signed __int64 GetFreeSpace()
{
struct statvfs v1; // [rsp+0h] [rbp-78h] BYREF

if ( statvfs("/", &v1) != -1 )
return (signed __int64)(v1.f_bavail * v1.f_bsize) / 1047552;
WriteToLog(1LL, "Cannot get information about file system");
return 0LL;
}

canonical_decompiled_code:
signed __int64 func0()
{
struct statvfs v1; // [rsp+0h] [rbp-78h] BYREF

if ( statvfs("/", &v1) != -1 )
return (signed __int64)(v1.f_bavail * v1.f_bsize) / 1047552;
WriteToLog(1LL, "Cannot get information about file system");
return 0LL;
}

original_code:
static unsigned long GetFreeSpace(void)
{
 struct statvfs vfsbuffer;
 if (statvfs("/", &vfsbuffer) == -1)
 {
  WriteToLog(1, "Cannot get information about file system");
  return 0;
 }
 long long total_free_space = (long long)vfsbuffer.f_bsize * (long long)vfsbuffer.f_bavail;
 long long total_free_MB = total_free_space/(long long)(1023*1024);
 return (unsigned long)total_free_MB;
}

canonical_original_code:
static unsigned long GetFreeSpace()
{
 struct statvfs vfsbuffer;
 if (statvfs("/", &vfsbuffer) == -1)
 {
  WriteToLog(1, "Cannot get information about file system");
  return 0;
 }
 long long total_free_space = (long long)vfsbuffer.f_bsize * (long long)vfsbuffer.f_bavail;
 long long total_free_MB = total_free_space/(long long)(1023*1024);
 return (unsigned long)total_free_MB;
}

variable_types: struct statvfs { unsigned long f_bsize; unsigned long f_frsize; unsigned long f_blocks; unsigned long f_bfree; unsigned long f_bavail; unsigned long f_files; unsigned long f_ffree; unsigned long f_favail; unsigned long f_fsid; unsigned long f_flag; unsigned long f_namemax; int __f_spare[6]; }

return_type: unsigned long


IsRawVideoFile

decompiled_code:
bool __fastcall IsRawVideoFile(const char *a1)
{
return strstr(a1, "h264") != 0LL;
}

canonical_decompiled_code:
bool __fastcall func2(const char *a1)
{
return strstr(a1, "h264") != 0LL;
}

original_code:
static _Bool IsRawVideoFile(char *file)
{
 if (strstr(file, "h264") == ((void *)0))
 {
  return 0;
 }
 return 1;
}

variable_types:
file
char *


CheckLink

decompiled_code:
__int64 __fastcall CheckLink(char *src)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // er12
__int64 result; // rax
char v5[72]; // [rsp+0h] [rbp-48h] BYREF

v1 = socket(2, 2, 0);
if ( v1 < 0 )
{
WriteToLog(1LL, "Failed to create socket");
result = 0xFFFFFFFFLL;
}
else
{
v2 = v1;
strncpy(v5, src, 0x10uLL);
v3 = ioctl(v2, 0x8913uLL, v5);
close(v2);
if ( v3 == -1 )
WriteToLog(1LL, "Ioctl failed");
else
v3 = (v5[16] & 0x41) == 65;
result = v3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *src)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // er12
__int64 result; // rax
char v5[72]; // [rsp+0h] [rbp-48h] BYREF

v1 = socket(2, 2, 0);
if ( v1 < 0 )
{
WriteToLog(1LL, "Failed to create socket");
result = 0xFFFFFFFFLL;
}
else
{
v2 = v1;
strncpy(v5, src, 0x10uLL);
v3 = ioctl(v2, 0x8913uLL, v5);
close(v2);
if ( v3 == -1 )
WriteToLog(1LL, "Ioctl failed");
else
v3 = (v5[16] & 0x41) == 65;
result = v3;
}
return result;
}

original_code:
int CheckLink(char *interface)
{
 int state = 1;
 int sock = socket(2, SOCK_DGRAM, IPPROTO_IP);
 if (sock < 0)
 {
  WriteToLog(1,"Failed to create socket");
  return -1;
 }
 struct ifreq if_req;
 strncpy(if_req.ifr_ifrn.ifrn_name, interface, sizeof(if_req.ifr_ifrn.ifrn_name));
 int rv = ioctl(sock, 0x8913, &if_req);
 close(sock);
 if (rv == -1)
 {
  WriteToLog(1,"Ioctl failed");
  return -1;
 }
 return (if_req.ifr_ifru.ifru_flags & IFF_UP) && (if_req.ifr_ifru.ifru_flags & IFF_RUNNING);
}

variable_types:
interface
char *

variable_types: state int

variable_types: sock int

variable_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }

variable_types: rv int

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


GetMaxRecordingTime

decompiled_code:
unsigned __int64 GetMaxRecordingTime()
{
unsigned __int64 result; // rax

result = GetFreeSpace();
if ( result )
result = 60000 * (result / 0xC);
return result;
}

canonical_decompiled_code:
unsigned __int64 func5()
{
unsigned __int64 result; // rax

result = func0();
if ( result )
result = 60000 * (result / 0xC);
return result;
}

original_code:
unsigned long GetMaxRecordingTime(void)
{
 unsigned long free_space;
 if ((free_space = GetFreeSpace()) == 0)
 {
  return 0;
 }
 unsigned long minutes_of_recording = (free_space / (12));
 return (minutes_of_recording * 60000);
}

canonical_original_code:
unsigned long GetMaxRecordingTime()
{
 unsigned long free_space;
 if ((free_space = GetFreeSpace()) == 0)
 {
  return 0;
 }
 unsigned long minutes_of_recording = (free_space / (12));
 return (minutes_of_recording * 60000);
}


TerminateRecording

decompiled_code:
__int64 TerminateRecording()
{
__pid_t v0; // eax
__int64 result; // rax

v0 = FindRunningProcess_constprop_0();
if ( !v0 )
return 0xFFFFFFFFLL;
if ( kill(v0, 9) == -1 )
{
WriteToLog(1LL, "Error sending SIGTERM to recording process");
result = 0xFFFFFFFFLL;
}
else
{
WriteToLog(0LL, "raspivid killed successfully");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func6()
{
__pid_t v0; // eax
__int64 result; // rax

v0 = func3();
if ( !v0 )
return 0xFFFFFFFFLL;
if ( kill(v0, 9) == -1 )
{
WriteToLog(1LL, "Error sending SIGTERM to recording process");
result = 0xFFFFFFFFLL;
}
else
{
WriteToLog(0LL, "raspivid killed successfully");
result = 0LL;
}
return result;
}

original_code:
int TerminateRecording(void)
{
 pid_t recordingPid = FindRunningProcess("raspivid");
 if (recordingPid == 0)
 {
  return -1;
 }
 if (kill(recordingPid, 9) == -1)
 {
  WriteToLog(1, "Error sending SIGTERM to recording process");
  return -1;
 }
 WriteToLog(0, "raspivid killed successfully");
 return 0;
}

canonical_original_code:
int TerminateRecording()
{
 int recordingPid = FindRunningProcess("raspivid");
 if (recordingPid == 0)
 {
  return -1;
 }
 if (kill(recordingPid, 9) == -1)
 {
  WriteToLog(1, "Error sending SIGTERM to recording process");
  return -1;
 }
 WriteToLog(0, "raspivid killed successfully");
 return 0;
}

variable_types: recordingPid int


OutputFileName

decompiled_code:
char *OutputFileName()
{
struct tm *v0; // rbx
char *v1; // rax
char *v2; // r12
time_t timer; // [rsp+8h] [rbp-20h] BYREF

time(&timer);
v0 = localtime(&timer);
v1 = (char *)calloc(0x1000uLL, 1uLL);
v2 = v1;
if ( v1 )
snprintf(
v1,
0x1000uLL,
"/home/pi/Recordings/VID_%d-%d-%d_%d-%d-%d.h264",
(unsigned int)v0->tm_year,
(unsigned int)v0->tm_mon,
(unsigned int)v0->tm_mday,
(unsigned int)v0->tm_hour,
(unsigned int)v0->tm_min,
(unsigned int)v0->tm_sec);
else
WriteToLog(1LL, "Unable to allocate string for filename");
return v2;
}

canonical_decompiled_code:
char *func7()
{
struct tm *v0; // rbx
char *v1; // rax
char *v2; // r12
time_t timer; // [rsp+8h] [rbp-20h] BYREF

time(&timer);
v0 = localtime(&timer);
v1 = (char *)calloc(0x1000uLL, 1uLL);
v2 = v1;
if ( v1 )
snprintf(
v1,
0x1000uLL,
"/home/pi/Recordings/VID_%d-%d-%d_%d-%d-%d.h264",
(unsigned int)v0->tm_year,
(unsigned int)v0->tm_mon,
(unsigned int)v0->tm_mday,
(unsigned int)v0->tm_hour,
(unsigned int)v0->tm_min,
(unsigned int)v0->tm_sec);
else
WriteToLog(1LL, "Unable to allocate string for filename");
return v2;
}

original_code:
char *OutputFileName(void)
{
 time_t rawtime;
 struct tm *ltime;
 time(&rawtime);
 ltime = localtime(&rawtime);
 char *dateTimeString = calloc(4096, sizeof(char));
 if (dateTimeString == ((void *)0))
 {
  WriteToLog(1, "Unable to allocate string for filename");
  return ((void *)0);
 }
 snprintf(dateTimeString, 4096, "/home/pi/Recordings/VID_%d-%d-%d_%d-%d-%d.h264", ltime->tm_year, ltime->tm_mon, ltime->tm_mday, ltime->tm_hour, ltime->tm_min, ltime->tm_sec);
 return dateTimeString;
}

canonical_original_code:
char *OutputFileName()
{
 long rawtime;
 struct tm *ltime;
 time(&rawtime);
 ltime = localtime(&rawtime);
 char *dateTimeString = calloc(4096, sizeof(char));
 if (dateTimeString == ((void *)0))
 {
  WriteToLog(1, "Unable to allocate string for filename");
  return ((void *)0);
 }
 snprintf(dateTimeString, 4096, "/home/pi/Recordings/VID_%d-%d-%d_%d-%d-%d.h264", ltime->tm_year, ltime->tm_mon, ltime->tm_mday, ltime->tm_hour, ltime->tm_min, ltime->tm_sec);
 return dateTimeString;
}

variable_types: rawtime long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *

variable_types:
dateTimeString
char *

return_type: char *


ConvertOutputFileName

decompiled_code:
char *__fastcall ConvertOutputFileName(char *src)
{
int v1; // eax
char *v2; // r12
unsigned int v3; // eax

v1 = strlen(src);
v2 = (char *)calloc(v1, 1uLL);
if ( v2 )
{
v3 = (unsigned int)strstr(src, "h264");
strncpy(v2, src, (int)(v3 - (_DWORD)src));
*(_DWORD *)&v2[strlen(v2)] = 3436653;
}
else
{
WriteToLog(0LL, "Unable to allocate memory for convFileName");
}
return v2;
}

canonical_decompiled_code:
char *__fastcall func9(char *src)
{
int v1; // eax
char *v2; // r12
unsigned int v3; // eax

v1 = strlen(src);
v2 = (char *)calloc(v1, 1uLL);
if ( v2 )
{
v3 = (unsigned int)strstr(src, "h264");
strncpy(v2, src, (int)(v3 - (_DWORD)src));
*(_DWORD *)&v2[strlen(v2)] = 3436653;
}
else
{
WriteToLog(0LL, "Unable to allocate memory for convFileName");
}
return v2;
}

original_code:
char *ConvertOutputFileName(char *filename)
{
 int length = strlen(filename);
 char *convFileName = calloc(length, sizeof(char));
 if (convFileName == ((void *)0))
 {
  WriteToLog(0, "Unable to allocate memory for convFileName");
  return ((void *)0);
 }
 int amount = strstr(filename, "h264") - filename;
 strncpy(convFileName, filename, amount);
 strncat(convFileName, "mp4", 3);
 return convFileName;
}

variable_types:
filename
char *

variable_types: length int

variable_types:
convFileName
char *

variable_types: amount int


ConvertThread

decompiled_code:
void __fastcall __noreturn ConvertThread(void *a1)
{
const char *v1; // rax
char *v2; // rbp
const char *v3; // rax
char *v4; // r12
int v5; // eax
int stat_loc; // [rsp+Ch] [rbp-203Ch] BYREF
struct timespec requested_time; // [rsp+10h] [rbp-2038h] BYREF
struct timespec remaining; // [rsp+20h] [rbp-2028h] BYREF
char s[4096]; // [rsp+30h] [rbp-2018h] BYREF
char v10[4120]; // [rsp+1030h] [rbp-1018h] BYREF

requested_time.tv_sec = 0LL;
requested_time.tv_nsec = 10000000LL;
while ( 1 )
{
do
{
do
nanosleep(&requested_time, &remaining);
while ( !convert );
}
while ( recordingOn || convertInProgress );
convertInProgress = 1;
v1 = (const char *)GetFileToConvert();
v2 = (char *)v1;
if ( v1 )
{
snprintf(s, 0x1000uLL, "%s", v1);
free(v2);
v3 = (const char *)ConvertOutputFileName(s);
v4 = (char *)v3;
if ( v3 )
{
snprintf(v10, 0x1000uLL, "%s", v3);
free(v4);
v5 = fork();
if ( v5 == -1 )
{
WriteToLog(1LL, "Unable to fork child process");
}
else if ( v5 )
{
if ( v5 > 0 )
{
waitpid(v5, &stat_loc, 0);
if ( !BYTE1(stat_loc) )
{
convertInProgress = 0;
unlink(s);
}
}
}
else
{
execl("/usr/bin/MP4Box", "MP4Box", "-fps", "25", "-add", s, v10);
}
}
else
{
convertInProgress = 0;
free(v2);
}
}
else
{
convert = 0;
convertInProgress = 0;
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
const char *v1; // rax
char *v2; // rbp
const char *v3; // rax
char *v4; // r12
int v5; // eax
int stat_loc; // [rsp+Ch] [rbp-203Ch] BYREF
struct timespec requested_time; // [rsp+10h] [rbp-2038h] BYREF
struct timespec remaining; // [rsp+20h] [rbp-2028h] BYREF
char s[4096]; // [rsp+30h] [rbp-2018h] BYREF
char v10[4120]; // [rsp+1030h] [rbp-1018h] BYREF

requested_time.tv_sec = 0LL;
requested_time.tv_nsec = 10000000LL;
while ( 1 )
{
do
{
do
nanosleep(&requested_time, &remaining);
while ( !convert );
}
while ( recordingOn || convertInProgress );
convertInProgress = 1;
v1 = (const char *)GetFileToConvert();
v2 = (char *)v1;
if ( v1 )
{
snprintf(s, 0x1000uLL, "%s", v1);
free(v2);
v3 = (const char *)ConvertOutputFileName(s);
v4 = (char *)v3;
if ( v3 )
{
snprintf(v10, 0x1000uLL, "%s", v3);
free(v4);
v5 = fork();
if ( v5 == -1 )
{
WriteToLog(1LL, "Unable to fork child process");
}
else if ( v5 )
{
if ( v5 > 0 )
{
waitpid(v5, &stat_loc, 0);
if ( !BYTE1(stat_loc) )
{
convertInProgress = 0;
unlink(s);
}
}
}
else
{
execl("/usr/bin/MP4Box", "MP4Box", "-fps", "25", "-add", s, v10);
}
}
else
{
convertInProgress = 0;
free(v2);
}
}
else
{
convert = 0;
convertInProgress = 0;
}
}
}

original_code:
void *ConvertThread(void *param)
{
 struct timespec req, rem;
 req.tv_sec = 0;
 req.tv_nsec = 10000000;
 while(1)
 {
  nanosleep(&req, &rem);
  if (!convert || recordingOn || convertInProgress)
  {
   continue;
  }
  convertInProgress = 1;
  char *inputFile = GetFileToConvert();
  if (inputFile == ((void *)0))
  {
   convert = 0;
   convertInProgress = 0;
   continue;
  }
  char input[4096];
  snprintf(input, 4096, "%s", inputFile);
  free(inputFile);
  char *outputFile = ConvertOutputFileName(input);
  if (outputFile == ((void *)0))
  {
   convertInProgress = 0;
   free(inputFile);
   continue;
  }
  char output[4096];
  snprintf(output, 4096, "%s", outputFile);
  free(outputFile);
  pid_t convertPid = fork();
  if (convertPid == -1)
  {
   WriteToLog(1, "Unable to fork child process");
   continue;
  }
  else if (convertPid == 0)
  {
   execl("/usr/bin/MP4Box", "MP4Box", "-fps", "25", "-add", input, output);
  }
  else if (convertPid > 0)
  {
   int status;
   waitpid(convertPid, &status, 0);
   if ((((status) & 0xff00) >> 8) == 0)
   {
    convertInProgress = 0;
    unlink(input);
   }
  }
 }
}

canonical_original_code:
void *ConvertThread(void *param)
{
 struct timespec req; struct timespec rem;
 req.tv_sec = 0;
 req.tv_nsec = 10000000;
 while(1)
 {
  nanosleep(&req, &rem);
  if (!convert || recordingOn || convertInProgress)
  {
   continue;
  }
  convertInProgress = 1;
  char *inputFile = GetFileToConvert();
  if (inputFile == ((void *)0))
  {
   convert = 0;
   convertInProgress = 0;
   continue;
  }
  char input[4096];
  snprintf(input, 4096, "%s", inputFile);
  free(inputFile);
  char *outputFile = ConvertOutputFileName(input);
  if (outputFile == ((void *)0))
  {
   convertInProgress = 0;
   free(inputFile);
   continue;
  }
  char output[4096];
  snprintf(output, 4096, "%s", outputFile);
  free(outputFile);
  int convertPid = fork();
  if (convertPid == -1)
  {
   WriteToLog(1, "Unable to fork child process");
   continue;
  }
  else if (convertPid == 0)
  {
   execl("/usr/bin/MP4Box", "MP4Box", "-fps", "25", "-add", input, output);
  }
  else if (convertPid > 0)
  {
   int status;
   waitpid(convertPid, &status, 0);
   if ((((status) & 0xff00) >> 8) == 0)
   {
    convertInProgress = 0;
    unlink(input);
   }
  }
 }
}

variable_types:
param
void *

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

variable_types:
inputFile
char *

variable_types:
input
char [4096]

variable_types:
outputFile
char *

variable_types:
output
char [4096]

variable_types: convertPid int

variable_types: status int

return_type: void *


NetworkThread

decompiled_code:
void __fastcall __noreturn NetworkThread(void *a1)
{
int v1; // ebx
struct timespec v2; // [rsp+0h] [rbp-28h] BYREF
struct timespec remaining; // [rsp+10h] [rbp-18h] BYREF

v2.tv_sec = 0LL;
v2.tv_nsec = 100000000LL;
while ( 1 )
{
v1 = CheckLink("eth0");
WriteToLog(0LL, "Still running...");
if ( v1 )
{
if ( v1 == 1 )
{
if ( recordingOn )
{
TerminateRecording(0LL, "Still running...");
recordingOn = 0;
convert = 1;
}
else if ( !convertInProgress )
{
convert = 1;
}
}
else if ( v1 == -1 )
{
WriteToLog(0LL, "Error while checking for connection state");
}
}
else if ( !convert )
{
recordingOn = 1;
}
nanosleep(&v2, &remaining);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1(void *a1)
{
int v1; // ebx
struct timespec v2; // [rsp+0h] [rbp-28h] BYREF
struct timespec remaining; // [rsp+10h] [rbp-18h] BYREF

v2.tv_sec = 0LL;
v2.tv_nsec = 100000000LL;
while ( 1 )
{
v1 = CheckLink("eth0");
WriteToLog(0LL, "Still running...");
if ( v1 )
{
if ( v1 == 1 )
{
if ( recordingOn )
{
TerminateRecording(0LL, "Still running...");
recordingOn = 0;
convert = 1;
}
else if ( !convertInProgress )
{
convert = 1;
}
}
else if ( v1 == -1 )
{
WriteToLog(0LL, "Error while checking for connection state");
}
}
else if ( !convert )
{
recordingOn = 1;
}
nanosleep(&v2, &remaining);
}
}

original_code:
void *NetworkThread(void *param)
{
 struct timespec req, rem;
 req.tv_sec = 0;
 req.tv_nsec = 100000000;
 while(1)
 {
  int retval = CheckLink("eth0");
  WriteToLog(0, "Still running...");
  switch(retval)
  {
   case -1:
    WriteToLog(0, "Error while checking for connection state");
    break;
   case 0:
       if (!convert)
    {
     recordingOn = 1;
    }
    break;
   case 1:
    if (recordingOn)
    {
     TerminateRecording();
     recordingOn = 0;
     convert = 1;
    }
    else if (!convertInProgress)
    {
     convert=1;
    }
    break;
  };
  nanosleep(&req, &rem);
 }
 return 0;
}

canonical_original_code:
void *NetworkThread(void *param)
{
 struct timespec req; struct timespec rem;
 req.tv_sec = 0;
 req.tv_nsec = 100000000;
 while(1)
 {
  int retval = CheckLink("eth0");
  WriteToLog(0, "Still running...");
  switch(retval)
  {
   case -1:
    WriteToLog(0, "Error while checking for connection state");
    break;
   case 0:
       if (!convert)
    {
     recordingOn = 1;
    }
    break;
   case 1:
    if (recordingOn)
    {
     TerminateRecording();
     recordingOn = 0;
     convert = 1;
    }
    else if (!convertInProgress)
    {
     convert=1;
    }
    break;
  };
  nanosleep(&req, &rem);
 }
 return 0;
}

variable_types: retval int

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


RecordingThread

decompiled_code:
void *__fastcall RecordingThread(void *a1)
{
int v1; // eax
__int64 v2; // r13
void *v3; // r12
char *v4; // rax
char *v5; // rbx
int stat_loc; // [rsp+Ch] [rbp-3Ch] BYREF
struct timespec requested_time; // [rsp+10h] [rbp-38h] BYREF
struct timespec remaining; // [rsp+20h] [rbp-28h] BYREF

requested_time.tv_sec = 0LL;
requested_time.tv_nsec = 10000000LL;
while ( 1 )
{
do
{
do
nanosleep(&requested_time, &remaining);
while ( !recordingOn );
}
while ( convert );
v1 = fork();
if ( v1 == -1 )
break;
if ( v1 )
{
if ( v1 > 0 )
waitpid(v1, &stat_loc, 0);
}
else
{
v2 = GetMaxRecordingTime();
if ( !v2 )
{
WriteToLog(1LL, "Not enough space to start recording");
exit(1);
}
v3 = (void *)OutputFileName();
if ( !v3 )
{
WriteToLog(1LL, "Cannot get output file name.");
exit(1);
}
v4 = (char *)calloc(0x40uLL, 1uLL);
v5 = v4;
if ( !v4 )
{
free(v3);
WriteToLog(1LL, "Cannot convert duration to string");
exit(1);
}
snprintf(v4, 0x40uLL, "%lu", v2);
execl(
"/usr/bin/raspivid",
"raspivid",
"-o",
v3,
"-t",
v5,
"-vs",
"-rot",
"270",
"-ex",
"antishake",
"-awb",
"auto",
"-ifx",
"none",
"-b",
"2000000",
"-fps",
"25",
0LL);
}
}
WriteToLog(1LL, "Unable to fork child process");
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func2(void *a1)
{
int v1; // eax
__int64 v2; // r13
void *v3; // r12
char *v4; // rax
char *v5; // rbx
int stat_loc; // [rsp+Ch] [rbp-3Ch] BYREF
struct timespec requested_time; // [rsp+10h] [rbp-38h] BYREF
struct timespec remaining; // [rsp+20h] [rbp-28h] BYREF

requested_time.tv_sec = 0LL;
requested_time.tv_nsec = 10000000LL;
while ( 1 )
{
do
{
do
nanosleep(&requested_time, &remaining);
while ( !recordingOn );
}
while ( convert );
v1 = fork();
if ( v1 == -1 )
break;
if ( v1 )
{
if ( v1 > 0 )
waitpid(v1, &stat_loc, 0);
}
else
{
v2 = GetMaxRecordingTime();
if ( !v2 )
{
WriteToLog(1LL, "Not enough space to start recording");
exit(1);
}
v3 = (void *)OutputFileName();
if ( !v3 )
{
WriteToLog(1LL, "Cannot get output file name.");
exit(1);
}
v4 = (char *)calloc(0x40uLL, 1uLL);
v5 = v4;
if ( !v4 )
{
free(v3);
WriteToLog(1LL, "Cannot convert duration to string");
exit(1);
}
snprintf(v4, 0x40uLL, "%lu", v2);
execl(
"/usr/bin/raspivid",
"raspivid",
"-o",
v3,
"-t",
v5,
"-vs",
"-rot",
"270",
"-ex",
"antishake",
"-awb",
"auto",
"-ifx",
"none",
"-b",
"2000000",
"-fps",
"25",
0LL);
}
}
WriteToLog(1LL, "Unable to fork child process");
return 0LL;
}

original_code:
void *RecordingThread(void *param)
{
 struct timespec req, rem;
 req.tv_sec = 0;
 req.tv_nsec = 10000000;
 while (1)
 {
  nanosleep(&req, &rem);
  if ((!recordingOn) || (convert))
  {
   continue;
  }
  pid_t raspividPid = fork();
  if (raspividPid == -1)
  {
   WriteToLog(1, "Unable to fork child process");
   return 0;
  }
  else if (raspividPid == 0)
  {
   char *rotation = "270";
   unsigned long maxTime = GetMaxRecordingTime();
   if (maxTime == 0)
   {
    WriteToLog(1, "Not enough space to start recording");
    _exit(1);
   }
   char *fileName = OutputFileName();
   if (fileName == ((void *)0))
   {
    WriteToLog(1, "Cannot get output file name.");
    _exit(1);
   }
   char *durationStr = calloc(64, sizeof(char));
   if (durationStr == ((void *)0))
   {
    free(fileName);
    WriteToLog(1, "Cannot convert duration to string");
    _exit(1);
   }
   snprintf(durationStr, 64, "%lu", maxTime);
   execl("/usr/bin/raspivid","raspivid", "-o", fileName, "-t", durationStr, "-vs", "-rot", rotation, "-ex", "antishake", "-awb", "auto", "-ifx", "none", "-b", "2000000", "-fps", "25", ((void *)0));
  }
  else if (raspividPid > 0)
  {
   int status;
   waitpid(raspividPid, &status, 0);
  }
 }
 return 0;
}

canonical_original_code:
void *RecordingThread(void *param)
{
 struct timespec req; struct timespec rem;
 req.tv_sec = 0;
 req.tv_nsec = 10000000;
 while (1)
 {
  nanosleep(&req, &rem);
  if ((!recordingOn) || (convert))
  {
   continue;
  }
  int raspividPid = fork();
  if (raspividPid == -1)
  {
   WriteToLog(1, "Unable to fork child process");
   return 0;
  }
  else if (raspividPid == 0)
  {
   char *rotation = "270";
   unsigned long maxTime = GetMaxRecordingTime();
   if (maxTime == 0)
   {
    WriteToLog(1, "Not enough space to start recording");
    _exit(1);
   }
   char *fileName = OutputFileName();
   if (fileName == ((void *)0))
   {
    WriteToLog(1, "Cannot get output file name.");
    _exit(1);
   }
   char *durationStr = calloc(64, sizeof(char));
   if (durationStr == ((void *)0))
   {
    free(fileName);
    WriteToLog(1, "Cannot convert duration to string");
    _exit(1);
   }
   snprintf(durationStr, 64, "%lu", maxTime);
   execl("/usr/bin/raspivid","raspivid", "-o", fileName, "-t", durationStr, "-vs", "-rot", rotation, "-ex", "antishake", "-awb", "auto", "-ifx", "none", "-b", "2000000", "-fps", "25", ((void *)0));
  }
  else if (raspividPid > 0)
  {
   int status;
   waitpid(raspividPid, &status, 0);
  }
 }
 return 0;
}

variable_types: raspividPid int

variable_types:
rotation
char *

variable_types:
maxTime
unsigned long

variable_types:
fileName
char *

variable_types:
durationStr
char *


mainloop

decompiled_code:
int mainloop()
{
int result; // eax
pthread_t th; // [rsp+0h] [rbp-28h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-20h] BYREF
pthread_t v3; // [rsp+10h] [rbp-18h] BYREF
void *thread_return[2]; // [rsp+18h] [rbp-10h] BYREF

recordingOn = 0;
convert = 0;
convertInProgress = 0;
if ( pthread_create(&th, 0LL, RecordingThread, 0LL) == -1 )
{
WriteToLog(1LL, "Cannot create Recording Thread");
exit(1);
}
if ( pthread_create(&newthread, 0LL, (void *(*)(void *))NetworkThread, 0LL) == -1 )
{
WriteToLog(1LL, "Cannot create Networking Thread");
exit(1);
}
if ( pthread_create(&v3, 0LL, (void *(*)(void *))ConvertThread, 0LL) == -1 )
{
WriteToLog(1LL, "Cannot create Converting Thread");
exit(1);
}
if ( pthread_join(th, thread_return) == -1 )
{
WriteToLog(1LL, "Error while waiting for Recording Thread to finish");
exit(1);
}
if ( pthread_join(newthread, thread_return) == -1 )
{
WriteToLog(1LL, "Error while waiting for Networking Thread to finish");
exit(1);
}
result = pthread_join(v3, thread_return);
if ( result == -1 )
{
WriteToLog(1LL, "Error while waiting for Converting Thread to finish");
exit(1);
}
return result;
}

canonical_decompiled_code:
int func3()
{
int result; // eax
pthread_t th; // [rsp+0h] [rbp-28h] BYREF
pthread_t newthread; // [rsp+8h] [rbp-20h] BYREF
pthread_t v3; // [rsp+10h] [rbp-18h] BYREF
void *thread_return[2]; // [rsp+18h] [rbp-10h] BYREF

recordingOn = 0;
convert = 0;
convertInProgress = 0;
if ( pthread_create(&th, 0LL, RecordingThread, 0LL) == -1 )
{
WriteToLog(1LL, "Cannot create Recording Thread");
exit(1);
}
if ( pthread_create(&newthread, 0LL, (void *(*)(void *))NetworkThread, 0LL) == -1 )
{
WriteToLog(1LL, "Cannot create Networking Thread");
exit(1);
}
if ( pthread_create(&v3, 0LL, (void *(*)(void *))ConvertThread, 0LL) == -1 )
{
WriteToLog(1LL, "Cannot create Converting Thread");
exit(1);
}
if ( pthread_join(th, thread_return) == -1 )
{
WriteToLog(1LL, "Error while waiting for Recording Thread to finish");
exit(1);
}
if ( pthread_join(newthread, thread_return) == -1 )
{
WriteToLog(1LL, "Error while waiting for Networking Thread to finish");
exit(1);
}
result = pthread_join(v3, thread_return);
if ( result == -1 )
{
WriteToLog(1LL, "Error while waiting for Converting Thread to finish");
exit(1);
}
return result;
}

original_code:
void mainloop(void)
{
 pthread_t recordingThread;
 pthread_t networkThread;
 pthread_t convertingThread;
 recordingOn = 0;
 convert = 0;
 convertInProgress = 0;
 if (pthread_create(&recordingThread, ((void *)0), RecordingThread, ((void *)0)) == -1)
 {
  WriteToLog(1, "Cannot create Recording Thread");
  _exit(1);
 }
 if (pthread_create(&networkThread, ((void *)0), NetworkThread, ((void *)0)) == -1)
 {
  WriteToLog(1, "Cannot create Networking Thread");
  _exit(1);
 }
 if (pthread_create(&convertingThread, ((void *)0), ConvertThread, ((void *)0)) == -1)
 {
  WriteToLog(1, "Cannot create Converting Thread");
  _exit(1);
 }
 void *result;
 if (pthread_join(recordingThread, &result) == -1)
 {
  WriteToLog(1,"Error while waiting for Recording Thread to finish");
  _exit(1);
 }
 if (pthread_join(networkThread, &result) == -1)
 {
  WriteToLog(1, "Error while waiting for Networking Thread to finish");
  _exit(1);
 }
 if (pthread_join(convertingThread, &result) == -1)
 {
  WriteToLog(1, "Error while waiting for Converting Thread to finish");
  _exit(1);
 }
}

canonical_original_code:
void mainloop()
{
 unsigned long recordingThread;
 unsigned long networkThread;
 unsigned long convertingThread;
 recordingOn = 0;
 convert = 0;
 convertInProgress = 0;
 if (pthread_create(&recordingThread, ((void *)0), RecordingThread, ((void *)0)) == -1)
 {
  WriteToLog(1, "Cannot create Recording Thread");
  _exit(1);
 }
 if (pthread_create(&networkThread, ((void *)0), NetworkThread, ((void *)0)) == -1)
 {
  WriteToLog(1, "Cannot create Networking Thread");
  _exit(1);
 }
 if (pthread_create(&convertingThread, ((void *)0), ConvertThread, ((void *)0)) == -1)
 {
  WriteToLog(1, "Cannot create Converting Thread");
  _exit(1);
 }
 void *result;
 if (pthread_join(recordingThread, &result) == -1)
 {
  WriteToLog(1,"Error while waiting for Recording Thread to finish");
  _exit(1);
 }
 if (pthread_join(networkThread, &result) == -1)
 {
  WriteToLog(1, "Error while waiting for Networking Thread to finish");
  _exit(1);
 }
 if (pthread_join(convertingThread, &result) == -1)
 {
  WriteToLog(1, "Error while waiting for Converting Thread to finish");
  _exit(1);
 }
}

variable_types:
recordingThread
unsigned long

variable_types:
networkThread
unsigned long

variable_types:
convertingThread
unsigned long

variable_types:
result
void *


die

decompiled_code:
void __fastcall __noreturn die(__int64 a1)
{
WriteToLog(1LL, a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(__int64 a1)
{
WriteToLog(1LL, a1);
exit(1);
}

original_code:
static void die(char *msg)
{
 WriteToLog(1, msg);
 _exit(1);
}

variable_types:
msg
char *


daemonise

decompiled_code:
__int64 daemonise()
{
__pid_t v0; // eax
__pid_t v1; // eax

v0 = fork();
if ( v0 == -1 )
goto LABEL_12;
if ( v0 )
goto LABEL_11;
if ( setsid() == -1 )
die((__int64)"failed to become a session leader while daemonising");
signal(1, (__sighandler_t)((char *)die + 1));
v1 = fork();
if ( v1 == -1 )
LABEL_12:
die((__int64)"failed to fork while daemonising");
if ( v1 )
LABEL_11:
exit(0);
if ( chdir("/") == -1 )
die((__int64)"failed to change working directory while daemonising");
umask(0);
close(0);
close(1);
close(2);
if ( open("/dev/null", 0) == -1 )
die((__int64)"failed to reopen stdin while daemonising");
if ( open("/dev/null", 1) == -1 )
die((__int64)"failed to reopen stdout while daemonising");
if ( open("/dev/null", 2) == -1 )
die((__int64)"failed to reopen stderr while daemonising");
return mainloop();
}

canonical_decompiled_code:
__int64 func1()
{
__pid_t v0; // eax
__pid_t v1; // eax

v0 = fork();
if ( v0 == -1 )
goto LABEL_12;
if ( v0 )
goto LABEL_11;
if ( setsid() == -1 )
func0((__int64)"failed to become a session leader while daemonising");
signal(1, (__sighandler_t)((char *)die + 1));
v1 = fork();
if ( v1 == -1 )
LABEL_12:
func0((__int64)"failed to fork while daemonising");
if ( v1 )
LABEL_11:
exit(0);
if ( chdir("/") == -1 )
func0((__int64)"failed to change working directory while daemonising");
umask(0);
close(0);
close(1);
close(2);
if ( open("/dev/null", 0) == -1 )
func0((__int64)"failed to reopen stdin while daemonising");
if ( open("/dev/null", 1) == -1 )
func0((__int64)"failed to reopen stdout while daemonising");
if ( open("/dev/null", 2) == -1 )
func0((__int64)"failed to reopen stderr while daemonising");
return mainloop();
}

original_code:
void daemonise(void)
{
 pid_t pid = fork();
 if (pid == -1)
 {
  die("failed to fork while daemonising");
 }
 else if (pid != 0)
 {
  _exit(0);
 }
 if (setsid() == -1)
 {
  die("failed to become a session leader while daemonising");
 }
 signal(1,((__sighandler_t) 1));
 pid = fork();
 if (pid == -1)
 {
  die("failed to fork while daemonising");
 }
 else if (pid != 0)
 {
  _exit(0);
 }
 if (chdir("/") == -1)
 {
  die("failed to change working directory while daemonising");
 }
 umask(0);
 close(0);
 close(1);
 close(2);
 if (open("/dev/null", 00) == -1)
 {
  die("failed to reopen stdin while daemonising");
 }
 if (open("/dev/null", 01) == -1)
 {
  die("failed to reopen stdout while daemonising");
 }
 if (open("/dev/null", 02) == -1)
 {
  die("failed to reopen stderr while daemonising");
 }
 mainloop();
}

canonical_original_code:
void daemonise()
{
 int pid = fork();
 if (pid == -1)
 {
  die("failed to fork while daemonising");
 }
 else if (pid != 0)
 {
  _exit(0);
 }
 if (setsid() == -1)
 {
  die("failed to become a session leader while daemonising");
 }
 signal(1,((void (*)(int)) 1));
 pid = fork();
 if (pid == -1)
 {
  die("failed to fork while daemonising");
 }
 else if (pid != 0)
 {
  _exit(0);
 }
 if (chdir("/") == -1)
 {
  die("failed to change working directory while daemonising");
 }
 umask(0);
 close(0);
 close(1);
 close(2);
 if (open("/dev/null", 00) == -1)
 {
  die("failed to reopen stdin while daemonising");
 }
 if (open("/dev/null", 01) == -1)
 {
  die("failed to reopen stdout while daemonising");
 }
 if (open("/dev/null", 02) == -1)
 {
  die("failed to reopen stderr while daemonising");
 }
 mainloop();
}

variable_types: pid int


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
const char *v4; // rdi
__int64 v5; // xmm0_8
double i; // xmm1_8
unsigned __int16 v7; // ax
unsigned __int16 v8; // ax
__int64 v9; // rdx
__int64 v10; // rcx
__int64 v11; // r8
__int64 v12; // r9
float v13; // xmm0_4
__int64 v14; // [rsp+0h] [rbp-18h]
float cosx; // [rsp+8h] [rbp-10h] BYREF
float sinx[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3 = 0;
puts("r=20000");
v4 = "e=1";
puts("e=1");
v5 = 1065353216LL;
for ( i = 0.0; ; *(_QWORD *)&i = LODWORD(sinx[0]) )
{
v7 = float_to_lasershark_xy(v4, argv, *(double *)&v5, i);
v8 = float_to_lasershark_xy("s=%u,%u,%u,%u,%u,%u\n", argv, v7, 4095LL, 4095LL, 1LL, v14);
printf("s=%u,%u,%u,%u,%u,%u\n", v8, v9, v10, v11, v12, 1LL);
argv = (const char **)&cosx;
v13 = (float)(v3 + 1) * 0.0062831854;
v4 = (const char *)sinx;
++v3;
sincosf(v13, sinx, &cosx);
v5 = LODWORD(cosx);
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
const char *v4; // rdi
__int64 v5; // xmm0_8
double i; // xmm1_8
unsigned __int16 v7; // ax
unsigned __int16 v8; // ax
__int64 v9; // rdx
__int64 v10; // rcx
__int64 v11; // r8
__int64 v12; // r9
float v13; // xmm0_4
__int64 v14; // [rsp+0h] [rbp-18h]
float cosx; // [rsp+8h] [rbp-10h] BYREF
float sinx[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3 = 0;
puts("r=20000");
v4 = "e=1";
puts("e=1");
v5 = 1065353216LL;
for ( i = 0.0; ; *(_QWORD *)&i = LODWORD(sinx[0]) )
{
v7 = func7(v4, argv, *(double *)&v5, i);
v8 = func7("s=%u,%u,%u,%u,%u,%u\n", argv, v7, 4095LL, 4095LL, 1LL, v14);
printf("s=%u,%u,%u,%u,%u,%u\n", v8, v9, v10, v11, v12, 1LL);
argv = (const char **)&cosx;
v13 = (float)(v3 + 1) * 0.0062831854;
v4 = (const char *)sinx;
++v3;
sincosf(v13, sinx, &cosx);
v5 = LODWORD(cosx);
}
}

original_code:
int main (int argc, char *argv[])
{
    double x_f, y_f;
    uint32_t index = 0;
    float step = 2*3.14159265358979323846/1000;
    printf("r=20000\n");
    printf("e=1\n");
    x_f = 0;
    y_f = 0;
    while(1) {
        x_f = sinf(index*step);
        y_f = cosf(index*step);
        printf("s=%u,%u,%u,%u,%u,%u\n",
               float_to_lasershark_xy(x_f), float_to_lasershark_xy(y_f), 4095,4095,1,1);
        index++;
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    double x_f; double y_f;
    unsigned int index = 0;
    float step = 2*3.14159265358979323846/1000;
    printf("r=20000\n");
    printf("e=1\n");
    x_f = 0;
    y_f = 0;
    while(1) {
        x_f = sinf(index*step);
        y_f = cosf(index*step);
        printf("s=%u,%u,%u,%u,%u,%u\n",
               float_to_lasershark_xy(x_f), float_to_lasershark_xy(y_f), 4095,4095,1,1);
        index++;
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
index
unsigned int

variable_types: step float


decompiled_code:
__int64 __fastcall float_to_lasershark_xy(const char *a1, float a2)
{
return (unsigned int)(int)((a2 + 1.0) * 4095.0 * 0.5);
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1, float a2)
{
return (unsigned int)(int)((a2 + 1.0) * 4095.0 * 0.5);
}

original_code:
static uint16_t float_to_lasershark_xy(float var)
{
    uint16_t val = (4095 * (var + 1.0)/2.0);
    return val;
}

canonical_original_code:
static unsigned short float_to_lasershark_xy(float var)
{
    unsigned short val = (4095 * (var + 1.0)/2.0);
    return val;
}

variable_types: var float

variable_types:
val
unsigned short

return_type: unsigned short


repo: macrosheep/snake

decompiled_code:
void __fastcall __noreturn get_input(void *a1)
{
while ( 1 )
input = wgetch(stdscr);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
while ( 1 )
input = wgetch(stdscr);
}

original_code:
void *get_input()
{
  while (1) {
    input = wgetch(stdscr);
  };
  pthread_exit(((void *)0));
}


decompiled_code:
__int64 setup_env()
{
setlocale(6, locale);
initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, 0);
keypad(stdscr, 1);
curs_set(0);
define_key(&locale[2], 9000);
define_key(&locale[4], 9001);
define_key(&locale[6], 9002);
define_key(&locale[8], 9003);
define_key(&locale[10], 9004);
define_key(&locale[12], 9005);
define_key(&locale[14], 9006);
define_key(&locale[16], 9007);
define_key(&locale[18], 9008);
start_color();
init_pair(1, 1, 0);
init_pair(2, 2, 0);
init_pair(3, 7, 0);
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
setlocale(6, locale);
initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, 0);
keypad(stdscr, 1);
curs_set(0);
define_key(&locale[2], 9000);
define_key(&locale[4], 9001);
define_key(&locale[6], 9002);
define_key(&locale[8], 9003);
define_key(&locale[10], 9004);
define_key(&locale[12], 9005);
define_key(&locale[14], 9006);
define_key(&locale[16], 9007);
define_key(&locale[18], 9008);
start_color();
init_pair(1, 1, 0);
init_pair(2, 2, 0);
init_pair(3, 7, 0);
return 0LL;
}

original_code:
int setup_env()
{
  setlocale(6, "C");
  initscr();
  cbreak();
  noecho();
  nonl();
  intrflush(stdscr, 0);
  keypad(stdscr, 1);
  curs_set(0);
  define_key("q", 9000);
  define_key("w", 9001);
  define_key("e", 9002);
  define_key("s", 9003);
  define_key("d", 9004);
  define_key("z", 9005);
  define_key("x", 9006);
  define_key("r", 9007);
  define_key("p", 9008);
  start_color();
  init_pair(1, 1, 0);
  init_pair(2, 2, 0);
  init_pair(3, 7, 0);
  return 0;
}


original_code:
int init_snake()
{
  snake_t * snake;
  snake_t * prev;
  int x = COLS/2;
  int y = LINES/2;
  int lx = 0;
  head = snake = malloc(sizeof(snake_t));
  if (((void *)0) == snake)
    return -1;
  snake->y = y;
  snake->x = x;
  snake->prev = ((void *)0);
  snake->next = ((void *)0);
  prev = head;
  lx = x-6;
  for (x--; x > lx; x--) {
    snake = malloc(sizeof(snake_t));
    if (((void *)0) == snake)
      return -1;
    snake->y = y;
    snake->x = x;
    snake->prev = prev;
    snake->prev->next = snake;
    snake->next = ((void *)0);
    prev = snake;
  }
  tail = prev;
  curr = RIGHT;
  return 0;
}

canonical_original_code:
int init_snake()
{
  struct snake *snake;
  struct snake *prev;
  int x = COLS/2;
  int y = LINES/2;
  int lx = 0;
  head = snake = malloc(sizeof(snake_t));
  if (((void *)0) == snake)
    return -1;
  snake->y = y;
  snake->x = x;
  snake->prev = ((void *)0);
  snake->next = ((void *)0);
  prev = head;
  lx = x-6;
  for (x--; x > lx; x--) {
    snake = malloc(sizeof(snake_t));
    if (((void *)0) == snake)
      return -1;
    snake->y = y;
    snake->x = x;
    snake->prev = prev;
    snake->prev->next = snake;
    snake->next = ((void *)0);
    prev = snake;
  }
  tail = prev;
  curr = RIGHT;
  return 0;
}

variable_types: struct snake { int x; int y; struct snake *prev; struct snake *next; } *

variable_types: x int

variable_types: y int

variable_types: lx int


decompiled_code:
__int64 draw_snake()
{
void *v0; // rbx

v0 = head;
wattr_on(stdscr, 0x200uLL, 0LL);
for ( ; v0; v0 = (void *)*((_QWORD *)v0 + 2) )
{
if ( wmove(stdscr, *((_DWORD *)v0 + 1), *(_DWORD *)v0) != -1 )
waddnstr(stdscr, &locale[20], -1);
}
wattr_off(stdscr, 0x200uLL, 0LL);
wrefresh(stdscr);
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
void *v0; // rbx

v0 = head;
wattr_on(stdscr, 0x200uLL, 0LL);
for ( ; v0; v0 = (void *)*((_QWORD *)v0 + 2) )
{
if ( wmove(stdscr, *((_DWORD *)v0 + 1), *(_DWORD *)v0) != -1 )
waddnstr(stdscr, &locale[20], -1);
}
wattr_off(stdscr, 0x200uLL, 0LL);
wrefresh(stdscr);
return 0LL;
}

user_defined_types: struct snake { int x; int y; struct snake *prev; struct snake *next; }


decompiled_code:
__int64 snake_grow_one()
{
_QWORD *v0; // rax
__int64 v1; // rdx

v0 = malloc(0x18uLL);
if ( !v0 )
return 0xFFFFFFFFLL;
v1 = tail;
*v0 = 0LL;
tail = (__int64)v0;
v0[1] = v1;
*(_QWORD *)(v1 + 16) = v0;
v0[2] = 0LL;
return 0LL;
}

canonical_decompiled_code:
__int64 func4()
{
_QWORD *v0; // rax
__int64 v1; // rdx

v0 = malloc(0x18uLL);
if ( !v0 )
return 0xFFFFFFFFLL;
v1 = tail;
*v0 = 0LL;
tail = (__int64)v0;
v0[1] = v1;
*(_QWORD *)(v1 + 16) = v0;
v0[2] = 0LL;
return 0LL;
}

original_code:
int snake_grow_one()
{
  snake_t * snake = ((void *)0);
  snake = malloc(sizeof(snake_t));
  if (((void *)0) == snake)
    return -1;
  snake->y = 0;
  snake->x = 0;
  snake->prev = tail;
  snake->prev->next = snake;
  snake->next = ((void *)0);
  tail = snake;
  return 0;
}

canonical_original_code:
int snake_grow_one()
{
  struct snake *snake = ((void *)0);
  snake = malloc(sizeof(snake_t));
  if (((void *)0) == snake)
    return -1;
  snake->y = 0;
  snake->x = 0;
  snake->prev = tail;
  snake->prev->next = snake;
  snake->next = ((void *)0);
  tail = snake;
  return 0;
}


decompiled_code:
__int64 destory_snake()
{
void *v0; // rbx
void *v1; // rdi
__int64 result; // rax

v0 = head;
while ( v0 )
{
v1 = v0;
v0 = (void *)*((_QWORD *)v0 + 2);
free(v1);
}
tail = 0LL;
result = 0LL;
head = 0LL;
return result;
}

canonical_decompiled_code:
__int64 func5()
{
void *v0; // rbx
void *v1; // rdi
__int64 result; // rax

v0 = head;
while ( v0 )
{
v1 = v0;
v0 = (void *)*((_QWORD *)v0 + 2);
free(v1);
}
tail = 0LL;
result = 0LL;
head = 0LL;
return result;
}

original_code:
int destory_snake()
{
  snake_t * snake = head;
  snake_t * tmp = ((void *)0);
  while (((void *)0) != snake) {
    tmp = snake;
    snake = snake->next;
    free(tmp);
  };
  head = tail = ((void *)0);
  return 0;
}

canonical_original_code:
int destory_snake()
{
  struct snake *snake = head;
  struct snake *tmp = ((void *)0);
  while (((void *)0) != snake) {
    tmp = snake;
    snake = snake->next;
    free(tmp);
  };
  head = tail = ((void *)0);
  return 0;
}


decompiled_code:
__int64 exit_snake()
{
destory_snake();
destory_map();
return 0LL;
}

canonical_decompiled_code:
__int64 func6()
{
func5();
destory_map();
return 0LL;
}

original_code:
int exit_snake()
{
  destory_snake();
  destory_map();
  return 0;
}


decompiled_code:
__int64 check_input()
{
switch ( input )
{
case 260:
if ( curr != 3 )
{
curr = 2;
goto LABEL_22;
}
return 0LL;
case 259:
if ( curr != 1 )
{
curr = 0;
input = 0;
}
return 0LL;
case 261:
if ( curr != 2 )
{
curr = 3;
goto LABEL_22;
}
return 0LL;
case 258:
if ( curr )
{
curr = 1;
goto LABEL_22;
}
return 0LL;
case 9001:
if ( curr != 7 )
{
curr = 4;
goto LABEL_22;
}
return 0LL;
case 9002:
if ( curr != 6 )
{
curr = 5;
goto LABEL_22;
}
return 0LL;
case 9003:
if ( curr != 5 )
{
curr = 6;
goto LABEL_22;
}
return 0LL;
case 9004:
if ( curr != 4 )
{
curr = 7;
goto LABEL_22;
}
return 0LL;
case 9000:
exit_flag = 1;
goto LABEL_22;
case 9008:
pause_flag = 1;
goto LABEL_22;
case 9005:
if ( (int)speed <= 999999 )
{
speed += 100000;
goto LABEL_22;
}
return 0LL;
}
if ( input != 9006 || (int)speed <= 100000 )
return 0LL;
speed -= 100000;
LABEL_22:
input = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 func7()
{
switch ( input )
{
case 260:
if ( curr != 3 )
{
curr = 2;
goto LABEL_22;
}
return 0LL;
case 259:
if ( curr != 1 )
{
curr = 0;
input = 0;
}
return 0LL;
case 261:
if ( curr != 2 )
{
curr = 3;
goto LABEL_22;
}
return 0LL;
case 258:
if ( curr )
{
curr = 1;
goto LABEL_22;
}
return 0LL;
case 9001:
if ( curr != 7 )
{
curr = 4;
goto LABEL_22;
}
return 0LL;
case 9002:
if ( curr != 6 )
{
curr = 5;
goto LABEL_22;
}
return 0LL;
case 9003:
if ( curr != 5 )
{
curr = 6;
goto LABEL_22;
}
return 0LL;
case 9004:
if ( curr != 4 )
{
curr = 7;
goto LABEL_22;
}
return 0LL;
case 9000:
exit_flag = 1;
goto LABEL_22;
case 9008:
pause_flag = 1;
goto LABEL_22;
case 9005:
if ( (int)speed <= 999999 )
{
speed += 100000;
goto LABEL_22;
}
return 0LL;
}
if ( input != 9006 || (int)speed <= 100000 )
return 0LL;
speed -= 100000;
LABEL_22:
input = 0;
return 0LL;
}

original_code:
int check_input()
{
  if (input == 0404 && curr != RIGHT)
    curr = LEFT;
  else if (input == 0403 && curr != DOWN)
    curr = UP;
  else if (input == 0405 && curr != LEFT)
    curr = RIGHT;
  else if (input == 0402 && curr != UP)
    curr = DOWN;
  else if (input == 9001 && curr != DOWN_RIGHT)
    curr = UP_LEFT;
  else if (input == 9002 && curr != DOWN_LEFT)
    curr = UP_RIGHT;
  else if (input == 9003 && curr != UP_RIGHT)
    curr = DOWN_LEFT;
  else if (input == 9004 && curr != UP_LEFT)
    curr = DOWN_RIGHT;
  else if (input == 9000)
    exit_flag = 1;
  else if (input == 9008)
    pause_flag = 1;
  else if (input == 9005 && speed < 1000000)
    speed = speed + 100000;
  else if (input == 9006 && speed > 100000)
    speed = speed - 100000;
  else
    return 0;
  input = 0;
  return 0;
}


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

result = (_QWORD *)*((_QWORD *)head + 2);
if ( !result )
return 0LL;
while ( *result != *(_QWORD *)head )
{
result = (_QWORD *)result[2];
if ( !result )
return result;
}
return (_QWORD *)(get_input + 1);
}

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

result = (_QWORD *)*((_QWORD *)head + 2);
if ( !result )
return 0LL;
while ( *result != *(_QWORD *)head )
{
result = (_QWORD *)result[2];
if ( !result )
return result;
}
return (_QWORD *)(get_input + 1);
}

original_code:
int is_dead_snake()
{
  snake_t * snake = head->next;
  while (((void *)0) != snake) {
    if (snake->x == head->x && snake->y == head->y)
      return 1;
    snake = snake->next;
  };
  return 0;
}

canonical_original_code:
int is_dead_snake()
{
  struct snake *snake = head->next;
  while (((void *)0) != snake) {
    if (snake->x == head->x && snake->y == head->y)
      return 1;
    snake = snake->next;
  };
  return 0;
}


decompiled_code:
__int64 check_dead()
{
int v0; // esi
_BOOL4 v2; // er8

if ( *(int *)head <= 0 )
return 1LL;
if ( *(_DWORD *)head >= COLS - 2 )
return 1LL;
v0 = *((_DWORD *)head + 1);
if ( v0 <= 1 || v0 >= LINES - 1 )
return 1LL;
if ( !(unsigned int)is_dead_snake() )
v2 = is_wall() != 0;
return v2;
}

canonical_decompiled_code:
__int64 func9()
{
int v0; // esi
_BOOL4 v2; // er8

if ( *(int *)head <= 0 )
return 1LL;
if ( *(_DWORD *)head >= COLS - 2 )
return 1LL;
v0 = *((_DWORD *)head + 1);
if ( v0 <= 1 || v0 >= LINES - 1 )
return 1LL;
if ( !(unsigned int)func8() )
v2 = is_wall() != 0;
return v2;
}

original_code:
int check_dead()
{
  if (head->x < 1 || head->x > COLS-3 || head->y < 2 || head->y > LINES-2)
    return 1;
  if (is_dead_snake())
    return 1;
  if (is_wall(head->x, head->y))
    return 1;
  return 0;
}


decompiled_code:
__int64 snake_dead_exit()
{
int v0; // eax

if ( wmove(stdscr, LINES / 2, COLS / 2 - 10) != -1 )
waddnstr(stdscr, "Retry(R) Exit(E)", -1);
while ( 1 )
{
v0 = wgetch(stdscr);
if ( v0 == 9002 )
break;
if ( v0 == 9007 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 func10()
{
int v0; // eax

if ( wmove(stdscr, LINES / 2, COLS / 2 - 10) != -1 )
waddnstr(stdscr, "Retry(R) Exit(E)", -1);
while ( 1 )
{
v0 = wgetch(stdscr);
if ( v0 == 9002 )
break;
if ( v0 == 9007 )
return 0LL;
}
return 1LL;
}

original_code:
int snake_dead_exit()
{
  int input;
  int ret;
  (wmove((stdscr),((LINES/2)),((COLS/2-10))) == (-1) ? (-1) : waddnstr((stdscr),(("Retry(R)  Exit(E)")),-1));
input:
  input = wgetch(stdscr);
  switch (input) {
    case 9002:
      ret = 1;
      break;
    case 9007:
      ret = 0;
      break;
    default:
      goto input;
  };
  return ret;
}

variable_types: input int

variable_types: ret int


decompiled_code:
void inc_score()
{
++score;
}

canonical_decompiled_code:
void func11()
{
++score;
}

original_code:
int inc_score()
{
  score++;
}

canonical_original_code:
int inc_score()
{
  score++;
}


decompiled_code:
__int64 draw_score()
{
char v1[40]; // [rsp+0h] [rbp-28h] BYREF

sprintf(v1, "Score: %d", (unsigned int)score);
if ( wmove(stdscr, 0, 1) != -1 )
waddnstr(stdscr, v1, -1);
wrefresh(stdscr);
return 0LL;
}

canonical_decompiled_code:
__int64 func12()
{
char v1[40]; // [rsp+0h] [rbp-28h] BYREF

sprintf(v1, "Score: %d", (unsigned int)score);
if ( wmove(stdscr, 0, 1) != -1 )
waddnstr(stdscr, v1, -1);
wrefresh(stdscr);
return 0LL;
}

original_code:
int draw_score()
{
  char str[20];
  sprintf(str, "Score: %d", score);
  (wmove((stdscr),((0)),((1))) == (-1) ? (-1) : waddnstr((stdscr),((str)),-1));
  wrefresh(stdscr);
  return 0;
}

variable_types:
str
char [20]


decompiled_code:
int draw_help()
{
if ( wmove(stdscr, 0, 15) != -1 )
waddnstr(stdscr, "q:quit p:pause | move: w:upleft e:upright s:downleft d:downright x:speedup z:speeddown", -1);
return wrefresh(stdscr);
}

canonical_decompiled_code:
int func13()
{
if ( wmove(stdscr, 0, 15) != -1 )
waddnstr(stdscr, "q:quit p:pause | move: w:upleft e:upright s:downleft d:downright x:speedup z:speeddown", -1);
return wrefresh(stdscr);
}

original_code:
int draw_help()
{
  (wmove((stdscr),((0)),((15))) == (-1) ? (-1) : waddnstr((stdscr),(("q:quit p:pause | move: w:upleft e:upright s:downleft d:downright x:speedup z:speeddown")),-1));
  wrefresh(stdscr);
}


decompiled_code:
int __fastcall draw_box(int a1, int a2, int a3, int a4)
{
int v6; // er14
int v7; // er12

if ( wmove(stdscr, a2, a1 + 1) != -1 )
whline(stdscr, HIDWORD(acs_map[56]), a4 - 1);
v6 = a2 + a3;
if ( wmove(stdscr, a2 + a3, a1 + 1) != -1 )
whline(stdscr, HIDWORD(acs_map[56]), a4 - 1);
if ( wmove(stdscr, a2 + 1, a1) != -1 )
wvline(stdscr, LODWORD(acs_map[60]), a3 - 1);
v7 = a1 + a4;
if ( wmove(stdscr, a2 + 1, v7) != -1 )
wvline(stdscr, LODWORD(acs_map[60]), a3 - 1);
if ( wmove(stdscr, a2, a1) != -1 )
waddch(stdscr, LODWORD(acs_map[54]));
if ( wmove(stdscr, a2, v7) != -1 )
waddch(stdscr, HIDWORD(acs_map[53]));
if ( wmove(stdscr, v6, a1) != -1 )
waddch(stdscr, HIDWORD(acs_map[54]));
if ( wmove(stdscr, v6, v7) != -1 )
waddch(stdscr, LODWORD(acs_map[53]));
return wrefresh(stdscr);
}

canonical_decompiled_code:
int __fastcall func14(int a1, int a2, int a3, int a4)
{
int v6; // er14
int v7; // er12

if ( wmove(stdscr, a2, a1 + 1) != -1 )
whline(stdscr, HIDWORD(acs_map[56]), a4 - 1);
v6 = a2 + a3;
if ( wmove(stdscr, a2 + a3, a1 + 1) != -1 )
whline(stdscr, HIDWORD(acs_map[56]), a4 - 1);
if ( wmove(stdscr, a2 + 1, a1) != -1 )
wvline(stdscr, LODWORD(acs_map[60]), a3 - 1);
v7 = a1 + a4;
if ( wmove(stdscr, a2 + 1, v7) != -1 )
wvline(stdscr, LODWORD(acs_map[60]), a3 - 1);
if ( wmove(stdscr, a2, a1) != -1 )
waddch(stdscr, LODWORD(acs_map[54]));
if ( wmove(stdscr, a2, v7) != -1 )
waddch(stdscr, HIDWORD(acs_map[53]));
if ( wmove(stdscr, v6, a1) != -1 )
waddch(stdscr, HIDWORD(acs_map[54]));
if ( wmove(stdscr, v6, v7) != -1 )
waddch(stdscr, LODWORD(acs_map[53]));
return wrefresh(stdscr);
}

variable_types: hight int

variable_types: width int


decompiled_code:
__int64 snake_start()
{
do
{
score = 0;
init_snake();
init_map();
wclear(stdscr);
wrefresh(stdscr);
draw_score();
draw_help();
draw_box(0, 1, LINES - 2, COLS - 2);
draw_snake();
draw_map();
gen_food();
if ( pthread_create(&thread_input, 0LL, (void *(*)(void *))get_input, 0LL) )
LABEL_13:
exit(1);
while ( 1 )
{
usleep(speed);
check_input();
if ( exit_flag )
{
pthread_cancel(thread_input);
exit_snake();
return 0LL;
}
if ( pause_flag )
{
pthread_cancel(thread_input);
wgetch(stdscr);
pause_flag = 0;
if ( pthread_create(&thread_input, 0LL, (void *(*)(void *))get_input, 0LL) )
goto LABEL_13;
}
move_snake();
if ( (unsigned int)check_dead() )
break;
if ( (unsigned int)check_food() )
{
inc_score();
draw_score();
snake_grow_one();
gen_food();
}
}
pthread_cancel(thread_input);
}
while ( !(unsigned int)snake_dead_exit() );
exit_snake();
return 0LL;
}

canonical_decompiled_code:
__int64 func15()
{
do
{
score = 0;
func2();
init_map();
wclear(stdscr);
wrefresh(stdscr);
func12();
func13();
func14(0, 1, LINES - 2, COLS - 2);
func3();
draw_map();
gen_food();
if ( pthread_create(&thread_input, 0LL, (void *(*)(void *))get_input, 0LL) )
LABEL_13:
exit(1);
while ( 1 )
{
usleep(speed);
func7();
if ( exit_flag )
{
pthread_cancel(thread_input);
func6();
return 0LL;
}
if ( pause_flag )
{
pthread_cancel(thread_input);
wgetch(stdscr);
pause_flag = 0;
if ( pthread_create(&thread_input, 0LL, (void *(*)(void *))get_input, 0LL) )
goto LABEL_13;
}
move_snake();
if ( (unsigned int)func9() )
break;
if ( (unsigned int)check_food() )
{
func11();
func12();
func4();
gen_food();
}
}
pthread_cancel(thread_input);
}
while ( !(unsigned int)func10() );
func6();
return 0LL;
}

original_code:
int snake_start()
{
restart:
  score = 0;
  init_snake();
  init_map();
  wclear(stdscr);
  wrefresh(stdscr);
  draw_score();
  draw_help();
  draw_box(0, 1, LINES-2, COLS-2);
  draw_snake();
  draw_map();
  gen_food();
  if ((pthread_create(&thread_input, ((void *)0), get_input, ((void *)0))) != 0)
    exit(1);
  while (1) {
    usleep(speed);
    check_input();
    if (exit_flag) {
      pthread_cancel(thread_input);
      exit_snake();
      break;
    }
    if (pause_flag) {
      pthread_cancel(thread_input);
      wgetch(stdscr);
      pause_flag = 0;
      if ((pthread_create(&thread_input, ((void *)0), get_input, ((void *)0))) != 0)
        exit(1);
    }
    move_snake();
    if (check_dead()) {
      pthread_cancel(thread_input);
      if (snake_dead_exit()) {
        exit_snake();
        break;
      }
      goto restart;
    }
    if (check_food()) {
      inc_score();
      draw_score();
      snake_grow_one();
      gen_food();
    }
  };
  return 0;
}


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

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

original_code:
int main()
{
  int i=0;
  setup_env();
  snake_start();
  endwin();
  return 0;
}

canonical_original_code:
int main()
{
  int i = 0;
  setup_env();
  snake_start();
  endwin();
  return 0;
}

variable_types: i int


decompiled_code:
__int64 cut_tail()
{
if ( wmove(stdscr, *(_DWORD *)(tail + 4LL), *tail) != -1 )
waddnstr(stdscr, " ", -1);
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
if ( wmove(stdscr, *(_DWORD *)(tail + 4LL), *tail) != -1 )
waddnstr(stdscr, " ", -1);
return 0LL;
}


decompiled_code:
__int64 draw_head()
{
wattr_on(stdscr, 0x200uLL, 0LL);
if ( wmove(stdscr, *(_DWORD *)(head + 4LL), *head) != -1 )
waddnstr(stdscr, "@", -1);
wattr_off(stdscr, 0x200uLL, 0LL);
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
wattr_on(stdscr, 0x200uLL, 0LL);
if ( wmove(stdscr, *(_DWORD *)(head + 4LL), *head) != -1 )
waddnstr(stdscr, "@", -1);
wattr_off(stdscr, 0x200uLL, 0LL);
return 0LL;
}


decompiled_code:
__int64 __fastcall move_one_step(int a1, int a2)
{
__int64 v2; // rax
__int64 v3; // rcx
__int64 v4; // rdx

cut_tail();
v2 = tail;
v3 = head;
v4 = *(_QWORD *)(tail + 8LL);
*(_QWORD *)(v4 + 16) = 0LL;
*(_DWORD *)v2 = a1;
*(_DWORD *)(v2 + 4) = a2;
*(_QWORD *)(v2 + 16) = v3;
*(_QWORD *)(v2 + 8) = 0LL;
*(_QWORD *)(v3 + 8) = v2;
head = v2;
tail = v4;
draw_head();
wrefresh(stdscr);
return 0LL;
}

original_code:
int move_one_step(int x, int y)
{
  snake_t * snake;
  cut_tail();
  snake = tail->prev;
  snake->next = ((void *)0);
  tail->x = x;
  tail->y = y;
  tail->prev = ((void *)0);
  tail->next = head;
  head->prev = tail;
  head = tail;
  tail = snake;
  draw_head();
  wrefresh(stdscr);
  return 0;
}

canonical_original_code:
int move_one_step(int x, int y)
{
  struct snake *snake;
  cut_tail();
  snake = tail->prev;
  snake->next = ((void *)0);
  tail->x = x;
  tail->y = y;
  tail->prev = ((void *)0);
  tail->next = head;
  head->prev = tail;
  head = tail;
  tail = snake;
  draw_head();
  wrefresh(stdscr);
  return 0;
}


decompiled_code:
__int64 move_left()
{
move_one_step(*head - 1, *(_DWORD *)(head + 4LL));
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
func2(*head - 1, *(_DWORD *)(head + 4LL));
return 0LL;
}

original_code:
int move_left()
{
  move_one_step(head->x - 1, head->y);
  return 0;
}


decompiled_code:
__int64 move_right()
{
move_one_step(*head + 1, *(_DWORD *)(head + 4LL));
return 0LL;
}

canonical_decompiled_code:
__int64 func4()
{
func2(*head + 1, *(_DWORD *)(head + 4LL));
return 0LL;
}

original_code:
int move_right()
{
  move_one_step(head->x + 1, head->y);
  return 0;
}


decompiled_code:
__int64 move_up()
{
move_one_step(*head, *(_DWORD *)(head + 4LL) - 1);
return 0LL;
}

canonical_decompiled_code:
__int64 func5()
{
func2(*head, *(_DWORD *)(head + 4LL) - 1);
return 0LL;
}

original_code:
int move_up()
{
  move_one_step(head->x, head->y - 1);
  return 0;
}


decompiled_code:
__int64 move_down()
{
move_one_step(*head, *(_DWORD *)(head + 4LL) + 1);
return 0LL;
}

canonical_decompiled_code:
__int64 func6()
{
func2(*head, *(_DWORD *)(head + 4LL) + 1);
return 0LL;
}

original_code:
int move_down()
{
  move_one_step(head->x, head->y + 1);
  return 0;
}


decompiled_code:
__int64 move_up_left()
{
move_one_step(*head - 1, *(_DWORD *)(head + 4LL) - 1);
return 0LL;
}

canonical_decompiled_code:
__int64 func7()
{
func2(*head - 1, *(_DWORD *)(head + 4LL) - 1);
return 0LL;
}

original_code:
int move_up_left()
{
  move_one_step(head->x - 1, head->y - 1);
  return 0;
}


decompiled_code:
__int64 move_up_right()
{
move_one_step(*head + 1, *(_DWORD *)(head + 4LL) - 1);
return 0LL;
}

canonical_decompiled_code:
__int64 func8()
{
func2(*head + 1, *(_DWORD *)(head + 4LL) - 1);
return 0LL;
}

original_code:
int move_up_right()
{
  move_one_step(head->x + 1, head->y - 1);
  return 0;
}


decompiled_code:
__int64 move_down_left()
{
move_one_step(*head - 1, *(_DWORD *)(head + 4LL) + 1);
return 0LL;
}

canonical_decompiled_code:
__int64 func9()
{
func2(*head - 1, *(_DWORD *)(head + 4LL) + 1);
return 0LL;
}

original_code:
int move_down_left()
{
  move_one_step(head->x - 1, head->y + 1);
  return 0;
}


decompiled_code:
__int64 move_down_right()
{
move_one_step(*head + 1, *(_DWORD *)(head + 4LL) + 1);
return 0LL;
}

canonical_decompiled_code:
__int64 func10()
{
func2(*head + 1, *(_DWORD *)(head + 4LL) + 1);
return 0LL;
}

original_code:
int move_down_right()
{
  move_one_step(head->x + 1, head->y + 1);
  return 0;
}


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

switch ( curr )
{
case 0:
result = move_up();
break;
case 1:
result = move_down();
break;
case 2:
result = move_left();
break;
case 3:
result = move_right();
break;
case 4:
result = move_up_left();
break;
case 5:
result = move_up_right();
break;
case 6:
result = move_down_left();
break;
case 7:
result = move_down_right();
break;
default:
result = 0LL;
break;
}
return result;
}

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

switch ( curr )
{
case 0:
result = func5();
break;
case 1:
result = func6();
break;
case 2:
result = func3();
break;
case 3:
result = func4();
break;
case 4:
result = func7();
break;
case 5:
result = func8();
break;
case 6:
result = func9();
break;
case 7:
result = func10();
break;
default:
result = 0LL;
break;
}
return result;
}

original_code:
int move_snake()
{
  switch (curr) {
    case UP:
      move_up();
      break;
    case DOWN:
      move_down();
      break;
    case RIGHT:
      move_right();
      break;
    case LEFT:
      move_left();
      break;
    case UP_LEFT:
      move_up_left();
      break;
    case UP_RIGHT:
      move_up_right();
      break;
    case DOWN_RIGHT:
      move_down_right();
      break;
    case DOWN_LEFT:
      move_down_left();
      break;
    default:
      return 0;
  };
}


decompiled_code:
__int64 __fastcall is_snake(int a1, int a2)
{
__int64 result; // rax

result = head;
if ( !head )
return 0LL;
while ( *(_DWORD *)result != a1 || *(_DWORD *)(result + 4) != a2 )
{
result = *(_QWORD *)(result + 16);
if ( !result )
return result;
}
return 1LL;
}

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

result = head;
if ( !head )
return 0LL;
while ( *(_DWORD *)result != a1 || *(_DWORD *)(result + 4) != a2 )
{
result = *(_QWORD *)(result + 16);
if ( !result )
return result;
}
return 1LL;
}

original_code:
int is_snake(int x, int y)
{
  snake_t * snake = head;
  while (((void *)0) != snake) {
    if (snake->x == x && snake->y == y)
      return 1;
    snake = snake->next;
  };
  return 0;
}

canonical_original_code:
int is_snake(int x, int y)
{
  struct snake *snake = head;
  while (((void *)0) != snake) {
    if (snake->x == x && snake->y == y)
      return 1;
    snake = snake->next;
  };
  return 0;
}


decompiled_code:
_BOOL8 invalid_food()
{
int v0; // er8
_BOOL8 result; // rax

v0 = is_snake(food, *(&food + 1));
result = 1LL;
if ( !v0 )
result = (unsigned int)is_wall() != 0;
return result;
}

canonical_decompiled_code:
_BOOL8 func1()
{
int v0; // er8
_BOOL8 result; // rax

v0 = func0(food, *(&food + 1));
result = 1LL;
if ( !v0 )
result = (unsigned int)is_wall() != 0;
return result;
}

original_code:
int invalid_food()
{
  if (is_snake(food.x, food.y))
    return 1;
  if (is_wall(food.x, food.y))
    return 1;
  return 0;
}


decompiled_code:
__int64 gen_food()
{
unsigned int v0; // eax

do
{
v0 = time(0LL);
srand(v0);
food = rand() % (COLS - 2) + 1;
*(&food + 1) = rand() % (LINES - 3) + 2;
}
while ( invalid_food() );
wattr_on(stdscr, 0x100uLL, 0LL);
if ( wmove(stdscr, *(&food + 1), food) != -1 )
waddnstr(stdscr, "*", -1);
wattr_off(stdscr, 0x100uLL, 0LL);
wrefresh(stdscr);
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
unsigned int v0; // eax

do
{
v0 = time(0LL);
srand(v0);
food = rand() % (COLS - 2) + 1;
*(&food + 1) = rand() % (LINES - 3) + 2;
}
while ( func1() );
wattr_on(stdscr, 0x100uLL, 0LL);
if ( wmove(stdscr, *(&food + 1), food) != -1 )
waddnstr(stdscr, "*", -1);
wattr_off(stdscr, 0x100uLL, 0LL);
wrefresh(stdscr);
return 0LL;
}


decompiled_code:
_BOOL8 check_food()
{
_BOOL8 result; // rax

result = 0LL;
if ( *head == food )
result = *(_DWORD *)(head + 4LL) == *(&food + 1);
return result;
}

canonical_decompiled_code:
_BOOL8 func3()
{
_BOOL8 result; // rax

result = 0LL;
if ( *head == food )
result = *(_DWORD *)(head + 4LL) == *(&food + 1);
return result;
}

original_code:
int check_food()
{
  if (head->x == food.x && head->y == food.y)
    return 1;
  return 0;
}


original_code:
int init_map()
{
  int i,j;
  wall_map = (int **)calloc(COLS, sizeof(int*));
  for (i=0; i<COLS; i++)
    wall_map[i] = (int *)calloc(LINES, sizeof(int));
  for (i=0; i<10; i++)
    wall_map[COLS/2+20][LINES/2-5+i] = 1;
  for (i=0; i<10; i++)
    wall_map[COLS/2+21][LINES/2-5+i] = 1;
  for (i=0; i<10; i++)
    wall_map[COLS/2-20][LINES/2-5+i] = 1;
  for (i=0; i<10; i++)
    wall_map[COLS/2-21][LINES/2-5+i] = 1;
}

canonical_original_code:
int init_map()
{
  int i; int j;
  wall_map = (int **)calloc(COLS, sizeof(int *));
  for (i=0; i<COLS; i++)
    wall_map[i] = (int *)calloc(LINES, sizeof(int));
  for (i=0; i<10; i++)
    wall_map[COLS/2+20][LINES/2-5+i] = 1;
  for (i=0; i<10; i++)
    wall_map[COLS/2+21][LINES/2-5+i] = 1;
  for (i=0; i<10; i++)
    wall_map[COLS/2-20][LINES/2-5+i] = 1;
  for (i=0; i<10; i++)
    wall_map[COLS/2-21][LINES/2-5+i] = 1;
}

variable_types: j int


decompiled_code:
__int64 destory_map()
{
__int64 v0; // rbx
void *v1; // rdi

if ( COLS > 0 )
{
v0 = 0LL;
do
{
v1 = (void *)*((_QWORD *)wall_map + v0++);
free(v1);
}
while ( COLS > (int)v0 );
}
free(wall_map);
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 v0; // rbx
void *v1; // rdi

if ( COLS > 0 )
{
v0 = 0LL;
do
{
v1 = (void *)*((_QWORD *)wall_map + v0++);
free(v1);
}
while ( COLS > (int)v0 );
}
free(wall_map);
return 0LL;
}

original_code:
int destory_map()
{
  int i;
  for (i=0; i<COLS; i++)
    free(wall_map[i]);
  free(wall_map);
  return 0;
}


decompiled_code:
__int64 draw_map()
{
__int64 v0; // r12
__int64 i; // rbx

v0 = 0LL;
wattr_on(stdscr, 0x300uLL, 0LL);
if ( COLS > 0 )
{
do
{
for ( i = 0LL; LINES > (int)i; ++i )
{
while ( !*(_DWORD *)(*((_QWORD *)wall_map + v0) + 4 * i) || wmove(stdscr, i, v0) == -1 )
{
if ( LINES <= (int)++i )
goto LABEL_8;
}
waddnstr(stdscr, "#", -1);
}
LABEL_8:
++v0;
}
while ( COLS > (int)v0 );
}
wattr_off(stdscr, 0x300uLL, 0LL);
wrefresh(stdscr);
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // r12
__int64 i; // rbx

v0 = 0LL;
wattr_on(stdscr, 0x300uLL, 0LL);
if ( COLS > 0 )
{
do
{
for ( i = 0LL; LINES > (int)i; ++i )
{
while ( !*(_DWORD *)(*((_QWORD *)wall_map + v0) + 4 * i) || wmove(stdscr, i, v0) == -1 )
{
if ( LINES <= (int)++i )
goto LABEL_8;
}
waddnstr(stdscr, "#", -1);
}
LABEL_8:
++v0;
}
while ( COLS > (int)v0 );
}
wattr_off(stdscr, 0x300uLL, 0LL);
wrefresh(stdscr);
return 0LL;
}


decompiled_code:
_BOOL8 __fastcall is_wall(int a1, int a2)
{
return *(_DWORD *)(*((_QWORD *)wall_map + a1) + 4LL * a2) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(int a1, int a2)
{
return *(_DWORD *)(*((_QWORD *)wall_map + a1) + 4LL * a2) != 0;
}

original_code:
int is_wall(int x, int y)
{
  if (wall_map[x][y])
    return 1;
  return 0;
}


repo: macrosheep/superlrc

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

if ( (*((unsigned int (**)(void))player + 5))() || (result = init_player(), (_DWORD)result) )
{
*(_DWORD *)(a1 + 8) = (*(__int64 (**)(void))player)();
result = 1LL;
}
else
{
*(_DWORD *)(a1 + 8) = 2;
}
return result;
}

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

if ( (*((unsigned int (**)(void))player + 5))() || (result = func1(), (_DWORD)result) )
{
*(_DWORD *)(a1 + 8) = (*(__int64 (**)(void))player)();
result = 1LL;
}
else
{
*(_DWORD *)(a1 + 8) = 2;
}
return result;
}

original_code:
gboolean
lget_player_state(sLrcPlugin* lrcPlug)
{
 if (!player->init()) {
  if (!_init_player()) {
   lrcPlug->playerstate = STOPPED;
   return (0);
  }
 }
 lrcPlug->playerstate = player->player_get_state();
 return (!(0));
}

canonical_original_code:
int lget_player_state(struct slrcplugin *lrcPlug)
{
 if (!player->init()) {
  if (!_init_player()) {
   lrcPlug->playerstate = STOPPED;
   return (0);
  }
 }
 lrcPlug->playerstate = player->player_get_state();
 return (!(0));
}

variable_types: struct slrcplugin { struct sLrcInfo { char *title; char *artist; int length; struct _GPtrArray { void **pdata; unsigned int len; } *lyrcs; } *lrc; enum PlayerStateEnum { PLAYING=0, PAUSED=1, STOPPED=2 } playerstate; int songchanged; int lrcredowned; char *currsong; char *currsongpath; char *currlrcname; int totaltime; int currplaytime; int lastplaytime; int (*init_lyrcs)(char *filename, struct sLrcInfo *lrcinfo); int (*get_curr_lrcname)(struct slrcplugin *lrcPlug); int (*get_player_state)(struct slrcplugin *lrcPlug); int (*get_music_state)(struct slrcplugin *lrcPlug); int (*set_play_time)(int time); int (*get_avalible_lrc_num)(struct slrcplugin *lrcPlug, int *num); int (*get_avalible_lrc_info)(struct slrcplugin *lrcPlug, char **title, char **artist, char **album, int index); int (*get_lrc_by_index)(struct slrcplugin *lrcPlug, int index); } *

user_defined_types: struct slrcplugin { struct sLrcInfo *lrc; enum PlayerStateEnum playerstate; int songchanged; int lrcredowned; char *currsong; char *currsongpath; char *currlrcname; int totaltime; int currplaytime; int lastplaytime; int (*init_lyrcs)(char *filename, struct sLrcInfo *lrcinfo); int (*get_curr_lrcname)(struct slrcplugin *lrcPlug); int (*get_player_state)(struct slrcplugin *lrcPlug); int (*get_music_state)(struct slrcplugin *lrcPlug); int (*set_play_time)(int time); int (*get_avalible_lrc_num)(struct slrcplugin *lrcPlug, int *num); int (*get_avalible_lrc_info)(struct slrcplugin *lrcPlug, char **title, char **artist, char **album, int index); int (*get_lrc_by_index)(struct slrcplugin *lrcPlug, int index); }

user_defined_types: struct sLrcInfo { char *title; char *artist; int length; struct _GPtrArray *lyrcs; }


original_code:
gboolean
lget_music_state(sLrcPlugin* lrcPlug)
{
 gchar *currsong = ((void *)0);
 gchar *songpath = ((void *)0);
 if (!player->init()) {
  if (!_init_player())
   return (0);
 }
 player->player_get_currsong(&currsong,&songpath);
 if (!currsong) {g_printf("can't get currsong!\n");return (0);}
 if (lrcPlug->currsong) {
  if (g_strcmp0(lrcPlug->currsong,currsong)) {
   lrcPlug->songchanged = (!(0));
   g_free(lrcPlug->currsong);
   g_free(lrcPlug->currsongpath);
   lrcPlug->currsong = currsong;
   lrcPlug->currsongpath = songpath;
   goto otherinfo;
  }
  g_free(currsong);
  currsong = ((void *)0);
  g_free(songpath);
  songpath = ((void *)0);
 } else {
  lrcPlug->currsong = currsong;
  lrcPlug->currsongpath = songpath;
 }
otherinfo:
 lrcPlug->totaltime = player->player_get_totaltime();
 lrcPlug->lastplaytime = lrcPlug->currplaytime;
 lrcPlug->currplaytime = player->player_get_currplaytime();
 return (!(0));
}

canonical_original_code:
int lget_music_state(struct slrcplugin *lrcPlug)
{
 char *currsong = ((void *)0);
 char *songpath = ((void *)0);
 if (!player->init()) {
  if (!_init_player())
   return (0);
 }
 player->player_get_currsong(&currsong,&songpath);
 if (!currsong) {g_printf("can't get currsong!\n");return (0);}
 if (lrcPlug->currsong) {
  if (g_strcmp0(lrcPlug->currsong,currsong)) {
   lrcPlug->songchanged = (!(0));
   g_free(lrcPlug->currsong);
   g_free(lrcPlug->currsongpath);
   lrcPlug->currsong = currsong;
   lrcPlug->currsongpath = songpath;
   goto otherinfo;
  }
  g_free(currsong);
  currsong = ((void *)0);
  g_free(songpath);
  songpath = ((void *)0);
 } else {
  lrcPlug->currsong = currsong;
  lrcPlug->currsongpath = songpath;
 }
otherinfo:
 lrcPlug->totaltime = player->player_get_totaltime();
 lrcPlug->lastplaytime = lrcPlug->currplaytime;
 lrcPlug->currplaytime = player->player_get_currplaytime();
 return (!(0));
}

variable_types:
currsong
char *

variable_types:
songpath
char *

user_defined_types: enum PlayerStateEnum { PLAYING=0, PAUSED=1, STOPPED=2 }

user_defined_types: struct _GPtrArray { void **pdata; unsigned int len; }


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

if ( (*((unsigned int (**)(void))player + 5))() || (unsigned int)init_player() )
result = (*((__int64 (__fastcall **)(_QWORD))player + 4))(a1);
else
result = 0LL;
return result;
}

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

if ( (*((unsigned int (**)(void))player + 5))() || (unsigned int)func1() )
result = (*((__int64 (__fastcall **)(_QWORD))player + 4))(a1);
else
result = 0LL;
return result;
}

original_code:
gboolean
lset_play_time(gint time)
{
 if (!player->init()) {
  if (!_init_player())
   return (0);
 }
 player->player_set_currplaytime_second(time);
}

canonical_original_code:
int lset_play_time(int time)
{
 if (!player->init()) {
  if (!_init_player())
   return (0);
 }
 player->player_set_currplaytime_second(time);
}

variable_types: time int


decompiled_code:
__int64 __fastcall lget_curr_lrcname(__int64 a1)
{
__int64 v2; // rdi
__int64 v3; // r12
__int64 v4; // rax
__int64 result; // rax

v2 = *(_QWORD *)(a1 + 40);
if ( v2 )
{
g_free(v2);
*(_QWORD *)(a1 + 40) = 0LL;
}
v3 = g_strdup_printf("%s%s", *(const char **)(a1 + 24), ".lrc");
v4 = g_build_filename(*(_QWORD *)(a1 + 32), v3, 0LL);
*(_QWORD *)(a1 + 40) = v4;
if ( (unsigned int)g_file_test(v4, 16LL) || (unsigned int)download_lyric(a1, 0) )
{
g_free(v3);
result = 1LL;
}
else
{
g_free(v3);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 v2; // rdi
__int64 v3; // r12
__int64 v4; // rax
__int64 result; // rax

v2 = *(_QWORD *)(a1 + 40);
if ( v2 )
{
g_free(v2);
*(_QWORD *)(a1 + 40) = 0LL;
}
v3 = g_strdup_printf("%s%s", *(const char **)(a1 + 24), ".lrc");
v4 = g_build_filename(*(_QWORD *)(a1 + 32), v3, 0LL);
*(_QWORD *)(a1 + 40) = v4;
if ( (unsigned int)g_file_test(v4, 16LL) || (unsigned int)func2(a1, 0) )
{
g_free(v3);
result = 1LL;
}
else
{
g_free(v3);
result = 0LL;
}
return result;
}

original_code:
gboolean
lget_curr_lrcname(sLrcPlugin* lrcPlug)
{
 gchar *buf;
 gint i;
 if (lrcPlug->currlrcname) {
  g_free(lrcPlug->currlrcname);
  lrcPlug->currlrcname = ((void *)0);
 }
 buf = g_strdup_printf("%s%s",(lrcPlug->currsong),".lrc");
 lrcPlug->currlrcname = g_build_filename(lrcPlug->currsongpath,buf,((void *)0));
 if (!g_file_test(lrcPlug->currlrcname, G_FILE_TEST_EXISTS)) {
  if (!_download_lyric(lrcPlug,0)) {
   g_free(buf);
   return (0);
  }
 }
 g_free(buf);
 return (!(0));
}

canonical_original_code:
int lget_curr_lrcname(struct slrcplugin *lrcPlug)
{
 char *buf;
 int i;
 if (lrcPlug->currlrcname) {
  g_free(lrcPlug->currlrcname);
  lrcPlug->currlrcname = ((void *)0);
 }
 buf = g_strdup_printf("%s%s",(lrcPlug->currsong),".lrc");
 lrcPlug->currlrcname = g_build_filename(lrcPlug->currsongpath,buf,((void *)0));
 if (!g_file_test(lrcPlug->currlrcname, G_FILE_TEST_EXISTS)) {
  if (!_download_lyric(lrcPlug,0)) {
   g_free(buf);
   return (0);
  }
 }
 g_free(buf);
 return (!(0));
}

variable_types:
buf
char *


decompiled_code:
__int64 __fastcall lget_lrc_by_index(__int64 a1, unsigned int a2)
{
download_lyric(a1, a2);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, unsigned int a2)
{
func2(a1, a2);
return 1LL;
}

original_code:
gboolean
lget_lrc_by_index(struct slrcplugin* lrcPlug,gint index)
{
 _download_lyric(lrcPlug,index);
 return (!(0));
}

canonical_original_code:
int lget_lrc_by_index(struct slrcplugin *lrcPlug, int index)
{
 _download_lyric(lrcPlug,index);
 return (!(0));
}

variable_types: index int


decompiled_code:
_BOOL8 audacious_init()
{
return (unsigned int)audacious_get_state() != 2;
}

canonical_decompiled_code:
_BOOL8 func1()
{
return (unsigned int)func0() != 2;
}

original_code:
gboolean
audacious_init(void)
{
 if (audacious_get_state() == STOPPED)
  return (0);
 return (!(0));
}

canonical_original_code:
int audacious_init()
{
 if (audacious_get_state() == STOPPED)
  return (0);
 return (!(0));
}


decompiled_code:
__int64 __fastcall audacious_set_currplaytime_second(unsigned int a1)
{
const char *v1; // rbp

v1 = (const char *)g_strdup_printf("%s%d", "audtool --playback-seek ", a1);
system(v1);
g_free(v1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1)
{
const char *v1; // rbp

v1 = (const char *)g_strdup_printf("%s%d", "audtool --playback-seek ", a1);
system(v1);
g_free(v1);
return 1LL;
}

original_code:
gboolean
audacious_set_currplaytime_second(gint time)
{
 gchar *cmd;
 cmd = g_strdup_printf("%s%d","audtool --playback-seek ",time);
 system(cmd);
 g_free(cmd);
 return (!(0));
}

canonical_original_code:
int audacious_set_currplaytime_second(int time)
{
 char *cmd;
 cmd = g_strdup_printf("%s%d","audtool --playback-seek ",time);
 system(cmd);
 g_free(cmd);
 return (!(0));
}

variable_types:
cmd
char *


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

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

original_code:
gint
rhythmbox_get_totaltime()
{
 return 0;
}


decompiled_code:
_BOOL8 rhythmbox_init()
{
return (unsigned int)rhythmbox_get_state() != 2;
}

canonical_decompiled_code:
_BOOL8 func2()
{
return (unsigned int)func1() != 2;
}

original_code:
gboolean
rhythmbox_init(void)
{
 if (rhythmbox_get_state() == STOPPED)
  return (0);
 return (!(0));
}

canonical_original_code:
int rhythmbox_init()
{
 if (rhythmbox_get_state() == STOPPED)
  return (0);
 return (!(0));
}


decompiled_code:
__int64 __fastcall rhythmbox_set_currplaytime_second(unsigned int a1)
{
const char *v1; // rbp

v1 = (const char *)g_strdup_printf(
"%s%d%s",
"dbus-send --session --print-reply --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox/Player org.gnom"
"e.Rhythmbox.Player.setElapsed uint32:",
a1,
" >/dev/null");
system(v1);
g_free(v1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1)
{
const char *v1; // rbp

v1 = (const char *)g_strdup_printf(
"%s%d%s",
"dbus-send --session --print-reply --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox/Player org.gnom"
"e.Rhythmbox.Player.setElapsed uint32:",
a1,
" >/dev/null");
system(v1);
g_free(v1);
return 1LL;
}

original_code:
gboolean
rhythmbox_set_currplaytime_second(gint time)
{
        gchar *cmd;
        cmd = g_strdup_printf("%s%d%s","dbus-send --session --print-reply --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox/Player org.gnome.Rhythmbox.Player.setElapsed uint32:",time," >/dev/null");
        system(cmd);
        g_free(cmd);
        return (!(0));
}

canonical_original_code:
int rhythmbox_set_currplaytime_second(int time)
{
        char *cmd;
        cmd = g_strdup_printf("%s%d%s","dbus-send --session --print-reply --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox/Player org.gnome.Rhythmbox.Player.setElapsed uint32:",time," >/dev/null");
        system(cmd);
        g_free(cmd);
        return (!(0));
}


decompiled_code:
void __fastcall delete_image(__int64 a1)
{
free(*(void **)(a1 + 8));
free(*(void **)(a1 + 16));
}

original_code:
void delete_image(image_t *img)
{
    free(img->src);
    free(img->dst);
    return;
}

canonical_original_code:
void delete_image(struct _image_t *img)
{
    free(img->src);
    free(img->dst);
    return;
}

variable_types: struct _image_t { int width; int height; unsigned char *src; unsigned char *dst; } *


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v4[2]; // [rsp+0h] [rbp-28h] BYREF
__int64 v5; // [rsp+8h] [rbp-20h]
__int64 v6; // [rsp+10h] [rbp-18h]

read_ppm((__int64)v4, "in.ppm");
rgb2y(v5, v6, v4[1] * v4[0]);
write_ppm(v4, "out.ppm");
delete_image((__int64)v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v4[2]; // [rsp+0h] [rbp-28h] BYREF
__int64 v5; // [rsp+8h] [rbp-20h]
__int64 v6; // [rsp+10h] [rbp-18h]

func0((__int64)v4, "in.ppm");
rgb2y(v5, v6, v4[1] * v4[0]);
func1(v4, "out.ppm");
func2((__int64)v4);
return 0;
}

original_code:
int main(int argc, char **argv)
{
    image_t image;
    read_ppm(&image, "in.ppm");
    rgb2y(image.src, image.dst, image.width * image.height);
    write_ppm(&image, "out.ppm");
    delete_image(&image);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    struct _image_t image;
    read_ppm(&image, "in.ppm");
    rgb2y(image.src, image.dst, image.width * image.height);
    write_ppm(&image, "out.ppm");
    delete_image(&image);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct _image_t { int width; int height; unsigned char *src; unsigned char *dst; }


testBits

original_code:
void
testBits ( float f32, const char* f16_binstr, const char* f32_binstr )
{
    int len;
    int i;
    printf("f16 = ");
    len = strlen( f16_binstr );
    for (i=0;i<len;i++)
    {
        if ( f16_binstr[i] == '1' )
        {
            printf("1");
        }
        else if ( f16_binstr[i] == '0' )
        {
            printf("0");
        }
    }
    printf("\n");
    printf("f32 = ");
    len = strlen( f32_binstr );
    for (i=0;i<len;i++)
    {
        if ( f32_binstr[i] == '1' )
        {
            printf("1");
        }
        else if ( f32_binstr[i] == '0' )
        {
            printf("0");
        }
    }
    printf("\n");
    printf("print \"    test_f16 = half_convert( 0x\", f32, \" );\\n\"\n" );
    printf("print \"    CHECK_HALF( test_f16, 0x\", f16, \", 0x\", f32, \" )\\n\"\n" );
}

canonical_original_code:
void testBits(float f32, char *f16_binstr, char *f32_binstr)
{
    int len;
    int i;
    printf("f16 = ");
    len = strlen( f16_binstr );
    for (i=0;i<len;i++)
    {
        if ( f16_binstr[i] == '1' )
        {
            printf("1");
        }
        else if ( f16_binstr[i] == '0' )
        {
            printf("0");
        }
    }
    printf("\n");
    printf("f32 = ");
    len = strlen( f32_binstr );
    for (i=0;i<len;i++)
    {
        if ( f32_binstr[i] == '1' )
        {
            printf("1");
        }
        else if ( f32_binstr[i] == '0' )
        {
            printf("0");
        }
    }
    printf("\n");
    printf("print \"    test_f16 = half_convert( 0x\", f32, \" );\\n\"\n" );
    printf("print \"    CHECK_HALF( test_f16, 0x\", f16, \", 0x\", f32, \" )\\n\"\n" );
}

variable_types: len int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("obase=16");
puts("ibase=2");
puts("print \"#include <stdio.h>\\n\"");
puts("print \"#include <stdint.h>\\n\"");
puts("print \"typedef union FLOAT_32 FLOAT_32;\\n\"");
puts("print \"union FLOAT_32\\n\"");
puts("print \"{\\n\"");
puts("print \" float f32;\\n\"");
puts("print \" uint32_t u32;\\n\"");
puts("print \"};\\n\"");
puts("print \"uint16_t half_convert( uint32_t );\\n\"");
puts("print \"\\n\"");
printf(
"print \"#define CHECK_HALF(arg0,arg1,arg2) if (arg0 != arg1) { FLOAT_32 f; f.u32 = arg2; test_count++; printf(\\q[%%"
"s:%%d] FAILED: 0x%%04x != 0x%%04x (%%f)\\\\n\\q,__FILE__,__LINE__,arg0,arg1,f.f32); } else { test_count++; test_pass"
"_count++; }\\n\"\n");
puts("print \"\\n\"");
puts("print \"int main( void )\\n\"");
puts("print \"{\\n\"");
puts("print \" FLOAT_32 test;\\n\"");
puts("print \" uint16_t test_f16;\\n\"");
puts("print \" int test_count = 0;\\n\"");
puts("print \" int test_pass_count = 0;\\n\"");
puts("print \"\\n\"");
print_half_tests();
puts("print \"\\n\"");
printf("print \" printf(\\q[%%s] Passed %%d of %%d\\\\n\\q, __FILE__, test_pass_count, test_count);\\n\"\n");
puts("print \" return (0);\\n\"");
puts("print \"}\\n\"");
puts("quit");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("obase=16");
puts("ibase=2");
puts("print \"#include <stdio.h>\\n\"");
puts("print \"#include <stdint.h>\\n\"");
puts("print \"typedef union FLOAT_32 FLOAT_32;\\n\"");
puts("print \"union FLOAT_32\\n\"");
puts("print \"{\\n\"");
puts("print \" float f32;\\n\"");
puts("print \" uint32_t u32;\\n\"");
puts("print \"};\\n\"");
puts("print \"uint16_t half_convert( uint32_t );\\n\"");
puts("print \"\\n\"");
printf(
"print \"#define CHECK_HALF(arg0,arg1,arg2) if (arg0 != arg1) { FLOAT_32 f; f.u32 = arg2; test_count++; printf(\\q[%%"
"s:%%d] FAILED: 0x%%04x != 0x%%04x (%%f)\\\\n\\q,__FILE__,__LINE__,arg0,arg1,f.f32); } else { test_count++; test_pass"
"_count++; }\\n\"\n");
puts("print \"\\n\"");
puts("print \"int main( void )\\n\"");
puts("print \"{\\n\"");
puts("print \" FLOAT_32 test;\\n\"");
puts("print \" uint16_t test_f16;\\n\"");
puts("print \" int test_count = 0;\\n\"");
puts("print \" int test_pass_count = 0;\\n\"");
puts("print \"\\n\"");
func2();
puts("print \"\\n\"");
printf("print \" printf(\\q[%%s] Passed %%d of %%d\\\\n\\q, __FILE__, test_pass_count, test_count);\\n\"\n");
puts("print \" return (0);\\n\"");
puts("print \"}\\n\"");
puts("quit");
return 0;
}

original_code:
int
main( void )
{
    printf("obase=16\n");
    printf("ibase=2\n");
    printf("print \"#include <stdio.h>\\n\"\n" );
    printf("print \"#include <stdint.h>\\n\"\n" );
    printf("print \"typedef union FLOAT_32 FLOAT_32;\\n\"\n" );
    printf("print \"union FLOAT_32\\n\"\n" );
    printf("print \"{\\n\"\n" );
    printf("print \"    float    f32;\\n\"\n" );
    printf("print \"    uint32_t u32;\\n\"\n" );
    printf("print \"};\\n\"\n" );
    printf("print \"uint16_t half_convert( uint32_t );\\n\"\n" );
    printf("print \"\\n\"\n" );
    printf("print \"#define CHECK_HALF(arg0,arg1,arg2) if (arg0 != arg1) { FLOAT_32 f; f.u32 = arg2; test_count++; printf(\\q[%%s:%%d] FAILED: 0x%%04x != 0x%%04x (%%f)\\\\n\\q,__FILE__,__LINE__,arg0,arg1,f.f32); } else { test_count++; test_pass_count++; }\\n\"\n" );
    printf("print \"\\n\"\n" );
    printf("print \"int main( void )\\n\"\n" );
    printf("print \"{\\n\"\n" );
    printf("print \"    FLOAT_32 test;\\n\"\n" );
    printf("print \"    uint16_t test_f16;\\n\"\n" );
    printf("print \"    int      test_count = 0;\\n\"\n" );
    printf("print \"    int      test_pass_count = 0;\\n\"\n" );
    printf("print \"\\n\"\n" );
    print_half_tests();
    printf("print \"\\n\"\n" );
    printf("print \"    printf(\\q[%%s] Passed %%d of %%d\\\\n\\q, __FILE__, test_pass_count, test_count);\\n\"\n" );
    printf("print \"    return (0);\\n\"\n" );
    printf("print \"}\\n\"\n" );
    printf("quit\n");
    return (0);
}

canonical_original_code:
int main()
{
    printf("obase=16\n");
    printf("ibase=2\n");
    printf("print \"#include <stdio.h>\\n\"\n" );
    printf("print \"#include <stdint.h>\\n\"\n" );
    printf("print \"typedef union FLOAT_32 FLOAT_32;\\n\"\n" );
    printf("print \"union FLOAT_32\\n\"\n" );
    printf("print \"{\\n\"\n" );
    printf("print \"    float    f32;\\n\"\n" );
    printf("print \"    uint32_t u32;\\n\"\n" );
    printf("print \"};\\n\"\n" );
    printf("print \"uint16_t half_convert( uint32_t );\\n\"\n" );
    printf("print \"\\n\"\n" );
    printf("print \"#define CHECK_HALF(arg0,arg1,arg2) if (arg0 != arg1) { FLOAT_32 f; f.u32 = arg2; test_count++; printf(\\q[%%s:%%d] FAILED: 0x%%04x != 0x%%04x (%%f)\\\\n\\q,__FILE__,__LINE__,arg0,arg1,f.f32); } else { test_count++; test_pass_count++; }\\n\"\n" );
    printf("print \"\\n\"\n" );
    printf("print \"int main( void )\\n\"\n" );
    printf("print \"{\\n\"\n" );
    printf("print \"    FLOAT_32 test;\\n\"\n" );
    printf("print \"    uint16_t test_f16;\\n\"\n" );
    printf("print \"    int      test_count = 0;\\n\"\n" );
    printf("print \"    int      test_pass_count = 0;\\n\"\n" );
    printf("print \"\\n\"\n" );
    print_half_tests();
    printf("print \"\\n\"\n" );
    printf("print \"    printf(\\q[%%s] Passed %%d of %%d\\\\n\\q, __FILE__, test_pass_count, test_count);\\n\"\n" );
    printf("print \"    return (0);\\n\"\n" );
    printf("print \"}\\n\"\n" );
    printf("quit\n");
    return (0);
}


repo: mad-p/codeiq-crossing

original_code:
int count_and_add(const int n) {
  int c = 0;
  int m = n;
  while (n >= max) max <<= 1;
  while (m < max) {
    if (m >= n) {
      c += counts[m];
    }
    if (m <= n) {
      counts[m]++;
    }
    int k = m & (m - 1);
    m = k | ((m - k) << 1);
  }
  return c;
}

canonical_original_code:
int count_and_add(int n) {
  int c = 0;
  int m = n;
  while (n >= max) max <<= 1;
  while (m < max) {
    if (m >= n) {
      c += counts[m];
    }
    if (m <= n) {
      counts[m]++;
    }
    int k = m & (m - 1);
    m = k | ((m - k) << 1);
  }
  return c;
}

variable_types: n int

variable_types: c int

variable_types: m int

variable_types: k int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
__int64 v4; // rax
int v5; // eax

v3 = time(0LL);
v4 = count((char *)argv[1]);
printf("%lld,", v4);
v5 = time(0LL);
printf("%d\n", (unsigned int)(v5 - v3));
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
__int64 v4; // rax
int v5; // eax

v3 = time(0LL);
v4 = func8((char *)argv[1]);
printf("%lld,", v4);
v5 = time(0LL);
printf("%d\n", (unsigned int)(v5 - v3));
return 0;
}

original_code:
int main(int argc, char **argv) {
  time_t st = time(((void *)0));
  uint64_t c = count(argv[1]);
  printf("%lld,", c);
  time_t en = time(((void *)0));
  printf("%d\n", (int)(en - st));
  return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
  long st = time(((void *)0));
  unsigned long c = count(argv[1]);
  printf("%lld,", c);
  long en = time(((void *)0));
  printf("%d\n", (int)(en - st));
  return 0;
}

variable_types: st long

variable_types:
c
unsigned long

variable_types: en long


StackNew

repo: maddenpj/stacklib

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

*(_DWORD *)a1 = a2;
*(_DWORD *)(a1 + 4) = 0;
*(_DWORD *)(a1 + 8) = 8 * a2;
result = malloc(8 * a2);
*(_QWORD *)(a1 + 16) = result;
return result;
}

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

*(_DWORD *)a1 = a2;
*(_DWORD *)(a1 + 4) = 0;
*(_DWORD *)(a1 + 8) = 8 * a2;
result = malloc(8 * a2);
*(_QWORD *)(a1 + 16) = result;
return result;
}

original_code:
void StackNew(Stack* s, int elem_size)
{
  s->elem_size = elem_size;
  s->log_length = 0;
  s->actual_length = 8 * elem_size;
  s->buf = (char*) malloc(s->actual_length);
}

canonical_original_code:
void StackNew(struct Stack *s, int elem_size)
{
  s->elem_size = elem_size;
  s->log_length = 0;
  s->actual_length = 8 * elem_size;
  s->buf = (char *) malloc(s->actual_length);
}

variable_types: struct Stack { int elem_size; int log_length; int actual_length; char *buf; } *


StackPush

decompiled_code:
__int64 __fastcall StackPush(int *a1, const void *a2)
{
__int64 v4; // rax
size_t v5; // rdx
int v6; // esi
char *v7; // rdi
__int64 result; // rax
char *v9; // rax

v4 = a1[1];
v5 = *a1;
v6 = a1[2];
v7 = (char *)*((_QWORD *)a1 + 2);
if ( (int)v4 + (int)v5 >= v6 )
{
v9 = (char *)realloc(v7, 2 * v6);
v5 = *a1;
a1[2] *= 2;
*((_QWORD *)a1 + 2) = v9;
v7 = v9;
v4 = a1[1];
}
memcpy(&v7[v4], a2, v5);
result = (unsigned int)*a1;
a1[1] += result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int *a1, const void *a2)
{
__int64 v4; // rax
size_t v5; // rdx
int v6; // esi
char *v7; // rdi
__int64 result; // rax
char *v9; // rax

v4 = a1[1];
v5 = *a1;
v6 = a1[2];
v7 = (char *)*((_QWORD *)a1 + 2);
if ( (int)v4 + (int)v5 >= v6 )
{
v9 = (char *)realloc(v7, 2 * v6);
v5 = *a1;
a1[2] *= 2;
*((_QWORD *)a1 + 2) = v9;
v7 = v9;
v4 = a1[1];
}
memcpy(&v7[v4], a2, v5);
result = (unsigned int)*a1;
a1[1] += result;
return result;
}

original_code:
void StackPush(Stack* s, void* elem)
{
  if(s->log_length + s->elem_size >= s->actual_length) {
    s->buf = realloc(s->buf, s->actual_length * 2);
    s->actual_length *= 2;
  }
  memcpy(s->buf + s->log_length, elem, s->elem_size);
  s->log_length += s->elem_size;
}

canonical_original_code:
void StackPush(struct Stack *s, void *elem)
{
  if(s->log_length + s->elem_size >= s->actual_length) {
    s->buf = realloc(s->buf, s->actual_length * 2);
    s->actual_length *= 2;
  }
  memcpy(s->buf + s->log_length, elem, s->elem_size);
  s->log_length += s->elem_size;
}

variable_types:
elem
void *

user_defined_types: struct Stack { int elem_size; int log_length; int actual_length; char *buf; }


StackPop

decompiled_code:
void *__fastcall StackPop(int *a1, void *a2)
{
int v3; // esi
size_t v4; // rdx
int v5; // esi
void *result; // rax

v3 = a1[1];
if ( v3 )
{
v4 = *a1;
v5 = v3 - v4;
a1[1] = v5;
result = memcpy(a2, (const void *)(*((_QWORD *)a1 + 2) + v5), v4);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func2(int *a1, void *a2)
{
int v3; // esi
size_t v4; // rdx
int v5; // esi
void *result; // rax

v3 = a1[1];
if ( v3 )
{
v4 = *a1;
v5 = v3 - v4;
a1[1] = v5;
result = memcpy(a2, (const void *)(*((_QWORD *)a1 + 2) + v5), v4);
}
return result;
}

original_code:
void StackPop(Stack *s, void* dest)
{
  if(s->log_length == 0) return;
  s->log_length -= s->elem_size;
  memcpy(dest, s->buf+s->log_length, s->elem_size);
}

canonical_original_code:
void StackPop(struct Stack *s, void *dest)
{
  if(s->log_length == 0) return;
  s->log_length -= s->elem_size;
  memcpy(dest, s->buf+s->log_length, s->elem_size);
}

variable_types:
dest
void *


StackFree

decompiled_code:
void __fastcall StackFree(__int64 a1)
{
free(*(void **)(a1 + 16));
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1)
{
free(*(void **)(a1 + 16));
}

original_code:
void StackFree(Stack* s)
{
  free(s->buf);
}

canonical_original_code:
void StackFree(struct Stack *s)
{
  free(s->buf);
}


main

repo: mafintosh/tag

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbx
const char *v4; // rbp
size_t v5; // rax
char v6; // r12
__int64 v7; // rbx
int v8; // er15
int v9; // ebp
int v11; // [rsp+8h] [rbp-440h]
char buf[1080]; // [rsp+10h] [rbp-438h] BYREF

if ( argc <= 1 )
error(
"\n"
" Usage: tag [tags]\n"
" - tag all lines piped to stdin with a message\n"
"\n"
" Examples:\n"
" tag hello-world # -> hello-world\n"
" tag %h [%d] # -> hostname [YYYY-MM-DD HH:mm:ss UTC]\n"
" tag %D # -> YYYY-MM-DD HH:mm:ss in local time\n"
" tag %l test # -> line-number test\n"
"\n");
v3 = argv + 1;
do
{
v4 = *v3;
v5 = strlen(*v3);
if ( (int)compile_format(v5, v4) < 0 )
error("Invalid format\n");
++v3;
}
while ( v3 != &argv[(unsigned int)(argc - 2) + 2] );
v6 = 10;
while ( 1 )
{
v11 = read(0, buf, 0x400uLL);
if ( v11 <= 0 )
return 0;
v7 = 0LL;
v8 = 0;
do
{
while ( 1 )
{
v9 = v7;
if ( v6 == 10 )
{
apply_format();
if ( write(1, &stamp, stamp_len) < 0 )
goto LABEL_12;
}
v6 = buf[v7];
if ( v6 == 10 )
break;
++v7;
if ( v9 + 1 >= v11 )
goto LABEL_15;
}
if ( write(1, &buf[v8], (int)v7 - v8 + 1) < 0 )
goto LABEL_12;
v8 = v7 + 1;
v6 = buf[v7++];
}
while ( v9 + 1 < v11 );
LABEL_15:
if ( v8 < v11 && write(1, &buf[v8], v11 - v8) < 0 )
LABEL_12:
exit(1);
}
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbx
const char *v4; // rbp
size_t v5; // rax
char v6; // r12
__int64 v7; // rbx
int v8; // er15
int v9; // ebp
int v11; // [rsp+8h] [rbp-440h]
char buf[1080]; // [rsp+10h] [rbp-438h] BYREF

if ( argc <= 1 )
func13(
"\n"
" Usage: tag [tags]\n"
" - tag all lines piped to stdin with a message\n"
"\n"
" Examples:\n"
" tag hello-world # -> hello-world\n"
" tag %h [%d] # -> hostname [YYYY-MM-DD HH:mm:ss UTC]\n"
" tag %D # -> YYYY-MM-DD HH:mm:ss in local time\n"
" tag %l test # -> line-number test\n"
"\n");
v3 = argv + 1;
do
{
v4 = *v3;
v5 = strlen(*v3);
if ( (int)func12(v5, v4) < 0 )
func13("Invalid format\n");
++v3;
}
while ( v3 != &argv[(unsigned int)(argc - 2) + 2] );
v6 = 10;
while ( 1 )
{
v11 = read(0, buf, 0x400uLL);
if ( v11 <= 0 )
return 0;
v7 = 0LL;
v8 = 0;
do
{
while ( 1 )
{
v9 = v7;
if ( v6 == 10 )
{
func11();
if ( write(1, &stamp, stamp_len) < 0 )
goto LABEL_12;
}
v6 = buf[v7];
if ( v6 == 10 )
break;
++v7;
if ( v9 + 1 >= v11 )
goto LABEL_15;
}
if ( write(1, &buf[v8], (int)v7 - v8 + 1) < 0 )
goto LABEL_12;
v8 = v7 + 1;
v6 = buf[v7++];
}
while ( v9 + 1 < v11 );
LABEL_15:
if ( v8 < v11 && write(1, &buf[v8], v11 - v8) < 0 )
LABEL_12:
exit(1);
}
}

original_code:
int
main(int argc, char *argv[]) {
 if (argc < 2) {
  error(
   "\n"
   "  Usage: tag [tags]\n"
   "  - tag all lines piped to stdin with a message\n"
   "\n"
   "  Examples:\n"
   "    tag hello-world  # -> hello-world\n"
   "    tag %h [%d]      # -> hostname [YYYY-MM-DD HH:mm:ss UTC]\n"
   "    tag %D           # -> YYYY-MM-DD HH:mm:ss in local time\n"
   "    tag %l test      # -> line-number test\n"
   "\n"
  );
 }
 for (int i = 1; i < argc; i++) {
  if (compile_format(strlen(argv[i]), argv[i]) < 0) error("Invalid format\n");
 }
 char prev = '\n';
 char buf[1024];
 int len;
 while ((len = read(0, &buf, 1024)) > 0) {
  int offset = 0;
  for (int i = 0; i < len; i++) {
   if (prev == '\n') {
    apply_format();
    if (write(1, stamp, stamp_len) < 0) exit(1);
   }
   if (buf[i] == '\n') {
    if (write(1, buf+offset, i-offset+1) < 0) exit(1);
    offset = i+1;
   }
   prev = buf[i];
  }
  if (offset < len) {
   if (write(1, buf+offset, len-offset) < 0) exit(1);
  }
 }
 return 0;
}

variable_types:
argv
char *[]

variable_types: i int

variable_types: prev char

variable_types:
buf
char [1024]

variable_types: offset int