decompiled_code:
const char *__fastcall get_jpeg_dim(_BYTE *a1, int a2)
{
unsigned __int64 v2; // rdx
_BYTE *v3; // rdi
_WORD *v4; // rax
_WORD *v5; // rdi
int v6; // ecx

if ( *a1 == 0xFF && a1[1] == 0xD8 )
{
v2 = (unsigned __int64)&a1[a2];
v3 = a1 + 2;
while ( (unsigned __int64)v3 <= v2 )
{
v4 = v3 + 1;
if ( *v3 == 0xFF )
goto LABEL_12;
if ( v2 < (unsigned __int64)v4 )
break;
while ( 1 )
{
v4 = (_WORD *)((char *)v4 + 1);
if ( *((_BYTE *)v4 - 1) == 0xFF )
break;
if ( (_WORD *)(v2 + 1) == v4 )
return "(invalid)";
}
do
{
LABEL_12:
v5 = v4;
v6 = *(unsigned __int8 *)v4;
v4 = (_WORD *)((char *)v4 + 1);
if ( v2 < (unsigned __int64)v4 )
return "(invalid 4)";
}
while ( v6 == 255 );
if ( (v6 & 0xF0) == 0xC0 && (v6 & 0xF7) != 0xC4 && v6 != 200 )
{
if ( v2 >= (unsigned __int64)(v5 + 2)
&& v2 >= (unsigned __int64)(v4 + 2)
&& v2 >= (unsigned __int64)v4 + 5
&& v2 >= (unsigned __int64)(v4 + 3) )
{
sprintf(
buffer_2,
"(%dx%d)",
(unsigned __int16)__ROL2__(*(_WORD *)((char *)v4 + 5), 8),
(unsigned __int16)__ROL2__(*(_WORD *)((char *)v4 + 3), 8));
return buffer_2;
}
return "(invalid)";
}
if ( (unsigned int)(v6 - 216) > 1 && v2 >= (unsigned __int64)(v5 + 1) )
{
v3 = (char *)v4 + (unsigned __int16)__ROL2__(*v4, 8);
if ( v2 >= (unsigned __int64)v3 )
continue;
}
return "(invalid)";
}
}
return "(invalid)";
}

canonical_decompiled_code:
const char *__fastcall func2(_BYTE *a1, int a2)
{
unsigned __int64 v2; // rdx
_BYTE *v3; // rdi
_WORD *v4; // rax
_WORD *v5; // rdi
int v6; // ecx

if ( *a1 == 0xFF && a1[1] == 0xD8 )
{
v2 = (unsigned __int64)&a1[a2];
v3 = a1 + 2;
while ( (unsigned __int64)v3 <= v2 )
{
v4 = v3 + 1;
if ( *v3 == 0xFF )
goto LABEL_12;
if ( v2 < (unsigned __int64)v4 )
break;
while ( 1 )
{
v4 = (_WORD *)((char *)v4 + 1);
if ( *((_BYTE *)v4 - 1) == 0xFF )
break;
if ( (_WORD *)(v2 + 1) == v4 )
return "(invalid)";
}
do
{
LABEL_12:
v5 = v4;
v6 = *(unsigned __int8 *)v4;
v4 = (_WORD *)((char *)v4 + 1);
if ( v2 < (unsigned __int64)v4 )
return "(invalid 4)";
}
while ( v6 == 255 );
if ( (v6 & 0xF0) == 0xC0 && (v6 & 0xF7) != 0xC4 && v6 != 200 )
{
if ( v2 >= (unsigned __int64)(v5 + 2)
&& v2 >= (unsigned __int64)(v4 + 2)
&& v2 >= (unsigned __int64)v4 + 5
&& v2 >= (unsigned __int64)(v4 + 3) )
{
sprintf(
buffer_2,
"(%dx%d)",
(unsigned __int16)__ROL2__(*(_WORD *)((char *)v4 + 5), 8),
(unsigned __int16)__ROL2__(*(_WORD *)((char *)v4 + 3), 8));
return buffer_2;
}
return "(invalid)";
}
if ( (unsigned int)(v6 - 216) > 1 && v2 >= (unsigned __int64)(v5 + 1) )
{
v3 = (char *)v4 + (unsigned __int16)__ROL2__(*v4, 8);
if ( v2 >= (unsigned __int64)v3 )
continue;
}
return "(invalid)";
}
}
return "(invalid)";
}

original_code:
static char *get_jpeg_dim(void *img, int32 extent)
{
  unsigned char *dp=(unsigned char *) img;
  unsigned char *ep=dp+extent;
  static char buffer[256];
  unsigned int t1, t2, w, h;
  t1=*(dp++);
  t2=*(dp++);
  if (t1!=0xff || t2!=0xD8 )
  {
   return "(invalid)";
  }
  while(1)
  {
   if (dp>ep) return "(invalid)";
   for(t1=*(dp++);t1!=0xff;t1=*(dp++)) if (dp>ep) return "(invalid)";
   do { t1=*(dp++); if (dp>ep) return "(invalid 4)";} while (t1 == 0xff);
   if ((t1 & 0xF0) == 0xC0 && !(t1==0xC4 || t1==0xC8 || t1==0xCC))
   {
    dp+=3;
    if (dp>ep) return "(invalid)";
    h=*(dp++) << 8;
    if (dp>ep) return "(invalid)";
    h|=*(dp++);
    if (dp>ep) return "(invalid)";
    w=*(dp++) << 8;
    if (dp>ep) return "(invalid)";
    w|=*(dp);
    sprintf(buffer, "(%dx%d)",w,h);
    return buffer;
   }
   else if (t1==0xD8 || t1==0xD9)
    break;
   else
   { int l;
    if (dp>ep) return "(invalid)";
     l=*(dp++) << 8;
    if (dp>ep) return "(invalid)";
     l|= *(dp++);
     l-=2;
     dp+=l;
     if (dp>ep) return "(invalid)";
    }
   }
  return "(invalid)";
}

