decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
const char **v4; // rbx
__int64 v5; // rcx
__int64 v6; // rbp
__int64 v7; // r8
__int64 v8; // r9
__int64 v9; // rdi
unsigned int *v10; // rax
unsigned int *v11; // r12
int v12; // edx
int result; // eax

v3 = prepare_arguments(argc, argv, envp);
if ( !v3 )
{
fwrite("Unable to prepare arguments\n", 1uLL, 0x1CuLL, stderr);
print_usage(*argv);
return 1;
}
v4 = (const char **)v3;
v6 = prepare_image(v3);
if ( !v6 )
{
fwrite("Unable to prepare image\n", 1uLL, 0x18uLL, stderr);
LABEL_17:
free_arguments(v4);
return 1;
}
v9 = *(unsigned int *)v4;
if ( (_DWORD)v9 != -1 && !(unsigned int)is_valid_hash_method(v9) )
{
fwrite("Unsupported hash method\n", 1uLL, 0x18uLL, stderr);
LABEL_16:
free_bitmap_image(v6);
goto LABEL_17;
}
if ( *((_DWORD *)v4 + 1) != -1 && !(unsigned int)is_valid_cipher_method() )
{
fwrite("Unsupported cipher method\n", 1uLL, 0x1AuLL, stderr);
goto LABEL_16;
}
v10 = reveal_message_from_image(v6, *((_DWORD *)v4 + 1), *(unsigned int *)v4, v5, v7, v8);
v11 = v10;
if ( !v10 )
{
fwrite("Unable to reveal hidden message from image\n", 1uLL, 0x2BuLL, stderr);
goto LABEL_16;
}
v12 = write_back_message(v10, v4[4], *((_DWORD *)v4 + 2));
result = 0;
if ( !v12 )
{
fwrite("Unable to write back the message to the file\n", 1uLL, 0x2DuLL, stderr);
free(v11);
goto LABEL_16;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
const char **v4; // rbx
__int64 v5; // rcx
__int64 v6; // rbp
__int64 v7; // r8
__int64 v8; // r9
__int64 v9; // rdi
unsigned int *v10; // rax
unsigned int *v11; // r12
int v12; // edx
int result; // eax

v3 = prepare_arguments(argc, argv, envp);
if ( !v3 )
{
fwrite("Unable to prepare arguments\n", 1uLL, 0x1CuLL, stderr);
func0(*argv);
return 1;
}
v4 = (const char **)v3;
v6 = prepare_image(v3);
if ( !v6 )
{
fwrite("Unable to prepare image\n", 1uLL, 0x18uLL, stderr);
LABEL_17:
free_arguments(v4);
return 1;
}
v9 = *(unsigned int *)v4;
if ( (_DWORD)v9 != -1 && !(unsigned int)is_valid_hash_method(v9) )
{
fwrite("Unsupported hash method\n", 1uLL, 0x18uLL, stderr);
LABEL_16:
free_bitmap_image(v6);
goto LABEL_17;
}
if ( *((_DWORD *)v4 + 1) != -1 && !(unsigned int)is_valid_cipher_method() )
{
fwrite("Unsupported cipher method\n", 1uLL, 0x1AuLL, stderr);
goto LABEL_16;
}
v10 = func3(v6, *((_DWORD *)v4 + 1), *(unsigned int *)v4, v5, v7, v8);
v11 = v10;
if ( !v10 )
{
fwrite("Unable to reveal hidden message from image\n", 1uLL, 0x2BuLL, stderr);
goto LABEL_16;
}
v12 = func4(v10, v4[4], *((_DWORD *)v4 + 2));
result = 0;
if ( !v12 )
{
fwrite("Unable to write back the message to the file\n", 1uLL, 0x2DuLL, stderr);
free(v11);
goto LABEL_16;
}
return result;
}

original_code:
int main(int argc, char *argv[]) {
 int ret = 0;
 struct arguments_ *args = ((void *)0);
 struct bitmap_image_ *image = ((void *)0);
 struct wrapped_message_ *message = ((void *)0);
 if ((args = prepare_arguments(argc, argv)) == ((void *)0)) {
  fprintf(stderr, "Unable to prepare arguments\n");
  print_usage(argv[0]);
  ret = 1;
  goto out;
 }
 if ((image = prepare_image(args)) == ((void *)0)) {
  fprintf(stderr, "Unable to prepare image\n");
  ret = 1;
  goto out;
 }
 if (args->hash_id != (0x7fffffff * 2U + 1U) && is_valid_hash_method(args->hash_id) == 0) {
  fprintf(stderr, "Unsupported hash method\n");
  ret = 1;
  goto out;
 }
 if (args->scatter_id != (0x7fffffff * 2U + 1U) && is_valid_cipher_method(args->scatter_id) == 0) {
  fprintf(stderr, "Unsupported cipher method\n");
  ret = 1;
  goto out;
 }
 if ((message = reveal_message_from_image(image, args->scatter_id, args->hash_id)) == ((void *)0)) {
  fprintf(stderr, "Unable to reveal hidden message from image\n");
  ret = 1;
  goto out;
 }
 if (write_back_message(message, args->message, args->msg_from_file) == 0) {
  fprintf(stderr, "Unable to write back the message to the file\n");
  ret = 1;
  goto out;
 }
 return 0;
out:
 if (message != ((void *)0))
  free(message);
 if (image != ((void *)0))
  free_bitmap_image(image);
 if (args != ((void *)0))
  free_arguments(args);
 return ret;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct arguments_ { int hash_id; int scatter_id; int msg_from_file; int just_zeroize; char *suffix; char *image; char *message; } *


repo: dodgerblue/waldo

decompiled_code:
char *__fastcall get_message_to_hide(const char *a1, int a2)
{
int v2; // eax
int v3; // ebp
__off_t v4; // rax
size_t v5; // r12
void *v6; // rax
void *v7; // rbx
ssize_t v8; // rax

if ( !a2 )
return strdup(a1);
v2 = open(a1, 0);
v3 = v2;
if ( v2 == -1 )
{
fwrite("Unable to open message file\n", 1uLL, 0x1CuLL, stderr);
return 0LL;
}
v4 = lseek(v2, 0LL, 2);
v5 = v4;
if ( v4 == -1 )
{
fwrite("Unable to seek to end of message file\n", 1uLL, 0x26uLL, stderr);
LABEL_20:
close(v3);
return 0LL;
}
if ( debug )
printf("File size: %ld\n", v4);
if ( lseek(v3, 0LL, 0) == -1 )
{
fwrite("Unable to seek to start of message file\n", 1uLL, 0x28uLL, stderr);
goto LABEL_20;
}
v6 = malloc(v5);
v7 = v6;
if ( !v6 )
{
fwrite("Unable to alloc memory to the message\n", 1uLL, 0x26uLL, stderr);
goto LABEL_20;
}
v8 = read(v3, v6, v5);
if ( v8 == -1 )
{
fwrite("Unable to read the message from the message file\n", 1uLL, 0x31uLL, stderr);
LABEL_19:
free(v7);
goto LABEL_20;
}
if ( v5 != v8 )
{
fwrite("Did not read enough bytes from the message file\n", 1uLL, 0x30uLL, stderr);
goto LABEL_19;
}
close(v3);
return (char *)v7;
}

canonical_decompiled_code:
char *__fastcall func1(const char *a1, int a2)
{
int v2; // eax
int v3; // ebp
__off_t v4; // rax
size_t v5; // r12
void *v6; // rax
void *v7; // rbx
ssize_t v8; // rax

if ( !a2 )
return strdup(a1);
v2 = open(a1, 0);
v3 = v2;
if ( v2 == -1 )
{
fwrite("Unable to open message file\n", 1uLL, 0x1CuLL, stderr);
return 0LL;
}
v4 = lseek(v2, 0LL, 2);
v5 = v4;
if ( v4 == -1 )
{
fwrite("Unable to seek to end of message file\n", 1uLL, 0x26uLL, stderr);
LABEL_20:
close(v3);
return 0LL;
}
if ( debug )
printf("File size: %ld\n", v4);
if ( lseek(v3, 0LL, 0) == -1 )
{
fwrite("Unable to seek to start of message file\n", 1uLL, 0x28uLL, stderr);
goto LABEL_20;
}
v6 = malloc(v5);
v7 = v6;
if ( !v6 )
{
fwrite("Unable to alloc memory to the message\n", 1uLL, 0x26uLL, stderr);
goto LABEL_20;
}
v8 = read(v3, v6, v5);
if ( v8 == -1 )
{
fwrite("Unable to read the message from the message file\n", 1uLL, 0x31uLL, stderr);
LABEL_19:
free(v7);
goto LABEL_20;
}
if ( v5 != v8 )
{
fwrite("Did not read enough bytes from the message file\n", 1uLL, 0x30uLL, stderr);
goto LABEL_19;
}
close(v3);
return (char *)v7;
}

original_code:
char* get_message_to_hide(char *message, int is_file) {
 char *result = ((void *)0);
 off_t start, stop;
 int fd;
 ssize_t read_bytes;
 if (is_file == 0) {
  result = strdup(message);
  return result;
 }
 fd = open(message, 00);
 if (fd == -1) {
  fprintf(stderr, "Unable to open message file\n");
  goto out_fail;
 }
 stop = lseek(fd, 0, 2);
 if (stop == -1) {
  fprintf(stderr, "Unable to seek to end of message file\n");
  goto out_seek_fail;
 }
 if (debug)
  printf("File size: %ld\n", (long int) stop);
 start = lseek(fd, 0, 0);
 if (start == -1) {
  fprintf(stderr, "Unable to seek to start of message file\n");
  goto out_seek_fail;
 }
 result = malloc(stop);
 if (result == ((void *)0)) {
  fprintf(stderr, "Unable to alloc memory to the message\n");
  goto out_seek_fail;
 }
 read_bytes = read(fd, result, (size_t) stop);
 if (read_bytes == -1) {
  fprintf(stderr, "Unable to read the message from the message file\n");
  goto out_read_fail;
 }
 if (read_bytes != (ssize_t) stop) {
  fprintf(stderr, "Did not read enough bytes from the message file\n");
  goto out_read_fail;
 }
 close(fd);
 return result;
out_read_fail:
 free(result);
out_seek_fail:
 close(fd);
out_fail:
 return ((void *)0);
}

canonical_original_code:
char *get_message_to_hide(char *message, int is_file) {
 char *result = ((void *)0);
 long start; long stop;
 int fd;
 long read_bytes;
 if (is_file == 0) {
  result = strdup(message);
  return result;
 }
 fd = open(message, 00);
 if (fd == -1) {
  fprintf(stderr, "Unable to open message file\n");
  goto out_fail;
 }
 stop = lseek(fd, 0, 2);
 if (stop == -1) {
  fprintf(stderr, "Unable to seek to end of message file\n");
  goto out_seek_fail;
 }
 if (debug)
  printf("File size: %ld\n", (long) stop);
 start = lseek(fd, 0, 0);
 if (start == -1) {
  fprintf(stderr, "Unable to seek to start of message file\n");
  goto out_seek_fail;
 }
 result = malloc(stop);
 if (result == ((void *)0)) {
  fprintf(stderr, "Unable to alloc memory to the message\n");
  goto out_seek_fail;
 }
 read_bytes = read(fd, result, (unsigned long) stop);
 if (read_bytes == -1) {
  fprintf(stderr, "Unable to read the message from the message file\n");
  goto out_read_fail;
 }
 if (read_bytes != (long) stop) {
  fprintf(stderr, "Did not read enough bytes from the message file\n");
  goto out_read_fail;
 }
 close(fd);
 return result;
out_read_fail:
 free(result);
out_seek_fail:
 close(fd);
out_fail:
 return ((void *)0);
}

variable_types:
message
char *

variable_types:
result
char *

variable_types: start long

variable_types: stop long

variable_types: fd int

return_type: char *


decompiled_code:
char *__fastcall prepare_message(__int64 a1)
{
char *v1; // rax
char *v2; // rbx

v1 = get_message_to_hide(*(const char **)(a1 + 32), *(_DWORD *)(a1 + 8));
v2 = v1;
if ( v1 )
{
if ( debug )
printf("Message to hide: %s\n", v1);
}
else
{
fwrite("Unable to read message to hide\n", 1uLL, 0x1FuLL, stderr);
}
return v2;
}

canonical_decompiled_code:
char *__fastcall func2(__int64 a1)
{
char *v1; // rax
char *v2; // rbx

v1 = func1(*(const char **)(a1 + 32), *(_DWORD *)(a1 + 8));
v2 = v1;
if ( v1 )
{
if ( debug )
printf("Message to hide: %s\n", v1);
}
else
{
fwrite("Unable to read message to hide\n", 1uLL, 0x1FuLL, stderr);
}
return v2;
}

original_code:
char *prepare_message(struct arguments_ *args) {
 char *message = ((void *)0);
 message = get_message_to_hide(args->message, args->msg_from_file);
 if (message == ((void *)0)) {
  fprintf(stderr, "Unable to read message to hide\n");
  return ((void *)0);
 }
 if (debug)
  printf("Message to hide: %s\n", message);
 return message;
}

user_defined_types: struct arguments_ { int hash_id; int scatter_id; int msg_from_file; int just_zeroize; char *suffix; char *image; char *message; }


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

v2 = wrap_message(a2);
v3 = v2;
if ( v2 )
{
if ( debug )
print_wrapped_message(v2);
}
else
{
fwrite("Unable to wrap message\n", 1uLL, 0x17uLL, stderr);
}
return v3;
}

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

v2 = wrap_message(a2);
v3 = v2;
if ( v2 )
{
if ( debug )
print_wrapped_message(v2);
}
else
{
fwrite("Unable to wrap message\n", 1uLL, 0x17uLL, stderr);
}
return v3;
}

original_code:
char *prepare_wrapped_message(int hash_type, char *message) {
 char *msg = ((void *)0);
 msg = wrap_message(hash_methods[hash_type], message);
 if (msg == ((void *)0)) {
  fprintf(stderr, "Unable to wrap message\n");
  return ((void *)0);
 }
 if (debug)
  print_wrapped_message((struct wrapped_message_*) msg);
 return msg;
}

canonical_original_code:
char *prepare_wrapped_message(int hash_type, char *message) {
 char *msg = ((void *)0);
 msg = wrap_message(hash_methods[hash_type], message);
 if (msg == ((void *)0)) {
  fprintf(stderr, "Unable to wrap message\n");
  return ((void *)0);
 }
 if (debug)
  print_wrapped_message((struct wrapped_message_ *) msg);
 return msg;
}

variable_types:
msg
char *

user_defined_types: struct wrapped_message_ { unsigned int hash_id; unsigned int msg_length; char buffer[]; }


