decompiled_code:
__int64 __fastcall reduce_sum(_QWORD **a1, unsigned int a2)
{
__int64 v2; // rbp
int *v3; // r12
int *v4; // rbp
int v5; // er13
int v6; // er14

v2 = (*a1)[1];
v3 = (int *)reduce(**a1);
v4 = (int *)reduce(v2);
v5 = *v3;
v6 = *v4;
free_money(v3);
free_money(v4);
return create_money((unsigned int)(v5 + v6), a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD **a1, unsigned int a2)
{
__int64 v2; // rbp
int *v3; // r12
int *v4; // rbp
int v5; // er13
int v6; // er14

v2 = (*a1)[1];
v3 = (int *)reduce(**a1);
v4 = (int *)reduce(v2);
v5 = *v3;
v6 = *v4;
free_money(v3);
free_money(v4);
return create_money((unsigned int)(v5 + v6), a2);
}

original_code:
static Money *
reduce_sum( const Expression *exp, Currency to ){
  Sum *sum = exp->value;
  Expression *augend_exp = ( Expression * ) sum->augend;
  Expression *addend_exp = ( Expression * ) sum->addend;
  MoneyProtected *reduced_augend = ( MoneyProtected * ) reduce( augend_exp, to );
  MoneyProtected *reduced_addend = ( MoneyProtected * ) reduce( addend_exp, to );
  unsigned int augend = reduced_augend->amount;
  unsigned int addend = reduced_addend->amount;
  free_money( ( Money * ) reduced_augend );
  free_money( ( Money * ) reduced_addend );
  return create_money( augend + addend, to );
}

canonical_original_code:
static struct Money *reduce_sum(struct Expression *exp, enum Currency to){
  struct Sum *sum = exp->value;
  struct Expression *augend_exp = ( Expression * ) sum->augend;
  struct Expression *addend_exp = ( Expression * ) sum->addend;
  struct MoneyProtected *reduced_augend = ( MoneyProtected * ) reduce( augend_exp, to );
  struct MoneyProtected *reduced_addend = ( MoneyProtected * ) reduce( addend_exp, to );
  unsigned int augend = reduced_augend->amount;
  unsigned int addend = reduced_addend->amount;
  free_money( ( struct Money * ) reduced_augend );
  free_money( ( struct Money * ) reduced_addend );
  return create_money( augend + addend, to );
}

variable_types:
to
enum Currency { USD=0, CHF=1 }

variable_types: struct Sum { struct Expression { void *value; struct Money {  } *(*reduce)(struct Expression *exp, enum Currency { USD=0, CHF=1 } to); } *augend; struct Expression *addend; } *

variable_types:
augend
unsigned int

variable_types:
addend
unsigned int

user_defined_types: struct Sum { struct Expression *augend; struct Expression *addend; }


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

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

original_code:
unsigned int
hash_pair( const void *key ) {
  return 0;
}

canonical_original_code:
unsigned int hash_pair(void *key) {
  return 0;
}

variable_types:
key
void *

return_type: unsigned int


decompiled_code:
_BOOL8 __fastcall compare_pair(_DWORD *a1, _DWORD *a2)
{
bool v2; // al

v2 = 0;
if ( *a1 == *a2 )
v2 = a1[1] == a2[1];
return v2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(_DWORD *a1, _DWORD *a2)
{
bool v2; // al

v2 = 0;
if ( *a1 == *a2 )
v2 = a1[1] == a2[1];
return v2;
}

original_code:
_Bool
compare_pair( const void *x, const void *y ) {
  const Pair *px = x;
  const Pair *py = y;
  return px->from == py->from && px->to == py->to;
}

canonical_original_code:
_Bool compare_pair(void *x, void *y) {
  struct Pair *px = x;
  struct Pair *py = y;
  return px->from == py->from && px->to == py->to;
}

variable_types:
x
void *

variable_types:
y
void *

variable_types: struct Pair { enum Currency { USD=0, CHF=1 } from; enum Currency { USD=0, CHF=1 } to; } *

user_defined_types: struct Pair { enum Currency from; enum Currency to; }


decompiled_code:
__int64 __fastcall reduce_mul(_QWORD **a1, unsigned int a2)
{
_QWORD *v2; // r12
int *v3; // rax
int v4; // ebp

v2 = *a1;
v3 = (int *)reduce(**a1);
v4 = *v3;
LODWORD(v2) = *((_DWORD *)v2 + 2);
free_money(v3);
return create_money((unsigned int)((_DWORD)v2 * v4), a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD **a1, unsigned int a2)
{
_QWORD *v2; // r12
int *v3; // rax
int v4; // ebp

v2 = *a1;
v3 = (int *)reduce(**a1);
v4 = *v3;
LODWORD(v2) = *((_DWORD *)v2 + 2);
free_money(v3);
return create_money((unsigned int)((_DWORD)v2 * v4), a2);
}

original_code:
static Money *
reduce_mul( const Expression *exp, Currency to ){
  Mul *mul = exp->value;
  Expression *multiplicand_exp = ( Expression * ) mul->multiplicand ;
  MoneyProtected *reduced_multiplicand = ( MoneyProtected * ) reduce( multiplicand_exp, to );
  unsigned int multiplicand = reduced_multiplicand->amount;
  unsigned int multiplier = mul->multiplier;
  free_money( (Money * ) reduced_multiplicand );
  return create_money( multiplicand * multiplier, to );
}

canonical_original_code:
static struct Money *reduce_mul(struct Expression *exp, enum Currency to){
  struct Mul *mul = exp->value;
  struct Expression *multiplicand_exp = ( Expression * ) mul->multiplicand;
  struct MoneyProtected *reduced_multiplicand = ( MoneyProtected * ) reduce( multiplicand_exp, to );
  unsigned int multiplicand = reduced_multiplicand->amount;
  unsigned int multiplier = mul->multiplier;
  free_money( (struct Money * ) reduced_multiplicand );
  return create_money( multiplicand * multiplier, to );
}

variable_types: struct Expression { void *value; struct Money *(*reduce)(struct Expression *exp, enum Currency { USD=0, CHF=1 } to); } *

variable_types: struct Mul { struct Expression { void *value; struct Money {  } *(*reduce)(struct Expression *exp, enum Currency { USD=0, CHF=1 } to); } *multiplicand; unsigned int multiplier; } *

variable_types: struct MoneyProtected { unsigned int amount; enum Currency { USD=0, CHF=1 } currency; } *

variable_types:
multiplicand
unsigned int

variable_types:
multiplier
unsigned int

return_type: struct Money {  } *

user_defined_types: struct Expression { void *value; struct Money *(*reduce)(struct Expression *exp, enum Currency to); }

user_defined_types: struct Mul { struct Expression *multiplicand; unsigned int multiplier; }

user_defined_types: struct MoneyProtected { unsigned int amount; enum Currency currency; }


repo: yatsukhnenko/redis-proxy

decompiled_code:
int *__fastcall rp_socket_init(int *a1)
{
int *v1; // rbx
int v2; // eax
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1 = a1;
optval[0] = 1;
v2 = socket(2, 2049, 0);
*a1 = v2;
if ( v2 < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
syslog(3, "socket at %s:%d - %s", "rp_socket.c", 9LL, v5);
v1 = 0LL;
}
else if ( setsockopt(v2, 1, 9, optval, 4u) < 0 )
{
v6 = _errno_location();
v7 = strerror(*v6);
syslog(3, "setsockopt at %s:%d - %s", "rp_socket.c", 24LL, v7);
close(*a1);
v1 = 0LL;
}
return v1;
}

canonical_decompiled_code:
int *__fastcall func0(int *a1)
{
int *v1; // rbx
int v2; // eax
int *v4; // rax
char *v5; // rax
int *v6; // rax
char *v7; // rax
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1 = a1;
optval[0] = 1;
v2 = socket(2, 2049, 0);
*a1 = v2;
if ( v2 < 0 )
{
v4 = _errno_location();
v5 = strerror(*v4);
syslog(3, "socket at %s:%d - %s", "rp_socket.c", 9LL, v5);
v1 = 0LL;
}
else if ( setsockopt(v2, 1, 9, optval, 4u) < 0 )
{
v6 = _errno_location();
v7 = strerror(*v6);
syslog(3, "setsockopt at %s:%d - %s", "rp_socket.c", 24LL, v7);
close(*a1);
v1 = 0LL;
}
return v1;
}

original_code:
int *rp_socket_init(int *sockfd)
{
    int yes = 1;
    if((*sockfd = socket(2, SOCK_STREAM | SOCK_NONBLOCK, 0)) < 0) {
        syslog(3, "socket at %s:%d - %s", "rp_socket.c", 9, strerror((*__errno_location ())));
        return ((void *)0);
    }
    if(setsockopt(*sockfd, 1, 9, &yes, sizeof(yes)) < 0) {
        syslog(3, "setsockopt at %s:%d - %s", "rp_socket.c", 24, strerror((*__errno_location ())));
        close(*sockfd);
        return ((void *)0);
    }
    return sockfd;
}

variable_types:
sockfd
int *

variable_types: yes int

return_type: int *


decompiled_code:
int *__fastcall rp_listen(int *a1, int a2, __int16 a3)
{
int *result; // rax
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
struct sockaddr v11; // [rsp+0h] [rbp-38h] BYREF
int optval; // [rsp+18h] [rbp-20h] BYREF
int fd[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

optval = 1;
result = rp_socket_init(fd);
if ( result )
{
if ( setsockopt(fd[0], 1, 2, &optval, 4u) < 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
syslog(3, "setsockopt at %s:%d - %s", "rp_socket.c", 40LL, v6);
close(fd[0]);
result = 0LL;
}
else
{
v11.sa_family = 2;
*(_DWORD *)&v11.sa_data[2] = a2;
*(_WORD *)v11.sa_data = a3;
if ( bind(fd[0], &v11, 0x10u) < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "bind at %s:%d - %s", "rp_socket.c", 48LL, v8);
close(fd[0]);
result = 0LL;
}
else if ( listen(fd[0], 128) < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
syslog(3, "listen at %s:%d - %s", "rp_socket.c", 53LL, v10);
close(fd[0]);
result = 0LL;
}
else
{
*a1 = fd[0];
result = a1;
}
}
}
return result;
}

canonical_decompiled_code:
int *__fastcall func1(int *a1, int a2, __int16 a3)
{
int *result; // rax
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
struct sockaddr v11; // [rsp+0h] [rbp-38h] BYREF
int optval; // [rsp+18h] [rbp-20h] BYREF
int fd[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

optval = 1;
result = func0(fd);
if ( result )
{
if ( setsockopt(fd[0], 1, 2, &optval, 4u) < 0 )
{
v5 = _errno_location();
v6 = strerror(*v5);
syslog(3, "setsockopt at %s:%d - %s", "rp_socket.c", 40LL, v6);
close(fd[0]);
result = 0LL;
}
else
{
v11.sa_family = 2;
*(_DWORD *)&v11.sa_data[2] = a2;
*(_WORD *)v11.sa_data = a3;
if ( bind(fd[0], &v11, 0x10u) < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "bind at %s:%d - %s", "rp_socket.c", 48LL, v8);
close(fd[0]);
result = 0LL;
}
else if ( listen(fd[0], 128) < 0 )
{
v9 = _errno_location();
v10 = strerror(*v9);
syslog(3, "listen at %s:%d - %s", "rp_socket.c", 53LL, v10);
close(fd[0]);
result = 0LL;
}
else
{
*a1 = fd[0];
result = a1;
}
}
}
return result;
}

original_code:
int *rp_listen(int *sockfd, in_addr_t address, in_port_t port)
{
    int s, yes = 1;
    struct sockaddr_in addr;
    if(rp_socket_init(&s) == ((void *)0)) {
        return ((void *)0);
    }
    if(setsockopt(s, 1, 2, &yes, sizeof(yes)) < 0) {
        syslog(3, "setsockopt at %s:%d - %s", "rp_socket.c", 40, strerror((*__errno_location ())));
        close(s);
        return ((void *)0);
    }
    addr.sin_family = 2;
    addr.sin_addr.s_addr = address;
    addr.sin_port = port;
    if(bind(s, (const struct sockaddr *)&addr, sizeof(addr)) < 0) {
        syslog(3, "bind at %s:%d - %s", "rp_socket.c", 48, strerror((*__errno_location ())));
        close(s);
        return ((void *)0);
    }
    if(listen(s, 128) < 0) {
        syslog(3, "listen at %s:%d - %s", "rp_socket.c", 53, strerror((*__errno_location ())));
        close(s);
        return ((void *)0);
    }
    *sockfd = s;
    return sockfd;
}

canonical_original_code:
int *rp_listen(int *sockfd, unsigned int address, unsigned short port)
{
    int s; int yes = 1;
    struct sockaddr_in addr;
    if(rp_socket_init(&s) == ((void *)0)) {
        return ((void *)0);
    }
    if(setsockopt(s, 1, 2, &yes, sizeof(yes)) < 0) {
        syslog(3, "setsockopt at %s:%d - %s", "rp_socket.c", 40, strerror((*__errno_location ())));
        close(s);
        return ((void *)0);
    }
    addr.sin_family = 2;
    addr.sin_addr.s_addr = address;
    addr.sin_port = port;
    if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
        syslog(3, "bind at %s:%d - %s", "rp_socket.c", 48, strerror((*__errno_location ())));
        close(s);
        return ((void *)0);
    }
    if(listen(s, 128) < 0) {
        syslog(3, "listen at %s:%d - %s", "rp_socket.c", 53, strerror((*__errno_location ())));
        close(s);
        return ((void *)0);
    }
    *sockfd = s;
    return sockfd;
}

variable_types:
address
unsigned int

variable_types:
port
unsigned short

variable_types: s int

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

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

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


decompiled_code:
__int64 __fastcall rp_recv(int fd, __int64 a2)
{
ssize_t v2; // rax
unsigned int v3; // edx
int *v5; // rax

while ( 1 )
{
if ( *(_DWORD *)a2 + 4095 >= *(_DWORD *)(a2 + 8) && (unsigned int)rp_buffer_resize(a2, 4096LL) != 1 )
goto LABEL_6;
v2 = recv(fd, (void *)(*(_QWORD *)(a2 + 16) + *(_QWORD *)a2), *(int *)(a2 + 8) - *(_QWORD *)a2 - 1LL, 64);
if ( v2 <= 0 )
break;
*(_QWORD *)a2 += v2;
}
v3 = -1;
if ( v2 )
{
v5 = _errno_location();
v3 = -1;
if ( *v5 == 11 )
{
LABEL_6:
*(_BYTE *)(*(_QWORD *)(a2 + 16) + *(_QWORD *)a2) = 0;
return 1;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, __int64 a2)
{
ssize_t v2; // rax
unsigned int v3; // edx
int *v5; // rax

while ( 1 )
{
if ( *(_DWORD *)a2 + 4095 >= *(_DWORD *)(a2 + 8) && (unsigned int)rp_buffer_resize(a2, 4096LL) != 1 )
goto LABEL_6;
v2 = recv(fd, (void *)(*(_QWORD *)(a2 + 16) + *(_QWORD *)a2), *(int *)(a2 + 8) - *(_QWORD *)a2 - 1LL, 64);
if ( v2 <= 0 )
break;
*(_QWORD *)a2 += v2;
}
v3 = -1;
if ( v2 )
{
v5 = _errno_location();
v3 = -1;
if ( *v5 == 11 )
{
LABEL_6:
*(_BYTE *)(*(_QWORD *)(a2 + 16) + *(_QWORD *)a2) = 0;
return 1;
}
}
return v3;
}

original_code:
int rp_recv(int sockfd, rp_buffer_t *buffer)
{
    ssize_t i;
    for(;;) {
        if((int)buffer->used + 4096 > buffer->s.length) {
            if(rp_buffer_resize(buffer, 4096) != 1) {
                break;
            }
        }
        if((i = recv(sockfd, &buffer->s.data[buffer->used],
            buffer->s.length - buffer->used - 1, MSG_DONTWAIT)) <= 0) {
            if(i && ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)) {
                break;
            }
            return -1;
        }
        buffer->used += i;
    }
    buffer->s.data[buffer->used] = '\0';
    return 1;
}

canonical_original_code:
int rp_recv(int sockfd, struct rp_buffer_t *buffer)
{
    long i;
    for(;;) {
        if((int)buffer->used + 4096 > buffer->s.length) {
            if(rp_buffer_resize(buffer, 4096) != 1) {
                break;
            }
        }
        if((i = recv(sockfd, &buffer->s.data[buffer->used],
            buffer->s.length - buffer->used - 1, MSG_DONTWAIT)) <= 0) {
            if(i && ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)) {
                break;
            }
            return -1;
        }
        buffer->used += i;
    }
    buffer->s.data[buffer->used] = '\0';
    return 1;
}

variable_types: sockfd int

variable_types: struct rp_buffer_t { unsigned long used; struct rp_string_t { int length; char *data; } s; unsigned int r; unsigned int w; } *

variable_types: i long

return_type: int

user_defined_types: struct rp_buffer_t { unsigned long used; struct rp_string_t s; unsigned int r; unsigned int w; }


decompiled_code:
__int64 __fastcall rp_send(int fd, __int64 *a2)
{
unsigned int v2; // eax
__int64 v3; // rdx
ssize_t v4; // rax
unsigned int v5; // edx

v2 = *((_DWORD *)a2 + 7);
v3 = *a2;
if ( v2 >= (unsigned int)*a2 )
return 1;
while ( 1 )
{
v4 = send(fd, (const void *)(a2[2] + v2), v3 - v2, 16448);
if ( v4 <= 0 )
break;
v2 = *((_DWORD *)a2 + 7) + v4;
*((_DWORD *)a2 + 7) = v2;
v3 = *a2;
if ( v2 >= (unsigned int)*a2 )
return 1;
}
v5 = -1;
if ( v4 )
v5 = 2 * (*_errno_location() == 11) - 1;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, __int64 *a2)
{
unsigned int v2; // eax
__int64 v3; // rdx
ssize_t v4; // rax
unsigned int v5; // edx

v2 = *((_DWORD *)a2 + 7);
v3 = *a2;
if ( v2 >= (unsigned int)*a2 )
return 1;
while ( 1 )
{
v4 = send(fd, (const void *)(a2[2] + v2), v3 - v2, 16448);
if ( v4 <= 0 )
break;
v2 = *((_DWORD *)a2 + 7) + v4;
*((_DWORD *)a2 + 7) = v2;
v3 = *a2;
if ( v2 >= (unsigned int)*a2 )
return 1;
}
v5 = -1;
if ( v4 )
v5 = 2 * (*_errno_location() == 11) - 1;
return v5;
}

original_code:
int rp_send(int sockfd, rp_buffer_t *buffer)
{
    ssize_t i;
    while(buffer->w < (int)buffer->used) {
        if((i = send(sockfd, &buffer->s.data[buffer->w],
            buffer->used - buffer->w, MSG_DONTWAIT | MSG_NOSIGNAL)) <= 0) {
            if(i && ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)) {
                break;
            }
            return -1;
        }
        buffer->w += i;
    }
    return 1;
}

canonical_original_code:
int rp_send(int sockfd, struct rp_buffer_t *buffer)
{
    long i;
    while(buffer->w < (int)buffer->used) {
        if((i = send(sockfd, &buffer->s.data[buffer->w],
            buffer->used - buffer->w, MSG_DONTWAIT | MSG_NOSIGNAL)) <= 0) {
            if(i && ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)) {
                break;
            }
            return -1;
        }
        buffer->w += i;
    }
    return 1;
}