canonical_original_code:
static char *get_jpeg_dim(void *img, int extent)
{
  unsigned char *dp = (unsigned char *) img;
  unsigned char *ep = dp+extent;
  static char buffer[256];
  unsigned int t1; unsigned int t2; unsigned int w; unsigned int h;
  t1=*(dp++);
  t2=*(dp++);
  if (t1!=0xff || t2!=0xD8 )
  {
   return "(invalid)";
  }
  while(1)
  {
   if (dp>ep) return "(invalid)";
   for(t1=*(dp++);t1!=0xff;t1=*(dp++)) if (dp>ep) return "(invalid)";
   do { t1=*(dp++); if (dp>ep) return "(invalid 4)";} while (t1 == 0xff);
   if ((t1 & 0xF0) == 0xC0 && !(t1==0xC4 || t1==0xC8 || t1==0xCC))
   {
    dp+=3;
    if (dp>ep) return "(invalid)";
    h=*(dp++) << 8;
    if (dp>ep) return "(invalid)";
    h|=*(dp++);
    if (dp>ep) return "(invalid)";
    w=*(dp++) << 8;
    if (dp>ep) return "(invalid)";
    w|=*(dp);
    sprintf(buffer, "(%dx%d)",w,h);
    return buffer;
   }
   else if (t1==0xD8 || t1==0xD9)
    break;
   else
   { int l;
    if (dp>ep) return "(invalid)";
     l=*(dp++) << 8;
    if (dp>ep) return "(invalid)";
     l|= *(dp++);
     l-=2;
     dp+=l;
     if (dp>ep) return "(invalid)";
    }
   }
  return "(invalid)";
}

variable_types:
dp
unsigned char *

variable_types:
ep
unsigned char *

variable_types:
buffer
char [256]

variable_types:
w
unsigned int

variable_types:
h
unsigned int


repo: ricpelo/elcirculo

canonical_original_code:
static char *get_png_dim(void *img, int extent)
{
 unsigned char *dp = (unsigned char *)img;
 static char buffer[256];
 int w; int h;
 if (extent<33 ||
 !(dp[0]==137 && dp[1]==80 && dp[2]==78 && dp[3]==71 &&
        dp[4]==13 && dp[5] == 10 && dp[6] == 26 && dp[7]==10)||
 !(dp[12]=='I' && dp[13]=='H' && dp[14]=='D' && dp[15]=='R'))
 return "(invalid)";
 w=read_int(dp+16);
 h=read_int(dp+20);
 sprintf(buffer,"(%dx%d)",w,h);
 return buffer;
}

variable_types:
img
void *

variable_types: extent int

variable_types: w int

variable_types: h int

return_type: char *


original_code:
int32 magscrolls_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.if-legends.org/~msmemorial/memorial.htm")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.if-legends.org/~msmemorial/memorial.htm");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"magscrolls",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".mag")+1) < 512 ? (strlen(".mag")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".mag", 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 magscrolls_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.if-legends.org/~msmemorial/memorial.htm")+1)) return -3; } while (0);
                strcpy((char *) output,"http://www.if-legends.org/~msmemorial/memorial.htm");
                return 0;
  case 0x202:
                do { if (output_extent < (512)) return -3; } while (0);
                strncpy((char *) output,"magscrolls",output_extent-1);
                return 0;
  case 0x203:
                ll=(strlen(".mag")+1) < 512 ? (strlen(".mag")+1):512;
                do { if (output_extent < (ll)) return -3; } while (0);
                strncpy((char *) output, ".mag", 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

return_type: int


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

result = 0xFFFFFFFFLL;
if ( a2 > 41 )
result = 2 * (unsigned int)(*a1 == 1666408781) - 1;
return result;
}

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

result = 0xFFFFFFFFLL;
if ( a2 > 41 )
result = 2 * (unsigned int)(*a1 == 1666408781) - 1;
return result;
}

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

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


decompiled_code:
__int64 __fastcall get_story_file_IFID(__int64 a1, int a2, void *a3, int a4)
{
int v5; // er9
char *v7; // rax
int v8; // esi
__int64 v9; // rdi
__int64 result; // rax
const char *v11; // r12
size_t v12; // rax

if ( a2 <= 41 )
return 0xFFFFFFFFLL;
if ( off_320 )
{
v5 = *(unsigned __int8 *)(a1 + 13);
v7 = (char *)&unk_304;
v8 = 0;
v9 = a1 + 12;
while ( ((unsigned __int8)v5 > 2u || *((_DWORD *)v7 - 1) != v5)
&& (*(_OWORD *)v9 != *(_OWORD *)v7 || *((_DWORD *)v7 + 4) != *(_DWORD *)(v9 + 16)) )
{
v7 += 72;
++v8;
if ( !*(_QWORD *)(v7 + 28) )
goto LABEL_9;
}
v11 = off_330[9 * v8];
v12 = strlen(v11);
if ( a4 <= (int)v12 )
{
result = 4294967293LL;
}
else
{
memcpy(a3, v11, v12 + 1);
result = 1LL;
}
}
else
{
LABEL_9:
strcpy((char *)a3, "MAGNETIC-");
result = 4294967292LL;
}
return result;
}

original_code:
static int32 get_story_file_IFID(void *story_file, int32 extent, char *output, int32 output_extent)
{
 int i;
 unsigned char *sf=(unsigned char *)story_file;
 if (extent < 42) return -1;
 for(i=0;manifest[i].title;i++)
  if ((sf[13]<3 && manifest[i].gv==sf[13]) || memcmp(manifest[i].header,sf+12,20)==0)
   {
    do { if (output_extent < (((int32) strlen(manifest[i].ifid)+1))) return -3; } while (0);
    strcpy(output,manifest[i].ifid);
    return 1;
   }
 strcpy(output,"MAGNETIC-");
 return -4;
}

canonical_original_code:
static int get_story_file_IFID(void *story_file, int extent, char *output, int output_extent)
{
 int i;
 unsigned char *sf = (unsigned char *)story_file;
 if (extent < 42) return -1;
 for(i=0;manifest[i].title;i++)
  if ((sf[13]<3 && manifest[i].gv==sf[13]) || memcmp(manifest[i].header,sf+12,20)==0)
   {
    do { if (output_extent < (((int) strlen(manifest[i].ifid)+1))) return -3; } while (0);
    strcpy(output,manifest[i].ifid);
    return 1;
   }
 strcpy(output,"MAGNETIC-");
 return -4;
}

