main

repo: mikenorthorp/binaryBuddy

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const void *v3; // rax
const void *v4; // rax
const void *v5; // rax
const void *v6; // rbp

puts("Running basic tests, you can run your own these are just examples..");
puts("Initializing memory to 2048bytes");
start_memory(2048LL, argv);
puts("The following test the get memory function");
v3 = (const void *)get_memory(2LL);
printf("The address for 2bytes of memory is %p\n", v3);
printf("Size is %d", 4LL);
v4 = (const void *)get_memory(4LL);
printf(" and address is %p\n", v4);
printf("Size is %d", 4LL);
v5 = (const void *)get_memory(4LL);
printf(" and address is %p\n", v5);
printf("Size is %d", 4LL);
v6 = (const void *)get_memory(4LL);
printf(" and address is %p\n", v6);
release_memory(v6);
puts("Testing release memory on remaining stuff, should display leaks and free everything");
end_memory();
puts("End of testing");
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const void *v3; // rax
const void *v4; // rax
const void *v5; // rax
const void *v6; // rbp

puts("Running basic tests, you can run your own these are just examples..");
puts("Initializing memory to 2048bytes");
func7(2048LL, argv);
puts("The following test the get memory function");
v3 = (const void *)get_memory(2LL);
printf("The address for 2bytes of memory is %p\n", v3);
printf("Size is %d", 4LL);
v4 = (const void *)get_memory(4LL);
printf(" and address is %p\n", v4);
printf("Size is %d", 4LL);
v5 = (const void *)get_memory(4LL);
printf(" and address is %p\n", v5);
printf("Size is %d", 4LL);
v6 = (const void *)get_memory(4LL);
printf(" and address is %p\n", v6);
func8(v6);
puts("Testing release memory on remaining stuff, should display leaks and free everything");
func9();
puts("End of testing");
return 1;
}

original_code:
int main( int argc, char **argv )
{
    printf("Running basic tests, you can run your own these are just examples..\n");
    printf("Initializing memory to 2048bytes\n");
    start_memory(2048);
    printf("The following test the get memory function\n");
    printf("The address for 2bytes of memory is %p\n", get_memory(2));
    int testSize = sizeof(testStruct);
    printf("Size is %d", testSize);
    testStruct *test1 = get_memory(testSize);
    printf(" and address is %p\n", (void *)test1);
    printf("Size is %d", testSize);
    testStruct *test2 = get_memory(testSize);
    printf(" and address is %p\n", (void *)test2);
    printf("Size is %d", testSize);
    testStruct *test3 = get_memory(testSize);
    printf(" and address is %p\n", (void *)test3);
    release_memory(test3);
    printf("Testing release memory on remaining stuff, should display leaks and free everything\n");
    end_memory();
    printf("End of testing\n");
    return 1;
}

canonical_original_code:
int main(int argc, char **argv)
{
    printf("Running basic tests, you can run your own these are just examples..\n");
    printf("Initializing memory to 2048bytes\n");
    start_memory(2048);
    printf("The following test the get memory function\n");
    printf("The address for 2bytes of memory is %p\n", get_memory(2));
    int testSize = sizeof(testStruct);
    printf("Size is %d", testSize);
    struct testStruct *test1 = get_memory(testSize);
    printf(" and address is %p\n", (void *)test1);
    printf("Size is %d", testSize);
    struct testStruct *test2 = get_memory(testSize);
    printf(" and address is %p\n", (void *)test2);
    printf("Size is %d", testSize);
    struct testStruct *test3 = get_memory(testSize);
    printf(" and address is %p\n", (void *)test3);
    release_memory(test3);
    printf("Testing release memory on remaining stuff, should display leaks and free everything\n");
    end_memory();
    printf("End of testing\n");
    return 1;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: testSize int


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

mallocPointer = malloc(a1);
if ( mallocPointer )
{
totalMemory = a1;
result = 1LL;
freeMemory = a1;
}
else
{
puts("Could not allocate memory");
result = 0LL;
}
return result;
}

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

mallocPointer = malloc(a1);
if ( mallocPointer )
{
totalMemory = a1;
result = 1LL;
freeMemory = a1;
}
else
{
puts("Could not allocate memory");
result = 0LL;
}
return result;
}

original_code:
int start_memory(int size)
{
    if ((mallocPointer = malloc(size)))
    {
        totalMemory = size;
        freeMemory = totalMemory;
        return 1;
    }
    else
    {
        printf("Could not allocate memory\n");
        return 0;
    }
}

variable_types: size int

return_type: int


decompiled_code:
int __fastcall release_memory(_WORD *a1)
{
*a1 = 0;
return printf("Released memory at %p\n", a1);
}

canonical_decompiled_code:
int __fastcall func8(_WORD *a1)
{
*a1 = 0;
return printf("Released memory at %p\n", a1);
}

original_code:
void release_memory( void *p )
{
    ((buddyNode *)p)->isUsed = 0;
    printf("Released memory at %p\n", p);
}

canonical_original_code:
void release_memory(void *p)
{
    ((struct buddyNode *)p)->isUsed = 0;
    printf("Released memory at %p\n", p);
}

variable_types:
p
void *

return_type: void

user_defined_types: struct buddyNode { short isUsed; short side; }


decompiled_code:
void end_memory()
{
double v0; // xmm0_8
__int64 v1; // r13
char *v2; // rdi
int v3; // er14
__int64 v4; // rbp
int v5; // ebx
char *v6; // r12

v0 = pow(2.0, (double)depth);
v1 = lowestBlockSize;
v2 = (char *)mallocPointer;
v3 = (int)v0;
if ( (int)v0 )
{
v4 = 0LL;
v5 = 0;
do
{
while ( 1 )
{
v6 = &v2[v4];
if ( *(_WORD *)&v2[v4] == 1 )
break;
++v5;
v4 += v1;
if ( v3 == v5 )
goto LABEL_6;
}
++v5;
printf("You have a leak at block %p\n", &v2[v4]);
printf("Releasing memory at block %p\n", v6);
v4 += v1;
v2 = (char *)mallocPointer;
*(_WORD *)v6 = 0;
}
while ( v3 != v5 );
}
LABEL_6:
free(v2);
}

canonical_decompiled_code:
void func9()
{
double v0; // xmm0_8
__int64 v1; // r13
char *v2; // rdi
int v3; // er14
__int64 v4; // rbp
int v5; // ebx
char *v6; // r12

v0 = pow(2.0, (double)depth);
v1 = lowestBlockSize;
v2 = (char *)mallocPointer;
v3 = (int)v0;
if ( (int)v0 )
{
v4 = 0LL;
v5 = 0;
do
{
while ( 1 )
{
v6 = &v2[v4];
if ( *(_WORD *)&v2[v4] == 1 )
break;
++v5;
v4 += v1;
if ( v3 == v5 )
goto LABEL_6;
}
++v5;
printf("You have a leak at block %p\n", &v2[v4]);
printf("Releasing memory at block %p\n", v6);
v4 += v1;
v2 = (char *)mallocPointer;
*(_WORD *)v6 = 0;
}
while ( v3 != v5 );
}
LABEL_6:
free(v2);
}

original_code:
void end_memory(void)
{
    int partitionSizeLowest = (int)pow(2.0, ((double)depth));
    int blockSize = lowestBlockSize;
    void *pointer;
    int i;
    for (i = 0; i < partitionSizeLowest; i++)
    {
        pointer = (mallocPointer + (i * (blockSize)));
        if (((buddyNode *)pointer)->isUsed == 1)
        {
            printf("You have a leak at block %p\n", pointer);
            printf("Releasing memory at block %p\n", pointer);
            ((buddyNode *)pointer)->isUsed = 0;
        }
    }
    free(mallocPointer);
}

canonical_original_code:
void end_memory()
{
    int partitionSizeLowest = (int)pow(2.0, ((double)depth));
    int blockSize = lowestBlockSize;
    void *pointer;
    int i;
    for (i = 0; i < partitionSizeLowest; i++)
    {
        pointer = (mallocPointer + (i * (blockSize)));
        if (((struct buddyNode *)pointer)->isUsed == 1)
        {
            printf("You have a leak at block %p\n", pointer);
            printf("Releasing memory at block %p\n", pointer);
            ((struct buddyNode *)pointer)->isUsed = 0;
        }
    }
    free(mallocPointer);
}

variable_types: partitionSizeLowest int

variable_types: blockSize int

variable_types:
pointer
void *

variable_types: i int


decompiled_code:
char *__fastcall schema_add(int a1, int a2, const char *a3, int a4)
{
char *v6; // r14
int v7; // ebx
void *v8; // rdi
char *v9; // rax
__int64 v10; // rbp
char *result; // rax
void *v12; // rdx

if ( a1 < 0 )
_assert_fail("type >= 0", "schema.c", 0x12u, "schema_add");
v6 = (char *)attrs + 16 * a1;
v7 = *(_DWORD *)v6;
if ( !*(_DWORD *)v6 )
{
*(_DWORD *)v6 = 20;
v12 = malloc(0x140uLL);
*((_QWORD *)v6 + 1) = v12;
memset(v12, 0, 0x140uLL);
v7 = *(_DWORD *)v6;
}
if ( a2 >= v7 )
{
do
{
v8 = (void *)*((_QWORD *)v6 + 1);
*(_DWORD *)v6 = 2 * v7;
v9 = (char *)realloc(v8, 32LL * v7);
*((_QWORD *)v6 + 1) = v9;
memset(&v9[16 * v7], 0, 16LL * v7);
v7 = *(_DWORD *)v6;
}
while ( *(_DWORD *)v6 <= a2 );
}
v10 = *((_QWORD *)v6 + 1) + 16LL * a2;
result = strdup(a3);
*(_DWORD *)(v10 + 8) = a4;
*(_QWORD *)v10 = result;
return result;
}

canonical_decompiled_code:
char *__fastcall func0(int a1, int a2, const char *a3, int a4)
{
char *v6; // r14
int v7; // ebx
void *v8; // rdi
char *v9; // rax
__int64 v10; // rbp
char *result; // rax
void *v12; // rdx

if ( a1 < 0 )
_assert_fail("type >= 0", "schema.c", 0x12u, "schema_add");
v6 = (char *)attrs + 16 * a1;
v7 = *(_DWORD *)v6;
if ( !*(_DWORD *)v6 )
{
*(_DWORD *)v6 = 20;
v12 = malloc(0x140uLL);
*((_QWORD *)v6 + 1) = v12;
memset(v12, 0, 0x140uLL);
v7 = *(_DWORD *)v6;
}
if ( a2 >= v7 )
{
do
{
v8 = (void *)*((_QWORD *)v6 + 1);
*(_DWORD *)v6 = 2 * v7;
v9 = (char *)realloc(v8, 32LL * v7);
*((_QWORD *)v6 + 1) = v9;
memset(&v9[16 * v7], 0, 16LL * v7);
v7 = *(_DWORD *)v6;
}
while ( *(_DWORD *)v6 <= a2 );
}
v10 = *((_QWORD *)v6 + 1) + 16LL * a2;
result = strdup(a3);
*(_DWORD *)(v10 + 8) = a4;
*(_QWORD *)v10 = result;
return result;
}

original_code:
static void schema_add(int type, int attr, char *name, int objtype)
{
 attrs_t *a;
 ((void) sizeof ((type >= 0) ? 1 : 0), __extension__ ({ if (type >= 0) ; else __assert_fail ("type >= 0", "schema.c", 18, __extension__ __PRETTY_FUNCTION__); }));
 if (attrs[type].n == 0) {
  attrs[type].n = 20;
  attrs[type].a = (attr_t *) malloc( 20*sizeof(const attr_t) );
  memset( attrs[type].a, 0, 20*sizeof(const attr_t) );
 }
 a = &attrs[type];
 while (attr >= a->n) {
  int osz = a->n;
  a->n *= 2;
  a->a = (attr_t *) realloc( a->a, a->n*sizeof(const attr_t) );
  memset( a->a+osz, 0, osz*sizeof(const attr_t) );
 }
 a->a[attr].name = strdup(name);
 a->a[attr].objtype = objtype;
 return;
}

canonical_original_code:
static void schema_add(int type, int attr, char *name, int objtype)
{
 struct attrs_t *a;
 ((void) sizeof ((type >= 0) ? 1 : 0), __extension__ ({ if (type >= 0) ; else __assert_fail ("type >= 0", "schema.c", 18, __extension__ __PRETTY_FUNCTION__); }));
 if (attrs[type].n == 0) {
  attrs[type].n = 20;
  attrs[type].a = (struct attr_t *) malloc( 20*sizeof(struct attr_t) );
  memset( attrs[type].a, 0, 20*sizeof(struct attr_t) );
 }
 a = &attrs[type];
 while (attr >= a->n) {
  int osz = a->n;
  a->n *= 2;
  a->a = (struct attr_t *) realloc( a->a, a->n*sizeof(struct attr_t) );
  memset( a->a+osz, 0, osz*sizeof(struct attr_t) );
 }
 a->a[attr].name = strdup(name);
 a->a[attr].objtype = objtype;
 return;
}

variable_types: type int

variable_types: attr int

variable_types:
name
char *

variable_types: objtype int

variable_types: struct attrs_t { int n; struct attr_t { char *name; int objtype; } *a; } *

variable_types: osz int

user_defined_types: struct attrs_t { int n; struct attr_t *a; }


decompiled_code:
void load_schema()
{
char *v0; // rdi

if ( !loaded_1 )
{
loaded_1 = 1;
v0 = getenv("TIVO_SCHEMA");
if ( !v0 || !(unsigned int)preload_schema_file(v0) )
preload_schema_isra_0();
}
}

canonical_decompiled_code:
void func3()
{
char *v0; // rdi

if ( !loaded_1 )
{
loaded_1 = 1;
v0 = getenv("TIVO_SCHEMA");
if ( !v0 || !(unsigned int)func1(v0) )
func2();
}
}

original_code:
static void load_schema(void)
{
 static int loaded;
 char *filename;
 if (loaded) return;
 loaded = 1;
 filename = getenv("TIVO_SCHEMA");
 if (filename && preload_schema_file(filename)) return;
 preload_schema();
}

canonical_original_code:
static void load_schema()
{
 static int loaded;
 char *filename;
 if (loaded) return;
 loaded = 1;
 filename = getenv("TIVO_SCHEMA");
 if (filename && preload_schema_file(filename)) return;
 preload_schema();
}

variable_types: loaded int

variable_types:
filename
char *


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

if ( !types )
load_schema();
result = 0LL;
if ( ntypes > a1 )
result = *((_QWORD *)types + a1);
return result;
}

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

if ( !types )
func3();
result = 0LL;
if ( ntypes > a1 )
result = *((_QWORD *)types + a1);
return result;
}

original_code:
const char *schema_type(int type)
{
 if (!types) load_schema();
 return (type>=ntypes) ? 0 : types[type];
}

return_type: char *


decompiled_code:
const char *__fastcall schema_attrib(int a1, int a2)
{
const char *result; // rax
char *v3; // rbx

schema_type(a1);
result = "UNKNOWN";
if ( ntypes > a1 )
{
v3 = (char *)attrs + 16 * a1;
if ( a2 < *(_DWORD *)v3 )
{
result = *(const char **)(*((_QWORD *)v3 + 1) + 16LL * a2);
if ( !result )
result = "UNKNOWN";
}
}
return result;
}

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

func4(a1);
result = "UNKNOWN";
if ( ntypes > a1 )
{
v3 = (char *)attrs + 16 * a1;
if ( a2 < *(_DWORD *)v3 )
{
result = *(const char **)(*((_QWORD *)v3 + 1) + 16LL * a2);
if ( !result )
result = "UNKNOWN";
}
}
return result;
}

