popData

decompiled_code:
__int64 __fastcall popData(char **a1)
{
__int64 result; // rax
void *v2; // rdi

result = 0LL;
if ( *current != *start )
{
f_elock(fd);
if ( a1 )
strcpy(*a1, (const char *)queue + (unsigned __int16)*start);
v2 = queue;
*start = ((unsigned __int16)*start + 64) % (qsize << 6);
msync(v2, msize, 4);
f_unlock(fd);
result = abs32((unsigned __int16)*current - (unsigned __int16)*start);
}
return result;
}

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

result = 0LL;
if ( *current != *start )
{
f_elock(fd);
if ( a1 )
strcpy(*a1, (const char *)queue + (unsigned __int16)*start);
v2 = queue;
*start = ((unsigned __int16)*start + 64) % (qsize << 6);
msync(v2, msize, 4);
f_unlock(fd);
result = abs32((unsigned __int16)*current - (unsigned __int16)*start);
}
return result;
}

original_code:
int popData(char** result) {
 if (abs(*current - *start) == 0) {
  return 0;
 }
 f_elock(fd);
 if (result != ((void *)0)) {
  sprintf(*result, "%s", queue + *start);
 }
 *start = (*start + 64) % (qsize * 64);
 msync(queue, msize, 4);
 f_unlock(fd);
 return abs(*current - *start);
}

canonical_original_code:
int popData(char **result) {
 if (abs(*current - *start) == 0) {
  return 0;
 }
 f_elock(fd);
 if (result != ((void *)0)) {
  sprintf(*result, "%s", queue + *start);
 }
 *start = (*start + 64) % (qsize * 64);
 msync(queue, msize, 4);
 f_unlock(fd);
 return abs(*current - *start);
}

variable_types:
result
char **


pickData

repo: pierinz/citofonoweb

decompiled_code:
__int64 __fastcall pickData(char **a1)
{
if ( *current == *start )
return 0LL;
f_elock(fd);
strcpy(*a1, (const char *)queue + (unsigned __int16)*start);
f_unlock(fd);
return abs32((unsigned __int16)*current - (unsigned __int16)*start);
}

canonical_decompiled_code:
__int64 __fastcall func2(char **a1)
{
if ( *current == *start )
return 0LL;
f_elock(fd);
strcpy(*a1, (const char *)queue + (unsigned __int16)*start);
f_unlock(fd);
return abs32((unsigned __int16)*current - (unsigned __int16)*start);
}

original_code:
int pickData(char** result) {
 if (abs(*current - *start) == 0) {
  return 0;
 }
 f_elock(fd);
 sprintf(*result, "%s", queue + *start);
 f_unlock(fd);
 return abs(*current - *start);
}

canonical_original_code:
int pickData(char **result) {
 if (abs(*current - *start) == 0) {
  return 0;
 }
 f_elock(fd);
 sprintf(*result, "%s", queue + *start);
 f_unlock(fd);
 return abs(*current - *start);
}

return_type: int


logmessage

decompiled_code:
int __fastcall logmessage(char *s)
{
struct tm *v1; // rax
time_t timer; // [rsp+8h] [rbp-30h] BYREF
char sa[40]; // [rsp+10h] [rbp-28h] BYREF

time(&timer);
v1 = localtime(&timer);
strftime(sa, 0x16uLL, "%d/%m/%y %H:%M:%S - ", v1);
pthread_mutex_lock(&mutex);
fputs(sa, flog);
fputs(s, flog);
fputc(10, flog);
fflush(flog);
return pthread_mutex_unlock(&mutex);
}

canonical_decompiled_code:
int __fastcall func1(char *s)
{
struct tm *v1; // rax
time_t timer; // [rsp+8h] [rbp-30h] BYREF
char sa[40]; // [rsp+10h] [rbp-28h] BYREF

time(&timer);
v1 = localtime(&timer);
strftime(sa, 0x16uLL, "%d/%m/%y %H:%M:%S - ", v1);
pthread_mutex_lock(&mutex);
fputs(sa, flog);
fputs(s, flog);
fputc(10, flog);
fflush(flog);
return pthread_mutex_unlock(&mutex);
}

original_code:
void logmessage(char *message) {
 char buffer[22];
 time_t rawtime;
 struct tm * timeinfo;
 time(&rawtime);
 timeinfo = localtime(&rawtime);
 strftime(buffer, 22, "%d/%m/%y %H:%M:%S - ", timeinfo);
 pthread_mutex_lock(&mutex);
 fputs(buffer, flog);
 fputs(message, flog);
 fputs("\n", flog);
 fflush(flog);
 pthread_mutex_unlock(&mutex);
}

canonical_original_code:
void logmessage(char *message) {
 char buffer[22];
 long rawtime;
 struct tm *timeinfo;
 time(&rawtime);
 timeinfo = localtime(&rawtime);
 strftime(buffer, 22, "%d/%m/%y %H:%M:%S - ", timeinfo);
 pthread_mutex_lock(&mutex);
 fputs(buffer, flog);
 fputs(message, flog);
 fputs("\n", flog);
 fflush(flog);
 pthread_mutex_unlock(&mutex);
}

variable_types:
message
char *

variable_types:
buffer
char [22]

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

return_type: void


rotate

decompiled_code:
int rotate()
{
pthread_mutex_lock(&mutex);
fclose(flog);
while ( !access(logfile, 0) )
usleep(0x4E20u);
flog = fopen(logfile, "a");
pthread_mutex_unlock(&mutex);
fwrite("Logfile rotated.", 1uLL, 0x10uLL, stderr);
return logmessage("Logfile rotated.");
}

canonical_decompiled_code:
int func4()
{
pthread_mutex_lock(&mutex);
fclose(flog);
while ( !access(logfile, 0) )
usleep(0x4E20u);
flog = fopen(logfile, "a");
pthread_mutex_unlock(&mutex);
fwrite("Logfile rotated.", 1uLL, 0x10uLL, stderr);
return func1("Logfile rotated.");
}

original_code:
void rotate() {
 pthread_mutex_lock(&mutex);
 fclose(flog);
 while (access(logfile, 0) == 0) {
  usleep(20000);
 }
 flog = fopen(logfile, "a");
 pthread_mutex_unlock(&mutex);
 fprintf(stderr, "Logfile rotated.");
 logmessage("Logfile rotated.");
}


clean

decompiled_code:
void clean()
{
if ( flog )
fclose(flog);
free(logfile);
free(source);
free(helper);
}

canonical_decompiled_code:
void func5()
{
if ( flog )
fclose(flog);
free(logfile);
free(source);
free(helper);
}

original_code:
void clean() {
 if (flog)
  fclose(flog);
 free(logfile);
 free(source);
 free(helper);
}


fatal

