decompiled_code:
// attributes: thunk
__int64 stack_free()
{
return sl_list_free();
}

canonical_decompiled_code:
__int64 func1()
{
return sl_list_free();
}

original_code:
void stack_free(stack stk) {
    sl_list_free(stk);
}

canonical_original_code:
void stack_free(struct sl_list_t *stk) {
    sl_list_free(stk);
}

variable_types: struct sl_list_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; struct sl_list_node_t { void *data; struct sl_list_node_t *next; } *front; unsigned long length; int (*cfunc)(); void (*free_func)(); } *


repo: paulgriffiths/cdatastruct

decompiled_code:
// attributes: thunk
__int64 stack_length()
{
return sl_list_length();
}

canonical_decompiled_code:
__int64 func2()
{
return sl_list_length();
}

original_code:
size_t stack_length(const stack stk) {
    return sl_list_length(stk);
}

canonical_original_code:
unsigned long stack_length(struct sl_list_t *stk) {
    return sl_list_length(stk);
}

return_type: unsigned long

user_defined_types: struct sl_list_t { union pthread_mutex_t mutex; struct sl_list_node_t *front; unsigned long length; int (*cfunc)(); void (*free_func)(); }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }

user_defined_types: struct sl_list_node_t { void *data; struct sl_list_node_t *next; }

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


decompiled_code:
// attributes: thunk
__int64 stack_isempty()
{
return sl_list_isempty();
}

canonical_decompiled_code:
__int64 func3()
{
return sl_list_isempty();
}

original_code:
_Bool stack_isempty(const stack stk) {
    return sl_list_isempty(stk);
}

canonical_original_code:
_Bool stack_isempty(struct sl_list_t *stk) {
    return sl_list_isempty(stk);
}


decompiled_code:
__int64 __fastcall stack_pop(__int64 a1)
{
__int64 *v1; // rax
__int64 v2; // r12

v1 = (__int64 *)sl_list_remove_at(a1, 0LL);
v2 = *v1;
free(v1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 *v1; // rax
__int64 v2; // r12

v1 = (__int64 *)sl_list_remove_at(a1, 0LL);
v2 = *v1;
free(v1);
return v2;
}

original_code:
void * stack_pop(stack stk) {
    sl_list_node popped_node = sl_list_remove_at(stk, 0);
    void * data = popped_node->data;
    free(popped_node);
    return data;
}

canonical_original_code:
void *stack_pop(struct sl_list_t *stk) {
    struct sl_list_node_t *popped_node = sl_list_remove_at(stk, 0);
    void *data = popped_node->data;
    free(popped_node);
    return data;
}

variable_types: struct sl_list_node_t { void *data; struct sl_list_node_t *next; } *

variable_types:
data
void *

return_type: void *


decompiled_code:
__int64 stack_peek()
{
return *(_QWORD *)sl_list_first();
}

canonical_decompiled_code:
__int64 func5()
{
return *(_QWORD *)sl_list_first();
}

original_code:
void * stack_peek(stack stk) {
    sl_list_itr peek_node = sl_list_first(stk);
    return peek_node->data;
}

canonical_original_code:
void *stack_peek(struct sl_list_t *stk) {
    struct sl_list_node_t *peek_node = sl_list_first(stk);
    return peek_node->data;
}


decompiled_code:
// attributes: thunk
__int64 stack_push()
{
return sl_list_prepend();
}

canonical_decompiled_code:
__int64 func6()
{
return sl_list_prepend();
}

original_code:
void stack_push(stack stk, void * data) {
    sl_list_prepend(stk, data);
}

canonical_original_code:
void stack_push(struct sl_list_t *stk, void *data) {
    sl_list_prepend(stk, data);
}

return_type: void


decompiled_code:
// attributes: thunk
__int64 stack_lock()
{
return sl_list_lock();
}

canonical_decompiled_code:
__int64 func7()
{
return sl_list_lock();
}

original_code:
void stack_lock(stack stk) {
    sl_list_lock(stk);
}

canonical_original_code:
void stack_lock(struct sl_list_t *stk) {
    sl_list_lock(stk);
}


decompiled_code:
// attributes: thunk
__int64 stack_unlock()
{
return sl_list_unlock();
}

canonical_decompiled_code:
__int64 func8()
{
return sl_list_unlock();
}

original_code:
void stack_unlock(stack stk) {
    sl_list_unlock(stk);
}

canonical_original_code:
void stack_unlock(struct sl_list_t *stk) {
    sl_list_unlock(stk);
}


decompiled_code:
__int64 __fastcall queue_init(__int64 a1)
{
return dl_list_init(0LL, a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return dl_list_init(0LL, a1);
}

original_code:
queue queue_init(void (*free_func)(void *)) {
    return dl_list_init(((void *)0), free_func);
}

canonical_original_code:
struct dl_list_t *queue_init(void (*free_func)(void *)) {
    return dl_list_init(((void *)0), free_func);
}

return_type: struct dl_list_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; struct dl_list_node_t { void *data; struct dl_list_node_t *next; struct dl_list_node_t *prev; } *front; struct dl_list_node_t *back; unsigned long length; int (*cfunc)(); void (*free_func)(); } *

user_defined_types: struct dl_list_t { union pthread_mutex_t mutex; struct dl_list_node_t *front; struct dl_list_node_t *back; unsigned long length; int (*cfunc)(); void (*free_func)(); }


decompiled_code:
// attributes: thunk
__int64 queue_free()
{
return dl_list_free();
}

canonical_decompiled_code:
__int64 func1()
{
return dl_list_free();
}

original_code:
void queue_free(queue que) {
    dl_list_free(que);
}

canonical_original_code:
void queue_free(struct dl_list_t *que) {
    dl_list_free(que);
}

variable_types: struct dl_list_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; struct dl_list_node_t { void *data; struct dl_list_node_t *next; struct dl_list_node_t *prev; } *front; struct dl_list_node_t *back; unsigned long length; int (*cfunc)(); void (*free_func)(); } *


decompiled_code:
// attributes: thunk
__int64 queue_length()
{
return dl_list_length();
}

canonical_decompiled_code:
__int64 func2()
{
return dl_list_length();
}

original_code:
size_t queue_length(const queue que) {
    return dl_list_length(que);
}

canonical_original_code:
unsigned long queue_length(struct dl_list_t *que) {
    return dl_list_length(que);
}

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


decompiled_code:
// attributes: thunk
__int64 queue_isempty()
{
return dl_list_isempty();
}

canonical_decompiled_code:
__int64 func3()
{
return dl_list_isempty();
}

original_code:
_Bool queue_isempty(const queue que) {
    return dl_list_isempty(que);
}

canonical_original_code:
_Bool queue_isempty(struct dl_list_t *que) {
    return dl_list_isempty(que);
}


