decompiled_code:
ssize_t __fastcall oled_sleep_set(int a1, char a2)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

qmemcpy(buf, "SOO", 3);
buf[3] = a2;
return write(a1, buf, 4uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func4(int a1, char a2)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

qmemcpy(buf, "SOO", 3);
buf[3] = a2;
return func0(a1, buf, 4uLL);
}

original_code:
void
oled_sleep_set (int fd, _Bool val)
{
    uint8_t buf[] = { 'S', 'O', 'O', val ? 1 : 0 };
    _write(fd, buf, sizeof (buf));
}

canonical_original_code:
void oled_sleep_set(int fd, _Bool val)
{
    unsigned char buf[] = { 'S', 'O', 'O', val ? 1 : 0 };
    _write(fd, buf, sizeof (buf));
}


repo: garlick/pi-ted-envoy

decompiled_code:
ssize_t __fastcall oled_text_pos_set(int a1, char a2, char a3)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = 84;
buf[1] = 80;
buf[2] = a2;
buf[3] = a3;
return write(a1, buf, 4uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func5(int a1, char a2, char a3)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = 84;
buf[1] = 80;
buf[2] = a2;
buf[3] = a3;
return func0(a1, buf, 4uLL);
}

original_code:
void
oled_text_pos_set (int fd, uint8_t x, uint8_t y)
{
    uint8_t buf[] = { 'T', 'P', x, y };
    _write(fd, buf, sizeof (buf));
}

canonical_original_code:
void oled_text_pos_set(int fd, unsigned char x, unsigned char y)
{
    unsigned char buf[] = { 'T', 'P', x, y };
    _write(fd, buf, sizeof (buf));
}

variable_types: fd int

variable_types:
x
unsigned char

variable_types:
y
unsigned char

variable_types:
buf
unsigned char []

return_type: void


decompiled_code:
void oled_printf(int fd, const char *a2, ...)
{
void *ptr; // [rsp+0h] [rbp-D8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
if ( vasprintf((char **)&ptr, a2, arg) < 0 )
{
fwrite("out of memory\n", 1uLL, 0xEuLL, stderr);
exit(1);
}
oled_puts(fd, ptr);
free(ptr);
}

canonical_decompiled_code:
void func6(int fd, const char *a2, ...)
{
void *ptr; // [rsp+0h] [rbp-D8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
if ( vasprintf((char **)&ptr, a2, arg) < 0 )
{
fwrite("out of memory\n", 1uLL, 0xEuLL, stderr);
exit(1);
}
func1(fd, ptr);
free(ptr);
}

original_code:
void
oled_printf (int fd, const char *fmt, ...)
{
    va_list ap;
    char *s;
    int n;
    __builtin_va_start(ap,fmt);
    n = vasprintf (&s, fmt, ap);
    if (n < 0) {
        fprintf (stderr, "out of memory\n");
        exit (1);
    }
    __builtin_va_end(ap);
    _oled_puts (fd, s);
    free (s);
}

canonical_original_code:
void oled_printf(int fd, char *fmt, ...)
{
    __builtin_va_list ap;
    char *s;
    int n;
    __builtin_va_start(ap,fmt);
    n = vasprintf (&s, fmt, ap);
    if (n < 0) {
        fprintf (stderr, "out of memory\n");
        exit (1);
    }
    __builtin_va_end(ap);
    _oled_puts (fd, s);
    free (s);
}

variable_types:
fmt
char *

variable_types:
s
char *

variable_types: n int