decompiled_code:
void __fastcall __noreturn fatal(char *s)
{
fprintf(stderr, "%s\n", s);
logmessage(s);
fwrite("Fatal error - program terminated.\n", 1uLL, 0x22uLL, stderr);
logmessage("Fatal error - program terminated.");
clean();
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(char *s)
{
fprintf(stderr, "%s\n", s);
func1(s);
fwrite("Fatal error - program terminated.\n", 1uLL, 0x22uLL, stderr);
func1("Fatal error - program terminated.");
func5();
exit(1);
}

original_code:
void fatal(char* message) {
 fprintf(stderr, "%s\n", message);
 logmessage(message);
 fprintf(stderr, "Fatal error - program terminated.\n");
 logmessage("Fatal error - program terminated.");
 clean();
 exit(1);
}

canonical_original_code:
void fatal(char *message) {
 fprintf(stderr, "%s\n", message);
 logmessage(message);
 fprintf(stderr, "Fatal error - program terminated.\n");
 logmessage("Fatal error - program terminated.");
 clean();
 exit(1);
}


GPIOValid

decompiled_code:
__int64 GPIOValid()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func11()
{
return 1LL;
}

original_code:
int GPIOValid(int pin) {
 return 1;
}

canonical_original_code:
int GPIOValid(int pin) {
 return 1;
}

variable_types: pin int


GPIOExport

decompiled_code:
__int64 __fastcall GPIOExport(unsigned int a1)
{
unsigned int v1; // er13
int v3; // eax
char v4[72]; // [rsp+0h] [rbp-48h] BYREF

v1 = 0;
snprintf(v4, 0x23uLL, "/sys/class/gpio/gpio%d/", a1);
if ( access(v4, 1) != -1 )
return v1;
v1 = open("/sys/class/gpio/export", 1);
if ( v1 == -1 )
{
fwrite("Failed to open export for writing!\n", 1uLL, 0x23uLL, stderr);
return v1;
}
v3 = snprintf(v4, 3uLL, "%d", a1);
if ( write(v1, v4, v3) == -1 )
{
v1 = -1;
fwrite("Failed to export gpio!\n", 1uLL, 0x17uLL, stderr);
return v1;
}
close(v1);
usleep(0x30D40u);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned int a1)
{
unsigned int v1; // er13
int v3; // eax
char v4[72]; // [rsp+0h] [rbp-48h] BYREF

v1 = 0;
snprintf(v4, 0x23uLL, "/sys/class/gpio/gpio%d/", a1);
if ( access(v4, 1) != -1 )
return v1;
v1 = open("/sys/class/gpio/export", 1);
if ( v1 == -1 )
{
fwrite("Failed to open export for writing!\n", 1uLL, 0x23uLL, stderr);
return v1;
}
v3 = snprintf(v4, 3uLL, "%d", a1);
if ( write(v1, v4, v3) == -1 )
{
v1 = -1;
fwrite("Failed to export gpio!\n", 1uLL, 0x17uLL, stderr);
return v1;
}
close(v1);
usleep(0x30D40u);
return 0LL;
}

original_code:
int GPIOExport(int pin) {
 char buffer[35];
 ssize_t bytes_written;
 int fd;
 snprintf(buffer, 35, "/sys/class/gpio/gpio%d/", pin);
 if (access(buffer, 1) != -1) {
  return (0);
 }
 fd = open("/sys/class/gpio/export", 01);
 if (-1 == fd) {
  fprintf(stderr, "Failed to open export for writing!\n");
  return (-1);
 }
 bytes_written = snprintf(buffer, 3, "%d", pin);
 if (-1 == write(fd, buffer, bytes_written)) {
  fprintf(stderr, "Failed to export gpio!\n");
  return (-1);
 }
 close(fd);
 usleep(200000);
 return (0);
}

canonical_original_code:
int GPIOExport(int pin) {
 char buffer[35];
 long bytes_written;
 int fd;
 snprintf(buffer, 35, "/sys/class/gpio/gpio%d/", pin);
 if (access(buffer, 1) != -1) {
  return (0);
 }
 fd = open("/sys/class/gpio/export", 01);
 if (-1 == fd) {
  fprintf(stderr, "Failed to open export for writing!\n");
  return (-1);
 }
 bytes_written = snprintf(buffer, 3, "%d", pin);
 if (-1 == write(fd, buffer, bytes_written)) {
  fprintf(stderr, "Failed to export gpio!\n");
  return (-1);
 }
 close(fd);
 usleep(200000);
 return (0);
}

variable_types:
buffer
char [35]

variable_types: fd int


GPIOUnexport

decompiled_code:
__int64 __fastcall GPIOUnexport(unsigned int a1)
{
unsigned int v1; // er12
int v2; // eax
int v3; // edi
char s[27]; // [rsp+Dh] [rbp-1Bh] BYREF

v1 = open("/sys/class/gpio/unexport", 1);
if ( v1 == -1 )
{
fwrite("Failed to open unexport for writing!\n", 1uLL, 0x25uLL, stderr);
}
else
{
v2 = snprintf(s, 3uLL, "%d", a1);
write(v1, s, v2);
v3 = v1;
v1 = 0;
close(v3);
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned int a1)
{
unsigned int v1; // er12
int v2; // eax
int v3; // edi
char s[27]; // [rsp+Dh] [rbp-1Bh] BYREF

v1 = open("/sys/class/gpio/unexport", 1);
if ( v1 == -1 )
{
fwrite("Failed to open unexport for writing!\n", 1uLL, 0x25uLL, stderr);
}
else
{
v2 = snprintf(s, 3uLL, "%d", a1);
write(v1, s, v2);
v3 = v1;
v1 = 0;
close(v3);
}
return v1;
}

original_code:
int GPIOUnexport(int pin) {
 char buffer[3];
 ssize_t bytes_written;
 int fd;
 fd = open("/sys/class/gpio/unexport", 01);
 if (-1 == fd) {
  fprintf(stderr, "Failed to open unexport for writing!\n");
  return (-1);
 }
 bytes_written = snprintf(buffer, 3, "%d", pin);
 write(fd, buffer, bytes_written);
 close(fd);
 return (0);
}

canonical_original_code:
int GPIOUnexport(int pin) {
 char buffer[3];
 long bytes_written;
 int fd;
 fd = open("/sys/class/gpio/unexport", 01);
 if (-1 == fd) {
  fprintf(stderr, "Failed to open unexport for writing!\n");
  return (-1);
 }
 bytes_written = snprintf(buffer, 3, "%d", pin);
 write(fd, buffer, bytes_written);
 close(fd);
 return (0);
}

variable_types:
buffer
char [3]


GPIODirection

decompiled_code:
__int64 __fastcall GPIODirection(unsigned int a1, int a2)
{
unsigned int v2; // er12
const char *v3; // r13
int v4; // edi
char v6[72]; // [rsp+0h] [rbp-48h] BYREF

snprintf(v6, 0x23uLL, "/sys/class/gpio/gpio%d/direction", a1);
v2 = open(v6, 1);
if ( v2 == -1 )
{
fprintf(stderr, "Failed to open gpio direction %s for writing!\n", v6);
}
else
{
v3 = "out";
if ( !a2 )
v3 = "in";
if ( write(v2, v3, 3LL - (a2 == 0)) == -1 )
{
v2 = -1;
fprintf(stderr, "Failed to set direction %s on %s!\n", v3, v6);
}
else
{
v4 = v2;
v2 = 0;
close(v4);
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned int a1, int a2)
{
unsigned int v2; // er12
const char *v3; // r13
int v4; // edi
char v6[72]; // [rsp+0h] [rbp-48h] BYREF

snprintf(v6, 0x23uLL, "/sys/class/gpio/gpio%d/direction", a1);
v2 = open(v6, 1);
if ( v2 == -1 )
{
fprintf(stderr, "Failed to open gpio direction %s for writing!\n", v6);
}
else
{
v3 = "out";
if ( !a2 )
v3 = "in";
if ( write(v2, v3, 3LL - (a2 == 0)) == -1 )
{
v2 = -1;
fprintf(stderr, "Failed to set direction %s on %s!\n", v3, v6);
}
else
{
v4 = v2;
v2 = 0;
close(v4);
}
}
return v2;
}

original_code:
int GPIODirection(int pin, int dir) {
 static const char s_directions_str[] = "in\0out";
 char path[35];
 int fd;
 snprintf(path, 35, "/sys/class/gpio/gpio%d/direction", pin);
 fd = open(path, 01);
 if (-1 == fd) {
  fprintf(stderr, "Failed to open gpio direction %s for writing!\n", path);
  return (-1);
 }
 if (-1 == write(fd, &s_directions_str[0 == dir ? 0 : 3], 0 == dir ? 2 : 3)) {
  fprintf(stderr, "Failed to set direction %s on %s!\n", &s_directions_str[0 == dir ? 0 : 3], path);
  return (-1);
 }
 close(fd);
 return (0);
}

canonical_original_code:
int GPIODirection(int pin, int dir) {
 static char s_directions_str[] = "in\0out";
 char path[35];
 int fd;
 snprintf(path, 35, "/sys/class/gpio/gpio%d/direction", pin);
 fd = open(path, 01);
 if (-1 == fd) {
  fprintf(stderr, "Failed to open gpio direction %s for writing!\n", path);
  return (-1);
 }
 if (-1 == write(fd, &s_directions_str[0 == dir ? 0 : 3], 0 == dir ? 2 : 3)) {
  fprintf(stderr, "Failed to set direction %s on %s!\n", &s_directions_str[0 == dir ? 0 : 3], path);
  return (-1);
 }
 close(fd);
 return (0);
}

variable_types: dir int

variable_types:
path
char [35]


GPIORead

decompiled_code:
__int64 __fastcall GPIORead(unsigned int a1)
{
int v1; // eax
unsigned int v2; // er12
char buf[3]; // [rsp+Dh] [rbp-2Bh] BYREF
char s[40]; // [rsp+10h] [rbp-28h] BYREF

snprintf(s, 0x1EuLL, "/sys/class/gpio/gpio%d/value", a1);
v1 = open(s, 0);
v2 = v1;
if ( v1 == -1 )
{
fwrite("Failed to open gpio value for reading!\n", 1uLL, 0x27uLL, stderr);
}
else if ( read(v1, buf, 3uLL) == -1 )
{
v2 = -1;
fwrite("Failed to read value!\n", 1uLL, 0x16uLL, stderr);
}
else
{
close(v2);
v2 = atoi(buf);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func15(unsigned int a1)
{
int v1; // eax
unsigned int v2; // er12
char buf[3]; // [rsp+Dh] [rbp-2Bh] BYREF
char s[40]; // [rsp+10h] [rbp-28h] BYREF

snprintf(s, 0x1EuLL, "/sys/class/gpio/gpio%d/value", a1);
v1 = open(s, 0);
v2 = v1;
if ( v1 == -1 )
{
fwrite("Failed to open gpio value for reading!\n", 1uLL, 0x27uLL, stderr);
}
else if ( read(v1, buf, 3uLL) == -1 )
{
v2 = -1;
fwrite("Failed to read value!\n", 1uLL, 0x16uLL, stderr);
}
else
{
close(v2);
v2 = atoi(buf);
}
return v2;
}

original_code:
int GPIORead(int pin) {
 char path[30];
 char value_str[3];
 int fd;
 snprintf(path, 30, "/sys/class/gpio/gpio%d/value", pin);
 fd = open(path, 00);
 if (-1 == fd) {
  fprintf(stderr, "Failed to open gpio value for reading!\n");
  return (-1);
 }
 if (-1 == read(fd, value_str, 3)) {
  fprintf(stderr, "Failed to read value!\n");
  return (-1);
 }
 close(fd);
 return (atoi(value_str));
}

variable_types:
path
char [30]


GPIOWrite

decompiled_code:
__int64 __fastcall GPIOWrite(unsigned int a1, int a2)
{
int v2; // eax
unsigned int v3; // er12
int v4; // edi
char v6[56]; // [rsp+0h] [rbp-38h] BYREF

snprintf(v6, 0x1EuLL, "/sys/class/gpio/gpio%d/value", a1);
v2 = open(v6, 1);
v3 = v2;
if ( v2 == -1 )
{
fprintf(stderr, "Failed to open gpio value %s for writing!\n", v6);
perror("open");
}
else if ( write(v2, &s_values_str_0[-(a2 == 0) + 1], 1uLL) == 1 )
{
v4 = v3;
v3 = 0;
close(v4);
}
else
{
v3 = -1;
fprintf(stderr, "Failed to write value on %s!\n", v6);
perror("write");
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func16(unsigned int a1, int a2)
{
int v2; // eax
unsigned int v3; // er12
int v4; // edi
char v6[56]; // [rsp+0h] [rbp-38h] BYREF

snprintf(v6, 0x1EuLL, "/sys/class/gpio/gpio%d/value", a1);
v2 = open(v6, 1);
v3 = v2;
if ( v2 == -1 )
{
fprintf(stderr, "Failed to open gpio value %s for writing!\n", v6);
perror("open");
}
else if ( write(v2, &s_values_str_0[-(a2 == 0) + 1], 1uLL) == 1 )
{
v4 = v3;
v3 = 0;
close(v4);
}
else
{
v3 = -1;
fprintf(stderr, "Failed to write value on %s!\n", v6);
perror("write");
}
return v3;
}

original_code:
int GPIOWrite(int pin, int value) {
 static const char s_values_str[] = "01";
 char path[30];
 int fd;
 snprintf(path, 30, "/sys/class/gpio/gpio%d/value", pin);
 fd = open(path, 01);
 if (-1 == fd) {
  fprintf(stderr, "Failed to open gpio value %s for writing!\n", path);
  perror("open");
  return (-1);
 }
 if (1 != write(fd, &s_values_str[0 == value ? 0 : 1], 1)) {
  fprintf(stderr, "Failed to write value on %s!\n", path);
  perror("write");
  return (-1);
 }
 close(fd);
 return (0);
}

canonical_original_code:
int GPIOWrite(int pin, int value) {
 static char s_values_str[] = "01";
 char path[30];
 int fd;
 snprintf(path, 30, "/sys/class/gpio/gpio%d/value", pin);
 fd = open(path, 01);
 if (-1 == fd) {
  fprintf(stderr, "Failed to open gpio value %s for writing!\n", path);
  perror("open");
  return (-1);
 }
 if (1 != write(fd, &s_values_str[0 == value ? 0 : 1], 1)) {
  fprintf(stderr, "Failed to write value on %s!\n", path);
  perror("write");
  return (-1);
 }
 close(fd);
 return (0);
}

variable_types: value int


decompiled_code:
int __fastcall set_interface_attribs(int fd, speed_t speed, int a3)
{
tcflag_t v4; // edx
int result; // eax
int *v6; // rax
int *v7; // rax
struct termios v8; // [rsp+0h] [rbp-58h] BYREF

v8.c_ospeed = 0;
*(_QWORD *)&v8.c_cc[31] = 0LL;
*(_OWORD *)&v8.c_iflag = 0LL;
*(_OWORD *)&v8.c_line = 0LL;
*(_OWORD *)&v8.c_cc[15] = 0LL;
if ( tcgetattr(fd, &v8) )
{
v6 = __errno_location();
printf("error %d from tcgetattr\n", (unsigned int)*v6);
result = -1;
}
else
{
cfsetospeed(&v8, speed);
cfsetispeed(&v8, speed);
v4 = v8.c_cflag;
*(_QWORD *)&v8.c_iflag = v8.c_iflag & 0xFFFFE3FE;
BYTE1(v4) = BYTE1(v8.c_cflag) & 0xFC;
*(_WORD *)&v8.c_cc[5] = 5;
*(_QWORD *)&v8.c_cflag = (a3 | v4) & 0x7FFFFFBF | 0x8B0;
result = tcsetattr(fd, 0, &v8);
if ( result )
{
v7 = __errno_location();
printf("error %d from tcsetattr\n", (unsigned int)*v7);
result = -1;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(int fd, speed_t speed, int a3)
{
tcflag_t v4; // edx
int result; // eax
int *v6; // rax
int *v7; // rax
struct termios v8; // [rsp+0h] [rbp-58h] BYREF

v8.c_ospeed = 0;
*(_QWORD *)&v8.c_cc[31] = 0LL;
*(_OWORD *)&v8.c_iflag = 0LL;
*(_OWORD *)&v8.c_line = 0LL;
*(_OWORD *)&v8.c_cc[15] = 0LL;
if ( tcgetattr(fd, &v8) )
{
v6 = __errno_location();
printf("error %d from tcgetattr\n", (unsigned int)*v6);
result = -1;
}
else
{
cfsetospeed(&v8, speed);
cfsetispeed(&v8, speed);
v4 = v8.c_cflag;
*(_QWORD *)&v8.c_iflag = v8.c_iflag & 0xFFFFE3FE;
BYTE1(v4) = BYTE1(v8.c_cflag) & 0xFC;
*(_WORD *)&v8.c_cc[5] = 5;
*(_QWORD *)&v8.c_cflag = (a3 | v4) & 0x7FFFFFBF | 0x8B0;
result = tcsetattr(fd, 0, &v8);
if ( result )
{
v7 = __errno_location();
printf("error %d from tcsetattr\n", (unsigned int)*v7);
result = -1;
}
}
return result;
}

original_code:
int set_interface_attribs(int fd, int speed, int parity) {
 struct termios tty;
 memset(&tty, 0, sizeof tty);
 if (tcgetattr(fd, &tty) != 0) {
  printf("error %d from tcgetattr\n", (*__errno_location ()));
  return -1;
 }
 cfsetospeed(&tty, speed);
 cfsetispeed(&tty, speed);
 tty.c_cflag = (tty.c_cflag & ~0000060) | 0000060;
 tty.c_iflag &= ~0000001;
 tty.c_lflag = 0;
 tty.c_oflag = 0;
 tty.c_cc[6] = 0;
 tty.c_cc[5] = 5;
 tty.c_iflag &= ~(0002000 | 0010000 | 0004000);
 tty.c_cflag |= (0004000 | 0000200);
 tty.c_cflag &= ~(0000400 | 0001000);
 tty.c_cflag |= parity;
 tty.c_cflag &= ~0000100;
 tty.c_cflag &= ~020000000000;
 if (tcsetattr(fd, 0, &tty) != 0) {
  printf("error %d from tcsetattr\n", (*__errno_location ()));
  return -1;
 }
 return 0;
}

variable_types: speed int

variable_types: parity int

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


decompiled_code:
int __fastcall set_blocking(int fd, int a2)
{
int result; // eax
int *v3; // rax
int *v4; // rax
struct termios v5; // [rsp+0h] [rbp-58h] BYREF

v5.c_ospeed = 0;
*(_QWORD *)&v5.c_cc[31] = 0LL;
*(_OWORD *)&v5.c_iflag = 0LL;
*(_OWORD *)&v5.c_line = 0LL;
*(_OWORD *)&v5.c_cc[15] = 0LL;
if ( tcgetattr(fd, &v5) )
{
v4 = __errno_location();
result = printf("error %d from tggetattr\n", (unsigned int)*v4);
}
else
{
v5.c_cc[5] = 5;
v5.c_cc[6] = a2 != 0;
result = tcsetattr(fd, 0, &v5);
if ( result )
{
v3 = __errno_location();
result = printf("error %d setting term attributes\n", (unsigned int)*v3);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(int fd, int a2)
{
int result; // eax
int *v3; // rax
int *v4; // rax
struct termios v5; // [rsp+0h] [rbp-58h] BYREF

v5.c_ospeed = 0;
*(_QWORD *)&v5.c_cc[31] = 0LL;
*(_OWORD *)&v5.c_iflag = 0LL;
*(_OWORD *)&v5.c_line = 0LL;
*(_OWORD *)&v5.c_cc[15] = 0LL;
if ( tcgetattr(fd, &v5) )
{
v4 = __errno_location();
result = printf("error %d from tggetattr\n", (unsigned int)*v4);
}
else
{
v5.c_cc[5] = 5;
v5.c_cc[6] = a2 != 0;
result = tcsetattr(fd, 0, &v5);
if ( result )
{
v3 = __errno_location();
result = printf("error %d setting term attributes\n", (unsigned int)*v3);
}
}
return result;
}

original_code:
void set_blocking(int fd, int should_block) {
 struct termios tty;
 memset(&tty, 0, sizeof tty);
 if (tcgetattr(fd, &tty) != 0) {
  printf("error %d from tggetattr\n", (*__errno_location ()));
  return;
 }
 tty.c_cc[6] = should_block ? 1 : 0;
 tty.c_cc[5] = 5;
 if (tcsetattr(fd, 0, &tty) != 0)
  printf("error %d setting term attributes\n", (*__errno_location ()));
}

user_defined_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


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

result = (unsigned int)(a1 - 2);
if ( (unsigned int)result <= 1 || a1 == 15 )
loop = 0;
return result;
}

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

result = (unsigned int)(a1 - 2);
if ( (unsigned int)result <= 1 || a1 == 15 )
loop = 0;
return result;
}

original_code:
void signal_handler(int signum) {
 if ((signum == 15) || (signum == 2) || (signum == 3)) {
  loop = 0;
 }
}

variable_types: signum int


prepareDev

decompiled_code:
__int64 __fastcall prepareDev(const char *a1)
{
int v1; // eax
unsigned int v2; // er12
__int64 result; // rax
__int64 v4[2]; // [rsp+0h] [rbp-118h] BYREF
__int128 v5; // [rsp+10h] [rbp-108h]
__int128 v6; // [rsp+20h] [rbp-F8h]
__int128 v7; // [rsp+30h] [rbp-E8h]
__int128 v8; // [rsp+40h] [rbp-D8h]
__int128 v9; // [rsp+50h] [rbp-C8h]
__int128 v10; // [rsp+60h] [rbp-B8h]
__int128 v11; // [rsp+70h] [rbp-A8h]
__int128 v12; // [rsp+80h] [rbp-98h]
__int128 v13; // [rsp+90h] [rbp-88h]
__int128 v14; // [rsp+A0h] [rbp-78h]
__int128 v15; // [rsp+B0h] [rbp-68h]
__int128 v16; // [rsp+C0h] [rbp-58h]
__int128 v17; // [rsp+D0h] [rbp-48h]
__int128 v18; // [rsp+E0h] [rbp-38h]
__int128 v19; // [rsp+F0h] [rbp-28h]

v4[0] = 0x6E776F6E6B6E55LL;
v4[1] = 0LL;
v5 = 0LL;
v6 = 0LL;
v7 = 0LL;
v8 = 0LL;
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
v12 = 0LL;
v13 = 0LL;
v14 = 0LL;
v15 = 0LL;
v16 = 0LL;
v17 = 0LL;
v18 = 0LL;
v19 = 0LL;
v1 = open(a1, 0);
if ( v1 == -1 )
{
fprintf(stderr, "%s is not a valid device.\n", a1);
perror("error: ");
if ( retry < 0 || retry >= max_retry )
exit(1);
++retry;
fwrite("Trying to recover...\n", 1uLL, 0x15uLL, stderr);
usleep(timeout);
result = (unsigned int)prepareDev(a1);
}
else
{
v2 = v1;
if ( verbose > 0 )
{
ioctl(v1, 0x81004506uLL, v4);
fprintf(stderr, "Reading From : %s (%s)\n", a1, (const char *)v4);
}
ioctl(v2, 0x40044590uLL, 1LL);
if ( retry >= 0 )
{
retry = 0;
fwrite("Crash counter set to 0.\n", 1uLL, 0x18uLL, stderr);
}
result = v2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1)
{
int v1; // eax
unsigned int v2; // er12
__int64 result; // rax
__int64 v4[2]; // [rsp+0h] [rbp-118h] BYREF
__int128 v5; // [rsp+10h] [rbp-108h]
__int128 v6; // [rsp+20h] [rbp-F8h]
__int128 v7; // [rsp+30h] [rbp-E8h]
__int128 v8; // [rsp+40h] [rbp-D8h]
__int128 v9; // [rsp+50h] [rbp-C8h]
__int128 v10; // [rsp+60h] [rbp-B8h]
__int128 v11; // [rsp+70h] [rbp-A8h]
__int128 v12; // [rsp+80h] [rbp-98h]
__int128 v13; // [rsp+90h] [rbp-88h]
__int128 v14; // [rsp+A0h] [rbp-78h]
__int128 v15; // [rsp+B0h] [rbp-68h]
__int128 v16; // [rsp+C0h] [rbp-58h]
__int128 v17; // [rsp+D0h] [rbp-48h]
__int128 v18; // [rsp+E0h] [rbp-38h]
__int128 v19; // [rsp+F0h] [rbp-28h]

v4[0] = 0x6E776F6E6B6E55LL;
v4[1] = 0LL;
v5 = 0LL;
v6 = 0LL;
v7 = 0LL;
v8 = 0LL;
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
v12 = 0LL;
v13 = 0LL;
v14 = 0LL;
v15 = 0LL;
v16 = 0LL;
v17 = 0LL;
v18 = 0LL;
v19 = 0LL;
v1 = open(a1, 0);
if ( v1 == -1 )
{
fprintf(stderr, "%s is not a valid device.\n", a1);
perror("error: ");
if ( retry < 0 || retry >= max_retry )
exit(1);
++retry;
fwrite("Trying to recover...\n", 1uLL, 0x15uLL, stderr);
usleep(timeout);
result = (unsigned int)func7(a1);
}
else
{
v2 = v1;
if ( verbose > 0 )
{
ioctl(v1, 0x81004506uLL, v4);
fprintf(stderr, "Reading From : %s (%s)\n", a1, (const char *)v4);
}
ioctl(v2, 0x40044590uLL, 1LL);
if ( retry >= 0 )
{
retry = 0;
fwrite("Crash counter set to 0.\n", 1uLL, 0x18uLL, stderr);
}
result = v2;
}
return result;
}

original_code:
int prepareDev(char* devname) {
 int fd = sizeof (struct input_event);
 char name[256] = "Unknown";
 if ((fd = open(devname, 00)) == -1) {
  fprintf(stderr, "%s is not a valid device.\n", devname);
  perror("error: ");
  if (retry > -1 && retry < max_retry) {
   retry++;
   fprintf(stderr, "Trying to recover...\n");
   usleep(timeout);
   return prepareDev(devname);
  }
  exit(1);
 }
 if (verbose > 0) {
  ioctl(fd, (((2U) << (((0 +8)+8)+14)) | (('E') << (0 +8)) | ((0x06) << 0) | ((sizeof (name)) << ((0 +8)+8))), name);
  fprintf(stderr, "Reading From : %s (%s)\n", devname, name);
 }
 ioctl(fd, (((1U) << (((0 +8)+8)+14)) | ((('E')) << (0 +8)) | (((0x90)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), 1);
 if (retry > -1) {
  retry = 0;
  fprintf(stderr, "Crash counter set to 0.\n");
 }
 return fd;
}

canonical_original_code:
int prepareDev(char *devname) {
 int fd = sizeof (struct input_event);
 char name[256] = "Unknown";
 if ((fd = open(devname, 00)) == -1) {
  fprintf(stderr, "%s is not a valid device.\n", devname);
  perror("error: ");
  if (retry > -1 && retry < max_retry) {
   retry++;
   fprintf(stderr, "Trying to recover...\n");
   usleep(timeout);
   return prepareDev(devname);
  }
  exit(1);
 }
 if (verbose > 0) {
  ioctl(fd, (((2U) << (((0 +8)+8)+14)) | (('E') << (0 +8)) | ((0x06) << 0) | ((sizeof (name)) << ((0 +8)+8))), name);
  fprintf(stderr, "Reading From : %s (%s)\n", devname, name);
 }
 ioctl(fd, (((1U) << (((0 +8)+8)+14)) | ((('E')) << (0 +8)) | (((0x90)) << 0) | ((((sizeof(int)))) << ((0 +8)+8))), 1);
 if (retry > -1) {
  retry = 0;
  fprintf(stderr, "Crash counter set to 0.\n");
 }
 return fd;
}

variable_types:
devname
char *

variable_types:
name
char [256]


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er12
__int64 v5; // r15
unsigned __int16 v6; // ax
unsigned __int16 v7; // r13
unsigned __int8 v8; // r14
unsigned int v9; // ebp
bool v10; // zf
unsigned __int16 v12; // ax
unsigned __int8 v13; // al
unsigned int v14; // ebx
unsigned __int8 v15; // al
unsigned int v16; // ebx
char s[2]; // [rsp+8h] [rbp-40h] BYREF
char v18; // [rsp+Ah] [rbp-3Eh]

while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "nvh");
if ( v3 == -1 )
break;
switch ( v3 )
{
case 'n':
init = 0;
break;
case 'v':
++verbose;
break;
case 'h':
puts(
"Usage: piface_tool [options] <pin number> [in|out [<value>]]\n"
"Read/Set the piface pins.\n"
"Pin '8' is a shortcut for all pins.\n"
"\n"
"-n\t\tForce no init (use if other programs are using the piface)\n"
"-v\t\tVerbose\n"
"-h\t\tShow this message\n");
exit(1);
}
}
v4 = optind;
if ( argc == optind )
{
puts("Missing argument(s)");
exit(1);
}
v5 = optind;
v6 = atoi(argv[v5]);
v7 = v6;
v8 = v6;
if ( v6 > 8u )
{
printf("Invalid pin: %d\n", (unsigned int)(__int16)v6);
exit(1);
}
snprintf(s, 7uLL, "%s", argv[v5 + 1]);
if ( argc > v4 + 2 )
{
v12 = atoi(argv[v5 + 2]);
v9 = (__int16)v12;
if ( v12 > 1u )
{
printf("Invalid value: %d\n", (unsigned int)(__int16)v7);
exit(1);
}
}
else
{
v9 = 0;
}
if ( verbose )
fwrite("Loading previous state\n", 1uLL, 0x17uLL, stderr);
get_piface();
if ( verbose )
fwrite("Opening SPI port\n", 1uLL, 0x11uLL, stderr);
pfio_init_spi();
if ( !*(_WORD *)s_piface )
{
if ( verbose )
fwrite("Initializing piface...\n", 1uLL, 0x17uLL, stderr);
pfio_init_piface();
pfio_reset();
v10 = verbose == 0;
*(_WORD *)s_piface = 1;
if ( !v10 )
fwrite("Piface ready\n", 1uLL, 0xDuLL, stderr);
}
if ( *(_WORD *)s != 28265 || v18 )
{
if ( strcmp(s, "out") )
{
printf("Invalid mode: %s\n", s);
exit(1);
}
if ( v7 == 8 )
{
v16 = 7;
do
pfio_digital_write(v16--, v9);
while ( v16 );
v8 = 0;
}
pfio_digital_write(v8, v9);
}
else
{
if ( v7 == 8 )
{
v14 = 7;
do
{
v15 = pfio_digital_read(v14);
printf("%d\n", v15);
--v14;
}
while ( v14 );
v8 = 0;
}
v13 = pfio_digital_read(v8);
printf("%d\n", v13);
}
pfio_deinit();
munmap(s_piface, size);
LOWORD(fl) = 2;
if ( fcntl(fd, 6, &fl) == -1 )
{
perror("fcntl");
exit(1);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er12
__int64 v5; // r15
unsigned __int16 v6; // ax
unsigned __int16 v7; // r13
unsigned __int8 v8; // r14
unsigned int v9; // ebp
bool v10; // zf
unsigned __int16 v12; // ax
unsigned __int8 v13; // al
unsigned int v14; // ebx
unsigned __int8 v15; // al
unsigned int v16; // ebx
char s[2]; // [rsp+8h] [rbp-40h] BYREF
char v18; // [rsp+Ah] [rbp-3Eh]

while ( 1 )
{
v3 = getopt(argc, (char *const *)argv, "nvh");
if ( v3 == -1 )
break;
switch ( v3 )
{
case 'n':
init = 0;
break;
case 'v':
++verbose;
break;
case 'h':
puts(
"Usage: piface_tool [options] <pin number> [in|out [<value>]]\n"
"Read/Set the piface pins.\n"
"Pin '8' is a shortcut for all pins.\n"
"\n"
"-n\t\tForce no init (use if other programs are using the piface)\n"
"-v\t\tVerbose\n"
"-h\t\tShow this message\n");
exit(1);
}
}
v4 = optind;
if ( argc == optind )
{
puts("Missing argument(s)");
exit(1);
}
v5 = optind;
v6 = atoi(argv[v5]);
v7 = v6;
v8 = v6;
if ( v6 > 8u )
{
printf("Invalid pin: %d\n", (unsigned int)(__int16)v6);
exit(1);
}
snprintf(s, 7uLL, "%s", argv[v5 + 1]);
if ( argc > v4 + 2 )
{
v12 = atoi(argv[v5 + 2]);
v9 = (__int16)v12;
if ( v12 > 1u )
{
printf("Invalid value: %d\n", (unsigned int)(__int16)v7);
exit(1);
}
}
else
{
v9 = 0;
}
if ( verbose )
fwrite("Loading previous state\n", 1uLL, 0x17uLL, stderr);
func7();
if ( verbose )
fwrite("Opening SPI port\n", 1uLL, 0x11uLL, stderr);
func11();
if ( !*(_WORD *)s_piface )
{
if ( verbose )
fwrite("Initializing piface...\n", 1uLL, 0x17uLL, stderr);
func12();
func20();
v10 = verbose == 0;
*(_WORD *)s_piface = 1;
if ( !v10 )
fwrite("Piface ready\n", 1uLL, 0xDuLL, stderr);
}
if ( *(_WORD *)s != 28265 || v18 )
{
if ( strcmp(s, "out") )
{
printf("Invalid mode: %s\n", s);
exit(1);
}
if ( v7 == 8 )
{
v16 = 7;
do
func19(v16--, v9);
while ( v16 );
v8 = 0;
}
func19(v8, v9);
}
else
{
if ( v7 == 8 )
{
v14 = 7;
do
{
v15 = func18(v14);
printf("%d\n", v15);
--v14;
}
while ( v14 );
v8 = 0;
}
v13 = func18(v8);
printf("%d\n", v13);
}
func13();
munmap(s_piface, size);
LOWORD(fl) = 2;
if ( fcntl(fd, 6, &fl) == -1 )
{
perror("fcntl");
exit(1);
}
return 0;
}

original_code:
int main(int argc, char** argv) {
 int c,index;
 char mode[8];
 short pin=0;
 short value=0;
    while ((c = getopt (argc, argv, "nvh")) != -1){
        switch (c){
   case 'v':
    verbose++;
    break;
   case 'n':
    init=0;
    break;
            case 'h':
                printf("Usage: piface_tool [options] <pin number> [in|out [<value>]]\n"
                    "Read/Set the piface pins.\n"
     "Pin '8' is a shortcut for all pins.\n\n"
     "-n\t\tForce no init (use if other programs are using the piface)\n"
                    "-v\t\tVerbose\n"
                    "-h\t\tShow this message\n\n"
                );
                exit (1);
        }
    }
 index = optind;
 if (index == argc){
  printf("Missing argument(s)\n");
  exit(1);
 }
 pin=atoi(argv[index]);
 if (pin<0 || pin>8){
  printf("Invalid pin: %d\n",pin);
  exit(1);
 }
 index++;
 snprintf(mode,7,"%s",argv[index]);
 index++;
 if (index < argc)
  value=atoi(argv[index]);
 if (value<0 || value>1){
  printf("Invalid value: %d\n",pin);
  exit(1);
 }
 if (verbose)
  fprintf(stderr,"Loading previous state\n");
 get_piface();
 if (verbose)
  fprintf(stderr,"Opening SPI port\n");
 pfio_init_spi();
 if (! s_piface->piface_init){
  if (verbose)
   fprintf(stderr,"Initializing piface...\n");
  pfio_init_piface();
  pfio_reset();
  s_piface->piface_init=1;
  if (verbose)
   fprintf(stderr,"Piface ready\n");
 }
 if (strcmp(mode,"in")==0){
  if (pin==8){
   while (pin>1){
    pin--;
    printf("%d\n",pfio_digital_read(pin));
   }
   pin--;
  }
  printf("%d\n",pfio_digital_read(pin));
 }
 else if (strcmp(mode,"out")==0){
  if (pin==8){
   while (pin>1){
    pin--;
    pfio_digital_write(pin,value);
   }
   pin--;
  }
  pfio_digital_write(pin,value);
 }
 else{
  printf("Invalid mode: %s\n",mode);
  exit(1);
 }
 pfio_deinit();
 munmap(s_piface,size);
 fl.l_type = 2;
 if (fcntl(fd, 6, &fl)==-1){
  perror("fcntl");
  exit(1);
 }
 return (0);
}

canonical_original_code:
int main(int argc, char **argv) {
 int c; int index;
 char mode[8];
 short pin = 0;
 short value = 0;
    while ((c = getopt (argc, argv, "nvh")) != -1){
        switch (c){
   case 'v':
    verbose++;
    break;
   case 'n':
    init=0;
    break;
            case 'h':
                printf("Usage: piface_tool [options] <pin number> [in|out [<value>]]\n"
                    "Read/Set the piface pins.\n"
     "Pin '8' is a shortcut for all pins.\n\n"
     "-n\t\tForce no init (use if other programs are using the piface)\n"
                    "-v\t\tVerbose\n"
                    "-h\t\tShow this message\n\n"
                );
                exit (1);
        }
    }
 index = optind;
 if (index == argc){
  printf("Missing argument(s)\n");
  exit(1);
 }
 pin=atoi(argv[index]);
 if (pin<0 || pin>8){
  printf("Invalid pin: %d\n",pin);
  exit(1);
 }
 index++;
 snprintf(mode,7,"%s",argv[index]);
 index++;
 if (index < argc)
  value=atoi(argv[index]);
 if (value<0 || value>1){
  printf("Invalid value: %d\n",pin);
  exit(1);
 }
 if (verbose)
  fprintf(stderr,"Loading previous state\n");
 get_piface();
 if (verbose)
  fprintf(stderr,"Opening SPI port\n");
 pfio_init_spi();
 if (! s_piface->piface_init){
  if (verbose)
   fprintf(stderr,"Initializing piface...\n");
  pfio_init_piface();
  pfio_reset();
  s_piface->piface_init=1;
  if (verbose)
   fprintf(stderr,"Piface ready\n");
 }
 if (strcmp(mode,"in")==0){
  if (pin==8){
   while (pin>1){
    pin--;
    printf("%d\n",pfio_digital_read(pin));
   }
   pin--;
  }
  printf("%d\n",pfio_digital_read(pin));
 }
 else if (strcmp(mode,"out")==0){
  if (pin==8){
   while (pin>1){
    pin--;
    pfio_digital_write(pin,value);
   }
   pin--;
  }
  pfio_digital_write(pin,value);
 }
 else{
  printf("Invalid mode: %s\n",mode);
  exit(1);
 }
 pfio_deinit();
 munmap(s_piface,size);
 fl.l_type = 2;
 if (fcntl(fd, 6, &fl)==-1){
  perror("fcntl");
  exit(1);
 }
 return (0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: c int

variable_types: index int

variable_types:
mode
char [8]

variable_types: pin short

variable_types: value short


decompiled_code:
_WORD *get_piface()
{
int v0; // ebx
unsigned __int64 v1; // rbp
_WORD *result; // rax

v0 = 0;
fd = open("/tmp/piface.status", 2);
if ( fd < 0 )
{
if ( *__errno_location() != 2 || (fd = open("/tmp/piface.status", 66, 448LL), fchmod(fd, 0x1B6u), fd < 0) )
{
perror("fopen");
exit(1);
}
v0 = 1;
}
fl = 1;
qword_404148 = 0LL;
qword_404150 = 0LL;
dword_404158 = getpid();
if ( fcntl(fd, 7, &fl) == -1 )
{
perror("fcntl");
exit(1);
}
v1 = sysconf(30);
size = sysconf(30) * (1 / v1 + 1);
if ( ftruncate(fd, size) < 0 )
{
perror("ftruncate");
exit(1);
}
result = mmap(0LL, size, 3, 1, fd, 0LL);
s_piface = result;
if ( result == (_WORD *)-1LL )
{
perror("mmap");
exit(1);
}
if ( v0 )
{
if ( init )
*result = 0;
}
return result;
}

canonical_decompiled_code:
_WORD *func7()
{
int v0; // ebx
unsigned __int64 v1; // rbp
_WORD *result; // rax

v0 = 0;
fd = open("/tmp/piface.status", 2);
if ( fd < 0 )
{
if ( *__errno_location() != 2 || (fd = open("/tmp/piface.status", 66, 448LL), fchmod(fd, 0x1B6u), fd < 0) )
{
perror("fopen");
exit(1);
}
v0 = 1;
}
fl = 1;
qword_404148 = 0LL;
qword_404150 = 0LL;
dword_404158 = getpid();
if ( fcntl(fd, 7, &fl) == -1 )
{
perror("fcntl");
exit(1);
}
v1 = sysconf(30);
size = sysconf(30) * (1 / v1 + 1);
if ( ftruncate(fd, size) < 0 )
{
perror("ftruncate");
exit(1);
}
result = mmap(0LL, size, 3, 1, fd, 0LL);
s_piface = result;
if ( result == (_WORD *)-1LL )
{
perror("mmap");
exit(1);
}
if ( v0 )
{
if ( init )
*result = 0;
}
return result;
}

original_code:
void get_piface() {
 int pages, new=0;
 fd=open("/tmp/piface.status", 02);
 if (fd < 0 && (*__errno_location ())==2){
  fd=open("/tmp/piface.status", 02 | 0100, (0400|0200|0100));
  fchmod(fd, 0666);
  new=1;
 }
 if (fd < 0){
  perror("fopen");
  exit(1);
 }
 fl.l_type = 1;
 fl.l_whence = 0;
 fl.l_start = 0;
 fl.l_len = 0;
 fl.l_pid = getpid();
 if (fcntl(fd, 7, &fl)==-1){
  perror("fcntl");
  exit(1);
 }
 pages=1+((sizeof(p_mem)-1)/sysconf(_SC_PAGESIZE));
 size=pages*sysconf(_SC_PAGESIZE);
 if (ftruncate(fd,size)<0){
  perror("ftruncate");
  exit(1);
 }
 s_piface=(p_mem*) mmap(((void *)0), size, 0x1|0x2, 0x01, fd, 0);
 if (s_piface==((void *) -1)){
  perror("mmap");
  exit(1);
 }
 if (new && init)
  s_piface->piface_init=0;
}

canonical_original_code:
void get_piface() {
 int pages; int new = 0;
 fd=open("/tmp/piface.status", 02);
 if (fd < 0 && (*__errno_location ())==2){
  fd=open("/tmp/piface.status", 02 | 0100, (0400|0200|0100));
  fchmod(fd, 0666);
  new=1;
 }
 if (fd < 0){
  perror("fopen");
  exit(1);
 }
 fl.l_type = 1;
 fl.l_whence = 0;
 fl.l_start = 0;
 fl.l_len = 0;
 fl.l_pid = getpid();
 if (fcntl(fd, 7, &fl)==-1){
  perror("fcntl");
  exit(1);
 }
 pages=1+((sizeof(p_mem)-1)/sysconf(_SC_PAGESIZE));
 size=pages*sysconf(_SC_PAGESIZE);
 if (ftruncate(fd,size)<0){
  perror("ftruncate");
  exit(1);
 }
 s_piface=(struct p *) mmap(((void *)0), size, 0x1|0x2, 0x01, fd, 0);
 if (s_piface==((void *) -1)){
  perror("mmap");
  exit(1);
 }
 if (new && init)
  s_piface->piface_init=0;
}

variable_types: pages int

variable_types: new int


repo: pierre/holt-winters

original_code:
int main() {
    double series[] = {3.93, 5.31, 7.24, 9.64, 12.90, 17.10, 23.20, 31.40, 39.80, 50.20, 62.90, 76.00, 92.00, 105.70, 122.80, 131.70, 151.30, 179.30, 203.20};
    int forecast = 19;
    double alpha = 0.9999208;
    double beta = 0;
    double gamma = 0;
    int start_time = 2;
    int seasonal = 0;
    int period = 0;
    double a0 = series[0];
    double b0 = 0;
    double s[] = {};
    double errors;
    int nb_computations = forecast - start_time - 1;
    double *estimated_level = malloc(nb_computations * sizeof(double));
    double *estimated_trend = malloc(nb_computations * sizeof(double));
    double *estimated_season = malloc(nb_computations * sizeof(double));
    HoltWinters(
        series,
        &forecast,
        &alpha,
        &beta,
        &gamma,
        &start_time,
        &seasonal,
        &period,
        &a0,
        &b0,
        s,
        &errors,
        estimated_level,
        estimated_trend,
        estimated_season
    );
    int i = 0;
    int first_year = 1800;
    printf("Estimated:\n");
    for (i = 0; i < nb_computations; i++) {
        printf("\tyear = %d, level: %f, trend: %f\n", first_year + i * 10, estimated_level[i], estimated_trend[i]);
    }
    free(estimated_level);
    free(estimated_trend);
    free(estimated_season);
    return 0;
}

variable_types:
series
double []

variable_types: forecast int

variable_types: alpha double

variable_types: beta double

variable_types: gamma double

variable_types: seasonal int

variable_types: period int

variable_types:
s
double []

variable_types: errors double

variable_types: i int


HoltWinters

original_code:
void HoltWinters (
          double *x,
          int *xl,
          double *alpha,
          double *beta,
          double *gamma,
          int *start_time,
          int *seasonal,
          int *period,
          double *a,
          double *b,
          double *s,
          double *SSE,
          double *level,
          double *trend,
          double *season
    )
{
    double res = 0, xhat = 0, stmp = 0;
    int i, i0, s0;
    level[0] = *a;
    if (*beta > 0) trend[0] = *b;
    if (*gamma > 0) memcpy(season, s, *period * sizeof(double));
    for (i = *start_time - 1; i < *xl; i++) {
        i0 = i - *start_time + 2;
        s0 = i0 + *period - 1;
        xhat = level[i0 - 1] + (*beta > 0 ? trend[i0 - 1] : 0);
        stmp = *gamma > 0 ? season[s0 - *period] : (*seasonal != 1);
        if (*seasonal == 1)
            xhat += stmp;
        else
            xhat *= stmp;
        res = x[i] - xhat;
        *SSE += res * res;
        if (*seasonal == 1)
            level[i0] = *alpha * (x[i] - stmp)
                      + (1 - *alpha) * (level[i0 - 1] + trend[i0 - 1]);
        else
            level[i0] = *alpha * (x[i] / stmp)
                      + (1 - *alpha) * (level[i0 - 1] + trend[i0 - 1]);
        if (*beta > 0)
            trend[i0] = *beta * (level[i0] - level[i0 - 1])
                      + (1 - *beta) * trend[i0 - 1];
        if (*gamma > 0) {
            if (*seasonal == 1)
                season[s0] = *gamma * (x[i] - level[i0])
                           + (1 - *gamma) * stmp;
            else
                season[s0] = *gamma * (x[i] / level[i0])
                       + (1 - *gamma) * stmp;
        }
    }
}

canonical_original_code:
void HoltWinters(double *x, int *xl, double *alpha, double *beta, double *gamma, int *start_time, int *seasonal, int *period, double *a, double *b, double *s, double *SSE, double *level, double *trend, double *season)
{
    double res = 0; double xhat = 0; double stmp = 0;
    int i; int i0; int s0;
    level[0] = *a;
    if (*beta > 0) trend[0] = *b;
    if (*gamma > 0) memcpy(season, s, *period * sizeof(double));
    for (i = *start_time - 1; i < *xl; i++) {
        i0 = i - *start_time + 2;
        s0 = i0 + *period - 1;
        xhat = level[i0 - 1] + (*beta > 0 ? trend[i0 - 1] : 0);
        stmp = *gamma > 0 ? season[s0 - *period] : (*seasonal != 1);
        if (*seasonal == 1)
            xhat += stmp;
        else
            xhat *= stmp;
        res = x[i] - xhat;
        *SSE += res * res;
        if (*seasonal == 1)
            level[i0] = *alpha * (x[i] - stmp)
                      + (1 - *alpha) * (level[i0 - 1] + trend[i0 - 1]);
        else
            level[i0] = *alpha * (x[i] / stmp)
                      + (1 - *alpha) * (level[i0 - 1] + trend[i0 - 1]);
        if (*beta > 0)
            trend[i0] = *beta * (level[i0] - level[i0 - 1])
                      + (1 - *beta) * trend[i0 - 1];
        if (*gamma > 0) {
            if (*seasonal == 1)
                season[s0] = *gamma * (x[i] - level[i0])
                           + (1 - *gamma) * stmp;
            else
                season[s0] = *gamma * (x[i] / level[i0])
                       + (1 - *gamma) * stmp;
        }
    }
}

variable_types:
x
double *

variable_types:
xl
int *

variable_types:
alpha
double *

variable_types:
beta
double *

variable_types:
gamma
double *

variable_types:
seasonal
int *

variable_types:
period
int *

variable_types:
a
double *

variable_types:
b
double *

variable_types:
s
double *

variable_types:
SSE
double *

variable_types:
level
double *

variable_types:
trend
double *

variable_types:
season
double *

variable_types: res double

variable_types: xhat double

variable_types: stmp double


decompiled_code:
__int64 __fastcall smtp_connect(const char *a1)
{
struct hostent *v1; // rax
unsigned int v2; // er12
socklen_t v3; // edx
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
__int64 v9; // rcx
const char *v10; // r8
int *v11; // rax
char *v12; // rax
int dest; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr v14; // [rsp+10h] [rbp-18h] BYREF

v1 = gethostbyname(a1);
if ( !v1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
v9 = 126LL;
v10 = v8;
LABEL_7:
v2 = -1;
fprintf(stderr, "%s (%d) : %s\n", "libsmtp.c", v9, v10);
return v2;
}
memcpy(&dest, *(const void **)v1->h_addr_list, v1->h_length);
v2 = socket(2, 1, 0);
if ( v2 == -1 )
{
v5 = _errno_location();
v6 = strerror(*v5);
fprintf(stderr, "%s (%d) : %s\n", "libsmtp.c", 132LL, v6);
return v2;
}
*(_QWORD *)&v14.sa_data[6] = 0LL;
v14.sa_family = 2;
*(_WORD *)v14.sa_data = _bswap_16_constprop_0(v2, &v14, 16LL);
*(_DWORD *)&v14.sa_data[2] = dest;
if ( connect(v2, &v14, v3) == -1 )
{
v11 = _errno_location();
v12 = strerror(*v11);
v9 = 141LL;
v10 = v12;
goto LABEL_7;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
struct hostent *v1; // rax
unsigned int v2; // er12
socklen_t v3; // edx
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
__int64 v9; // rcx
const char *v10; // r8
int *v11; // rax
char *v12; // rax
int dest; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr v14; // [rsp+10h] [rbp-18h] BYREF

v1 = gethostbyname(a1);
if ( !v1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
v9 = 126LL;
v10 = v8;
LABEL_7:
v2 = -1;
fprintf(stderr, "%s (%d) : %s\n", "libsmtp.c", v9, v10);
return v2;
}
memcpy(&dest, *(const void **)v1->h_addr_list, v1->h_length);
v2 = socket(2, 1, 0);
if ( v2 == -1 )
{
v5 = _errno_location();
v6 = strerror(*v5);
fprintf(stderr, "%s (%d) : %s\n", "libsmtp.c", 132LL, v6);
return v2;
}
*(_QWORD *)&v14.sa_data[6] = 0LL;
v14.sa_family = 2;
*(_WORD *)v14.sa_data = func0(v2, &v14, 16LL);
*(_DWORD *)&v14.sa_data[2] = dest;
if ( connect(v2, &v14, v3) == -1 )
{
v11 = _errno_location();
v12 = strerror(*v11);
v9 = 141LL;
v10 = v12;
goto LABEL_7;
}
return v2;
}

original_code:
int smtp_connect(const char * smtp_server)
{
    struct hostent * host;
    struct in_addr ip_address;
    int sock;
    struct sockaddr_in sin;
    int ret;
    host = gethostbyname(smtp_server);
    if (host == ((void *)0)) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 126, strerror((*__errno_location ()))); return -1; } while(0);
    }
    memcpy(&ip_address, host->h_addr_list[0], host->h_length);
    sock = socket(2, SOCK_STREAM, 0);
    if (sock == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 132, strerror((*__errno_location ()))); return -1; } while(0);
    }
    memset(&sin, 0, sizeof sin);
    sin.sin_family = 2;
    sin.sin_port = htons(25);
    sin.sin_addr = ip_address;
    ret = connect(sock, (struct sockaddr *)&sin, sizeof sin);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 141, strerror((*__errno_location ()))); return -1; } while(0);
    }
    return sock;
}

canonical_original_code:
int smtp_connect(char *smtp_server)
{
    struct hostent *host;
    struct in_addr ip_address;
    int sock;
    struct sockaddr_in sin;
    int ret;
    host = gethostbyname(smtp_server);
    if (host == ((void *)0)) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 126, strerror((*__errno_location ()))); return -1; } while(0);
    }
    memcpy(&ip_address, host->h_addr_list[0], host->h_length);
    sock = socket(2, SOCK_STREAM, 0);
    if (sock == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 132, strerror((*__errno_location ()))); return -1; } while(0);
    }
    memset(&sin, 0, sizeof sin);
    sin.sin_family = 2;
    sin.sin_port = htons(25);
    sin.sin_addr = ip_address;
    ret = connect(sock, (struct sockaddr *)&sin, sizeof sin);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 141, strerror((*__errno_location ()))); return -1; } while(0);
    }
    return sock;
}

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