original_code:
const char *schema_attrib(int type, int attr)
{
 attrs_t a;
 (void) schema_type(type);
 if (type >= ntypes) {
  return "UNKNOWN";
 }
 a = attrs[type];
 if (attr >= a.n) {
  return "UNKNOWN";
 }
 if (!a.a[attr].name) {
  return "UNKNOWN";
 }
 return a.a[attr].name;
}

canonical_original_code:
char *schema_attrib(int type, int attr)
{
 struct attrs_t a;
 (void) schema_type(type);
 if (type >= ntypes) {
  return "UNKNOWN";
 }
 a = attrs[type];
 if (attr >= a.n) {
  return "UNKNOWN";
 }
 if (!a.a[attr].name) {
  return "UNKNOWN";
 }
 return a.a[attr].name;
}

variable_types: struct attrs_t { int n; struct attr_t { char *name; int objtype; } *a; }


decompiled_code:
char *get_ipaddr()
{
int v0; // eax
int v1; // er8
char *result; // rax
__int128 v3; // [rsp+0h] [rbp-38h] BYREF
struct in_addr in[4]; // [rsp+10h] [rbp-28h]
__int64 v5; // [rsp+20h] [rbp-18h]

v3 = 0LL;
v5 = 0LL;
*(_OWORD *)&in[0].s_addr = 0LL;
v0 = socket(2, 1, 0);
if ( v0 < 0 )
{
perror("socket()");
result = 0LL;
}
else
{
LOWORD(in[0].s_addr) = 2;
strcpy((char *)&v3, "eth0");
v1 = ioctl(v0, 0x8915uLL, &v3);
result = 0LL;
if ( !v1 )
result = inet_ntoa(in[1]);
}
return result;
}

canonical_decompiled_code:
char *func1()
{
int v0; // eax
int v1; // er8
char *result; // rax
__int128 v3; // [rsp+0h] [rbp-38h] BYREF
struct in_addr in[4]; // [rsp+10h] [rbp-28h]
__int64 v5; // [rsp+20h] [rbp-18h]

v3 = 0LL;
v5 = 0LL;
*(_OWORD *)&in[0].s_addr = 0LL;
v0 = socket(2, 1, 0);
if ( v0 < 0 )
{
perror("socket()");
result = 0LL;
}
else
{
LOWORD(in[0].s_addr) = 2;
strcpy((char *)&v3, "eth0");
v1 = ioctl(v0, 0x8915uLL, &v3);
result = 0LL;
if ( !v1 )
result = inet_ntoa(in[1]);
}
return result;
}

original_code:
static char *get_ipaddr()
{
  int sfd;
  struct ifreq ifr;
  struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_ifru.ifru_addr;
  memset(&ifr, 0, sizeof ifr);
  if (0 > (sfd = socket(2, SOCK_STREAM, 0))) {
    perror("socket()");
    return 0;
  }
  strcpy(ifr.ifr_ifrn.ifrn_name, "eth0");
  sin->sin_family = 2;
  if (0 == ioctl(sfd, 0x8915, &ifr)) {
   return inet_ntoa(sin->sin_addr);
  }
  return 0;
}

variable_types: sfd int

variable_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }

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

user_defined_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }

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


usage

decompiled_code:
void __noreturn usage()
{
fprintf(
stderr,
"\n"
"usage: %s [options] [<path|fsid> ...]\n"
"\n"
" options:\n"
"\t-a\t\tOpen output file for appending (requires [-o path])\n"
"\t-h\t\tDisplay this usage info.\n"
" -i <ip> IP address for burst mode commands\n"
"\t-o <path>\tWrite output to the specified file.\n"
" -R <path|fsid> Export the parts of a recording FSID.\n"
"\n"
" Output defaults to stdout if not present on the command line.\n",
(const char *)prog);
credits();
exit(1);
}

canonical_decompiled_code:
void __noreturn func0()
{
fprintf(
stderr,
"\n"
"usage: %s [options] [<path|fsid> ...]\n"
"\n"
" options:\n"
"\t-a\t\tOpen output file for appending (requires [-o path])\n"
"\t-h\t\tDisplay this usage info.\n"
" -i <ip> IP address for burst mode commands\n"
"\t-o <path>\tWrite output to the specified file.\n"
" -R <path|fsid> Export the parts of a recording FSID.\n"
"\n"
" Output defaults to stdout if not present on the command line.\n",
(const char *)prog);
credits();
exit(1);
}

original_code:
static void usage(void)
{
 fprintf(stderr,"\nusage: %s [options] [<path|fsid> ...]\n\n   options:\n	-a		Open output file for appending (requires [-o path])\n	-h		Display this usage info.\n        -i <ip>         IP address for burst mode commands\n	-o <path>	Write output to the specified file.\n        -R <path|fsid>  Export the parts of a recording FSID.\n\n   Output defaults to stdout if not present on the command line.\n", prog );
 credits();
 exit(1);
}

canonical_original_code:
static void usage()
{
 fprintf(stderr,"\nusage: %s [options] [<path|fsid> ...]\n\n   options:\n	-a		Open output file for appending (requires [-o path])\n	-h		Display this usage info.\n        -i <ip>         IP address for burst mode commands\n	-o <path>	Write output to the specified file.\n        -R <path|fsid>  Export the parts of a recording FSID.\n\n   Output defaults to stdout if not present on the command line.\n", prog );
 credits();
 exit(1);
}


decompiled_code:
int __fastcall mfsinfo_usage(const char *a1)
{
return fprintf(stderr, "Usage:\n%s /dev/hdX [/dev/hdY]\n", a1);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
return fprintf(stderr, "Usage:\n%s /dev/hdX [/dev/hdY]\n", a1);
}

original_code:
void
mfsinfo_usage (char *progname)
{
 fprintf (stderr, "Usage:\n%s /dev/hdX [/dev/hdY]\n", progname);
}

canonical_original_code:
void mfsinfo_usage(char *progname)
{
 fprintf (stderr, "Usage:\n%s /dev/hdX [/dev/hdY]\n", progname);
}

variable_types:
progname
char *


decompiled_code:
void __fastcall read_all(int fd, void *buf, int a3)
{
int v4; // ebx
int v5; // eax

if ( a3 )
{
v4 = a3;
do
{
v5 = read(fd, buf, v4);
if ( v5 <= 0 )
exit(1);
buf = (char *)buf + v5;
v4 -= v5;
}
while ( v4 );
}
}

canonical_decompiled_code:
void __fastcall func2(int fd, void *buf, int a3)
{
int v4; // ebx
int v5; // eax

if ( a3 )
{
v4 = a3;
do
{
v5 = read(fd, buf, v4);
if ( v5 <= 0 )
exit(1);
buf = (char *)buf + v5;
v4 -= v5;
}
while ( v4 );
}
}

original_code:
void read_all(int fd, void *buf, int size)
{
 while (size) {
  int n = read(fd, buf, size);
  if (n <= 0) {
   exit(1);
  }
  buf += n;
  size -= n;
 }
}

variable_types: fd int

variable_types:
buf
void *

variable_types: size int

variable_types: n int


decompiled_code:
void __fastcall write_all(int fd, void *buf, int a3)
{
int v4; // ebx
int v5; // eax

if ( a3 )
{
v4 = a3;
do
{
v5 = write(fd, buf, v4);
if ( v5 <= 0 )
exit(1);
buf = (char *)buf + v5;
v4 -= v5;
}
while ( v4 );
}
}

canonical_decompiled_code:
void __fastcall func3(int fd, void *buf, int a3)
{
int v4; // ebx
int v5; // eax

if ( a3 )
{
v4 = a3;
do
{
v5 = write(fd, buf, v4);
if ( v5 <= 0 )
exit(1);
buf = (char *)buf + v5;
v4 -= v5;
}
while ( v4 );
}
}

original_code:
void write_all(int fd, void *buf, int size)
{
 while (size) {
  int n = write(fd, buf, size);
  if (n <= 0) {
   exit(1);
  }
  buf += n;
  size -= n;
 }
}


decompiled_code:
__int64 __fastcall open_socket_out(char *name, __int16 a2)
{
int v2; // eax
unsigned int v3; // er12
struct hostent *v4; // rax
__int16 v5; // ax
sa_family_t v6; // dx
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
int *v12; // rax
char *v13; // rax
int v14; // [rsp+8h] [rbp-30h] BYREF
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v14 = 1;
optval = 0xFFFF;
v2 = socket(2, 1, 0);
if ( v2 == -1 )
return (unsigned int)-1;
v3 = v2;
v4 = gethostbyname(name);
if ( !v4 )
{
if ( !use_syslog )
goto LABEL_18;
if ( fileno(stderr) == 2 )
{
syslog(3, "unknown host: %s\n", name);
return (unsigned int)-1;
}
if ( fileno(stderr) != 1 )
{
LABEL_18:
v3 = -1;
fprintf(stderr, "unknown host: %s\n", name);
return v3;
}
if ( use_syslog > 1 )
{
syslog(6, "unknown host: %s\n", name);
return (unsigned int)-1;
}
return (unsigned int)-1;
}
memcpy(&addr.sa_data[2], *(const void **)v4->h_addr_list, v4->h_length);
v5 = _bswap_16(a2);
addr.sa_family = v6;
*(_WORD *)addr.sa_data = v5;
if ( connect(v3, &addr, 0x10u) )
{
close(v3);
if ( !use_syslog )
goto LABEL_6;
if ( fileno(stderr) == 2 )
{
v12 = _errno_location();
v3 = -1;
v13 = strerror(*v12);
syslog(3, "failed to connect to %s - %s\n", name, v13);
return v3;
}
if ( fileno(stderr) == 1 )
{
if ( use_syslog > 1 )
{
v10 = _errno_location();
v3 = -1;
v11 = strerror(*v10);
syslog(6, "failed to connect to %s - %s\n", name, v11);
return v3;
}
}
else
{
LABEL_6:
v8 = _errno_location();
v9 = strerror(*v8);
fprintf(stderr, "failed to connect to %s - %s\n", name, v9);
}
return (unsigned int)-1;
}
setsockopt(v3, 1, 8, &optval, 4u);
setsockopt(v3, 6, 1, &v14, 4u);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *name, __int16 a2)
{
int v2; // eax
unsigned int v3; // er12
struct hostent *v4; // rax
__int16 v5; // ax
sa_family_t v6; // dx
int *v8; // rax
char *v9; // rax
int *v10; // rax
char *v11; // rax
int *v12; // rax
char *v13; // rax
int v14; // [rsp+8h] [rbp-30h] BYREF
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

v14 = 1;
optval = 0xFFFF;
v2 = socket(2, 1, 0);
if ( v2 == -1 )
return (unsigned int)-1;
v3 = v2;
v4 = gethostbyname(name);
if ( !v4 )
{
if ( !use_syslog )
goto LABEL_18;
if ( fileno(stderr) == 2 )
{
syslog(3, "unknown host: %s\n", name);
return (unsigned int)-1;
}
if ( fileno(stderr) != 1 )
{
LABEL_18:
v3 = -1;
fprintf(stderr, "unknown host: %s\n", name);
return v3;
}
if ( use_syslog > 1 )
{
syslog(6, "unknown host: %s\n", name);
return (unsigned int)-1;
}
return (unsigned int)-1;
}
memcpy(&addr.sa_data[2], *(const void **)v4->h_addr_list, v4->h_length);
v5 = func0(a2);
addr.sa_family = v6;
*(_WORD *)addr.sa_data = v5;
if ( connect(v3, &addr, 0x10u) )
{
close(v3);
if ( !use_syslog )
goto LABEL_6;
if ( fileno(stderr) == 2 )
{
v12 = _errno_location();
v3 = -1;
v13 = strerror(*v12);
syslog(3, "failed to connect to %s - %s\n", name, v13);
return v3;
}
if ( fileno(stderr) == 1 )
{
if ( use_syslog > 1 )
{
v10 = _errno_location();
v3 = -1;
v11 = strerror(*v10);
syslog(6, "failed to connect to %s - %s\n", name, v11);
return v3;
}
}
else
{
LABEL_6:
v8 = _errno_location();
v9 = strerror(*v8);
fprintf(stderr, "failed to connect to %s - %s\n", name, v9);
}
return (unsigned int)-1;
}
setsockopt(v3, 1, 8, &optval, 4u);
setsockopt(v3, 6, 1, &v14, 4u);
return v3;
}

original_code:
int open_socket_out(char *host, int port)
{
 int type = SOCK_STREAM;
 struct sockaddr_in sock_out;
 int res;
 struct hostent *hp;
 int nodelay = 1;
 int window = 65535;
 res = socket(2, type, 0);
 if (res == -1) {
  return -1;
 }
 hp = gethostbyname(host);
 if (!hp) {
  (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "unknown host: %s\n", host),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"unknown host: %s\n", host),1) : 1) : fprintf((stderr),"unknown host: %s\n", host)) : fprintf((stderr),"unknown host: %s\n", host));
  return -1;
 }
 memcpy(&sock_out.sin_addr, hp->h_addr_list[0], hp->h_length);
 sock_out.sin_port = htons(port);
 sock_out.sin_family = 2;
 if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) {
  close(res);
  (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "failed to connect to %s - %s\n", host, strerror((*__errno_location ()))),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"failed to connect to %s - %s\n", host, strerror((*__errno_location ()))),1) : 1) : fprintf((stderr),"failed to connect to %s - %s\n", host, strerror((*__errno_location ())))) : fprintf((stderr),"failed to connect to %s - %s\n", host, strerror((*__errno_location ()))));
  return -1;
 }
 setsockopt(res, 1, 8, &window, sizeof(int));
 setsockopt(res, IPPROTO_TCP, 1, &nodelay, sizeof(int));
 return res;
}

variable_types:
host
char *

variable_types: port int

variable_types: type 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: res int

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types: nodelay int

variable_types: window int

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


decompiled_code:
__off64_t __fastcall ll_seek(int a1, __off64_t a2, int a3)
{
__off64_t result; // rax

result = lseek64(a1, a2, a3);
if ( result == -1 )
{
if ( use_syslog )
{
if ( fileno(stderr) == 2 )
{
syslog(3, "llseek failed\n");
goto LABEL_7;
}
if ( fileno(stderr) == 1 )
{
if ( use_syslog > 1 )
syslog(6, "llseek failed\n");
LABEL_7:
exit(1);
}
}
fwrite("llseek failed\n", 1uLL, 0xEuLL, stderr);
goto LABEL_7;
}
return result;
}

canonical_decompiled_code:
__off64_t __fastcall func6(int a1, __off64_t a2, int a3)
{
__off64_t result; // rax

result = lseek64(a1, a2, a3);
if ( result == -1 )
{
if ( use_syslog )
{
if ( fileno(stderr) == 2 )
{
syslog(3, "llseek failed\n");
goto LABEL_7;
}
if ( fileno(stderr) == 1 )
{
if ( use_syslog > 1 )
syslog(6, "llseek failed\n");
LABEL_7:
exit(1);
}
}
fwrite("llseek failed\n", 1uLL, 0xEuLL, stderr);
goto LABEL_7;
}
return result;
}

original_code:
u64 ll_seek(int fd, u64 offset, int whence)
{
 u64 result;
 int ret = 0;
 result = lseek(fd, offset, whence);
 if(ret || (result == (off_t)-1))
 {
  (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "llseek failed\n"),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"llseek failed\n"),1) : 1) : fprintf((stderr),"llseek failed\n")) : fprintf((stderr),"llseek failed\n"));
  exit(1);
 }
 return result;
}

canonical_original_code:
unsigned int ll_seek(int fd, unsigned int offset, int whence)
{
 unsigned int result;
 int ret = 0;
 result = lseek(fd, offset, whence);
 if(ret || (result == (off_t)-1))
 {
  (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "llseek failed\n"),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"llseek failed\n"),1) : 1) : fprintf((stderr),"llseek failed\n")) : fprintf((stderr),"llseek failed\n"));
  exit(1);
 }
 return result;
}