decompiled_code:
__int64 __fastcall oled_init(unsigned int a1)
{
int v1; // eax
unsigned int v2; // ebx

v1 = open("/dev/i2c-1", 2);
if ( v1 < 0 )
{
perror("/dev/i2c-1");
exit(1);
}
v2 = v1;
if ( ioctl(v1, 0x703uLL, a1) < 0 )
{
perror("ioctl I2C_SLAVE");
exit(1);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1)
{
int v1; // eax
unsigned int v2; // ebx

v1 = open("/dev/i2c-1", 2);
if ( v1 < 0 )
{
perror("/dev/i2c-1");
exit(1);
}
v2 = v1;
if ( ioctl(v1, 0x703uLL, a1) < 0 )
{
perror("ioctl I2C_SLAVE");
exit(1);
}
return v2;
}

original_code:
int
oled_init(int addr)
{
    const char *devname = "/dev/i2c-1";
    int fd;
    fd = open (devname, 02);
    if (fd < 0) {
        perror (devname);
        exit (1);
    }
    if (ioctl (fd, 0x0703, addr) < 0) {
        perror ("ioctl I2C_SLAVE");
        exit (1);
    }
    return fd;
}

canonical_original_code:
int oled_init(int addr)
{
    char *devname = "/dev/i2c-1";
    int fd;
    fd = open (devname, 02);
    if (fd < 0) {
        perror (devname);
        exit (1);
    }
    if (ioctl (fd, 0x0703, addr) < 0) {
        perror ("ioctl I2C_SLAVE");
        exit (1);
    }
    return fd;
}

variable_types: addr int

variable_types:
devname
char *

return_type: int


decompiled_code:
int __fastcall oled_fini(int a1)
{
return close(a1);
}

canonical_decompiled_code:
int __fastcall func8(int a1)
{
return close(a1);
}

original_code:
void
oled_fini(int fd)
{
    close (fd);
}

canonical_original_code:
void oled_fini(int fd)
{
    close (fd);
}


decompiled_code:
int __fastcall oled_addr_set(unsigned int a1, char a2)
{
int v2; // ebx
char buf[14]; // [rsp+Ah] [rbp-Eh] BYREF

qmemcpy(buf, "SI2CA", 5);
buf[5] = a2;
v2 = oled_init(a1);
write(v2, buf, 6uLL);
return oled_fini(v2);
}

canonical_decompiled_code:
int __fastcall func9(unsigned int a1, char a2)
{
int v2; // ebx
char buf[14]; // [rsp+Ah] [rbp-Eh] BYREF

qmemcpy(buf, "SI2CA", 5);
buf[5] = a2;
v2 = func7(a1);
func0(v2, buf, 6uLL);
return func8(v2);
}

original_code:
void
oled_addr_set (int oldaddr, int newaddr)
{
    uint8_t buf[] = { 'S', 'I', '2', 'C', 'A', newaddr };
    int fd;
    fd = oled_init (oldaddr);
    _write(fd, buf, sizeof (buf));
    oled_fini (fd);
}

canonical_original_code:
void oled_addr_set(int oldaddr, int newaddr)
{
    unsigned char buf[] = { 'S', 'I', '2', 'C', 'A', newaddr };
    int fd;
    fd = oled_init (oldaddr);
    _write(fd, buf, sizeof (buf));
    oled_fini (fd);
}

variable_types: oldaddr int

variable_types: newaddr int


decompiled_code:
ssize_t __fastcall led_brightness_set(int a1, char a2)
{
char buf[13]; // [rsp+Bh] [rbp-Dh] BYREF

buf[0] = 10;
buf[1] = a2;
buf[2] = -1;
buf[3] = 0;
buf[4] = 0;
return write(a1, buf, 5uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func6(int a1, char a2)
{
char buf[13]; // [rsp+Bh] [rbp-Dh] BYREF

buf[0] = 10;
buf[1] = a2;
buf[2] = -1;
buf[3] = 0;
buf[4] = 0;
return func2(a1, buf, 5uLL);
}

original_code:
void
led_brightness_set (int fd, uint8_t val)
{
    uint8_t buf[] = { 0x0a, val, 0xff, 0 , 0};
    _write (fd, buf, sizeof (buf));
}

canonical_original_code:
void led_brightness_set(int fd, unsigned char val)
{
    unsigned char buf[] = { 0x0a, val, 0xff, 0 , 0};
    _write (fd, buf, sizeof (buf));
}

variable_types:
val
unsigned char


decompiled_code:
ssize_t __fastcall led_addr_set(int a1, char a2)
{
char buf[10]; // [rsp+Eh] [rbp-Ah] BYREF

buf[0] = 8;
buf[1] = a2;
return write(a1, buf, 2uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func7(int a1, char a2)
{
char buf[10]; // [rsp+Eh] [rbp-Ah] BYREF

buf[0] = 8;
buf[1] = a2;
return func2(a1, buf, 2uLL);
}

original_code:
void
led_addr_set (int fd, uint8_t newaddr)
{
    uint8_t buf[] = { 0x08, newaddr };
    _write (fd, buf, sizeof (buf));
}

canonical_original_code:
void led_addr_set(int fd, unsigned char newaddr)
{
    unsigned char buf[] = { 0x08, newaddr };
    _write (fd, buf, sizeof (buf));
}

variable_types:
newaddr
unsigned char


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

buf[0] = 1539;
return write(a1, buf, 2uLL);
}

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

buf[0] = 1539;
return func2(a1, buf, 2uLL);
}

original_code:
void
led_reset (int fd)
{
    uint8_t buf[] = { 0x03, 0x06 };
    _write (fd, buf, sizeof (buf));
}

canonical_original_code:
void led_reset(int fd)
{
    unsigned char buf[] = { 0x03, 0x06 };
    _write (fd, buf, sizeof (buf));
}


decompiled_code:
ssize_t __fastcall led_sleep_set(int a1, int a2)
{
char buf[13]; // [rsp+Bh] [rbp-Dh] BYREF

buf[0] = 4;
buf[1] = a2 == 0 ? -95 : -91;
buf[2] = 0;
buf[3] = 0;
buf[4] = 0;
return write(a1, buf, 5uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func9(int a1, int a2)
{
char buf[13]; // [rsp+Bh] [rbp-Dh] BYREF

buf[0] = 4;
buf[1] = a2 == 0 ? -95 : -91;
buf[2] = 0;
buf[3] = 0;
buf[4] = 0;
return func2(a1, buf, 5uLL);
}

original_code:
void
led_sleep_set (int fd, int val)
{
    uint8_t buf[] = { 0x04, val ? 0xa5 : 0xa1, 0, 0, 0 };
    _write (fd, buf, sizeof (buf));
}

canonical_original_code:
void led_sleep_set(int fd, int val)
{
    unsigned char buf[] = { 0x04, val ? 0xa5 : 0xa1, 0, 0, 0 };
    _write (fd, buf, sizeof (buf));
}

variable_types: val int


decompiled_code:
__int64 __fastcall led_status_get(int fd)
{
unsigned __int8 v2; // [rsp+Eh] [rbp-Ah] BYREF
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

buf[0] = 6;
write(fd, buf, 1uLL);
if ( (unsigned int)read(fd, &v2, 1uLL) != 1 )
{
perror("read status byte");
exit(1);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func10(int fd)
{
unsigned __int8 v2; // [rsp+Eh] [rbp-Ah] BYREF
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

buf[0] = 6;
func2(fd, buf, 1uLL);
if ( (unsigned int)func4(fd, &v2, 1uLL) != 1 )
{
perror("read status byte");
exit(1);
}
return v2;
}

original_code:
uint8_t
led_status_get (int fd)
{
    uint8_t wbuf[] = { 0x06 };
    uint8_t rbuf[1];
    _write (fd, wbuf, sizeof (wbuf));
    if (_read (fd, rbuf, sizeof (rbuf)) != 1) {
        perror ("read status byte");
        exit (1);
    }
    return rbuf[0];
}

canonical_original_code:
unsigned char led_status_get(int fd)
{
    unsigned char wbuf[] = { 0x06 };
    unsigned char rbuf[1];
    _write (fd, wbuf, sizeof (wbuf));
    if (_read (fd, rbuf, sizeof (rbuf)) != 1) {
        perror ("read status byte");
        exit (1);
    }
    return rbuf[0];
}

variable_types:
wbuf
unsigned char []

variable_types:
rbuf
unsigned char [1]

return_type: unsigned char


decompiled_code:
int __fastcall led_version_print(int fd)
{
int v1; // eax
char v3[31]; // [rsp+0h] [rbp-28h] BYREF
char buf[9]; // [rsp+1Fh] [rbp-9h] BYREF

buf[0] = 31;
write(fd, buf, 1uLL);
v1 = read(fd, v3, 0x13uLL);
return printf("%.*s\n", v1, v3);
}

canonical_decompiled_code:
int __fastcall func11(int fd)
{
int v1; // eax
char v3[31]; // [rsp+0h] [rbp-28h] BYREF
char buf[9]; // [rsp+1Fh] [rbp-9h] BYREF

buf[0] = 31;
func2(fd, buf, 1uLL);
v1 = func4(fd, v3, 0x13uLL);
return printf("%.*s\n", v1, v3);
}

original_code:
void
led_version_print (int fd)
{
    uint8_t wbuf[] = { 0x1f };
    uint8_t rbuf[19];
    int n;
    _write (fd, wbuf, sizeof (wbuf));
    n = _read (fd, rbuf, sizeof (rbuf));
    printf ("%.*s\n", n, (char *)rbuf);
}

canonical_original_code:
void led_version_print(int fd)
{
    unsigned char wbuf[] = { 0x1f };
    unsigned char rbuf[19];
    int n;
    _write (fd, wbuf, sizeof (wbuf));
    n = _read (fd, rbuf, sizeof (rbuf));
    printf ("%.*s\n", n, (char *)rbuf);
}

variable_types:
rbuf
unsigned char [19]


decompiled_code:
int __fastcall led_test(int a1)
{
int i; // ebx
int result; // eax
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

for ( i = 0; i != 8; ++i )
{
v3[0] = 16843009 * (unsigned __int8)(1 << i);
led_display(a1, (char *)v3);
result = usleep(0x186A0u);
}
return result;
}

canonical_decompiled_code:
int __fastcall func12(int a1)
{
int i; // ebx
int result; // eax
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

for ( i = 0; i != 8; ++i )
{
v3[0] = 16843009 * (unsigned __int8)(1 << i);
func3(a1, (char *)v3);
result = usleep(0x186A0u);
}
return result;
}

original_code:
void
led_test (int fd)
{
    int i;
    uint8_t buf[4];
    for (i = 0; i < 8; i++) {
        memset (buf, 1<<i, 4);
        _led_display (fd, buf);
        usleep (1000*100);
    }
}

canonical_original_code:
void led_test(int fd)
{
    int i;
    unsigned char buf[4];
    for (i = 0; i < 8; i++) {
        memset (buf, 1<<i, 4);
        _led_display (fd, buf);
        usleep (1000*100);
    }
}

variable_types: fd int

variable_types: i int

variable_types:
buf
unsigned char [4]


decompiled_code:
ssize_t led_printf(int a1, const char *a2, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF
char s[10]; // [rsp+26h] [rbp-C2h] BYREF

va_start(arg, a2);
vsnprintf(s, 0xAuLL, a2, arg);
return led_puts(a1, s);
}

canonical_decompiled_code:
ssize_t func13(int a1, const char *a2, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF
char s[10]; // [rsp+26h] [rbp-C2h] BYREF

va_start(arg, a2);
vsnprintf(s, 0xAuLL, a2, arg);
return func5(a1, s);
}

original_code:
void
led_printf (int fd, const char *fmt, ...)
{
    char s[10];
    va_list ap;
    __builtin_va_start(ap,fmt);
    vsnprintf (s, sizeof (s), fmt, ap);
    __builtin_va_end(ap);
    _led_puts (fd, s);
}

canonical_original_code:
void led_printf(int fd, char *fmt, ...)
{
    char s[10];
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    vsnprintf (s, sizeof (s), fmt, ap);
    __builtin_va_end(ap);
    _led_puts (fd, s);
}

variable_types:
s
char [10]


decompiled_code:
__int64 __fastcall led_init(unsigned int a1)
{
int v1; // eax
unsigned int v2; // ebx

v1 = open("/dev/i2c-1", 2);
if ( v1 < 0 )
{
perror("/dev/i2c-1");
exit(1);
}
v2 = v1;
if ( ioctl(v1, 0x703uLL, a1) < 0 )
{
perror("ioctl I2C_SLAVE");
exit(1);
}
return v2;
}

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

v1 = open("/dev/i2c-1", 2);
if ( v1 < 0 )
{
perror("/dev/i2c-1");
exit(1);
}
v2 = v1;
if ( ioctl(v1, 0x703uLL, a1) < 0 )
{
perror("ioctl I2C_SLAVE");
exit(1);
}
return v2;
}

original_code:
int
led_init(int addr)
{
    const char *devname = "/dev/i2c-1";
    int fd;
    fd = open (devname, 02);
    if (fd < 0) {
        perror (devname);
        exit (1);
    }
    if (ioctl (fd, 0x0703, addr) < 0) {
        perror ("ioctl I2C_SLAVE");
        exit (1);
    }
    return fd;
}

canonical_original_code:
int led_init(int addr)
{
    char *devname = "/dev/i2c-1";
    int fd;
    fd = open (devname, 02);
    if (fd < 0) {
        perror (devname);
        exit (1);
    }
    if (ioctl (fd, 0x0703, addr) < 0) {
        perror ("ioctl I2C_SLAVE");
        exit (1);
    }
    return fd;
}


decompiled_code:
int __fastcall led_fini(int a1)
{
return close(a1);
}

canonical_decompiled_code:
int __fastcall func15(int a1)
{
return close(a1);
}

original_code:
void
led_fini(int fd)
{
    close (fd);
}

canonical_original_code:
void led_fini(int fd)
{
    close (fd);
}


oom

decompiled_code:
void __noreturn oom()
{
fwrite("out of memory\n", 1uLL, 0xEuLL, stderr);
exit(1);
}

canonical_decompiled_code:
void __noreturn func0()
{
fwrite("out of memory\n", 1uLL, 0xEuLL, stderr);
exit(1);
}

original_code:
void oom (void)
{
    fprintf (stderr, "out of memory\n");
    exit (1);
}

canonical_original_code:
void oom()
{
    fprintf (stderr, "out of memory\n");
    exit (1);
}


xzmalloc

decompiled_code:
void *__fastcall xzmalloc(size_t n)
{
void *v1; // rax

v1 = malloc(n);
if ( !v1 )
oom();
return memset(v1, 0, n);
}

canonical_decompiled_code:
void *__fastcall func1(size_t n)
{
void *v1; // rax

v1 = malloc(n);
if ( !v1 )
func0();
return memset(v1, 0, n);
}

original_code:
void *xzmalloc (size_t size)
{
    void *new;
    new = malloc (size);
    if (!new)
        oom ();
    memset (new, 0, size);
    return new;
}

canonical_original_code:
void *xzmalloc(unsigned long size)
{
    void *new;
    new = malloc (size);
    if (!new)
        oom ();
    memset (new, 0, size);
    return new;
}

variable_types:
size
unsigned long

variable_types:
new
void *

return_type: void *


xstrdup

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

result = strdup(a1);
if ( !result )
oom();
return result;
}

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

result = strdup(a1);
if ( !result )
func0();
return result;
}

original_code:
char *xstrdup (const char *s)
{
    char *cpy = strdup (s);
    if (!cpy)
        oom ();
    return cpy;
}

canonical_original_code:
char *xstrdup(char *s)
{
    char *cpy = strdup (s);
    if (!cpy)
        oom ();
    return cpy;
}

variable_types:
s
char *

variable_types:
cpy
char *

return_type: char *


repo: garrybodsworth/jinc

original_code:
void TokenParsingContext_init(struct TokenParsingContext* context, size_t initial_capacity)
{
    context->position = 0;
    context->offset = 0;
    context->parse_start = 0;
    context->parse_start_offset = 0;
    context->parse_token_type = TOKEN_NONE;
    context->capacity = 0;
    context->length = 0;
    context->initial_capacity = initial_capacity;
    context->token_super = -1;
}

canonical_original_code:
void TokenParsingContext_init(struct TokenParsingContext *context, unsigned long initial_capacity)
{
    context->position = 0;
    context->offset = 0;
    context->parse_start = 0;
    context->parse_start_offset = 0;
    context->parse_token_type = TOKEN_NONE;
    context->capacity = 0;
    context->length = 0;
    context->initial_capacity = initial_capacity;
    context->token_super = -1;
}

variable_types: struct TokenParsingContext { int position; int offset; int token_super; int parse_start; int parse_start_offset; enum TokenType { TOKEN_NONE=0, TOKEN_PRIMITIVE=1, TOKEN_OBJECT=2, TOKEN_ARRAY=3, TOKEN_STRING=4, TOKEN_UNICODE=5 } parse_token_type; unsigned long initial_capacity; struct Token { enum TokenType { TOKEN_NONE=0, TOKEN_PRIMITIVE=1, TOKEN_OBJECT=2, TOKEN_ARRAY=3, TOKEN_STRING=4, TOKEN_UNICODE=5 } type; int start; int end; int size; int parent; } *tokens; unsigned long capacity; unsigned long length; } *

user_defined_types: struct TokenParsingContext { int position; int offset; int token_super; int parse_start; int parse_start_offset; enum TokenType parse_token_type; unsigned long initial_capacity; struct Token *tokens; unsigned long capacity; unsigned long length; }

user_defined_types: struct Token { enum TokenType type; int start; int end; int size; int parent; }


decompiled_code:
void __fastcall TokenParsingContext_free(__int64 a1)
{
void *v2; // rdi

v2 = *(void **)(a1 + 32);
if ( v2 )
{
free(v2);
*(_QWORD *)(a1 + 32) = 0LL;
}
}

original_code:
void TokenParsingContext_free(struct TokenParsingContext* context)
{
    if (context->tokens != ((void *)0)) {
        free(context->tokens);
        context->tokens = ((void *)0);
    }
}

canonical_original_code:
void TokenParsingContext_free(struct TokenParsingContext *context)
{
    if (context->tokens != ((void *)0)) {
        free(context->tokens);
        context->tokens = ((void *)0);
    }
}

user_defined_types: enum TokenType { TOKEN_NONE=0, TOKEN_PRIMITIVE=1, TOKEN_OBJECT=2, TOKEN_ARRAY=3, TOKEN_STRING=4, TOKEN_UNICODE=5 }


decompiled_code:
__int64 __fastcall TokenParsingContext_Resize(__int64 a1)
{
unsigned __int64 v1; // rax
unsigned __int64 v2; // rbp
void *v3; // rax

v1 = *(_QWORD *)(a1 + 40);
v2 = *(_QWORD *)(a1 + 24);
if ( v1 >= v2 )
v2 = 2 * v1;
v3 = realloc(*(void **)(a1 + 32), 20 * v2);
if ( !v3 )
return 4294967293LL;
*(_QWORD *)(a1 + 32) = v3;
*(_QWORD *)(a1 + 40) = v2;
return 0LL;
}

original_code:
enum ErrorType TokenParsingContext_Resize(struct TokenParsingContext* context)
{
    size_t new_capacity = (context->capacity < context->initial_capacity) ? context->initial_capacity : context->capacity * 2;
    struct Token* new_items = (struct Token*)realloc(context->tokens, new_capacity * sizeof(struct Token));
    if (new_items == ((void *)0)) {
        return ERROR_ALLOCATION_FAILED;
    }
    context->tokens = new_items;
    context->capacity = new_capacity;
    return SUCCESS;
}

canonical_original_code:
enum ErrorType TokenParsingContext_Resize(struct TokenParsingContext *context)
{
    unsigned long new_capacity = (context->capacity < context->initial_capacity) ? context->initial_capacity : context->capacity * 2;
    struct Token *new_items = (struct Token*)realloc(context->tokens, new_capacity * sizeof(struct Token));
    if (new_items == ((void *)0)) {
        return ERROR_ALLOCATION_FAILED;
    }
    context->tokens = new_items;
    context->capacity = new_capacity;
    return SUCCESS;
}

variable_types: struct Token { enum TokenType { TOKEN_NONE=0, TOKEN_PRIMITIVE=1, TOKEN_OBJECT=2, TOKEN_ARRAY=3, TOKEN_STRING=4, TOKEN_UNICODE=5 } type; int start; int end; int size; int parent; } *

return_type: enum ErrorType { ERROR_INVALID_CHAR=-1, ERROR_INCOMPLETE_JSON=-2, ERROR_ALLOCATION_FAILED=-3, SUCCESS=0 }


original_code:
enum ErrorType TokenParsingContext_Parse(struct TokenParsingContext* context, const char* json)
{
    if (json == ((void *)0)) {
        return ERROR_INCOMPLETE_JSON;
    }
    if (context->position != 0) {
        context->offset += context->position;
        context->position = 0;
    }
    for (; json[context->position] != '\0'; context->position++) {
        char c = json[context->position];
        if (context->parse_token_type == TOKEN_STRING || context->parse_token_type == TOKEN_UNICODE) {
            if (c == '\"') {
                if (((context)->length >= (context)->capacity)) {
                    enum ErrorType error_type = TokenParsingContext_Resize(context);
                    if (error_type != SUCCESS) {
                        return error_type;
                    }
                }
                context->tokens[context->length].type = context->parse_token_type;
                context->tokens[context->length].start = context->parse_start_offset + context->parse_start;
                context->tokens[context->length].end = context->offset + context->position;
                context->tokens[context->length].size = 0;
                context->tokens[context->length++].parent = context->token_super;
                if (context->token_super != -1) {
                    context->tokens[context->token_super].size++;
                }
                context->parse_start = 0;
                context->parse_start_offset = 0;
                context->parse_token_type = TOKEN_NONE;
                continue;
            }
            if (c == '\\') {
                switch (json[++context->position]) {
                    case '\"': case '/': case '\\': case 'b':
                    case 'f': case 'r': case 'n': case 't':
                        break;
                    case 'u':
                        context->parse_token_type = TOKEN_UNICODE;
                        break;
                    default:
                        context->position = context->parse_start;
                        return ERROR_INVALID_CHAR;
                }
            }
            continue;
        }
        if (context->parse_token_type == TOKEN_PRIMITIVE) {
            switch (c) {
                case '\t': case '\r': case '\n': case ' ':
                case ',': case ']': case '}':
                    {
                        if (((context)->length >= (context)->capacity)) {
                            enum ErrorType error_type = TokenParsingContext_Resize(context);
                            if (error_type != SUCCESS) {
                                return error_type;
                            }
                        }
                        context->tokens[context->length].type = context->parse_token_type;
                        context->tokens[context->length].start = context->parse_start_offset + context->parse_start;
                        context->tokens[context->length].end = context->offset + context->position;
                        context->tokens[context->length].size = 0;
                        context->tokens[context->length++].parent = context->token_super;
                        context->parse_start = 0;
                        context->parse_start_offset = 0;
                        context->parse_token_type = TOKEN_NONE;
                        if (context->token_super != -1) {
                            context->tokens[context->token_super].size++;
                        }
                        context->position--;
                    }
                    break;
                default:
                    if (c < 32 || c >= 127) {
                        context->position = context->parse_start;
                        return ERROR_INVALID_CHAR;
                    }
                    break;
            }
            continue;
        }
        switch (c)
        {
            case '{': case '[':
                {
                    int tokparent = -1;
                    if (context->token_super != -1) {
                        context->tokens[context->token_super].size++;
                        tokparent = context->token_super;
                    }
                    if (((context)->length >= (context)->capacity)) {
                        enum ErrorType error_type = TokenParsingContext_Resize(context);
                        if (error_type != SUCCESS) {
                            return error_type;
                        }
                    }
                    context->tokens[context->length].type = (c == '{' ? TOKEN_OBJECT : TOKEN_ARRAY);
                    context->tokens[context->length].start = context->offset + context->position;
                    context->tokens[context->length].end = -1;
                    context->tokens[context->length].size = 0;
                    context->tokens[context->length++].parent = tokparent;
                    context->token_super = context->length - 1;
                }
                break;
            case '}': case ']':
                if (context->length < 1) {
                    return ERROR_INVALID_CHAR;
                }
                int token = context->length - 1;
                while (1) {
                    struct Token* currentToken = &(context->tokens[token]);
                    if (currentToken->start != -1 && currentToken->end == -1) {
                        if (currentToken->type != (c == '}' ? TOKEN_OBJECT : TOKEN_ARRAY)) {
                            return ERROR_INVALID_CHAR;
                        }
                        currentToken->end = context->offset + context->position + 1;
                        context->token_super = currentToken->parent;
                        break;
                    }
                    if (currentToken->parent == -1) {
                        break;
                    }
                    token = currentToken->parent;
                }
                break;
            case '\"':
                context->parse_token_type = TOKEN_STRING;
                context->parse_start = context->position + 1;
                context->parse_start_offset = context->offset;
                break;
            case '\t': case '\r': case '\n': case ':':
            case ',': case ' ':
                break;
            case '-': case '0': case '1': case '2':
            case '3': case '4': case '5': case '6':
            case '7': case '8': case '9': case 't':
            case 'f': case 'n':
                context->parse_token_type = TOKEN_PRIMITIVE;
                context->parse_start = context->position;
                context->parse_start_offset = context->offset;
                break;
            default:
                return ERROR_INVALID_CHAR;
        }
    }
    for (int i ; i < context->length ; i++) {
        if (context->tokens[i].end == -1) {
            return ERROR_INCOMPLETE_JSON;
        }
    }
    return SUCCESS;
}

canonical_original_code:
enum ErrorType TokenParsingContext_Parse(struct TokenParsingContext *context, char *json)
{
    if (json == ((void *)0)) {
        return ERROR_INCOMPLETE_JSON;
    }
    if (context->position != 0) {
        context->offset += context->position;
        context->position = 0;
    }
    for (; json[context->position] != '\0'; context->position++) {
        char c = json[context->position];
        if (context->parse_token_type == TOKEN_STRING || context->parse_token_type == TOKEN_UNICODE) {
            if (c == '\"') {
                if (((context)->length >= (context)->capacity)) {
                    enum ErrorType error_type = TokenParsingContext_Resize(context);
                    if (error_type != SUCCESS) {
                        return error_type;
                    }
                }
                context->tokens[context->length].type = context->parse_token_type;
                context->tokens[context->length].start = context->parse_start_offset + context->parse_start;
                context->tokens[context->length].end = context->offset + context->position;
                context->tokens[context->length].size = 0;
                context->tokens[context->length++].parent = context->token_super;
                if (context->token_super != -1) {
                    context->tokens[context->token_super].size++;
                }
                context->parse_start = 0;
                context->parse_start_offset = 0;
                context->parse_token_type = TOKEN_NONE;
                continue;
            }
            if (c == '\\') {
                switch (json[++context->position]) {
                    case '\"': case '/': case '\\': case 'b':
                    case 'f': case 'r': case 'n': case 't':
                        break;
                    case 'u':
                        context->parse_token_type = TOKEN_UNICODE;
                        break;
                    default:
                        context->position = context->parse_start;
                        return ERROR_INVALID_CHAR;
                }
            }
            continue;
        }
        if (context->parse_token_type == TOKEN_PRIMITIVE) {
            switch (c) {
                case '\t': case '\r': case '\n': case ' ':
                case ',': case ']': case '}':
                    {
                        if (((context)->length >= (context)->capacity)) {
                            enum ErrorType error_type = TokenParsingContext_Resize(context);
                            if (error_type != SUCCESS) {
                                return error_type;
                            }
                        }
                        context->tokens[context->length].type = context->parse_token_type;
                        context->tokens[context->length].start = context->parse_start_offset + context->parse_start;
                        context->tokens[context->length].end = context->offset + context->position;
                        context->tokens[context->length].size = 0;
                        context->tokens[context->length++].parent = context->token_super;
                        context->parse_start = 0;
                        context->parse_start_offset = 0;
                        context->parse_token_type = TOKEN_NONE;
                        if (context->token_super != -1) {
                            context->tokens[context->token_super].size++;
                        }
                        context->position--;
                    }
                    break;
                default:
                    if (c < 32 || c >= 127) {
                        context->position = context->parse_start;
                        return ERROR_INVALID_CHAR;
                    }
                    break;
            }
            continue;
        }
        switch (c)
        {
            case '{': case '[':
                {
                    int tokparent = -1;
                    if (context->token_super != -1) {
                        context->tokens[context->token_super].size++;
                        tokparent = context->token_super;
                    }
                    if (((context)->length >= (context)->capacity)) {
                        enum ErrorType error_type = TokenParsingContext_Resize(context);
                        if (error_type != SUCCESS) {
                            return error_type;
                        }
                    }
                    context->tokens[context->length].type = (c == '{' ? TOKEN_OBJECT : TOKEN_ARRAY);
                    context->tokens[context->length].start = context->offset + context->position;
                    context->tokens[context->length].end = -1;
                    context->tokens[context->length].size = 0;
                    context->tokens[context->length++].parent = tokparent;
                    context->token_super = context->length - 1;
                }
                break;
            case '}': case ']':
                if (context->length < 1) {
                    return ERROR_INVALID_CHAR;
                }
                int token = context->length - 1;
                while (1) {
                    struct Token *currentToken = &(context->tokens[token]);
                    if (currentToken->start != -1 && currentToken->end == -1) {
                        if (currentToken->type != (c == '}' ? TOKEN_OBJECT : TOKEN_ARRAY)) {
                            return ERROR_INVALID_CHAR;
                        }
                        currentToken->end = context->offset + context->position + 1;
                        context->token_super = currentToken->parent;
                        break;
                    }
                    if (currentToken->parent == -1) {
                        break;
                    }
                    token = currentToken->parent;
                }
                break;
            case '\"':
                context->parse_token_type = TOKEN_STRING;
                context->parse_start = context->position + 1;
                context->parse_start_offset = context->offset;
                break;
            case '\t': case '\r': case '\n': case ':':
            case ',': case ' ':
                break;
            case '-': case '0': case '1': case '2':
            case '3': case '4': case '5': case '6':
            case '7': case '8': case '9': case 't':
            case 'f': case 'n':
                context->parse_token_type = TOKEN_PRIMITIVE;
                context->parse_start = context->position;
                context->parse_start_offset = context->offset;
                break;
            default:
                return ERROR_INVALID_CHAR;
        }
    }
    for (int i; i < context->length ; i++) {
        if (context->tokens[i].end == -1) {
            return ERROR_INCOMPLETE_JSON;
        }
    }
    return SUCCESS;
}

variable_types:
json
char *

variable_types: c char

variable_types: enum ErrorType { ERROR_INVALID_CHAR=-1, ERROR_INCOMPLETE_JSON=-2, ERROR_ALLOCATION_FAILED=-3, SUCCESS=0 }

variable_types: tokparent int

variable_types: token int


initStack

repo: garvitlnmiit/WebGraphAlgo

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

result = malloc(0x194uLL);
*(_QWORD *)result = 0LL;
result[100] = -1;
return result;
}

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

result = malloc(0x194uLL);
*(_QWORD *)result = 0LL;
result[100] = -1;
return result;
}

original_code:
extern Stack* initStack() {
 Stack *ptr = (Stack*)malloc( sizeof(Stack) );
 memset( ptr, 0, sizeof(ptr) );
 ptr->sp = -1;
 return ptr;
}

canonical_original_code:
extern struct stack *initStack() {
 struct stack *ptr = (Stack*)malloc( sizeof(Stack) );
 memset( ptr, 0, sizeof(ptr) );
 ptr->sp = -1;
 return ptr;
}

variable_types: struct stack { int array[100]; int sp; } *


push

decompiled_code:
int __fastcall push(__int64 a1, int a2)
{
int v2; // eax
int result; // eax

v2 = *(_DWORD *)(a1 + 400);
if ( v2 == 100 )
return puts("Stack Full");
result = v2 + 1;
*(_DWORD *)(a1 + 400) = result;
*(_DWORD *)(a1 + 4LL * result) = a2;
return result;
}

canonical_decompiled_code:
int __fastcall func13(__int64 a1, int a2)
{
int v2; // eax
int result; // eax

v2 = *(_DWORD *)(a1 + 400);
if ( v2 == 100 )
return puts("Stack Full");
result = v2 + 1;
*(_DWORD *)(a1 + 400) = result;
*(_DWORD *)(a1 + 4LL * result) = a2;
return result;
}

original_code:
extern void push( Stack *ptr , int val ) {
 if( ptr->sp!= 100 ) {
  (ptr->sp)++;
  ptr->array[ptr->sp] = val;
 }else
  printf("Stack Full\n");
}

canonical_original_code:
extern void push(struct stack *ptr, int val) {
 if( ptr->sp!= 100 ) {
  (ptr->sp)++;
  ptr->array[ptr->sp] = val;
 }else
  printf("Stack Full\n");
}

variable_types: val int

user_defined_types: struct stack { int array[100]; int sp; }


pop

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

v1 = *(_DWORD *)(a1 + 400);
if ( v1 == -1 )
return puts("Empty Stack");
*(_DWORD *)(a1 + 4LL * v1) = 0;
result = v1 - 1;
*(_DWORD *)(a1 + 400) = result;
return result;
}

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

v1 = *(_DWORD *)(a1 + 400);
if ( v1 == -1 )
return puts("Empty Stack");
*(_DWORD *)(a1 + 4LL * v1) = 0;
result = v1 - 1;
*(_DWORD *)(a1 + 400) = result;
return result;
}