variable_types: sock int

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

variable_types: ret int

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

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


decompiled_code:
__int64 __fastcall smtp_send_command(int fd, char *s, char *a3, unsigned int a4)
{
size_t v6; // rax
size_t v7; // rbx
char *v8; // rbp
size_t v9; // rax
unsigned int v10; // er14
unsigned int v11; // eax
char *v12; // rbp
int *v14; // rax
char *v15; // rax
__int64 v16; // rcx
const char *v17; // r8
int *v18; // rax
char *v19; // rax
size_t v20; // [rsp+8h] [rbp-440h]
char buf[1080]; // [rsp+10h] [rbp-438h] BYREF

v6 = strlen(s);
v7 = v6;
if ( a3 )
{
v20 = strlen(a3);
v8 = (char *)malloc(v7 + v20 + 3);
memcpy(v8, s, v7);
memcpy(&v8[v7], a3, v20 + 1);
}
else
{
v8 = (char *)malloc(v6 + 3);
memcpy(v8, s, v7 + 1);
}
v9 = strlen(v8);
strcpy(&v8[v9], "\r\n");
v10 = send(fd, v8, v9 + 2, 0);
if ( v10 == -1 )
{
v18 = _errno_location();
v19 = strerror(*v18);
v16 = 173LL;
v17 = v19;
goto LABEL_10;
}
v11 = recv(fd, buf, 0x400uLL, 0);
v10 = v11;
if ( v11 == -1 )
{
v14 = _errno_location();
v15 = strerror(*v14);
v16 = 178LL;
v17 = v15;
LABEL_10:
fprintf(stderr, "%s (%d) : %s\n", "libsmtp.c", v16, v17);
return v10;
}
buf[v11] = 0;
free(v8);
v12 = (char *)malloc(0x400uLL);
sprintf(v12, "%d ", a4);
if ( strstr(buf, v12) )
{
v10 = 0;
free(v12);
}
else
{
v10 = -1;
fprintf(stderr, "%s (%d) : Bad smtp response : %s", "libsmtp.c", 190LL, buf);
}
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, char *s, char *a3, unsigned int a4)
{
size_t v6; // rax
size_t v7; // rbx
char *v8; // rbp
size_t v9; // rax
unsigned int v10; // er14
unsigned int v11; // eax
char *v12; // rbp
int *v14; // rax
char *v15; // rax
__int64 v16; // rcx
const char *v17; // r8
int *v18; // rax
char *v19; // rax
size_t v20; // [rsp+8h] [rbp-440h]
char buf[1080]; // [rsp+10h] [rbp-438h] BYREF

v6 = strlen(s);
v7 = v6;
if ( a3 )
{
v20 = strlen(a3);
v8 = (char *)malloc(v7 + v20 + 3);
memcpy(v8, s, v7);
memcpy(&v8[v7], a3, v20 + 1);
}
else
{
v8 = (char *)malloc(v6 + 3);
memcpy(v8, s, v7 + 1);
}
v9 = strlen(v8);
strcpy(&v8[v9], "\r\n");
v10 = send(fd, v8, v9 + 2, 0);
if ( v10 == -1 )
{
v18 = _errno_location();
v19 = strerror(*v18);
v16 = 173LL;
v17 = v19;
goto LABEL_10;
}
v11 = recv(fd, buf, 0x400uLL, 0);
v10 = v11;
if ( v11 == -1 )
{
v14 = _errno_location();
v15 = strerror(*v14);
v16 = 178LL;
v17 = v15;
LABEL_10:
fprintf(stderr, "%s (%d) : %s\n", "libsmtp.c", v16, v17);
return v10;
}
buf[v11] = 0;
free(v8);
v12 = (char *)malloc(0x400uLL);
sprintf(v12, "%d ", a4);
if ( strstr(buf, v12) )
{
v10 = 0;
free(v12);
}
else
{
v10 = -1;
fprintf(stderr, "%s (%d) : Bad smtp response : %s", "libsmtp.c", 190LL, buf);
}
return v10;
}