variable_types:
output
char *

variable_types: i int

variable_types:
sf
unsigned char *


decompiled_code:
size_t __fastcall get_story_file_extension(__int64 a1, int a2, const char *a3, int a4)
{
char v5; // dl
int v7; // ecx
__int64 v8; // rax
unsigned int i; // eax
__int64 v10; // rdx

if ( !a1 || !a2 )
return 0xFFFFFFFFLL;
v5 = 109;
v7 = 1;
v8 = 2LL;
do
{
if ( v5 == 44 )
break;
v5 = byte_5D1[v8];
v7 = v8++;
}
while ( v5 );
if ( a4 <= v7 )
return 4294967293LL;
if ( v7 )
{
for ( i = 0; i < v7; ++i )
{
v10 = i;
a3[v10] = byte_5D1[v10];
}
}
a3[v7] = 0;
return strlen(a3);
}

canonical_decompiled_code:
size_t __fastcall func2(__int64 a1, int a2, const char *a3, int a4)
{
char v5; // dl
int v7; // ecx
__int64 v8; // rax
unsigned int i; // eax
__int64 v10; // rdx

if ( !a1 || !a2 )
return 0xFFFFFFFFLL;
v5 = 109;
v7 = 1;
v8 = 2LL;
do
{
if ( v5 == 44 )
break;
v5 = byte_5D1[v8];
v7 = v8++;
}
while ( v5 );
if ( a4 <= v7 )
return 4294967293LL;
if ( v7 )
{
for ( i = 0; i < v7; ++i )
{
v10 = i;
a3[v10] = byte_5D1[v10];
}
}
a3[v7] = 0;
return strlen(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;".mag"[i] && ".mag"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".mag",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;".mag"[i] && ".mag"[i]!=',';i++);
 do { if (output_extent < (i+1)) return -3; } while (0);
 memcpy(out,".mag",i);
 out[i]=0;
 return strlen(out);
}

variable_types:
sf
void *

variable_types:
out
char *


decompiled_code:
__int64 __fastcall blorb_get_chunk(__int64 a1, int a2, int *a3)
{
int v3; // esi
int v5; // ebx
int v6; // edx
int v7; // eax
int v8; // edx
_DWORD *v9; // r8
int v10; // er10
_DWORD *v11; // r11
__int64 v12; // rcx
__int64 result; // rax

v3 = a2 - 8;
if ( v3 <= 12 )
return 0LL;
v5 = *a3;
v6 = 12;
while ( 1 )
{
v7 = read_int((unsigned int *)(a1 + v6 + 4));
if ( v5 == *(_DWORD *)(a1 + v12) )
break;
v6 = v7 + (v7 & 1) + 8 + v8;
if ( v6 >= v3 )
return 0LL;
}
*v9 = v7;
if ( v10 >= v7 )
{
result = 1LL;
*v11 = v8 + 8;
return result;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2, int *a3)
{
int v3; // esi
int v5; // ebx
int v6; // edx
int v7; // eax
int v8; // edx
_DWORD *v9; // r8
int v10; // er10
_DWORD *v11; // r11
__int64 v12; // rcx
__int64 result; // rax

v3 = a2 - 8;
if ( v3 <= 12 )
return 0LL;
v5 = *a3;
v6 = 12;
while ( 1 )
{
v7 = func0((unsigned int *)(a1 + v6 + 4));
if ( v5 == *(_DWORD *)(a1 + v12) )
break;
v6 = v7 + (v7 & 1) + 8 + v8;
if ( v6 >= v3 )
return 0LL;
}
*v9 = v7;
if ( v10 >= v7 )
{
result = 1LL;
*v11 = v8 + 8;
return result;
}
return 0LL;
}

original_code:
static int32 blorb_get_chunk(void *blorb_file, int32 extent, char *id, int32 *begin, int32 *output_extent)
{
 int32 i=12, j;
 while(i<extent-8)
 {
  if (memcmp(((char *)blorb_file)+i,id,4)==0)
  {
   *output_extent=read_int((char *)blorb_file+i+4);
   if (*output_extent > extent) return 0;
   *begin=i+8;
   return 1;
  }
  j=read_int((char *)blorb_file+i+4);
  if (j%2) j++;
  i+=j+8;
 }
 return 0;
}

canonical_original_code:
static int blorb_get_chunk(void *blorb_file, int extent, char *id, int *begin, int *output_extent)
{
 int i = 12; int j;
 while(i<extent-8)
 {
  if (memcmp(((char *)blorb_file)+i,id,4)==0)
  {
   *output_extent=read_int((char *)blorb_file+i+4);
   if (*output_extent > extent) return 0;
   *begin=i+8;
   return 1;
  }
  j=read_int((char *)blorb_file+i+4);
  if (j%2) j++;
  i+=j+8;
 }
 return 0;
}

variable_types:
id
char *

variable_types:
begin
int *

variable_types: j int


decompiled_code:
__int64 __fastcall blorb_get_resource(__int64 a1, int a2, _DWORD *a3, int a4, _DWORD *a5, _DWORD *a6)
{
int v10; // esi
__int64 v11; // r10
unsigned int *v12; // rdx
int v13; // ecx
__int64 v14; // r9
__int64 v15; // r10
int v17; // [rsp+Ch] [rbp-2Ch]
int v18; // [rsp+Ch] [rbp-2Ch]

if ( !(unsigned int)blorb_get_chunk(a1, a2, (int *)"RIdx") )
return 0LL;
v10 = read_int((unsigned int *)(a1 + v17));
if ( v10 <= 0 )
return 0LL;
v12 = (unsigned int *)(v11 + 4);
v13 = 1;
while ( *(v12 - 1) != *a3 || (unsigned int)read_int(v12) != a4 )
{
v12 += 3;
if ( v10 == v13 )
return 0LL;
++v13;
}
v18 = read_int((unsigned int *)(v15 + v14 + 8));
*a5 = v18 + 8;
*a6 = read_int((unsigned int *)(a1 + v18 + 4));
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2, _DWORD *a3, int a4, _DWORD *a5, _DWORD *a6)
{
int v10; // esi
__int64 v11; // r10
unsigned int *v12; // rdx
int v13; // ecx
__int64 v14; // r9
__int64 v15; // r10
int v17; // [rsp+Ch] [rbp-2Ch]
int v18; // [rsp+Ch] [rbp-2Ch]

if ( !(unsigned int)func2(a1, a2, (int *)"RIdx") )
return 0LL;
v10 = func0((unsigned int *)(a1 + v17));
if ( v10 <= 0 )
return 0LL;
v12 = (unsigned int *)(v11 + 4);
v13 = 1;
while ( *(v12 - 1) != *a3 || (unsigned int)func0(v12) != a4 )
{
v12 += 3;
if ( v10 == v13 )
return 0LL;
++v13;
}
v18 = func0((unsigned int *)(v15 + v14 + 8));
*a5 = v18 + 8;
*a6 = func0((unsigned int *)(a1 + v18 + 4));
return 1LL;
}

original_code:
static int32 blorb_get_resource(void *blorb_file, int32 extent, char *rid, int32 number, int32 *begin, int32 *output_extent)
{
 int32 ridx_len;
 int32 i,j;
 void *ridx;
 if (blorb_get_chunk(blorb_file, extent,"RIdx",&i,&ridx_len)==0)
  return 0;
 ridx=(char *)blorb_file+i+4;
 ridx_len=read_int((char *)blorb_file+i);
 for(i=0;i<ridx_len;i++)
 {
  if(memcmp((char *)ridx+(i*12),rid,4)==0 && read_int((char *)ridx+(i*12)+4)==number)
  {
   j=i;
   i=read_int((char *)ridx+(j*12)+8);
   *begin=i+8;
   *output_extent=read_int((char *)blorb_file+i+4);
   return 1;
  }
 }
 return 0;
}

canonical_original_code:
static int blorb_get_resource(void *blorb_file, int extent, char *rid, int number, int *begin, int *output_extent)
{
 int ridx_len;
 int i; int j;
 void *ridx;
 if (blorb_get_chunk(blorb_file, extent,"RIdx",&i,&ridx_len)==0)
  return 0;
 ridx=(char *)blorb_file+i+4;
 ridx_len=read_int((char *)blorb_file+i);
 for(i=0;i<ridx_len;i++)
 {
  if(memcmp((char *)ridx+(i*12),rid,4)==0 && read_int((char *)ridx+(i*12)+4)==number)
  {
   j=i;
   i=read_int((char *)ridx+(j*12)+8);
   *begin=i+8;
   *output_extent=read_int((char *)blorb_file+i+4);
   return 1;
  }
 }
 return 0;
}

variable_types:
rid
char *

variable_types: number int

variable_types:
ridx
void *


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

result = blorb_get_resource(a1, a2, "Exec", 0, &v3, &v4);
if ( (_DWORD)result )
result = v4;
return result;
}

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