variable_types:
offset
unsigned int

variable_types: whence int

variable_types:
result
unsigned int

variable_types: ret int

return_type: unsigned int


decompiled_code:
ssize_t __fastcall read_sectors(int fd, void *buf, unsigned int a3, int a4)
{
unsigned int v4; // ebx

v4 = a4 << 9;
ll_seek(fd, (unsigned __int64)a3 << 9, 0);
return read(fd, buf, v4) >> 9;
}

canonical_decompiled_code:
ssize_t __fastcall func7(int fd, void *buf, unsigned int a3, int a4)
{
unsigned int v4; // ebx

v4 = a4 << 9;
func6(fd, (unsigned __int64)a3 << 9, 0);
return read(fd, buf, v4) >> 9;
}

original_code:
u32 read_sectors(int fd, void *buf, u32 sector, u32 count)
{
 ll_seek(fd, ((u64)sector)<<9, 0);
 return read(fd, buf, count * 512)>>9;
}

canonical_original_code:
unsigned int read_sectors(int fd, void *buf, unsigned int sector, unsigned int count)
{
 ll_seek(fd, ((unsigned int)sector)<<9, 0);
 return read(fd, buf, count * 512)>>9;
}

variable_types:
sector
unsigned int

variable_types:
count
unsigned int


decompiled_code:
ssize_t __fastcall write_sectors(int fd, void *buf, unsigned int a3, int a4)
{
unsigned int v4; // ebx

if ( fs_inconsistent )
{
if ( use_syslog )
{
if ( fileno(stderr) == 2 )
{
syslog(3, "Filesystem is inconsistent, cannot write\n");
goto LABEL_8;
}
if ( fileno(stderr) == 1 )
{
if ( use_syslog > 1 )
syslog(6, "Filesystem is inconsistent, cannot write\n");
LABEL_8:
exit(1);
}
}
fwrite("Filesystem is inconsistent, cannot write\n", 1uLL, 0x29uLL, stderr);
goto LABEL_8;
}
v4 = a4 << 9;
ll_seek(fd, (unsigned __int64)a3 << 9, 0);
return write(fd, buf, v4) >> 9;
}

canonical_decompiled_code:
ssize_t __fastcall func8(int fd, void *buf, unsigned int a3, int a4)
{
unsigned int v4; // ebx

if ( fs_inconsistent )
{
if ( use_syslog )
{
if ( fileno(stderr) == 2 )
{
syslog(3, "Filesystem is inconsistent, cannot write\n");
goto LABEL_8;
}
if ( fileno(stderr) == 1 )
{
if ( use_syslog > 1 )
syslog(6, "Filesystem is inconsistent, cannot write\n");
LABEL_8:
exit(1);
}
}
fwrite("Filesystem is inconsistent, cannot write\n", 1uLL, 0x29uLL, stderr);
goto LABEL_8;
}
v4 = a4 << 9;
func6(fd, (unsigned __int64)a3 << 9, 0);
return write(fd, buf, v4) >> 9;
}

original_code:
u32 write_sectors(int fd, void *buf, u32 sector, u32 count)
{
 if(fs_inconsistent)
 {
  (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "Filesystem is inconsistent, cannot write\n"),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"Filesystem is inconsistent, cannot write\n"),1) : 1) : fprintf((stderr),"Filesystem is inconsistent, cannot write\n")) : fprintf((stderr),"Filesystem is inconsistent, cannot write\n"));
  exit(1);
 }
 {
 ll_seek(fd, ((u64)sector)<<9, 0);
 return write(fd, buf, count * 512)>>9;
 }
}

canonical_original_code:
unsigned int write_sectors(int fd, void *buf, unsigned int sector, unsigned int count)
{
 if(fs_inconsistent)
 {
  (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "Filesystem is inconsistent, cannot write\n"),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"Filesystem is inconsistent, cannot write\n"),1) : 1) : fprintf((stderr),"Filesystem is inconsistent, cannot write\n")) : fprintf((stderr),"Filesystem is inconsistent, cannot write\n"));
  exit(1);
 }
 {
 ll_seek(fd, ((unsigned int)sector)<<9, 0);
 return write(fd, buf, count * 512)>>9;
 }
}

variable_types: fd int

variable_types:
buf
void *


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

result = fcntl64(a1, 3LL, 0LL);
if ( (_DWORD)result != -1 && (result & 0x800) == 0 )
{
BYTE1(result) |= 8u;
result = fcntl64((unsigned int)a1, 4LL, (unsigned int)result);
}
return result;
}

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

result = fcntl64(a1, 3LL, 0LL);
if ( (_DWORD)result != -1 && (result & 0x800) == 0 )
{
BYTE1(result) |= 8u;
result = fcntl64((unsigned int)a1, 4LL, (unsigned int)result);
}
return result;
}

original_code:
void set_nonblocking(int fd)
{
 int val;
 if((val = fcntl(fd, 3, 0)) == -1)
  return;
 if (!(val & 04000)) {
  val |= 04000;
  fcntl(fd, 4, val);
 }
}

variable_types: val int


decompiled_code:
__int64 __fastcall bitcount32(int a1)
{
unsigned int i; // er8

for ( i = 0; a1; a1 &= a1 - 1 )
++i;
return i;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1)
{
unsigned int i; // er8

for ( i = 0; a1; a1 &= a1 - 1 )
++i;
return i;
}

original_code:
int bitcount32(u32 x)
{
 int count;
 for (count=0; x; count++)
  x &= (x-1);
 return count;
}

canonical_original_code:
int bitcount32(unsigned int x)
{
 int count;
 for (count=0; x; count++)
  x &= (x-1);
 return count;
}

variable_types:
x
unsigned int

variable_types: count int


decompiled_code:
int mls_usage()
{
return fprintf(stderr, "Usage:\n%s [/dev/hda [/dev/hdb]] /path", (const char *)progname);
}

canonical_decompiled_code:
int func1()
{
return fprintf(stderr, "Usage:\n%s [/dev/hda [/dev/hdb]] /path", (const char *)progname);
}

original_code:
void
mls_usage ()
{
 fprintf (stderr, "Usage:\n%s [/dev/hda [/dev/hdb]] /path", progname);
}

canonical_original_code:
void mls_usage()
{
 fprintf (stderr, "Usage:\n%s [/dev/hda [/dev/hdb]] /path", progname);
}


decompiled_code:
__int64 __fastcall find_file_by_name(__int64 a1, unsigned int a2, const char *a3)
{
int v4; // ebx
__int64 v5; // r12

if ( a2 <= 4 )
return 0LL;
v4 = 4;
while ( 1 )
{
v5 = a1 + v4;
if ( !strcmp((const char *)(v5 + 6), a3) )
break;
v4 += *(unsigned __int8 *)(v5 + 4);
if ( v4 >= a2 )
return 0LL;
}
return a1 + v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned int a2, const char *a3)
{
int v4; // ebx
__int64 v5; // r12

if ( a2 <= 4 )
return 0LL;
v4 = 4;
while ( 1 )
{
v5 = a1 + v4;
if ( !strcmp((const char *)(v5 + 6), a3) )
break;
v4 += *(unsigned __int8 *)(v5 + 4);
if ( v4 >= a2 )
return 0LL;
}
return a1 + v4;
}

original_code:
fs_entry *
find_file_by_name (unsigned char *dir, unsigned int dirlen, unsigned char *name)
{
 int offset = 4;
 while (offset < dirlen)
 {
  fs_entry *cur = (fs_entry *) (dir + offset);
  if (!strcmp (cur->name, name))
  {
   return cur;
  }
  offset += cur->entry_length;
 }
 return 0;
}

canonical_original_code:
struct fs_entry_s *find_file_by_name(unsigned char *dir, unsigned int dirlen, unsigned char *name)
{
 int offset = 4;
 while (offset < dirlen)
 {
  struct fs_entry_s *cur = (fs_entry *) (dir + offset);
  if (!strcmp (cur->name, name))
  {
   return cur;
  }
  offset += cur->entry_length;
 }
 return 0;
}

variable_types:
dir
unsigned char *

variable_types:
dirlen
unsigned int

variable_types:
name
unsigned char *

variable_types: offset int

variable_types: struct fs_entry_s { unsigned int fsid; unsigned char entry_length; enum fsid_type_e { tyNone=0, tyFile=1, tyStream=2, tyDir=4, tyDb=8 } type; unsigned char name[]; } *

user_defined_types: struct fs_entry_s { unsigned int fsid; unsigned char entry_length; enum fsid_type_e type; unsigned char name[]; }


decompiled_code:
void __fastcall print_file_details(__int64 a1)
{
unsigned int v2; // edi
unsigned int v3; // eax
__int64 v4; // rax
unsigned int *v5; // rbp
struct tm *v6; // rax
char v7; // cl
unsigned int v8; // eax
const char *v9; // rdx
const char *v10; // r8
__int64 v11; // r9
unsigned int v12; // eax
const char *v13; // rdx
__int64 v14; // r8
__int64 v15; // r9
__int64 v16; // r10
char v17; // cl
time_t timer; // [rsp+8h] [rbp-40h] BYREF
char s[16]; // [rsp+10h] [rbp-38h] BYREF
char v20; // [rsp+20h] [rbp-28h]

v2 = *(_DWORD *)a1;
v20 = 0;
*(__m128i *)s = _mm_load_si128((const __m128i *)&LC5);
v3 = _bswap_32(v2);
v4 = mfs_read_inode_by_fsid(mfs, v3);
v5 = (unsigned int *)v4;
if ( !v4 )
{
v17 = *(_BYTE *)(a1 + 5);
if ( (unsigned __int8)(v17 - 1) <= 7u && v17 == 2 )
goto LABEL_10;
LABEL_4:
v8 = _bswap_32(*(_DWORD *)a1);
printf(" %-26s%-8s%8d%16s%7d\n", (const char *)(a1 + 6), v9, v8, v10, v11);
goto LABEL_5;
}
timer = (unsigned int)_bswap_32(*(_DWORD *)(v4 + 36));
v6 = localtime(&timer);
strftime(s, 0x10uLL, "%D %R", v6);
v7 = *(_BYTE *)(a1 + 5);
if ( (unsigned __int8)(v7 - 1) > 7u || v7 != 2 )
{
_bswap_32(v5[6]);
goto LABEL_4;
}
_bswap_32(v5[8]);
_bswap_32(v5[6]);
_bswap_32(v5[7]);
LABEL_10:
v12 = _bswap_32(*(_DWORD *)a1);
printf(" %-26s%-8s%8d%16s%7d x %d(%d)\n", (const char *)(a1 + 6), v13, v12, s, v15, v14, v16);
LABEL_5:
if ( v5 )
free(v5);
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1)
{
unsigned int v2; // edi
unsigned int v3; // eax
__int64 v4; // rax
unsigned int *v5; // rbp
struct tm *v6; // rax
char v7; // cl
unsigned int v8; // eax
const char *v9; // rdx
const char *v10; // r8
__int64 v11; // r9
unsigned int v12; // eax
const char *v13; // rdx
__int64 v14; // r8
__int64 v15; // r9
__int64 v16; // r10
char v17; // cl
time_t timer; // [rsp+8h] [rbp-40h] BYREF
char s[16]; // [rsp+10h] [rbp-38h] BYREF
char v20; // [rsp+20h] [rbp-28h]

v2 = *(_DWORD *)a1;
v20 = 0;
*(__m128i *)s = _mm_load_si128((const __m128i *)&LC5);
v3 = func0(v2);
v4 = mfs_read_inode_by_fsid(mfs, v3);
v5 = (unsigned int *)v4;
if ( !v4 )
{
v17 = *(_BYTE *)(a1 + 5);
if ( (unsigned __int8)(v17 - 1) <= 7u && v17 == 2 )
goto LABEL_10;
LABEL_4:
v8 = func0(*(_DWORD *)a1);
printf(" %-26s%-8s%8d%16s%7d\n", (const char *)(a1 + 6), v9, v8, v10, v11);
goto LABEL_5;
}
timer = (unsigned int)func0(*(_DWORD *)(v4 + 36));
v6 = localtime(&timer);
strftime(s, 0x10uLL, "%D %R", v6);
v7 = *(_BYTE *)(a1 + 5);
if ( (unsigned __int8)(v7 - 1) > 7u || v7 != 2 )
{
func0(v5[6]);
goto LABEL_4;
}
func0(v5[8]);
func0(v5[6]);
func0(v5[7]);
LABEL_10:
v12 = func0(*(_DWORD *)a1);
printf(" %-26s%-8s%8d%16s%7d x %d(%d)\n", (const char *)(a1 + 6), v13, v12, s, v15, v14, v16);
LABEL_5:
if ( v5 )
free(v5);
}

original_code:
void
print_file_details (fs_entry * file)
{
 unsigned char *type = "ty???";
 unsigned char date[17] = "xx/xx/xx xx:xx";
 mfs_inode *nfo;
 time_t modtime;
 nfo = mfs_read_inode_by_fsid (mfs, htonl (file->fsid));
 if (nfo)
 {
  modtime = htonl (nfo->lastmodified);
  strftime (date, 16, "%D %R", localtime (&modtime));
 }
 switch (file->type)
 {
 case tyFile:
  type = "tyFile";
  break;
 case tyStream:
  type = "tyStream";
  break;
 case tyDir:
  type = "tyDir";
  break;
 case tyDb:
  type = "tyDb";
  break;
 default:
  type = "ty???";
  break;
 }
 if (file->type == tyStream)
 {
  printf ("    %-26s%-8s%8d%16s%7d x %d(%d)\n", file->name, type, htonl (file->fsid), date, nfo ? htonl (nfo->blocksize) : -1, nfo ? htonl (nfo->size) : -1, nfo ? htonl (nfo->blockused) : -1);
 }
 else
 {
  printf ("    %-26s%-8s%8d%16s%7d\n", file->name, type, htonl (file->fsid), date, nfo ? htonl (nfo->size) : -1);
 }
 if (nfo)
  free (nfo);
}

canonical_original_code:
void print_file_details(struct fs_entry_s *file)
{
 unsigned char *type = "ty???";
 unsigned char date[17] = "xx/xx/xx xx:xx";
 struct mfs_inode_s *nfo;
 long modtime;
 nfo = mfs_read_inode_by_fsid (mfs, htonl (file->fsid));
 if (nfo)
 {
  modtime = htonl (nfo->lastmodified);
  strftime (date, 16, "%D %R", localtime (&modtime));
 }
 switch (file->type)
 {
 case tyFile:
  type = "tyFile";
  break;
 case tyStream:
  type = "tyStream";
  break;
 case tyDir:
  type = "tyDir";
  break;
 case tyDb:
  type = "tyDb";
  break;
 default:
  type = "ty???";
  break;
 }
 if (file->type == tyStream)
 {
  printf ("    %-26s%-8s%8d%16s%7d x %d(%d)\n", file->name, type, htonl (file->fsid), date, nfo ? htonl (nfo->blocksize) : -1, nfo ? htonl (nfo->size) : -1, nfo ? htonl (nfo->blockused) : -1);
 }
 else
 {
  printf ("    %-26s%-8s%8d%16s%7d\n", file->name, type, htonl (file->fsid), date, nfo ? htonl (nfo->size) : -1);
 }
 if (nfo)
  free (nfo);
}

variable_types:
type
unsigned char *

variable_types:
date
unsigned char [17]

variable_types: struct mfs_inode_s { unsigned int fsid; unsigned int refcount; unsigned int unk1; unsigned int unk2; unsigned int inode; unsigned int unk3; unsigned int size; unsigned int blocksize; unsigned int blockused; unsigned int lastmodified; enum fsid_type_e { tyNone=0, tyFile=1, tyStream=2, tyDir=4, tyDb=8 } type; unsigned char unk6; unsigned short beef; unsigned int sig; unsigned int checksum; unsigned int inode_flags; unsigned int numblocks; struct { unsigned int sector; unsigned int count; } datablocks[]; } *