original_code:
int smtp_send_command(int sock, const char * cmd, const char * data,
    int return_code)
{
    int ret;
    char * buf;
    char recvbuf[1024];
    if (data != ((void *)0)) {
        buf = malloc(strlen(cmd) + strlen(data) + 3);
    }
    else {
        buf = malloc(strlen(cmd) + 3);
    }
    strcpy(buf, cmd);
    if (data != ((void *)0)) {
        strcat(buf, data);
    }
    strcat(buf, "\r\n");
    ret = send(sock, buf, strlen(buf), 0);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 173, strerror((*__errno_location ()))); return -1; } while(0);
    }
    ret = recv(sock, recvbuf, sizeof recvbuf, 0);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 178, strerror((*__errno_location ()))); return -1; } while(0);
    }
    recvbuf[ret] = '\0';
    free(buf);
    buf = malloc(1024);
    ret = sprintf(buf, "%d ", return_code);
    if (ret < 0) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 186, strerror((*__errno_location ()))); return -1; } while(0);
    }
    if (strstr(recvbuf, buf) == ((void *)0)) {
        fprintf(stderr, "%s (%d) : Bad smtp response : %s", "libsmtp.c", 190, recvbuf);
        return -1;
    }
    free(buf);
    return 0;
}

canonical_original_code:
int smtp_send_command(int sock, char *cmd, char *data, int return_code)
{
    int ret;
    char *buf;
    char recvbuf[1024];
    if (data != ((void *)0)) {
        buf = malloc(strlen(cmd) + strlen(data) + 3);
    }
    else {
        buf = malloc(strlen(cmd) + 3);
    }
    strcpy(buf, cmd);
    if (data != ((void *)0)) {
        strcat(buf, data);
    }
    strcat(buf, "\r\n");
    ret = send(sock, buf, strlen(buf), 0);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 173, strerror((*__errno_location ()))); return -1; } while(0);
    }
    ret = recv(sock, recvbuf, sizeof recvbuf, 0);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 178, strerror((*__errno_location ()))); return -1; } while(0);
    }
    recvbuf[ret] = '\0';
    free(buf);
    buf = malloc(1024);
    ret = sprintf(buf, "%d ", return_code);
    if (ret < 0) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 186, strerror((*__errno_location ()))); return -1; } while(0);
    }
    if (strstr(recvbuf, buf) == ((void *)0)) {
        fprintf(stderr, "%s (%d) : Bad smtp response : %s", "libsmtp.c", 190, recvbuf);
        return -1;
    }
    free(buf);
    return 0;
}

