decompiled_code:
void __fastcall blorb_treaty(int a1, __int64 a2, int a3, void *a4, int a5)
{
if ( ((a1 & 0x100) == 0 || (int)claim_story_file(a2, a3) >= 0) && ((a1 & 0x200) == 0 || a5 && a4) )
{
if ( a1 == 776 )
{
get_story_file_IFID(a2, a3, (__int64)a4, a5);
}
else if ( a1 > 776 )
{
if ( a1 == 1297 )
{
get_story_extent(a2, a3);
}
else if ( a1 <= 1297 )
{
switch ( a1 )
{
case 778:
get_story_file_cover(a2, a3, a4, a5);
break;
case 779:
get_story_file_extension(a2, a3, a4, a5);
break;
case 777:
get_story_file_metadata(a2, a3, a4, a5);
break;
}
}
else if ( a1 == 1808 )
{
get_story_format(a2, a3, (char *)a4, a5);
}
else if ( a1 == 1809 )
{
get_story_file(a2, a3, a4, a5);
}
}
else if ( a1 == 263 )
{
get_story_file_cover_format(a2, a3);
}
else if ( a1 <= 263 )
{
if ( a1 == 261 )
{
get_story_file_metadata_extent(a2, a3);
}
else if ( a1 == 262 )
{
get_story_file_cover_extent(a2, a3);
}
}
else if ( a1 == 514 )
{
if ( a5 > 511 )
strncpy((char *)a4, "blorb", a5 - 1);
}
else if ( a1 == 515 )
{
if ( a5 > 37 )
strncpy((char *)a4, ".blorb,.blb,.zblorb,.zlb,.gblorb,.glb", a5);
}
else if ( a1 == 513 && a5 > 28 )
{
strcpy((char *)a4, "http://eblong.com/zarf/blorb");
}
}
}

original_code:
int32 blorb_treaty(int32 selector,
                   void *story_file, int32 extent,
                   void *output, int32 output_extent)
{
 int32 ll, csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((signed) strlen("http://eblong.com/zarf/blorb")+1)) return -3; } while (0);
                strcpy((char *) output,"http://eblong.com/zarf/blorb");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"blorb",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".blorb,.blb,.zblorb,.zlb,.gblorb,.glb")+1) < 512 ? (strlen(".blorb,.blb,.zblorb,.zlb,.gblorb,.glb")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".blorb,.blb,.zblorb,.zlb,.gblorb,.glb", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
                return get_story_file_metadata_extent(story_file, extent);
  case 0x309:
                return get_story_file_metadata(story_file, extent, (char *)output, output_extent);
  case 0x106:
                return get_story_file_cover_extent(story_file, extent);
  case 0x107:
                return get_story_file_cover_format(story_file, extent);
  case 0x30A:
                return get_story_file_cover(story_file, extent, output, output_extent);
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
  case 0x710:
                return get_story_format(story_file, extent, (char *)output, output_extent);
  case 0x511:
                return get_story_extent(story_file, extent);
  case 0x711:
                return get_story_file(story_file, extent, output, output_extent);
 }
 return -2;
}

canonical_original_code:
int blorb_treaty(int selector, void *story_file, int extent, void *output, int output_extent)
{
 int ll; int csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((int) strlen("http://eblong.com/zarf/blorb")+1)) return -3; } while (0);
                strcpy((char *) output,"http://eblong.com/zarf/blorb");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"blorb",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".blorb,.blb,.zblorb,.zlb,.gblorb,.glb")+1) < 512 ? (strlen(".blorb,.blb,.zblorb,.zlb,.gblorb,.glb")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".blorb,.blb,.zblorb,.zlb,.gblorb,.glb", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
                return get_story_file_metadata_extent(story_file, extent);
  case 0x309:
                return get_story_file_metadata(story_file, extent, (char *)output, output_extent);
  case 0x106:
                return get_story_file_cover_extent(story_file, extent);
  case 0x107:
                return get_story_file_cover_format(story_file, extent);
  case 0x30A:
                return get_story_file_cover(story_file, extent, output, output_extent);
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
  case 0x710:
                return get_story_format(story_file, extent, (char *)output, output_extent);
  case 0x511:
                return get_story_extent(story_file, extent);
  case 0x711:
                return get_story_file(story_file, extent, output, output_extent);
 }
 return -2;
}

variable_types: selector int

variable_types:
output
void *

variable_types: ll int

variable_types: csf int


repo: ricpelo/elcirculo

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

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

original_code:
static int32 read_int(void *inp)
{
  unsigned char *mem=(unsigned char *)inp;
  int32 i4 = mem[0],
                    i3 = mem[1],
                    i2 = mem[2],
                    i1 = mem[3];
  return i1 | (i2<<8) | (i3<<16) | (i4<<24);
}

canonical_original_code:
static int read_int(void *inp)
{
  unsigned char *mem = (unsigned char *)inp;
  int i4 = mem[0]; int i3 = mem[1]; int i2 = mem[2]; int i1 = mem[3];
  return i1 | (i2<<8) | (i3<<16) | (i4<<24);
}

variable_types:
inp
void *

variable_types:
mem
unsigned char *

return_type: int


decompiled_code:
__int64 __fastcall claim_story_file(__int64 a1, int a2)
{
if ( a2 <= 15 )
return 0xFFFFFFFFLL;
if ( !memcmp((const void *)a1, "FORM", 4uLL) )
return (unsigned int)-(memcmp((const void *)(a1 + 8), "IFRS", 4uLL) != 0);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
if ( a2 <= 15 )
return 0xFFFFFFFFLL;
if ( !memcmp((const void *)a1, "FORM", 4uLL) )
return (unsigned int)-(memcmp((const void *)(a1 + 8), "IFRS", 4uLL) != 0);
return 0xFFFFFFFFLL;
}

original_code:
static int32 claim_story_file(void *story_file, int32 extent)
{
 int i;
 if (extent<16 ||
     memcmp(story_file,"FORM",4) ||
     memcmp((char *)story_file+8,"IFRS",4)
    ) i= -1;
 else i= 0;
 return i;
}

canonical_original_code:
static int claim_story_file(void *story_file, int extent)
{
 int i;
 if (extent<16 ||
     memcmp(story_file,"FORM",4) ||
     memcmp((char *)story_file+8,"IFRS",4)
    ) i= -1;
 else i= 0;
 return i;
}

variable_types: extent int

variable_types: i int