user_defined_types: struct rp_string_t { int length; char *data; }


decompiled_code:
__int64 __fastcall rp_event_handler_init(__int64 a1)
{
__int64 v1; // rbx
void *v2; // rax
int *v4; // rax
char *v5; // rax

v1 = a1;
*(_QWORD *)(a1 + 8) = 4096LL;
v2 = calloc(0x1000uLL, 0x10uLL);
*(_QWORD *)(a1 + 16) = v2;
if ( v2 )
{
memset(v2, 0, 0x10000uLL);
if ( !rp_epoll_init(a1) )
{
free(*(void **)(a1 + 16));
v1 = 0LL;
}
}
else
{
v4 = _errno_location();
v5 = strerror(*v4);
syslog(3, "calloc at %s:%d - %s", "rp_event.c", 19LL, v5);
v1 = 0LL;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // rbx
void *v2; // rax
int *v4; // rax
char *v5; // rax

v1 = a1;
*(_QWORD *)(a1 + 8) = 4096LL;
v2 = calloc(0x1000uLL, 0x10uLL);
*(_QWORD *)(a1 + 16) = v2;
if ( v2 )
{
memset(v2, 0, 0x10000uLL);
if ( !rp_epoll_init(a1) )
{
free(*(void **)(a1 + 16));
v1 = 0LL;
}
}
else
{
v4 = _errno_location();
v5 = strerror(*v4);
syslog(3, "calloc at %s:%d - %s", "rp_event.c", 19LL, v5);
v1 = 0LL;
}
return v1;
}

original_code:
rp_event_handler_t *rp_event_handler_init(rp_event_handler_t *eh)
{
    eh->maxevents = 4096;
    if((eh->events = calloc(eh->maxevents, sizeof(rp_event_t))) == ((void *)0)) {
        syslog(3, "calloc at %s:%d - %s", "rp_event.c", 19, strerror((*__errno_location ())));
        return ((void *)0);
    }
    memset(eh->events, 0, eh->maxevents * sizeof(rp_event_t));
    if(rp_epoll_init(eh) == ((void *)0)) {
        free(eh->events);
        return ((void *)0);
    }
    return eh;
}

canonical_original_code:
struct rp_event_handler *rp_event_handler_init(struct rp_event_handler *eh)
{
    eh->maxevents = 4096;
    if((eh->events = calloc(eh->maxevents, sizeof(rp_event_t))) == ((void *)0)) {
        syslog(3, "calloc at %s:%d - %s", "rp_event.c", 19, strerror((*__errno_location ())));
        return ((void *)0);
    }
    memset(eh->events, 0, eh->maxevents * sizeof(rp_event_t));
    if(rp_epoll_init(eh) == ((void *)0)) {
        free(eh->events);
        return ((void *)0);
    }
    return eh;
}

variable_types:
eh
struct rp_event_handler *

return_type: struct rp_event_handler { void *data; unsigned long maxevents; struct rp_event_t { short events; void *data; } *events; int (*add)(struct rp_event_handler *eh, int sockfd, struct rp_event_t *e); int (*del)(struct rp_event_handler *eh, int sockfd, struct rp_event_t *e); void (*poll)(struct rp_event_handler *eh, struct timeval { long tv_sec; long tv_usec; } *timeout); } *

user_defined_types: struct rp_event_handler { void *data; unsigned long maxevents; struct rp_event_t *events; int (*add)(struct rp_event_handler *eh, int sockfd, struct rp_event_t *e); int (*del)(struct rp_event_handler *eh, int sockfd, struct rp_event_t *e); void (*poll)(struct rp_event_handler *eh, struct timeval *timeout); }


decompiled_code:
__int64 __fastcall rp_epoll_add(__int64 a1, int a2, __int16 *a3)
{
int *v5; // rdi
__int16 v6; // ax
__int64 result; // rax
int *v8; // rax
char *v9; // rax
__int64 v10; // r12
struct epoll_event event; // [rsp+4h] [rbp-24h] BYREF

v5 = *(int **)a1;
event.events = 0;
v6 = *a3;
if ( (*a3 & 1) != 0 )
{
event.events = 1;
if ( (*(_BYTE *)(*(_QWORD *)(a1 + 16) + 16LL * a2) & 1) == 0 )
{
if ( (v6 & 2) == 0 )
goto LABEL_11;
goto LABEL_5;
}
*a3 = v6 & 0xFFFE;
}
v6 = *a3;
if ( (*a3 & 2) == 0 )
goto LABEL_7;
LABEL_5:
event.events |= 4u;
if ( (*(_BYTE *)(*(_QWORD *)(a1 + 16) + 16LL * a2) & 2) != 0 )
{
*a3 = v6 & 0xFFFD;
LABEL_7:
result = 1LL;
if ( !*a3 )
return result;
}
LABEL_11:
event.data.fd = a2;
event.events |= 0x18u;
v10 = 16LL * a2;
if ( epoll_ctl(*v5, *(_WORD *)(*(_QWORD *)(a1 + 16) + v10) == 0 ? 1 : 3, a2, &event) )
{
v8 = _errno_location();
v9 = strerror(*v8);
syslog(3, "epoll_ctl at %s:%d - %s", "rp_epoll.c", 59LL, v9);
result = 0xFFFFFFFFLL;
}
else
{
*(_WORD *)(*(_QWORD *)(a1 + 16) + 16LL * a2) |= *a3;
*(_QWORD *)(*(_QWORD *)(a1 + 16) + v10 + 8) = *((_QWORD *)a3 + 1);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2, __int16 *a3)
{
int *v5; // rdi
__int16 v6; // ax
__int64 result; // rax
int *v8; // rax
char *v9; // rax
__int64 v10; // r12
struct epoll_event event; // [rsp+4h] [rbp-24h] BYREF

v5 = *(int **)a1;
event.events = 0;
v6 = *a3;
if ( (*a3 & 1) != 0 )
{
event.events = 1;
if ( (*(_BYTE *)(*(_QWORD *)(a1 + 16) + 16LL * a2) & 1) == 0 )
{
if ( (v6 & 2) == 0 )
goto LABEL_11;
goto LABEL_5;
}
*a3 = v6 & 0xFFFE;
}
v6 = *a3;
if ( (*a3 & 2) == 0 )
goto LABEL_7;
LABEL_5:
event.events |= 4u;
if ( (*(_BYTE *)(*(_QWORD *)(a1 + 16) + 16LL * a2) & 2) != 0 )
{
*a3 = v6 & 0xFFFD;
LABEL_7:
result = 1LL;
if ( !*a3 )
return result;
}
LABEL_11:
event.data.fd = a2;
event.events |= 0x18u;
v10 = 16LL * a2;
if ( epoll_ctl(*v5, *(_WORD *)(*(_QWORD *)(a1 + 16) + v10) == 0 ? 1 : 3, a2, &event) )
{
v8 = _errno_location();
v9 = strerror(*v8);
syslog(3, "epoll_ctl at %s:%d - %s", "rp_epoll.c", 59LL, v9);
result = 0xFFFFFFFFLL;
}
else
{
*(_WORD *)(*(_QWORD *)(a1 + 16) + 16LL * a2) |= *a3;
*(_QWORD *)(*(_QWORD *)(a1 + 16) + v10 + 8) = *((_QWORD *)a3 + 1);
result = 1LL;
}
return result;
}

original_code:
int rp_epoll_add(rp_event_handler_t *eh, int sockfd, rp_event_t *e)
{
    int op;
    struct epoll_event event;
    rp_epoll_data_t *ed = eh->data;
    event.events = 0;
    if(e->events & 1) {
        event.events |= EPOLLIN;
        if(eh->events[sockfd].events & 1) {
            e->events &= ~1;
        }
    }
    if(e->events & 2) {
        event.events |= EPOLLOUT;
        if(eh->events[sockfd].events & 2) {
            e->events &= ~2;
        }
    }
    if(e->events) {
        event.data.fd = sockfd;
        event.events |= EPOLLERR | EPOLLHUP;
        op = !eh->events[sockfd].events ? 1 : 3;
        if(epoll_ctl(ed->epfd, op, sockfd, &event)) {
            syslog(3, "epoll_ctl at %s:%d - %s", "rp_epoll.c", 59, strerror((*__errno_location ())));
            return -1;
        }
        eh->events[sockfd].events |= e->events;
        eh->events[sockfd].data = e->data;
    }
    return 1;
}

canonical_original_code:
int rp_epoll_add(struct rp_event_handler *eh, int sockfd, struct rp_event_t *e)
{
    int op;
    struct epoll_event event;
    struct rp_epoll_data_t *ed = eh->data;
    event.events = 0;
    if(e->events & 1) {
        event.events |= EPOLLIN;
        if(eh->events[sockfd].events & 1) {
            e->events &= ~1;
        }
    }
    if(e->events & 2) {
        event.events |= EPOLLOUT;
        if(eh->events[sockfd].events & 2) {
            e->events &= ~2;
        }
    }
    if(e->events) {
        event.data.fd = sockfd;
        event.events |= EPOLLERR | EPOLLHUP;
        op = !eh->events[sockfd].events ? 1 : 3;
        if(epoll_ctl(ed->epfd, op, sockfd, &event)) {
            syslog(3, "epoll_ctl at %s:%d - %s", "rp_epoll.c", 59, strerror((*__errno_location ())));
            return -1;
        }
        eh->events[sockfd].events |= e->events;
        eh->events[sockfd].data = e->data;
    }
    return 1;
}

variable_types: struct rp_event_handler { void *data; unsigned long maxevents; struct rp_event_t { short events; void *data; } *events; int (*add)(struct rp_event_handler *eh, int sockfd, struct rp_event_t *e); int (*del)(struct rp_event_handler *eh, int sockfd, struct rp_event_t *e); void (*poll)(struct rp_event_handler *eh, struct timeval { long tv_sec; long tv_usec; } *timeout); } *

variable_types:
e
struct rp_event_t *

variable_types: op int

variable_types: struct rp_epoll_data_t { int epfd; struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } *events; } *

user_defined_types: struct epoll_event { unsigned int events; union epoll_data data; }

user_defined_types: struct rp_epoll_data_t { int epfd; struct epoll_event *events; }


original_code:
int rp_epoll_del(rp_event_handler_t *eh, int sockfd, rp_event_t *e)
{
    int op;
    struct epoll_event event;
    rp_epoll_data_t *ed = eh->data;
    e->events &= eh->events[sockfd].events;
    if(e->events) {
        event.events = 0;
        if(eh->events[sockfd].events & 1) {
            if(e->events & 1) {
                e->events &= ~1;
            } else {
                e->events |= 1;
                event.events |= EPOLLIN;
            }
        } else {
            e->events &= ~1;
        }
        if(eh->events[sockfd].events & 2) {
            if(e->events & 2) {
                e->events &= ~2;
            } else {
                e->events |= 2;
                event.events |= EPOLLOUT;
            }
        } else {
            e->events &= ~2;
        }
        if(e->events != eh->events[sockfd].events) {
            event.data.fd = sockfd;
            event.events |= EPOLLERR | EPOLLHUP;
            op = !e->events ? 2 : 3;
            if(epoll_ctl(ed->epfd, op, sockfd, &event)) {
                syslog(3, "epoll_ctl at %s:%d - %s", "rp_epoll.c", 102, strerror((*__errno_location ())));
                return -1;
            }
            eh->events[sockfd].events = e->events;
            if(!eh->events[sockfd].events) {
                eh->events[sockfd].data = ((void *)0);
            }
        }
    }
    return 1;
}

canonical_original_code:
int rp_epoll_del(struct rp_event_handler *eh, int sockfd, struct rp_event_t *e)
{
    int op;
    struct epoll_event event;
    struct rp_epoll_data_t *ed = eh->data;
    e->events &= eh->events[sockfd].events;
    if(e->events) {
        event.events = 0;
        if(eh->events[sockfd].events & 1) {
            if(e->events & 1) {
                e->events &= ~1;
            } else {
                e->events |= 1;
                event.events |= EPOLLIN;
            }
        } else {
            e->events &= ~1;
        }
        if(eh->events[sockfd].events & 2) {
            if(e->events & 2) {
                e->events &= ~2;
            } else {
                e->events |= 2;
                event.events |= EPOLLOUT;
            }
        } else {
            e->events &= ~2;
        }
        if(e->events != eh->events[sockfd].events) {
            event.data.fd = sockfd;
            event.events |= EPOLLERR | EPOLLHUP;
            op = !e->events ? 2 : 3;
            if(epoll_ctl(ed->epfd, op, sockfd, &event)) {
                syslog(3, "epoll_ctl at %s:%d - %s", "rp_epoll.c", 102, strerror((*__errno_location ())));
                return -1;
            }
            eh->events[sockfd].events = e->events;
            if(!eh->events[sockfd].events) {
                eh->events[sockfd].data = ((void *)0);
            }
        }
    }
    return 1;
}

variable_types: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; }