variable_types:
cmd
char *

variable_types:
data
char *

variable_types:
buf
char *

variable_types:
recvbuf
char [1024]


decompiled_code:
__int64 __fastcall smtp_send_data(int fd, const char **a2)
{
time_t v3; // rax
const char *v4; // r15
const char *v5; // rdi
size_t v6; // r14
size_t v7; // r13
size_t v8; // rbp
size_t v9; // rax
char *v10; // rbp
struct tm *v11; // rax
char *v12; // rax
char *v13; // rax
char *v14; // rax
size_t v15; // rax
char *v16; // rax
unsigned int v17; // er13
unsigned int v18; // eax
char *v19; // rbp
size_t v21; // r13
size_t v22; // rbp
size_t v23; // rax
struct tm *v24; // rax
char *v25; // rax
char *v26; // rax
int *v27; // rax
char *v28; // rax
__int64 v29; // rcx
const char *v30; // r8
int *v31; // rax
char *v32; // rax
time_t timer; // [rsp+8h] [rbp-470h] BYREF
char s[48]; // [rsp+10h] [rbp-468h] BYREF
char buf[1080]; // [rsp+40h] [rbp-438h] BYREF

v3 = time(0LL);
v4 = a2[2];
v5 = *a2;
timer = v3;
if ( v4 )
{
v6 = strlen(v5);
v7 = strlen(a2[1]);
v8 = strlen(a2[3]);
v9 = strlen(v4);
v10 = (char *)malloc(v9 + v8 + v6 + v7 + 140);
v11 = gmtime(&timer);
strftime(s, 0x28uLL, "%a, %d %b %Y %H:%M:%S +0000", v11);
strcpy(v10, "From: ");
v12 = stpcpy(v10 + 6, a2[1]);
strcpy(v12, "\r\nTo: ");
v13 = stpcpy(v12 + 6, *a2);
*(_QWORD *)v13 = 0x203A657461440A0DLL;
v14 = stpcpy(v13 + 8, s);
strcpy(v14, "\r\nSubject: ");
strcpy(v14 + 11, a2[2]);
}
else
{
v21 = strlen(v5);
v22 = strlen(a2[1]);
v23 = strlen(a2[3]);
v10 = (char *)malloc(v23 + v21 + v22 + 140);
v24 = gmtime(&timer);
strftime(s, 0x28uLL, "%a, %d %b %Y %H:%M:%S +0000", v24);
strcpy(v10, "From: ");
v25 = stpcpy(v10 + 6, a2[1]);
strcpy(v25, "\r\nTo: ");
v26 = stpcpy(v25 + 6, *a2);
*(_QWORD *)v26 = 0x203A657461440A0DLL;
strcpy(v26 + 8, s);
}
v15 = strlen(v10);
strcpy(&v10[v15], "\r\n\r\n");
v16 = stpcpy(&v10[v15 + 4], a2[3]);
strcpy(v16, "\r\n.\r\n");
v17 = send(fd, v10, v16 - v10 + 5, 0);
if ( v17 == -1 )
{
v31 = _errno_location();
v32 = strerror(*v31);
v29 = 247LL;
v30 = v32;
goto LABEL_10;
}
v18 = recv(fd, buf, 0x400uLL, 0);
v17 = v18;
if ( v18 == -1 )
{
v27 = _errno_location();
v28 = strerror(*v27);
v29 = 252LL;
v30 = v28;
LABEL_10:
fprintf(stderr, "%s (%d) : %s\n", "libsmtp.c", v29, v30);
return v17;
}
buf[v18] = 0;
free(v10);
v19 = (char *)malloc(0xCuLL);
sprintf(v19, "%d ", 250LL);
if ( strstr(buf, v19) )
{
v17 = 0;
free(v19);
}
else
{
v17 = -1;
fprintf(stderr, "%s (%d) : Bad smtp response : %s", "libsmtp.c", 269LL, buf);
}
return v17;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, const char **a2)
{
time_t v3; // rax
const char *v4; // r15
const char *v5; // rdi
size_t v6; // r14
size_t v7; // r13
size_t v8; // rbp
size_t v9; // rax
char *v10; // rbp
struct tm *v11; // rax
char *v12; // rax
char *v13; // rax
char *v14; // rax
size_t v15; // rax
char *v16; // rax
unsigned int v17; // er13
unsigned int v18; // eax
char *v19; // rbp
size_t v21; // r13
size_t v22; // rbp
size_t v23; // rax
struct tm *v24; // rax
char *v25; // rax
char *v26; // rax
int *v27; // rax
char *v28; // rax
__int64 v29; // rcx
const char *v30; // r8
int *v31; // rax
char *v32; // rax
time_t timer; // [rsp+8h] [rbp-470h] BYREF
char s[48]; // [rsp+10h] [rbp-468h] BYREF
char buf[1080]; // [rsp+40h] [rbp-438h] BYREF

v3 = time(0LL);
v4 = a2[2];
v5 = *a2;
timer = v3;
if ( v4 )
{
v6 = strlen(v5);
v7 = strlen(a2[1]);
v8 = strlen(a2[3]);
v9 = strlen(v4);
v10 = (char *)malloc(v9 + v8 + v6 + v7 + 140);
v11 = gmtime(&timer);
strftime(s, 0x28uLL, "%a, %d %b %Y %H:%M:%S +0000", v11);
strcpy(v10, "From: ");
v12 = stpcpy(v10 + 6, a2[1]);
strcpy(v12, "\r\nTo: ");
v13 = stpcpy(v12 + 6, *a2);
*(_QWORD *)v13 = 0x203A657461440A0DLL;
v14 = stpcpy(v13 + 8, s);
strcpy(v14, "\r\nSubject: ");
strcpy(v14 + 11, a2[2]);
}
else
{
v21 = strlen(v5);
v22 = strlen(a2[1]);
v23 = strlen(a2[3]);
v10 = (char *)malloc(v23 + v21 + v22 + 140);
v24 = gmtime(&timer);
strftime(s, 0x28uLL, "%a, %d %b %Y %H:%M:%S +0000", v24);
strcpy(v10, "From: ");
v25 = stpcpy(v10 + 6, a2[1]);
strcpy(v25, "\r\nTo: ");
v26 = stpcpy(v25 + 6, *a2);
*(_QWORD *)v26 = 0x203A657461440A0DLL;
strcpy(v26 + 8, s);
}
v15 = strlen(v10);
strcpy(&v10[v15], "\r\n\r\n");
v16 = stpcpy(&v10[v15 + 4], a2[3]);
strcpy(v16, "\r\n.\r\n");
v17 = send(fd, v10, v16 - v10 + 5, 0);
if ( v17 == -1 )
{
v31 = _errno_location();
v32 = strerror(*v31);
v29 = 247LL;
v30 = v32;
goto LABEL_10;
}
v18 = recv(fd, buf, 0x400uLL, 0);
v17 = v18;
if ( v18 == -1 )
{
v27 = _errno_location();
v28 = strerror(*v27);
v29 = 252LL;
v30 = v28;
LABEL_10:
fprintf(stderr, "%s (%d) : %s\n", "libsmtp.c", v29, v30);
return v17;
}
buf[v18] = 0;
free(v10);
v19 = (char *)malloc(0xCuLL);
sprintf(v19, "%d ", 250LL);
if ( strstr(buf, v19) )
{
v17 = 0;
free(v19);
}
else
{
v17 = -1;
fprintf(stderr, "%s (%d) : Bad smtp response : %s", "libsmtp.c", 269LL, buf);
}
return v17;
}

original_code:
int smtp_send_data(int sock, const libsmtp_msg_t * msg)
{
    int ret;
    char * buf;
    char date[40];
    char recvbuf[1024];
    short subject_defined = 0;
    time_t send_time = time(((void *)0));
    if (msg->subject != ((void *)0)) {
        subject_defined = 1;
    }
    if (subject_defined) {
        buf = malloc(strlen(msg->to) + strlen(msg->from) + strlen(msg->data)
            + 40 + 100 + strlen(msg->subject));
    }
    else {
        buf = malloc(strlen(msg->to) + strlen(msg->from) + strlen(msg->data)
            + 40 + 100);
    }
    strftime(date, 40, "%a, %d %b %Y  %H:%M:%S +0000", gmtime(&send_time));
    strcpy(buf, "From: ");
    strcat(buf, msg->from);
    strcat(buf, "\r\nTo: ");
    strcat(buf, msg->to);
    strcat(buf, "\r\nDate: ");
    strcat(buf, date);
    if (subject_defined) {
        strcat(buf, "\r\nSubject: ");
        strcat(buf, msg->subject);
    }
    strcat(buf, "\r\n\r\n");
    strcat(buf, msg->data);
    strcat(buf, "\r\n.\r\n");
    ret = send(sock, buf, strlen(buf), 0);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 247, strerror((*__errno_location ()))); return -1; } while(0);
    }
    ret = recv(sock, recvbuf, sizeof recvbuf, 0);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 252, strerror((*__errno_location ()))); return -1; } while(0);
    }
    recvbuf[ret] = '\0';
    free(buf);
    buf = malloc(12);
    ret = sprintf(buf, "%d ", 250);
    if (ret < 0) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 265, strerror((*__errno_location ()))); return -1; } while(0);
    }
    if (strstr(recvbuf, buf) == ((void *)0)) {
        fprintf(stderr, "%s (%d) : Bad smtp response : %s", "libsmtp.c", 269, recvbuf);
        return -1;
    }
    free(buf);
    return 0;
}