variable_types: modtime long

user_defined_types: struct mfs_inode_s { unsigned int fsid; unsigned int refcount; unsigned int unk1; unsigned int unk2; unsigned int inode; unsigned int unk3; unsigned int size; unsigned int blocksize; unsigned int blockused; unsigned int lastmodified; enum fsid_type_e type; unsigned char unk6; unsigned short beef; unsigned int sig; unsigned int checksum; unsigned int inode_flags; unsigned int numblocks; struct { unsigned int sector; unsigned int count; } datablocks[]; }


decompiled_code:
void __fastcall list_file(char *s)
{
char *v1; // r12
void *v2; // r14
char v3; // al
__int64 v4; // r15
void *v5; // r13
int v6; // ebx
char *v7; // rax
char *v8; // rbp
__int64 v9; // rax
unsigned int v10; // eax
bool v11; // al
unsigned int v12; // eax
__int64 v13; // rbp
__int64 v14; // r12
signed int i; // ebp
unsigned int v16[15]; // [rsp+Ch] [rbp-3Ch] BYREF

v1 = s;
v16[0] = 0;
if ( !s )
{
v2 = (void *)mfs_read_inode_by_fsid(mfs, 1LL);
if ( v2 )
{
v1 = "/";
goto LABEL_4;
}
LABEL_37:
mfs_perror(mfs, "Read root directory");
exit(1);
}
v2 = (void *)mfs_read_inode_by_fsid(mfs, 1LL);
if ( !v2 )
goto LABEL_37;
if ( !*s )
{
v5 = 0LL;
puts(" Name Type FsId Date Time Size");
puts(" ---- ---- ---- ---- ---- ----");
goto LABEL_35;
}
LABEL_4:
v3 = *v1;
v4 = 0LL;
v5 = 0LL;
while ( 1 )
{
v6 = 0;
if ( v3 == 47 )
{
do
{
++v1;
++v6;
}
while ( *v1 == 47 );
}
v7 = strchr(v1, 47);
v8 = v7;
if ( v7 )
*v7 = 0;
if ( !*v1 )
break;
if ( v5 )
free(v5);
v9 = mfs_read_inode_data(mfs, v2, v16);
v5 = (void *)v9;
if ( !v9 )
{
mfs_perror(mfs, "Read directory");
exit(1);
}
v4 = find_file_by_name(v9, v16[0], v1);
if ( !v4 )
goto LABEL_36;
free(v2);
v10 = _bswap_32(*(_DWORD *)v4);
v2 = (void *)mfs_read_inode_by_fsid(mfs, v10);
if ( !v2 )
goto LABEL_36;
if ( !v8 )
{
puts(" Name Type FsId Date Time Size");
puts(" ---- ---- ---- ---- ---- ----");
LABEL_29:
print_file_details(v4);
return;
}
v3 = v8[1];
*v8 = 47;
v1 = v8 + 1;
if ( !v3 )
{
puts(" Name Type FsId Date Time Size");
puts(" ---- ---- ---- ---- ---- ----");
v11 = 1;
LABEL_18:
if ( *(_BYTE *)(v4 + 5) == 4 && v11 )
{
free(v2);
v12 = _bswap_32(*(_DWORD *)v4);
v13 = mfs_read_inode_by_fsid(mfs, v12);
goto LABEL_21;
}
goto LABEL_29;
}
}
puts(" Name Type FsId Date Time Size");
puts(" ---- ---- ---- ---- ---- ----");
if ( v4 )
{
v11 = v6 != 0;
goto LABEL_18;
}
LABEL_35:
free(v2);
v13 = mfs_read_inode_by_fsid(mfs, 1LL);
LABEL_21:
if ( !v13 )
goto LABEL_36;
if ( v5 )
free(v5);
v14 = mfs_read_inode_data(mfs, v13, v16);
if ( !v14 )
{
LABEL_36:
fwrite("No such file or directory!\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
for ( i = 4; i < v16[0]; i += *(unsigned __int8 *)(v14 + i + 4) )
print_file_details(v14 + i);
}

canonical_decompiled_code:
void __fastcall func4(char *s)
{
char *v1; // r12
void *v2; // r14
char v3; // al
__int64 v4; // r15
void *v5; // r13
int v6; // ebx
char *v7; // rax
char *v8; // rbp
__int64 v9; // rax
unsigned int v10; // eax
bool v11; // al
unsigned int v12; // eax
__int64 v13; // rbp
__int64 v14; // r12
signed int i; // ebp
unsigned int v16[15]; // [rsp+Ch] [rbp-3Ch] BYREF

v1 = s;
v16[0] = 0;
if ( !s )
{
v2 = (void *)mfs_read_inode_by_fsid(mfs, 1LL);
if ( v2 )
{
v1 = "/";
goto LABEL_4;
}
LABEL_37:
mfs_perror(mfs, "Read root directory");
exit(1);
}
v2 = (void *)mfs_read_inode_by_fsid(mfs, 1LL);
if ( !v2 )
goto LABEL_37;
if ( !*s )
{
v5 = 0LL;
puts(" Name Type FsId Date Time Size");
puts(" ---- ---- ---- ---- ---- ----");
goto LABEL_35;
}
LABEL_4:
v3 = *v1;
v4 = 0LL;
v5 = 0LL;
while ( 1 )
{
v6 = 0;
if ( v3 == 47 )
{
do
{
++v1;
++v6;
}
while ( *v1 == 47 );
}
v7 = strchr(v1, 47);
v8 = v7;
if ( v7 )
*v7 = 0;
if ( !*v1 )
break;
if ( v5 )
free(v5);
v9 = mfs_read_inode_data(mfs, v2, v16);
v5 = (void *)v9;
if ( !v9 )
{
mfs_perror(mfs, "Read directory");
exit(1);
}
v4 = func2(v9, v16[0], v1);
if ( !v4 )
goto LABEL_36;
free(v2);
v10 = func0(*(_DWORD *)v4);
v2 = (void *)mfs_read_inode_by_fsid(mfs, v10);
if ( !v2 )
goto LABEL_36;
if ( !v8 )
{
puts(" Name Type FsId Date Time Size");
puts(" ---- ---- ---- ---- ---- ----");
LABEL_29:
func3(v4);
return;
}
v3 = v8[1];
*v8 = 47;
v1 = v8 + 1;
if ( !v3 )
{
puts(" Name Type FsId Date Time Size");
puts(" ---- ---- ---- ---- ---- ----");
v11 = 1;
LABEL_18:
if ( *(_BYTE *)(v4 + 5) == 4 && v11 )
{
free(v2);
v12 = func0(*(_DWORD *)v4);
v13 = mfs_read_inode_by_fsid(mfs, v12);
goto LABEL_21;
}
goto LABEL_29;
}
}
puts(" Name Type FsId Date Time Size");
puts(" ---- ---- ---- ---- ---- ----");
if ( v4 )
{
v11 = v6 != 0;
goto LABEL_18;
}
LABEL_35:
free(v2);
v13 = mfs_read_inode_by_fsid(mfs, 1LL);
LABEL_21:
if ( !v13 )
goto LABEL_36;
if ( v5 )
free(v5);
v14 = mfs_read_inode_data(mfs, v13, v16);
if ( !v14 )
{
LABEL_36:
fwrite("No such file or directory!\n", 1uLL, 0x1BuLL, stderr);
exit(1);
}
for ( i = 4; i < v16[0]; i += *(unsigned __int8 *)(v14 + i + 4) )
func3(v14 + i);
}

original_code:
void
list_file (unsigned char *name)
{
 mfs_inode *cur_nfo = 0;
 unsigned char *cur_dir = 0;
 unsigned int cur_dir_length = 0;
 fs_entry *cur_file = 0;
 int trailingslash = 0;
 if (!name)
 {
  name = "/";
 }
 cur_nfo = mfs_read_inode_by_fsid (mfs, 1);
 if (!cur_nfo)
 {
  mfs_perror (mfs, "Read root directory");
  exit (1);
 }
 while (*name)
 {
  char *next;
  trailingslash = 0;
  while (*name == '/')
   name++, trailingslash++;
  next = strchr (name, '/');
  if (next)
   *next = 0;
  if (!*name)
  {
   break;
  }
  if (cur_dir)
   free (cur_dir);
  cur_dir = mfs_read_inode_data (mfs, cur_nfo, &cur_dir_length);
  if (!cur_dir)
  {
   mfs_perror (mfs, "Read directory");
   exit (1);
  }
  cur_file = find_file_by_name (cur_dir, cur_dir_length, name);
  if (!cur_file)
  {
   fprintf (stderr, "No such file or directory!\n");
   exit (1);
  }
  free (cur_nfo);
  cur_nfo = mfs_read_inode_by_fsid (mfs, htonl (cur_file->fsid));
  if (!cur_nfo)
  {
   fprintf (stderr, "No such file or directory!\n");
   exit (1);
  }
  if (next)
  {
   *next = '/';
   trailingslash = 1;
   name = next + 1;
  }
  else
  {
   trailingslash = 0;
   break;
  }
 }
 printf ("    Name                      Type        FsId      Date  Time   Size\n");
 printf ("    ----                      ----        ----      ----  ----   ----\n");
 if (!cur_file || (cur_file->type == tyDir && trailingslash))
 {
  int offset = 4;
  free (cur_nfo);
  if (cur_file)
  {
   cur_nfo = mfs_read_inode_by_fsid (mfs, htonl (cur_file->fsid));
  }
  else
  {
   cur_nfo = mfs_read_inode_by_fsid (mfs, 1);
  }
  if (!cur_nfo)
  {
   fprintf (stderr, "No such file or directory!\n");
   exit (1);
  }
  if (cur_dir)
   free (cur_dir);
  cur_dir = mfs_read_inode_data (mfs, cur_nfo, &cur_dir_length);
  if (!cur_dir)
  {
   fprintf (stderr, "No such file or directory!\n");
   exit (1);
  }
  for (offset = 4; offset < cur_dir_length; offset += cur_file->entry_length)
  {
   cur_file = (fs_entry *) (cur_dir + offset);
   print_file_details (cur_file);
  }
 }
 else
 {
  print_file_details (cur_file);
 }
}

canonical_original_code:
void list_file(unsigned char *name)
{
 struct mfs_inode_s *cur_nfo = 0;
 unsigned char *cur_dir = 0;
 unsigned int cur_dir_length = 0;
 struct fs_entry_s *cur_file = 0;
 int trailingslash = 0;
 if (!name)
 {
  name = "/";
 }
 cur_nfo = mfs_read_inode_by_fsid (mfs, 1);
 if (!cur_nfo)
 {
  mfs_perror (mfs, "Read root directory");
  exit (1);
 }
 while (*name)
 {
  char *next;
  trailingslash = 0;
  while (*name == '/')
   name++, trailingslash++;
  next = strchr (name, '/');
  if (next)
   *next = 0;
  if (!*name)
  {
   break;
  }
  if (cur_dir)
   free (cur_dir);
  cur_dir = mfs_read_inode_data (mfs, cur_nfo, &cur_dir_length);
  if (!cur_dir)
  {
   mfs_perror (mfs, "Read directory");
   exit (1);
  }
  cur_file = find_file_by_name (cur_dir, cur_dir_length, name);
  if (!cur_file)
  {
   fprintf (stderr, "No such file or directory!\n");
   exit (1);
  }
  free (cur_nfo);
  cur_nfo = mfs_read_inode_by_fsid (mfs, htonl (cur_file->fsid));
  if (!cur_nfo)
  {
   fprintf (stderr, "No such file or directory!\n");
   exit (1);
  }
  if (next)
  {
   *next = '/';
   trailingslash = 1;
   name = next + 1;
  }
  else
  {
   trailingslash = 0;
   break;
  }
 }
 printf ("    Name                      Type        FsId      Date  Time   Size\n");
 printf ("    ----                      ----        ----      ----  ----   ----\n");
 if (!cur_file || (cur_file->type == tyDir && trailingslash))
 {
  int offset = 4;
  free (cur_nfo);
  if (cur_file)
  {
   cur_nfo = mfs_read_inode_by_fsid (mfs, htonl (cur_file->fsid));
  }
  else
  {
   cur_nfo = mfs_read_inode_by_fsid (mfs, 1);
  }
  if (!cur_nfo)
  {
   fprintf (stderr, "No such file or directory!\n");
   exit (1);
  }
  if (cur_dir)
   free (cur_dir);
  cur_dir = mfs_read_inode_data (mfs, cur_nfo, &cur_dir_length);
  if (!cur_dir)
  {
   fprintf (stderr, "No such file or directory!\n");
   exit (1);
  }
  for (offset = 4; offset < cur_dir_length; offset += cur_file->entry_length)
  {
   cur_file = (struct fs_entry_s *) (cur_dir + offset);
   print_file_details (cur_file);
  }
 }
 else
 {
  print_file_details (cur_file);
 }
}

variable_types: trailingslash int

variable_types:
next
char *

user_defined_types: enum fsid_type_e { tyNone=0, tyFile=1, tyStream=2, tyDir=4, tyDb=8 }


decompiled_code:
__int64 __fastcall mls_main(int a1, _QWORD *a2, __int64 a3, __int64 a4, char *a5)
{
const char *v5; // r12
char *v6; // rbp
__int64 result; // rax
char *v8; // r13

v5 = (const char *)a2[1];
progname = *a2;
if ( a1 <= 2 )
{
v6 = (char *)v5;
v8 = getenv("MFS_HDA");
a5 = getenv("MFS_HDB");
if ( !v8 || (v5 = v8, !*v8) )
{
a5 = "/dev/hdb";
v5 = "/dev/hda";
}
}
else
{
v6 = (char *)a2[2];
if ( a1 != 3 )
{
if ( a1 != 4 )
{
mls_usage();
return 1LL;
}
a5 = (char *)a2[2];
v6 = (char *)a2[3];
}
}
mfs = mfs_init(v5, a5, 0LL);
if ( mfs )
{
if ( (unsigned int)mfs_has_error(mfs) )
{
mfs_perror(mfs, *a2);
result = 1LL;
}
else
{
list_file(v6);
result = 0LL;
}
}
else
{
fwrite("mfs_init: Failed. Bailing.\n", 1uLL, 0x1CuLL, stderr);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, _QWORD *a2, __int64 a3, __int64 a4, char *a5)
{
const char *v5; // r12
char *v6; // rbp
__int64 result; // rax
char *v8; // r13

v5 = (const char *)a2[1];
progname = *a2;
if ( a1 <= 2 )
{
v6 = (char *)v5;
v8 = getenv("MFS_HDA");
a5 = getenv("MFS_HDB");
if ( !v8 || (v5 = v8, !*v8) )
{
a5 = "/dev/hdb";
v5 = "/dev/hda";
}
}
else
{
v6 = (char *)a2[2];
if ( a1 != 3 )
{
if ( a1 != 4 )
{
func1();
return 1LL;
}
a5 = (char *)a2[2];
v6 = (char *)a2[3];
}
}
mfs = mfs_init(v5, a5, 0LL);
if ( mfs )
{
if ( (unsigned int)mfs_has_error(mfs) )
{
mfs_perror(mfs, *a2);
result = 1LL;
}
else
{
func4(v6);
result = 0LL;
}
}
else
{
fwrite("mfs_init: Failed. Bailing.\n", 1uLL, 0x1CuLL, stderr);
result = 1LL;
}
return result;
}

original_code:
int
mls_main (int argc, char **argv)
{
 char *arg = argv[1];
 char *hda, *hdb;
 progname = argv[0];
 if (argc > 2)
 {
  hda = argv[1];
  arg = argv[2];
  if (argc > 3)
  {
   hdb = argv[2];
   arg = argv[3];
   if (argc > 4)
   {
    mls_usage ();
    return 1;
   }
  }
 }
 else
 {
  hda = getenv ("MFS_HDA");
  hdb = getenv ("MFS_HDB");
  if (!hda || !*hda)
  {
   hda = "/dev/hda";
   hdb = "/dev/hdb";
  }
 }
 mfs = mfs_init (hda, hdb, 00);
 if (!mfs)
 {
  fprintf (stderr, "mfs_init: Failed.  Bailing.\n");
  return 1;
 }
 if (mfs_has_error (mfs))
 {
  mfs_perror (mfs, argv[0]);
  return 1;
 }
 list_file (arg);
 return 0;
}

canonical_original_code:
int mls_main(int argc, char **argv)
{
 char *arg = argv[1];
 char *hda; char *hdb;
 progname = argv[0];
 if (argc > 2)
 {
  hda = argv[1];
  arg = argv[2];
  if (argc > 3)
  {
   hdb = argv[2];
   arg = argv[3];
   if (argc > 4)
   {
    mls_usage ();
    return 1;
   }
  }
 }
 else
 {
  hda = getenv ("MFS_HDA");
  hdb = getenv ("MFS_HDB");
  if (!hda || !*hda)
  {
   hda = "/dev/hda";
   hdb = "/dev/hdb";
  }
 }
 mfs = mfs_init (hda, hdb, 00);
 if (!mfs)
 {
  fprintf (stderr, "mfs_init: Failed.  Bailing.\n");
  return 1;
 }
 if (mfs_has_error (mfs))
 {
  mfs_perror (mfs, argv[0]);
  return 1;
 }
 list_file (arg);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
arg
char *

variable_types:
hda
char *

variable_types:
hdb
char *


decompiled_code:
void __fastcall load_object(unsigned int a1)
{
int v1; // ebx

if ( loaded != a1 )
{
if ( ptr )
free(ptr);
loaded = a1;
v1 = mfs_fsid_size(a1);
dword_610 = v1;
ptr = malloc(v1);
mfs_fsid_pread(a1, ptr, 0LL, (unsigned int)v1);
}
}

canonical_decompiled_code:
void __fastcall func1(unsigned int a1)
{
int v1; // ebx

if ( loaded != a1 )
{
if ( ptr )
free(ptr);
loaded = a1;
v1 = mfs_fsid_size(a1);
dword_610 = v1;
ptr = malloc(v1);
mfs_fsid_pread(a1, ptr, 0LL, (unsigned int)v1);
}
}

original_code:
static void load_object(int fsid)
{
 if (fsid == loaded.fsid) return;
 if (loaded.buf) free(loaded.buf);
 loaded.fsid = fsid;
 loaded.size = mfs_fsid_size(fsid);
 loaded.buf = malloc(loaded.size);
 mfs_fsid_pread(fsid, loaded.buf, 0, loaded.size);
}

variable_types: fsid int


decompiled_code:
__int64 __fastcall query_part(unsigned int a1, int a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char a7)
{
__int64 v8[3]; // [rsp+0h] [rbp-48h] BYREF
int v9; // [rsp+18h] [rbp-30h]
__int16 v10; // [rsp+1Ch] [rbp-2Ch] BYREF
unsigned int v11; // [rsp+1Eh] [rbp-2Ah]
__int16 v12; // [rsp+22h] [rbp-26h]
__int64 *v13; // [rsp+24h] [rbp-24h]
int v14; // [rsp+2Ch] [rbp-1Ch]
char *v15; // [rsp+38h] [rbp-10h]

v8[2] = a3;
v15 = &a7;
v10 = -17599;
v9 = a2;
v8[1] = a4;
v11 = (unsigned int)callback_0;
v12 = -17847;
v13 = v8;
v14 = -1864106167;
v8[0] = 0LL;
if ( (unsigned int)mfs_fsid_type() != 8 )
{
fprintf(stderr, "%d is not an object\n", a1);
exit(1);
}
load_object(a1);
parse_object(a1, ptr, &v10);
return v8[0];
}

original_code:
void *query_part(int fsid, int subobj, const char *name, int *len)
{
 void *ret = ((void *)0);
 void callback(int fsid, struct mfs_subobj_header *obj,
        struct mfs_attr_header *attr, void *data)
  {
   if (!attr) return;
   if (!(obj->flags && subobj == -1) && !(obj->id == subobj)) return;
   if (strcmp(schema_attrib(obj->obj_type, attr->attr), name)) return;
   *len = attr->len;
   ret = data;
  }
 if (mfs_fsid_type(fsid) != 8) {
  fprintf(stderr,"%d is not an object\n", fsid);
  exit(1);
 }
 load_object(fsid);
 parse_object(fsid, loaded.buf, callback);
 return ret;
}

canonical_original_code:
void *query_part(int fsid, int subobj, char *name, int *len)
{
 void *ret = ((void *)0);
 void callback(int fsid, struct mfs_subobj_header *obj,
        struct mfs_attr_header *attr, void *data)
  {
   if (!attr) return;
   if (!(obj->flags && subobj == -1) && !(obj->id == subobj)) return;
   if (strcmp(schema_attrib(obj->obj_type, attr->attr), name)) return;
   *len = attr->len;
   ret = data;
  }
 if (mfs_fsid_type(fsid) != 8) {
  fprintf(stderr,"%d is not an object\n", fsid);
  exit(1);
 }
 load_object(fsid);
 parse_object(fsid, loaded.buf, callback);
 return ret;
}

variable_types: subobj int

variable_types:
name
char *

variable_types:
len
int *

variable_types:
ret
void *

return_type: void *


decompiled_code:
__int64 __fastcall query_subobj_path(int a1, int a2, const char *a3, _DWORD *a4, __int64 *a5, int a6)
{
char *v9; // rax
__int64 v10; // r8
__int64 v11; // r9
size_t v12; // rdx
void *v13; // rsp
void *v14; // rax
__int64 v15; // r8
__int64 v16; // r9
__int64 v17; // rax
char *v18; // r8
unsigned int *v19; // rbx
unsigned __int64 v20; // rdx
unsigned int v21; // er14
unsigned int v22; // edi
unsigned int v23; // eax
unsigned int v24; // er11
__int64 v25; // rax
char *s; // [rsp+0h] [rbp-40h] BYREF
unsigned int *v28; // [rsp+8h] [rbp-38h]

LODWORD(v28) = a1;
if ( a6 <= 0 )
return 0;
v9 = strchr(a3, 47);
if ( !v9 )
{
v25 = query_part((unsigned int)v28, a2, (__int64)a3, (__int64)a4, v10, v11, (char)s);
*a5 = v25;
return v25 != 0;
}
s = v9;
v12 = strnlen(a3, v9 - a3);
v13 = alloca(v12 + 9);
*((_BYTE *)&s + v12) = 0;
v14 = memcpy(&s, a3, v12);
v17 = query_part((unsigned int)v28, a2, (__int64)v14, (__int64)a4, v15, v16, (char)s);
v18 = s;
*a5 = v17;
v19 = (unsigned int *)v17;
if ( !v17 )
return 0;
v20 = (unsigned __int64)(*a4 - 4) >> 3;
if ( (int)v20 <= 0 )
return 0;
v21 = 0;
s = v18 + 1;
v28 = (unsigned int *)(v17 + 8LL * (unsigned int)(v20 - 1) + 8);
do
{
v22 = *v19;
v19 += 2;
_bswap_32(v22);
v23 = _bswap_32(*(v19 - 1));
v21 += query_subobj_path(v24, v23, s, &a4[v21], &a5[v21]);
}
while ( v19 != v28 );
return v21;
}

original_code:
static int query_subobj_path(int fsid, int subobj, const char *path, int *len,
         void *ret[], const int max)
{
 int i=0;
 int idx = 0;
 const char *tok, *end;
 if (max <=0) return 0;
 end = strchr(path,'/');
 tok = (end) ? (__extension__ ({ const char *__old = (path); size_t __len = strnlen (__old, (end-path)); char *__new = (char *) __builtin_alloca (__len + 1); __new[__len] = '\0'; (char *) memcpy (__new, __old, __len); })) : path;
 ret[idx] = query_part(fsid, subobj, tok, &len[idx]);
 if (!ret[idx]) return idx;
 if (end) {
  struct mfs_obj_attr *objattr = ret[idx];
  int count = (len[idx]-4)/sizeof(*objattr);
  for (i=0;i<count;i++) {
   int n;
   fsid = ntohl(objattr->fsid);
   subobj = ntohl(objattr->subobj);
   n = query_subobj_path(
    fsid, subobj, end+1, len+idx,
    ret+idx, max-idx );
   idx += n;
   objattr++;
  }
 } else
  idx++;
 return idx;
}

canonical_original_code:
static int query_subobj_path(int fsid, int subobj, char *path, int *len, void *ret[], int max)
{
 int i = 0;
 int idx = 0;
 char *tok; char *end;
 if (max <=0) return 0;
 end = strchr(path,'/');
 tok = (end) ? (__extension__ ({ char *__old = (path); unsigned long __len = strnlen (__old, (end-path)); char *__new = (char *) __builtin_alloca (__len + 1); __new[__len] = '\0'; (char *) memcpy (__new, __old, __len); })) : path;
 ret[idx] = query_part(fsid, subobj, tok, &len[idx]);
 if (!ret[idx]) return idx;
 if (end) {
  struct mfs_obj_attr *objattr = ret[idx];
  int count = (len[idx]-4)/sizeof(*objattr);
  for (i=0;i<count;i++) {
   int n;
   fsid = ntohl(objattr->fsid);
   subobj = ntohl(objattr->subobj);
   n = query_subobj_path(
    fsid, subobj, end+1, len+idx,
    ret+idx, max-idx );
   idx += n;
   objattr++;
  }
 } else
  idx++;
 return idx;
}

variable_types:
path
char *

variable_types:
ret
void *[]

variable_types: max int

variable_types: i int

variable_types: idx int

variable_types:
tok
char *

variable_types:
end
char *

variable_types: struct mfs_obj_attr { unsigned int fsid; int subobj; } *

variable_types: count int

variable_types: n int


decompiled_code:
__int64 __fastcall query_string(int a1, const char *a2)
{
bool v2; // zf
__int64 result; // rax
int v4; // [rsp+4h] [rbp-14h] BYREF
__int64 v5; // [rsp+8h] [rbp-10h] BYREF

v4 = 0;
v5 = 0LL;
v2 = (unsigned int)query_subobj_path(a1, -1, a2, &v4, &v5, 1) == 1;
result = 0LL;
if ( v2 )
result = v5;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, const char *a2)
{
bool v2; // zf
__int64 result; // rax
int v4; // [rsp+4h] [rbp-14h] BYREF
__int64 v5; // [rsp+8h] [rbp-10h] BYREF

v4 = 0;
v5 = 0LL;
v2 = (unsigned int)func4(a1, -1, a2, &v4, &v5, 1) == 1;
result = 0LL;
if ( v2 )
result = v5;
return result;
}

original_code:
char *query_string(int fsid, char *path)
{
 int len = 0;
 void *p = {0};
 int n = query_subobj_path(fsid, -1, path, &len, &p, 1);
 return (n==1) ? (char *)p : 0;
}

variable_types: len int

variable_types:
p
void *


decompiled_code:
__int64 __fastcall query_int(int a1, const char *a2)
{
__int64 result; // rax
int v3; // [rsp+4h] [rbp-14h] BYREF
unsigned int *v4; // [rsp+8h] [rbp-10h] BYREF

v3 = 0;
v4 = 0LL;
if ( (unsigned int)query_subobj_path(a1, -1, a2, &v3, (__int64 *)&v4, 1) == 1 )
result = _bswap_32(*v4);
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, const char *a2)
{
__int64 result; // rax
int v3; // [rsp+4h] [rbp-14h] BYREF
unsigned int *v4; // [rsp+8h] [rbp-10h] BYREF

v3 = 0;
v4 = 0LL;
if ( (unsigned int)func4(a1, -1, a2, &v3, (__int64 *)&v4, 1) == 1 )
result = func2(*v4);
else
result = 0LL;
return result;
}

original_code:
int query_int(int fsid, char *path)
{
 int len = 0;
 void *p = 0;
 int n = query_subobj_path(fsid, -1, path, &len, &p, 1);
 return (n==1) ? ntohl(*((int*)p)) : 0;
}

canonical_original_code:
int query_int(int fsid, char *path)
{
 int len = 0;
 void *p = 0;
 int n = query_subobj_path(fsid, -1, path, &len, &p, 1);
 return (n==1) ? ntohl(*((int *)p)) : 0;
}

variable_types: fsid int


decompiled_code:
__int64 __fastcall query_int_list(int a1, const char *a2, __int64 a3, int a4)
{
void *v5; // rsp
void *v6; // rsp
int v7; // er8
__int64 v8; // rdx
int v9; // eax
__int64 v10; // rdx
__int64 v11; // rcx
__int64 v13; // [rsp+0h] [rbp-10h] BYREF

v5 = alloca(4LL * a4 + 8);
v6 = alloca(8LL * a4 + 8);
v7 = query_subobj_path(a1, -1, a2, &v13, &v13, a4);
if ( v7 > 0 )
{
v8 = 0LL;
do
{
v9 = _bswap_32(*(_DWORD *)*(&v13 + v8));
*(_DWORD *)(a3 + 4 * v10) = v9;
v8 = v10 + 1;
}
while ( v11 != v8 );
}
return (unsigned int)v7;
}

original_code:
int query_int_list(int fsid, char *path, int ret[], int max)
{
 int i;
 int *len = __builtin_alloca (max*sizeof(int));
 void **p = __builtin_alloca (max*sizeof(void *));
 int n=query_subobj_path(fsid, -1, path, len, p, max);
 for(i=0; i<n; i++)
  ret[i] = ntohl(*((int*)p[i]));
 return n;
}

canonical_original_code:
int query_int_list(int fsid, char *path, int ret[], int max)
{
 int i;
 int *len = __builtin_alloca (max*sizeof(int));
 void **p = __builtin_alloca (max*sizeof(void *));
 int n = query_subobj_path(fsid, -1, path, len, p, max);
 for(i=0; i<n; i++)
  ret[i] = ntohl(*((int *)p[i]));
 return n;
}

variable_types:
ret
int []

variable_types:
p
void **


decompiled_code:
int query_streams()
{
unsigned int v0; // eax
__int64 v1; // r13
int result; // eax
unsigned int i; // er12
int *v4; // rbx
int v5; // ebp
const char *v6; // rax
const char *v7; // rdi
char *v8; // rbx
char *v9; // rbp
__int64 v10; // rdx
unsigned int v11; // [rsp+Ch] [rbp-102Ch] BYREF
char v12[4136]; // [rsp+10h] [rbp-1028h] BYREF

v0 = mfs_resolve();
v1 = mfs_dir(v0, &v11);
result = v11;
if ( v11 )
{
for ( i = 0; i < v11; ++i )
{
v4 = (int *)(v1 + 16LL * i);
v5 = query_int_list(*v4, "Part/File", (__int64)v12, 1024);
fprintf(stdout, "%d\t", (unsigned int)*v4);
v6 = (const char *)query_string(*v4, "Showing/Program/Title");
fprintf(stdout, "%s\t", v6);
v7 = (const char *)query_string(*v4, "Showing/Program/EpisodeTitle");
if ( !v7 )
v7 = &s;
fputs(v7, stdout);
if ( v5 )
{
v8 = v12;
v9 = &v12[4 * (v5 - 1) + 4];
do
{
v10 = *(unsigned int *)v8;
v8 += 4;
fprintf(stdout, "\t%d", v10);
}
while ( v9 != v8 );
}
result = fputc(10, stdout);
}
}
if ( v1 )
result = mfs_dir_free(v1);
return result;
}

canonical_decompiled_code:
int func8()
{
unsigned int v0; // eax
__int64 v1; // r13
int result; // eax
unsigned int i; // er12
int *v4; // rbx
int v5; // ebp
const char *v6; // rax
const char *v7; // rdi
char *v8; // rbx
char *v9; // rbp
__int64 v10; // rdx
unsigned int v11; // [rsp+Ch] [rbp-102Ch] BYREF
char v12[4136]; // [rsp+10h] [rbp-1028h] BYREF

v0 = mfs_resolve();
v1 = mfs_dir(v0, &v11);
result = v11;
if ( v11 )
{
for ( i = 0; i < v11; ++i )
{
v4 = (int *)(v1 + 16LL * i);
v5 = func7(*v4, "Part/File", (__int64)v12, 1024);
fprintf(stdout, "%d\t", (unsigned int)*v4);
v6 = (const char *)func5(*v4, "Showing/Program/Title");
fprintf(stdout, "%s\t", v6);
v7 = (const char *)func5(*v4, "Showing/Program/EpisodeTitle");
if ( !v7 )
v7 = &s;
fputs(v7, stdout);
if ( v5 )
{
v8 = v12;
v9 = &v12[4 * (v5 - 1) + 4];
do
{
v10 = *(unsigned int *)v8;
v8 += 4;
fprintf(stdout, "\t%d", v10);
}
while ( v9 != v8 );
}
result = fputc(10, stdout);
}
}
if ( v1 )
result = mfs_dir_free(v1);
return result;
}

original_code:
void query_streams(const char *path)
{
 struct mfs_dirent *dir;
 int parts[1024];
 u32 count, i, j;
 const char *ep;
 dir = mfs_dir(mfs_resolve(path), &count);
 for (i=0;i<count;i++) {
  int n = query_int_list(dir[i].fsid, "Part/File", parts, 1024 );
  fprintf(stdout, "%d\t", dir[i].fsid );
  fprintf(stdout,"%s\t",
         query_string(dir[i].fsid, "Showing/Program/Title"));
  ep = query_string(dir[i].fsid, "Showing/Program/EpisodeTitle");
  fprintf(stdout, "%s", ep?ep:"" );
  for(j=0;j<n; j++)
   fprintf(stdout, "\t%d", parts[j] );
  fprintf(stdout,"\n");
 }
 if (dir) mfs_dir_free(dir);
}

canonical_original_code:
void query_streams(char *path)
{
 struct mfs_dirent *dir;
 int parts[1024];
 unsigned int count; unsigned int i; unsigned int j;
 char *ep;
 dir = mfs_dir(mfs_resolve(path), &count);
 for (i=0;i<count;i++) {
  int n = query_int_list(dir[i].fsid, "Part/File", parts, 1024 );
  fprintf(stdout, "%d\t", dir[i].fsid );
  fprintf(stdout,"%s\t",
         query_string(dir[i].fsid, "Showing/Program/Title"));
  ep = query_string(dir[i].fsid, "Showing/Program/EpisodeTitle");
  fprintf(stdout, "%s", ep?ep:"" );
  for(j=0;j<n; j++)
   fprintf(stdout, "\t%d", parts[j] );
  fprintf(stdout,"\n");
 }
 if (dir) mfs_dir_free(dir);
}

variable_types: struct mfs_dirent { unsigned int fsid; unsigned char type; char *name; } *

variable_types:
parts
int [1024]

variable_types:
count
unsigned int

variable_types:
i
unsigned int

variable_types:
j
unsigned int

variable_types:
ep
char *


decompiled_code:
void __fastcall partition_parse(__int64 a1)
{
unsigned int v1; // er15
unsigned int v2; // er13
unsigned int v3; // er12
int v4; // eax
int v5; // ebp
int v6; // er14
int v7; // ebx
unsigned __int16 v8; // ax
int v9; // eax
unsigned int v10; // edi
int v11; // eax
__int64 v12; // rcx
int v13; // edx
__int16 v14[2]; // [rsp+0h] [rbp-238h] BYREF
unsigned int v15; // [rsp+4h] [rbp-234h]
unsigned int v16; // [rsp+8h] [rbp-230h]
unsigned int v17; // [rsp+Ch] [rbp-22Ch]
int v18; // [rsp+30h] [rbp-208h]

v1 = 0;
v2 = a1;
read_sectors(a1, v14, 1LL, 1LL);
if ( (unsigned int)num_devs() )
{
do
{
v3 = 0;
v4 = dev_start_sector(v1);
v5 = v4 + 1;
v6 = v4;
read_sectors(v2, v14, (unsigned int)(v4 + 1), 1LL);
v7 = _bswap_32(v15);
if ( v7 > 0 )
{
do
{
read_sectors(v2, v14, v3 + v5, 1LL);
v8 = _bswap_16(v14[0]);
if ( v8 != 20557 )
{
if ( use_syslog )
{
if ( use_syslog > 1 )
syslog(6, "wrong magic %x in partition %d\n", v8, v3);
}
else
{
printf("wrong magic %x in partition %d\n", v8, v3);
}
exit(1);
}
if ( v18 == 5457485 )
{
v9 = _bswap_32(v16);
v10 = v17;
pmaps[2 * num_partitions] = v6 + v9;
v11 = _bswap_32(v10);
pmaps[2 * v12 + 1] = v11;
num_partitions = v13 + 1;
}
++v3;
}
while ( v7 != v3 );
}
++v1;
}
while ( (unsigned int)num_devs() > v1 );
}
if ( !use_syslog )
goto LABEL_13;
if ( fileno(stderr) == 2 )
{
syslog(3, "Found %d MFS partitions\n", (unsigned int)num_partitions);
goto LABEL_14;
}
if ( fileno(stderr) == 1 )
{
if ( use_syslog > 1 )
syslog(6, "Found %d MFS partitions\n", (unsigned int)num_partitions);
}
else
{
LABEL_13:
fprintf(stderr, "Found %d MFS partitions\n", (unsigned int)num_partitions);
}
LABEL_14:
use_ptable = 1;
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1)
{
unsigned int v1; // er15
unsigned int v2; // er13
unsigned int v3; // er12
int v4; // eax
int v5; // ebp
int v6; // er14
int v7; // ebx
unsigned __int16 v8; // ax
int v9; // eax
unsigned int v10; // edi
int v11; // eax
__int64 v12; // rcx
int v13; // edx
__int16 v14[2]; // [rsp+0h] [rbp-238h] BYREF
unsigned int v15; // [rsp+4h] [rbp-234h]
unsigned int v16; // [rsp+8h] [rbp-230h]
unsigned int v17; // [rsp+Ch] [rbp-22Ch]
int v18; // [rsp+30h] [rbp-208h]

v1 = 0;
v2 = a1;
read_sectors(a1, v14, 1LL, 1LL);
if ( (unsigned int)num_devs() )
{
do
{
v3 = 0;
v4 = dev_start_sector(v1);
v5 = v4 + 1;
v6 = v4;
read_sectors(v2, v14, (unsigned int)(v4 + 1), 1LL);
v7 = func1(v15);
if ( v7 > 0 )
{
do
{
read_sectors(v2, v14, v3 + v5, 1LL);
v8 = func0(v14[0]);
if ( v8 != 20557 )
{
if ( use_syslog )
{
if ( use_syslog > 1 )
syslog(6, "wrong magic %x in partition %d\n", v8, v3);
}
else
{
printf("wrong magic %x in partition %d\n", v8, v3);
}
exit(1);
}
if ( v18 == 5457485 )
{
v9 = func1(v16);
v10 = v17;
pmaps[2 * num_partitions] = v6 + v9;
v11 = func1(v10);
pmaps[2 * v12 + 1] = v11;
num_partitions = v13 + 1;
}
++v3;
}
while ( v7 != v3 );
}
++v1;
}
while ( (unsigned int)num_devs() > v1 );
}
if ( !use_syslog )
goto LABEL_13;
if ( fileno(stderr) == 2 )
{
syslog(3, "Found %d MFS partitions\n", (unsigned int)num_partitions);
goto LABEL_14;
}
if ( fileno(stderr) == 1 )
{
if ( use_syslog > 1 )
syslog(6, "Found %d MFS partitions\n", (unsigned int)num_partitions);
}
else
{
LABEL_13:
fprintf(stderr, "Found %d MFS partitions\n", (unsigned int)num_partitions);
}
LABEL_14:
use_ptable = 1;
}

original_code:
void partition_parse(int fd)
{
 char buf[512];
 struct tivo_partition *tp;
 int i, count, dev_no;
 tp = (struct tivo_partition *)buf;
 read_sectors(fd, tp, 1, 1);
 count = ntohl(tp->map_count);
 for (dev_no = 0; dev_no < num_devs(); ++dev_no) {
  u32 offset = dev_start_sector(dev_no);
  read_sectors(fd, tp, offset+1, 1);
  count = ntohl(tp->map_count);
  for (i=0;i<count;i++) {
   read_sectors(fd, tp, offset+i+1, 1);
   if (ntohs(tp->signature) != 0x504d) {
    (use_syslog ? (use_syslog>=2 ? (syslog(6,"wrong magic %x in partition %d\n", ntohs(tp->signature), i),1) : 1) : printf("wrong magic %x in partition %d\n", ntohs(tp->signature), i));
    exit(1);
   }
   if (strcmp(tp->type, "MFS") == 0) {
    pmaps[num_partitions].start =
        ntohl(tp->start_block)+offset;
    pmaps[num_partitions].length =
        ntohl(tp->block_count);
    num_partitions++;
   }
  }
 }
 (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "Found %d MFS partitions\n", num_partitions),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"Found %d MFS partitions\n", num_partitions),1) : 1) : fprintf((stderr),"Found %d MFS partitions\n", num_partitions)) : fprintf((stderr),"Found %d MFS partitions\n", num_partitions));
 use_ptable = 1;
}