user_defined_types: struct rp_event_t { short events; void *data; }

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


original_code:
void rp_epoll_wait(rp_event_handler_t *eh, struct timeval *timeout)
{
    int i, n;
    struct timeval tv;
    rp_connection_t *c;
    struct epoll_event *event;
    rp_epoll_data_t *ed = eh->data;
    i = timeout != ((void *)0) ? timeout->tv_sec * 1000 + timeout->tv_usec / 1000 : -1;
    if((n = epoll_wait(ed->epfd, ed->events, eh->maxevents, i)) < 0) {
        syslog(3, "epoll_wait at %s:%d - %s", "rp_epoll.c", 124, strerror((*__errno_location ())));
    } else if(n > 0) {
        gettimeofday(&tv, ((void *)0));
        for(i = 0; i < n; i++) {
            event = &ed->events[i];
            c = eh->events[event->data.fd].data;
            c->time = tv.tv_sec + tv.tv_usec / 1000000.0;
            if(!(event->events & (EPOLLERR | EPOLLHUP))) {
                if((!(event->events & EPOLLIN) || c->on.read(c, eh) == 1)
                    && (!(event->events & EPOLLOUT) || c->on.write(c, eh) == 1)) {
                    continue;
                }
            }
            c->on.close(c, eh);
        }
    }
}

canonical_original_code:
void rp_epoll_wait(struct rp_event_handler *eh, struct timeval *timeout)
{
    int i; int n;
    struct timeval tv;
    struct rp_connection *c;
    struct epoll_event *event;
    struct rp_epoll_data_t *ed = eh->data;
    i = timeout != ((void *)0) ? timeout->tv_sec * 1000 + timeout->tv_usec / 1000 : -1;
    if((n = epoll_wait(ed->epfd, ed->events, eh->maxevents, i)) < 0) {
        syslog(3, "epoll_wait at %s:%d - %s", "rp_epoll.c", 124, strerror((*__errno_location ())));
    } else if(n > 0) {
        gettimeofday(&tv, ((void *)0));
        for(i = 0; i < n; i++) {
            event = &ed->events[i];
            c = eh->events[event->data.fd].data;
            c->time = tv.tv_sec + tv.tv_usec / 1000000.0;
            if(!(event->events & (EPOLLERR | EPOLLHUP))) {
                if((!(event->events & EPOLLIN) || c->on.read(c, eh) == 1)
                    && (!(event->events & EPOLLOUT) || c->on.write(c, eh) == 1)) {
                    continue;
                }
            }
            c->on.close(c, eh);
        }
    }
}

variable_types:
timeout
struct timeval *

variable_types: i int

variable_types: n int

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

variable_types: struct rp_connection { int sockfd; double time; unsigned short flags; unsigned int address; unsigned short port; struct { long ping; struct rp_string_t { int length; char *data; } auth; struct rp_string_t address; unsigned short port; } settings; struct rp_connection *connection; void *data; struct { int (*read)(struct rp_connection *c, void *data); int (*write)(struct rp_connection *c, void *data); void (*close)(struct rp_connection *c, void *data); } on; } *

variable_types: struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } *

return_type: void

user_defined_types: struct rp_connection { int sockfd; double time; unsigned short flags; unsigned int address; unsigned short port; struct { long ping; struct rp_string_t auth; struct rp_string_t address; unsigned short port; } settings; struct rp_connection *connection; void *data; struct { int (*read)(struct rp_connection *c, void *data); int (*write)(struct rp_connection *c, void *data); void (*close)(struct rp_connection *c, void *data); } on; }