canonical_original_code:
int smtp_send_data(int sock, struct libsmtp_msg *msg)
{
    int ret;
    char *buf;
    char date[40];
    char recvbuf[1024];
    short subject_defined = 0;
    long send_time = time(((void *)0));
    if (msg->subject != ((void *)0)) {
        subject_defined = 1;
    }
    if (subject_defined) {
        buf = malloc(strlen(msg->to) + strlen(msg->from) + strlen(msg->data)
            + 40 + 100 + strlen(msg->subject));
    }
    else {
        buf = malloc(strlen(msg->to) + strlen(msg->from) + strlen(msg->data)
            + 40 + 100);
    }
    strftime(date, 40, "%a, %d %b %Y  %H:%M:%S +0000", gmtime(&send_time));
    strcpy(buf, "From: ");
    strcat(buf, msg->from);
    strcat(buf, "\r\nTo: ");
    strcat(buf, msg->to);
    strcat(buf, "\r\nDate: ");
    strcat(buf, date);
    if (subject_defined) {
        strcat(buf, "\r\nSubject: ");
        strcat(buf, msg->subject);
    }
    strcat(buf, "\r\n\r\n");
    strcat(buf, msg->data);
    strcat(buf, "\r\n.\r\n");
    ret = send(sock, buf, strlen(buf), 0);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 247, strerror((*__errno_location ()))); return -1; } while(0);
    }
    ret = recv(sock, recvbuf, sizeof recvbuf, 0);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 252, strerror((*__errno_location ()))); return -1; } while(0);
    }
    recvbuf[ret] = '\0';
    free(buf);
    buf = malloc(12);
    ret = sprintf(buf, "%d ", 250);
    if (ret < 0) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 265, strerror((*__errno_location ()))); return -1; } while(0);
    }
    if (strstr(recvbuf, buf) == ((void *)0)) {
        fprintf(stderr, "%s (%d) : Bad smtp response : %s", "libsmtp.c", 269, recvbuf);
        return -1;
    }
    free(buf);
    return 0;
}

variable_types: struct libsmtp_msg { char *to; char *from; char *subject; char *data; } *

variable_types:
date
char [40]


decompiled_code:
__int64 __fastcall libsmtp_send(const char *a1, char **a2)
{
int v2; // eax
int v3; // ebp
unsigned int v4; // er12
int *v6; // rax
char *v7; // rax
__int64 v8; // rcx
const char *v9; // r8
int *v10; // rax
char *v11; // rax
char v12[1048]; // [rsp+0h] [rbp-418h] BYREF

v2 = smtp_connect(a1);
if ( v2 == -1 )
return (unsigned int)-1;
v3 = v2;
v4 = recv(v2, v12, 0x400uLL, 0);
if ( v4 == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
v8 = 70LL;
v9 = v7;
LABEL_15:
fprintf(stderr, "%s (%d) : %s\n", "libsmtp.c", v8, v9);
return v4;
}
if ( !strstr(v12, "220 ") )
{
fprintf(stderr, "%s (%d) : Bad welcome message : %s", "libsmtp.c", 73LL, v12);
return (unsigned int)-1;
}
if ( (unsigned int)smtp_send_command(v3, "MAIL FROM: ", a2[1], 0xFAu) == -1
|| (unsigned int)smtp_send_command(v3, "RCPT TO: ", *a2, 0xFAu) == -1
|| (unsigned int)smtp_send_command(v3, "DATA", 0LL, 0x162u) == -1
|| (unsigned int)smtp_send_data(v3, (const char **)a2) == -1
|| (unsigned int)smtp_send_command(v3, "QUIT", 0LL, 0xDDu) == -1 )
{
return (unsigned int)-1;
}
v4 = close(v3);
if ( v4 == -1 )
{
v10 = _errno_location();
v11 = strerror(*v10);
v8 = 108LL;
v9 = v11;
goto LABEL_15;
}
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1, char **a2)
{
int v2; // eax
int v3; // ebp
unsigned int v4; // er12
int *v6; // rax
char *v7; // rax
__int64 v8; // rcx
const char *v9; // r8
int *v10; // rax
char *v11; // rax
char v12[1048]; // [rsp+0h] [rbp-418h] BYREF

v2 = func1(a1);
if ( v2 == -1 )
return (unsigned int)-1;
v3 = v2;
v4 = recv(v2, v12, 0x400uLL, 0);
if ( v4 == -1 )
{
v6 = _errno_location();
v7 = strerror(*v6);
v8 = 70LL;
v9 = v7;
LABEL_15:
fprintf(stderr, "%s (%d) : %s\n", "libsmtp.c", v8, v9);
return v4;
}
if ( !strstr(v12, "220 ") )
{
fprintf(stderr, "%s (%d) : Bad welcome message : %s", "libsmtp.c", 73LL, v12);
return (unsigned int)-1;
}
if ( (unsigned int)func2(v3, "MAIL FROM: ", a2[1], 0xFAu) == -1
|| (unsigned int)func2(v3, "RCPT TO: ", *a2, 0xFAu) == -1
|| (unsigned int)func2(v3, "DATA", 0LL, 0x162u) == -1
|| (unsigned int)func3(v3, (const char **)a2) == -1
|| (unsigned int)func2(v3, "QUIT", 0LL, 0xDDu) == -1 )
{
return (unsigned int)-1;
}
v4 = close(v3);
if ( v4 == -1 )
{
v10 = _errno_location();
v11 = strerror(*v10);
v8 = 108LL;
v9 = v11;
goto LABEL_15;
}
return 0;
}

original_code:
int libsmtp_send(const char * smtp_server, const libsmtp_msg_t * msg)
{
    int ret;
    int sock;
    char buf[1024];
    sock = smtp_connect(smtp_server);
    if (sock == -1) {
        return -1;
    }
    ret = recv(sock, buf, sizeof buf, 0);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 70, strerror((*__errno_location ()))); return -1; } while(0);
    }
    if (strstr(buf, "220 ") == ((void *)0)) {
        fprintf(stderr, "%s (%d) : Bad welcome message : %s", "libsmtp.c", 73, buf);
        return -1;
    }
    ret = smtp_send_command(sock, "MAIL FROM: ", msg->from, 250);
    if (ret == -1) {
        return -1;
    }
    ret = smtp_send_command(sock, "RCPT TO: ", msg->to, 250);
    if (ret == -1) {
        return -1;
    }
    ret = smtp_send_command(sock, "DATA", ((void *)0), 354);
    if (ret == -1) {
        return -1;
    }
    ret = smtp_send_data(sock, msg);
    if (ret == -1) {
        return -1;
    }
    ret = smtp_send_command(sock, "QUIT", ((void *)0), 221);
    if (ret == -1) {
        return -1;
    }
    ret = close(sock);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 108, strerror((*__errno_location ()))); return -1; } while(0);
    }
    return 0;
}

canonical_original_code:
int libsmtp_send(char *smtp_server, struct libsmtp_msg *msg)
{
    int ret;
    int sock;
    char buf[1024];
    sock = smtp_connect(smtp_server);
    if (sock == -1) {
        return -1;
    }
    ret = recv(sock, buf, sizeof buf, 0);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 70, strerror((*__errno_location ()))); return -1; } while(0);
    }
    if (strstr(buf, "220 ") == ((void *)0)) {
        fprintf(stderr, "%s (%d) : Bad welcome message : %s", "libsmtp.c", 73, buf);
        return -1;
    }
    ret = smtp_send_command(sock, "MAIL FROM: ", msg->from, 250);
    if (ret == -1) {
        return -1;
    }
    ret = smtp_send_command(sock, "RCPT TO: ", msg->to, 250);
    if (ret == -1) {
        return -1;
    }
    ret = smtp_send_command(sock, "DATA", ((void *)0), 354);
    if (ret == -1) {
        return -1;
    }
    ret = smtp_send_data(sock, msg);
    if (ret == -1) {
        return -1;
    }
    ret = smtp_send_command(sock, "QUIT", ((void *)0), 221);
    if (ret == -1) {
        return -1;
    }
    ret = close(sock);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libsmtp.c", 108, strerror((*__errno_location ()))); return -1; } while(0);
    }
    return 0;
}

variable_types:
buf
char [1024]

user_defined_types: struct libsmtp_msg { char *to; char *from; char *subject; char *data; }


decompiled_code:
__int64 __fastcall pop3_connect(const char *a1)
{
struct hostent *v1; // rax
unsigned int v2; // er12
socklen_t v3; // edx
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
__int64 v9; // rcx
const char *v10; // r8
int *v11; // rax
char *v12; // rax
int dest; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr v14; // [rsp+10h] [rbp-18h] BYREF

v1 = gethostbyname(a1);
if ( !v1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
v9 = 148LL;
v10 = v8;
LABEL_7:
v2 = -1;
fprintf(stderr, "%s (%d) : %s\n", "libpop3.c", v9, v10);
return v2;
}
memcpy(&dest, *(const void **)v1->h_addr_list, v1->h_length);
v2 = socket(2, 1, 0);
if ( v2 == -1 )
{
v5 = _errno_location();
v6 = strerror(*v5);
fprintf(stderr, "%s (%d) : %s\n", "libpop3.c", 154LL, v6);
return v2;
}
*(_QWORD *)&v14.sa_data[6] = 0LL;
v14.sa_family = 2;
*(_WORD *)v14.sa_data = _bswap_16_constprop_0(v2, &v14, 16LL);
*(_DWORD *)&v14.sa_data[2] = dest;
if ( connect(v2, &v14, v3) == -1 )
{
v11 = _errno_location();
v12 = strerror(*v11);
v9 = 163LL;
v10 = v12;
goto LABEL_7;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
struct hostent *v1; // rax
unsigned int v2; // er12
socklen_t v3; // edx
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
__int64 v9; // rcx
const char *v10; // r8
int *v11; // rax
char *v12; // rax
int dest; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr v14; // [rsp+10h] [rbp-18h] BYREF

v1 = gethostbyname(a1);
if ( !v1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
v9 = 148LL;
v10 = v8;
LABEL_7:
v2 = -1;
fprintf(stderr, "%s (%d) : %s\n", "libpop3.c", v9, v10);
return v2;
}
memcpy(&dest, *(const void **)v1->h_addr_list, v1->h_length);
v2 = socket(2, 1, 0);
if ( v2 == -1 )
{
v5 = _errno_location();
v6 = strerror(*v5);
fprintf(stderr, "%s (%d) : %s\n", "libpop3.c", 154LL, v6);
return v2;
}
*(_QWORD *)&v14.sa_data[6] = 0LL;
v14.sa_family = 2;
*(_WORD *)v14.sa_data = func0(v2, &v14, 16LL);
*(_DWORD *)&v14.sa_data[2] = dest;
if ( connect(v2, &v14, v3) == -1 )
{
v11 = _errno_location();
v12 = strerror(*v11);
v9 = 163LL;
v10 = v12;
goto LABEL_7;
}
return v2;
}

original_code:
int pop3_connect(const char * pop3_server)
{
    struct hostent * host;
    struct in_addr ip_address;
    int sock;
    struct sockaddr_in sin;
    int ret;
    host = gethostbyname(pop3_server);
    if (host == ((void *)0)) {
        do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 148, strerror((*__errno_location ()))); return -1; } while(0);
    }
    memcpy(&ip_address, host->h_addr_list[0], host->h_length);
    sock = socket(2, SOCK_STREAM, 0);
    if (sock == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 154, strerror((*__errno_location ()))); return -1; } while(0);
    }
    memset(&sin, 0, sizeof sin);
    sin.sin_family = 2;
    sin.sin_port = htons(110);
    sin.sin_addr = ip_address;
    ret = connect(sock, (struct sockaddr *)&sin, sizeof sin);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 163, strerror((*__errno_location ()))); return -1; } while(0);
    }
    return sock;
}

canonical_original_code:
int pop3_connect(char *pop3_server)
{
    struct hostent *host;
    struct in_addr ip_address;
    int sock;
    struct sockaddr_in sin;
    int ret;
    host = gethostbyname(pop3_server);
    if (host == ((void *)0)) {
        do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 148, strerror((*__errno_location ()))); return -1; } while(0);
    }
    memcpy(&ip_address, host->h_addr_list[0], host->h_length);
    sock = socket(2, SOCK_STREAM, 0);
    if (sock == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 154, strerror((*__errno_location ()))); return -1; } while(0);
    }
    memset(&sin, 0, sizeof sin);
    sin.sin_family = 2;
    sin.sin_port = htons(110);
    sin.sin_addr = ip_address;
    ret = connect(sock, (struct sockaddr *)&sin, sizeof sin);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 163, strerror((*__errno_location ()))); return -1; } while(0);
    }
    return sock;
}

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


original_code:
int pop3_send_command(int sock, const char * cmd, const char * data,
    char ** response)
{
    int ret;
    char * buf;
    char recvbuf[1024];
    if (data != ((void *)0)) {
        buf = malloc(strlen(cmd) + strlen(data) + 3);
    }
    else {
        buf = malloc(strlen(cmd) + 3);
    }
    strcpy(buf, cmd);
    if (data != ((void *)0)) {
        strcat(buf, data);
    }
    strcat(buf, "\r\n");
    ret = send(sock, buf, strlen(buf), 0);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 195, strerror((*__errno_location ()))); return -1; } while(0);
    }
    ret = recv(sock, recvbuf, sizeof recvbuf, 0);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 200, strerror((*__errno_location ()))); return -1; } while(0);
    }
    recvbuf[ret] = '\0';
    if (strstr(recvbuf, "+OK") == ((void *)0)) {
        fprintf(stderr, "%s (%d) : Bad pop3 response : %s", "libpop3.c", 205, recvbuf);
        return -1;
    }
    if (response != ((void *)0)) {
        *response = malloc(strlen(recvbuf) + 1);
        strcpy(*response, recvbuf);
    }
    return 0;
}

canonical_original_code:
int pop3_send_command(int sock, char *cmd, char *data, char **response)
{
    int ret;
    char *buf;
    char recvbuf[1024];
    if (data != ((void *)0)) {
        buf = malloc(strlen(cmd) + strlen(data) + 3);
    }
    else {
        buf = malloc(strlen(cmd) + 3);
    }
    strcpy(buf, cmd);
    if (data != ((void *)0)) {
        strcat(buf, data);
    }
    strcat(buf, "\r\n");
    ret = send(sock, buf, strlen(buf), 0);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 195, strerror((*__errno_location ()))); return -1; } while(0);
    }
    ret = recv(sock, recvbuf, sizeof recvbuf, 0);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 200, strerror((*__errno_location ()))); return -1; } while(0);
    }
    recvbuf[ret] = '\0';
    if (strstr(recvbuf, "+OK") == ((void *)0)) {
        fprintf(stderr, "%s (%d) : Bad pop3 response : %s", "libpop3.c", 205, recvbuf);
        return -1;
    }
    if (response != ((void *)0)) {
        *response = malloc(strlen(recvbuf) + 1);
        strcpy(*response, recvbuf);
    }
    return 0;
}