decompiled_code:
__int64 __fastcall write_back_image(__int64 a1, const char *a2, const char *a3)
{
int v5; // eax
int v6; // edi
char *v7; // rax
char *v8; // rbx
unsigned int v9; // ebp
__int64 result; // rax

v5 = strlen(a2);
if ( a3 )
{
v6 = strlen(a3) + v5;
}
else
{
v6 = v5 + 8;
a3 = "_new.bmp";
}
v7 = (char *)malloc(v6);
v8 = v7;
if ( v7 )
{
sprintf(v7, "%s%s", a2, a3);
v9 = open(v8, 578, 438LL);
if ( v9 == -1 )
{
fwrite("Unable to open new image file for writing\n", 1uLL, 0x2AuLL, stderr);
}
else
{
free(v8);
if ( (unsigned int)write_bitmap_image(a1, v9) != -1 )
{
close(v9);
return 0LL;
}
fwrite("Unable to write back image\n", 1uLL, 0x1BuLL, stderr);
close(v9);
}
free(v8);
result = 1LL;
}
else
{
fwrite("Unable to allocate memory for write back image filename\n", 1uLL, 0x38uLL, stderr);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, const char *a2, const char *a3)
{
int v5; // eax
int v6; // edi
char *v7; // rax
char *v8; // rbx
unsigned int v9; // ebp
__int64 result; // rax

v5 = strlen(a2);
if ( a3 )
{
v6 = strlen(a3) + v5;
}
else
{
v6 = v5 + 8;
a3 = "_new.bmp";
}
v7 = (char *)malloc(v6);
v8 = v7;
if ( v7 )
{
sprintf(v7, "%s%s", a2, a3);
v9 = open(v8, 578, 438LL);
if ( v9 == -1 )
{
fwrite("Unable to open new image file for writing\n", 1uLL, 0x2AuLL, stderr);
}
else
{
free(v8);
if ( (unsigned int)write_bitmap_image(a1, v9) != -1 )
{
close(v9);
return 0LL;
}
fwrite("Unable to write back image\n", 1uLL, 0x1BuLL, stderr);
close(v9);
}
free(v8);
result = 1LL;
}
else
{
fwrite("Unable to allocate memory for write back image filename\n", 1uLL, 0x38uLL, stderr);
result = 1LL;
}
return result;
}

original_code:
int write_back_image(struct bitmap_image_ *image, char *filename, char *suffix) {
 char *new_filename = ((void *)0);
 int length = strlen(filename);
 int fd;
 if (suffix != ((void *)0))
  length += strlen(suffix);
 else {
  length += strlen("_new.bmp");
  suffix = "_new.bmp";
 }
 new_filename = malloc(length);
 if (new_filename == ((void *)0)) {
  fprintf(stderr, "Unable to allocate memory for write back image filename\n");
  goto out_fail;
 }
 sprintf(new_filename, "%s%s", filename, suffix);
 fd = open(new_filename, 02 | 0100 | 01000, 0666);
 if (fd == -1) {
  fprintf(stderr, "Unable to open new image file for writing\n");
  goto out_fail_open;
 }
 free(new_filename);
 if (write_bitmap_image(image, fd) == -1) {
  fprintf(stderr, "Unable to write back image\n");
  goto out_fail_write;
 }
 close(fd);
 return 0;
out_fail_write:
 close(fd);
out_fail_open:
 free(new_filename);
out_fail:
 return 1;
}

variable_types: struct bitmap_image_ { struct bitmap_file_header_ { union { char buffer[14]; struct { char header_field[2]; char size[4]; char reserved[4]; char offset[4]; } fields; }; } *bfh; struct bitmap_info_header_ { char size[4]; union { char buffer[36]; struct { char width[4]; char height[4]; char planes[2]; char depth[2]; char compression[4]; char data_size[4]; char horizontal_rule[4]; char vertical_resolution[4]; char colors[4]; char vip_colors[4]; } fields; }; } *bih; long data_size; char *data; long trailer_size; char *trailer; } *

variable_types:
filename
char *

variable_types:
suffix
char *

variable_types: length int

return_type: int

user_defined_types: struct bitmap_image_ { struct bitmap_file_header_ *bfh; struct bitmap_info_header_ *bih; long data_size; char *data; long trailer_size; char *trailer; }


decompiled_code:
int __fastcall print_usage(const char *a1)
{
__int64 v1; // rdx
unsigned __int64 v2; // rbx
__int64 v3; // rdx
const char **v4; // rbx

fprintf(
stderr,
"Usage: %s [OPTIONS] <image> <message>\n"
"\n"
" \timage - the image file used to hide the message\n"
" \tmessage - the message to hide, as a string\n"
" \t\n"
" \tOPTIONS can be one or more of the following: \n"
" \t-h <hash method> - the method using for hashing the message\n"
" \t-s <scatter method> - the method used for hiding the message\n"
" \t-f - the message to hide will be read from a file, provided as the 'message' parameter\n"
" \t-z - just zeroize the bits in the new image, w/o adding any info\n"
" \t-r <suffix> - add custom suffix to the new image (default is '_new.bmp')\n"
" \t\n",
a1);
fwrite("\tAvailable hash methods:\n", 1uLL, 0x19uLL, stderr);
v1 = hash_methods;
if ( hash_methods != -1 )
{
v2 = (unsigned int)((_DWORD)&hash_methods + 28);
do
{
fprintf(stderr, "\t%u - %s\n", v1, (const char *)v2);
v2 += 48LL;
v1 = *(unsigned int *)(v2 - 28);
}
while ( (_DWORD)v1 != -1 );
}
fputc(10, stderr);
fwrite("\tAvailable scatter methods:\n", 1uLL, 0x1CuLL, stderr);
v3 = cipher_methods;
if ( cipher_methods != -1 )
{
v4 = (const char **)(unsigned int)((_DWORD)&cipher_methods + 8);
do
{
fprintf(stderr, "\t%u - %s - %s\n", v3, *v4, v4[1]);
v4 += 4;
v3 = *((unsigned int *)v4 - 2);
}
while ( (_DWORD)v3 != -1 );
}
return fputc(10, stderr);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
__int64 v1; // rdx
unsigned __int64 v2; // rbx
__int64 v3; // rdx
const char **v4; // rbx

fprintf(
stderr,
"Usage: %s [OPTIONS] <image> <message>\n"
"\n"
" \timage - the image file used to hide the message\n"
" \tmessage - the message to hide, as a string\n"
" \t\n"
" \tOPTIONS can be one or more of the following: \n"
" \t-h <hash method> - the method using for hashing the message\n"
" \t-s <scatter method> - the method used for hiding the message\n"
" \t-f - the message to hide will be read from a file, provided as the 'message' parameter\n"
" \t-z - just zeroize the bits in the new image, w/o adding any info\n"
" \t-r <suffix> - add custom suffix to the new image (default is '_new.bmp')\n"
" \t\n",
a1);
fwrite("\tAvailable hash methods:\n", 1uLL, 0x19uLL, stderr);
v1 = hash_methods;
if ( hash_methods != -1 )
{
v2 = (unsigned int)((_DWORD)&hash_methods + 28);
do
{
fprintf(stderr, "\t%u - %s\n", v1, (const char *)v2);
v2 += 48LL;
v1 = *(unsigned int *)(v2 - 28);
}
while ( (_DWORD)v1 != -1 );
}
fputc(10, stderr);
fwrite("\tAvailable scatter methods:\n", 1uLL, 0x1CuLL, stderr);
v3 = cipher_methods;
if ( cipher_methods != -1 )
{
v4 = (const char **)(unsigned int)((_DWORD)&cipher_methods + 8);
do
{
fprintf(stderr, "\t%u - %s - %s\n", v3, *v4, v4[1]);
v4 += 4;
v3 = *((unsigned int *)v4 - 2);
}
while ( (_DWORD)v3 != -1 );
}
return fputc(10, stderr);
}

original_code:
void print_usage(char *program_name) {
 int i;
 fprintf(stderr, "Usage: %s [OPTIONS] <image> <message>\n\n 	image - the image file used to hide the message\n 	message - the message to hide, as a string\n 	\n 	OPTIONS can be one or more of the following: \n 	-h <hash method> - the method using for hashing the message\n 	-s <scatter method> - the method used for hiding the message\n 	-f - the message to hide will be read from a file, provided as the 'message' parameter\n 	-z - just zeroize the bits in the new image, w/o adding any info\n 	-r <suffix> - add custom suffix to the new image (default is '_new.bmp')\n 	\n",
 program_name);
 fprintf(stderr, "\tAvailable hash methods:\n");
 for (i = 0; hash_methods[i].id != (0x7fffffff * 2U + 1U); i ++) {
  fprintf(stderr, "\t%u - %s\n", hash_methods[i].id, hash_methods[i].name);
 }
 fprintf(stderr, "\n");
 fprintf(stderr, "\tAvailable scatter methods:\n");
 for (i = 0; cipher_methods[i].id != (0x7fffffff * 2U + 1U); i ++) {
  fprintf(stderr, "\t%u - %s - %s\n", cipher_methods[i].id, cipher_methods[i].codename, cipher_methods[i].description);
 }
 fprintf(stderr, "\n");
}

variable_types: i int

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rax
const char **v4; // rbx
char *v5; // rbp
__int64 v6; // r12
__int64 v7; // r13
int v8; // er13

v3 = (const char **)prepare_arguments(argc, argv, envp);
v4 = v3;
if ( !v3 )
{
fwrite("Unable to prepare arguments\n", 1uLL, 0x1CuLL, stderr);
print_usage(*argv);
LABEL_18:
v8 = 1;
goto LABEL_29;
}
if ( *(_DWORD *)v3 == -1 )
*(_DWORD *)v3 = 0;
if ( *((_DWORD *)v3 + 1) == -1 )
*((_DWORD *)v3 + 1) = 0;
v5 = prepare_message((__int64)v3);
if ( !v5 )
{
fwrite("Unable to prepare message\n", 1uLL, 0x1AuLL, stderr);
goto LABEL_18;
}
v6 = prepare_image(v4);
if ( v6 )
{
if ( !*(_DWORD *)v4 || (unsigned int)is_valid_hash_method() )
{
if ( *((_DWORD *)v4 + 1) && !(unsigned int)is_valid_cipher_method() )
{
fwrite("Unsupported cipher method\n", 1uLL, 0x1AuLL, stderr);
v8 = 1;
}
else
{
v7 = prepare_wrapped_message(*(unsigned int *)v4, (__int64)v5);
if ( v7 )
{
if ( (unsigned int)message_fits(v6, v7) )
{
if ( *((_DWORD *)v4 + 3) )
{
v8 = zeroize_image(v6);
if ( v8 == 1 )
goto LABEL_27;
}
else
{
v8 = hide_message_in_image(v6, v7);
if ( v8 == 1 )
goto LABEL_27;
}
v8 = write_back_image(v6, v4[3], v4[2]);
goto LABEL_27;
}
fwrite("The provided image is too small to fit the message\n", 1uLL, 0x33uLL, stderr);
v8 = 1;
}
else
{
fwrite("Unable to prepare the wrapped message\n", 1uLL, 0x26uLL, stderr);
v8 = 1;
}
}
}
else
{
fwrite("Unsupported hash method\n", 1uLL, 0x18uLL, stderr);
v8 = 1;
}
LABEL_27:
free_bitmap_image(v6);
goto LABEL_28;
}
fwrite("Unable to prepare image\n", 1uLL, 0x18uLL, stderr);
v8 = 1;
LABEL_28:
free(v5);
LABEL_29:
if ( v4 )
free_arguments(v4);
return v8;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rax
const char **v4; // rbx
char *v5; // rbp
__int64 v6; // r12
__int64 v7; // r13
int v8; // er13

v3 = (const char **)prepare_arguments(argc, argv, envp);
v4 = v3;
if ( !v3 )
{
fwrite("Unable to prepare arguments\n", 1uLL, 0x1CuLL, stderr);
func0(*argv);
LABEL_18:
v8 = 1;
goto LABEL_29;
}
if ( *(_DWORD *)v3 == -1 )
*(_DWORD *)v3 = 0;
if ( *((_DWORD *)v3 + 1) == -1 )
*((_DWORD *)v3 + 1) = 0;
v5 = func2((__int64)v3);
if ( !v5 )
{
fwrite("Unable to prepare message\n", 1uLL, 0x1AuLL, stderr);
goto LABEL_18;
}
v6 = prepare_image(v4);
if ( v6 )
{
if ( !*(_DWORD *)v4 || (unsigned int)is_valid_hash_method() )
{
if ( *((_DWORD *)v4 + 1) && !(unsigned int)is_valid_cipher_method() )
{
fwrite("Unsupported cipher method\n", 1uLL, 0x1AuLL, stderr);
v8 = 1;
}
else
{
v7 = func3(*(unsigned int *)v4, (__int64)v5);
if ( v7 )
{
if ( (unsigned int)message_fits(v6, v7) )
{
if ( *((_DWORD *)v4 + 3) )
{
v8 = zeroize_image(v6);
if ( v8 == 1 )
goto LABEL_27;
}
else
{
v8 = hide_message_in_image(v6, v7);
if ( v8 == 1 )
goto LABEL_27;
}
v8 = func4(v6, v4[3], v4[2]);
goto LABEL_27;
}
fwrite("The provided image is too small to fit the message\n", 1uLL, 0x33uLL, stderr);
v8 = 1;
}
else
{
fwrite("Unable to prepare the wrapped message\n", 1uLL, 0x26uLL, stderr);
v8 = 1;
}
}
}
else
{
fwrite("Unsupported hash method\n", 1uLL, 0x18uLL, stderr);
v8 = 1;
}
LABEL_27:
free_bitmap_image(v6);
goto LABEL_28;
}
fwrite("Unable to prepare image\n", 1uLL, 0x18uLL, stderr);
v8 = 1;
LABEL_28:
free(v5);
LABEL_29:
if ( v4 )
free_arguments(v4);
return v8;
}

original_code:
int main(int argc, char *argv[]) {
 struct arguments_ *args = ((void *)0);
 char *message = ((void *)0);
 struct bitmap_image_ *image = ((void *)0);
 char *wrapped_message = ((void *)0);
 int ret = 0;
 if ((args = prepare_arguments(argc, argv)) == ((void *)0)) {
  fprintf(stderr, "Unable to prepare arguments\n");
  print_usage(argv[0]);
  ret = 1;
  goto out;
 }
 if (args->hash_id == (0x7fffffff * 2U + 1U)) args->hash_id = 0;
 if (args->scatter_id == (0x7fffffff * 2U + 1U)) args->scatter_id = 0;
 if ((message = prepare_message(args)) == ((void *)0)) {
  fprintf(stderr, "Unable to prepare message\n");
  ret = 1;
  goto out;
 }
 if ((image = prepare_image(args)) == ((void *)0)) {
  fprintf(stderr, "Unable to prepare image\n");
  ret = 1;
  goto out;
 }
 if (args->hash_id != 0 && is_valid_hash_method(args->hash_id) == 0) {
  fprintf(stderr, "Unsupported hash method\n");
  ret = 1;
  goto out;
 }
 if (args->scatter_id != 0 && is_valid_cipher_method(args->scatter_id) == 0) {
  fprintf(stderr, "Unsupported cipher method\n");
  ret = 1;
  goto out;
 }
 if ((wrapped_message = prepare_wrapped_message(args->hash_id, message)) == ((void *)0)) {
  fprintf(stderr, "Unable to prepare the wrapped message\n");
  ret = 1;
  goto out;
 }
 if (message_fits(image, (struct wrapped_message_ *) wrapped_message, cipher_methods[args->scatter_id]) == 0) {
  fprintf(stderr, "The provided image is too small to fit the message\n");
  ret = 1;
  goto out;
 }
 if (args->just_zeroize) {
  ret = zeroize_image(image, cipher_methods[args->scatter_id]);
  if (ret == 1) goto out;
  goto write_image;
 }
 ret = hide_message_in_image(image, (struct wrapped_message_ *) wrapped_message, cipher_methods[args->scatter_id]);
 if (ret == 1) goto out;
write_image:
 ret = write_back_image(image, args->image, args->suffix);
out:
 if (image != ((void *)0))
  free_bitmap_image(image);
 if (message != ((void *)0))
  free(message);
 if (args != ((void *)0))
  free_arguments(args);
 return ret;
}

