tightloop

decompiled_code:
__int64 tightloop()
{
__int64 result; // rax
__int64 v1; // rdx

for ( result = 0LL; result != 400000000; ++result )
{
v1 = result + counter;
counter = v1;
}
return result;
}

canonical_decompiled_code:
__int64 func6()
{
__int64 result; // rax
__int64 v1; // rdx

for ( result = 0LL; result != 400000000; ++result )
{
v1 = result + counter;
counter = v1;
}
return result;
}

original_code:
void tightloop() {
  unsigned j;
  for (j = 0; j < N; ++j) {
    counter += j;
  }
}

canonical_original_code:
void tightloop() {
  unsigned int j;
  for (j = 0; j < N; ++j) {
    counter += j;
  }
}

variable_types:
j
unsigned int


foo

repo: rbanffy/loop-anomaly-benchmark

decompiled_code:
void foo()
{
;
}

canonical_decompiled_code:
void func7()
{
;
}

original_code:
void foo() {
}

canonical_original_code:
void foo() {
}

return_type: void


decompiled_code:
__int64 loop_with_extra_call()
{
__int64 v0; // rax
__int64 v1; // rdx
__int64 result; // rax

do
{
foo();
v1 = v0 + counter;
result = v0 + 1;
counter = v1;
}
while ( result != 400000000 );
return result;
}

canonical_decompiled_code:
__int64 func8()
{
__int64 v0; // rax
__int64 v1; // rdx
__int64 result; // rax

do
{
func7();
v1 = v0 + counter;
result = v0 + 1;
counter = v1;
}
while ( result != 400000000 );
return result;
}

original_code:
void loop_with_extra_call() {
  unsigned j;
  for (j = 0; j < N; ++j) {
    __asm__("call foo");
    counter += j;
  }
}

canonical_original_code:
void loop_with_extra_call() {
  unsigned int j;
  for (j = 0; j < N; ++j) {
    __asm__("call foo");
    counter += j;
  }
}


drawImg

repo: rbryan/psychic-dangerzone

decompiled_code:
__int64 drawImg()
{
glBindTexture();
glBegin();
glColor3f();
glTexCoord2f();
glVertex3f();
glColor3f();
glTexCoord2f();
glVertex3f();
glColor3f();
glTexCoord2f();
glVertex3f();
glColor3f();
glTexCoord2f();
glVertex3f();
glColor3f();
glTexCoord2f();
glVertex3f();
glColor3f();
glTexCoord2f();
glVertex3f();
return glEnd();
}

canonical_decompiled_code:
__int64 func7()
{
glBindTexture();
glBegin();
glColor3f();
glTexCoord2f();
glVertex3f();
glColor3f();
glTexCoord2f();
glVertex3f();
glColor3f();
glTexCoord2f();
glVertex3f();
glColor3f();
glTexCoord2f();
glVertex3f();
glColor3f();
glTexCoord2f();
glVertex3f();
glColor3f();
glTexCoord2f();
glVertex3f();
return glEnd();
}

original_code:
void drawImg(int X, int Y, int i){
 double delta = 2.0/5;
 double x = X*delta-1;
 double y = Y*delta-1;
 glBindTexture(0x0DE1, i);
 glBegin(0x0004);
 glColor3f(1, 1, 1);
 glTexCoord2f(0,1);
 glVertex3f(x, y, 0);
 glColor3f(1, 1, 1);
 glTexCoord2f(0,0);
 glVertex3f(x, y+delta, 0);
 glColor3f(1, 1, 1);
 glTexCoord2f(1,0);
 glVertex3f(x+delta, y+delta, 0);
 glColor3f(1, 1, 1);
 glTexCoord2f(0,.999);
 glVertex3f(x, y, 0);
 glColor3f(1, 1, 1);
 glTexCoord2f(1,.999);
 glVertex3f(x+delta, y, 0);
 glColor3f(1, 1, 1);
 glTexCoord2f(1,0);
 glVertex3f(x+delta, y+delta, 0);
 glEnd();
}

variable_types: X int

variable_types: Y int

variable_types: i int

variable_types: delta double

variable_types: x double

variable_types: y double


main

canonical_name: main

repo: rbryan/raytracer

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

v3 = 1;
if ( !(unsigned int)renderframe(argc, argv, envp) )
{
v3 = 0;
printf("This clearly did not work. Could not renderframe().");
}
return v3;
}

original_code:
int main(int argc, char *argv[]){
 if(renderframe()){
  return 1;
 }
 else{
  printf("This clearly did not work. Could not renderframe().");
  return 0;
 }
}

variable_types: argc int

variable_types:
argv
char *[]

return_type: int


dotproduct

decompiled_code:
double __fastcall dotproduct(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14)
{
return a9 * a12 + a10 * a13 + a11 * a14;
}

canonical_decompiled_code:
double __fastcall func0(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14)
{
return a9 * a12 + a10 * a13 + a11 * a14;
}

original_code:
double dotproduct(vector a, vector b){
 return (a.x*b.x+a.y*b.y+a.z*b.z);
}

canonical_original_code:
double dotproduct(struct vector a, struct vector b){
 return (a.x*b.x+a.y*b.y+a.z*b.z);
}

variable_types: struct vector { double x; double y; double z; }

variable_types:
b
struct vector

return_type: double


original_code:
int in_angle(vector vec1 , vector vec2 , vector point1 , vector point2, vector point3, vector intersect){
 dot_product = dotproduct(vec1,vec2);
 dist1_2 = sqrt( pow(point2.x-point1.x,2) + pow(point2.y-point1.y,2) + pow(point2.z-point1.z,2));
 dist1_3 = sqrt( pow(point3.x-point1.x,2) + pow(point3.y-point1.y,2) + pow(point3.z-point1.z,2));
 dist1_inter = sqrt( pow(intersect.x-point1.x,2) + pow(intersect.y-point1.y,2) + pow(intersect.z-point1.z,2));
 vec1_inter.x = intersect.x - point1.x;
 vec1_inter.y = intersect.y - point1.y;
 vec1_inter.z = intersect.z - point1.z;
 theta = acos(dot_product/(dist1_2*dist1_3));
 theta_intersect = acos(dotproduct(vec1_inter,vec1)/(dist1_inter*dist1_2));
 theta_intersect2 = acos(dotproduct(vec1_inter,vec2)/(dist1_inter*dist1_3));
 if(theta >= theta_intersect && theta >= theta_intersect2){
  return 1;
 }
 return 0;
}

canonical_original_code:
int in_angle(struct vector vec1, struct vector vec2, struct vector point1, struct vector point2, struct vector point3, struct vector intersect){
 dot_product = dotproduct(vec1,vec2);
 dist1_2 = sqrt( pow(point2.x-point1.x,2) + pow(point2.y-point1.y,2) + pow(point2.z-point1.z,2));
 dist1_3 = sqrt( pow(point3.x-point1.x,2) + pow(point3.y-point1.y,2) + pow(point3.z-point1.z,2));
 dist1_inter = sqrt( pow(intersect.x-point1.x,2) + pow(intersect.y-point1.y,2) + pow(intersect.z-point1.z,2));
 vec1_inter.x = intersect.x - point1.x;
 vec1_inter.y = intersect.y - point1.y;
 vec1_inter.z = intersect.z - point1.z;
 theta = acos(dot_product/(dist1_2*dist1_3));
 theta_intersect = acos(dotproduct(vec1_inter,vec1)/(dist1_inter*dist1_2));
 theta_intersect2 = acos(dotproduct(vec1_inter,vec2)/(dist1_inter*dist1_3));
 if(theta >= theta_intersect && theta >= theta_intersect2){
  return 1;
 }
 return 0;
}

variable_types:
intersect
struct vector

user_defined_types: struct vector { double x; double y; double z; }


repo: rbu/driftnet