canonical_original_code:
void partition_parse(int fd)
{
 char buf[512];
 struct tivo_partition *tp;
 int i; int count; int dev_no;
 tp = (struct tivo_partition *)buf;
 read_sectors(fd, tp, 1, 1);
 count = ntohl(tp->map_count);
 for (dev_no = 0; dev_no < num_devs(); ++dev_no) {
  unsigned int offset = dev_start_sector(dev_no);
  read_sectors(fd, tp, offset+1, 1);
  count = ntohl(tp->map_count);
  for (i=0;i<count;i++) {
   read_sectors(fd, tp, offset+i+1, 1);
   if (ntohs(tp->signature) != 0x504d) {
    (use_syslog ? (use_syslog>=2 ? (syslog(6,"wrong magic %x in partition %d\n", ntohs(tp->signature), i),1) : 1) : printf("wrong magic %x in partition %d\n", ntohs(tp->signature), i));
    exit(1);
   }
   if (strcmp(tp->type, "MFS") == 0) {
    pmaps[num_partitions].start =
        ntohl(tp->start_block)+offset;
    pmaps[num_partitions].length =
        ntohl(tp->block_count);
    num_partitions++;
   }
  }
 }
 (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "Found %d MFS partitions\n", num_partitions),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"Found %d MFS partitions\n", num_partitions),1) : 1) : fprintf((stderr),"Found %d MFS partitions\n", num_partitions)) : fprintf((stderr),"Found %d MFS partitions\n", num_partitions));
 use_ptable = 1;
}

