decompiled_code:
void __fastcall __noreturn http_request_thread(__int64 a1)
{
unsigned int v1; // eax
__int64 v2; // r12
size_t v3; // rax
unsigned int v4; // ebx
unsigned int v5; // eax
unsigned int v6; // eax
int v7; // ebx
__int16 v8[2]; // [rsp+0h] [rbp-1028h] BYREF
struct in_addr in; // [rsp+4h] [rbp-1024h] BYREF
char buf[4120]; // [rsp+10h] [rbp-1018h] BYREF

if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 234LL);
v4 = *(__int16 *)(a1 + 16);
v5 = (unsigned int)inet_ntoa((struct in_addr)(*(struct in_addr **)a1)->s_addr);
fprintf(stream, "HTTP REQUEST %s:%d\n", (const char *)v5, v4);
fputc(10, stream);
}
v8[0] = 2;
v8[1] = _bswap_16(*(_WORD *)(a1 + 16));
v1 = (unsigned int)inet_ntoa((struct in_addr)(*(struct in_addr **)a1)->s_addr);
Inet_pton(2LL, v1, &in);
v2 = (unsigned int)Socket(2LL, 1LL, 0LL);
Connect(v2, v8, 16LL);
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 246LL);
v6 = (unsigned int)inet_ntoa(in);
fprintf(stream, "connected to %s\n", (const char *)v6);
fputc(10, stream);
}
memset(buf, 0, 0x1000uLL);
read(*(_DWORD *)(a1 + 8), buf, 0x1000uLL);
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 255LL);
fwrite("======================", 1uLL, 0x16uLL, stream);
fputc(10, stream);
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 256LL);
fwrite("client->proxy->server:", 1uLL, 0x16uLL, stream);
fputc(10, stream);
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 257LL);
fprintf(stream, "======================\n%s", buf);
fputc(10, stream);
}
}
}
v3 = strlen(buf);
write(v2, buf, v3);
memset(buf, 0, 0x1000uLL);
while ( 1 )
{
v7 = read(v2, buf, 0x1000uLL);
if ( v7 <= 0 )
break;
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 268LL);
fwrite("=====================", 1uLL, 0x15uLL, stream);
fputc(10, stream);
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 269LL);
fwrite("client<-proxy<-sever:", 1uLL, 0x15uLL, stream);
fputc(10, stream);
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 270LL);
fprintf(stream, "=====================\n%s", buf);
fputc(10, stream);
}
}
}
write(*(_DWORD *)(a1 + 8), buf, (unsigned int)v7);
}
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 273LL);
fwrite("FINISHED PROCESSING REQUEST", 1uLL, 0x1BuLL, stream);
fputc(10, stream);
}
close(v2);
close(*(_DWORD *)(a1 + 8));
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 32));
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(__int64 a1)
{
unsigned int v1; // eax
__int64 v2; // r12
size_t v3; // rax
unsigned int v4; // ebx
unsigned int v5; // eax
unsigned int v6; // eax
int v7; // ebx
__int16 v8[2]; // [rsp+0h] [rbp-1028h] BYREF
struct in_addr in; // [rsp+4h] [rbp-1024h] BYREF
char buf[4120]; // [rsp+10h] [rbp-1018h] BYREF

if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 234LL);
v4 = *(__int16 *)(a1 + 16);
v5 = (unsigned int)inet_ntoa((struct in_addr)(*(struct in_addr **)a1)->s_addr);
fprintf(stream, "HTTP REQUEST %s:%d\n", (const char *)v5, v4);
fputc(10, stream);
}
v8[0] = 2;
v8[1] = func6(*(_WORD *)(a1 + 16));
v1 = (unsigned int)inet_ntoa((struct in_addr)(*(struct in_addr **)a1)->s_addr);
Inet_pton(2LL, v1, &in);
v2 = (unsigned int)Socket(2LL, 1LL, 0LL);
Connect(v2, v8, 16LL);
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 246LL);
v6 = (unsigned int)inet_ntoa(in);
fprintf(stream, "connected to %s\n", (const char *)v6);
fputc(10, stream);
}
memset(buf, 0, 0x1000uLL);
read(*(_DWORD *)(a1 + 8), buf, 0x1000uLL);
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 255LL);
fwrite("======================", 1uLL, 0x16uLL, stream);
fputc(10, stream);
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 256LL);
fwrite("client->proxy->server:", 1uLL, 0x16uLL, stream);
fputc(10, stream);
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 257LL);
fprintf(stream, "======================\n%s", buf);
fputc(10, stream);
}
}
}
v3 = strlen(buf);
write(v2, buf, v3);
memset(buf, 0, 0x1000uLL);
while ( 1 )
{
v7 = read(v2, buf, 0x1000uLL);
if ( v7 <= 0 )
break;
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 268LL);
fwrite("=====================", 1uLL, 0x15uLL, stream);
fputc(10, stream);
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 269LL);
fwrite("client<-proxy<-sever:", 1uLL, 0x15uLL, stream);
fputc(10, stream);
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 270LL);
fprintf(stream, "=====================\n%s", buf);
fputc(10, stream);
}
}
}
write(*(_DWORD *)(a1 + 8), buf, (unsigned int)v7);
}
if ( logging_level <= 0 )
{
fprintf(stream, "%s:%d:\t", "services.c", 273LL);
fwrite("FINISHED PROCESSING REQUEST", 1uLL, 0x1BuLL, stream);
fputc(10, stream);
}
close(v2);
close(*(_DWORD *)(a1 + 8));
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 32));
pthread_exit(0LL);
}

original_code:
void *
http_request_thread(void *connection_ptr)
{
    int n;
    char buf[4096];
    struct sockaddr_in serveraddr;
    struct Connection *connection;
    int serverfd;
    connection = (struct Connection *)connection_ptr;
    do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 234); fprintf(stream, "HTTP REQUEST %s:%d\n", inet_ntoa(connection->node->ip), connection->port); fprintf(stream, "\n"); } } while(0);
    serveraddr.sin_family = 2;
    serveraddr.sin_port = htons(connection->port);
    Inet_pton(2, inet_ntoa(connection->node->ip),
              &serveraddr.sin_addr);
    serverfd = Socket(2, SOCK_STREAM, 0);
    Connect(serverfd, (struct sockaddr *)&serveraddr,
            sizeof(serveraddr));
    do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 246); fprintf(stream, "connected to %s\n", inet_ntoa(serveraddr.sin_addr)); fprintf(stream, "\n"); } } while(0);
    memset(buf, 0, sizeof(char) * 4096);
    read(connection->clientfd, buf, 4096);
    do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 255); fprintf(stream, "======================"); fprintf(stream, "\n"); } } while(0);
    do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 256); fprintf(stream, "client->proxy->server:"); fprintf(stream, "\n"); } } while(0);
    do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 257); fprintf(stream, "======================\n%s", buf); fprintf(stream, "\n"); } } while(0);
    write(serverfd, buf, strlen(buf));
    memset(buf, 0, sizeof(char) * 4096);
    while ( (n = read(serverfd, buf, 4096)) > 0) {
        do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 268); fprintf(stream, "====================="); fprintf(stream, "\n"); } } while(0);
        do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 269); fprintf(stream, "client<-proxy<-sever:"); fprintf(stream, "\n"); } } while(0);
        do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 270); fprintf(stream, "=====================\n%s", buf); fprintf(stream, "\n"); } } while(0);
        write(connection->clientfd, buf, n);
    }
    do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 273); fprintf(stream, "FINISHED PROCESSING REQUEST"); fprintf(stream, "\n"); } } while(0);
    close(serverfd);
    close(connection->clientfd);
    pthread_mutex_unlock(&connection->in_progress);
    pthread_exit(((void *)0));
}

canonical_original_code:
void *http_request_thread(void *connection_ptr)
{
    int n;
    char buf[4096];
    struct sockaddr_in serveraddr;
    struct Connection *connection;
    int serverfd;
    connection = (struct Connection *)connection_ptr;
    do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 234); fprintf(stream, "HTTP REQUEST %s:%d\n", inet_ntoa(connection->node->ip), connection->port); fprintf(stream, "\n"); } } while(0);
    serveraddr.sin_family = 2;
    serveraddr.sin_port = htons(connection->port);
    Inet_pton(2, inet_ntoa(connection->node->ip),
              &serveraddr.sin_addr);
    serverfd = Socket(2, SOCK_STREAM, 0);
    Connect(serverfd, (struct sockaddr *)&serveraddr,
            sizeof(serveraddr));
    do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 246); fprintf(stream, "connected to %s\n", inet_ntoa(serveraddr.sin_addr)); fprintf(stream, "\n"); } } while(0);
    memset(buf, 0, sizeof(char) * 4096);
    read(connection->clientfd, buf, 4096);
    do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 255); fprintf(stream, "======================"); fprintf(stream, "\n"); } } while(0);
    do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 256); fprintf(stream, "client->proxy->server:"); fprintf(stream, "\n"); } } while(0);
    do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 257); fprintf(stream, "======================\n%s", buf); fprintf(stream, "\n"); } } while(0);
    write(serverfd, buf, strlen(buf));
    memset(buf, 0, sizeof(char) * 4096);
    while ( (n = read(serverfd, buf, 4096)) > 0) {
        do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 268); fprintf(stream, "====================="); fprintf(stream, "\n"); } } while(0);
        do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 269); fprintf(stream, "client<-proxy<-sever:"); fprintf(stream, "\n"); } } while(0);
        do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 270); fprintf(stream, "=====================\n%s", buf); fprintf(stream, "\n"); } } while(0);
        write(connection->clientfd, buf, n);
    }
    do { if (logging_level <= DEBUG) { fprintf(stream, "%s:%d:\t", "services.c", 273); fprintf(stream, "FINISHED PROCESSING REQUEST"); fprintf(stream, "\n"); } } while(0);
    close(serverfd);
    close(connection->clientfd);
    pthread_mutex_unlock(&connection->in_progress);
    pthread_exit(((void *)0));
}

variable_types: n int

variable_types:
buf
char [4096]

variable_types: serverfd int

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

user_defined_types: struct Node { int ip; unsigned int rtt; struct Node *next; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


repo: dgkimura/Arbitrator

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

result = "HTTP";
if ( a1 != 1 )
{
result = "HTTPS";
if ( a1 != 2 )
{
result = "unknown";
if ( !a1 )
result = "FTP";
}
}
return result;
}

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

result = "HTTP";
if ( a1 != 1 )
{
result = "HTTPS";
if ( a1 != 2 )
{
result = "unknown";
if ( !a1 )
result = "FTP";
}
}
return result;
}

original_code:
const char *
service_str(service_e service_type)
{
    switch (service_type) {
        case (FTP):
            return "FTP";
        case (HTTP):
            return "HTTP";
        case (HTTPS):
            return "HTTPS";
        default:
            return "unknown";
    }
}

canonical_original_code:
char *service_str(enum service_e service_type)
{
    switch (service_type) {
        case (FTP):
            return "FTP";
        case (HTTP):
            return "HTTP";
        case (HTTPS):
            return "HTTPS";
        default:
            return "unknown";
    }
}

variable_types: enum service_e { FTP=0, HTTP=1, HTTPS=2 }

return_type: char *


decompiled_code:
int __fastcall start_service(int a1, __int16 a2)
{
pthread_t *v2; // rbx
const char *v4; // rax

v2 = (pthread_t *)check_service(a1, a2);
if ( !v2 )
{
v2 = (pthread_t *)add_service(a1, a2);
if ( logging_level <= 1 )
{
v4 = service_str(a1);
fprintf(stream, "STARTED service %s:%hu", v4, (unsigned int)a2);
fputc(10, stream);
}
}
return pthread_create(v2 + 1, 0LL, (void *(*)(void *))run_service_thread, v2);
}

canonical_decompiled_code:
int __fastcall func11(int a1, __int16 a2)
{
pthread_t *v2; // rbx
const char *v4; // rax

v2 = (pthread_t *)func9(a1, a2);
if ( !v2 )
{
v2 = (pthread_t *)add_service(a1, a2);
if ( logging_level <= 1 )
{
v4 = func10(a1);
fprintf(stream, "STARTED service %s:%hu", v4, (unsigned int)a2);
fputc(10, stream);
}
}
return pthread_create(v2 + 1, 0LL, (void *(*)(void *))run_service_thread, v2);
}

original_code:
int
start_service(service_e service_type, short port)
{
    struct Service *service;
    if ( (service = check_service(service_type, port)) == ((void *)0)) {
        service = add_service(service_type, port);
        do { if (logging_level <= INFO) { fprintf(stream, "STARTED service %s:%hu", service_str(service_type), port); fprintf(stream, "\n"); } } while(0);
    }
    pthread_create(&service->thread, ((void *)0), run_service_thread, service);
}

canonical_original_code:
int start_service(enum service_e service_type, short port)
{
    struct Service *service;
    if ( (service = check_service(service_type, port)) == ((void *)0)) {
        service = add_service(service_type, port);
        do { if (logging_level <= INFO) { fprintf(stream, "STARTED service %s:%hu", service_str(service_type), port); fprintf(stream, "\n"); } } while(0);
    }
    pthread_create(&service->thread, ((void *)0), run_service_thread, service);
}

variable_types: port short

variable_types: struct Service { short port; enum service_e { FTP=0, HTTP=1, HTTPS=2 } name; unsigned long thread; void *(*handle)(void *); struct hashtable { unsigned int table_size; struct hashentry { char *key; void *value; struct hashentry *next; } *entries[]; } *connection_table; struct list_head { struct list_head *next; } list; } *

return_type: int

user_defined_types: struct Service { short port; enum service_e name; unsigned long thread; void *(*handle)(void *); struct hashtable *connection_table; struct list_head list; }

user_defined_types: struct hashtable { unsigned int table_size; struct hashentry *entries[]; }


decompiled_code:
int algorithm_dynamicratio()
{
return puts("IMPLEMENT: dynamic ratio.");
}

canonical_decompiled_code:
int func0()
{
return puts("IMPLEMENT: dynamic ratio.");
}

