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 *


repo: rbu/driftnet

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

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

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

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

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

return_type: void

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)
{
_QWORD *v2; // rax

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

canonical_decompiled_code:
int __fastcall func4(void *src, size_t n)
{
_QWORD *v2; // rax

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

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

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


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:
unsigned __int8 *__fastcall jpeg_skip_block(unsigned __int8 *a1, unsigned __int64 a2)
{
unsigned __int64 v2; // rax
unsigned __int8 *v3; // rdi
bool v4; // cc
unsigned __int8 *result; // rax

if ( a2 <= 1 )
return 0LL;
v2 = a1[1] | (*a1 << 8);
v3 = &a1[v2];
v4 = v2 <= a2;
result = 0LL;
if ( v4 )
result = v3;
return result;
}

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

if ( a2 <= 1 )
return 0LL;
v2 = a1[1] | (*a1 << 8);
v3 = &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 *v3; // rbx
unsigned __int64 v4; // r13
__int64 v5; // rdi
unsigned int v6; // er14
char s1[4]; // [rsp+Bh] [rbp-2Dh] BYREF
char v9; // [rsp+Fh] [rbp-29h]

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

canonical_decompiled_code:
char *__fastcall func4(__int64 a1, __int64 a2)
{
__int64 *v3; // rbx
unsigned __int64 v4; // r13
__int64 v5; // rdi
unsigned int v6; // er14
char s1[4]; // [rsp+Bh] [rbp-2Dh] BYREF
char v9; // [rsp+Fh] [rbp-29h]

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

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


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 )
abort();
return result;
}

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

result = malloc(a1);
if ( !result )
abort();
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 )
abort();
return result;
}

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

result = calloc(a1, a2);
if ( !result )
abort();
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 )
abort();
}
return result;
}

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

result = realloc(a1, a2);
if ( a2 )
{
if ( !result )
abort();
}
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; // rbx
unsigned int v6; // ebp
time_t v7; // rax
int result; // eax
int v9; // ebp
__int64 v10[13]; // [rsp+0h] [rbp-68h] BYREF

v10[0] = 0LL;
v10[1] = 0LL;
v10[2] = 0LL;
v10[3] = 0LL;
v10[4] = 0LL;
v10[5] = 0LL;
v10[6] = 0LL;
v10[7] = 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; // rbx
unsigned int v6; // ebp
time_t v7; // rax
int result; // eax
int v9; // ebp
__int64 v10[13]; // [rsp+0h] [rbp-68h] BYREF

v10[0] = 0LL;
v10[1] = 0LL;
v10[2] = 0LL;
v10[3] = 0LL;
v10[4] = 0LL;
v10[5] = 0LL;
v10[6] = 0LL;
v10[7] = 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; // ebx
char *v4; // rax

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

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