variable_types: fd int

variable_types:
buf
char [512]

variable_types: struct tivo_partition { unsigned short signature; unsigned short res1; unsigned int map_count; unsigned int start_block; unsigned int block_count; char name[32]; char type[32]; unsigned int data_start; unsigned int data_count; unsigned int status; unsigned int boot_start; unsigned int boot_size; unsigned int boot_load; unsigned int boot_load2; unsigned int boot_entry; unsigned int boot_entry2; unsigned int boot_cksum; char processor[16]; } *

variable_types: i int

variable_types:
offset
unsigned int


decompiled_code:
__int64 __fastcall partition_remap(unsigned int a1)
{
unsigned int v2; // eax
__int64 v3; // rdx
int v4; // ecx
__int64 v6; // rdi

if ( !use_ptable )
return a1;
if ( num_partitions <= 0 )
{
LODWORD(v6) = 0;
goto LABEL_7;
}
v2 = pmaps[1];
v3 = 1LL;
v4 = 0;
if ( v2 <= a1 )
{
while ( 1 )
{
v6 = (int)v3;
v4 += v2 & 0xFFFFFC00;
if ( v3 == num_partitions )
break;
v2 = pmaps[2 * v3++ + 1];
if ( v2 + v4 > a1 )
return pmaps[2 * v6] - v4 + a1;
}
LABEL_7:
if ( num_partitions != (_DWORD)v6 )
return a1;
if ( use_syslog )
{
if ( fileno(stderr) == 2 )
{
syslog(3, "Failed to partition map sector %d\n", a1);
goto LABEL_16;
}
if ( fileno(stderr) == 1 )
{
if ( use_syslog > 1 )
syslog(6, "Failed to partition map sector %d\n", a1);
LABEL_16:
exit(1);
}
}
fprintf(stderr, "Failed to partition map sector %d\n", a1);
goto LABEL_16;
}
v4 = 0;
v6 = 0LL;
return pmaps[2 * v6] - v4 + a1;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
unsigned int v2; // eax
__int64 v3; // rdx
int v4; // ecx
__int64 v6; // rdi

if ( !use_ptable )
return a1;
if ( num_partitions <= 0 )
{
LODWORD(v6) = 0;
goto LABEL_7;
}
v2 = pmaps[1];
v3 = 1LL;
v4 = 0;
if ( v2 <= a1 )
{
while ( 1 )
{
v6 = (int)v3;
v4 += v2 & 0xFFFFFC00;
if ( v3 == num_partitions )
break;
v2 = pmaps[2 * v3++ + 1];
if ( v2 + v4 > a1 )
return pmaps[2 * v6] - v4 + a1;
}
LABEL_7:
if ( num_partitions != (_DWORD)v6 )
return a1;
if ( use_syslog )
{
if ( fileno(stderr) == 2 )
{
syslog(3, "Failed to partition map sector %d\n", a1);
goto LABEL_16;
}
if ( fileno(stderr) == 1 )
{
if ( use_syslog > 1 )
syslog(6, "Failed to partition map sector %d\n", a1);
LABEL_16:
exit(1);
}
}
fprintf(stderr, "Failed to partition map sector %d\n", a1);
goto LABEL_16;
}
v4 = 0;
v6 = 0LL;
return pmaps[2 * v6] - v4 + a1;
}

original_code:
u32 partition_remap(u32 sec)
{
 int i;
 u32 start=0;
 if (!use_ptable) return sec;
 for (i=0; i<num_partitions; i++) {
  if (sec < start + pmaps[i].length) {
   return pmaps[i].start + (sec - start);
  }
  start += (pmaps[i].length & ~(1024 -1));
 }
 if (i == num_partitions) {
  (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "Failed to partition map sector %d\n", sec),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"Failed to partition map sector %d\n", sec),1) : 1) : fprintf((stderr),"Failed to partition map sector %d\n", sec)) : fprintf((stderr),"Failed to partition map sector %d\n", sec));
  exit(1);
 }
 return sec;
}

canonical_original_code:
unsigned int partition_remap(unsigned int sec)
{
 int i;
 unsigned int start = 0;
 if (!use_ptable) return sec;
 for (i=0; i<num_partitions; i++) {
  if (sec < start + pmaps[i].length) {
   return pmaps[i].start + (sec - start);
  }
  start += (pmaps[i].length & ~(1024 -1));
 }
 if (i == num_partitions) {
  (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "Failed to partition map sector %d\n", sec),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"Failed to partition map sector %d\n", sec),1) : 1) : fprintf((stderr),"Failed to partition map sector %d\n", sec)) : fprintf((stderr),"Failed to partition map sector %d\n", sec));
  exit(1);
 }
 return sec;
}