variable_types:
response
char **


decompiled_code:
__int64 __fastcall pop3_pull_mail(int fd, int a2, void **a3)
{
__int64 v4; // rbp
int v5; // ebx
int v6; // er15
unsigned int v7; // eax
unsigned int v8; // er15
size_t v9; // rax
char *v10; // r15
char *v11; // rax
char **v12; // r15
void *v13; // r15
int *v15; // rax
char *v16; // rax
int v17; // [rsp+4h] [rbp-54h] BYREF
void *ptr; // [rsp+8h] [rbp-50h] BYREF
char s[68]; // [rsp+14h] [rbp-44h] BYREF

*a3 = malloc(8LL * a2);
if ( a2 <= 0 )
return 0;
v4 = 0LL;
v5 = 1;
while ( 1 )
{
sprintf(s, "%d", (unsigned int)v5);
v8 = pop3_send_command(fd, "RETR ", s, &ptr);
if ( v8 == -1 )
break;
_isoc99_sscanf(ptr, "%*s %d ", &v17);
free(ptr);
v6 = v17;
ptr = malloc(v17 + 4);
v7 = recv(fd, ptr, v6 + 3, 0);
v8 = v7;
if ( v7 == -1 )
{
v15 = _errno_location();
v16 = strerror(*v15);
fprintf(stderr, "%s (%d) : %s\n", "libpop3.c", 248LL, v16);
return v8;
}
++v5;
*((_BYTE *)ptr + (int)v7) = 0;
v9 = strlen((const char *)ptr);
v10 = (char *)*a3;
v11 = (char *)malloc(v9);
v12 = (char **)&v10[v4];
v4 += 8LL;
*v12 = v11;
v13 = ptr;
strcpy(v11, (const char *)ptr);
free(v13);
if ( a2 < v5 )
return 0;
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, int a2, void **a3)
{
__int64 v4; // rbp
int v5; // ebx
int v6; // er15
unsigned int v7; // eax
unsigned int v8; // er15
size_t v9; // rax
char *v10; // r15
char *v11; // rax
char **v12; // r15
void *v13; // r15
int *v15; // rax
char *v16; // rax
int v17; // [rsp+4h] [rbp-54h] BYREF
void *ptr; // [rsp+8h] [rbp-50h] BYREF
char s[68]; // [rsp+14h] [rbp-44h] BYREF

*a3 = malloc(8LL * a2);
if ( a2 <= 0 )
return 0;
v4 = 0LL;
v5 = 1;
while ( 1 )
{
sprintf(s, "%d", (unsigned int)v5);
v8 = func2(fd, "RETR ", s, &ptr);
if ( v8 == -1 )
break;
_isoc99_sscanf(ptr, "%*s %d ", &v17);
free(ptr);
v6 = v17;
ptr = malloc(v17 + 4);
v7 = recv(fd, ptr, v6 + 3, 0);
v8 = v7;
if ( v7 == -1 )
{
v15 = _errno_location();
v16 = strerror(*v15);
fprintf(stderr, "%s (%d) : %s\n", "libpop3.c", 248LL, v16);
return v8;
}
++v5;
*((_BYTE *)ptr + (int)v7) = 0;
v9 = strlen((const char *)ptr);
v10 = (char *)*a3;
v11 = (char *)malloc(v9);
v12 = (char **)&v10[v4];
v4 += 8LL;
*v12 = v11;
v13 = ptr;
strcpy(v11, (const char *)ptr);
free(v13);
if ( a2 < v5 )
return 0;
}
return v8;
}

original_code:
int pop3_pull_mail(int sock, int mail_count, char *** mails)
{
    int ret;
    int i;
    int msg_size;
    char buf[12];
    char * recvbuf;
    *mails = malloc(mail_count * sizeof (char *));
    for (i = 1; i < mail_count + 1; i++) {
        ret = sprintf(buf, "%d", i);
        if (ret < 0) {
            do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 234, strerror((*__errno_location ()))); return -1; } while(0);
        }
        ret = pop3_send_command(sock, "RETR ", buf, &recvbuf);
        if (ret == -1) {
            return -1;
        }
        sscanf(recvbuf, "%*s %d ", &msg_size);
        free(recvbuf);
        recvbuf = malloc(msg_size + 4);
        ret = recv(sock, recvbuf, msg_size + 3, 0);
        if (ret == -1) {
            do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 248, strerror((*__errno_location ()))); return -1; } while(0);
        }
        recvbuf[ret] = '\0';
        (*mails)[i - 1] = malloc(strlen(recvbuf));
        strcpy((*mails)[i - 1], recvbuf);
        free(recvbuf);
    }
    return 0;
}

canonical_original_code:
int pop3_pull_mail(int sock, int mail_count, char ***mails)
{
    int ret;
    int i;
    int msg_size;
    char buf[12];
    char *recvbuf;
    *mails = malloc(mail_count * sizeof (char *));
    for (i = 1; i < mail_count + 1; i++) {
        ret = sprintf(buf, "%d", i);
        if (ret < 0) {
            do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 234, strerror((*__errno_location ()))); return -1; } while(0);
        }
        ret = pop3_send_command(sock, "RETR ", buf, &recvbuf);
        if (ret == -1) {
            return -1;
        }
        sscanf(recvbuf, "%*s %d ", &msg_size);
        free(recvbuf);
        recvbuf = malloc(msg_size + 4);
        ret = recv(sock, recvbuf, msg_size + 3, 0);
        if (ret == -1) {
            do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 248, strerror((*__errno_location ()))); return -1; } while(0);
        }
        recvbuf[ret] = '\0';
        (*mails)[i - 1] = malloc(strlen(recvbuf));
        strcpy((*mails)[i - 1], recvbuf);
        free(recvbuf);
    }
    return 0;
}

variable_types:
mails
char ***

variable_types: i int

variable_types:
buf
char [12]

variable_types:
recvbuf
char *


decompiled_code:
__int64 __fastcall libpop3_recv(int *a1, void **a2, const char *a3, char *a4, char *a5)
{
int v7; // eax
int v8; // ebp
unsigned int v9; // er12
char *v10; // r12
unsigned int v11; // ebx
int *v13; // rax
char *v14; // rax
__int64 v15; // rcx
const char *v16; // r8
int *v17; // rax
char *v18; // rax
__int64 v19; // [rsp+8h] [rbp-440h] BYREF
char buf[1080]; // [rsp+10h] [rbp-438h] BYREF

v7 = pop3_connect(a3);
if ( v7 == -1 )
return (unsigned int)-1;
v8 = v7;
v9 = recv(v7, buf, 0x400uLL, 0);
if ( v9 == -1 )
{
v13 = _errno_location();
v14 = strerror(*v13);
v15 = 74LL;
v16 = v14;
goto LABEL_21;
}
if ( !strstr(buf, "+OK") )
{
fprintf(stderr, "%s (%d) : Bad welcome message : %s", "libpop3.c", 77LL, buf);
return (unsigned int)-1;
}
if ( (unsigned int)pop3_send_command(v8, "USER ", a4, 0LL) == -1
|| (unsigned int)pop3_send_command(v8, "PASS ", a5, 0LL) == -1
|| (unsigned int)pop3_send_command(v8, "STAT", 0LL, &v19) == -1 )
{
return (unsigned int)-1;
}
_isoc99_sscanf(v19, "%*s %d ", a1);
if ( *a1 > 0 )
{
if ( (unsigned int)pop3_pull_mail(v8, *a1, a2) == -1 )
return (unsigned int)-1;
v10 = (char *)malloc(0xCuLL);
if ( *a1 > 0 )
{
v11 = 1;
do
{
sprintf(v10, "%d", v11);
if ( (unsigned int)pop3_send_command(v8, "DELE ", v10, 0LL) == -1 )
return (unsigned int)-1;
}
while ( *a1 >= (int)++v11 );
}
free(v10);
}
if ( (unsigned int)pop3_send_command(v8, "QUIT", 0LL, 0LL) != -1 )
{
v9 = close(v8);
if ( v9 != -1 )
return 0;
v17 = _errno_location();
v18 = strerror(*v17);
v15 = 130LL;
v16 = v18;
LABEL_21:
fprintf(stderr, "%s (%d) : %s\n", "libpop3.c", v15, v16);
return v9;
}
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func4(int *a1, void **a2, const char *a3, char *a4, char *a5)
{
int v7; // eax
int v8; // ebp
unsigned int v9; // er12
char *v10; // r12
unsigned int v11; // ebx
int *v13; // rax
char *v14; // rax
__int64 v15; // rcx
const char *v16; // r8
int *v17; // rax
char *v18; // rax
__int64 v19; // [rsp+8h] [rbp-440h] BYREF
char buf[1080]; // [rsp+10h] [rbp-438h] BYREF

v7 = func1(a3);
if ( v7 == -1 )
return (unsigned int)-1;
v8 = v7;
v9 = recv(v7, buf, 0x400uLL, 0);
if ( v9 == -1 )
{
v13 = _errno_location();
v14 = strerror(*v13);
v15 = 74LL;
v16 = v14;
goto LABEL_21;
}
if ( !strstr(buf, "+OK") )
{
fprintf(stderr, "%s (%d) : Bad welcome message : %s", "libpop3.c", 77LL, buf);
return (unsigned int)-1;
}
if ( (unsigned int)func2(v8, "USER ", a4, 0LL) == -1
|| (unsigned int)func2(v8, "PASS ", a5, 0LL) == -1
|| (unsigned int)func2(v8, "STAT", 0LL, &v19) == -1 )
{
return (unsigned int)-1;
}
_isoc99_sscanf(v19, "%*s %d ", a1);
if ( *a1 > 0 )
{
if ( (unsigned int)func3(v8, *a1, a2) == -1 )
return (unsigned int)-1;
v10 = (char *)malloc(0xCuLL);
if ( *a1 > 0 )
{
v11 = 1;
do
{
sprintf(v10, "%d", v11);
if ( (unsigned int)func2(v8, "DELE ", v10, 0LL) == -1 )
return (unsigned int)-1;
}
while ( *a1 >= (int)++v11 );
}
free(v10);
}
if ( (unsigned int)func2(v8, "QUIT", 0LL, 0LL) != -1 )
{
v9 = close(v8);
if ( v9 != -1 )
return 0;
v17 = _errno_location();
v18 = strerror(*v17);
v15 = 130LL;
v16 = v18;
LABEL_21:
fprintf(stderr, "%s (%d) : %s\n", "libpop3.c", v15, v16);
return v9;
}
return (unsigned int)-1;
}

original_code:
int libpop3_recv(int * mail_count, char *** mails,
    const char * pop3_server, const char * user, const char * pass)
{
    int ret;
    int sock;
    char buf[1024];
    char * id;
    int i;
    char * response;
    sock = pop3_connect(pop3_server);
    if (sock == -1) {
        return -1;
    }
    ret = recv(sock, buf, sizeof buf, 0);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 74, strerror((*__errno_location ()))); return -1; } while(0);
    }
    if (strstr(buf, "+OK") == ((void *)0)) {
        fprintf(stderr, "%s (%d) : Bad welcome message : %s", "libpop3.c", 77, buf);
        return -1;
    }
    ret = pop3_send_command(sock, "USER ", user, ((void *)0));
    if (ret == -1) {
        return -1;
    }
    ret = pop3_send_command(sock, "PASS ", pass, ((void *)0));
    if (ret == -1) {
        return -1;
    }
    ret = pop3_send_command(sock, "STAT", ((void *)0), &response);
    if (ret == -1) {
        return -1;
    }
    sscanf(response, "%*s %d ", mail_count);
    if (*mail_count > 0) {
        ret = pop3_pull_mail(sock, *mail_count, mails);
        if (ret == -1) {
            return -1;
        }
        id = malloc(12);
        for (i = 1; i < (*mail_count + 1); i++) {
            sprintf(id, "%d", i);
            ret = pop3_send_command(sock, "DELE ", id, ((void *)0));
            if (ret == -1) {
                return -1;
            }
        }
        free(id);
    }
    ret = pop3_send_command(sock, "QUIT", ((void *)0), ((void *)0));
    if (ret == -1) {
        return -1;
    }
    ret = close(sock);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 130, strerror((*__errno_location ()))); return -1; } while(0);
    }
    return 0;
}

canonical_original_code:
int libpop3_recv(int *mail_count, char ***mails, char *pop3_server, char *user, char *pass)
{
    int ret;
    int sock;
    char buf[1024];
    char *id;
    int i;
    char *response;
    sock = pop3_connect(pop3_server);
    if (sock == -1) {
        return -1;
    }
    ret = recv(sock, buf, sizeof buf, 0);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 74, strerror((*__errno_location ()))); return -1; } while(0);
    }
    if (strstr(buf, "+OK") == ((void *)0)) {
        fprintf(stderr, "%s (%d) : Bad welcome message : %s", "libpop3.c", 77, buf);
        return -1;
    }
    ret = pop3_send_command(sock, "USER ", user, ((void *)0));
    if (ret == -1) {
        return -1;
    }
    ret = pop3_send_command(sock, "PASS ", pass, ((void *)0));
    if (ret == -1) {
        return -1;
    }
    ret = pop3_send_command(sock, "STAT", ((void *)0), &response);
    if (ret == -1) {
        return -1;
    }
    sscanf(response, "%*s %d ", mail_count);
    if (*mail_count > 0) {
        ret = pop3_pull_mail(sock, *mail_count, mails);
        if (ret == -1) {
            return -1;
        }
        id = malloc(12);
        for (i = 1; i < (*mail_count + 1); i++) {
            sprintf(id, "%d", i);
            ret = pop3_send_command(sock, "DELE ", id, ((void *)0));
            if (ret == -1) {
                return -1;
            }
        }
        free(id);
    }
    ret = pop3_send_command(sock, "QUIT", ((void *)0), ((void *)0));
    if (ret == -1) {
        return -1;
    }
    ret = close(sock);
    if (ret == -1) {
        do { fprintf(stderr, "%s (%d) : %s\n","libpop3.c", 130, strerror((*__errno_location ()))); return -1; } while(0);
    }
    return 0;
}

variable_types:
user
char *

variable_types:
pass
char *

variable_types:
id
char *

variable_types:
response
char *


original_code:
int git_oid_mkstr(git_oid *out, const char *str)
{
 int p;
 for (p = 0; p < sizeof(out->id); p++, str += 2) {
  int v = (from_hex[(unsigned char)str[0]] << 4)
         | from_hex[(unsigned char)str[1]];
  if (v < 0)
   return (-1 - 1);
  out->id[p] = (unsigned char)v;
 }
 return 0;
}

canonical_original_code:
int git_oid_mkstr(struct git_oid *out, char *str)
{
 int p;
 for (p = 0; p < sizeof(out->id); p++, str += 2) {
  int v = (from_hex[(unsigned char)str[0]] << 4)
         | from_hex[(unsigned char)str[1]];
  if (v < 0)
   return (-1 - 1);
  out->id[p] = (unsigned char)v;
 }
 return 0;
}

variable_types: struct git_oid { unsigned char id[20]; } *

variable_types:
str
char *

variable_types: p int

variable_types: v int


