decompiled_code:
__int64 __fastcall handle_client_handshake(int fd)
{
const char **v2; // r14
__int64 result; // rax
const char *v4; // rdi
const char **v5; // rbx
char *v6; // rbx
char **v7; // r12
char *v8; // rax
const char *v9; // rbx
size_t v10; // r15
void *v11; // rsp
size_t v12; // rax
void *v13; // r15
void *v14; // rbx
char s[96]; // [rsp+0h] [rbp-60h] BYREF

v2 = (const char **)read_request(fd);
result = 0LL;
if ( v2 )
{
puts("received request:\n-----");
v4 = *v2;
if ( *v2 )
{
v5 = v2;
do
{
puts(v4);
v4 = *++v5;
}
while ( *v5 );
}
puts("-----");
v6 = (char *)*v2;
if ( !*v2 )
goto LABEL_9;
v7 = (char **)v2;
while ( !startswith(v6, "Sec-WebSocket-Key") )
{
v6 = *++v7;
if ( !*v7 )
goto LABEL_9;
}
v8 = header_value(v6);
v9 = v8;
if ( v8 )
{
strcpy(s, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
v10 = strlen(v8);
v11 = alloca(v10 + strlen(s) + 1);
strcpy(s, v9);
strcat(s, s);
v12 = strlen(s);
v13 = (void *)sha1(s, v12);
v14 = (void *)base64_encode(v13, 20LL);
free(v13);
release_lines(v2);
puts("Sending response:");
send_line(fd, "HTTP/1.1 101 Switching Protocols\r\n");
send_line(fd, "Upgrade: websocket\r\n");
send_line(fd, "Connection: Upgrade\r\n");
send_line(fd, "Sec-WebSocket-Accept: ");
send_line(fd, v14);
send_line(fd, "\r\n");
send_line(fd, "\r\n");
puts("-----");
free(v14);
result = 1LL;
}
else
{
LABEL_9:
puts("Sending response:");
send_line(fd, "HTTP/1.1 400 Bad Request\r\n\r\n");
puts("-----");
release_lines(v2);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int fd)
{
const char **v2; // r14
__int64 result; // rax
const char *v4; // rdi
const char **v5; // rbx
char *v6; // rbx
char **v7; // r12
char *v8; // rax
const char *v9; // rbx
size_t v10; // r15
void *v11; // rsp
size_t v12; // rax
void *v13; // r15
void *v14; // rbx
char s[96]; // [rsp+0h] [rbp-60h] BYREF

v2 = (const char **)func5(fd);
result = 0LL;
if ( v2 )
{
puts("received request:\n-----");
v4 = *v2;
if ( *v2 )
{
v5 = v2;
do
{
puts(v4);
v4 = *++v5;
}
while ( *v5 );
}
puts("-----");
v6 = (char *)*v2;
if ( !*v2 )
goto LABEL_9;
v7 = (char **)v2;
while ( !func6(v6, "Sec-WebSocket-Key") )
{
v6 = *++v7;
if ( !*v7 )
goto LABEL_9;
}
v8 = func7(v6);
v9 = v8;
if ( v8 )
{
strcpy(s, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
v10 = strlen(v8);
v11 = alloca(v10 + strlen(s) + 1);
strcpy(s, v9);
strcat(s, s);
v12 = strlen(s);
v13 = (void *)sha1(s, v12);
v14 = (void *)base64_encode(v13, 20LL);
free(v13);
func4(v2);
puts("Sending response:");
func8(fd, "HTTP/1.1 101 Switching Protocols\r\n");
func8(fd, "Upgrade: websocket\r\n");
func8(fd, "Connection: Upgrade\r\n");
func8(fd, "Sec-WebSocket-Accept: ");
func8(fd, v14);
func8(fd, "\r\n");
func8(fd, "\r\n");
puts("-----");
free(v14);
result = 1LL;
}
else
{
LABEL_9:
puts("Sending response:");
func8(fd, "HTTP/1.1 400 Bad Request\r\n\r\n");
puts("-----");
func4(v2);
result = 0LL;
}
}
return result;
}

original_code:
static int handle_client_handshake(int sock) {
    char **request = read_request(sock);
    if (request == ((void *)0)) {
        return 0;
    }
    printf("received request:\n-----\n");
    for (char **p = request; *p != ((void *)0); p++) {
        printf("%s\n", *p);
    }
    printf("-----\n");
    char *sec_websocket_key = ((void *)0);
    for (char **p = request; *p != ((void *)0); p++) {
        if (startswith(*p, "Sec-WebSocket-Key")) {
            sec_websocket_key = header_value(*p);
            break;
        }
    }
    if (sec_websocket_key == ((void *)0)) {
        printf("Sending response:\n");
        send_line(sock, "HTTP/1.1 400 Bad Request\r\n\r\n");
        printf("-----\n");
        release_lines(request);
        return 0;
    }
    char guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    char keybuf[strlen(sec_websocket_key) + strlen(guid) + 1];
    strcpy(keybuf, sec_websocket_key);
    strcat(keybuf, guid);
    unsigned char *hashed = sha1(keybuf, strlen(keybuf));
    char *encoded = base64_encode(hashed, 20);
    free(hashed);
    release_lines(request);
    printf("Sending response:\n");
    send_line(sock, "HTTP/1.1 101 Switching Protocols\r\n");
    send_line(sock, "Upgrade: websocket\r\n");
    send_line(sock, "Connection: Upgrade\r\n");
    send_line(sock, "Sec-WebSocket-Accept: ");
    send_line(sock, encoded);
    send_line(sock, "\r\n");
    send_line(sock, "\r\n");
    printf("-----\n");
    free(encoded);
    return 1;
}

canonical_original_code:
static int handle_client_handshake(int sock) {
    char **request = read_request(sock);
    if (request == ((void *)0)) {
        return 0;
    }
    printf("received request:\n-----\n");
    for (char **p = request; *p != ((void *)0); p++) {
        printf("%s\n", *p);
    }
    printf("-----\n");
    char *sec_websocket_key = ((void *)0);
    for (char **p = request; *p != ((void *)0); p++) {
        if (startswith(*p, "Sec-WebSocket-Key")) {
            sec_websocket_key = header_value(*p);
            break;
        }
    }
    if (sec_websocket_key == ((void *)0)) {
        printf("Sending response:\n");
        send_line(sock, "HTTP/1.1 400 Bad Request\r\n\r\n");
        printf("-----\n");
        release_lines(request);
        return 0;
    }
    char guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    char keybuf[-1];
    strcpy(keybuf, sec_websocket_key);
    strcat(keybuf, guid);
    unsigned char *hashed = sha1(keybuf, strlen(keybuf));
    char *encoded = base64_encode(hashed, 20);
    free(hashed);
    release_lines(request);
    printf("Sending response:\n");
    send_line(sock, "HTTP/1.1 101 Switching Protocols\r\n");
    send_line(sock, "Upgrade: websocket\r\n");
    send_line(sock, "Connection: Upgrade\r\n");
    send_line(sock, "Sec-WebSocket-Accept: ");
    send_line(sock, encoded);
    send_line(sock, "\r\n");
    send_line(sock, "\r\n");
    printf("-----\n");
    free(encoded);
    return 1;
}

variable_types:
request
char **

variable_types:
guid
char []

variable_types:
keybuf
char [-1]

variable_types:
hashed
unsigned char *

variable_types:
encoded
char *


repo: franshub/networking

decompiled_code:
ssize_t __fastcall send_close_frame(int a1)
{
__int16 buf[5]; // [rsp+Eh] [rbp-Ah] BYREF

buf[0] = 136;
return send(a1, buf, 2uLL, 0);
}

canonical_decompiled_code:
ssize_t __fastcall func10(int a1)
{
__int16 buf[5]; // [rsp+Eh] [rbp-Ah] BYREF

buf[0] = 136;
return send(a1, buf, 2uLL, 0);
}

original_code:
static void send_close_frame(int sock) {
    unsigned char buf[2] = { 0x88, 0x00 };
    send(sock, buf, sizeof(buf), 0);
}

variable_types: sock int

variable_types:
buf
unsigned char [2]

return_type: void


original_code:
static int parse_frame_header(unsigned char *buffer, struct frame_header *header) {
    header->fin = (buffer[0] & 0x80) >> 7;
    header->rsv1 = (buffer[0] & 0x40) >> 6;
    header->rsv2 = (buffer[0] & 0x20) >> 5;
    header->rsv3 = (buffer[0] & 0x10) >> 4;
    header->opcode = buffer[0] & 0x0f;
    header->has_mask = (buffer[1] & 0x80) >> 7;
    unsigned int payload_length = buffer[1] & 0x7f;
    int mask_offset = 2;
    if (payload_length < 126) {
        header->length_lsw = payload_length;
        header->length_msw = 0;
    } else if (payload_length == 126) {
        header->length_lsw = (buffer[2] << 8) | buffer[9];
        header->length_msw = 0;
        mask_offset += 2;
    } else {
        header->length_lsw = (buffer[2] << 24) | (buffer[3] << 16) | (buffer[4] << 8) | buffer[5];
        header->length_msw = (buffer[6] << 24) | (buffer[7] << 16) | (buffer[8] << 8) | buffer[9];
        mask_offset += 4;
    }
    if (header->has_mask) {
        for (int i = 0; i < 4; i++) {
            header->masking_key[i] = buffer[mask_offset + i];
        }
    } else {
        memset(header->masking_key, 0, 4);
    }
    static char *opcode_names[] = {
        "continuation", "text", "binary",
        "reserved", "reserved", "reserved", "reserved", "reserved",
        "close", "ping", "pong",
        "reserved", "reserved", "reserved", "reserved", "reserved"
    };
    printf("frame:");
    if (header->rsv1 != 0) {
        printf(" rsv1=1");
    }
    if (header->rsv2 != 0) {
        printf(" rsv2=1");
    }
    if (header->rsv3 != 0) {
        printf(" rsv3=1");
    }
    printf(" %d %s", header->opcode, opcode_names[header->opcode]);
    if (!header->fin) {
        printf(" not final");
    }
    if (header->length_msw > 0) {
        printf(" %lu * 2^32 + ", header->length_msw);
    }
    printf(" %lu bytes", header->length_lsw);
    if (header->has_mask) {
        printf(" masked");
    }
    printf(" ");
    return 1;
}

variable_types:
buffer
unsigned char *

variable_types: struct frame_header { int fin; int rsv1; int rsv2; int rsv3; int opcode; int has_mask; unsigned long length_msw; unsigned long length_lsw; unsigned char masking_key[4]; } *

variable_types: i int

return_type: int


decompiled_code:
__int64 __fastcall read_frame_header(int fd, __int64 a2)
{
int v2; // ebx
int v3; // ebp
const char *v4; // rdi
ssize_t v6; // rax
int v7; // eax
int v8; // edx
unsigned __int8 v9; // [rsp+2h] [rbp-36h] BYREF
unsigned __int8 v10; // [rsp+3h] [rbp-35h]

v2 = 2;
v3 = 0;
while ( 1 )
{
v6 = recv(fd, &v9 + v3, v2 - v3, 0);
if ( v6 <= 0 )
break;
v7 = v3 + v6;
v3 = v7;
if ( v7 > 1 && v2 == 2 )
{
v2 = ((v10 >> 5) & 4) + 2;
v8 = v10 & 0x7F;
if ( v8 == 126 )
{
v2 = ((v10 >> 5) & 4) + 4;
}
else if ( v8 == 127 )
{
v2 = ((v10 >> 5) & 4) + 10;
}
}
if ( v2 <= v7 )
return parse_frame_header(&v9, a2);
}
v4 = "connection closed unexpectedly\n";
if ( v6 )
v4 = "connection lost\n";
printf(v4);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func13(int fd, __int64 a2)
{
int v2; // ebx
int v3; // ebp
const char *v4; // rdi
ssize_t v6; // rax
int v7; // eax
int v8; // edx
unsigned __int8 v9; // [rsp+2h] [rbp-36h] BYREF
unsigned __int8 v10; // [rsp+3h] [rbp-35h]

v2 = 2;
v3 = 0;
while ( 1 )
{
v6 = recv(fd, &v9 + v3, v2 - v3, 0);
if ( v6 <= 0 )
break;
v7 = v3 + v6;
v3 = v7;
if ( v7 > 1 && v2 == 2 )
{
v2 = ((v10 >> 5) & 4) + 2;
v8 = v10 & 0x7F;
if ( v8 == 126 )
{
v2 = ((v10 >> 5) & 4) + 4;
}
else if ( v8 == 127 )
{
v2 = ((v10 >> 5) & 4) + 10;
}
}
if ( v2 <= v7 )
return func12(&v9, a2);
}
v4 = "connection closed unexpectedly\n";
if ( v6 )
v4 = "connection lost\n";
printf(v4);
return 0LL;
}

original_code:
static int read_frame_header(int sock, struct frame_header *header) {
    unsigned char buffer[14];
    int byte_count = 0;
    int needed_byte_count = 2;
    while (byte_count < needed_byte_count) {
        ssize_t nr_recv = recv(sock, buffer + byte_count, needed_byte_count - byte_count, 0);
        if (nr_recv <= 0) {
            printf(nr_recv == 0 ? "connection closed unexpectedly\n" : "connection lost\n");
            return 0;
        }
        byte_count += nr_recv;
        if (byte_count >= 2 && needed_byte_count == 2) {
            if (buffer[1] & 0x80) {
                needed_byte_count += 4;
            }
            int payload_length = buffer[1] & 0x7f;
            if (payload_length == 126) {
                needed_byte_count += 2;
            } else if (payload_length == 127) {
                needed_byte_count += 8;
            }
        }
    }
    return parse_frame_header(buffer, header);
}

canonical_original_code:
static int read_frame_header(int sock, struct frame_header *header) {
    unsigned char buffer[14];
    int byte_count = 0;
    int needed_byte_count = 2;
    while (byte_count < needed_byte_count) {
        long nr_recv = recv(sock, buffer + byte_count, needed_byte_count - byte_count, 0);
        if (nr_recv <= 0) {
            printf(nr_recv == 0 ? "connection closed unexpectedly\n" : "connection lost\n");
            return 0;
        }
        byte_count += nr_recv;
        if (byte_count >= 2 && needed_byte_count == 2) {
            if (buffer[1] & 0x80) {
                needed_byte_count += 4;
            }
            int payload_length = buffer[1] & 0x7f;
            if (payload_length == 126) {
                needed_byte_count += 2;
            } else if (payload_length == 127) {
                needed_byte_count += 8;
            }
        }
    }
    return parse_frame_header(buffer, header);
}

variable_types:
buffer
unsigned char [14]

user_defined_types: struct frame_header { int fin; int rsv1; int rsv2; int rsv3; int opcode; int has_mask; unsigned long length_msw; unsigned long length_lsw; unsigned char masking_key[4]; }


decompiled_code:
__int64 __fastcall read_frame_body(int a1, __int64 a2)
{
__int64 v3; // r15
int v4; // ebx
const char *v6; // rdi
ssize_t v7; // rbp
unsigned __int8 v8; // si
ssize_t v9; // rax
ssize_t v10; // r12
const unsigned __int16 **v11; // r13
__int64 v12; // [rsp+0h] [rbp-148h]
char buf[312]; // [rsp+10h] [rbp-138h] BYREF

printf("body: ");
v12 = *(_QWORD *)(a2 + 24);
v3 = *(_QWORD *)(a2 + 32);
if ( *(_OWORD *)(a2 + 24) == 0LL )
{
puts("<empty>");
return 1LL;
}
putchar(34);
v4 = 0;
while ( (unsigned __int64)(v3 - 1) > 0xFE )
{
v9 = recv(a1, buf, 0x100uLL, 0);
v10 = v9;
if ( v9 <= 0 )
goto LABEL_4;
if ( !v3 )
{
v3 = 0x100000000LL - v9;
--v12;
goto LABEL_19;
}
LABEL_18:
v3 -= v10;
LABEL_19:
v11 = _ctype_b_loc();
v7 = 0LL;
do
{
v8 = *(_BYTE *)(a2 + v4 + 40) ^ buf[v7];
v4 = (v4 + 1) % 4;
if ( ((*v11)[v8] & 0x4000) != 0 )
putchar(v8);
else
printf("\\x%02.2x", v8);
++v7;
}
while ( v10 > v7 );
if ( !(v3 | v12) )
{
puts("\"");
return 1LL;
}
}
v10 = recv(a1, buf, (int)v3, 0);
if ( v10 > 0 )
goto LABEL_18;
LABEL_4:
v6 = "\nconnection closed unexpectedly\n";
if ( v10 )
v6 = "\nconnection lost\n";
printf(v6);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func14(int a1, __int64 a2)
{
__int64 v3; // r15
int v4; // ebx
const char *v6; // rdi
ssize_t v7; // rbp
unsigned __int8 v8; // si
ssize_t v9; // rax
ssize_t v10; // r12
const unsigned __int16 **v11; // r13
__int64 v12; // [rsp+0h] [rbp-148h]
char buf[312]; // [rsp+10h] [rbp-138h] BYREF

printf("body: ");
v12 = *(_QWORD *)(a2 + 24);
v3 = *(_QWORD *)(a2 + 32);
if ( *(_OWORD *)(a2 + 24) == 0LL )
{
puts("<empty>");
return 1LL;
}
putchar(34);
v4 = 0;
while ( (unsigned __int64)(v3 - 1) > 0xFE )
{
v9 = recv(a1, buf, 0x100uLL, 0);
v10 = v9;
if ( v9 <= 0 )
goto LABEL_4;
if ( !v3 )
{
v3 = 0x100000000LL - v9;
--v12;
goto LABEL_19;
}
LABEL_18:
v3 -= v10;
LABEL_19:
v11 = _ctype_b_loc();
v7 = 0LL;
do
{
v8 = *(_BYTE *)(a2 + v4 + 40) ^ buf[v7];
v4 = (v4 + 1) % 4;
if ( ((*v11)[v8] & 0x4000) != 0 )
putchar(v8);
else
printf("\\x%02.2x", v8);
++v7;
}
while ( v10 > v7 );
if ( !(v3 | v12) )
{
puts("\"");
return 1LL;
}
}
v10 = recv(a1, buf, (int)v3, 0);
if ( v10 > 0 )
goto LABEL_18;
LABEL_4:
v6 = "\nconnection closed unexpectedly\n";
if ( v10 )
v6 = "\nconnection lost\n";
printf(v6);
return 0LL;
}

original_code:
static int read_frame_body(int sock, struct frame_header *header) {
    printf("body: ");
    unsigned long msw = header->length_msw;
    unsigned long lsw = header->length_lsw;
    if (msw == 0 && lsw == 0) {
        printf("<empty>\n");
        return 1;
    }
    printf("\"");
    int mask_ix = 0;
    while (msw > 0 || lsw > 0) {
        unsigned char buffer[256];
        int chunksize = (lsw > 0 && lsw < sizeof(buffer)) ? lsw : sizeof(buffer);
        ssize_t nr_recv = recv(sock, buffer, chunksize, 0);
        if (nr_recv <= 0) {
            printf(nr_recv == 0 ? "\nconnection closed unexpectedly\n" : "\nconnection lost\n");
            return 0;
        }
        if (lsw > 0) {
            lsw -= nr_recv;
        } else {
            lsw = (unsigned long) 0xffffffff - (nr_recv - 1);
            msw--;
        }
        for (int i = 0; i < nr_recv; i++) {
            unsigned char ch = buffer[i] ^ header->masking_key[mask_ix];
            mask_ix = (mask_ix + 1) % 4;
            if (((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int) _ISprint)) {
                printf("%c", ch);
            } else {
                printf("\\x%02.2x", ch);
            }
        }
    }
    printf("\"\n");
    return 1;
}

canonical_original_code:
static int read_frame_body(int sock, struct frame_header *header) {
    printf("body: ");
    unsigned long msw = header->length_msw;
    unsigned long lsw = header->length_lsw;
    if (msw == 0 && lsw == 0) {
        printf("<empty>\n");
        return 1;
    }
    printf("\"");
    int mask_ix = 0;
    while (msw > 0 || lsw > 0) {
        unsigned char buffer[256];
        int chunksize = (lsw > 0 && lsw < sizeof(buffer)) ? lsw : sizeof(buffer);
        long nr_recv = recv(sock, buffer, chunksize, 0);
        if (nr_recv <= 0) {
            printf(nr_recv == 0 ? "\nconnection closed unexpectedly\n" : "\nconnection lost\n");
            return 0;
        }
        if (lsw > 0) {
            lsw -= nr_recv;
        } else {
            lsw = (unsigned long) 0xffffffff - (nr_recv - 1);
            msw--;
        }
        for (int i = 0; i < nr_recv; i++) {
            unsigned char ch = buffer[i] ^ header->masking_key[mask_ix];
            mask_ix = (mask_ix + 1) % 4;
            if (((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short) _ISprint)) {
                printf("%c", ch);
            } else {
                printf("\\x%02.2x", ch);
            }
        }
    }
    printf("\"\n");
    return 1;
}

variable_types:
msw
unsigned long

variable_types:
lsw
unsigned long

variable_types:
buffer
unsigned char [256]

variable_types: chunksize int

variable_types:
ch
unsigned char


decompiled_code:
int __fastcall handle_client(int fd)
{
_DWORD v2[14]; // [rsp+0h] [rbp-38h] BYREF

if ( !(unsigned int)handle_client_handshake(fd) )
return close(fd);
while ( (unsigned int)read_frame_header(fd, (__int64)v2) && (unsigned int)read_frame_body(fd, (__int64)v2) )
{
if ( v2[4] == 8 )
{
send_close_frame(fd);
return close(fd);
}
send_text_frame(fd, "you said something");
}
return close(fd);
}

canonical_decompiled_code:
int __fastcall func15(int fd)
{
_DWORD v2[14]; // [rsp+0h] [rbp-38h] BYREF

if ( !(unsigned int)func9(fd) )
return close(fd);
while ( (unsigned int)func13(fd, (__int64)v2) && (unsigned int)func14(fd, (__int64)v2) )
{
if ( v2[4] == 8 )
{
func10(fd);
return close(fd);
}
func11(fd, "you said something");
}
return close(fd);
}

original_code:
static void handle_client(int sock) {
    int ok = handle_client_handshake(sock);
    if (!ok) {
        close(sock);
        return;
    }
    while (1) {
        struct frame_header header;
        int ok = read_frame_header(sock, &header);
        if (!ok) {
            break;
        }
        ok = read_frame_body(sock, &header);
        if (!ok) {
            break;
        }
        if (header.opcode == 0x08) {
            send_close_frame(sock);
            break;
        }
        send_text_frame(sock, "you said something");
    }
    close(sock);
}

variable_types: ok int

variable_types: struct frame_header { int fin; int rsv1; int rsv2; int rsv3; int opcode; int has_mask; unsigned long length_msw; unsigned long length_lsw; unsigned char masking_key[4]; }


decompiled_code:
__int64 __fastcall create_socket(unsigned __int16 a1)
{
int v1; // eax
unsigned int v2; // ebx
struct sockaddr v4; // [rsp+0h] [rbp-38h] BYREF
int optval[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

v1 = socket(2, 1, 0);
if ( v1 < 0 )
{
puts("socket() failed");
v2 = -1;
}
else
{
v2 = v1;
optval[0] = 1;
setsockopt(v1, 1, 2, optval, 4u);
*(_DWORD *)&v4.sa_data[10] = 0;
v4.sa_family = 2;
*(_WORD *)v4.sa_data = _bswap_16(a1);
*(_QWORD *)&v4.sa_data[2] = (unsigned int)_bswap_32(0);
if ( bind(v2, &v4, 0x10u) )
{
printf("bind() to port %d in INADDR_ANY failed\n", a1);
close(v2);
v2 = -1;
}
else if ( listen(v2, 0) )
{
puts("listen() failed");
close(v2);
v2 = -1;
}
else
{
printf("socket bound to port %d\n", a1);
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int16 a1)
{
int v1; // eax
unsigned int v2; // ebx
struct sockaddr v4; // [rsp+0h] [rbp-38h] BYREF
int optval[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

v1 = socket(2, 1, 0);
if ( v1 < 0 )
{
puts("socket() failed");
v2 = -1;
}
else
{
v2 = v1;
optval[0] = 1;
setsockopt(v1, 1, 2, optval, 4u);
*(_DWORD *)&v4.sa_data[10] = 0;
v4.sa_family = 2;
*(_WORD *)v4.sa_data = func0(a1);
*(_QWORD *)&v4.sa_data[2] = (unsigned int)func1(0);
if ( bind(v2, &v4, 0x10u) )
{
printf("bind() to port %d in INADDR_ANY failed\n", a1);
close(v2);
v2 = -1;
}
else if ( listen(v2, 0) )
{
puts("listen() failed");
close(v2);
v2 = -1;
}
else
{
printf("socket bound to port %d\n", a1);
}
}
return v2;
}

original_code:
static int create_socket(uint16_t port) {
    int sock = socket(2, SOCK_STREAM, 0);
    if (sock < 0) {
        printf("socket() failed\n");
        return -1;
    }
    int enable = 1;
    if (setsockopt(sock, 1, 2, &enable, sizeof(enable)) != 0) {
    }
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = 2;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    socklen_t addr_len = sizeof(addr);
    if (bind(sock, (struct sockaddr *) &addr, addr_len) != 0) {
        printf("bind() to port %d in INADDR_ANY failed\n", port);
        close(sock);
        return -1;
    }
    if (listen(sock, 0) != 0) {
        printf("listen() failed\n");
        close(sock);
        return -1;
    }
    printf("socket bound to port %d\n", port);
    return sock;
}

canonical_original_code:
static int create_socket(unsigned short port) {
    int sock = socket(2, SOCK_STREAM, 0);
    if (sock < 0) {
        printf("socket() failed\n");
        return -1;
    }
    int enable = 1;
    if (setsockopt(sock, 1, 2, &enable, sizeof(enable)) != 0) {
    }
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = 2;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    unsigned int addr_len = sizeof(addr);
    if (bind(sock, (struct sockaddr *) &addr, addr_len) != 0) {
        printf("bind() to port %d in INADDR_ANY failed\n", port);
        close(sock);
        return -1;
    }
    if (listen(sock, 0) != 0) {
        printf("listen() failed\n");
        close(sock);
        return -1;
    }
    printf("socket bound to port %d\n", port);
    return sock;
}

variable_types:
port
unsigned short

variable_types: enable int

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

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

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


decompiled_code:
int __fastcall handle_incoming(int fd)
{
int v1; // eax
int v2; // ebx
char v4[76]; // [rsp+0h] [rbp-68h] BYREF
socklen_t addr_len; // [rsp+4Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+50h] [rbp-18h] BYREF

addr_len = 16;
puts("accepting incoming http-websocket connections...");
v1 = accept(fd, &addr, &addr_len);
if ( v1 < 0 )
return puts("accept() failed");
v2 = v1;
sa_tostring(&addr.sa_family, v4);
printf("connected with %s\n", v4);
return handle_client(v2);
}

canonical_decompiled_code:
int __fastcall func16(int fd)
{
int v1; // eax
int v2; // ebx
char v4[76]; // [rsp+0h] [rbp-68h] BYREF
socklen_t addr_len; // [rsp+4Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+50h] [rbp-18h] BYREF

addr_len = 16;
puts("accepting incoming http-websocket connections...");
v1 = accept(fd, &addr, &addr_len);
if ( v1 < 0 )
return puts("accept() failed");
v2 = v1;
func3(&addr.sa_family, v4);
printf("connected with %s\n", v4);
return func15(v2);
}

original_code:
static void handle_incoming(int sock) {
    struct sockaddr_in remote_addr;
    socklen_t remote_size = sizeof(remote_addr);
    printf("accepting incoming http-websocket connections...\n");
    int csock = accept(sock, (struct sockaddr *) &remote_addr, &remote_size);
    if (csock < 0) {
        printf("accept() failed\n");
        return;
    }
    char addr_s[64];
    sa_tostring((struct sockaddr *) &remote_addr, addr_s);
    printf("connected with %s\n", addr_s);
    handle_client(csock);
}

canonical_original_code:
static void handle_incoming(int sock) {
    struct sockaddr_in remote_addr;
    unsigned int remote_size = sizeof(remote_addr);
    printf("accepting incoming http-websocket connections...\n");
    int csock = accept(sock, (struct sockaddr *) &remote_addr, &remote_size);
    if (csock < 0) {
        printf("accept() failed\n");
        return;
    }
    char addr_s[64];
    sa_tostring((struct sockaddr *) &remote_addr, addr_s);
    printf("connected with %s\n", addr_s);
    handle_client(csock);
}

variable_types: csock int


main

canonical_name: main

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

if ( argc <= 1 )
{
puts("expected a port number");
}
else
{
v3 = atoi(argv[1]);
v4 = create_socket(v3);
handle_incoming(v4);
close(v4);
}
return 0;
}

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

if ( argc <= 1 )
{
puts("expected a port number");
}
else
{
v3 = atoi(argv[1]);
v4 = func2(v3);
func16(v4);
close(v4);
}
return 0;
}

original_code:
int main(int argc, char *argv[]) {
    if (argc < 2) {
        printf("expected a port number\n");
        return 0;
    }
    uint16_t port = (uint16_t) atoi(argv[1]);
    int sock = create_socket(port);
    handle_incoming(sock);
    close(sock);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    if (argc < 2) {
        printf("expected a port number\n");
        return 0;
    }
    unsigned short port = (uint16_t) atoi(argv[1]);
    int sock = create_socket(port);
    handle_incoming(sock);
    close(sock);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: sock int


decompiled_code:
__int64 __fastcall calculate_addendum_size(__int64 a1)
{
return (unsigned int)((((8 * a1 + 65) & 0xFFFFFFFFFFFFFE00LL) + 512) >> 3) - (unsigned int)a1;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
return (unsigned int)((((8 * a1 + 65) & 0xFFFFFFFFFFFFFE00LL) + 512) >> 3) - (unsigned int)a1;
}

original_code:
static int calculate_addendum_size(unsigned long message_size) {
    unsigned long original_size_in_bits = 8 * message_size;
    unsigned long total_size_in_bits = original_size_in_bits + 1 + 64;
    total_size_in_bits += 8 * 64 - total_size_in_bits % (8 * 64);
    return total_size_in_bits / 8 - message_size;
}


decompiled_code:
unsigned __int64 __fastcall fill_addendum(_BYTE *a1, int a2, int a3)
{
__int64 v3; // r12

v3 = a3;
memset(a1, 0, a3);
*a1 = 0x80;
return write_int((__int64)&a1[v3 - 8], 8 * a2, 8);
}

original_code:
static void fill_addendum(unsigned char *buffer, int message_size, int addendum_size) {
    memset(buffer, 0, addendum_size);
    buffer[0] = (unsigned char) 0x80;
    write_int(buffer + addendum_size - 8, 8 * message_size, 8);
}

variable_types:
buffer
unsigned char *


original_code:
static unsigned char *format_hash(struct h *h) {
    unsigned char *result = (unsigned char *) malloc(20);
    write_int(result + 0, h->h0, 4);
    write_int(result + 4, h->h1, 4);
    write_int(result + 8, h->h2, 4);
    write_int(result + 12, h->h3, 4);
    write_int(result + 16, h->h4, 4);
    return result;
}

variable_types: struct h { unsigned long h0; unsigned long h1; unsigned long h2; unsigned long h3; unsigned long h4; } *

variable_types:
result
unsigned char *

return_type: unsigned char *


original_code:
unsigned char *sha1(unsigned char *message, unsigned long message_size) {
    struct h h;
    initialise_h(&h);
    unsigned long chunk_count = message_size / 64;
    for (unsigned long i = 0; i < chunk_count; i++) {
        process_chunk(&h, message + i * 64);
    }
    unsigned char last_chunks[2 * 64];
    int remaining_size = message_size % 64;
    int addendum_size = calculate_addendum_size(message_size);
    if (remaining_size > 0) {
        memcpy(last_chunks, message + chunk_count * 64, remaining_size);
    }
    fill_addendum(last_chunks + remaining_size, message_size, addendum_size);
    process_chunk(&h, last_chunks);
    if (remaining_size + addendum_size > 64) {
        process_chunk(&h, last_chunks + 64);
    }
    return format_hash(&h);
}

canonical_original_code:
unsigned char *sha1(unsigned char *message, unsigned long message_size) {
    struct h h;
    initialise_h(&h);
    unsigned long chunk_count = message_size / 64;
    for (unsigned long i = 0; i < chunk_count; i++) {
        process_chunk(&h, message + i * 64);
    }
    unsigned char last_chunks[-1];
    int remaining_size = message_size % 64;
    int addendum_size = calculate_addendum_size(message_size);
    if (remaining_size > 0) {
        memcpy(last_chunks, message + chunk_count * 64, remaining_size);
    }
    fill_addendum(last_chunks + remaining_size, message_size, addendum_size);
    process_chunk(&h, last_chunks);
    if (remaining_size + addendum_size > 64) {
        process_chunk(&h, last_chunks + 64);
    }
    return format_hash(&h);
}

variable_types:
message
unsigned char *

variable_types: struct h { unsigned long h0; unsigned long h1; unsigned long h2; unsigned long h3; unsigned long h4; }

variable_types:
i
unsigned long


decompiled_code:
__int64 __fastcall decode_char(char a1)
{
__int64 result; // rax

switch ( a1 )
{
case '+':
result = 62LL;
break;
case '/':
result = 63LL;
break;
case '0':
result = 52LL;
break;
case '1':
result = 53LL;
break;
case '2':
result = 54LL;
break;
case '3':
result = 55LL;
break;
case '4':
result = 56LL;
break;
case '5':
result = 57LL;
break;
case '6':
result = 58LL;
break;
case '7':
result = 59LL;
break;
case '8':
result = 60LL;
break;
case '9':
result = 61LL;
break;
case 'A':
result = 0LL;
break;
case 'B':
result = 1LL;
break;
case 'C':
result = 2LL;
break;
case 'D':
result = 3LL;
break;
case 'E':
result = 4LL;
break;
case 'F':
result = 5LL;
break;
case 'G':
result = 6LL;
break;
case 'H':
result = 7LL;
break;
case 'I':
result = 8LL;
break;
case 'J':
result = 9LL;
break;
case 'K':
result = 10LL;
break;
case 'L':
result = 11LL;
break;
case 'M':
result = 12LL;
break;
case 'N':
result = 13LL;
break;
case 'O':
result = 14LL;
break;
case 'P':
result = 15LL;
break;
case 'Q':
result = 16LL;
break;
case 'R':
result = 17LL;
break;
case 'S':
result = 18LL;
break;
case 'T':
result = 19LL;
break;
case 'U':
result = 20LL;
break;
case 'V':
result = 21LL;
break;
case 'W':
result = 22LL;
break;
case 'X':
result = 23LL;
break;
case 'Y':
result = 24LL;
break;
case 'Z':
result = 25LL;
break;
case 'a':
result = 26LL;
break;
case 'b':
result = 27LL;
break;
case 'c':
result = 28LL;
break;
case 'd':
result = 29LL;
break;
case 'e':
result = 30LL;
break;
case 'f':
result = 31LL;
break;
case 'g':
result = 32LL;
break;
case 'h':
result = 33LL;
break;
case 'i':
result = 34LL;
break;
case 'j':
result = 35LL;
break;
case 'k':
result = 36LL;
break;
case 'l':
result = 37LL;
break;
case 'm':
result = 38LL;
break;
case 'n':
result = 39LL;
break;
case 'o':
result = 40LL;
break;
case 'p':
result = 41LL;
break;
case 'q':
result = 42LL;
break;
case 'r':
result = 43LL;
break;
case 's':
result = 44LL;
break;
case 't':
result = 45LL;
break;
case 'u':
result = 46LL;
break;
case 'v':
result = 47LL;
break;
case 'w':
result = 48LL;
break;
case 'x':
result = 49LL;
break;
case 'y':
result = 50LL;
break;
case 'z':
result = 51LL;
break;
default:
result = 64LL;
break;
}
return result;
}

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

switch ( a1 )
{
case '+':
result = 62LL;
break;
case '/':
result = 63LL;
break;
case '0':
result = 52LL;
break;
case '1':
result = 53LL;
break;
case '2':
result = 54LL;
break;
case '3':
result = 55LL;
break;
case '4':
result = 56LL;
break;
case '5':
result = 57LL;
break;
case '6':
result = 58LL;
break;
case '7':
result = 59LL;
break;
case '8':
result = 60LL;
break;
case '9':
result = 61LL;
break;
case 'A':
result = 0LL;
break;
case 'B':
result = 1LL;
break;
case 'C':
result = 2LL;
break;
case 'D':
result = 3LL;
break;
case 'E':
result = 4LL;
break;
case 'F':
result = 5LL;
break;
case 'G':
result = 6LL;
break;
case 'H':
result = 7LL;
break;
case 'I':
result = 8LL;
break;
case 'J':
result = 9LL;
break;
case 'K':
result = 10LL;
break;
case 'L':
result = 11LL;
break;
case 'M':
result = 12LL;
break;
case 'N':
result = 13LL;
break;
case 'O':
result = 14LL;
break;
case 'P':
result = 15LL;
break;
case 'Q':
result = 16LL;
break;
case 'R':
result = 17LL;
break;
case 'S':
result = 18LL;
break;
case 'T':
result = 19LL;
break;
case 'U':
result = 20LL;
break;
case 'V':
result = 21LL;
break;
case 'W':
result = 22LL;
break;
case 'X':
result = 23LL;
break;
case 'Y':
result = 24LL;
break;
case 'Z':
result = 25LL;
break;
case 'a':
result = 26LL;
break;
case 'b':
result = 27LL;
break;
case 'c':
result = 28LL;
break;
case 'd':
result = 29LL;
break;
case 'e':
result = 30LL;
break;
case 'f':
result = 31LL;
break;
case 'g':
result = 32LL;
break;
case 'h':
result = 33LL;
break;
case 'i':
result = 34LL;
break;
case 'j':
result = 35LL;
break;
case 'k':
result = 36LL;
break;
case 'l':
result = 37LL;
break;
case 'm':
result = 38LL;
break;
case 'n':
result = 39LL;
break;
case 'o':
result = 40LL;
break;
case 'p':
result = 41LL;
break;
case 'q':
result = 42LL;
break;
case 'r':
result = 43LL;
break;
case 's':
result = 44LL;
break;
case 't':
result = 45LL;
break;
case 'u':
result = 46LL;
break;
case 'v':
result = 47LL;
break;
case 'w':
result = 48LL;
break;
case 'x':
result = 49LL;
break;
case 'y':
result = 50LL;
break;
case 'z':
result = 51LL;
break;
default:
result = 64LL;
break;
}
return result;
}

original_code:
static unsigned char decode_char(char ch) {
    switch (ch) {
    case 'A': return 0;
    case 'B': return 1;
    case 'C': return 2;
    case 'D': return 3;
    case 'E': return 4;
    case 'F': return 5;
    case 'G': return 6;
    case 'H': return 7;
    case 'I': return 8;
    case 'J': return 9;
    case 'K': return 10;
    case 'L': return 11;
    case 'M': return 12;
    case 'N': return 13;
    case 'O': return 14;
    case 'P': return 15;
    case 'Q': return 16;
    case 'R': return 17;
    case 'S': return 18;
    case 'T': return 19;
    case 'U': return 20;
    case 'V': return 21;
    case 'W': return 22;
    case 'X': return 23;
    case 'Y': return 24;
    case 'Z': return 25;
    case 'a': return 26;
    case 'b': return 27;
    case 'c': return 28;
    case 'd': return 29;
    case 'e': return 30;
    case 'f': return 31;
    case 'g': return 32;
    case 'h': return 33;
    case 'i': return 34;
    case 'j': return 35;
    case 'k': return 36;
    case 'l': return 37;
    case 'm': return 38;
    case 'n': return 39;
    case 'o': return 40;
    case 'p': return 41;
    case 'q': return 42;
    case 'r': return 43;
    case 's': return 44;
    case 't': return 45;
    case 'u': return 46;
    case 'v': return 47;
    case 'w': return 48;
    case 'x': return 49;
    case 'y': return 50;
    case 'z': return 51;
    case '0': return 52;
    case '1': return 53;
    case '2': return 54;
    case '3': return 55;
    case '4': return 56;
    case '5': return 57;
    case '6': return 58;
    case '7': return 59;
    case '8': return 60;
    case '9': return 61;
    case '+': return 62;
    case '/': return 63;
    }
    return 64;
}

variable_types: ch char

return_type: unsigned char


decompiled_code:
__int64 __fastcall decode_chunk(char *a1, _BYTE *a2)
{
char *v2; // rbx
int v3; // er13
unsigned __int8 v4; // al

v2 = a1;
v3 = 0;
while ( 1 )
{
v4 = decode_char(*v2);
if ( v4 > 0x3Fu )
break;
v3 = v4 | (v3 << 6);
if ( ++v2 == a1 + 4 )
{
*a2 = BYTE2(v3);
a2[1] = BYTE1(v3);
a2[2] = v3;
return 1LL;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *a1, _BYTE *a2)
{
char *v2; // rbx
int v3; // er13
unsigned __int8 v4; // al

v2 = a1;
v3 = 0;
while ( 1 )
{
v4 = func7(*v2);
if ( v4 > 0x3Fu )
break;
v3 = v4 | (v3 << 6);
if ( ++v2 == a1 + 4 )
{
*a2 = BYTE2(v3);
a2[1] = BYTE1(v3);
a2[2] = v3;
return 1LL;
}
}
return 0LL;
}

original_code:
static int decode_chunk(char *chunk, unsigned char *decoded) {
    unsigned long buffer = 0;
    for (int i = 0; i < 4; i++) {
        unsigned char value = decode_char(chunk[i]);
        if (value >= 64) {
            return 0;
        }
        buffer <<= 6;
        buffer |= value;
    }
    decoded[0] = (buffer >> 16) & 0xff;
    decoded[1] = (buffer >> 8) & 0xff;
    decoded[2] = buffer & 0xff;
    return 1;
}

variable_types:
chunk
char *

variable_types:
decoded
unsigned char *

variable_types:
buffer
unsigned long

variable_types: i int

variable_types:
value
unsigned char


decompiled_code:
__int64 __fastcall decode_padded_chunk(__int64 a1, _BYTE *a2)
{
__int64 v3; // rbx
int v4; // ebp
int v5; // edx
char v6; // di
unsigned __int8 v7; // al
__int64 result; // rax
__int64 v9; // rax

v3 = 0LL;
v4 = 0;
while ( 1 )
{
v5 = v3;
v6 = *(_BYTE *)(a1 + v3);
if ( v6 == 61 )
{
if ( (int)v3 > 3 )
{
LABEL_5:
*a2 = BYTE2(v4);
if ( v5 <= 2 )
{
if ( v5 != 2 )
return 1LL;
goto LABEL_20;
}
}
else
{
result = 0LL;
if ( (int)v3 <= 1 )
return result;
if ( (_DWORD)v3 == 2 )
{
if ( (v4 & 0xF) != 0 )
return result;
LODWORD(v9) = 2;
}
else
{
result = 0LL;
if ( (v4 & 3) != 0 )
return result;
LODWORD(v9) = 3;
}
v9 = (int)v9;
do
{
if ( *(_BYTE *)(a1 + v9) != 61 )
return 0LL;
v4 <<= 6;
++v9;
}
while ( (int)v9 <= 3 );
*a2 = BYTE2(v4);
if ( (_DWORD)v3 != 3 )
goto LABEL_20;
}
a2[1] = BYTE1(v4);
LABEL_20:
a2[2] = v4;
return 1LL;
}
v7 = decode_char(v6);
if ( v7 > 0x3Fu )
return 0LL;
v4 = v7 | (v4 << 6);
v5 = ++v3;
if ( v3 == 4 )
goto LABEL_5;
}
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, _BYTE *a2)
{
__int64 v3; // rbx
int v4; // ebp
int v5; // edx
char v6; // di
unsigned __int8 v7; // al
__int64 result; // rax
__int64 v9; // rax

v3 = 0LL;
v4 = 0;
while ( 1 )
{
v5 = v3;
v6 = *(_BYTE *)(a1 + v3);
if ( v6 == 61 )
{
if ( (int)v3 > 3 )
{
LABEL_5:
*a2 = BYTE2(v4);
if ( v5 <= 2 )
{
if ( v5 != 2 )
return 1LL;
goto LABEL_20;
}
}
else
{
result = 0LL;
if ( (int)v3 <= 1 )
return result;
if ( (_DWORD)v3 == 2 )
{
if ( (v4 & 0xF) != 0 )
return result;
LODWORD(v9) = 2;
}
else
{
result = 0LL;
if ( (v4 & 3) != 0 )
return result;
LODWORD(v9) = 3;
}
v9 = (int)v9;
do
{
if ( *(_BYTE *)(a1 + v9) != 61 )
return 0LL;
v4 <<= 6;
++v9;
}
while ( (int)v9 <= 3 );
*a2 = BYTE2(v4);
if ( (_DWORD)v3 != 3 )
goto LABEL_20;
}
a2[1] = BYTE1(v4);
LABEL_20:
a2[2] = v4;
return 1LL;
}
v7 = func7(v6);
if ( v7 > 0x3Fu )
return 0LL;
v4 = v7 | (v4 << 6);
v5 = ++v3;
if ( v3 == 4 )
goto LABEL_5;
}
}

original_code:
static int decode_padded_chunk(char *chunk, unsigned char *decoded) {
    unsigned long buffer = 0;
    int i;
    for (i = 0; i < 4; i++) {
        if (chunk[i] == PAD) {
            break;
        }
        unsigned char value = decode_char(chunk[i]);
        if (value >= 64) {
            return 0;
        }
        buffer <<= 6;
        buffer |= value;
    }
    if (i < 4) {
        if (i < 2
            || (i == 2 && (buffer & 0x0f) != 0)
            || (i == 3 && (buffer & 0x03) != 0)) {
            return 0;
        }
        for (int j = i; j < 4; j++) {
            if (chunk[j] != PAD) {
                return 0;
            }
            buffer <<= 6;
        }
    }
    decoded[0] = (buffer >> 16) & 0xff;
    if (i >= 3) {
        decoded[1] = (buffer >> 8) & 0xff;
    }
    if (i >= 2) {
        decoded[2] = buffer & 0xff;
    }
    return 1;
}

variable_types: j int


repo: frantony/canon-led-blink-test

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

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

original_code:
uint32_t checksumm_init()
{
 return 0;
}

canonical_original_code:
unsigned int checksumm_init()
{
 return 0;
}

return_type: unsigned int


decompiled_code:
__int64 __fastcall checksumm_update(unsigned int a1, __int64 a2, int a3)
{
__int64 v3; // rax

if ( a3 > 1 )
{
v3 = 0LL;
do
a1 += *(unsigned __int16 *)(a2 + 2 * v3++);
while ( a3 / 2 > (int)v3 );
}
if ( (a3 & 1) != 0 )
a1 += *(char *)(a2 + a3 - 1);
return a1;
}

original_code:
uint32_t checksumm_update(uint32_t cs, char *buf, int len)
{
 int i;
 for (i=0;i<len/2;i++)
  cs+=((unsigned short*)buf)[i];
 if (len&1)
  cs+=buf[len-1];
 return cs;
}

canonical_original_code:
unsigned int checksumm_update(unsigned int cs, char *buf, int len)
{
 int i;
 for (i=0;i<len/2;i++)
  cs+=((unsigned short *)buf)[i];
 if (len&1)
  cs+=buf[len-1];
 return cs;
}

variable_types:
cs
unsigned int

variable_types:
buf
char *

variable_types: len int


usage

decompiled_code:
void __noreturn usage()
{
puts("pakfir <out_fir_file> <in_wif_file> <camera_id> [version]");
exit(1);
}

canonical_decompiled_code:
void __noreturn func14()
{
puts("pakfir <out_fir_file> <in_wif_file> <camera_id> [version]");
exit(1);
}

original_code:
int usage()
{
 printf("pakfir <out_fir_file> <in_wif_file> <camera_id> [version]\n");
 exit(1);
}


dance

decompiled_code:
char __fastcall dance(char a1, int a2)
{
char result; // al

if ( a2 != 3 * (a2 / 3) )
return ~a1;
result = __ROL1__(a1, 4);
if ( (a2 & 1) == 0 )
result = a1 ^ 0xA0;
return result;
}

canonical_decompiled_code:
char __fastcall func7(char a1, int a2)
{
char result; // al

if ( a2 != 3 * (a2 / 3) )
return ~a1;
result = __ROL1__(a1, 4);
if ( (a2 & 1) == 0 )
result = a1 ^ 0xA0;
return result;
}

original_code:
unsigned char dance(unsigned char allbest, int fudgey) {
 if ((fudgey % 3) !=0)
  return allbest ^ 0xff;
 if ((fudgey & 1) == 0)
  return allbest ^ 0xa0;
 return (allbest >> 4) | (allbest << 4);
}

variable_types:
allbest
unsigned char

variable_types: fudgey int


repo: frasanz/MmMgrid

original_code:
void initialize_sub_mesh(double ** sub_mesh, int number_nodes_base, double value)
{
 int i,j;
 for(i=0;i<number_nodes_base;i++){
  for(j=0;j<=i;j++){
   if(value==42.0)
    sub_mesh[i][j]=1.0*rand();
   else if(value==43.0)
    sub_mesh[i][j]=1.0+1.0*sin(0.01*i*j);
   else if(value==44.0)
    sub_mesh[i][j]=j;
   else
    sub_mesh[i][j]=value;
  }
 }
}

canonical_original_code:
void initialize_sub_mesh(double **sub_mesh, int number_nodes_base, double value)
{
 int i; int j;
 for(i=0;i<number_nodes_base;i++){
  for(j=0;j<=i;j++){
   if(value==42.0)
    sub_mesh[i][j]=1.0*rand();
   else if(value==43.0)
    sub_mesh[i][j]=1.0+1.0*sin(0.01*i*j);
   else if(value==44.0)
    sub_mesh[i][j]=j;
   else
    sub_mesh[i][j]=value;
  }
 }
}

variable_types: value double


decompiled_code:
void __fastcall bound_sub_mesh(__int64 a1, int a2, double a3)
{
__int64 v3; // rax

if ( a2 > 0 )
{
v3 = 0LL;
do
{
*(double *)(*(_QWORD *)(a1 + v3) + v3) = a3;
**(double **)(a1 + v3) = a3;
*(double *)(*(_QWORD *)(a1 + 8LL * a2 - 8) + v3) = a3;
v3 += 8LL;
}
while ( v3 != 8LL * (unsigned int)a2 );
}
}

original_code:
void bound_sub_mesh(double ** sub_mesh, int number_nodes_base, double value)
{
 int i;
 for(i=0;i<number_nodes_base;i++){
  sub_mesh[i][i]=value;
  sub_mesh[i][0]=value;
  sub_mesh[number_nodes_base-1][i]=value;
 }
}

canonical_original_code:
void bound_sub_mesh(double **sub_mesh, int number_nodes_base, double value)
{
 int i;
 for(i=0;i<number_nodes_base;i++){
  sub_mesh[i][i]=value;
  sub_mesh[i][0]=value;
  sub_mesh[number_nodes_base-1][i]=value;
 }
}


decompiled_code:
int __fastcall show_sub_mesh(_QWORD *a1, int a2)
{
int i; // ebp
__int64 j; // rbx
int result; // eax

if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
{
for ( j = 0LL; i >= (int)j; ++j )
printf("%1.1e ", *(double *)(*a1 + 8 * j));
result = putchar(10);
++a1;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(_QWORD *a1, int a2)
{
int i; // ebp
__int64 j; // rbx
int result; // eax

if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
{
for ( j = 0LL; i >= (int)j; ++j )
printf("%1.1e ", *(double *)(*a1 + 8 * j));
result = putchar(10);
++a1;
}
}
return result;
}

original_code:
void show_sub_mesh(double ** sub_mesh, int number_nodes_base)
{
 int i,j;
 for(i=0;i<number_nodes_base;i++){
  for(j=0;j<=i;j++){
   printf("%1.1e ",sub_mesh[i][j]);
  }
  printf("\n");
 }
}

canonical_original_code:
void show_sub_mesh(double **sub_mesh, int number_nodes_base)
{
 int i; int j;
 for(i=0;i<number_nodes_base;i++){
  for(j=0;j<=i;j++){
   printf("%1.1e ",sub_mesh[i][j]);
  }
  printf("\n");
 }
}


decompiled_code:
int __fastcall show_structure(unsigned int *a1, unsigned int *a2, int a3, int a4)
{
unsigned int i; // er12
int result; // eax
unsigned int v10; // ebp

if ( a3 > 0 )
{
for ( i = 0; i != a3; ++i )
{
result = printf("\t[INFO] Element %d[%d] has edges %d %d %d\n", i, *a1, a1[4], a1[5], a1[6]);
a1 += 24;
}
}
if ( a4 > 0 )
{
v10 = 0;
do
{
result = printf(
"\t[INFO] Edge %d[%d] is used by elements %d %d and has vertex %d %d\n",
v10++,
*a2,
a2[3],
a2[4],
a2[1],
a2[2]);
a2 += 8;
}
while ( a4 != v10 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(unsigned int *a1, unsigned int *a2, int a3, int a4)
{
unsigned int i; // er12
int result; // eax
unsigned int v10; // ebp

if ( a3 > 0 )
{
for ( i = 0; i != a3; ++i )
{
result = printf("\t[INFO] Element %d[%d] has edges %d %d %d\n", i, *a1, a1[4], a1[5], a1[6]);
a1 += 24;
}
}
if ( a4 > 0 )
{
v10 = 0;
do
{
result = printf(
"\t[INFO] Edge %d[%d] is used by elements %d %d and has vertex %d %d\n",
v10++,
*a2,
a2[3],
a2[4],
a2[1],
a2[2]);
a2 += 8;
}
while ( a4 != v10 );
}
return result;
}

original_code:
void show_structure(Element * element, Edge * edge, int number_element, int number_edges)
{
 int i;
 for(i=0;i<number_element;i++){
  printf("\t[INFO] Element %d[%d]  has edges %d %d %d\n",i,element[i].id,element[i].edge[0],element[i].edge[1],element[i].edge[2]);
 }
 for(i=0;i<number_edges;i++){
  printf("\t[INFO] Edge %d[%d] is used by elements %d %d and has vertex %d %d\n",i,edge[i].id,edge[i].element[0],edge[i].element[1],edge[i].node[0],edge[i].node[1]);
 }
}

canonical_original_code:
void show_structure(struct Element *element, struct Edge *edge, int number_element, int number_edges)
{
 int i;
 for(i=0;i<number_element;i++){
  printf("\t[INFO] Element %d[%d]  has edges %d %d %d\n",i,element[i].id,element[i].edge[0],element[i].edge[1],element[i].edge[2]);
 }
 for(i=0;i<number_edges;i++){
  printf("\t[INFO] Edge %d[%d] is used by elements %d %d and has vertex %d %d\n",i,edge[i].id,edge[i].element[0],edge[i].element[1],edge[i].node[0],edge[i].node[1]);
 }
}

variable_types: struct Element { int id; int node[3]; int edge[3]; int nei[3][3]; int n_levels; struct Operator { double v[3][3]; } *operator; struct Mesh { double **u; double **u_gh; double **v; double **v_gh; double **d; double **d_gh; double **f; double **f_gh; int number_nodes; int number_nodes_base; } *mesh; double max_error; } *

variable_types: struct Edge { int id; int node[2]; int element[2]; int n_levels; struct One_edge { double **u; double **v; double **d; double **f; int number_nodes_base; } *oe; } *

user_defined_types: struct Element { int id; int node[3]; int edge[3]; int nei[3][3]; int n_levels; struct Operator *operator; struct Mesh *mesh; double max_error; }

user_defined_types: struct Edge { int id; int node[2]; int element[2]; int n_levels; struct One_edge *oe; }


decompiled_code:
int __fastcall show_structure_elements(unsigned int *a1, int a2)
{
unsigned int i; // ebp
int result; // eax

if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
{
result = printf(
"\t[INFO] Element %d[%d] has neighbor the elements %d(%d %d) %d(%d %d) %d(%d %d)\n",
i,
*a1,
a1[7],
a1[8],
a1[9],
a1[10],
a1[11],
a1[12],
a1[13],
a1[14],
a1[15]);
a1 += 24;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(unsigned int *a1, int a2)
{
unsigned int i; // ebp
int result; // eax

if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
{
result = printf(
"\t[INFO] Element %d[%d] has neighbor the elements %d(%d %d) %d(%d %d) %d(%d %d)\n",
i,
*a1,
a1[7],
a1[8],
a1[9],
a1[10],
a1[11],
a1[12],
a1[13],
a1[14],
a1[15]);
a1 += 24;
}
}
return result;
}

original_code:
void show_structure_elements(Element * element, int number_element)
{
 int i;
 for(i=0;i<number_element;i++){
  printf("\t[INFO] Element %d[%d]  has neighbor the elements  %d(%d %d) %d(%d %d) %d(%d %d)\n",i,element[i].id,
  element[i].nei[0][0],element[i].nei[0][1],element[i].nei[0][2],
  element[i].nei[1][0],element[i].nei[1][1],element[i].nei[1][2],
  element[i].nei[2][0],element[i].nei[2][1],element[i].nei[2][2]);
 }
}

canonical_original_code:
void show_structure_elements(struct Element *element, int number_element)
{
 int i;
 for(i=0;i<number_element;i++){
  printf("\t[INFO] Element %d[%d]  has neighbor the elements  %d(%d %d) %d(%d %d) %d(%d %d)\n",i,element[i].id,
  element[i].nei[0][0],element[i].nei[0][1],element[i].nei[0][2],
  element[i].nei[1][0],element[i].nei[1][1],element[i].nei[1][2],
  element[i].nei[2][0],element[i].nei[2][1],element[i].nei[2][2]);
 }
}

user_defined_types: struct Mesh { double **u; double **u_gh; double **v; double **v_gh; double **d; double **d_gh; double **f; double **f_gh; int number_nodes; int number_nodes_base; }


decompiled_code:
void *__fastcall allocate1D(int a1)
{
void *result; // rax

result = malloc(8LL * a1);
if ( !result )
{
fwrite("\n Fallo en malloc (allocate1D)\n ", 1uLL, 0x20uLL, stderr);
exit(1);
}
return result;
}

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

result = malloc(8LL * a1);
if ( !result )
{
fwrite("\n Fallo en malloc (allocate1D)\n ", 1uLL, 0x20uLL, stderr);
exit(1);
}
return result;
}

original_code:
double *
 allocate1D(int dim1)
{
 double * vector;
 vector = (double *)malloc(dim1*sizeof(double));
 if(vector==((void *)0)){
  fprintf(stderr,"\n Fallo en malloc (allocate1D)\n ");
  exit(1);
 }
 return vector;
}

canonical_original_code:
double *allocate1D(int dim1)
{
 double *vector;
 vector = (double *)malloc(dim1*sizeof(double));
 if(vector==((void *)0)){
  fprintf(stderr,"\n Fallo en malloc (allocate1D)\n ");
  exit(1);
 }
 return vector;
}

variable_types:
vector
double *

return_type: double *


decompiled_code:
void __fastcall deallocate3D(void *ptr, int a2, int a3)
{
void **v4; // rbp
__int64 v5; // r12
__int64 v6; // rbx

if ( a2 > 0 )
{
v4 = (void **)ptr;
v5 = 8LL * (unsigned int)(a3 - 1) + 8;
do
{
v6 = 0LL;
if ( a3 > 0 )
{
do
{
free(*(void **)((char *)*v4 + v6));
v6 += 8LL;
}
while ( v6 != v5 );
}
free(*v4++);
}
while ( v4 != (void **)((char *)ptr + 8 * (unsigned int)(a2 - 1) + 8) );
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr, int a2, int a3)
{
void **v4; // rbp
__int64 v5; // r12
__int64 v6; // rbx

if ( a2 > 0 )
{
v4 = (void **)ptr;
v5 = 8LL * (unsigned int)(a3 - 1) + 8;
do
{
v6 = 0LL;
if ( a3 > 0 )
{
do
{
free(*(void **)((char *)*v4 + v6));
v6 += 8LL;
}
while ( v6 != v5 );
}
free(*v4++);
}
while ( v4 != (void **)((char *)ptr + 8 * (unsigned int)(a2 - 1) + 8) );
}
free(ptr);
}

original_code:
void
 deallocate3D(double ***matriz,
        int dim1,
        int dim2)
{
 int i,j;
 for(i=0;i<dim1;i++)
 {
  for(j=0;j<dim2;j++)
   free(matriz[i][j]);
  free(matriz[i]);
 }
 free(matriz);
}

canonical_original_code:
void deallocate3D(double ***matriz, int dim1, int dim2)
{
 int i; int j;
 for(i=0;i<dim1;i++)
 {
  for(j=0;j<dim2;j++)
   free(matriz[i][j]);
  free(matriz[i]);
 }
 free(matriz);
}

variable_types:
matriz
double ***


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

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

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

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

original_code:
void deallocate2D (double **matriz, int dim1)
{
 int i;
 for(i=0;i<dim1;i++)
  free(matriz[i]);
 free(matriz);
}

canonical_original_code:
void deallocate2D(double **matriz, int dim1)
{
 int i;
 for(i=0;i<dim1;i++)
  free(matriz[i]);
 free(matriz);
}

variable_types:
matriz
double **


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

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

original_code:
void deallocate1D(double * vector)
{
 free(vector);
}

canonical_original_code:
void deallocate1D(double *vector)
{
 free(vector);
}


inicializa

decompiled_code:
double __fastcall inicializa(__int64 a1, int a2)
{
int v2; // eax
_QWORD *v3; // r12
int v4; // er13
int v5; // er14
__int64 v6; // rbp
int v7; // ebx
double v8; // xmm0_8
double result; // xmm0_8

v2 = a2 - 1;
if ( a2 - 1 <= 0 )
return pow((double)v2, 2.0);
if ( v2 > 1 )
{
v3 = (_QWORD *)(a1 + 8);
v4 = a2 - 1;
v5 = 1;
do
{
v6 = 8LL;
v7 = 1;
do
{
v8 = cos((double)v7);
result = sin(v8 * (double)v5) + 10.1;
*(double *)(*v3 + v6) = result;
++v7;
v6 += 8LL;
}
while ( v7 != v4 );
++v5;
++v3;
}
while ( v5 != v4 );
}
return result;
}

canonical_decompiled_code:
double __fastcall func0(__int64 a1, int a2)
{
int v2; // eax
_QWORD *v3; // r12
int v4; // er13
int v5; // er14
__int64 v6; // rbp
int v7; // ebx
double v8; // xmm0_8
double result; // xmm0_8

v2 = a2 - 1;
if ( a2 - 1 <= 0 )
return pow((double)v2, 2.0);
if ( v2 > 1 )
{
v3 = (_QWORD *)(a1 + 8);
v4 = a2 - 1;
v5 = 1;
do
{
v6 = 8LL;
v7 = 1;
do
{
v8 = cos((double)v7);
result = sin(v8 * (double)v5) + 10.1;
*(double *)(*v3 + v6) = result;
++v7;
v6 += 8LL;
}
while ( v7 != v4 );
++v5;
++v3;
}
while ( v5 != v4 );
}
return result;
}

original_code:
void inicializa(double **f,int dimension)
{
 int i,j;
 int divisor=pow(dimension-1,2);
 for(i=1; i< dimension-1; i++)
 {
  for(j=1; j<dimension-1; j++)
  {
   f[i][j]=10.1+sin(i*cos(j));
  }
 }
}

canonical_original_code:
void inicializa(double **f, int dimension)
{
 int i; int j;
 int divisor = pow(dimension-1,2);
 for(i=1; i< dimension-1; i++)
 {
  for(j=1; j<dimension-1; j++)
  {
   f[i][j]=10.1+sin(i*cos(j));
  }
 }
}

variable_types:
f
double **

variable_types: dimension int

variable_types: divisor int


decompiled_code:
void __fastcall inicializa_cero(_QWORD *a1, int a2)
{
_QWORD *v2; // rcx
__int64 v3; // rdi
__int64 v4; // rsi
__int64 v5; // rax

if ( a2 > 0 )
{
v2 = a1;
v3 = (__int64)&a1[(unsigned int)(a2 - 1) + 1];
v4 = 8LL * (unsigned int)a2;
do
{
v5 = 0LL;
do
{
*(_QWORD *)(*v2 + v5) = 0LL;
v5 += 8LL;
}
while ( v4 != v5 );
++v2;
}
while ( v2 != (_QWORD *)v3 );
}
}

canonical_decompiled_code:
void __fastcall func1(_QWORD *a1, int a2)
{
_QWORD *v2; // rcx
__int64 v3; // rdi
__int64 v4; // rsi
__int64 v5; // rax

if ( a2 > 0 )
{
v2 = a1;
v3 = (__int64)&a1[(unsigned int)(a2 - 1) + 1];
v4 = 8LL * (unsigned int)a2;
do
{
v5 = 0LL;
do
{
*(_QWORD *)(*v2 + v5) = 0LL;
v5 += 8LL;
}
while ( v4 != v5 );
++v2;
}
while ( v2 != (_QWORD *)v3 );
}
}

original_code:
void inicializa_cero(double **matriz, int dimension)
{
 int i,j;
 for(i=0;i<dimension;i++)
  for(j=0;j<dimension;j++)
   matriz[i][j]=0.0;
}

canonical_original_code:
void inicializa_cero(double **matriz, int dimension)
{
 int i; int j;
 for(i=0;i<dimension;i++)
  for(j=0;j<dimension;j++)
   matriz[i][j]=0.0;
}

variable_types: i int


original_code:
void calcula_defecto(double **d, double **f, double **u, int dimension)
{
 int i,j;
 double h2=pow(1.0/(dimension-1),2);
 for(i=1;i<dimension-1;i++)
 {
  for(j=1;j<dimension-1;j++)
  {
   d[i][j]=f[i][j]-(4*u[i][j]-u[i-1][j]-u[i+1][j]-u[i][j-1]-u[i][j+1])/h2;
  }
 }
}

canonical_original_code:
void calcula_defecto(double **d, double **f, double **u, int dimension)
{
 int i; int j;
 double h2 = pow(1.0/(dimension-1),2);
 for(i=1;i<dimension-1;i++)
 {
  for(j=1;j<dimension-1;j++)
  {
   d[i][j]=f[i][j]-(4*u[i][j]-u[i-1][j]-u[i+1][j]-u[i][j-1]-u[i][j+1])/h2;
  }
 }
}

variable_types:
d
double **

variable_types:
u
double **


restringe

variable_types:
in
double **

variable_types:
fin
double **

variable_types: j int


interpola

original_code:
void interpola(double **in, double **fin, int dim_inicial,int dim_final)
{
 int i,j;
 for(i=0;i<dim_inicial;i++)
 {
  for(j=0;j<dim_inicial;j++)
  {
   fin[2*i][2*j]=in[i][j];
   if(2*i+1<dim_final)
    fin[2*i+1][2*j]=(in[i][j]+in[i+1][j])/2;
   if(2*j+1<dim_final)
    fin[2*i][2*j+1]=(in[i][j]+in[i][j+1])/2;
   if(2*i+1<dim_final && 2*j+1<dim_final)
    fin[2*i+1][2*j+1]=(in[i][j]+in[i][j+1]+in[i+1][j]+in[i+1][j+1])/4;
  }
 }
}

canonical_original_code:
void interpola(double **in, double **fin, int dim_inicial, int dim_final)
{
 int i; int j;
 for(i=0;i<dim_inicial;i++)
 {
  for(j=0;j<dim_inicial;j++)
  {
   fin[2*i][2*j]=in[i][j];
   if(2*i+1<dim_final)
    fin[2*i+1][2*j]=(in[i][j]+in[i+1][j])/2;
   if(2*j+1<dim_final)
    fin[2*i][2*j+1]=(in[i][j]+in[i][j+1])/2;
   if(2*i+1<dim_final && 2*j+1<dim_final)
    fin[2*i+1][2*j+1]=(in[i][j]+in[i][j+1]+in[i+1][j]+in[i+1][j+1])/4;
  }
 }
}


exacta

decompiled_code:
__int64 __fastcall exacta(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = *(_QWORD *)(a2 + 8);
*(double *)(result + 8) = 0.0625 * *(double *)(*(_QWORD *)(a1 + 8) + 8LL);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = *(_QWORD *)(a2 + 8);
*(double *)(result + 8) = 0.0625 * *(double *)(*(_QWORD *)(a1 + 8) + 8LL);
return result;
}

original_code:
void exacta(double **d, double **v)
{
 v[1][1]=d[1][1]/16;
}

variable_types:
v
double **


decompiled_code:
void __fastcall copia_matriz(_QWORD *a1, _QWORD *a2, int a3)
{
_QWORD *v3; // rcx
__int64 v4; // rax

if ( a3 > 0 )
{
v3 = a1;
do
{
v4 = 0LL;
do
{
*(_QWORD *)(*a2 + v4) = *(_QWORD *)(*v3 + v4);
v4 += 8LL;
}
while ( 8LL * (unsigned int)a3 != v4 );
++v3;
++a2;
}
while ( v3 != &a1[(unsigned int)(a3 - 1) + 1] );
}
}

canonical_decompiled_code:
void __fastcall func6(_QWORD *a1, _QWORD *a2, int a3)
{
_QWORD *v3; // rcx
__int64 v4; // rax

if ( a3 > 0 )
{
v3 = a1;
do
{
v4 = 0LL;
do
{
*(_QWORD *)(*a2 + v4) = *(_QWORD *)(*v3 + v4);
v4 += 8LL;
}
while ( 8LL * (unsigned int)a3 != v4 );
++v3;
++a2;
}
while ( v3 != &a1[(unsigned int)(a3 - 1) + 1] );
}
}

original_code:
void copia_matriz(double **d1, double **d2, int dim)
{
 int i,j;
 for(i=0;i<dim;i++)
 {
  for(j=0;j<dim;j++)
  {
   d2[i][j]=d1[i][j];
  }
 }
}

canonical_original_code:
void copia_matriz(double **d1, double **d2, int dim)
{
 int i; int j;
 for(i=0;i<dim;i++)
 {
  for(j=0;j<dim;j++)
  {
   d2[i][j]=d1[i][j];
  }
 }
}

variable_types: dim int


variable_types: id int

variable_types: struct Vertex { int id; double c[2]; } *

variable_types:
B
double [2][2]

variable_types:
C
double [2][2]

variable_types: det double

variable_types: k int

variable_types:
Sxx
double [3][3]

variable_types:
Sxy
double [3][3]

variable_types:
Syy
double [3][3]


decompiled_code:
int __fastcall show_operator(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, int a8, int a9, int a10, int a11, int a12, int a13, int a14, int a15, __int64 a16)
{
int result; // eax
__int64 v17; // r12
int i; // ebp
int j; // ebx

result = a15;
if ( a15 > 0 )
{
v17 = 0LL;
do
{
for ( i = 0; i != 3; ++i )
{
for ( j = 0; j != 3; ++j )
printf("%f ", *(double *)(a16 + 8 * (3LL * i + j) + v17));
putchar(10);
}
result = putchar(10);
v17 += 72LL;
}
while ( 72LL * (unsigned int)a15 != v17 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, int a8, int a9, int a10, int a11, int a12, int a13, int a14, int a15, __int64 a16)
{
int result; // eax
__int64 v17; // r12
int i; // ebp
int j; // ebx

result = a15;
if ( a15 > 0 )
{
v17 = 0LL;
do
{
for ( i = 0; i != 3; ++i )
{
for ( j = 0; j != 3; ++j )
printf("%f ", *(double *)(a16 + 8 * (3LL * i + j) + v17));
putchar(10);
}
result = putchar(10);
v17 += 72LL;
}
while ( 72LL * (unsigned int)a15 != v17 );
}
return result;
}

original_code:
void show_operator(Element element)
{
 int i,j,k;
 for(i=0;i<element.n_levels;i++){
  for(j=0;j<3;j++){
   for(k=0;k<3;k++)
    printf("%f ",element.operator[i].v[j][k]);
   printf("\n");
  }
  printf("\n");
 }
}

canonical_original_code:
void show_operator(struct Element element)
{
 int i; int j; int k;
 for(i=0;i<element.n_levels;i++){
  for(j=0;j<3;j++){
   for(k=0;k<3;k++)
    printf("%f ",element.operator[i].v[j][k]);
   printf("\n");
  }
  printf("\n");
 }
}

variable_types: struct Element { int id; int node[3]; int edge[3]; int nei[3][3]; int n_levels; struct Operator { double v[3][3]; } *operator; struct Mesh { double **u; double **u_gh; double **v; double **v_gh; double **d; double **d_gh; double **f; double **f_gh; int number_nodes; int number_nodes_base; } *mesh; double max_error; }

variable_types: i int


canonical_original_code:
void suaviza_gsrb(double **u, double **f, int iteraciones, int dim)
{
 int i; int j; int k; int l;
 double h2 = pow(1.0/(dim-1),2);
 for(k=0;k<iteraciones;k++)
 {
  for(l=0;l<2;l++)
  {
   for(i=1;i<dim-1;i++)
   {
    for(j=1+(i+l+1)%2;j<dim-1;j=j+2)
    {
     u[i][j]=0.25*(f[i][j]*h2+u[i-1][j]+u[i+1][j]+u[i][j-1]+u[i][j+1]);
    }
   }
  }
 }
}

variable_types:
u
double **

variable_types:
f
double **

variable_types: iteraciones int

variable_types: l int


suaviza

decompiled_code:
void __fastcall suaviza(_QWORD *a1, __int64 a2, int a3, const char *a4, int a5)
{
if ( strcmp(a4, "gsrb") )
{
puts("[Erro] suavizador no encontrado");
exit(-10);
}
suaviza_gsrb(a1, a2, a3, a5);
}

canonical_decompiled_code:
void __fastcall func1(_QWORD *a1, __int64 a2, int a3, const char *a4, int a5)
{
if ( strcmp(a4, "gsrb") )
{
puts("[Erro] suavizador no encontrado");
exit(-10);
}
func0(a1, a2, a3, a5);
}

original_code:
void suaviza(double **u, double **f, int iteraciones, const char *tipo, int dim)
{
 if(!strcmp(tipo,"gsrb"))
  suaviza_gsrb(u, f,iteraciones,dim);
 else
 {
  printf("[Erro] suavizador no encontrado\n");
  exit(-10);
 }
}

canonical_original_code:
void suaviza(double **u, double **f, int iteraciones, char *tipo, int dim)
{
 if(!strcmp(tipo,"gsrb"))
  suaviza_gsrb(u, f,iteraciones,dim);
 else
 {
  printf("[Erro] suavizador no encontrado\n");
  exit(-10);
 }
}

variable_types:
tipo
char *


decompiled_code:
int __fastcall muestra_matriz(const char *a1, _QWORD *a2, int a3)
{
int result; // eax
_QWORD *v5; // rbp
__int64 v6; // r13
__int64 v7; // r12
__int64 v8; // rbx

result = printf("%s=\n", a1);
if ( a3 > 0 )
{
v5 = a2;
v6 = (__int64)&a2[(unsigned int)(a3 - 1) + 1];
v7 = 8LL * (unsigned int)a3;
do
{
v8 = 0LL;
do
{
printf("%1.4f\t", *(double *)(*v5 + v8));
v8 += 8LL;
}
while ( v7 != v8 );
result = putchar(10);
++v5;
}
while ( v5 != (_QWORD *)v6 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(const char *a1, _QWORD *a2, int a3)
{
int result; // eax
_QWORD *v5; // rbp
__int64 v6; // r13
__int64 v7; // r12
__int64 v8; // rbx

result = printf("%s=\n", a1);
if ( a3 > 0 )
{
v5 = a2;
v6 = (__int64)&a2[(unsigned int)(a3 - 1) + 1];
v7 = 8LL * (unsigned int)a3;
do
{
v8 = 0LL;
do
{
printf("%1.4f\t", *(double *)(*v5 + v8));
v8 += 8LL;
}
while ( v7 != v8 );
result = putchar(10);
++v5;
}
while ( v5 != (_QWORD *)v6 );
}
return result;
}

original_code:
void muestra_matriz(char * nombre, double ** matriz, int dim1, int dim2)
{
 int i,j;
 printf("%s=\n",nombre);
 for(i=0;i<dim1;i++)
 {
  for(j=0;j<dim1;j++)
  {
   printf("%1.4f\t",matriz[i][j]);
  }
  printf("\n");
 }
}

canonical_original_code:
void muestra_matriz(char *nombre, double **matriz, int dim1, int dim2)
{
 int i; int j;
 printf("%s=\n",nombre);
 for(i=0;i<dim1;i++)
 {
  for(j=0;j<dim1;j++)
  {
   printf("%1.4f\t",matriz[i][j]);
  }
  printf("\n");
 }
}

variable_types:
nombre
char *

variable_types:
matriz
double **

variable_types: j int


decompiled_code:
void __fastcall multigrid_kernel(__int64 a1, int a2, int a3, int a4, int a5, int a6, unsigned int a7, int a8)
{
_QWORD *v9; // r12
int i; // ebp
int k; // ebp
__int64 v12; // r12
_QWORD *v13; // r12
unsigned int l; // ebp
__int64 v15; // r12
int m; // er13
__int64 v17; // rbp
int v18; // er12
int n; // er12
__int64 v20; // r13
__int64 v21; // rbp
unsigned int ii; // er12
int ll; // ebp
__int64 v24; // r12
_QWORD *v25; // rbp
__int64 v26; // rbp
int mm; // er12
_QWORD *v28; // r12
int jj; // ebp
_QWORD *v30; // r13
unsigned int v31; // er12
int v32; // eax
__int64 v33; // [rsp+8h] [rbp-70h]
unsigned int j; // [rsp+8h] [rbp-70h]
__int64 v35; // [rsp+8h] [rbp-70h]
__int64 v36; // [rsp+8h] [rbp-70h]
unsigned int kk; // [rsp+8h] [rbp-70h]
__int64 v38; // [rsp+18h] [rbp-60h]
unsigned int v42; // [rsp+34h] [rbp-44h]

if ( debug > 8 )
printf("\t\tIn level %d\n", a7);
if ( !a7 )
return;
v42 = a7 - 1;
if ( a8 > 0 )
{
v33 = 72LL * (int)a7;
v38 = v33 - 72;
v9 = (_QWORD *)(a1 + 80);
for ( i = 0; ; ++i )
{
if ( a3 == i || (int)a7 < a2 )
{
initialize_sub_mesh(*(_QWORD *)(*v9 + v33 + 32), *(unsigned int *)(*v9 + v33 + 68), 0.0);
initialize_sub_mesh(*(_QWORD *)(*v9 + v33 + 16), *(unsigned int *)(*v9 + v33 + 68), 0.0);
initialize_sub_mesh(*(_QWORD *)(*v9 + v38 + 48), *(unsigned int *)(*v9 + v38 + 68), 0.0);
initialize_sub_mesh(*(_QWORD *)(*v9 + v38), *(unsigned int *)(*v9 + v38 + 68), 0.0);
}
v9 += 12;
if ( a8 == i + 1 )
break;
}
v30 = (_QWORD *)(a1 + 80);
v31 = 0;
if ( debug > 10 )
{
do
{
printf("Element %d: u\n", v31);
show_sub_mesh(*(_QWORD *)(*v30 + 72LL * (int)a7), *(unsigned int *)(*v30 + 72LL * (int)a7 + 68));
v32 = v31++;
v30 += 12;
}
while ( i != v32 );
}
}
for ( j = 0; j != 3; ++j )
{
v12 = a1;
u_to_ghost(a1, a7, (unsigned int)a8);
if ( a8 > 0 )
{
for ( k = 0; k != a8; ++k )
{
if ( a3 == k || (int)a7 < a2 )
smooth_rgb(a7, j);
v12 += 96LL;
}
}
}
if ( debug > 10 )
{
if ( a8 <= 0 )
{
u_to_ghost(a1, a7, (unsigned int)a8);
if ( debug <= 10 )
{
LABEL_87:
multigrid_kernel(a1, a2, a3, a4, a5, a6, v42, a8);
if ( debug <= 8 )
goto LABEL_80;
goto LABEL_88;
}
}
else
{
v13 = (_QWORD *)(a1 + 80);
for ( l = 0; l != a8; ++l )
{
printf("Element %d: u\n", l);
show_sub_mesh(*(_QWORD *)(*v13 + 72LL * (int)a7), *(unsigned int *)(*v13 + 72LL * (int)a7 + 68));
v13 += 12;
}
u_to_ghost(a1, a7, l);
if ( debug <= 10 )
goto LABEL_28;
}
goto LABEL_25;
}
u_to_ghost(a1, a7, (unsigned int)a8);
if ( debug > 10 )
LABEL_25:
print_ghost(a1, (unsigned int)a8, a7);
if ( a8 <= 0 )
goto LABEL_87;
LABEL_28:
v35 = 72LL * (int)a7;
v15 = a1;
for ( m = 0; m < a8; ++m )
{
if ( a3 == m || (int)a7 < a2 )
{
defect(a7);
if ( debug > 10 )
show_sub_mesh(
*(_QWORD *)(*(_QWORD *)(v15 + 80) + v35 + 32),
*(unsigned int *)(*(_QWORD *)(v15 + 80) + v35 + 68));
}
v15 += 96LL;
}
v17 = a1;
v18 = 0;
v36 = v35 - 72;
do
{
if ( a3 == v18 || (int)a7 < a2 )
{
restrict_one(a7);
if ( debug > 10 )
show_sub_mesh(
*(_QWORD *)(*(_QWORD *)(v17 + 80) + v36 + 48),
*(unsigned int *)(*(_QWORD *)(v17 + 80) + v36 + 68));
}
++v18;
v17 += 96LL;
}
while ( a8 > v18 );
multigrid_kernel(a1, a2, a3, a4, a5, a6, v42, a8);
if ( debug <= 8 )
goto LABEL_82;
LABEL_88:
printf("\t\tReturn to level %d\n", a7);
if ( a8 > 0 )
{
LABEL_82:
v20 = a1;
for ( n = 0; n < a8; ++n )
{
if ( a3 == n || (int)a7 < a2 )
{
interpolate_one(a7);
if ( debug > 10 )
{
printf("Element %d v\n", (unsigned int)n);
show_sub_mesh(
*(_QWORD *)(*(_QWORD *)(v20 + 80) + 72LL * (int)a7 + 16),
*(unsigned int *)(*(_QWORD *)(v20 + 80) + 72LL * (int)a7 + 68));
}
}
v20 += 96LL;
}
v21 = a1;
for ( ii = 0; (int)ii < a8; ++ii )
{
if ( a3 == ii || (int)a7 < a2 )
{
if ( debug > 10 )
{
printf("Element %d, pre add\n", ii);
show_sub_mesh(
*(_QWORD *)(*(_QWORD *)(v21 + 80) + 72LL * (int)a7),
*(unsigned int *)(*(_QWORD *)(v21 + 80) + 72LL * (int)a7 + 68));
}
add_error(a7);
if ( debug > 10 )
{
printf("Element %d, post add\n", ii);
show_sub_mesh(
*(_QWORD *)(*(_QWORD *)(v21 + 80) + 72LL * (int)a7),
*(unsigned int *)(*(_QWORD *)(v21 + 80) + 72LL * (int)a7 + 68));
}
}
v21 += 96LL;
}
if ( debug > 10 )
{
v28 = (_QWORD *)(a1 + 80);
for ( jj = 0; jj < a8; ++jj )
{
printf("Element %d\n", (unsigned int)jj);
show_sub_mesh(*(_QWORD *)(*v28 + 72LL * (int)a7), *(unsigned int *)(*v28 + 72LL * (int)a7 + 68));
v28 += 12;
}
}
}
LABEL_80:
for ( kk = 0; kk != 3; ++kk )
{
v24 = a1;
u_to_ghost(a1, a7, (unsigned int)a8);
if ( a8 > 0 )
{
for ( ll = 0; ll != a8; ++ll )
{
if ( a3 == ll || (int)a7 < a2 )
smooth_rgb(a7, kk);
v24 += 96LL;
}
}
}
if ( debug <= 10 )
{
u_to_ghost(a1, a7, (unsigned int)a8);
if ( a8 <= 0 )
return;
}
else
{
if ( a8 <= 0 )
{
u_to_ghost(a1, a7, (unsigned int)a8);
return;
}
v25 = (_QWORD *)(a1 + 80);
do
{
show_sub_mesh(*(_QWORD *)(*v25 + 72LL * (int)a7), *(unsigned int *)(*v25 + 72LL * (int)a7 + 68));
v25 += 12;
}
while ( (_QWORD *)(a1 + 96LL * (unsigned int)(a8 - 1) + 176) != v25 );
u_to_ghost(a1, a7, (unsigned int)a8);
}
v26 = a1;
for ( mm = 0; mm < a8; ++mm )
{
if ( *(_DWORD *)(v26 + 64) - 1 == a7 )
defect(a7);
v26 += 96LL;
}
}

original_code:
void multigrid_kernel(Element * element,
  int levels,
  int ele,
  int lev,
  char * mode,
  int * smooth_levels,
  int this_level,
  int number_elements
  )
{
 if(debug>8)
  printf("\t\tIn level %d\n",this_level);
 int e;
 int k;
 int next_level;
 if(this_level==0){
 }
 else{
  next_level=this_level-1;
  for(e=0;e<number_elements;e++){
   if(e==ele || this_level < levels){
   initialize_sub_mesh(element[e].mesh[this_level].d,
     element[e].mesh[this_level].number_nodes_base,0.0);
   initialize_sub_mesh(element[e].mesh[this_level].v,
     element[e].mesh[this_level].number_nodes_base,0.0);
   initialize_sub_mesh(element[e].mesh[this_level-1].f,
     element[e].mesh[this_level-1].number_nodes_base,0.0);
   initialize_sub_mesh(element[e].mesh[this_level-1].u,
     element[e].mesh[this_level-1].number_nodes_base,0.0);
   }
  }
  if(debug>10){
   for(e=0;e<number_elements;e++){
    printf("Element %d: u\n",e);
    show_sub_mesh(element[e].mesh[this_level].u,element[e].mesh[this_level].number_nodes_base);
   }
  }
  for(k=0;k<3;k++){
   u_to_ghost(element,this_level,number_elements);
   for(e=0;e<number_elements;e++){
    if(e==ele || this_level < levels){
     smooth_rgb(element[e],this_level,k);
    }
   }
  }
  if(debug>10){
   for(e=0;e<number_elements;e++){
    printf("Element %d: u\n",e);
    show_sub_mesh(element[e].mesh[this_level].u,element[e].mesh[this_level].number_nodes_base);
   }
  }
  u_to_ghost(element,this_level,number_elements);
  if(debug>10)
   print_ghost(element,number_elements,this_level);
  for(e=0;e<number_elements;e++){
   if(e==ele || this_level < levels){
    defect(element[e],this_level);
    if(debug>10)
     show_sub_mesh(element[e].mesh[this_level].d,element[e].mesh[this_level].number_nodes_base);
   }
  }
  for(e=0;e<number_elements;e++){
   if(e==ele || this_level < levels){
    restrict_one(element[e],this_level);
    if(debug>10)
     show_sub_mesh(element[e].mesh[this_level-1].f,element[e].mesh[this_level-1].number_nodes_base);
   }
  }
  multigrid_kernel(element,levels,ele,lev,mode,smooth_levels,next_level,number_elements);
  if(debug>8)
   printf("\t\tReturn to level %d\n",this_level);
  for(e=0;e<number_elements;e++){
   if(e==ele || this_level < levels){
    interpolate_one(element[e],this_level);
    if(debug>10){
     printf("Element %d v\n",e);
     show_sub_mesh(element[e].mesh[this_level].v,element[e].mesh[this_level].number_nodes_base);
    }
   }
  }
  for(e=0;e<number_elements;e++){
   if(e==ele || this_level < levels){
    if(debug>10){
     printf("Element %d, pre add\n",e);
     show_sub_mesh(element[e].mesh[this_level].u,element[e].mesh[this_level].number_nodes_base);
    }
    add_error(element[e],this_level);
    if(debug>10){
     printf("Element %d, post add\n",e);
     show_sub_mesh(element[e].mesh[this_level].u,element[e].mesh[this_level].number_nodes_base);
    }
   }
  }
  if(debug>10){
   for(e=0;e<number_elements;e++){
    printf("Element %d\n",e);
    show_sub_mesh(element[e].mesh[this_level].u,element[e].mesh[this_level].number_nodes_base);
   }
  }
  for(k=0;k<3;k++){
   u_to_ghost(element,this_level,number_elements);
   for(e=0;e<number_elements;e++){
    if(e==ele || this_level < levels){
     smooth_rgb(element[e],this_level,k);
    }
   }
  }
  if(debug>10)
   for(e=0;e<number_elements;e++)
    show_sub_mesh(element[e].mesh[this_level].u,element[e].mesh[this_level].number_nodes_base);
  u_to_ghost(element,this_level,number_elements);
  for(e=0;e<number_elements;e++){
   if(element[e].n_levels-1==this_level)
    defect(element[e],this_level);
  }
 }
}

canonical_original_code:
void multigrid_kernel(struct Element *element, int levels, int ele, int lev, char *mode, int *smooth_levels, int this_level, int number_elements)
{
 if(debug>8)
  printf("\t\tIn level %d\n",this_level);
 int e;
 int k;
 int next_level;
 if(this_level==0){
 }
 else{
  next_level=this_level-1;
  for(e=0;e<number_elements;e++){
   if(e==ele || this_level < levels){
   initialize_sub_mesh(element[e].mesh[this_level].d,
     element[e].mesh[this_level].number_nodes_base,0.0);
   initialize_sub_mesh(element[e].mesh[this_level].v,
     element[e].mesh[this_level].number_nodes_base,0.0);
   initialize_sub_mesh(element[e].mesh[this_level-1].f,
     element[e].mesh[this_level-1].number_nodes_base,0.0);
   initialize_sub_mesh(element[e].mesh[this_level-1].u,
     element[e].mesh[this_level-1].number_nodes_base,0.0);
   }
  }
  if(debug>10){
   for(e=0;e<number_elements;e++){
    printf("Element %d: u\n",e);
    show_sub_mesh(element[e].mesh[this_level].u,element[e].mesh[this_level].number_nodes_base);
   }
  }
  for(k=0;k<3;k++){
   u_to_ghost(element,this_level,number_elements);
   for(e=0;e<number_elements;e++){
    if(e==ele || this_level < levels){
     smooth_rgb(element[e],this_level,k);
    }
   }
  }
  if(debug>10){
   for(e=0;e<number_elements;e++){
    printf("Element %d: u\n",e);
    show_sub_mesh(element[e].mesh[this_level].u,element[e].mesh[this_level].number_nodes_base);
   }
  }
  u_to_ghost(element,this_level,number_elements);
  if(debug>10)
   print_ghost(element,number_elements,this_level);
  for(e=0;e<number_elements;e++){
   if(e==ele || this_level < levels){
    defect(element[e],this_level);
    if(debug>10)
     show_sub_mesh(element[e].mesh[this_level].d,element[e].mesh[this_level].number_nodes_base);
   }
  }
  for(e=0;e<number_elements;e++){
   if(e==ele || this_level < levels){
    restrict_one(element[e],this_level);
    if(debug>10)
     show_sub_mesh(element[e].mesh[this_level-1].f,element[e].mesh[this_level-1].number_nodes_base);
   }
  }
  multigrid_kernel(element,levels,ele,lev,mode,smooth_levels,next_level,number_elements);
  if(debug>8)
   printf("\t\tReturn to level %d\n",this_level);
  for(e=0;e<number_elements;e++){
   if(e==ele || this_level < levels){
    interpolate_one(element[e],this_level);
    if(debug>10){
     printf("Element %d v\n",e);
     show_sub_mesh(element[e].mesh[this_level].v,element[e].mesh[this_level].number_nodes_base);
    }
   }
  }
  for(e=0;e<number_elements;e++){
   if(e==ele || this_level < levels){
    if(debug>10){
     printf("Element %d, pre add\n",e);
     show_sub_mesh(element[e].mesh[this_level].u,element[e].mesh[this_level].number_nodes_base);
    }
    add_error(element[e],this_level);
    if(debug>10){
     printf("Element %d, post add\n",e);
     show_sub_mesh(element[e].mesh[this_level].u,element[e].mesh[this_level].number_nodes_base);
    }
   }
  }
  if(debug>10){
   for(e=0;e<number_elements;e++){
    printf("Element %d\n",e);
    show_sub_mesh(element[e].mesh[this_level].u,element[e].mesh[this_level].number_nodes_base);
   }
  }
  for(k=0;k<3;k++){
   u_to_ghost(element,this_level,number_elements);
   for(e=0;e<number_elements;e++){
    if(e==ele || this_level < levels){
     smooth_rgb(element[e],this_level,k);
    }
   }
  }
  if(debug>10)
   for(e=0;e<number_elements;e++)
    show_sub_mesh(element[e].mesh[this_level].u,element[e].mesh[this_level].number_nodes_base);
  u_to_ghost(element,this_level,number_elements);
  for(e=0;e<number_elements;e++){
   if(element[e].n_levels-1==this_level)
    defect(element[e],this_level);
  }
 }
}

variable_types: struct Element { int id; int node[3]; int edge[3]; int nei[3][3]; int n_levels; struct Operator { double v[3][3]; } *operator; struct Mesh { double **u; double **u_gh; double **v; double **v_gh; double **d; double **d_gh; double **f; double **f_gh; int number_nodes; int number_nodes_base; } *mesh; double max_error; } *

variable_types: levels int

variable_types: ele int

variable_types: lev int

variable_types:
mode
char *

variable_types: e int


original_code:
void u_to_ghost(Element * element, int level,int number_elements)
{
 int i,j;
 int neighbor;
 int sent;
 int nnb;
 for(i=0;i<number_elements;i++){
  nnb=element[i].mesh[level].number_nodes_base;
  if((neighbor=element[i].nei[0][0])>=0){
   sent=element[i].nei[0][2]*element[neighbor].nei[element[i].nei[0][1]][2];
   if(element[i].nei[0][1]==0){
    if(sent==1){
     for(j=0;j<=nnb-2;j++){
      element[i].mesh[level].u_gh[0][j]=element[neighbor].mesh[level].u[nnb-2][j];
     }
    }else{
     for(j=0;j<=nnb-2;j++){
      element[i].mesh[level].u_gh[0][j]=element[neighbor].mesh[level].u[nnb-2][nnb-2-j];
     }
    }
   }else if(element[i].nei[0][1]==1){
    sent=element[i].nei[0][2]*element[neighbor].nei[element[i].nei[0][1]][2];
    if(sent==1){
     for(j=0;j<=nnb-2;j++)
      element[i].mesh[level].u_gh[0][j]=element[neighbor].mesh[level].u[nnb-1-j][nnb-2-j];
    }else{
     for(j=0;j<=nnb-2;j++){
      element[i].mesh[level].u_gh[0][j]=element[neighbor].mesh[level].u[(j+1)][j];
     }
    }
   }else if(element[i].nei[0][1]==2){
    sent=element[i].nei[0][2]*element[neighbor].nei[element[i].nei[0][1]][2];
    if(sent==1){
     for(j=0;j<=nnb-2;j++){
      element[i].mesh[level].u_gh[0][j]=element[neighbor].mesh[level].u[j+1][1];
     }
    }else{
     for(j=0;j<=nnb-2;j++)
      element[i].mesh[level].u_gh[0][j]=element[neighbor].mesh[level].u[nnb-1-j][1];
    }
   }
  }
  if((neighbor=element[i].nei[1][0])>0){
  }
  if((neighbor=element[i].nei[2][0])>0){
  }
 }
}

canonical_original_code:
void u_to_ghost(struct Element *element, int level, int number_elements)
{
 int i; int j;
 int neighbor;
 int sent;
 int nnb;
 for(i=0;i<number_elements;i++){
  nnb=element[i].mesh[level].number_nodes_base;
  if((neighbor=element[i].nei[0][0])>=0){
   sent=element[i].nei[0][2]*element[neighbor].nei[element[i].nei[0][1]][2];
   if(element[i].nei[0][1]==0){
    if(sent==1){
     for(j=0;j<=nnb-2;j++){
      element[i].mesh[level].u_gh[0][j]=element[neighbor].mesh[level].u[nnb-2][j];
     }
    }else{
     for(j=0;j<=nnb-2;j++){
      element[i].mesh[level].u_gh[0][j]=element[neighbor].mesh[level].u[nnb-2][nnb-2-j];
     }
    }
   }else if(element[i].nei[0][1]==1){
    sent=element[i].nei[0][2]*element[neighbor].nei[element[i].nei[0][1]][2];
    if(sent==1){
     for(j=0;j<=nnb-2;j++)
      element[i].mesh[level].u_gh[0][j]=element[neighbor].mesh[level].u[nnb-1-j][nnb-2-j];
    }else{
     for(j=0;j<=nnb-2;j++){
      element[i].mesh[level].u_gh[0][j]=element[neighbor].mesh[level].u[(j+1)][j];
     }
    }
   }else if(element[i].nei[0][1]==2){
    sent=element[i].nei[0][2]*element[neighbor].nei[element[i].nei[0][1]][2];
    if(sent==1){
     for(j=0;j<=nnb-2;j++){
      element[i].mesh[level].u_gh[0][j]=element[neighbor].mesh[level].u[j+1][1];
     }
    }else{
     for(j=0;j<=nnb-2;j++)
      element[i].mesh[level].u_gh[0][j]=element[neighbor].mesh[level].u[nnb-1-j][1];
    }
   }
  }
  if((neighbor=element[i].nei[1][0])>0){
  }
  if((neighbor=element[i].nei[2][0])>0){
  }
 }
}

variable_types: level int

variable_types: neighbor int

variable_types: sent int

variable_types: nnb int


original_code:
void print_ghost(Element * element, int number_elements, int level)
{
 int i,j,k;
 printf("Ghost nodes\n");
 for(i=0;i<number_elements;i++){
  printf("Element %d\n",i);
  for(j=0;j<3;j++){
   for(k=0;k<element[i].mesh[level].number_nodes_base-1;k++)
    printf("%f ",element[i].mesh[level].u_gh[j][k]);
  printf("--\n");
  }
 }
}

canonical_original_code:
void print_ghost(struct Element *element, int number_elements, int level)
{
 int i; int j; int k;
 printf("Ghost nodes\n");
 for(i=0;i<number_elements;i++){
  printf("Element %d\n",i);
  for(j=0;j<3;j++){
   for(k=0;k<element[i].mesh[level].number_nodes_base-1;k++)
    printf("%f ",element[i].mesh[level].u_gh[j][k]);
  printf("--\n");
  }
 }
}

variable_types: i int

variable_types: k int


original_code:
double calcula_max(double **x, int dimension)
{
 int i,j;
 double max=0.0;
 for(i=0;i<dimension;i++)
 {
  for(j=0;j<dimension;j++)
  {
   if(max<fabs(x[i][j]))
    max=fabs(x[i][j]);
  }
 }
 return max;
}

canonical_original_code:
double calcula_max(double **x, int dimension)
{
 int i; int j;
 double max = 0.0;
 for(i=0;i<dimension;i++)
 {
  for(j=0;j<dimension;j++)
  {
   if(max<fabs(x[i][j]))
    max=fabs(x[i][j]);
  }
 }
 return max;
}

variable_types:
x
double **

variable_types: dimension int

variable_types: max double

return_type: double


main

repo: frdeso/adding-kernel-tracepoints

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
puts("X86_TRAP_BP, INT3");
__debugbreak();
}

original_code:
int main()
{
 printf("X86_TRAP_BP, INT3\n");
 asm("INT3");
 printf("X86_DIV_0\n");
 int a = 1, b = atoi((const char*)'0');
 float c = (float)a/(float)b;
 printf("%f\n",c);
 return 0;
}

canonical_original_code:
int main()
{
 printf("X86_TRAP_BP, INT3\n");
 asm("INT3");
 printf("X86_DIV_0\n");
 int a = 1; int b = atoi((const char*)'0');
 float c = (float)a/(float)b;
 printf("%f\n",c);
 return 0;
}

variable_types: a int

variable_types: b int

variable_types: c float


PrintNode

decompiled_code:
int __fastcall PrintNode(unsigned int *a1)
{
return printf("\\%d\n", *a1);
}

canonical_decompiled_code:
int __fastcall func7(unsigned int *a1)
{
return printf("\\%d\n", *a1);
}

original_code:
void PrintNode(node *n)
{
 printf("\\%d\n",n->value);
}

canonical_original_code:
void PrintNode(struct node *n)
{
 printf("\\%d\n",n->value);
}

variable_types: struct node { int value; struct node *left; struct node *right; } *


PrintLine

decompiled_code:
int PrintLine()
{
int v0; // ebx

putchar(10);
v0 = 80;
do
{
putchar(45);
--v0;
}
while ( v0 );
return putchar(10);
}

canonical_decompiled_code:
int func8()
{
int v0; // ebx

putchar(10);
v0 = 80;
do
{
putchar(45);
--v0;
}
while ( v0 );
return putchar(10);
}

original_code:
void PrintLine()
{
 int i;
 printf("\n");
 for(i = 0; i< 80; ++i)
  printf("-");
 printf("\n");
}


FindMinNode

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

result = a1;
if ( a1 )
{
if ( *(_QWORD *)(a1 + 8) )
result = FindMinNode();
}
return result;
}

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

result = a1;
if ( a1 )
{
if ( *(_QWORD *)(a1 + 8) )
result = func9();
}
return result;
}

original_code:
node *FindMinNode(node *n)
{
 if(n == ((void *)0))
  return ((void *)0);
 else if(n->left)
  return FindMinNode(n->left);
 else
  return n;
}

canonical_original_code:
struct node *FindMinNode(struct node *n)
{
 if(n == ((void *)0))
  return ((void *)0);
 else if(n->left)
  return FindMinNode(n->left);
 else
  return n;
}

variable_types:
n
struct node *

return_type: struct node { int value; struct node *left; struct node *right; } *


PrintTreePreOrder

decompiled_code:
__int64 __fastcall PrintTreePreOrder(__int64 a1, __int16 a2)
{
int v2; // ebx
unsigned int v3; // ebp
__int64 result; // rax

if ( a1 )
{
if ( a2 > 0 )
{
v2 = 0;
do
{
putchar(124);
++v2;
}
while ( v2 != a2 );
}
PrintNode((unsigned int *)a1);
v3 = (__int16)(a2 + 1);
PrintTreePreOrder(*(_QWORD *)(a1 + 8), v3);
result = PrintTreePreOrder(*(_QWORD *)(a1 + 16), v3);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int16 a2)
{
int v2; // ebx
unsigned int v3; // ebp
__int64 result; // rax

if ( a1 )
{
if ( a2 > 0 )
{
v2 = 0;
do
{
putchar(124);
++v2;
}
while ( v2 != a2 );
}
func7((unsigned int *)a1);
v3 = (__int16)(a2 + 1);
func10(*(_QWORD *)(a1 + 8), v3);
result = func10(*(_QWORD *)(a1 + 16), v3);
}
return result;
}

original_code:
void PrintTreePreOrder(node *n, short depth)
{
 if(n != ((void *)0))
 {
  int i;
  for(i = 0;i<depth;++i)
   printf("|");
  PrintNode(n);
  PrintTreePreOrder(n->left,depth + 1);
  PrintTreePreOrder(n->right,depth + 1);
 }
 return;
}

canonical_original_code:
void PrintTreePreOrder(struct node *n, short depth)
{
 if(n != ((void *)0))
 {
  int i;
  for(i = 0;i<depth;++i)
   printf("|");
  PrintNode(n);
  PrintTreePreOrder(n->left,depth + 1);
  PrintTreePreOrder(n->right,depth + 1);
 }
 return;
}

variable_types: depth short

user_defined_types: struct node { int value; struct node *left; struct node *right; }


PrintTreeInOrder

decompiled_code:
int __fastcall PrintTreeInOrder(__int64 a1)
{
int result; // eax

if ( *(_QWORD *)(a1 + 8) )
PrintTreeInOrder();
result = PrintNode((unsigned int *)a1);
if ( *(_QWORD *)(a1 + 16) )
result = PrintTreeInOrder();
return result;
}

canonical_decompiled_code:
int __fastcall func11(__int64 a1)
{
int result; // eax

if ( *(_QWORD *)(a1 + 8) )
func11();
result = func7((unsigned int *)a1);
if ( *(_QWORD *)(a1 + 16) )
result = func11();
return result;
}

original_code:
void PrintTreeInOrder(node *n)
{
 if(n->left != ((void *)0))
  PrintTreeInOrder(n->left);
 PrintNode(n);
 if(n->right != ((void *)0))
  PrintTreeInOrder(n->right);
}

canonical_original_code:
void PrintTreeInOrder(struct node *n)
{
 if(n->left != ((void *)0))
  PrintTreeInOrder(n->left);
 PrintNode(n);
 if(n->right != ((void *)0))
  PrintTreeInOrder(n->right);
}


PrintTreePostOrder

decompiled_code:
int __fastcall PrintTreePostOrder(__int64 a1)
{
if ( *(_QWORD *)(a1 + 8) )
PrintTreePostOrder();
if ( *(_QWORD *)(a1 + 16) )
PrintTreePostOrder();
return PrintNode((unsigned int *)a1);
}

canonical_decompiled_code:
int __fastcall func12(__int64 a1)
{
if ( *(_QWORD *)(a1 + 8) )
func12();
if ( *(_QWORD *)(a1 + 16) )
func12();
return func7((unsigned int *)a1);
}

original_code:
void PrintTreePostOrder(node *n)
{
 if(n->left != ((void *)0))
  PrintTreePostOrder(n->left);
 if(n->right != ((void *)0))
  PrintTreePostOrder(n->right);
 PrintNode(n);
}

canonical_original_code:
void PrintTreePostOrder(struct node *n)
{
 if(n->left != ((void *)0))
  PrintTreePostOrder(n->left);
 if(n->right != ((void *)0))
  PrintTreePostOrder(n->right);
 PrintNode(n);
}


FindTreeDepth

decompiled_code:
__int64 __fastcall FindTreeDepth(__int64 a1)
{
__int64 result; // rax
int v2; // ebp
int v3; // edx

result = 0LL;
if ( a1 )
{
v2 = FindTreeDepth(*(_QWORD *)(a1 + 8));
v3 = FindTreeDepth(*(_QWORD *)(a1 + 16));
result = (unsigned int)(v3 + 1);
if ( v2 > v3 )
result = (unsigned int)(v2 + 1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
__int64 result; // rax
int v2; // ebp
int v3; // edx

result = 0LL;
if ( a1 )
{
v2 = func13(*(_QWORD *)(a1 + 8));
v3 = func13(*(_QWORD *)(a1 + 16));
result = (unsigned int)(v3 + 1);
if ( v2 > v3 )
result = (unsigned int)(v2 + 1);
}
return result;
}

original_code:
int FindTreeDepth(node *n)
{
 if(n == ((void *)0))
  return 0;
 int left_depth = FindTreeDepth(n->left);
 int right_depth = FindTreeDepth(n->right);
 if(left_depth > right_depth)
  return left_depth + 1;
 else
  return right_depth + 1;
}

canonical_original_code:
int FindTreeDepth(struct node *n)
{
 if(n == ((void *)0))
  return 0;
 int left_depth = FindTreeDepth(n->left);
 int right_depth = FindTreeDepth(n->right);
 if(left_depth > right_depth)
  return left_depth + 1;
 else
  return right_depth + 1;
}


PrintBFS

decompiled_code:
__int64 __fastcall PrintBFS(__int64 a1)
{
__int64 result; // rax
unsigned int *v2; // rax

puts("Breadth-First Search ");
result = g_queue_new();
if ( a1 )
{
g_queue_push_tail();
while ( !(unsigned int)g_queue_is_empty() )
{
v2 = (unsigned int *)g_queue_pop_head();
if ( v2 )
{
PrintNode(v2);
g_queue_push_tail();
g_queue_push_tail();
}
}
result = g_queue_free();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1)
{
__int64 result; // rax
unsigned int *v2; // rax

puts("Breadth-First Search ");
result = g_queue_new();
if ( a1 )
{
g_queue_push_tail();
while ( !(unsigned int)g_queue_is_empty() )
{
v2 = (unsigned int *)g_queue_pop_head();
if ( v2 )
{
func7(v2);
g_queue_push_tail();
g_queue_push_tail();
}
}
result = g_queue_free();
}
return result;
}

original_code:
void PrintBFS(node *n)
{
 printf("Breadth-First Search \n");
 GQueue *q = g_queue_new();
 node * current;
 if(n == ((void *)0))
  return;
 g_queue_push_tail(q,n);
 while(!g_queue_is_empty(q))
 {
  current = g_queue_pop_head(q);
  if(current != ((void *)0))
  {
   PrintNode(current);
   g_queue_push_tail(q, current->left);
   g_queue_push_tail(q, current->right);
  }
 }
 g_queue_free(q);
}

canonical_original_code:
void PrintBFS(struct node *n)
{
 printf("Breadth-First Search \n");
 struct _GQueue *q = g_queue_new();
 struct node *current;
 if(n == ((void *)0))
  return;
 g_queue_push_tail(q,n);
 while(!g_queue_is_empty(q))
 {
  current = g_queue_pop_head(q);
  if(current != ((void *)0))
  {
   PrintNode(current);
   g_queue_push_tail(q, current->left);
   g_queue_push_tail(q, current->right);
  }
 }
 g_queue_free(q);
}

variable_types: struct _GQueue { struct _GList { void *data; struct _GList *next; struct _GList *prev; } *head; struct _GList *tail; unsigned int length; } *

user_defined_types: struct _GQueue { struct _GList *head; struct _GList *tail; unsigned int length; }


PrintReverseBFS

decompiled_code:
__int64 __fastcall PrintReverseBFS(__int64 a1)
{
__int64 result; // rax
int v2; // er13
int v3; // ebx
unsigned int *v4; // rax

puts("Reverse Breadth-First Search ");
g_queue_new();
result = g_queue_new();
if ( a1 )
{
g_queue_push_tail();
while ( !(unsigned int)g_queue_is_empty() )
{
if ( g_queue_pop_head() )
{
g_queue_push_head();
g_queue_push_tail();
g_queue_push_tail();
}
}
v2 = g_queue_get_length();
if ( v2 > 0 )
{
v3 = 0;
do
{
v4 = (unsigned int *)g_queue_pop_head();
PrintNode(v4);
++v3;
}
while ( v2 != v3 );
}
g_queue_free();
result = g_queue_free();
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1)
{
__int64 result; // rax
int v2; // er13
int v3; // ebx
unsigned int *v4; // rax

puts("Reverse Breadth-First Search ");
g_queue_new();
result = g_queue_new();
if ( a1 )
{
g_queue_push_tail();
while ( !(unsigned int)g_queue_is_empty() )
{
if ( g_queue_pop_head() )
{
g_queue_push_head();
g_queue_push_tail();
g_queue_push_tail();
}
}
v2 = g_queue_get_length();
if ( v2 > 0 )
{
v3 = 0;
do
{
v4 = (unsigned int *)g_queue_pop_head();
func7(v4);
++v3;
}
while ( v2 != v3 );
}
g_queue_free();
result = g_queue_free();
}
return result;
}

original_code:
void PrintReverseBFS(node *n)
{
 printf("Reverse Breadth-First Search \n");
 GQueue *q = g_queue_new();
 GQueue *s = g_queue_new();
 node * current;
 if(n == ((void *)0))
  return;
 g_queue_push_tail(q,n);
 while(!g_queue_is_empty(q))
 {
  current = g_queue_pop_head(q);
  if(current != ((void *)0))
  {
   g_queue_push_head(s, current);
   g_queue_push_tail(q, current->left);
   g_queue_push_tail(q, current->right);
  }
 }
 int i, size = g_queue_get_length(s);
 for(i = 0; i<size;++i)
 {
  current = g_queue_pop_head(s);
  PrintNode(current);
 }
 g_queue_free(s);
 g_queue_free(q);
}

canonical_original_code:
void PrintReverseBFS(struct node *n)
{
 printf("Reverse Breadth-First Search \n");
 struct _GQueue *q = g_queue_new();
 struct _GQueue *s = g_queue_new();
 struct node *current;
 if(n == ((void *)0))
  return;
 g_queue_push_tail(q,n);
 while(!g_queue_is_empty(q))
 {
  current = g_queue_pop_head(q);
  if(current != ((void *)0))
  {
   g_queue_push_head(s, current);
   g_queue_push_tail(q, current->left);
   g_queue_push_tail(q, current->right);
  }
 }
 int i; int size = g_queue_get_length(s);
 for(i = 0; i<size;++i)
 {
  current = g_queue_pop_head(s);
  PrintNode(current);
 }
 g_queue_free(s);
 g_queue_free(q);
}

variable_types: size int

user_defined_types: struct _GList { void *data; struct _GList *next; struct _GList *prev; }


getAllNodes

decompiled_code:
__int64 __fastcall getAllNodes(__int64 a1, __int64 a2)
{
__int64 v3; // rdi
__int64 result; // rax

if ( *(_QWORD *)(a1 + 8) )
a2 = ((__int64 (*)(void))getAllNodes)();
g_queue_push_tail();
v3 = *(_QWORD *)(a1 + 16);
result = a2;
if ( v3 )
result = getAllNodes(v3, a2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, __int64 a2)
{
__int64 v3; // rdi
__int64 result; // rax

if ( *(_QWORD *)(a1 + 8) )
a2 = ((__int64 (*)(void))getAllNodes)();
g_queue_push_tail();
v3 = *(_QWORD *)(a1 + 16);
result = a2;
if ( v3 )
result = func16(v3, a2);
return result;
}

original_code:
GQueue *getAllNodes(node *n, GQueue *q)
{
 if(n->left != ((void *)0))
  q = getAllNodes( n->left, q);
 g_queue_push_tail(q,n);
 if(n->right != ((void *)0))
  q = getAllNodes(n->right, q);
 return q;
}

canonical_original_code:
struct _GQueue *getAllNodes(struct node *n, struct _GQueue *q)
{
 if(n->left != ((void *)0))
  q = getAllNodes( n->left, q);
 g_queue_push_tail(q,n);
 if(n->right != ((void *)0))
  q = getAllNodes(n->right, q);
 return q;
}

variable_types:
q
struct _GQueue *

return_type: struct _GQueue { struct _GList { void *data; struct _GList *next; struct _GList *prev; } *head; struct _GList *tail; unsigned int length; } *


AddToBalancedTree

decompiled_code:
__int64 __fastcall AddToBalancedTree(__int64 a1, _DWORD *a2)
{
__int64 result; // rax

if ( *a2 < *(_DWORD *)a1 )
{
result = *(_QWORD *)(a1 + 8);
if ( result )
result = AddToBalancedTree(*(_QWORD *)(a1 + 8));
else
*(_QWORD *)(a1 + 8) = a2;
}
else
{
result = *(_QWORD *)(a1 + 16);
if ( result )
result = AddToBalancedTree(*(_QWORD *)(a1 + 16));
else
*(_QWORD *)(a1 + 16) = a2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1, _DWORD *a2)
{
__int64 result; // rax

if ( *a2 < *(_DWORD *)a1 )
{
result = *(_QWORD *)(a1 + 8);
if ( result )
result = func17(*(_QWORD *)(a1 + 8));
else
*(_QWORD *)(a1 + 8) = a2;
}
else
{
result = *(_QWORD *)(a1 + 16);
if ( result )
result = func17(*(_QWORD *)(a1 + 16));
else
*(_QWORD *)(a1 + 16) = a2;
}
return result;
}

original_code:
void AddToBalancedTree(node *root, node *n)
{
 if(n->value >= root->value)
 {
  if(root->right == ((void *)0))
   root->right = n;
  else
   AddToBalancedTree(root->right, n);
 }
 else
 {
  if(root->left == ((void *)0))
   root->left = n;
  else
   AddToBalancedTree(root->left, n);
 }
}

canonical_original_code:
void AddToBalancedTree(struct node *root, struct node *n)
{
 if(n->value >= root->value)
 {
  if(root->right == ((void *)0))
   root->right = n;
  else
   AddToBalancedTree(root->right, n);
 }
 else
 {
  if(root->left == ((void *)0))
   root->left = n;
  else
   AddToBalancedTree(root->left, n);
 }
}


BalanceThatTree

decompiled_code:
__int64 __fastcall BalanceThatTree(__int64 a1)
{
__int64 v1; // rsi
int v2; // eax
int v3; // er13
int i; // ebx
unsigned int *v5; // rax
int v6; // er12
__int64 v7; // r13
int v8; // ebx
_DWORD *v9; // rax
int v10; // eax

puts("Get elements... ");
v1 = g_queue_new();
getAllNodes(a1, v1);
v2 = g_queue_get_length();
if ( v2 <= 0 )
{
printf("Creating balanced tree...");
g_queue_pop_head();
}
else
{
v3 = v2;
for ( i = 0; i != v3; ++i )
{
v5 = (unsigned int *)g_queue_peek_nth();
PrintNode(v5);
v6 = i;
}
printf("Creating balanced tree...");
v7 = g_queue_pop_head();
v8 = 0;
do
{
v9 = (_DWORD *)g_queue_peek_nth();
AddToBalancedTree(v7, v9);
v10 = v8++;
}
while ( v6 != v10 );
}
g_queue_free();
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1)
{
__int64 v1; // rsi
int v2; // eax
int v3; // er13
int i; // ebx
unsigned int *v5; // rax
int v6; // er12
__int64 v7; // r13
int v8; // ebx
_DWORD *v9; // rax
int v10; // eax

puts("Get elements... ");
v1 = g_queue_new();
func16(a1, v1);
v2 = g_queue_get_length();
if ( v2 <= 0 )
{
printf("Creating balanced tree...");
g_queue_pop_head();
}
else
{
v3 = v2;
for ( i = 0; i != v3; ++i )
{
v5 = (unsigned int *)g_queue_peek_nth();
func7(v5);
v6 = i;
}
printf("Creating balanced tree...");
v7 = g_queue_pop_head();
v8 = 0;
do
{
v9 = (_DWORD *)g_queue_peek_nth();
func17(v7, v9);
v10 = v8++;
}
while ( v6 != v10 );
}
g_queue_free();
return 0LL;
}

original_code:
node *BalanceThatTree(node *n)
{
 printf("Get elements... \n");
 GQueue *q = g_queue_new();
 q = getAllNodes(n, q);
 node *current;
 int i;
 int size = g_queue_get_length(q);
 for(i = 0;i < size;++i)
 {
  current = g_queue_peek_nth(q, i);
  PrintNode(current);
 }
 printf("Creating balanced tree...");
 node *root = g_queue_pop_head(q);
 for(i = 0;i < size;++i)
 {
  current = g_queue_peek_nth(q, i);
  AddToBalancedTree(root, current);
 }
 g_queue_free(q);
}

canonical_original_code:
struct node *BalanceThatTree(struct node *n)
{
 printf("Get elements... \n");
 struct _GQueue *q = g_queue_new();
 q = getAllNodes(n, q);
 struct node *current;
 int i;
 int size = g_queue_get_length(q);
 for(i = 0;i < size;++i)
 {
  current = g_queue_peek_nth(q, i);
  PrintNode(current);
 }
 printf("Creating balanced tree...");
 struct node *root = g_queue_pop_head(q);
 for(i = 0;i < size;++i)
 {
  current = g_queue_peek_nth(q, i);
  AddToBalancedTree(root, current);
 }
 g_queue_free(q);
}