variable_types:
sec
unsigned int

variable_types:
start
unsigned int


decompiled_code:
__int64 partition_total_size()
{
unsigned int v0; // er8
__int64 v1; // rax
int v2; // edx

v0 = 0;
if ( use_ptable && num_partitions > 0 )
{
v1 = 0LL;
do
{
v2 = pmaps[2 * v1++ + 1];
v0 += v2 & 0xFFFFFC00;
}
while ( v1 != num_partitions );
}
return v0;
}

canonical_decompiled_code:
__int64 func4()
{
unsigned int v0; // er8
__int64 v1; // rax
int v2; // edx

v0 = 0;
if ( use_ptable && num_partitions > 0 )
{
v1 = 0LL;
do
{
v2 = pmaps[2 * v1++ + 1];
v0 += v2 & 0xFFFFFC00;
}
while ( v1 != num_partitions );
}
return v0;
}

original_code:
u32 partition_total_size(void)
{
 u32 total=0;
 int i;
 if (!use_ptable) return 0;
 for (i=0; i<num_partitions; i++) {
  total += (pmaps[i].length & ~(1024 -1));
 }
 return total;
}

canonical_original_code:
unsigned int partition_total_size()
{
 unsigned int total = 0;
 int i;
 if (!use_ptable) return 0;
 for (i=0; i<num_partitions; i++) {
  total += (pmaps[i].length & ~(1024 -1));
 }
 return total;
}

variable_types:
total
unsigned int


decompiled_code:
void clear_use_ptable()
{
use_ptable = 0;
}

canonical_decompiled_code:
void func5()
{
use_ptable = 0;
}


writeall

decompiled_code:
__int64 __fastcall writeall(int fd, void *buf, size_t n)
{
size_t v4; // rbx
int v5; // er14
ssize_t v6; // rax
__int64 v7; // r12
__int64 result; // rax
int v9; // eax

if ( n )
{
v4 = n;
v5 = 0;
do
{
while ( 1 )
{
v6 = write(fd, buf, v4);
v7 = v6;
if ( v6 >= 0 )
break;
v9 = *_errno_location();
if ( v9 != 4 && v9 != 11 )
goto LABEL_8;
}
buf = (char *)buf + v6;
v5 += v6;
v4 -= v6;
}
while ( v4 );
result = v5;
}
else
{
v7 = 0LL;
LABEL_8:
result = v7;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd, void *buf, size_t n)
{
size_t v4; // rbx
int v5; // er14
ssize_t v6; // rax
__int64 v7; // r12
__int64 result; // rax
int v9; // eax

if ( n )
{
v4 = n;
v5 = 0;
do
{
while ( 1 )
{
v6 = write(fd, buf, v4);
v7 = v6;
if ( v6 >= 0 )
break;
v9 = *_errno_location();
if ( v9 != 4 && v9 != 11 )
goto LABEL_8;
}
buf = (char *)buf + v6;
v5 += v6;
v4 -= v6;
}
while ( v4 );
result = v5;
}
else
{
v7 = 0LL;
LABEL_8:
result = v7;
}
return result;
}

original_code:
static inline ssize_t writeall( int fd, void *buf, size_t count )
{
 unsigned char *p = buf;
 int rc = 0;
 ssize_t ret = 0;
 while (count > 0) {
   ret = write( fd, p, count );
   if (ret < 0) {
     if ((*__errno_location ()) != 4 && (*__errno_location ()) != 11)
       return ret;
     continue;
   }
   p += ret;
   count -= ret;
   rc += ret;
 }
 return rc;
}

canonical_original_code:
static inline long writeall(int fd, void *buf, unsigned long count)
{
 unsigned char *p = buf;
 int rc = 0;
 long ret = 0;
 while (count > 0) {
   ret = write( fd, p, count );
   if (ret < 0) {
     if ((*__errno_location ()) != 4 && (*__errno_location ()) != 11)
       return ret;
     continue;
   }
   p += ret;
   count -= ret;
   rc += ret;
 }
 return rc;
}

variable_types:
buf
void *

variable_types:
count
unsigned long

variable_types:
p
unsigned char *

variable_types: rc int

variable_types: ret long

return_type: long


decompiled_code:
__int64 __fastcall export_file(unsigned int a1, int a2, unsigned __int64 a3, size_t a4, int a5, int a6, int a7)
{
size_t v7; // rbx
int v8; // ecx
void *v9; // rsp
unsigned __int64 v10; // r13
size_t v11; // r14
unsigned __int64 v12; // rbx
__int64 v13; // r15
unsigned __int64 v14; // rax
unsigned int v15; // er8
FILE *v16; // rcx
size_t v17; // rbx
int v18; // eax
unsigned int v20; // er12
unsigned __int64 v21; // rcx
int v22; // esi
int v23; // eax
unsigned __int64 v24; // rax
size_t v25; // r14
int v26; // er13
int v27; // eax
__int64 v28; // rdx
int v29; // eax
size_t v30; // rax
unsigned int v31; // er13
size_t v32; // r13
__int64 v33; // rax
int v34; // er8
int v35; // eax
int *v36; // rax
char *v37; // rax
__int64 v38; // [rsp+0h] [rbp-280h] BYREF
__int64 v39; // [rsp+8h] [rbp-278h]
size_t n; // [rsp+10h] [rbp-270h]
unsigned int v41; // [rsp+18h] [rbp-268h]
int v42; // [rsp+1Ch] [rbp-264h]
int v43; // [rsp+20h] [rbp-260h]
int fd; // [rsp+24h] [rbp-25Ch]
unsigned __int64 v45; // [rsp+28h] [rbp-258h]
unsigned __int64 v46; // [rsp+30h] [rbp-250h]
void *buf; // [rsp+38h] [rbp-248h]
struct timespec requested_time; // [rsp+40h] [rbp-240h] BYREF
char v49[56]; // [rsp+50h] [rbp-230h] BYREF
unsigned int v50; // [rsp+88h] [rbp-1F8h]
int v51[125]; // [rsp+8Ch] [rbp-1F4h]

fd = a2;
v45 = a3;
n = a4;
v43 = a5;
if ( a6 )
{
v42 = a6;
v7 = a6 << 9;
}
else
{
v42 = 256;
v7 = 0x20000LL;
}
v8 = v43;
v9 = alloca(v7 + 16);
if ( v43 > 0 )
{
requested_time.tv_sec = v43 / 1000;
v43 %= 1000;
requested_time.tv_nsec = 1000000 * (v8 % 1000);
}
mfs_load_inode(a1, v49);
v10 = mfs_fsid_size(a1);
if ( v10 < v45 )
return (unsigned int)-1;
if ( n + v45 > v10 || !n )
n = v10 - v45;
if ( a7 )
{
if ( !use_syslog )
goto LABEL_76;
if ( fileno(stderr) == 2 )
{
syslog(3, "exporting fsid %d of size %lld starting at offset %lld for %lld bytes\n", a1, v10, v45, n);
goto LABEL_15;
}
if ( fileno(stderr) != 1 )
{
LABEL_76:
fprintf(stderr, "exporting fsid %d of size %lld starting at offset %lld for %lld bytes\n", a1, v10, v45, n);
goto LABEL_15;
}
if ( use_syslog > 1 )
syslog(6, "exporting fsid %d of size %lld starting at offset %lld for %lld bytes\n", a1, v10, v45, n);
}
LABEL_15:
mfs_readahead(1LL);
if ( !v50 )
{
if ( n <= v7 )
goto LABEL_29;
v16 = stderr;
if ( use_syslog )
{
if ( fileno(stderr) == 2 )
{
syslog(3, "inode data too big for buffer!? Output truncated.\n");
n = v7;
goto LABEL_29;
}
if ( fileno(stderr) == 1 )
{
n = v7;
if ( use_syslog > 1 )
syslog(6, "inode data too big for buffer!? Output truncated.\n");
LABEL_29:
v17 = n;
mfs_fsid_pread(a1, &v38, v45, (unsigned int)n);
v18 = writeall(fd, &v38, v17);
if ( v18 < 0 || (v15 = v17, v18 != v17) )
{
LABEL_73:
if ( use_syslog )
{
v36 = _errno_location();
v37 = strerror(*v36);
syslog(3, "%s: %s", "write failed:", v37);
}
else
{
perror("write failed:");
}
v15 = -1;
}
return v15;
}
v16 = stderr;
}
fwrite("inode data too big for buffer!? Output truncated.\n", 1uLL, 0x33uLL, v16);
n = v7;
goto LABEL_29;
}
if ( !n )
goto LABEL_57;
buf = &v38;
v11 = n;
v12 = 0LL;
v39 = 0LL;
v41 = 0;
while ( 1 )
{
v13 = v51[2 * v39 + 1];
v14 = v12 + (v13 << 9);
if ( v14 >= v45 )
break;
LABEL_19:
v12 = v14;
LABEL_20:
if ( v50 <= (unsigned int)++v39 )
return (unsigned int)(n - v11);
}
if ( (int)v13 <= 0 )
{
v14 = v12;
goto LABEL_19;
}
v20 = v51[2 * v39];
while ( 1 )
{
if ( v45 <= v12 )
{
v21 = v11;
}
else
{
v21 = v11;
v22 = v45 - v12;
v12 += (int)v45 - (int)v12;
LODWORD(v13) = v13 - (v22 >> 9);
v20 += v22 >> 9;
if ( (v22 & 0x1FF) != 0 )
{
LODWORD(v46) = v22 & 0x1FF;
v32 = 512 - (int)v46;
if ( v32 > v11 )
v32 = v11;
mfs_read_sectors(buf, v20, 1LL, v11);
v33 = writeall(fd, (char *)buf + (int)v46, v32);
v34 = v33;
if ( (int)v33 <= 0 )
goto LABEL_73;
if ( (_DWORD)v32 != (_DWORD)v33 )
{
if ( use_syslog )
{
v46 = v33;
if ( fileno(stderr) == 2 )
{
syslog(3, "short write: %d/%d\n", (unsigned int)v46, (unsigned int)v32);
v34 = v46;
}
else
{
v35 = fileno(stderr);
v34 = v46;
if ( v35 == 1 )
{
if ( use_syslog > 1 )
{
syslog(6, "short write: %d/%d\n", (unsigned int)v46, (unsigned int)v32);
v34 = v46;
}
}
else
{
fprintf(stderr, "short write: %d/%d\n", (unsigned int)v46, (unsigned int)v32);
v34 = v46;
}
}
}
else
{
v46 = v33;
fprintf(stderr, "short write: %d/%d\n", (unsigned int)v33, (unsigned int)v32);
v34 = v46;
}
}
++v20;
LODWORD(v13) = v13 - 1;
v21 = v11 - v34;
v12 += v32;
}
}
v23 = v42;
v46 = v21;
if ( (int)v13 <= v42 )
v23 = v13;
v24 = v23 << 9;
if ( v24 > v21 )
LODWORD(v24) = v21;
v25 = (int)v24;
v26 = v24;
mfs_read_partial(buf, v20, (int)v24);
v27 = writeall(fd, buf, v25);
if ( v27 < 0 || v26 != v27 )
goto LABEL_73;
v28 = v27;
v29 = v27 >> 9;
v12 += v28;
v11 = v46 - v28;
LODWORD(v13) = v13 - v29;
v20 += v29;
if ( a7 )
{
v30 = 100 * (n - v46 + v28) / n;
v31 = v30;
if ( v41 != (_DWORD)v30 )
{
if ( use_syslog )
{
if ( fileno(stderr) == 2 )
{
syslog(3, "%d%%\r", v31);
}
else if ( fileno(stderr) == 1 )
{
if ( use_syslog > 1 )
syslog(6, "%d%%\r", v31);
}
else
{
fprintf(stderr, "%d%%\r", v31);
}
}
else
{
fprintf(stderr, "%d%%\r", (unsigned int)v30);
}
fflush(stderr);
v41 = v31;
}
}
if ( v43 <= 0 )
break;
nanosleep(&requested_time, 0LL);
if ( (int)v13 <= 0 )
goto LABEL_56;
LABEL_37:
if ( !v11 )
goto LABEL_57;
}
if ( !v43 )
sched_yield();
if ( (int)v13 > 0 )
goto LABEL_37;
LABEL_56:
if ( v11 )
goto LABEL_20;
LABEL_57:
LODWORD(v11) = 0;
return (unsigned int)(n - v11);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, int a2, unsigned __int64 a3, size_t a4, int a5, int a6, int a7)
{
size_t v7; // rbx
int v8; // ecx
void *v9; // rsp
unsigned __int64 v10; // r13
size_t v11; // r14
unsigned __int64 v12; // rbx
__int64 v13; // r15
unsigned __int64 v14; // rax
unsigned int v15; // er8
FILE *v16; // rcx
size_t v17; // rbx
int v18; // eax
unsigned int v20; // er12
unsigned __int64 v21; // rcx
int v22; // esi
int v23; // eax
unsigned __int64 v24; // rax
size_t v25; // r14
int v26; // er13
int v27; // eax
__int64 v28; // rdx
int v29; // eax
size_t v30; // rax
unsigned int v31; // er13
size_t v32; // r13
__int64 v33; // rax
int v34; // er8
int v35; // eax
int *v36; // rax
char *v37; // rax
__int64 v38; // [rsp+0h] [rbp-280h] BYREF
__int64 v39; // [rsp+8h] [rbp-278h]
size_t n; // [rsp+10h] [rbp-270h]
unsigned int v41; // [rsp+18h] [rbp-268h]
int v42; // [rsp+1Ch] [rbp-264h]
int v43; // [rsp+20h] [rbp-260h]
int fd; // [rsp+24h] [rbp-25Ch]
unsigned __int64 v45; // [rsp+28h] [rbp-258h]
unsigned __int64 v46; // [rsp+30h] [rbp-250h]
void *buf; // [rsp+38h] [rbp-248h]
struct timespec requested_time; // [rsp+40h] [rbp-240h] BYREF
char v49[56]; // [rsp+50h] [rbp-230h] BYREF
unsigned int v50; // [rsp+88h] [rbp-1F8h]
int v51[125]; // [rsp+8Ch] [rbp-1F4h]

fd = a2;
v45 = a3;
n = a4;
v43 = a5;
if ( a6 )
{
v42 = a6;
v7 = a6 << 9;
}
else
{
v42 = 256;
v7 = 0x20000LL;
}
v8 = v43;
v9 = alloca(v7 + 16);
if ( v43 > 0 )
{
requested_time.tv_sec = v43 / 1000;
v43 %= 1000;
requested_time.tv_nsec = 1000000 * (v8 % 1000);
}
mfs_load_inode(a1, v49);
v10 = mfs_fsid_size(a1);
if ( v10 < v45 )
return (unsigned int)-1;
if ( n + v45 > v10 || !n )
n = v10 - v45;
if ( a7 )
{
if ( !use_syslog )
goto LABEL_76;
if ( fileno(stderr) == 2 )
{
syslog(3, "exporting fsid %d of size %lld starting at offset %lld for %lld bytes\n", a1, v10, v45, n);
goto LABEL_15;
}
if ( fileno(stderr) != 1 )
{
LABEL_76:
fprintf(stderr, "exporting fsid %d of size %lld starting at offset %lld for %lld bytes\n", a1, v10, v45, n);
goto LABEL_15;
}
if ( use_syslog > 1 )
syslog(6, "exporting fsid %d of size %lld starting at offset %lld for %lld bytes\n", a1, v10, v45, n);
}
LABEL_15:
mfs_readahead(1LL);
if ( !v50 )
{
if ( n <= v7 )
goto LABEL_29;
v16 = stderr;
if ( use_syslog )
{
if ( fileno(stderr) == 2 )
{
syslog(3, "inode data too big for buffer!? Output truncated.\n");
n = v7;
goto LABEL_29;
}
if ( fileno(stderr) == 1 )
{
n = v7;
if ( use_syslog > 1 )
syslog(6, "inode data too big for buffer!? Output truncated.\n");
LABEL_29:
v17 = n;
mfs_fsid_pread(a1, &v38, v45, (unsigned int)n);
v18 = func0(fd, &v38, v17);
if ( v18 < 0 || (v15 = v17, v18 != v17) )
{
LABEL_73:
if ( use_syslog )
{
v36 = _errno_location();
v37 = strerror(*v36);
syslog(3, "%s: %s", "write failed:", v37);
}
else
{
perror("write failed:");
}
v15 = -1;
}
return v15;
}
v16 = stderr;
}
fwrite("inode data too big for buffer!? Output truncated.\n", 1uLL, 0x33uLL, v16);
n = v7;
goto LABEL_29;
}
if ( !n )
goto LABEL_57;
buf = &v38;
v11 = n;
v12 = 0LL;
v39 = 0LL;
v41 = 0;
while ( 1 )
{
v13 = v51[2 * v39 + 1];
v14 = v12 + (v13 << 9);
if ( v14 >= v45 )
break;
LABEL_19:
v12 = v14;
LABEL_20:
if ( v50 <= (unsigned int)++v39 )
return (unsigned int)(n - v11);
}
if ( (int)v13 <= 0 )
{
v14 = v12;
goto LABEL_19;
}
v20 = v51[2 * v39];
while ( 1 )
{
if ( v45 <= v12 )
{
v21 = v11;
}
else
{
v21 = v11;
v22 = v45 - v12;
v12 += (int)v45 - (int)v12;
LODWORD(v13) = v13 - (v22 >> 9);
v20 += v22 >> 9;
if ( (v22 & 0x1FF) != 0 )
{
LODWORD(v46) = v22 & 0x1FF;
v32 = 512 - (int)v46;
if ( v32 > v11 )
v32 = v11;
mfs_read_sectors(buf, v20, 1LL, v11);
v33 = func0(fd, (char *)buf + (int)v46, v32);
v34 = v33;
if ( (int)v33 <= 0 )
goto LABEL_73;
if ( (_DWORD)v32 != (_DWORD)v33 )
{
if ( use_syslog )
{
v46 = v33;
if ( fileno(stderr) == 2 )
{
syslog(3, "short write: %d/%d\n", (unsigned int)v46, (unsigned int)v32);
v34 = v46;
}
else
{
v35 = fileno(stderr);
v34 = v46;
if ( v35 == 1 )
{
if ( use_syslog > 1 )
{
syslog(6, "short write: %d/%d\n", (unsigned int)v46, (unsigned int)v32);
v34 = v46;
}
}
else
{
fprintf(stderr, "short write: %d/%d\n", (unsigned int)v46, (unsigned int)v32);
v34 = v46;
}
}
}
else
{
v46 = v33;
fprintf(stderr, "short write: %d/%d\n", (unsigned int)v33, (unsigned int)v32);
v34 = v46;
}
}
++v20;
LODWORD(v13) = v13 - 1;
v21 = v11 - v34;
v12 += v32;
}
}
v23 = v42;
v46 = v21;
if ( (int)v13 <= v42 )
v23 = v13;
v24 = v23 << 9;
if ( v24 > v21 )
LODWORD(v24) = v21;
v25 = (int)v24;
v26 = v24;
mfs_read_partial(buf, v20, (int)v24);
v27 = func0(fd, buf, v25);
if ( v27 < 0 || v26 != v27 )
goto LABEL_73;
v28 = v27;
v29 = v27 >> 9;
v12 += v28;
v11 = v46 - v28;
LODWORD(v13) = v13 - v29;
v20 += v29;
if ( a7 )
{
v30 = 100 * (n - v46 + v28) / n;
v31 = v30;
if ( v41 != (_DWORD)v30 )
{
if ( use_syslog )
{
if ( fileno(stderr) == 2 )
{
syslog(3, "%d%%\r", v31);
}
else if ( fileno(stderr) == 1 )
{
if ( use_syslog > 1 )
syslog(6, "%d%%\r", v31);
}
else
{
fprintf(stderr, "%d%%\r", v31);
}
}
else
{
fprintf(stderr, "%d%%\r", (unsigned int)v30);
}
fflush(stderr);
v41 = v31;
}
}
if ( v43 <= 0 )
break;
nanosleep(&requested_time, 0LL);
if ( (int)v13 <= 0 )
goto LABEL_56;
LABEL_37:
if ( !v11 )
goto LABEL_57;
}
if ( !v43 )
sched_yield();
if ( (int)v13 > 0 )
goto LABEL_37;
LABEL_56:
if ( v11 )
goto LABEL_20;
LABEL_57:
LODWORD(v11) = 0;
return (unsigned int)(n - v11);
}

original_code:
int export_file(const u32 fsid, const int fd, u64 start,
   u64 count, int delayms, u32 nbufs, int verbose )
{
        struct mfs_inode inode;
        int run, ret, rlen, blen;
        int pct, last_pct=0;
        u64 ofs=0, size;
 u64 total;
 struct timespec delay;
 int nb = (nbufs>0) ? nbufs : 256;
        unsigned char *buf = __builtin_alloca (nb*512);
 if (!buf) {
  (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "Couldn't allocate buffer: %d\n", nb*512),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"Couldn't allocate buffer: %d\n", nb*512),1) : 1) : fprintf((stderr),"Couldn't allocate buffer: %d\n", nb*512)) : fprintf((stderr),"Couldn't allocate buffer: %d\n", nb*512));
  exit(1);
 }
 if (delayms > 0) {
  delay.tv_sec = delayms/1000;
  delayms %= 1000;
  delay.tv_nsec = delayms*1000000;
 }
        mfs_load_inode(fsid, &inode);
        size = mfs_fsid_size(fsid);
 if (start>size) {
  return -1;
 }
 if (start+count>size || count==0) {
  count = size-start;
 }
 total = count;
 if (verbose)
  (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "exporting fsid %d of size %lld starting at offset %lld for %lld bytes\n", fsid, size, start, count),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"exporting fsid %d of size %lld starting at offset %lld for %lld bytes\n", fsid, size, start, count),1) : 1) : fprintf((stderr),"exporting fsid %d of size %lld starting at offset %lld for %lld bytes\n", fsid, size, start, count)) : fprintf((stderr),"exporting fsid %d of size %lld starting at offset %lld for %lld bytes\n", fsid, size, start, count));
 mfs_readahead(1);
 if(inode.num_runs == 0) {
  if (count > nb*512) {
   (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "inode data too big for buffer!?  Output truncated.\n"),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"inode data too big for buffer!?  Output truncated.\n"),1) : 1) : fprintf((stderr),"inode data too big for buffer!?  Output truncated.\n")) : fprintf((stderr),"inode data too big for buffer!?  Output truncated.\n"));
   count = nb*512;
  }
  mfs_fsid_pread( fsid, buf, start, count );
  ret = writeall(fd,buf,count);
  if (ret < 0 || ret != count) {
   (use_syslog ? (syslog(3,"%s: %s",("write failed:"),strerror((*__errno_location ()))),1) : perror("write failed:"));
   return -1;
  }
  return count;
 }
        for (run=0;count>0 && run<inode.num_runs;run++) {
                int len=inode.u.runs[run].len;
                int sec=inode.u.runs[run].start;
  if (start > ofs+((u64)len<<9)) {
   ofs +=((u64)len<<9);
   continue;
  }
                while (len>0 && count>0) {
   if (start > ofs) {
    int bofs = start-ofs;
    int sofs = bofs>>9;
    int bmod = bofs&(512 -1);
    ofs += bofs;
    len -= sofs;
    sec += sofs;
    if (bmod) {
     blen = ((512 -bmod)<(count)?(512 -bmod):(count));
     mfs_read_sectors(buf,sec,1);
     ret = writeall( fd, buf+bmod, blen);
     if (ret <= 0) {
      (use_syslog ? (syslog(3,"%s: %s",("write failed:"),strerror((*__errno_location ()))),1) : perror("write failed:"));
      return -1;
     } else if (ret != blen) {
      (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "short write: %d/%d\n", ret, blen),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"short write: %d/%d\n", ret, blen),1) : 1) : fprintf((stderr),"short write: %d/%d\n", ret, blen)) : fprintf((stderr),"short write: %d/%d\n", ret, blen));
     }
     sec++;
     len--;
     count -= ret;
     ofs += blen;
    }
   }
                        rlen = ((nb)<(len)?(nb):(len));
   blen = ((rlen<<9)<(count)?(rlen<<9):(count));
   mfs_read_partial(buf, sec, blen);
                        ret = writeall(fd,buf,blen);
                        if (ret < 0 || ret != blen) {
    (use_syslog ? (syslog(3,"%s: %s",("write failed:"),strerror((*__errno_location ()))),1) : perror("write failed:"));
    return -1;
   }
   ofs += ret;
   count -= ret;
                        ret >>= 9;
                        len -= ret;
                        sec += ret;
   if (verbose) {
    pct = (100 * (total-count))/total;
    if (pct != last_pct) {
     (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "%d%%\r", pct),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"%d%%\r", pct),1) : 1) : fprintf((stderr),"%d%%\r", pct)) : fprintf((stderr),"%d%%\r", pct));
     fflush(stderr);
     last_pct = pct;
    }
   }
   if (delayms > 0)
    nanosleep( &delay, ((void *)0) );
   else if (delayms == 0)
    sched_yield();
                }
        }
 return total-count;
}