decompiled_code:
__int64 __fastcall get_story_file_metadata_extent(__int64 a1, int a2)
{
__int64 result; // rax
int v3; // [rsp+8h] [rbp-10h] BYREF
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

result = blorb_get_chunk(a1, a2, "IFmd", v4, &v3);
if ( (_DWORD)result )
result = (unsigned int)(v3 + 1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2)
{
__int64 result; // rax
int v3; // [rsp+8h] [rbp-10h] BYREF
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

result = func2(a1, a2, "IFmd", v4, &v3);
if ( (_DWORD)result )
result = (unsigned int)(v3 + 1);
return result;
}

original_code:
static int32 get_story_file_metadata_extent(void *blorb_file, int32 extent)
{
 int32 i,j;
 if (blorb_get_chunk(blorb_file,extent,"IFmd",&i,&j)) return j+1;
 return 0;
}

canonical_original_code:
static int get_story_file_metadata_extent(void *blorb_file, int extent)
{
 int i; int j;
 if (blorb_get_chunk(blorb_file,extent,"IFmd",&i,&j)) return j+1;
 return 0;
}

variable_types: j int


decompiled_code:
__int64 __fastcall get_story_file_cover_extent(__int64 a1, int a2)
{
__int64 result; // rax
unsigned int v3; // [rsp+8h] [rbp-10h] BYREF
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

result = blorb_get_cover(a1, a2, v4, &v3);
if ( (_DWORD)result )
result = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, int a2)
{
__int64 result; // rax
unsigned int v3; // [rsp+8h] [rbp-10h] BYREF
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

result = func6(a1, a2, v4, &v3);
if ( (_DWORD)result )
result = v3;
return result;
}

original_code:
static int32 get_story_file_cover_extent(void *blorb_file, int32 extent)
{
 int32 i,j;
 if (blorb_get_cover(blorb_file,extent,&i,&j)) return j;
 return 0;
}

canonical_original_code:
static int get_story_file_cover_extent(void *blorb_file, int extent)
{
 int i; int j;
 if (blorb_get_cover(blorb_file,extent,&i,&j)) return j;
 return 0;
}


decompiled_code:
__int64 __fastcall get_story_file_cover_format(__int64 a1, int a2)
{
int v3; // [rsp+8h] [rbp-10h] BYREF
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

return blorb_get_cover(a1, a2, v4, &v3);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int a2)
{
int v3; // [rsp+8h] [rbp-10h] BYREF
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

return func6(a1, a2, v4, &v3);
}

original_code:
static int32 get_story_file_cover_format(void *blorb_file, int32 extent)
{
 int32 i,j;
 return blorb_get_cover(blorb_file, extent, &i,&j);
}

canonical_original_code:
static int get_story_file_cover_format(void *blorb_file, int extent)
{
 int i; int j;
 return blorb_get_cover(blorb_file, extent, &i,&j);
}


decompiled_code:
__int64 __fastcall get_story_file_metadata(__int64 a1, int a2, void *a3, int a4)
{
__int64 result; // rax
int v7; // ebx
int v8; // [rsp+8h] [rbp-30h] BYREF
int v9[11]; // [rsp+Ch] [rbp-2Ch] BYREF

result = blorb_get_chunk(a1, a2, "IFmd", v9, &v8);
if ( (_DWORD)result )
{
v7 = v8;
if ( v8 >= a4 )
{
result = 4294967293LL;
}
else
{
memcpy(a3, (const void *)(a1 + v9[0]), v8);
*((_BYTE *)a3 + v7) = 0;
result = (unsigned int)(v7 + 1);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, int a2, void *a3, int a4)
{
__int64 result; // rax
int v7; // ebx
int v8; // [rsp+8h] [rbp-30h] BYREF
int v9[11]; // [rsp+Ch] [rbp-2Ch] BYREF

result = func2(a1, a2, "IFmd", v9, &v8);
if ( (_DWORD)result )
{
v7 = v8;
if ( v8 >= a4 )
{
result = 4294967293LL;
}
else
{
memcpy(a3, (const void *)(a1 + v9[0]), v8);
*((_BYTE *)a3 + v7) = 0;
result = (unsigned int)(v7 + 1);
}
}
return result;
}

original_code:
static int32 get_story_file_metadata(void *blorb_file, int32 extent, char *output, int32 output_extent)
{
 int32 i,j;
 if (!blorb_get_chunk(blorb_file, extent,"IFmd",&i,&j)) return 0;
 do { if (output_extent < (j+1)) return -3; } while (0);
 memcpy(output,(char *)blorb_file+i,j);
 output[j]=0;
 return j+1;
}

canonical_original_code:
static int get_story_file_metadata(void *blorb_file, int extent, char *output, int output_extent)
{
 int i; int j;
 if (!blorb_get_chunk(blorb_file, extent,"IFmd",&i,&j)) return 0;
 do { if (output_extent < (j+1)) return -3; } while (0);
 memcpy(output,(char *)blorb_file+i,j);
 output[j]=0;
 return j+1;
}

variable_types:
output
char *


decompiled_code:
__int64 __fastcall get_story_file_cover(__int64 a1, int a2, void *a3, int a4)
{
unsigned int v6; // ebx
int v8; // [rsp+8h] [rbp-30h] BYREF
int v9[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v6 = blorb_get_cover(a1, a2, v9, &v8);
if ( v6 )
{
v6 = v8;
if ( v8 > a4 )
v6 = -3;
else
memcpy(a3, (const void *)(a1 + v9[0]), v8);
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, int a2, void *a3, int a4)
{
unsigned int v6; // ebx
int v8; // [rsp+8h] [rbp-30h] BYREF
int v9[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v6 = func6(a1, a2, v9, &v8);
if ( v6 )
{
v6 = v8;
if ( v8 > a4 )
v6 = -3;
else
memcpy(a3, (const void *)(a1 + v9[0]), v8);
}
return v6;
}

original_code:
static int32 get_story_file_cover(void *blorb_file, int32 extent, void *output, int32 output_extent)
{
 int32 i,j;
 if (!blorb_get_cover(blorb_file, extent,&i,&j)) return 0;
 do { if (output_extent < (j)) return -3; } while (0);
 memcpy(output,(char *)blorb_file+i,j);
 return j;
}

canonical_original_code:
static int get_story_file_cover(void *blorb_file, int extent, void *output, int output_extent)
{
 int i; int j;
 if (!blorb_get_cover(blorb_file, extent,&i,&j)) return 0;
 do { if (output_extent < (j)) return -3; } while (0);
 memcpy(output,(char *)blorb_file+i,j);
 return j;
}


decompiled_code:
__int64 __fastcall get_story_file_IFID(__int64 a1, int a2, __int64 a3, unsigned int a4)
{
int v5; // eax
unsigned int v6; // er13
int v7; // ebx
void *v8; // r15

v5 = get_story_file_metadata_extent(a1, a2);
v6 = 0;
if ( v5 > 0 )
{
v7 = v5;
v8 = (void *)my_malloc((unsigned int)v5, "Metadata buffer");
if ( (int)get_story_file_metadata(a1, a2, v8, v7) > 0 )
{
v6 = ifiction_get_IFID(v8, a3, a4);
free(v8);
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, int a2, __int64 a3, unsigned int a4)
{
int v5; // eax
unsigned int v6; // er13
int v7; // ebx
void *v8; // r15

v5 = func3(a1, a2);
v6 = 0;
if ( v5 > 0 )
{
v7 = v5;
v8 = (void *)my_malloc((unsigned int)v5, "Metadata buffer");
if ( (int)func9(a1, a2, v8, v7) > 0 )
{
v6 = ifiction_get_IFID(v8, a3, a4);
free(v8);
}
}
return v6;
}

original_code:
static int32 get_story_file_IFID(void *b, int32 e, char *output, int32 output_extent)
{
 int32 j;
 char *md;
 j=get_story_file_metadata_extent(b,e);
 if (j<=0) return 0;
 md=(char *)my_malloc(j, "Metadata buffer");
 j=get_story_file_metadata(b,e,md,j);
 if (j<=0) return 0;
 j=ifiction_get_IFID(md,output,output_extent);
 free(md);
 return j;
}

canonical_original_code:
static int get_story_file_IFID(void *b, int e, char *output, int output_extent)
{
 int j;
 char *md;
 j=get_story_file_metadata_extent(b,e);
 if (j<=0) return 0;
 md=(char *)my_malloc(j, "Metadata buffer");
 j=get_story_file_metadata(b,e,md,j);
 if (j<=0) return 0;
 j=ifiction_get_IFID(md,output,output_extent);
 free(md);
 return j;
}

variable_types:
b
void *

variable_types: e int

variable_types:
md
char *


decompiled_code:
size_t __fastcall get_story_file_extension(__int64 a1, int a2, void *a3, int a4)
{
__int64 v7; // rax
int v8; // ecx
char v9; // dl
__int64 v10; // rbx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v7 = 1LL;
do
{
v8 = v7;
v9 = *(&src + v7++);
}
while ( v9 && v9 != 44 );
if ( a4 <= v8 )
return 4294967293LL;
v10 = v8;
memcpy(a3, &src, v8);
*((_BYTE *)a3 + v10) = 0;
return strlen((const char *)a3);
}

canonical_decompiled_code:
size_t __fastcall func13(__int64 a1, int a2, void *a3, int a4)
{
__int64 v7; // rax
int v8; // ecx
char v9; // dl
__int64 v10; // rbx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v7 = 1LL;
do
{
v8 = v7;
v9 = *(&src + v7++);
}
while ( v9 && v9 != 44 );
if ( a4 <= v8 )
return 4294967293LL;
v10 = v8;
memcpy(a3, &src, v8);
*((_BYTE *)a3 + v10) = 0;
return strlen((const char *)a3);
}

original_code:
static int32 get_story_file_extension(void *sf, int32 extent, char *out, int32 output_extent)
{
 int i;
 if (!sf || !extent) return -1;
 for(i=0;".blorb,.blb,.zblorb,.zlb,.gblorb,.glb"[i] && ".blorb,.blb,.zblorb,.zlb,.gblorb,.glb"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".blorb,.blb,.zblorb,.zlb,.gblorb,.glb",i);
 out[i]=0;
 return strlen(out);
}

canonical_original_code:
static int get_story_file_extension(void *sf, int extent, char *out, int output_extent)
{
 int i;
 if (!sf || !extent) return -1;
 for(i=0;".blorb,.blb,.zblorb,.zlb,.gblorb,.glb"[i] && ".blorb,.blb,.zblorb,.zlb,.gblorb,.glb"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".blorb,.blb,.zblorb,.zlb,.gblorb,.glb",i);
 out[i]=0;
 return strlen(out);
}

variable_types:
sf
void *

variable_types:
out
char *


decompiled_code:
__int64 __fastcall read_hugo_addx(unsigned __int16 *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 *a1)
{
return *a1;
}

original_code:
static int32 read_hugo_addx(unsigned char *from)
{
 return ((unsigned int) from[0])| ((unsigned int)from[1] << 8);
}

canonical_original_code:
static int read_hugo_addx(unsigned char *from)
{
 return ((unsigned int) from[0])| ((unsigned int)from[1] << 8);
}

variable_types:
from
unsigned char *


original_code:
int32 hugo_treaty(int32 selector,
                   void *story_file, int32 extent,
                   void *output, int32 output_extent)
{
 int32 ll, csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((signed) strlen("http://www.generalcoffee.com")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.generalcoffee.com");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"hugo",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".hex")+1) < 512 ? (strlen(".hex")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".hex", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
  case 0x309:
                return 0;
  case 0x106:
  case 0x107:
  case 0x30A:
                return 0;
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}

canonical_original_code:
int hugo_treaty(int selector, void *story_file, int extent, void *output, int output_extent)
{
 int ll; int csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((int) strlen("http://www.generalcoffee.com")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.generalcoffee.com");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"hugo",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".hex")+1) < 512 ? (strlen(".hex")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".hex", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
  case 0x309:
                return 0;
  case 0x106:
  case 0x107:
  case 0x30A:
                return 0;
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}


decompiled_code:
__int64 __fastcall claim_story_file(_BYTE *a1, int a2)
{
int v2; // er12
_BYTE *v3; // rax
unsigned __int16 *v4; // r13

if ( !a1 )
return 0xFFFFFFFFLL;
if ( a2 <= 39 )
return 0xFFFFFFFFLL;
v2 = *a1 < 0x22u ? 4 : 16;
v3 = a1 + 3;
do
{
if ( (unsigned __int8)(*v3 - 32) > 0x5Eu )
return 0xFFFFFFFFLL;
++v3;
}
while ( v3 != a1 + 11 );
v4 = (unsigned __int16 *)(a1 + 11);
while ( (int)(v2 * read_hugo_addx(v4)) <= a2 )
{
if ( ++v4 == (unsigned __int16 *)(a1 + 25) )
return 1LL;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(_BYTE *a1, int a2)
{
int v2; // er12
_BYTE *v3; // rax
unsigned __int16 *v4; // r13

if ( !a1 )
return 0xFFFFFFFFLL;
if ( a2 <= 39 )
return 0xFFFFFFFFLL;
v2 = *a1 < 0x22u ? 4 : 16;
v3 = a1 + 3;
do
{
if ( (unsigned __int8)(*v3 - 32) > 0x5Eu )
return 0xFFFFFFFFLL;
++v3;
}
while ( v3 != a1 + 11 );
v4 = (unsigned __int16 *)(a1 + 11);
while ( (int)(v2 * func0(v4)) <= a2 )
{
if ( ++v4 == (unsigned __int16 *)(a1 + 25) )
return 1LL;
}
return 0xFFFFFFFFLL;
}

original_code:
static int32 claim_story_file(void *story_file, int32 extent)
{
 unsigned char *sf=(unsigned char *)story_file;
 int32 i;
 int32 scale;
 if (!story_file || extent < 0x28) return -1;
 if (sf[0]<34) scale=4;
 else scale=16;
 for(i=3;i<0x0B;i++) if (sf[i]<0x20 || sf[i]>0x7e) return -1;
 for(i=0x0b;i<0x18;i+=2)
 if (read_hugo_addx(sf+i) * scale > extent) return -1;
 return 1;
}

canonical_original_code:
static int claim_story_file(void *story_file, int extent)
{
 unsigned char *sf = (unsigned char *)story_file;
 int i;
 int scale;
 if (!story_file || extent < 0x28) return -1;
 if (sf[0]<34) scale=4;
 else scale=16;
 for(i=3;i<0x0B;i++) if (sf[i]<0x20 || sf[i]>0x7e) return -1;
 for(i=0x0b;i<0x18;i+=2)
 if (read_hugo_addx(sf+i) * scale > extent) return -1;
 return 1;
}

variable_types:
sf
unsigned char *

variable_types: scale int


decompiled_code:
size_t __fastcall get_story_file_extension(__int64 a1, int a2, void *a3, int a4)
{
__int64 v6; // rax
int v7; // ecx
char v8; // dl
__int64 v9; // rbx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v6 = 1LL;
do
{
v7 = v6;
v8 = byte_3DC[v6++];
}
while ( v8 && v8 != 44 );
if ( a4 <= v7 )
return 4294967293LL;
v9 = v7;
memcpy(a3, byte_3DC, v7);
*((_BYTE *)a3 + v9) = 0;
return strlen((const char *)a3);
}

canonical_decompiled_code:
size_t __fastcall func2(__int64 a1, int a2, void *a3, int a4)
{
__int64 v6; // rax
int v7; // ecx
char v8; // dl
__int64 v9; // rbx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v6 = 1LL;
do
{
v7 = v6;
v8 = byte_3DC[v6++];
}
while ( v8 && v8 != 44 );
if ( a4 <= v7 )
return 4294967293LL;
v9 = v7;
memcpy(a3, byte_3DC, v7);
*((_BYTE *)a3 + v9) = 0;
return strlen((const char *)a3);
}

original_code:
static int32 get_story_file_extension(void *sf, int32 extent, char *out, int32 output_extent)
{
 int i;
 if (!sf || !extent) return -1;
 for(i=0;".hex"[i] && ".hex"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".hex",i);
 out[i]=0;
 return strlen(out);
}

canonical_original_code:
static int get_story_file_extension(void *sf, int extent, char *out, int output_extent)
{
 int i;
 if (!sf || !extent) return -1;
 for(i=0;".hex"[i] && ".hex"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".hex",i);
 out[i]=0;
 return strlen(out);
}


decompiled_code:
__int64 __fastcall get_story_file_IFID(char *a1, int a2, void *a3, int a4)
{
__int64 v5; // rbx
const unsigned __int16 *v6; // rsi
char *v7; // rax
int v8; // er12
__int64 v9; // rax
int v10; // edx
__int64 v11; // rbx
char s[39]; // [rsp+10h] [rbp-68h] BYREF
__int64 v15; // [rsp+37h] [rbp-41h] BYREF
char v16; // [rsp+3Fh] [rbp-39h] BYREF

if ( a2 <= 10 )
return 0xFFFFFFFFLL;
v5 = 0LL;
while ( memcmp(&a1[v5], "UUID://", 7uLL) )
{
if ( ++v5 == a2 )
goto LABEL_5;
}
if ( a2 > (int)v5 )
{
v8 = v5 + 7;
if ( a2 > (int)v5 + 7 )
{
v9 = v8;
while ( a1[v9] != 47 )
{
if ( a2 <= (int)++v9 )
goto LABEL_5;
}
if ( a2 > (int)v9 )
{
v10 = v9 - v8;
if ( (int)v9 - v8 > a4 )
return 4294967293LL;
v11 = v10;
memcpy(a3, &a1[v8], v10);
*((_BYTE *)a3 + v11) = 0;
return 1LL;
}
}
}
LABEL_5:
v15 = *(_QWORD *)(a1 + 3);
v16 = 0;
v6 = *_ctype_b_loc();
v7 = (char *)&v15;
do
{
if ( (v6[*v7] & 8) == 0 )
*v7 = 45;
++v7;
}
while ( &v16 != v7 );
sprintf(s, "HUGO-%d-%02X-%02X-%s", (unsigned int)*a1, (unsigned int)a1[1], (unsigned int)a1[2], (const char *)&v15);
if ( a4 <= (int)strlen(s) )
return 4294967293LL;
strcpy((char *)a3, s);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *a1, int a2, void *a3, int a4)
{
__int64 v5; // rbx
const unsigned __int16 *v6; // rsi
char *v7; // rax
int v8; // er12
__int64 v9; // rax
int v10; // edx
__int64 v11; // rbx
char s[39]; // [rsp+10h] [rbp-68h] BYREF
__int64 v15; // [rsp+37h] [rbp-41h] BYREF
char v16; // [rsp+3Fh] [rbp-39h] BYREF

if ( a2 <= 10 )
return 0xFFFFFFFFLL;
v5 = 0LL;
while ( memcmp(&a1[v5], "UUID://", 7uLL) )
{
if ( ++v5 == a2 )
goto LABEL_5;
}
if ( a2 > (int)v5 )
{
v8 = v5 + 7;
if ( a2 > (int)v5 + 7 )
{
v9 = v8;
while ( a1[v9] != 47 )
{
if ( a2 <= (int)++v9 )
goto LABEL_5;
}
if ( a2 > (int)v9 )
{
v10 = v9 - v8;
if ( (int)v9 - v8 > a4 )
return 4294967293LL;
v11 = v10;
memcpy(a3, &a1[v8], v10);
*((_BYTE *)a3 + v11) = 0;
return 1LL;
}
}
}
LABEL_5:
v15 = *(_QWORD *)(a1 + 3);
v16 = 0;
v6 = *_ctype_b_loc();
v7 = (char *)&v15;
do
{
if ( (v6[*v7] & 8) == 0 )
*v7 = 45;
++v7;
}
while ( &v16 != v7 );
sprintf(s, "HUGO-%d-%02X-%02X-%s", (unsigned int)*a1, (unsigned int)a1[1], (unsigned int)a1[2], (const char *)&v15);
if ( a4 <= (int)strlen(s) )
return 4294967293LL;
strcpy((char *)a3, s);
return 1LL;
}

original_code:
static int32 get_story_file_IFID(void *s_file, int32 extent, char *output, int32 output_extent)
{
 int32 i,j;
 char ser[9];
 char buffer[32];
 char *story_file = (char *) s_file;
 if (extent<0x0B) return -1;
 for(i=0;i<extent;i++) if (memcmp((char *)story_file+i,"UUID://",7)==0) break;
 if (i<extent)
  {
   for(j=i+7;j<extent && ((char *)story_file)[j]!='/';j++);
   if (j<extent)
   {
    i+=7;
    do { if (output_extent < (j-i)) return -3; } while (0);
    memcpy(output,(char *)story_file+i,j-i);
    output[j-i]=0;
    return 1;
   }
  }
 memcpy(ser, (char *) story_file+0x03, 8);
 ser[8]=0;
 for(j=0;j<8;j++)
  if (!((*__ctype_b_loc ())[(int) ((ser[j]))] & (unsigned short int) _ISalnum)) ser[j]='-';
 sprintf(buffer,"HUGO-%d-%02X-%02X-%s",story_file[0],story_file[1], story_file[2],ser);
 do { if (output_extent < ((signed) strlen(buffer)+1)) return -3; } while (0);
 strcpy((char *)output,buffer);
 return 1;
}

canonical_original_code:
static int get_story_file_IFID(void *s_file, int extent, char *output, int output_extent)
{
 int i; int j;
 char ser[9];
 char buffer[32];
 char *story_file = (char *) s_file;
 if (extent<0x0B) return -1;
 for(i=0;i<extent;i++) if (memcmp((char *)story_file+i,"UUID://",7)==0) break;
 if (i<extent)
  {
   for(j=i+7;j<extent && ((char *)story_file)[j]!='/';j++);
   if (j<extent)
   {
    i+=7;
    do { if (output_extent < (j-i)) return -3; } while (0);
    memcpy(output,(char *)story_file+i,j-i);
    output[j-i]=0;
    return 1;
   }
  }
 memcpy(ser, (char *) story_file+0x03, 8);
 ser[8]=0;
 for(j=0;j<8;j++)
  if (!((*__ctype_b_loc ())[(int) ((ser[j]))] & (unsigned short) _ISalnum)) ser[j]='-';
 sprintf(buffer,"HUGO-%d-%02X-%02X-%s",story_file[0],story_file[1], story_file[2],ser);
 do { if (output_extent < ((int) strlen(buffer)+1)) return -3; } while (0);
 strcpy((char *)output,buffer);
 return 1;
}

variable_types:
ser
char [9]

variable_types:
buffer
char [32]


decompiled_code:
void __fastcall advsys_treaty(int a1, __int64 a2, unsigned int a3, char *a4, int a5)
{
if ( ((a1 & 0x100) == 0 || (int)claim_story_file(a2, a3) >= 0) && ((a1 & 0x200) == 0 || a5 && a4) )
{
if ( a1 == 515 )
{
if ( a5 > 4 )
strncpy(a4, byte_2A6, a5);
}
else if ( a1 > 515 )
{
if ( a1 > 778 )
{
if ( a1 == 779 )
get_story_file_extension(a2, a3, a4, a5);
}
else if ( a1 <= 776 && a1 == 776 )
{
get_story_file_IFID(a2, a3, a4, a5);
}
}
else if ( a1 == 513 )
{
if ( a5 > 55 )
strcpy(a4, "http://www.ifarchive.org/if-archive/programming/advsys/");
}
else if ( a1 > 513 && a5 > 511 )
{
strncpy(a4, "advsys", a5 - 1);
}
}
}

original_code:
int32 advsys_treaty(int32 selector,
                   void *story_file, int32 extent,
                   void *output, int32 output_extent)
{
 int32 ll, csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((signed) strlen("http://www.ifarchive.org/if-archive/programming/advsys/")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.ifarchive.org/if-archive/programming/advsys/");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"advsys",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".dat")+1) < 512 ? (strlen(".dat")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".dat", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
  case 0x309:
                return 0;
  case 0x106:
  case 0x107:
  case 0x30A:
                return 0;
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}

canonical_original_code:
int advsys_treaty(int selector, void *story_file, int extent, void *output, int output_extent)
{
 int ll; int csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((int) strlen("http://www.ifarchive.org/if-archive/programming/advsys/")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.ifarchive.org/if-archive/programming/advsys/");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"advsys",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".dat")+1) < 512 ? (strlen(".dat")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".dat", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
  case 0x309:
                return 0;
  case 0x106:
  case 0x107:
  case 0x30A:
                return 0;
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}

variable_types: selector int

variable_types:
output
void *

variable_types: ll int

variable_types: csf int


decompiled_code:
__int64 __fastcall claim_story_file(__int64 a1, int a2)
{
__int64 result; // rax
__int64 i; // rdx
char v4[6]; // [rsp+1h] [rbp-7h] BYREF

result = 0xFFFFFFFFLL;
if ( a2 > 7 )
{
for ( i = 0LL; i != 6; ++i )
v4[i] = ~(*(_BYTE *)(a1 + i + 2) + 30);
result = strcmp(v4, "ADVSYS") == 0 ? 1 : -1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
__int64 result; // rax
__int64 i; // rdx
char v4[6]; // [rsp+1h] [rbp-7h] BYREF

result = 0xFFFFFFFFLL;
if ( a2 > 7 )
{
for ( i = 0LL; i != 6; ++i )
v4[i] = ~(*(_BYTE *)(a1 + i + 2) + 30);
result = strcmp(v4, "ADVSYS") == 0 ? 1 : -1;
}
return result;
}

original_code:
static int32 claim_story_file(void *story_file, int32 extent)
{
 char buf[7];
 int i;
 if (extent >=8)
 {
   for(i=0;i<6;i++)
    buf[i]=~(((char *)story_file)[i+2]+30);
   buf[6]=0;
   if (strcmp(buf,"ADVSYS")==0) return 1;
 }
 return -1;
}

canonical_original_code:
static int claim_story_file(void *story_file, int extent)
{
 char buf[7];
 int i;
 if (extent >=8)
 {
   for(i=0;i<6;i++)
    buf[i]=~(((char *)story_file)[i+2]+30);
   buf[6]=0;
   if (strcmp(buf,"ADVSYS")==0) return 1;
 }
 return -1;
}

variable_types: extent int

variable_types:
buf
char [7]

variable_types: i int


decompiled_code:
size_t __fastcall get_story_file_extension(__int64 a1, int a2, void *a3, int a4)
{
__int64 v6; // rax
int v7; // ecx
char v8; // dl
__int64 v9; // rbx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v6 = 1LL;
do
{
v7 = v6;
v8 = byte_2A6[v6++];
}
while ( v8 && v8 != 44 );
if ( a4 <= v7 )
return 4294967293LL;
v9 = v7;
memcpy(a3, byte_2A6, v7);
*((_BYTE *)a3 + v9) = 0;
return strlen((const char *)a3);
}

canonical_decompiled_code:
size_t __fastcall func2(__int64 a1, int a2, void *a3, int a4)
{
__int64 v6; // rax
int v7; // ecx
char v8; // dl
__int64 v9; // rbx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v6 = 1LL;
do
{
v7 = v6;
v8 = byte_2A6[v6++];
}
while ( v8 && v8 != 44 );
if ( a4 <= v7 )
return 4294967293LL;
v9 = v7;
memcpy(a3, byte_2A6, v7);
*((_BYTE *)a3 + v9) = 0;
return strlen((const char *)a3);
}

original_code:
static int32 get_story_file_extension(void *sf, int32 extent, char *out, int32 output_extent)
{
 int i;
 if (!sf || !extent) return -1;
 for(i=0;".dat"[i] && ".dat"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".dat",i);
 out[i]=0;
 return strlen(out);
}

canonical_original_code:
static int get_story_file_extension(void *sf, int extent, char *out, int output_extent)
{
 int i;
 if (!sf || !extent) return -1;
 for(i=0;".dat"[i] && ".dat"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".dat",i);
 out[i]=0;
 return strlen(out);
}


decompiled_code:
__int64 __fastcall read_zint(unsigned __int8 *a1)
{
return a1[1] | (*a1 << 8);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1)
{
return a1[1] | (*a1 << 8);
}

original_code:
static int32 read_zint(unsigned char *sf)
{
 return ((int32)sf[0] << 8) | ((int32) sf[1]);
}

canonical_original_code:
static int read_zint(unsigned char *sf)
{
 return ((int)sf[0] << 8) | ((int) sf[1]);
}


original_code:
int32 zcode_treaty(int32 selector,
                   void *story_file, int32 extent,
                   void *output, int32 output_extent)
{
 int32 ll, csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((signed) strlen("http://www.inform-fiction.org")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.inform-fiction.org");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"zcode",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".z3,.z4,.z5,.z6,.z7,.z8")+1) < 512 ? (strlen(".z3,.z4,.z5,.z6,.z7,.z8")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".z3,.z4,.z5,.z6,.z7,.z8", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
  case 0x309:
                return 0;
  case 0x106:
  case 0x107:
  case 0x30A:
                return 0;
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}

canonical_original_code:
int zcode_treaty(int selector, void *story_file, int extent, void *output, int output_extent)
{
 int ll; int csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((int) strlen("http://www.inform-fiction.org")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.inform-fiction.org");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"zcode",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".z3,.z4,.z5,.z6,.z7,.z8")+1) < 512 ? (strlen(".z3,.z4,.z5,.z6,.z7,.z8")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".z3,.z4,.z5,.z6,.z7,.z8", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
  case 0x309:
                return 0;
  case 0x106:
  case 0x107:
  case 0x30A:
                return 0;
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}


decompiled_code:
__int64 __fastcall claim_story_file(_BYTE *a1, int a2)
{
__int64 result; // rax
unsigned __int8 *v3; // r12
int v4; // eax

result = 0xFFFFFFFFLL;
if ( a2 > 59 )
{
result = 0xFFFFFFFFLL;
if ( (unsigned __int8)(*a1 - 1) <= 7u )
{
v3 = a1 + 4;
while ( 1 )
{
v4 = read_zint(v3);
if ( a2 < v4 || v4 <= 63 )
break;
v3 += 2;
if ( v3 == a1 + 16 )
return 1LL;
}
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_BYTE *a1, int a2)
{
__int64 result; // rax
unsigned __int8 *v3; // r12
int v4; // eax

result = 0xFFFFFFFFLL;
if ( a2 > 59 )
{
result = 0xFFFFFFFFLL;
if ( (unsigned __int8)(*a1 - 1) <= 7u )
{
v3 = a1 + 4;
while ( 1 )
{
v4 = func0(v3);
if ( a2 < v4 || v4 <= 63 )
break;
v3 += 2;
if ( v3 == a1 + 16 )
return 1LL;
}
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
static int32 claim_story_file(void *story_file, int32 extent)
{
 unsigned char *sf=(unsigned char *)story_file;
 int32 i,j;
 if (extent<0x3c ||
     sf[0] < 1 ||
     sf[0] > 8
    ) return -1;
 for(i=4;i<=14;i+=2)
 {
  j=read_zint(sf+i);
  if (j>extent || j < 0x40) return -1;
 }
 return 1;
}

canonical_original_code:
static int claim_story_file(void *story_file, int extent)
{
 unsigned char *sf = (unsigned char *)story_file;
 int i; int j;
 if (extent<0x3c ||
     sf[0] < 1 ||
     sf[0] > 8
    ) return -1;
 for(i=4;i<=14;i+=2)
 {
  j=read_zint(sf+i);
  if (j>extent || j < 0x40) return -1;
 }
 return 1;
}

variable_types: j int


decompiled_code:
__int64 __fastcall get_story_file_extension(char *a1, int a2, char *a3, int a4)
{
__int64 v5; // rdx
__int64 result; // rax

if ( !a2 )
return 0xFFFFFFFFLL;
v5 = (unsigned int)*a1;
if ( (int)v5 <= 9 )
{
if ( a4 <= 3 )
{
result = 4294967293LL;
}
else
{
sprintf(a3, ".z%d", v5);
result = 3LL;
}
}
else if ( a4 <= 4 )
{
result = 4294967293LL;
}
else
{
sprintf(a3, ".z%d", v5);
result = 4LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1, int a2, char *a3, int a4)
{
__int64 v5; // rdx
__int64 result; // rax

if ( !a2 )
return 0xFFFFFFFFLL;
v5 = (unsigned int)*a1;
if ( (int)v5 <= 9 )
{
if ( a4 <= 3 )
{
result = 4294967293LL;
}
else
{
sprintf(a3, ".z%d", v5);
result = 3LL;
}
}
else if ( a4 <= 4 )
{
result = 4294967293LL;
}
else
{
sprintf(a3, ".z%d", v5);
result = 4LL;
}
return result;
}

original_code:
static int32 get_story_file_extension(void *sf, int32 extent, char *out, int32 output_extent)
{
 int v;
 if (!extent) return -1;
 v= ((char *) sf)[0];
 if (v>9) do { if (output_extent < (5)) return -3; } while (0);
 else do { if (output_extent < (4)) return -3; } while (0);
 sprintf(out,".z%d",v);
 return 3+(v>9);
}

canonical_original_code:
static int get_story_file_extension(void *sf, int extent, char *out, int output_extent)
{
 int v;
 if (!extent) return -1;
 v= ((char *) sf)[0];
 if (v>9) do { if (output_extent < (5)) return -3; } while (0);
 else do { if (output_extent < (4)) return -3; } while (0);
 sprintf(out,".z%d",v);
 return 3+(v>9);
}

variable_types: v int


decompiled_code:
__int64 __fastcall get_story_file_IFID(__int64 a1, int a2, char *a3, int a4)
{
int v5; // edx
__int64 v6; // rbx
unsigned int v7; // ebx
const unsigned __int16 *v8; // r8
char *v9; // rax
int v10; // er13
__int64 v11; // rax
int v12; // edx
__int64 v13; // rbx
__int64 result; // rax
char s[41]; // [rsp+10h] [rbp-68h] BYREF
int v17; // [rsp+39h] [rbp-3Fh] BYREF
__int16 v18; // [rsp+3Dh] [rbp-3Bh]
char v19; // [rsp+3Fh] [rbp-39h] BYREF

if ( a2 <= 28 )
return 0xFFFFFFFFLL;
v5 = *(_DWORD *)(a1 + 18);
v17 = v5;
v18 = *(_WORD *)(a1 + 22);
v19 = 0;
if ( (unsigned __int8)(v5 - 56) <= 1u || (_BYTE)v5 == 48 && (unsigned __int8)(BYTE1(v17) - 48) <= 5u )
goto LABEL_7;
v6 = 0LL;
while ( memcmp((const void *)(a1 + v6), "UUID://", 7uLL) )
{
if ( ++v6 == a2 )
goto LABEL_7;
}
if ( a2 <= (int)v6 )
goto LABEL_7;
v10 = v6 + 7;
if ( a2 <= (int)v6 + 7 )
goto LABEL_7;
v11 = v10;
while ( *(_BYTE *)(a1 + v11) != 47 )
{
if ( a2 <= (int)++v11 )
goto LABEL_7;
}
if ( a2 <= (int)v11 )
{
LABEL_7:
v7 = *(unsigned __int8 *)(a1 + 3) | (*(unsigned __int8 *)(a1 + 2) << 8);
v8 = *_ctype_b_loc();
v9 = (char *)&v17;
do
{
if ( (v8[*v9] & 8) == 0 )
*v9 = 45;
++v9;
}
while ( v9 != &v19 );
if ( !strcmp((const char *)&v17, "000000") || (v8[(char)v17] & 0x800) == 0 || (_BYTE)v17 == 56 )
sprintf(s, "ZCODE-%d-%s", v7, (const char *)&v17);
else
sprintf(
s,
"ZCODE-%d-%s-%04X",
v7,
(const char *)&v17,
(*(unsigned __int8 *)(a1 + 28) << 8) | (unsigned int)*(unsigned __int8 *)(a1 + 29));
if ( a4 <= (int)strlen(s) )
{
result = 4294967293LL;
}
else
{
strcpy(a3, s);
result = 1LL;
}
}
else
{
v12 = v11 - v10;
if ( (int)v11 - v10 > a4 )
{
result = 4294967293LL;
}
else
{
v13 = v12;
memcpy(a3, (const void *)(a1 + v10), v12);
a3[v13] = 0;
result = 1LL;
}
}
return result;
}

original_code:
static int32 get_story_file_IFID(void *story_file, int32 extent, char *output, int32 output_extent)
{
 int32 i,j;
 char ser[7];
 char buffer[32];
 if (extent<0x1D) return -1;
 memcpy(ser, (char *) story_file+0x12, 6);
 ser[6]=0;
 if (!(ser[0]=='8' || ser[0]=='9' ||
     (ser[0]=='0' && ser[1]>='0' && ser[1]<='5')))
 {
  for(i=0;i<extent;i++) if (memcmp((char *)story_file+i,"UUID://",7)==0) break;
  if (i<extent)
  {
   for(j=i+7;j<extent && ((char *)story_file)[j]!='/';j++);
   if (j<extent)
   {
    i+=7;
    do { if (output_extent < (j-i)) return -3; } while (0);
    memcpy(output,(char *)story_file+i,j-i);
    output[j-i]=0;
    return 1;
   }
  }
 }
 i=((unsigned char *)story_file)[2] << 8 |((unsigned char *)story_file)[3];
 for(j=0;j<6;j++)
  if (!((*__ctype_b_loc ())[(int) ((ser[j]))] & (unsigned short int) _ISalnum)) ser[j]='-';
 j=((unsigned char *)story_file)[0x1C] << 8 |((unsigned char *)story_file)[0x1D];
 if (strcmp(ser,"000000") && ((*__ctype_b_loc ())[(int) ((ser[0]))] & (unsigned short int) _ISdigit) && ser[0]!='8')
  sprintf(buffer,"ZCODE-%d-%s-%04X",i,ser,j);
 else
  sprintf(buffer,"ZCODE-%d-%s",i,ser);
 do { if (output_extent < ((signed) strlen(buffer)+1)) return -3; } while (0);
 strcpy((char *)output,buffer);
 return 1;
}

canonical_original_code:
static int get_story_file_IFID(void *story_file, int extent, char *output, int output_extent)
{
 int i; int j;
 char ser[7];
 char buffer[32];
 if (extent<0x1D) return -1;
 memcpy(ser, (char *) story_file+0x12, 6);
 ser[6]=0;
 if (!(ser[0]=='8' || ser[0]=='9' ||
     (ser[0]=='0' && ser[1]>='0' && ser[1]<='5')))
 {
  for(i=0;i<extent;i++) if (memcmp((char *)story_file+i,"UUID://",7)==0) break;
  if (i<extent)
  {
   for(j=i+7;j<extent && ((char *)story_file)[j]!='/';j++);
   if (j<extent)
   {
    i+=7;
    do { if (output_extent < (j-i)) return -3; } while (0);
    memcpy(output,(char *)story_file+i,j-i);
    output[j-i]=0;
    return 1;
   }
  }
 }
 i=((unsigned char *)story_file)[2] << 8 |((unsigned char *)story_file)[3];
 for(j=0;j<6;j++)
  if (!((*__ctype_b_loc ())[(int) ((ser[j]))] & (unsigned short) _ISalnum)) ser[j]='-';
 j=((unsigned char *)story_file)[0x1C] << 8 |((unsigned char *)story_file)[0x1D];
 if (strcmp(ser,"000000") && ((*__ctype_b_loc ())[(int) ((ser[0]))] & (unsigned short) _ISdigit) && ser[0]!='8')
  sprintf(buffer,"ZCODE-%d-%s-%04X",i,ser,j);
 else
  sprintf(buffer,"ZCODE-%d-%s",i,ser);
 do { if (output_extent < ((int) strlen(buffer)+1)) return -3; } while (0);
 strcpy((char *)output,buffer);
 return 1;
}

variable_types:
output
char *

variable_types:
ser
char [7]


decompiled_code:
void __fastcall tads3_treaty(int a1, __int64 a2, unsigned int a3, char *a4, int a5)
{
if ( ((a1 & 0x100) == 0 || (int)claim_story_file(a2, a3) >= 0) && ((a1 & 0x200) == 0 || a5 && a4) )
{
if ( a1 == 514 )
{
if ( a5 > 511 )
strncpy(a4, "tads3", a5 - 1);
}
else if ( a1 <= 514 )
{
if ( a1 == 262 )
{
get_story_file_cover_extent(a2, a3);
}
else if ( a1 <= 262 )
{
if ( a1 != 260 && a1 == 261 )
get_story_file_metadata_extent(a2, a3);
}
else if ( a1 == 263 )
{
get_story_file_cover_format(a2, a3);
}
else if ( a1 == 513 && a5 > 19 )
{
strcpy(a4, "http://www.tads.org");
}
}
else if ( a1 == 777 )
{
get_story_file_metadata(a2, a3, a4, (unsigned int)a5);
}
else if ( a1 <= 777 )
{
if ( a1 == 515 )
{
if ( a5 > 3 )
strncpy(a4, byte_2F0, a5);
}
else if ( a1 == 776 )
{
get_story_file_IFID(a2, a3, a4, (unsigned int)a5);
}
}
else if ( a1 == 778 )
{
get_story_file_cover(a2, a3, a4, (unsigned int)a5);
}
else if ( a1 == 779 )
{
get_story_file_extension(a2, a3, a4, a5);
}
}
}

original_code:
int32 tads3_treaty(int32 selector,
                   void *story_file, int32 extent,
                   void *output, int32 output_extent)
{
 int32 ll, csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((signed) strlen("http://www.tads.org")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.tads.org");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"tads3",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".t3")+1) < 512 ? (strlen(".t3")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".t3", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
                return get_story_file_metadata_extent(story_file, extent);
  case 0x309:
                return get_story_file_metadata(story_file, extent, (char *)output, output_extent);
  case 0x106:
                return get_story_file_cover_extent(story_file, extent);
  case 0x107:
                return get_story_file_cover_format(story_file, extent);
  case 0x30A:
                return get_story_file_cover(story_file, extent, output, output_extent);
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}

canonical_original_code:
int tads3_treaty(int selector, void *story_file, int extent, void *output, int output_extent)
{
 int ll; int csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((int) strlen("http://www.tads.org")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.tads.org");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"tads3",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".t3")+1) < 512 ? (strlen(".t3")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".t3", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
                return get_story_file_metadata_extent(story_file, extent);
  case 0x309:
                return get_story_file_metadata(story_file, extent, (char *)output, output_extent);
  case 0x106:
                return get_story_file_cover_extent(story_file, extent);
  case 0x107:
                return get_story_file_cover_format(story_file, extent);
  case 0x30A:
                return get_story_file_cover(story_file, extent, output, output_extent);
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}


decompiled_code:
__int64 __fastcall claim_story_file(__int64 a1, __int64 a2)
{
return (unsigned int)tads_match_sig(a1, a2, &unk_2E4) == 0 ? -1 : 1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
return (unsigned int)tads_match_sig(a1, a2, &unk_2E4) == 0 ? -1 : 1;
}

original_code:
static int32 claim_story_file(void *story_file, int32 extent)
{
    if (tads_match_sig(story_file, extent, "T3-image\015\012\032"))
        return 1;
    return -1;
}

canonical_original_code:
static int claim_story_file(void *story_file, int extent)
{
    if (tads_match_sig(story_file, extent, "T3-image\015\012\032"))
        return 1;
    return -1;
}


decompiled_code:
__int64 get_story_file_metadata_extent()
{
return tads_get_story_file_metadata_extent();
}

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

original_code:
static int32 get_story_file_metadata_extent(void *story_file, int32 extent)
{
    return tads_get_story_file_metadata_extent(story_file, extent);
}

canonical_original_code:
static int get_story_file_metadata_extent(void *story_file, int extent)
{
    return tads_get_story_file_metadata_extent(story_file, extent);
}


decompiled_code:
__int64 get_story_file_metadata()
{
return tads_get_story_file_metadata();
}

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

original_code:
static int32 get_story_file_metadata(void *story_file, int32 extent,
                                     char *buf, int32 bufsize)
{
    return tads_get_story_file_metadata(story_file, extent, buf, bufsize);
}

canonical_original_code:
static int get_story_file_metadata(void *story_file, int extent, char *buf, int bufsize)
{
    return tads_get_story_file_metadata(story_file, extent, buf, bufsize);
}

variable_types:
buf
char *

variable_types: bufsize int


decompiled_code:
__int64 get_story_file_cover_extent()
{
return tads_get_story_file_cover_extent();
}

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

original_code:
static int32 get_story_file_cover_extent(void *story_file, int32 story_len)
{
    return tads_get_story_file_cover_extent(story_file, story_len);
}

canonical_original_code:
static int get_story_file_cover_extent(void *story_file, int story_len)
{
    return tads_get_story_file_cover_extent(story_file, story_len);
}


decompiled_code:
__int64 get_story_file_cover_format()
{
return tads_get_story_file_cover_format();
}

canonical_decompiled_code:
__int64 func4()
{
return tads_get_story_file_cover_format();
}

original_code:
static int32 get_story_file_cover_format(void *story_file, int32 story_len)
{
    return tads_get_story_file_cover_format(story_file, story_len);
}

canonical_original_code:
static int get_story_file_cover_format(void *story_file, int story_len)
{
    return tads_get_story_file_cover_format(story_file, story_len);
}


decompiled_code:
__int64 get_story_file_cover()
{
return tads_get_story_file_cover();
}

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

original_code:
static int32 get_story_file_cover(void *story_file, int32 story_len,
                                  void *outbuf, int32 output_extent)
{
    return tads_get_story_file_cover(story_file, story_len,
                                     outbuf, output_extent);
}

canonical_original_code:
static int get_story_file_cover(void *story_file, int story_len, void *outbuf, int output_extent)
{
    return tads_get_story_file_cover(story_file, story_len,
                                     outbuf, output_extent);
}

variable_types:
outbuf
void *


decompiled_code:
__int64 get_story_file_IFID()
{
return tads_get_story_file_IFID();
}

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

original_code:
static int32 get_story_file_IFID(void *story_file, int32 extent,
                                 char *output, int32 output_extent)
{
    return tads_get_story_file_IFID(story_file, extent,
                                    output, output_extent);
}

canonical_original_code:
static int get_story_file_IFID(void *story_file, int extent, char *output, int output_extent)
{
    return tads_get_story_file_IFID(story_file, extent,
                                    output, output_extent);
}


decompiled_code:
size_t __fastcall get_story_file_extension(__int64 a1, int a2, void *a3, int a4)
{
__int64 v6; // rax
int v7; // ecx
char v8; // dl
__int64 v9; // rbx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v6 = 1LL;
do
{
v7 = v6;
v8 = byte_2F0[v6++];
}
while ( v8 && v8 != 44 );
if ( a4 <= v7 )
return 4294967293LL;
v9 = v7;
memcpy(a3, byte_2F0, v7);
*((_BYTE *)a3 + v9) = 0;
return strlen((const char *)a3);
}

canonical_decompiled_code:
size_t __fastcall func7(__int64 a1, int a2, void *a3, int a4)
{
__int64 v6; // rax
int v7; // ecx
char v8; // dl
__int64 v9; // rbx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v6 = 1LL;
do
{
v7 = v6;
v8 = byte_2F0[v6++];
}
while ( v8 && v8 != 44 );
if ( a4 <= v7 )
return 4294967293LL;
v9 = v7;
memcpy(a3, byte_2F0, v7);
*((_BYTE *)a3 + v9) = 0;
return strlen((const char *)a3);
}

original_code:
static int32 get_story_file_extension(void *sf, int32 extent, char *out, int32 output_extent)
{
 int i;
 if (!sf || !extent) return -1;
 for(i=0;".t3"[i] && ".t3"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".t3",i);
 out[i]=0;
 return strlen(out);
}

canonical_original_code:
static int get_story_file_extension(void *sf, int extent, char *out, int output_extent)
{
 int i;
 if (!sf || !extent) return -1;
 for(i=0;".t3"[i] && ".t3"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".t3",i);
 out[i]=0;
 return strlen(out);
}

variable_types:
sf
void *

variable_types:
out
char *


decompiled_code:
__int64 __fastcall read_agt_short(unsigned __int16 *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 *a1)
{
return *a1;
}

original_code:
static int32 read_agt_short(unsigned char *sf)
{
 return sf[0] | (int32) sf[1]<<8;
}

canonical_original_code:
static int read_agt_short(unsigned char *sf)
{
 return sf[0] | (int) sf[1]<<8;
}

variable_types:
sf
unsigned char *


decompiled_code:
__int64 __fastcall read_agt_int(unsigned __int16 *a1)
{
int v1; // ebx

v1 = read_agt_short(a1 + 1);
return (v1 << 16) | (unsigned int)read_agt_short(a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int16 *a1)
{
int v1; // ebx

v1 = func0(a1 + 1);
return (v1 << 16) | (unsigned int)func0(a1);
}

original_code:
static int32 read_agt_int(unsigned char *sf)
{
 return (read_agt_short(sf+2) << 16) | read_agt_short(sf);
}

canonical_original_code:
static int read_agt_int(unsigned char *sf)
{
 return (read_agt_short(sf+2) << 16) | read_agt_short(sf);
}


decompiled_code:
void __fastcall agt_treaty(int a1, const void *a2, int a3, char *a4, int a5)
{
if ( ((a1 & 0x100) == 0 || (int)claim_story_file(a2, a3) >= 0) && ((a1 & 0x200) == 0 || a5 && a4) )
{
if ( a1 == 515 )
{
if ( a5 > 4 )
strncpy(a4, byte_31A, a5);
}
else if ( a1 > 515 )
{
if ( a1 > 778 )
{
if ( a1 == 779 )
get_story_file_extension((__int64)a2, a3, a4, a5);
}
else if ( a1 <= 776 && a1 == 776 )
{
get_story_file_IFID((__int64)a2, a3, a4, a5);
}
}
else if ( a1 == 513 )
{
if ( a5 > 59 )
strcpy(a4, "http://www.ifarchive.org/indexes/if-archiveXprogrammingXagt");
}
else if ( a1 > 513 && a5 > 511 )
{
strncpy(a4, "agt", a5 - 1);
}
}
}

original_code:
int32 agt_treaty(int32 selector,
                   void *story_file, int32 extent,
                   void *output, int32 output_extent)
{
 int32 ll, csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((signed) strlen("http://www.ifarchive.org/indexes/if-archiveXprogrammingXagt")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.ifarchive.org/indexes/if-archiveXprogrammingXagt");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"agt",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".agx")+1) < 512 ? (strlen(".agx")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".agx", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
  case 0x309:
                return 0;
  case 0x106:
  case 0x107:
  case 0x30A:
                return 0;
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}

canonical_original_code:
int agt_treaty(int selector, void *story_file, int extent, void *output, int output_extent)
{
 int ll; int csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((int) strlen("http://www.ifarchive.org/indexes/if-archiveXprogrammingXagt")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.ifarchive.org/indexes/if-archiveXprogrammingXagt");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"agt",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".agx")+1) < 512 ? (strlen(".agx")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".agx", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
  case 0x309:
                return 0;
  case 0x106:
  case 0x107:
  case 0x30A:
                return 0;
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}

variable_types: selector int


decompiled_code:
__int64 __fastcall claim_story_file(const void *a1, int a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( a2 > 35 )
result = memcmp(a1, &AGX_MAGIC, 4uLL) == 0 ? 1 : -1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(const void *a1, int a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( a2 > 35 )
result = memcmp(a1, &AGX_MAGIC, 4uLL) == 0 ? 1 : -1;
return result;
}

original_code:
static int32 claim_story_file(void *story_file, int32 extent)
{
 if (extent<36 || memcmp(story_file,AGX_MAGIC,4)) return -1;
 return 1;
}

canonical_original_code:
static int claim_story_file(void *story_file, int extent)
{
 if (extent<36 || memcmp(story_file,AGX_MAGIC,4)) return -1;
 return 1;
}


decompiled_code:
__int64 __fastcall get_story_file_IFID(__int64 a1, int a2, char *a3, int a4)
{
int v6; // eax
__int64 v7; // rbx
unsigned int v8; // er14
__int64 v9; // rcx

v6 = read_agt_int((unsigned __int16 *)(a1 + 32));
if ( v6 + 5 >= a2 )
return 0xFFFFFFFFLL;
v7 = v6;
v8 = read_agt_short((unsigned __int16 *)(a1 + v6));
v9 = (unsigned int)read_agt_int((unsigned __int16 *)(a1 + v7 + 2));
if ( a4 <= 18 )
return 4294967293LL;
sprintf(a3, "AGT-%05d-%08X", v8, v9);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, char *a3, int a4)
{
int v6; // eax
__int64 v7; // rbx
unsigned int v8; // er14
__int64 v9; // rcx

v6 = func1((unsigned __int16 *)(a1 + 32));
if ( v6 + 5 >= a2 )
return 0xFFFFFFFFLL;
v7 = v6;
v8 = func0((unsigned __int16 *)(a1 + v6));
v9 = (unsigned int)func1((unsigned __int16 *)(a1 + v7 + 2));
if ( a4 <= 18 )
return 4294967293LL;
sprintf(a3, "AGT-%05d-%08X", v8, v9);
return 1LL;
}

original_code:
static int32 get_story_file_IFID(void *story_file, int32 extent, char *output, int32 output_extent)
{
 int32 l, game_version, game_sig;
 unsigned char *sf=(unsigned char *)story_file;
 l=read_agt_int(sf+32);
 if (extent<l+6) return -1;
 game_version = read_agt_short(sf+l);
 game_sig=read_agt_int(sf+l+2);
 do { if (output_extent < (19)) return -3; } while (0);
 sprintf(output,"AGT-%05d-%08X",game_version,game_sig);
 return 1;
}

canonical_original_code:
static int get_story_file_IFID(void *story_file, int extent, char *output, int output_extent)
{
 int l; int game_version; int game_sig;
 unsigned char *sf = (unsigned char *)story_file;
 l=read_agt_int(sf+32);
 if (extent<l+6) return -1;
 game_version = read_agt_short(sf+l);
 game_sig=read_agt_int(sf+l+2);
 do { if (output_extent < (19)) return -3; } while (0);
 sprintf(output,"AGT-%05d-%08X",game_version,game_sig);
 return 1;
}

variable_types: l int


decompiled_code:
size_t __fastcall get_story_file_extension(__int64 a1, int a2, void *a3, int a4)
{
__int64 v6; // rax
int v7; // ecx
char v8; // dl
__int64 v9; // rbx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v6 = 1LL;
do
{
v7 = v6;
v8 = byte_31A[v6++];
}
while ( v8 && v8 != 44 );
if ( a4 <= v7 )
return 4294967293LL;
v9 = v7;
memcpy(a3, byte_31A, v7);
*((_BYTE *)a3 + v9) = 0;
return strlen((const char *)a3);
}

canonical_decompiled_code:
size_t __fastcall func4(__int64 a1, int a2, void *a3, int a4)
{
__int64 v6; // rax
int v7; // ecx
char v8; // dl
__int64 v9; // rbx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v6 = 1LL;
do
{
v7 = v6;
v8 = byte_31A[v6++];
}
while ( v8 && v8 != 44 );
if ( a4 <= v7 )
return 4294967293LL;
v9 = v7;
memcpy(a3, byte_31A, v7);
*((_BYTE *)a3 + v9) = 0;
return strlen((const char *)a3);
}

original_code:
static int32 get_story_file_extension(void *sf, int32 extent, char *out, int32 output_extent)
{
 int i;
 if (!sf || !extent) return -1;
 for(i=0;".agx"[i] && ".agx"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".agx",i);
 out[i]=0;
 return strlen(out);
}

canonical_original_code:
static int get_story_file_extension(void *sf, int extent, char *out, int output_extent)
{
 int i;
 if (!sf || !extent) return -1;
 for(i=0;".agx"[i] && ".agx"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".agx",i);
 out[i]=0;
 return strlen(out);
}


original_code:
int32 glulx_treaty(int32 selector,
                   void *story_file, int32 extent,
                   void *output, int32 output_extent)
{
 int32 ll, csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((signed) strlen("http://eblong.com/zarf/glulx")+1)) return -3; } while (0);
                strcpy((char *) output,"http://eblong.com/zarf/glulx");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"glulx",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".ulx")+1) < 512 ? (strlen(".ulx")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".ulx", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
  case 0x309:
                return 0;
  case 0x106:
  case 0x107:
  case 0x30A:
                return 0;
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}

canonical_original_code:
int glulx_treaty(int selector, void *story_file, int extent, void *output, int output_extent)
{
 int ll; int csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((int) strlen("http://eblong.com/zarf/glulx")+1)) return -3; } while (0);
                strcpy((char *) output,"http://eblong.com/zarf/glulx");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"glulx",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".ulx")+1) < 512 ? (strlen(".ulx")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".ulx", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
  case 0x309:
                return 0;
  case 0x106:
  case 0x107:
  case 0x30A:
                return 0;
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}

variable_types:
output
void *

variable_types: ll int


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

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

original_code:
static int32 read_int(unsigned char *mem)
{
  int32 i4 = mem[0],
                    i3 = mem[1],
                    i2 = mem[2],
                    i1 = mem[3];
  return i1 | (i2<<8) | (i3<<16) | (i4<<24);
}

canonical_original_code:
static int read_int(unsigned char *mem)
{
  int i4 = mem[0]; int i3 = mem[1]; int i2 = mem[2]; int i1 = mem[3];
  return i1 | (i2<<8) | (i3<<16) | (i4<<24);
}

variable_types:
mem
unsigned char *


decompiled_code:
__int64 __fastcall claim_story_file(const void *a1, int a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( a2 > 255 )
result = memcmp(a1, "Glul", 4uLL) == 0 ? 1 : -1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const void *a1, int a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( a2 > 255 )
result = memcmp(a1, "Glul", 4uLL) == 0 ? 1 : -1;
return result;
}

original_code:
static int32 claim_story_file(void *story_file, int32 extent)
{
 if (extent<256 ||
     memcmp(story_file,"Glul",4)
    ) return -1;
 return 1;
}

canonical_original_code:
static int claim_story_file(void *story_file, int extent)
{
 if (extent<256 ||
     memcmp(story_file,"Glul",4)
    ) return -1;
 return 1;
}

variable_types: extent int


decompiled_code:
size_t __fastcall get_story_file_extension(__int64 a1, int a2, void *a3, int a4)
{
__int64 v6; // rax
int v7; // ecx
char v8; // dl
__int64 v9; // rbx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v6 = 1LL;
do
{
v7 = v6;
v8 = byte_403[v6++];
}
while ( v8 && v8 != 44 );
if ( a4 <= v7 )
return 4294967293LL;
v9 = v7;
memcpy(a3, byte_403, v7);
*((_BYTE *)a3 + v9) = 0;
return strlen((const char *)a3);
}

canonical_decompiled_code:
size_t __fastcall func2(__int64 a1, int a2, void *a3, int a4)
{
__int64 v6; // rax
int v7; // ecx
char v8; // dl
__int64 v9; // rbx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v6 = 1LL;
do
{
v7 = v6;
v8 = byte_403[v6++];
}
while ( v8 && v8 != 44 );
if ( a4 <= v7 )
return 4294967293LL;
v9 = v7;
memcpy(a3, byte_403, v7);
*((_BYTE *)a3 + v9) = 0;
return strlen((const char *)a3);
}

original_code:
static int32 get_story_file_extension(void *sf, int32 extent, char *out, int32 output_extent)
{
 int i;
 if (!sf || !extent) return -1;
 for(i=0;".ulx"[i] && ".ulx"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".ulx",i);
 out[i]=0;
 return strlen(out);
}

canonical_original_code:
static int get_story_file_extension(void *sf, int extent, char *out, int output_extent)
{
 int i;
 if (!sf || !extent) return -1;
 for(i=0;".ulx"[i] && ".ulx"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".ulx",i);
 out[i]=0;
 return strlen(out);
}

variable_types: i int


decompiled_code:
__int64 __fastcall get_story_file_IFID(__int64 a1, int a2, void *a3, int a4)
{
__int64 v5; // rbx
unsigned int v6; // ebx
unsigned int v7; // er12
const unsigned __int16 *v8; // rsi
char *v9; // rax
int v10; // er12
__int64 v11; // rax
int v12; // edx
__int64 v13; // rbx
unsigned int v15; // eax
char s[41]; // [rsp+10h] [rbp-68h] BYREF
int v18; // [rsp+39h] [rbp-3Fh] BYREF
__int16 v19; // [rsp+3Dh] [rbp-3Bh]
char v20; // [rsp+3Fh] [rbp-39h] BYREF

if ( a2 <= 255 )
return 0xFFFFFFFFLL;
v5 = 0LL;
while ( memcmp((const void *)(a1 + v5), "UUID://", 7uLL) )
{
if ( ++v5 == a2 )
goto LABEL_5;
}
if ( a2 > (int)v5 )
{
v10 = v5 + 7;
if ( a2 > (int)v5 + 7 )
{
v11 = v10;
while ( *(_BYTE *)(a1 + v11) != 47 )
{
if ( a2 <= (int)++v11 )
goto LABEL_5;
}
if ( a2 > (int)v11 )
{
v12 = v11 - v10;
if ( (int)v11 - v10 > a4 )
return 4294967293LL;
v13 = v12;
memcpy(a3, (const void *)(a1 + v10), v12);
*((_BYTE *)a3 + v13) = 0;
return 1LL;
}
}
}
LABEL_5:
v6 = read_int((unsigned int *)(a1 + 32));
if ( !memcmp((const void *)(a1 + 36), "Info", 4uLL) )
{
v7 = *(char *)(a1 + 53) | (*(char *)(a1 + 52) << 8);
v18 = *(_DWORD *)(a1 + 54);
v19 = *(_WORD *)(a1 + 58);
v20 = 0;
v8 = *_ctype_b_loc();
v9 = (char *)&v18;
do
{
if ( (v8[*v9] & 8) == 0 )
*v9 = 45;
++v9;
}
while ( &v20 != v9 );
sprintf(s, "GLULX-%u-%s-%04X", v7, (const char *)&v18, v6);
}
else
{
v15 = read_int((unsigned int *)(a1 + 12));
sprintf(s, "GLULX-%08X-%08X", v15, v6);
}
if ( a4 <= (int)strlen(s) )
return 4294967293LL;
strcpy((char *)a3, s);
return 1LL;
}

original_code:
static int32 get_story_file_IFID(void *story_file, int32 extent, char *output, int32 output_extent)
{
 int32 i,j, k;
 char ser[7];
 char buffer[32];
 if (extent<256) return -1;
 for(i=0;i<extent;i++) if (memcmp((char *)story_file+i,"UUID://",7)==0) break;
 if (i<extent)
  {
   for(j=i+7;j<extent && ((char *)story_file)[j]!='/';j++);
   if (j<extent)
   {
    i+=7;
    do { if (output_extent < (j-i)) return -3; } while (0);
    memcpy(output,(char *)story_file+i,j-i);
    output[j-i]=0;
    return 1;
   }
  }
 j=read_int((unsigned char *)story_file+32);
 k=read_int((unsigned char *)story_file+12);
 if (memcmp((char *)story_file+36,"Info",4)==0)
 {
  char *bb=(char *)story_file+52;
  k= (int) bb[0]<<8 | (int) bb[1];
  memcpy(ser,bb+2,6);
  ser[6]=0;
  for(i=0;i<6;i++) if (!((*__ctype_b_loc ())[(int) ((ser[i]))] & (unsigned short int) _ISalnum)) ser[i]='-';
  sprintf(buffer,"GLULX-%u-%s-%04X",k,ser,j);
 }
 else
  sprintf(buffer,"GLULX-%08X-%08X",k,j);
 do { if (output_extent < ((signed) strlen(buffer)+1)) return -3; } while (0);
 strcpy((char *)output,buffer);
 return 1;
}

canonical_original_code:
static int get_story_file_IFID(void *story_file, int extent, char *output, int output_extent)
{
 int i; int j; int k;
 char ser[7];
 char buffer[32];
 if (extent<256) return -1;
 for(i=0;i<extent;i++) if (memcmp((char *)story_file+i,"UUID://",7)==0) break;
 if (i<extent)
  {
   for(j=i+7;j<extent && ((char *)story_file)[j]!='/';j++);
   if (j<extent)
   {
    i+=7;
    do { if (output_extent < (j-i)) return -3; } while (0);
    memcpy(output,(char *)story_file+i,j-i);
    output[j-i]=0;
    return 1;
   }
  }
 j=read_int((unsigned char *)story_file+32);
 k=read_int((unsigned char *)story_file+12);
 if (memcmp((char *)story_file+36,"Info",4)==0)
 {
  char *bb = (char *)story_file+52;
  k= (int) bb[0]<<8 | (int) bb[1];
  memcpy(ser,bb+2,6);
  ser[6]=0;
  for(i=0;i<6;i++) if (!((*__ctype_b_loc ())[(int) ((ser[i]))] & (unsigned short) _ISalnum)) ser[i]='-';
  sprintf(buffer,"GLULX-%u-%s-%04X",k,ser,j);
 }
 else
  sprintf(buffer,"GLULX-%08X-%08X",k,j);
 do { if (output_extent < ((int) strlen(buffer)+1)) return -3; } while (0);
 strcpy((char *)output,buffer);
 return 1;
}

variable_types: j int

variable_types: k int

variable_types:
buffer
char [32]

variable_types:
bb
char *


decompiled_code:
__int64 __fastcall str_long(_BYTE *a1, unsigned int a2)
{
__int64 result; // rax

result = a2;
*a1 = HIBYTE(a2);
a1[1] = BYTE2(a2);
a1[2] = BYTE1(a2);
a1[3] = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_BYTE *a1, unsigned int a2)
{
__int64 result; // rax

result = a2;
*a1 = HIBYTE(a2);
a1[1] = BYTE2(a2);
a1[2] = BYTE1(a2);
a1[3] = a2;
return result;
}

original_code:
void str_long(char *f, unsigned int v)
{
  unsigned char v1=v&0xFF,
    v2=(v>>8)&0xFF,
    v3=(v>>16)&0xFF,
    v4=(v>>24)&0xFF;
  f[0]=v4;
  f[1]=v3;
  f[2]=v2;
  f[3]=v1;
}

canonical_original_code:
void str_long(char *f, unsigned int v)
{
  unsigned char v1 = v&0xFF; unsigned char v2 = (v>>8)&0xFF; unsigned char v3 = (v>>16)&0xFF; unsigned char v4 = (v>>24)&0xFF;
  f[0]=v4;
  f[1]=v3;
  f[2]=v2;
  f[3]=v1;
}

variable_types:
f
char *

variable_types:
v
unsigned int

return_type: void


decompiled_code:
__int64 __fastcall str_short(_BYTE *a1, unsigned int a2)
{
__int64 result; // rax

result = a2;
*a1 = BYTE1(a2);
a1[1] = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(_BYTE *a1, unsigned int a2)
{
__int64 result; // rax

result = a2;
*a1 = BYTE1(a2);
a1[1] = a2;
return result;
}

original_code:
void str_short(char *f, unsigned int v)
{
  unsigned char v1=v&0xFF,
    v2=(v>>8)&0xFF;
  f[0]=v2;
  f[1]=v1;
}

canonical_original_code:
void str_short(char *f, unsigned int v)
{
  unsigned char v1 = v&0xFF; unsigned char v2 = (v>>8)&0xFF;
  f[0]=v2;
  f[1]=v1;
}


decompiled_code:
void *__fastcall my_realloc(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
{
fwrite("Error: Memory exceeded!\n", 1uLL, 0x18uLL, stderr);
exit(2);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1, size_t a2)
{
void *result; // rax

result = realloc(a1, a2);
if ( !result )
{
fwrite("Error: Memory exceeded!\n", 1uLL, 0x18uLL, stderr);
exit(2);
}
return result;
}

original_code:
void *my_realloc(void *buf, size_t size)
{
 buf=realloc(buf,size);
 if (!buf)
  {
        fprintf(stderr,"Error: Memory exceeded!\n");
        exit(2);
   }
   return buf;
}

canonical_original_code:
void *my_realloc(void *buf, unsigned long size)
{
 buf=realloc(buf,size);
 if (!buf)
  {
        fprintf(stderr,"Error: Memory exceeded!\n");
        exit(2);
   }
   return buf;
}

variable_types:
buf
void *

variable_types:
size
unsigned long

return_type: void *


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

result = malloc(a1);
if ( !result )
{
fwrite("Error: Memory exceeded!\n", 1uLL, 0x18uLL, stderr);
exit(2);
}
return result;
}

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

result = malloc(a1);
if ( !result )
{
fwrite("Error: Memory exceeded!\n", 1uLL, 0x18uLL, stderr);
exit(2);
}
return result;
}

original_code:
void *my_malloc(int size)
{
 void *buf=malloc(size);
 if (!buf)
  {
        fprintf(stderr,"Error: Memory exceeded!\n");
        exit(2);
   }
   return buf;
}

canonical_original_code:
void *my_malloc(int size)
{
 void *buf = malloc(size);
 if (!buf)
  {
        fprintf(stderr,"Error: Memory exceeded!\n");
        exit(2);
   }
   return buf;
}

variable_types: size int


decompiled_code:
__int64 __fastcall taf_translate(int a1)
{
vbr_state = (1140671485 * vbr_state + 12820163) & 0xFFFFFF;
return a1 ^ ((unsigned int)(255 * vbr_state) >> 24);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
vbr_state = (1140671485 * vbr_state + 12820163) & 0xFFFFFF;
return a1 ^ ((unsigned int)(255 * vbr_state) >> 24);
}

original_code:
static unsigned char taf_translate (unsigned char c)
{
 int32 r;
 vbr_state = (vbr_state*0x43FD43FD +0x00C39EC3) & 0x00FFFFFF;
 r=(0x7f * 2 + 1) * (unsigned) vbr_state;
 r/=((unsigned) 0x00FFFFFF)+1;
 return r^c;
}

canonical_original_code:
static unsigned char taf_translate(unsigned char c)
{
 int r;
 vbr_state = (vbr_state*0x43FD43FD +0x00C39EC3) & 0x00FFFFFF;
 r=(0x7f * 2 + 1) * (unsigned int) vbr_state;
 r/=((unsigned int) 0x00FFFFFF)+1;
 return r^c;
}

variable_types:
c
unsigned char

variable_types: r int

return_type: unsigned char


original_code:
int32 adrift_treaty(int32 selector,
                   void *story_file, int32 extent,
                   void *output, int32 output_extent)
{
 int32 ll, csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((signed) strlen("http://www.adrift.org.uk")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.adrift.org.uk");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"adrift",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".taf")+1) < 512 ? (strlen(".taf")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".taf", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
  case 0x309:
                return 0;
  case 0x106:
  case 0x107:
  case 0x30A:
                return 0;
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}

canonical_original_code:
int adrift_treaty(int selector, void *story_file, int extent, void *output, int output_extent)
{
 int ll; int csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((int) strlen("http://www.adrift.org.uk")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.adrift.org.uk");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"adrift",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".taf")+1) < 512 ? (strlen(".taf")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".taf", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
  case 0x309:
                return 0;
  case 0x106:
  case 0x107:
  case 0x30A:
                return 0;
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}

variable_types: selector int

variable_types: csf int


decompiled_code:
__int64 __fastcall claim_story_file(__int64 a1, int a2)
{
__int64 result; // rax
__int64 i; // rbx
char v4[24]; // [rsp+8h] [rbp-18h] BYREF

v4[7] = 0;
vbr_state = 10526342;
result = 0xFFFFFFFFLL;
if ( a2 > 11 )
{
for ( i = 0LL; i != 7; ++i )
v4[i] = taf_translate(*(unsigned __int8 *)(a1 + i));
result = strcmp(v4, "Version") == 0 ? 1 : -1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
__int64 result; // rax
__int64 i; // rbx
char v4[24]; // [rsp+8h] [rbp-18h] BYREF

v4[7] = 0;
vbr_state = 10526342;
result = 0xFFFFFFFFLL;
if ( a2 > 11 )
{
for ( i = 0LL; i != 7; ++i )
v4[i] = func0(*(unsigned __int8 *)(a1 + i));
result = strcmp(v4, "Version") == 0 ? 1 : -1;
}
return result;
}

original_code:
static int32 claim_story_file(void *story_file, int32 extent)
{
 unsigned char buf[8];
 int i;
 unsigned char *sf=(unsigned char *)story_file;
 buf[7]=0;
 vbr_state=0x00A09E86;
 if (extent<12) return -1;
 for(i=0;i<7;i++) buf[i]=taf_translate(sf[i]);
 if (strcmp((char *)buf,"Version")) return -1;
 return 1;
}

canonical_original_code:
static int claim_story_file(void *story_file, int extent)
{
 unsigned char buf[8];
 int i;
 unsigned char *sf = (unsigned char *)story_file;
 buf[7]=0;
 vbr_state=0x00A09E86;
 if (extent<12) return -1;
 for(i=0;i<7;i++) buf[i]=taf_translate(sf[i]);
 if (strcmp((char *)buf,"Version")) return -1;
 return 1;
}

variable_types:
buf
unsigned char [8]

variable_types:
sf
unsigned char *


decompiled_code:
__int64 __fastcall get_story_file_IFID(unsigned __int8 *a1, int a2, char *a3, int a4)
{
__int64 result; // rax
int v7; // ebx
unsigned int v8; // eax
char nptr[44]; // [rsp+0h] [rbp-2Ch] BYREF

vbr_state = 10526342;
result = 0xFFFFFFFFLL;
if ( a2 > 11 )
{
nptr[3] = 0;
v7 = 8;
do
{
taf_translate(0);
--v7;
}
while ( v7 );
nptr[0] = taf_translate(a1[8]);
taf_translate(0);
nptr[1] = taf_translate(a1[10]);
nptr[2] = taf_translate(a1[11]);
result = 4294967293LL;
if ( a4 > 11 )
{
v8 = atoi(nptr);
sprintf(a3, "ADRIFT-%03d-", v8);
result = 4294967292LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 *a1, int a2, char *a3, int a4)
{
__int64 result; // rax
int v7; // ebx
unsigned int v8; // eax
char nptr[44]; // [rsp+0h] [rbp-2Ch] BYREF

vbr_state = 10526342;
result = 0xFFFFFFFFLL;
if ( a2 > 11 )
{
nptr[3] = 0;
v7 = 8;
do
{
func0(0);
--v7;
}
while ( v7 );
nptr[0] = func0(a1[8]);
func0(0);
nptr[1] = func0(a1[10]);
nptr[2] = func0(a1[11]);
result = 4294967293LL;
if ( a4 > 11 )
{
v8 = atoi(nptr);
sprintf(a3, "ADRIFT-%03d-", v8);
result = 4294967292LL;
}
}
return result;
}

original_code:
static int32 get_story_file_IFID(void *story_file, int32 extent, char *output, int32 output_extent)
{
 int adv;
 unsigned char buf[4];
 unsigned char *sf=(unsigned char *)story_file;
 vbr_state=0x00A09E86;
 if (extent <12) return -1;
 buf[3]=0;
 for(adv=0;adv<8;adv++) taf_translate(0);
 buf[0]=taf_translate(sf[8]);
 taf_translate(0);
 buf[1]=taf_translate(sf[10]);
 buf[2]=taf_translate(sf[11]);
 adv=atoi((char *) buf);
 do { if (output_extent < (12)) return -3; } while (0);
 sprintf(output,"ADRIFT-%03d-",adv);
 return -4;
}

canonical_original_code:
static int get_story_file_IFID(void *story_file, int extent, char *output, int output_extent)
{
 int adv;
 unsigned char buf[4];
 unsigned char *sf = (unsigned char *)story_file;
 vbr_state=0x00A09E86;
 if (extent <12) return -1;
 buf[3]=0;
 for(adv=0;adv<8;adv++) taf_translate(0);
 buf[0]=taf_translate(sf[8]);
 taf_translate(0);
 buf[1]=taf_translate(sf[10]);
 buf[2]=taf_translate(sf[11]);
 adv=atoi((char *) buf);
 do { if (output_extent < (12)) return -3; } while (0);
 sprintf(output,"ADRIFT-%03d-",adv);
 return -4;
}

variable_types: extent int

variable_types:
output
char *

variable_types: adv int

variable_types:
buf
unsigned char [4]


decompiled_code:
size_t __fastcall get_story_file_extension(__int64 a1, int a2, void *a3, int a4)
{
__int64 v6; // rax
int v7; // ecx
char v8; // dl
__int64 v9; // rbx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v6 = 1LL;
do
{
v7 = v6;
v8 = byte_34D[v6++];
}
while ( v8 && v8 != 44 );
if ( a4 <= v7 )
return 4294967293LL;
v9 = v7;
memcpy(a3, byte_34D, v7);
*((_BYTE *)a3 + v9) = 0;
return strlen((const char *)a3);
}

canonical_decompiled_code:
size_t __fastcall func3(__int64 a1, int a2, void *a3, int a4)
{
__int64 v6; // rax
int v7; // ecx
char v8; // dl
__int64 v9; // rbx

if ( !a1 )
return 0xFFFFFFFFLL;
if ( !a2 )
return 0xFFFFFFFFLL;
v6 = 1LL;
do
{
v7 = v6;
v8 = byte_34D[v6++];
}
while ( v8 && v8 != 44 );
if ( a4 <= v7 )
return 4294967293LL;
v9 = v7;
memcpy(a3, byte_34D, v7);
*((_BYTE *)a3 + v9) = 0;
return strlen((const char *)a3);
}

original_code:
static int32 get_story_file_extension(void *sf, int32 extent, char *out, int32 output_extent)
{
 int i;
 if (!sf || !extent) return -1;
 for(i=0;".taf"[i] && ".taf"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".taf",i);
 out[i]=0;
 return strlen(out);
}

canonical_original_code:
static int get_story_file_extension(void *sf, int extent, char *out, int output_extent)
{
 int i;
 if (!sf || !extent) return -1;
 for(i=0;".taf"[i] && ".taf"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".taf",i);
 out[i]=0;
 return strlen(out);
}

variable_types:
sf
void *

variable_types:
out
char *

variable_types: i int


decompiled_code:
const char *__fastcall get_url(const char *a1, const char *a2)
{
char v3[1032]; // [rsp+0h] [rbp-408h] BYREF

sprintf(v3, "curl --silent --fail %s/metadata/%s", a2, a1);
system(v3);
return a1;
}

canonical_decompiled_code:
const char *__fastcall func0(const char *a1, const char *a2)
{
char v3[1032]; // [rsp+0h] [rbp-408h] BYREF

sprintf(v3, "curl --silent --fail %s/metadata/%s", a2, a1);
system(v3);
return a1;
}

original_code:
char *get_url(char *ifid, char *from)
{
 char cmdbuf[1024];
 sprintf(cmdbuf, "curl --silent --fail %s/metadata/%s",from,ifid);
 system(cmdbuf);
 return ifid;
}

variable_types:
ifid
char *

variable_types:
from
char *

variable_types:
cmdbuf
char [1024]

return_type: char *


decompiled_code:
const char *__fastcall get_url_cover(const char *a1, const char *a2)
{
char v3[1032]; // [rsp+0h] [rbp-408h] BYREF

sprintf(v3, "curl --silent --fail %s/cover/%s > %s.jpg", a2, a1, a1);
system(v3);
return a1;
}

canonical_decompiled_code:
const char *__fastcall func1(const char *a1, const char *a2)
{
char v3[1032]; // [rsp+0h] [rbp-408h] BYREF

sprintf(v3, "curl --silent --fail %s/cover/%s > %s.jpg", a2, a1, a1);
system(v3);
return a1;
}

original_code:
char *get_url_cover(char *ifid, char *from)
{
 char cmdbuf[1024];
 sprintf(cmdbuf, "curl --silent --fail %s/cover/%s > %s.jpg",from,ifid, ifid);
 system(cmdbuf);
 return ifid;
}


decompiled_code:
char __fastcall verify_eh(const char *a1, int *a2)
{
int v2; // eax
bool v4; // zf

v2 = *a2;
if ( *a2 >= 0 )
{
if ( v2 || (v4 = memcmp(a1, "Warning", 7uLL) == 0, (LOBYTE(v2) = !v4) != 0) )
{
isok = 0;
LOBYTE(v2) = fprintf(stderr, "%s\n", a1);
}
}
return v2;
}

canonical_decompiled_code:
char __fastcall func0(const char *a1, int *a2)
{
int v2; // eax
bool v4; // zf

v2 = *a2;
if ( *a2 >= 0 )
{
if ( v2 || (v4 = memcmp(a1, "Warning", 7uLL) == 0, (LOBYTE(v2) = !v4) != 0) )
{
isok = 0;
LOBYTE(v2) = fprintf(stderr, "%s\n", a1);
}
}
return v2;
}

original_code:
static void verify_eh(char *e, void *ctx)
{
 if (*((int *)ctx) < 0) return;
 if (*((int *)ctx) || strncmp(e,"Warning",7))
  { isok=0;
   fprintf(stderr, "%s\n",e);
  }
}

variable_types:
e
char *

variable_types:
ctx
void *


original_code:
static void examine_tag(struct XMLTag *xtg, void *ctx)
{
 struct IFiction_Info *xti=(struct IFiction_Info *)ctx;
 if (strcmp("ifid",xtg->tag)==0 && strcmp(xti->ifid,"UNKNOWN")==0)
 {
 memcpy(xti->ifid,xtg->begin,xtg->end-xtg->begin);
 xti->ifid[xtg->end-xtg->begin]=0;
 }
}

canonical_original_code:
static void examine_tag(struct XMLTag *xtg, void *ctx)
{
 struct IFiction_Info *xti = (struct IFiction_Info *)ctx;
 if (strcmp("ifid",xtg->tag)==0 && strcmp(xti->ifid,"UNKNOWN")==0)
 {
 memcpy(xti->ifid,xtg->begin,xtg->end-xtg->begin);
 xti->ifid[xtg->end-xtg->begin]=0;
 }
}

variable_types: struct XMLTag { int beginl; char tag[256]; char fulltag[256]; char *begin; char *end; char occurences[256]; char rocurrences[256]; struct XMLTag *next; } *

variable_types: struct IFiction_Info { char ifid[256]; int wmode; } *


decompiled_code:
char *__fastcall babel_ifiction_ifid(__int64 a1)
{
char *result; // rax
const char *i; // rsi
char v3[520]; // [rsp+0h] [rbp-208h] BYREF

if ( !(unsigned int)ifiction_get_IFID(a1, v3, 512LL) )
return (char *)fwrite("Error: No IFIDs found in iFiction file\n", 1uLL, 0x27uLL, stderr);
result = strtok(v3, ",");
for ( i = result; result; i = result )
{
printf("IFID: %s\n", i);
result = strtok(0LL, ",");
}
return result;
}

canonical_decompiled_code:
char *__fastcall func3(__int64 a1)
{
char *result; // rax
const char *i; // rsi
char v3[520]; // [rsp+0h] [rbp-208h] BYREF

if ( !(unsigned int)ifiction_get_IFID(a1, v3, 512LL) )
return (char *)fwrite("Error: No IFIDs found in iFiction file\n", 1uLL, 0x27uLL, stderr);
result = strtok(v3, ",");
for ( i = result; result; i = result )
{
printf("IFID: %s\n", i);
result = strtok(0LL, ",");
}
return result;
}

original_code:
void babel_ifiction_ifid(char *md)
{
 char output[512];
 int i;
 char *ep;
 i=ifiction_get_IFID(md,output,512);
 if (!i)
 {
  fprintf(stderr,"Error: No IFIDs found in iFiction file\n");
  return;
 }
 ep=strtok(output,",");
 while(ep)
 {
  printf("IFID: %s\n",ep);
  ep=strtok(((void *)0),",");
 }
}

variable_types:
md
char *

variable_types:
output
char [512]

variable_types:
ep
char *


decompiled_code:
__int64 __fastcall babel_ifiction_fish(__int64 a1)
{
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = -1;
return ifiction_parse(a1, write_story_to_disk, 0LL, verify_eh, v2);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = -1;
return ifiction_parse(a1, write_story_to_disk, 0LL, verify_eh, v2);
}

original_code:
void babel_ifiction_fish(char *md)
{
 int i=-1;
 ifiction_parse(md,write_story_to_disk,((void *)0),verify_eh,&i);
}

canonical_original_code:
void babel_ifiction_fish(char *md)
{
 int i = -1;
 ifiction_parse(md,write_story_to_disk,((void *)0),verify_eh,&i);
}


decompiled_code:
int __fastcall deep_ifiction_verify(__int64 a1, int a2)
{
int result; // eax
int v3; // [rsp+Ch] [rbp-11Ch] BYREF
__int64 v4[32]; // [rsp+10h] [rbp-118h] BYREF
int v5; // [rsp+110h] [rbp-18h]

v3 = 0;
v5 = 0;
isok = 1;
v4[0] = 0x4E574F4E4B4E55LL;
result = ifiction_parse(a1, examine_tag, v4, verify_eh, &v3);
if ( a2 )
{
if ( isok )
result = printf("Verified %s\n", (const char *)v4);
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1, int a2)
{
int result; // eax
int v3; // [rsp+Ch] [rbp-11Ch] BYREF
__int64 v4[32]; // [rsp+10h] [rbp-118h] BYREF
int v5; // [rsp+110h] [rbp-18h]

v3 = 0;
v5 = 0;
isok = 1;
v4[0] = 0x4E574F4E4B4E55LL;
result = ifiction_parse(a1, examine_tag, v4, verify_eh, &v3);
if ( a2 )
{
if ( isok )
result = printf("Verified %s\n", (const char *)v4);
}
return result;
}

original_code:
void deep_ifiction_verify(char *md, int f)
{
 struct IFiction_Info ii;
 int i=0;
 ii.wmode=0;
 isok=1;
 strcpy(ii.ifid,"UNKNOWN");
 ifiction_parse(md,examine_tag,&ii,verify_eh,&i);
 if (f&& isok) printf("Verified %s\n",ii.ifid);
}

canonical_original_code:
void deep_ifiction_verify(char *md, int f)
{
 struct IFiction_Info ii;
 int i = 0;
 ii.wmode=0;
 isok=1;
 strcpy(ii.ifid,"UNKNOWN");
 ifiction_parse(md,examine_tag,&ii,verify_eh,&i);
 if (f&& isok) printf("Verified %s\n",ii.ifid);
}

variable_types: f int

variable_types: struct IFiction_Info { char ifid[256]; int wmode; }


decompiled_code:
int __fastcall babel_ifiction_verify(__int64 a1)
{
return deep_ifiction_verify(a1, 1);
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1)
{
return func5(a1, 1);
}

original_code:
void babel_ifiction_verify(char *md)
{
 deep_ifiction_verify(md,1);
}


decompiled_code:
int __fastcall babel_ifiction_lint(__int64 a1)
{
int result; // eax
int v2; // [rsp+Ch] [rbp-11Ch] BYREF
__int64 v3[32]; // [rsp+10h] [rbp-118h] BYREF
int v4; // [rsp+110h] [rbp-18h]

v2 = 1;
v4 = 1;
isok = 1;
v3[0] = 0x4E574F4E4B4E55LL;
result = ifiction_parse(a1, examine_tag, v3, verify_eh, &v2);
if ( isok )
result = printf("%s conforms to iFiction style guidelines\n", (const char *)v3);
return result;
}

canonical_decompiled_code:
int __fastcall func7(__int64 a1)
{
int result; // eax
int v2; // [rsp+Ch] [rbp-11Ch] BYREF
__int64 v3[32]; // [rsp+10h] [rbp-118h] BYREF
int v4; // [rsp+110h] [rbp-18h]

v2 = 1;
v4 = 1;
isok = 1;
v3[0] = 0x4E574F4E4B4E55LL;
result = ifiction_parse(a1, examine_tag, v3, verify_eh, &v2);
if ( isok )
result = printf("%s conforms to iFiction style guidelines\n", (const char *)v3);
return result;
}

original_code:
void babel_ifiction_lint(char *md)
{
 struct IFiction_Info ii;
 int i=1;
 ii.wmode=1;
 isok=1;
 strcpy(ii.ifid,"UNKNOWN");
 ifiction_parse(md,examine_tag,&ii,verify_eh,&i);
 if (isok) printf("%s conforms to iFiction style guidelines\n",ii.ifid);
}

canonical_original_code:
void babel_ifiction_lint(char *md)
{
 struct IFiction_Info ii;
 int i = 1;
 ii.wmode=1;
 isok=1;
 strcpy(ii.ifid,"UNKNOWN");
 ifiction_parse(md,examine_tag,&ii,verify_eh,&i);
 if (isok) printf("%s conforms to iFiction style guidelines\n",ii.ifid);
}

user_defined_types: struct IFiction_Info { char ifid[256]; int wmode; }


decompiled_code:
void __fastcall tads2_treaty(int a1, __int64 a2, unsigned int a3, char *a4, int a5)
{
if ( ((a1 & 0x100) == 0 || (int)claim_story_file(a2, a3) >= 0) && ((a1 & 0x200) == 0 || a5 && a4) )
{
if ( a1 == 514 )
{
if ( a5 > 511 )
strncpy(a4, "tads2", a5 - 1);
}
else if ( a1 <= 514 )
{
if ( a1 == 262 )
{
get_story_file_cover_extent(a2, a3);
}
else if ( a1 <= 262 )
{
if ( a1 != 260 && a1 == 261 )
get_story_file_metadata_extent(a2, a3);
}
else if ( a1 == 263 )
{
get_story_file_cover_format(a2, a3);
}
else if ( a1 == 513 && a5 > 19 )
{
strcpy(a4, "http://www.tads.org");
}
}
else if ( a1 == 777 )
{
get_story_file_metadata(a2, a3, a4, (unsigned int)a5);
}
else if ( a1 <= 777 )
{
if ( a1 == 515 )
{
if ( a5 > 4 )
strncpy(a4, byte_2F1, a5);
}
else if ( a1 == 776 )
{
get_story_file_IFID(a2, a3, a4, (unsigned int)a5);
}
}
else if ( a1 == 778 )
{
get_story_file_cover(a2, a3, a4, (unsigned int)a5);
}
else if ( a1 == 779 )
{
get_story_file_extension(a2, a3, a4, a5);
}
}
}

original_code:
int32 tads2_treaty(int32 selector,
                   void *story_file, int32 extent,
                   void *output, int32 output_extent)
{
 int32 ll, csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((signed) strlen("http://www.tads.org")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.tads.org");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"tads2",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".gam")+1) < 512 ? (strlen(".gam")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".gam", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
                return get_story_file_metadata_extent(story_file, extent);
  case 0x309:
                return get_story_file_metadata(story_file, extent, (char *)output, output_extent);
  case 0x106:
                return get_story_file_cover_extent(story_file, extent);
  case 0x107:
                return get_story_file_cover_format(story_file, extent);
  case 0x30A:
                return get_story_file_cover(story_file, extent, output, output_extent);
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}

canonical_original_code:
int tads2_treaty(int selector, void *story_file, int extent, void *output, int output_extent)
{
 int ll; int csf;
 if ((0x100 & selector) &&
     (csf=claim_story_file(story_file, extent))<0)
         return -1;
 if ((0x200 & selector) &&
     (output_extent ==0 ||
     output==((void *)0)))
        return -3;
 switch(selector)
 {
  case 0x201:
                do { if (output_extent < ((int) strlen("http://www.tads.org")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.tads.org");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"tads2",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".gam")+1) < 512 ? (strlen(".gam")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".gam", output_extent);
                return 0;
  case 0x104:
                return csf;
  case 0x105:
                return get_story_file_metadata_extent(story_file, extent);
  case 0x309:
                return get_story_file_metadata(story_file, extent, (char *)output, output_extent);
  case 0x106:
                return get_story_file_cover_extent(story_file, extent);
  case 0x107:
                return get_story_file_cover_format(story_file, extent);
  case 0x30A:
                return get_story_file_cover(story_file, extent, output, output_extent);
  case 0x308:
                return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
  case 0x30B:
                return get_story_file_extension(story_file, extent, (char *)output, output_extent);
 }
 return -2;
}

variable_types: selector int

variable_types: extent int

variable_types:
output
void *

variable_types: ll int

variable_types: csf int


decompiled_code:
__int64 __fastcall claim_story_file(__int64 a1, __int64 a2)
{
return (unsigned int)tads_match_sig(a1, a2, &unk_2E4) == 0 ? -1 : 1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
return (unsigned int)tads_match_sig(a1, a2, &unk_2E4) == 0 ? -1 : 1;
}

original_code:
static int32 claim_story_file(void *story_file, int32 extent)
{
    if (tads_match_sig(story_file, extent, "TADS2 bin\012\015\032"))
        return 1;
    return -1;
}

canonical_original_code:
static int claim_story_file(void *story_file, int extent)
{
    if (tads_match_sig(story_file, extent, "TADS2 bin\012\015\032"))
        return 1;
    return -1;
}


decompiled_code:
__int64 get_story_file_metadata_extent()
{
return tads_get_story_file_metadata_extent();
}

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

original_code:
static int32 get_story_file_metadata_extent(void *story_file, int32 extent)
{
    return tads_get_story_file_metadata_extent(story_file, extent);
}

canonical_original_code:
static int get_story_file_metadata_extent(void *story_file, int extent)
{
    return tads_get_story_file_metadata_extent(story_file, extent);
}