decompiled_code:
__int64 __fastcall rp_strstr(int *a1, _BYTE *a2)
{
unsigned __int64 v4; // rdi
__int64 v5; // rcx
char v6; // r8
unsigned __int64 v7; // rax
_BYTE *v8; // rdx
__int64 v9; // rsi

v4 = *a1;
if ( !v4 )
return 0LL;
v5 = *((_QWORD *)a1 + 1);
v6 = *a2;
v7 = 0LL;
while ( 1 )
{
v9 = v5 + v7;
if ( *(_BYTE *)(v5 + v7) == v6 )
break;
++v7;
LABEL_7:
if ( v7 >= v4 )
return 0LL;
}
v8 = a2;
while ( *v8 )
{
++v7;
if ( *(_BYTE *)(v5 + v7) != *++v8 )
{
if ( !*v8 )
return v9;
goto LABEL_7;
}
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func1(int *a1, _BYTE *a2)
{
unsigned __int64 v4; // rdi
__int64 v5; // rcx
char v6; // r8
unsigned __int64 v7; // rax
_BYTE *v8; // rdx
__int64 v9; // rsi

v4 = *a1;
if ( !v4 )
return 0LL;
v5 = *((_QWORD *)a1 + 1);
v6 = *a2;
v7 = 0LL;
while ( 1 )
{
v9 = v5 + v7;
if ( *(_BYTE *)(v5 + v7) == v6 )
break;
++v7;
LABEL_7:
if ( v7 >= v4 )
return 0LL;
}
v8 = a2;
while ( *v8 )
{
++v7;
if ( *(_BYTE *)(v5 + v7) != *++v8 )
{
if ( !*v8 )
return v9;
goto LABEL_7;
}
}
return v9;
}

original_code:
char *rp_strstr(rp_string_t *s, const char *needle)
{
    char *ptr, *str;
    size_t i = 0;
    while(i < s->length) {
        if(s->data[i] != *needle) {
            i++;
            continue;
        }
        ptr = &s->data[i];
        str = (char *)needle;
        while(*str != '\0' && s->data[++i] == *++str);
        if(*str == '\0') {
            return ptr;
        }
    }
    return ((void *)0);
}

canonical_original_code:
char *rp_strstr(struct rp_string_t *s, char *needle)
{
    char *ptr; char *str;
    unsigned long i = 0;
    while(i < s->length) {
        if(s->data[i] != *needle) {
            i++;
            continue;
        }
        ptr = &s->data[i];
        str = (char *)needle;
        while(*str != '\0' && s->data[++i] == *++str);
        if(*str == '\0') {
            return ptr;
        }
    }
    return ((void *)0);
}

variable_types: struct rp_string_t { int length; char *data; } *

variable_types:
needle
char *

variable_types:
ptr
char *

variable_types:
str
char *

variable_types:
i
unsigned long

return_type: char *


original_code:
long int rp_strtol(rp_string_t *s)
{
    long i = 0;
    int sign = 1;
    while(s->length > 0) {
        if(*s->data == '-') {
            if(sign > 0) {
                sign = -1;
                s->data++;
                s->length--;
                continue;
            }
        } else if(*s->data >= '0' && *s->data <= '9') {
            i = i * 10 + *s->data - '0';
            s->data++;
            s->length--;
            continue;
        }
        break;
    }
    return i * sign;
}

canonical_original_code:
long rp_strtol(struct rp_string_t *s)
{
    long i = 0;
    int sign = 1;
    while(s->length > 0) {
        if(*s->data == '-') {
            if(sign > 0) {
                sign = -1;
                s->data++;
                s->length--;
                continue;
            }
        } else if(*s->data >= '0' && *s->data <= '9') {
            i = i * 10 + *s->data - '0';
            s->data++;
            s->length--;
            continue;
        }
        break;
    }
    return i * sign;
}

variable_types: i long

variable_types: sign int

return_type: long


decompiled_code:
__int64 __fastcall rp_buffer_resize(__int64 a1, int a2)
{
int v2; // eax
size_t v3; // rbx
void *v4; // rax
__int64 result; // rax
int *v6; // rax
char *v7; // rax

v2 = *(_DWORD *)(a1 + 8);
v3 = v2 + a2;
if ( v2 < 0 )
v3 = a2;
v4 = realloc(*(void **)(a1 + 16), v3);
if ( v4 )
{
*(_DWORD *)(a1 + 8) = v3;
*(_QWORD *)(a1 + 16) = v4;
result = 1LL;
}
else
{
v6 = _errno_location();
v7 = strerror(*v6);
syslog(3, "realloc at %s:%d - %s", "rp_common.c", 10LL, v7);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
int v2; // eax
size_t v3; // rbx
void *v4; // rax
__int64 result; // rax
int *v6; // rax
char *v7; // rax

v2 = *(_DWORD *)(a1 + 8);
v3 = v2 + a2;
if ( v2 < 0 )
v3 = a2;
v4 = realloc(*(void **)(a1 + 16), v3);
if ( v4 )
{
*(_DWORD *)(a1 + 8) = v3;
*(_QWORD *)(a1 + 16) = v4;
result = 1LL;
}
else
{
v6 = _errno_location();
v7 = strerror(*v6);
syslog(3, "realloc at %s:%d - %s", "rp_common.c", 10LL, v7);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int rp_buffer_resize(rp_buffer_t *buffer, int delta)
{
    char *data;
    size_t size;
    size = buffer->s.length < 0 ? delta : buffer->s.length + delta;
    if((data = realloc(buffer->s.data, size)) == ((void *)0)) {
        syslog(3, "realloc at %s:%d - %s", "rp_common.c", 10, strerror((*__errno_location ())));
        return -1;
    }
    buffer->s.length = size;
    buffer->s.data = data;
    return 1;
}

canonical_original_code:
int rp_buffer_resize(struct rp_buffer_t *buffer, int delta)
{
    char *data;
    unsigned long size;
    size = buffer->s.length < 0 ? delta : buffer->s.length + delta;
    if((data = realloc(buffer->s.data, size)) == ((void *)0)) {
        syslog(3, "realloc at %s:%d - %s", "rp_common.c", 10, strerror((*__errno_location ())));
        return -1;
    }
    buffer->s.length = size;
    buffer->s.data = data;
    return 1;
}

variable_types: struct rp_buffer_t { unsigned long used; struct rp_string_t { int length; char *data; } s; unsigned int r; unsigned int w; } *

variable_types: delta int

variable_types:
data
char *

variable_types:
size
unsigned long


original_code:
int rp_buffer_append(rp_buffer_t *buffer, rp_string_t *s)
{
    if(s->length > 0) {
        if((int)buffer->used + s->length > buffer->s.length) {
            if(rp_buffer_resize(buffer, (s->length / 4096 + 1) * 4096) != 1) {
                return -1;
            }
        }
        memcpy(&buffer->s.data[buffer->used], s->data, s->length);
        buffer->used += s->length;
    }
    return 1;
}

canonical_original_code:
int rp_buffer_append(struct rp_buffer_t *buffer, struct rp_string_t *s)
{
    if(s->length > 0) {
        if((int)buffer->used + s->length > buffer->s.length) {
            if(rp_buffer_resize(buffer, (s->length / 4096 + 1) * 4096) != 1) {
                return -1;
            }
        }
        memcpy(&buffer->s.data[buffer->used], s->data, s->length);
        buffer->used += s->length;
    }
    return 1;
}

variable_types:
s
struct rp_string_t *


decompiled_code:
__int64 __fastcall rp_command_auth(__int64 a1, __int64 a2)
{
__int16 v2; // bp
__int64 v3; // rax
int v4; // edx

v2 = *(_WORD *)(a2 + 16);
if ( (v2 & 0x40) == 0 )
{
v3 = *(_QWORD *)(a2 + 88);
if ( *(_DWORD *)(v3 + 32) != *(_DWORD *)(*(_QWORD *)(v3 + 72) + 12LL) )
return rp_sprintf(a1, "-ERR operation not permitted");
v4 = *(_DWORD *)(a2 + 40);
if ( v4 != *(_DWORD *)(v3 + 40) || strncmp(*(const char **)(a2 + 48), *(const char **)(v3 + 48), v4) )
return rp_sprintf(a1, "-ERR operation not permitted");
}
*(_WORD *)(a2 + 16) = v2 | 0x40;
return rp_sprintf(a1, "+OK");
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int16 v2; // bp
__int64 v3; // rax
int v4; // edx

v2 = *(_WORD *)(a2 + 16);
if ( (v2 & 0x40) == 0 )
{
v3 = *(_QWORD *)(a2 + 88);
if ( *(_DWORD *)(v3 + 32) != *(_DWORD *)(*(_QWORD *)(v3 + 72) + 12LL) )
return rp_sprintf(a1, "-ERR operation not permitted");
v4 = *(_DWORD *)(a2 + 40);
if ( v4 != *(_DWORD *)(v3 + 40) || strncmp(*(const char **)(a2 + 48), *(const char **)(v3 + 48), v4) )
return rp_sprintf(a1, "-ERR operation not permitted");
}
*(_WORD *)(a2 + 16) = v2 | 0x40;
return rp_sprintf(a1, "+OK");
}

original_code:
rp_string_t *rp_command_auth(rp_string_t *s, void *data)
{
    rp_connection_t *c = data;
    rp_client_t *client = c->data;
    if(!(c->flags & 0x0040) && (
        client->cmd.argc != client->cmd.proto->argc ||
        c->settings.auth.length != client->cmd.argv.length ||
        strncmp(c->settings.auth.data, client->cmd.argv.data, c->settings.auth.length))
    ) {
        return rp_sprintf(s, "-ERR operation not permitted");
    }
    c->flags |= 0x0040;
    return rp_sprintf(s, "+OK");
}

canonical_original_code:
struct rp_string_t *rp_command_auth(struct rp_string_t *s, void *data)
{
    struct rp_connection *c = data;
    struct rp_client_t *client = c->data;
    if(!(c->flags & 0x0040) && (
        client->cmd.argc != client->cmd.proto->argc ||
        c->settings.auth.length != client->cmd.argv.length ||
        strncmp(c->settings.auth.data, client->cmd.argv.data, c->settings.auth.length))
    ) {
        return rp_sprintf(s, "-ERR operation not permitted");
    }
    c->flags |= 0x0040;
    return rp_sprintf(s, "+OK");
}

variable_types:
data
void *

variable_types: struct rp_client_t { struct rp_buffer_t { unsigned long used; struct rp_string_t { int length; char *data; } s; unsigned int r; unsigned int w; } buffer; struct rp_command_t { int argc; unsigned int i; struct rp_string_t argv; struct rp_string_t name; struct rp_command_proto_t { char *name; unsigned int flags; int argc; struct rp_string_t *(*handler)(struct rp_string_t *s, void *data); } *proto; struct rp_string_t txt; } cmd; struct rp_connection { int sockfd; double time; unsigned short flags; unsigned int address; unsigned short port; struct { long ping; struct rp_string_t auth; struct rp_string_t address; unsigned short port; } settings; struct rp_connection *connection; void *data; struct { int (*read)(struct rp_connection *c, void *data); int (*write)(struct rp_connection *c, void *data); void (*close)(struct rp_connection *c, void *data); } on; } *server; } *

return_type: struct rp_string_t { int length; char *data; } *

user_defined_types: struct rp_client_t { struct rp_buffer_t buffer; struct rp_command_t cmd; struct rp_connection *server; }

user_defined_types: struct rp_command_t { int argc; unsigned int i; struct rp_string_t argv; struct rp_string_t name; struct rp_command_proto_t *proto; struct rp_string_t txt; }


decompiled_code:
__int64 __fastcall rp_command_ping(__int64 a1)
{
return rp_sprintf(a1, "+PONG");
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
return rp_sprintf(a1, "+PONG");
}

original_code:
rp_string_t *rp_command_ping(rp_string_t *s, void *data)
{
    return rp_sprintf(s, "+PONG");
}

canonical_original_code:
struct rp_string_t *rp_command_ping(struct rp_string_t *s, void *data)
{
    return rp_sprintf(s, "+PONG");
}


decompiled_code:
__int64 __fastcall rp_command_quit(__int64 a1, __int64 a2)
{
*(_WORD *)(a2 + 16) |= 0x200u;
return rp_sprintf(a1, "+OK");
}

original_code:
rp_string_t *rp_command_quit(rp_string_t *s, void *data)
{
    rp_connection_t *c = data;
    c->flags |= 0x0200;
    return rp_sprintf(s, "+OK");
}

canonical_original_code:
struct rp_string_t *rp_command_quit(struct rp_string_t *s, void *data)
{
    struct rp_connection *c = data;
    c->flags |= 0x0200;
    return rp_sprintf(s, "+OK");
}


decompiled_code:
const char **__fastcall rp_command_lookup(int *a1)
{
int v1; // ebp
unsigned int v2; // eax
const char **v3; // rbx
const char *v4; // rsi
const char *v5; // r12

v1 = *a1;
v2 = *a1 - 1;
v3 = 0LL;
if ( v2 <= 0xE )
{
v3 = (const char **)rp_commands[v2];
if ( v3 )
{
v4 = *v3;
if ( *v3 )
{
v5 = (const char *)*((_QWORD *)a1 + 1);
while ( strncasecmp(v5, v4, v1) )
{
v3 += 3;
v4 = *v3;
if ( !*v3 )
return 0LL;
}
}
else
{
v3 = 0LL;
}
}
}
return v3;
}

canonical_decompiled_code:
const char **__fastcall func3(int *a1)
{
int v1; // ebp
unsigned int v2; // eax
const char **v3; // rbx
const char *v4; // rsi
const char *v5; // r12

v1 = *a1;
v2 = *a1 - 1;
v3 = 0LL;
if ( v2 <= 0xE )
{
v3 = (const char **)rp_commands[v2];
if ( v3 )
{
v4 = *v3;
if ( *v3 )
{
v5 = (const char *)*((_QWORD *)a1 + 1);
while ( strncasecmp(v5, v4, v1) )
{
v3 += 3;
v4 = *v3;
if ( !*v3 )
return 0LL;
}
}
else
{
v3 = 0LL;
}
}
}
return v3;
}

original_code:
rp_command_proto_t *rp_command_lookup(rp_string_t *name)
{
    rp_command_proto_t *ptr;
    if(name->length > 0 && name->length < 16 &&
        (ptr = rp_commands[name->length - 1]) != ((void *)0)) {
        while(ptr->name != ((void *)0)) {
            if(strncasecmp(name->data, ptr->name, name->length) == 0) {
                return ptr;
            }
            ptr++;
        }
    }
    return ((void *)0);
}

canonical_original_code:
struct rp_command_proto_t *rp_command_lookup(struct rp_string_t *name)
{
    struct rp_command_proto_t *ptr;
    if(name->length > 0 && name->length < 16 &&
        (ptr = rp_commands[name->length - 1]) != ((void *)0)) {
        while(ptr->name != ((void *)0)) {
            if(strncasecmp(name->data, ptr->name, name->length) == 0) {
                return ptr;
            }
            ptr++;
        }
    }
    return ((void *)0);
}

variable_types:
name
struct rp_string_t *

variable_types: struct rp_command_proto_t { char *name; unsigned int flags; int argc; struct rp_string_t { int length; char *data; } *(*handler)(struct rp_string_t *s, void *data); } *

user_defined_types: struct rp_command_proto_t { char *name; unsigned int flags; int argc; struct rp_string_t *(*handler)(struct rp_string_t *s, void *data); }


original_code:
int rp_request_parse(rp_buffer_t *buffer, rp_command_t *cmd)
{
    int i;
    char *ptr;
    rp_string_t s;
    s.data = &buffer->s.data[buffer->r];
    s.length = buffer->used - buffer->r;
    while((ptr = rp_strstr(&s, "\r\n")) != ((void *)0)) {
        buffer->r = ptr - buffer->s.data + 2;
        if(cmd->argc < -1) {
            cmd->txt.data = s.data;
            if(*s.data != '*') {
                return -1;
            }
            s.data++;
            s.length--;
            if((i = rp_strtol(&s)) <= 0 || s.data != ptr) {
                return -1;
            }
            cmd->i = 0;
            cmd->argc = i;
            cmd->argv.data = ((void *)0);
            cmd->argv.length = -1 - 1;
        } else if(cmd->argv.length < -1) {
            if(*s.data != '$') {
                return -1;
            }
            s.data++;
            s.length--;
            if((i = rp_strtol(&s)) < -1 || s.data != ptr) {
                return -1;
            }
            cmd->argv.length = i;
            cmd->argv.data = ((void *)0);
        } else {
            if(cmd->argv.data == ((void *)0)) {
                cmd->argv.data = s.data;
            }
            i = ptr - cmd->argv.data;
            if(cmd->argv.length == -1 || cmd->argv.length == i) {
                if(!cmd->i) {
                    cmd->name.data = cmd->argv.data;
                    cmd->name.length = cmd->argv.length;
                }
                if(++cmd->i == cmd->argc) {
                    cmd->txt.length = ptr - cmd->txt.data + 2;
                    return 1;
                }
                cmd->argv.data = ((void *)0);
                cmd->argv.length = -1 - 1;
            } else if(i > cmd->argv.length) {
                return -1;
            }
        }
        s.data = &buffer->s.data[buffer->r];
        s.length = buffer->used - buffer->r;
    }
    return 0;
}

canonical_original_code:
int rp_request_parse(struct rp_buffer_t *buffer, struct rp_command_t *cmd)
{
    int i;
    char *ptr;
    struct rp_string_t s;
    s.data = &buffer->s.data[buffer->r];
    s.length = buffer->used - buffer->r;
    while((ptr = rp_strstr(&s, "\r\n")) != ((void *)0)) {
        buffer->r = ptr - buffer->s.data + 2;
        if(cmd->argc < -1) {
            cmd->txt.data = s.data;
            if(*s.data != '*') {
                return -1;
            }
            s.data++;
            s.length--;
            if((i = rp_strtol(&s)) <= 0 || s.data != ptr) {
                return -1;
            }
            cmd->i = 0;
            cmd->argc = i;
            cmd->argv.data = ((void *)0);
            cmd->argv.length = -1 - 1;
        } else if(cmd->argv.length < -1) {
            if(*s.data != '$') {
                return -1;
            }
            s.data++;
            s.length--;
            if((i = rp_strtol(&s)) < -1 || s.data != ptr) {
                return -1;
            }
            cmd->argv.length = i;
            cmd->argv.data = ((void *)0);
        } else {
            if(cmd->argv.data == ((void *)0)) {
                cmd->argv.data = s.data;
            }
            i = ptr - cmd->argv.data;
            if(cmd->argv.length == -1 || cmd->argv.length == i) {
                if(!cmd->i) {
                    cmd->name.data = cmd->argv.data;
                    cmd->name.length = cmd->argv.length;
                }
                if(++cmd->i == cmd->argc) {
                    cmd->txt.length = ptr - cmd->txt.data + 2;
                    return 1;
                }
                cmd->argv.data = ((void *)0);
                cmd->argv.length = -1 - 1;
            } else if(i > cmd->argv.length) {
                return -1;
            }
        }
        s.data = &buffer->s.data[buffer->r];
        s.length = buffer->used - buffer->r;
    }
    return 0;
}

variable_types: struct rp_command_t { int argc; unsigned int i; struct rp_string_t argv; struct rp_string_t name; struct rp_command_proto_t { char *name; unsigned int flags; int argc; struct rp_string_t *(*handler)(struct rp_string_t *s, void *data); } *proto; struct rp_string_t txt; } *

variable_types: i int

variable_types: struct rp_string_t { int length; char *data; }


original_code:
int rp_reply_parse(rp_buffer_t *buffer, rp_command_t *cmd)
{
    int i;
    char *ptr;
    rp_string_t s;
    s.data = &buffer->s.data[buffer->r];
    s.length = buffer->used - buffer->r;
    while((ptr = rp_strstr(&s, "\r\n")) != ((void *)0)) {
        buffer->r = ptr - buffer->s.data + 2;
        if(cmd->argc < -1) {
            cmd->i = 0;
            switch(*s.data) {
                case '*':
                    s.data++;
                    s.length--;
                    if((i = rp_strtol(&s)) < -1 || s.data != ptr) {
                        return -1;
                    } else if(i <= 0) {
                        return 1;
                    }
                    cmd->argc = i;
                    cmd->argv.data = ((void *)0);
                    cmd->argv.length = -1 - 1;
                    break;
                case '$':
                    s.data++;
                    s.length--;
                    if((i = rp_strtol(&s)) < -1 || s.data != ptr) {
                        return -1;
                    } else if(i < 0) {
                        return 1;
                    }
                    cmd->argc = 1;
                    cmd->argv.data = ((void *)0);
                    cmd->argv.length = i;
                    break;
                case ':':
                    s.data++;
                    s.length--;
                    i = rp_strtol(&s);
                    if(s.data != ptr) {
                        return -1;
                    }
                case '+':
                case '-':
                    return 1;
                default:
                    return -1;
            }
        } else if(cmd->argv.length < -1) {
            if(*s.data != '$') {
                return -1;
            }
            s.data++;
            s.length--;
            if((i = rp_strtol(&s)) < -1 || s.data != ptr) {
                return -1;
            }
            cmd->argv.length = i;
            cmd->argv.data = ((void *)0);
        } else {
            if(cmd->argv.data == ((void *)0)) {
                cmd->argv.data = s.data;
            }
            i = ptr - cmd->argv.data;
            if(cmd->argv.length == -1 || cmd->argv.length == i) {
                if(++cmd->i == cmd->argc) {
                    return 1;
                }
                cmd->argv.data = ((void *)0);
                cmd->argv.length = -1 - 1;
            } else if(i > cmd->argv.length) {
                return -1;
            }
        }
        s.data = &buffer->s.data[buffer->r];
        s.length = buffer->used - buffer->r;
    }
    return 0;
}

canonical_original_code:
int rp_reply_parse(struct rp_buffer_t *buffer, struct rp_command_t *cmd)
{
    int i;
    char *ptr;
    struct rp_string_t s;
    s.data = &buffer->s.data[buffer->r];
    s.length = buffer->used - buffer->r;
    while((ptr = rp_strstr(&s, "\r\n")) != ((void *)0)) {
        buffer->r = ptr - buffer->s.data + 2;
        if(cmd->argc < -1) {
            cmd->i = 0;
            switch(*s.data) {
                case '*':
                    s.data++;
                    s.length--;
                    if((i = rp_strtol(&s)) < -1 || s.data != ptr) {
                        return -1;
                    } else if(i <= 0) {
                        return 1;
                    }
                    cmd->argc = i;
                    cmd->argv.data = ((void *)0);
                    cmd->argv.length = -1 - 1;
                    break;
                case '$':
                    s.data++;
                    s.length--;
                    if((i = rp_strtol(&s)) < -1 || s.data != ptr) {
                        return -1;
                    } else if(i < 0) {
                        return 1;
                    }
                    cmd->argc = 1;
                    cmd->argv.data = ((void *)0);
                    cmd->argv.length = i;
                    break;
                case ':':
                    s.data++;
                    s.length--;
                    i = rp_strtol(&s);
                    if(s.data != ptr) {
                        return -1;
                    }
                case '+':
                case '-':
                    return 1;
                default:
                    return -1;
            }
        } else if(cmd->argv.length < -1) {
            if(*s.data != '$') {
                return -1;
            }
            s.data++;
            s.length--;
            if((i = rp_strtol(&s)) < -1 || s.data != ptr) {
                return -1;
            }
            cmd->argv.length = i;
            cmd->argv.data = ((void *)0);
        } else {
            if(cmd->argv.data == ((void *)0)) {
                cmd->argv.data = s.data;
            }
            i = ptr - cmd->argv.data;
            if(cmd->argv.length == -1 || cmd->argv.length == i) {
                if(++cmd->i == cmd->argc) {
                    return 1;
                }
                cmd->argv.data = ((void *)0);
                cmd->argv.length = -1 - 1;
            } else if(i > cmd->argv.length) {
                return -1;
            }
        }
        s.data = &buffer->s.data[buffer->r];
        s.length = buffer->used - buffer->r;
    }
    return 0;
}

variable_types:
ptr
char *


decompiled_code:
__int64 __fastcall rp_queue_push(_QWORD *a1, __int64 a2)
{
_QWORD *v2; // rax
__int64 v3; // rdx
__int64 result; // rax
int *v5; // rax
char *v6; // rax

v2 = malloc(0x18uLL);
if ( v2 )
{
if ( *a1 )
{
v3 = a1[2];
v2[1] = v3;
*(_QWORD *)(v3 + 16) = v2;
}
else
{
v2[1] = 0LL;
a1[1] = v2;
}
*v2 = a2;
v2[2] = 0LL;
a1[2] = v2;
++*a1;
result = 1LL;
}
else
{
v5 = _errno_location();
v6 = strerror(*v5);
syslog(3, "malloc at %s:%d - %s", "rp_queue.c", 8LL, v6);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1, __int64 a2)
{
_QWORD *v2; // rax
__int64 v3; // rdx
__int64 result; // rax
int *v5; // rax
char *v6; // rax

v2 = malloc(0x18uLL);
if ( v2 )
{
if ( *a1 )
{
v3 = a1[2];
v2[1] = v3;
*(_QWORD *)(v3 + 16) = v2;
}
else
{
v2[1] = 0LL;
a1[1] = v2;
}
*v2 = a2;
v2[2] = 0LL;
a1[2] = v2;
++*a1;
result = 1LL;
}
else
{
v5 = _errno_location();
v6 = strerror(*v5);
syslog(3, "malloc at %s:%d - %s", "rp_queue.c", 8LL, v6);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int rp_queue_push(rp_queue_t *queue, void *data)
{
    rp_queue_element_t *qe;
    if((qe = malloc(sizeof(rp_queue_element_t))) == ((void *)0)) {
        syslog(3, "malloc at %s:%d - %s", "rp_queue.c", 8, strerror((*__errno_location ())));
        return -1;
    }
    if(queue->size) {
        qe->prev = queue->last;
        queue->last->next = qe;
    } else {
        qe->next = qe->prev = ((void *)0);
        queue->first = qe;
    }
    qe->data = data;
    qe->next = ((void *)0);
    queue->last = qe;
    queue->size++;
    return 1;
}

canonical_original_code:
int rp_queue_push(struct rp_queue_t *queue, void *data)
{
    struct rp_queue_element *qe;
    if((qe = malloc(sizeof(rp_queue_element_t))) == ((void *)0)) {
        syslog(3, "malloc at %s:%d - %s", "rp_queue.c", 8, strerror((*__errno_location ())));
        return -1;
    }
    if(queue->size) {
        qe->prev = queue->last;
        queue->last->next = qe;
    } else {
        qe->next = qe->prev = ((void *)0);
        queue->first = qe;
    }
    qe->data = data;
    qe->next = ((void *)0);
    queue->last = qe;
    queue->size++;
    return 1;
}

variable_types: struct rp_queue_t { unsigned long size; struct rp_queue_element { void *data; struct rp_queue_element *prev; struct rp_queue_element *next; } *first; struct rp_queue_element *last; } *

user_defined_types: struct rp_queue_t { unsigned long size; struct rp_queue_element *first; struct rp_queue_element *last; }


repo: yeahimfreaky/rash

decompiled_code:
void sig_handler()
{
;
}

canonical_decompiled_code:
void func7()
{
;
}

original_code:
void sig_handler(int sig)
{
 if( sig == 2 );
}

variable_types: sig int


decompiled_code:
int __fastcall builtin_cmd(const char **a1)
{
int result; // eax

result = 1;
if ( !strcmp("cd", *a1) )
{
if ( !a1[1] )
a1[1] = getenv("HOME");
result = chdir(a1[1]);
if ( result )
{
printf("rash: can't cd to %s\n", a1[1]);
result = 0;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(const char **a1)
{
int result; // eax

result = 1;
if ( !strcmp("cd", *a1) )
{
if ( !a1[1] )
a1[1] = getenv("HOME");
result = chdir(a1[1]);
if ( result )
{
printf("rash: can't cd to %s\n", a1[1]);
result = 0;
}
}
return result;
}

original_code:
int builtin_cmd(char **cmd)
{
 if( !strcmp("cd",cmd[0]) ) {
  if( cmd[1]==((void *)0) )
   cmd[1] = getenv("HOME");
  if( chdir(cmd[1]) )
   printf("rash: can't cd to %s\n", cmd[1]);
  return 0;
 }
 return 1;
}

variable_types:
cmd
char **


decompiled_code:
__int64 __fastcall execute_cmd(char **argv)
{
parseandredirectin(*argv);
execvp(*argv, argv);
perror("execute_cmd failed");
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(char **argv)
{
func16(*argv);
execvp(*argv, argv);
perror("execute_cmd failed");
return 1LL;
}

original_code:
int execute_cmd(char **cmd)
{
 parseandredirectin(cmd[0]);
 execvp( cmd[0] , cmd );
 perror("execute_cmd failed");
 return 1;
}


decompiled_code:
char *input_loop()
{
int v0; // ebp
char *result; // rax
const char *v2; // rbp
int v3; // ebx
char *v4; // rax
__int64 v5; // rbx
bool v6; // zf
__pid_t v7; // eax

puts("Welcome to RASH!\n\t\tBe safe\n\n\tType ctrl-d or \"exit\" to exit");
printf("->) ");
while ( 1 )
{
result = fgets(linebuf, 255, stdin);
if ( !result )
break;
rash_log_data = time(0LL);
v2 = linebuf;
v3 = strlen(linebuf);
qword_4049A8 = (__int64)calloc(v3 + 1, 1uLL);
strncpy((char *)qword_4049A8, v2, v3);
addmsg(rash_log_data, qword_4049A8);
v4 = strtok_r(linebuf, delim, &tokp);
p = (__int64)v4;
if ( v4 )
{
v5 = 1LL;
do
{
v0 = v5;
qword_404198[v5] = (__int64)v4;
v4 = strtok_r(0LL, delim, &tokp);
p = (__int64)v4;
++v5;
}
while ( v4 );
}
else
{
v0 = 0;
}
(&argvv)[v0] = 0LL;
if ( argvv && builtin_cmd((const char **)&argvv) )
{
v6 = strcmp("exit", argvv) == 0;
result = (char *)!v6;
if ( v6 )
return result;
v7 = fork();
if ( v7 == -1 )
{
perror("Failed to fork child");
}
else if ( v7 )
{
wait(0LL);
}
else if ( (unsigned int)execute_cmd(&argvv) )
{
return (char *)1;
}
}
printf("->) ");
}
return result;
}

canonical_decompiled_code:
char *func10()
{
int v0; // ebp
char *result; // rax
const char *v2; // rbp
int v3; // ebx
char *v4; // rax
__int64 v5; // rbx
bool v6; // zf
__pid_t v7; // eax

puts("Welcome to RASH!\n\t\tBe safe\n\n\tType ctrl-d or \"exit\" to exit");
printf("->) ");
while ( 1 )
{
result = fgets(linebuf, 255, stdin);
if ( !result )
break;
rash_log_data = time(0LL);
v2 = linebuf;
v3 = strlen(linebuf);
qword_4049A8 = (__int64)calloc(v3 + 1, 1uLL);
strncpy((char *)qword_4049A8, v2, v3);
func12(rash_log_data, qword_4049A8);
v4 = strtok_r(linebuf, delim, &tokp);
p = (__int64)v4;
if ( v4 )
{
v5 = 1LL;
do
{
v0 = v5;
qword_404198[v5] = (__int64)v4;
v4 = strtok_r(0LL, delim, &tokp);
p = (__int64)v4;
++v5;
}
while ( v4 );
}
else
{
v0 = 0;
}
(&argvv)[v0] = 0LL;
if ( argvv && func8((const char **)&argvv) )
{
v6 = strcmp("exit", argvv) == 0;
result = (char *)!v6;
if ( v6 )
return result;
v7 = fork();
if ( v7 == -1 )
{
perror("Failed to fork child");
}
else if ( v7 )
{
wait(0LL);
}
else if ( (unsigned int)func9(&argvv) )
{
return (char *)1;
}
}
printf("->) ");
}
return result;
}

original_code:
int input_loop(void)
{
 int childpid, i, linelen;
 for( printf("Welcome to RASH!\n\t\tBe safe\n\n\tType ctrl-d or \"exit\" to exit\n")
     , printf("->) ")
     ; fgets( linebuf, 255, stdin ) != ((void *)0)
     ; printf("->) ") )
 {
  rash_log_data.time = time(((void *)0));
  linelen = strlen(linebuf);
  rash_log_data.string = calloc(linelen + 1, sizeof(char));
  strncpy(rash_log_data.string, linebuf, linelen);
  addmsg(rash_log_data);
  i = 0;
  for( p = strtok_r(linebuf, " \f\n\r\t\v", &tokp); p;
      p = strtok_r(((void *)0), " \f\n\r\t\v", &tokp) )
  {
   argvv[i++] = p;
  }
  argvv[i] = ((void *)0);
  if( argvv[0] != ((void *)0) ) {
   if( builtin_cmd(argvv) ) {
    if( !strcmp("exit",argvv[0]) )
     return 0;
    if( (childpid = fork()) == -1 )
     perror("Failed to fork child");
    else if( childpid == 0 ) {
     if( execute_cmd(argvv) )
      return 1;
    } else {
     wait(((void *)0));
    }
   }
  }
 }
 return 0;
}

canonical_original_code:
int input_loop()
{
 int childpid; int i; int linelen;
 for( printf("Welcome to RASH!\n\t\tBe safe\n\n\tType ctrl-d or \"exit\" to exit\n")
     , printf("->) ")
     ; fgets( linebuf, 255, stdin ) != ((void *)0)
     ; printf("->) ") )
 {
  rash_log_data.time = time(((void *)0));
  linelen = strlen(linebuf);
  rash_log_data.string = calloc(linelen + 1, sizeof(char));
  strncpy(rash_log_data.string, linebuf, linelen);
  addmsg(rash_log_data);
  i = 0;
  for( p = strtok_r(linebuf, " \f\n\r\t\v", &tokp); p;
      p = strtok_r(((void *)0), " \f\n\r\t\v", &tokp) )
  {
   argvv[i++] = p;
  }
  argvv[i] = ((void *)0);
  if( argvv[0] != ((void *)0) ) {
   if( builtin_cmd(argvv) ) {
    if( !strcmp("exit",argvv[0]) )
     return 0;
    if( (childpid = fork()) == -1 )
     perror("Failed to fork child");
    else if( childpid == 0 ) {
     if( execute_cmd(argvv) )
      return 1;
    } else {
     wait(((void *)0));
    }
   }
  }
 }
 return 0;
}

variable_types: childpid int

variable_types: linelen int


cleanup

decompiled_code:
void cleanup()
{
free(linebuf);
}

canonical_decompiled_code:
void func11()
{
free(linebuf);
}

original_code:
void cleanup(void)
{
 free(linebuf);
}

canonical_original_code:
void cleanup()
{
 free(linebuf);
}


main

canonical_name: main

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

linebuf = (char *)calloc(0xFFuLL, 1uLL);
if ( linebuf )
{
if ( signal(2, (__sighandler_t)sig_handler) == (__sighandler_t)-1LL )
fwrite("signal() error!\n", 1uLL, 0x10uLL, stderr);
v3 = (unsigned int)input_loop();
if ( (unsigned int)savelog("rash.log") )
perror("rash: savelog() failed");
puts("");
cleanup();
exit(v3);
}
fwrite("malloc problem\n", 1uLL, 0xFuLL, stderr);
exit(1);
}

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

linebuf = (char *)calloc(0xFFuLL, 1uLL);
if ( linebuf )
{
if ( signal(2, (__sighandler_t)sig_handler) == (__sighandler_t)-1LL )
fwrite("signal() error!\n", 1uLL, 0x10uLL, stderr);
v3 = (unsigned int)func10();
if ( (unsigned int)func15("rash.log") )
perror("rash: savelog() failed");
puts("");
func11();
exit(v3);
}
fwrite("malloc problem\n", 1uLL, 0xFuLL, stderr);
exit(1);
}

original_code:
int main( int argc, char **argv )
{
 int exitcode;
 linebuf = calloc( 255,sizeof(char) );
 if( linebuf == ((void *)0) ) {
  fprintf( stderr,"malloc problem\n" );
  exit(1);
 }
 if( signal(2, sig_handler) == ((__sighandler_t) -1) )
  fprintf( stderr, "signal() error!\n" );
 exitcode = input_loop();
 if( savelog("rash.log") != 0 )
  perror("rash: savelog() failed");
 puts("");
 cleanup();
 exit(exitcode);
}

canonical_original_code:
int main(int argc, char **argv)
{
 int exitcode;
 linebuf = calloc( 255,sizeof(char) );
 if( linebuf == ((void *)0) ) {
  fprintf( stderr,"malloc problem\n" );
  exit(1);
 }
 if( signal(2, sig_handler) == ((__sighandler_t) -1) )
  fprintf( stderr, "signal() error!\n" );
 exitcode = input_loop();
 if( savelog("rash.log") != 0 )
  perror("rash: savelog() failed");
 puts("");
 cleanup();
 exit(exitcode);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: exitcode int


repo: yelkin/socket-programming

decompiled_code:
void __fastcall check_errno(int a1)
{
int *v1; // rax

if ( a1 == -1 )
{
v1 = __errno_location();
perror((const char *)*v1);
exit(1);
}
}

canonical_decompiled_code:
void __fastcall func7(int a1)
{
int *v1; // rax

if ( a1 == -1 )
{
v1 = __errno_location();
perror((const char *)*v1);
exit(1);
}
}

original_code:
void check_errno(int retcode)
{
    if(retcode == -1)
    {
        int err=(*__errno_location ());
        perror(err);
        exit(EXIT_FAILURE);
    }
}

canonical_original_code:
void check_errno(int retcode)
{
    if(retcode == -1)
    {
        int err = (*__errno_location ());
        perror(err);
        exit(EXIT_FAILURE);
    }
}

variable_types: retcode int

variable_types: err int


decompiled_code:
__int64 get_unix_socket()
{
unsigned int v0; // ebx
int v1; // eax
int v2; // eax
_BYTE v4[104]; // [rsp+0h] [rbp-88h] BYREF
int v5; // [rsp+68h] [rbp-20h]
__int16 v6; // [rsp+6Ch] [rbp-1Ch]

v0 = socket(1, 1, 0);
check_errno(v0);
memset(v4, 0, sizeof(v4));
v5 = 0;
v6 = 0;
*(_WORD *)v4 = 1;
strncpy(&v4[2], SOCKET_NAME, 0x6BuLL)[107] = 0;
v1 = strlen(&v4[2]);
v4[2] = 0;
v2 = bind(v0, (const struct sockaddr *)v4, v1 + 2);
check_errno(v2);
return v0;
}

canonical_decompiled_code:
__int64 func8()
{
unsigned int v0; // ebx
int v1; // eax
int v2; // eax
_BYTE v4[104]; // [rsp+0h] [rbp-88h] BYREF
int v5; // [rsp+68h] [rbp-20h]
__int16 v6; // [rsp+6Ch] [rbp-1Ch]

v0 = socket(1, 1, 0);
func7(v0);
memset(v4, 0, sizeof(v4));
v5 = 0;
v6 = 0;
*(_WORD *)v4 = 1;
strncpy(&v4[2], SOCKET_NAME, 0x6BuLL)[107] = 0;
v1 = strlen(&v4[2]);
v4[2] = 0;
v2 = bind(v0, (const struct sockaddr *)v4, v1 + 2);
func7(v2);
return v0;
}

original_code:
int get_unix_socket()
{
    int sockfd;
    struct sockaddr_un sock_un;
    sockfd = socket(1, SOCK_STREAM, 0);
    check_errno(sockfd);
    memset(&sock_un, 0, sizeof sock_un);
    sock_un.sun_family = 1;
    strncpy(sock_un.sun_path, SOCKET_NAME, sizeof sock_un.sun_path-1)
            [sizeof sock_un.sun_path-1] = 0;
    int len = ((size_t) (((struct sockaddr_un *) 0)->sun_path) + strlen ((&sock_un)->sun_path));
    sock_un.sun_path[0]=0;
    check_errno(bind(sockfd, (struct sockaddr *)&sock_un, len));
    return sockfd;
}

variable_types: sockfd int

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

variable_types: len int


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

v3 = get_unix_socket(argc, argv, envp);
system("netstat -pa --unix|grep qweasd");
close(v3);
return 0;
}

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

v3 = func8(argc, argv, envp);
system("netstat -pa --unix|grep qweasd");
close(v3);
return 0;
}

original_code:
int main(int argc, char** argv)
{
    int sockfd = get_unix_socket();
    system("netstat -pa --unix|grep qweasd");
    close(sockfd);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int sockfd = get_unix_socket();
    system("netstat -pa --unix|grep qweasd");
    close(sockfd);
    return 0;
}


decompiled_code:
int __fastcall show_usage(const char *a1)
{
return fprintf(stderr, "USAGE: %s -h|--host IP_ADDRESS -p|--port PORT\n", a1);
}

canonical_decompiled_code:
int __fastcall func8(const char *a1)
{
return fprintf(stderr, "USAGE: %s -h|--host IP_ADDRESS -p|--port PORT\n", a1);
}

original_code:
void show_usage(char *program_name)
{
    fprintf(stderr, "USAGE: %s -h|--host IP_ADDRESS -p|--port PORT\n",
            program_name);
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
unsigned int v4; // er13
int v5; // eax
int v6; // ebx
const char *v7; // rdx
const char *v8; // rdx
int v9; // eax
int v11; // ebx
char *v12; // rax
int v13; // ebx
char *v14; // rax
__int64 v15[2]; // [rsp+0h] [rbp-58h] BYREF
struct sockaddr buf; // [rsp+10h] [rbp-48h] BYREF
int longind[11]; // [rsp+2Ch] [rbp-2Ch] BYREF

longind[0] = 0;
while ( 1 )
{
v5 = getopt_long(argc, (char *const *)argv, "h:p:", &long_options_0, longind);
if ( v5 == -1 )
break;
if ( v5 == 104 )
{
v3 = _bss_start;
}
else
{
if ( v5 != 112 )
{
show_usage(*argv);
exit(1);
}
v4 = atoi(_bss_start);
}
}
fprintf(stderr, "Call arguments: host='%s' port='%d'\n", v3, v4);
v6 = socket(2, 1, 0);
if ( v6 == -1 )
{
v11 = *__errno_location();
v12 = strerror(v11);
printf("ERROR: %s\n", v12);
exit(v11);
}
*(_QWORD *)&buf.sa_family = 2LL;
*(_QWORD *)&buf.sa_data[6] = 0LL;
*(_WORD *)buf.sa_data = _bswap_16(v4);
if ( inet_pton(2, v3, &buf.sa_data[2]) == -1 )
{
v13 = *__errno_location();
v14 = strerror(v13);
printf("ERROR: %s\n", v14);
exit(v13);
}
v15[0] = 1LL;
v15[1] = 0LL;
if ( setsockopt(v6, 1, 20, v15, 0x10u) < 0 )
error((int)"setsockopt failed\n", 1, v7);
if ( setsockopt(v6, 1, 21, v15, 0x10u) < 0 )
error((int)"setsockopt failed\n", 1, v8);
v9 = connect(v6, &buf, 0x10u);
if ( v9 )
{
if ( v9 == -1 )
printf("Port %d is closed on host %s\n", v4, v3);
else
printf("Error connecting to %s:%d", v3, v4);
}
else
{
printf("Port %d is open on host %s\n", v4, v3);
close(v6);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // r12
unsigned int v4; // er13
int v5; // eax
int v6; // ebx
const char *v7; // rdx
const char *v8; // rdx
int v9; // eax
int v11; // ebx
char *v12; // rax
int v13; // ebx
char *v14; // rax
__int64 v15[2]; // [rsp+0h] [rbp-58h] BYREF
struct sockaddr buf; // [rsp+10h] [rbp-48h] BYREF
int longind[11]; // [rsp+2Ch] [rbp-2Ch] BYREF

longind[0] = 0;
while ( 1 )
{
v5 = getopt_long(argc, (char *const *)argv, "h:p:", &long_options_0, longind);
if ( v5 == -1 )
break;
if ( v5 == 104 )
{
v3 = _bss_start;
}
else
{
if ( v5 != 112 )
{
func8(*argv);
exit(1);
}
v4 = atoi(_bss_start);
}
}
fprintf(stderr, "Call arguments: host='%s' port='%d'\n", v3, v4);
v6 = socket(2, 1, 0);
if ( v6 == -1 )
{
v11 = *__errno_location();
v12 = strerror(v11);
printf("ERROR: %s\n", v12);
exit(v11);
}
*(_QWORD *)&buf.sa_family = 2LL;
*(_QWORD *)&buf.sa_data[6] = 0LL;
*(_WORD *)buf.sa_data = func7(v4);
if ( inet_pton(2, v3, &buf.sa_data[2]) == -1 )
{
v13 = *__errno_location();
v14 = strerror(v13);
printf("ERROR: %s\n", v14);
exit(v13);
}
v15[0] = 1LL;
v15[1] = 0LL;
if ( setsockopt(v6, 1, 20, v15, 0x10u) < 0 )
error((int)"setsockopt failed\n", 1, v7);
if ( setsockopt(v6, 1, 21, v15, 0x10u) < 0 )
error((int)"setsockopt failed\n", 1, v8);
v9 = connect(v6, &buf, 0x10u);
if ( v9 )
{
if ( v9 == -1 )
printf("Port %d is closed on host %s\n", v4, v3);
else
printf("Error connecting to %s:%d", v3, v4);
}
else
{
printf("Port %d is open on host %s\n", v4, v3);
close(v6);
}
return 0;
}

original_code:
int main(int argc, char** argv)
{
    char* host;
    int port;
    int ret;
    char *short_options = "h:p:";
    static struct option long_options[] = {
        {"host", 1, 0, 'h'},
        {"port", 1, 0, 'p'},
        {0, 0, 0, 0}
    };
    int opt;
    int option_index = 0;
    while(1)
    {
        opt = getopt_long(argc, argv, short_options, long_options, &option_index);
        if(opt == -1)
        {
            break;
        }
        switch(opt)
        {
        case 'h':
            host = optarg;
            break;
        case 'p':
            port = atoi(optarg);
            break;
        default:
            show_usage(argv[0]);
            exit(1);
        }
    }
    fprintf(stderr, "Call arguments: host='%s' port='%d'\n", host, port);
    int sock = socket(2, SOCK_STREAM, 0);
    if((sock) == -1) { int err=(*__errno_location ()); printf("ERROR: %s\n", strerror(err)); exit(err); };
    struct sockaddr_in adr_inet;
    memset(&adr_inet, 0, sizeof adr_inet);
    adr_inet.sin_family = 2;
    adr_inet.sin_port = htons(port);
    ret = inet_pton(2, host, &(adr_inet.sin_addr));
    if((ret) == -1) { int err=(*__errno_location ()); printf("ERROR: %s\n", strerror(err)); exit(err); };
    struct timeval timeout;
    timeout.tv_sec = 1;
    timeout.tv_usec = 0;
    if (setsockopt (sock, 1, 20, (char *)&timeout,
                sizeof(timeout)) < 0)
        error("setsockopt failed\n");
    if (setsockopt (sock, 1, 21, (char *)&timeout,
                sizeof(timeout)) < 0)
        error("setsockopt failed\n");
    ret = connect(sock, &adr_inet, sizeof adr_inet);
    if(ret == 0) {
        printf("Port %d is open on host %s\n", port, host);
        close(sock);
    } else if (ret == -1) {
        printf("Port %d is closed on host %s\n", port, host);
    } else {
        printf("Error connecting to %s:%d", host, port);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    char *host;
    int port;
    int ret;
    char *short_options = "h:p:";
    static struct option long_options[] = {
        {"host", 1, 0, 'h'},
        {"port", 1, 0, 'p'},
        {0, 0, 0, 0}
    };
    int opt;
    int option_index = 0;
    while(1)
    {
        opt = getopt_long(argc, argv, short_options, long_options, &option_index);
        if(opt == -1)
        {
            break;
        }
        switch(opt)
        {
        case 'h':
            host = optarg;
            break;
        case 'p':
            port = atoi(optarg);
            break;
        default:
            show_usage(argv[0]);
            exit(1);
        }
    }
    fprintf(stderr, "Call arguments: host='%s' port='%d'\n", host, port);
    int sock = socket(2, SOCK_STREAM, 0);
    if((sock) == -1) { int err = (*__errno_location ()); printf("ERROR: %s\n", strerror(err)); exit(err); };
    struct sockaddr_in adr_inet;
    memset(&adr_inet, 0, sizeof adr_inet);
    adr_inet.sin_family = 2;
    adr_inet.sin_port = htons(port);
    ret = inet_pton(2, host, &(adr_inet.sin_addr));
    if((ret) == -1) { int err = (*__errno_location ()); printf("ERROR: %s\n", strerror(err)); exit(err); };
    struct timeval timeout;
    timeout.tv_sec = 1;
    timeout.tv_usec = 0;
    if (setsockopt (sock, 1, 20, (char *)&timeout,
                sizeof(timeout)) < 0)
        error("setsockopt failed\n");
    if (setsockopt (sock, 1, 21, (char *)&timeout,
                sizeof(timeout)) < 0)
        error("setsockopt failed\n");
    ret = connect(sock, &adr_inet, sizeof adr_inet);
    if(ret == 0) {
        printf("Port %d is open on host %s\n", port, host);
        close(sock);
    } else if (ret == -1) {
        printf("Port %d is closed on host %s\n", port, host);
    } else {
        printf("Error connecting to %s:%d", host, port);
    }
    return 0;
}

variable_types:
host
char *

variable_types: port int

variable_types: ret int

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types: opt int

variable_types: sock int

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

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


destroyClient

decompiled_code:
void __fastcall destroyClient(void *ptr, int a2)
{
void **v2; // rbx

if ( a2 > 0 )
{
v2 = (void **)ptr;
do
{
free(*v2);
v2 += 5;
}
while ( v2 != (void **)((char *)ptr + 40 * (unsigned int)(a2 - 1) + 40) );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr, int a2)
{
void **v2; // rbx

if ( a2 > 0 )
{
v2 = (void **)ptr;
do
{
free(*v2);
v2 += 5;
}
while ( v2 != (void **)((char *)ptr + 40 * (unsigned int)(a2 - 1) + 40) );
}
free(ptr);
}

original_code:
void destroyClient(Node clients[], int num){
    int i;
    for(i = 0; i<num; i++){
        free(clients[i].name);
    }
    free(clients);
    return;
}

canonical_original_code:
void destroyClient(struct ClientPTR clients[], int num){
    int i;
    for(i = 0; i<num; i++){
        free(clients[i].name);
    }
    free(clients);
    return;
}

variable_types: struct ClientPTR { char *name; int id; float price; float total; struct ClientPTR *nextSucc; struct ClientPTR *nextFail; } []

variable_types: num int

variable_types: i int


printOrders

decompiled_code:
int __fastcall printOrders(__int64 a1, int a2)
{
__int64 v2; // rbp
int result; // eax
__int64 i; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
{
result = printf("Customer name: %s\n", *(const char **)v2);
for ( i = *(_QWORD *)(v2 + 24); i; i = *(_QWORD *)(i + 24) )
result = printf("%s | %f | %f\n", *(const char **)i, *(float *)(i + 12), *(float *)(i + 16));
v2 += 40LL;
}
while ( v2 != a1 + 40LL * (unsigned int)(a2 - 1) + 40 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1, int a2)
{
__int64 v2; // rbp
int result; // eax
__int64 i; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
{
result = printf("Customer name: %s\n", *(const char **)v2);
for ( i = *(_QWORD *)(v2 + 24); i; i = *(_QWORD *)(i + 24) )
result = printf("%s | %f | %f\n", *(const char **)i, *(float *)(i + 12), *(float *)(i + 16));
v2 += 40LL;
}
while ( v2 != a1 + 40LL * (unsigned int)(a2 - 1) + 40 );
}
return result;
}

original_code:
void printOrders(Node clients[], int num){
    int i;
    for(i=0; i<num; i++){
        Node *ptr = &clients[i];
        printf("Customer name: %s\n", ptr->name);
        while(ptr->nextSucc!=((void *)0)){
            ptr = ptr->nextSucc;
            printf("%s | %f | %f\n", ptr->name, ptr->price, ptr->total);
        }
    }
    return;
}

canonical_original_code:
void printOrders(struct ClientPTR clients[], int num){
    int i;
    for(i=0; i<num; i++){
        struct ClientPTR *ptr = &clients[i];
        printf("Customer name: %s\n", ptr->name);
        while(ptr->nextSucc!=((void *)0)){
            ptr = ptr->nextSucc;
            printf("%s | %f | %f\n", ptr->name, ptr->price, ptr->total);
        }
    }
    return;
}

variable_types: struct ClientPTR { char *name; int id; float price; float total; struct ClientPTR *nextSucc; struct ClientPTR *nextFail; } *


orders

original_code:
void orders(Node *client, char* book){
    char *token;
    int info = 0;
    float price;
    Node *currBook = (Node *)malloc(sizeof(Node));
    token = strtok(book, "|");
    while(token!=((void *)0)){
        if(info == 0)
            currBook->name = token;
        if(info == 1){
            price = atof(token);
            currBook->price = price;
        }
        info++;
        token = strtok(((void *)0), "|");
    }
    Node *ptr;
    free(token);
    float currPrice = client->total;
    if(currPrice-price<0){
        ptr = client;
        while(ptr->nextFail!=((void *)0)){
            ptr = ptr->nextFail;
        }
        ptr->nextFail = currBook;
        ptr = ptr->nextFail;
        ptr->nextFail=((void *)0);
    }
    else{
        ptr = client;
        currBook->total = currPrice - price;
        client->total = currPrice - price;
        while(ptr->nextSucc!=((void *)0)){
            ptr = ptr->nextSucc;
        }
        ptr->nextSucc = currBook;
        ptr = ptr->nextSucc;
        ptr->nextSucc=((void *)0);
    }
}

canonical_original_code:
void orders(struct ClientPTR *client, char *book){
    char *token;
    int info = 0;
    float price;
    struct ClientPTR *currBook = (Node *)malloc(sizeof(Node));
    token = strtok(book, "|");
    while(token!=((void *)0)){
        if(info == 0)
            currBook->name = token;
        if(info == 1){
            price = atof(token);
            currBook->price = price;
        }
        info++;
        token = strtok(((void *)0), "|");
    }
    struct ClientPTR *ptr;
    free(token);
    float currPrice = client->total;
    if(currPrice-price<0){
        ptr = client;
        while(ptr->nextFail!=((void *)0)){
            ptr = ptr->nextFail;
        }
        ptr->nextFail = currBook;
        ptr = ptr->nextFail;
        ptr->nextFail=((void *)0);
    }
    else{
        ptr = client;
        currBook->total = currPrice - price;
        client->total = currPrice - price;
        while(ptr->nextSucc!=((void *)0)){
            ptr = ptr->nextSucc;
        }
        ptr->nextSucc = currBook;
        ptr = ptr->nextSucc;
        ptr->nextSucc=((void *)0);
    }
}

variable_types:
book
char *

variable_types:
token
char *

variable_types: info int

variable_types: price float

variable_types: currPrice float

user_defined_types: struct ClientPTR { char *name; int id; float price; float total; struct ClientPTR *nextSucc; struct ClientPTR *nextFail; }


repo: yeslogic/prince-html

decompiled_code:
_QWORD *create_comment_token()
{
_QWORD *v0; // rbx

v0 = malloc(0x60uLL);
*(_DWORD *)v0 = 3;
v0[9] = string_append(0LL, 0LL);
return v0;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *v0; // rbx

v0 = malloc(0x60uLL);
*(_DWORD *)v0 = 3;
v0[9] = string_append(0LL, 0LL);
return v0;
}

original_code:
token *create_comment_token(void)
{
 token *tk;
 tk = malloc(sizeof(token));
 tk->type = TOKEN_COMMENT;
 tk->cmt.comment= string_append(((void *)0), '\0');
 return tk;
}

canonical_original_code:
struct token_s *create_comment_token()
{
 struct token_s *tk;
 tk = malloc(sizeof(token));
 tk->type = TOKEN_COMMENT;
 tk->cmt.comment= string_append(((void *)0), '\0');
 return tk;
}

variable_types: struct token_s { enum token_type { TOKEN_DOCTYPE=0, TOKEN_START_TAG=1, TOKEN_END_TAG=2, TOKEN_COMMENT=3, TOKEN_MULTI_CHAR=4, TOKEN_EOF=5 } type; struct doctype_token_s { unsigned char *doctype_name; unsigned char *doctype_public_identifier; unsigned char *doctype_system_identifier; enum force_quirks_flag_type { ON=0, OFF=1 } force_quirks_flag; } dt; struct start_tag_token_s { unsigned char *tag_name; enum self_closing_flag_type { SET=0, UNSET=1 } self_closing_flag; struct attribute_list_s { unsigned char *name; unsigned char *value; enum namespace_type { HTML=0, MATHML=1, SVG=2, XLINK=3, XML=4, XMLNS=5, DEFAULT=6 } attr_ns; struct attribute_list_s *tail; } *attributes; } stt; struct end_tag_token_s { unsigned char *tag_name; } ett; struct comment_token_s { unsigned char *comment; } cmt; struct multi_char_token_s { unsigned char *mch; long char_count; } mcht; } *

user_defined_types: struct token_s { enum token_type type; struct doctype_token_s dt; struct start_tag_token_s stt; struct end_tag_token_s ett; struct comment_token_s cmt; struct multi_char_token_s mcht; }

user_defined_types: struct doctype_token_s { unsigned char *doctype_name; unsigned char *doctype_public_identifier; unsigned char *doctype_system_identifier; enum force_quirks_flag_type force_quirks_flag; }

user_defined_types: struct start_tag_token_s { unsigned char *tag_name; enum self_closing_flag_type self_closing_flag; struct attribute_list_s *attributes; }

user_defined_types: struct attribute_list_s { unsigned char *name; unsigned char *value; enum namespace_type attr_ns; struct attribute_list_s *tail; }


decompiled_code:
_DWORD *create_eof_token()
{
_DWORD *result; // rax

result = malloc(0x60uLL);
*result = 5;
return result;
}

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

result = malloc(0x60uLL);
*result = 5;
return result;
}

original_code:
token *create_eof_token(void)
{
 token *tk;
 tk = malloc(sizeof(token));
 tk->type = TOKEN_EOF;
 return tk;
}

canonical_original_code:
struct token_s *create_eof_token()
{
 struct token_s *tk;
 tk = malloc(sizeof(token));
 tk->type = TOKEN_EOF;
 return tk;
}

return_type: struct token_s { enum token_type { TOKEN_DOCTYPE=0, TOKEN_START_TAG=1, TOKEN_END_TAG=2, TOKEN_COMMENT=3, TOKEN_MULTI_CHAR=4, TOKEN_EOF=5 } type; struct doctype_token_s { unsigned char *doctype_name; unsigned char *doctype_public_identifier; unsigned char *doctype_system_identifier; enum force_quirks_flag_type { ON=0, OFF=1 } force_quirks_flag; } dt; struct start_tag_token_s { unsigned char *tag_name; enum self_closing_flag_type { SET=0, UNSET=1 } self_closing_flag; struct attribute_list_s { unsigned char *name; unsigned char *value; enum namespace_type { HTML=0, MATHML=1, SVG=2, XLINK=3, XML=4, XMLNS=5, DEFAULT=6 } attr_ns; struct attribute_list_s *tail; } *attributes; } stt; struct end_tag_token_s { unsigned char *tag_name; } ett; struct comment_token_s { unsigned char *comment; } cmt; struct multi_char_token_s { unsigned char *mch; long char_count; } mcht; } *


decompiled_code:
void __fastcall free_token(void *ptr)
{
unsigned int v1; // eax

if ( ptr )
{
v1 = *(_DWORD *)ptr;
if ( *(_DWORD *)ptr == 2 )
{
free(*((void **)ptr + 8));
}
else if ( v1 > 2 )
{
if ( v1 == 3 )
free(*((void **)ptr + 9));
}
else if ( v1 )
{
free(*((void **)ptr + 5));
}
else
{
free(*((void **)ptr + 1));
free(*((void **)ptr + 2));
free(*((void **)ptr + 3));
}
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
unsigned int v1; // eax

if ( ptr )
{
v1 = *(_DWORD *)ptr;
if ( *(_DWORD *)ptr == 2 )
{
free(*((void **)ptr + 8));
}
else if ( v1 > 2 )
{
if ( v1 == 3 )
free(*((void **)ptr + 9));
}
else if ( v1 )
{
free(*((void **)ptr + 5));
}
else
{
free(*((void **)ptr + 1));
free(*((void **)ptr + 2));
free(*((void **)ptr + 3));
}
free(ptr);
}
}

original_code:
void free_token(token *tk)
{
 if(tk != ((void *)0))
 {
  switch (tk->type)
  {
   case TOKEN_DOCTYPE:
    free(tk->dt.doctype_name);
    free(tk->dt.doctype_public_identifier);
    free(tk->dt.doctype_system_identifier);
    break;
   case TOKEN_START_TAG:
    free(tk->stt.tag_name);
    break;
   case TOKEN_END_TAG:
    free(tk->ett.tag_name);
    break;
   case TOKEN_COMMENT:
    free(tk->cmt.comment);
    break;
   case TOKEN_MULTI_CHAR:
    break;
   case TOKEN_EOF:
    break;
   default:
    break;
  }
  free(tk);
 }
}

canonical_original_code:
void free_token(struct token_s *tk)
{
 if(tk != ((void *)0))
 {
  switch (tk->type)
  {
   case TOKEN_DOCTYPE:
    free(tk->dt.doctype_name);
    free(tk->dt.doctype_public_identifier);
    free(tk->dt.doctype_system_identifier);
    break;
   case TOKEN_START_TAG:
    free(tk->stt.tag_name);
    break;
   case TOKEN_END_TAG:
    free(tk->ett.tag_name);
    break;
   case TOKEN_COMMENT:
    free(tk->cmt.comment);
    break;
   case TOKEN_MULTI_CHAR:
    break;
   case TOKEN_EOF:
    break;
   default:
    break;
  }
  free(tk);
 }
}

user_defined_types: enum token_type { TOKEN_DOCTYPE=0, TOKEN_START_TAG=1, TOKEN_END_TAG=2, TOKEN_COMMENT=3, TOKEN_MULTI_CHAR=4, TOKEN_EOF=5 }

user_defined_types: struct end_tag_token_s { unsigned char *tag_name; }

user_defined_types: struct comment_token_s { unsigned char *comment; }

user_defined_types: struct multi_char_token_s { unsigned char *mch; long char_count; }

user_defined_types: enum force_quirks_flag_type { ON=0, OFF=1 }

user_defined_types: enum self_closing_flag_type { SET=0, UNSET=1 }

user_defined_types: enum namespace_type { HTML=0, MATHML=1, SVG=2, XLINK=3, XML=4, XMLNS=5, DEFAULT=6 }


decompiled_code:
void __fastcall free_attributes(void *ptr)
{
void **v1; // rbx
void **v2; // rdi

v1 = (void **)ptr;
if ( ptr )
{
do
{
free(*v1);
free(v1[1]);
v2 = v1;
v1 = (void **)v1[3];
free(v2);
}
while ( v1 );
}
}

canonical_decompiled_code:
void __fastcall func3(void *ptr)
{
void **v1; // rbx
void **v2; // rdi

v1 = (void **)ptr;
if ( ptr )
{
do
{
free(*v1);
free(v1[1]);
v2 = v1;
v1 = (void **)v1[3];
free(v2);
}
while ( v1 );
}
}

original_code:
void free_attributes(attribute_list *list)
{
 attribute_list *temp_ptr;
 while(list != ((void *)0))
 {
  free(list->name);
  free(list->value);
  temp_ptr = list;
  list = list->tail;
  free(temp_ptr);
 }
}

canonical_original_code:
void free_attributes(struct attribute_list_s *list)
{
 struct attribute_list_s *temp_ptr;
 while(list != ((void *)0))
 {
  free(list->name);
  free(list->value);
  temp_ptr = list;
  list = list->tail;
  free(temp_ptr);
 }
}

variable_types: struct attribute_list_s { unsigned char *name; unsigned char *value; enum namespace_type { HTML=0, MATHML=1, SVG=2, XLINK=3, XML=4, XMLNS=5, DEFAULT=6 } attr_ns; struct attribute_list_s *tail; } *


decompiled_code:
__int64 __fastcall attribute_in_list(char *s2, char *a2, __int64 a3)
{
__int64 v3; // rbx
const char *v5; // r12

v3 = a3;
if ( !s2 || !a2 )
return 0LL;
if ( !a3 )
return 0LL;
do
{
if ( *(_QWORD *)v3 )
{
v5 = *(const char **)(v3 + 8);
if ( v5 )
{
if ( !strcmp(*(const char **)v3, s2) && !strcmp(v5, a2) )
return 1LL;
}
}
v3 = *(_QWORD *)(v3 + 24);
}
while ( v3 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *s2, char *a2, __int64 a3)
{
__int64 v3; // rbx
const char *v5; // r12

v3 = a3;
if ( !s2 || !a2 )
return 0LL;
if ( !a3 )
return 0LL;
do
{
if ( *(_QWORD *)v3 )
{
v5 = *(const char **)(v3 + 8);
if ( v5 )
{
if ( !strcmp(*(const char **)v3, s2) && !strcmp(v5, a2) )
return 1LL;
}
}
v3 = *(_QWORD *)(v3 + 24);
}
while ( v3 );
return 0LL;
}

original_code:
int attribute_in_list(unsigned char *name, unsigned char *value, attribute_list *list)
{
 if((name == ((void *)0)) || (value == ((void *)0)))
 {
  return 0;
 }
 while(list != ((void *)0))
 {
  if((list->name != ((void *)0)) && (list->value != ((void *)0)))
  {
   if((strcmp(list->name, name) == 0) && (strcmp(list->value, value) == 0))
   {
    return 1;
   }
  }
  list = list->tail;
 }
 return 0;
}

canonical_original_code:
int attribute_in_list(unsigned char *name, unsigned char *value, struct attribute_list_s *list)
{
 if((name == ((void *)0)) || (value == ((void *)0)))
 {
  return 0;
 }
 while(list != ((void *)0))
 {
  if((list->name != ((void *)0)) && (list->value != ((void *)0)))
  {
   if((strcmp(list->name, name) == 0) && (strcmp(list->value, value) == 0))
   {
    return 1;
   }
  }
  list = list->tail;
 }
 return 0;
}

variable_types:
name
unsigned char *

variable_types:
value
unsigned char *


decompiled_code:
__int64 __fastcall attr_list_comp(__int64 a1, __int64 a2)
{
__int64 v2; // r12
__int64 v3; // rbx
__int64 result; // rax

v2 = a2;
if ( a1 )
{
v3 = a1;
while ( (unsigned int)attribute_in_list(*(char **)v3, *(char **)(v3 + 8), a2) == 1 )
{
v3 = *(_QWORD *)(v3 + 24);
if ( !v3 )
goto LABEL_5;
}
result = 1LL;
}
else
{
LABEL_5:
if ( a2 )
{
while ( (unsigned int)attribute_in_list(*(char **)v2, *(char **)(v2 + 8), a1) == 1 )
{
v2 = *(_QWORD *)(v2 + 24);
if ( !v2 )
return 0LL;
}
result = 1LL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
__int64 v2; // r12
__int64 v3; // rbx
__int64 result; // rax

v2 = a2;
if ( a1 )
{
v3 = a1;
while ( (unsigned int)func4(*(char **)v3, *(char **)(v3 + 8), a2) == 1 )
{
v3 = *(_QWORD *)(v3 + 24);
if ( !v3 )
goto LABEL_5;
}
result = 1LL;
}
else
{
LABEL_5:
if ( a2 )
{
while ( (unsigned int)func4(*(char **)v2, *(char **)(v2 + 8), a1) == 1 )
{
v2 = *(_QWORD *)(v2 + 24);
if ( !v2 )
return 0LL;
}
result = 1LL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
int attr_list_comp(attribute_list *attr1, attribute_list *attr2)
{
 attribute_list *temp_attr1, *temp_attr2;
 temp_attr1 = attr1;
 temp_attr2 = attr2;
 while(temp_attr1 != ((void *)0))
 {
  if(attribute_in_list(temp_attr1->name, temp_attr1->value, attr2) != 1)
  {
   return 1;
  }
  temp_attr1 = temp_attr1->tail;
 }
 while(temp_attr2 != ((void *)0))
 {
  if(attribute_in_list(temp_attr2->name, temp_attr2->value, attr1) != 1)
  {
   return 1;
  }
  temp_attr2 = temp_attr2->tail;
 }
 return 0;
}

canonical_original_code:
int attr_list_comp(struct attribute_list_s *attr1, struct attribute_list_s *attr2)
{
 struct attribute_list_s *temp_attr1; struct attribute_list_s *temp_attr2;
 temp_attr1 = attr1;
 temp_attr2 = attr2;
 while(temp_attr1 != ((void *)0))
 {
  if(attribute_in_list(temp_attr1->name, temp_attr1->value, attr2) != 1)
  {
   return 1;
  }
  temp_attr1 = temp_attr1->tail;
 }
 while(temp_attr2 != ((void *)0))
 {
  if(attribute_in_list(temp_attr2->name, temp_attr2->value, attr1) != 1)
  {
   return 1;
  }
  temp_attr2 = temp_attr2->tail;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall attribute_name_in_list(char *s2, __int64 a2)
{
__int64 v2; // rbx
__int64 result; // rax

v2 = a2;
if ( !s2 )
_assert_fail("attr_name != NULL", "token.c", 0x12Au, "attribute_name_in_list");
result = 0LL;
if ( a2 )
{
do
{
if ( *(_QWORD *)v2 && !strcmp(*(const char **)v2, s2) )
return 1LL;
v2 = *(_QWORD *)(v2 + 24);
}
while ( v2 );
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *s2, __int64 a2)
{
__int64 v2; // rbx
__int64 result; // rax

v2 = a2;
if ( !s2 )
_assert_fail("attr_name != NULL", "token.c", 0x12Au, "attribute_name_in_list");
result = 0LL;
if ( a2 )
{
do
{
if ( *(_QWORD *)v2 && !strcmp(*(const char **)v2, s2) )
return 1LL;
v2 = *(_QWORD *)(v2 + 24);
}
while ( v2 );
result = 0LL;
}
return result;
}

original_code:
int attribute_name_in_list(unsigned char *attr_name, attribute_list *list)
{
 ((void) sizeof ((attr_name != ((void *)0)) ? 1 : 0), __extension__ ({ if (attr_name != ((void *)0)) ; else __assert_fail ("attr_name != NULL", "token.c", 298, __extension__ __PRETTY_FUNCTION__); }));
 while(list != ((void *)0))
 {
  if(list->name != ((void *)0))
  {
   if(strcmp(list->name, attr_name) == 0)
   {
    return 1;
   }
  }
  list = list->tail;
 }
 return 0;
}

canonical_original_code:
int attribute_name_in_list(unsigned char *attr_name, struct attribute_list_s *list)
{
 ((void) sizeof ((attr_name != ((void *)0)) ? 1 : 0), __extension__ ({ if (attr_name != ((void *)0)) ; else __assert_fail ("attr_name != NULL", "token.c", 298, __extension__ __PRETTY_FUNCTION__); }));
 while(list != ((void *)0))
 {
  if(list->name != ((void *)0))
  {
   if(strcmp(list->name, attr_name) == 0)
   {
    return 1;
   }
  }
  list = list->tail;
 }
 return 0;
}


decompiled_code:
char *__fastcall get_attribute_value(char *s2, char *a2)
{
char *v2; // rbx

v2 = a2;
if ( !s2 )
_assert_fail("attr_name != NULL", "token.c", 0x142u, "get_attribute_value");
if ( a2 )
{
while ( !*(_QWORD *)v2 || strcmp(*(const char **)v2, s2) )
{
v2 = (char *)*((_QWORD *)v2 + 3);
if ( !v2 )
return v2;
}
v2 = (char *)*((_QWORD *)v2 + 1);
if ( v2 )
v2 = strdup(v2);
}
return v2;
}

canonical_decompiled_code:
char *__fastcall func7(char *s2, char *a2)
{
char *v2; // rbx

v2 = a2;
if ( !s2 )
_assert_fail("attr_name != NULL", "token.c", 0x142u, "get_attribute_value");
if ( a2 )
{
while ( !*(_QWORD *)v2 || strcmp(*(const char **)v2, s2) )
{
v2 = (char *)*((_QWORD *)v2 + 3);
if ( !v2 )
return v2;
}
v2 = (char *)*((_QWORD *)v2 + 1);
if ( v2 )
v2 = strdup(v2);
}
return v2;
}

original_code:
unsigned char *get_attribute_value(unsigned char *attr_name, attribute_list *list)
{
 ((void) sizeof ((attr_name != ((void *)0)) ? 1 : 0), __extension__ ({ if (attr_name != ((void *)0)) ; else __assert_fail ("attr_name != NULL", "token.c", 322, __extension__ __PRETTY_FUNCTION__); }));
 while(list != ((void *)0))
 {
  if(list->name != ((void *)0))
  {
   if(strcmp(list->name, attr_name) == 0)
   {
    if(list->value != ((void *)0))
    {
     unsigned char *attr_value;
     attr_value = strdup(list->value);
     return attr_value;
    }
    else
    {
     return ((void *)0);
    }
   }
  }
  list = list->tail;
 }
 return ((void *)0);
}

canonical_original_code:
unsigned char *get_attribute_value(unsigned char *attr_name, struct attribute_list_s *list)
{
 ((void) sizeof ((attr_name != ((void *)0)) ? 1 : 0), __extension__ ({ if (attr_name != ((void *)0)) ; else __assert_fail ("attr_name != NULL", "token.c", 322, __extension__ __PRETTY_FUNCTION__); }));
 while(list != ((void *)0))
 {
  if(list->name != ((void *)0))
  {
   if(strcmp(list->name, attr_name) == 0)
   {
    if(list->value != ((void *)0))
    {
     unsigned char *attr_value;
     attr_value = strdup(list->value);
     return attr_value;
    }
    else
    {
     return ((void *)0);
    }
   }
  }
  list = list->tail;
 }
 return ((void *)0);
}

return_type: unsigned char *


decompiled_code:
void *__fastcall remove_attribute(char *s2, void *ptr)
{
void *v2; // r13
void *v3; // rbx
__int64 v4; // rbx
_QWORD *v5; // r14
void *v6; // r12
char *v7; // r15

v2 = ptr;
if ( !s2 )
_assert_fail("attr_name != NULL", "token.c", 0x167u, "remove_attribute");
if ( ptr )
{
v3 = *(void **)ptr;
if ( *(_QWORD *)ptr && !strcmp((const char *)v3, s2) )
{
v6 = (void *)*((_QWORD *)ptr + 3);
free(v3);
free(*((void **)ptr + 1));
free(ptr);
v2 = v6;
}
else
{
v4 = *((_QWORD *)ptr + 3);
if ( v4 )
{
v5 = ptr;
while ( 1 )
{
v7 = *(char **)v4;
if ( *(_QWORD *)v4 )
{
if ( !strcmp(*(const char **)v4, s2) )
break;
}
v5 = (_QWORD *)v4;
if ( !*(_QWORD *)(v4 + 24) )
return v2;
v4 = *(_QWORD *)(v4 + 24);
}
free(v7);
free(*(void **)(v4 + 8));
v5[3] = *(_QWORD *)(v4 + 24);
free((void *)v4);
}
}
}
return v2;
}

canonical_decompiled_code:
void *__fastcall func8(char *s2, void *ptr)
{
void *v2; // r13
void *v3; // rbx
__int64 v4; // rbx
_QWORD *v5; // r14
void *v6; // r12
char *v7; // r15

v2 = ptr;
if ( !s2 )
_assert_fail("attr_name != NULL", "token.c", 0x167u, "remove_attribute");
if ( ptr )
{
v3 = *(void **)ptr;
if ( *(_QWORD *)ptr && !strcmp((const char *)v3, s2) )
{
v6 = (void *)*((_QWORD *)ptr + 3);
free(v3);
free(*((void **)ptr + 1));
free(ptr);
v2 = v6;
}
else
{
v4 = *((_QWORD *)ptr + 3);
if ( v4 )
{
v5 = ptr;
while ( 1 )
{
v7 = *(char **)v4;
if ( *(_QWORD *)v4 )
{
if ( !strcmp(*(const char **)v4, s2) )
break;
}
v5 = (_QWORD *)v4;
if ( !*(_QWORD *)(v4 + 24) )
return v2;
v4 = *(_QWORD *)(v4 + 24);
}
free(v7);
free(*(void **)(v4 + 8));
v5[3] = *(_QWORD *)(v4 + 24);
free((void *)v4);
}
}
}
return v2;
}

original_code:
attribute_list *remove_attribute(unsigned char *attr_name, attribute_list *list)
{
 ((void) sizeof ((attr_name != ((void *)0)) ? 1 : 0), __extension__ ({ if (attr_name != ((void *)0)) ; else __assert_fail ("attr_name != NULL", "token.c", 359, __extension__ __PRETTY_FUNCTION__); }));
 if(list == ((void *)0))
 {
  return ((void *)0);
 }
 else
 {
  if((list->name != ((void *)0)) && (strcmp(list->name, attr_name) == 0))
  {
   attribute_list *list_tail = list->tail;
   free(list->name);
   free(list->value);
   free(list);
   return list_tail;
  }
  else
  {
   attribute_list *previous_node, *temp_list = list;
   previous_node = temp_list;
   temp_list = temp_list->tail;
   while(temp_list != ((void *)0))
   {
    if((temp_list->name != ((void *)0)) && (strcmp(temp_list->name, attr_name) == 0))
    {
     free(temp_list->name);
     free(temp_list->value);
     previous_node->tail = temp_list->tail;
     free(temp_list);
     break;
    }
    previous_node = temp_list;
    temp_list = temp_list->tail;
   }
   return list;
  }
 }
}

canonical_original_code:
struct attribute_list_s *remove_attribute(unsigned char *attr_name, struct attribute_list_s *list)
{
 ((void) sizeof ((attr_name != ((void *)0)) ? 1 : 0), __extension__ ({ if (attr_name != ((void *)0)) ; else __assert_fail ("attr_name != NULL", "token.c", 359, __extension__ __PRETTY_FUNCTION__); }));
 if(list == ((void *)0))
 {
  return ((void *)0);
 }
 else
 {
  if((list->name != ((void *)0)) && (strcmp(list->name, attr_name) == 0))
  {
   struct attribute_list_s *list_tail = list->tail;
   free(list->name);
   free(list->value);
   free(list);
   return list_tail;
  }
  else
  {
   struct attribute_list_s *previous_node; struct attribute_list_s *temp_list = list;
   previous_node = temp_list;
   temp_list = temp_list->tail;
   while(temp_list != ((void *)0))
   {
    if((temp_list->name != ((void *)0)) && (strcmp(temp_list->name, attr_name) == 0))
    {
     free(temp_list->name);
     free(temp_list->value);
     previous_node->tail = temp_list->tail;
     free(temp_list);
     break;
    }
    previous_node = temp_list;
    temp_list = temp_list->tail;
   }
   return list;
  }
 }
}

variable_types:
list
struct attribute_list_s *

return_type: struct attribute_list_s { unsigned char *name; unsigned char *value; enum namespace_type { HTML=0, MATHML=1, SVG=2, XLINK=3, XML=4, XMLNS=5, DEFAULT=6 } attr_ns; struct attribute_list_s *tail; } *