original_code:
node_t *
algorithm_dynamicratio(const char *source_ip)
{
    printf("IMPLEMENT: dynamic ratio.\n");
}

canonical_original_code:
struct Node *algorithm_dynamicratio(char *source_ip)
{
    printf("IMPLEMENT: dynamic ratio.\n");
}

return_type: struct Node { int ip; unsigned int rtt; struct Node *next; } *


decompiled_code:
int init_dynamicratio()
{
return puts("IMPLEMENT: init dynamic ratio.");
}

canonical_decompiled_code:
int func1()
{
return puts("IMPLEMENT: init dynamic ratio.");
}

original_code:
int
init_dynamicratio()
{
    printf("IMPLEMENT: init dynamic ratio.\n");
}


decompiled_code:
__int64 __fastcall set_algorithm(unsigned int a1)
{
LODWORD(current_algorithm) = a1;
if ( a1 == 2 )
{
*(&current_algorithm + 1) = (__int64 (*)(void))&algorithm_ratio;
*(&current_algorithm + 2) = (__int64 (*)(void))&init_ratio;
goto LABEL_8;
}
if ( a1 <= 2 )
{
if ( a1 )
{
*(&current_algorithm + 1) = (__int64 (*)(void))&algorithm_leastconnections;
*(&current_algorithm + 2) = (__int64 (*)(void))&init_leastconnections;
}
else
{
*(&current_algorithm + 1) = (__int64 (*)(void))&algorithm_dynamicratio;
*(&current_algorithm + 2) = (__int64 (*)(void))&init_dynamicratio;
}
goto LABEL_8;
}
if ( a1 == 3 )
{
*(&current_algorithm + 1) = (__int64 (*)(void))&algorithm_roundrobin;
*(&current_algorithm + 2) = (__int64 (*)(void))&init_roundrobin;
LABEL_8:
(*(&current_algorithm + 2))();
return 1LL;
}
printf("%s: Unknown algorithm name\n", "set_algorithm");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
LODWORD(current_algorithm) = a1;
if ( a1 == 2 )
{
*(&current_algorithm + 1) = (__int64 (*)(void))&algorithm_ratio;
*(&current_algorithm + 2) = (__int64 (*)(void))&init_ratio;
goto LABEL_8;
}
if ( a1 <= 2 )
{
if ( a1 )
{
*(&current_algorithm + 1) = (__int64 (*)(void))&algorithm_leastconnections;
*(&current_algorithm + 2) = (__int64 (*)(void))&init_leastconnections;
}
else
{
*(&current_algorithm + 1) = (__int64 (*)(void))&algorithm_dynamicratio;
*(&current_algorithm + 2) = (__int64 (*)(void))&init_dynamicratio;
}
goto LABEL_8;
}
if ( a1 == 3 )
{
*(&current_algorithm + 1) = (__int64 (*)(void))&algorithm_roundrobin;
*(&current_algorithm + 2) = (__int64 (*)(void))&init_roundrobin;
LABEL_8:
(*(&current_algorithm + 2))();
return 1LL;
}
printf("%s: Unknown algorithm name\n", "set_algorithm");
return 0xFFFFFFFFLL;
}

original_code:
int
set_algorithm(algorithm_name name)
{
    current_algorithm.name = name;
    switch (current_algorithm.name) {
        case DYNAMICRATIO:
            current_algorithm.function = algorithm_dynamicratio;
            current_algorithm.initialize = init_dynamicratio;
            break;
        case LEASTCONNECTIONS:
            current_algorithm.function = algorithm_leastconnections;
            current_algorithm.initialize = init_leastconnections;
            break;
        case RATIO:
            current_algorithm.function = algorithm_ratio;
            current_algorithm.initialize = init_ratio;
            break;
        case ROUNDROBIN:
            current_algorithm.function = algorithm_roundrobin;
            current_algorithm.initialize = init_roundrobin;
            break;
        default:
            printf("%s: Unknown algorithm name\n", __func__);
            return -1;
    }
    current_algorithm.initialize();
    return 1;
}

canonical_original_code:
int set_algorithm(enum algorithm_name name)
{
    current_algorithm.name = name;
    switch (current_algorithm.name) {
        case DYNAMICRATIO:
            current_algorithm.function = algorithm_dynamicratio;
            current_algorithm.initialize = init_dynamicratio;
            break;
        case LEASTCONNECTIONS:
            current_algorithm.function = algorithm_leastconnections;
            current_algorithm.initialize = init_leastconnections;
            break;
        case RATIO:
            current_algorithm.function = algorithm_ratio;
            current_algorithm.initialize = init_ratio;
            break;
        case ROUNDROBIN:
            current_algorithm.function = algorithm_roundrobin;
            current_algorithm.initialize = init_roundrobin;
            break;
        default:
            printf("%s: Unknown algorithm name\n", __func__);
            return -1;
    }
    current_algorithm.initialize();
    return 1;
}

variable_types: enum algorithm_name { DYNAMICRATIO=0, LEASTCONNECTIONS=1, RATIO=2, ROUNDROBIN=3 }


CmdStatus

decompiled_code:
int CmdStatus()
{
return puts("Cmd Status");
}

canonical_decompiled_code:
int func0()
{
return puts("Cmd Status");
}

original_code:
int
CmdStatus(int cargc, char *cargv[])
{
  printf("Cmd Status\n");
  return;
}

variable_types: cargc int

variable_types:
cargv
char *[]


CmdLog

decompiled_code:
int CmdLog()
{
return puts("Cmd Log");
}

canonical_decompiled_code:
int func1()
{
return puts("Cmd Log");
}

original_code:
int
CmdLog(int cargc, char *cargv[])
{
  printf("Cmd Log\n");
  return;
}


CmdVerbose