decompiled_code:
__int64 __fastcall queue_pop(__int64 a1)
{
__int64 *v1; // rax
__int64 v2; // r12

v1 = (__int64 *)dl_list_remove_at(a1, 0LL);
v2 = *v1;
free(v1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 *v1; // rax
__int64 v2; // r12

v1 = (__int64 *)dl_list_remove_at(a1, 0LL);
v2 = *v1;
free(v1);
return v2;
}

original_code:
void * queue_pop(queue que) {
    dl_list_node popped_node = dl_list_remove_at(que, 0);
    void * data = popped_node->data;
    free(popped_node);
    return data;
}

canonical_original_code:
void *queue_pop(struct dl_list_t *que) {
    struct dl_list_node_t *popped_node = dl_list_remove_at(que, 0);
    void *data = popped_node->data;
    free(popped_node);
    return data;
}

variable_types: struct dl_list_node_t { void *data; struct dl_list_node_t *next; struct dl_list_node_t *prev; } *


decompiled_code:
// attributes: thunk
__int64 queue_pushback()
{
return dl_list_append();
}

canonical_decompiled_code:
__int64 func5()
{
return dl_list_append();
}

original_code:
void queue_pushback(queue que, void * data) {
    dl_list_append(que, data);
}

canonical_original_code:
void queue_pushback(struct dl_list_t *que, void *data) {
    dl_list_append(que, data);
}


decompiled_code:
// attributes: thunk
__int64 queue_lock()
{
return dl_list_lock();
}

canonical_decompiled_code:
__int64 func6()
{
return dl_list_lock();
}

original_code:
void queue_lock(queue que) {
    dl_list_lock(que);
}

canonical_original_code:
void queue_lock(struct dl_list_t *que) {
    dl_list_lock(que);
}


decompiled_code:
// attributes: thunk
__int64 queue_unlock()
{
return dl_list_unlock();
}

canonical_decompiled_code:
__int64 func7()
{
return dl_list_unlock();
}

original_code:
void queue_unlock(queue que) {
    dl_list_unlock(que);
}

canonical_original_code:
void queue_unlock(struct dl_list_t *que) {
    dl_list_unlock(que);
}


repo: paulguy/coloradjust

decompiled_code:
void __fastcall btext_free(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
void btext_free(bfont *f) {
 free((f->fontData)[0]);
 free(f);
}

canonical_original_code:
void btext_free(struct bfont *f) {
 free((f->fontData)[0]);
 free(f);
}

variable_types: struct bfont { unsigned char *fontData[96]; unsigned char height; unsigned char widths[96]; } *


decompiled_code:
__int64 __fastcall btext_calcWidth(__int64 a1, unsigned __int8 *a2)
{
int v2; // eax
__int64 v3; // rdx
unsigned int v4; // er8

v2 = *a2;
if ( !(_BYTE)v2 )
return 0LL;
LODWORD(v3) = 0;
v4 = 0;
do
{
while ( (unsigned __int8)(v2 - 32) <= 0x5Fu )
{
v4 += *(unsigned __int8 *)(a1 + v2 - 32 + 769);
v3 = (unsigned int)(v3 + 1);
v2 = a2[v3];
if ( !(_BYTE)v2 )
return v4;
}
v4 += *(unsigned __int8 *)(a1 + 864);
v3 = (unsigned int)(v3 + 1);
v2 = a2[v3];
}
while ( (_BYTE)v2 );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned __int8 *a2)
{
int v2; // eax
__int64 v3; // rdx
unsigned int v4; // er8

v2 = *a2;
if ( !(_BYTE)v2 )
return 0LL;
LODWORD(v3) = 0;
v4 = 0;
do
{
while ( (unsigned __int8)(v2 - 32) <= 0x5Fu )
{
v4 += *(unsigned __int8 *)(a1 + v2 - 32 + 769);
v3 = (unsigned int)(v3 + 1);
v2 = a2[v3];
if ( !(_BYTE)v2 )
return v4;
}
v4 += *(unsigned __int8 *)(a1 + 864);
v3 = (unsigned int)(v3 + 1);
v2 = a2[v3];
}
while ( (_BYTE)v2 );
return v4;
}

original_code:
Uint32 btext_calcWidth(bfont *f, const char *text) {
 Uint32 width, offset;
 unsigned char *utext;
 utext = (unsigned char *)text;
 offset = width = 0;
 while(utext[offset] != '\0') {
  if(utext[offset] >= ' ' && utext[offset] <= '\x7f')
   width += f->widths[utext[offset] - 32];
  else
   width += f->widths[95];
  offset++;
 }
 return(width);
}

canonical_original_code:
unsigned int btext_calcWidth(struct bfont *f, char *text) {
 unsigned int width; unsigned int offset;
 unsigned char *utext;
 utext = (unsigned char *)text;
 offset = width = 0;
 while(utext[offset] != '\0') {
  if(utext[offset] >= ' ' && utext[offset] <= '\x7f')
   width += f->widths[utext[offset] - 32];
  else
   width += f->widths[95];
  offset++;
 }
 return(width);
}

variable_types:
text
char *

variable_types:
width
unsigned int

variable_types:
offset
unsigned int

variable_types:
utext
unsigned char *

return_type: unsigned int

user_defined_types: struct bfont { unsigned char *fontData[96]; unsigned char height; unsigned char widths[96]; }


decompiled_code:
__int64 __fastcall btext_clipTextToWidth(__int64 a1, unsigned __int8 *a2, unsigned int a3, unsigned int a4)
{
int v4; // eax
__int64 v7; // r8
unsigned int v8; // edx

v4 = *a2;
LODWORD(v7) = 0;
if ( (_BYTE)v4 )
{
v8 = 0;
do
{
if ( (unsigned __int8)(v4 - 32) <= 0x5Fu )
{
v8 += *(unsigned __int8 *)(a1 + v4 - 32 + 769);
if ( v8 > a3 )
goto LABEL_7;
}
else
{
v8 += *(unsigned __int8 *)(a1 + 864);
if ( v8 > a3 )
{
LABEL_7:
LODWORD(v7) = (__PAIR64__(v7, a4) - 1) >> 32;
return (unsigned int)v7;
}
}
v7 = (unsigned int)(v7 + 1);
v4 = a2[v7];
}
while ( (_BYTE)v4 );
}
return (unsigned int)v7;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned __int8 *a2, unsigned int a3, unsigned int a4)
{
int v4; // eax
__int64 v7; // r8
unsigned int v8; // edx

v4 = *a2;
LODWORD(v7) = 0;
if ( (_BYTE)v4 )
{
v8 = 0;
do
{
if ( (unsigned __int8)(v4 - 32) <= 0x5Fu )
{
v8 += *(unsigned __int8 *)(a1 + v4 - 32 + 769);
if ( v8 > a3 )
goto LABEL_7;
}
else
{
v8 += *(unsigned __int8 *)(a1 + 864);
if ( v8 > a3 )
{
LABEL_7:
LODWORD(v7) = (__PAIR64__(v7, a4) - 1) >> 32;
return (unsigned int)v7;
}
}
v7 = (unsigned int)(v7 + 1);
v4 = a2[v7];
}
while ( (_BYTE)v4 );
}
return (unsigned int)v7;
}

original_code:
Uint32 btext_clipTextToWidth(bfont *f, const char *text, Uint32 limit, int overlap) {
 Uint32 width, offset;
 unsigned char *utext;
 utext = (unsigned char *)text;
 offset = width = 0;
 while(utext[offset] != '\0') {
  if(utext[offset] >= ' ' && utext[offset] <= '\x7f')
   width += f->widths[utext[offset] - 32];
  else
   width += f->widths[95];
  if(width > limit) {
   if(overlap == 0)
    return(offset - 1);
   return(offset);
  }
  offset++;
 }
 return(offset);
}

canonical_original_code:
unsigned int btext_clipTextToWidth(struct bfont *f, char *text, unsigned int limit, int overlap) {
 unsigned int width; unsigned int offset;
 unsigned char *utext;
 utext = (unsigned char *)text;
 offset = width = 0;
 while(utext[offset] != '\0') {
  if(utext[offset] >= ' ' && utext[offset] <= '\x7f')
   width += f->widths[utext[offset] - 32];
  else
   width += f->widths[95];
  if(width > limit) {
   if(overlap == 0)
    return(offset - 1);
   return(offset);
  }
  offset++;
 }
 return(offset);
}

variable_types:
limit
unsigned int

variable_types: overlap int


main

canonical_name: main

repo: paulguy/filesmasher

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // eax
int v5; // ebp
__off64_t v6; // r14
__int64 v7; // r13
int v8; // er12
_QWORD *v9; // rax
const char *v10; // rsi
__int64 v11; // rbx
__int64 v12; // rsi
_QWORD *v13; // rax
time_t v14; // r14
__int64 v15; // rbx
time_t v16; // rax
unsigned int v17; // eax
__off64_t v18; // rdx
__int64 v19; // rax
_QWORD *v20; // rcx
__off64_t v21; // rsi
int result; // eax
const char *v23; // rdi
int v24; // eax
__int64 v25; // rsi
__off64_t *v26; // rax
__off64_t v27; // rdx
__off64_t v28; // r10
__int64 v29; // rax
__off64_t *v30; // rcx
__off64_t v31; // r9
const char *v32; // rax
__int64 v33; // [rsp+8h] [rbp-60h]
char buf; // [rsp+1Fh] [rbp-49h] BYREF
_QWORD **v35; // [rsp+20h] [rbp-48h] BYREF
__off64_t offset[8]; // [rsp+28h] [rbp-40h] BYREF

if ( argc <= 2 )
{
fwrite("USAGE: filesmasher <filename> <amount> [ranges]\n", 1uLL, 0x30uLL, stderr);
result = -1;
}
else
{
v4 = open(argv[1], 1, envp);
v5 = v4;
if ( v4 < 0 )
{
fprintf(stderr, "Couldn't open %s for writing: ", argv[1]);
perror("open");
result = -1;
}
else
{
v6 = lseek64(v4, 0LL, 2);
if ( v6 < 0 )
{
LABEL_34:
perror("lseek64");
return -1;
}
v7 = atol(argv[2]);
if ( v7 <= 0 )
{
fwrite("\"amount\" must be a value >0.\n", 1uLL, 0x1DuLL, stderr);
result = -1;
}
else
{
if ( argc != 4 )
{
v8 = 1;
v35 = (_QWORD **)range_new(1LL);
v9 = *v35;
*v9 = 0LL;
v9[1] = v6 - 1;
v9[2] = 0LL;
v9[3] = v6 - 1;
LABEL_7:
v10 = argv[1];
v11 = 0LL;
printf("Filename: %s (%ld bytes)\n", v10, v6);
printf("Amount of Corruption: %ld\n", v7);
printf("Ranges: (%d)\n", (unsigned int)v8);
do
{
v12 = (unsigned int)v11;
v13 = v35[v11++];
printf("%d. %ld-%ld, %ld-%ld\n", v12, *v13, v13[1], v13[2], v13[3]);
}
while ( v11 != v8 );
v14 = 0LL;
v15 = 0LL;
v16 = time(0LL);
v17 = arand_init(v16);
printf("random found to be %d bits.\n", v17);
v33 = v35[v8 - 1][3] + 1LL;
while ( 1 )
{
arand_random64(offset, 63LL);
v18 = offset[0] % v33;
v19 = (__int64)v35;
offset[0] %= v33;
while ( 1 )
{
v20 = *(_QWORD **)v19;
v21 = *(_QWORD *)(*(_QWORD *)v19 + 16LL);
if ( v18 > v21 && v18 < v20[3] )
break;
v19 += 8LL;
if ( &v35[(unsigned int)(v8 - 1) + 1] == (_QWORD **)v19 )
goto LABEL_14;
}
offset[0] = *v20 + v18 - v21;
LABEL_14:
arand_random8(&buf, 8LL);
if ( lseek64(v5, offset[0], 0) < 0 )
goto LABEL_34;
if ( write(v5, &buf, 1uLL) < 0 )
{
perror("write");
return -1;
}
++v15;
if ( time(0LL) > v14 )
{
printf("\r%li/%li", v15, v7);
fflush(stdout);
v14 = time(0LL);
}
if ( v7 == v15 )
{
printf("\r%li/%li\n", v7, v7);
range_delete(v35, (unsigned int)v8);
close(v5);
return 0;
}
}
}
v23 = argv[3];
v35 = 0LL;
v24 = range_parse(v23, &v35);
v8 = v24;
if ( v24 < 0 )
{
v32 = (const char *)range_error_string((unsigned int)v24);
fprintf(stderr, "range_parse: (%d) %s.\n", (unsigned int)v8, v32);
result = -1;
}
else if ( v24 )
{
v25 = 0LL;
while ( 2 )
{
v26 = v35[v25];
v27 = *v26;
v28 = v26[1];
if ( *v26 > v28 )
{
fprintf(stderr, "Range start %ld is greater than range end %ld.\n", v27, v26[1]);
result = -1;
}
else
{
if ( v28 < v6 )
{
v29 = 0LL;
do
{
if ( (_DWORD)v25 != (_DWORD)v29 )
{
v30 = v35[v29];
if ( v28 >= *v30 )
{
v31 = v30[1];
if ( v28 <= v31 )
{
fprintf(stderr, "Range %ld-%ld overlaps with range %ld-%ld.\n", v27, v28, *v30, v31);
return -1;
}
}
}
++v29;
}
while ( v8 > (int)v29 );
if ( v8 > (int)++v25 )
continue;
goto LABEL_7;
}
fprintf(stderr, "Range %ld-%ld goes past end of file (%ld).\n", v27, v26[1], v6 - 1);
result = -1;
}
break;
}
}
else
{
fwrite("No valid ranges found, this is a bug.\n", 1uLL, 0x26uLL, stderr);
result = -1;
}
}
}
}
return result;
}

original_code:
int main(int argc, char **argv) {
 int outfile;
 FileRange **ranges;
 int nranges;
 off64_t filesize, randpos, totalarea;
 int i, j;
 long amount, il;
 int32_t randbits;
 uint8_t randval;
 time_t lastupdate;
 if(argc < 3) {
  fprintf(stderr, "USAGE: filesmasher <filename> <amount> [ranges]\n");
  return(-1);
 }
 outfile = open(argv[1], 01);
 if(outfile < 0) {
  fprintf(stderr, "Couldn't open %s for writing: ", argv[1]);
  perror("open");
  return(-1);
 }
 filesize = lseek64(outfile, 0, 2);
 if(filesize < 0) {
  perror("lseek64");
  return(-1);
 }
 amount = atol(argv[2]);
 if(amount < 1) {
  fprintf(stderr, "\"amount\" must be a value >0.\n");
  return(-1);
 }
 if(argc == 4) {
  ranges = ((void *)0);
  nranges = range_parse(argv[3], &ranges);
  if(nranges < 0) {
   fprintf(stderr, "range_parse: (%d) %s.\n", nranges, range_error_string(nranges));
   return(-1);
  }
  if(nranges == 0) {
   fprintf(stderr, "No valid ranges found, this is a bug.\n");
   return(-1);
  }
  for(i = 0; i < nranges; i++) {
   if(ranges[i]->start > ranges[i]->end) {
    fprintf(stderr, "Range start %ld is greater than range end %ld.\n", ranges[i]->start, ranges[i]->end);
    return(-1);
   }
   if(ranges[i]->end > filesize - 1) {
    fprintf(stderr, "Range %ld-%ld goes past end of file (%ld).\n", ranges[i]->start, ranges[i]->end, filesize - 1);
    return(-1);
   }
   for(j = 0; j < nranges; j++) {
    if(i != j) {
     if( ranges[i]->end >= ranges[j]->start &&
      ranges[i]->end <= ranges[j]->end) {
      fprintf(stderr, "Range %ld-%ld overlaps with range %ld-%ld.\n",
          ranges[i]->start, ranges[i]->end,
          ranges[j]->start, ranges[j]->end);
      return(-1);
     }
    }
   }
  }
 } else {
  ranges = range_new(1);
  nranges = 1;
  ranges[0]->start = 0;
  ranges[0]->end = filesize - 1;
  ranges[0]->totalstart = 0;
  ranges[0]->totalend = filesize - 1;
 }
 printf("Filename: %s (%ld bytes)\n", argv[1], filesize);
 printf("Amount of Corruption: %ld\n", amount);
 printf("Ranges: (%d)\n", nranges);
 for(i = 0; i < nranges; i++) {
  printf("%d. %ld-%ld, %ld-%ld\n", i, ranges[i]->start, ranges[i]->end, ranges[i]->totalstart, ranges[i]->totalend);
 }
 randbits = arand_init(time(((void *)0)));
 printf("random found to be %d bits.\n", randbits);
 lastupdate = 0;
 totalarea = ranges[nranges - 1]->totalend + 1;
 for(il = 0; il < amount; il++) {
  arand_random64((uint64_t *)&randpos, 63);
  randpos %= totalarea;
  for(i = 0; i < nranges; i++) {
   if(randpos > ranges[i]->totalstart && randpos < ranges[i]->totalend) {
    randpos = randpos - ranges[i]->totalstart + ranges[i]->start;
    break;
   }
  }
  arand_random8(&randval, 8);
  if(lseek64(outfile, randpos, 0) < 0) {
   perror("lseek64");
   return(-1);
  }
  if(write(outfile, &randval, 1) < 0) {
   perror("write");
   return(-1);
  }
  if(time(((void *)0)) > lastupdate) {
   printf("\r%li/%li", il + 1, amount);
   fflush(stdout);
   lastupdate = time(((void *)0));
  }
 }
 printf("\r%li/%li\n", il, amount);
 range_delete(ranges, nranges);
 close(outfile);
 return(0);
}

canonical_original_code:
int main(int argc, char **argv) {
 int outfile;
 struct FileRange **ranges;
 int nranges;
 long filesize; long randpos; long totalarea;
 int i; int j;
 long amount; long il;
 int randbits;
 unsigned char randval;
 long lastupdate;
 if(argc < 3) {
  fprintf(stderr, "USAGE: filesmasher <filename> <amount> [ranges]\n");
  return(-1);
 }
 outfile = open(argv[1], 01);
 if(outfile < 0) {
  fprintf(stderr, "Couldn't open %s for writing: ", argv[1]);
  perror("open");
  return(-1);
 }
 filesize = lseek64(outfile, 0, 2);
 if(filesize < 0) {
  perror("lseek64");
  return(-1);
 }
 amount = atol(argv[2]);
 if(amount < 1) {
  fprintf(stderr, "\"amount\" must be a value >0.\n");
  return(-1);
 }
 if(argc == 4) {
  ranges = ((void *)0);
  nranges = range_parse(argv[3], &ranges);
  if(nranges < 0) {
   fprintf(stderr, "range_parse: (%d) %s.\n", nranges, range_error_string(nranges));
   return(-1);
  }
  if(nranges == 0) {
   fprintf(stderr, "No valid ranges found, this is a bug.\n");
   return(-1);
  }
  for(i = 0; i < nranges; i++) {
   if(ranges[i]->start > ranges[i]->end) {
    fprintf(stderr, "Range start %ld is greater than range end %ld.\n", ranges[i]->start, ranges[i]->end);
    return(-1);
   }
   if(ranges[i]->end > filesize - 1) {
    fprintf(stderr, "Range %ld-%ld goes past end of file (%ld).\n", ranges[i]->start, ranges[i]->end, filesize - 1);
    return(-1);
   }
   for(j = 0; j < nranges; j++) {
    if(i != j) {
     if( ranges[i]->end >= ranges[j]->start &&
      ranges[i]->end <= ranges[j]->end) {
      fprintf(stderr, "Range %ld-%ld overlaps with range %ld-%ld.\n",
          ranges[i]->start, ranges[i]->end,
          ranges[j]->start, ranges[j]->end);
      return(-1);
     }
    }
   }
  }
 } else {
  ranges = range_new(1);
  nranges = 1;
  ranges[0]->start = 0;
  ranges[0]->end = filesize - 1;
  ranges[0]->totalstart = 0;
  ranges[0]->totalend = filesize - 1;
 }
 printf("Filename: %s (%ld bytes)\n", argv[1], filesize);
 printf("Amount of Corruption: %ld\n", amount);
 printf("Ranges: (%d)\n", nranges);
 for(i = 0; i < nranges; i++) {
  printf("%d. %ld-%ld, %ld-%ld\n", i, ranges[i]->start, ranges[i]->end, ranges[i]->totalstart, ranges[i]->totalend);
 }
 randbits = arand_init(time(((void *)0)));
 printf("random found to be %d bits.\n", randbits);
 lastupdate = 0;
 totalarea = ranges[nranges - 1]->totalend + 1;
 for(il = 0; il < amount; il++) {
  arand_random64((unsigned long *)&randpos, 63);
  randpos %= totalarea;
  for(i = 0; i < nranges; i++) {
   if(randpos > ranges[i]->totalstart && randpos < ranges[i]->totalend) {
    randpos = randpos - ranges[i]->totalstart + ranges[i]->start;
    break;
   }
  }
  arand_random8(&randval, 8);
  if(lseek64(outfile, randpos, 0) < 0) {
   perror("lseek64");
   return(-1);
  }
  if(write(outfile, &randval, 1) < 0) {
   perror("write");
   return(-1);
  }
  if(time(((void *)0)) > lastupdate) {
   printf("\r%li/%li", il + 1, amount);
   fflush(stdout);
   lastupdate = time(((void *)0));
  }
 }
 printf("\r%li/%li\n", il, amount);
 range_delete(ranges, nranges);
 close(outfile);
 return(0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: outfile int

variable_types: struct FileRange { long start; long end; long totalstart; long totalend; } **

variable_types: nranges int

variable_types: filesize long

variable_types: randpos long

variable_types: totalarea long

variable_types: i int

variable_types: j int

variable_types: amount long

variable_types: il long

variable_types: randbits int

variable_types:
randval
unsigned char

variable_types: lastupdate long

return_type: int


decompiled_code:
__int64 __fastcall arand_init(unsigned int a1)
{
_arand_randtest = 0x7FFFFFFF;
_arand_randbits = 31;
srandom(a1);
return (unsigned int)_arand_randbits;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
_arand_randtest = 0x7FFFFFFF;
_arand_randbits = 31;
srandom(a1);
return (unsigned int)_arand_randbits;
}

original_code:
int32_t arand_init(unsigned int seed) {
 int32_t i;
 __arand_randtest = 0xFFFFFFFF;
 for(i = 32; i > 0; i--) {
  if((__arand_randtest & 2147483647) == __arand_randtest) {
   __arand_randbits = i;
   break;
  }
  __arand_randtest >>= 1;
 }
 srandom(seed);
 return(__arand_randbits);
}

canonical_original_code:
int arand_init(unsigned int seed) {
 int i;
 __arand_randtest = 0xFFFFFFFF;
 for(i = 32; i > 0; i--) {
  if((__arand_randtest & 2147483647) == __arand_randtest) {
   __arand_randbits = i;
   break;
  }
  __arand_randtest >>= 1;
 }
 srandom(seed);
 return(__arand_randbits);
}

variable_types:
seed
unsigned int


decompiled_code:
__int64 __fastcall arand_random64(__int64 *a1, int a2)
{
__int64 v2; // rax
int v3; // ebx
__int64 v4; // rax
char v5; // cl

if ( (unsigned int)(a2 - 1) > 0x3F )
return 0xFFFFFFFFLL;
v2 = random();
v3 = a2 - _arand_randbits;
for ( *a1 = v2; v3 > 0; *a1 = v2 )
{
v4 = random();
v5 = a2 - v3;
v3 -= _arand_randbits;
v2 = *a1 | (v4 << v5);
}
*a1 = _arand_bitmasks[a2] & v2;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 *a1, int a2)
{
__int64 v2; // rax
int v3; // ebx
__int64 v4; // rax
char v5; // cl

if ( (unsigned int)(a2 - 1) > 0x3F )
return 0xFFFFFFFFLL;
v2 = random();
v3 = a2 - _arand_randbits;
for ( *a1 = v2; v3 > 0; *a1 = v2 )
{
v4 = random();
v5 = a2 - v3;
v3 -= _arand_randbits;
v2 = *a1 | (v4 << v5);
}
*a1 = _arand_bitmasks[a2] & v2;
return 0LL;
}

original_code:
int arand_random64(uint64_t *value, int32_t bits) {
 int32_t bitsleft;
 if(bits < 1 || bits > 64) {
  return(-1);
 }
 bitsleft = bits;
 *value = random();
 bitsleft -= __arand_randbits;
 while(bitsleft > 0) {
  *value |= (random() << (bits - bitsleft));
  bitsleft -= __arand_randbits;
 }
 *value &= __arand_bitmasks[bits];
 return(0);
}

canonical_original_code:
int arand_random64(unsigned long *value, int bits) {
 int bitsleft;
 if(bits < 1 || bits > 64) {
  return(-1);
 }
 bitsleft = bits;
 *value = random();
 bitsleft -= __arand_randbits;
 while(bitsleft > 0) {
  *value |= (random() << (bits - bitsleft));
  bitsleft -= __arand_randbits;
 }
 *value &= __arand_bitmasks[bits];
 return(0);
}

variable_types:
value
unsigned long *

variable_types: bits int

variable_types: bitsleft int


decompiled_code:
__int64 __fastcall arand_random32(int *a1, int a2)
{
int v2; // eax
int i; // ebx

if ( (unsigned int)(a2 - 1) > 0x1F )
return 0xFFFFFFFFLL;
v2 = random();
*a1 = v2;
for ( i = a2 - _arand_randbits; i > 0; i -= _arand_randbits )
{
v2 = *a1 | (random() << ((unsigned __int8)a2 - (unsigned __int8)i));
*a1 = v2;
}
*a1 = _arand_bitmasks[a2] & v2;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int *a1, int a2)
{
int v2; // eax
int i; // ebx

if ( (unsigned int)(a2 - 1) > 0x1F )
return 0xFFFFFFFFLL;
v2 = random();
*a1 = v2;
for ( i = a2 - _arand_randbits; i > 0; i -= _arand_randbits )
{
v2 = *a1 | (random() << ((unsigned __int8)a2 - (unsigned __int8)i));
*a1 = v2;
}
*a1 = _arand_bitmasks[a2] & v2;
return 0LL;
}

original_code:
int arand_random32(uint32_t *value, int32_t bits) {
 int32_t bitsleft;
 if(bits < 1 || bits > 32) {
  return(-1);
 }
 bitsleft = bits;
 *value = random();
 bitsleft -= __arand_randbits;
 while(bitsleft > 0) {
  *value |= (random() << (bits - bitsleft));
  bitsleft -= __arand_randbits;
 }
 *value &= __arand_bitmasks[bits];
 return(0);
}

canonical_original_code:
int arand_random32(unsigned int *value, int bits) {
 int bitsleft;
 if(bits < 1 || bits > 32) {
  return(-1);
 }
 bitsleft = bits;
 *value = random();
 bitsleft -= __arand_randbits;
 while(bitsleft > 0) {
  *value |= (random() << (bits - bitsleft));
  bitsleft -= __arand_randbits;
 }
 *value &= __arand_bitmasks[bits];
 return(0);
}

variable_types:
value
unsigned int *


decompiled_code:
__int64 __fastcall arand_random16(_WORD *a1, int a2)
{
__int16 v2; // ax
int v3; // ebx
__int64 v4; // rax
char v5; // cl

if ( (unsigned int)(a2 - 1) > 0xF )
return 0xFFFFFFFFLL;
v2 = random();
v3 = a2 - _arand_randbits;
for ( *a1 = v2; v3 > 0; *a1 = v2 )
{
v4 = random();
v5 = a2 - v3;
v3 -= _arand_randbits;
v2 = *a1 | (v4 << v5);
}
*a1 = _arand_bitmasks[a2] & v2;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(_WORD *a1, int a2)
{
__int16 v2; // ax
int v3; // ebx
__int64 v4; // rax
char v5; // cl

if ( (unsigned int)(a2 - 1) > 0xF )
return 0xFFFFFFFFLL;
v2 = random();
v3 = a2 - _arand_randbits;
for ( *a1 = v2; v3 > 0; *a1 = v2 )
{
v4 = random();
v5 = a2 - v3;
v3 -= _arand_randbits;
v2 = *a1 | (v4 << v5);
}
*a1 = _arand_bitmasks[a2] & v2;
return 0LL;
}

original_code:
int arand_random16(uint16_t *value, int32_t bits) {
 int32_t bitsleft;
 if(bits < 1 || bits > 16) {
  return(-1);
 }
 bitsleft = bits;
 *value = random();
 bitsleft -= __arand_randbits;
 while(bitsleft > 0) {
  *value |= (random() << (bits - bitsleft));
  bitsleft -= __arand_randbits;
 }
 *value &= __arand_bitmasks[bits];
 return(0);
}

canonical_original_code:
int arand_random16(unsigned short *value, int bits) {
 int bitsleft;
 if(bits < 1 || bits > 16) {
  return(-1);
 }
 bitsleft = bits;
 *value = random();
 bitsleft -= __arand_randbits;
 while(bitsleft > 0) {
  *value |= (random() << (bits - bitsleft));
  bitsleft -= __arand_randbits;
 }
 *value &= __arand_bitmasks[bits];
 return(0);
}

variable_types:
value
unsigned short *


decompiled_code:
__int64 __fastcall arand_random8(_BYTE *a1, int a2)
{
char v2; // al
int i; // ebx

if ( (unsigned int)(a2 - 1) > 7 )
return 0xFFFFFFFFLL;
v2 = random();
*a1 = v2;
for ( i = a2 - _arand_randbits; i > 0; i -= _arand_randbits )
{
v2 = *a1 | (random() << ((unsigned __int8)a2 - (unsigned __int8)i));
*a1 = v2;
}
*a1 = _arand_bitmasks[a2] & v2;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(_BYTE *a1, int a2)
{
char v2; // al
int i; // ebx

if ( (unsigned int)(a2 - 1) > 7 )
return 0xFFFFFFFFLL;
v2 = random();
*a1 = v2;
for ( i = a2 - _arand_randbits; i > 0; i -= _arand_randbits )
{
v2 = *a1 | (random() << ((unsigned __int8)a2 - (unsigned __int8)i));
*a1 = v2;
}
*a1 = _arand_bitmasks[a2] & v2;
return 0LL;
}

original_code:
int arand_random8(uint8_t *value, int32_t bits) {
 int32_t bitsleft;
 if(bits < 1 || bits > 8) {
  return(-1);
 }
 bitsleft = bits;
 *value = random();
 bitsleft -= __arand_randbits;
 while(bitsleft > 0) {
  *value |= (random() << (bits - bitsleft));
  bitsleft -= __arand_randbits;
 }
 *value &= __arand_bitmasks[bits];
 return(0);
}

canonical_original_code:
int arand_random8(unsigned char *value, int bits) {
 int bitsleft;
 if(bits < 1 || bits > 8) {
  return(-1);
 }
 bitsleft = bits;
 *value = random();
 bitsleft -= __arand_randbits;
 while(bitsleft > 0) {
  *value |= (random() << (bits - bitsleft));
  bitsleft -= __arand_randbits;
 }
 *value &= __arand_bitmasks[bits];
 return(0);
}

variable_types:
value
unsigned char *


decompiled_code:
void __fastcall range_delete(void *ptr, int a2)
{
void *v2; // r12
void **v3; // rbx
void **v4; // rbp
void *v5; // rdi

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

canonical_decompiled_code:
void __fastcall func1(void *ptr, int a2)
{
void *v2; // r12
void **v3; // rbx
void **v4; // rbp
void *v5; // rdi

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

original_code:
void range_delete(FileRange ** ranges, int count) {
 int i;
 for(i = 0; i < count; i++) {
  free(ranges[i]);
 }
 free(ranges);
}

canonical_original_code:
void range_delete(struct FileRange **ranges, int count) {
 int i;
 for(i = 0; i < count; i++) {
  free(ranges[i]);
 }
 free(ranges);
}

variable_types: struct FileRange { long start; long end; long totalstart; long totalend; } **

variable_types: count int


decompiled_code:
__int64 __fastcall range_verify(char *a1)
{
char v1; // al
unsigned int v2; // er8
int v3; // edx
int v4; // esi
__int64 v5; // rcx
__int64 v7; // rcx
char v8; // al
int v9; // edx

v1 = *a1;
v2 = 0;
if ( !*a1 )
return v2;
v3 = 0;
v4 = 0;
v5 = 0LL;
while ( v1 != 45 )
{
if ( v1 == 44 )
{
if ( !v4 )
return 0xFFFFFFFFLL;
v1 = a1[v5 + 1];
++v2;
v3 = v5 + 1;
if ( !v1 )
return v2;
if ( v1 == 45 || v1 == 44 )
return 0xFFFFFFFFLL;
}
LABEL_5:
if ( (unsigned __int8)(v1 - 48) > 9u )
return 0xFFFFFFFFLL;
++v3;
v4 = 1;
v5 = v3;
v1 = a1[v3];
if ( !v1 )
{
LABEL_7:
++v2;
return v2;
}
}
if ( v4 )
{
v7 = v3 + 1;
v8 = a1[v7];
if ( !v8 )
return v2;
if ( v8 != 44 )
{
while ( 1 )
{
v9 = v7;
if ( (unsigned __int8)(v8 - 48) > 9u )
break;
v8 = a1[v7 + 1];
if ( !v8 )
goto LABEL_7;
++v7;
if ( v8 == 44 )
{
v3 = v9 + 2;
++v2;
v1 = a1[v3];
if ( !v1 )
return v2;
if ( (unsigned __int8)(v1 - 44) > 1u )
goto LABEL_5;
return 0xFFFFFFFFLL;
}
}
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1)
{
char v1; // al
unsigned int v2; // er8
int v3; // edx
int v4; // esi
__int64 v5; // rcx
__int64 v7; // rcx
char v8; // al
int v9; // edx

v1 = *a1;
v2 = 0;
if ( !*a1 )
return v2;
v3 = 0;
v4 = 0;
v5 = 0LL;
while ( v1 != 45 )
{
if ( v1 == 44 )
{
if ( !v4 )
return 0xFFFFFFFFLL;
v1 = a1[v5 + 1];
++v2;
v3 = v5 + 1;
if ( !v1 )
return v2;
if ( v1 == 45 || v1 == 44 )
return 0xFFFFFFFFLL;
}
LABEL_5:
if ( (unsigned __int8)(v1 - 48) > 9u )
return 0xFFFFFFFFLL;
++v3;
v4 = 1;
v5 = v3;
v1 = a1[v3];
if ( !v1 )
{
LABEL_7:
++v2;
return v2;
}
}
if ( v4 )
{
v7 = v3 + 1;
v8 = a1[v7];
if ( !v8 )
return v2;
if ( v8 != 44 )
{
while ( 1 )
{
v9 = v7;
if ( (unsigned __int8)(v8 - 48) > 9u )
break;
v8 = a1[v7 + 1];
if ( !v8 )
goto LABEL_7;
++v7;
if ( v8 == 44 )
{
v3 = v9 + 2;
++v2;
v1 = a1[v3];
if ( !v1 )
return v2;
if ( (unsigned __int8)(v1 - 44) > 1u )
goto LABEL_5;
return 0xFFFFFFFFLL;
}
}
}
}
return 0xFFFFFFFFLL;
}

original_code:
int range_verify(char *rangeString) {
 int stage, numfound;
 int i;
 int count;
 count = 0; stage = 0; numfound = 0;
 for(i = 0; rangeString[i] != '\0'; i++) {
  switch(stage) {
   case 0:
    if(rangeString[i] == '-') {
     if(numfound == 0) {
      return(-1);
     }
     stage = 1;
     numfound = 0;
    } else if(rangeString[i] == ',') {
     if(numfound == 0) {
      return(-1);
     }
     count++;
     numfound = 0;
    } else if((rangeString[i] >= '0' && rangeString[i] <= '9')) {
     numfound = 1;
    } else {
     return(-1);
    }
    break;
   case 1:
    if(rangeString[i] == ',') {
     if(numfound == 0) {
      return(-1);
     }
     count++;
     stage = 0;
     numfound = 0;
    } else if((rangeString[i] >= '0' && rangeString[i] <= '9')) {
     numfound = 1;
    } else {
     return(-1);
    }
    break;
   default:
    return(-2);
  }
 }
 if(numfound == 1) {
  count++;
 }
 return(count);
}

canonical_original_code:
int range_verify(char *rangeString) {
 int stage; int numfound;
 int i;
 int count;
 count = 0; stage = 0; numfound = 0;
 for(i = 0; rangeString[i] != '\0'; i++) {
  switch(stage) {
   case 0:
    if(rangeString[i] == '-') {
     if(numfound == 0) {
      return(-1);
     }
     stage = 1;
     numfound = 0;
    } else if(rangeString[i] == ',') {
     if(numfound == 0) {
      return(-1);
     }
     count++;
     numfound = 0;
    } else if((rangeString[i] >= '0' && rangeString[i] <= '9')) {
     numfound = 1;
    } else {
     return(-1);
    }
    break;
   case 1:
    if(rangeString[i] == ',') {
     if(numfound == 0) {
      return(-1);
     }
     count++;
     stage = 0;
     numfound = 0;
    } else if((rangeString[i] >= '0' && rangeString[i] <= '9')) {
     numfound = 1;
    } else {
     return(-1);
    }
    break;
   default:
    return(-2);
  }
 }
 if(numfound == 1) {
  count++;
 }
 return(count);
}

variable_types:
rangeString
char *

variable_types: stage int

variable_types: numfound int


original_code:
int range_parse_next(char *rangeString, FileRange *range) {
 char numbuffer[256];
 int i, j;
 j = 0;
 for(i = 0; i < 256; i++) {
  if((rangeString[i] >= '0' && rangeString[i] <= '9')) {
   numbuffer[i] = rangeString[i];
  } else if(rangeString[i] == '-') {
   numbuffer[i] = '\0';
   range->start = (off64_t)atol(numbuffer);
   i++;
   for(j = 0; j < 256; j++) {
    if((rangeString[i + j] >= '0' && rangeString[i + j] <= '9')) {
     numbuffer[j] = rangeString[i + j];
    } else if(rangeString[i + j] == '\0' || rangeString[i + j] == ',') {
     numbuffer[j] = '\0';
     range->end = (off64_t)atol(numbuffer);
     if(rangeString[i + j] != '\0') {
      j++;
     }
     break;
    } else {
     return(-1);
    }
   }
   break;
  } else if(rangeString[i] == '\0' || rangeString[i] == ',') {
   numbuffer[i] = '\0';
   range->start = (off64_t)atol(numbuffer);
   range->end = range->start;
   if(rangeString[i] != '\0') {
    i++;
   }
   break;
  } else {
   return(-1);
  }
 }
 return(i + j);
}

canonical_original_code:
int range_parse_next(char *rangeString, struct FileRange *range) {
 char numbuffer[256];
 int i; int j;
 j = 0;
 for(i = 0; i < 256; i++) {
  if((rangeString[i] >= '0' && rangeString[i] <= '9')) {
   numbuffer[i] = rangeString[i];
  } else if(rangeString[i] == '-') {
   numbuffer[i] = '\0';
   range->start = (long)atol(numbuffer);
   i++;
   for(j = 0; j < 256; j++) {
    if((rangeString[i + j] >= '0' && rangeString[i + j] <= '9')) {
     numbuffer[j] = rangeString[i + j];
    } else if(rangeString[i + j] == '\0' || rangeString[i + j] == ',') {
     numbuffer[j] = '\0';
     range->end = (long)atol(numbuffer);
     if(rangeString[i + j] != '\0') {
      j++;
     }
     break;
    } else {
     return(-1);
    }
   }
   break;
  } else if(rangeString[i] == '\0' || rangeString[i] == ',') {
   numbuffer[i] = '\0';
   range->start = (long)atol(numbuffer);
   range->end = range->start;
   if(rangeString[i] != '\0') {
    i++;
   }
   break;
  } else {
   return(-1);
  }
 }
 return(i + j);
}

variable_types: struct FileRange { long start; long end; long totalstart; long totalend; } *

variable_types:
numbuffer
char [256]

variable_types: i int

variable_types: j int


original_code:
int range_parse(char *rangeString, FileRange ***ranges) {
 int count;
 int i;
 int ret;
 int parsepos;
 count = range_verify(rangeString);
 if(count < 1) {
  return(count);
 }
 *ranges = range_new(count);
 if(*ranges == ((void *)0)) {
  return(-3);
 }
 parsepos = 0;
 for(i = 0; i < count; i++) {
  ret = range_parse_next(&(rangeString[parsepos]), (*ranges)[i]);
  if(ret < 1) {
   range_delete(*ranges, count);
   return(-4);
  }
  parsepos += ret;
  if(rangeString[parsepos] == '\0' && i < count - 1) {
   range_delete(*ranges, count);
   return(-5);
  }
  if(i == 0) {
   (*ranges)[i]->totalstart = 0;
   (*ranges)[i]->totalend = (*ranges)[i]->end - (*ranges)[i]->start;
  } else {
   (*ranges)[i]->totalstart = (*ranges)[i - 1]->totalend + 1;
   (*ranges)[i]->totalend = (*ranges)[i]->totalstart + ((*ranges)[i]->end - (*ranges)[i]->start);
  }
 }
 return(count);
}

canonical_original_code:
int range_parse(char *rangeString, struct FileRange ***ranges) {
 int count;
 int i;
 int ret;
 int parsepos;
 count = range_verify(rangeString);
 if(count < 1) {
  return(count);
 }
 *ranges = range_new(count);
 if(*ranges == ((void *)0)) {
  return(-3);
 }
 parsepos = 0;
 for(i = 0; i < count; i++) {
  ret = range_parse_next(&(rangeString[parsepos]), (*ranges)[i]);
  if(ret < 1) {
   range_delete(*ranges, count);
   return(-4);
  }
  parsepos += ret;
  if(rangeString[parsepos] == '\0' && i < count - 1) {
   range_delete(*ranges, count);
   return(-5);
  }
  if(i == 0) {
   (*ranges)[i]->totalstart = 0;
   (*ranges)[i]->totalend = (*ranges)[i]->end - (*ranges)[i]->start;
  } else {
   (*ranges)[i]->totalstart = (*ranges)[i - 1]->totalend + 1;
   (*ranges)[i]->totalend = (*ranges)[i]->totalstart + ((*ranges)[i]->end - (*ranges)[i]->start);
  }
 }
 return(count);
}

variable_types: struct FileRange { long start; long end; long totalstart; long totalend; } ***

variable_types: ret int

variable_types: parsepos int


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

if ( (unsigned int)(a1 + 5) > 4 )
result = RANGE_ERRORCODES;
else
result = *(&RANGE_ERRORCODES - a1);
return result;
}

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

if ( (unsigned int)(a1 + 5) > 4 )
result = RANGE_ERRORCODES;
else
result = *(&RANGE_ERRORCODES - a1);
return result;
}

original_code:
const char *range_error_string(int errorcode) {
 if(errorcode > -1 || errorcode < RANGE_ERRORMAX) {
  return(RANGE_ERRORCODES[0]);
 }
 return(RANGE_ERRORCODES[-errorcode]);
}

variable_types: errorcode int

return_type: char *


initstr

repo: paulguy/pcspkrplay

decompiled_code:
_QWORD *initstr()
{
_QWORD *v0; // r12
_BYTE *v1; // rax

v0 = malloc(0x10uLL);
v1 = malloc(1uLL);
*v0 = v1;
*v1 = 0;
v0[1] = 0x100000001LL;
return v0;
}

original_code:
str *initstr() {
 str *t;
 t = malloc(sizeof(str));
 t->data = malloc(1);
 t->data[0] = '\0';
 t->strsize = 1;
 t->memsize = 1;
 return(t);
}

canonical_original_code:
struct str *initstr() {
 struct str *t;
 t = malloc(sizeof(str));
 t->data = malloc(1);
 t->data[0] = '\0';
 t->strsize = 1;
 t->memsize = 1;
 return(t);
}

variable_types: struct str { char *data; int strsize; int memsize; } *


freestr

decompiled_code:
void __fastcall freestr(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
void freestr(str *t) {
 free(t->data);
 free(t);
}

canonical_original_code:
void freestr(struct str *t) {
 free(t->data);
 free(t);
}

user_defined_types: struct str { char *data; int strsize; int memsize; }


concatchar

decompiled_code:
__int64 __fastcall concatchar(char **a1, const void *a2, int a3)
{
__int64 v5; // rax
int v6; // ebx
char *v7; // rdi
int v8; // ebp
char *v9; // rax
char *v10; // rax

v5 = *((int *)a1 + 2);
v6 = *((_DWORD *)a1 + 3);
v7 = *a1;
v8 = v5 + a3;
if ( v6 >= (int)v5 + a3 )
goto LABEL_5;
do
v6 *= 2;
while ( v8 > v6 );
v9 = (char *)realloc(v7, v6);
v7 = v9;
if ( v9 )
{
*a1 = v9;
v5 = *((int *)a1 + 2);
LABEL_5:
memcpy(&v7[v5 - 1], a2, a3);
v10 = *a1;
*((_DWORD *)a1 + 2) = v8;
*((_DWORD *)a1 + 3) = v6;
v10[v8 - 1] = 0;
return 0LL;
}
return 0xFFFFFFFFLL;
}

original_code:
int concatchar(str* s, char *data, int len) {
 int target;
 int newsize;
 char *t;
 target = len + s->strsize;
 if(target > s->memsize) {
  newsize = s->memsize;
  while(newsize < target)
   newsize *= 2;
  t = realloc(s->data, newsize);
  if(t == ((void *)0))
   return(-1);
  s->data = t;
 } else {
  newsize = s->memsize;
 }
 memcpy(&(s->data[s->strsize - 1]), data, len);
 s->strsize = target;
 s->memsize = newsize;
 s->data[s->strsize - 1] = '\0';
 return(0);
}

canonical_original_code:
int concatchar(struct str *s, char *data, int len) {
 int target;
 int newsize;
 char *t;
 target = len + s->strsize;
 if(target > s->memsize) {
  newsize = s->memsize;
  while(newsize < target)
   newsize *= 2;
  t = realloc(s->data, newsize);
  if(t == ((void *)0))
   return(-1);
  s->data = t;
 } else {
  newsize = s->memsize;
 }
 memcpy(&(s->data[s->strsize - 1]), data, len);
 s->strsize = target;
 s->memsize = newsize;
 s->data[s->strsize - 1] = '\0';
 return(0);
}

variable_types:
data
char *

variable_types: len int

variable_types: target int

variable_types: newsize int

variable_types:
t
char *


statusout

decompiled_code:
int __fastcall statusout(unsigned int a1, unsigned int a2)
{
return fprintf(stderr, "%u/%u\r", a1, a2);
}

canonical_decompiled_code:
int __fastcall func0(unsigned int a1, unsigned int a2)
{
return fprintf(stderr, "%u/%u\r", a1, a2);
}

original_code:
void statusout(int cur, int max) {
 fprintf(stderr, "%u/%u\r", cur, max);
}

variable_types: cur int

variable_types: max int


signalhandler

decompiled_code:
void __fastcall __noreturn signalhandler(unsigned int a1)
{
fprintf(stderr, "\n\nSignal %i received.\n", a1);
playnote((unsigned int)speaker, 0LL, 0LL);
close(speaker);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(unsigned int a1)
{
fprintf(stderr, "\n\nSignal %i received.\n", a1);
playnote((unsigned int)speaker, 0LL, 0LL);
close(speaker);
exit(0);
}

original_code:
void signalhandler(int signum) {
 fprintf(stderr, "\n\nSignal %i received.\n", signum);
 playnote(speaker, 0, 0);
 close(speaker);
 exit(0);
}

variable_types: signum int


usage

decompiled_code:
void __fastcall __noreturn usage(const char *a1)
{
fprintf(
stderr,
"USAGE: %s <options>\n"
"Options:\n"
"--decompile / -d - Decompile string, used for outputting size-optimized code. (Currently disabled)\n"
"--device / -e device - Select event device, default is %s.\n"
"--filename / -f filename - Read from filename instead of standard input.\n"
"--quiet / -q - Don't play, only compile (and optionally decompile) then quit.\n"
"--display / -s - Display playback status.\n"
"--help / -h - Print help/usage. Refer to README for more information.\n"
"--debug - Enable debug mode.\n",
a1,
DEFAULT_DEVICE);
exit(-1);
}

canonical_decompiled_code:
void __fastcall __noreturn func2(const char *a1)
{
fprintf(
stderr,
"USAGE: %s <options>\n"
"Options:\n"
"--decompile / -d - Decompile string, used for outputting size-optimized code. (Currently disabled)\n"
"--device / -e device - Select event device, default is %s.\n"
"--filename / -f filename - Read from filename instead of standard input.\n"
"--quiet / -q - Don't play, only compile (and optionally decompile) then quit.\n"
"--display / -s - Display playback status.\n"
"--help / -h - Print help/usage. Refer to README for more information.\n"
"--debug - Enable debug mode.\n",
a1,
DEFAULT_DEVICE);
exit(-1);
}

original_code:
void usage(char *name) {
 fprintf(stderr, "USAGE: %s <options>\nOptions:\n--decompile / -d          - Decompile string, used for outputting size-optimized code. (Currently disabled)\n--device    / -e device   - Select event device, default is %s.\n--filename  / -f filename - Read from filename instead of standard input.\n--quiet     / -q          - Don't play, only compile (and optionally decompile) then quit.\n--display   / -s          - Display playback status.\n--help      / -h          - Print help/usage.  Refer to README for more information.\n--debug                   - Enable debug mode.\n", name, DEFAULT_DEVICE);
 exit(-1);
}

variable_types:
name
char *


initsong

decompiled_code:
_QWORD *initsong()
{
_QWORD *result; // rax

result = malloc(0x38uLL);
if ( result )
{
*(_DWORD *)result = 0;
result[1] = 0LL;
result[2] = 0LL;
result[3] = 0LL;
}
return result;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *result; // rax

result = malloc(0x38uLL);
if ( result )
{
*(_DWORD *)result = 0;
result[1] = 0LL;
result[2] = 0LL;
result[3] = 0LL;
}
return result;
}

original_code:
song *initsong() {
 song *s;
 s = malloc(sizeof(song));
 if(s == ((void *)0)) {
  return(((void *)0));
 }
 s->count = 0;
 s->first = ((void *)0);
 s->last = ((void *)0);
 s->current = ((void *)0);
 return(s);
}

canonical_original_code:
struct song *initsong() {
 struct song *s;
 s = malloc(sizeof(song));
 if(s == ((void *)0)) {
  return(((void *)0));
 }
 s->count = 0;
 s->first = ((void *)0);
 s->last = ((void *)0);
 s->current = ((void *)0);
 return(s);
}

variable_types: struct song { unsigned int count; struct command { enum instruction { NOTE=0, MOV=1, ADD=2, SUB=3, CMP=4, JNE=5, JE=6, JL=7, JG=8, JMP=9, BRA=10, RET=11, CFL=12, HALT=13, LABEL=14, JNEL=15, JEL=16, JLL=17, JGL=18, JMPL=19, BRAL=20, NOP=21 } cmd; unsigned int data; unsigned int reg; struct command *next; struct command *prev; } *first; struct command *last; struct command *current; struct command **offsets; int nlabels; int *labels; } *

user_defined_types: struct song { unsigned int count; struct command *first; struct command *last; struct command *current; struct command **offsets; int nlabels; int *labels; }

user_defined_types: struct command { enum instruction cmd; unsigned int data; unsigned int reg; struct command *next; struct command *prev; }


seeksong

decompiled_code:
__int64 __fastcall seeksong(__int64 a1, unsigned int a2)
{
if ( *(_DWORD *)a1 < a2 )
return 4294967294LL;
*(_QWORD *)(a1 + 24) = *(_QWORD *)(*(_QWORD *)(a1 + 32) + 8LL * (int)a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned int a2)
{
if ( *(_DWORD *)a1 < a2 )
return 4294967294LL;
*(_QWORD *)(a1 + 24) = *(_QWORD *)(*(_QWORD *)(a1 + 32) + 8LL * (int)a2);
return 0LL;
}

original_code:
int seeksong(song *s, int pos) {
 int i;
 if(pos > s->count)
  return(-2);
 s->current = s->offsets[pos];
 return(0);
}

canonical_original_code:
int seeksong(struct song *s, int pos) {
 int i;
 if(pos > s->count)
  return(-2);
 s->current = s->offsets[pos];
 return(0);
}

variable_types: pos int

variable_types: i int

user_defined_types: enum instruction { NOTE=0, MOV=1, ADD=2, SUB=3, CMP=4, JNE=5, JE=6, JL=7, JG=8, JMP=9, BRA=10, RET=11, CFL=12, HALT=13, LABEL=14, JNEL=15, JEL=16, JLL=17, JGL=18, JMPL=19, BRAL=20, NOP=21 }


initcommand

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

result = malloc(0x20uLL);
if ( result )
{
result[2] = 0;
*(_QWORD *)result = 0xFF00000015LL;
*((_QWORD *)result + 3) = 0LL;
*((_QWORD *)result + 2) = 0LL;
}
return result;
}

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

result = malloc(0x20uLL);
if ( result )
{
result[2] = 0;
*(_QWORD *)result = 0xFF00000015LL;
*((_QWORD *)result + 3) = 0LL;
*((_QWORD *)result + 2) = 0LL;
}
return result;
}

original_code:
command *initcommand() {
 command *c;
 c = malloc(sizeof(command));
 if(c == ((void *)0)) {
  return(((void *)0));
 }
 c->cmd = NOP;
 c->reg = (0);
 c->data = (0 << 8 | 0xFF);
 c->prev = ((void *)0);
 c->next = ((void *)0);
 return(c);
}

canonical_original_code:
struct command *initcommand() {
 struct command *c;
 c = malloc(sizeof(command));
 if(c == ((void *)0)) {
  return(((void *)0));
 }
 c->cmd = NOP;
 c->reg = (0);
 c->data = (0 << 8 | 0xFF);
 c->prev = ((void *)0);
 c->next = ((void *)0);
 return(c);
}

variable_types: struct command { enum instruction { NOTE=0, MOV=1, ADD=2, SUB=3, CMP=4, JNE=5, JE=6, JL=7, JG=8, JMP=9, BRA=10, RET=11, CFL=12, HALT=13, LABEL=14, JNEL=15, JEL=16, JLL=17, JGL=18, JMPL=19, BRAL=20, NOP=21 } cmd; unsigned int data; unsigned int reg; struct command *next; struct command *prev; } *


freecommand

decompiled_code:
__int64 __fastcall freecommand(void *a1)
{
if ( !a1 )
return 4294967294LL;
free(a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(void *a1)
{
if ( !a1 )
return 4294967294LL;
free(a1);
return 0LL;
}

original_code:
int freecommand(command *c) {
 if(c == ((void *)0))
  return(-2);
 free(c);
 return(0);
}

canonical_original_code:
int freecommand(struct command *c) {
 if(c == ((void *)0))
  return(-2);
 free(c);
 return(0);
}


freesong

decompiled_code:
__int64 __fastcall freesong(void *ptr)
{
_QWORD *i; // rdi
_QWORD *v3; // rbx

for ( i = (_QWORD *)*((_QWORD *)ptr + 1); ; i = v3 )
{
if ( !i )
{
free(*((void **)ptr + 4));
free(ptr);
return 0LL;
}
v3 = (_QWORD *)i[2];
if ( (int)freecommand(i) < 0 )
break;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func4(void *ptr)
{
_QWORD *i; // rdi
_QWORD *v3; // rbx

for ( i = (_QWORD *)*((_QWORD *)ptr + 1); ; i = v3 )
{
if ( !i )
{
free(*((void **)ptr + 4));
free(ptr);
return 0LL;
}
v3 = (_QWORD *)i[2];
if ( (int)func3(i) < 0 )
break;
}
return 0xFFFFFFFFLL;
}

original_code:
int freesong(song *s) {
 command *cur;
 command *next;
 cur = s->first;
 while(cur != ((void *)0)) {
  next = cur->next;
  if(freecommand(cur) < 0)
   return(-1);
  cur = next;
 }
 free(s->offsets);
 free(s);
 return(0);
}

canonical_original_code:
int freesong(struct song *s) {
 struct command *cur;
 struct command *next;
 cur = s->first;
 while(cur != ((void *)0)) {
  next = cur->next;
  if(freecommand(cur) < 0)
   return(-1);
  cur = next;
 }
 free(s->offsets);
 free(s);
 return(0);
}


addcmd

original_code:
int addcmd(command *c, song *s) {
 if(s->current == ((void *)0)) {
  if(s->first == ((void *)0)) {
   s->first = c;
   s->last = c;
   s->current = ((void *)0);
  } else {
   s->last->next = c;
   c->prev = s->last;
   s->last = c;
   s->current = ((void *)0);
  }
 } else {
  if(s->current == s->first) {
   s->current->prev = c;
   c->next = s->current;
   s->first = c;
  } else {
   c->prev = s->current->prev;
   c->next = s->current;
   s->current->prev->next = c;
   s->current->prev = c;
  }
 }
 s->count++;
 return(0);
}

canonical_original_code:
int addcmd(struct command *c, struct song *s) {
 if(s->current == ((void *)0)) {
  if(s->first == ((void *)0)) {
   s->first = c;
   s->last = c;
   s->current = ((void *)0);
  } else {
   s->last->next = c;
   c->prev = s->last;
   s->last = c;
   s->current = ((void *)0);
  }
 } else {
  if(s->current == s->first) {
   s->current->prev = c;
   c->next = s->current;
   s->first = c;
  } else {
   c->prev = s->current->prev;
   c->next = s->current;
   s->current->prev->next = c;
   s->current->prev = c;
  }
 }
 s->count++;
 return(0);
}

variable_types: struct song { unsigned int count; struct command *first; struct command *last; struct command *current; struct command **offsets; int nlabels; int *labels; } *


delcmd

original_code:
command *delcmd(song *s) {
 command *cur, *set;
 if(s->current == ((void *)0))
  return(((void *)0));
 cur = s->current;
 if(s->current == s->first) {
  if(s->current == s->last) {
   s->first = ((void *)0);
   s->last = ((void *)0);
   s->current = ((void *)0);
  } else {
   set = s->current->next;
   s->first = s->current->next;
   s->current->next->prev = ((void *)0);
   s->current->next = ((void *)0);
   s->current = set;
  }
 } else {
  if(s->current == s->last) {
   s->last = s->current->prev;
   s->current->prev->next = ((void *)0);
   s->current->prev = ((void *)0);
   s->current = ((void *)0);
  } else {
   set = s->current->next;
   s->current->prev->next = s->current->next;
   s->current->next->prev = s->current->prev;
   s->current = set;
  }
 }
 return(cur);
}

canonical_original_code:
struct command *delcmd(struct song *s) {
 struct command *cur; struct command *set;
 if(s->current == ((void *)0))
  return(((void *)0));
 cur = s->current;
 if(s->current == s->first) {
  if(s->current == s->last) {
   s->first = ((void *)0);
   s->last = ((void *)0);
   s->current = ((void *)0);
  } else {
   set = s->current->next;
   s->first = s->current->next;
   s->current->next->prev = ((void *)0);
   s->current->next = ((void *)0);
   s->current = set;
  }
 } else {
  if(s->current == s->last) {
   s->last = s->current->prev;
   s->current->prev->next = ((void *)0);
   s->current->prev = ((void *)0);
   s->current = ((void *)0);
  } else {
   set = s->current->next;
   s->current->prev->next = s->current->next;
   s->current->next->prev = s->current->prev;
   s->current = set;
  }
 }
 return(cur);
}

return_type: struct command { enum instruction { NOTE=0, MOV=1, ADD=2, SUB=3, CMP=4, JNE=5, JE=6, JL=7, JG=8, JMP=9, BRA=10, RET=11, CFL=12, HALT=13, LABEL=14, JNEL=15, JEL=16, JLL=17, JGL=18, JMPL=19, BRAL=20, NOP=21 } cmd; unsigned int data; unsigned int reg; struct command *next; struct command *prev; } *


rewindsong

decompiled_code:
__int64 __fastcall rewindsong(__int64 a1)
{
*(_QWORD *)(a1 + 24) = *(_QWORD *)(a1 + 8);
return 0LL;
}

original_code:
int rewindsong(song *s) {
 s->current = s->first;
 return(0);
}

canonical_original_code:
int rewindsong(struct song *s) {
 s->current = s->first;
 return(0);
}


nextcommand

original_code:
int nextcommand(song *s) {
 if(s->current == ((void *)0))
  return(-2);
 s->current = s->current->next;
 if(s->current == ((void *)0))
  return(1);
 return(0);
}

canonical_original_code:
int nextcommand(struct song *s) {
 if(s->current == ((void *)0))
  return(-2);
 s->current = s->current->next;
 if(s->current == ((void *)0))
  return(1);
 return(0);
}


prevcommand

original_code:
int prevcommand(song *s) {
 if(s->current->prev == ((void *)0))
  return(-2);
 s->current = s->current->prev;
 return(0);
}

canonical_original_code:
int prevcommand(struct song *s) {
 if(s->current->prev == ((void *)0))
  return(-2);
 s->current = s->current->prev;
 return(0);
}


playsong

decompiled_code:
__int64 __fastcall playsong(int a1, unsigned int *a2, __int64 a3, void (__fastcall *a4)(_QWORD, _QWORD), int a5)
{
int v7; // er13
unsigned int *v10; // rax
unsigned int v11; // er8
unsigned int v13; // edx
__int64 v14; // rax
__int64 v15; // rax
unsigned int *v16; // rdx
__int64 v17; // rsi
unsigned int v18; // edx
__int64 v19; // rsi
int v20; // edx
unsigned int v21; // edx
__int64 v22; // rdx
__int64 v23; // rax
unsigned int v24; // ecx
bool v25; // zf
__int64 v26; // rdx
__int64 v27; // rax
__int64 v28; // rdx
__int64 v29; // rax
__int64 v30; // rdx
__int64 v31; // rax
unsigned int v32; // eax
__int64 v33; // rsi
unsigned __int64 v34; // rsi
int v35; // eax
unsigned int v36; // eax
struct timespec v37; // [rsp+0h] [rbp-58h] BYREF
struct timespec remaining; // [rsp+10h] [rbp-48h] BYREF

v7 = 2;
while ( 1 )
{
if ( a4 )
a4((unsigned int)(v7 - 1), *a2);
v10 = (unsigned int *)*((_QWORD *)a2 + 3);
*(_QWORD *)(a3 + 248) = v10;
if ( !v10 )
break;
v11 = *v10;
switch ( *v10 )
{
case 0u:
v33 = v10[1];
if ( (unsigned __int8)v33 == 255 )
{
if ( (int)playnote(a1, (unsigned int)v33 >> 8, *(_DWORD *)(a3 + 56)) < 0 )
return 4;
}
else
{
if ( (unsigned int)v33 > 0x19 )
return 2;
if ( (int)playnote(a1, *(_DWORD *)(a3 + 4 * v33), *(_DWORD *)(a3 + 56)) < 0 )
return 4;
}
v34 = 0xDF8475800uLL / *(unsigned int *)(a3 + 4) / *(unsigned int *)(a3 + 12);
v35 = *(_DWORD *)(a3 + 44);
if ( v35 == 1 )
{
v34 = (3 * v34) >> 1;
}
else if ( v35 == 2 )
{
v34 /= 3uLL;
}
v37.tv_sec = v34 / 0x3B9ACA00;
v37.tv_nsec = v34 % 0x3B9ACA00;
while ( nanosleep(&v37, &remaining) == -1 )
v37 = remaining;
LABEL_14:
v16 = (unsigned int *)*((_QWORD *)a2 + 3);
v10 = *(unsigned int **)(a3 + 248);
LABEL_20:
if ( v16 == v10 )
{
LABEL_44:
if ( (unsigned int)nextcommand(a2) == 1 )
return 4 * ((unsigned int)playnote(a1, 0, 0) >> 31);
}
if ( a5 == 1 )
return 6;
++v7;
break;
case 1u:
v30 = v10[2];
if ( (unsigned int)v30 > 0x19 )
return 3;
v31 = v10[1];
if ( (unsigned __int8)v31 == 255 )
{
v32 = (unsigned int)v31 >> 8;
if ( (_DWORD)v30 != 11 )
goto LABEL_88;
}
else
{
if ( (unsigned int)v31 > 0x19 )
return 2;
v32 = *(_DWORD *)(a3 + 4 * v31);
if ( (_DWORD)v30 != 11 )
{
LABEL_88:
*(_DWORD *)(a3 + 4 * v30) = v32;
v32 = *(_DWORD *)(a3 + 44);
goto LABEL_89;
}
}
if ( *(_DWORD *)(a3 + 44) == v32 )
{
*(_DWORD *)(a3 + 44) = 0;
v32 = 0;
}
else
{
*(_DWORD *)(a3 + 44) = v32;
}
LABEL_89:
if ( *(_DWORD *)(a3 + 56) > 6u )
*(_DWORD *)(a3 + 56) = 6;
if ( !*(_DWORD *)(a3 + 12) )
*(_DWORD *)(a3 + 12) = 1;
if ( !*(_DWORD *)(a3 + 4) )
*(_DWORD *)(a3 + 4) = 1;
if ( v32 > 2 )
*(_DWORD *)(a3 + 44) = 0;
goto LABEL_44;
case 2u:
v28 = v10[2];
if ( (unsigned int)v28 > 0x19 )
return 3;
v29 = v10[1];
if ( (unsigned __int8)v29 == 255 )
{
*(_DWORD *)(a3 + 4 * v28) += (unsigned int)v29 >> 8;
}
else
{
if ( (unsigned int)v29 > 0x19 )
return 2;
*(_DWORD *)(a3 + 4 * v28) += *(_DWORD *)(a3 + 4 * v29);
}
if ( *(_DWORD *)(a3 + 56) > 6u )
*(_DWORD *)(a3 + 56) = 6;
if ( *(_DWORD *)(a3 + 44) > 2u )
*(_DWORD *)(a3 + 44) = 0;
goto LABEL_44;
case 3u:
v26 = v10[2];
if ( (unsigned int)v26 > 0x19 )
return v11;
v27 = v10[1];
if ( (unsigned __int8)v27 == 255 )
{
*(_DWORD *)(a3 + 4 * v26) -= (unsigned int)v27 >> 8;
}
else
{
if ( (unsigned int)v27 > 0x19 )
return 2;
*(_DWORD *)(a3 + 4 * v26) -= *(_DWORD *)(a3 + 4 * v27);
}
if ( !*(_DWORD *)(a3 + 12) )
*(_DWORD *)(a3 + 12) = 1;
goto LABEL_44;
case 4u:
v22 = v10[2];
if ( (unsigned int)v22 > 0x19 )
return 3;
v23 = v10[1];
if ( (unsigned __int8)v23 == 255 )
{
v36 = (unsigned int)v23 >> 8;
v25 = *(_DWORD *)(a3 + 4 * v22) == v36;
if ( *(_DWORD *)(a3 + 4 * v22) < v36 )
goto LABEL_111;
}
else
{
if ( (unsigned int)v23 > 0x19 )
return 2;
v24 = *(_DWORD *)(a3 + 4 * v22);
v25 = *(_DWORD *)(a3 + 4 * v23) == v24;
if ( *(_DWORD *)(a3 + 4 * v23) > v24 )
{
LABEL_111:
*(_DWORD *)(a3 + 104) |= 2u;
goto LABEL_44;
}
}
if ( v25 )
*(_DWORD *)(a3 + 104) |= 1u;
goto LABEL_44;
case 5u:
v19 = v10[1];
if ( (unsigned __int8)v19 != 255 )
{
if ( (unsigned int)v19 > 0x19 )
return 2;
if ( (*(_BYTE *)(a3 + 104) & 1) == 0 )
{
LABEL_19:
seeksong(a2, *(unsigned int *)(a3 + 4 * v19));
v16 = (unsigned int *)*((_QWORD *)a2 + 3);
v10 = *(unsigned int **)(a3 + 248);
goto LABEL_20;
}
goto LABEL_44;
}
if ( (*(_BYTE *)(a3 + 104) & 1) != 0 )
goto LABEL_44;
LABEL_108:
seeksong(a2, (unsigned int)v19 >> 8);
v16 = (unsigned int *)*((_QWORD *)a2 + 3);
v10 = *(unsigned int **)(a3 + 248);
goto LABEL_20;
case 6u:
v19 = v10[1];
if ( (unsigned __int8)v19 == 255 )
{
if ( (*(_BYTE *)(a3 + 104) & 1) == 0 )
goto LABEL_44;
goto LABEL_108;
}
if ( (unsigned int)v19 > 0x19 )
return 2;
if ( (*(_BYTE *)(a3 + 104) & 1) == 0 )
goto LABEL_44;
goto LABEL_19;
case 7u:
v19 = v10[1];
if ( (unsigned __int8)v19 == 255 )
{
if ( (*(_BYTE *)(a3 + 104) & 2) != 0 )
goto LABEL_44;
goto LABEL_108;
}
if ( (unsigned int)v19 > 0x19 )
return 2;
if ( (*(_BYTE *)(a3 + 104) & 2) != 0 )
goto LABEL_44;
goto LABEL_19;
case 8u:
v19 = v10[1];
if ( (unsigned __int8)v19 == 255 )
{
if ( (*(_BYTE *)(a3 + 104) & 2) == 0 )
goto LABEL_44;
goto LABEL_108;
}
if ( (unsigned int)v19 > 0x19 )
return 2;
if ( (*(_BYTE *)(a3 + 104) & 2) == 0 )
goto LABEL_44;
goto LABEL_19;
case 9u:
goto LABEL_17;
case 0xAu:
v17 = *((_QWORD *)v10 + 2);
if ( !v17 )
return 1;
v18 = *(_DWORD *)(a3 + 240);
*(_QWORD *)(a3 + 8LL * v18 + 112) = v17;
*(_DWORD *)(a3 + 240) = v18 + 1;
LABEL_17:
v19 = v10[1];
if ( (unsigned __int8)v19 == 255 )
goto LABEL_108;
if ( (unsigned int)v19 <= 0x19 )
goto LABEL_19;
return 2;
case 0xBu:
v20 = *(_DWORD *)(a3 + 240);
if ( !v20 )
return 1;
v21 = v20 - 1;
*(_DWORD *)(a3 + 240) = v21;
v16 = *(unsigned int **)(a3 + 8LL * v21 + 112);
*((_QWORD *)a2 + 3) = v16;
goto LABEL_20;
case 0xCu:
*(_DWORD *)(a3 + 104) = 0;
goto LABEL_44;
case 0xDu:
goto LABEL_6;
case 0xEu:
v16 = v10;
goto LABEL_20;
case 0xFu:
v14 = v10[1];
if ( (unsigned __int8)v14 == 255 )
{
if ( (*(_BYTE *)(a3 + 104) & 1) != 0 )
goto LABEL_44;
goto LABEL_24;
}
if ( (unsigned int)v14 > 0x19 )
return 2;
if ( (*(_BYTE *)(a3 + 104) & 1) != 0 )
goto LABEL_44;
goto LABEL_12;
case 0x10u:
v14 = v10[1];
if ( (unsigned __int8)v14 == 255 )
{
if ( (*(_BYTE *)(a3 + 104) & 1) == 0 )
goto LABEL_44;
goto LABEL_24;
}
if ( (unsigned int)v14 > 0x19 )
return 2;
if ( (*(_BYTE *)(a3 + 104) & 1) == 0 )
goto LABEL_44;
goto LABEL_12;
case 0x11u:
v14 = v10[1];
if ( (unsigned __int8)v14 == 255 )
{
if ( (*(_BYTE *)(a3 + 104) & 2) != 0 )
goto LABEL_44;
goto LABEL_24;
}
if ( (unsigned int)v14 > 0x19 )
return 2;
if ( (*(_BYTE *)(a3 + 104) & 2) != 0 )
goto LABEL_44;
goto LABEL_12;
case 0x12u:
v14 = v10[1];
if ( (unsigned __int8)v14 == 255 )
{
if ( (*(_BYTE *)(a3 + 104) & 2) != 0 )
{
LABEL_24:
v15 = (unsigned int)v14 >> 8;
if ( a2[10] < (unsigned int)v15 )
return 2;
goto LABEL_13;
}
}
else
{
if ( (unsigned int)v14 > 0x19 )
return 2;
if ( (*(_BYTE *)(a3 + 104) & 2) != 0 )
{
LABEL_12:
v15 = *(unsigned int *)(a3 + 4 * v14);
if ( (unsigned int)v15 > a2[10] )
return 2;
LABEL_13:
seeksong(a2, *(unsigned int *)(*((_QWORD *)a2 + 6) + 4 * v15));
goto LABEL_14;
}
}
goto LABEL_44;
case 0x13u:
goto LABEL_10;
case 0x14u:
v13 = *(_DWORD *)(a3 + 240);
*(_QWORD *)(a3 + 8LL * v13 + 112) = *((_QWORD *)v10 + 2);
*(_DWORD *)(a3 + 240) = v13 + 1;
LABEL_10:
v14 = v10[1];
if ( (unsigned __int8)v14 == 255 )
goto LABEL_24;
if ( (unsigned int)v14 <= 0x19 )
goto LABEL_12;
return 2;
default:
JUMPOUT(0xFF9LL);
}
}
LABEL_6:
nextcommand(a2);
return 4 * ((unsigned int)playnote(a1, 0, 0) >> 31);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, unsigned int *a2, __int64 a3, void (__fastcall *a4)(_QWORD, _QWORD), int a5)
{
int v7; // er13
unsigned int *v10; // rax
unsigned int v11; // er8
unsigned int v13; // edx
__int64 v14; // rax
__int64 v15; // rax
unsigned int *v16; // rdx
__int64 v17; // rsi
unsigned int v18; // edx
__int64 v19; // rsi
int v20; // edx
unsigned int v21; // edx
__int64 v22; // rdx
__int64 v23; // rax
unsigned int v24; // ecx
bool v25; // zf
__int64 v26; // rdx
__int64 v27; // rax
__int64 v28; // rdx
__int64 v29; // rax
__int64 v30; // rdx
__int64 v31; // rax
unsigned int v32; // eax
__int64 v33; // rsi
unsigned __int64 v34; // rsi
int v35; // eax
unsigned int v36; // eax
struct timespec v37; // [rsp+0h] [rbp-58h] BYREF
struct timespec remaining; // [rsp+10h] [rbp-48h] BYREF

v7 = 2;
while ( 1 )
{
if ( a4 )
a4((unsigned int)(v7 - 1), *a2);
v10 = (unsigned int *)*((_QWORD *)a2 + 3);
*(_QWORD *)(a3 + 248) = v10;
if ( !v10 )
break;
v11 = *v10;
switch ( *v10 )
{
case 0u:
v33 = v10[1];
if ( (unsigned __int8)v33 == 255 )
{
if ( (int)func0(a1, (unsigned int)v33 >> 8, *(_DWORD *)(a3 + 56)) < 0 )
return 4;
}
else
{
if ( (unsigned int)v33 > 0x19 )
return 2;
if ( (int)func0(a1, *(_DWORD *)(a3 + 4 * v33), *(_DWORD *)(a3 + 56)) < 0 )
return 4;
}
v34 = 0xDF8475800uLL / *(unsigned int *)(a3 + 4) / *(unsigned int *)(a3 + 12);
v35 = *(_DWORD *)(a3 + 44);
if ( v35 == 1 )
{
v34 = (3 * v34) >> 1;
}
else if ( v35 == 2 )
{
v34 /= 3uLL;
}
v37.tv_sec = v34 / 0x3B9ACA00;
v37.tv_nsec = v34 % 0x3B9ACA00;
while ( nanosleep(&v37, &remaining) == -1 )
v37 = remaining;
LABEL_14:
v16 = (unsigned int *)*((_QWORD *)a2 + 3);
v10 = *(unsigned int **)(a3 + 248);
LABEL_20:
if ( v16 == v10 )
{
LABEL_44:
if ( (unsigned int)nextcommand(a2) == 1 )
return 4 * ((unsigned int)func0(a1, 0, 0) >> 31);
}
if ( a5 == 1 )
return 6;
++v7;
break;
case 1u:
v30 = v10[2];
if ( (unsigned int)v30 > 0x19 )
return 3;
v31 = v10[1];
if ( (unsigned __int8)v31 == 255 )
{
v32 = (unsigned int)v31 >> 8;
if ( (_DWORD)v30 != 11 )
goto LABEL_88;
}
else
{
if ( (unsigned int)v31 > 0x19 )
return 2;
v32 = *(_DWORD *)(a3 + 4 * v31);
if ( (_DWORD)v30 != 11 )
{
LABEL_88:
*(_DWORD *)(a3 + 4 * v30) = v32;
v32 = *(_DWORD *)(a3 + 44);
goto LABEL_89;
}
}
if ( *(_DWORD *)(a3 + 44) == v32 )
{
*(_DWORD *)(a3 + 44) = 0;
v32 = 0;
}
else
{
*(_DWORD *)(a3 + 44) = v32;
}
LABEL_89:
if ( *(_DWORD *)(a3 + 56) > 6u )
*(_DWORD *)(a3 + 56) = 6;
if ( !*(_DWORD *)(a3 + 12) )
*(_DWORD *)(a3 + 12) = 1;
if ( !*(_DWORD *)(a3 + 4) )
*(_DWORD *)(a3 + 4) = 1;
if ( v32 > 2 )
*(_DWORD *)(a3 + 44) = 0;
goto LABEL_44;
case 2u:
v28 = v10[2];
if ( (unsigned int)v28 > 0x19 )
return 3;
v29 = v10[1];
if ( (unsigned __int8)v29 == 255 )
{
*(_DWORD *)(a3 + 4 * v28) += (unsigned int)v29 >> 8;
}
else
{
if ( (unsigned int)v29 > 0x19 )
return 2;
*(_DWORD *)(a3 + 4 * v28) += *(_DWORD *)(a3 + 4 * v29);
}
if ( *(_DWORD *)(a3 + 56) > 6u )
*(_DWORD *)(a3 + 56) = 6;
if ( *(_DWORD *)(a3 + 44) > 2u )
*(_DWORD *)(a3 + 44) = 0;
goto LABEL_44;
case 3u:
v26 = v10[2];
if ( (unsigned int)v26 > 0x19 )
return v11;
v27 = v10[1];
if ( (unsigned __int8)v27 == 255 )
{
*(_DWORD *)(a3 + 4 * v26) -= (unsigned int)v27 >> 8;
}
else
{
if ( (unsigned int)v27 > 0x19 )
return 2;
*(_DWORD *)(a3 + 4 * v26) -= *(_DWORD *)(a3 + 4 * v27);
}
if ( !*(_DWORD *)(a3 + 12) )
*(_DWORD *)(a3 + 12) = 1;
goto LABEL_44;
case 4u:
v22 = v10[2];
if ( (unsigned int)v22 > 0x19 )
return 3;
v23 = v10[1];
if ( (unsigned __int8)v23 == 255 )
{
v36 = (unsigned int)v23 >> 8;
v25 = *(_DWORD *)(a3 + 4 * v22) == v36;
if ( *(_DWORD *)(a3 + 4 * v22) < v36 )
goto LABEL_111;
}
else
{
if ( (unsigned int)v23 > 0x19 )
return 2;
v24 = *(_DWORD *)(a3 + 4 * v22);
v25 = *(_DWORD *)(a3 + 4 * v23) == v24;
if ( *(_DWORD *)(a3 + 4 * v23) > v24 )
{
LABEL_111:
*(_DWORD *)(a3 + 104) |= 2u;
goto LABEL_44;
}
}
if ( v25 )
*(_DWORD *)(a3 + 104) |= 1u;
goto LABEL_44;
case 5u:
v19 = v10[1];
if ( (unsigned __int8)v19 != 255 )
{
if ( (unsigned int)v19 > 0x19 )
return 2;
if ( (*(_BYTE *)(a3 + 104) & 1) == 0 )
{
LABEL_19:
seeksong(a2, *(unsigned int *)(a3 + 4 * v19));
v16 = (unsigned int *)*((_QWORD *)a2 + 3);
v10 = *(unsigned int **)(a3 + 248);
goto LABEL_20;
}
goto LABEL_44;
}
if ( (*(_BYTE *)(a3 + 104) & 1) != 0 )
goto LABEL_44;
LABEL_108:
seeksong(a2, (unsigned int)v19 >> 8);
v16 = (unsigned int *)*((_QWORD *)a2 + 3);
v10 = *(unsigned int **)(a3 + 248);
goto LABEL_20;
case 6u:
v19 = v10[1];
if ( (unsigned __int8)v19 == 255 )
{
if ( (*(_BYTE *)(a3 + 104) & 1) == 0 )
goto LABEL_44;
goto LABEL_108;
}
if ( (unsigned int)v19 > 0x19 )
return 2;
if ( (*(_BYTE *)(a3 + 104) & 1) == 0 )
goto LABEL_44;
goto LABEL_19;
case 7u:
v19 = v10[1];
if ( (unsigned __int8)v19 == 255 )
{
if ( (*(_BYTE *)(a3 + 104) & 2) != 0 )
goto LABEL_44;
goto LABEL_108;
}
if ( (unsigned int)v19 > 0x19 )
return 2;
if ( (*(_BYTE *)(a3 + 104) & 2) != 0 )
goto LABEL_44;
goto LABEL_19;
case 8u:
v19 = v10[1];
if ( (unsigned __int8)v19 == 255 )
{
if ( (*(_BYTE *)(a3 + 104) & 2) == 0 )
goto LABEL_44;
goto LABEL_108;
}
if ( (unsigned int)v19 > 0x19 )
return 2;
if ( (*(_BYTE *)(a3 + 104) & 2) == 0 )
goto LABEL_44;
goto LABEL_19;
case 9u:
goto LABEL_17;
case 0xAu:
v17 = *((_QWORD *)v10 + 2);
if ( !v17 )
return 1;
v18 = *(_DWORD *)(a3 + 240);
*(_QWORD *)(a3 + 8LL * v18 + 112) = v17;
*(_DWORD *)(a3 + 240) = v18 + 1;
LABEL_17:
v19 = v10[1];
if ( (unsigned __int8)v19 == 255 )
goto LABEL_108;
if ( (unsigned int)v19 <= 0x19 )
goto LABEL_19;
return 2;
case 0xBu:
v20 = *(_DWORD *)(a3 + 240);
if ( !v20 )
return 1;
v21 = v20 - 1;
*(_DWORD *)(a3 + 240) = v21;
v16 = *(unsigned int **)(a3 + 8LL * v21 + 112);
*((_QWORD *)a2 + 3) = v16;
goto LABEL_20;
case 0xCu:
*(_DWORD *)(a3 + 104) = 0;
goto LABEL_44;
case 0xDu:
goto LABEL_6;
case 0xEu:
v16 = v10;
goto LABEL_20;
case 0xFu:
v14 = v10[1];
if ( (unsigned __int8)v14 == 255 )
{
if ( (*(_BYTE *)(a3 + 104) & 1) != 0 )
goto LABEL_44;
goto LABEL_24;
}
if ( (unsigned int)v14 > 0x19 )
return 2;
if ( (*(_BYTE *)(a3 + 104) & 1) != 0 )
goto LABEL_44;
goto LABEL_12;
case 0x10u:
v14 = v10[1];
if ( (unsigned __int8)v14 == 255 )
{
if ( (*(_BYTE *)(a3 + 104) & 1) == 0 )
goto LABEL_44;
goto LABEL_24;
}
if ( (unsigned int)v14 > 0x19 )
return 2;
if ( (*(_BYTE *)(a3 + 104) & 1) == 0 )
goto LABEL_44;
goto LABEL_12;
case 0x11u:
v14 = v10[1];
if ( (unsigned __int8)v14 == 255 )
{
if ( (*(_BYTE *)(a3 + 104) & 2) != 0 )
goto LABEL_44;
goto LABEL_24;
}
if ( (unsigned int)v14 > 0x19 )
return 2;
if ( (*(_BYTE *)(a3 + 104) & 2) != 0 )
goto LABEL_44;
goto LABEL_12;
case 0x12u:
v14 = v10[1];
if ( (unsigned __int8)v14 == 255 )
{
if ( (*(_BYTE *)(a3 + 104) & 2) != 0 )
{
LABEL_24:
v15 = (unsigned int)v14 >> 8;
if ( a2[10] < (unsigned int)v15 )
return 2;
goto LABEL_13;
}
}
else
{
if ( (unsigned int)v14 > 0x19 )
return 2;
if ( (*(_BYTE *)(a3 + 104) & 2) != 0 )
{
LABEL_12:
v15 = *(unsigned int *)(a3 + 4 * v14);
if ( (unsigned int)v15 > a2[10] )
return 2;
LABEL_13:
seeksong(a2, *(unsigned int *)(*((_QWORD *)a2 + 6) + 4 * v15));
goto LABEL_14;
}
}
goto LABEL_44;
case 0x13u:
goto LABEL_10;
case 0x14u:
v13 = *(_DWORD *)(a3 + 240);
*(_QWORD *)(a3 + 8LL * v13 + 112) = *((_QWORD *)v10 + 2);
*(_DWORD *)(a3 + 240) = v13 + 1;
LABEL_10:
v14 = v10[1];
if ( (unsigned __int8)v14 == 255 )
goto LABEL_24;
if ( (unsigned int)v14 <= 0x19 )
goto LABEL_12;
return 2;
default:
JUMPOUT(0xFF9LL);
}
}
LABEL_6:
nextcommand(a2);
return 4 * ((unsigned int)func0(a1, 0, 0) >> 31);
}

original_code:
vmexception playsong(int fd, song *s, vmstate *vm, void (*status)(int cur, int max), int step) {
 int i;
 struct timespec req, rem;
 unsigned long int notelen;
 unsigned int t;
 int running;
 i = 1;
 running = 1;
 while(running == 1) {
  if(status != ((void *)0))
   status(i, s->count);
  vm->last = s->current;
  if(s->current != ((void *)0)) {
   switch(s->current->cmd) {
    case NOTE:
      if((s->current->data & 0xFF) == 0xFF) {
       t = (s->current->data >> 8);
       if(playnote(fd, (s->current->data >> 8), vm->regs[(14)]) < 0)
        return(DEVICE_ERROR);
      } else if(s->current->data < 26) {
       if(playnote(fd, vm->regs[s->current->data], vm->regs[(14)]) < 0)
        return(DEVICE_ERROR);
      } else {
       return(BAD_ARGUMENT);
      }
     notelen = 60000000000ul / vm->regs[(1)] / vm->regs[(3)];
     if(vm->regs[(11)] == (1)) {
      notelen = notelen * 3 / 2;
     } else if(vm->regs[(11)] == (2)) {
      notelen = notelen / 3;
     }
     req.tv_sec = notelen / 1000000000ul;
     req.tv_nsec = notelen % 1000000000ul;
     while(nanosleep(&req, &rem) == -1) {
      req.tv_sec = rem.tv_sec;
      req.tv_nsec = rem.tv_nsec;
     }
     break;
    case MOV:
     if(s->current->reg < 26) {
      if((s->current->data & 0xFF) == 0xFF) {
       if(s->current->reg == (11)) {
        if(vm->regs[(11)] == (s->current->data >> 8)) {
         vm->regs[(11)] = 0;
        } else {
         vm->regs[s->current->reg] = (s->current->data >> 8);
        }
       } else {
        vm->regs[s->current->reg] = (s->current->data >> 8);
       }
      } else if(s->current->data < 26) {
       if(s->current->reg == (11)) {
        if(vm->regs[(11)] == vm->regs[s->current->data]) {
         vm->regs[(11)] = 0;
        } else {
         vm->regs[s->current->reg] = vm->regs[s->current->data];
        }
       } else {
        vm->regs[s->current->reg] = vm->regs[s->current->data];
       }
      } else {
       return(BAD_ARGUMENT);
      }
     } else {
      return(BAD_REGISTER);
     }
     if(vm->regs[(14)] > 6)
      vm->regs[(14)] = 6;
     if(vm->regs[(3)] < 1)
      vm->regs[(3)] = 1;
     if(vm->regs[(1)] < 1)
      vm->regs[(1)] = 1;
     if(vm->regs[(11)] > 2)
      vm->regs[(11)] = 0;
     break;
    case ADD:
     if(s->current->reg < 26) {
      if((s->current->data & 0xFF) == 0xFF) {
       vm->regs[s->current->reg] += (s->current->data >> 8);
      } else if(s->current->data < 26) {
       vm->regs[s->current->reg] += vm->regs[s->current->data];
      } else {
       return(BAD_ARGUMENT);
      }
     } else {
      return(BAD_REGISTER);
     }
     if(vm->regs[(14)] > 6)
      vm->regs[(14)] = 6;
     if(vm->regs[(11)] > 2)
      vm->regs[(11)] = 0;
     break;
    case SUB:
     if(s->current->reg < 26) {
      if((s->current->data & 0xFF) == 0xFF) {
       vm->regs[s->current->reg] -= (s->current->data >> 8);
      } else if(s->current->data < 26) {
       vm->regs[s->current->reg] -= vm->regs[s->current->data];
      } else {
       return(BAD_ARGUMENT);
      }
     } else {
      return(BAD_REGISTER);
     }
     if(vm->regs[(3)] < 1)
      vm->regs[(3)] = 1;
     break;
    case CMP:
     if(s->current->reg < 26) {
      if((s->current->data & 0xFF) == 0xFF) {
       t = (s->current->data >> 8);
       if(t > vm->regs[s->current->reg])
        vm->flags |= (2);
       if(t == vm->regs[s->current->reg])
        vm->flags |= (1);
      } else if(s->current->data < 26) {
       if(vm->regs[s->current->data] > vm->regs[s->current->reg])
        vm->flags |= (2);
       if(vm->regs[s->current->data] == vm->regs[s->current->reg])
        vm->flags |= (1);
      } else {
       return(BAD_ARGUMENT);
      }
     } else {
      return(BAD_REGISTER);
     }
     break;
    case JNE:
     if((s->current->data & 0xFF) == 0xFF) {
      if(!(vm->flags & (1))) {
       t = (s->current->data >> 8);
       seeksong(s, t);
      }
     } else if(s->current->data < 26) {
      if(!(vm->flags & (1)))
       seeksong(s, vm->regs[s->current->data]);
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JE:
     if((s->current->data & 0xFF) == 0xFF) {
      if(vm->flags & (1)) {
       t = (s->current->data >> 8);
       seeksong(s, t);
      }
     } else if(s->current->data < 26) {
      if(vm->flags & (1))
       seeksong(s, vm->regs[s->current->data]);
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JL:
     if((s->current->data & 0xFF) == 0xFF) {
      if(!(vm->flags & (2))) {
       t = (s->current->data >> 8);
       seeksong(s, t);
      }
     } else if(s->current->data < 26) {
      if(!(vm->flags & (2)))
       seeksong(s, vm->regs[s->current->data]);
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JG:
     if((s->current->data & 0xFF) == 0xFF) {
      if(vm->flags & (2)) {
       t = (s->current->data >> 8);
       seeksong(s, t);
      }
     } else if(s->current->data < 26) {
      if(vm->flags & (2))
       seeksong(s, vm->regs[s->current->data]);
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JMP:
     if((s->current->data & 0xFF) == 0xFF) {
      t = (s->current->data >> 8);
      seeksong(s, t);
     } else if(s->current->data < 26) {
      seeksong(s, vm->regs[s->current->data]);
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case BRA:
     if(s->current->next == ((void *)0))
      return(ILLEGAL_INSTRUCTION);
     vm->stack[vm->sp] = s->current->next;
     vm->sp++;
     if((s->current->data & 0xFF) == 0xFF) {
      t = (s->current->data >> 8);
      seeksong(s, t);
     } else if(s->current->data < 26) {
      seeksong(s, vm->regs[s->current->data]);
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JNEL:
     if((s->current->data & 0xFF) == 0xFF) {
      if(!(vm->flags & (1))) {
       t = (s->current->data >> 8);
       if(t > s->nlabels)
        return(BAD_ARGUMENT);
       seeksong(s, s->labels[t]);
      }
     } else if(s->current->data < 26) {
      if(!(vm->flags & (1))) {
       if(vm->regs[s->current->data] > s->nlabels)
        return(BAD_ARGUMENT);
       seeksong(s, s->labels[vm->regs[s->current->data]]);
      }
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JEL:
     if((s->current->data & 0xFF) == 0xFF) {
      if(vm->flags & (1)) {
       t = (s->current->data >> 8);
       if(t > s->nlabels)
        return(BAD_ARGUMENT);
       seeksong(s, s->labels[t]);
      }
     } else if(s->current->data < 26) {
      if(vm->flags & (1)) {
       if(vm->regs[s->current->data] > s->nlabels)
        return(BAD_ARGUMENT);
       seeksong(s, s->labels[vm->regs[s->current->data]]);
      }
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JLL:
     if((s->current->data & 0xFF) == 0xFF) {
      if(!(vm->flags & (2))) {
       t = (s->current->data >> 8);
       if(t > s->nlabels)
        return(BAD_ARGUMENT);
       seeksong(s, s->labels[t]);
      }
     } else if(s->current->data < 26) {
      if(!(vm->flags & (2))) {
       if(vm->regs[s->current->data] > s->nlabels)
        return(BAD_ARGUMENT);
       seeksong(s, s->labels[vm->regs[s->current->data]]);
      }
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JGL:
     if((s->current->data & 0xFF) == 0xFF) {
      if(vm->flags & (2)) {
       t = (s->current->data >> 8);
       if(t > s->nlabels)
        return(BAD_ARGUMENT);
       seeksong(s, s->labels[t]);
      }
     } else if(s->current->data < 26) {
      if(vm->flags & (2)) {
       if(vm->regs[s->current->data] > s->nlabels)
        return(BAD_ARGUMENT);
       seeksong(s, s->labels[vm->regs[s->current->data]]);
      }
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JMPL:
     if((s->current->data & 0xFF) == 0xFF) {
      t = (s->current->data >> 8);
      if(t > s->nlabels)
       return(BAD_ARGUMENT);
      seeksong(s, s->labels[t]);
     } else if(s->current->data < 26) {
       if(vm->regs[s->current->data] > s->nlabels)
       return(BAD_ARGUMENT);
      seeksong(s, s->labels[vm->regs[s->current->data]]);
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case BRAL:
     vm->stack[vm->sp] = s->current->next;
     vm->sp++;
     if((s->current->data & 0xFF) == 0xFF) {
      t = (s->current->data >> 8);
      if(t > s->nlabels)
       return(BAD_ARGUMENT);
      seeksong(s, s->labels[t]);
     } else if(s->current->data < 26) {
       if(vm->regs[s->current->data] > s->nlabels)
       return(BAD_ARGUMENT);
      seeksong(s, s->labels[vm->regs[s->current->data]]);
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case RET:
     if(vm->sp == 0)
      return(ILLEGAL_INSTRUCTION);
     vm->sp--;
     s->current = vm->stack[vm->sp];
     break;
    case CFL:
     vm->flags = 0;
     break;
    case HALT:
     running = 0;
     break;
    case LABEL:
     break;
    default:
     return(ILLEGAL_INSTRUCTION);
     break;
   }
  } else {
   running = 0;
  }
  i++;
  if(s->current == vm->last)
   if(nextcommand(s) == 1)
    running = 0;
  if(running == 1 && step == 1) {
   return(BREAKPOINT);
  }
 }
 if(playnote(fd, 0, 0) < 0)
  return(DEVICE_ERROR);
 return(PROGRAM_ENDED);
}

canonical_original_code:
enum vmexception playsong(int fd, struct song *s, struct vmstate *vm, void (*status)(int cur, int max), int step) {
 int i;
 struct timespec req; struct timespec rem;
 unsigned long notelen;
 unsigned int t;
 int running;
 i = 1;
 running = 1;
 while(running == 1) {
  if(status != ((void *)0))
   status(i, s->count);
  vm->last = s->current;
  if(s->current != ((void *)0)) {
   switch(s->current->cmd) {
    case NOTE:
      if((s->current->data & 0xFF) == 0xFF) {
       t = (s->current->data >> 8);
       if(playnote(fd, (s->current->data >> 8), vm->regs[(14)]) < 0)
        return(DEVICE_ERROR);
      } else if(s->current->data < 26) {
       if(playnote(fd, vm->regs[s->current->data], vm->regs[(14)]) < 0)
        return(DEVICE_ERROR);
      } else {
       return(BAD_ARGUMENT);
      }
     notelen = 60000000000ul / vm->regs[(1)] / vm->regs[(3)];
     if(vm->regs[(11)] == (1)) {
      notelen = notelen * 3 / 2;
     } else if(vm->regs[(11)] == (2)) {
      notelen = notelen / 3;
     }
     req.tv_sec = notelen / 1000000000ul;
     req.tv_nsec = notelen % 1000000000ul;
     while(nanosleep(&req, &rem) == -1) {
      req.tv_sec = rem.tv_sec;
      req.tv_nsec = rem.tv_nsec;
     }
     break;
    case MOV:
     if(s->current->reg < 26) {
      if((s->current->data & 0xFF) == 0xFF) {
       if(s->current->reg == (11)) {
        if(vm->regs[(11)] == (s->current->data >> 8)) {
         vm->regs[(11)] = 0;
        } else {
         vm->regs[s->current->reg] = (s->current->data >> 8);
        }
       } else {
        vm->regs[s->current->reg] = (s->current->data >> 8);
       }
      } else if(s->current->data < 26) {
       if(s->current->reg == (11)) {
        if(vm->regs[(11)] == vm->regs[s->current->data]) {
         vm->regs[(11)] = 0;
        } else {
         vm->regs[s->current->reg] = vm->regs[s->current->data];
        }
       } else {
        vm->regs[s->current->reg] = vm->regs[s->current->data];
       }
      } else {
       return(BAD_ARGUMENT);
      }
     } else {
      return(BAD_REGISTER);
     }
     if(vm->regs[(14)] > 6)
      vm->regs[(14)] = 6;
     if(vm->regs[(3)] < 1)
      vm->regs[(3)] = 1;
     if(vm->regs[(1)] < 1)
      vm->regs[(1)] = 1;
     if(vm->regs[(11)] > 2)
      vm->regs[(11)] = 0;
     break;
    case ADD:
     if(s->current->reg < 26) {
      if((s->current->data & 0xFF) == 0xFF) {
       vm->regs[s->current->reg] += (s->current->data >> 8);
      } else if(s->current->data < 26) {
       vm->regs[s->current->reg] += vm->regs[s->current->data];
      } else {
       return(BAD_ARGUMENT);
      }
     } else {
      return(BAD_REGISTER);
     }
     if(vm->regs[(14)] > 6)
      vm->regs[(14)] = 6;
     if(vm->regs[(11)] > 2)
      vm->regs[(11)] = 0;
     break;
    case SUB:
     if(s->current->reg < 26) {
      if((s->current->data & 0xFF) == 0xFF) {
       vm->regs[s->current->reg] -= (s->current->data >> 8);
      } else if(s->current->data < 26) {
       vm->regs[s->current->reg] -= vm->regs[s->current->data];
      } else {
       return(BAD_ARGUMENT);
      }
     } else {
      return(BAD_REGISTER);
     }
     if(vm->regs[(3)] < 1)
      vm->regs[(3)] = 1;
     break;
    case CMP:
     if(s->current->reg < 26) {
      if((s->current->data & 0xFF) == 0xFF) {
       t = (s->current->data >> 8);
       if(t > vm->regs[s->current->reg])
        vm->flags |= (2);
       if(t == vm->regs[s->current->reg])
        vm->flags |= (1);
      } else if(s->current->data < 26) {
       if(vm->regs[s->current->data] > vm->regs[s->current->reg])
        vm->flags |= (2);
       if(vm->regs[s->current->data] == vm->regs[s->current->reg])
        vm->flags |= (1);
      } else {
       return(BAD_ARGUMENT);
      }
     } else {
      return(BAD_REGISTER);
     }
     break;
    case JNE:
     if((s->current->data & 0xFF) == 0xFF) {
      if(!(vm->flags & (1))) {
       t = (s->current->data >> 8);
       seeksong(s, t);
      }
     } else if(s->current->data < 26) {
      if(!(vm->flags & (1)))
       seeksong(s, vm->regs[s->current->data]);
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JE:
     if((s->current->data & 0xFF) == 0xFF) {
      if(vm->flags & (1)) {
       t = (s->current->data >> 8);
       seeksong(s, t);
      }
     } else if(s->current->data < 26) {
      if(vm->flags & (1))
       seeksong(s, vm->regs[s->current->data]);
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JL:
     if((s->current->data & 0xFF) == 0xFF) {
      if(!(vm->flags & (2))) {
       t = (s->current->data >> 8);
       seeksong(s, t);
      }
     } else if(s->current->data < 26) {
      if(!(vm->flags & (2)))
       seeksong(s, vm->regs[s->current->data]);
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JG:
     if((s->current->data & 0xFF) == 0xFF) {
      if(vm->flags & (2)) {
       t = (s->current->data >> 8);
       seeksong(s, t);
      }
     } else if(s->current->data < 26) {
      if(vm->flags & (2))
       seeksong(s, vm->regs[s->current->data]);
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JMP:
     if((s->current->data & 0xFF) == 0xFF) {
      t = (s->current->data >> 8);
      seeksong(s, t);
     } else if(s->current->data < 26) {
      seeksong(s, vm->regs[s->current->data]);
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case BRA:
     if(s->current->next == ((void *)0))
      return(ILLEGAL_INSTRUCTION);
     vm->stack[vm->sp] = s->current->next;
     vm->sp++;
     if((s->current->data & 0xFF) == 0xFF) {
      t = (s->current->data >> 8);
      seeksong(s, t);
     } else if(s->current->data < 26) {
      seeksong(s, vm->regs[s->current->data]);
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JNEL:
     if((s->current->data & 0xFF) == 0xFF) {
      if(!(vm->flags & (1))) {
       t = (s->current->data >> 8);
       if(t > s->nlabels)
        return(BAD_ARGUMENT);
       seeksong(s, s->labels[t]);
      }
     } else if(s->current->data < 26) {
      if(!(vm->flags & (1))) {
       if(vm->regs[s->current->data] > s->nlabels)
        return(BAD_ARGUMENT);
       seeksong(s, s->labels[vm->regs[s->current->data]]);
      }
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JEL:
     if((s->current->data & 0xFF) == 0xFF) {
      if(vm->flags & (1)) {
       t = (s->current->data >> 8);
       if(t > s->nlabels)
        return(BAD_ARGUMENT);
       seeksong(s, s->labels[t]);
      }
     } else if(s->current->data < 26) {
      if(vm->flags & (1)) {
       if(vm->regs[s->current->data] > s->nlabels)
        return(BAD_ARGUMENT);
       seeksong(s, s->labels[vm->regs[s->current->data]]);
      }
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JLL:
     if((s->current->data & 0xFF) == 0xFF) {
      if(!(vm->flags & (2))) {
       t = (s->current->data >> 8);
       if(t > s->nlabels)
        return(BAD_ARGUMENT);
       seeksong(s, s->labels[t]);
      }
     } else if(s->current->data < 26) {
      if(!(vm->flags & (2))) {
       if(vm->regs[s->current->data] > s->nlabels)
        return(BAD_ARGUMENT);
       seeksong(s, s->labels[vm->regs[s->current->data]]);
      }
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JGL:
     if((s->current->data & 0xFF) == 0xFF) {
      if(vm->flags & (2)) {
       t = (s->current->data >> 8);
       if(t > s->nlabels)
        return(BAD_ARGUMENT);
       seeksong(s, s->labels[t]);
      }
     } else if(s->current->data < 26) {
      if(vm->flags & (2)) {
       if(vm->regs[s->current->data] > s->nlabels)
        return(BAD_ARGUMENT);
       seeksong(s, s->labels[vm->regs[s->current->data]]);
      }
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case JMPL:
     if((s->current->data & 0xFF) == 0xFF) {
      t = (s->current->data >> 8);
      if(t > s->nlabels)
       return(BAD_ARGUMENT);
      seeksong(s, s->labels[t]);
     } else if(s->current->data < 26) {
       if(vm->regs[s->current->data] > s->nlabels)
       return(BAD_ARGUMENT);
      seeksong(s, s->labels[vm->regs[s->current->data]]);
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case BRAL:
     vm->stack[vm->sp] = s->current->next;
     vm->sp++;
     if((s->current->data & 0xFF) == 0xFF) {
      t = (s->current->data >> 8);
      if(t > s->nlabels)
       return(BAD_ARGUMENT);
      seeksong(s, s->labels[t]);
     } else if(s->current->data < 26) {
       if(vm->regs[s->current->data] > s->nlabels)
       return(BAD_ARGUMENT);
      seeksong(s, s->labels[vm->regs[s->current->data]]);
     } else {
      return(BAD_ARGUMENT);
     }
     break;
    case RET:
     if(vm->sp == 0)
      return(ILLEGAL_INSTRUCTION);
     vm->sp--;
     s->current = vm->stack[vm->sp];
     break;
    case CFL:
     vm->flags = 0;
     break;
    case HALT:
     running = 0;
     break;
    case LABEL:
     break;
    default:
     return(ILLEGAL_INSTRUCTION);
     break;
   }
  } else {
   running = 0;
  }
  i++;
  if(s->current == vm->last)
   if(nextcommand(s) == 1)
    running = 0;
  if(running == 1 && step == 1) {
   return(BREAKPOINT);
  }
 }
 if(playnote(fd, 0, 0) < 0)
  return(DEVICE_ERROR);
 return(PROGRAM_ENDED);
}

variable_types: fd int

variable_types: struct vmstate { unsigned int regs[26]; unsigned int flags; struct command *stack[16]; unsigned int sp; struct command *last; } *

variable_types:
status
void (*)(int cur, int max)

variable_types: step int

variable_types: struct timespec { long tv_sec; long tv_nsec; }

variable_types:
notelen
unsigned long

variable_types:
t
unsigned int

variable_types: running int

return_type: enum vmexception { PROGRAM_ENDED=0, ILLEGAL_INSTRUCTION=1, BAD_ARGUMENT=2, BAD_REGISTER=3, DEVICE_ERROR=4, VM_ERROR=5, BREAKPOINT=6 }


initvm

canonical_original_code:
struct vmstate *initvm() {
 int i;
 struct vmstate *vm;
 vm = malloc(sizeof(vmstate));
 if(vm == ((void *)0))
  return(((void *)0));
 for(i = 0; i < 26; i++) {
  vm->regs[i] = 0;
 }
 vm->flags = 0;
 vm->regs[(14)] = (3);
 vm->regs[(3)] = (32);
 vm->regs[(1)] = (50);
 vm->regs[(11)] = (0);
 for(i = 0; i < 16; i++) {
  vm->stack[i] = 0;
 }
 vm->sp = 0;
 return(vm);
}

variable_types: struct vmstate { unsigned int regs[26]; unsigned int flags; struct command { enum instruction { NOTE=0, MOV=1, ADD=2, SUB=3, CMP=4, JNE=5, JE=6, JL=7, JG=8, JMP=9, BRA=10, RET=11, CFL=12, HALT=13, LABEL=14, JNEL=15, JEL=16, JLL=17, JGL=18, JMPL=19, BRAL=20, NOP=21 } cmd; unsigned int data; unsigned int reg; struct command *next; struct command *prev; } *stack[16]; unsigned int sp; struct command *last; } *

user_defined_types: struct vmstate { unsigned int regs[26]; unsigned int flags; struct command *stack[16]; unsigned int sp; struct command *last; }


decompiled_code:
__int64 __fastcall read1arg(int *a1, int *a2, int a3, __int64 a4)
{
int v4; // eax
int v5; // er8
_BYTE *v7; // rdi
int v8; // er13
int v9; // eax
__int64 result; // rax
char nptr[51]; // [rsp+5h] [rbp-33h] BYREF

strcpy(nptr, "00000000");
v4 = *a1;
nptr[10] = 0;
v5 = v4 + 1;
if ( v4 + 1 >= a3 )
return 1LL;
*a1 = v5;
if ( v4 + 2 >= a3 )
return 0LL;
v7 = (_BYTE *)(a4 + v5);
if ( (unsigned __int8)(*v7 - 97) > 0x19u )
{
if ( (unsigned __int8)(*v7 - 48) <= 9u )
{
v8 = readnum(v7, (unsigned int)(a3 - v5), nptr, 8LL, 94LL);
v9 = atoi(nptr) << 8;
LOBYTE(v9) = -1;
*a2 = v9;
*a1 += v8;
return 0LL;
}
return 1LL;
}
result = 0LL;
*a2 = (char)(*v7 - 97);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int *a1, int *a2, int a3, __int64 a4)
{
int v4; // eax
int v5; // er8
_BYTE *v7; // rdi
int v8; // er13
int v9; // eax
__int64 result; // rax
char nptr[51]; // [rsp+5h] [rbp-33h] BYREF

strcpy(nptr, "00000000");
v4 = *a1;
nptr[10] = 0;
v5 = v4 + 1;
if ( v4 + 1 >= a3 )
return 1LL;
*a1 = v5;
if ( v4 + 2 >= a3 )
return 0LL;
v7 = (_BYTE *)(a4 + v5);
if ( (unsigned __int8)(*v7 - 97) > 0x19u )
{
if ( (unsigned __int8)(*v7 - 48) <= 9u )
{
v8 = readnum(v7, (unsigned int)(a3 - v5), nptr, 8LL, 94LL);
v9 = atoi(nptr) << 8;
LOBYTE(v9) = -1;
*a2 = v9;
*a1 += v8;
return 0LL;
}
return 1LL;
}
result = 0LL;
*a2 = (char)(*v7 - 97);
return result;
}

original_code:
int read1arg(int *pos, int *data, int length, char *songstr) {
 int t;
 int t2;
 char tmpstr[11] = "00000000";
 if(*pos + 1 < length) {
  ++*pos;
  if(*pos + 1 < length) {
   if(songstr[*pos] >= 'a' && songstr[*pos] <= 'z' ) {
    *data = (songstr[*pos] - 'a');
   } else if(songstr[*pos] >= '0' && songstr[*pos] <= '9') {
    t2 = readnum(&(songstr[*pos]), length - *pos, tmpstr, 8, '^');
    t = atoi(tmpstr);
    *data = (t << 8 | 0xFF);
    *pos += t2;
   } else {
    return(1);
   }
  }
 } else {
  return(1);
 }
 return(0);
}

variable_types:
pos
int *

variable_types:
data
int *

variable_types: length int

variable_types:
songstr
char *

variable_types: t int

variable_types:
tmpstr
char [11]


decompiled_code:
__int64 __fastcall read2arg(int *a1, int *a2, _DWORD *a3, int a4, __int64 a5)
{
int v5; // eax
int v6; // eax
unsigned __int8 v8; // al
unsigned int v9; // er12
int v10; // edx
int v11; // eax
_BYTE *v12; // rdi
int v13; // er13
int v14; // eax
__int64 result; // rax
char nptr[51]; // [rsp+5h] [rbp-33h] BYREF

strcpy(nptr, "00000000");
v5 = *a1;
nptr[10] = 0;
v6 = v5 + 1;
if ( v6 >= a4 )
return 1;
*a1 = v6;
v8 = *(_BYTE *)(a5 + v6) - 97;
if ( v8 > 0x19u )
return 1;
v9 = 0;
*a3 = (char)v8;
v10 = *a1 + 1;
v11 = *a1 + 2;
*a1 = v10;
if ( v11 >= a4 )
return v9;
v12 = (_BYTE *)(a5 + v10);
if ( (unsigned __int8)(*v12 - 97) > 0x19u )
{
if ( (unsigned __int8)(*v12 - 48) <= 9u )
{
v13 = readnum(v12, (unsigned int)(a4 - v10), nptr, 8LL, 94LL);
v14 = atoi(nptr) << 8;
LOBYTE(v14) = -1;
*a2 = v14;
*a1 += v13;
return v9;
}
return 1;
}
result = 0LL;
*a2 = (char)(*v12 - 97);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int *a1, int *a2, _DWORD *a3, int a4, __int64 a5)
{
int v5; // eax
int v6; // eax
unsigned __int8 v8; // al
unsigned int v9; // er12
int v10; // edx
int v11; // eax
_BYTE *v12; // rdi
int v13; // er13
int v14; // eax
__int64 result; // rax
char nptr[51]; // [rsp+5h] [rbp-33h] BYREF

strcpy(nptr, "00000000");
v5 = *a1;
nptr[10] = 0;
v6 = v5 + 1;
if ( v6 >= a4 )
return 1;
*a1 = v6;
v8 = *(_BYTE *)(a5 + v6) - 97;
if ( v8 > 0x19u )
return 1;
v9 = 0;
*a3 = (char)v8;
v10 = *a1 + 1;
v11 = *a1 + 2;
*a1 = v10;
if ( v11 >= a4 )
return v9;
v12 = (_BYTE *)(a5 + v10);
if ( (unsigned __int8)(*v12 - 97) > 0x19u )
{
if ( (unsigned __int8)(*v12 - 48) <= 9u )
{
v13 = readnum(v12, (unsigned int)(a4 - v10), nptr, 8LL, 94LL);
v14 = atoi(nptr) << 8;
LOBYTE(v14) = -1;
*a2 = v14;
*a1 += v13;
return v9;
}
return 1;
}
result = 0LL;
*a2 = (char)(*v12 - 97);
return result;
}

original_code:
int read2arg(int *pos, int *data, int *reg, int length, char *songstr) {
 int t;
 int t2;
 char tmpstr[11] = "00000000";
 if(*pos + 1 < length) {
  ++*pos;
  if(songstr[*pos] >= 'a' && songstr[*pos] <= 'z') {
   *reg = (songstr[*pos] - 'a');
   ++*pos;
  } else {
   return(1);
  }
  if(*pos + 1 < length) {
   if(songstr[*pos] >= 'a' && songstr[*pos] <= 'z' ) {
    *data = (songstr[*pos] - 'a');
   } else if(songstr[*pos] >= '0' && songstr[*pos] <= '9') {
    t2 = readnum(&(songstr[*pos]), length - *pos, tmpstr, 8, '^');
    t = atoi(tmpstr);
    *data = (t << 8 | 0xFF);
    *pos += t2;
   } else {
    return(1);
   }
  }
 } else {
  return(1);
 }
 return(0);
}

variable_types:
reg
int *


islabel

decompiled_code:
_BOOL8 __fastcall islabel(__int64 a1, int *a2)
{
int v2; // eax

v2 = *a2 + 1;
*a2 = v2;
return *(_BYTE *)(a1 + v2) == 58;
}

original_code:
int islabel(char *songstr, int *pos) {
 ++*pos;
 if(songstr[*pos] == ':')
  return(1);
 return(0);
}