original_code:
extern void pop( Stack *ptr ) {
 if( ptr->sp != -1 ) {
  ptr->array[ptr->sp] = 0;
  (ptr->sp)--;
 }else
  printf("Empty Stack\n");
}

canonical_original_code:
extern void pop(struct stack *ptr) {
 if( ptr->sp != -1 ) {
  ptr->array[ptr->sp] = 0;
  (ptr->sp)--;
 }else
  printf("Empty Stack\n");
}


peek

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

result = *(unsigned int *)(a1 + 400);
if ( (_DWORD)result != -1 )
result = *(unsigned int *)(a1 + 4LL * (int)result);
return result;
}

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

result = *(unsigned int *)(a1 + 400);
if ( (_DWORD)result != -1 )
result = *(unsigned int *)(a1 + 4LL * (int)result);
return result;
}

original_code:
extern int peek( Stack *ptr ) {
 if( ptr->sp != -1 )
  return ptr->array[ptr->sp];
 else
  return -1;
}

canonical_original_code:
extern int peek(struct stack *ptr) {
 if( ptr->sp != -1 )
  return ptr->array[ptr->sp];
 else
  return -1;
}


contains

decompiled_code:
__int64 __fastcall contains(__int64 a1, int a2)
{
int v2; // edx
__int64 v3; // rax

v2 = *(_DWORD *)(a1 + 400);
if ( v2 < 0 )
return 0LL;
v3 = 0LL;
while ( *(_DWORD *)(a1 + 4 * v3) != a2 )
{
if ( v2 < (int)++v3 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, int a2)
{
int v2; // edx
__int64 v3; // rax

v2 = *(_DWORD *)(a1 + 400);
if ( v2 < 0 )
return 0LL;
v3 = 0LL;
while ( *(_DWORD *)(a1 + 4 * v3) != a2 )
{
if ( v2 < (int)++v3 )
return 0LL;
}
return 1LL;
}

original_code:
extern int contains( Stack *ptr, int val ) {
 int i;
 for( i=0; i<=ptr->sp; i++ )
  if( ptr->array[i] == val )
   return 1;
 return 0;
}

canonical_original_code:
extern int contains(struct stack *ptr, int val) {
 int i;
 for( i=0; i<=ptr->sp; i++ )
  if( ptr->array[i] == val )
   return 1;
 return 0;
}


repo: garyshi/snapdiff

decompiled_code:
__int64 __fastcall align_ptr(__int64 a1, int a2)
{
return a2 + a1 - 1 - (int)((a2 + a1 - 1) % (unsigned __int64)a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, int a2)
{
return a2 + a1 - 1 - (int)((a2 + a1 - 1) % (unsigned __int64)a2);
}

original_code:
void *align_ptr(const void *p, int alignment)
{
 const void *q = p + alignment - 1;
 int mod = ((uint64_t)q) % alignment;
 return (void *)(q - mod);
}

canonical_original_code:
void *align_ptr(void *p, int alignment)
{
 void *q = p + alignment - 1;
 int mod = ((uint64_t)q) % alignment;
 return (void *)(q - mod);
}

variable_types:
p
void *

variable_types: alignment int

variable_types:
q
void *

variable_types: mod int


decompiled_code:
__int64 __fastcall dev_get_size(int a1)
{
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

if ( ioctl(a1, 0x80081272uLL, &v2) >= 0 )
return v2;
perror("ioctl BLKGETSIZE64");
return -1LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1)
{
__int64 v2; // [rsp+8h] [rbp-10h] BYREF

if ( ioctl(a1, 0x80081272uLL, &v2) >= 0 )
return v2;
perror("ioctl BLKGETSIZE64");
return -1LL;
}

variable_types: fd int

variable_types: size long

return_type: long


decompiled_code:
__int64 __fastcall read_chunk(int *a1, __int64 a2, void *a3)
{
int v3; // ebx
void *v4; // rsp
__int64 v5; // rbx
char *v6; // r14
int v7; // er15
__off64_t v8; // rbx
unsigned int v9; // eax
__int64 result; // rax
__int64 v11; // [rsp+0h] [rbp-40h] BYREF
void *dest; // [rsp+8h] [rbp-38h]

dest = a3;
v3 = a1[8];
v4 = alloca(v3 + 4096);
v5 = a2 * v3;
v6 = (char *)align_ptr(&v11, 4096LL);
v7 = a1[8];
if ( (v7 & 0xFFF) != 0 )
v7 += ((unsigned int)(v7 >> 31) >> 20) - (((_WORD)v7 + ((unsigned int)(v7 >> 31) >> 20)) & 0xFFF) + 4096;
v8 = v5 - v5 % 4096;
if ( lseek64(*a1, v8, 0) < 0 )
{
perror("lseek64");
result = 0xFFFFFFFFLL;
}
else
{
v9 = read(*a1, v6, v7);
if ( v7 == v9 )
{
memcpy(dest, &v6[a1[8] * a2 - v8], a1[8]);
result = 0LL;
}
else
{
fprintf(stderr, "read returns %d (expecting %d)\n", v9, (unsigned int)v7);
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int *a1, __int64 a2, void *a3)
{
int v3; // ebx
void *v4; // rsp
__int64 v5; // rbx
char *v6; // r14
int v7; // er15
__off64_t v8; // rbx
unsigned int v9; // eax
__int64 result; // rax
__int64 v11; // [rsp+0h] [rbp-40h] BYREF
void *dest; // [rsp+8h] [rbp-38h]

dest = a3;
v3 = a1[8];
v4 = alloca(v3 + 4096);
v5 = a2 * v3;
v6 = (char *)align_ptr(&v11, 4096LL);
v7 = a1[8];
if ( (v7 & 0xFFF) != 0 )
v7 += ((unsigned int)(v7 >> 31) >> 20) - (((_WORD)v7 + ((unsigned int)(v7 >> 31) >> 20)) & 0xFFF) + 4096;
v8 = v5 - v5 % 4096;
if ( lseek64(*a1, v8, 0) < 0 )
{
perror("lseek64");
result = 0xFFFFFFFFLL;
}
else
{
v9 = read(*a1, v6, v7);
if ( v7 == v9 )
{
memcpy(dest, &v6[a1[8] * a2 - v8], a1[8]);
result = 0LL;
}
else
{
fprintf(stderr, "read returns %d (expecting %d)\n", v9, (unsigned int)v7);
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int read_chunk(struct snapshot_info *info, int64_t chunk, void *buf)
{
 uint8_t buffer[info->chunk_size+4096];
 off64_t offset = chunk * info->chunk_size;
 void *p = align_ptr(buffer, 4096);
 int n, len = info->chunk_size;
 n = len % 4096;
 if (n) len += 4096 - n;
 n = offset % 4096;
 if (n) offset -= n;
 if (lseek64(info->fd, offset, 0) < 0) {
  perror("lseek64");
  return -1;
 }
 n = read(info->fd, p, len);
 if (n != len) {
  fprintf(stderr, "read returns %d (expecting %d)\n", n, len);
  return -1;
 }
 memcpy(buf, p + (chunk * info->chunk_size - offset), info->chunk_size);
 return 0;
}

canonical_original_code:
int read_chunk(struct snapshot_info *info, long chunk, void *buf)
{
 unsigned char buffer[-1];
 long offset = chunk * info->chunk_size;
 void *p = align_ptr(buffer, 4096);
 int n; int len = info->chunk_size;
 n = len % 4096;
 if (n) len += 4096 - n;
 n = offset % 4096;
 if (n) offset -= n;
 if (lseek64(info->fd, offset, 0) < 0) {
  perror("lseek64");
  return -1;
 }
 n = read(info->fd, p, len);
 if (n != len) {
  fprintf(stderr, "read returns %d (expecting %d)\n", n, len);
  return -1;
 }
 memcpy(buf, p + (chunk * info->chunk_size - offset), info->chunk_size);
 return 0;
}

variable_types: struct snapshot_info { int fd; long devsize; unsigned int magic; int chunk_sectors; int exc_per_area; } *

variable_types: chunk long

variable_types:
buf
void *

variable_types:
buffer
unsigned char [-1]

variable_types: offset long

variable_types: n int

variable_types: len int


decompiled_code:
__int64 __fastcall dump_area(int *a1)
{
void *v1; // rsp
__int64 v2; // rax
_QWORD *v3; // rbx
unsigned int v4; // er12
__int64 v5; // rdx
_BYTE v7[32]; // [rsp+0h] [rbp-20h] BYREF

v1 = alloca(a1[8]);
v2 = area_to_chunk();
if ( (int)read_chunk(a1, v2, v7) < 0 )
return (unsigned int)-1;
v3 = v7;
if ( a1[9] <= 0 )
return 0;
v4 = 0;
do
{
v5 = v3[1];
if ( !v5 )
break;
printf("COW %lld => %lld\n", *v3, v5);
++v4;
v3 += 2;
}
while ( 16 * a1[9] > (int)(16 * v4) );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(int *a1)
{
void *v1; // rsp
__int64 v2; // rax
_QWORD *v3; // rbx
unsigned int v4; // er12
__int64 v5; // rdx
_BYTE v7[32]; // [rsp+0h] [rbp-20h] BYREF

v1 = alloca(a1[8]);
v2 = area_to_chunk();
if ( (int)func0(a1, v2, v7) < 0 )
return (unsigned int)-1;
v3 = v7;
if ( a1[9] <= 0 )
return 0;
v4 = 0;
do
{
v5 = v3[1];
if ( !v5 )
break;
printf("COW %lld => %lld\n", *v3, v5);
++v4;
v3 += 2;
}
while ( 16 * a1[9] > (int)(16 * v4) );
return v4;
}

original_code:
int dump_area(struct snapshot_info *info, int64_t area)
{
 uint8_t data[info->chunk_size];
 int64_t chunk = area_to_chunk(info, area);
 int64_t *old_chunk, *new_chunk;
 int i, num_excs;
 if (read_chunk(info, chunk, data) < 0) return -1;
 for (i = 0, num_excs = 0; i < info->exc_per_area * 16; i += 16, ++ num_excs) {
  old_chunk = (int64_t *)(data + i);
  new_chunk = (int64_t *)(data + i + 8);
  if (*new_chunk == 0) break;
  printf("COW %lld => %lld\n", *old_chunk, *new_chunk);
 }
 return num_excs;
}

canonical_original_code:
int dump_area(struct snapshot_info *info, long area)
{
 unsigned char data[-1];
 long chunk = area_to_chunk(info, area);
 long *old_chunk; long *new_chunk;
 int i; int num_excs;
 if (read_chunk(info, chunk, data) < 0) return -1;
 for (i = 0, num_excs = 0; i < info->exc_per_area * 16; i += 16, ++ num_excs) {
  old_chunk = (long *)(data + i);
  new_chunk = (long *)(data + i + 8);
  if (*new_chunk == 0) break;
  printf("COW %lld => %lld\n", *old_chunk, *new_chunk);
 }
 return num_excs;
}

variable_types: area long

variable_types:
data
unsigned char [-1]

variable_types: i int

user_defined_types: struct snapshot_info { int fd; long devsize; unsigned int magic; int chunk_sectors; int exc_per_area; }


decompiled_code:
__int64 __fastcall read_block(int fd, __off64_t a2, __int64 a3, int a4)
{
int v6; // ebx
__int64 result; // rax
int v8; // eax
int v9; // edx

if ( lseek64(fd, a2, 0) < 0 )
{
perror("lseek64");
return 0xFFFFFFFFLL;
}
v6 = 0;
result = 0LL;
if ( a4 > 0 )
{
while ( 1 )
{
v8 = read(fd, (void *)(a3 + v6), a4 - v6);
v9 = v8;
if ( !v8 )
return 0xFFFFFFFFLL;
if ( v8 < 0 )
{
if ( *_errno_location() != 4 )
return 0xFFFFFFFFLL;
v9 = 0;
}
v6 += v9;
if ( a4 <= v6 )
return 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd, __off64_t a2, __int64 a3, int a4)
{
int v6; // ebx
__int64 result; // rax
int v8; // eax
int v9; // edx

if ( lseek64(fd, a2, 0) < 0 )
{
perror("lseek64");
return 0xFFFFFFFFLL;
}
v6 = 0;
result = 0LL;
if ( a4 > 0 )
{
while ( 1 )
{
v8 = read(fd, (void *)(a3 + v6), a4 - v6);
v9 = v8;
if ( !v8 )
return 0xFFFFFFFFLL;
if ( v8 < 0 )
{
if ( *_errno_location() != 4 )
return 0xFFFFFFFFLL;
v9 = 0;
}
v6 += v9;
if ( a4 <= v6 )
return 0LL;
}
}
return result;
}

original_code:
int read_block(int fd, int64_t offset, void *buf, int blksize)
{
 int n, read_bytes;
 if (lseek64(fd, offset, 0) < 0) {
  perror("lseek64");
  return -1;
 }
 for (read_bytes = 0; read_bytes < blksize; read_bytes += n) {
  n = read(fd, buf+read_bytes, blksize-read_bytes);
  if (n == 0) return -1;
  if (n < 0) {
   if ((*__errno_location ()) != 4) return -1;
   n = 0;
  }
 }
 return 0;
}

canonical_original_code:
int read_block(int fd, long offset, void *buf, int blksize)
{
 int n; int read_bytes;
 if (lseek64(fd, offset, 0) < 0) {
  perror("lseek64");
  return -1;
 }
 for (read_bytes = 0; read_bytes < blksize; read_bytes += n) {
  n = read(fd, buf+read_bytes, blksize-read_bytes);
  if (n == 0) return -1;
  if (n < 0) {
   if ((*__errno_location ()) != 4) return -1;
   n = 0;
  }
 }
 return 0;
}

variable_types: blksize int


canonical_decompiled_code:
__int64 __fastcall func1(int *a1, __int64 a2, void *a3)
{
int v3; // ebx
void *v4; // rsp
__int64 v5; // rbx
char *v6; // r14
int v7; // er15
__off64_t v8; // rbx
unsigned int v9; // eax
__int64 result; // rax
__int64 v11; // [rsp+0h] [rbp-40h] BYREF
void *dest; // [rsp+8h] [rbp-38h]

dest = a3;
v3 = a1[8];
v4 = alloca(v3 + 4096);
v5 = a2 * v3;
v6 = (char *)align_ptr(&v11, 4096LL);
v7 = a1[8];
if ( (v7 & 0xFFF) != 0 )
v7 += ((unsigned int)(v7 >> 31) >> 20) - (((_WORD)v7 + ((unsigned int)(v7 >> 31) >> 20)) & 0xFFF) + 4096;
v8 = v5 - v5 % 4096;
if ( lseek64(*a1, v8, 0) < 0 )
{
perror("lseek64");
result = 0xFFFFFFFFLL;
}
else
{
v9 = read(*a1, v6, v7);
if ( v7 == v9 )
{
memcpy(dest, &v6[a1[8] * a2 - v8], a1[8]);
result = 0LL;
}
else
{
fprintf(stderr, "read returns %d (expecting %d)\n", v9, (unsigned int)v7);
result = 0xFFFFFFFFLL;
}
}
return result;
}

variable_types:
p
void *


decompiled_code:
__int64 __fastcall show_diff(__int64 a1, __int64 a2, int a3)
{
int v3; // er15
unsigned __int8 *v4; // r13
unsigned __int8 *v5; // r12
__int64 v6; // r14
unsigned __int8 *v7; // rbx
unsigned __int8 *v8; // rbp

v3 = a3 - 16;
if ( a3 - 16 > 0 )
{
v4 = (unsigned __int8 *)(a2 + 16);
v5 = (unsigned __int8 *)(a1 + 16);
v6 = 0LL;
do
{
v7 = (unsigned __int8 *)(a2 + v6);
v8 = (unsigned __int8 *)(a1 + v6);
if ( memcmp((const void *)(a1 + v6), (const void *)(a2 + v6), 0x10uLL) )
{
printf("%04x ", (unsigned int)v6);
do
printf("%02x", *v8++);
while ( v8 != v5 );
putchar(32);
do
printf("%02x", *v7++);
while ( v7 != v4 );
putchar(10);
}
v4 += 16;
v5 += 16;
v6 += 16LL;
}
while ( v3 > (int)v6 );
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, int a3)
{
int v3; // er15
unsigned __int8 *v4; // r13
unsigned __int8 *v5; // r12
__int64 v6; // r14
unsigned __int8 *v7; // rbx
unsigned __int8 *v8; // rbp

v3 = a3 - 16;
if ( a3 - 16 > 0 )
{
v4 = (unsigned __int8 *)(a2 + 16);
v5 = (unsigned __int8 *)(a1 + 16);
v6 = 0LL;
do
{
v7 = (unsigned __int8 *)(a2 + v6);
v8 = (unsigned __int8 *)(a1 + v6);
if ( memcmp((const void *)(a1 + v6), (const void *)(a2 + v6), 0x10uLL) )
{
printf("%04x ", (unsigned int)v6);
do
printf("%02x", *v8++);
while ( v8 != v5 );
putchar(32);
do
printf("%02x", *v7++);
while ( v7 != v4 );
putchar(10);
}
v4 += 16;
v5 += 16;
v6 += 16LL;
}
while ( v3 > (int)v6 );
}
return 0LL;
}

original_code:
int show_diff(uint8_t *p1, uint8_t *p2, int size)
{
 const int linelen = 16;
 int i, j;
 for (i = 0; i < size-linelen; i += linelen) {
  if (memcmp(p1+i, p2+i, linelen) == 0) continue;
  printf("%04x ", i);
  for (j = 0; j < linelen; ++ j) printf("%02x", p1[i+j]);
  putchar(' ');
  for (j = 0; j < linelen; ++ j) printf("%02x", p2[i+j]);
  putchar('\n');
 }
 return 0;
}

canonical_original_code:
int show_diff(unsigned char *p1, unsigned char *p2, int size)
{
 int linelen = 16;
 int i; int j;
 for (i = 0; i < size-linelen; i += linelen) {
  if (memcmp(p1+i, p2+i, linelen) == 0) continue;
  printf("%04x ", i);
  for (j = 0; j < linelen; ++ j) printf("%02x", p1[i+j]);
  putchar(' ');
  for (j = 0; j < linelen; ++ j) printf("%02x", p2[i+j]);
  putchar('\n');
 }
 return 0;
}

variable_types: size int

variable_types: linelen int

variable_types: j int


decompiled_code:
__int64 __fastcall diff_area(int *a1)
{
__int64 v1; // rsi
void *v2; // rsp
__off64_t *v3; // rbx
void *v4; // rsp
void *v5; // rsp
unsigned int v6; // er13
__off64_t v7; // rdx
_BYTE v9[48]; // [rsp+0h] [rbp-30h] BYREF

v1 = area_to_chunk();
v2 = alloca(a1[8]);
v3 = (__off64_t *)v9;
v4 = alloca(a1[8]);
v5 = alloca(a1[8]);
if ( (int)read_chunk(a1, v1, v9) < 0 )
return (unsigned int)-1;
if ( a1[9] <= 0 )
return 0;
v6 = 0;
do
{
v7 = v3[1];
if ( !v7 )
break;
printf("COW %lld => %lld\n", *v3, v7);
read_chunk(a1, v3[1], v9);
read_block(a1[1], *v3, (__int64)v9, a1[8]);
show_diff((__int64)v9, (__int64)v9, a1[8]);
putchar(10);
++v6;
v3 += 2;
}
while ( 16 * a1[9] > (int)(16 * v6) );
return v6;
}

original_code:
int diff_area(struct snapshot_info *info, int64_t area)
{
 int i, j, num_excs;
 int64_t *old_chunk, *new_chunk;
 int64_t chunk = area_to_chunk(info, area);
 uint8_t data[info->chunk_size], old_buf[info->chunk_size], new_buf[info->chunk_size];
 if (read_chunk(info, chunk, data) < 0) return -1;
 for (i = 0, num_excs = 0; i < info->exc_per_area * 16; i += 16, ++ num_excs) {
  old_chunk = (int64_t *)(data + i);
  new_chunk = (int64_t *)(data + i + 8);
  if (*new_chunk == 0) break;
  printf("COW %lld => %lld\n", *old_chunk, *new_chunk);
  read_chunk(info, *new_chunk, new_buf);
  read_block(info->fd_origin, *old_chunk, old_buf, info->chunk_size);
  show_diff(old_buf, new_buf, info->chunk_size);
  putchar('\n');
 }
 return num_excs;
}

canonical_original_code:
int diff_area(struct snapshot_info *info, long area)
{
 int i; int j; int num_excs;
 long *old_chunk; long *new_chunk;
 long chunk = area_to_chunk(info, area);
 unsigned char data[-1]; unsigned char old_buf[-1]; unsigned char new_buf[-1];
 if (read_chunk(info, chunk, data) < 0) return -1;
 for (i = 0, num_excs = 0; i < info->exc_per_area * 16; i += 16, ++ num_excs) {
  old_chunk = (long *)(data + i);
  new_chunk = (long *)(data + i + 8);
  if (*new_chunk == 0) break;
  printf("COW %lld => %lld\n", *old_chunk, *new_chunk);
  read_chunk(info, *new_chunk, new_buf);
  read_block(info->fd_origin, *old_chunk, old_buf, info->chunk_size);
  show_diff(old_buf, new_buf, info->chunk_size);
  putchar('\n');
 }
 return num_excs;
}


repo: gasman/divideo

decompiled_code:
_BOOL8 __fastcall sequences_match(__int64 a1, __int64 a2, __int64 a3)
{
unsigned __int64 v5; // rdx

v5 = a3 + 6;
if ( v5 > 0x1B00 )
v5 = 6912LL;
return memcmp((const void *)(a1 + a3), (const void *)(a3 + a2), v5 - a3) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(__int64 a1, __int64 a2, __int64 a3)
{
unsigned __int64 v5; // rdx

v5 = a3 + 6;
if ( v5 > 0x1B00 )
v5 = 6912LL;
return memcmp((const void *)(a1 + a3), (const void *)(a3 + a2), v5 - a3) == 0;
}

original_code:
static int sequences_match(unsigned char *old_frame, unsigned char *new_frame, size_t addr) {
 size_t end = addr + 6;
 if (end > 0x1b00) end = 0x1b00;
 return ( memcmp(old_frame + addr, new_frame + addr, end - addr) == 0 );
}

canonical_original_code:
static int sequences_match(unsigned char *old_frame, unsigned char *new_frame, unsigned long addr) {
 unsigned long end = addr + 6;
 if (end > 0x1b00) end = 0x1b00;
 return ( memcmp(old_frame + addr, new_frame + addr, end - addr) == 0 );
}

variable_types:
addr
unsigned long

variable_types:
end
unsigned long


original_code:
int delta_compressor_compute_deltas(unsigned char *old_frame, unsigned char *new_frame, st_delta *deltas, int max_delta_length) {
 size_t addr = 0;
 size_t delta_offset, delta_length;
 int delta_count = 0;
 while (addr < 0x1b00) {
  while (old_frame[addr] == new_frame[addr] && addr < 0x1b00) {
   addr++;
  }
  if (addr == 0x1b00) break;
  delta_offset = addr;
  delta_length = 0;
  while ( addr < 0x1b00 && delta_length < max_delta_length && !sequences_match(old_frame, new_frame, addr) ) {
   delta_length += 2;
   addr += 2;
  }
  if (delta_offset + delta_length > 0x1b00) {
   delta_offset -= 1;
  }
  deltas[delta_count].offset = delta_offset;
  deltas[delta_count].length = delta_length;
  deltas[delta_count].time = -1;
  delta_count++;
 }
 return delta_count;
}

canonical_original_code:
int delta_compressor_compute_deltas(unsigned char *old_frame, unsigned char *new_frame, struct delta *deltas, int max_delta_length) {
 unsigned long addr = 0;
 unsigned long delta_offset; unsigned long delta_length;
 int delta_count = 0;
 while (addr < 0x1b00) {
  while (old_frame[addr] == new_frame[addr] && addr < 0x1b00) {
   addr++;
  }
  if (addr == 0x1b00) break;
  delta_offset = addr;
  delta_length = 0;
  while ( addr < 0x1b00 && delta_length < max_delta_length && !sequences_match(old_frame, new_frame, addr) ) {
   delta_length += 2;
   addr += 2;
  }
  if (delta_offset + delta_length > 0x1b00) {
   delta_offset -= 1;
  }
  deltas[delta_count].offset = delta_offset;
  deltas[delta_count].length = delta_length;
  deltas[delta_count].time = -1;
  delta_count++;
 }
 return delta_count;
}

variable_types: struct delta { unsigned long offset; unsigned long length; double time; } *


decompiled_code:
int __fastcall delta_compressor_compute_optimal_deltas(__int64 a1, __int64 a2, __m128i *a3, unsigned int *a4)
{
int *v5; // rbx
unsigned int v6; // ebp
int v7; // eax
double v8; // xmm0_8
__int64 v9; // rdx
int result; // eax
double v12; // [rsp+10h] [rbp-48h] BYREF
unsigned int v13[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

v5 = (int *)&max_delta_length_candidates;
do
{
v6 = *v5;
v7 = delta_compressor_compute_deltas(a1, a2, deltas_0, *v5);
delta_compressor_pack_deltas((__int64)deltas_0, v7, a3, &v12, v13);
v8 = v12;
if ( v12 <= 141500.0 )
break;
++v5;
}
while ( v5 != (int *)&LC0 );
v9 = v13[0];
*a4 = v13[0];
result = printf("delta len: %d\tpackets: %d\taudio: %dHz\tframe time: %f\n", v6, v9, (unsigned int)(25 * v9), v8);
if ( v12 > 141500.0 )
result = puts("WARNING: too much video data for one frame - playback will probably be jittery");
return result;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1, __int64 a2, __m128i *a3, unsigned int *a4)
{
int *v5; // rbx
unsigned int v6; // ebp
int v7; // eax
double v8; // xmm0_8
__int64 v9; // rdx
int result; // eax
double v12; // [rsp+10h] [rbp-48h] BYREF
unsigned int v13[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

v5 = (int *)&max_delta_length_candidates;
do
{
v6 = *v5;
v7 = func1(a1, a2, deltas_0, *v5);
func2((__int64)deltas_0, v7, a3, &v12, v13);
v8 = v12;
if ( v12 <= 141500.0 )
break;
++v5;
}
while ( v5 != (int *)&LC0 );
v9 = v13[0];
*a4 = v13[0];
result = printf("delta len: %d\tpackets: %d\taudio: %dHz\tframe time: %f\n", v6, v9, (unsigned int)(25 * v9), v8);
if ( v12 > 141500.0 )
result = puts("WARNING: too much video data for one frame - playback will probably be jittery");
return result;
}

original_code:
void delta_compressor_compute_optimal_deltas(unsigned char *old_frame, unsigned char *new_frame, st_delta *packed_deltas, int *final_delta_count_out) {
 int delta_count, final_delta_count, max_delta_length;
 double frame_time;
 static st_delta deltas[10000];
 int i;
 for (i = 0; i < 18; i++) {
  max_delta_length = max_delta_length_candidates[i];
  delta_count = delta_compressor_compute_deltas(old_frame, new_frame, deltas, max_delta_length);
  delta_compressor_pack_deltas(deltas, delta_count, packed_deltas, &frame_time, &final_delta_count);
  if (frame_time <= 141500) break;
 }
 *final_delta_count_out = final_delta_count;
 printf("delta len: %d\tpackets: %d\taudio: %dHz\tframe time: %f\n", max_delta_length, final_delta_count, final_delta_count*25, frame_time);
 if (frame_time > 141500) {
  printf("WARNING: too much video data for one frame - playback will probably be jittery\n");
 }
}

canonical_original_code:
void delta_compressor_compute_optimal_deltas(unsigned char *old_frame, unsigned char *new_frame, struct delta *packed_deltas, int *final_delta_count_out) {
 int delta_count; int final_delta_count; int max_delta_length;
 double frame_time;
 static struct delta deltas[10000];
 int i;
 for (i = 0; i < 18; i++) {
  max_delta_length = max_delta_length_candidates[i];
  delta_count = delta_compressor_compute_deltas(old_frame, new_frame, deltas, max_delta_length);
  delta_compressor_pack_deltas(deltas, delta_count, packed_deltas, &frame_time, &final_delta_count);
  if (frame_time <= 141500) break;
 }
 *final_delta_count_out = final_delta_count;
 printf("delta len: %d\tpackets: %d\taudio: %dHz\tframe time: %f\n", max_delta_length, final_delta_count, final_delta_count*25, frame_time);
 if (frame_time > 141500) {
  printf("WARNING: too much video data for one frame - playback will probably be jittery\n");
 }
}

variable_types: struct delta { unsigned long offset; unsigned long length; double time; } [10000]


original_code:
static void init_rendering(st_converter_data *convdata, st_rendering *rendering, int bright, int paper, int ink) {
 int x, y, i;
 float d;
 rendering->attribute = (bright << 6) | (paper << 3) | ink;
 memcpy(rendering->ink, zx_palette[bright][ink], 4);
 memcpy(rendering->paper, zx_palette[bright][paper], 4);
 for (y = 0; y < convdata->dither_map_size; y++) {
  for (x = 0; x < convdata->dither_map_size; x++) {
   for (i = 0; i < 3; i++) {
    d = (float) ((*(convdata->dither_map))[y][x]);
    d = d / (convdata->dither_map_size * convdata->dither_map_size + 1) - 0.5;
    rendering->threshold_map[y][x][i] = ((float)(rendering->paper[i] - rendering->ink[i])) * d;
   }
  }
 }
}

canonical_original_code:
static void init_rendering(struct converter_data *convdata, struct rendering *rendering, int bright, int paper, int ink) {
 int x; int y; int i;
 float d;
 rendering->attribute = (bright << 6) | (paper << 3) | ink;
 memcpy(rendering->ink, zx_palette[bright][ink], 4);
 memcpy(rendering->paper, zx_palette[bright][paper], 4);
 for (y = 0; y < convdata->dither_map_size; y++) {
  for (x = 0; x < convdata->dither_map_size; x++) {
   for (i = 0; i < 3; i++) {
    d = (float) ((*(convdata->dither_map))[y][x]);
    d = d / (convdata->dither_map_size * convdata->dither_map_size + 1) - 0.5;
    rendering->threshold_map[y][x][i] = ((float)(rendering->paper[i] - rendering->ink[i])) * d;
   }
  }
 }
}

variable_types: struct converter_data { struct rendering { unsigned char attribute; unsigned char ink[4]; unsigned char paper[4]; float threshold_map[4][4][3]; char bitmap[8][8]; } renderings[56]; int rendering_count; int (*dither_map)[4][4]; int dither_map_size; } *

variable_types:
rendering
struct rendering *

variable_types: bright int

variable_types: paper int

variable_types: ink int

variable_types: x int

variable_types: y int

variable_types: d float

user_defined_types: struct converter_data { struct rendering renderings[56]; int rendering_count; int (*dither_map)[4][4]; int dither_map_size; }


original_code:
void image_to_spectrum_converter_init(st_converter_data *convdata, int use_colour, int dither_map_size) {
 int bright, ink, paper, i;
 switch (dither_map_size) {
  case 4:
   convdata->dither_map = &dither_map_4x4;
   convdata->dither_map_size = 4;
   break;
  case 2:
   convdata->dither_map = &dither_map_2x2;
   convdata->dither_map_size = 2;
   break;
  default:
   convdata->dither_map = &dither_map_1x1;
   convdata->dither_map_size = 1;
   break;
 }
 if (use_colour) {
  i = 0;
  for (bright = 0; bright < 2; bright++) {
   for (paper = 0; paper < 7; paper++) {
    for (ink = paper+1; ink < 8; ink++) {
     init_rendering(convdata, &(convdata->renderings[i]), bright, paper, ink);
     i++;
    }
   }
  }
  convdata->rendering_count = i;
 } else {
  convdata->rendering_count = 1;
  init_rendering(convdata, &(convdata->renderings[0]), 1, 0, 7);
 }
}

canonical_original_code:
void image_to_spectrum_converter_init(struct converter_data *convdata, int use_colour, int dither_map_size) {
 int bright; int ink; int paper; int i;
 switch (dither_map_size) {
  case 4:
   convdata->dither_map = &dither_map_4x4;
   convdata->dither_map_size = 4;
   break;
  case 2:
   convdata->dither_map = &dither_map_2x2;
   convdata->dither_map_size = 2;
   break;
  default:
   convdata->dither_map = &dither_map_1x1;
   convdata->dither_map_size = 1;
   break;
 }
 if (use_colour) {
  i = 0;
  for (bright = 0; bright < 2; bright++) {
   for (paper = 0; paper < 7; paper++) {
    for (ink = paper+1; ink < 8; ink++) {
     init_rendering(convdata, &(convdata->renderings[i]), bright, paper, ink);
     i++;
    }
   }
  }
  convdata->rendering_count = i;
 } else {
  convdata->rendering_count = 1;
  init_rendering(convdata, &(convdata->renderings[0]), 1, 0, 7);
 }
}

variable_types: i int

user_defined_types: struct rendering { unsigned char attribute; unsigned char ink[4]; unsigned char paper[4]; float threshold_map[4][4][3]; char bitmap[8][8]; }


original_code:
void image_to_spectrum_convert(st_converter_data *convdata, unsigned char *image_data, unsigned char *zx_data) {
 static unsigned char original_bitmap[8*8*4];
 unsigned char third, ychar, xchar, ypix, zx_byte;
 int i, rendering_closeness;
 int best_rendering_closeness;
 st_rendering *best_rendering;
 for (third = 0; third < 3; third++) {
  for (ychar = 0; ychar < 8; ychar++) {
   for (xchar = 0; xchar < 32; xchar++) {
    for (ypix = 0; ypix < 8; ypix++) {
     memcpy(original_bitmap + (ypix << 5), image_data + ( (third << 16) | (ychar << 13) | (ypix << 10) | (xchar << 5) ), 32);
    }
    best_rendering_closeness = 0xffffff;
    for (i = 0; i < convdata->rendering_count; i++) {
     rendering_closeness = render_char(convdata, &(convdata->renderings[i]), original_bitmap);
     if (rendering_closeness < best_rendering_closeness) {
      best_rendering_closeness = rendering_closeness;
      best_rendering = &(convdata->renderings[i]);
     }
    }
    zx_data[0x1800 | (third << 8) | (ychar << 5) | xchar] = best_rendering->attribute;
    for (ypix = 0; ypix < 8; ypix++) {
     zx_byte = 0;
     for (i = 0; i < 8; i++) {
      if (best_rendering->bitmap[ypix][i]) {
       zx_byte |= (1 << (7 - i));
      }
     }
     zx_data[(third << 11) | (ypix << 8) | (ychar << 5) | xchar] = zx_byte;
    }
   }
  }
 }
}

canonical_original_code:
void image_to_spectrum_convert(struct converter_data *convdata, unsigned char *image_data, unsigned char *zx_data) {
 static unsigned char original_bitmap[-1];
 unsigned char third; unsigned char ychar; unsigned char xchar; unsigned char ypix; unsigned char zx_byte;
 int i; int rendering_closeness;
 int best_rendering_closeness;
 struct rendering *best_rendering;
 for (third = 0; third < 3; third++) {
  for (ychar = 0; ychar < 8; ychar++) {
   for (xchar = 0; xchar < 32; xchar++) {
    for (ypix = 0; ypix < 8; ypix++) {
     memcpy(original_bitmap + (ypix << 5), image_data + ( (third << 16) | (ychar << 13) | (ypix << 10) | (xchar << 5) ), 32);
    }
    best_rendering_closeness = 0xffffff;
    for (i = 0; i < convdata->rendering_count; i++) {
     rendering_closeness = render_char(convdata, &(convdata->renderings[i]), original_bitmap);
     if (rendering_closeness < best_rendering_closeness) {
      best_rendering_closeness = rendering_closeness;
      best_rendering = &(convdata->renderings[i]);
     }
    }
    zx_data[0x1800 | (third << 8) | (ychar << 5) | xchar] = best_rendering->attribute;
    for (ypix = 0; ypix < 8; ypix++) {
     zx_byte = 0;
     for (i = 0; i < 8; i++) {
      if (best_rendering->bitmap[ypix][i]) {
       zx_byte |= (1 << (7 - i));
      }
     }
     zx_data[(third << 11) | (ypix << 8) | (ychar << 5) | xchar] = zx_byte;
    }
   }
  }
 }
}

variable_types:
third
unsigned char

variable_types:
ychar
unsigned char

variable_types:
xchar
unsigned char

variable_types:
ypix
unsigned char

variable_types: struct rendering { unsigned char attribute; unsigned char ink[4]; unsigned char paper[4]; float threshold_map[4][4][3]; char bitmap[8][8]; } *


repo: gattschardo/tetris

decompiled_code:
_BOOL8 __fastcall assert_eq(const char *a1, unsigned int a2, __int64 a3)
{
int v3; // ebx

v3 = a3;
if ( a2 != (_DWORD)a3 )
printf("%s: expected %d, got %d\n", a1, a3, a2);
return a2 == v3;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(const char *a1, unsigned int a2, __int64 a3)
{
int v3; // ebx

v3 = a3;
if ( a2 != (_DWORD)a3 )
printf("%s: expected %d, got %d\n", a1, a3, a2);
return a2 == v3;
}

original_code:
int assert_eq(char const *f, int a, int b)
{
 if (a != b) {
  printf("%s: expected %d, got %d\n", f, b, a);
 }
 return a == b;
}

canonical_original_code:
int assert_eq(char *f, int a, int b)
{
 if (a != b) {
  printf("%s: expected %d, got %d\n", f, b, a);
 }
 return a == b;
}

variable_types:
f
char *

variable_types: a int

variable_types: b int


decompiled_code:
_BOOL8 test_const_modf_state_const()
{
unsigned int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1[0] = 5;
const_modf(v1, 3LL);
return assert_eq("test_const_modf_state_const", v1[0], 5LL);
}

canonical_decompiled_code:
_BOOL8 func8()
{
unsigned int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1[0] = 5;
func11(v1, 3LL);
return func7("test_const_modf_state_const", v1[0], 5LL);
}

original_code:
int test_const_modf_state_const(void)
{
 int s = 5;
 const_modf(&s, 3);
 return assert_eq(__func__, s, 5);
}

canonical_original_code:
int test_const_modf_state_const()
{
 int s = 5;
 const_modf(&s, 3);
 return assert_eq(__func__, s, 5);
}

variable_types: s int


decompiled_code:
_BOOL8 test_const_modf_ret_const()
{
unsigned int v0; // eax
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = 3;
v0 = const_modf(v2, 7LL);
return assert_eq("test_const_modf_ret_const", v0, 3LL);
}

canonical_decompiled_code:
_BOOL8 func9()
{
unsigned int v0; // eax
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = 3;
v0 = func11(v2, 7LL);
return func7("test_const_modf_ret_const", v0, 3LL);
}

original_code:
int test_const_modf_ret_const(void)
{
 int s = 3;
 int v = 7;
 int r = const_modf(&s, v);
 return assert_eq(__func__, r, 3);
}

canonical_original_code:
int test_const_modf_ret_const()
{
 int s = 3;
 int v = 7;
 int r = const_modf(&s, v);
 return assert_eq(__func__, r, 3);
}

variable_types: v int

variable_types: r int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( !test_const_modf_state_const() )
{
printf("Test failed: %s\n", "test_const_modf_state_const");
exit(1);
}
if ( !test_const_modf_ret_const() )
{
printf("Test failed: %s\n", "test_const_modf_ret_const");
exit(1);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( !func8() )
{
printf("Test failed: %s\n", "test_const_modf_state_const");
exit(1);
}
if ( !func9() )
{
printf("Test failed: %s\n", "test_const_modf_ret_const");
exit(1);
}
return 0;
}

original_code:
int main(void)
{
 if (!test_const_modf_state_const()) { printf("Test failed: %s\n", "test_const_modf_state_const"); exit(1); };
 if (!test_const_modf_ret_const()) { printf("Test failed: %s\n", "test_const_modf_ret_const"); exit(1); };
 return 0;
}

canonical_original_code:
int main()
{
 if (!test_const_modf_state_const()) { printf("Test failed: %s\n", "test_const_modf_state_const"); exit(1); };
 if (!test_const_modf_ret_const()) { printf("Test failed: %s\n", "test_const_modf_ret_const"); exit(1); };
 return 0;
}


decompiled_code:
__int64 __fastcall const_modf(unsigned int *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned int *a1)
{
return *a1;
}

original_code:
int const_modf(int *s, int v)
{
 return *s;
}

variable_types:
s
int *


orf

decompiled_code:
__int64 __fastcall orf(_DWORD *a1, unsigned int a2)
{
*a1 = a2 == 1 || *a1 != 0;
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func12(_DWORD *a1, unsigned int a2)
{
*a1 = a2 == 1 || *a1 != 0;
return a2;
}

original_code:
int orf(int *s, int v)
{
 *s = *s || (v == 1);
 return v;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdx
const char *v4; // rcx
__int64 v5; // r8
__int64 v7; // [rsp+0h] [rbp-38h] BYREF

puts("Enter your the info: \n[fName LName age]");
__isoc99_fscanf();
print_intro(&v7, "%s %s %d", v3, v4, v5);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdx
const char *v4; // rcx
__int64 v5; // r8
__int64 v7; // [rsp+0h] [rbp-38h] BYREF

puts("Enter your the info: \n[fName LName age]");
__isoc99_fscanf();
func9(&v7, "%s %s %d", v3, v4, v5);
return 0;
}

original_code:
int main(int argc,char **argv){
  INTRO_t intro;
  printf("Enter your the info: \n[fName LName age]\n");
  fscanf(stdin,"%s %s %d",intro.firstName,intro.lastName,&(intro.age));
  print_intro(&intro);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv){
  struct intro_s intro;
  printf("Enter your the info: \n[fName LName age]\n");
  fscanf(stdin,"%s %s %d",intro.firstName,intro.lastName,&(intro.age));
  print_intro(&intro);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct intro_s { char firstName[20]; char lastName[20]; int age; }


decompiled_code:
int print_an_address()
{
const char *v1[7]; // [rsp+0h] [rbp-38h] BYREF

v1[0] = "1823 23rd Ave NE";
v1[1] = "Seatle";
v1[2] = "WA";
v1[4] = "98023";
return print_address(v1);
}

original_code:
void print_an_address(void)
{
  ADDRESS_t address;
    address.street="1823 23rd Ave NE";
    address.city ="Seatle";
    address.region="WA";
    address.postal_code = "98023";
  print_address(&address);
}

canonical_original_code:
void print_an_address()
{
  struct address_s address;
    address.street="1823 23rd Ave NE";
    address.city ="Seatle";
    address.region="WA";
    address.postal_code = "98023";
  print_address(&address);
}

variable_types: struct address_s { char *street; char *city; char *region; char *country; char *postal_code; }


repo: gavincabbage/nim

decompiled_code:
__int64 __fastcall s_send(int fd, __int64 a2, int a3)
{
int v3; // ebx
int v4; // ebp
int v5; // eax

if ( a3 <= 0 )
return 0LL;
v3 = a3;
v4 = 0;
while ( 1 )
{
v5 = write(fd, (const void *)(a2 + v4), v3);
if ( v5 < 0 )
break;
v3 -= v5;
v4 += v5;
if ( v3 <= 0 )
return 0LL;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func7(int fd, __int64 a2, int a3)
{
int v3; // ebx
int v4; // ebp
int v5; // eax

if ( a3 <= 0 )
return 0LL;
v3 = a3;
v4 = 0;
while ( 1 )
{
v5 = write(fd, (const void *)(a2 + v4), v3);
if ( v5 < 0 )
break;
v3 -= v5;
v4 += v5;
if ( v3 <= 0 )
return 0LL;
}
return 0xFFFFFFFFLL;
}

original_code:
int s_send(int sock, void *buffer, int size) {
 int to_send = size; int sent = 0; int num;
 while (to_send > 0) {
  if ( (num = write(sock, buffer+sent, to_send)) < 0 )
   return -1;
  else { to_send -= num; sent += num; }
 } return 0;
}

variable_types: sock int

variable_types:
buffer
void *

variable_types: size int

variable_types: sent int

variable_types: num int


decompiled_code:
__int64 __fastcall s_recv(int fd, __int64 a2, int a3)
{
int v3; // ebx
int v4; // ebp
int v5; // eax

if ( a3 <= 0 )
return 0LL;
v3 = a3;
v4 = 0;
while ( 1 )
{
v5 = read(fd, (void *)(a2 + v4), v3);
if ( v5 < 0 )
break;
v3 -= v5;
v4 += v5;
if ( v3 <= 0 )
return 0LL;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func8(int fd, __int64 a2, int a3)
{
int v3; // ebx
int v4; // ebp
int v5; // eax

if ( a3 <= 0 )
return 0LL;
v3 = a3;
v4 = 0;
while ( 1 )
{
v5 = read(fd, (void *)(a2 + v4), v3);
if ( v5 < 0 )
break;
v3 -= v5;
v4 += v5;
if ( v3 <= 0 )
return 0LL;
}
return 0xFFFFFFFFLL;
}

original_code:
int s_recv(int sock, void *buffer, int size) {
 int to_rec = size; int rec = 0; int num;
 while (to_rec > 0) {
  if ( (num = read(sock, buffer+rec, to_rec)) < 0 )
   return -1;
  else { to_rec -= num; rec += num; }
 } return 0;
}

variable_types: rec int


decompiled_code:
__int64 __fastcall update_board(int a1, int a2)
{
int v2; // edi
__int64 result; // rax
int v4; // esi

v2 = a1 - 1;
result = (unsigned int)(7 * v2 + a2 - 1);
v4 = 7 * v2 + 7;
if ( (int)result < v4 )
{
do
{
*(_BYTE *)(board + (int)result) = 88;
result = (unsigned int)(result + 1);
}
while ( v4 != (_DWORD)result );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, int a2)
{
int v2; // edi
__int64 result; // rax
int v4; // esi

v2 = a1 - 1;
result = (unsigned int)(7 * v2 + a2 - 1);
v4 = 7 * v2 + 7;
if ( (int)result < v4 )
{
do
{
*(_BYTE *)(board + (int)result) = 88;
result = (unsigned int)(result + 1);
}
while ( v4 != (_DWORD)result );
}
return result;
}

original_code:
void update_board(int row, int col) {
 int start = (7 * (row - 1)) + col - 1;
 int end = 7 * row;
 int i;
 for (i = start; i < end; i++) {
  board->board[i] = 'X';
 }
}

variable_types: row int

variable_types: col int

variable_types: start int

variable_types: end int

variable_types: i int


decompiled_code:
__int64 game_over()
{
_BYTE *v0; // rax

v0 = (_BYTE *)board;
while ( *v0 != 79 )
{
if ( ++v0 == (_BYTE *)(board + 28) )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 func10()
{
_BYTE *v0; // rax

v0 = (_BYTE *)board;
while ( *v0 != 79 )
{
if ( ++v0 == (_BYTE *)(board + 28) )
return 1LL;
}
return 0LL;
}

original_code:
int game_over() {
 int i;
 for (i = 0; i < 28; i++) {
  if (board->board[i] == 'O') return 0;
 }
 return 1;
}


isLeft

decompiled_code:
_BOOL8 __fastcall isLeft(char a1)
{
_BOOL8 result; // rax

if ( a1 == 91 )
return 1LL;
if ( a1 > 91 )
return a1 == 123;
result = 1LL;
if ( a1 != 40 )
result = a1 == 60;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(char a1)
{
_BOOL8 result; // rax

if ( a1 == 91 )
return 1LL;
if ( a1 > 91 )
return a1 == 123;
result = 1LL;
if ( a1 != 40 )
result = a1 == 60;
return result;
}

original_code:
int isLeft(char c){
 int ret = 0;
 switch(c){
 case '<':
 case '{':
 case '[':
 case '(':
  ret = 1;
  break;
 default:
  ret = 0;
  break;
 }
 return ret;
}

variable_types: c char

variable_types: ret int


isRight

decompiled_code:
_BOOL8 __fastcall isRight(char a1)
{
_BOOL8 result; // rax

if ( a1 == 93 )
return 1LL;
if ( a1 > 93 )
return a1 == 125;
result = 1LL;
if ( a1 != 41 )
result = a1 == 62;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(char a1)
{
_BOOL8 result; // rax

if ( a1 == 93 )
return 1LL;
if ( a1 > 93 )
return a1 == 125;
result = 1LL;
if ( a1 != 41 )
result = a1 == 62;
return result;
}

original_code:
int isRight(char c){
 int ret = 0;
 switch(c){
 case '>':
 case '}':
 case ']':
 case ')':
  ret = 1;
  break;
 default:
  ret = 0;
  break;
 }
 return ret;
}


isMatch

decompiled_code:
_BOOL8 __fastcall isMatch(char a1, char a2)
{
_BOOL8 result; // rax

if ( a1 == 91 )
return a2 == 93;
if ( a1 > 91 )
{
result = a2 == 125;
if ( a1 != 123 )
result = 0LL;
}
else if ( a1 == 40 )
{
result = a2 == 41;
}
else if ( a1 == 60 )
{
result = a2 == 62;
}
else
{
result = 0LL;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(char a1, char a2)
{
_BOOL8 result; // rax

if ( a1 == 91 )
return a2 == 93;
if ( a1 > 91 )
{
result = a2 == 125;
if ( a1 != 123 )
result = 0LL;
}
else if ( a1 == 40 )
{
result = a2 == 41;
}
else if ( a1 == 60 )
{
result = a2 == 62;
}
else
{
result = 0LL;
}
return result;
}

original_code:
Status isMatch(char left, char right){
 int ret = 0;
 switch(left){
 case '<':
  if( right == '>'){
   ret = 1;
  }
  break;
 case '{':
  if( right == '}'){
   ret = 1;
  }
  break;
 case '[':
  if( right == ']'){
   ret = 1;
  }
  break;
 case '(':
  if( right == ')'){
   ret = 1;
  }
  break;
 default:
  ret = 0;
  break;
 }
 return ret;
}

canonical_original_code:
int isMatch(char left, char right){
 int ret = 0;
 switch(left){
 case '<':
  if( right == '>'){
   ret = 1;
  }
  break;
 case '{':
  if( right == '}'){
   ret = 1;
  }
  break;
 case '[':
  if( right == ']'){
   ret = 1;
  }
  break;
 case '(':
  if( right == ')'){
   ret = 1;
  }
  break;
 default:
  ret = 0;
  break;
 }
 return ret;
}

variable_types: left char

variable_types: right char


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
unsigned int v4; // eax
int v5; // ebx
unsigned int i; // ebx
__int64 v7; // rsi
unsigned int v8; // eax
unsigned int v9; // eax
__int64 v10; // rsi
unsigned int v11; // eax
unsigned int v13; // [rsp+4h] [rbp-14h] BYREF
__int64 v14[2]; // [rsp+8h] [rbp-10h] BYREF

v14[0] = 0LL;
v3 = time(0LL);
srand(v3);
v4 = StackLength(0LL, argv);
printf("list length is %d\n", v4);
v5 = InitStack(v14);
if ( v5 != -1 )
{
for ( i = 0; i != 10; ++i )
{
v7 = i;
Push(v14[0], i);
}
v8 = StackLength(v14[0], v7);
printf("list length is %d\n", v8);
PrintStack(v14[0]);
Pop(v14[0], &v13);
v9 = StackLength(v14[0], &v13);
v10 = v13;
printf("pop e is %d ,length is %d \n", v13, v9);
ClearStack(v14);
v11 = StackLength(v14[0], v10);
printf("list length is %d\n", v11);
printf("scanner: is it matching? ");
v5 = scanner("hello(){}");
if ( v5 )
{
puts("true ");
v5 = 0;
}
else
{
puts("false ");
}
}
return v5;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
unsigned int v4; // eax
int v5; // ebx
unsigned int i; // ebx
__int64 v7; // rsi
unsigned int v8; // eax
unsigned int v9; // eax
__int64 v10; // rsi
unsigned int v11; // eax
unsigned int v13; // [rsp+4h] [rbp-14h] BYREF
__int64 v14[2]; // [rsp+8h] [rbp-10h] BYREF

v14[0] = 0LL;
v3 = time(0LL);
srand(v3);
v4 = StackLength(0LL, argv);
printf("list length is %d\n", v4);
v5 = InitStack(v14);
if ( v5 != -1 )
{
for ( i = 0; i != 10; ++i )
{
v7 = i;
Push(v14[0], i);
}
v8 = StackLength(v14[0], v7);
printf("list length is %d\n", v8);
PrintStack(v14[0]);
Pop(v14[0], &v13);
v9 = StackLength(v14[0], &v13);
v10 = v13;
printf("pop e is %d ,length is %d \n", v13, v9);
ClearStack(v14);
v11 = StackLength(v14[0], v10);
printf("list length is %d\n", v11);
printf("scanner: is it matching? ");
v5 = func3("hello(){}");
if ( v5 )
{
puts("true ");
v5 = 0;
}
else
{
puts("false ");
}
}
return v5;
}

original_code:
int main(int argc, const char *argv[])
{
 Linklist *testList = ((void *)0);
 int i;
 srand(time(0));
 printf("list length is %d\n",linklist_length(testList));
 for(i = 0; i < 10 ; i++){
  linklist_insert(&testList, i,0);
 }
 printf("list length is %d\n",linklist_length(testList));
 linklist_print(testList);
 linklist_remove(&testList, 9);
 printf("list length is %d\n",linklist_length(testList));
 linklist_print(testList);
 linklist_destroy(&testList);
 linklist_print(testList);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 struct _tag_Linklist *testList = ((void *)0);
 int i;
 srand(time(0));
 printf("list length is %d\n",linklist_length(testList));
 for(i = 0; i < 10 ; i++){
  linklist_insert(&testList, i,0);
 }
 printf("list length is %d\n",linklist_length(testList));
 linklist_print(testList);
 linklist_remove(&testList, 9);
 printf("list length is %d\n",linklist_length(testList));
 linklist_print(testList);
 linklist_destroy(&testList);
 linklist_print(testList);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct _tag_Linklist { int value; struct _tag_Linklist *next; } *


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

v2 = malloc(0x10uLL);
*a1 = v2;
if ( v2 )
{
*(_DWORD *)v2 = a2;
v2[1] = 0LL;
}
return 0LL;
}

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

v2 = malloc(0x10uLL);
*a1 = v2;
if ( v2 )
{
*(_DWORD *)v2 = a2;
v2[1] = 0LL;
}
return 0LL;
}

original_code:
int linklist_create(Linklist** linklist,value_t value){
 *linklist = (Linklist *)malloc(sizeof(Linklist));
 if(*linklist != ((void *)0)){
  (*linklist)->value=value;
  (*linklist)->next = ((void *)0);
 }
 return 0;
}

canonical_original_code:
int linklist_create(struct _tag_Linklist **linklist, int value){
 *linklist = (struct _tag_Linklist *)malloc(sizeof(Linklist));
 if(*linklist != ((void *)0)){
  (*linklist)->value=value;
  (*linklist)->next = ((void *)0);
 }
 return 0;
}

variable_types: struct _tag_Linklist { int value; struct _tag_Linklist *next; } **

variable_types: value int


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

if ( !a1 )
return 0xFFFFFFFFLL;
LODWORD(result) = 0;
do
{
a1 = *(_QWORD *)(a1 + 8);
result = (unsigned int)(result + 1);
}
while ( a1 );
return result;
}

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

if ( !a1 )
return 0xFFFFFFFFLL;
LODWORD(result) = 0;
do
{
a1 = *(_QWORD *)(a1 + 8);
result = (unsigned int)(result + 1);
}
while ( a1 );
return result;
}

original_code:
int linklist_length(Linklist * linklist){
 int len = 0;
 Linklist *point;
 if(linklist == ((void *)0)){
  return -1;
 }
 point = linklist;
 while(point != ((void *)0)){
  point = point->next;
  len++;
 }
 return len;
}

canonical_original_code:
int linklist_length(struct _tag_Linklist *linklist){
 int len = 0;
 struct _tag_Linklist *point;
 if(linklist == ((void *)0)){
  return -1;
 }
 point = linklist;
 while(point != ((void *)0)){
  point = point->next;
  len++;
 }
 return len;
}

variable_types: len int

user_defined_types: struct _tag_Linklist { int value; struct _tag_Linklist *next; }


decompiled_code:
__int64 __fastcall linklist_insert(__int64 *a1, int a2, int a3)
{
if ( a3 < 0 )
return 0xFFFFFFFFLL;
if ( *a1 )
return linklist_insert_native(a1, a2, a3);
puts("linklist is null ");
linklist_create(a1, a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 *a1, int a2, int a3)
{
if ( a3 < 0 )
return 0xFFFFFFFFLL;
if ( *a1 )
return func3(a1, a2, a3);
puts("linklist is null ");
func0(a1, a2);
return 0LL;
}

original_code:
int linklist_insert(Linklist ** linklist,value_t value , int pos){
 if(pos < 0){
  return -1;
 }
 if(*linklist == ((void *)0)){
  printf("linklist is null \n");
  linklist_create(linklist,value);
  return 0;
 }
 return linklist_insert_native(linklist,value,pos);
}

canonical_original_code:
int linklist_insert(struct _tag_Linklist **linklist, int value, int pos){
 if(pos < 0){
  return -1;
 }
 if(*linklist == ((void *)0)){
  printf("linklist is null \n");
  linklist_create(linklist,value);
  return 0;
 }
 return linklist_insert_native(linklist,value,pos);
}

variable_types: pos int


decompiled_code:
__int64 __fastcall linklist_remove(__int64 *a1, int a2)
{
_QWORD *v2; // rbx
int v3; // eax
_QWORD *v4; // rdx

v2 = (_QWORD *)*a1;
if ( a2 < 0 || !v2 )
return 0xFFFFFFFFLL;
if ( (int)linklist_length(*a1) < a2 )
return 0xFFFFFFFFLL;
if ( a2 <= 0 )
{
*a1 = v2[1];
}
else
{
v3 = 0;
do
{
v4 = v2;
v2 = (_QWORD *)v2[1];
++v3;
}
while ( v2 && a2 > v3 );
v4[1] = v2[1];
}
free(v2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 *a1, int a2)
{
_QWORD *v2; // rbx
int v3; // eax
_QWORD *v4; // rdx

v2 = (_QWORD *)*a1;
if ( a2 < 0 || !v2 )
return 0xFFFFFFFFLL;
if ( (int)func2(*a1) < a2 )
return 0xFFFFFFFFLL;
if ( a2 <= 0 )
{
*a1 = v2[1];
}
else
{
v3 = 0;
do
{
v4 = v2;
v2 = (_QWORD *)v2[1];
++v3;
}
while ( v2 && a2 > v3 );
v4[1] = v2[1];
}
free(v2);
return 0LL;
}

original_code:
int linklist_remove(Linklist ** linklist, int pos){
 Linklist* curr;
 Linklist* prev = ((void *)0);
 int i = 0;
 if(!(*linklist) || pos <0){
  return -1;
 }
 if(pos > linklist_length(*linklist)){
  return -1;
 }
 curr = *linklist;
 while(curr && pos > i){
  prev = curr;
  curr = curr->next;
  i++;
 }
 if(prev != ((void *)0)){
  prev->next = curr->next;
 }else{
  *linklist = curr->next;
 }
 free(curr);
 return 0;
}

canonical_original_code:
int linklist_remove(struct _tag_Linklist **linklist, int pos){
 struct _tag_Linklist *curr;
 struct _tag_Linklist *prev = ((void *)0);
 int i = 0;
 if(!(*linklist) || pos <0){
  return -1;
 }
 if(pos > linklist_length(*linklist)){
  return -1;
 }
 curr = *linklist;
 while(curr && pos > i){
  prev = curr;
  curr = curr->next;
  i++;
 }
 if(prev != ((void *)0)){
  prev->next = curr->next;
 }else{
  *linklist = curr->next;
 }
 free(curr);
 return 0;
}


decompiled_code:
__int64 __fastcall linklist_print(unsigned int *a1)
{
unsigned int *v1; // rbx
__int64 result; // rax

if ( a1 )
{
v1 = a1;
printf("value is ");
do
{
printf("%d ", *v1);
v1 = (unsigned int *)*((_QWORD *)v1 + 1);
}
while ( v1 );
putchar(10);
result = 0LL;
}
else
{
puts("it is null");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int *a1)
{
unsigned int *v1; // rbx
__int64 result; // rax

if ( a1 )
{
v1 = a1;
printf("value is ");
do
{
printf("%d ", *v1);
v1 = (unsigned int *)*((_QWORD *)v1 + 1);
}
while ( v1 );
putchar(10);
result = 0LL;
}
else
{
puts("it is null");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int linklist_print(Linklist * linklist){
 Linklist * point ;
 if(linklist == ((void *)0)){
  printf("it is null\n");
  return -1;
 }
 point = linklist;
 if(point)
  printf("value is ");
 else{
  printf("linklist is null \n");
  goto exit;
 }
 while(point != ((void *)0)){
  printf("%d ",point->value);
  point = point->next;
 }
 printf("\n");
exit:
 return 0;
}

canonical_original_code:
int linklist_print(struct _tag_Linklist *linklist){
 struct _tag_Linklist *point;
 if(linklist == ((void *)0)){
  printf("it is null\n");
  return -1;
 }
 point = linklist;
 if(point)
  printf("value is ");
 else{
  printf("linklist is null \n");
  goto exit;
 }
 while(point != ((void *)0)){
  printf("%d ",point->value);
  point = point->next;
 }
 printf("\n");
exit:
 return 0;
}


find

repo: gazeddyw/apple-and-blackberry-crumble

decompiled_code:
int find()
{
__int64 v0; // rcx
_QWORD *v1; // rax

printf("ID to find: ");
v0 = (unsigned int)getInt();
v1 = first;
if ( first )
{
while ( *(_DWORD *)*v1 != (_DWORD)v0 )
{
v1 = (_QWORD *)v1[1];
if ( !v1 )
return (int)v1;
}
printf("\nFound %s of %s (%d)!\n", *(const char **)(*v1 + 8LL), *(const char **)(*v1 + 16LL), v0);
LODWORD(v1) = sleep(1u);
}
return (int)v1;
}

canonical_decompiled_code:
int func7()
{
__int64 v0; // rcx
_QWORD *v1; // rax

printf("ID to find: ");
v0 = (unsigned int)func12();
v1 = first;
if ( first )
{
while ( *(_DWORD *)*v1 != (_DWORD)v0 )
{
v1 = (_QWORD *)v1[1];
if ( !v1 )
return (int)v1;
}
printf("\nFound %s of %s (%d)!\n", *(const char **)(*v1 + 8LL), *(const char **)(*v1 + 16LL), v0);
LODWORD(v1) = sleep(1u);
}
return (int)v1;
}

original_code:
void find(void)
{
    printf("ID to find: ");
    int id = getInt();
    node *ptr = first;
    while (ptr != ((void *)0))
    {
        if (ptr->student->id == id)
        {
            printf("\nFound %s of %s (%d)!\n",
                    ptr->student->name, ptr->student->house, id);
            sleep(1);
            break;
        }
        ptr = ptr->next;
    }
}

canonical_original_code:
void find()
{
    printf("ID to find: ");
    int id = getInt();
    struct node *ptr = first;
    while (ptr != ((void *)0))
    {
        if (ptr->student->id == id)
        {
            printf("\nFound %s of %s (%d)!\n",
                    ptr->student->name, ptr->student->house, id);
            sleep(1);
            break;
        }
        ptr = ptr->next;
    }
}

variable_types: id int

variable_types: struct node { struct student { int id; char *name; char *house; } *student; struct node *next; } *

user_defined_types: struct node { struct student *student; struct node *next; }


traverse

decompiled_code:
int traverse()
{
unsigned int **v0; // rbx

printf("\nLIST IS NOW: ");
v0 = (unsigned int **)first;
if ( first )
{
do
{
printf("%s of %s (%d) ", *((const char **)*v0 + 1), *((const char **)*v0 + 2), **v0);
v0 = (unsigned int **)v0[1];
}
while ( v0 );
}
fflush(stdout);
sleep(1u);
return puts("\n");
}

canonical_decompiled_code:
int func8()
{
unsigned int **v0; // rbx

printf("\nLIST IS NOW: ");
v0 = (unsigned int **)first;
if ( first )
{
do
{
printf("%s of %s (%d) ", *((const char **)*v0 + 1), *((const char **)*v0 + 2), **v0);
v0 = (unsigned int **)v0[1];
}
while ( v0 );
}
fflush(stdout);
sleep(1u);
return puts("\n");
}

original_code:
void traverse(void)
{
    printf("\nLIST IS NOW: ");
    node *ptr = first;
    while (ptr != ((void *)0))
    {
        printf("%s of %s (%d) ",
                ptr->student->name, ptr->student->house, ptr->student->id);
        ptr = ptr->next;
    }
    fflush(stdout);
    sleep(1);
    printf("\n\n");
}

canonical_original_code:
void traverse()
{
    printf("\nLIST IS NOW: ");
    struct node *ptr = first;
    while (ptr != ((void *)0))
    {
        printf("%s of %s (%d) ",
                ptr->student->name, ptr->student->house, ptr->student->id);
        ptr = ptr->next;
    }
    fflush(stdout);
    sleep(1);
    printf("\n\n");
}

user_defined_types: struct student { int id; char *name; char *house; }


delete

decompiled_code:
int delete()
{
int v0; // eax
_QWORD *v1; // rbx
_QWORD *v2; // rcx
void *v4; // rdi
void *v5; // rdi

printf("ID to delete: ");
v0 = getInt();
if ( first )
{
v1 = first;
v2 = 0LL;
while ( *(_DWORD *)*v1 != v0 )
{
v2 = v1;
if ( !v1[1] )
return traverse();
v1 = (_QWORD *)v1[1];
}
if ( first == v1 )
{
first = (void *)v1[1];
free(*(void **)(*v1 + 8LL));
free(*(void **)(*v1 + 16LL));
free((void *)*v1);
free(v1);
}
else
{
v2[1] = v1[1];
v4 = *(void **)(*v1 + 8LL);
if ( v4 )
free(v4);
v5 = *(void **)(*v1 + 16LL);
if ( v5 )
free(v5);
free((void *)*v1);
free(v1);
}
}
return traverse();
}

canonical_decompiled_code:
int func9()
{
int v0; // eax
_QWORD *v1; // rbx
_QWORD *v2; // rcx
void *v4; // rdi
void *v5; // rdi

printf("ID to delete: ");
v0 = func12();
if ( first )
{
v1 = first;
v2 = 0LL;
while ( *(_DWORD *)*v1 != v0 )
{
v2 = v1;
if ( !v1[1] )
return func8();
v1 = (_QWORD *)v1[1];
}
if ( first == v1 )
{
first = (void *)v1[1];
free(*(void **)(*v1 + 8LL));
free(*(void **)(*v1 + 16LL));
free((void *)*v1);
free(v1);
}
else
{
v2[1] = v1[1];
v4 = *(void **)(*v1 + 8LL);
if ( v4 )
free(v4);
v5 = *(void **)(*v1 + 16LL);
if ( v5 )
free(v5);
free((void *)*v1);
free(v1);
}
}
return func8();
}

original_code:
void delete(void)
{
    printf("ID to delete: ");
    int n = getInt();
    node *ptr = first;
    node *predptr = ((void *)0);
    while (ptr != ((void *)0))
    {
        if (ptr->student->id == n)
        {
            if (ptr == first)
            {
                first = ptr->next;
                free(ptr->student->name);
                free(ptr->student->house);
                free(ptr->student);
                free(ptr);
            }
            else
            {
                predptr->next = ptr->next;
                if (ptr->student->name != ((void *)0))
                    free(ptr->student->name);
                if (ptr->student->house != ((void *)0))
                    free(ptr->student->house);
                free(ptr->student);
                free(ptr);
            }
            break;
        }
        else
        {
            predptr = ptr;
            ptr = ptr->next;
        }
    }
    traverse();
}

canonical_original_code:
void delete()
{
    printf("ID to delete: ");
    int n = getInt();
    struct node *ptr = first;
    struct node *predptr = ((void *)0);
    while (ptr != ((void *)0))
    {
        if (ptr->student->id == n)
        {
            if (ptr == first)
            {
                first = ptr->next;
                free(ptr->student->name);
                free(ptr->student->house);
                free(ptr->student);
                free(ptr);
            }
            else
            {
                predptr->next = ptr->next;
                if (ptr->student->name != ((void *)0))
                    free(ptr->student->name);
                if (ptr->student->house != ((void *)0))
                    free(ptr->student->house);
                free(ptr->student);
                free(ptr);
            }
            break;
        }
        else
        {
            predptr = ptr;
            ptr = ptr->next;
        }
    }
    traverse();
}

variable_types: n int