decompiled_code:
int __fastcall CmdVerbose(int a1, __int64 a2)
{
const char *v2; // rbx
int result; // eax

if ( a1 <= 1 )
goto LABEL_4;
v2 = *(const char **)(a2 + 8);
if ( !strcasecmp(v2, "on") )
{
Verbose = 1;
puts("Verbose mode is now on");
return set_logging(stdout, 0LL);
}
if ( !strcasecmp(v2, "off") )
{
Verbose = 0;
puts("Verbose mode is now off");
result = set_logging(stdout, 1LL);
}
else
{
LABEL_4:
if ( Verbose )
result = puts("Verbose mode is currently on.");
else
result = puts("Verbose mode is currently off.");
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(int a1, __int64 a2)
{
const char *v2; // rbx
int result; // eax

if ( a1 <= 1 )
goto LABEL_4;
v2 = *(const char **)(a2 + 8);
if ( !strcasecmp(v2, "on") )
{
Verbose = 1;
puts("Verbose mode is now on");
return set_logging(stdout, 0LL);
}
if ( !strcasecmp(v2, "off") )
{
Verbose = 0;
puts("Verbose mode is now off");
result = set_logging(stdout, 1LL);
}
else
{
LABEL_4:
if ( Verbose )
result = puts("Verbose mode is currently on.");
else
result = puts("Verbose mode is currently off.");
}
return result;
}

original_code:
int
CmdVerbose(int cargc, char *cargv[])
{
  if (cargc > 1) {
    if (!strcasecmp(cargv[1], "on")) {
      Verbose = 1;
      printf("Verbose mode is now on\n");
      set_logging(stdout, DEBUG);
      return;
    }
    if (!strcasecmp(cargv[1], "off")) {
      Verbose = 0;
      printf("Verbose mode is now off\n");
      set_logging(stdout, INFO);
      return;
    }
  }
  if (Verbose) {
    printf("Verbose mode is currently on.\n");
  } else {
    printf("Verbose mode is currently off.\n");
  }
  return;
}


CmdStartStop

decompiled_code:
int __fastcall CmdStartStop(int a1, const char **a2, __int64 (__fastcall *a3)(_QWORD, _QWORD))
{
const char *v4; // rbp
const char *v5; // rbp
unsigned int v6; // er12
int result; // eax
__int16 v8; // ax

if ( a1 == 1 )
return puts("****list start stop options****");
v4 = a2[1];
if ( strcasecmp(v4, "service") )
return printf("Cannot start or stop \"%s\"\n", v4);
if ( a1 == 4 )
{
v5 = a2[2];
if ( !strcasecmp(v5, "FTP") )
{
v6 = 0;
}
else if ( !strcasecmp(v5, "HTTP") )
{
v6 = 1;
}
else
{
v6 = 2;
if ( strcasecmp(v5, "HTTPS") )
return printf("unknown service type \"%s\"\n", v5);
}
v8 = atoi(a2[3]);
result = a3(v6, (unsigned int)v8);
}
else
{
puts("****list current services****");
result = show_services();
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(int a1, const char **a2, __int64 (__fastcall *a3)(_QWORD, _QWORD))
{
const char *v4; // rbp
const char *v5; // rbp
unsigned int v6; // er12
int result; // eax
__int16 v8; // ax

if ( a1 == 1 )
return puts("****list start stop options****");
v4 = a2[1];
if ( strcasecmp(v4, "service") )
return printf("Cannot start or stop \"%s\"\n", v4);
if ( a1 == 4 )
{
v5 = a2[2];
if ( !strcasecmp(v5, "FTP") )
{
v6 = 0;
}
else if ( !strcasecmp(v5, "HTTP") )
{
v6 = 1;
}
else
{
v6 = 2;
if ( strcasecmp(v5, "HTTPS") )
return printf("unknown service type \"%s\"\n", v5);
}
v8 = atoi(a2[3]);
result = a3(v6, (unsigned int)v8);
}
else
{
puts("****list current services****");
result = show_services();
}
return result;
}

original_code:
int
CmdStartStop(int cargc, char *cargv[], start_stop_service_routine func)
{
  service_e service;
  if (cargc == 1) {
    printf("****list start stop options****\n");
    return;
  }
  if (!strcasecmp(cargv[1], "service")) {
    if (cargc != 4) {
      printf("****list current services****\n");
      show_services();
      return;
    }
    if (!strcasecmp(cargv[2], "FTP")) { service = FTP; }
    else if (!strcasecmp(cargv[2], "HTTP")) { service = HTTP; }
    else if (!strcasecmp(cargv[2], "HTTPS")) { service = HTTPS; }
    else {
      printf("unknown service type \"%s\"\n", cargv[2]);
      return;
    }
    func(service, atoi(cargv[3]));
  } else {
    printf("Cannot start or stop \"%s\"\n", cargv[1]);
  }
  return;
}

canonical_original_code:
int CmdStartStop(int cargc, char *cargv[], int func(enum service_e, short))
{
  enum service_e service;
  if (cargc == 1) {
    printf("****list start stop options****\n");
    return;
  }
  if (!strcasecmp(cargv[1], "service")) {
    if (cargc != 4) {
      printf("****list current services****\n");
      show_services();
      return;
    }
    if (!strcasecmp(cargv[2], "FTP")) { service = FTP; }
    else if (!strcasecmp(cargv[2], "HTTP")) { service = HTTP; }
    else if (!strcasecmp(cargv[2], "HTTPS")) { service = HTTPS; }
    else {
      printf("unknown service type \"%s\"\n", cargv[2]);
      return;
    }
    func(service, atoi(cargv[3]));
  } else {
    printf("Cannot start or stop \"%s\"\n", cargv[1]);
  }
  return;
}

variable_types: int (enum service_e { FTP=0, HTTP=1, HTTPS=2 }, short)


CmdAddRemove

decompiled_code:
int __fastcall CmdAddRemove(int a1, __int64 a2, __int64 (__fastcall *a3)(_QWORD))
{
const char *v4; // r13

if ( a1 == 1 )
return puts("****list options for add remove****");
v4 = *(const char **)(a2 + 8);
if ( strcasecmp(v4, "node") )
return printf("cannot add or remove \"%s\"\n", v4);
if ( a1 == 3 )
return a3(*(_QWORD *)(a2 + 16));
puts("****list current set of nodes****");
return show_nodes();
}

canonical_decompiled_code:
int __fastcall func4(int a1, __int64 a2, __int64 (__fastcall *a3)(_QWORD))
{
const char *v4; // r13

if ( a1 == 1 )
return puts("****list options for add remove****");
v4 = *(const char **)(a2 + 8);
if ( strcasecmp(v4, "node") )
return printf("cannot add or remove \"%s\"\n", v4);
if ( a1 == 3 )
return a3(*(_QWORD *)(a2 + 16));
puts("****list current set of nodes****");
return show_nodes();
}

original_code:
int
CmdAddRemove(int cargc, char *cargv[], add_remove_node_routine func)
{
  if (cargc == 1) {
    printf("****list options for add remove****\n");
    return;
  }
  if (!strcasecmp(cargv[1], "node")) {
    if (cargc != 3) {
      printf("****list current set of nodes****\n");
      show_nodes();
      return;
    }
    func(cargv[2]);
  } else {
    printf("cannot add or remove \"%s\"\n", cargv[1]);
  }
  return;
}

canonical_original_code:
int CmdAddRemove(int cargc, char *cargv[], int func(char *))
{
  if (cargc == 1) {
    printf("****list options for add remove****\n");
    return;
  }
  if (!strcasecmp(cargv[1], "node")) {
    if (cargc != 3) {
      printf("****list current set of nodes****\n");
      show_nodes();
      return;
    }
    func(cargv[2]);
  } else {
    printf("cannot add or remove \"%s\"\n", cargv[1]);
  }
  return;
}

variable_types:
func
int (char *)


CmdSet

decompiled_code:
int __fastcall CmdSet(int a1, __int64 a2)
{
const char *v2; // r12
const char *v3; // rbx
int result; // eax

if ( a1 == 1 )
return puts("****list set options****");
v2 = *(const char **)(a2 + 8);
if ( strcasecmp(v2, "algorithm") )
return printf("cannot set \"%s\"\n", v2);
if ( a1 == 3 )
{
v3 = *(const char **)(a2 + 16);
if ( !strcasecmp(v3, "DYNAMICRATIO") )
{
result = set_algorithm(0LL);
}
else if ( !strcasecmp(v3, "LEASTCONNECTIONS") )
{
result = set_algorithm(1LL);
}
else if ( !strcasecmp(v3, "RATIO") )
{
result = set_algorithm(2LL);
}
else if ( !strcasecmp(v3, "ROUNDROBIN") )
{
result = set_algorithm(3LL);
}
else
{
result = printf("unknown algorithm type \"%s\"\n", v3);
}
}
else
{
printf("Currently set algorithm is:");
if ( current_algorithm == 2 )
{
result = puts(" RATIO");
}
else if ( current_algorithm > 2u )
{
if ( current_algorithm == 3 )
result = puts(" ROUNDROBIN");
else
result = puts(" undefined");
}
else if ( current_algorithm )
{
result = puts(" LEASTCONNECTIONS");
}
else
{
result = puts(" DYNAMICRATIO");
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(int a1, __int64 a2)
{
const char *v2; // r12
const char *v3; // rbx
int result; // eax

if ( a1 == 1 )
return puts("****list set options****");
v2 = *(const char **)(a2 + 8);
if ( strcasecmp(v2, "algorithm") )
return printf("cannot set \"%s\"\n", v2);
if ( a1 == 3 )
{
v3 = *(const char **)(a2 + 16);
if ( !strcasecmp(v3, "DYNAMICRATIO") )
{
result = set_algorithm(0LL);
}
else if ( !strcasecmp(v3, "LEASTCONNECTIONS") )
{
result = set_algorithm(1LL);
}
else if ( !strcasecmp(v3, "RATIO") )
{
result = set_algorithm(2LL);
}
else if ( !strcasecmp(v3, "ROUNDROBIN") )
{
result = set_algorithm(3LL);
}
else
{
result = printf("unknown algorithm type \"%s\"\n", v3);
}
}
else
{
printf("Currently set algorithm is:");
if ( current_algorithm == 2 )
{
result = puts(" RATIO");
}
else if ( current_algorithm > 2u )
{
if ( current_algorithm == 3 )
result = puts(" ROUNDROBIN");
else
result = puts(" undefined");
}
else if ( current_algorithm )
{
result = puts(" LEASTCONNECTIONS");
}
else
{
result = puts(" DYNAMICRATIO");
}
}
return result;
}

original_code:
int
CmdSet(int cargc, char * cargv[])
{
  if (cargc == 1) {
    printf("****list set options****\n");
    return;
  }
  if (!strcasecmp(cargv[1], "algorithm")) {
    if (cargc != 3) {
      printf("Currently set algorithm is:");
      switch (current_algorithm.name) {
      case DYNAMICRATIO:
        printf(" DYNAMICRATIO\n");
        break;
      case LEASTCONNECTIONS:
        printf(" LEASTCONNECTIONS\n");
        break;
      case RATIO:
        printf(" RATIO\n");
        break;
      case ROUNDROBIN:
        printf(" ROUNDROBIN\n");
        break;
      default:
        printf(" undefined\n");
        break;
      }
      return;
    }
    if (!strcasecmp(cargv[2], "DYNAMICRATIO")) { set_algorithm(DYNAMICRATIO); }
    else if (!strcasecmp(cargv[2], "LEASTCONNECTIONS")) { set_algorithm(LEASTCONNECTIONS); }
    else if (!strcasecmp(cargv[2], "RATIO")) { set_algorithm(RATIO); }
    else if (!strcasecmp(cargv[2], "ROUNDROBIN")) { set_algorithm(ROUNDROBIN); }
    else {
      printf("unknown algorithm type \"%s\"\n", cargv[2]);
    }
  } else {
    printf("cannot set \"%s\"\n", cargv[1]);
  }
  return;
}

canonical_original_code:
int CmdSet(int cargc, char *cargv[])
{
  if (cargc == 1) {
    printf("****list set options****\n");
    return;
  }
  if (!strcasecmp(cargv[1], "algorithm")) {
    if (cargc != 3) {
      printf("Currently set algorithm is:");
      switch (current_algorithm.name) {
      case DYNAMICRATIO:
        printf(" DYNAMICRATIO\n");
        break;
      case LEASTCONNECTIONS:
        printf(" LEASTCONNECTIONS\n");
        break;
      case RATIO:
        printf(" RATIO\n");
        break;
      case ROUNDROBIN:
        printf(" ROUNDROBIN\n");
        break;
      default:
        printf(" undefined\n");
        break;
      }
      return;
    }
    if (!strcasecmp(cargv[2], "DYNAMICRATIO")) { set_algorithm(DYNAMICRATIO); }
    else if (!strcasecmp(cargv[2], "LEASTCONNECTIONS")) { set_algorithm(LEASTCONNECTIONS); }
    else if (!strcasecmp(cargv[2], "RATIO")) { set_algorithm(RATIO); }
    else if (!strcasecmp(cargv[2], "ROUNDROBIN")) { set_algorithm(ROUNDROBIN); }
    else {
      printf("unknown algorithm type \"%s\"\n", cargv[2]);
    }
  } else {
    printf("cannot set \"%s\"\n", cargv[1]);
  }
  return;
}


CmdRead

decompiled_code:
int __fastcall CmdRead(int a1, __int64 a2)
{
FILE *v2; // rax

if ( a1 <= 1 )
{
LODWORD(v2) = puts("must specify a file to open.");
}
else
{
v2 = fopen(*(const char **)(a2 + 8), "r");
ReadFile = v2;
if ( !v2 )
LODWORD(v2) = printf("Error in opening \"%s\"\n", *(const char **)(a2 + 8));
}
return (int)v2;
}

canonical_decompiled_code:
int __fastcall func6(int a1, __int64 a2)
{
FILE *v2; // rax

if ( a1 <= 1 )
{
LODWORD(v2) = puts("must specify a file to open.");
}
else
{
v2 = fopen(*(const char **)(a2 + 8), "r");
ReadFile = v2;
if ( !v2 )
LODWORD(v2) = printf("Error in opening \"%s\"\n", *(const char **)(a2 + 8));
}
return (int)v2;
}

original_code:
int
CmdRead(int cargc, char *cargv[])
{
  if (cargc < 2) {
    printf("must specify a file to open.\n");
    return;
  }
  if ((ReadFile = fopen(cargv[1], "r")) == ((void *)0)) {
    printf("Error in opening \"%s\"\n", cargv[1]);
  }
  return;
}


CmdHelp

decompiled_code:
int CmdHelp()
{
puts("Commands are:");
puts(" \"quit\" - quit this program");
puts(" \"status\" - print the current system status");
puts(" \"log\" - print the message log");
puts(" \"verbose [on|off]\" - switch between verbose and quite mode");
puts(" \"start service [FTP | HTTP | HTTPS] <port>\" ");
puts(" \"stop service [FTP | HTTP | HTTPS] <port>\" ");
puts(" \"add node <ipaddress>\" ");
puts(" \"remove node <ipaddress>\" ");
puts(" \"set algorithm [DYNAMICRATIO | LEASTCONNECTIONS | RATIO | ROUNDROBIN]\" ");
puts(" \"reed <file>\" - read commands from the specified file");
return puts(" \"help\" or \"?\" - print this help message");
}

canonical_decompiled_code:
int func7()
{
puts("Commands are:");
puts(" \"quit\" - quit this program");
puts(" \"status\" - print the current system status");
puts(" \"log\" - print the message log");
puts(" \"verbose [on|off]\" - switch between verbose and quite mode");
puts(" \"start service [FTP | HTTP | HTTPS] <port>\" ");
puts(" \"stop service [FTP | HTTP | HTTPS] <port>\" ");
puts(" \"add node <ipaddress>\" ");
puts(" \"remove node <ipaddress>\" ");
puts(" \"set algorithm [DYNAMICRATIO | LEASTCONNECTIONS | RATIO | ROUNDROBIN]\" ");
puts(" \"reed <file>\" - read commands from the specified file");
return puts(" \"help\" or \"?\" - print this help message");
}

original_code:
int
CmdHelp(int cargc, char *cargv[])
{
  printf("Commands are:\n");
  printf("  \"quit\" - quit this program\n");
  printf("  \"status\" - print the current system status\n");
  printf("  \"log\" - print the message log\n");
  printf("  \"verbose [on|off]\" - switch between verbose and quite mode\n");
  printf("  \"start service [FTP | HTTP | HTTPS] <port>\" \n");
  printf("  \"stop service [FTP | HTTP | HTTPS] <port>\" \n");
  printf("  \"add node <ipaddress>\" \n");
  printf("  \"remove node <ipaddress>\" \n");
  printf("  \"set algorithm [DYNAMICRATIO | LEASTCONNECTIONS | RATIO | ROUNDROBIN]\" \n");
  printf("  \"reed <file>\" - read commands from the specified file\n");
  printf("  \"help\" or \"?\" - print this help message\n");
  return;
}


CmdUnknown

decompiled_code:
int __fastcall CmdUnknown(__int64 a1, const char **a2)
{
return printf("UNKNOWN COMMAND '%s'!\n", *a2);
}

canonical_decompiled_code:
int __fastcall func8(__int64 a1, const char **a2)
{
return printf("UNKNOWN COMMAND '%s'!\n", *a2);
}

original_code:
int
CmdUnknown(int cargc, char *cargv[])
{
  printf("UNKNOWN COMMAND '%s'!\n", cargv[0]);
}


decompiled_code:
void __noreturn prompt_user()
{
FILE *v0; // rdx
__int64 v1; // rbx
unsigned int v2; // ebp
char *v3; // rax
const char *v4; // rbx
char *s1[32]; // [rsp+0h] [rbp-318h] BYREF
char s[536]; // [rsp+100h] [rbp-218h] BYREF

puts("Welcome to Arbitrator v0.0");
while ( 1 )
{
v0 = ReadFile;
if ( !ReadFile )
{
printf("Arbitrator > ");
v0 = ReadFile;
if ( !ReadFile )
v0 = stdin;
}
if ( !fgets(s, 512, v0) && ReadFile )
{
ReadFile = 0LL;
s[0] = 0;
}
s1[0] = strtok(s, " \t\n");
if ( s1[0] )
{
v1 = 1LL;
do
{
v2 = v1;
v3 = strtok(0LL, " \t\n");
s1[v1++] = v3;
}
while ( v3 );
if ( v2 )
{
v4 = s1[0];
if ( !strcasecmp(s1[0], "quit") )
{
puts("Cleanup, exiting arbitrator...");
exit(0);
}
if ( !strcasecmp(v4, "status") )
{
CmdStatus();
}
else if ( !strcasecmp(v4, "log") )
{
CmdLog();
}
else if ( !strcasecmp(v4, "verbose") )
{
CmdVerbose(v2, (__int64)s1);
}
else if ( !strcasecmp(v4, "?") || !strcasecmp(v4, "help") )
{
CmdHelp();
}
else if ( !strcasecmp(v4, "start") )
{
CmdStartStop(v2, (const char **)s1, (__int64 (__fastcall *)(_QWORD, _QWORD))&start_service);
}
else if ( !strcasecmp(v4, "stop") )
{
CmdStartStop(v2, (const char **)s1, (__int64 (__fastcall *)(_QWORD, _QWORD))&stop_service);
}
else if ( !strcasecmp(v4, "add") )
{
CmdAddRemove(v2, (__int64)s1, (__int64 (__fastcall *)(_QWORD))&add_node);
}
else if ( !strcasecmp(v4, "remove") )
{
CmdAddRemove(v2, (__int64)s1, (__int64 (__fastcall *)(_QWORD))&remove_node);
}
else if ( !strcasecmp(v4, "set") )
{
CmdSet(v2, (__int64)s1);
}
else if ( !strcasecmp(v4, "read") )
{
CmdRead(v2, (__int64)s1);
}
else
{
CmdUnknown(v2, (const char **)s1);
}
}
}
}
}

canonical_decompiled_code:
void __noreturn func9()
{
FILE *v0; // rdx
__int64 v1; // rbx
unsigned int v2; // ebp
char *v3; // rax
const char *v4; // rbx
char *s1[32]; // [rsp+0h] [rbp-318h] BYREF
char s[536]; // [rsp+100h] [rbp-218h] BYREF

puts("Welcome to Arbitrator v0.0");
while ( 1 )
{
v0 = ReadFile;
if ( !ReadFile )
{
printf("Arbitrator > ");
v0 = ReadFile;
if ( !ReadFile )
v0 = stdin;
}
if ( !fgets(s, 512, v0) && ReadFile )
{
ReadFile = 0LL;
s[0] = 0;
}
s1[0] = strtok(s, " \t\n");
if ( s1[0] )
{
v1 = 1LL;
do
{
v2 = v1;
v3 = strtok(0LL, " \t\n");
s1[v1++] = v3;
}
while ( v3 );
if ( v2 )
{
v4 = s1[0];
if ( !strcasecmp(s1[0], "quit") )
{
puts("Cleanup, exiting arbitrator...");
exit(0);
}
if ( !strcasecmp(v4, "status") )
{
func0();
}
else if ( !strcasecmp(v4, "log") )
{
func1();
}
else if ( !strcasecmp(v4, "verbose") )
{
func2(v2, (__int64)s1);
}
else if ( !strcasecmp(v4, "?") || !strcasecmp(v4, "help") )
{
func7();
}
else if ( !strcasecmp(v4, "start") )
{
func3(v2, (const char **)s1, (__int64 (__fastcall *)(_QWORD, _QWORD))&start_service);
}
else if ( !strcasecmp(v4, "stop") )
{
func3(v2, (const char **)s1, (__int64 (__fastcall *)(_QWORD, _QWORD))&stop_service);
}
else if ( !strcasecmp(v4, "add") )
{
func4(v2, (__int64)s1, (__int64 (__fastcall *)(_QWORD))&add_node);
}
else if ( !strcasecmp(v4, "remove") )
{
func4(v2, (__int64)s1, (__int64 (__fastcall *)(_QWORD))&remove_node);
}
else if ( !strcasecmp(v4, "set") )
{
func5(v2, (__int64)s1);
}
else if ( !strcasecmp(v4, "read") )
{
func6(v2, (__int64)s1);
}
else
{
func8(v2, (const char **)s1);
}
}
}
}
}

original_code:
void *prompt_user(void *input)
{
  char Cmdline[512];
  int cargc;
  char *cargv[32];
  printf("Welcome to Arbitrator v0.0\n");
  while (1) {
    do {
      if (ReadFile == ((void *)0)) printf("Arbitrator > ");
      if ((fgets(Cmdline, 512, (ReadFile == ((void *)0) ? stdin: ReadFile)) == ((void *)0)) && (ReadFile != ((void *)0))) {ReadFile = ((void *)0); Cmdline[0] = '\0';};
      cargc = 0;
      cargv[cargc] = strtok(Cmdline, " \t\n");
      while (cargv[cargc] != ((void *)0)) {
        cargv[++cargc] = strtok(((void *)0), " \t\n");
      }
    } while (cargc == 0);
    if (!strcasecmp(cargv[0], "quit")) break;
    else if (!strcasecmp(cargv[0], "status")) CmdStatus(cargc, cargv);
    else if (!strcasecmp(cargv[0], "log")) CmdLog(cargc, cargv);
    else if (!strcasecmp(cargv[0], "verbose")) CmdVerbose(cargc, cargv);
    else if (!strcasecmp(cargv[0], "?")) CmdHelp(cargc, cargv);
    else if (!strcasecmp(cargv[0], "help")) CmdHelp(cargc, cargv);
    else if (!strcasecmp(cargv[0], "start")) CmdStartStop(cargc, cargv, start_service);
    else if (!strcasecmp(cargv[0], "stop")) CmdStartStop(cargc, cargv, stop_service);
    else if (!strcasecmp(cargv[0], "add")) CmdAddRemove(cargc, cargv, add_node);
    else if (!strcasecmp(cargv[0], "remove")) CmdAddRemove(cargc, cargv, remove_node);
    else if (!strcasecmp(cargv[0], "set")) CmdSet(cargc, cargv);
    else if (!strcasecmp(cargv[0], "read")) CmdRead(cargc, cargv);
    else CmdUnknown(cargc, cargv);
  }
  printf("Cleanup, exiting arbitrator...\n");
  exit(0);
}

variable_types:
input
void *

variable_types:
Cmdline
char [512]

variable_types:
cargv
char *[32]

return_type: void *


Accept

decompiled_code:
__int64 __fastcall Accept(int fd, struct sockaddr *addr, socklen_t *addr_len)
{
int v4; // ebx

while ( 1 )
{
v4 = accept(fd, addr, addr_len);
if ( v4 >= 0 )
break;
if ( (*_errno_location() & 0xFFFFFFDF) != 71 )
{
perror("accept error");
return (unsigned int)v4;
}
}
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd, struct sockaddr *addr, socklen_t *addr_len)
{
int v4; // ebx

while ( 1 )
{
v4 = accept(fd, addr, addr_len);
if ( v4 >= 0 )
break;
if ( (*_errno_location() & 0xFFFFFFDF) != 71 )
{
perror("accept error");
return (unsigned int)v4;
}
}
return (unsigned int)v4;
}

original_code:
int
Accept(int fd, struct sockaddr *sa, socklen_t *salenptr)
{
    int n;
again:
    if ( (n = accept(fd, sa, salenptr)) < 0) {
        if ((*__errno_location ()) == 71 || (*__errno_location ()) == 103)
            goto again;
        else
            perror("accept error");
    }
    return n;
}

canonical_original_code:
int Accept(int fd, struct sockaddr *sa, unsigned int *salenptr)
{
    int n;
again:
    if ( (n = accept(fd, sa, salenptr)) < 0) {
        if ((*__errno_location ()) == 71 || (*__errno_location ()) == 103)
            goto again;
        else
            perror("accept error");
    }
    return n;
}

variable_types: fd int

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

variable_types:
salenptr
unsigned int *


Bind

decompiled_code:
void __fastcall Bind(int a1, const struct sockaddr *a2, socklen_t a3)
{
if ( bind(a1, a2, a3) < 0 )
perror("bind error");
}

canonical_decompiled_code:
void __fastcall func1(int a1, const struct sockaddr *a2, socklen_t a3)
{
if ( bind(a1, a2, a3) < 0 )
perror("bind error");
}

original_code:
void
Bind(int fd, struct sockaddr *sa, socklen_t salen)
{
    if (bind(fd, sa, salen) < 0)
        perror("bind error");
}

canonical_original_code:
void Bind(int fd, struct sockaddr *sa, unsigned int salen)
{
    if (bind(fd, sa, salen) < 0)
        perror("bind error");
}

variable_types:
salen
unsigned int

return_type: void

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


Connect

decompiled_code:
void __fastcall Connect(int a1, const struct sockaddr *a2, socklen_t a3)
{
if ( connect(a1, a2, a3) < 0 )
perror("connect error");
}

canonical_decompiled_code:
void __fastcall func2(int a1, const struct sockaddr *a2, socklen_t a3)
{
if ( connect(a1, a2, a3) < 0 )
perror("connect error");
}

original_code:
void
Connect(int fd, struct sockaddr *sa, socklen_t salen)
{
    if (connect(fd, sa, salen) < 0)
        perror("connect error");
}

canonical_original_code:
void Connect(int fd, struct sockaddr *sa, unsigned int salen)
{
    if (connect(fd, sa, salen) < 0)
        perror("connect error");
}


decompiled_code:
void __fastcall Inet_pton(int a1, const char *a2, void *a3)
{
if ( inet_pton(a1, a2, a3) <= 0 )
perror("inet_pton error");
}

canonical_decompiled_code:
void __fastcall func3(int a1, const char *a2, void *a3)
{
if ( inet_pton(a1, a2, a3) <= 0 )
perror("inet_pton error");
}

original_code:
void
Inet_pton(int family, const char *strptr, void *addrptr)
{
    int n;
    if ( (n = inet_pton(family, strptr, addrptr)) < 1)
        perror("inet_pton error");
}

canonical_original_code:
void Inet_pton(int family, char *strptr, void *addrptr)
{
    int n;
    if ( (n = inet_pton(family, strptr, addrptr)) < 1)
        perror("inet_pton error");
}

variable_types: family int

variable_types:
strptr
char *

variable_types:
addrptr
void *

variable_types: n int


Listen

decompiled_code:
void __fastcall Listen(int fd, int n)
{
signed int v3; // eax

v3 = (unsigned int)getenv("LISTENQ");
if ( v3 )
n = atoi((const char *)v3);
if ( listen(fd, n) < 0 )
perror("listen error");
}

canonical_decompiled_code:
void __fastcall func4(int fd, int n)
{
signed int v3; // eax

v3 = (unsigned int)getenv("LISTENQ");
if ( v3 )
n = atoi((const char *)v3);
if ( listen(fd, n) < 0 )
perror("listen error");
}

original_code:
void
Listen(int fd, int backlog)
{
    char *ptr;
    if ( (ptr = getenv("LISTENQ")) != ((void *)0))
        backlog = atoi(ptr);
    if (listen(fd, backlog) < 0)
        perror("listen error");
}

variable_types: backlog int

variable_types:
ptr
char *


Socket

decompiled_code:
__int64 __fastcall Socket(int a1, int a2, int a3)
{
int v3; // ebx

v3 = socket(a1, a2, a3);
if ( v3 < 0 )
perror("socket error");
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, int a2, int a3)
{
int v3; // ebx

v3 = socket(a1, a2, a3);
if ( v3 < 0 )
perror("socket error");
return (unsigned int)v3;
}

original_code:
int
Socket(int family, int type, int protocol)
{
    int n;
    if ( (n = socket(family, type, protocol)) < 0)
        perror("socket error");
    return n;
}

variable_types: type int

variable_types: protocol int


decompiled_code:
int algorithm_ratio()
{
return puts("IMPLEMENT: ratio.");
}

canonical_decompiled_code:
int func0()
{
return puts("IMPLEMENT: ratio.");
}

original_code:
node_t *
algorithm_ratio(const char *source_ip)
{
    printf("IMPLEMENT: ratio.\n");
}

canonical_original_code:
struct Node *algorithm_ratio(char *source_ip)
{
    printf("IMPLEMENT: ratio.\n");
}


decompiled_code:
int init_ratio()
{
return puts("IMPLEMENT: init ratio.");
}

canonical_decompiled_code:
int func1()
{
return puts("IMPLEMENT: init ratio.");
}

original_code:
int
init_ratio()
{
    printf("IMPLEMENT: init ratio.\n");
}


decompiled_code:
__int64 init_prompt()
{
return prompt_user(0LL);
}

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

original_code:
void
init_prompt()
{
    prompt_user(((void *)0));
}


decompiled_code:
__int64 init_services()
{
services[4] = &services[4];
services[5] = &services[4];
set_algorithm(3LL);
start_service(1LL, 9877LL);
return start_service(1LL, 9878LL);
}

canonical_decompiled_code:
__int64 func1()
{
services[4] = &services[4];
services[5] = &services[4];
set_algorithm(3LL);
start_service(1LL, 9877LL);
return start_service(1LL, 9878LL);
}

original_code:
void
init_services()
{
    do { (&services.list)->next = (&services.list); (&services.list)->prev = (&services.list); } while (0);
    set_algorithm(ROUNDROBIN);
    start_service(HTTP, 9877);
    start_service(HTTP, 9878);
}


decompiled_code:
__int64 cleanup_services()
{
return stop_service(1LL, 80LL);
}

canonical_decompiled_code:
__int64 func2()
{
return stop_service(1LL, 80LL);
}

original_code:
void
cleanup_services()
{
    stop_service(HTTP, 80);
}


original_code:
void __attribute__((constructor))
init_nodes()
{
    add_node("192.168.1.3");
}


decompiled_code:
struct _IO_FILE *init_logging()
{
struct _IO_FILE *result; // rax

result = stdout;
stream = (__int64)stdout;
logging_level = 1;
return result;
}

canonical_decompiled_code:
struct _IO_FILE *func4()
{
struct _IO_FILE *result; // rax

result = stdout;
stream = (__int64)stdout;
logging_level = 1;
return result;
}

original_code:
void __attribute__((constructor))
init_logging()
{
    stream = stdout;
    logging_level = INFO;
}

canonical_original_code:
void init_logging()
{
    stream = stdout;
    logging_level = INFO;
}


decompiled_code:
__int64 algorithm_roundrobin()
{
pthread_mutex_lock(&currentnode_lock);
if ( !*(_QWORD *)(currentnode + 8) )
currentnode = node_head;
pthread_mutex_unlock(&currentnode_lock);
return currentnode;
}

canonical_decompiled_code:
__int64 func0()
{
pthread_mutex_lock(&currentnode_lock);
if ( !*(_QWORD *)(currentnode + 8) )
currentnode = node_head;
pthread_mutex_unlock(&currentnode_lock);
return currentnode;
}

original_code:
node_t *
algorithm_roundrobin(const char *source_ip)
{
    pthread_mutex_lock(&currentnode_lock);
    if (currentnode->next == ((void *)0)) {
        currentnode = node_head;
    }
    pthread_mutex_unlock(&currentnode_lock);
    return currentnode;
}

canonical_original_code:
struct Node *algorithm_roundrobin(char *source_ip)
{
    pthread_mutex_lock(&currentnode_lock);
    if (currentnode->next == ((void *)0)) {
        currentnode = node_head;
    }
    pthread_mutex_unlock(&currentnode_lock);
    return currentnode;
}


decompiled_code:
int init_roundrobin()
{
int result; // eax

puts("Setup RoundRobin");
pthread_mutex_init(&currentnode_lock, 0LL);
result = node_head;
currentnode = node_head;
if ( !node_head )
result = puts("No nodes...");
return result;
}

canonical_decompiled_code:
int func1()
{
int result; // eax

puts("Setup RoundRobin");
pthread_mutex_init(&currentnode_lock, 0LL);
result = node_head;
currentnode = node_head;
if ( !node_head )
result = puts("No nodes...");
return result;
}

original_code:
int
init_roundrobin()
{
    printf("Setup RoundRobin\n");
    pthread_mutex_init(&currentnode_lock, ((void *)0));
    currentnode = node_head;
    if (currentnode == ((void *)0)) {
        printf("No nodes...\n");
    }
}


decompiled_code:
__int64 __fastcall hash_function(const char *a1, unsigned int a2)
{
size_t v2; // rax
const char *v3; // rdx
char *v4; // rdi
unsigned int v5; // eax

v2 = strlen(a1);
v3 = a1;
v4 = (char *)&a1[v2];
v5 = 0;
while ( v3 != v4 )
v5 += *v3++;
return v5 % a2;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, unsigned int a2)
{
size_t v2; // rax
const char *v3; // rdx
char *v4; // rdi
unsigned int v5; // eax

v2 = strlen(a1);
v3 = a1;
v4 = (char *)&a1[v2];
v5 = 0;
while ( v3 != v4 )
v5 += *v3++;
return v5 % a2;
}

original_code:
unsigned int
hash_function(char *key, unsigned int maxsize)
{
    int sum, i;
    sum = 0;
    for (i = 0; i < strlen(key); i++) {
        sum += key[i];
    }
    return (unsigned int)sum % maxsize;
}

canonical_original_code:
unsigned int hash_function(char *key, unsigned int maxsize)
{
    int sum; int i;
    sum = 0;
    for (i = 0; i < strlen(key); i++) {
        sum += key[i];
    }
    return (unsigned int)sum % maxsize;
}

variable_types:
key
char *

variable_types:
maxsize
unsigned int

variable_types: sum int

variable_types: i int

return_type: unsigned int


decompiled_code:
__int64 __fastcall add_entry(unsigned int *a1, const char *a2, __int64 a3)
{
_QWORD *v4; // rbx
size_t v5; // rax
void *v6; // r12
size_t v7; // rax
unsigned int *v8; // rdx

v4 = malloc(0x18uLL);
v4[1] = a3;
v5 = strlen(a2);
v6 = malloc(v5);
*v4 = v6;
v7 = strlen(a2);
memcpy(v6, a2, v7);
v8 = &a1[2 * (unsigned int)hash_function((const char *)v6, *a1) + 2];
v4[2] = v8;
*(_QWORD *)v8 = v4;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int *a1, const char *a2, __int64 a3)
{
_QWORD *v4; // rbx
size_t v5; // rax
void *v6; // r12
size_t v7; // rax
unsigned int *v8; // rdx

v4 = malloc(0x18uLL);
v4[1] = a3;
v5 = strlen(a2);
v6 = malloc(v5);
*v4 = v6;
v7 = strlen(a2);
memcpy(v6, a2, v7);
v8 = &a1[2 * (unsigned int)func1((const char *)v6, *a1) + 2];
v4[2] = v8;
*(_QWORD *)v8 = v4;
return 0LL;
}

original_code:
int
add_entry(hashtable_t *table, char *key, void *value)
{
    unsigned int hash_index;
    hashentry_t *new_entry = (hashentry_t *)malloc(sizeof(hashentry_t));
    new_entry->value = value;
    new_entry->key = malloc(sizeof(char) * strlen(key));
    memcpy(new_entry->key, key, strlen(key));
    hash_index = hash_function(new_entry->key, table->table_size);
    new_entry->next = &table->entries[hash_index];
    table->entries[hash_index] = new_entry;
    return 0;
}

canonical_original_code:
int add_entry(struct hashtable *table, char *key, void *value)
{
    unsigned int hash_index;
    struct hashentry *new_entry = (hashentry_t *)malloc(sizeof(hashentry_t));
    new_entry->value = value;
    new_entry->key = malloc(sizeof(char) * strlen(key));
    memcpy(new_entry->key, key, strlen(key));
    hash_index = hash_function(new_entry->key, table->table_size);
    new_entry->next = &table->entries[hash_index];
    table->entries[hash_index] = new_entry;
    return 0;
}

variable_types: struct hashtable { unsigned int table_size; struct hashentry { char *key; void *value; struct hashentry *next; } *entries[]; } *

variable_types:
value
void *


decompiled_code:
__int64 __fastcall remove_entry(unsigned int *a1, const char *a2)
{
int v3; // eax
_BYTE **v4; // rdi
__int64 result; // rax
_BYTE **v6; // rcx

v3 = hash_function(a2, *a1);
v4 = *(_BYTE ***)&a1[2 * v3 + 2];
if ( !v4 )
return 0xFFFFFFFFLL;
if ( **v4 == *a2 )
{
*(_QWORD *)&a1[2 * v3 + 2] = v4[2];
free(v4);
result = 0LL;
}
else
{
while ( 1 )
{
v6 = v4;
v4 = (_BYTE **)v4[2];
if ( !v4 )
break;
if ( *a2 == **v4 )
{
v6[2] = v4[2];
return 0xFFFFFFFFLL;
}
}
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int *a1, const char *a2)
{
int v3; // eax
_BYTE **v4; // rdi
__int64 result; // rax
_BYTE **v6; // rcx

v3 = func1(a2, *a1);
v4 = *(_BYTE ***)&a1[2 * v3 + 2];
if ( !v4 )
return 0xFFFFFFFFLL;
if ( **v4 == *a2 )
{
*(_QWORD *)&a1[2 * v3 + 2] = v4[2];
free(v4);
result = 0LL;
}
else
{
while ( 1 )
{
v6 = v4;
v4 = (_BYTE **)v4[2];
if ( !v4 )
break;
if ( *a2 == **v4 )
{
v6[2] = v4[2];
return 0xFFFFFFFFLL;
}
}
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int
remove_entry(hashtable_t *table, void *key)
{
    unsigned int hash_index;
    struct hashentry *hash_entry, *temp_entry;
    hash_index = hash_function(key, table->table_size);
    if ( (hash_entry = table->entries[hash_index]) == ((void *)0)) {
        return -1;
    }
    else if (!memcmp(hash_entry->key, key, sizeof(*key))) {
        table->entries[hash_index] = table->entries[hash_index]->next;
    }
    else {
        while (hash_entry->next != ((void *)0)) {
            if (!memcmp(hash_entry->next->key, key, sizeof(*key))) {
                temp_entry = hash_entry->next;
                hash_entry->next = hash_entry->next->next;
                hash_entry = temp_entry;
                break;
            }
            hash_entry = hash_entry->next;
        }
        return -1;
    }
    free(hash_entry);
    return 0;
}

canonical_original_code:
int remove_entry(struct hashtable *table, void *key)
{
    unsigned int hash_index;
    struct hashentry *hash_entry; struct hashentry *temp_entry;
    hash_index = hash_function(key, table->table_size);
    if ( (hash_entry = table->entries[hash_index]) == ((void *)0)) {
        return -1;
    }
    else if (!memcmp(hash_entry->key, key, sizeof(*key))) {
        table->entries[hash_index] = table->entries[hash_index]->next;
    }
    else {
        while (hash_entry->next != ((void *)0)) {
            if (!memcmp(hash_entry->next->key, key, sizeof(*key))) {
                temp_entry = hash_entry->next;
                hash_entry->next = hash_entry->next->next;
                hash_entry = temp_entry;
                break;
            }
            hash_entry = hash_entry->next;
        }
        return -1;
    }
    free(hash_entry);
    return 0;
}

variable_types:
key
void *

variable_types: struct hashentry { char *key; void *value; struct hashentry *next; } *


decompiled_code:
void __fastcall delete_table(unsigned int **a1)
{
unsigned int *v2; // rdi
unsigned int v3; // ebx
const char **v4; // rbp

v2 = *a1;
if ( *v2 )
{
v3 = 0;
do
{
v4 = *(const char ***)&v2[2 * v3 + 2];
if ( v4 )
{
printf("REMOVING KEY %s\n", *v4);
remove_entry(*a1, *v4);
}
++v3;
v2 = *a1;
}
while ( **a1 > v3 );
}
free(v2);
*a1 = 0LL;
}

canonical_decompiled_code:
void __fastcall func4(unsigned int **a1)
{
unsigned int *v2; // rdi
unsigned int v3; // ebx
const char **v4; // rbp

v2 = *a1;
if ( *v2 )
{
v3 = 0;
do
{
v4 = *(const char ***)&v2[2 * v3 + 2];
if ( v4 )
{
printf("REMOVING KEY %s\n", *v4);
func3(*a1, *v4);
}
++v3;
v2 = *a1;
}
while ( **a1 > v3 );
}
free(v2);
*a1 = 0LL;
}

original_code:
void
delete_table(hashtable_t **table)
{
    int i;
    struct hashentry *hash_entry;
    for (i = 0; i < (*table)->table_size; i++) {
        if ( (hash_entry = (*table)->entries[i]) != ((void *)0)) {
            printf("REMOVING KEY %s\n", hash_entry->key);
            remove_entry(*table, hash_entry->key);
        }
    }
    free(*table);
    *table = ((void *)0);
}

canonical_original_code:
void delete_table(struct hashtable **table)
{
    int i;
    struct hashentry *hash_entry;
    for (i = 0; i < (*table)->table_size; i++) {
        if ( (hash_entry = (*table)->entries[i]) != ((void *)0)) {
            printf("REMOVING KEY %s\n", hash_entry->key);
            remove_entry(*table, hash_entry->key);
        }
    }
    free(*table);
    *table = ((void *)0);
}

variable_types: struct hashtable { unsigned int table_size; struct hashentry { char *key; void *value; struct hashentry *next; } *entries[]; } **


decompiled_code:
__int64 __fastcall get_entry(unsigned int *a1, const char *a2)
{
__int64 result; // rax

result = *(_QWORD *)&a1[2 * (unsigned int)hash_function(a2, *a1) + 2];
if ( result )
{
while ( **(_BYTE **)result != *a2 )
{
result = *(_QWORD *)(result + 16);
if ( !result )
return result;
}
result = *(_QWORD *)(result + 8);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int *a1, const char *a2)
{
__int64 result; // rax

result = *(_QWORD *)&a1[2 * (unsigned int)func1(a2, *a1) + 2];
if ( result )
{
while ( **(_BYTE **)result != *a2 )
{
result = *(_QWORD *)(result + 16);
if ( !result )
return result;
}
result = *(_QWORD *)(result + 8);
}
return result;
}

original_code:
void *
get_entry(hashtable_t *table, char *key)
{
    unsigned int hash_index;
    struct hashentry *hash_entry;
    hash_index = hash_function(key, table->table_size);
    hash_entry = table->entries[hash_index];
    while (hash_entry != ((void *)0)) {
        if (!memcmp(hash_entry->key, key, sizeof(*key))) {
            return hash_entry->value;
        }
        hash_entry = hash_entry->next;
    }
    return ((void *)0);
}

canonical_original_code:
void *get_entry(struct hashtable *table, char *key)
{
    unsigned int hash_index;
    struct hashentry *hash_entry;
    hash_index = hash_function(key, table->table_size);
    hash_entry = table->entries[hash_index];
    while (hash_entry != ((void *)0)) {
        if (!memcmp(hash_entry->key, key, sizeof(*key))) {
            return hash_entry->value;
        }
        hash_entry = hash_entry->next;
    }
    return ((void *)0);
}

user_defined_types: struct hashentry { char *key; void *value; struct hashentry *next; }


decompiled_code:
int __fastcall add_node(__int64 a1)
{
struct in_addr *v1; // rbx
unsigned int v2; // eax

v1 = (struct in_addr *)malloc(0x10uLL);
Inet_pton(2LL, a1, v1);
*(_QWORD *)&v1[2].s_addr = node_head;
node_head = (__int64)v1;
v2 = (unsigned int)inet_ntoa((struct in_addr)v1->s_addr);
return printf("Added node: %s\n", (const char *)v2);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
struct in_addr *v1; // rbx
unsigned int v2; // eax

v1 = (struct in_addr *)malloc(0x10uLL);
Inet_pton(2LL, a1, v1);
*(_QWORD *)&v1[2].s_addr = node_head;
node_head = (__int64)v1;
v2 = (unsigned int)inet_ntoa((struct in_addr)v1->s_addr);
return printf("Added node: %s\n", (const char *)v2);
}

original_code:
int
add_node(char *ip_destination)
{
    node_t *new_node = malloc(sizeof(node_t));
    Inet_pton(2, ip_destination, &new_node->ip);
    new_node->next = node_head;
    node_head = new_node;
    printf("Added node: %s\n", inet_ntoa(node_head->ip));
}

canonical_original_code:
int add_node(char *ip_destination)
{
    struct Node *new_node = malloc(sizeof(node_t));
    Inet_pton(2, ip_destination, &new_node->ip);
    new_node->next = node_head;
    node_head = new_node;
    printf("Added node: %s\n", inet_ntoa(node_head->ip));
}

variable_types: struct Node { int ip; unsigned int rtt; struct Node *next; } *


repo: dgomes/HomeAutomation

decompiled_code:
__int64 __fastcall make_socket(unsigned __int16 a1)
{
int v1; // eax
unsigned int v2; // ebx
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v1 = socket(2, 1, 0);
if ( v1 < 0 )
{
fprintf(stderr, "%s: Can't create socket\n", "remote.c");
exit(1);
}
v2 = v1;
optval = 1;
if ( setsockopt(v1, 1, 2, &optval, 4u) < 0 )
perror("socket reuseaddr");
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16(a1);
*(_DWORD *)&addr.sa_data[2] = _bswap_32(0);
if ( bind(v2, &addr, 0x10u) < 0 )
{
fprintf(stderr, "%s: Can't bind socket\n", "remote.c");
exit(1);
}
fprintf(stderr, "listennig on port %d\n", a1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int16 a1)
{
int v1; // eax
unsigned int v2; // ebx
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v1 = socket(2, 1, 0);
if ( v1 < 0 )
{
fprintf(stderr, "%s: Can't create socket\n", "remote.c");
exit(1);
}
v2 = v1;
optval = 1;
if ( setsockopt(v1, 1, 2, &optval, 4u) < 0 )
perror("socket reuseaddr");
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func0(a1);
*(_DWORD *)&addr.sa_data[2] = func1(0);
if ( bind(v2, &addr, 0x10u) < 0 )
{
fprintf(stderr, "%s: Can't bind socket\n", "remote.c");
exit(1);
}
fprintf(stderr, "listennig on port %d\n", a1);
return v2;
}

original_code:
int make_socket (uint16_t port) {
 int sock;
 struct sockaddr_in name;
 sock = socket (2, SOCK_STREAM, 0);
 if (sock < 0)
 {
  fprintf(stderr, "%s: Can't create socket\n", "remote.c");
  exit (1);
 }
 int on = 1;
 int ret = setsockopt( sock, 1, 2, &on, sizeof(on) );
 if (ret < 0)
  perror("socket reuseaddr");
 name.sin_family = 2;
 name.sin_port = htons (port);
 name.sin_addr.s_addr = htonl (((in_addr_t) 0x00000000));
 if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0)
 {
  fprintf(stderr, "%s: Can't bind socket\n", "remote.c");
  exit (1);
 }
 fprintf(stderr, "listennig on port %d\n",port);
 return sock;
}

canonical_original_code:
int make_socket(unsigned short port) {
 int sock;
 struct sockaddr_in name;
 sock = socket (2, SOCK_STREAM, 0);
 if (sock < 0)
 {
  fprintf(stderr, "%s: Can't create socket\n", "remote.c");
  exit (1);
 }
 int on = 1;
 int ret = setsockopt( sock, 1, 2, &on, sizeof(on) );
 if (ret < 0)
  perror("socket reuseaddr");
 name.sin_family = 2;
 name.sin_port = htons (port);
 name.sin_addr.s_addr = htonl (((unsigned int) 0x00000000));
 if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0)
 {
  fprintf(stderr, "%s: Can't bind socket\n", "remote.c");
  exit (1);
 }
 fprintf(stderr, "listennig on port %d\n",port);
 return sock;
}

variable_types:
port
unsigned short

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: on int

variable_types: ret int

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


acceptRemote

decompiled_code:
__int64 __fastcall acceptRemote(int a1)
{
int v1; // eax
unsigned int v2; // ebp
unsigned __int16 v3; // bx
unsigned int v4; // eax
socklen_t addr_len[2]; // [rsp+8h] [rbp-30h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

*(_QWORD *)addr_len = 16LL;
v1 = accept(a1, &addr, addr_len);
if ( v1 < 0 )
{
perror("accept");
exit(1);
}
v2 = v1;
v3 = _bswap_16(*(__int16 *)addr.sa_data);
v4 = (unsigned int)inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
fprintf(stderr, "Server: connect from host %s, port %d.\n", (const char *)v4, v3);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
int v1; // eax
unsigned int v2; // ebp
unsigned __int16 v3; // bx
unsigned int v4; // eax
socklen_t addr_len[2]; // [rsp+8h] [rbp-30h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

*(_QWORD *)addr_len = 16LL;
v1 = accept(a1, &addr, addr_len);
if ( v1 < 0 )
{
perror("accept");
exit(1);
}
v2 = v1;
v3 = func0(*(__int16 *)addr.sa_data);
v4 = (unsigned int)inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
fprintf(stderr, "Server: connect from host %s, port %d.\n", (const char *)v4, v3);
return v2;
}

original_code:
int acceptRemote(int fd) {
 int new;
 struct sockaddr_in clientname;
 size_t size = sizeof (clientname);
 new = accept (fd, (struct sockaddr *) &clientname, &size);
 if (new < 0) {
  perror ("accept");
  exit (1);
 }
 fprintf (stderr, "Server: connect from host %s, port %d.\n", inet_ntoa (clientname.sin_addr), ntohs (clientname.sin_port));
 return new;
}

canonical_original_code:
int acceptRemote(int fd) {
 int new;
 struct sockaddr_in clientname;
 unsigned long size = sizeof (clientname);
 new = accept (fd, (struct sockaddr *) &clientname, &size);
 if (new < 0) {
  perror ("accept");
  exit (1);
 }
 fprintf (stderr, "Server: connect from host %s, port %d.\n", inet_ntoa (clientname.sin_addr), ntohs (clientname.sin_port));
 return new;
}

variable_types: fd int

variable_types: new int

variable_types:
size
unsigned long


repo: dgomezferro/libpaxos

decompiled_code:
int acc_deliver_callback()
{
return printf("Warning:%s invoked when ACCEPTOR_UPDATE_ON_DELIVER is undefined!\n", "acc_deliver_callback");
}

canonical_decompiled_code:
int func1()
{
return printf("Warning:%s invoked when ACCEPTOR_UPDATE_ON_DELIVER is undefined!\n", "acc_deliver_callback");
}

original_code:
void
acc_deliver_callback(char * value, size_t size, iid_t iid, ballot_t ballot, int proposer) {
    (proposer = proposer);;
    (value = value);;
    (size = size);;
    (iid = iid);;
    (ballot = ballot);;
    printf("Warning:%s invoked when ACCEPTOR_UPDATE_ON_DELIVER is undefined!\n", __func__);
}

canonical_original_code:
void acc_deliver_callback(char *value, unsigned long size, unsigned int iid, unsigned int ballot, int proposer) {
    (proposer = proposer);;
    (value = value);;
    (size = size);;
    (iid = iid);;
    (ballot = ballot);;
    printf("Warning:%s invoked when ACCEPTOR_UPDATE_ON_DELIVER is undefined!\n", __func__);
}

variable_types:
value
char *

variable_types:
iid
unsigned int

variable_types:
ballot
unsigned int

variable_types: proposer int


decompiled_code:
__int64 init_acc_network()
{
unsigned int *v0; // rax
__int64 result; // rax

to_proposers = udp_sendbuf_new(1LL, "10.78.36.211", 6003LL);
if ( to_proposers )
{
to_learners = udp_sendbuf_new(5LL, "10.78.36.211", 6001LL);
if ( to_learners )
{
v0 = (unsigned int *)udp_receiver_new(6002LL);
for_acceptor = (__int64)v0;
if ( v0 )
{
event_set(&acceptor_msg_event, *v0, 18LL, acc_handle_newmsg, 0LL);
event_add(&acceptor_msg_event, 0LL);
result = 0LL;
}
else
{
puts("Error creating acceptor network receiver");
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Error creating acceptor->learners network sender");
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Error creating acceptor->proposers network sender");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
unsigned int *v0; // rax
__int64 result; // rax

to_proposers = udp_sendbuf_new(1LL, "10.78.36.211", 6003LL);
if ( to_proposers )
{
to_learners = udp_sendbuf_new(5LL, "10.78.36.211", 6001LL);
if ( to_learners )
{
v0 = (unsigned int *)udp_receiver_new(6002LL);
for_acceptor = (__int64)v0;
if ( v0 )
{
event_set(&acceptor_msg_event, *v0, 18LL, acc_handle_newmsg, 0LL);
event_add(&acceptor_msg_event, 0LL);
result = 0LL;
}
else
{
puts("Error creating acceptor network receiver");
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Error creating acceptor->learners network sender");
result = 0xFFFFFFFFLL;
}
}
else
{
puts("Error creating acceptor->proposers network sender");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int
init_acc_network() {
    to_proposers = udp_sendbuf_new(1, "10.78.36.211", 6003);
    if(to_proposers == ((void *)0)) {
        printf("Error creating acceptor->proposers network sender\n");
        return (-1);
    }
    to_learners = udp_sendbuf_new(5, "10.78.36.211", 6001, "10.78.36.212", 6001, "10.78.36.213", 6001, "10.78.36.211", 6007, "10.78.36.215", 6001);
    if(to_learners == ((void *)0)) {
        printf("Error creating acceptor->learners network sender\n");
        return (-1);
    }
    for_acceptor = udp_receiver_new(6002);
    if (for_acceptor == ((void *)0)) {
        printf("Error creating acceptor network receiver\n");
        return (-1);
    }
    event_set(&acceptor_msg_event, for_acceptor->sock, 0x02|0x10, acc_handle_newmsg, ((void *)0));
    event_add(&acceptor_msg_event, ((void *)0));
    return 0;
}


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

event_set(&repeat_accept_event, 0xFFFFFFFFLL, 0LL, acc_periodic_repeater, 0LL);
qword_688 = 0LL;
periodic_repeat_interval = 3LL;
result = event_add(&repeat_accept_event, &periodic_repeat_interval);
if ( (_DWORD)result )
{
puts("Error while adding first periodic repeater event");
result = 0xFFFFFFFFLL;
}
return result;
}

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

event_set(&repeat_accept_event, 0xFFFFFFFFLL, 0LL, acc_periodic_repeater, 0LL);
qword_688 = 0LL;
periodic_repeat_interval = 3LL;
result = event_add(&repeat_accept_event, &periodic_repeat_interval);
if ( (_DWORD)result )
{
puts("Error while adding first periodic repeater event");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int
init_acc_timers() {
    event_set((&repeat_accept_event), -1, 0, (acc_periodic_repeater), (((void *)0)));
 ((&periodic_repeat_interval)->tv_sec = (&periodic_repeat_interval)->tv_usec = 0);
 periodic_repeat_interval.tv_sec = 3;
    periodic_repeat_interval.tv_usec = 0;
 if(event_add(&repeat_accept_event, &periodic_repeat_interval) != 0) {
    printf("Error while adding first periodic repeater event\n");
       return -1;
 }
    return 0;
}


decompiled_code:
__int64 __fastcall acc_apply_prepare(__int64 a1, __int64 a2)
{
if ( !a2 )
return stablestorage_save_prepare();
if ( *(_DWORD *)(a2 + 4) >= *(_DWORD *)(a1 + 4) )
return 0LL;
if ( !*(_WORD *)(a2 + 12) )
return stablestorage_save_prepare();
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
if ( !a2 )
return stablestorage_save_prepare();
if ( *(_DWORD *)(a2 + 4) >= *(_DWORD *)(a1 + 4) )
return 0LL;
if ( !*(_WORD *)(a2 + 12) )
return stablestorage_save_prepare();
return 0LL;
}

original_code:
static acceptor_record *
acc_apply_prepare(prepare_req * pr, acceptor_record * rec) {
    if (rec != ((void *)0) && rec->ballot >= pr->ballot) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Prepare request for iid:%lu dropped (ballots curr:%u recv:%u)\n", pr->iid, rec->ballot, pr->ballot) ;};
        return ((void *)0);
    }
    if (rec != ((void *)0) && rec->is_final) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Prepare request for iid:%lu dropped             (stored value is final)\n", pr->iid) ;};
        return ((void *)0);
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Prepare request is valid for iid:%lu (ballot:%u)\n", pr->iid, pr->ballot) ;};
    rec = stablestorage_save_prepare(pr, rec);
    return rec;
}

canonical_original_code:
static struct accept_ack_t *acc_apply_prepare(struct prepare_req_t *pr, struct accept_ack_t *rec) {
    if (rec != ((void *)0) && rec->ballot >= pr->ballot) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Prepare request for iid:%lu dropped (ballots curr:%u recv:%u)\n", pr->iid, rec->ballot, pr->ballot) ;};
        return ((void *)0);
    }
    if (rec != ((void *)0) && rec->is_final) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Prepare request for iid:%lu dropped             (stored value is final)\n", pr->iid) ;};
        return ((void *)0);
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Prepare request is valid for iid:%lu (ballot:%u)\n", pr->iid, pr->ballot) ;};
    rec = stablestorage_save_prepare(pr, rec);
    return rec;
}

variable_types: struct prepare_req_t { unsigned int iid; unsigned int ballot; } *

variable_types:
rec
struct accept_ack_t *

return_type: struct accept_ack_t { unsigned int iid; unsigned int ballot; unsigned int value_ballot; short is_final; unsigned long value_size; char value[]; } *


decompiled_code:
__int64 __fastcall handle_prepare_req_batch(__int16 *a1)
{
__int16 v1; // bx
__int64 v2; // rax
__int64 v3; // rsi

sendbuf_clear(to_proposers, 2LL, (unsigned int)(__int16)this_acceptor_id);
stablestorage_tx_begin();
if ( *a1 > 0 )
{
v1 = 0;
do
{
if ( *(_DWORD *)&a1[4 * v1 + 2] == -1 )
{
v2 = (__int64)acc_apply_infinite_prepare((__int64)&a1[4 * v1 + 2]);
}
else
{
v3 = stablestorage_get_record();
v2 = acc_apply_prepare((__int64)&a1[4 * v1 + 2], v3);
}
if ( v2 )
sendbuf_add_prepare_ack(to_proposers, v2);
++v1;
}
while ( *a1 > v1 );
}
stablestorage_tx_end();
return sendbuf_flush(to_proposers);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int16 *a1)
{
__int16 v1; // bx
__int64 v2; // rax
__int64 v3; // rsi

sendbuf_clear(to_proposers, 2LL, (unsigned int)(__int16)this_acceptor_id);
stablestorage_tx_begin();
if ( *a1 > 0 )
{
v1 = 0;
do
{
if ( *(_DWORD *)&a1[4 * v1 + 2] == -1 )
{
v2 = (__int64)func0((__int64)&a1[4 * v1 + 2]);
}
else
{
v3 = stablestorage_get_record();
v2 = func4((__int64)&a1[4 * v1 + 2], v3);
}
if ( v2 )
sendbuf_add_prepare_ack(to_proposers, v2);
++v1;
}
while ( *a1 > v1 );
}
stablestorage_tx_end();
return sendbuf_flush(to_proposers);
}

original_code:
static void
handle_prepare_req_batch(prepare_req_batch* prb) {
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Handling prepare for %d instances\n", prb->count) ;};
    sendbuf_clear(to_proposers, prepare_acks, this_acceptor_id);
    stablestorage_tx_begin();
    short int i;
    acceptor_record * rec;
    prepare_req * pr;
    for(i = 0; i < prb->count; i++) {
        pr = &prb->prepares[i];
        if(pr->iid == ((iid_t) -1)) {
            rec = acc_apply_infinite_prepare(pr);
        } else {
            rec = stablestorage_get_record(pr->iid);
            rec = acc_apply_prepare(pr, rec);
        }
        if(rec != ((void *)0)) {
            sendbuf_add_prepare_ack(to_proposers, rec);
        }
    }
    stablestorage_tx_end();
    sendbuf_flush(to_proposers);
}

canonical_original_code:
static void handle_prepare_req_batch(struct prepare_req_batch_t *prb) {
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Handling prepare for %d instances\n", prb->count) ;};
    sendbuf_clear(to_proposers, prepare_acks, this_acceptor_id);
    stablestorage_tx_begin();
    short i;
    struct accept_ack_t *rec;
    struct prepare_req_t *pr;
    for(i = 0; i < prb->count; i++) {
        pr = &prb->prepares[i];
        if(pr->iid == ((iid_t) -1)) {
            rec = acc_apply_infinite_prepare(pr);
        } else {
            rec = stablestorage_get_record(pr->iid);
            rec = acc_apply_prepare(pr, rec);
        }
        if(rec != ((void *)0)) {
            sendbuf_add_prepare_ack(to_proposers, rec);
        }
    }
    stablestorage_tx_end();
    sendbuf_flush(to_proposers);
}

variable_types: struct prepare_req_batch_t { short count; short proposer_id; struct prepare_req_t { unsigned int iid; unsigned int ballot; } prepares[]; } *

variable_types: i short

variable_types: struct accept_ack_t { unsigned int iid; unsigned int ballot; unsigned int value_ballot; short is_final; unsigned long value_size; char value[]; } *

user_defined_types: struct prepare_req_batch_t { short count; short proposer_id; struct prepare_req_t prepares[]; }


decompiled_code:
__int64 __fastcall handle_repeat_req_batch(__int16 *a1)
{
__int16 v1; // bx
__int64 v2; // rax

sendbuf_clear(to_learners, 8LL, (unsigned int)(__int16)this_acceptor_id);
stablestorage_tx_begin();
if ( *a1 > 0 )
{
v1 = 0;
do
{
v2 = stablestorage_get_record(*(unsigned int *)&a1[2 * v1 + 2]);
if ( v2 && *(_QWORD *)(v2 + 16) )
sendbuf_add_accept_ack(to_learners, v2);
++v1;
}
while ( *a1 > v1 );
}
stablestorage_tx_end();
return sendbuf_flush(to_learners);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int16 *a1)
{
__int16 v1; // bx
__int64 v2; // rax

sendbuf_clear(to_learners, 8LL, (unsigned int)(__int16)this_acceptor_id);
stablestorage_tx_begin();
if ( *a1 > 0 )
{
v1 = 0;
do
{
v2 = stablestorage_get_record(*(unsigned int *)&a1[2 * v1 + 2]);
if ( v2 && *(_QWORD *)(v2 + 16) )
sendbuf_add_accept_ack(to_learners, v2);
++v1;
}
while ( *a1 > v1 );
}
stablestorage_tx_end();
return sendbuf_flush(to_learners);
}

original_code:
static void
handle_repeat_req_batch(repeat_req_batch* rrb) {
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Repeating accept for %d instances\n", rrb->count) ;};
    sendbuf_clear(to_learners, accept_acks, this_acceptor_id);
    stablestorage_tx_begin();
    short int i;
    acceptor_record * rec;
    for(i = 0; i < rrb->count; i++) {
        rec = stablestorage_get_record(rrb->requests[i]);
        if(rec != ((void *)0) && rec->value_size > 0) {
            sendbuf_add_accept_ack(to_learners, rec);
        } else {
            if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Cannot retransmit iid:%lu no value accepted \n", rrb->requests[i]) ;};
        }
    }
    stablestorage_tx_end();
    sendbuf_flush(to_learners);
}

canonical_original_code:
static void handle_repeat_req_batch(struct repeat_req_batch_t *rrb) {
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Repeating accept for %d instances\n", rrb->count) ;};
    sendbuf_clear(to_learners, accept_acks, this_acceptor_id);
    stablestorage_tx_begin();
    short i;
    struct accept_ack_t *rec;
    for(i = 0; i < rrb->count; i++) {
        rec = stablestorage_get_record(rrb->requests[i]);
        if(rec != ((void *)0) && rec->value_size > 0) {
            sendbuf_add_accept_ack(to_learners, rec);
        } else {
            if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Cannot retransmit iid:%lu no value accepted \n", rrb->requests[i]) ;};
        }
    }
    stablestorage_tx_end();
    sendbuf_flush(to_learners);
}

variable_types: struct repeat_req_batch_t { short count; unsigned int requests[]; } *

user_defined_types: struct accept_ack_t { unsigned int iid; unsigned int ballot; unsigned int value_ballot; short is_final; unsigned long value_size; char value[]; }


decompiled_code:
__int64 acc_retransmit_latest_accept()
{
__int64 v0; // rsi

sendbuf_clear(to_learners, 8LL, (unsigned int)(__int16)this_acceptor_id);
stablestorage_tx_begin();
v0 = stablestorage_get_record((unsigned int)highest_accepted_iid);
sendbuf_add_accept_ack(to_learners, v0);
stablestorage_tx_end();
return sendbuf_flush(to_learners);
}

canonical_decompiled_code:
__int64 func7()
{
__int64 v0; // rsi

sendbuf_clear(to_learners, 8LL, (unsigned int)(__int16)this_acceptor_id);
stablestorage_tx_begin();
v0 = stablestorage_get_record((unsigned int)highest_accepted_iid);
sendbuf_add_accept_ack(to_learners, v0);
stablestorage_tx_end();
return sendbuf_flush(to_learners);
}

original_code:
static void
acc_retransmit_latest_accept() {
    acceptor_record * rec;
    sendbuf_clear(to_learners, accept_acks, this_acceptor_id);
    stablestorage_tx_begin();
    rec = stablestorage_get_record(highest_accepted_iid);
    sendbuf_add_accept_ack(to_learners, rec);
    stablestorage_tx_end();
    sendbuf_flush(to_learners);
}

canonical_original_code:
static void acc_retransmit_latest_accept() {
    struct accept_ack_t *rec;
    sendbuf_clear(to_learners, accept_acks, this_acceptor_id);
    stablestorage_tx_begin();
    rec = stablestorage_get_record(highest_accepted_iid);
    sendbuf_add_accept_ack(to_learners, rec);
    stablestorage_tx_end();
    sendbuf_flush(to_learners);
}


decompiled_code:
int acc_periodic_repeater()
{
int result; // eax

if ( highest_accepted_iid )
acc_retransmit_latest_accept();
result = event_add(&repeat_accept_event, &periodic_repeat_interval);
if ( result )
result = puts("Error while adding next repeater periodic event");
return result;
}

canonical_decompiled_code:
int func8()
{
int result; // eax

if ( highest_accepted_iid )
func7();
result = event_add(&repeat_accept_event, &periodic_repeat_interval);
if ( result )
result = puts("Error while adding next repeater periodic event");
return result;
}

original_code:
static void
acc_periodic_repeater(int fd, short event, void *arg)
{
    (fd = fd);;
    (event = event);;
    (arg = arg);;
    if (highest_accepted_iid > 0) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("re-sending most recent accept, iid:%lu\n", highest_accepted_iid) ;};
        acc_retransmit_latest_accept();
    }
    if(event_add(&repeat_accept_event, &periodic_repeat_interval) != 0) {
    printf("Error while adding next repeater periodic event\n");
 }
}

variable_types: event short

variable_types:
arg
void *


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

if ( a2 && *(_DWORD *)(a2 + 4) > (unsigned int)a1[1] )
return 0LL;
result = stablestorage_save_accept(a1);
if ( *a1 > (unsigned int)highest_accepted_iid )
highest_accepted_iid = *a1;
return result;
}

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

if ( a2 && *(_DWORD *)(a2 + 4) > (unsigned int)a1[1] )
return 0LL;
result = stablestorage_save_accept(a1);
if ( *a1 > (unsigned int)highest_accepted_iid )
highest_accepted_iid = *a1;
return result;
}

original_code:
static acceptor_record *
acc_apply_accept(accept_req * ar, acceptor_record * rec) {
    if (rec != ((void *)0) && rec->ballot > ar->ballot) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Accept for iid:%lu dropped (ballots curr:%u recv:%u)\n", ar->iid, rec->ballot, ar->ballot) ;};
        return ((void *)0);
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Accepting for iid:%lu (ballot:%u)\n", ar->iid, ar->ballot) ;};
    rec = stablestorage_save_accept(ar);
    if(ar->iid > highest_accepted_iid) {
        highest_accepted_iid = ar->iid;
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Highest accepted is now iid:%lu\n", highest_accepted_iid) ;};
    }
    return rec;
}

canonical_original_code:
static struct accept_ack_t *acc_apply_accept(struct accept_req_t *ar, struct accept_ack_t *rec) {
    if (rec != ((void *)0) && rec->ballot > ar->ballot) {
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Accept for iid:%lu dropped (ballots curr:%u recv:%u)\n", ar->iid, rec->ballot, ar->ballot) ;};
        return ((void *)0);
    }
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Accepting for iid:%lu (ballot:%u)\n", ar->iid, ar->ballot) ;};
    rec = stablestorage_save_accept(ar);
    if(ar->iid > highest_accepted_iid) {
        highest_accepted_iid = ar->iid;
        if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Highest accepted is now iid:%lu\n", highest_accepted_iid) ;};
    }
    return rec;
}

variable_types: struct accept_req_t { unsigned int iid; unsigned int ballot; unsigned long value_size; char value[]; } *


decompiled_code:
__int64 __fastcall handle_accept_req_batch(__int16 *a1)
{
__int64 v1; // r12
int v2; // ebp
__int64 v3; // rsi
__int64 v4; // rax

sendbuf_clear(to_learners, 8LL, (unsigned int)(__int16)this_acceptor_id);
stablestorage_tx_begin();
if ( *a1 > 0 )
{
v1 = 0LL;
v2 = 0;
do
{
v3 = stablestorage_get_record(*(unsigned int *)((char *)a1 + v1 + 4));
v4 = acc_apply_accept((int *)((char *)a1 + v1 + 4), v3);
if ( v4 )
sendbuf_add_accept_ack(to_learners, v4);
v1 += *(_QWORD *)((char *)a1 + v1 + 12) + 16LL;
++v2;
}
while ( *a1 > (__int16)v2 );
}
stablestorage_tx_end();
return sendbuf_flush(to_learners);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int16 *a1)
{
__int64 v1; // r12
int v2; // ebp
__int64 v3; // rsi
__int64 v4; // rax

sendbuf_clear(to_learners, 8LL, (unsigned int)(__int16)this_acceptor_id);
stablestorage_tx_begin();
if ( *a1 > 0 )
{
v1 = 0LL;
v2 = 0;
do
{
v3 = stablestorage_get_record(*(unsigned int *)((char *)a1 + v1 + 4));
v4 = func9((int *)((char *)a1 + v1 + 4), v3);
if ( v4 )
sendbuf_add_accept_ack(to_learners, v4);
v1 += *(_QWORD *)((char *)a1 + v1 + 12) + 16LL;
++v2;
}
while ( *a1 > (__int16)v2 );
}
stablestorage_tx_end();
return sendbuf_flush(to_learners);
}

original_code:
static void
handle_accept_req_batch(accept_req_batch* arb) {
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Handling accept for %d instances\n", arb->count) ;};
    sendbuf_clear(to_learners, accept_acks, this_acceptor_id);
    stablestorage_tx_begin();
    short int i;
    size_t data_offset = 0;
    accept_req * ar;
    acceptor_record * rec;
    for(i = 0; i < arb->count; i++) {
        ar = (accept_req*) &arb->data[data_offset];
        rec = stablestorage_get_record(ar->iid);
        rec = acc_apply_accept(ar, rec);
        if(rec != ((void *)0)) {
            sendbuf_add_accept_ack(to_learners, rec);
        }
        data_offset += (ar->value_size + sizeof(accept_req));
    }
    stablestorage_tx_end();
    sendbuf_flush(to_learners);
}

canonical_original_code:
static void handle_accept_req_batch(struct accept_req_batch_t *arb) {
    if(0 >= 3) { printf("[%s] ", __func__) ; printf ("Handling accept for %d instances\n", arb->count) ;};
    sendbuf_clear(to_learners, accept_acks, this_acceptor_id);
    stablestorage_tx_begin();
    short i;
    unsigned long data_offset = 0;
    struct accept_req_t *ar;
    struct accept_ack_t *rec;
    for(i = 0; i < arb->count; i++) {
        ar = (struct accept_req_t *) &arb->data[data_offset];
        rec = stablestorage_get_record(ar->iid);
        rec = acc_apply_accept(ar, rec);
        if(rec != ((void *)0)) {
            sendbuf_add_accept_ack(to_learners, rec);
        }
        data_offset += (ar->value_size + sizeof(accept_req));
    }
    stablestorage_tx_end();
    sendbuf_flush(to_learners);
}

variable_types: struct accept_req_batch_t { short count; short proposer_id; char data[]; } *

user_defined_types: struct accept_req_t { unsigned int iid; unsigned int ballot; unsigned long value_size; char value[]; }


decompiled_code:
int __fastcall acc_handle_newmsg(int a1)
{
__int64 v1; // rsi

if ( *(_DWORD *)for_acceptor != a1 )
_assert_fail("sock == for_acceptor->sock", "acceptor.c", 0x13Au, "acc_handle_newmsg");
if ( (int)udp_read_next_message() < 0 )
return puts("Dropping invalid acceptor message");
v1 = *(unsigned int *)(for_acceptor + 28);
switch ( (_DWORD)v1 )
{
case 4:
return handle_accept_req_batch((__int16 *)(for_acceptor + 32));
case 0x10:
return handle_repeat_req_batch((__int16 *)(for_acceptor + 32));
case 1:
return handle_prepare_req_batch((__int16 *)(for_acceptor + 32));
}
return printf("Unknow msg type %d received by acceptor\n", v1);
}

canonical_decompiled_code:
int __fastcall func11(int a1)
{
__int64 v1; // rsi

if ( *(_DWORD *)for_acceptor != a1 )
_assert_fail("sock == for_acceptor->sock", "acceptor.c", 0x13Au, "acc_handle_newmsg");
if ( (int)udp_read_next_message() < 0 )
return puts("Dropping invalid acceptor message");
v1 = *(unsigned int *)(for_acceptor + 28);
switch ( (_DWORD)v1 )
{
case 4:
return func10((__int16 *)(for_acceptor + 32));
case 0x10:
return func6((__int16 *)(for_acceptor + 32));
case 1:
return func5((__int16 *)(for_acceptor + 32));
}
return printf("Unknow msg type %d received by acceptor\n", v1);
}

original_code:
static void
acc_handle_newmsg(int sock, short event, void *arg) {
    (sock = sock);;
    (event = event);;
    (arg = arg);;
    ((void) sizeof ((sock == for_acceptor->sock) ? 1 : 0), __extension__ ({ if (sock == for_acceptor->sock) ; else __assert_fail ("sock == for_acceptor->sock", "acceptor.c", 314, __extension__ __PRETTY_FUNCTION__); }));
    int valid = udp_read_next_message(for_acceptor);
    if (valid < 0) {
        printf("Dropping invalid acceptor message\n");
        return;
    }
    paxos_msg * msg = (paxos_msg*) &for_acceptor->recv_buffer;
    switch(msg->type) {
        case prepare_reqs: {
            handle_prepare_req_batch((prepare_req_batch*) msg->data);
        }
        break;
        case accept_reqs: {
            handle_accept_req_batch((accept_req_batch*) msg->data);
        }
        break;
        case repeat_reqs: {
            handle_repeat_req_batch((repeat_req_batch*) msg->data);
        }
        break;
        default: {
            printf("Unknow msg type %d received by acceptor\n", msg->type);
        }
    }
}

canonical_original_code:
static void acc_handle_newmsg(int sock, short event, void *arg) {
    (sock = sock);;
    (event = event);;
    (arg = arg);;
    ((void) sizeof ((sock == for_acceptor->sock) ? 1 : 0), __extension__ ({ if (sock == for_acceptor->sock) ; else __assert_fail ("sock == for_acceptor->sock", "acceptor.c", 314, __extension__ __PRETTY_FUNCTION__); }));
    int valid = udp_read_next_message(for_acceptor);
    if (valid < 0) {
        printf("Dropping invalid acceptor message\n");
        return;
    }
    struct paxos_msg_t *msg = (paxos_msg*) &for_acceptor->recv_buffer;
    switch(msg->type) {
        case prepare_reqs: {
            handle_prepare_req_batch((struct prepare_req_batch_t *) msg->data);
        }
        break;
        case accept_reqs: {
            handle_accept_req_batch((struct accept_req_batch_t *) msg->data);
        }
        break;
        case repeat_reqs: {
            handle_repeat_req_batch((struct repeat_req_batch_t *) msg->data);
        }
        break;
        default: {
            printf("Unknow msg type %d received by acceptor\n", msg->type);
        }
    }
}

variable_types: sock int

variable_types: valid int

variable_types: struct paxos_msg_t { unsigned long data_size; enum pax_msg_code_e { prepare_reqs=1, prepare_acks=2, accept_reqs=4, accept_acks=8, repeat_reqs=16, submit=32, leader_announce=64, alive_ping=65 } type; char data[]; } *

user_defined_types: struct paxos_msg_t { unsigned long data_size; enum pax_msg_code_e type; char data[]; }

user_defined_types: struct accept_req_batch_t { short count; short proposer_id; char data[]; }

user_defined_types: struct repeat_req_batch_t { short count; unsigned int requests[]; }

user_defined_types: struct prepare_req_t { unsigned int iid; unsigned int ballot; }


decompiled_code:
__int64 init_acc_stable_storage()
{
return stablestorage_init((unsigned int)this_acceptor_id);
}

canonical_decompiled_code:
__int64 func12()
{
return stablestorage_init((unsigned int)this_acceptor_id);
}

original_code:
static int
init_acc_stable_storage() {
    return stablestorage_init(this_acceptor_id);
}


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

learner_suspend();
if ( (unsigned int)init_acc_network() )
{
puts("Acceptor network init failed");
result = 0xFFFFFFFFLL;
}
else if ( (unsigned int)init_acc_timers() )
{
puts("Acceptor timers init failed");
result = 0xFFFFFFFFLL;
}
else
{
result = init_acc_stable_storage();
if ( (_DWORD)result )
{
puts("Acceptor stable storage init failed");
result = 0xFFFFFFFFLL;
}
}
return result;
}

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

learner_suspend();
if ( (unsigned int)func2() )
{
puts("Acceptor network init failed");
result = 0xFFFFFFFFLL;
}
else if ( (unsigned int)func3() )
{
puts("Acceptor timers init failed");
result = 0xFFFFFFFFLL;
}
else
{
result = func12();
if ( (_DWORD)result )
{
puts("Acceptor stable storage init failed");
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
static int init_acceptor() {
    if(0 >= 1) { printf("[%s] ", __func__) ; printf ("Acceptor shutting down learner events\n") ;};
    learner_suspend();
    if(init_acc_network() != 0) {
        printf("Acceptor network init failed\n");
        return -1;
    }
    if(init_acc_timers() != 0){
        printf("Acceptor timers init failed\n");
        return -1;
    }
    if(init_acc_stable_storage() != 0) {
        printf("Acceptor stable storage init failed\n");
        return -1;
    }
    return 0;
}


decompiled_code:
__int64 __fastcall acceptor_init(unsigned int a1)
{
unsigned int v1; // ebx

if ( a1 > 2 )
{
printf("Invalid acceptor id:%d\n", a1);
v1 = -1;
}
else
{
this_acceptor_id = a1;
v1 = learner_init(acc_deliver_callback, init_acceptor);
if ( v1 )
{
puts("Could not start the learner!");
v1 = -1;
}
else
{
printf("Acceptor %d is ready\n", (unsigned int)this_acceptor_id);
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func14(unsigned int a1)
{
unsigned int v1; // ebx

if ( a1 > 2 )
{
printf("Invalid acceptor id:%d\n", a1);
v1 = -1;
}
else
{
this_acceptor_id = a1;
v1 = learner_init(acc_deliver_callback, init_acceptor);
if ( v1 )
{
puts("Could not start the learner!");
v1 = -1;
}
else
{
printf("Acceptor %d is ready\n", (unsigned int)this_acceptor_id);
}
}
return v1;
}

original_code:
int acceptor_init(int acceptor_id) {
    if(3 >= (sizeof(unsigned int)*8)) {
        printf("Error, this library currently supports at most:%d acceptors\n",
            (int)(sizeof(unsigned int)*8));
        printf("(the number of bits in a 'unsigned int', used as acceptor id)\n");
        return -1;
    }
    if(acceptor_id < 0 || acceptor_id >= 3) {
        printf("Invalid acceptor id:%d\n", acceptor_id);
        return -1;
    }
    this_acceptor_id = acceptor_id;
    if(0 >= 1) { printf("[%s] ", __func__) ; printf ("Acceptor %d starting...\n", this_acceptor_id) ;};
    if (learner_init(acc_deliver_callback, init_acceptor) != 0) {
        printf("Could not start the learner!\n");
        return -1;
    }
    printf("Acceptor %d is ready\n", this_acceptor_id);
    return 0;
}


decompiled_code:
__int64 __fastcall acceptor_init_recover(unsigned int a1)
{
stablestorage_do_recovery();
return acceptor_init(a1);
}

canonical_decompiled_code:
__int64 __fastcall func15(unsigned int a1)
{
stablestorage_do_recovery();
return func14(a1);
}

original_code:
int acceptor_init_recover(int acceptor_id) {
    stablestorage_do_recovery();
    return acceptor_init(acceptor_id);
}


decompiled_code:
__int64 acceptor_exit()
{
if ( (unsigned int)stablestorage_shutdown() )
puts("stablestorage shutdown failed!");
return 0LL;
}

canonical_decompiled_code:
__int64 func16()
{
if ( (unsigned int)stablestorage_shutdown() )
puts("stablestorage shutdown failed!");
return 0LL;
}

original_code:
int acceptor_exit() {
    if (stablestorage_shutdown() != 0) {
        printf("stablestorage shutdown failed!\n");
    }
    return 0;
}


decompiled_code:
unsigned __int64 __fastcall save_latency_info(_QWORD *a1, __int64 *a2)
{
unsigned __int64 result; // rax
int v4; // esi
unsigned __int64 v5; // rtt

subtract_in_place_timevals(a2, a1);
if ( is_gt_timeval(a2, &max_latency) )
{
max_latency = *a2;
qword_B68 = a2[1];
}
if ( is_lt_timeval(a2, &min_latency) )
{
min_latency = *a2;
qword_B78 = a2[1];
}
sum_in_place_timevals(&aggregated_latency, a2);
result = ++aggregated_latency_count;
if ( sample_frequency )
{
v4 = samples_count;
if ( samples_count <= 499 )
{
v5 = result;
result /= (unsigned __int64)sample_frequency;
if ( !(v5 % sample_frequency) )
{
result = samples;
*(double *)(samples + 8LL * samples_count) = (float)((float)((float)(int)a2[1] / 1000.0)
+ (float)((float)(int)*a2 * 1000.0));
samples_count = v4 + 1;
}
}
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func5(_QWORD *a1, __int64 *a2)
{
unsigned __int64 result; // rax
int v4; // esi
unsigned __int64 v5; // rtt

func4(a2, a1);
if ( func1(a2, &max_latency) )
{
max_latency = *a2;
qword_B68 = a2[1];
}
if ( func2(a2, &min_latency) )
{
min_latency = *a2;
qword_B78 = a2[1];
}
func3(&aggregated_latency, a2);
result = ++aggregated_latency_count;
if ( sample_frequency )
{
v4 = samples_count;
if ( samples_count <= 499 )
{
v5 = result;
result /= (unsigned __int64)sample_frequency;
if ( !(v5 % sample_frequency) )
{
result = samples;
*(double *)(samples + 8LL * samples_count) = (float)((float)((float)(int)a2[1] / 1000.0)
+ (float)((float)(int)*a2 * 1000.0));
samples_count = v4 + 1;
}
}
}
return result;
}

original_code:
static void
save_latency_info (client_value_record * cvr, struct timeval * curr_time) {
    subtract_in_place_timevals(curr_time, &cvr->creation_time);
    struct timeval * time_taken = curr_time;
    if(is_gt_timeval(time_taken, &max_latency)) {
        max_latency.tv_sec = time_taken->tv_sec;
        max_latency.tv_usec = time_taken->tv_usec;
    }
    if(is_lt_timeval(time_taken, &min_latency)) {
        min_latency.tv_sec = time_taken->tv_sec;
        min_latency.tv_usec = time_taken->tv_usec;
    }
    sum_in_place_timevals(&aggregated_latency, time_taken);
    aggregated_latency_count += 1;
    if(sample_frequency != 0 &&
    samples_count < max_samples &&
    aggregated_latency_count % sample_frequency == 0) {
        samples[samples_count] = ((float)time_taken->tv_sec*1000) +
            ((float)time_taken->tv_usec/1000);
        samples_count += 1;
    }
}

canonical_original_code:
static void save_latency_info(struct client_value_record_t *cvr, struct timeval *curr_time) {
    subtract_in_place_timevals(curr_time, &cvr->creation_time);
    struct timeval *time_taken = curr_time;
    if(is_gt_timeval(time_taken, &max_latency)) {
        max_latency.tv_sec = time_taken->tv_sec;
        max_latency.tv_usec = time_taken->tv_usec;
    }
    if(is_lt_timeval(time_taken, &min_latency)) {
        min_latency.tv_sec = time_taken->tv_sec;
        min_latency.tv_usec = time_taken->tv_usec;
    }
    sum_in_place_timevals(&aggregated_latency, time_taken);
    aggregated_latency_count += 1;
    if(sample_frequency != 0 &&
    samples_count < max_samples &&
    aggregated_latency_count % sample_frequency == 0) {
        samples[samples_count] = ((float)time_taken->tv_sec*1000) +
            ((float)time_taken->tv_usec/1000);
        samples_count += 1;
    }
}

variable_types: struct client_value_record_t { struct timeval { long tv_sec; long tv_usec; } creation_time; struct timeval expire_time; unsigned long value_size; char value[-1]; } *

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

user_defined_types: struct client_value_record_t { struct timeval creation_time; struct timeval expire_time; unsigned long value_size; char value[-1]; }


decompiled_code:
__int64 __fastcall submit_old_value(__int64 a1)
{
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

++retried_count;
gettimeofday(&v2, 0LL);
sum_timevals((_QWORD *)(a1 + 16), (__int64)&v2, &values_timeout);
return pax_submit_nonblock(psh, a1 + 40, *(_QWORD *)(a1 + 32));
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

++retried_count;
gettimeofday(&v2, 0LL);
func0((_QWORD *)(a1 + 16), (__int64)&v2, &values_timeout);
return pax_submit_nonblock(psh, a1 + 40, *(_QWORD *)(a1 + 32));
}

original_code:
static void
submit_old_value(client_value_record * cvr) {
    retried_count += 1;
    struct timeval time_now;
    gettimeofday(&time_now, ((void *)0));
    sum_timevals(&cvr->expire_time, &time_now, &values_timeout);
    pax_submit_nonblock(psh, cvr->value, cvr->value_size);
}

canonical_original_code:
static void submit_old_value(struct client_value_record_t *cvr) {
    retried_count += 1;
    struct timeval time_now;
    gettimeofday(&time_now, ((void *)0));
    sum_timevals(&cvr->expire_time, &time_now, &values_timeout);
    pax_submit_nonblock(psh, cvr->value, cvr->value_size);
}

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


decompiled_code:
int print_benchmark_parameters()
{
printf("sample_frequency set to: %d\n", (unsigned int)sample_frequency);
printf("print_step set to: %d\n", (unsigned int)print_step);
printf("values_timeout set to: %d\n", (unsigned int)values_timeout);
printf("concurrent_values set to: %d\n", (unsigned int)concurrent_values);
printf("min_val_size set to: %d\n", (unsigned int)min_val_size);
printf("max_val_size set to: %d\n", (unsigned int)max_val_size);
printf("duration set to: %d\n", (unsigned int)duration);
printf("Initial submission delay set to: %d\n", wait_after_init);
return fflush(stdout);
}

canonical_decompiled_code:
int func8()
{
printf("sample_frequency set to: %d\n", (unsigned int)sample_frequency);
printf("print_step set to: %d\n", (unsigned int)print_step);
printf("values_timeout set to: %d\n", (unsigned int)values_timeout);
printf("concurrent_values set to: %d\n", (unsigned int)concurrent_values);
printf("min_val_size set to: %d\n", (unsigned int)min_val_size);
printf("max_val_size set to: %d\n", (unsigned int)max_val_size);
printf("duration set to: %d\n", (unsigned int)duration);
printf("Initial submission delay set to: %d\n", wait_after_init);
return fflush(stdout);
}

original_code:
static void
print_benchmark_parameters () {
    printf("sample_frequency set to: %d\n", sample_frequency);
    printf("print_step set to: %d\n", print_step);
    printf("values_timeout set to: %d\n", (int)values_timeout.tv_sec);
    printf("concurrent_values set to: %d\n", concurrent_values);
    printf("min_val_size set to: %d\n", min_val_size);
    printf("max_val_size set to: %d\n", max_val_size);
    printf("duration set to: %d\n", duration);
    printf("Initial submission delay set to: %d\n", wait_after_init);
    fflush(stdout);
}

canonical_original_code:
static void print_benchmark_parameters() {
    printf("sample_frequency set to: %d\n", sample_frequency);
    printf("print_step set to: %d\n", print_step);
    printf("values_timeout set to: %d\n", (int)values_timeout.tv_sec);
    printf("concurrent_values set to: %d\n", concurrent_values);
    printf("min_val_size set to: %d\n", min_val_size);
    printf("max_val_size set to: %d\n", max_val_size);
    printf("duration set to: %d\n", duration);
    printf("Initial submission delay set to: %d\n", wait_after_init);
    fflush(stdout);
}