result = func4(a1, a2, "Exec", 0, &v3, &v4);
if ( (_DWORD)result )
result = v4;
return result;
}

original_code:
static int32 get_story_extent(void *blorb_file, int32 extent)
{
 int32 i,j;
 if (blorb_get_resource(blorb_file, extent, "Exec", 0, &i, &j))
 {
  return j;
 }
 return 0;
}

canonical_original_code:
static int get_story_extent(void *blorb_file, int extent)
{
 int i; int j;
 if (blorb_get_resource(blorb_file, extent, "Exec", 0, &i, &j))
 {
  return j;
 }
 return 0;
}

variable_types: extent int


decompiled_code:
__int64 __fastcall blorb_get_cover(__int64 a1, int a2, int *a3, int *a4)
{
__int64 result; // rax
_DWORD *v7; // rdx
_DWORD *v8; // r8
_DWORD *v9; // r9
_DWORD *v10; // rdx
int v11; // [rsp+8h] [rbp-28h]
int v12; // [rsp+8h] [rbp-28h]
int v13; // [rsp+Ch] [rbp-24h]

if ( !(unsigned int)blorb_get_chunk(a1, a2, (int *)"Fspc") )
return 0LL;
if ( v13 <= 3 )
return 0LL;
v12 = read_int((unsigned int *)(a1 + v11));
if ( !(unsigned int)blorb_get_resource(a1, a2, v7, v12, v8, v9) )
return 0LL;
*a3 = v12;
*a4 = v13;
v10 = (_DWORD *)(a1 + v12 - 8);
result = 1LL;
if ( *v10 != 541544016 )
{
if ( *v10 == 1195724874 )
return 2LL;
return 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2, int *a3, int *a4)
{
__int64 result; // rax
_DWORD *v7; // rdx
_DWORD *v8; // r8
_DWORD *v9; // r9
_DWORD *v10; // rdx
int v11; // [rsp+8h] [rbp-28h]
int v12; // [rsp+8h] [rbp-28h]
int v13; // [rsp+Ch] [rbp-24h]

if ( !(unsigned int)func2(a1, a2, (int *)"Fspc") )
return 0LL;
if ( v13 <= 3 )
return 0LL;
v12 = func0((unsigned int *)(a1 + v11));
if ( !(unsigned int)func4(a1, a2, v7, v12, v8, v9) )
return 0LL;
*a3 = v12;
*a4 = v13;
v10 = (_DWORD *)(a1 + v12 - 8);
result = 1LL;
if ( *v10 != 541544016 )
{
if ( *v10 == 1195724874 )
return 2LL;
return 0LL;
}
return result;
}

original_code:
static int32 blorb_get_cover(void *blorb_file, int32 extent, int32 *begin, int32 *output_extent)
{
 int i,j;
 if (blorb_get_chunk(blorb_file,extent,"Fspc",&i,&j))
 {
  if (j<4) return 0;
  i=read_int((char *)blorb_file+i);
  if (!blorb_get_resource(blorb_file,extent,"Pict",i,&i,&j)) return 0;
  *begin=i;
  *output_extent=j;
  if (memcmp((char *)blorb_file+i-8,"PNG ",4)==0) return 1;
  else if (memcmp((char *)blorb_file+i-8,"JPEG",4)==0) return 2;
 }
 return 0;
}

canonical_original_code:
static int blorb_get_cover(void *blorb_file, int extent, int *begin, int *output_extent)
{
 int i; int j;
 if (blorb_get_chunk(blorb_file,extent,"Fspc",&i,&j))
 {
  if (j<4) return 0;
  i=read_int((char *)blorb_file+i);
  if (!blorb_get_resource(blorb_file,extent,"Pict",i,&i,&j)) return 0;
  *begin=i;
  *output_extent=j;
  if (memcmp((char *)blorb_file+i-8,"PNG ",4)==0) return 1;
  else if (memcmp((char *)blorb_file+i-8,"JPEG",4)==0) return 2;
 }
 return 0;
}


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

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

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

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

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

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


decompiled_code:
char *__fastcall blorb_chunk_for_name(char *s1)
{
const char *v1; // rsi
const char *v2; // r12
char **i; // rbx
__int64 v5; // rbx
const __int32_t **v6; // rax
__int64 v7; // rdx
unsigned int v8; // eax
__int64 v9; // rsi

v1 = "zcode";
v2 = "ZCOD";
for ( i = TranslateExec; ; v1 = i[1] )
{
if ( !strcmp(s1, v1) )
return (char *)v2;
v2 = i[2];
i += 2;
if ( !v2 )
break;
}
v5 = 0LL;
while ( s1[v5] )
{
v6 = _ctype_toupper_loc();
v7 = buffer_0[v5++];
*(_BYTE *)(v5 + 2495) = (*v6)[v7];
if ( v5 == 4 )
goto LABEL_9;
}
if ( (_DWORD)v5 != 4 )
{
v8 = 0;
do
{
v9 = v8++;
buffer_0[(int)v5 + v9] = 32;
}
while ( v8 < 4 - (int)v5 );
}
LABEL_9:
byte_9C4 = 0;
return buffer_0;
}

canonical_decompiled_code:
char *__fastcall func14(char *s1)
{
const char *v1; // rsi
const char *v2; // r12
char **i; // rbx
__int64 v5; // rbx
const __int32_t **v6; // rax
__int64 v7; // rdx
unsigned int v8; // eax
__int64 v9; // rsi

v1 = "zcode";
v2 = "ZCOD";
for ( i = TranslateExec; ; v1 = i[1] )
{
if ( !strcmp(s1, v1) )
return (char *)v2;
v2 = i[2];
i += 2;
if ( !v2 )
break;
}
v5 = 0LL;
while ( s1[v5] )
{
v6 = _ctype_toupper_loc();
v7 = buffer_0[v5++];
*(_BYTE *)(v5 + 2495) = (*v6)[v7];
if ( v5 == 4 )
goto LABEL_9;
}
if ( (_DWORD)v5 != 4 )
{
v8 = 0;
do
{
v9 = v8++;
buffer_0[(int)v5 + v9] = 32;
}
while ( v8 < 4 - (int)v5 );
}
LABEL_9:
byte_9C4 = 0;
return buffer_0;
}

original_code:
char *blorb_chunk_for_name(char *name)
{
 static char buffer[5];
 int j;
 for(j=0;TranslateExec[j];j+=2)
  if (strcmp(name,TranslateExec[j+1])==0) return TranslateExec[j];
 for(j=0;j<4 && name[j];j++) buffer[j]=toupper(buffer[j]);
 while(j<4) buffer[j++]=' ';
 buffer[4]=0;
 return buffer;
}

variable_types:
name
char *

variable_types:
buffer
char [5]


decompiled_code:
char *__fastcall blorb_get_story_format(__int64 a1, int a2)
{
void (__fastcall *v2)(__int64, _QWORD, _QWORD, char *, __int64); // rax
unsigned __int64 v3; // rbx
int *v4; // rax

v2 = (void (__fastcall *)(__int64, _QWORD, _QWORD, char *, __int64))treaty_registry;
if ( !treaty_registry )
return 0LL;
v3 = (unsigned int)((_DWORD)&treaty_registry + 8);
do
{
v2(514LL, 0LL, 0LL, fn_1, 512LL);
v4 = (int *)blorb_chunk_for_name(fn_1);
if ( (unsigned int)blorb_get_chunk(a1, a2, v4) )
return fn_1;
v2 = *(void (__fastcall **)(__int64, _QWORD, _QWORD, char *, __int64))v3;
v3 += 8LL;
}
while ( v2 );
return 0LL;
}

canonical_decompiled_code:
char *__fastcall func15(__int64 a1, int a2)
{
void (__fastcall *v2)(__int64, _QWORD, _QWORD, char *, __int64); // rax
unsigned __int64 v3; // rbx
int *v4; // rax

v2 = (void (__fastcall *)(__int64, _QWORD, _QWORD, char *, __int64))treaty_registry;
if ( !treaty_registry )
return 0LL;
v3 = (unsigned int)((_DWORD)&treaty_registry + 8);
do
{
v2(514LL, 0LL, 0LL, fn_1, 512LL);
v4 = (int *)func14(fn_1);
if ( (unsigned int)func2(a1, a2, v4) )
return fn_1;
v2 = *(void (__fastcall **)(__int64, _QWORD, _QWORD, char *, __int64))v3;
v3 += 8LL;
}
while ( v2 );
return 0LL;
}

original_code:
static char *blorb_get_story_format(void *blorb_file, int32 extent)
{
 int32 i, j;
 for(j=0;treaty_registry[j];j++)
 {
  static char fn[512];
  treaty_registry[j](0x202,((void *)0),0,fn,512);
  if (blorb_get_chunk(blorb_file,extent,blorb_chunk_for_name(fn),&i, &i)) return fn;
 }
 return ((void *)0);
}

canonical_original_code:
static char *blorb_get_story_format(void *blorb_file, int extent)
{
 int i; int j;
 for(j=0;treaty_registry[j];j++)
 {
  static char fn[512];
  treaty_registry[j](0x202,((void *)0),0,fn,512);
  if (blorb_get_chunk(blorb_file,extent,blorb_chunk_for_name(fn),&i, &i)) return fn;
 }
 return ((void *)0);
}

variable_types:
fn
char [512]


decompiled_code:
__int64 __fastcall get_story_format(__int64 a1, int a2, void *a3, int a4)
{
char *v6; // rax
char *v7; // rbp
size_t v8; // rax
int v9; // ebx

v6 = blorb_get_story_format(a1, a2);
if ( !v6 )
return 0LL;
v7 = v6;
v8 = strlen(v6);
v9 = v8;
if ( a4 <= (int)v8 )
return 4294967293LL;
memcpy(a3, v7, v8 + 1);
return (unsigned int)(v9 + 1);
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1, int a2, void *a3, int a4)
{
char *v6; // rax
char *v7; // rbp
size_t v8; // rax
int v9; // ebx

v6 = func15(a1, a2);
if ( !v6 )
return 0LL;
v7 = v6;
v8 = strlen(v6);
v9 = v8;
if ( a4 <= (int)v8 )
return 4294967293LL;
memcpy(a3, v7, v8 + 1);
return (unsigned int)(v9 + 1);
}

original_code:
static int32 get_story_format(void *blorb_file, int32 extent, char *output, int32 output_extent)
{
 char *o;
 o=blorb_get_story_format(blorb_file, extent);
 if (!o) return 0;
 do { if (output_extent < ((signed) strlen(o)+1)) return -3; } while (0);
 strcpy(output,o);
 return strlen(o)+1;
}

canonical_original_code:
static int get_story_format(void *blorb_file, int extent, char *output, int output_extent)
{
 char *o;
 o=blorb_get_story_format(blorb_file, extent);
 if (!o) return 0;
 do { if (output_extent < ((int) strlen(o)+1)) return -3; } while (0);
 strcpy(output,o);
 return strlen(o)+1;
}

variable_types:
o
char *


decompiled_code:
void __fastcall blorb_treaty(int a1, _DWORD *a2, int a3, __m128i *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((__int64)a2, a3, (__int64)a4, a5);
}
else if ( a1 > 776 )
{
if ( a1 == 1297 )
{
get_story_extent((__int64)a2, a3);
}
else if ( a1 <= 1297 )
{
switch ( a1 )
{
case 778:
get_story_file_cover((__int64)a2, a3, a4, a5);
break;
case 779:
get_story_file_extension((__int64)a2, a3, a4->m128i_i8, a5);
break;
case 777:
get_story_file_metadata((__int64)a2, a3, a4, a5);
break;
}
}
else if ( a1 == 1808 )
{
get_story_format((__int64)a2, a3, a4, a5);
}
else if ( a1 == 1809 )
{
get_story_file((__int64)a2, a3, a4, a5);
}
}
else if ( a1 == 263 )
{
get_story_file_cover_format((__int64)a2, a3);
}
else if ( a1 <= 263 )
{
if ( a1 == 261 )
{
get_story_file_metadata_extent((__int64)a2, a3);
}
else if ( a1 == 262 )
{
get_story_file_cover_extent((__int64)a2, a3);
}
}
else if ( a1 == 514 )
{
if ( a5 > 511 )
strncpy(a4->m128i_i8, "blorb", a5 - 1);
}
else if ( a1 == 515 )
{
if ( a5 > 37 )
strncpy(a4->m128i_i8, LC6, a5);
}
else if ( a1 == 513 && a5 > 28 )
{
strcpy(a4[1].m128i_i8, "m/zarf/blorb");
*a4 = _mm_load_si128((const __m128i *)&LC10);
}
}
}

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


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 *


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