variable_types: ret int

user_defined_types: struct bitmap_file_header_ { union { char buffer[14]; struct { char header_field[2]; char size[4]; char reserved[4]; char offset[4]; } fields; }; }

user_defined_types: struct bitmap_info_header_ { char size[4]; union { char buffer[36]; struct { char width[4]; char height[4]; char planes[2]; char depth[2]; char compression[4]; char data_size[4]; char horizontal_rule[4]; char vertical_resolution[4]; char colors[4]; char vip_colors[4]; } fields; }; }


decompiled_code:
_DWORD *alloc_arguments()
{
_DWORD *v0; // rax
_DWORD *v1; // rbx

v0 = malloc(0x28uLL);
v1 = v0;
if ( v0 )
{
*v0 = -1;
v0[1] = -1;
v0[2] = 0;
v0[3] = 0;
*((_QWORD *)v0 + 2) = 0LL;
*((_QWORD *)v0 + 3) = 0LL;
*((_QWORD *)v0 + 4) = 0LL;
}
else
{
fwrite("Unable to alloc memory for arguments structure\n", 1uLL, 0x2FuLL, stderr);
}
return v1;
}

canonical_decompiled_code:
_DWORD *func0()
{
_DWORD *v0; // rax
_DWORD *v1; // rbx

v0 = malloc(0x28uLL);
v1 = v0;
if ( v0 )
{
*v0 = -1;
v0[1] = -1;
v0[2] = 0;
v0[3] = 0;
*((_QWORD *)v0 + 2) = 0LL;
*((_QWORD *)v0 + 3) = 0LL;
*((_QWORD *)v0 + 4) = 0LL;
}
else
{
fwrite("Unable to alloc memory for arguments structure\n", 1uLL, 0x2FuLL, stderr);
}
return v1;
}

original_code:
struct arguments_* alloc_arguments() {
 struct arguments_ *result = ((void *)0);
 result = malloc (sizeof(struct arguments_));
 if (result == ((void *)0)) {
  fprintf(stderr, "Unable to alloc memory for arguments structure\n");
  return ((void *)0);
 }
 result->hash_id = (0x7fffffff * 2U + 1U);
 result->scatter_id = (0x7fffffff * 2U + 1U);
 result->msg_from_file = 0;
 result->just_zeroize = 0;
 result->suffix = ((void *)0);
 result->image = ((void *)0);
 result->message = ((void *)0);
 return result;
}

canonical_original_code:
struct arguments_ *alloc_arguments() {
 struct arguments_ *result = ((void *)0);
 result = malloc (sizeof(struct arguments_));
 if (result == ((void *)0)) {
  fprintf(stderr, "Unable to alloc memory for arguments structure\n");
  return ((void *)0);
 }
 result->hash_id = (0x7fffffff * 2U + 1U);
 result->scatter_id = (0x7fffffff * 2U + 1U);
 result->msg_from_file = 0;
 result->just_zeroize = 0;
 result->suffix = ((void *)0);
 result->image = ((void *)0);
 result->message = ((void *)0);
 return result;
}

return_type: struct arguments_ { int hash_id; int scatter_id; int msg_from_file; int just_zeroize; char *suffix; char *image; char *message; } *


decompiled_code:
void __fastcall free_arguments(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi

if ( ptr )
{
v2 = (void *)*((_QWORD *)ptr + 2);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 3);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 4);
if ( v4 )
free(v4);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
void *v4; // rdi

if ( ptr )
{
v2 = (void *)*((_QWORD *)ptr + 2);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 3);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 4);
if ( v4 )
free(v4);
free(ptr);
}
}

original_code:
void free_arguments(struct arguments_ *args) {
 if (args == ((void *)0))
  return;
 if (args->suffix != ((void *)0))
  free (args->suffix);
 if (args->image != ((void *)0))
  free (args->image);
 if (args->message != ((void *)0))
  free (args->message);
 free(args);
}