v2 = memcmp(s, "driftnet-", 9uLL) == 0;
v3 = !v2;
if ( !v2 )
return 0;
v4 = strrchr(s, 46);
if ( v4 )
{
v3 = 1;
if ( strcmp(v4, ".jpeg") )
{
if ( strcmp(v4, ".gif") && 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 *

return_type: int


decompiled_code:
void __fastcall connection_extract_media(__int64 a1, int a2)
{
int *i; // rbp
unsigned __int64 v3; // rbx
__int64 v4; // rdx
int v5; // ecx
__int64 v6; // r15
unsigned __int64 j; // r12
const void *v8; // [rsp+0h] [rbp-48h] BYREF
size_t v9[8]; // [rsp+8h] [rbp-40h] BYREF

for ( i = *(int **)(a1 + 48); i; i = (int *)*((_QWORD *)i + 4) )
{
if ( i[1] > 0 && i[7] )
{
for ( j = 0LL; j != 20; j += 4LL )
{
if ( (dword_4A8[2 * j] & a2) != 0 )
{
v3 = *(_QWORD *)(a1 + 24) + i[j / 4 + 2] + (__int64)*i;
if ( v3 )
{
while ( 1 )
{
v4 = *(_QWORD *)(a1 + 24);
v5 = i[1];
if ( v3 >= v4 + v5 + (__int64)*i )
break;
v6 = ((__int64 (__fastcall *)(unsigned __int64, unsigned __int64, const void **, size_t *))*(&funcs_38C + j))(
v3,
v5 + *i - (v3 - v4),
&v8,
v9);
if ( v8 && (!max_tmpfiles || (int)count_temporary_files() < max_tmpfiles) )
((void (__fastcall *)(const char *, const void *, size_t))*(&off_4B8 + j))((&driver)[j], v8, v9[0]);
if ( v6 == v3 )
break;
v3 = v6;
}
}
i[j / 4 + 2] = v3 - *(_DWORD *)(a1 + 24) - *i;
}
}
i[7] = 0;
}
}
}

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)
{
__int64 result; // rax

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

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

if ( a1[1] == 1 )
result = (unsigned int)(4 * (a1[6] + 12000 * a1[4] / a1[5]));
else
result = (unsigned int)(a1[6] + 144000 * a1[4] / a1[5]);
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; // rbp
char *v6; // r12
int v7; // er13
int v8; // eax
char *v9; // rbx
char *v10; // rax
char v13[120]; // [rsp+10h] [rbp-78h] BYREF

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

canonical_decompiled_code:
char *__fastcall func0(void *s, unsigned __int64 a2, _QWORD *a3, _QWORD *a4)
{
char *v4; // rbp
char *v6; // r12
int v7; // er13
int v8; // eax
char *v9; // rbx
char *v10; // rax
char v13[120]; // [rsp+10h] [rbp-78h] BYREF

*a3 = 0LL;
v4 = (char *)s;
if ( a2 <= 3 )
return v4;
v6 = (char *)s + a2 - 4;
if ( s >= v6 )
return v4;
while ( 1 )
{
v10 = (char *)memchr(v4, 255, a2 - 4 - (v4 - (_BYTE *)s));
v9 = v10;
v4 = v10;
if ( !v10 )
return (char *)s + a2 - 4;
if ( (v10[1] & 0xE0) != 0xE0 )
{
v4 = v10 + 1;
goto LABEL_12;
}
if ( (unsigned int)mpeg_hdr_parse(v10, v13) )
break;
v4 = v9 + 1;
LABEL_12:
if ( v4 >= v6 )
return v4;
}
v7 = 0;
while ( 1 )
{
++v7;
v8 = mpeg_hdr_nextframe_offset(v13);
if ( !v8 )
return v9 + 1;
v9 += v8;
if ( v9 >= v6 || v7 > 99 )
break;
if ( !(unsigned int)mpeg_hdr_parse(v9, v13) )
return v4;
}
if ( v7 > 99 )
{
*a3 = v4;
*a4 = v9 - v4;
v4 = v9;
}
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

canonical_name: 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") )
{
puts("Debug On");
v3 = argv + 1;
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") )
{
puts("Debug On");
v3 = argv + 1;
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 v3; // er14
unsigned int v4; // er15
int v6; // eax
char *v7; // rax
size_t v8; // rax
unsigned int v9; // er12
int *v10; // rbx
int v11; // eax
int v12; // ebp
char *v13; // rax
char *v14; // rax
char *v15; // rax
size_t v16; // rax
char *v17; // rax
unsigned int v19; // [rsp+Ch] [rbp-4Ch]
char buf[72]; // [rsp+10h] [rbp-48h] BYREF

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

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

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

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; // r15
int v4; // eax
int v5; // er13
char *v6; // rbp
int v7; // er12
char *v8; // rbx
__int64 result; // rax

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

canonical_decompiled_code:
__int64 __fastcall func1(char *s, unsigned int a2, _DWORD *a3)
{
char *v3; // r15
int v4; // eax
int v5; // er13
char *v6; // rbp
int v7; // er12
char *v8; // rbx
__int64 result; // rax

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

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; // rbx
char *i; // rbp
char *v4; // rax
char v5; // al

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

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

v2 = s;
s[a2 - 1] = 0;
for ( i = &s[(int)(strlen(s) + 1)]; ; memmove(v2, v2 + 1, i - v2) )
{
v4 = strchr(v2, 92);
v2 = v4;
if ( !v4 )
break;
v5 = v4[1];
--i;
if ( v5 != 92 && v5 != 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; // rbx

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

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

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

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; // rbx

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; // rbx

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; // er13
size_t v5; // rax
const char *v6; // rsi
size_t v7; // rax
ssize_t v8; // rax
char buf[248]; // [rsp+0h] [rbp-128h] BYREF
int v11; // [rsp+F8h] [rbp-30h]
__int16 v12; // [rsp+FCh] [rbp-2Ch]
char v13; // [rsp+FEh] [rbp-2Ah]

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

canonical_decompiled_code:
__int64 __fastcall func5(int fd)
{
int v1; // eax
int v2; // ebx
unsigned int v3; // eax
unsigned int v4; // er13
size_t v5; // rax
const char *v6; // rsi
size_t v7; // rax
ssize_t v8; // rax
char buf[248]; // [rsp+0h] [rbp-128h] BYREF
int v11; // [rsp+F8h] [rbp-30h]
__int16 v12; // [rsp+FCh] [rbp-2Ch]
char v13; // [rsp+FEh] [rbp-2Ah]

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

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

canonical_decompiled_code:
__int64 func6()
{
int v0; // eax
unsigned int v1; // ebx

v0 = accept(socklisten, 0LL, 0LL);
v1 = v0;
if ( v0 == -1 )
{
perror("accept");
}
else
{
func5(v0);
close(v1);
v1 = 0;
}
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; // ebx
struct sigaction v2; // [rsp+0h] [rbp-118h] BYREF
struct sockaddr addr; // [rsp+A0h] [rbp-78h] BYREF

memset(&v2.sa_mask, 0, 0x90uLL);
v2.sa_handler = (__sighandler_t)end;
if ( !(unsigned int)init_serial("r") )
{
read_next_entry();
end_serial();
}
v0 = socket(1, 1, 0);
socklisten = v0;
if ( v0 == -1 )
{
perror("socket");
return v0;
}
addr.sa_family = 1;
strcpy(addr.sa_data, "/tmp/gtd.socket");
if ( bind(v0, &addr, 0x6Eu) == -1 )
{
perror("bind");
LABEL_14:
close(socklisten);
return (unsigned int)-1;
}
chmod("/tmp/gtd.socket", 0x1FFu);
if ( listen(v0, 1) == -1 )
{
perror("listen");
LABEL_13:
unlink("/tmp/gtd.socket");
goto LABEL_14;
}
if ( sigaction(2, &v2, 0LL) == -1 || sigaction(15, &v2, 0LL) == -1 )
goto LABEL_13;
return 0;
}

canonical_decompiled_code:
__int64 func7()
{
unsigned int v0; // ebx
struct sigaction v2; // [rsp+0h] [rbp-118h] BYREF
struct sockaddr addr; // [rsp+A0h] [rbp-78h] BYREF

memset(&v2.sa_mask, 0, 0x90uLL);
v2.sa_handler = (__sighandler_t)end;
if ( !(unsigned int)init_serial("r") )
{
read_next_entry();
end_serial();
}
v0 = socket(1, 1, 0);
socklisten = v0;
if ( v0 == -1 )
{
perror("socket");
return v0;
}
addr.sa_family = 1;
strcpy(addr.sa_data, "/tmp/gtd.socket");
if ( bind(v0, &addr, 0x6Eu) == -1 )
{
perror("bind");
LABEL_14:
close(socklisten);
return (unsigned int)-1;
}
chmod("/tmp/gtd.socket", 0x1FFu);
if ( listen(v0, 1) == -1 )
{
perror("listen");
LABEL_13:
unlink("/tmp/gtd.socket");
goto LABEL_14;
}
if ( sigaction(2, &v2, 0LL) == -1 || sigaction(15, &v2, 0LL) == -1 )
goto LABEL_13;
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)
{
__int64 result; // rax
int v2; // ebx

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

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

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

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


check

decompiled_code:
__int64 __fastcall check(char *a1, char *a2)
{
char v2; // al

if ( *a1 == 45 )
{
v2 = a1[1];
if ( v2 == 76 )
{
a1 = a2;
}
else if ( v2 > 76 )
{
if ( v2 == 80 )
a1 = a2;
}
else if ( (v2 & 0xDF) == 0 )
{
a1 = getenv("OLDPWD");
if ( !a1 )
return 0LL;
}
}
return move(a1);
}

canonical_decompiled_code:
__int64 __fastcall func8(char *a1, char *a2)
{
char v2; // al

if ( *a1 == 45 )
{
v2 = a1[1];
if ( v2 == 76 )
{
a1 = a2;
}
else if ( v2 > 76 )
{
if ( v2 == 80 )
a1 = a2;
}
else if ( (v2 & 0xDF) == 0 )
{
a1 = getenv("OLDPWD");
if ( !a1 )
return 0LL;
}
}
return func7(a1);
}

original_code:
int check(const char * opt, const char * path){
  const char * target=path;
  if (opt[0]=='-'){
    switch (opt[1]){
      case '\0':
      case ' ':
        target=getenv("OLDPWD");
        if (!target) return 0;
        break;
      case 'L':
      case 'P':
        break;
      default:
        target=opt;
        break;
    }
  }else{
    target=opt;
  }
  return move(target);
}

canonical_original_code:
int check(char *opt, char *path){
  char *target = path;
  if (opt[0]=='-'){
    switch (opt[1]){
      case '\0':
      case ' ':
        target=getenv("OLDPWD");
        if (!target) return 0;
        break;
      case 'L':
      case 'P':
        break;
      default:
        target=opt;
        break;
    }
  }else{
    target=opt;
  }
  return move(target);
}

variable_types:
opt
char *

variable_types:
target
char *


isrel

decompiled_code:
__int64 __fastcall isrel(_BYTE *a1)
{
return (*a1 != 47) & (unsigned __int8)(*a1 != 126);
}

canonical_decompiled_code:
__int64 __fastcall func9(_BYTE *a1)
{
return (*a1 != 47) & (unsigned __int8)(*a1 != 126);
}

original_code:
int isrel(const char * path){
  return (path[0]=='/' || path[0]=='~') ? 0 : 1;
}

canonical_original_code:
int isrel(char *path){
  return (path[0]=='/' || path[0]=='~') ? 0 : 1;
}


isdir

decompiled_code:
_BOOL8 __fastcall isdir(const char *a1)
{
int v1; // eax

v1 = open(a1, 2);
if ( v1 == -1 )
return *__errno_location() == 21;
close(v1);
return 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func10(const char *a1)
{
int v1; // eax

v1 = open(a1, 2);
if ( v1 == -1 )
return *__errno_location() == 21;
close(v1);
return 0LL;
}

original_code:
int isdir(const char * path){
  int ret=0;
  ret=open(path,02);
  if (ret!=-1){
    close(ret);
    ;
    return 0;
  } else {
    if ((*__errno_location ())==21){
      ;
      return 1;
    }
    ;
    return 0;
  }
  return 0;
}

canonical_original_code:
int isdir(char *path){
  int ret = 0;
  ret=open(path,02);
  if (ret!=-1){
    close(ret);
    ;
    return 0;
  } else {
    if ((*__errno_location ())==21){
      ;
      return 1;
    }
    ;
    return 0;
  }
  return 0;
}


original_code:
int push_event(char event, const char *path, int relative) {
  char buff[254 +1];
  int sockfd;
  int ret;
  int length;
  struct sockaddr_un server={ .sun_family=1, };
  strcpy(server.sun_path,"/tmp/gtd.socket");
  sockfd=socket(1, SOCK_STREAM,0);
  ret=connect(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr_un));
  if(ret==-10){
    ;
    close(sockfd);
    return -1;
  }
  buff[0] = event;
  length = strlen(path);
  length = length>254 -1 ? 254 -1 : length;
  if (event == 's') {
    realpath(path, buff+1);
    length = strlen(buff);
  } else {
    length++;
    memcpy(buff+1, path, length);
  }
  ;
  ;
  ret=send(sockfd, buff, length+1, 0);
  if(ret==-1){
    ;
    return -1;
  }
  if (event == 'f') {
    int data_read=recv(sockfd,buff,254,0);
    while(data_read<254){
      ret=recv(sockfd,buff+data_read,254 -data_read,MSG_DONTWAIT);
      if (ret==-1){
        if ((*__errno_location ())==11 || (*__errno_location ())==11){
          break;
        } else {
          ;
          return -1;
        }
      } else if (ret == 0){
        ;
        return -1;
      }
      data_read+=ret;
    }
    if (data_read < 0) {
      ;
      return -1;
    }
    close(sockfd);
    buff[data_read]='\0';
    ;
    if (buff[0] == '\n') {
      ;
      return -1;
    }
    printf("%s\n", buff);
  } else if (event == 's') {
    printf("%s\n", path);
  }
  return 0;
}

canonical_original_code:
int push_event(char event, char *path, int relative) {
  char buff[-1];
  int sockfd;
  int ret;
  int length;
  struct sockaddr_un server = { .sun_family=1, };
  strcpy(server.sun_path,"/tmp/gtd.socket");
  sockfd=socket(1, SOCK_STREAM,0);
  ret=connect(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr_un));
  if(ret==-10){
    ;
    close(sockfd);
    return -1;
  }
  buff[0] = event;
  length = strlen(path);
  length = length>254 -1 ? 254 -1 : length;
  if (event == 's') {
    realpath(path, buff+1);
    length = strlen(buff);
  } else {
    length++;
    memcpy(buff+1, path, length);
  }
  ;
  ;
  ret=send(sockfd, buff, length+1, 0);
  if(ret==-1){
    ;
    return -1;
  }
  if (event == 'f') {
    int data_read = recv(sockfd,buff,254,0);
    while(data_read<254){
      ret=recv(sockfd,buff+data_read,254 -data_read,MSG_DONTWAIT);
      if (ret==-1){
        if ((*__errno_location ())==11 || (*__errno_location ())==11){
          break;
        } else {
          ;
          return -1;
        }
      } else if (ret == 0){
        ;
        return -1;
      }
      data_read+=ret;
    }
    if (data_read < 0) {
      ;
      return -1;
    }
    close(sockfd);
    buff[data_read]='\0';
    ;
    if (buff[0] == '\n') {
      ;
      return -1;
    }
    printf("%s\n", buff);
  } else if (event == 's') {
    printf("%s\n", path);
  }
  return 0;
}

variable_types: event char

variable_types: relative int

variable_types:
buff
char [-1]

variable_types: length int

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


handle

decompiled_code:
__int64 __fastcall handle(const char *a1)
{
if ( a1 )
{
if ( isdir(a1) )
{
if ( (unsigned int)push_event(115, a1) != -1 )
return 0LL;
}
else if ( (unsigned int)push_event(102, a1) != -1 )
{
return 0LL;
}
printf("%s", a1);
return 0LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(const char *a1)
{
if ( a1 )
{
if ( func10(a1) )
{
if ( (unsigned int)func11(115, a1) != -1 )
return 0LL;
}
else if ( (unsigned int)func11(102, a1) != -1 )
{
return 0LL;
}
printf("%s", a1);
return 0LL;
}
return 0LL;
}

original_code:
int handle(const char * path){
  if (!path) return 0;
  if (isdir(path) == 1) {
    ;
    if (push_event('s', path, 1) == -1) {
      printf("%s", path);
    }
    return 0;
  } else {
    ;
    if (push_event('f', path, 0) == -1) {
      printf("%s", path);
    }
  }
  return 0;
}

canonical_original_code:
int handle(char *path){
  if (!path) return 0;
  if (isdir(path) == 1) {
    ;
    if (push_event('s', path, 1) == -1) {
      printf("%s", path);
    }
    return 0;
  } else {
    ;
    if (push_event('f', path, 0) == -1) {
      printf("%s", path);
    }
  }
  return 0;
}

variable_types:
path
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int v4; // eax

result = 0;
if ( argc != 1 )
{
if ( argc == 2 )
{
result = handle(argv[1]);
}
else
{
v4 = 1;
if ( argc <= 1 )
{
result = 0;
}
else
{
do
++v4;
while ( argc != v4 );
result = 0;
}
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
int v4; // eax

result = 0;
if ( argc != 1 )
{
if ( argc == 2 )
{
result = func12(argv[1]);
}
else
{
v4 = 1;
if ( argc <= 1 )
{
result = 0;
}
else
{
do
++v4;
while ( argc != v4 );
result = 0;
}
}
}
return result;
}

original_code:
int main(int argc, char *argv[]){
  int i;
  switch(argc){
    case 1:
      return 0;
      break;
    case 2:
      return handle(argv[1]);
      break;
    default:
      for (i=1; i<argc; i++){
        ;
      }
  }
  return 0;
}

variable_types: i int


dumpTable

decompiled_code:
__int64 dumpTable()
{
__int64 *v0; // r12
__int64 v1; // rbx

fwrite("dumping histfile\n", 1uLL, 0x11uLL, stderr);
init_serial("w");
v0 = &htable;
do
{
v1 = *v0;
if ( *v0 )
{
do
{
dump_entry(v1);
v1 = *(_QWORD *)(v1 + 8);
}
while ( *v0 != v1 );
}
++v0;
}
while ( v0 != (__int64 *)(unsigned int)((_DWORD)&htable + 424) );
fwrite("end: removing resources\n", 1uLL, 0x18uLL, stderr);
return end_serial();
}

canonical_decompiled_code:
__int64 func0()
{
__int64 *v0; // r12
__int64 v1; // rbx

fwrite("dumping histfile\n", 1uLL, 0x11uLL, stderr);
init_serial("w");
v0 = &htable;
do
{
v1 = *v0;
if ( *v0 )
{
do
{
dump_entry(v1);
v1 = *(_QWORD *)(v1 + 8);
}
while ( *v0 != v1 );
}
++v0;
}
while ( v0 != (__int64 *)(unsigned int)((_DWORD)&htable + 424) );
fwrite("end: removing resources\n", 1uLL, 0x18uLL, stderr);
return end_serial();
}

original_code:
void dumpTable() {
  int i = 0;
  fprintf(stderr,"dumping histfile\n");
  init_serial("w");
  for(; i < 53; i++) {
    if(htable[i] != ((void *)0)) {
      struct entry *j = htable[i];
      do {
        dump_entry(j);
        j = j->next;
      } while (j != htable[i]);
    }
  }
  fprintf(stderr,"end: removing resources\n");
  end_serial();
}

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