decompiled_code:
__int64 __fastcall audiochunk_write(char **a1, int a2)
{
char *v2; // rax
char *v3; // rbx
unsigned __int64 v4; // rax
size_t v5; // r14

v2 = a1[1];
if ( !v2 )
return 0LL;
v3 = *a1;
v4 = (unsigned __int64)&v2[(_QWORD)*a1];
do
{
v5 = v4 - (_QWORD)v3;
if ( v4 >= (unsigned __int64)(v3 + 1024) )
v5 = 1024LL;
if ( write(a2, v3, v5) == -1 && *_errno_location() != 4 )
return 0xFFFFFFFFLL;
v3 += v5;
v4 = (unsigned __int64)&a1[1][(_QWORD)*a1];
}
while ( (unsigned __int64)v3 < v4 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(char **a1, int a2)
{
char *v2; // rax
char *v3; // rbx
unsigned __int64 v4; // rax
size_t v5; // r14

v2 = a1[1];
if ( !v2 )
return 0LL;
v3 = *a1;
v4 = (unsigned __int64)&v2[(_QWORD)*a1];
do
{
v5 = v4 - (_QWORD)v3;
if ( v4 >= (unsigned __int64)(v3 + 1024) )
v5 = 1024LL;
if ( write(a2, v3, v5) == -1 && *_errno_location() != 4 )
return 0xFFFFFFFFLL;
v3 += v5;
v4 = (unsigned __int64)&a1[1][(_QWORD)*a1];
}
while ( (unsigned __int64)v3 < v4 );
return 0LL;
}

original_code:
static int audiochunk_write(const audiochunk A, int fd) {
    const unsigned char *p;
    ssize_t n;
    if (A->len == 0)
        return 0;
    p = A->data;
    do {
        size_t d = 1024;
        if (p + d > A->data + A->len)
            d = A->data + A->len - p;
        n = write(fd, p, d);
        if (n == -1 && (*__errno_location ()) != 4)
            return -1;
        else
            p += d;
    } while (p < A->data + A->len);
    return 0;
}

canonical_original_code:
static int audiochunk_write(struct _audiochunk *A, int fd) {
    unsigned char *p;
    long n;
    if (A->len == 0)
        return 0;
    p = A->data;
    do {
        unsigned long d = 1024;
        if (p + d > A->data + A->len)
            d = A->data + A->len - p;
        n = write(fd, p, d);
        if (n == -1 && (*__errno_location ()) != 4)
            return -1;
        else
            p += d;
    } while (p < A->data + A->len);
    return 0;
}

variable_types: struct _audiochunk { unsigned char *data; unsigned long len; struct _audiochunk *next; } *

variable_types: fd int

variable_types:
p
unsigned char *

variable_types: n long

variable_types:
d
unsigned long


decompiled_code:
__int64 __fastcall audiochunk_delete(_QWORD *a1)
{
xfree(*a1);
return xfree(a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1)
{
xfree(*a1);
return xfree(a1);
}

original_code:
static void audiochunk_delete(audiochunk A) {
    xfree(A->data);
    xfree(A);
}

canonical_original_code:
static void audiochunk_delete(struct _audiochunk *A) {
    xfree(A->data);
    xfree(A);
}

user_defined_types: struct _audiochunk { unsigned char *data; unsigned long len; struct _audiochunk *next; }


decompiled_code:
void __fastcall __noreturn mpeg_play(void *a1)
{
__int64 v1; // rbx
int *v2; // rax
char *v3; // rax
struct timespec v4; // [rsp+0h] [rbp-18h] BYREF

while ( 1 )
{
while ( 1 )
{
pthread_mutex_lock(&mpeg_mtx);
v1 = *((_QWORD *)rd + 2);
pthread_mutex_unlock(&mpeg_mtx);
if ( v1 )
break;
v4.tv_sec = 0LL;
v4.tv_nsec = 100000000LL;
nanosleep(&v4, 0LL);
}
if ( (unsigned int)audiochunk_write((char **)v1, mpeg_fd) == -1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
fprintf(stderr, "driftnet: write to MPEG player: %s\n", v3);
}
pthread_mutex_lock(&mpeg_mtx);
buffered -= *(_QWORD *)(v1 + 8);
audiochunk_delete(rd);
rd = (void *)v1;
pthread_mutex_unlock(&mpeg_mtx);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func2(void *a1)
{
__int64 v1; // rbx
int *v2; // rax
char *v3; // rax
struct timespec v4; // [rsp+0h] [rbp-18h] BYREF

while ( 1 )
{
while ( 1 )
{
pthread_mutex_lock(&mpeg_mtx);
v1 = *((_QWORD *)rd + 2);
pthread_mutex_unlock(&mpeg_mtx);
if ( v1 )
break;
v4.tv_sec = 0LL;
v4.tv_nsec = 100000000LL;
nanosleep(&v4, 0LL);
}
if ( (unsigned int)func0((char **)v1, mpeg_fd) == -1 )
{
v2 = _errno_location();
v3 = strerror(*v2);
fprintf(stderr, "driftnet: write to MPEG player: %s\n", v3);
}
pthread_mutex_lock(&mpeg_mtx);
buffered -= *(_QWORD *)(v1 + 8);
func1(rd);
rd = (void *)v1;
pthread_mutex_unlock(&mpeg_mtx);
}
}

original_code:
static void *mpeg_play(void *a) {
    audiochunk A;
    A = (audiochunk)a;
    while (1) {
        audiochunk A;
        pthread_mutex_lock(&mpeg_mtx);
        A = rd->next;
        pthread_mutex_unlock(&mpeg_mtx);
        if (A) {
            if (audiochunk_write(A, mpeg_fd) == -1)
                fprintf(stderr, "driftnet"": write to MPEG player: %s\n", strerror((*__errno_location ())));
            pthread_mutex_lock(&mpeg_mtx);
            buffered -= A->len;
            audiochunk_delete(rd);
            rd = A;
            pthread_mutex_unlock(&mpeg_mtx);
        } else {
            struct timespec tm;
            tm.tv_sec = 0;
            tm.tv_nsec = 100000000;
            nanosleep(&tm, ((void *)0));
        }
    }
    return ((void *)0);
}

canonical_original_code:
static void *mpeg_play(void *a) {
    struct _audiochunk *A;
    A = (struct _audiochunk *)a;
    while (1) {
        struct _audiochunk *A;
        pthread_mutex_lock(&mpeg_mtx);
        A = rd->next;
        pthread_mutex_unlock(&mpeg_mtx);
        if (A) {
            if (audiochunk_write(A, mpeg_fd) == -1)
                fprintf(stderr, "driftnet"": write to MPEG player: %s\n", strerror((*__errno_location ())));
            pthread_mutex_lock(&mpeg_mtx);
            buffered -= A->len;
            audiochunk_delete(rd);
            rd = A;
            pthread_mutex_unlock(&mpeg_mtx);
        } else {
            struct timespec tm;
            tm.tv_sec = 0;
            tm.tv_nsec = 100000000;
            nanosleep(&tm, ((void *)0));
        }
    }
    return ((void *)0);
}

variable_types:
a
void *

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

return_type: void *


decompiled_code:
int mpeg_player_manager()
{
int result; // eax
time_t v1; // rbx
unsigned int v2; // eax
__pid_t v3; // ebp
int v4; // eax
int *v5; // rax
char *v6; // rdx
int *v7; // rax
char *v8; // rdx
int stat_loc; // [rsp+Ch] [rbp-BCh] BYREF
struct sigaction v10; // [rsp+10h] [rbp-B8h] BYREF

memset(&v10, 0, sizeof(v10));
result = sigaction(17, &v10, 0LL);
if ( !foad )
{
while ( 1 )
{
fprintf(stderr, "driftnet: starting MPEG player `%s'\n", audio_mpeg_player);
v1 = time(0LL);
v2 = fork();
v3 = v2;
if ( v2 == -1 )
break;
if ( !v2 )
{
execl("/bin/sh", "/bin/sh", "-c", audio_mpeg_player, 0LL);
v7 = _errno_location();
v8 = strerror(*v7);
fprintf(stderr, "driftnet: exec: %s\n", v8);
LABEL_18:
exit(-1);
}
if ( verbose )
fprintf(stderr, "driftnet: MPEG player has PID %d\n", v2);
waitpid(v3, &stat_loc, 0);
if ( verbose )
{
if ( (stat_loc & 0x7F) != 0 )
{
if ( (char)((stat_loc & 0x7F) + 1) > 1 )
fprintf(stderr, "driftnet: MPEG player killed by signal %d\n", stat_loc & 0x7F);
}
else
{
fprintf(stderr, "driftnet: MPEG player exited with status %d\n", BYTE1(stat_loc));
}
}
result = foad;
if ( !foad )
{
if ( time(0LL) - v1 <= 4 )
{
v4 = time(0LL);
fprintf(
stderr,
"driftnet: MPEG player expired after %d seconds, sleeping for a bit\n",
(unsigned int)(v4 - v1));
sleep(5u);
}
result = foad;
if ( !foad )
continue;
}
return result;
}
v5 = _errno_location();
v6 = strerror(*v5);
fprintf(stderr, "driftnet: fork: %s\n", v6);
goto LABEL_18;
}
return result;
}

canonical_decompiled_code:
int func3()
{
int result; // eax
time_t v1; // rbx
unsigned int v2; // eax
__pid_t v3; // ebp
int v4; // eax
int *v5; // rax
char *v6; // rdx
int *v7; // rax
char *v8; // rdx
int stat_loc; // [rsp+Ch] [rbp-BCh] BYREF
struct sigaction v10; // [rsp+10h] [rbp-B8h] BYREF

memset(&v10, 0, sizeof(v10));
result = sigaction(17, &v10, 0LL);
if ( !foad )
{
while ( 1 )
{
fprintf(stderr, "driftnet: starting MPEG player `%s'\n", audio_mpeg_player);
v1 = time(0LL);
v2 = fork();
v3 = v2;
if ( v2 == -1 )
break;
if ( !v2 )
{
execl("/bin/sh", "/bin/sh", "-c", audio_mpeg_player, 0LL);
v7 = _errno_location();
v8 = strerror(*v7);
fprintf(stderr, "driftnet: exec: %s\n", v8);
LABEL_18:
exit(-1);
}
if ( verbose )
fprintf(stderr, "driftnet: MPEG player has PID %d\n", v2);
waitpid(v3, &stat_loc, 0);
if ( verbose )
{
if ( (stat_loc & 0x7F) != 0 )
{
if ( (char)((stat_loc & 0x7F) + 1) > 1 )
fprintf(stderr, "driftnet: MPEG player killed by signal %d\n", stat_loc & 0x7F);
}
else
{
fprintf(stderr, "driftnet: MPEG player exited with status %d\n", BYTE1(stat_loc));
}
}
result = foad;
if ( !foad )
{
if ( time(0LL) - v1 <= 4 )
{
v4 = time(0LL);
fprintf(
stderr,
"driftnet: MPEG player expired after %d seconds, sleeping for a bit\n",
(unsigned int)(v4 - v1));
sleep(5u);
}
result = foad;
if ( !foad )
continue;
}
return result;
}
v5 = _errno_location();
v6 = strerror(*v5);
fprintf(stderr, "driftnet: fork: %s\n", v6);
goto LABEL_18;
}
return result;
}

original_code:
static void mpeg_player_manager(void) {
    extern sig_atomic_t foad;
    struct sigaction sa = {{0}};
    pid_t mpeg_pid;
    sa.__sigaction_handler.sa_handler = ((__sighandler_t) 0);
    sigaction(17, &sa, ((void *)0));
    while (!foad) {
        time_t whenstarted;
        int st;
        fprintf(stderr, "driftnet"": starting MPEG player `%s'\n", audio_mpeg_player);
        whenstarted = time(((void *)0));
        switch ((mpeg_pid = fork())) {
            case 0:
                execl("/bin/sh", "/bin/sh", "-c", audio_mpeg_player, ((void *)0));
                fprintf(stderr, "driftnet"": exec: %s\n", strerror((*__errno_location ())));
                exit(-1);
                break;
            case -1:
                fprintf(stderr, "driftnet"": fork: %s\n", strerror((*__errno_location ())));
                exit(-1);
                break;
            default:
                if (verbose)
                    fprintf(stderr, "driftnet"": MPEG player has PID %d\n", (int)mpeg_pid);
                break;
        }
        waitpid(mpeg_pid, &st, 0);
        mpeg_pid = 0;
        if (verbose) {
            if ((((st) & 0x7f) == 0))
                fprintf(stderr, "driftnet"": MPEG player exited with status %d\n", (((st) & 0xff00) >> 8));
            else if ((((signed char) (((st) & 0x7f) + 1) >> 1) > 0))
                fprintf(stderr, "driftnet"": MPEG player killed by signal %d\n", ((st) & 0x7f));
        }
        if (!foad && time(((void *)0)) - whenstarted < 5) {
            fprintf(stderr, "driftnet"": MPEG player expired after %d seconds, sleeping for a bit\n", (int)(time(((void *)0)) - whenstarted));
            sleep(5);
        }
    }
    if (mpeg_pid)
        kill(mpeg_pid, 15);
}

canonical_original_code:
static void mpeg_player_manager() {
    extern int foad;
    struct sigaction sa = {{0}};
    int mpeg_pid;
    sa.__sigaction_handler.sa_handler = ((void (*)(int)) 0);
    sigaction(17, &sa, ((void *)0));
    while (!foad) {
        long whenstarted;
        int st;
        fprintf(stderr, "driftnet"": starting MPEG player `%s'\n", audio_mpeg_player);
        whenstarted = time(((void *)0));
        switch ((mpeg_pid = fork())) {
            case 0:
                execl("/bin/sh", "/bin/sh", "-c", audio_mpeg_player, ((void *)0));
                fprintf(stderr, "driftnet"": exec: %s\n", strerror((*__errno_location ())));
                exit(-1);
                break;
            case -1:
                fprintf(stderr, "driftnet"": fork: %s\n", strerror((*__errno_location ())));
                exit(-1);
                break;
            default:
                if (verbose)
                    fprintf(stderr, "driftnet"": MPEG player has PID %d\n", (int)mpeg_pid);
                break;
        }
        waitpid(mpeg_pid, &st, 0);
        mpeg_pid = 0;
        if (verbose) {
            if ((((st) & 0x7f) == 0))
                fprintf(stderr, "driftnet"": MPEG player exited with status %d\n", (((st) & 0xff00) >> 8));
            else if ((((char) (((st) & 0x7f) + 1) >> 1) > 0))
                fprintf(stderr, "driftnet"": MPEG player killed by signal %d\n", ((st) & 0x7f));
        }
        if (!foad && time(((void *)0)) - whenstarted < 5) {
            fprintf(stderr, "driftnet"": MPEG player expired after %d seconds, sleeping for a bit\n", (int)(time(((void *)0)) - whenstarted));
            sleep(5);
        }
    }
    if (mpeg_pid)
        kill(mpeg_pid, 15);
}

variable_types: foad int

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

variable_types: whenstarted long

variable_types: st int

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


decompiled_code:
int __fastcall mpeg_submit_chunk(void *src, size_t n)
{
int result; // eax
_QWORD *v3; // rax
__int64 v4; // rdx

pthread_mutex_lock(&mpeg_mtx);
if ( (unsigned __int64)buffered <= 0x800000 )
{
v3 = audiochunk_new(src, n);
v4 = wr;
buffered += n;
wr = (__int64)v3;
*(_QWORD *)(v4 + 16) = v3;
result = pthread_mutex_unlock(&mpeg_mtx);
}
else
{
if ( verbose )
fprintf(stderr, "driftnet: MPEG buffer full with %d bytes\n", buffered);
result = pthread_mutex_unlock(&mpeg_mtx);
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(void *src, size_t n)
{
int result; // eax
_QWORD *v3; // rax
__int64 v4; // rdx

pthread_mutex_lock(&mpeg_mtx);
if ( (unsigned __int64)buffered <= 0x800000 )
{
v3 = audiochunk_new(src, n);
v4 = wr;
buffered += n;
wr = (__int64)v3;
*(_QWORD *)(v4 + 16) = v3;
result = pthread_mutex_unlock(&mpeg_mtx);
}
else
{
if ( verbose )
fprintf(stderr, "driftnet: MPEG buffer full with %d bytes\n", buffered);
result = pthread_mutex_unlock(&mpeg_mtx);
}
return result;
}

original_code:
void mpeg_submit_chunk(const unsigned char *data, const size_t len) {
    audiochunk A;
    pthread_mutex_lock(&mpeg_mtx);
    if (buffered > (8 * 1024 * 1024)) {
        if (verbose)
            fprintf(stderr, "driftnet"": MPEG buffer full with %d bytes\n", buffered);
        goto finish;
    }
    A = audiochunk_new(data, len);
    wr->next = A;
    wr = wr->next;
    buffered += len;
finish:
    pthread_mutex_unlock(&mpeg_mtx);
}

canonical_original_code:
void mpeg_submit_chunk(unsigned char *data, unsigned long len) {
    struct _audiochunk *A;
    pthread_mutex_lock(&mpeg_mtx);
    if (buffered > (8 * 1024 * 1024)) {
        if (verbose)
            fprintf(stderr, "driftnet"": MPEG buffer full with %d bytes\n", buffered);
        goto finish;
    }
    A = audiochunk_new(data, len);
    wr->next = A;
    wr = wr->next;
    buffered += len;
finish:
    pthread_mutex_unlock(&mpeg_mtx);
}

variable_types:
data
unsigned char *

variable_types:
len
unsigned long


decompiled_code:
int do_mpeg_player()
{
pthread_t v1; // [rsp+0h] [rbp-18h] BYREF
int pipedes[2]; // [rsp+8h] [rbp-10h] BYREF

wr = (__int64)audiochunk_new(0LL, 0LL);
rd = (void *)wr;
pipe(pipedes);
mpeg_mgr_pid = fork();
if ( !mpeg_mgr_pid )
{
close(pipedes[1]);
dup2(pipedes[0], 0);
mpeg_player_manager();
exit(-1);
}
close(pipedes[0]);
mpeg_fd = pipedes[1];
return pthread_create(&v1, 0LL, (void *(*)(void *))mpeg_play, rd);
}

canonical_decompiled_code:
int func5()
{
pthread_t v1; // [rsp+0h] [rbp-18h] BYREF
int pipedes[2]; // [rsp+8h] [rbp-10h] BYREF

wr = (__int64)audiochunk_new(0LL, 0LL);
rd = (void *)wr;
pipe(pipedes);
mpeg_mgr_pid = fork();
if ( !mpeg_mgr_pid )
{
close(pipedes[1]);
dup2(pipedes[0], 0);
func3();
exit(-1);
}
close(pipedes[0]);
mpeg_fd = pipedes[1];
return pthread_create(&v1, 0LL, (void *(*)(void *))mpeg_play, rd);
}

original_code:
void do_mpeg_player(void) {
    int pp[2];
    pthread_t thr;
    rd = wr = list = audiochunk_new(((void *)0), 0);
    pipe(pp);
    mpeg_mgr_pid = fork();
    if (mpeg_mgr_pid == 0) {
        close(pp[1]);
        dup2(pp[0], 0);
        mpeg_player_manager();
        exit(-1);
    } else {
        close(pp[0]);
        mpeg_fd = pp[1];
        pthread_create(&thr, ((void *)0), mpeg_play, rd);
    }
}

canonical_original_code:
void do_mpeg_player() {
    int pp[2];
    unsigned long thr;
    rd = wr = list = audiochunk_new(((void *)0), 0);
    pipe(pp);
    mpeg_mgr_pid = fork();
    if (mpeg_mgr_pid == 0) {
        close(pp[1]);
        dup2(pp[0], 0);
        mpeg_player_manager();
        exit(-1);
    } else {
        close(pp[0]);
        mpeg_fd = pp[1];
        pthread_create(&thr, ((void *)0), mpeg_play, rd);
    }
}

variable_types:
pp
int [2]

variable_types:
thr
unsigned long


original_code:
unsigned char *find_gif_image(const unsigned char *data, const size_t len, unsigned char **gifdata, size_t *giflen) {
    unsigned char *gifhdr, *block;
    int gotimgblock = 0;
    int ncolours;
    *gifdata = ((void *)0);
    if (len < 6) return (unsigned char*)data;
    gifhdr = memstr(data, len, (unsigned char*) "GIF89a", 6);
    if (!gifhdr) gifhdr = memstr(data, len, (unsigned char*) "GIF87a", 6);
    if (!gifhdr) return (unsigned char*)(data + len - 6);
    if (data + len - gifhdr < 14) return gifhdr;
    ncolours = (1 << ((gifhdr[10] & 0x7) + 1));
    block = gifhdr + 13;
    if (block >= data + len) return gifhdr;
    if (gifhdr[10] & 0x80) block += 3 * ncolours;
    if (block >= data + len) return gifhdr;
    do {
        switch (*block) {
            case 0x2c:
                if (block + 9 > data + len) return gifhdr;
                if (block[9] & 0x80) {
                    block += 3 * ((1 << ((gifhdr[9] & 0x7) + 1)));
                    if (block >= data + len) return gifhdr;
                }
                block += 10;
                ++block;
                do {
                    if (block >= data + len) return gifhdr;
                    block += *block + 1;
                    if (block >= data + len) return gifhdr;
                } while (*block);
                ++block;
                if (block >= data + len) return gifhdr;
                gotimgblock = 1;
                break;
            case 0x21:
                ++block;
                if (block >= data + len) return gifhdr;
                if (*block == 0xf9) {
                    ++block;
                    if (block >= data + len) return gifhdr;
                    block += *block + 2;
                    if (block >= data + len) return gifhdr;
                    break;
                } else if (*block == 0xfe) {
                    ++block;
                    do {
                        if (block >= data + len) return gifhdr;
                        block += *block + 1;
                        if (block >= data + len) return gifhdr;
                    } while (*block);
                    ++block;
                    if (block >= data + len) return gifhdr;
                } else if (*block == 0x01) {
                    ++block;
                    if (block >= data + len) return gifhdr;
                    if (*block != 12) return gifhdr + 6;
                    block += 13;
                    do {
                        if (block >= data + len) return gifhdr;
                        block += *block + 1;
                        if (block >= data + len) return gifhdr;
                    } while (*block);
                    ++block;
                    if (block >= data + len) return gifhdr;
                } else if (*block == 0xff) {
                    ++block;
                    if (block >= data + len) return gifhdr;
                    if (*block != 11) return gifhdr + 6;
                    block += 12;
                    do {
                        if (block >= data + len) return gifhdr;
                        block += *block + 1;
                        if (block >= data + len) return gifhdr;
                    } while (*block);
                    ++block;
                    if (block >= data + len) return gifhdr;
                } else {
                    return gifhdr + 6;
                }
                break;
            case 0x3b:
                *gifdata = gifhdr;
                *giflen = block - gifhdr + 1;
                return block + 1;
                break;
            default:
                return gifhdr + 6;
        }
    } while (1);
}

canonical_original_code:
unsigned char *find_gif_image(unsigned char *data, unsigned long len, unsigned char **gifdata, unsigned long *giflen) {
    unsigned char *gifhdr; unsigned char *block;
    int gotimgblock = 0;
    int ncolours;
    *gifdata = ((void *)0);
    if (len < 6) return (unsigned char *)data;
    gifhdr = memstr(data, len, (unsigned char *) "GIF89a", 6);
    if (!gifhdr) gifhdr = memstr(data, len, (unsigned char *) "GIF87a", 6);
    if (!gifhdr) return (unsigned char *)(data + len - 6);
    if (data + len - gifhdr < 14) return gifhdr;
    ncolours = (1 << ((gifhdr[10] & 0x7) + 1));
    block = gifhdr + 13;
    if (block >= data + len) return gifhdr;
    if (gifhdr[10] & 0x80) block += 3 * ncolours;
    if (block >= data + len) return gifhdr;
    do {
        switch (*block) {
            case 0x2c:
                if (block + 9 > data + len) return gifhdr;
                if (block[9] & 0x80) {
                    block += 3 * ((1 << ((gifhdr[9] & 0x7) + 1)));
                    if (block >= data + len) return gifhdr;
                }
                block += 10;
                ++block;
                do {
                    if (block >= data + len) return gifhdr;
                    block += *block + 1;
                    if (block >= data + len) return gifhdr;
                } while (*block);
                ++block;
                if (block >= data + len) return gifhdr;
                gotimgblock = 1;
                break;
            case 0x21:
                ++block;
                if (block >= data + len) return gifhdr;
                if (*block == 0xf9) {
                    ++block;
                    if (block >= data + len) return gifhdr;
                    block += *block + 2;
                    if (block >= data + len) return gifhdr;
                    break;
                } else if (*block == 0xfe) {
                    ++block;
                    do {
                        if (block >= data + len) return gifhdr;
                        block += *block + 1;
                        if (block >= data + len) return gifhdr;
                    } while (*block);
                    ++block;
                    if (block >= data + len) return gifhdr;
                } else if (*block == 0x01) {
                    ++block;
                    if (block >= data + len) return gifhdr;
                    if (*block != 12) return gifhdr + 6;
                    block += 13;
                    do {
                        if (block >= data + len) return gifhdr;
                        block += *block + 1;
                        if (block >= data + len) return gifhdr;
                    } while (*block);
                    ++block;
                    if (block >= data + len) return gifhdr;
                } else if (*block == 0xff) {
                    ++block;
                    if (block >= data + len) return gifhdr;
                    if (*block != 11) return gifhdr + 6;
                    block += 12;
                    do {
                        if (block >= data + len) return gifhdr;
                        block += *block + 1;
                        if (block >= data + len) return gifhdr;
                    } while (*block);
                    ++block;
                    if (block >= data + len) return gifhdr;
                } else {
                    return gifhdr + 6;
                }
                break;
            case 0x3b:
                *gifdata = gifhdr;
                *giflen = block - gifhdr + 1;
                return block + 1;
                break;
            default:
                return gifhdr + 6;
        }
    } while (1);
}

variable_types:
gifdata
unsigned char **

variable_types:
giflen
unsigned long *

variable_types:
gifhdr
unsigned char *

variable_types:
block
unsigned char *

variable_types: gotimgblock int

variable_types: ncolours int

return_type: unsigned char *


decompiled_code:
char *__fastcall jpeg_skip_block(_WORD *a1, unsigned __int64 a2)
{
unsigned __int16 v2; // ax
char *v3; // rdi
bool v4; // cc
char *result; // rax

if ( a2 <= 1 )
return 0LL;
v2 = __ROL2__(*a1, 8);
v3 = (char *)a1 + v2;
v4 = v2 <= a2;
result = 0LL;
if ( v4 )
result = v3;
return result;
}

canonical_decompiled_code:
char *__fastcall func2(_WORD *a1, unsigned __int64 a2)
{
unsigned __int16 v2; // ax
char *v3; // rdi
bool v4; // cc
char *result; // rax

if ( a2 <= 1 )
return 0LL;
v2 = __ROL2__(*a1, 8);
v3 = (char *)a1 + v2;
v4 = v2 <= a2;
result = 0LL;
if ( v4 )
result = v3;
return result;
}

original_code:
unsigned char *jpeg_skip_block(unsigned char *d, size_t len) {
    int l;
    if (len < 2) return ((void *)0);
    l = ((*(d) << 8) | *((d) + 1));
    if (l > len) return ((void *)0);
    return d + l;
}

canonical_original_code:
unsigned char *jpeg_skip_block(unsigned char *d, unsigned long len) {
    int l;
    if (len < 2) return ((void *)0);
    l = ((*(d) << 8) | *((d) + 1));
    if (l > len) return ((void *)0);
    return d + l;
}

variable_types:
d
unsigned char *

variable_types: l int


original_code:
unsigned char *find_jpeg_image(const unsigned char *data, const size_t len, unsigned char **jpegdata, size_t *jpeglen) {
    unsigned char *jpeghdr, *block;
    *jpegdata = ((void *)0);
    jpeghdr = memstr(data, len, (unsigned char*) "\xff\xd8", 2);
    if (!jpeghdr) return (unsigned char*)(data + len - 1);
    if (jpeghdr + 2 > data + len) return jpeghdr;
    block = jpeg_next_marker(jpeghdr + 2, len - 2 - (jpeghdr - data));
    if (!block || (block - data) >= len) return jpeghdr;
    while ((block = jpeg_skip_block(block + 1, len - (block + 1 - data)))) {
        block = jpeg_next_marker(block, len - (block - data));
        if (!block || (block - data) >= len) return jpeghdr;
        if (*block == 0xda) {
            block = memstr(block, len - (block - data), (unsigned char*) "\xff\xd9", 2);
            if (block) {
                *jpegdata = jpeghdr;
                *jpeglen = block + 2 - jpeghdr;
                return block + 2;
            } else break;
        }
    }
    return jpeghdr;
}

canonical_original_code:
unsigned char *find_jpeg_image(unsigned char *data, unsigned long len, unsigned char **jpegdata, unsigned long *jpeglen) {
    unsigned char *jpeghdr; unsigned char *block;
    *jpegdata = ((void *)0);
    jpeghdr = memstr(data, len, (unsigned char *) "\xff\xd8", 2);
    if (!jpeghdr) return (unsigned char *)(data + len - 1);
    if (jpeghdr + 2 > data + len) return jpeghdr;
    block = jpeg_next_marker(jpeghdr + 2, len - 2 - (jpeghdr - data));
    if (!block || (block - data) >= len) return jpeghdr;
    while ((block = jpeg_skip_block(block + 1, len - (block + 1 - data)))) {
        block = jpeg_next_marker(block, len - (block - data));
        if (!block || (block - data) >= len) return jpeghdr;
        if (*block == 0xda) {
            block = memstr(block, len - (block - data), (unsigned char *) "\xff\xd9", 2);
            if (block) {
                *jpegdata = jpeghdr;
                *jpeglen = block + 2 - jpeghdr;
                return block + 2;
            } else break;
        }
    }
    return jpeghdr;
}

variable_types:
jpegdata
unsigned char **

variable_types:
jpeglen
unsigned long *

variable_types:
jpeghdr
unsigned char *


decompiled_code:
char *__fastcall find_png_eoi(__int64 a1, __int64 a2)
{
__int64 *v2; // r14
unsigned __int64 v3; // r13
__int64 v5; // rdi
unsigned int v6; // ebx
size_t v7; // rdx
char s1[4]; // [rsp+Bh] [rbp-2Dh] BYREF
char v10; // [rsp+Fh] [rbp-29h]

v2 = (__int64 *)(a1 + 8);
v3 = a1 + a2 - 12;
if ( a1 + 8 > v3 )
return 0LL;
while ( 1 )
{
v5 = *v2;
v10 = 0;
*(_DWORD *)s1 = HIDWORD(v5);
v6 = _bswap_32(v5);
if ( !strncasecmp(s1, "iend", v7) )
break;
if ( v6 <= (unsigned __int64)(a2 - ((_QWORD)v2 - a1)) )
{
v2 = (__int64 *)((char *)v2 + v6 + 12);
if ( v3 >= (unsigned __int64)v2 )
continue;
}
return 0LL;
}
return (char *)v2 + 12;
}

canonical_decompiled_code:
char *__fastcall func4(__int64 a1, __int64 a2)
{
__int64 *v2; // r14
unsigned __int64 v3; // r13
__int64 v5; // rdi
unsigned int v6; // ebx
size_t v7; // rdx
char s1[4]; // [rsp+Bh] [rbp-2Dh] BYREF
char v10; // [rsp+Fh] [rbp-29h]

v2 = (__int64 *)(a1 + 8);
v3 = a1 + a2 - 12;
if ( a1 + 8 > v3 )
return 0LL;
while ( 1 )
{
v5 = *v2;
v10 = 0;
*(_DWORD *)s1 = HIDWORD(v5);
v6 = func0(v5);
if ( !strncasecmp(s1, "iend", v7) )
break;
if ( v6 <= (unsigned __int64)(a2 - ((_QWORD)v2 - a1)) )
{
v2 = (__int64 *)((char *)v2 + v6 + 12);
if ( v3 >= (unsigned __int64)v2 )
continue;
}
return 0LL;
}
return (char *)v2 + 12;
}

original_code:
unsigned char *find_png_eoi(unsigned char *buffer, const size_t len) {
    unsigned char *end_data, *data, chunk_code[4 + 1];
    struct png_chunk chunk;
    u_int32_t datalen;
    data = (buffer + 8);
    end_data = (buffer + len - (sizeof(struct png_chunk) + 4));
    while (data <= end_data) {
        memcpy(&chunk, data, sizeof chunk);
        memset(chunk_code, '\0', 4 + 1);
        memcpy(chunk_code, chunk.code, 4);
        datalen = ntohl(chunk.datalen);
        if (!strncasecmp((char*) chunk_code, "iend", 4))
            return (unsigned char *)(data + sizeof(struct png_chunk) + 4);
        if (datalen > (len - (data - buffer)))
            return ((void *)0);
        data += (sizeof(struct png_chunk) + datalen + 4);
    }
    return ((void *)0);
}

canonical_original_code:
unsigned char *find_png_eoi(unsigned char *buffer, unsigned long len) {
    unsigned char *end_data; unsigned char *data; unsigned char chunk_code[-1];
    struct png_chunk chunk;
    unsigned int datalen;
    data = (buffer + 8);
    end_data = (buffer + len - (sizeof(struct png_chunk) + 4));
    while (data <= end_data) {
        memcpy(&chunk, data, sizeof chunk);
        memset(chunk_code, '\0', 4 + 1);
        memcpy(chunk_code, chunk.code, 4);
        datalen = ntohl(chunk.datalen);
        if (!strncasecmp((char *) chunk_code, "iend", 4))
            return (unsigned char *)(data + sizeof(struct png_chunk) + 4);
        if (datalen > (len - (data - buffer)))
            return ((void *)0);
        data += (sizeof(struct png_chunk) + datalen + 4);
    }
    return ((void *)0);
}

variable_types:
buffer
unsigned char *

variable_types: struct png_chunk { unsigned int datalen; unsigned char code[4]; }

variable_types:
datalen
unsigned int


decompiled_code:
char *__fastcall find_png_image(__int64 a1, unsigned __int64 a2, __int64 *a3, _QWORD *a4)
{
__int64 v4; // r12
char *result; // rax
__int64 v8; // rax
__int64 v9; // r14

v4 = a1;
*a3 = 0LL;
if ( a2 <= 7 )
return (char *)v4;
v8 = memstr(a1, a2, &unk_5CB, 8LL);
v9 = v8;
if ( !v8 )
return (char *)(a1 + a2 - 8);
result = find_png_eoi(v8, a1 + a2 - v8);
if ( !result )
return (char *)v9;
*a3 = v9;
*a4 = &result[-v9];
return result;
}

canonical_decompiled_code:
char *__fastcall func5(__int64 a1, unsigned __int64 a2, __int64 *a3, _QWORD *a4)
{
__int64 v4; // r12
char *result; // rax
__int64 v8; // rax
__int64 v9; // r14

v4 = a1;
*a3 = 0LL;
if ( a2 <= 7 )
return (char *)v4;
v8 = memstr(a1, a2, &unk_5CB, 8LL);
v9 = v8;
if ( !v8 )
return (char *)(a1 + a2 - 8);
result = func4(v8, a1 + a2 - v8);
if ( !result )
return (char *)v9;
*a3 = v9;
*a4 = &result[-v9];
return result;
}

original_code:
unsigned char *find_png_image(const unsigned char *data, const size_t len, unsigned char **pngdata, size_t *pnglen) {
    unsigned char *pnghdr, *data_end, *png_eoi;
    *pngdata = ((void *)0);
    if (len < 8)
       return (unsigned char*)data;
    pnghdr = memstr(data, len, (unsigned char*) "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a", 8);
    if (!pnghdr)
        return (unsigned char*)(data + len - 8);
    data_end = (unsigned char *)(data + len);
    if ((png_eoi = find_png_eoi(pnghdr, (data_end - pnghdr))) == ((void *)0))
        return pnghdr;
    *pngdata = pnghdr;
    *pnglen = (png_eoi - pnghdr);
    return png_eoi;
}

canonical_original_code:
unsigned char *find_png_image(unsigned char *data, unsigned long len, unsigned char **pngdata, unsigned long *pnglen) {
    unsigned char *pnghdr; unsigned char *data_end; unsigned char *png_eoi;
    *pngdata = ((void *)0);
    if (len < 8)
       return (unsigned char *)data;
    pnghdr = memstr(data, len, (unsigned char *) "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a", 8);
    if (!pnghdr)
        return (unsigned char *)(data + len - 8);
    data_end = (unsigned char *)(data + len);
    if ((png_eoi = find_png_eoi(pnghdr, (data_end - pnghdr))) == ((void *)0))
        return pnghdr;
    *pngdata = pnghdr;
    *pnglen = (png_eoi - pnghdr);
    return png_eoi;
}

variable_types:
data
unsigned char *

variable_types:
len
unsigned long

variable_types:
pngdata
unsigned char **

variable_types:
pnglen
unsigned long *

variable_types:
pnghdr
unsigned char *


original_code:
connection connection_new(const struct in_addr *src, const struct in_addr *dst, const short int sport, const short int dport) {
    connection c;
    do { struct _connection as__z = {0}; c = xmalloc(sizeof *c); *c = as__z; } while (0);
    c->src = *src;
    c->dst = *dst;
    c->sport = sport;
    c->dport = dport;
    c->alloc = 16384;
    c->data = xmalloc(c->alloc);
    c->last = time(((void *)0));
    c->blocks = ((void *)0);
    return c;
}

canonical_original_code:
struct _connection *connection_new(struct in_addr *src, struct in_addr *dst, short sport, short dport) {
    struct _connection *c;
    do { struct _connection as__z = {0}; c = xmalloc(sizeof *c); *c = as__z; } while (0);
    c->src = *src;
    c->dst = *dst;
    c->sport = sport;
    c->dport = dport;
    c->alloc = 16384;
    c->data = xmalloc(c->alloc);
    c->last = time(((void *)0));
    c->blocks = ((void *)0);
    return c;
}

variable_types:
src
struct in_addr *

variable_types:
dst
struct in_addr *

variable_types: sport short

variable_types: dport short

variable_types: struct _connection { struct in_addr { unsigned int s_addr; } src; short sport; unsigned int isn; unsigned int len; unsigned char *data; int fin; long last; struct datablock { int off; struct datablock *next; } *blocks; } *

user_defined_types: struct _connection { struct in_addr src; short sport; unsigned int isn; unsigned int len; unsigned char *data; int fin; long last; struct datablock *blocks; }


decompiled_code:
void __fastcall connection_delete(void *ptr)
{
_QWORD *v2; // rbx
void *v3; // rdi

v2 = (_QWORD *)*((_QWORD *)ptr + 6);
while ( v2 )
{
v3 = v2;
v2 = (_QWORD *)v2[4];
free(v3);
}
free(*((void **)ptr + 3));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
_QWORD *v2; // rbx
void *v3; // rdi

v2 = (_QWORD *)*((_QWORD *)ptr + 6);
while ( v2 )
{
v3 = v2;
v2 = (_QWORD *)v2[4];
free(v3);
}
free(*((void **)ptr + 3));
free(ptr);
}

original_code:
void connection_delete(connection c) {
    struct datablock *b;
    for (b = c->blocks; b;) {
        struct datablock *b2;
        b2 = b->next;
        free(b);
        b = b2;
    }
    free(c->data);
    free(c);
}

canonical_original_code:
void connection_delete(struct _connection *c) {
    struct datablock *b;
    for (b = c->blocks; b;) {
        struct datablock *b2;
        b2 = b->next;
        free(b);
        b = b2;
    }
    free(c->data);
    free(c);
}

variable_types: struct datablock { int off; struct datablock *next; } *


original_code:
void connection_push(connection c, const unsigned char *data, unsigned int off, unsigned int len) {
    struct datablock *B, *b, *bl, BZ = {0};
    int a;
    ((void) sizeof ((c->alloc > 0) ? 1 : 0), __extension__ ({ if (c->alloc > 0) ; else __assert_fail ("c->alloc > 0", "connection.c", 59, __extension__ __PRETTY_FUNCTION__); }));
    if (off + len > c->alloc) {
        do
            c->alloc *= 2;
        while (off + len > c->alloc);
        c->data = (unsigned char*)xrealloc(c->data, c->alloc);
    }
    memcpy(c->data + off, data, len);
    if (off + len > c->len) c->len = off + len;
    c->last = time(((void *)0));
    B = xmalloc(sizeof *B);
    *B = BZ;
    B->off = off;
    B->len = len;
    B->dirty = 1;
    B->next = ((void *)0);
    for (b = c->blocks, bl = ((void *)0); ; bl = b, b = b->next) {
        if ((!b || off <= b->off) && (!bl || off > bl->off)) {
            B->next = b;
            if (bl)
                bl->next = B;
            else
                c->blocks = B;
            break;
        }
    }
    do {
        a = 0;
        for (b = c->blocks; b; b = b->next) {
            if (b->next && b->off + b->len >= b->next->off) {
                struct datablock *bb;
                bb = b->next;
                b->len = (bb->off + bb->len) - b->off;
                b->next = bb->next;
                b->dirty = 1;
                free(bb);
                ++a;
            }
        }
    } while (a);
}

canonical_original_code:
void connection_push(struct _connection *c, unsigned char *data, unsigned int off, unsigned int len) {
    struct datablock *B; struct datablock *b; struct datablock *bl; struct datablock BZ = {0};
    int a;
    ((void) sizeof ((c->alloc > 0) ? 1 : 0), __extension__ ({ if (c->alloc > 0) ; else __assert_fail ("c->alloc > 0", "connection.c", 59, __extension__ __PRETTY_FUNCTION__); }));
    if (off + len > c->alloc) {
        do
            c->alloc *= 2;
        while (off + len > c->alloc);
        c->data = (unsigned char *)xrealloc(c->data, c->alloc);
    }
    memcpy(c->data + off, data, len);
    if (off + len > c->len) c->len = off + len;
    c->last = time(((void *)0));
    B = xmalloc(sizeof *B);
    *B = BZ;
    B->off = off;
    B->len = len;
    B->dirty = 1;
    B->next = ((void *)0);
    for (b = c->blocks, bl = ((void *)0); ; bl = b, b = b->next) {
        if ((!b || off <= b->off) && (!bl || off > bl->off)) {
            B->next = b;
            if (bl)
                bl->next = B;
            else
                c->blocks = B;
            break;
        }
    }
    do {
        a = 0;
        for (b = c->blocks; b; b = b->next) {
            if (b->next && b->off + b->len >= b->next->off) {
                struct datablock *bb;
                bb = b->next;
                b->len = (bb->off + bb->len) - b->off;
                b->next = bb->next;
                b->dirty = 1;
                free(bb);
                ++a;
            }
        }
    } while (a);
}

variable_types:
off
unsigned int

variable_types:
len
unsigned int

variable_types: struct datablock { int off; struct datablock *next; }

variable_types: a int


original_code:
unsigned char *find_http_req(const unsigned char *data, const size_t len, unsigned char **http, size_t *httplen) {
    unsigned char *req, *le, *blankline, *hosthdr;
    if (len < 40)
        return (unsigned char*)data;
    if (!(req = memstr(data, len, (unsigned char*) "GET ", 4)))
        return (unsigned char*)(data + len - 4);
    if (!(le = memstr(req + 4, (len - (data - (req + 4))), (unsigned char*) "\r\n", 2))) {
        if ((len - (data - (req + 4))) > 16384)
            return (unsigned char*)(req + 4);
        else
            return (unsigned char*)req;
    }
    if (le < req + 5)
        return le + 2;
    if (memcmp(le - 9, " HTTP/1.", 8) || !strchr("01", (int)*(le - 1)))
        return le + 2;
    if (!(blankline = memstr(le + 2, (len - (data - (le + 2))), (unsigned char*) "\r\n\r\n", 4))) {
        if ((len - (data - (le + 2))) > 16384)
            return (unsigned char*)(data + len - 4);
        else
            return req;
    }
    if (memcmp(req + 4, "http://", 7) == 0)
        goto found;
    if (!(hosthdr = memstr(le, blankline - le + 2, (unsigned char*) "\r\nHost: ", 8))) {
        return blankline + 4;
    }
found:
    *http = req;
    *httplen = blankline - req;
    return blankline + 4;
}

canonical_original_code:
unsigned char *find_http_req(unsigned char *data, unsigned long len, unsigned char **http, unsigned long *httplen) {
    unsigned char *req; unsigned char *le; unsigned char *blankline; unsigned char *hosthdr;
    if (len < 40)
        return (unsigned char *)data;
    if (!(req = memstr(data, len, (unsigned char *) "GET ", 4)))
        return (unsigned char *)(data + len - 4);
    if (!(le = memstr(req + 4, (len - (data - (req + 4))), (unsigned char *) "\r\n", 2))) {
        if ((len - (data - (req + 4))) > 16384)
            return (unsigned char *)(req + 4);
        else
            return (unsigned char *)req;
    }
    if (le < req + 5)
        return le + 2;
    if (memcmp(le - 9, " HTTP/1.", 8) || !strchr("01", (int)*(le - 1)))
        return le + 2;
    if (!(blankline = memstr(le + 2, (len - (data - (le + 2))), (unsigned char *) "\r\n\r\n", 4))) {
        if ((len - (data - (le + 2))) > 16384)
            return (unsigned char *)(data + len - 4);
        else
            return req;
    }
    if (memcmp(req + 4, "http://", 7) == 0)
        goto found;
    if (!(hosthdr = memstr(le, blankline - le + 2, (unsigned char *) "\r\nHost: ", 8))) {
        return blankline + 4;
    }
found:
    *http = req;
    *httplen = blankline - req;
    return blankline + 4;
}

variable_types:
http
unsigned char **

variable_types:
httplen
unsigned long *

variable_types:
req
unsigned char *

variable_types:
le
unsigned char *

variable_types:
blankline
unsigned char *

variable_types:
hosthdr
unsigned char *


original_code:
void dispatch_http_req(const char *mname, const unsigned char *data, const size_t len) {
    char *url;
    const char *path, *host;
    int pathlen, hostlen;
    const unsigned char *p;
    if (!(p = memstr(data, len, (unsigned char*) "\r\n", 2)))
        return;
    path = (const char*)(data + 4);
    pathlen = (p - 9) - (unsigned char*)path;
    if (memcmp(path, "http://", 7) == 0) {
        url = malloc(pathlen + 1);
        sprintf(url, "%.*s", pathlen, path);
    } else {
        if (!(p = memstr(p, len - (p - data), (unsigned char*) "\r\nHost: ", 8)))
            return;
        host = (const char*)(p + 8);
        if (!(p = memstr(p + 8, len - (p + 8 - data), (unsigned char*) "\r\n", 2)))
            return;
        hostlen = p - (const unsigned char*)host;
        if (hostlen == 0)
            return;
        url = malloc(hostlen + pathlen + 9);
        sprintf(url, "http://%.*s%.*s", hostlen, host, pathlen, path);
    }
    fprintf(stderr, "\n\n  %s\n\n", url);
    free(url);
}

canonical_original_code:
void dispatch_http_req(char *mname, unsigned char *data, unsigned long len) {
    char *url;
    char *path; char *host;
    int pathlen; int hostlen;
    unsigned char *p;
    if (!(p = memstr(data, len, (unsigned char *) "\r\n", 2)))
        return;
    path = (char *)(data + 4);
    pathlen = (p - 9) - (unsigned char *)path;
    if (memcmp(path, "http://", 7) == 0) {
        url = malloc(pathlen + 1);
        sprintf(url, "%.*s", pathlen, path);
    } else {
        if (!(p = memstr(p, len - (p - data), (unsigned char *) "\r\nHost: ", 8)))
            return;
        host = (char *)(p + 8);
        if (!(p = memstr(p + 8, len - (p + 8 - data), (unsigned char *) "\r\n", 2)))
            return;
        hostlen = p - (unsigned char *)host;
        if (hostlen == 0)
            return;
        url = malloc(hostlen + pathlen + 9);
        sprintf(url, "http://%.*s%.*s", hostlen, host, pathlen, path);
    }
    fprintf(stderr, "\n\n  %s\n\n", url);
    free(url);
}

variable_types:
mname
char *

variable_types:
data
unsigned char *

variable_types:
len
unsigned long

variable_types:
url
char *

variable_types:
path
char *

variable_types:
host
char *

variable_types: pathlen int

variable_types: hostlen int

variable_types:
p
unsigned char *


xmalloc

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

result = malloc(a1);
if ( !result )
xmalloc_cold();
return result;
}

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

result = malloc(a1);
if ( !result )
func6();
return result;
}

original_code:
void *xmalloc(size_t n) {
    void *v;
    v = malloc(n);
    if (!v) abort();
    return v;
}

canonical_original_code:
void *xmalloc(unsigned long n) {
    void *v;
    v = malloc(n);
    if (!v) abort();
    return v;
}

variable_types:
n
unsigned long

variable_types:
v
void *


xcalloc

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

result = calloc(a1, a2);
if ( !result )
xcalloc_cold();
return result;
}

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

result = calloc(a1, a2);
if ( !result )
func7();
return result;
}

original_code:
void *xcalloc(size_t n, size_t m) {
    void *v;
    v = calloc(n, m);
    if (!v) abort();
    return v;
}

canonical_original_code:
void *xcalloc(unsigned long n, unsigned long m) {
    void *v;
    v = calloc(n, m);
    if (!v) abort();
    return v;
}

variable_types:
m
unsigned long


xrealloc

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

result = realloc(a1, a2);
if ( a2 )
{
if ( !result )
xrealloc_cold();
}
return result;
}

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

result = realloc(a1, a2);
if ( a2 )
{
if ( !result )
func8();
}
return result;
}

original_code:
void *xrealloc(void *w, size_t n) {
    void *v;
    v = realloc(w, n);
    if (n != 0 && !v) abort();
    return v;
}

canonical_original_code:
void *xrealloc(void *w, unsigned long n) {
    void *v;
    v = realloc(w, n);
    if (n != 0 && !v) abort();
    return v;
}

variable_types:
w
void *


xfree

decompiled_code:
void __fastcall xfree(void *a1)
{
if ( a1 )
free(a1);
}

canonical_decompiled_code:
void __fastcall func3(void *a1)
{
if ( a1 )
free(a1);
}

original_code:
void xfree(void *v) {
    if (v) free(v);
}

canonical_original_code:
void xfree(void *v) {
    if (v) free(v);
}


xstrdup

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

v1 = strlen(src);
v2 = (char *)xmalloc(v1 + 1);
return strcpy(v2, src);
}

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

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