if ( a2 > 15 && *a1 == 1297239878 )
result = (unsigned int)-(a1[2] != 1397900873);
else
result = 0xFFFFFFFFLL;
return result;
}

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

if ( a2 > 15 && *a1 == 1297239878 )
result = (unsigned int)-(a1[2] != 1397900873);
else
result = 0xFFFFFFFFLL;
return result;
}

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: i int


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

result = blorb_get_chunk(a1, a2, (int *)"IFmd");
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+Ch] [rbp-4h]

result = func2(a1, a2, (int *)"IFmd");
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;
}


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

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

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

result = func6(a1, a2, &v3, (int *)&v4);
if ( (_DWORD)result )
result = v4;
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-8h] BYREF
int v4; // [rsp+Ch] [rbp-4h] BYREF

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

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

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

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; // [rsp+8h] [rbp-30h]
int v8; // [rsp+Ch] [rbp-2Ch]

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

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

result = func2(a1, a2, (int *)"IFmd");
if ( (_DWORD)result )
{
if ( v8 >= a4 )
{
result = 4294967293LL;
}
else
{
memcpy(a3, (const void *)(a1 + v7), v8);
*((_BYTE *)a3 + v8) = 0;
result = (unsigned int)(v8 + 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; // er12
int v8; // [rsp+8h] [rbp-30h] BYREF
int v9[11]; // [rsp+Ch] [rbp-2Ch] BYREF

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

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

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

variable_types: j int


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

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

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, int a2, __int64 a3, unsigned int a4)
{
int v6; // eax
int v7; // er12
void *v8; // rbx
unsigned int v9; // er12

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

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 *


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: extent int

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(__int64 a1, int a2)
{
unsigned __int16 *v2; // rax
unsigned __int16 *v3; // rdx
int v4; // eax
__int64 v5; // rdx
unsigned __int16 *v6; // rcx
int v7; // er8

if ( a1 && a2 > 39 )
{
v2 = (unsigned __int16 *)(a1 + 3);
while ( (unsigned __int8)(*(_BYTE *)v2 - 32) <= 0x5Eu )
{
v2 = (unsigned __int16 *)((char *)v2 + 1);
if ( v2 == (unsigned __int16 *)(a1 + 11) )
{
v3 = v2;
while ( 1 )
{
v4 = read_hugo_addx(v3);
if ( v7 * v4 > a2 )
break;
v3 = (unsigned __int16 *)(v5 + 2);
if ( v6 == v3 )
return 1LL;
}
return 0xFFFFFFFFLL;
}
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
unsigned __int16 *v2; // rax
unsigned __int16 *v3; // rdx
int v4; // eax
__int64 v5; // rdx
unsigned __int16 *v6; // rcx
int v7; // er8

if ( a1 && a2 > 39 )
{
v2 = (unsigned __int16 *)(a1 + 3);
while ( (unsigned __int8)(*(_BYTE *)v2 - 32) <= 0x5Eu )
{
v2 = (unsigned __int16 *)((char *)v2 + 1);
if ( v2 == (unsigned __int16 *)(a1 + 11) )
{
v3 = v2;
while ( 1 )
{
v4 = func0(v3);
if ( v7 * v4 > a2 )
break;
v3 = (unsigned __int16 *)(v5 + 2);
if ( v6 == v3 )
return 1LL;
}
return 0xFFFFFFFFLL;
}
}
}
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, const char *a3, int a4)
{
char v5; // dl
int v7; // ecx
__int64 v8; // rax
unsigned int i; // eax
__int64 v10; // rdx

if ( !a1 || !a2 )
return 0xFFFFFFFFLL;
v5 = 104;
v7 = 1;
v8 = 2LL;
do
{
if ( v5 == 44 )
break;
v5 = byte_433[v8];
v7 = v8++;
}
while ( v5 );
if ( a4 <= v7 )
return 4294967293LL;
if ( v7 )
{
for ( i = 0; i < v7; ++i )
{
v10 = i;
a3[v10] = byte_433[v10];
}
}
a3[v7] = 0;
return strlen(a3);
}

canonical_decompiled_code:
size_t __fastcall func2(__int64 a1, int a2, const char *a3, int a4)
{
char v5; // dl
int v7; // ecx
__int64 v8; // rax
unsigned int i; // eax
__int64 v10; // rdx

if ( !a1 || !a2 )
return 0xFFFFFFFFLL;
v5 = 104;
v7 = 1;
v8 = 2LL;
do
{
if ( v5 == 44 )
break;
v5 = byte_433[v8];
v7 = v8++;
}
while ( v5 );
if ( a4 <= v7 )
return 4294967293LL;
if ( v7 )
{
for ( i = 0; i < v7; ++i )
{
v10 = i;
a3[v10] = byte_433[v10];
}
}
a3[v7] = 0;
return strlen(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);
}


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]


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 i; // rdx
__int64 result; // rax
char v4[7]; // [rsp+1h] [rbp-7h] BYREF

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

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

if ( a2 <= 7 )
goto LABEL_5;
for ( i = 0LL; i != 6; ++i )
v4[i] = ~(*(_BYTE *)(a1 + i + 2) + 30);
v4[6] = 0;
if ( !strcmp(v4, "ADVSYS") )
result = 1LL;
else
LABEL_5:
result = 0xFFFFFFFFLL;
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:
buf
char [7]

variable_types: i int


decompiled_code:
size_t __fastcall get_story_file_extension(__int64 a1, int a2, const char *a3, int a4)
{
char v5; // dl
int v7; // ecx
__int64 v8; // rax
unsigned int i; // eax
__int64 v10; // rdx

if ( !a1 || !a2 )
return 0xFFFFFFFFLL;
v5 = 100;
v7 = 1;
v8 = 2LL;
do
{
if ( v5 == 44 )
break;
v5 = byte_28A[v8];
v7 = v8++;
}
while ( v5 );
if ( a4 <= v7 )
return 4294967293LL;
if ( v7 )
{
for ( i = 0; i < v7; ++i )
{
v10 = i;
a3[v10] = byte_28A[v10];
}
}
a3[v7] = 0;
return strlen(a3);
}

canonical_decompiled_code:
size_t __fastcall func1(__int64 a1, int a2, const char *a3, int a4)
{
char v5; // dl
int v7; // ecx
__int64 v8; // rax
unsigned int i; // eax
__int64 v10; // rdx

if ( !a1 || !a2 )
return 0xFFFFFFFFLL;
v5 = 100;
v7 = 1;
v8 = 2LL;
do
{
if ( v5 == 44 )
break;
v5 = byte_28A[v8];
v7 = v8++;
}
while ( v5 );
if ( a4 <= v7 )
return 4294967293LL;
if ( v7 )
{
for ( i = 0; i < v7; ++i )
{
v10 = i;
a3[v10] = byte_28A[v10];
}
}
a3[v7] = 0;
return strlen(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(_WORD *a1)
{
return (unsigned __int16)__ROL2__(*a1, 8);
}

canonical_decompiled_code:
__int64 __fastcall func0(_WORD *a1)
{
return (unsigned __int16)__ROL2__(*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)
{
_WORD *v3; // rcx
int v4; // eax
_WORD *v5; // rdx
__int64 v6; // rcx

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

canonical_decompiled_code:
__int64 __fastcall func1(_BYTE *a1, int a2)
{
_WORD *v3; // rcx
int v4; // eax
_WORD *v5; // rdx
__int64 v6; // rcx

if ( a2 > 59 && (unsigned __int8)(*a1 - 1) <= 7u )
{
v3 = a1 + 4;
while ( 1 )
{
v4 = func0(v3);
if ( a2 < v4 || v4 <= 63 )
break;
v3 = (_WORD *)(v6 + 2);
if ( v5 == v3 )
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,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

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

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

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

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


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, __int64 a4, int a5)
{
__m128i v8; // xmm0

if ( ((a1 & 0x100) == 0 || (int)claim_story_file(a2, a3) >= 0) && ((a1 & 0x200) == 0 || a5 && a4) )
{
if ( a1 == 514 )
{
if ( a5 > 511 )
strncpy((char *)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 )
{
v8 = _mm_load_si128((const __m128i *)&LC3);
*(_DWORD *)(a4 + 16) = 6779503;
*(__m128i *)a4 = v8;
}
}
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((char *)a4, ".t3", 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;
}

variable_types: extent int


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

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
return (unsigned int)tads_match_sig(a1, a2, &unk_2E6) == 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:
// attributes: thunk
__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:
// attributes: thunk
__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:
// attributes: thunk
__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:
// attributes: thunk
__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:
// attributes: thunk
__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:
// attributes: thunk
__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:
__int64 __fastcall get_story_file_extension(__int64 a1, int a2, __int64 a3, int a4)
{
__int64 result; // rax

if ( !a1 || !a2 )
return 0xFFFFFFFFLL;
if ( a4 <= 3 )
return 4294967293LL;
*(_BYTE *)(a3 + 2) = 51;
*(_WORD *)a3 = 29742;
result = 3LL;
*(_BYTE *)(a3 + 3) = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, int a2, __int64 a3, int a4)
{
__int64 result; // rax

if ( !a1 || !a2 )
return 0xFFFFFFFFLL;
if ( a4 <= 3 )
return 4294967293LL;
*(_BYTE *)(a3 + 2) = 51;
*(_WORD *)a3 = 29742;
result = 3LL;
*(_BYTE *)(a3 + 3) = 0;
return result;
}

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;
}

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(__int64 a1)
{
unsigned __int16 *v1; // r8
unsigned int v2; // eax
int v3; // edx

read_agt_short((unsigned __int16 *)(a1 + 2));
v2 = read_agt_short(v1);
return (v3 << 16) | v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
unsigned __int16 *v1; // r8
unsigned int v2; // eax
int v3; // edx

func0((unsigned __int16 *)(a1 + 2));
v2 = func0(v1);
return (v3 << 16) | v2;
}

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);
}


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(_DWORD *a1, int a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( a2 > 35 )
result = 2 * (unsigned int)(*a1 == AGX_MAGIC) - 1;
return result;
}

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

result = 0xFFFFFFFFLL;
if ( a2 > 35 )
result = 2 * (unsigned int)(*a1 == AGX_MAGIC) - 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)
{
int v2; // eax
__int64 v3; // r9
__int64 v4; // rdx
__int64 v5; // r9
unsigned int v6; // eax
char *v7; // r10
unsigned int v8; // er11
int v9; // ecx

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

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2)
{
int v2; // eax
__int64 v3; // r9
__int64 v4; // rdx
__int64 v5; // r9
unsigned int v6; // eax
char *v7; // r10
unsigned int v8; // er11
int v9; // ecx

v2 = func1(a1 + 32);
if ( v2 + 5 >= a2 )
return 0xFFFFFFFFLL;
func0((unsigned __int16 *)(v3 + v2));
v6 = func1(v5 + v4 + 2);
if ( v9 <= 18 )
return 4294967293LL;
sprintf(v7, "AGT-%05d-%08X", v8, v6);
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, const char *a3, int a4)
{
char v5; // dl
int v7; // ecx
__int64 v8; // rax
unsigned int i; // eax
__int64 v10; // rdx

if ( !a1 || !a2 )
return 0xFFFFFFFFLL;
v5 = 97;
v7 = 1;
v8 = 2LL;
do
{
if ( v5 == 44 )
break;
v5 = byte_2EA[v8];
v7 = v8++;
}
while ( v5 );
if ( a4 <= v7 )
return 4294967293LL;
if ( v7 )
{
for ( i = 0; i < v7; ++i )
{
v10 = i;
a3[v10] = byte_2EA[v10];
}
}
a3[v7] = 0;
return strlen(a3);
}

canonical_decompiled_code:
size_t __fastcall func4(__int64 a1, int a2, const char *a3, int a4)
{
char v5; // dl
int v7; // ecx
__int64 v8; // rax
unsigned int i; // eax
__int64 v10; // rdx

if ( !a1 || !a2 )
return 0xFFFFFFFFLL;
v5 = 97;
v7 = 1;
v8 = 2LL;
do
{
if ( v5 == 44 )
break;
v5 = byte_2EA[v8];
v7 = v8++;
}
while ( v5 );
if ( a4 <= v7 )
return 4294967293LL;
if ( v7 )
{
for ( i = 0; i < v7; ++i )
{
v10 = i;
a3[v10] = byte_2EA[v10];
}
}
a3[v7] = 0;
return strlen(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(_DWORD *a1, int a2)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( a2 > 255 )
result = 2 * (unsigned int)(*a1 == 1819634759) - 1;
return result;
}

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

result = 0xFFFFFFFFLL;
if ( a2 > 255 )
result = 2 * (unsigned int)(*a1 == 1819634759) - 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;
}


decompiled_code:
size_t __fastcall get_story_file_extension(__int64 a1, int a2, const char *a3, int a4)
{
char v5; // dl
int v7; // ecx
__int64 v8; // rax
unsigned int i; // eax
__int64 v10; // rdx

if ( !a1 || !a2 )
return 0xFFFFFFFFLL;
v5 = 117;
v7 = 1;
v8 = 2LL;
do
{
if ( v5 == 44 )
break;
v5 = byte_481[v8];
v7 = v8++;
}
while ( v5 );
if ( a4 <= v7 )
return 4294967293LL;
if ( v7 )
{
for ( i = 0; i < v7; ++i )
{
v10 = i;
a3[v10] = byte_481[v10];
}
}
a3[v7] = 0;
return strlen(a3);
}

canonical_decompiled_code:
size_t __fastcall func2(__int64 a1, int a2, const char *a3, int a4)
{
char v5; // dl
int v7; // ecx
__int64 v8; // rax
unsigned int i; // eax
__int64 v10; // rdx

if ( !a1 || !a2 )
return 0xFFFFFFFFLL;
v5 = 117;
v7 = 1;
v8 = 2LL;
do
{
if ( v5 == 44 )
break;
v5 = byte_481[v8];
v7 = v8++;
}
while ( v5 );
if ( a4 <= v7 )
return 4294967293LL;
if ( v7 )
{
for ( i = 0; i < v7; ++i )
{
v10 = i;
a3[v10] = byte_481[v10];
}
}
a3[v7] = 0;
return strlen(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


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:
void __fastcall str_long(_DWORD *a1, unsigned int a2)
{
*a1 = _byteswap_ulong(a2);
}

canonical_decompiled_code:
void __fastcall func3(_DWORD *a1, unsigned int a2)
{
*a1 = _byteswap_ulong(a2);
}

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:
void __fastcall str_short(_WORD *a1, __int16 a2)
{
*a1 = __ROL2__(a2, 8);
}

canonical_decompiled_code:
void __fastcall func4(_WORD *a1, __int16 a2)
{
*a1 = __ROL2__(a2, 8);
}

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