canonical_original_code:
int export_file(unsigned int fsid, int fd, unsigned int start, unsigned int count, int delayms, unsigned int nbufs, int verbose)
{
        struct mfs_inode inode;
        int run; int ret; int rlen; int blen;
        int pct; int last_pct = 0;
        unsigned int ofs = 0; unsigned int size;
 unsigned int total;
 struct timespec delay;
 int nb = (nbufs>0) ? nbufs : 256;
        unsigned char *buf = __builtin_alloca (nb*512);
 if (!buf) {
  (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "Couldn't allocate buffer: %d\n", nb*512),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"Couldn't allocate buffer: %d\n", nb*512),1) : 1) : fprintf((stderr),"Couldn't allocate buffer: %d\n", nb*512)) : fprintf((stderr),"Couldn't allocate buffer: %d\n", nb*512));
  exit(1);
 }
 if (delayms > 0) {
  delay.tv_sec = delayms/1000;
  delayms %= 1000;
  delay.tv_nsec = delayms*1000000;
 }
        mfs_load_inode(fsid, &inode);
        size = mfs_fsid_size(fsid);
 if (start>size) {
  return -1;
 }
 if (start+count>size || count==0) {
  count = size-start;
 }
 total = count;
 if (verbose)
  (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "exporting fsid %d of size %lld starting at offset %lld for %lld bytes\n", fsid, size, start, count),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"exporting fsid %d of size %lld starting at offset %lld for %lld bytes\n", fsid, size, start, count),1) : 1) : fprintf((stderr),"exporting fsid %d of size %lld starting at offset %lld for %lld bytes\n", fsid, size, start, count)) : fprintf((stderr),"exporting fsid %d of size %lld starting at offset %lld for %lld bytes\n", fsid, size, start, count));
 mfs_readahead(1);
 if(inode.num_runs == 0) {
  if (count > nb*512) {
   (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "inode data too big for buffer!?  Output truncated.\n"),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"inode data too big for buffer!?  Output truncated.\n"),1) : 1) : fprintf((stderr),"inode data too big for buffer!?  Output truncated.\n")) : fprintf((stderr),"inode data too big for buffer!?  Output truncated.\n"));
   count = nb*512;
  }
  mfs_fsid_pread( fsid, buf, start, count );
  ret = writeall(fd,buf,count);
  if (ret < 0 || ret != count) {
   (use_syslog ? (syslog(3,"%s: %s",("write failed:"),strerror((*__errno_location ()))),1) : perror("write failed:"));
   return -1;
  }
  return count;
 }
        for (run=0;count>0 && run<inode.num_runs;run++) {
                int len = inode.u.runs[run].len;
                int sec = inode.u.runs[run].start;
  if (start > ofs+((unsigned int)len<<9)) {
   ofs +=((unsigned int)len<<9);
   continue;
  }
                while (len>0 && count>0) {
   if (start > ofs) {
    int bofs = start-ofs;
    int sofs = bofs>>9;
    int bmod = bofs&(512 -1);
    ofs += bofs;
    len -= sofs;
    sec += sofs;
    if (bmod) {
     blen = ((512 -bmod)<(count)?(512 -bmod):(count));
     mfs_read_sectors(buf,sec,1);
     ret = writeall( fd, buf+bmod, blen);
     if (ret <= 0) {
      (use_syslog ? (syslog(3,"%s: %s",("write failed:"),strerror((*__errno_location ()))),1) : perror("write failed:"));
      return -1;
     } else if (ret != blen) {
      (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "short write: %d/%d\n", ret, blen),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"short write: %d/%d\n", ret, blen),1) : 1) : fprintf((stderr),"short write: %d/%d\n", ret, blen)) : fprintf((stderr),"short write: %d/%d\n", ret, blen));
     }
     sec++;
     len--;
     count -= ret;
     ofs += blen;
    }
   }
                        rlen = ((nb)<(len)?(nb):(len));
   blen = ((rlen<<9)<(count)?(rlen<<9):(count));
   mfs_read_partial(buf, sec, blen);
                        ret = writeall(fd,buf,blen);
                        if (ret < 0 || ret != blen) {
    (use_syslog ? (syslog(3,"%s: %s",("write failed:"),strerror((*__errno_location ()))),1) : perror("write failed:"));
    return -1;
   }
   ofs += ret;
   count -= ret;
                        ret >>= 9;
                        len -= ret;
                        sec += ret;
   if (verbose) {
    pct = (100 * (total-count))/total;
    if (pct != last_pct) {
     (use_syslog ? (fileno(stderr)==2 ? (syslog(3, "%d%%\r", pct),1) : fileno(stderr)==1 ? (use_syslog>=2 ? (syslog(6,"%d%%\r", pct),1) : 1) : fprintf((stderr),"%d%%\r", pct)) : fprintf((stderr),"%d%%\r", pct));
     fflush(stderr);
     last_pct = pct;
    }
   }
   if (delayms > 0)
    nanosleep( &delay, ((void *)0) );
   else if (delayms == 0)
    sched_yield();
                }
        }
 return total-count;
}

variable_types:
fsid
unsigned int

variable_types:
count
unsigned int

variable_types: delayms int

variable_types:
nbufs
unsigned int

variable_types: verbose int

variable_types: struct mfs_inode { unsigned int id; unsigned int typexx; unsigned int fill1[3]; unsigned int units; unsigned int size; unsigned int used_units; unsigned int used_size; unsigned int fill2; unsigned char type; unsigned char fill3; unsigned short fill4; unsigned int fill5; unsigned int crc; unsigned int flags; unsigned int num_runs; union { struct mfs_run { unsigned int start; unsigned int len; } runs[56]; char data[452]; } u; }

variable_types: run int

variable_types: ret int

variable_types: rlen int

variable_types: blen int

variable_types: pct int

variable_types:
ofs
unsigned int

variable_types:
size
unsigned int

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

variable_types: nb int

variable_types:
buf
unsigned char *

variable_types: len int

variable_types: sec int

variable_types: bofs int

variable_types: sofs int

variable_types: bmod int

user_defined_types: struct mfs_inode { unsigned int id; unsigned int typexx; unsigned int fill1[3]; unsigned int units; unsigned int size; unsigned int used_units; unsigned int used_size; unsigned int fill2; unsigned char type; unsigned char fill3; unsigned short fill4; unsigned int fill5; unsigned int crc; unsigned int flags; unsigned int num_runs; union { struct mfs_run runs[56]; char data[452]; } u; }