original_code:
char *xstrdup(const char *s) {
    char *t;
    t = xmalloc(strlen(s) + 1);
    strcpy(t, s);
    return t;
}

canonical_original_code:
char *xstrdup(char *s) {
    char *t;
    t = xmalloc(strlen(s) + 1);
    strcpy(t, s);
    return t;
}

variable_types:
s
char *

variable_types:
t
char *

return_type: char *


memstr

original_code:
unsigned char *memstr(const unsigned char *haystack, const size_t hlen,
                             const unsigned char *needle, const size_t nlen) {
    int skip[256], k;
    if (nlen == 0) return (unsigned char*)haystack;
    for (k = 0; k < 256; ++k) skip[k] = nlen;
    for (k = 0; k < nlen - 1; ++k) skip[needle[k]] = nlen - k - 1;
    for (k = nlen - 1; k < hlen; k += skip[haystack[k]]) {
        int i, j;
        for (j = nlen - 1, i = k; j >= 0 && haystack[i] == needle[j]; j--) i--;
        if (j == -1) return (unsigned char*)(haystack + i + 1);
    }
    return ((void *)0);
}

canonical_original_code:
unsigned char *memstr(unsigned char *haystack, unsigned long hlen, unsigned char *needle, unsigned long nlen) {
    int skip[256]; int k;
    if (nlen == 0) return (unsigned char *)haystack;
    for (k = 0; k < 256; ++k) skip[k] = nlen;
    for (k = 0; k < nlen - 1; ++k) skip[needle[k]] = nlen - k - 1;
    for (k = nlen - 1; k < hlen; k += skip[haystack[k]]) {
        int i; int j;
        for (j = nlen - 1, i = k; j >= 0 && haystack[i] == needle[j]; j--) i--;
        if (j == -1) return (unsigned char *)(haystack + i + 1);
    }
    return ((void *)0);
}