decompiled_code:
int __fastcall print_arguments(const char **a1)
{
const char *v1; // rsi
const char *v2; // rsi

puts("The parsed arguments structure:");
printf("Hash id: %d\n", *(unsigned int *)a1);
printf("Scatter id: %d\n", *((unsigned int *)a1 + 1));
v1 = "yes";
if ( !*((_DWORD *)a1 + 2) )
v1 = "no";
printf("Message from file: %s\n", v1);
v2 = "yes";
if ( !*((_DWORD *)a1 + 3) )
v2 = "no";
printf("Just zeroize: %s\n", v2);
printf("Output file suffix: %s\n", a1[2]);
printf("Image file: %s\n", a1[3]);
printf("Message: %s\n", a1[4]);
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func2(const char **a1)
{
const char *v1; // rsi
const char *v2; // rsi

puts("The parsed arguments structure:");
printf("Hash id: %d\n", *(unsigned int *)a1);
printf("Scatter id: %d\n", *((unsigned int *)a1 + 1));
v1 = "yes";
if ( !*((_DWORD *)a1 + 2) )
v1 = "no";
printf("Message from file: %s\n", v1);
v2 = "yes";
if ( !*((_DWORD *)a1 + 3) )
v2 = "no";
printf("Just zeroize: %s\n", v2);
printf("Output file suffix: %s\n", a1[2]);
printf("Image file: %s\n", a1[3]);
printf("Message: %s\n", a1[4]);
return putchar(10);
}

original_code:
void print_arguments(struct arguments_ *args) {
 printf("The parsed arguments structure:\n");
 printf("Hash id: %d\n", args->hash_id);
 printf("Scatter id: %d\n", args->scatter_id);
 printf("Message from file: %s\n", args->msg_from_file != 0 ? "yes" : "no");
 printf("Just zeroize: %s\n", args->just_zeroize != 0 ? "yes" : "no");
 printf("Output file suffix: %s\n", args->suffix);
 printf("Image file: %s\n", args->image);
 printf("Message: %s\n", args->message);
 printf("\n");
}


decompiled_code:
int __fastcall print_bitmap_file_header(char *a1)
{
return printf(
"Read bitmap file header:\n \tHeader field: %c%c\n \tImage size: %ld\n \tBitmap image offset: %ld\n",
(unsigned int)*a1,
(unsigned int)a1[1],
*(_QWORD *)(a1 + 2),
*(_QWORD *)(a1 + 10));
}

canonical_decompiled_code:
int __fastcall func0(char *a1)
{
return printf(
"Read bitmap file header:\n \tHeader field: %c%c\n \tImage size: %ld\n \tBitmap image offset: %ld\n",
(unsigned int)*a1,
(unsigned int)a1[1],
*(_QWORD *)(a1 + 2),
*(_QWORD *)(a1 + 10));
}

original_code:
void print_bitmap_file_header(struct bitmap_file_header_ *bfh) {
 printf("Read bitmap file header:\n 	Header field: %c%c\n 	Image size: %ld\n 	Bitmap image offset: %ld\n",
 bfh->fields.header_field[0], bfh->fields.header_field[1], *(long int*) bfh->fields.size, *(long int*) bfh->fields.offset);
}

canonical_original_code:
void print_bitmap_file_header(struct bitmap_file_header_ *bfh) {
 printf("Read bitmap file header:\n 	Header field: %c%c\n 	Image size: %ld\n 	Bitmap image offset: %ld\n",
 bfh->fields.header_field[0], bfh->fields.header_field[1], *(long *) bfh->fields.size, *(long *) bfh->fields.offset);
}

variable_types: struct bitmap_file_header_ { union { char buffer[14]; struct { char header_field[2]; char size[4]; char reserved[4]; char offset[4]; } fields; }; } *


decompiled_code:
int __fastcall print_dib_header(__int64 a1)
{
return printf(
"Read DIB header:\n"
" \tHeader size: %ld\n"
" \tImage width: %ld\n"
" \tImage height: %ld\n"
" \tColor planes: %d\n"
" \tColor depth: %d\n"
" \tCompression: %ld\n"
" \tData size: %ld\n"
" \tHorizontal rule: %ld\n"
" \tVertical resolution: %ld\n"
" \tNo. of colors used: %ld\n"
" \tNo. of VIP colors: %ld\n",
*(_QWORD *)a1,
*(_QWORD *)(a1 + 4),
*(_QWORD *)(a1 + 8),
(unsigned int)*(__int16 *)(a1 + 12),
(unsigned int)*(__int16 *)(a1 + 14),
*(_QWORD *)(a1 + 16),
*(_QWORD *)(a1 + 20),
*(_QWORD *)(a1 + 24),
*(_QWORD *)(a1 + 28),
*(_QWORD *)(a1 + 32),
*(_QWORD *)(a1 + 36));
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
return printf(
"Read DIB header:\n"
" \tHeader size: %ld\n"
" \tImage width: %ld\n"
" \tImage height: %ld\n"
" \tColor planes: %d\n"
" \tColor depth: %d\n"
" \tCompression: %ld\n"
" \tData size: %ld\n"
" \tHorizontal rule: %ld\n"
" \tVertical resolution: %ld\n"
" \tNo. of colors used: %ld\n"
" \tNo. of VIP colors: %ld\n",
*(_QWORD *)a1,
*(_QWORD *)(a1 + 4),
*(_QWORD *)(a1 + 8),
(unsigned int)*(__int16 *)(a1 + 12),
(unsigned int)*(__int16 *)(a1 + 14),
*(_QWORD *)(a1 + 16),
*(_QWORD *)(a1 + 20),
*(_QWORD *)(a1 + 24),
*(_QWORD *)(a1 + 28),
*(_QWORD *)(a1 + 32),
*(_QWORD *)(a1 + 36));
}

original_code:
void print_dib_header(struct bitmap_info_header_ *dh) {
 printf("Read DIB header:\n 	Header size: %ld\n 	Image width: %ld\n 	Image height: %ld\n 	Color planes: %d\n 	Color depth: %d\n 	Compression: %ld\n 	Data size: %ld\n 	Horizontal rule: %ld\n 	Vertical resolution: %ld\n 	No. of colors used: %ld\n 	No. of VIP colors: %ld\n",
 *(long int*) dh->size, *(long int*) dh->fields.width, *(long int*) dh->fields.height,
 *(short int*) dh->fields.planes, *(short int*) dh->fields.depth,
 *(long int*) dh->fields.compression, *(long int*) dh->fields.data_size,
 *(long int*) dh->fields.horizontal_rule, *(long int*) dh->fields.vertical_resolution,
 *(long int*) dh->fields.colors, *(long int*) dh->fields.vip_colors);
}

canonical_original_code:
void print_dib_header(struct bitmap_info_header_ *dh) {
 printf("Read DIB header:\n 	Header size: %ld\n 	Image width: %ld\n 	Image height: %ld\n 	Color planes: %d\n 	Color depth: %d\n 	Compression: %ld\n 	Data size: %ld\n 	Horizontal rule: %ld\n 	Vertical resolution: %ld\n 	No. of colors used: %ld\n 	No. of VIP colors: %ld\n",
 *(long *) dh->size, *(long *) dh->fields.width, *(long *) dh->fields.height,
 *(short *) dh->fields.planes, *(short *) dh->fields.depth,
 *(long *) dh->fields.compression, *(long *) dh->fields.data_size,
 *(long *) dh->fields.horizontal_rule, *(long *) dh->fields.vertical_resolution,
 *(long *) dh->fields.colors, *(long *) dh->fields.vip_colors);
}

variable_types: struct bitmap_info_header_ { char size[4]; union { char buffer[36]; struct { char width[4]; char height[4]; char planes[2]; char depth[2]; char compression[4]; char data_size[4]; char horizontal_rule[4]; char vertical_resolution[4]; char colors[4]; char vip_colors[4]; } fields; }; } *


decompiled_code:
__int64 __fastcall validate_image(__int64 a1)
{
unsigned int v1; // ecx

v1 = 0;
if ( *(_QWORD *)a1 != 40LL )
{
fprintf(
stderr,
"Unsupported DIB header type (size %ld) - currently only %s is supported\n",
*(_QWORD *)a1,
"BITMAPINFOHEADER");
v1 = 1;
}
if ( *(_WORD *)(a1 + 12) != 1 )
{
fwrite("Only 1 color planes are supported\n", 1uLL, 0x22uLL, stderr);
v1 = 1;
}
if ( *(_WORD *)(a1 + 14) != 24 )
{
fwrite("Only 24 color bit depth is supported\n", 1uLL, 0x25uLL, stderr);
v1 = 1;
}
if ( *(_QWORD *)(a1 + 16) )
{
fwrite("Compressed images are not supported in current implementation\n", 1uLL, 0x3EuLL, stderr);
v1 = 1;
}
if ( *(_QWORD *)(a1 + 20) != *(_QWORD *)(a1 + 8) * *(_QWORD *)(a1 + 4) * (*(__int16 *)(a1 + 14) / 8) )
{
fwrite("Data size != width * height * depth / 8\n", 1uLL, 0x28uLL, stderr);
v1 = 1;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
unsigned int v1; // ecx

v1 = 0;
if ( *(_QWORD *)a1 != 40LL )
{
fprintf(
stderr,
"Unsupported DIB header type (size %ld) - currently only %s is supported\n",
*(_QWORD *)a1,
"BITMAPINFOHEADER");
v1 = 1;
}
if ( *(_WORD *)(a1 + 12) != 1 )
{
fwrite("Only 1 color planes are supported\n", 1uLL, 0x22uLL, stderr);
v1 = 1;
}
if ( *(_WORD *)(a1 + 14) != 24 )
{
fwrite("Only 24 color bit depth is supported\n", 1uLL, 0x25uLL, stderr);
v1 = 1;
}
if ( *(_QWORD *)(a1 + 16) )
{
fwrite("Compressed images are not supported in current implementation\n", 1uLL, 0x3EuLL, stderr);
v1 = 1;
}
if ( *(_QWORD *)(a1 + 20) != *(_QWORD *)(a1 + 8) * *(_QWORD *)(a1 + 4) * (*(__int16 *)(a1 + 14) / 8) )
{
fwrite("Data size != width * height * depth / 8\n", 1uLL, 0x28uLL, stderr);
v1 = 1;
}
return v1;
}

original_code:
int validate_image(struct bitmap_info_header_ *bih) {
 int error_found = 0;
 int bytes_per_pixel = 0;
 if (*(long int*) bih->size != 40) {
  fprintf (stderr, "Unsupported DIB header type (size %ld) - currently only %s is supported\n",
   *(long int*)bih->size, "BITMAPINFOHEADER");
  error_found = 1;
 }
 if (*(short int*) bih->fields.planes != 1) {
  fprintf(stderr, "Only 1 color planes are supported\n");
  error_found = 1;
 }
 if (*(short int*) bih->fields.depth != 24 ) {
  fprintf(stderr, "Only 24 color bit depth is supported\n");
  error_found = 1;
 }
 if (*(long int*) bih->fields.compression != 0) {
  fprintf(stderr, "Compressed images are not supported in current implementation\n");
  error_found = 1;
 }
 bytes_per_pixel = *(short int*) bih->fields.depth / 8;
 if (*(long int*) bih->fields.data_size != *(long int*) bih->fields.width * *(long int*) bih->fields.height * bytes_per_pixel) {
  fprintf(stderr, "Data size != width * height * depth / 8\n");
  error_found = 1;
 }
 return error_found;
}

canonical_original_code:
int validate_image(struct bitmap_info_header_ *bih) {
 int error_found = 0;
 int bytes_per_pixel = 0;
 if (*(long *) bih->size != 40) {
  fprintf (stderr, "Unsupported DIB header type (size %ld) - currently only %s is supported\n",
   *(long *)bih->size, "BITMAPINFOHEADER");
  error_found = 1;
 }
 if (*(short *) bih->fields.planes != 1) {
  fprintf(stderr, "Only 1 color planes are supported\n");
  error_found = 1;
 }
 if (*(short *) bih->fields.depth != 24 ) {
  fprintf(stderr, "Only 24 color bit depth is supported\n");
  error_found = 1;
 }
 if (*(long *) bih->fields.compression != 0) {
  fprintf(stderr, "Compressed images are not supported in current implementation\n");
  error_found = 1;
 }
 bytes_per_pixel = *(short *) bih->fields.depth / 8;
 if (*(long *) bih->fields.data_size != *(long *) bih->fields.width * *(long *) bih->fields.height * bytes_per_pixel) {
  fprintf(stderr, "Data size != width * height * depth / 8\n");
  error_found = 1;
 }
 return error_found;
}


decompiled_code:
void *__fastcall read_dib_header(int fd)
{
_QWORD *v1; // rax
void *v2; // rbx

v1 = malloc(0x28uLL);
v2 = v1;
if ( !v1 )
{
fwrite("Unable to allocate memory for the bitmap info header\n", 1uLL, 0x35uLL, stderr);
return v2;
}
*v1 = 0LL;
v1[1] = 0LL;
v1[2] = 0LL;
v1[3] = 0LL;
v1[4] = 0LL;
if ( read(fd, v1, 0x28uLL) == -1 )
{
fwrite("Unable to read image header file\n", 1uLL, 0x21uLL, stderr);
LABEL_7:
free(v2);
return 0LL;
}
if ( (unsigned int)validate_image((__int64)v2) )
{
fwrite("Unsupported image format\n", 1uLL, 0x19uLL, stderr);
goto LABEL_7;
}
return v2;
}

canonical_decompiled_code:
void *__fastcall func4(int fd)
{
_QWORD *v1; // rax
void *v2; // rbx

v1 = malloc(0x28uLL);
v2 = v1;
if ( !v1 )
{
fwrite("Unable to allocate memory for the bitmap info header\n", 1uLL, 0x35uLL, stderr);
return v2;
}
*v1 = 0LL;
v1[1] = 0LL;
v1[2] = 0LL;
v1[3] = 0LL;
v1[4] = 0LL;
if ( read(fd, v1, 0x28uLL) == -1 )
{
fwrite("Unable to read image header file\n", 1uLL, 0x21uLL, stderr);
LABEL_7:
free(v2);
return 0LL;
}
if ( (unsigned int)func3((__int64)v2) )
{
fwrite("Unsupported image format\n", 1uLL, 0x19uLL, stderr);
goto LABEL_7;
}
return v2;
}

original_code:
struct bitmap_info_header_* read_dib_header(int fd) {
 struct bitmap_info_header_ *result;
 ssize_t read_bytes;
 result = malloc(sizeof(struct bitmap_info_header_));
 if (result == ((void *)0)) {
  fprintf(stderr, "Unable to allocate memory for the bitmap info header\n");
  goto out_fail;
 }
 memset(result, 0, sizeof(struct bitmap_info_header_));
 read_bytes = read(fd, result, sizeof(struct bitmap_info_header_));
 if (read_bytes == -1) {
  fprintf(stderr, "Unable to read image header file\n");
  goto out_unable_read;
 }
 if (validate_image(result)) {
  fprintf(stderr, "Unsupported image format\n");
  goto out_error_validate;
 }
 return result;
out_error_validate:
out_unable_read:
 free(result);
out_fail:
 return ((void *)0);
}

canonical_original_code:
struct bitmap_info_header_ *read_dib_header(int fd) {
 struct bitmap_info_header_ *result;
 long read_bytes;
 result = malloc(sizeof(struct bitmap_info_header_));
 if (result == ((void *)0)) {
  fprintf(stderr, "Unable to allocate memory for the bitmap info header\n");
  goto out_fail;
 }
 memset(result, 0, sizeof(struct bitmap_info_header_));
 read_bytes = read(fd, result, sizeof(struct bitmap_info_header_));
 if (read_bytes == -1) {
  fprintf(stderr, "Unable to read image header file\n");
  goto out_unable_read;
 }
 if (validate_image(result)) {
  fprintf(stderr, "Unsupported image format\n");
  goto out_error_validate;
 }
 return result;
out_error_validate:
out_unable_read:
 free(result);
out_fail:
 return ((void *)0);
}

return_type: struct bitmap_info_header_ { char size[4]; union { char buffer[36]; struct { char width[4]; char height[4]; char planes[2]; char depth[2]; char compression[4]; char data_size[4]; char horizontal_rule[4]; char vertical_resolution[4]; char colors[4]; char vip_colors[4]; } fields; }; } *


decompiled_code:
__int64 __fastcall write_bitmap_image(__int64 a1, int a2)
{
signed __int64 v2; // rdx
__int64 result; // rax

if ( write(a2, *(const void **)a1, 0xEuLL) == -1 )
{
fwrite("Unable to write image header\n", 1uLL, 0x1DuLL, stderr);
result = 0xFFFFFFFFLL;
}
else if ( write(a2, *(const void **)(a1 + 8), 0x28uLL) == -1 )
{
fwrite("Unable to write image info header\n", 1uLL, 0x22uLL, stderr);
result = 0xFFFFFFFFLL;
}
else if ( write(a2, *(const void **)(a1 + 24), *(_QWORD *)(a1 + 16)) == -1 )
{
fwrite("Unable to write image data\n", 1uLL, 0x1BuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
v2 = *(_QWORD *)(a1 + 32);
result = 0LL;
if ( v2 > 0 )
{
if ( write(a2, *(const void **)(a1 + 40), v2) == -1 )
{
fwrite("Unable to write image trailer\n", 1uLL, 0x1EuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2)
{
signed __int64 v2; // rdx
__int64 result; // rax

if ( write(a2, *(const void **)a1, 0xEuLL) == -1 )
{
fwrite("Unable to write image header\n", 1uLL, 0x1DuLL, stderr);
result = 0xFFFFFFFFLL;
}
else if ( write(a2, *(const void **)(a1 + 8), 0x28uLL) == -1 )
{
fwrite("Unable to write image info header\n", 1uLL, 0x22uLL, stderr);
result = 0xFFFFFFFFLL;
}
else if ( write(a2, *(const void **)(a1 + 24), *(_QWORD *)(a1 + 16)) == -1 )
{
fwrite("Unable to write image data\n", 1uLL, 0x1BuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
v2 = *(_QWORD *)(a1 + 32);
result = 0LL;
if ( v2 > 0 )
{
if ( write(a2, *(const void **)(a1 + 40), v2) == -1 )
{
fwrite("Unable to write image trailer\n", 1uLL, 0x1EuLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
}
return result;
}

original_code:
int write_bitmap_image(struct bitmap_image_ *bi, int fd) {
 if (write(fd, bi->bfh, sizeof(struct bitmap_file_header_)) == -1) {
  fprintf(stderr, "Unable to write image header\n");
  return -1;
 }
 if (write(fd, bi->bih, sizeof(struct bitmap_info_header_)) == -1) {
  fprintf(stderr, "Unable to write image info header\n");
  return -1;
 }
 if (write(fd, bi->data, bi->data_size) == -1) {
  fprintf(stderr, "Unable to write image data\n");
  return -1;
 }
 if (bi->trailer_size > 0)
  if (write(fd, bi->trailer, bi->trailer_size) == -1) {
   fprintf(stderr, "Unable to write image trailer\n");
   return -1;
  }
 return 0;
}


decompiled_code:
void __fastcall free_bitmap_image(__int64 a1)
{
void *v1; // rdi

free(*(void **)a1);
free(*(void **)(a1 + 8));
free(*(void **)(a1 + 24));
v1 = *(void **)(a1 + 40);
if ( v1 )
free(v1);
}

original_code:
void free_bitmap_image(struct bitmap_image_ *bi) {
 free(bi->bfh);
 free(bi->bih);
 free(bi->data);
 if (bi->trailer != ((void *)0))
  free(bi->trailer);
}


decompiled_code:
__int64 __fastcall prepare_arguments(int a1, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // rax
__int64 v4; // rbx

if ( a1 <= 2 )
return 0LL;
v2 = 8LL * a1;
if ( **(_BYTE **)(a2 + v2 - 8) == 45 )
return 0LL;
if ( **(_BYTE **)(a2 + v2 - 16) == 45 )
return 0LL;
v3 = parse_arguments();
v4 = v3;
if ( v3 && debug )
print_arguments(v3);
return v4;
}

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

if ( a1 <= 2 )
return 0LL;
v2 = 8LL * a1;
if ( **(_BYTE **)(a2 + v2 - 8) == 45 )
return 0LL;
if ( **(_BYTE **)(a2 + v2 - 16) == 45 )
return 0LL;
v3 = parse_arguments();
v4 = v3;
if ( v3 && debug )
print_arguments(v3);
return v4;
}

original_code:
struct arguments_ *prepare_arguments(int argc, char *argv[]) {
 struct arguments_ *args = ((void *)0);
 if (argc < 3 || argv[argc - 1][0] == '-' || argv[argc - 2][0] == '-') {
  return ((void *)0);
 }
 args = parse_arguments(argc, argv);
 if (args == ((void *)0)) {
  return ((void *)0);
 }
 if (debug)
  print_arguments(args);
 return args;
}

variable_types: argc int


decompiled_code:
__int64 __fastcall get_image(const char *a1)
{
unsigned int v1; // eax
int v2; // ebx
__int64 v3; // rbp

v1 = open(a1, 2);
if ( v1 == -1 )
{
fwrite("Unable to open image file\n", 1uLL, 0x1AuLL, stderr);
v3 = 0LL;
}
else
{
v2 = v1;
v3 = read_bitmap_image(v1);
if ( !v3 )
{
fwrite("Unable to read and parse bitmap image\n", 1uLL, 0x26uLL, stderr);
close(v2);
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
unsigned int v1; // eax
int v2; // ebx
__int64 v3; // rbp

v1 = open(a1, 2);
if ( v1 == -1 )
{
fwrite("Unable to open image file\n", 1uLL, 0x1AuLL, stderr);
v3 = 0LL;
}
else
{
v2 = v1;
v3 = read_bitmap_image(v1);
if ( !v3 )
{
fwrite("Unable to read and parse bitmap image\n", 1uLL, 0x26uLL, stderr);
close(v2);
}
}
return v3;
}

original_code:
struct bitmap_image_ *get_image(char *image_file) {
 struct bitmap_image_ *result = ((void *)0);
 int fd;
 fd = open(image_file, 02);
 if (fd == -1) {
  fprintf(stderr, "Unable to open image file\n");
  goto out_fail;
 }
 result = read_bitmap_image(fd);
 if (result == ((void *)0)) {
  fprintf(stderr, "Unable to read and parse bitmap image\n");
  close(fd);
  goto out_fail;
 }
 return result;
out_fail:
 return ((void *)0);
}

return_type: struct bitmap_image_ { struct bitmap_file_header_ { union { char buffer[14]; struct { char header_field[2]; char size[4]; char reserved[4]; char offset[4]; } fields; }; } *bfh; struct bitmap_info_header_ { char size[4]; union { char buffer[36]; struct { char width[4]; char height[4]; char planes[2]; char depth[2]; char compression[4]; char data_size[4]; char horizontal_rule[4]; char vertical_resolution[4]; char colors[4]; char vip_colors[4]; } fields; }; } *bih; long data_size; char *data; long trailer_size; char *trailer; } *


repo: doggkruse/smartRFtoPcap

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
pcap_dumpfile = pcap_open_dead();
if ( !pcap_dumpfile )
err(1, "pcap_open_dead: ");
dumper = pcap_dump_open();
pcap_dump_flush();
if ( !dumper )
{
warn("pcap_dump_open", "out-dump.pcap");
pcap_close();
exit(1);
}
inFile = fopen("input.psd", "r");
if ( !inFile )
{
puts("Error opening psd file");
exit(1);
}
while ( fread(&packetData, 1uLL, 0x10FuLL, inFile) )
{
qword_4040C5 /= 0x64uLL;
pcap_dump();
pcap_dump_flush();
printf(
"Packet: %u len: %u chan: 0x%02x rssi: %i\n",
(unsigned int)dword_4040C1,
(unsigned __int8)byte_4040CF,
byte_4040D0[(unsigned __int8)byte_4040CF - 1] & 0x7F,
(unsigned int)(unsigned __int8)byte_4040D0[(unsigned __int8)byte_4040CF - 2] - 94);
}
return 0;
}

original_code:
int main(int argc, const char * argv[])
{
    size_t bytesRead;
    struct pcap_pkthdr pcapHdr;
    struct timeval ts;
    int rssi, channel;
    pcap_dumpfile = pcap_open_dead(147, 128);
    if(pcap_dumpfile == ((void *)0))
    {
        err(1, "pcap_open_dead: ");
    }
    dumper = pcap_dump_open(pcap_dumpfile, "out-dump.pcap");
    pcap_dump_flush(dumper);
    if (dumper == ((void *)0))
    {
        warn("pcap_dump_open");
        pcap_close(pcap_dumpfile);
        exit(1);
    }
    inFile = fopen("input.psd", "r");
    if(((void *)0) == inFile)
    {
        printf("Error opening psd file\n");
        exit(1);
    }
    do
    {
        bytesRead = fread(&packetData, 1, sizeof(psdPacketCaptureType), inFile);
        if(bytesRead)
        {
            packetData.timeStamp /= 100;
            ts.tv_sec = (packetData.timeStamp / (uint64_t)1000000);
            ts.tv_usec = (packetData.timeStamp % (uint64_t)1000000);
            pcapHdr.ts = ts;
            pcapHdr.caplen = pcapHdr.len = packetData.packetLen - 2;
            pcap_dump((unsigned char *)dumper, &pcapHdr, packetData.packet);
            pcap_dump_flush(dumper);
            channel = packetData.packet[packetData.packetLen-1] & 0x7F;
            rssi = -94 + packetData.packet[packetData.packetLen-2];
            printf("Packet: %u len: %u chan: 0x%02x rssi: %i\n",
                   packetData.packetNumber, packetData.packetLen, channel, rssi);
        }
    }while(bytesRead > 0);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    unsigned long bytesRead;
    struct pcap_pkthdr pcapHdr;
    struct timeval ts;
    int rssi; int channel;
    pcap_dumpfile = pcap_open_dead(147, 128);
    if(pcap_dumpfile == ((void *)0))
    {
        err(1, "pcap_open_dead: ");
    }
    dumper = pcap_dump_open(pcap_dumpfile, "out-dump.pcap");
    pcap_dump_flush(dumper);
    if (dumper == ((void *)0))
    {
        warn("pcap_dump_open");
        pcap_close(pcap_dumpfile);
        exit(1);
    }
    inFile = fopen("input.psd", "r");
    if(((void *)0) == inFile)
    {
        printf("Error opening psd file\n");
        exit(1);
    }
    do
    {
        bytesRead = fread(&packetData, 1, sizeof(psdPacketCaptureType), inFile);
        if(bytesRead)
        {
            packetData.timeStamp /= 100;
            ts.tv_sec = (packetData.timeStamp / (unsigned long)1000000);
            ts.tv_usec = (packetData.timeStamp % (unsigned long)1000000);
            pcapHdr.ts = ts;
            pcapHdr.caplen = pcapHdr.len = packetData.packetLen - 2;
            pcap_dump((unsigned char *)dumper, &pcapHdr, packetData.packet);
            pcap_dump_flush(dumper);
            channel = packetData.packet[packetData.packetLen-1] & 0x7F;
            rssi = -94 + packetData.packet[packetData.packetLen-2];
            printf("Packet: %u len: %u chan: 0x%02x rssi: %i\n",
                   packetData.packetNumber, packetData.packetLen, channel, rssi);
        }
    }while(bytesRead > 0);
    return 0;
}

variable_types:
argv
char *[]

variable_types:
bytesRead
unsigned long

variable_types: struct pcap_pkthdr { struct timeval { long tv_sec; long tv_usec; } ts; unsigned int caplen; unsigned int len; }

variable_types: rssi int

variable_types: channel int

user_defined_types: struct pcap_pkthdr { struct timeval ts; unsigned int caplen; unsigned int len; }


repo: dokey/test

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er13
unsigned int v4; // er14
unsigned int v5; // ebp
unsigned int v6; // er12
unsigned int v8; // [rsp+8h] [rbp-30h] BYREF
unsigned int v9[11]; // [rsp+Ch] [rbp-2Ch] BYREF

printf("Enter two integer numbers : ");
scanf("%d%d", v9, &v8);
v3 = v9[0] - v8;
v4 = v8 * v9[0];
v5 = v9[0] % (__int64)(int)v8;
v6 = v9[0] / (__int64)(int)v8;
printf("%d + %d = %d\n", v9[0], v8, v8 + v9[0]);
printf("%d - %d = %d\n", v9[0], v8, v3);
printf("%d * %d = %d\n", v9[0], v8, v4);
printf("%d / %d = %d\n", v9[0], v8, v6);
printf("%d %% %d = %d\n", v9[0], v8, v5);
return 0;
}

original_code:
int main() {
    int arg1, arg2, add, sub, mul, quo, rem ;
    printf( "Enter two integer numbers : " );
    scanf( "%d%d", &arg1, &arg2 );
    __asm__ ( "addl %%ebx, %%eax;" : "=a" (add) : "a" (arg1) , "b" (arg2) );
    __asm__ ( "subl %%ebx, %%eax;" : "=a" (sub) : "a" (arg1) , "b" (arg2) );
    __asm__ ( "imull %%ebx, %%eax;" : "=a" (mul) : "a" (arg1) , "b" (arg2) );
    __asm__ ( "movl $0x0, %%edx;"
              "movl %2, %%eax;"
              "movl %3, %%ebx;"
               "idivl %%ebx;" : "=a" (quo), "=d" (rem) : "g" (arg1), "g" (arg2) );
    printf( "%d + %d = %d\n", arg1, arg2, add );
    printf( "%d - %d = %d\n", arg1, arg2, sub );
    printf( "%d * %d = %d\n", arg1, arg2, mul );
    printf( "%d / %d = %d\n", arg1, arg2, quo );
    printf( "%d %% %d = %d\n", arg1, arg2, rem );
    return 0 ;
}

canonical_original_code:
int main() {
    int arg1; int arg2; int add; int sub; int mul; int quo; int rem;
    printf( "Enter two integer numbers : " );
    scanf( "%d%d", &arg1, &arg2 );
    __asm__ ( "addl %%ebx, %%eax;" : "=a" (add) : "a" (arg1) , "b" (arg2) );
    __asm__ ( "subl %%ebx, %%eax;" : "=a" (sub) : "a" (arg1) , "b" (arg2) );
    __asm__ ( "imull %%ebx, %%eax;" : "=a" (mul) : "a" (arg1) , "b" (arg2) );
    __asm__ ( "movl $0x0, %%edx;"
              "movl %2, %%eax;"
              "movl %3, %%ebx;"
               "idivl %%ebx;" : "=a" (quo), "=d" (rem) : "g" (arg1), "g" (arg2) );
    printf( "%d + %d = %d\n", arg1, arg2, add );
    printf( "%d - %d = %d\n", arg1, arg2, sub );
    printf( "%d * %d = %d\n", arg1, arg2, mul );
    printf( "%d / %d = %d\n", arg1, arg2, quo );
    printf( "%d %% %d = %d\n", arg1, arg2, rem );
    return 0 ;
}

variable_types: add int

variable_types: sub int

variable_types: mul int

variable_types: quo int

variable_types: rem int


repo: dolfly/nmdb

decompiled_code:
void __fastcall sctp_reply_err(__int64 a1, unsigned int a2)
{
rep_send_error(a1, a2);
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, unsigned int a2)
{
func2(a1, a2);
}

original_code:
static void sctp_reply_err(const struct req_info *req, uint32_t reply)
{
 rep_send_error(req, reply);
}

canonical_original_code:
static void sctp_reply_err(struct req_info *req, unsigned int reply)
{
 rep_send_error(req, reply);
}

variable_types: struct req_info { int fd; int type; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *clisa; unsigned int clilen; unsigned int id; unsigned short cmd; unsigned short flags; unsigned char *payload; unsigned long psize; void (*reply_mini)(struct req_info *req, unsigned int reply); void (*reply_err)(struct req_info *req, unsigned int reply); void (*reply_long)(struct req_info *req, unsigned int reply, unsigned char *val, unsigned long vsize); } *

variable_types:
reply
unsigned int

user_defined_types: struct req_info { int fd; int type; struct sockaddr *clisa; unsigned int clilen; unsigned int id; unsigned short cmd; unsigned short flags; unsigned char *payload; unsigned long psize; void (*reply_mini)(struct req_info *req, unsigned int reply); void (*reply_err)(struct req_info *req, unsigned int reply); void (*reply_long)(struct req_info *req, unsigned int reply, unsigned char *val, unsigned long vsize); }


decompiled_code:
void __fastcall sctp_reply_mini(__int64 a1, unsigned int a2)
{
int v2; // eax
int v3[4]; // [rsp+0h] [rbp-10h] BYREF

if ( !settings[15] )
{
v2 = _bswap_32(a2);
v3[0] = *(_DWORD *)(a1 + 20);
v3[1] = v2;
rep_send(a1, v3, 8uLL);
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, unsigned int a2)
{
int v2; // eax
int v3[4]; // [rsp+0h] [rbp-10h] BYREF

if ( !settings[15] )
{
v2 = func1(a2);
v3[0] = *(_DWORD *)(a1 + 20);
v3[1] = v2;
func4(a1, v3, 8uLL);
}
}

original_code:
static void sctp_reply_mini(const struct req_info *req, uint32_t reply)
{
 unsigned char minibuf[8];
 if (settings.passive)
  return;
 reply = htonl(reply);
 memcpy(minibuf, &(req->id), 4);
 memcpy(minibuf + 4, &reply, 4);
 rep_send(req, minibuf, 8);
 return;
}

canonical_original_code:
static void sctp_reply_mini(struct req_info *req, unsigned int reply)
{
 unsigned char minibuf[8];
 if (settings.passive)
  return;
 reply = htonl(reply);
 memcpy(minibuf, &(req->id), 4);
 memcpy(minibuf + 4, &reply, 4);
 rep_send(req, minibuf, 8);
 return;
}

variable_types:
minibuf
unsigned char [8]

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


decompiled_code:
void __fastcall sctp_reply_long(__int64 a1, unsigned int a2, const void *a3, size_t a4)
{
int v6; // er14
_DWORD *v7; // rbx
int v8; // eax

if ( a3 )
{
v6 = _bswap_32(a2);
v7 = malloc(a4 + 12);
v8 = _bswap_32(a4);
*v7 = *(_DWORD *)(a1 + 20);
v7[1] = v6;
v7[2] = v8;
memcpy(v7 + 3, a3, a4);
rep_send(a1, v7, a4 + 12);
free(v7);
}
else
{
sctp_reply_mini(a1, a2);
}
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, unsigned int a2, const void *a3, size_t a4)
{
int v6; // er14
_DWORD *v7; // rbx
int v8; // eax

if ( a3 )
{
v6 = func1(a2);
v7 = malloc(a4 + 12);
v8 = func1(a4);
*v7 = *(_DWORD *)(a1 + 20);
v7[1] = v6;
v7[2] = v8;
memcpy(v7 + 3, a3, a4);
func4(a1, v7, a4 + 12);
free(v7);
}
else
{
func5(a1, a2);
}
}

original_code:
static void sctp_reply_long(const struct req_info *req, uint32_t reply,
   unsigned char *val, size_t vsize)
{
 if (val == ((void *)0)) {
  sctp_reply_mini(req, reply);
 } else {
  unsigned char *buf;
  size_t bsize;
  uint32_t t;
  reply = htonl(reply);
  bsize = 4 + 4 + 4 + vsize;
  buf = malloc(bsize);
  t = htonl(vsize);
  memcpy(buf, &(req->id), 4);
  memcpy(buf + 4, &reply, 4);
  memcpy(buf + 8, &t, 4);
  memcpy(buf + 12, val, vsize);
  rep_send(req, buf, bsize);
  free(buf);
 }
 return;
}

canonical_original_code:
static void sctp_reply_long(struct req_info *req, unsigned int reply, unsigned char *val, unsigned long vsize)
{
 if (val == ((void *)0)) {
  sctp_reply_mini(req, reply);
 } else {
  unsigned char *buf;
  unsigned long bsize;
  unsigned int t;
  reply = htonl(reply);
  bsize = 4 + 4 + 4 + vsize;
  buf = malloc(bsize);
  t = htonl(vsize);
  memcpy(buf, &(req->id), 4);
  memcpy(buf + 4, &reply, 4);
  memcpy(buf + 8, &t, 4);
  memcpy(buf + 12, val, vsize);
  rep_send(req, buf, bsize);
  free(buf);
 }
 return;
}

variable_types:
val
unsigned char *

variable_types:
vsize
unsigned long

variable_types:
buf
unsigned char *

variable_types:
bsize
unsigned long

variable_types:
t
unsigned int


decompiled_code:
__int64 sctp_init()
{
int v0; // eax
unsigned int v1; // ebx
int buf; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF
int optval[3]; // [rsp+2Ch] [rbp-Ch] BYREF

optval[0] = inet_pton(2, *(const char **)&settings[10], &buf);
if ( optval[0] <= 0 )
return (unsigned int)-1;
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = buf;
*(_WORD *)addr.sa_data = _bswap_16(settings[12]);
v0 = socket(2, 5, 132);
v1 = v0;
if ( v0 < 0 )
return (unsigned int)-1;
optval[0] = 1;
if ( setsockopt(v0, 1, 2, optval, 4u) < 0 )
{
errlog("Error in setsockopt():");
close(v1);
v1 = -1;
}
else
{
optval[0] = bind(v1, &addr, 0x10u);
if ( optval[0] < 0
|| (optval[0] = listen(v1, 1024), optval[0] < 0)
|| (optval[0] = 1, setsockopt(v1, 132, 3, optval, 4u) < 0) )
{
close(v1);
v1 = -1;
}
}
return v1;
}

canonical_decompiled_code:
__int64 func7()
{
int v0; // eax
unsigned int v1; // ebx
int buf; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF
int optval[3]; // [rsp+2Ch] [rbp-Ch] BYREF

optval[0] = inet_pton(2, *(const char **)&settings[10], &buf);
if ( optval[0] <= 0 )
return (unsigned int)-1;
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = buf;
*(_WORD *)addr.sa_data = func0(settings[12]);
v0 = socket(2, 5, 132);
v1 = v0;
if ( v0 < 0 )
return (unsigned int)-1;
optval[0] = 1;
if ( setsockopt(v0, 1, 2, optval, 4u) < 0 )
{
errlog("Error in setsockopt():");
close(v1);
v1 = -1;
}
else
{
optval[0] = bind(v1, &addr, 0x10u);
if ( optval[0] < 0
|| (optval[0] = listen(v1, 1024), optval[0] < 0)
|| (optval[0] = 1, setsockopt(v1, 132, 3, optval, 4u) < 0) )
{
close(v1);
v1 = -1;
}
}
return v1;
}

original_code:
int sctp_init(void)
{
 int fd, rv;
 struct sockaddr_in srvsa;
 struct in_addr ia;
 rv = inet_pton(2, settings.sctp_addr, &ia);
 if (rv <= 0)
  return -1;
 srvsa.sin_family = 2;
 srvsa.sin_addr.s_addr = ia.s_addr;
 srvsa.sin_port = htons(settings.sctp_port);
 fd = socket(2, SOCK_SEQPACKET, IPPROTO_SCTP);
 if (fd < 0)
  return -1;
 rv = 1;
 if (setsockopt(fd, 1, 2, &rv, sizeof(rv)) < 0 ) {
  errlog("Error in setsockopt():");
  close(fd);
  return -1;
 }
 rv = bind(fd, (struct sockaddr *) &srvsa, sizeof(srvsa));
 if (rv < 0) {
  close(fd);
  return -1;
 }
 rv = listen(fd, 1024);
 if (rv < 0) {
  close(fd);
  return -1;
 }
 rv = 1;
 if (setsockopt(fd, IPPROTO_SCTP, 3, &rv, sizeof(rv)) < 0 ) {
  close(fd);
  return -1;
 }
 return fd;
}

canonical_original_code:
int sctp_init()
{
 int fd; int rv;
 struct sockaddr_in srvsa;
 struct in_addr ia;
 rv = inet_pton(2, settings.sctp_addr, &ia);
 if (rv <= 0)
  return -1;
 srvsa.sin_family = 2;
 srvsa.sin_addr.s_addr = ia.s_addr;
 srvsa.sin_port = htons(settings.sctp_port);
 fd = socket(2, SOCK_SEQPACKET, IPPROTO_SCTP);
 if (fd < 0)
  return -1;
 rv = 1;
 if (setsockopt(fd, 1, 2, &rv, sizeof(rv)) < 0 ) {
  errlog("Error in setsockopt():");
  close(fd);
  return -1;
 }
 rv = bind(fd, (struct sockaddr *) &srvsa, sizeof(srvsa));
 if (rv < 0) {
  close(fd);
  return -1;
 }
 rv = listen(fd, 1024);
 if (rv < 0) {
  close(fd);
  return -1;
 }
 rv = 1;
 if (setsockopt(fd, IPPROTO_SCTP, 3, &rv, sizeof(rv)) < 0 ) {
  close(fd);
  return -1;
 }
 return fd;
}

variable_types: fd int

variable_types: rv int

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

variable_types:
ia
struct in_addr { unsigned int s_addr; }

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


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

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

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

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


decompiled_code:
ssize_t __fastcall sctp_recv(int a1)
{
ssize_t result; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-6Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-68h] BYREF
int v4[2]; // [rsp+20h] [rbp-58h] BYREF
struct sockaddr *v5; // [rsp+28h] [rbp-50h]
socklen_t v6; // [rsp+30h] [rbp-48h]
void (__fastcall *v7)(__int64, unsigned int); // [rsp+50h] [rbp-28h]
void (__fastcall *v8)(__int64, unsigned int); // [rsp+58h] [rbp-20h]
void (__fastcall *v9)(__int64, unsigned int, const void *, size_t); // [rsp+60h] [rbp-18h]

addr_len = 16;
result = recvfrom(a1, &static_buf, (size_t)&unk_11000, 0, &addr, &addr_len);
if ( (int)result >= 0 )
{
if ( (int)result > 7 )
{
++stats[17];
v4[0] = a1;
v4[1] = 4;
v5 = &addr;
v6 = addr_len;
v7 = sctp_reply_mini;
v8 = sctp_reply_err;
v9 = sctp_reply_long;
result = parse_message(v4, &static_buf, (int)result);
}
else
{
++stats[19];
}
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func9(int a1)
{
ssize_t result; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-6Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-68h] BYREF
int v4[2]; // [rsp+20h] [rbp-58h] BYREF
struct sockaddr *v5; // [rsp+28h] [rbp-50h]
socklen_t v6; // [rsp+30h] [rbp-48h]
void (__fastcall *v7)(__int64, unsigned int); // [rsp+50h] [rbp-28h]
void (__fastcall *v8)(__int64, unsigned int); // [rsp+58h] [rbp-20h]
void (__fastcall *v9)(__int64, unsigned int, const void *, size_t); // [rsp+60h] [rbp-18h]

addr_len = 16;
result = recvfrom(a1, &static_buf, (size_t)&unk_11000, 0, &addr, &addr_len);
if ( (int)result >= 0 )
{
if ( (int)result > 7 )
{
++stats[17];
v4[0] = a1;
v4[1] = 4;
v5 = &addr;
v6 = addr_len;
v7 = sctp_reply_mini;
v8 = sctp_reply_err;
v9 = sctp_reply_long;
result = parse_message(v4, &static_buf, (int)result);
}
else
{
++stats[19];
}
}
return result;
}

original_code:
void sctp_recv(int fd, short event, void *arg)
{
 int rv;
 struct req_info req;
 struct sockaddr_in clisa;
 socklen_t clilen;
 clilen = sizeof(clisa);
 rv = recvfrom(fd, static_buf, (68 * 1024), 0, (struct sockaddr *) &clisa,
   &clilen);
 if (rv < 0) {
  goto exit;
 }
 if (rv < 8) {
  stats.net_broken_req++;
  goto exit;
 }
 stats.msg_sctp++;
 req.fd = fd;
 req.type = 4;
 req.clisa = (struct sockaddr *) &clisa;
 req.clilen = clilen;
 req.reply_mini = sctp_reply_mini;
 req.reply_err = sctp_reply_err;
 req.reply_long = sctp_reply_long;
 parse_message(&req, static_buf, rv);
exit:
 return;
}

canonical_original_code:
void sctp_recv(int fd, short event, void *arg)
{
 int rv;
 struct req_info req;
 struct sockaddr_in clisa;
 unsigned int clilen;
 clilen = sizeof(clisa);
 rv = recvfrom(fd, static_buf, (68 * 1024), 0, (struct sockaddr *) &clisa,
   &clilen);
 if (rv < 0) {
  goto exit;
 }
 if (rv < 8) {
  stats.net_broken_req++;
  goto exit;
 }
 stats.msg_sctp++;
 req.fd = fd;
 req.type = 4;
 req.clisa = (struct sockaddr *) &clisa;
 req.clilen = clilen;
 req.reply_mini = sctp_reply_mini;
 req.reply_err = sctp_reply_err;
 req.reply_long = sctp_reply_long;
 parse_message(&req, static_buf, rv);
exit:
 return;
}

variable_types: event short

variable_types:
arg
void *

variable_types: struct req_info { int fd; int type; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *clisa; unsigned int clilen; unsigned int id; unsigned short cmd; unsigned short flags; unsigned char *payload; unsigned long psize; void (*reply_mini)(struct req_info *req, unsigned int reply); void (*reply_err)(struct req_info *req, unsigned int reply); void (*reply_long)(struct req_info *req, unsigned int reply, unsigned char *val, unsigned long vsize); }

variable_types:
clilen
unsigned int


decompiled_code:
void __fastcall rep_send_error(__int64 a1, unsigned int a2)
{
int v2; // er12
int v3; // eax
int buf[9]; // [rsp+0h] [rbp-24h] BYREF

if ( !settings[15] )
{
v2 = _bswap_32(0x800u);
v3 = _bswap_32(a2);
buf[0] = *(_DWORD *)(a1 + 20);
buf[1] = v2;
buf[2] = v3;
if ( (int)sendto(*(_DWORD *)a1, buf, 0xCuLL, 0, *(const struct sockaddr **)(a1 + 8), *(_DWORD *)(a1 + 16)) < 0 )
errlog("rep_send_error() failed");
}
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, unsigned int a2)
{
int v2; // er12
int v3; // eax
int buf[9]; // [rsp+0h] [rbp-24h] BYREF

if ( !settings[15] )
{
v2 = func1(0x800u);
v3 = func1(a2);
buf[0] = *(_DWORD *)(a1 + 20);
buf[1] = v2;
buf[2] = v3;
if ( (int)sendto(*(_DWORD *)a1, buf, 0xCuLL, 0, *(const struct sockaddr **)(a1 + 8), *(_DWORD *)(a1 + 16)) < 0 )
errlog("rep_send_error() failed");
}
}

original_code:
static void rep_send_error(const struct req_info *req, const unsigned int code)
{
 int r, c;
 unsigned char minibuf[3 * 4];
 if (settings.passive)
  return;
 r = htonl(0x800);
 c = htonl(code);
 memcpy(minibuf, &(req->id), 4);
 memcpy(minibuf + 4, &r, 4);
 memcpy(minibuf + 8, &c, 4);
 r = sendto(req->fd, minibuf, 3 * 4, 0, req->clisa, req->clilen);
 if (r < 0) {
  errlog("rep_send_error() failed");
 }
}

canonical_original_code:
static void rep_send_error(struct req_info *req, unsigned int code)
{
 int r; int c;
 unsigned char minibuf[-1];
 if (settings.passive)
  return;
 r = htonl(0x800);
 c = htonl(code);
 memcpy(minibuf, &(req->id), 4);
 memcpy(minibuf + 4, &r, 4);
 memcpy(minibuf + 8, &c, 4);
 r = sendto(req->fd, minibuf, 3 * 4, 0, req->clisa, req->clilen);
 if (r < 0) {
  errlog("rep_send_error() failed");
 }
}

variable_types:
code
unsigned int

variable_types: r int

variable_types: c int

variable_types:
minibuf
unsigned char [-1]


decompiled_code:
_BOOL8 log_init()
{
_BOOL8 result; // rax

if ( *((_QWORD *)&settings + 10) )
{
if ( !strcmp(*((const char **)&settings + 10), "-") )
{
logfd = 1;
result = 1LL;
}
else
{
logfd = open(*((const char **)&settings + 10), 1089, 432LL);
result = logfd >= 0;
}
}
else
{
logfd = -1;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
_BOOL8 func0()
{
_BOOL8 result; // rax

if ( *((_QWORD *)&settings + 10) )
{
if ( !strcmp(*((const char **)&settings + 10), "-") )
{
logfd = 1;
result = 1LL;
}
else
{
logfd = open(*((const char **)&settings + 10), 1089, 432LL);
result = logfd >= 0;
}
}
else
{
logfd = -1;
result = 1LL;
}
return result;
}

original_code:
int log_init(void)
{
 if (settings.logfname == ((void *)0)) {
  logfd = -1;
  return 1;
 }
 if (strcmp(settings.logfname, "-") == 0) {
  logfd = 1;
  return 1;
 }
 logfd = open(settings.logfname, 01 | 02000 | 0100, 0660);
 if (logfd < 0)
  return 0;
 return 1;
}

canonical_original_code:
int log_init()
{
 if (settings.logfname == ((void *)0)) {
  logfd = -1;
  return 1;
 }
 if (strcmp(settings.logfname, "-") == 0) {
  logfd = 1;
  return 1;
 }
 logfd = open(settings.logfname, 01 | 02000 | 0100, 0660);
 if (logfd < 0)
  return 0;
 return 1;
}


decompiled_code:
_BOOL8 log_reopen()
{
return log_init();
}

canonical_decompiled_code:
_BOOL8 func1()
{
return func0();
}

original_code:
int log_reopen(void)
{
 return log_init();
}

canonical_original_code:
int log_reopen()
{
 return log_init();
}


wlog

decompiled_code:
void wlog(char *format, ...)
{
struct tm *v1; // rax
int v2; // ebp
int v3; // ebx
time_t timer; // [rsp+8h] [rbp-4F0h] BYREF
char s[512]; // [rsp+10h] [rbp-4E8h] BYREF
char buf[520]; // [rsp+210h] [rbp-2E8h] BYREF
gcc_va_list arg; // [rsp+418h] [rbp-E0h] BYREF

if ( logfd != -1 )
{
timer = time(0LL);
v1 = localtime(&timer);
v2 = strftime(s, 0x200uLL, "%F %H:%M:%S ", v1);
va_start(arg, format);
v3 = vsnprintf(buf, 0x200uLL, format, arg);
write(logfd, s, v2);
write(logfd, buf, v3);
}
}

canonical_decompiled_code:
void func2(char *format, ...)
{
struct tm *v1; // rax
int v2; // ebp
int v3; // ebx
time_t timer; // [rsp+8h] [rbp-4F0h] BYREF
char s[512]; // [rsp+10h] [rbp-4E8h] BYREF
char buf[520]; // [rsp+210h] [rbp-2E8h] BYREF
gcc_va_list arg; // [rsp+418h] [rbp-E0h] BYREF

if ( logfd != -1 )
{
timer = time(0LL);
v1 = localtime(&timer);
v2 = strftime(s, 0x200uLL, "%F %H:%M:%S ", v1);
va_start(arg, format);
v3 = vsnprintf(buf, 0x200uLL, format, arg);
write(logfd, s, v2);
write(logfd, buf, v3);
}
}

original_code:
void wlog(const char *fmt, ...)
{
 int r, tr;
 va_list ap;
 char str[512];
 char timestr[512];
 time_t t;
 struct tm *tmp;
 if (logfd == -1)
  return;
 t = time(((void *)0));
 tmp = localtime(&t);
 tr = strftime(timestr, 512, "%F %H:%M:%S ", tmp);
 __builtin_va_start(ap,fmt);
 r = vsnprintf(str, 512, fmt, ap);
 __builtin_va_end(ap);
 write(logfd, timestr, tr);
 write(logfd, str, r);
}

canonical_original_code:
void wlog(char *fmt, ...)
{
 int r; int tr;
 __builtin_va_list ap;
 char str[512];
 char timestr[512];
 long t;
 struct tm *tmp;
 if (logfd == -1)
  return;
 t = time(((void *)0));
 tmp = localtime(&t);
 tr = strftime(timestr, 512, "%F %H:%M:%S ", tmp);
 __builtin_va_start(ap,fmt);
 r = vsnprintf(str, 512, fmt, ap);
 __builtin_va_end(ap);
 write(logfd, timestr, tr);
 write(logfd, str, r);
}

variable_types:
fmt
char *

variable_types: tr int

variable_types:
str
char [512]

variable_types:
timestr
char [512]

variable_types: t long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long __tm_gmtoff; char *__tm_zone; } *


errlog

decompiled_code:
void __fastcall errlog(const char *a1)
{
int *v1; // rax
char *v2; // rax

v1 = _errno_location();
v2 = strerror(*v1);
wlog("%s: %s\n", a1, v2);
}

canonical_decompiled_code:
void __fastcall func3(const char *a1)
{
int *v1; // rax
char *v2; // rax

v1 = _errno_location();
v2 = strerror(*v1);
func2("%s: %s\n", a1, v2);
}

original_code:
void errlog(const char *s)
{
 wlog("%s: %s\n", s, strerror((*__errno_location ())));
}

canonical_original_code:
void errlog(char *s)
{
 wlog("%s: %s\n", s, strerror((*__errno_location ())));
}

variable_types:
s
char *


decompiled_code:
_QWORD *queue_create()
{
_QWORD *v0; // rax
_QWORD *v1; // rbx
pthread_mutexattr_t attr; // [rsp+Ch] [rbp-Ch] BYREF

v0 = malloc(0x70uLL);
v1 = v0;
if ( v0 )
{
v0[11] = 0LL;
v0[12] = 0LL;
v0[13] = 0LL;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, 0);
pthread_mutex_init((pthread_mutex_t *)v1, &attr);
pthread_mutexattr_destroy(&attr);
pthread_cond_init((pthread_cond_t *)(v1 + 5), 0LL);
}
return v1;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *v0; // rax
_QWORD *v1; // rbx
pthread_mutexattr_t attr; // [rsp+Ch] [rbp-Ch] BYREF

v0 = malloc(0x70uLL);
v1 = v0;
if ( v0 )
{
v0[11] = 0LL;
v0[12] = 0LL;
v0[13] = 0LL;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, 0);
pthread_mutex_init((pthread_mutex_t *)v1, &attr);
pthread_mutexattr_destroy(&attr);
pthread_cond_init((pthread_cond_t *)(v1 + 5), 0LL);
}
return v1;
}

original_code:
struct queue *queue_create(void)
{
 struct queue *q;
 pthread_mutexattr_t attr;
 q = malloc(sizeof(struct queue));
 if (q == ((void *)0))
  return ((void *)0);
 q->size = 0;
 q->top = ((void *)0);
 q->bottom = ((void *)0);
 pthread_mutexattr_init(&attr);
 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
 pthread_mutex_init(&(q->lock), &attr);
 pthread_mutexattr_destroy(&attr);
 pthread_cond_init(&(q->cond), ((void *)0));
 return q;
}

canonical_original_code:
struct queue *queue_create()
{
 struct queue *q;
 union pthread_mutexattr_t attr;
 q = malloc(sizeof(struct queue));
 if (q == ((void *)0))
  return ((void *)0);
 q->size = 0;
 q->top = ((void *)0);
 q->bottom = ((void *)0);
 pthread_mutexattr_init(&attr);
 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
 pthread_mutex_init(&(q->lock), &attr);
 pthread_mutexattr_destroy(&attr);
 pthread_cond_init(&(q->cond), ((void *)0));
 return q;
}

variable_types: struct queue { 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; } lock; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; unsigned long size; struct queue_entry { unsigned int operation; struct req_info { int fd; int type; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *clisa; unsigned int clilen; unsigned int id; unsigned short cmd; unsigned short flags; unsigned char *payload; unsigned long psize; void (*reply_mini)(struct req_info *req, unsigned int reply); void (*reply_err)(struct req_info *req, unsigned int reply); void (*reply_long)(struct req_info *req, unsigned int reply, unsigned char *val, unsigned long vsize); } *req; unsigned char *key; unsigned char *val; unsigned char *newval; unsigned long ksize; unsigned long vsize; unsigned long nvsize; struct queue_entry *prev; } *top; } *

variable_types: union pthread_mutexattr_t { char __size[4]; int __align; }

user_defined_types: struct queue { union pthread_mutex_t lock; union pthread_cond_t cond; unsigned long size; struct queue_entry *top; }

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

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long 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 queue_entry { unsigned int operation; struct req_info *req; unsigned char *key; unsigned char *val; unsigned char *newval; unsigned long ksize; unsigned long vsize; unsigned long nvsize; struct queue_entry *prev; }


decompiled_code:
int __fastcall queue_lock(pthread_mutex_t *a1)
{
return pthread_mutex_lock(a1);
}

canonical_decompiled_code:
int __fastcall func1(pthread_mutex_t *a1)
{
return pthread_mutex_lock(a1);
}

original_code:
void queue_lock(struct queue *q)
{
 pthread_mutex_lock(&(q->lock));
}

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }

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


decompiled_code:
int __fastcall queue_unlock(pthread_mutex_t *a1)
{
return pthread_mutex_unlock(a1);
}

canonical_decompiled_code:
int __fastcall func2(pthread_mutex_t *a1)
{
return pthread_mutex_unlock(a1);
}

original_code:
void queue_unlock(struct queue *q)
{
 pthread_mutex_unlock(&(q->lock));
}


decompiled_code:
int __fastcall queue_signal(__int64 a1)
{
return pthread_cond_signal((pthread_cond_t *)(a1 + 40));
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1)
{
return pthread_cond_signal((pthread_cond_t *)(a1 + 40));
}

original_code:
void queue_signal(struct queue *q)
{
 pthread_cond_signal(&(q->cond));
}


decompiled_code:
int __fastcall queue_timedwait(pthread_mutex_t *mutex, struct timespec *abstime)
{
return pthread_cond_timedwait((pthread_cond_t *)&mutex[1], mutex, abstime);
}

canonical_decompiled_code:
int __fastcall func4(pthread_mutex_t *mutex, struct timespec *abstime)
{
return pthread_cond_timedwait((pthread_cond_t *)&mutex[1], mutex, abstime);
}

original_code:
int queue_timedwait(struct queue *q, struct timespec *ts)
{
 return pthread_cond_timedwait(&(q->cond), &(q->lock), ts);
}

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


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

result = malloc(0x48uLL);
if ( result )
{
*(_DWORD *)result = 0;
result[2] = 0LL;
result[3] = 0LL;
result[4] = 0LL;
result[5] = 0LL;
result[6] = 0LL;
result[7] = 0LL;
result[8] = 0LL;
}
return result;
}

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

result = malloc(0x48uLL);
if ( result )
{
*(_DWORD *)result = 0;
result[2] = 0LL;
result[3] = 0LL;
result[4] = 0LL;
result[5] = 0LL;
result[6] = 0LL;
result[7] = 0LL;
result[8] = 0LL;
}
return result;
}

original_code:
struct queue_entry *queue_entry_create(void)
{
 struct queue_entry *e;
 e = malloc(sizeof(struct queue_entry));
 if (e == ((void *)0))
  return ((void *)0);
 e->operation = 0;
 e->key = ((void *)0);
 e->val = ((void *)0);
 e->newval = ((void *)0);
 e->ksize = 0;
 e->vsize = 0;
 e->nvsize = 0;
 e->prev = ((void *)0);
 return e;
}

canonical_original_code:
struct queue_entry *queue_entry_create()
{
 struct queue_entry *e;
 e = malloc(sizeof(struct queue_entry));
 if (e == ((void *)0))
  return ((void *)0);
 e->operation = 0;
 e->key = ((void *)0);
 e->val = ((void *)0);
 e->newval = ((void *)0);
 e->ksize = 0;
 e->vsize = 0;
 e->nvsize = 0;
 e->prev = ((void *)0);
 return e;
}

variable_types: struct queue_entry { unsigned int operation; struct req_info { int fd; int type; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *clisa; unsigned int clilen; unsigned int id; unsigned short cmd; unsigned short flags; unsigned char *payload; unsigned long psize; void (*reply_mini)(struct req_info *req, unsigned int reply); void (*reply_err)(struct req_info *req, unsigned int reply); void (*reply_long)(struct req_info *req, unsigned int reply, unsigned char *val, unsigned long vsize); } *req; unsigned char *key; unsigned char *val; unsigned char *newval; unsigned long ksize; unsigned long vsize; unsigned long nvsize; struct queue_entry *prev; } *


decompiled_code:
void __fastcall queue_entry_free(void *ptr)
{
__int64 v2; // rax
void *v3; // rdi
void *v4; // rdi
void *v5; // rdi

v2 = *((_QWORD *)ptr + 1);
if ( v2 )
{
free(*(void **)(v2 + 8));
free(*((void **)ptr + 1));
}
v3 = (void *)*((_QWORD *)ptr + 2);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 3);
if ( v4 )
free(v4);
v5 = (void *)*((_QWORD *)ptr + 4);
if ( v5 )
free(v5);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func6(void *ptr)
{
__int64 v2; // rax
void *v3; // rdi
void *v4; // rdi
void *v5; // rdi

v2 = *((_QWORD *)ptr + 1);
if ( v2 )
{
free(*(void **)(v2 + 8));
free(*((void **)ptr + 1));
}
v3 = (void *)*((_QWORD *)ptr + 2);
if ( v3 )
free(v3);
v4 = (void *)*((_QWORD *)ptr + 3);
if ( v4 )
free(v4);
v5 = (void *)*((_QWORD *)ptr + 4);
if ( v5 )
free(v5);
free(ptr);
}

original_code:
void queue_entry_free(struct queue_entry *e) {
 if (e->req) {
  free(e->req->clisa);
  free(e->req);
 }
 if (e->key)
  free(e->key);
 if (e->val)
  free(e->val);
 if (e->newval)
  free(e->newval);
 free(e);
 return;
}


decompiled_code:
__int64 __fastcall queue_get(_QWORD *a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = a1[13];
if ( result )
{
v2 = *(_QWORD *)(result + 64);
a1[13] = v2;
if ( !v2 )
a1[12] = 0LL;
--a1[11];
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(_QWORD *a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = a1[13];
if ( result )
{
v2 = *(_QWORD *)(result + 64);
a1[13] = v2;
if ( !v2 )
a1[12] = 0LL;
--a1[11];
}
return result;
}

original_code:
struct queue_entry *queue_get(struct queue *q)
{
 struct queue_entry *e, *t;
 if (q->bottom == ((void *)0))
  return ((void *)0);
 e = q->bottom;
 t = q->bottom->prev;
 q->bottom = t;
 if (t == ((void *)0)) {
  q->top = ((void *)0);
 }
 q->size -= 1;
 return e;
}

canonical_original_code:
struct queue_entry *queue_get(struct queue *q)
{
 struct queue_entry *e; struct queue_entry *t;
 if (q->bottom == ((void *)0))
  return ((void *)0);
 e = q->bottom;
 t = q->bottom->prev;
 q->bottom = t;
 if (t == ((void *)0)) {
  q->top = ((void *)0);
 }
 q->size -= 1;
 return e;
}

variable_types: struct queue { 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; } lock; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } cond; unsigned long size; struct queue_entry *top; } *

return_type: struct queue_entry { unsigned int operation; struct req_info { int fd; int type; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *clisa; unsigned int clilen; unsigned int id; unsigned short cmd; unsigned short flags; unsigned char *payload; unsigned long psize; void (*reply_mini)(struct req_info *req, unsigned int reply); void (*reply_err)(struct req_info *req, unsigned int reply); void (*reply_long)(struct req_info *req, unsigned int reply, unsigned char *val, unsigned long vsize); } *req; unsigned char *key; unsigned char *val; unsigned char *newval; unsigned long ksize; unsigned long vsize; unsigned long nvsize; struct queue_entry *prev; } *


decompiled_code:
void __fastcall queue_free(pthread_mutex_t *mutex)
{
void *i; // rdi

for ( i = (void *)queue_get(mutex); i; i = (void *)queue_get(mutex) )
queue_entry_free(i);
pthread_mutex_destroy(mutex);
free(mutex);
}

canonical_decompiled_code:
void __fastcall func9(pthread_mutex_t *mutex)
{
void *i; // rdi

for ( i = (void *)func8(mutex); i; i = (void *)func8(mutex) )
func6(i);
pthread_mutex_destroy(mutex);
free(mutex);
}

original_code:
void queue_free(struct queue *q)
{
 struct queue_entry *e;
 (void)0;
 e = queue_get(q);
 while (e != ((void *)0)) {
  queue_entry_free(e);
  e = queue_get(q);
 }
 (void)0;
 pthread_mutex_destroy(&(q->lock));
 free(q);
 return;
}


decompiled_code:
_BOOL8 __fastcall queue_isempty(__int64 a1)
{
return *(_QWORD *)(a1 + 88) == 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func10(__int64 a1)
{
return *(_QWORD *)(a1 + 88) == 0LL;
}

original_code:
int queue_isempty(struct queue *q)
{
 return (q->size == 0);
}


decompiled_code:
void __fastcall tcp_reply_mini(int *a1, unsigned int a2)
{
int v2; // er12
int v3; // eax
int v4[9]; // [rsp+0h] [rbp-24h] BYREF

if ( !settings[15] )
{
v2 = _bswap_32(0xCu);
v3 = _bswap_32(a2);
v4[0] = v2;
v4[1] = a1[5];
v4[2] = v3;
rep_send(a1, (__int64)v4, 0xCuLL);
}
}

canonical_decompiled_code:
void __fastcall func6(int *a1, unsigned int a2)
{
int v2; // er12
int v3; // eax
int v4[9]; // [rsp+0h] [rbp-24h] BYREF

if ( !settings[15] )
{
v2 = func3(0xCu);
v3 = func3(a2);
v4[0] = v2;
v4[1] = a1[5];
v4[2] = v3;
func5(a1, (__int64)v4, 0xCuLL);
}
}

original_code:
static void tcp_reply_mini(const struct req_info *req, uint32_t reply)
{
 uint32_t len;
 unsigned char minibuf[12];
 if (settings.passive)
  return;
 len = htonl(12);
 reply = htonl(reply);
 memcpy(minibuf, &len, 4);
 memcpy(minibuf + 4, &(req->id), 4);
 memcpy(minibuf + 8, &reply, 4);
 rep_send(req, minibuf, 12);
 return;
}

canonical_original_code:
static void tcp_reply_mini(struct req_info *req, unsigned int reply)
{
 unsigned int len;
 unsigned char minibuf[12];
 if (settings.passive)
  return;
 len = htonl(12);
 reply = htonl(reply);
 memcpy(minibuf, &len, 4);
 memcpy(minibuf + 4, &(req->id), 4);
 memcpy(minibuf + 8, &reply, 4);
 rep_send(req, minibuf, 12);
 return;
}

variable_types: struct req_info { int fd; int type; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *clisa; unsigned int clilen; unsigned int id; unsigned short cmd; unsigned short flags; unsigned char *payload; unsigned long psize; void (*reply_mini)(struct req_info *req, unsigned int reply); void (*reply_err)(struct req_info *req, unsigned int reply); void (*reply_long)(struct req_info *req, unsigned int reply, unsigned char *val, unsigned long vsize); } *

variable_types:
reply
unsigned int

variable_types:
len
unsigned int

variable_types:
minibuf
unsigned char [12]


decompiled_code:
void __fastcall tcp_reply_long(int *a1, unsigned int a2, const void *a3, size_t a4)
{
int v6; // er15
_DWORD *v7; // rbx

if ( a3 )
{
v6 = _bswap_32(a2);
v7 = malloc(a4 + 16);
*v7 = _bswap_32((int)a4 + 16);
v7[1] = a1[5];
v7[2] = v6;
v7[3] = _bswap_32(a4);
memcpy(v7 + 4, a3, a4);
rep_send(a1, (__int64)v7, a4 + 16);
free(v7);
}
else
{
tcp_reply_mini(a1, a2);
}
}

canonical_decompiled_code:
void __fastcall func7(int *a1, unsigned int a2, const void *a3, size_t a4)
{
int v6; // er15
_DWORD *v7; // rbx

if ( a3 )
{
v6 = func3(a2);
v7 = malloc(a4 + 16);
*v7 = func3((int)a4 + 16);
v7[1] = a1[5];
v7[2] = v6;
v7[3] = func3(a4);
memcpy(v7 + 4, a3, a4);
func5(a1, (__int64)v7, a4 + 16);
free(v7);
}
else
{
func6(a1, a2);
}
}

original_code:
static void tcp_reply_long(const struct req_info *req, uint32_t reply,
   unsigned char *val, size_t vsize)
{
 if (val == ((void *)0)) {
  tcp_reply_mini(req, reply);
 } else {
  unsigned char *buf;
  size_t bsize;
  uint32_t t;
  reply = htonl(reply);
  bsize = 4 + 4 + 4 + 4 + vsize;
  buf = malloc(bsize);
  t = htonl(bsize);
  memcpy(buf, &t, 4);
  memcpy(buf + 4, &(req->id), 4);
  memcpy(buf + 8, &reply, 4);
  t = htonl(vsize);
  memcpy(buf + 12, &t, 4);
  memcpy(buf + 16, val, vsize);
  rep_send(req, buf, bsize);
  free(buf);
 }
 return;
}

canonical_original_code:
static void tcp_reply_long(struct req_info *req, unsigned int reply, unsigned char *val, unsigned long vsize)
{
 if (val == ((void *)0)) {
  tcp_reply_mini(req, reply);
 } else {
  unsigned char *buf;
  unsigned long bsize;
  unsigned int t;
  reply = htonl(reply);
  bsize = 4 + 4 + 4 + 4 + vsize;
  buf = malloc(bsize);
  t = htonl(bsize);
  memcpy(buf, &t, 4);
  memcpy(buf + 4, &(req->id), 4);
  memcpy(buf + 8, &reply, 4);
  t = htonl(vsize);
  memcpy(buf + 12, &t, 4);
  memcpy(buf + 16, val, vsize);
  rep_send(req, buf, bsize);
  free(buf);
 }
 return;
}

variable_types:
val
unsigned char *

variable_types:
vsize
unsigned long

variable_types:
buf
unsigned char *

variable_types:
bsize
unsigned long

variable_types:
t
unsigned int


decompiled_code:
void __fastcall tcp_reply_err(int *a1, unsigned int a2)
{
rep_send_error(a1, a2);
}

canonical_decompiled_code:
void __fastcall func8(int *a1, unsigned int a2)
{
func4(a1, a2);
}

original_code:
static void tcp_reply_err(const struct req_info *req, uint32_t reply)
{
 rep_send_error(req, reply);
}

canonical_original_code:
static void tcp_reply_err(struct req_info *req, unsigned int reply)
{
 rep_send_error(req, reply);
}


decompiled_code:
__int64 tcp_init()
{
int v0; // eax
unsigned int v1; // ebx
int buf; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF
int optval[3]; // [rsp+2Ch] [rbp-Ch] BYREF

optval[0] = inet_pton(2, *(const char **)&settings[2], &buf);
if ( optval[0] <= 0 )
return (unsigned int)-1;
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = buf;
*(_WORD *)addr.sa_data = _bswap_16(settings[4]);
v0 = socket(2, 1, 0);
v1 = v0;
if ( v0 < 0 )
return (unsigned int)-1;
optval[0] = 1;
if ( setsockopt(v0, 1, 2, optval, 4u) < 0
|| (optval[0] = bind(v1, &addr, 0x10u), optval[0] < 0)
|| (optval[0] = listen(v1, 1024), optval[0] < 0)
|| (optval[0] = 1, setsockopt(v1, 6, 1, optval, 4u) < 0) )
{
close(v1);
v1 = -1;
}
return v1;
}

canonical_decompiled_code:
__int64 func11()
{
int v0; // eax
unsigned int v1; // ebx
int buf; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF
int optval[3]; // [rsp+2Ch] [rbp-Ch] BYREF

optval[0] = inet_pton(2, *(const char **)&settings[2], &buf);
if ( optval[0] <= 0 )
return (unsigned int)-1;
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = buf;
*(_WORD *)addr.sa_data = func1(settings[4]);
v0 = socket(2, 1, 0);
v1 = v0;
if ( v0 < 0 )
return (unsigned int)-1;
optval[0] = 1;
if ( setsockopt(v0, 1, 2, optval, 4u) < 0
|| (optval[0] = bind(v1, &addr, 0x10u), optval[0] < 0)
|| (optval[0] = listen(v1, 1024), optval[0] < 0)
|| (optval[0] = 1, setsockopt(v1, 6, 1, optval, 4u) < 0) )
{
close(v1);
v1 = -1;
}
return v1;
}

original_code:
int tcp_init(void)
{
 int fd, rv;
 struct sockaddr_in srvsa;
 struct in_addr ia;
 rv = inet_pton(2, settings.tcp_addr, &ia);
 if (rv <= 0)
  return -1;
 srvsa.sin_family = 2;
 srvsa.sin_addr.s_addr = ia.s_addr;
 srvsa.sin_port = htons(settings.tcp_port);
 fd = socket(2, SOCK_STREAM, 0);
 if (fd < 0)
  return -1;
 rv = 1;
 if (setsockopt(fd, 1, 2, &rv, sizeof(rv)) < 0 ) {
  close(fd);
  return -1;
 }
 rv = bind(fd, (struct sockaddr *) &srvsa, sizeof(srvsa));
 if (rv < 0) {
  close(fd);
  return -1;
 }
 rv = listen(fd, 1024);
 if (rv < 0) {
  close(fd);
  return -1;
 }
 rv = 1;
 if (setsockopt(fd, IPPROTO_TCP, 1, &rv, sizeof(rv)) < 0 ) {
  close(fd);
  return -1;
 }
 return fd;
}

canonical_original_code:
int tcp_init()
{
 int fd; int rv;
 struct sockaddr_in srvsa;
 struct in_addr ia;
 rv = inet_pton(2, settings.tcp_addr, &ia);
 if (rv <= 0)
  return -1;
 srvsa.sin_family = 2;
 srvsa.sin_addr.s_addr = ia.s_addr;
 srvsa.sin_port = htons(settings.tcp_port);
 fd = socket(2, SOCK_STREAM, 0);
 if (fd < 0)
  return -1;
 rv = 1;
 if (setsockopt(fd, 1, 2, &rv, sizeof(rv)) < 0 ) {
  close(fd);
  return -1;
 }
 rv = bind(fd, (struct sockaddr *) &srvsa, sizeof(srvsa));
 if (rv < 0) {
  close(fd);
  return -1;
 }
 rv = listen(fd, 1024);
 if (rv < 0) {
  close(fd);
  return -1;
 }
 rv = 1;
 if (setsockopt(fd, IPPROTO_TCP, 1, &rv, sizeof(rv)) < 0 ) {
  close(fd);
  return -1;
 }
 return fd;
}

variable_types: fd int

variable_types: rv int

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

variable_types:
ia
struct in_addr { unsigned int s_addr; }


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

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

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

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


decompiled_code:
void __fastcall rep_send_error(int *a1, unsigned int a2)
{
int v2; // er13
int v3; // er12
int v4; // eax
int v5[14]; // [rsp+0h] [rbp-38h] BYREF

if ( !settings[15] )
{
v2 = _bswap_32(0x10u);
v3 = _bswap_32(0x800u);
v4 = _bswap_32(a2);
v5[0] = v2;
v5[1] = a1[5];
v5[2] = v3;
v5[3] = v4;
if ( send(*a1, v5, 0x10uLL, 0) < 0 )
errlog("rep_send_error() failed");
}
}

canonical_decompiled_code:
void __fastcall func4(int *a1, unsigned int a2)
{
int v2; // er13
int v3; // er12
int v4; // eax
int v5[14]; // [rsp+0h] [rbp-38h] BYREF

if ( !settings[15] )
{
v2 = func3(0x10u);
v3 = func3(0x800u);
v4 = func3(a2);
v5[0] = v2;
v5[1] = a1[5];
v5[2] = v3;
v5[3] = v4;
if ( send(*a1, v5, 0x10uLL, 0) < 0 )
errlog("rep_send_error() failed");
}
}

original_code:
static void rep_send_error(const struct req_info *req, const unsigned int code)
{
 uint32_t l, r, c;
 ssize_t rv;
 unsigned char minibuf[4 * 4];
 if (settings.passive)
  return;
 l = htonl(4 + 4 + 4 + 4);
 r = htonl(0x800);
 c = htonl(code);
 memcpy(minibuf, &l, 4);
 memcpy(minibuf + 4, &(req->id), 4);
 memcpy(minibuf + 8, &r, 4);
 memcpy(minibuf + 12, &c, 4);
 rv = send(req->fd, minibuf, 4 * 4, 0);
 if (rv < 0) {
  errlog("rep_send_error() failed");
 }
}

canonical_original_code:
static void rep_send_error(struct req_info *req, unsigned int code)
{
 unsigned int l; unsigned int r; unsigned int c;
 long rv;
 unsigned char minibuf[-1];
 if (settings.passive)
  return;
 l = htonl(4 + 4 + 4 + 4);
 r = htonl(0x800);
 c = htonl(code);
 memcpy(minibuf, &l, 4);
 memcpy(minibuf + 4, &(req->id), 4);
 memcpy(minibuf + 8, &r, 4);
 memcpy(minibuf + 12, &c, 4);
 rv = send(req->fd, minibuf, 4 * 4, 0);
 if (rv < 0) {
  errlog("rep_send_error() failed");
 }
}

variable_types:
code
unsigned int

variable_types:
l
unsigned int

variable_types:
r
unsigned int

variable_types:
c
unsigned int

variable_types: rv long

variable_types:
minibuf
unsigned char [-1]


decompiled_code:
_BOOL8 __fastcall xtdb_close(void *ptr)
{
int v1; // ebp

v1 = tdb_close(*(_QWORD *)ptr);
free(ptr);
return v1 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(void *ptr)
{
int v1; // ebp

v1 = tdb_close(*(_QWORD *)ptr);
free(ptr);
return v1 == 0;
}

original_code:
int xtdb_close(struct db_conn *db)
{
 int rv;
 rv = tdb_close(db->conn);
 free(db);
 return rv == 0;
}

variable_types: struct db_conn { void *conn; int (*set)(struct db_conn *db, unsigned char *key, unsigned long ksize, unsigned char *val, unsigned long vsize); int (*get)(struct db_conn *db, unsigned char *key, unsigned long ksize, unsigned char *val, unsigned long *vsize); int (*del)(struct db_conn *db, unsigned char *key, unsigned long ksize); int (*firstkey)(struct db_conn *db, unsigned char *key, unsigned long *ksize); int (*nextkey)(struct db_conn *db, unsigned char *key, unsigned long ksize, unsigned char *nextkey, unsigned long *nksize); int (*close)(struct db_conn *db); } *


decompiled_code:
_BOOL8 __fastcall xtdb_set(_QWORD *a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
return (unsigned int)tdb_store(*a1, a2, a3, a4, a5, 1LL) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(_QWORD *a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5)
{
return (unsigned int)tdb_store(*a1, a2, a3, a4, a5, 1LL) == 0;
}

original_code:
int xtdb_set(struct db_conn *db, const unsigned char *key, size_t ksize,
  unsigned char *val, size_t vsize)
{
 TDB_DATA k, v;
 k.dptr = (unsigned char *) key;
 k.dsize = ksize;
 v.dptr = (unsigned char *) val;
 v.dsize = vsize;
 return tdb_store(db->conn, k, v, 1) == 0;
}

canonical_original_code:
int xtdb_set(struct db_conn *db, unsigned char *key, unsigned long ksize, unsigned char *val, unsigned long vsize)
{
 struct TDB_DATA k; struct TDB_DATA v;
 k.dptr = (unsigned char *) key;
 k.dsize = ksize;
 v.dptr = (unsigned char *) val;
 v.dsize = vsize;
 return tdb_store(db->conn, k, v, 1) == 0;
}

variable_types:
key
unsigned char *

variable_types:
ksize
unsigned long

variable_types: struct TDB_DATA { unsigned char *dptr; unsigned long dsize; }

user_defined_types: struct db_conn { void *conn; int (*set)(struct db_conn *db, unsigned char *key, unsigned long ksize, unsigned char *val, unsigned long vsize); int (*get)(struct db_conn *db, unsigned char *key, unsigned long ksize, unsigned char *val, unsigned long *vsize); int (*del)(struct db_conn *db, unsigned char *key, unsigned long ksize); int (*firstkey)(struct db_conn *db, unsigned char *key, unsigned long *ksize); int (*nextkey)(struct db_conn *db, unsigned char *key, unsigned long ksize, unsigned char *nextkey, unsigned long *nksize); int (*close)(struct db_conn *db); }


decompiled_code:
__int64 __fastcall xtdb_get(_QWORD *a1, __int64 a2, __int64 a3, void *a4, size_t *a5)
{
__int64 v7; // rax
size_t v8; // rdx
void *v9; // rbx
__int64 result; // rax

v7 = tdb_fetch(*a1);
if ( !v7 )
return 0LL;
v9 = (void *)v7;
result = 0LL;
if ( *a5 >= v8 )
{
*a5 = v8;
memcpy(a4, v9, v8);
free(v9);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1, __int64 a2, __int64 a3, void *a4, size_t *a5)
{
__int64 v7; // rax
size_t v8; // rdx
void *v9; // rbx
__int64 result; // rax

v7 = tdb_fetch(*a1);
if ( !v7 )
return 0LL;
v9 = (void *)v7;
result = 0LL;
if ( *a5 >= v8 )
{
*a5 = v8;
memcpy(a4, v9, v8);
free(v9);
result = 1LL;
}
return result;
}

original_code:
int xtdb_get(struct db_conn *db, const unsigned char *key, size_t ksize,
  unsigned char *val, size_t *vsize)
{
 TDB_DATA k, v;
 k.dptr = (unsigned char *) key;
 k.dsize = ksize;
 v = tdb_fetch(db->conn, k);
 if (v.dptr == ((void *)0))
  return 0;
 if (v.dsize > *vsize)
  return 0;
 *vsize = v.dsize;
 memcpy(val, v.dptr, v.dsize);
 free(v.dptr);
 return 1;
}

canonical_original_code:
int xtdb_get(struct db_conn *db, unsigned char *key, unsigned long ksize, unsigned char *val, unsigned long *vsize)
{
 struct TDB_DATA k; struct TDB_DATA v;
 k.dptr = (unsigned char *) key;
 k.dsize = ksize;
 v = tdb_fetch(db->conn, k);
 if (v.dptr == ((void *)0))
  return 0;
 if (v.dsize > *vsize)
  return 0;
 *vsize = v.dsize;
 memcpy(val, v.dptr, v.dsize);
 free(v.dptr);
 return 1;
}

variable_types:
val
unsigned char *

variable_types:
vsize
unsigned long *

user_defined_types: struct TDB_DATA { unsigned char *dptr; unsigned long dsize; }


decompiled_code:
_BOOL8 __fastcall xtdb_del(_QWORD *a1)
{
return (unsigned int)tdb_delete(*a1) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(_QWORD *a1)
{
return (unsigned int)tdb_delete(*a1) == 0;
}

original_code:
int xtdb_del(struct db_conn *db, const unsigned char *key, size_t ksize)
{
 TDB_DATA k;
 k.dptr = (unsigned char *) key;
 k.dsize = ksize;
 return tdb_delete(db->conn, k) == 0;
}

canonical_original_code:
int xtdb_del(struct db_conn *db, unsigned char *key, unsigned long ksize)
{
 struct TDB_DATA k;
 k.dptr = (unsigned char *) key;
 k.dsize = ksize;
 return tdb_delete(db->conn, k) == 0;
}


decompiled_code:
__int64 __fastcall xtdb_firstkey(_QWORD *a1, void *a2, size_t *a3)
{
size_t v4; // rdx
void *v5; // rbx
__int64 result; // rax

v5 = (void *)tdb_firstkey(*a1);
result = 0LL;
if ( v5 )
{
*a3 = v4;
memcpy(a2, v5, v4);
free(v5);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1, void *a2, size_t *a3)
{
size_t v4; // rdx
void *v5; // rbx
__int64 result; // rax

v5 = (void *)tdb_firstkey(*a1);
result = 0LL;
if ( v5 )
{
*a3 = v4;
memcpy(a2, v5, v4);
free(v5);
result = 1LL;
}
return result;
}

original_code:
int xtdb_firstkey(struct db_conn *db, unsigned char *key, size_t *ksize)
{
 TDB_DATA k;
 k = tdb_firstkey(db->conn);
 if (k.dptr == ((void *)0))
  return 0;
 *ksize = k.dsize;
 memcpy(key, k.dptr, k.dsize);
 free(k.dptr);
 return 1;
}

canonical_original_code:
int xtdb_firstkey(struct db_conn *db, unsigned char *key, unsigned long *ksize)
{
 struct TDB_DATA k;
 k = tdb_firstkey(db->conn);
 if (k.dptr == ((void *)0))
  return 0;
 *ksize = k.dsize;
 memcpy(key, k.dptr, k.dsize);
 free(k.dptr);
 return 1;
}

variable_types:
ksize
unsigned long *