original_code:
int git_oid_fill_hex(git_oid *oid, char *str)
{
 const unsigned char *sha1 = oid->id;
 int i;
 for (i = 0; i < 20; i++) {
  static char hex[] = "0123456789abcdef";
  unsigned int val = sha1[i];
  *str++ = hex[val >> 4];
  *str++ = hex[val & 0xf];
 }
 return 0;
}

canonical_original_code:
int git_oid_fill_hex(struct git_oid *oid, char *str)
{
 unsigned char *sha1 = oid->id;
 int i;
 for (i = 0; i < 20; i++) {
  static char hex[] = "0123456789abcdef";
  unsigned int val = sha1[i];
  *str++ = hex[val >> 4];
  *str++ = hex[val & 0xf];
 }
 return 0;
}

variable_types:
hex
char []

variable_types:
val
unsigned int

user_defined_types: struct git_oid { unsigned char id[20]; }


decompiled_code:
__int64 __fastcall git_commit_id(__int64 a1)
{
return a1;
}

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

original_code:
const git_oid *git_commit_id(git_commit *c)
{
 return &c->id;
}

canonical_original_code:
struct git_oid *git_commit_id(struct git_commit *c)
{
 return &c->id;
}

variable_types: struct git_commit { struct git_oid id; long commit_time; unsigned int parsed; } *

return_type: struct git_oid { unsigned char id[20]; } *


decompiled_code:
__int64 __fastcall git_fopen(int *a1, const char *a2, int a3)
{
int v3; // eax

v3 = open64(a2, a3);
if ( v3 < 0 )
return 0xFFFFFFFFLL;
*a1 = v3;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int *a1, const char *a2, int a3)
{
int v3; // eax

v3 = open64(a2, a3);
if ( v3 < 0 )
return 0xFFFFFFFFLL;
*a1 = v3;
return 0LL;
}

original_code:
int git_fopen(git_file *out, const char *path, int flags)
{
 int r = open(path, flags);
 if (r < 0)
  return -1;
 *out = r;
 return 0;
}

canonical_original_code:
int git_fopen(int *out, char *path, int flags)
{
 int r = open(path, flags);
 if (r < 0)
  return -1;
 *out = r;
 return 0;
}

variable_types:
out
int *

variable_types:
path
char *

variable_types: flags int

variable_types: r int


decompiled_code:
__int64 __fastcall git_fread(int fd, void *buf, size_t nbytes)
{
size_t v4; // rbx
ssize_t v5; // rax
int v7; // eax

v4 = nbytes;
if ( !nbytes )
return 0LL;
while ( 1 )
{
while ( 1 )
{
v5 = read(fd, buf, v4);
if ( v5 >= 0 )
break;
v7 = *_errno_location();
if ( v7 != 4 && v7 != 11 )
return 0xFFFFFFFFLL;
}
if ( !v5 )
break;
buf = (char *)buf + v5;
v4 -= v5;
if ( !v4 )
return 0LL;
}
*_errno_location() = 32;
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, void *buf, size_t nbytes)
{
size_t v4; // rbx
ssize_t v5; // rax
int v7; // eax

v4 = nbytes;
if ( !nbytes )
return 0LL;
while ( 1 )
{
while ( 1 )
{
v5 = read(fd, buf, v4);
if ( v5 >= 0 )
break;
v7 = *_errno_location();
if ( v7 != 4 && v7 != 11 )
return 0xFFFFFFFFLL;
}
if ( !v5 )
break;
buf = (char *)buf + v5;
v4 -= v5;
if ( !v4 )
return 0LL;
}
*_errno_location() = 32;
return 0xFFFFFFFFLL;
}

original_code:
int git_fread(git_file fd, void *buf, size_t cnt)
{
 char *b = buf;
 while (cnt) {
  ssize_t r = read(fd, b, cnt);
  if (r < 0) {
   if ((*__errno_location ()) == 4 || (*__errno_location ()) == 11)
    continue;
   return -1;
  }
  if (!r) {
   (*__errno_location ()) = 32;
   return -1;
  }
  cnt -= r;
  b += r;
 }
 return 0;
}

canonical_original_code:
int git_fread(int fd, void *buf, unsigned long cnt)
{
 char *b = buf;
 while (cnt) {
  long r = read(fd, b, cnt);
  if (r < 0) {
   if ((*__errno_location ()) == 4 || (*__errno_location ()) == 11)
    continue;
   return -1;
  }
  if (!r) {
   (*__errno_location ()) = 32;
   return -1;
  }
  cnt -= r;
  b += r;
 }
 return 0;
}

variable_types: fd int

variable_types:
buf
void *

variable_types:
cnt
unsigned long

variable_types:
b
char *

variable_types: r long


decompiled_code:
__int64 __fastcall git_fwrite(int fd, void *buf, size_t n)
{
size_t v4; // rbx
ssize_t v5; // rax
int v7; // eax

v4 = n;
if ( !n )
return 0LL;
while ( 1 )
{
while ( 1 )
{
v5 = write(fd, buf, v4);
if ( v5 >= 0 )
break;
v7 = *_errno_location();
if ( v7 != 4 && v7 != 11 )
return 0xFFFFFFFFLL;
}
if ( !v5 )
break;
buf = (char *)buf + v5;
v4 -= v5;
if ( !v4 )
return 0LL;
}
*_errno_location() = 32;
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, void *buf, size_t n)
{
size_t v4; // rbx
ssize_t v5; // rax
int v7; // eax

v4 = n;
if ( !n )
return 0LL;
while ( 1 )
{
while ( 1 )
{
v5 = write(fd, buf, v4);
if ( v5 >= 0 )
break;
v7 = *_errno_location();
if ( v7 != 4 && v7 != 11 )
return 0xFFFFFFFFLL;
}
if ( !v5 )
break;
buf = (char *)buf + v5;
v4 -= v5;
if ( !v4 )
return 0LL;
}
*_errno_location() = 32;
return 0xFFFFFFFFLL;
}

original_code:
int git_fwrite(git_file fd, void *buf, size_t cnt)
{
 char *b = buf;
 while (cnt) {
  ssize_t r = write(fd, b, cnt);
  if (r < 0) {
   if ((*__errno_location ()) == 4 || (*__errno_location ()) == 11)
    continue;
   return -1;
  }
  if (!r) {
   (*__errno_location ()) = 32;
   return -1;
  }
  cnt -= r;
  b += r;
 }
 return 0;
}

canonical_original_code:
int git_fwrite(int fd, void *buf, unsigned long cnt)
{
 char *b = buf;
 while (cnt) {
  long r = write(fd, b, cnt);
  if (r < 0) {
   if ((*__errno_location ()) == 4 || (*__errno_location ()) == 11)
    continue;
   return -1;
  }
  if (!r) {
   (*__errno_location ()) = 32;
   return -1;
  }
  cnt -= r;
  b += r;
 }
 return 0;
}


decompiled_code:
__off_t __fastcall git_fsize(int a1)
{
__off_t result; // rax
struct stat64 v2; // [rsp+0h] [rbp-98h] BYREF

if ( fstat64(a1, &v2) )
result = -1LL;
else
result = v2.st_size;
return result;
}

canonical_decompiled_code:
__off_t __fastcall func3(int a1)
{
__off_t result; // rax
struct stat64 v2; // [rsp+0h] [rbp-98h] BYREF

if ( fstat64(a1, &v2) )
result = -1LL;
else
result = v2.st_size;
return result;
}

original_code:
off_t git_fsize(git_file fd)
{
 struct stat sb;
 if (fstat(fd, &sb))
  return -1;
 return sb.st_size;
}

canonical_original_code:
long git_fsize(int fd)
{
 struct stat sb;
 if (fstat(fd, &sb))
  return -1;
 return sb.st_size;
}

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

return_type: long

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


decompiled_code:
__int64 __fastcall open_alternates(__int64 a1)
{
_QWORD *v1; // rax

v1 = malloc(8uLL);
*(_QWORD *)(a1 + 8) = v1;
if ( !v1 )
return 0xFFFFFFFFLL;
*v1 = 0LL;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
_QWORD *v1; // rax

v1 = malloc(8uLL);
*(_QWORD *)(a1 + 8) = v1;
if ( !v1 )
return 0xFFFFFFFFLL;
*v1 = 0LL;
return 0LL;
}

original_code:
static int open_alternates(git_odb *db)
{
 unsigned n = 0;
 db->alternates = malloc(sizeof(*db->alternates) * (n + 1));
 if (!db->alternates)
  return -1;
 db->alternates[n] = ((void *)0);
 return 0;
}

canonical_original_code:
static int open_alternates(struct git_odb *db)
{
 unsigned int n = 0;
 db->alternates = malloc(sizeof(*db->alternates) * (n + 1));
 if (!db->alternates)
  return -1;
 db->alternates[n] = ((void *)0);
 return 0;
}

variable_types: struct git_odb { char *path; struct git_odb **alternates; } *

variable_types:
n
unsigned int


decompiled_code:
__int64 __fastcall git_odb_open(char ***a1, const char *a2)
{
char **v2; // rax
char **v3; // rbp
char *v4; // rax
__int64 result; // rax

v2 = (char **)malloc(0x10uLL);
if ( !v2 )
return 0xFFFFFFFFLL;
v3 = v2;
v4 = strdup(a2);
*v3 = v4;
if ( v4 )
{
v3[1] = 0LL;
result = 0LL;
*a1 = v3;
}
else
{
free(v3);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(char ***a1, const char *a2)
{
char **v2; // rax
char **v3; // rbp
char *v4; // rax
__int64 result; // rax

v2 = (char **)malloc(0x10uLL);
if ( !v2 )
return 0xFFFFFFFFLL;
v3 = v2;
v4 = strdup(a2);
*v3 = v4;
if ( v4 )
{
v3[1] = 0LL;
result = 0LL;
*a1 = v3;
}
else
{
free(v3);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int git_odb_open(git_odb **out, const char *objects_dir)
{
 git_odb *db = malloc(sizeof(*db));
 if (!db)
  return -1;
 db->path = strdup(objects_dir);
 if (!db->path) {
  free(db);
  return -1;
 }
 db->alternates = ((void *)0);
 *out = db;
 return 0;
}

canonical_original_code:
int git_odb_open(struct git_odb **out, char *objects_dir)
{
 struct git_odb *db = malloc(sizeof(*db));
 if (!db)
  return -1;
 db->path = strdup(objects_dir);
 if (!db->path) {
  free(db);
  return -1;
 }
 db->alternates = ((void *)0);
 *out = db;
 return 0;
}

variable_types: struct git_odb { char *path; struct git_odb **alternates; } **


original_code:
void git_odb_close(git_odb *db)
{
 if (!db)
  return;
 if (db->alternates) {
  git_odb **alt;
  for (alt = db->alternates; *alt; alt++)
   git_odb_close(*alt);
  free(db->alternates);
 }
 free(db->path);
 free(db);
}

canonical_original_code:
void git_odb_close(struct git_odb *db)
{
 if (!db)
  return;
 if (db->alternates) {
  struct git_odb **alt;
  for (alt = db->alternates; *alt; alt++)
   git_odb_close(*alt);
  free(db->alternates);
 }
 free(db->path);
 free(db);
}

user_defined_types: struct git_odb { char *path; struct git_odb **alternates; }


decompiled_code:
__int64 __fastcall git_odb_read(_QWORD *a1, __int64 a2, __int64 a3)
{
do
{
if ( !(unsigned int)git_odb__read_packed(a1, a2, a3) || !(unsigned int)git_odb__read_loose(a1, a2, a3) )
return 0LL;
}
while ( !*(_QWORD *)(a2 + 8) && !(unsigned int)open_alternates(a2) );
*a1 = 0LL;
return 4294967293LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1, __int64 a2, __int64 a3)
{
do
{
if ( !(unsigned int)git_odb__read_packed(a1, a2, a3) || !(unsigned int)git_odb__read_loose(a1, a2, a3) )
return 0LL;
}
while ( !*(_QWORD *)(a2 + 8) && !(unsigned int)func0(a2) );
*a1 = 0LL;
return 4294967293LL;
}

original_code:
int git_odb_read(
 git_obj *out,
 git_odb *db,
 const git_oid *id)
{
attempt:
 if (!git_odb__read_packed(out, db, id))
  return 0;
 if (!git_odb__read_loose(out, db, id))
  return 0;
 if (!db->alternates && !open_alternates(db))
  goto attempt;
 out->data = ((void *)0);
 return (-1 - 2);
}

canonical_original_code:
int git_odb_read(struct git_obj *out, struct git_odb *db, struct git_oid *id)
{
attempt:
 if (!git_odb__read_packed(out, db, id))
  return 0;
 if (!git_odb__read_loose(out, db, id))
  return 0;
 if (!db->alternates && !open_alternates(db))
  goto attempt;
 out->data = ((void *)0);
 return (-1 - 2);
}

variable_types: struct git_obj { void *data; unsigned long len; enum git_otype { GIT_OBJ_BAD=-1, GIT_OBJ__EXT1=0, GIT_OBJ_COMMIT=1, GIT_OBJ_TREE=2, GIT_OBJ_BLOB=3, GIT_OBJ_TAG=4, GIT_OBJ__EXT2=5, GIT_OBJ_OFS_DELTA=6, GIT_OBJ_REF_DELTA=7 } type; } *

variable_types: struct git_oid { unsigned char id[20]; } *

user_defined_types: struct git_obj { void *data; unsigned long len; enum git_otype type; }


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

if ( a1 == -2 )
return "Not a git oid";
result = "Unknown error";
if ( a1 == -3 )
result = "Object does not exist in the scope searched";
return result;
}

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

if ( a1 == -2 )
return "Not a git oid";
result = "Unknown error";
if ( a1 == -3 )
result = "Object does not exist in the scope searched";
return result;
}

original_code:
const char *git_strerror(int num)
{
 int i;
 for (i = 0; i < (sizeof(error_codes)/sizeof(error_codes[0])); i++)
  if (num == error_codes[i].num)
   return error_codes[i].str;
 return "Unknown error";
}

variable_types: num int

variable_types: i int

return_type: char *


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

result = sysconf(84);
if ( result <= 0 )
result = 1LL;
return result;
}

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

result = sysconf(84);
if ( result <= 0 )
result = 1LL;
return result;
}

original_code:
int git_online_cpus(void)
{
 long ncpus;
 if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0)
  return (int)ncpus;
 return 1;
}

canonical_original_code:
int git_online_cpus()
{
 long ncpus;
 if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0)
  return (int)ncpus;
 return 1;
}

variable_types: ncpus long


decompiled_code:
__int64 __fastcall get_spi_control_byte(int a1, char a2)
{
return a1 | (2 * a2) & 0xE | 0x40u;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, char a2)
{
return a1 | (2 * a2) & 0xE | 0x40u;
}

original_code:
static uint8_t get_spi_control_byte(uint8_t rw_cmd, uint8_t hw_addr)
{
    hw_addr = (hw_addr << 1) & 0xE;
    rw_cmd &= 1;
    return 0x40 | hw_addr | rw_cmd;
}

canonical_original_code:
static unsigned char get_spi_control_byte(unsigned char rw_cmd, unsigned char hw_addr)
{
    hw_addr = (hw_addr << 1) & 0xE;
    rw_cmd &= 1;
    return 0x40 | hw_addr | rw_cmd;
}

return_type: unsigned char