variable_types:
haystack
unsigned char *

variable_types:
hlen
unsigned long

variable_types:
needle
unsigned char *

variable_types:
nlen
unsigned long

variable_types:
skip
int [256]

variable_types: k int

variable_types: i int

variable_types: j int


decompiled_code:
int __fastcall dispatch_image(const char *a1, const void *a2, size_t a3)
{
size_t v4; // rax
char *v5; // rbp
unsigned int v6; // er12
time_t v7; // rax
int result; // eax
int v9; // er12
__int128 v10[6]; // [rsp+0h] [rbp-68h] BYREF

v10[0] = 0LL;
v10[1] = 0LL;
v10[2] = 0LL;
v10[3] = 0LL;
v4 = strlen(tmpdir);
v5 = (char *)xmalloc(v4 + 64);
v6 = rand();
v7 = time(0LL);
sprintf((char *)v10, "driftnet-%08x%08x.%s", v7, v6, a1);
sprintf(v5, "%s/%s", tmpdir, (const char *)v10);
result = open(v5, 193, 420LL);
if ( result != -1 )
{
v9 = result;
write(result, a2, a3);
close(v9);
if ( adjunct )
puts(v5);
else
write(dpychld_fd, v10, 0x40uLL);
result = xfree(v5);
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(const char *a1, const void *a2, size_t a3)
{
size_t v4; // rax
char *v5; // rbp
unsigned int v6; // er12
time_t v7; // rax
int result; // eax
int v9; // er12
__int128 v10[6]; // [rsp+0h] [rbp-68h] BYREF

v10[0] = 0LL;
v10[1] = 0LL;
v10[2] = 0LL;
v10[3] = 0LL;
v4 = strlen(tmpdir);
v5 = (char *)xmalloc(v4 + 64);
v6 = rand();
v7 = time(0LL);
sprintf((char *)v10, "driftnet-%08x%08x.%s", v7, v6, a1);
sprintf(v5, "%s/%s", tmpdir, (const char *)v10);
result = open(v5, 193, 420LL);
if ( result != -1 )
{
v9 = result;
write(result, a2, a3);
close(v9);
if ( adjunct )
puts(v5);
else
write(dpychld_fd, v10, 0x40uLL);
result = xfree(v5);
}
return result;
}

original_code:
void dispatch_image(const char *mname, const unsigned char *data, const size_t len) {
    char *buf, name[64] = {0};
    int fd;
    buf = xmalloc(strlen(tmpdir) + 64);
    sprintf(name, "driftnet-%08x%08x.%s", (unsigned int)time(((void *)0)), rand(), mname);
    sprintf(buf, "%s/%s", tmpdir, name);
    fd = open(buf, 01 | 0100 | 0200, 0644);
    if (fd == -1)
        return;
    write(fd, data, len);
    close(fd);
    if (adjunct)
        printf("%s\n", buf);
    else
        write(dpychld_fd, name, sizeof name);
    xfree(buf);
}

canonical_original_code:
void dispatch_image(char *mname, unsigned char *data, unsigned long len) {
    char *buf; char name[64] = {0};
    int fd;
    buf = xmalloc(strlen(tmpdir) + 64);
    sprintf(name, "driftnet-%08x%08x.%s", (unsigned int)time(((void *)0)), rand(), mname);
    sprintf(buf, "%s/%s", tmpdir, name);
    fd = open(buf, 01 | 0100 | 0200, 0644);
    if (fd == -1)
        return;
    write(fd, data, len);
    close(fd);
    if (adjunct)
        printf("%s\n", buf);
    else
        write(dpychld_fd, name, sizeof name);
    xfree(buf);
}

variable_types:
mname
char *

variable_types:
data
unsigned char *

variable_types:
len
unsigned long

variable_types:
buf
char *

variable_types:
name
char [64]

variable_types: fd int


decompiled_code:
__int64 __fastcall dispatch_mpeg_audio(__int64 a1, __int64 a2, __int64 a3)
{
return mpeg_submit_chunk(a2, a3);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, __int64 a3)
{
return mpeg_submit_chunk(a2, a3);
}

original_code:
void dispatch_mpeg_audio(const char *mname, const unsigned char *data, const size_t len) {
    mpeg_submit_chunk(data, len);
}

canonical_original_code:
void dispatch_mpeg_audio(char *mname, unsigned char *data, unsigned long len) {
    mpeg_submit_chunk(data, len);
}


decompiled_code:
_BOOL8 __fastcall is_driftnet_file(char *s)
{
bool v2; // zf
_BOOL4 v3; // er12
char *v4; // rax

v2 = memcmp(s, "driftnet-", 9uLL) == 0;
v3 = !v2;
if ( !v2 )
return 0LL;
v4 = strrchr(s, 46);
if ( v4 )
{
v3 = 1;
if ( strcmp(v4, ".jpeg") )
{
if ( strcmp(v4, ".gif") )
{
if ( strcmp(v4, ".png") )
v3 = strcmp(v4, ".mp3") == 0;
}
}
}
return v3;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(char *s)
{
bool v2; // zf
_BOOL4 v3; // er12
char *v4; // rax

v2 = memcmp(s, "driftnet-", 9uLL) == 0;
v3 = !v2;
if ( !v2 )
return 0LL;
v4 = strrchr(s, 46);
if ( v4 )
{
v3 = 1;
if ( strcmp(v4, ".jpeg") )
{
if ( strcmp(v4, ".gif") )
{
if ( strcmp(v4, ".png") )
v3 = strcmp(v4, ".mp3") == 0;
}
}
}
return v3;
}

original_code:
int is_driftnet_file(char *filename) {
    if (strncmp(filename, "driftnet-", 9) != 0) return 0;
    char *p = strrchr(filename, '.');
    if (p == 0) return 0;
    return (strcmp(p, ".jpeg") == 0 ||
           strcmp(p, ".gif") == 0 ||
           strcmp(p, ".png") == 0 ||
           strcmp(p, ".mp3") == 0);
}

variable_types:
filename
char *

variable_types:
p
char *


original_code:
void connection_extract_media(connection c, const enum mediatype T) {
    struct datablock *b;
    extern int max_tmpfiles;
    for (b = c->blocks; b; b = b->next) {
        if (b->len > 0 && b->dirty) {
            int i;
            for (i = 0; i < 5; ++i)
                if (driver[i].type & T) {
                    unsigned char *ptr, *oldptr, *media;
                    size_t mlen;
                    ptr = c->data + b->off + b->moff[i];
                    oldptr = ((void *)0);
                    while (ptr != oldptr && ptr < c->data + b->off + b->len) {
                        oldptr = ptr;
                        ptr = driver[i].find_data(ptr, (b->off + b->len) - (ptr - c->data), &media, &mlen);
                        if (media && (!max_tmpfiles || count_temporary_files() < max_tmpfiles))
                            driver[i].dispatch_data(driver[i].name, media, mlen);
                    }
                    b->moff[i] = ptr - c->data - b->off;
                }
            b->dirty = 0;
        }
    }
}

canonical_original_code:
void connection_extract_media(struct _connection *c, enum mediatype T) {
    struct datablock *b;
    extern int max_tmpfiles;
    for (b = c->blocks; b; b = b->next) {
        if (b->len > 0 && b->dirty) {
            int i;
            for (i = 0; i < 5; ++i)
                if (driver[i].type & T) {
                    unsigned char *ptr; unsigned char *oldptr; unsigned char *media;
                    unsigned long mlen;
                    ptr = c->data + b->off + b->moff[i];
                    oldptr = ((void *)0);
                    while (ptr != oldptr && ptr < c->data + b->off + b->len) {
                        oldptr = ptr;
                        ptr = driver[i].find_data(ptr, (b->off + b->len) - (ptr - c->data), &media, &mlen);
                        if (media && (!max_tmpfiles || count_temporary_files() < max_tmpfiles))
                            driver[i].dispatch_data(driver[i].name, media, mlen);
                    }
                    b->moff[i] = ptr - c->data - b->off;
                }
            b->dirty = 0;
        }
    }
}

variable_types: struct _connection { struct in_addr { unsigned int s_addr; } src; short sport; unsigned int isn; unsigned int len; unsigned char *data; int fin; long last; struct datablock { int off; struct datablock *next; } *blocks; } *

variable_types: enum mediatype { m_image=1, m_audio=2, m_text=4 }

variable_types:
ptr
unsigned char *

variable_types:
oldptr
unsigned char *

variable_types:
media
unsigned char *

variable_types:
mlen
unsigned long


variable_types: struct mpeg_audio_hdr { enum { m_vers_unknown=0, m_vers_1=1, m_vers_2=2, m_vers_2_5=3 } version; enum { m_layer_unknown=0, m_layer_1=1, m_layer_2=2, m_layer_3=3 } layer; int has_crc; unsigned short crc; int bitrate; int samplerate; int padded; int priv; enum { m_chan_stereo=0, m_chan_joint=1, m_chan_dual=2, m_chan_mono=3 } channels; unsigned char mode_extn; int copyr; unsigned char emph; } *

variable_types:
hh
unsigned int


decompiled_code:
__int64 __fastcall mpeg_hdr_nextframe_offset(_DWORD *a1)
{
int v1; // eax
int v2; // ecx
int v3; // esi
__int64 result; // rax

v1 = a1[4];
v2 = a1[5];
v3 = a1[6];
if ( a1[1] == 1 )
result = (unsigned int)(4 * (v3 + 12000 * v1 / v2));
else
result = (unsigned int)(v3 + 144000 * v1 / v2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1)
{
int v1; // eax
int v2; // ecx
int v3; // esi
__int64 result; // rax

v1 = a1[4];
v2 = a1[5];
v3 = a1[6];
if ( a1[1] == 1 )
result = (unsigned int)(4 * (v3 + 12000 * v1 / v2));
else
result = (unsigned int)(v3 + 144000 * v1 / v2);
return result;
}

original_code:
int mpeg_hdr_nextframe_offset(const struct mpeg_audio_hdr *h) {
    int off = 0;
    if (h->layer == m_layer_1)
        off += ((12 * h->bitrate * 1000) / h->samplerate + h->padded) * 4;
    else
        off += (144 * h->bitrate * 1000) / h->samplerate + h->padded;
    return off;
}

canonical_original_code:
int mpeg_hdr_nextframe_offset(struct mpeg_audio_hdr *h) {
    int off = 0;
    if (h->layer == m_layer_1)
        off += ((12 * h->bitrate * 1000) / h->samplerate + h->padded) * 4;
    else
        off += (144 * h->bitrate * 1000) / h->samplerate + h->padded;
    return off;
}

variable_types: off int

user_defined_types: struct mpeg_audio_hdr { enum { m_vers_unknown=0, m_vers_1=1, m_vers_2=2, m_vers_2_5=3 } version; enum { m_layer_unknown=0, m_layer_1=1, m_layer_2=2, m_layer_3=3 } layer; int has_crc; unsigned short crc; int bitrate; int samplerate; int padded; int priv; enum { m_chan_stereo=0, m_chan_joint=1, m_chan_dual=2, m_chan_mono=3 } channels; unsigned char mode_extn; int copyr; unsigned char emph; }


decompiled_code:
char *__fastcall find_mpeg_stream(void *s, unsigned __int64 a2, _QWORD *a3, _QWORD *a4)
{
char *v4; // r14
char *v7; // rbp
char *v8; // rax
char *v10; // r15
int v11; // ebx
int v12; // eax
__int64 v13; // rax
_BYTE v14[120]; // [rsp+0h] [rbp-78h] BYREF

v4 = (char *)s;
*a3 = 0LL;
if ( a2 > 3 )
{
v7 = (char *)s + a2 - 4;
if ( s < v7 )
{
while ( 1 )
{
v8 = (char *)memchr(v4, 255, a2 - 4 - (v4 - (_BYTE *)s));
v4 = v8;
if ( !v8 )
return (char *)s + a2 - 4;
if ( (v8[1] & 0xE0) == 0xE0 && (unsigned int)mpeg_hdr_parse(v8, v14) )
break;
if ( ++v4 >= v7 )
return v4;
}
v10 = v4;
v11 = 100;
while ( 1 )
{
v12 = mpeg_hdr_nextframe_offset(v14);
if ( !v12 )
return v10 + 1;
v10 += v12;
if ( !--v11 )
break;
if ( v10 >= v7 || !(unsigned int)mpeg_hdr_parse(v10, v14) )
return v4;
}
*a3 = v4;
v13 = v10 - v4;
v4 = v10;
*a4 = v13;
}
}
return v4;
}

canonical_decompiled_code:
char *__fastcall func0(void *s, unsigned __int64 a2, _QWORD *a3, _QWORD *a4)
{
char *v4; // r14
char *v7; // rbp
char *v8; // rax
char *v10; // r15
int v11; // ebx
int v12; // eax
__int64 v13; // rax
_BYTE v14[120]; // [rsp+0h] [rbp-78h] BYREF

v4 = (char *)s;
*a3 = 0LL;
if ( a2 > 3 )
{
v7 = (char *)s + a2 - 4;
if ( s < v7 )
{
while ( 1 )
{
v8 = (char *)memchr(v4, 255, a2 - 4 - (v4 - (_BYTE *)s));
v4 = v8;
if ( !v8 )
return (char *)s + a2 - 4;
if ( (v8[1] & 0xE0) == 0xE0 && (unsigned int)mpeg_hdr_parse(v8, v14) )
break;
if ( ++v4 >= v7 )
return v4;
}
v10 = v4;
v11 = 100;
while ( 1 )
{
v12 = mpeg_hdr_nextframe_offset(v14);
if ( !v12 )
return v10 + 1;
v10 += v12;
if ( !--v11 )
break;
if ( v10 >= v7 || !(unsigned int)mpeg_hdr_parse(v10, v14) )
return v4;
}
*a3 = v4;
v13 = v10 - v4;
v4 = v10;
*a4 = v13;
}
}
return v4;
}

original_code:
unsigned char *find_mpeg_stream(const unsigned char *data, const size_t len, unsigned char **mpegdata, size_t *mpeglen) {
    unsigned char *stream_start, *p;
    struct mpeg_audio_hdr H;
    *mpegdata = ((void *)0);
    if (len < 4) return (unsigned char*)data;
    p = (unsigned char*)data;
    while (p < data + len - 4) {
        int nframes;
        unsigned char *q;
        stream_start = memchr(p, 0xff, len - 4 - (p - data));
        if (!stream_start)
            return (unsigned char*)(data + len - 4);
        if ((stream_start[1] & 0xe0) != 0xe0) {
            p = stream_start + 1;
            continue;
        }
        if (!mpeg_hdr_parse(stream_start, &H)) {
            p = stream_start + 1;
            continue;
        }
        nframes = 0;
        q = stream_start;
        do {
            int delta;
            ++nframes;
            delta = mpeg_hdr_nextframe_offset(&H);
            if (delta == 0)
                return q + 1;
            q += delta;
        } while (nframes < 100 && q < data + len - 4 && mpeg_hdr_parse(q, &H));
        if (nframes >= 100) {
            *mpegdata = stream_start;
            *mpeglen = q - stream_start;
            return q;
        } else
            return stream_start;
    }
    return p;
}

canonical_original_code:
unsigned char *find_mpeg_stream(unsigned char *data, unsigned long len, unsigned char **mpegdata, unsigned long *mpeglen) {
    unsigned char *stream_start; unsigned char *p;
    struct mpeg_audio_hdr H;
    *mpegdata = ((void *)0);
    if (len < 4) return (unsigned char *)data;
    p = (unsigned char *)data;
    while (p < data + len - 4) {
        int nframes;
        unsigned char *q;
        stream_start = memchr(p, 0xff, len - 4 - (p - data));
        if (!stream_start)
            return (unsigned char *)(data + len - 4);
        if ((stream_start[1] & 0xe0) != 0xe0) {
            p = stream_start + 1;
            continue;
        }
        if (!mpeg_hdr_parse(stream_start, &H)) {
            p = stream_start + 1;
            continue;
        }
        nframes = 0;
        q = stream_start;
        do {
            int delta;
            ++nframes;
            delta = mpeg_hdr_nextframe_offset(&H);
            if (delta == 0)
                return q + 1;
            q += delta;
        } while (nframes < 100 && q < data + len - 4 && mpeg_hdr_parse(q, &H));
        if (nframes >= 100) {
            *mpegdata = stream_start;
            *mpeglen = q - stream_start;
            return q;
        } else
            return stream_start;
    }
    return p;
}

variable_types:
mpegdata
unsigned char **

variable_types:
mpeglen
unsigned long *

variable_types:
p
unsigned char *

variable_types: struct mpeg_audio_hdr { enum { m_vers_unknown=0, m_vers_1=1, m_vers_2=2, m_vers_2_5=3 } version; enum { m_layer_unknown=0, m_layer_1=1, m_layer_2=2, m_layer_3=3 } layer; int has_crc; unsigned short crc; int bitrate; int samplerate; int padded; int priv; enum { m_chan_stereo=0, m_chan_joint=1, m_chan_dual=2, m_chan_mono=3 } channels; unsigned char mode_extn; int copyr; unsigned char emph; }

variable_types: nframes int

variable_types:
q
unsigned char *

variable_types: delta int


main

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

v3 = argv;
if ( argc == 4 )
{
if ( !strcmp(argv[1], "-d") )
{
v3 = argv + 1;
puts("Debug On");
return lockfile_p((char *)v3[1]);
}
}
else if ( argc == 3 )
{
return lockfile_p((char *)v3[1]);
}
puts("Usage: lockfile [-d] <filename> <pid>");
return -1;
}

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

v3 = argv;
if ( argc == 4 )
{
if ( !strcmp(argv[1], "-d") )
{
v3 = argv + 1;
puts("Debug On");
return func8((char *)v3[1]);
}
}
else if ( argc == 3 )
{
return func8((char *)v3[1]);
}
puts("Usage: lockfile [-d] <filename> <pid>");
return -1;
}

original_code:
int main(int argc, char **argv, char **arge)
{
int debug = 0;
 if(argc == 4 && strcmp(argv[1], "-d") == 0)
 {
  debug = 1;
  printf("Debug On\n");
  argv++;
 }
 else if(argc != 3)
 { printf("Usage: lockfile [-d] <filename> <pid>\n");
  return -1;
 }
 return lockfile_p(argv[1], argv[2], debug);
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
arge
char **

variable_types: debug int


decompiled_code:
__int64 __fastcall lockfile_p(char *file, const char *a2, int a3)
{
int v4; // ebp
unsigned int v5; // er12
int *v6; // rax
int *v7; // r14
int v8; // eax
int v9; // er15
char *v11; // rax
size_t v12; // rax
int v13; // edi
char *v14; // rax
char *v15; // rax
int v16; // eax
char *v17; // rax
char *v18; // rax
size_t v19; // rax
unsigned int v20; // [rsp+Ch] [rbp-4Ch]
char buf[72]; // [rsp+10h] [rbp-48h] BYREF

v4 = 10;
while ( 1 )
{
v5 = open(file, 194, 0LL);
if ( v5 != -1 )
{
v12 = strlen(a2);
write(v5, a2, v12 + 1);
write(v5, "\n", 1uLL);
fchmod(v5, 0x180u);
v13 = v5;
v5 = 0;
close(v13);
return v5;
}
v6 = __errno_location();
v7 = v6;
if ( *v6 != 17 )
break;
v8 = open(file, 2, 0LL);
v9 = v8;
if ( v8 == -1 )
{
if ( *v7 != 2 )
{
if ( a3 )
{
v14 = strerror(*v7);
printf("2nd open error = %s\n", v14);
}
return v5;
}
}
else
{
if ( !_lock(v8, 1) )
{
if ( (unsigned int)read(v9, buf, 0x10uLL) == -1 )
{
if ( a3 != 1 )
goto LABEL_9;
v18 = strerror(*v7);
printf("read error = %s\n", v18);
}
else
{
v16 = atoi(buf);
v20 = v16;
if ( v16 > 1 && kill(v16, 0) == -1 )
{
if ( *v7 == 3 )
{
v5 = 0;
lseek(v9, 0LL, 0);
v19 = strlen(a2);
write(v9, a2, v19 + 1);
write(v9, "\n", 1uLL);
_lock(v9, 0);
close(v9);
return v5;
}
if ( a3 == 1 )
{
v17 = strerror(*v7);
printf("kill error = %s\n", v17);
}
LABEL_9:
close(v9);
return v5;
}
if ( a3 != 1 )
goto LABEL_9;
}
printf("kill: Process %d is still running\n", v20);
goto LABEL_9;
}
if ( ((*v7 - 11) & 0xFFFFFFFD) != 0 )
{
if ( a3 )
{
v11 = strerror(*v7);
printf("lock error = %s\n", v11);
}
goto LABEL_9;
}
close(v9);
sleep(2u);
}
if ( !--v4 )
return v5;
}
if ( a3 == 1 )
{
v15 = strerror(*v6);
printf("Open error = %s\n", v15);
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *file, const char *a2, int a3)
{
int v4; // ebp
unsigned int v5; // er12
int *v6; // rax
int *v7; // r14
int v8; // eax
int v9; // er15
char *v11; // rax
size_t v12; // rax
int v13; // edi
char *v14; // rax
char *v15; // rax
int v16; // eax
char *v17; // rax
char *v18; // rax
size_t v19; // rax
unsigned int v20; // [rsp+Ch] [rbp-4Ch]
char buf[72]; // [rsp+10h] [rbp-48h] BYREF

v4 = 10;
while ( 1 )
{
v5 = open(file, 194, 0LL);
if ( v5 != -1 )
{
v12 = strlen(a2);
write(v5, a2, v12 + 1);
write(v5, "\n", 1uLL);
fchmod(v5, 0x180u);
v13 = v5;
v5 = 0;
close(v13);
return v5;
}
v6 = __errno_location();
v7 = v6;
if ( *v6 != 17 )
break;
v8 = open(file, 2, 0LL);
v9 = v8;
if ( v8 == -1 )
{
if ( *v7 != 2 )
{
if ( a3 )
{
v14 = strerror(*v7);
printf("2nd open error = %s\n", v14);
}
return v5;
}
}
else
{
if ( !func7(v8, 1) )
{
if ( (unsigned int)read(v9, buf, 0x10uLL) == -1 )
{
if ( a3 != 1 )
goto LABEL_9;
v18 = strerror(*v7);
printf("read error = %s\n", v18);
}
else
{
v16 = atoi(buf);
v20 = v16;
if ( v16 > 1 && kill(v16, 0) == -1 )
{
if ( *v7 == 3 )
{
v5 = 0;
lseek(v9, 0LL, 0);
v19 = strlen(a2);
write(v9, a2, v19 + 1);
write(v9, "\n", 1uLL);
func7(v9, 0);
close(v9);
return v5;
}
if ( a3 == 1 )
{
v17 = strerror(*v7);
printf("kill error = %s\n", v17);
}
LABEL_9:
close(v9);
return v5;
}
if ( a3 != 1 )
goto LABEL_9;
}
printf("kill: Process %d is still running\n", v20);
goto LABEL_9;
}
if ( ((*v7 - 11) & 0xFFFFFFFD) != 0 )
{
if ( a3 )
{
v11 = strerror(*v7);
printf("lock error = %s\n", v11);
}
goto LABEL_9;
}
close(v9);
sleep(2u);
}
if ( !--v4 )
return v5;
}
if ( a3 == 1 )
{
v15 = strerror(*v6);
printf("Open error = %s\n", v15);
}
return v5;
}

original_code:
int lockfile_p(char *filename, char *pid_str, int debug) {
  int fd;
  int i;
  for (i = 0; i < 10; i++) {
    if ((fd = open(filename, 0100 | 0200 | 02, 0)) == -1) {
      if ((*__errno_location ()) == 17) {
        if ((fd = open(filename, 02, 0)) != -1) {
          char buff[16];
          int pid;
          int l;
          int r;
          if (__lock(fd, 1) == 0) {
            if ((l = read(fd, buff, 16)) != -1 && (pid = atoi(buff)) > 1 && (r = kill(pid, 0)) == -1 && (*__errno_location ()) == 3) {
              lseek(fd, 0, 0);
              write(fd, pid_str, strlen(pid_str) + 1);
              write(fd, "\n", 1);
              __lock(fd, 0);
              close(fd);
              return 0;
            } else if (debug == 1) {
              if (l == -1)
                printf("read error = %s\n", strerror((*__errno_location ())));
              if (r == -1)
                printf("kill error = %s\n", strerror((*__errno_location ())));
              else
                printf("kill: Process %d is still running\n", pid);
            }
          } else if ((*__errno_location ()) == 11 || (*__errno_location ()) == 13) {
            close(fd);
            sleep(sleep_time);
            continue;
          } else if (debug)
            printf("lock error = %s\n", strerror((*__errno_location ())));
          close(fd);
          return -1;
        } else if ((*__errno_location ()) == 2)
          continue;
        else if (debug)
          printf("2nd open error = %s\n", strerror((*__errno_location ())));
      } else if (debug == 1)
        printf("Open error = %s\n", strerror((*__errno_location ())));
      return -1;
    } else {
      write(fd, pid_str, strlen(pid_str) + 1);
      write(fd, "\n", 1);
      fchmod(fd, 0600);
      close(fd);
      return 0;
    }
  }
  return -1;
}

variable_types:
filename
char *

variable_types: fd int

variable_types: i int

variable_types:
buff
char [16]

variable_types: pid int

variable_types: l int

variable_types: r int


end

repo: rcatolino/gt

decompiled_code:
void __noreturn end()
{
dumpTable();
unlink("/tmp/gtd.socket");
close(socklisten);
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
dumpTable();
unlink("/tmp/gtd.socket");
close(socklisten);
exit(0);
}

original_code:
void end(int signb){
  dumpTable();
  unlink("/tmp/gtd.socket");
  close(socklisten);
  exit(0);
}

variable_types: signb int


escape

decompiled_code:
__int64 __fastcall escape(char *s, unsigned int a2, _DWORD *a3)
{
char *v3; // r14
int v4; // eax
int v5; // ebp
int v6; // er12
char *v7; // rbx
char *v8; // rdx
char *v9; // r15
unsigned int v10; // er12

v3 = s;
s[a2 - 1] = 0;
v4 = strlen(s);
v5 = v4 + 2;
v6 = (_DWORD)s + v4 + 1;
v7 = &s[v4 + 1];
while ( 1 )
{
v9 = strpbrk(s, " \\");
if ( !v9 )
break;
if ( a2 < (int)v7 + v5 - v6 )
{
v10 = -1;
fprintf(stderr, "escape error : buffer too small for %s.\n", v3);
return v10;
}
v8 = v7++;
memmove(v9 + 1, v9, v8 - v9);
*v9 = 92;
s = v9 + 2;
}
v10 = 0;
if ( a3 )
*a3 = strlen(v3);
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s, unsigned int a2, _DWORD *a3)
{
char *v3; // r14
int v4; // eax
int v5; // ebp
int v6; // er12
char *v7; // rbx
char *v8; // rdx
char *v9; // r15
unsigned int v10; // er12

v3 = s;
s[a2 - 1] = 0;
v4 = strlen(s);
v5 = v4 + 2;
v6 = (_DWORD)s + v4 + 1;
v7 = &s[v4 + 1];
while ( 1 )
{
v9 = strpbrk(s, " \\");
if ( !v9 )
break;
if ( a2 < (int)v7 + v5 - v6 )
{
v10 = -1;
fprintf(stderr, "escape error : buffer too small for %s.\n", v3);
return v10;
}
v8 = v7++;
memmove(v9 + 1, v9, v8 - v9);
*v9 = 92;
s = v9 + 2;
}
v10 = 0;
if ( a3 )
*a3 = strlen(v3);
return v10;
}

original_code:
int escape(char *buffer, uint32_t buffsize, int *strlength) {
  int strsize;
  buffer[buffsize-1] = '\0';
  strsize = strlen(buffer) + 1;
  char *spaceptr = buffer;
  while ((spaceptr = strpbrk(spaceptr, " \\"))) {
    strsize++;
    if (strsize > buffsize) {
      fprintf(stderr,"escape error : buffer too small for %s.\n", buffer);
      return -1;
    }
    memmove(spaceptr + 1, spaceptr, buffer + strsize - 1 - spaceptr);
    spaceptr[0] = '\\';
    spaceptr += 2;
  }
  strsize = strlen(buffer) + 1;
  if (strlength) {
    *strlength = strsize - 1;
  }
  return 0;
}

canonical_original_code:
int escape(char *buffer, unsigned int buffsize, int *strlength) {
  int strsize;
  buffer[buffsize-1] = '\0';
  strsize = strlen(buffer) + 1;
  char *spaceptr = buffer;
  while ((spaceptr = strpbrk(spaceptr, " \\"))) {
    strsize++;
    if (strsize > buffsize) {
      fprintf(stderr,"escape error : buffer too small for %s.\n", buffer);
      return -1;
    }
    memmove(spaceptr + 1, spaceptr, buffer + strsize - 1 - spaceptr);
    spaceptr[0] = '\\';
    spaceptr += 2;
  }
  strsize = strlen(buffer) + 1;
  if (strlength) {
    *strlength = strsize - 1;
  }
  return 0;
}

variable_types:
buffer
char *

variable_types:
buffsize
unsigned int

variable_types:
strlength
int *

variable_types: strsize int

variable_types:
spaceptr
char *


unescape

decompiled_code:
__int64 __fastcall unescape(char *s, int a2)
{
char *v2; // rbp
char *i; // rbx
char v4; // al
char *v5; // rax

v2 = s;
s[a2 - 1] = 0;
for ( i = &s[(int)(strlen(s) + 1)]; ; memmove(v2, v2 + 1, i - v2) )
{
v5 = strchr(v2, 92);
v2 = v5;
if ( !v5 )
break;
v4 = v5[1];
--i;
if ( v4 != 92 && v4 != 32 )
break;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *s, int a2)
{
char *v2; // rbp
char *i; // rbx
char v4; // al
char *v5; // rax

v2 = s;
s[a2 - 1] = 0;
for ( i = &s[(int)(strlen(s) + 1)]; ; memmove(v2, v2 + 1, i - v2) )
{
v5 = strchr(v2, 92);
v2 = v5;
if ( !v5 )
break;
v4 = v5[1];
--i;
if ( v4 != 92 && v4 != 32 )
break;
}
return 0LL;
}

original_code:
int unescape(char *buffer, uint32_t buffsize) {
  int strsize;
  buffer[buffsize-1] = '\0';
  strsize = strlen(buffer) + 1;
  char *spaceptr = buffer;
  while ((spaceptr = strchr(spaceptr, '\\')) &&
         (spaceptr[1] == '\\' || spaceptr[1] == ' ')) {
    strsize--;
    memmove(spaceptr, spaceptr + 1, buffer + strsize - spaceptr);
  }
  strsize = strlen(buffer) + 1;
  return 0;
}

canonical_original_code:
int unescape(char *buffer, unsigned int buffsize) {
  int strsize;
  buffer[buffsize-1] = '\0';
  strsize = strlen(buffer) + 1;
  char *spaceptr = buffer;
  while ((spaceptr = strchr(spaceptr, '\\')) &&
         (spaceptr[1] == '\\' || spaceptr[1] == ' ')) {
    strsize--;
    memmove(spaceptr, spaceptr + 1, buffer + strsize - spaceptr);
  }
  strsize = strlen(buffer) + 1;
  return 0;
}


decompiled_code:
__int64 __fastcall store_event(char *s)
{
char *v1; // rbp

v1 = strrchr(s, 47) + 1;
do
{
if ( strlen(v1) != 1 )
{
fprintf(stderr, "store_event: dirname=%s, path=%s\n", v1, s);
update_entry(s, v1);
return 0LL;
}
*v1 = 0;
v1 = strrchr(s, 47);
}
while ( v1 );
fwrite("store_event: invalid path\n", 1uLL, 0x1AuLL, stderr);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *s)
{
char *v1; // rbp

v1 = strrchr(s, 47) + 1;
do
{
if ( strlen(v1) != 1 )
{
fprintf(stderr, "store_event: dirname=%s, path=%s\n", v1, s);
update_entry(s, v1);
return 0LL;
}
*v1 = 0;
v1 = strrchr(s, 47);
}
while ( v1 );
fwrite("store_event: invalid path\n", 1uLL, 0x1AuLL, stderr);
return 0xFFFFFFFFLL;
}

original_code:
int store_event(const char * buff){
  char * dirname;
  const char * path;
  path=buff;
  dirname=strrchr(path,'/')+1;
  if(dirname==((void *)0)){
    fprintf(stderr,"store_event: invalid path\n");
    return -1;
  }
  while(strlen(dirname)==1){
    dirname[0]='\0';
    dirname=strrchr(path,'/');
    if(dirname==((void *)0)){
      fprintf(stderr,"store_event: invalid path\n");
      return -1;
    }
  }
  fprintf(stderr,"store_event: dirname=%s, path=%s\n",dirname,path);
  update_entry(path, dirname);
  return 0;
}

canonical_original_code:
int store_event(char *buff){
  char *dirname;
  char *path;
  path=buff;
  dirname=strrchr(path,'/')+1;
  if(dirname==((void *)0)){
    fprintf(stderr,"store_event: invalid path\n");
    return -1;
  }
  while(strlen(dirname)==1){
    dirname[0]='\0';
    dirname=strrchr(path,'/');
    if(dirname==((void *)0)){
      fprintf(stderr,"store_event: invalid path\n");
      return -1;
    }
  }
  fprintf(stderr,"store_event: dirname=%s, path=%s\n",dirname,path);
  update_entry(path, dirname);
  return 0;
}

variable_types:
buff
char *

variable_types:
dirname
char *

variable_types:
path
char *


decompiled_code:
__int64 __fastcall find_event(const char *a1)
{
unsigned int v1; // ebp
__int64 v2; // rax
__int64 v3; // r12

v1 = hash();
fprintf(stderr, "find_event: %s\n", a1);
v2 = find_entry(a1, v1);
v3 = v2;
if ( v2 )
{
fprintf(
stderr,
"find_event: found path associated with dirname, %s\n",
(const char *)(*(_QWORD *)(v2 + 272) + 16LL));
v3 = *(_QWORD *)(v3 + 272) + 16LL;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1)
{
unsigned int v1; // ebp
__int64 v2; // rax
__int64 v3; // r12

v1 = hash();
fprintf(stderr, "find_event: %s\n", a1);
v2 = find_entry(a1, v1);
v3 = v2;
if ( v2 )
{
fprintf(
stderr,
"find_event: found path associated with dirname, %s\n",
(const char *)(*(_QWORD *)(v2 + 272) + 16LL));
v3 = *(_QWORD *)(v3 + 272) + 16LL;
}
return v3;
}

original_code:
const char *find_event(const char *buff){
  struct entry *src;
  int key=hash(buff);
  fprintf(stderr,"find_event: %s\n", buff);
  src=find_entry(buff, key);
  if (src==((void *)0)){
    return ((void *)0);
  } else {
    fprintf(stderr,"find_event: found path associated with dirname, %s\n", src->first_candidate->path);
    return src->first_candidate->path;
  }
}

canonical_original_code:
char *find_event(char *buff){
  struct entry *src;
  int key = hash(buff);
  fprintf(stderr,"find_event: %s\n", buff);
  src=find_entry(buff, key);
  if (src==((void *)0)){
    return ((void *)0);
  } else {
    fprintf(stderr,"find_event: found path associated with dirname, %s\n", src->first_candidate->path);
    return src->first_candidate->path;
  }
}

variable_types: struct entry { struct entry *prev; struct entry *next; char dirname[254]; struct candidate { struct candidate *prev; struct candidate *next; char path[254]; int score; } *first_candidate; int state; } *

variable_types: key int

user_defined_types: struct entry { struct entry *prev; struct entry *next; char dirname[254]; struct candidate *first_candidate; int state; }


decompiled_code:
__int64 __fastcall read_event(int fd)
{
int v1; // eax
int v2; // ebx
unsigned int v3; // eax
unsigned int v4; // er14
size_t v5; // rax
const char *v6; // rsi
size_t v7; // rax
ssize_t v8; // rax
char vars0[248]; // [rsp+0h] [rbp+0h] BYREF
int anonymous0; // [rsp+F8h] [rbp+F8h]
__int16 anonymous1; // [rsp+FCh] [rbp+FCh]
char varsFE; // [rsp+FEh] [rbp+FEh]

v1 = recv(fd, vars0, 0xFEuLL, 0);
if ( v1 <= 253 )
{
v2 = v1;
while ( 1 )
{
v3 = recv(fd, &vars0[v2], 254 - v2, 64);
v4 = v3;
if ( v3 == -1 )
break;
if ( !v3 )
return (unsigned int)-1;
v2 += v3;
if ( v2 > 253 )
goto LABEL_7;
}
if ( *_errno_location() != 11 )
{
perror("recv");
return v4;
}
}
LABEL_7:
varsFE = 0;
v5 = strlen(vars0);
fprintf(stderr, "buffer len : %lu\n", v5);
fprintf(stderr, "event %c\n", (unsigned int)vars0[0]);
if ( (unsigned int)escape(vars0, 0xFFu, 0LL) == -1 )
return (unsigned int)-1;
if ( vars0[0] == 115 )
{
v4 = 0;
store_event(&vars0[1]);
return v4;
}
if ( vars0[0] != 102 )
{
fprintf(stderr, "read_event: received invalid message : %s\n", vars0);
return (unsigned int)-1;
}
v6 = (const char *)find_event(&vars0[1]);
if ( !v6 )
{
send(fd, "\n", 2uLL, 0);
v4 = 0;
fwrite("read_event: no path corresponding to dirname\n", 1uLL, 0x2DuLL, stderr);
return v4;
}
memset(vars0, 0, sizeof(vars0));
anonymous0 = 0;
anonymous1 = 0;
varsFE = 0;
strncpy(vars0, v6, 0xFFuLL);
if ( (unsigned int)unescape(vars0, 255) == -1 )
return (unsigned int)-1;
v7 = strlen(vars0);
if ( send(fd, vars0, v7 + 1, 0) == -1 )
{
v4 = -1;
perror("send");
}
else
{
v8 = recv(fd, vars0, 2uLL, 0);
v4 = 0;
fprintf(stderr, "read_event: connection closed, ret = %d\n", v8);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd)
{
int v1; // eax
int v2; // ebx
unsigned int v3; // eax
unsigned int v4; // er14
size_t v5; // rax
const char *v6; // rsi
size_t v7; // rax
ssize_t v8; // rax
char vars0[248]; // [rsp+0h] [rbp+0h] BYREF
int anonymous0; // [rsp+F8h] [rbp+F8h]
__int16 anonymous1; // [rsp+FCh] [rbp+FCh]
char varsFE; // [rsp+FEh] [rbp+FEh]

v1 = recv(fd, vars0, 0xFEuLL, 0);
if ( v1 <= 253 )
{
v2 = v1;
while ( 1 )
{
v3 = recv(fd, &vars0[v2], 254 - v2, 64);
v4 = v3;
if ( v3 == -1 )
break;
if ( !v3 )
return (unsigned int)-1;
v2 += v3;
if ( v2 > 253 )
goto LABEL_7;
}
if ( *_errno_location() != 11 )
{
perror("recv");
return v4;
}
}
LABEL_7:
varsFE = 0;
v5 = strlen(vars0);
fprintf(stderr, "buffer len : %lu\n", v5);
fprintf(stderr, "event %c\n", (unsigned int)vars0[0]);
if ( (unsigned int)func1(vars0, 0xFFu, 0LL) == -1 )
return (unsigned int)-1;
if ( vars0[0] == 115 )
{
v4 = 0;
func3(&vars0[1]);
return v4;
}
if ( vars0[0] != 102 )
{
fprintf(stderr, "read_event: received invalid message : %s\n", vars0);
return (unsigned int)-1;
}
v6 = (const char *)func4(&vars0[1]);
if ( !v6 )
{
send(fd, "\n", 2uLL, 0);
v4 = 0;
fwrite("read_event: no path corresponding to dirname\n", 1uLL, 0x2DuLL, stderr);
return v4;
}
memset(vars0, 0, sizeof(vars0));
anonymous0 = 0;
anonymous1 = 0;
varsFE = 0;
strncpy(vars0, v6, 0xFFuLL);
if ( (unsigned int)func2(vars0, 255) == -1 )
return (unsigned int)-1;
v7 = strlen(vars0);
if ( send(fd, vars0, v7 + 1, 0) == -1 )
{
v4 = -1;
perror("send");
}
else
{
v8 = recv(fd, vars0, 2uLL, 0);
v4 = 0;
fprintf(stderr, "read_event: connection closed, ret = %d\n", v8);
}
return v4;
}

original_code:
int read_event(int sockfd){
  char buffer[254 +1];
  int data_read=0;
  int ret=0;
  data_read = recv(sockfd, buffer, 254, 0);
  while(data_read < 254){
    ret = recv(sockfd, buffer+data_read, 254 -data_read, MSG_DONTWAIT);
    if (ret == -1) {
      if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11) {
        break;
      } else {
        perror("recv");
        return -1;
      }
    } else if (ret == 0) {
      return -1;
    }
    data_read+=ret;
  }
  buffer[254]='\0';
  fprintf(stderr,"buffer len : %lu\n",strlen(buffer));
  fprintf(stderr,"event %c\n",buffer[0]);
  if (escape(buffer, 254 +1, ((void *)0)) == -1) {
    return -1;
  }
  if (buffer[0] == 's') {
    store_event(buffer+1);
  } else if (buffer[0] == 'f') {
    const char * path;
    path=find_event(buffer+1);
    if(path == ((void *)0)){
      send(sockfd, "\n", 2, 0);
      fprintf(stderr,"read_event: no path corresponding to dirname\n");
    } else {
      memset(buffer, 0, 254 +1);
      strncpy(buffer, path, 254 +1);
      if (unescape(buffer, 254 +1) == -1) {
        return -1;
      }
      if(send(sockfd, buffer, strlen(buffer)+1, 0) == -1) {
        perror("send");
        return -1;
      }
      ret = recv(sockfd, buffer, 2, 0);
      fprintf(stderr,"read_event: connection closed, ret = %d\n", ret);
    }
  } else {
    fprintf(stderr,"read_event: received invalid message : %s\n", buffer);
    return -1;
  }
  return 0;
}

canonical_original_code:
int read_event(int sockfd){
  char buffer[-1];
  int data_read = 0;
  int ret = 0;
  data_read = recv(sockfd, buffer, 254, 0);
  while(data_read < 254){
    ret = recv(sockfd, buffer+data_read, 254 -data_read, MSG_DONTWAIT);
    if (ret == -1) {
      if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11) {
        break;
      } else {
        perror("recv");
        return -1;
      }
    } else if (ret == 0) {
      return -1;
    }
    data_read+=ret;
  }
  buffer[254]='\0';
  fprintf(stderr,"buffer len : %lu\n",strlen(buffer));
  fprintf(stderr,"event %c\n",buffer[0]);
  if (escape(buffer, 254 +1, ((void *)0)) == -1) {
    return -1;
  }
  if (buffer[0] == 's') {
    store_event(buffer+1);
  } else if (buffer[0] == 'f') {
    char *path;
    path=find_event(buffer+1);
    if(path == ((void *)0)){
      send(sockfd, "\n", 2, 0);
      fprintf(stderr,"read_event: no path corresponding to dirname\n");
    } else {
      memset(buffer, 0, 254 +1);
      strncpy(buffer, path, 254 +1);
      if (unescape(buffer, 254 +1) == -1) {
        return -1;
      }
      if(send(sockfd, buffer, strlen(buffer)+1, 0) == -1) {
        perror("send");
        return -1;
      }
      ret = recv(sockfd, buffer, 2, 0);
      fprintf(stderr,"read_event: connection closed, ret = %d\n", ret);
    }
  } else {
    fprintf(stderr,"read_event: received invalid message : %s\n", buffer);
    return -1;
  }
  return 0;
}

variable_types: sockfd int

variable_types:
buffer
char [-1]

variable_types: ret int


decompiled_code:
__int64 pop_event()
{
int v0; // eax
unsigned int v1; // er12
int v2; // edi

v0 = accept(socklisten, 0LL, 0LL);
v1 = v0;
if ( v0 == -1 )
{
perror("accept");
}
else
{
read_event(v0);
v2 = v1;
v1 = 0;
close(v2);
}
return v1;
}

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

v0 = accept(socklisten, 0LL, 0LL);
v1 = v0;
if ( v0 == -1 )
{
perror("accept");
}
else
{
func5(v0);
v2 = v1;
v1 = 0;
close(v2);
}
return v1;
}

original_code:
int pop_event(){
  int sockcon;
  sockcon=accept(socklisten, ((void *)0), ((void *)0));
  if(sockcon==-1){
    perror("accept");
    return -1;
  }
  read_event(sockcon);
  close(sockcon);
  return 0;
}

variable_types: sockcon int


init

decompiled_code:
__int64 init()
{
unsigned int v0; // er12
int v2; // edi
__int16 v3; // [rsp+0h] [rbp-118h] BYREF
__m128i v4; // [rsp+2h] [rbp-116h]
struct sigaction v5; // [rsp+70h] [rbp-A8h] BYREF

v5.sa_handler = (__sighandler_t)end;
memset(&v5.sa_mask, 0, 0x90uLL);
if ( !(unsigned int)init_serial("r") )
{
read_next_entry();
end_serial();
}
socklisten = socket(1, 1, 0);
v0 = socklisten;
if ( socklisten == -1 )
{
perror("socket");
return v0;
}
v3 = 1;
v4 = _mm_load_si128((const __m128i *)&LC20);
if ( bind(socklisten, (const struct sockaddr *)&v3, 0x6Eu) == -1 )
{
perror("bind");
v0 = socklisten;
goto LABEL_12;
}
chmod("/tmp/gtd.socket", 0x1FFu);
if ( listen(v0, 1) == -1 )
{
perror("listen");
v0 = socklisten;
LABEL_11:
unlink("/tmp/gtd.socket");
LABEL_12:
v2 = v0;
v0 = -1;
close(v2);
return v0;
}
if ( sigaction(2, &v5, 0LL) == -1 || sigaction(15, &v5, 0LL) == -1 )
goto LABEL_11;
return 0;
}

canonical_decompiled_code:
__int64 func7()
{
unsigned int v0; // er12
int v2; // edi
__int16 v3; // [rsp+0h] [rbp-118h] BYREF
__m128i v4; // [rsp+2h] [rbp-116h]
struct sigaction v5; // [rsp+70h] [rbp-A8h] BYREF

v5.sa_handler = (__sighandler_t)end;
memset(&v5.sa_mask, 0, 0x90uLL);
if ( !(unsigned int)init_serial("r") )
{
read_next_entry();
end_serial();
}
socklisten = socket(1, 1, 0);
v0 = socklisten;
if ( socklisten == -1 )
{
perror("socket");
return v0;
}
v3 = 1;
v4 = _mm_load_si128((const __m128i *)&LC20);
if ( bind(socklisten, (const struct sockaddr *)&v3, 0x6Eu) == -1 )
{
perror("bind");
v0 = socklisten;
goto LABEL_12;
}
chmod("/tmp/gtd.socket", 0x1FFu);
if ( listen(v0, 1) == -1 )
{
perror("listen");
v0 = socklisten;
LABEL_11:
unlink("/tmp/gtd.socket");
LABEL_12:
v2 = v0;
v0 = -1;
close(v2);
return v0;
}
if ( sigaction(2, &v5, 0LL) == -1 || sigaction(15, &v5, 0LL) == -1 )
goto LABEL_11;
return 0;
}

original_code:
int init(){
  int ret=0;
  struct sockaddr_un server;
 struct sigaction act = { .__sigaction_handler.sa_handler=end, .sa_flags = 0, };
  if(!init_serial("r")) {
    read_next_entry();
    end_serial();
  }
  socklisten=socket(1, SOCK_STREAM,0);
  if (socklisten==-1){
    perror("socket");
    goto out;
  }
  server.sun_family=1;
  strcpy(server.sun_path,"/tmp/gtd.socket");
  ret=bind(socklisten,(struct sockaddr*)&server,sizeof(struct sockaddr_un));
  if (ret==-1){
    perror("bind");
    goto outbind;
  }
  chmod("/tmp/gtd.socket", (0400|0200|0100) | ((0400|0200|0100) >> 3) | (((0400|0200|0100) >> 3) >> 3));
  ret=listen(socklisten,1);
  if (ret==-1){
    perror("listen");
    goto outlisten;
  }
 if(sigaction(2, &act, ((void *)0))==-1) goto outsig;
 if(sigaction(15, &act, ((void *)0))==-1) goto outsig;
  return 0;
outsig:
outlisten:
  unlink("/tmp/gtd.socket");
outbind:
  close(socklisten);
out:
  return -1;
}

canonical_original_code:
int init(){
  int ret = 0;
  struct sockaddr_un server;
 struct sigaction act = { .__sigaction_handler.sa_handler=end, .sa_flags = 0, };
  if(!init_serial("r")) {
    read_next_entry();
    end_serial();
  }
  socklisten=socket(1, SOCK_STREAM,0);
  if (socklisten==-1){
    perror("socket");
    goto out;
  }
  server.sun_family=1;
  strcpy(server.sun_path,"/tmp/gtd.socket");
  ret=bind(socklisten,(struct sockaddr *)&server,sizeof(struct sockaddr_un));
  if (ret==-1){
    perror("bind");
    goto outbind;
  }
  chmod("/tmp/gtd.socket", (0400|0200|0100) | ((0400|0200|0100) >> 3) | (((0400|0200|0100) >> 3) >> 3));
  ret=listen(socklisten,1);
  if (ret==-1){
    perror("listen");
    goto outlisten;
  }
 if(sigaction(2, &act, ((void *)0))==-1) goto outsig;
 if(sigaction(15, &act, ((void *)0))==-1) goto outsig;
  return 0;
outsig:
outlisten:
  unlink("/tmp/gtd.socket");
outbind:
  close(socklisten);
out:
  return -1;
}

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

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


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( (unsigned int)init(argc, argv, envp) != -1 )
{
while ( (unsigned int)pop_event() != -1 )
;
end(0LL);
}
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( (unsigned int)func7(argc, argv, envp) != -1 )
{
while ( (unsigned int)func6() != -1 )
;
func0(0LL);
}
return 1;
}

original_code:
int main(int argc, char * argv[]){
  if (init()==-1){
    return 1;
  }
  while(1){
    if(pop_event()==-1) end(0);
  }
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[]){
  if (init()==-1){
    return 1;
  }
  while(1){
    if(pop_event()==-1) end(0);
  }
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


move

decompiled_code:
__int64 __fastcall move(const char *a1)
{
int v1; // ebx

if ( !a1 )
return 0LL;
v1 = chdir(a1);
return (system("cd /home/") == -1) | (unsigned __int8)(v1 == -1);
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1)
{
int v1; // ebx

if ( !a1 )
return 0LL;
v1 = chdir(a1);
return (system("cd /home/") == -1) | (unsigned __int8)(v1 == -1);
}

original_code:
int move(const char *path){
  int chret=0;
  int sysret=0;
  if (!path){
    ;
    return 0;
  }
  switch(path[0]){
    case '/':
    case '~':
    default:
      chret=chdir(path);
      sysret=system("cd /home/");
      break;
  }
  if(chret==-1 || sysret==-1){
    ;
    return 1;
  }
  return 0;
}

canonical_original_code:
int move(char *path){
  int chret = 0;
  int sysret = 0;
  if (!path){
    ;
    return 0;
  }
  switch(path[0]){
    case '/':
    case '~':
    default:
      chret=chdir(path);
      sysret=system("cd /home/");
      break;
  }
  if(chret==-1 || sysret==-1){
    ;
    return 1;
  }
  return 0;
}

variable_types: chret int

variable_types: sysret int