StartGlutMain

decompiled_code:
__int64 __fastcall StartGlutMain(int a1, __int64 a2, __int64 a3, unsigned int a4, unsigned int a5)
{
__int64 result; // rax
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

v6[0] = a1;
glutInitWindowSize(a4, a5);
glutInit(v6, a2);
glutCreateWindow("Test");
if ( (unsigned int)glewInit() )
{
fwrite("Error: GLEW Fault.\n", 1uLL, 0x13uLL, stderr);
result = 0LL;
}
else
{
StartTime = OGGetAbsoluteTime();
glutSetKeyRepeat(0LL);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutKeyboardFunc(mykeyfunc);
glutKeyboardUpFunc(myupfunc);
glutSpecialFunc(myspecialfunc);
glutSpecialUpFunc(myspecialupfunc);
glutMotionFunc(mousemotion);
glutPassiveMotionFunc(mousepassive);
result = glutMouseFunc(mousefunc);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, __int64 a2, __int64 a3, unsigned int a4, unsigned int a5)
{
__int64 result; // rax
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

v6[0] = a1;
glutInitWindowSize(a4, a5);
glutInit(v6, a2);
glutCreateWindow("Test");
if ( (unsigned int)glewInit() )
{
fwrite("Error: GLEW Fault.\n", 1uLL, 0x13uLL, stderr);
result = 0LL;
}
else
{
StartTime = OGGetAbsoluteTime();
glutSetKeyRepeat(0LL);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutKeyboardFunc(mykeyfunc);
glutKeyboardUpFunc(myupfunc);
glutSpecialFunc(myspecialfunc);
glutSpecialUpFunc(myspecialupfunc);
glutMotionFunc(mousemotion);
glutPassiveMotionFunc(mousepassive);
result = glutMouseFunc(mousefunc);
}
return result;
}

original_code:
int StartGlutMain(int argc, char **argv, const char * title, int initsizeX, int initsizeY)
{
 int i;
 glutInitWindowSize(initsizeX,initsizeY);
 glutInit( &argc, argv );
 glutCreateWindow( "Test" );
 if( glewInit() != 0 )
 {
  fprintf( stderr, "Error: GLEW Fault.\n" );
  return 0;
 }
 StartTime = OGGetAbsoluteTime();
 glutSetKeyRepeat( 0x0000 );
 glutDisplayFunc( display );
 glutReshapeFunc( reshape );
 glutIdleFunc( idle );
 glutKeyboardFunc( mykeyfunc );
 glutKeyboardUpFunc( myupfunc );
 glutSpecialFunc( myspecialfunc );
 glutSpecialUpFunc( myspecialupfunc );
 glutMotionFunc( mousemotion );
 glutPassiveMotionFunc( mousepassive );
 glutMouseFunc( mousefunc );
}

canonical_original_code:
int StartGlutMain(int argc, char **argv, char *title, int initsizeX, int initsizeY)
{
 int i;
 glutInitWindowSize(initsizeX,initsizeY);
 glutInit( &argc, argv );
 glutCreateWindow( "Test" );
 if( glewInit() != 0 )
 {
  fprintf( stderr, "Error: GLEW Fault.\n" );
  return 0;
 }
 StartTime = OGGetAbsoluteTime();
 glutSetKeyRepeat( 0x0000 );
 glutDisplayFunc( display );
 glutReshapeFunc( reshape );
 glutIdleFunc( idle );
 glutKeyboardFunc( mykeyfunc );
 glutKeyboardUpFunc( myupfunc );
 glutSpecialFunc( myspecialfunc );
 glutSpecialUpFunc( myspecialupfunc );
 glutMotionFunc( mousemotion );
 glutPassiveMotionFunc( mousepassive );
 glutMouseFunc( mousefunc );
}

variable_types:
title
char *

variable_types: initsizeX int

variable_types: initsizeY int

variable_types: i int


DoGlutLoop

repo: cnlohr/bridgesim

decompiled_code:
__int64 DoGlutLoop()
{
return glutMainLoop();
}

canonical_decompiled_code:
__int64 func11()
{
return glutMainLoop();
}

original_code:
void DoGlutLoop()
{
 glutMainLoop();
}

canonical_original_code:
void DoGlutLoop()
{
 glutMainLoop();
}

return_type: void


SetMouseWarpMode

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

domousewarp = a1;
if ( a1 )
result = glutSetCursor(101LL);
else
result = glutSetCursor(100LL);
return result;
}

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

domousewarp = a1;
if ( a1 )
result = glutSetCursor(101LL);
else
result = glutSetCursor(100LL);
return result;
}

original_code:
void SetMouseWarpMode( int dowarp )
{
 domousewarp = dowarp;
 if( dowarp )
 {
  glutSetCursor( 0x0065 );
 }
 else
 {
  glutSetCursor( 0x0064 );
 }
}

canonical_original_code:
void SetMouseWarpMode(int dowarp)
{
 domousewarp = dowarp;
 if( dowarp )
 {
  glutSetCursor( 0x0065 );
 }
 else
 {
  glutSetCursor( 0x0064 );
 }
}

variable_types: dowarp int


OGSleep

decompiled_code:
unsigned int __fastcall OGSleep(unsigned int a1)
{
return sleep(a1);
}

canonical_decompiled_code:
unsigned int __fastcall func0(unsigned int a1)
{
return sleep(a1);
}

original_code:
void OGSleep( int is )
{
 sleep( is );
}

canonical_original_code:
void OGSleep(int is)
{
 sleep( is );
}

variable_types: is int


OGUSleep

decompiled_code:
int __fastcall OGUSleep(__useconds_t a1)
{
return usleep(a1);
}

canonical_decompiled_code:
int __fastcall func1(__useconds_t a1)
{
return usleep(a1);
}

original_code:
void OGUSleep( int ius )
{
 usleep( ius );
}

canonical_original_code:
void OGUSleep(int ius)
{
 usleep( ius );
}

variable_types: ius int


OGGetAbsoluteTime

decompiled_code:
double OGGetAbsoluteTime()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return (double)SLODWORD(v1.tv_usec) / 1000000.0 + (double)SLODWORD(v1.tv_sec);
}

canonical_decompiled_code:
double func2()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return (double)SLODWORD(v1.tv_usec) / 1000000.0 + (double)SLODWORD(v1.tv_sec);
}

original_code:
double OGGetAbsoluteTime()
{
 struct timeval tv;
 gettimeofday( &tv, 0 );
 return ((double)tv.tv_usec)/1000000. + (tv.tv_sec);
}

variable_types: struct timeval { long tv_sec; long tv_usec; }

return_type: double


OGGetFileTime

decompiled_code:
double __fastcall OGGetFileTime(const char *a1)
{
double result; // xmm0_8
struct stat v2; // [rsp+0h] [rbp-98h] BYREF

if ( stat(a1, &v2) < 0 )
result = *(double *)&LC1;
else
result = (double)SLODWORD(v2.st_mtim.tv_sec);
return result;
}

canonical_decompiled_code:
double __fastcall func3(const char *a1)
{
double result; // xmm0_8
struct stat v2; // [rsp+0h] [rbp-98h] BYREF

if ( stat(a1, &v2) < 0 )
result = *(double *)&LC1;
else
result = (double)SLODWORD(v2.st_mtim.tv_sec);
return result;
}

original_code:
double OGGetFileTime( const char * file )
{
 struct stat buff;
 int r = stat( file, &buff );
 if( r < 0 )
 {
  return -1;
 }
 return buff.st_mtim.tv_sec;
}

canonical_original_code:
double OGGetFileTime(char *file)
{
 struct stat buff;
 int r = stat( file, &buff );
 if( r < 0 )
 {
  return -1;
 }
 return buff.st_mtim.tv_sec;
}

variable_types:
file
char *

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types: r int

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


OGJoinThread

decompiled_code:
void *__fastcall OGJoinThread(void *ptr)
{
void *thread_return; // [rsp+8h] [rbp-10h] BYREF

if ( !ptr )
return 0LL;
pthread_join(*(_QWORD *)ptr, &thread_return);
free(ptr);
return thread_return;
}

canonical_decompiled_code:
void *__fastcall func5(void *ptr)
{
void *thread_return; // [rsp+8h] [rbp-10h] BYREF

if ( !ptr )
return 0LL;
pthread_join(*(_QWORD *)ptr, &thread_return);
free(ptr);
return thread_return;
}

original_code:
void * OGJoinThread( og_thread_t ot )
{
 void * retval;
 if( !ot )
 {
  return 0;
 }
 pthread_join( *(pthread_t*)ot, &retval );
 free( ot );
 return retval;
}

canonical_original_code:
void *OGJoinThread(void *ot)
{
 void *retval;
 if( !ot )
 {
  return 0;
 }
 pthread_join( *(unsigned long *)ot, &retval );
 free( ot );
 return retval;
}

variable_types:
ot
void *

variable_types:
retval
void *

return_type: void *


OGCancelThread

decompiled_code:
void __fastcall OGCancelThread(void *ptr)
{
if ( ptr )
{
pthread_cancel(*(_QWORD *)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func6(void *ptr)
{
if ( ptr )
{
pthread_cancel(*(_QWORD *)ptr);
free(ptr);
}
}

original_code:
void OGCancelThread( og_thread_t ot )
{
 if( !ot )
 {
  return;
 }
 pthread_cancel( *(pthread_t*)ot );
 free( ot );
}

canonical_original_code:
void OGCancelThread(void *ot)
{
 if( !ot )
 {
  return;
 }
 pthread_cancel( *(unsigned long *)ot );
 free( ot );
}


OGCreateMutex

decompiled_code:
pthread_mutex_t *OGCreateMutex()
{
pthread_mutex_t *v0; // rbx
pthread_mutexattr_t attr; // [rsp+Ch] [rbp-1Ch] BYREF

v0 = (pthread_mutex_t *)malloc(0x28uLL);
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, 1);
pthread_mutex_init(v0, &attr);
return v0;
}

canonical_decompiled_code:
pthread_mutex_t *func7()
{
pthread_mutex_t *v0; // rbx
pthread_mutexattr_t attr; // [rsp+Ch] [rbp-1Ch] BYREF

v0 = (pthread_mutex_t *)malloc(0x28uLL);
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, 1);
pthread_mutex_init(v0, &attr);
return v0;
}

original_code:
og_mutex_t OGCreateMutex()
{
 pthread_mutexattr_t mta;
 og_mutex_t r = malloc( sizeof( pthread_mutex_t ) );
 pthread_mutexattr_init(&mta);
 pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE);
 pthread_mutex_init( (pthread_mutex_t *)r, &mta );
 return r;
}

canonical_original_code:
void *OGCreateMutex()
{
 union pthread_mutexattr_t mta;
 void *r = malloc( sizeof( pthread_mutex_t ) );
 pthread_mutexattr_init(&mta);
 pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE);
 pthread_mutex_init( (union pthread_mutex_t *)r, &mta );
 return r;
}

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

variable_types:
r
void *

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

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

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


OGLockMutex

decompiled_code:
int __fastcall OGLockMutex(pthread_mutex_t *a1)
{
int result; // eax

if ( a1 )
result = pthread_mutex_lock(a1);
return result;
}

canonical_decompiled_code:
int __fastcall func8(pthread_mutex_t *a1)
{
int result; // eax

if ( a1 )
result = pthread_mutex_lock(a1);
return result;
}

original_code:
void OGLockMutex( og_mutex_t om )
{
 if( !om )
 {
  return;
 }
 pthread_mutex_lock( (pthread_mutex_t*)om );
}

canonical_original_code:
void OGLockMutex(void *om)
{
 if( !om )
 {
  return;
 }
 pthread_mutex_lock( (union pthread_mutex_t *)om );
}

variable_types:
om
void *


OGUnlockMutex

decompiled_code:
int __fastcall OGUnlockMutex(pthread_mutex_t *a1)
{
int result; // eax

if ( a1 )
result = pthread_mutex_unlock(a1);
return result;
}

canonical_decompiled_code:
int __fastcall func9(pthread_mutex_t *a1)
{
int result; // eax

if ( a1 )
result = pthread_mutex_unlock(a1);
return result;
}

original_code:
void OGUnlockMutex( og_mutex_t om )
{
 if( !om )
 {
  return;
 }
 pthread_mutex_unlock( (pthread_mutex_t*)om );
}

canonical_original_code:
void OGUnlockMutex(void *om)
{
 if( !om )
 {
  return;
 }
 pthread_mutex_unlock( (union pthread_mutex_t *)om );
}


OGDeleteMutex

decompiled_code:
void __fastcall OGDeleteMutex(void *ptr)
{
if ( ptr )
{
pthread_mutex_destroy((pthread_mutex_t *)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func10(void *ptr)
{
if ( ptr )
{
pthread_mutex_destroy((pthread_mutex_t *)ptr);
free(ptr);
}
}

original_code:
void OGDeleteMutex( og_mutex_t om )
{
 if( !om )
 {
  return;
 }
 pthread_mutex_destroy( (pthread_mutex_t*)om );
 free( om );
}

canonical_original_code:
void OGDeleteMutex(void *om)
{
 if( !om )
 {
  return;
 }
 pthread_mutex_destroy( (union pthread_mutex_t *)om );
 free( om );
}


OGCreateSema

decompiled_code:
sem_t *OGCreateSema()
{
sem_t *v0; // rbx

v0 = (sem_t *)malloc(0x20uLL);
sem_init(v0, 0, 0);
return v0;
}

canonical_decompiled_code:
sem_t *func11()
{
sem_t *v0; // rbx

v0 = (sem_t *)malloc(0x20uLL);
sem_init(v0, 0, 0);
return v0;
}

original_code:
og_sema_t OGCreateSema()
{
 sem_t * sem = malloc( sizeof( sem_t ) );
 sem_init( sem, 0, 0 );
 return (og_sema_t)sem;
}

canonical_original_code:
void *OGCreateSema()
{
 union sem_t *sem = malloc( sizeof( sem_t ) );
 sem_init( sem, 0, 0 );
 return (void *)sem;
}

variable_types: union sem_t { char __size[32]; long __align; } *


OGLockSema

decompiled_code:
int __fastcall OGLockSema(sem_t *a1)
{
return sem_wait(a1);
}

canonical_decompiled_code:
int __fastcall func12(sem_t *a1)
{
return sem_wait(a1);
}

original_code:
void OGLockSema( og_sema_t os )
{
 sem_wait( os );
}

canonical_original_code:
void OGLockSema(void *os)
{
 sem_wait( os );
}

variable_types:
os
void *


OGUnlockSema

decompiled_code:
int __fastcall OGUnlockSema(sem_t *a1)
{
return sem_post(a1);
}

canonical_decompiled_code:
int __fastcall func13(sem_t *a1)
{
return sem_post(a1);
}

original_code:
void OGUnlockSema( og_sema_t os )
{
 sem_post( os );
}

canonical_original_code:
void OGUnlockSema(void *os)
{
 sem_post( os );
}


OGDeleteSema

decompiled_code:
void __fastcall OGDeleteSema(void *ptr)
{
sem_destroy((sem_t *)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func14(void *ptr)
{
sem_destroy((sem_t *)ptr);
free(ptr);
}

original_code:
void OGDeleteSema( og_sema_t os )
{
 sem_destroy( os );
 free(os);
}

canonical_original_code:
void OGDeleteSema(void *os)
{
 sem_destroy( os );
 free(os);
}


CalculateTangentSpace

original_code:
float * CalculateTangentSpace( int Triangles, int VertexCount, int * Indices, float * verts, float * normals, float * texs )
{
 int i;
 float * tans = 0;
 if( normals && texs && verts )
 {
  tans = malloc( VertexCount * sizeof( float ) * 4 );
  memset( tans, 0, VertexCount * sizeof( float ) * 4 );
  float * tan1 = malloc( VertexCount * sizeof( float ) * 3 );
  float * tan2 = malloc( VertexCount * sizeof( float ) * 3 );
  memset( tan1, 0, VertexCount * sizeof( float ) * 3 );
  memset( tan2, 0, VertexCount * sizeof( float ) * 3 );
  for( i = 0; i < Triangles; i++ )
  {
   int v1 = Indices[i*3+0];
   int v2 = Indices[i*3+1];
   int v3 = Indices[i*3+2];
   float * t1 = &texs[v1*3];
   float * t2 = &texs[v2*3];
   float * t3 = &texs[v3*3];
   float * p1 = &verts[v1*3];
   float * p2 = &verts[v2*3];
   float * p3 = &verts[v3*3];
   float vec1[3];
   float vec2[3];
   float tex1[3];
   float tex2[3];
   sub3d( vec1, p2, p1 );
   sub3d( vec2, p3, p1 );
   sub3d( tex1, t2, t1 );
   sub3d( tex2, t3, t1 );
         float r = 1.0f / ( tex1[0] * tex2[1] - tex2[0] * tex1[1] );
   float sdir[3] = {
    (tex2[1] * vec1[0] - tex1[1] * vec2[0]) * r,
    (tex2[1] * vec1[1] - tex1[1] * vec2[1]) * r,
                (tex2[1] * vec1[2] - tex1[1] * vec2[2]) * r };
   float tdir[3] = {
    (tex1[0] * vec2[0] - tex2[0] * vec1[0]) * r,
    (tex1[0] * vec2[1] - tex2[0] * vec1[1]) * r,
                (tex1[0] * vec2[2] - tex2[0] * vec1[2]) * r };
   add3d( &tan1[v1*3], &tan1[v1*3], sdir );
   add3d( &tan1[v2*3], &tan1[v2*3], sdir );
   add3d( &tan1[v3*3], &tan1[v3*3], sdir );
   add3d( &tan2[v1*3], &tan2[v1*3], sdir );
   add3d( &tan2[v2*3], &tan2[v2*3], sdir );
   add3d( &tan2[v3*3], &tan2[v3*3], sdir );
  }
  for (i = 0; i < VertexCount; i++)
  {
      const float * n = &normals[i*3];
      const float * t = &tan1[i*3];
   float tmp[3];
   float tdn[3];
   float thisdot = dot3d( n, t );
   scale3d( tdn, n, thisdot );
   sub3d( tmp, t, tdn );
   normalize3d( &tans[i*4], tmp );
   cross3d( tmp, n, t );
   tans[i*4+3] = (dot3d( tmp, &tan2[i*3] ) < 0)?-1:1;
  }
 }
 return tans;
}

canonical_original_code:
float *CalculateTangentSpace(int Triangles, int VertexCount, int *Indices, float *verts, float *normals, float *texs)
{
 int i;
 float *tans = 0;
 if( normals && texs && verts )
 {
  tans = malloc( VertexCount * sizeof( float ) * 4 );
  memset( tans, 0, VertexCount * sizeof( float ) * 4 );
  float *tan1 = malloc( VertexCount * sizeof( float ) * 3 );
  float *tan2 = malloc( VertexCount * sizeof( float ) * 3 );
  memset( tan1, 0, VertexCount * sizeof( float ) * 3 );
  memset( tan2, 0, VertexCount * sizeof( float ) * 3 );
  for( i = 0; i < Triangles; i++ )
  {
   int v1 = Indices[i*3+0];
   int v2 = Indices[i*3+1];
   int v3 = Indices[i*3+2];
   float *t1 = &texs[v1*3];
   float *t2 = &texs[v2*3];
   float *t3 = &texs[v3*3];
   float *p1 = &verts[v1*3];
   float *p2 = &verts[v2*3];
   float *p3 = &verts[v3*3];
   float vec1[3];
   float vec2[3];
   float tex1[3];
   float tex2[3];
   sub3d( vec1, p2, p1 );
   sub3d( vec2, p3, p1 );
   sub3d( tex1, t2, t1 );
   sub3d( tex2, t3, t1 );
         float r = 1.0f / ( tex1[0] * tex2[1] - tex2[0] * tex1[1] );
   float sdir[3] = {
    (tex2[1] * vec1[0] - tex1[1] * vec2[0]) * r,
    (tex2[1] * vec1[1] - tex1[1] * vec2[1]) * r,
                (tex2[1] * vec1[2] - tex1[1] * vec2[2]) * r };
   float tdir[3] = {
    (tex1[0] * vec2[0] - tex2[0] * vec1[0]) * r,
    (tex1[0] * vec2[1] - tex2[0] * vec1[1]) * r,
                (tex1[0] * vec2[2] - tex2[0] * vec1[2]) * r };
   add3d( &tan1[v1*3], &tan1[v1*3], sdir );
   add3d( &tan1[v2*3], &tan1[v2*3], sdir );
   add3d( &tan1[v3*3], &tan1[v3*3], sdir );
   add3d( &tan2[v1*3], &tan2[v1*3], sdir );
   add3d( &tan2[v2*3], &tan2[v2*3], sdir );
   add3d( &tan2[v3*3], &tan2[v3*3], sdir );
  }
  for (i = 0; i < VertexCount; i++)
  {
      float *n = &normals[i*3];
      float *t = &tan1[i*3];
   float tmp[3];
   float tdn[3];
   float thisdot = dot3d( n, t );
   scale3d( tdn, n, thisdot );
   sub3d( tmp, t, tdn );
   normalize3d( &tans[i*4], tmp );
   cross3d( tmp, n, t );
   tans[i*4+3] = (dot3d( tmp, &tan2[i*3] ) < 0)?-1:1;
  }
 }
 return tans;
}

variable_types: Triangles int

variable_types: VertexCount int

variable_types:
Indices
int *

variable_types:
verts
float *

variable_types:
normals
float *

variable_types:
texs
float *

variable_types:
tans
float *

variable_types: r float

variable_types:
sdir
float [3]

variable_types:
tdir
float [3]

variable_types:
n
float *

variable_types:
t
float *

variable_types:
tmp
float [3]

variable_types:
tdn
float [3]

variable_types: thisdot float

return_type: float *


repo: cobbpg/pindurti

original_code:
void lcd_command(BYTE cmd) {
 if (emu->stop_cnt < lcd->next_w) return;
 if ((cmd & 0xfe) == 0x00) {
  lcd->w_len = cmd & 1;
 } else
 if ((cmd & 0xfe) == 0x02) {
  lcd->on = cmd & 1;
 } else
 if ((cmd & 0xfc) == 0x04) {
  lcd->up_dn = cmd & 1;
  lcd->cnt_sel = (cmd >> 1) & 1;
 } else
 if ((cmd & 0xf8) == 0x10) {
  lcd->amp = cmd & 3;
 } else
 if ((cmd & 0xf8) == 0x08) {
  lcd->amp2 = cmd & 3;
 } else
 if ((cmd & 0xf8) == 0x18) {
  lcd->test = (cmd >> 2) & 1;
  lcd->ctr = 0x3f;
 } else
 if ((cmd & 0xe0) == 0x20) {
  lcd->y = cmd & 0x1f;
  lcd->dummy = 1;
 } else
 if ((cmd & 0xc0) == 0x40) {
  lcd->z = cmd & 0x3f;
 } else
 if ((cmd & 0xc0) == 0x80) {
  lcd->x = cmd & 0x3f;
  lcd->dummy = 1;
 } else
 if ((cmd & 0xc0) == 0xc0) {
  lcd->ctr = cmd & 0x3f;
 }
 lcd->swht = lcd->cwht + lcd->cmul * (lcd->ctr - 32) - lcd->amul * (lcd->amp + lcd->amp2);
 lcd->sblk = lcd->cblk + lcd->cmul * (lcd->ctr - 32) - lcd->amul * (lcd->amp + lcd->amp2);
 if (lcd->tjit > 1)
  lcd->next_w = emu->stop_cnt + lcd->tmin + rand() % lcd->tjit;
 else
  lcd->next_w = emu->stop_cnt + lcd->tmin;
}

canonical_original_code:
void lcd_command(unsigned char cmd) {
 if (emu->stop_cnt < lcd->next_w) return;
 if ((cmd & 0xfe) == 0x00) {
  lcd->w_len = cmd & 1;
 } else
 if ((cmd & 0xfe) == 0x02) {
  lcd->on = cmd & 1;
 } else
 if ((cmd & 0xfc) == 0x04) {
  lcd->up_dn = cmd & 1;
  lcd->cnt_sel = (cmd >> 1) & 1;
 } else
 if ((cmd & 0xf8) == 0x10) {
  lcd->amp = cmd & 3;
 } else
 if ((cmd & 0xf8) == 0x08) {
  lcd->amp2 = cmd & 3;
 } else
 if ((cmd & 0xf8) == 0x18) {
  lcd->test = (cmd >> 2) & 1;
  lcd->ctr = 0x3f;
 } else
 if ((cmd & 0xe0) == 0x20) {
  lcd->y = cmd & 0x1f;
  lcd->dummy = 1;
 } else
 if ((cmd & 0xc0) == 0x40) {
  lcd->z = cmd & 0x3f;
 } else
 if ((cmd & 0xc0) == 0x80) {
  lcd->x = cmd & 0x3f;
  lcd->dummy = 1;
 } else
 if ((cmd & 0xc0) == 0xc0) {
  lcd->ctr = cmd & 0x3f;
 }
 lcd->swht = lcd->cwht + lcd->cmul * (lcd->ctr - 32) - lcd->amul * (lcd->amp + lcd->amp2);
 lcd->sblk = lcd->cblk + lcd->cmul * (lcd->ctr - 32) - lcd->amul * (lcd->amp + lcd->amp2);
 if (lcd->tjit > 1)
  lcd->next_w = emu->stop_cnt + lcd->tmin + rand() % lcd->tjit;
 else
  lcd->next_w = emu->stop_cnt + lcd->tmin;
}

variable_types:
cmd
unsigned char


original_code:
BYTE lcd_status() {
 return ((emu->stop_cnt < lcd->next_w) << 7) | (lcd->w_len << 6) |
  (lcd->on << 5) | (lcd->cnt_sel << 1) | (lcd->up_dn);
}

canonical_original_code:
unsigned char lcd_status() {
 return ((emu->stop_cnt < lcd->next_w) << 7) | (lcd->w_len << 6) |
  (lcd->on << 5) | (lcd->cnt_sel << 1) | (lcd->up_dn);
}

return_type: unsigned char


original_code:
void lcd_write(BYTE data) {
 if (emu->stop_cnt < lcd->next_w) return;
 int wl = (lcd->w_len) ? 8 : 6;
 int cn = (lcd->w_len) ? 15 : 20;
 int adr = lcd->x * 120 + lcd->y * wl;
 int i;
 for (i = 0; i < wl; i++) {
  if (!lcd->dat[adr]) lcd->wcnt[adr] += emu->stop_cnt - lcd->lcnt[adr];
  lcd->lcnt[adr] = emu->stop_cnt;
  lcd->dat[adr] = (data >> (wl - i - 1)) & 1;
  adr++;
 }
 switch ((lcd->cnt_sel << 1) | lcd->up_dn) {
  case 0: lcd->x = (BYTE)(lcd->x - 1) % 64; break;
  case 1: lcd->x = (BYTE)(lcd->x + 1) % 64; break;
  case 2: lcd->y = (BYTE)(lcd->y - 1 + cn) % cn; break;
  case 3: lcd->y = (BYTE)(lcd->y + 1) % cn; break;
 }
 if (lcd->tjit > 1)
  lcd->next_w = emu->stop_cnt + lcd->tmin + rand() % lcd->tjit;
 else
  lcd->next_w = emu->stop_cnt + lcd->tmin;
}

canonical_original_code:
void lcd_write(unsigned char data) {
 if (emu->stop_cnt < lcd->next_w) return;
 int wl = (lcd->w_len) ? 8 : 6;
 int cn = (lcd->w_len) ? 15 : 20;
 int adr = lcd->x * 120 + lcd->y * wl;
 int i;
 for (i = 0; i < wl; i++) {
  if (!lcd->dat[adr]) lcd->wcnt[adr] += emu->stop_cnt - lcd->lcnt[adr];
  lcd->lcnt[adr] = emu->stop_cnt;
  lcd->dat[adr] = (data >> (wl - i - 1)) & 1;
  adr++;
 }
 switch ((lcd->cnt_sel << 1) | lcd->up_dn) {
  case 0: lcd->x = (BYTE)(lcd->x - 1) % 64; break;
  case 1: lcd->x = (BYTE)(lcd->x + 1) % 64; break;
  case 2: lcd->y = (BYTE)(lcd->y - 1 + cn) % cn; break;
  case 3: lcd->y = (BYTE)(lcd->y + 1) % cn; break;
 }
 if (lcd->tjit > 1)
  lcd->next_w = emu->stop_cnt + lcd->tmin + rand() % lcd->tjit;
 else
  lcd->next_w = emu->stop_cnt + lcd->tmin;
}

variable_types:
data
unsigned char

variable_types: wl int

variable_types: cn int

variable_types: adr int

variable_types: i int


original_code:
BYTE lcd_read() {
 if (lcd->dummy) { lcd->dummy = 0; return 0; }
 int wl = (lcd->w_len) ? 8 : 6;
 int cn = (lcd->w_len) ? 15 : 20;
 int adr = lcd->x * 120 + lcd->y * wl;
 int i, v;
 v = 0;
 for (i = 0; i < wl; i++) v |= lcd->dat[adr + i] << (wl - i - 1);
 switch ((lcd->cnt_sel << 1) | lcd->up_dn) {
  case 0: lcd->x = (BYTE)(lcd->x - 1) % 64; break;
  case 1: lcd->x = (BYTE)(lcd->x + 1) % 64; break;
  case 2: lcd->y = (BYTE)(lcd->y - 1 + cn) % cn; break;
  case 3: lcd->y = (BYTE)(lcd->y + 1) % cn; break;
 }
 return v;
}

canonical_original_code:
unsigned char lcd_read() {
 if (lcd->dummy) { lcd->dummy = 0; return 0; }
 int wl = (lcd->w_len) ? 8 : 6;
 int cn = (lcd->w_len) ? 15 : 20;
 int adr = lcd->x * 120 + lcd->y * wl;
 int i; int v;
 v = 0;
 for (i = 0; i < wl; i++) v |= lcd->dat[adr + i] << (wl - i - 1);
 switch ((lcd->cnt_sel << 1) | lcd->up_dn) {
  case 0: lcd->x = (BYTE)(lcd->x - 1) % 64; break;
  case 1: lcd->x = (BYTE)(lcd->x + 1) % 64; break;
  case 2: lcd->y = (BYTE)(lcd->y - 1 + cn) % cn; break;
  case 3: lcd->y = (BYTE)(lcd->y + 1) % cn; break;
 }
 return v;
}

variable_types: v int


original_code:
void lcd_reset() {
 int i;
 lcd->x = lcd->y = lcd->z = lcd->on = lcd->test = lcd->amp = lcd->amp2 = lcd->next_w = 0;
 lcd->up_dn = lcd->cnt_sel = lcd->w_len = lcd->dummy = 1;
 for (i = 0; i < 120 * 64; i++) {
  lcd->dat[i] = lcd->wcnt[i] = lcd->lcnt[i] = 0;
  lcd->scr[i] = 0;
 }
 lcd->ctr = 18;
 lcd->tmin = 25;
 lcd->tjit = 22;
 lcd->sinc = 0.15 * 65535;
 lcd->sdec = 0.07 * 65535;
 lcd->cwht = 0.05 * 65535;
 lcd->cblk = 1.00 * 65535;
 lcd->cmul = 0.05 * 65535;
 lcd->amul = 0.15 * 65535;
 lcd->swht = lcd->cwht + lcd->cmul * (lcd->ctr - 32) - lcd->amul * (lcd->amp + lcd->amp2);
 lcd->sblk = lcd->cblk + lcd->cmul * (lcd->ctr - 32) - lcd->amul * (lcd->amp + lcd->amp2);
}


original_code:
void lcd_update() {
 int i, adr;
 int tmp;
 for (i = 0; i < 120 * 64; i++) {
  adr = (i + lcd->z * 120) % (120 * 64);
  if (!lcd->dat[adr]) lcd->wcnt[adr] += emu->stop_cnt - lcd->lcnt[adr];
  tmp = (lcd->wcnt[adr] << 8) / emu->stop_period;
  tmp = (tmp * lcd->swht + (256 - tmp) * lcd->sblk) >> 8;
  if (lcd->scr[i] > tmp) {
   lcd->scr[i] -= lcd->sdec;
   if (lcd->scr[i] < tmp) lcd->scr[i] = tmp;
  } else
  if (lcd->scr[i] < tmp) {
   lcd->scr[i] += lcd->sinc;
   if (lcd->scr[i] > tmp) lcd->scr[i] = tmp;
  }
  if (lcd->scr[i] < 0) lcd->scr[i] = 0;
  if (lcd->scr[i] > 65535) lcd->scr[i] = 65535;
  lcd->wcnt[adr] = lcd->lcnt[adr] = 0;
 }
 if (lcd->next_w > 0) lcd->next_w -= emu->stop_period;
}

canonical_original_code:
void lcd_update() {
 int i; int adr;
 int tmp;
 for (i = 0; i < 120 * 64; i++) {
  adr = (i + lcd->z * 120) % (120 * 64);
  if (!lcd->dat[adr]) lcd->wcnt[adr] += emu->stop_cnt - lcd->lcnt[adr];
  tmp = (lcd->wcnt[adr] << 8) / emu->stop_period;
  tmp = (tmp * lcd->swht + (256 - tmp) * lcd->sblk) >> 8;
  if (lcd->scr[i] > tmp) {
   lcd->scr[i] -= lcd->sdec;
   if (lcd->scr[i] < tmp) lcd->scr[i] = tmp;
  } else
  if (lcd->scr[i] < tmp) {
   lcd->scr[i] += lcd->sinc;
   if (lcd->scr[i] > tmp) lcd->scr[i] = tmp;
  }
  if (lcd->scr[i] < 0) lcd->scr[i] = 0;
  if (lcd->scr[i] > 65535) lcd->scr[i] = 65535;
  lcd->wcnt[adr] = lcd->lcnt[adr] = 0;
 }
 if (lcd->next_w > 0) lcd->next_w -= emu->stop_period;
}

variable_types: tmp int


decompiled_code:
void *__fastcall trap_allocate_next_begin(int a1)
{
void *v2; // rdi
int v3; // ebp
void *result; // rax

v2 = (void *)debug_log[SLODWORD(debug_log[75])];
if ( v2 )
free(v2);
v3 = debug_log[75];
result = malloc(a1);
debug_log[v3] = (__int64)result;
return result;
}

canonical_decompiled_code:
void *__fastcall func0(int a1)
{
void *v2; // rdi
int v3; // ebp
void *result; // rax

v2 = (void *)debug_log[SLODWORD(debug_log[75])];
if ( v2 )
free(v2);
v3 = debug_log[75];
result = malloc(a1);
debug_log[v3] = (__int64)result;
return result;
}

original_code:
void trap_allocate_next_begin(int l) {
 if (debug_log.list[debug_log.next] != ((void *)0)) free(debug_log.list[debug_log.next]);
 debug_log.list[debug_log.next] = malloc(l);
}

variable_types: l int


decompiled_code:
__int64 trap_allocate_next_end()
{
int v0; // ebx
__int64 result; // rax

v0 = debug_log[75];
*((_DWORD *)&debug_log[50] + v0) = strlen((const char *)debug_log[v0]);
result = (unsigned int)(50 * ((v0 + 1) / 50));
LODWORD(debug_log[75]) = (v0 + 1) % 50;
return result;
}

canonical_decompiled_code:
__int64 func1()
{
int v0; // ebx
__int64 result; // rax

v0 = debug_log[75];
*((_DWORD *)&debug_log[50] + v0) = strlen((const char *)debug_log[v0]);
result = (unsigned int)(50 * ((v0 + 1) / 50));
LODWORD(debug_log[75]) = (v0 + 1) % 50;
return result;
}

original_code:
void trap_allocate_next_end() {
 debug_log.llen[debug_log.next] = strlen(debug_log.list[debug_log.next]);
 debug_log.next = (debug_log.next + 1) % 50;
}


decompiled_code:
__int64 trap_dump_register_contents()
{
trap_allocate_next_begin(134);
sprintf(
(char *)debug_log[SLODWORD(debug_log[75])],
"pc=%04x sp=%04x af=%04x bc=%04x de=%04x hl=%04x\r\n"
"ix=%04x iy=%04x af'=%04x bc'=%04x de'=%04x hl'=%04x\r\n"
"r=%02x i=%02x im=%d halt=%d\r\n",
*(unsigned __int16 *)(z80 + 14LL),
*(unsigned __int16 *)(z80 + 12LL),
(unsigned __int16)*z80,
*(unsigned __int16 *)(z80 + 2LL),
*(unsigned __int16 *)(z80 + 4LL),
*(unsigned __int16 *)(z80 + 6LL),
*(unsigned __int16 *)(z80 + 8LL),
*(unsigned __int16 *)(z80 + 10LL),
*(unsigned __int16 *)(z80 + 16LL),
*(unsigned __int16 *)(z80 + 18LL),
*(unsigned __int16 *)(z80 + 20LL),
*(unsigned __int16 *)(z80 + 22LL),
*(unsigned __int8 *)(z80 + 26LL),
*(unsigned __int8 *)(z80 + 27LL),
*(unsigned __int8 *)(z80 + 28LL),
*(unsigned __int8 *)(z80 + 29LL));
return trap_allocate_next_end();
}

original_code:
void trap_dump_register_contents() {
 static const char *dformat = "pc=%04x sp=%04x af=%04x  bc=%04x  de=%04x  hl=%04x\r\nix=%04x iy=%04x af'=%04x bc'=%04x de'=%04x hl'=%04x\r\nr=%02x i=%02x im=%d halt=%d\r\n";
 trap_allocate_next_begin(strlen(dformat));
 sprintf(debug_log.list[debug_log.next], dformat,
  z80->pc, z80->sp.w, z80->af.w, z80->bc.w, z80->de.w, z80->hl.w, z80->ix.w, z80->iy.w, z80->afs, z80->bcs, z80->des, z80->hls,
  z80->r, z80->i, z80->im, z80->hlt);
 trap_allocate_next_end();
}

canonical_original_code:
void trap_dump_register_contents() {
 static char *dformat = "pc=%04x sp=%04x af=%04x  bc=%04x  de=%04x  hl=%04x\r\nix=%04x iy=%04x af'=%04x bc'=%04x de'=%04x hl'=%04x\r\nr=%02x i=%02x im=%d halt=%d\r\n";
 trap_allocate_next_begin(strlen(dformat));
 sprintf(debug_log.list[debug_log.next], dformat,
  z80->pc, z80->sp.w, z80->af.w, z80->bc.w, z80->de.w, z80->hl.w, z80->ix.w, z80->iy.w, z80->afs, z80->bcs, z80->des, z80->hls,
  z80->r, z80->i, z80->im, z80->hlt);
 trap_allocate_next_end();
}

variable_types:
dformat
char *


decompiled_code:
__int64 trap_printf()
{
int v0; // ebp
int i; // ebx
unsigned __int16 v2; // r12
char v3; // al
int v4; // eax
int v6; // eax
unsigned __int8 v7; // al
unsigned __int8 v8; // al
unsigned __int8 v9; // al
unsigned __int8 v10; // al
char v11[2040]; // [rsp+0h] [rbp-7F8h] BYREF

v0 = *(unsigned __int16 *)(z80 + 6LL);
for ( i = 0; i <= 1999; ++i )
{
while ( 1 )
{
v2 = v0 + 1;
v3 = z80_acc((unsigned __int16)v0);
if ( v3 != 92 )
break;
v0 += 2;
switch ( (unsigned __int8)z80_acc(v2) )
{
case '!':
if ( i > 1997 )
goto LABEL_6;
v6 = i + 1;
v11[i] = 13;
v11[i + 1] = 10;
break;
case 'A':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", (unsigned __int16)*z80);
v6 = i + 3;
break;
case 'B':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 2LL));
v6 = i + 3;
break;
case 'C':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 18LL));
v6 = i + 3;
break;
case 'D':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 4LL));
v6 = i + 3;
break;
case 'E':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 20LL));
v6 = i + 3;
break;
case 'F':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 16LL));
v6 = i + 3;
break;
case 'H':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 6LL));
v6 = i + 3;
break;
case 'L':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 22LL));
v6 = i + 3;
break;
case 'P':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 14LL));
v6 = i + 3;
break;
case 'S':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 12LL));
v6 = i + 3;
break;
case 'X':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 8LL));
v6 = i + 3;
break;
case 'Y':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 10LL));
v6 = i + 3;
break;
case '\\':
v11[i] = 92;
goto LABEL_6;
case 'a':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", (unsigned __int8)*z80);
v6 = i + 1;
break;
case 'b':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 2LL));
v6 = i + 1;
break;
case 'c':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 3LL));
v6 = i + 1;
break;
case 'd':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 4LL));
v6 = i + 1;
break;
case 'e':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 5LL));
v6 = i + 1;
break;
case 'f':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 1LL));
v6 = i + 1;
break;
case 'h':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 6LL));
v6 = i + 1;
break;
case 'i':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 27LL));
v6 = i + 1;
break;
case 'l':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 7LL));
v6 = i + 1;
break;
case 'm':
if ( i > 1997 )
goto LABEL_6;
v7 = z80_acc(*(unsigned __int16 *)(z80 + 2LL));
sprintf(&v11[i], "%02x", v7);
v6 = i + 1;
break;
case 'n':
if ( i > 1997 )
goto LABEL_6;
v8 = z80_acc(*(unsigned __int16 *)(z80 + 4LL));
sprintf(&v11[i], "%02x", v8);
v6 = i + 1;
break;
case 'o':
if ( i > 1997 )
goto LABEL_6;
v9 = z80_acc(*(unsigned __int16 *)(z80 + 8LL));
sprintf(&v11[i], "%02x", v9);
v6 = i + 1;
break;
case 'q':
if ( i > 1997 )
goto LABEL_6;
v10 = z80_acc(*(unsigned __int16 *)(z80 + 10LL));
sprintf(&v11[i], "%02x", v10);
v6 = i + 1;
break;
case 'r':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 26LL));
v6 = i + 1;
break;
case 'w':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 11LL));
v6 = i + 1;
break;
case 'x':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 9LL));
v6 = i + 1;
break;
case 'y':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 8LL));
v6 = i + 1;
break;
case 'z':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 10LL));
v6 = i + 1;
break;
default:
goto LABEL_6;
}
i = v6 + 1;
}
v11[i] = v3;
if ( !v3 )
break;
++v0;
LABEL_6:
;
}
v11[i] = 0;
v4 = strlen(v11);
trap_allocate_next_begin(v4);
strcpy((char *)debug_log[SLODWORD(debug_log[75])], v11);
return trap_allocate_next_end();
}

canonical_decompiled_code:
__int64 func3()
{
int v0; // ebp
int i; // ebx
unsigned __int16 v2; // r12
char v3; // al
int v4; // eax
int v6; // eax
unsigned __int8 v7; // al
unsigned __int8 v8; // al
unsigned __int8 v9; // al
unsigned __int8 v10; // al
char v11[2040]; // [rsp+0h] [rbp-7F8h] BYREF

v0 = *(unsigned __int16 *)(z80 + 6LL);
for ( i = 0; i <= 1999; ++i )
{
while ( 1 )
{
v2 = v0 + 1;
v3 = z80_acc((unsigned __int16)v0);
if ( v3 != 92 )
break;
v0 += 2;
switch ( (unsigned __int8)z80_acc(v2) )
{
case '!':
if ( i > 1997 )
goto LABEL_6;
v6 = i + 1;
v11[i] = 13;
v11[i + 1] = 10;
break;
case 'A':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", (unsigned __int16)*z80);
v6 = i + 3;
break;
case 'B':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 2LL));
v6 = i + 3;
break;
case 'C':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 18LL));
v6 = i + 3;
break;
case 'D':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 4LL));
v6 = i + 3;
break;
case 'E':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 20LL));
v6 = i + 3;
break;
case 'F':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 16LL));
v6 = i + 3;
break;
case 'H':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 6LL));
v6 = i + 3;
break;
case 'L':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 22LL));
v6 = i + 3;
break;
case 'P':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 14LL));
v6 = i + 3;
break;
case 'S':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 12LL));
v6 = i + 3;
break;
case 'X':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 8LL));
v6 = i + 3;
break;
case 'Y':
if ( i > 1995 )
goto LABEL_6;
sprintf(&v11[i], "%04x", *(unsigned __int16 *)(z80 + 10LL));
v6 = i + 3;
break;
case '\\':
v11[i] = 92;
goto LABEL_6;
case 'a':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", (unsigned __int8)*z80);
v6 = i + 1;
break;
case 'b':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 2LL));
v6 = i + 1;
break;
case 'c':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 3LL));
v6 = i + 1;
break;
case 'd':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 4LL));
v6 = i + 1;
break;
case 'e':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 5LL));
v6 = i + 1;
break;
case 'f':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 1LL));
v6 = i + 1;
break;
case 'h':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 6LL));
v6 = i + 1;
break;
case 'i':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 27LL));
v6 = i + 1;
break;
case 'l':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 7LL));
v6 = i + 1;
break;
case 'm':
if ( i > 1997 )
goto LABEL_6;
v7 = z80_acc(*(unsigned __int16 *)(z80 + 2LL));
sprintf(&v11[i], "%02x", v7);
v6 = i + 1;
break;
case 'n':
if ( i > 1997 )
goto LABEL_6;
v8 = z80_acc(*(unsigned __int16 *)(z80 + 4LL));
sprintf(&v11[i], "%02x", v8);
v6 = i + 1;
break;
case 'o':
if ( i > 1997 )
goto LABEL_6;
v9 = z80_acc(*(unsigned __int16 *)(z80 + 8LL));
sprintf(&v11[i], "%02x", v9);
v6 = i + 1;
break;
case 'q':
if ( i > 1997 )
goto LABEL_6;
v10 = z80_acc(*(unsigned __int16 *)(z80 + 10LL));
sprintf(&v11[i], "%02x", v10);
v6 = i + 1;
break;
case 'r':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 26LL));
v6 = i + 1;
break;
case 'w':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 11LL));
v6 = i + 1;
break;
case 'x':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 9LL));
v6 = i + 1;
break;
case 'y':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 8LL));
v6 = i + 1;
break;
case 'z':
if ( i > 1997 )
goto LABEL_6;
sprintf(&v11[i], "%02x", *(unsigned __int8 *)(z80 + 10LL));
v6 = i + 1;
break;
default:
goto LABEL_6;
}
i = v6 + 1;
}
v11[i] = v3;
if ( !v3 )
break;
++v0;
LABEL_6:
;
}
v11[i] = 0;
v4 = strlen(v11);
func0(v4);
strcpy((char *)debug_log[SLODWORD(debug_log[75])], v11);
return func1();
}

variable_types:
buf
char [-1]

variable_types:
ptr
unsigned short

variable_types:
c
unsigned char


decompiled_code:
__int64 debug_trap_init()
{
__int64 result; // rax

for ( result = 0LL; result != 50; ++result )
{
debug_log[result] = 0LL;
*((_DWORD *)&debug_log[50] + result) = 0;
}
LODWORD(debug_log[75]) = 0;
return result;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 result; // rax

for ( result = 0LL; result != 50; ++result )
{
debug_log[result] = 0LL;
*((_DWORD *)&debug_log[50] + result) = 0;
}
LODWORD(debug_log[75]) = 0;
return result;
}

original_code:
void debug_trap_init() {
 int i;
 for (i = 0; i < 50; i++) {
  debug_log.list[i] = ((void *)0);
  debug_log.llen[i] = 0;
 }
 debug_log.next = 0;
}


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

if ( !a1 )
return trap_dump_register_contents();
if ( a1 == 1 )
result = trap_printf();
return result;
}

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

if ( !a1 )
return func2();
if ( a1 == 1 )
result = func3();
return result;
}

original_code:
void execute_trap(BYTE id) {
 switch (id) {
 case 0x00: trap_dump_register_contents(); break;
 case 0x01: trap_printf(); break;
 }
}

canonical_original_code:
void execute_trap(unsigned char id) {
 switch (id) {
 case 0x00: trap_dump_register_contents(); break;
 case 0x01: trap_printf(); break;
 }
}

variable_types:
id
unsigned char


original_code:
void insert_hex(char *pos, int len) {
 memmove(pos + 5, pos + 1, 16);
 pos[0] = '$';
 pos[1] = '%';
 pos[2] = '0';
 pos[3] = '0' + len;
 pos[4] = 'x';
}

variable_types:
pos
char *

variable_types: len int


decompiled_code:
__int64 __fastcall opcode_name(unsigned __int16 a1)
{
char *v1; // rbx
unsigned __int8 v2; // r15
unsigned __int8 v3; // bl
int v4; // ebp
unsigned __int8 v5; // bl
char v6; // al
size_t v7; // rax
unsigned int v8; // er12
char v9; // al
char *v10; // rbx
int v11; // er13
unsigned __int8 v12; // bl
char v13; // al
unsigned __int8 v14; // bl
size_t v15; // rax
char *v16; // rbx
char v17; // al
char v18; // dl
char v19; // dl
char *i; // rax
__int64 j; // rbx
char *v22; // rbx
unsigned int v23; // eax
char *v24; // rcx
__int64 v25; // rdx
unsigned int v27; // edx
char v28; // r14
int v29; // [rsp+8h] [rbp-90h]
unsigned int v30; // [rsp+Ch] [rbp-8Ch]
char dest[76]; // [rsp+10h] [rbp-88h] BYREF
unsigned __int8 v32; // [rsp+5Ch] [rbp-3Ch] BYREF
unsigned __int8 v33; // [rsp+5Dh] [rbp-3Bh]
unsigned __int8 v34; // [rsp+5Eh] [rbp-3Ah]
unsigned __int8 v35; // [rsp+5Fh] [rbp-39h]
char v36; // [rsp+60h] [rbp-38h] BYREF

v1 = (char *)&v32;
do
{
*v1 = z80_acc((unsigned __int16)(a1 - (unsigned __int16)&v32 + (_WORD)v1));
++v1;
}
while ( v1 != &v36 );
v2 = v32;
if ( v32 == 0xCB )
{
v5 = v33;
v6 = v33 >> 6;
if ( v33 >> 6 == 2 )
{
strcpy(dest, "res");
}
else if ( v33 >> 6 == 3 )
{
strcpy(dest, "set");
}
else
{
if ( !v6 )
{
sprintf(dest, "%s ", &oc_shift[4 * (v33 >> 3)]);
goto LABEL_15;
}
if ( v6 == 1 )
strcpy(dest, "bit");
}
sprintf(&dest[3], " %d,", (v33 >> 3) & 7);
LABEL_15:
v7 = strlen(dest);
strcpy(&dest[v7], &oc_op8[5 * (v5 & 7)]);
v8 = -1;
v4 = 2;
goto LABEL_16;
}
if ( (v32 & 0xDF) != 0xDD )
{
strcpy(dest, &oc_base[12 * v32]);
v4 = 1;
LABEL_92:
if ( v2 == 0xED )
{
if ( (char)(v33 - 64) >= 0 )
{
strcpy(dest, &oc_ed[12 * v33 - 768]);
}
else if ( v33 )
{
if ( v33 == 1 )
strcpy(dest, "pti_printf");
else
strcpy(dest, "nop");
}
else
{
strcpy(dest, "pti_dump_registers");
}
++v4;
}
v17 = dest[0];
if ( !dest[0] )
{
if ( (v2 & 0xDF) == 0xDD )
{
v30 = -1;
v8 = -1;
goto LABEL_54;
}
qmemcpy(debug_ins, " ", 9);
goto LABEL_97;
}
v16 = dest;
v8 = -1;
do
{
switch ( v17 )
{
case 'A':
case 'W':
insert_hex((__int64)v16, 4);
v8 = *(&v32 + v4) + (*(&v32 + v4 + 1) << 8);
v4 += 2;
break;
case 'B':
case 'P':
insert_hex((__int64)v16, 2);
v8 = *(&v32 + v4++);
break;
case 'R':
insert_hex((__int64)v16, 4);
v8 = (char)*(&v32 + v4++) + a1 + 2;
break;
default:
break;
}
v17 = *++v16;
}
while ( *v16 );
goto LABEL_16;
}
v3 = v33;
if ( v33 != 0xCB )
{
strcpy(dest, &oc_base[12 * v32]);
if ( (v3 & 0xDF) == 0xDD || v3 == 0xED )
{
v4 = 1;
}
else
{
strcpy(dest, &oc_ix[12 * v3]);
v4 = 2;
}
goto LABEL_92;
}
v12 = v35;
v13 = v35 >> 6;
if ( v35 >> 6 == 2 )
{
strcpy(dest, "res");
}
else if ( v35 >> 6 == 3 )
{
strcpy(dest, "set");
}
else
{
if ( !v13 )
{
sprintf(dest, "%s ", &oc_shift[4 * (v35 >> 3)]);
goto LABEL_30;
}
if ( v13 == 1 )
strcpy(dest, "bit");
}
sprintf(&dest[3], " %d,", (v35 >> 3) & 7);
LABEL_30:
v14 = v12 & 7;
if ( v14 == 6 )
{
*(_DWORD *)&dest[strlen(dest)] = 2709544;
}
else
{
v15 = strlen(dest);
sprintf(&dest[v15], "(X)->%s", &oc_op8[5 * v14]);
}
v8 = -1;
v4 = 2;
LABEL_16:
v9 = dest[0];
v10 = &dest[3];
v11 = 2;
v30 = -1;
if ( !dest[0] )
{
if ( (v2 & 0xDF) == 0xDD )
{
v30 = -1;
goto LABEL_54;
}
qmemcpy(debug_ins, " ", 9);
goto LABEL_19;
}
do
{
if ( v9 == 88 )
{
*(__m128i *)(v10 + 1) = _mm_loadu_si128((const __m128i *)(v10 - 2));
*(v10 - 3) = 105;
*(v10 - 2) = 120;
v28 = *(&v32 + v4);
if ( v28 < 0 )
{
v29 = v11;
*(v10 - 1) = 45;
insert_hex((__int64)v10, 2);
if ( v8 == -1 )
{
v8 = -v28;
LABEL_85:
++v4;
goto LABEL_86;
}
}
else
{
v29 = v11;
*(v10 - 1) = 43;
insert_hex((__int64)v10, 2);
if ( v8 == -1 )
{
v8 = (unsigned __int8)v28;
goto LABEL_85;
}
}
if ( (v34 & 0x80u) != 0 )
{
v8 = -(char)v34;
v30 = v35;
v18 = 45;
}
else
{
v8 = v34;
v30 = v35;
v18 = 43;
}
dest[v29] = v18;
goto LABEL_85;
}
LABEL_86:
v9 = *(v10 - 2);
++v10;
++v11;
}
while ( v9 );
if ( (v2 & 0xDF) == 0xDD )
{
LABEL_54:
if ( v33 == 0xCB )
v4 = 4;
if ( v2 == 0xFD )
{
v19 = dest[0];
if ( dest[0] )
{
for ( i = &dest[1]; ; ++i )
{
if ( v19 == 105 && *i == 120 )
{
*i = 121;
v19 = 121;
}
else
{
v19 = *i;
if ( !*i )
break;
}
}
}
}
}
qmemcpy(debug_ins, " ", 9);
if ( v30 != -1 )
{
sprintf((char *)(unsigned int)((_DWORD)debug_ins + 9), dest, v8, v30);
goto LABEL_68;
}
LABEL_19:
if ( v8 != -1 )
{
sprintf((char *)(unsigned int)((_DWORD)debug_ins + 9), dest, v8);
goto LABEL_68;
}
LABEL_97:
sprintf((char *)(unsigned int)((_DWORD)debug_ins + 9), dest);
LABEL_68:
for ( j = 0LL; v4 > (int)j; ++j )
sprintf(&dest[2 * j], "%02x", *(&v32 + j));
v22 = dest;
v23 = strlen(dest);
v24 = debug_ins;
if ( v23 >= 8 )
{
v27 = 0;
do
{
*(_QWORD *)&debug_ins[v27] = *(_QWORD *)&dest[v27];
v27 += 8;
}
while ( v27 < (v23 & 0xFFFFFFF8) );
v24 = &debug_ins[v27];
v22 = &dest[v27];
}
v25 = 0LL;
if ( (v23 & 4) != 0 )
{
*(_DWORD *)v24 = *(_DWORD *)v22;
v25 = 4LL;
}
if ( (v23 & 2) != 0 )
{
*(_WORD *)&v24[v25] = *(_WORD *)&v22[v25];
v25 += 2LL;
}
if ( (v23 & 1) != 0 )
v24[v25] = v22[v25];
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int16 a1)
{
char *v1; // rbx
unsigned __int8 v2; // r15
unsigned __int8 v3; // bl
int v4; // ebp
unsigned __int8 v5; // bl
char v6; // al
size_t v7; // rax
unsigned int v8; // er12
char v9; // al
char *v10; // rbx
int v11; // er13
unsigned __int8 v12; // bl
char v13; // al
unsigned __int8 v14; // bl
size_t v15; // rax
char *v16; // rbx
char v17; // al
char v18; // dl
char v19; // dl
char *i; // rax
__int64 j; // rbx
char *v22; // rbx
unsigned int v23; // eax
char *v24; // rcx
__int64 v25; // rdx
unsigned int v27; // edx
char v28; // r14
int v29; // [rsp+8h] [rbp-90h]
unsigned int v30; // [rsp+Ch] [rbp-8Ch]
char dest[76]; // [rsp+10h] [rbp-88h] BYREF
unsigned __int8 v32; // [rsp+5Ch] [rbp-3Ch] BYREF
unsigned __int8 v33; // [rsp+5Dh] [rbp-3Bh]
unsigned __int8 v34; // [rsp+5Eh] [rbp-3Ah]
unsigned __int8 v35; // [rsp+5Fh] [rbp-39h]
char v36; // [rsp+60h] [rbp-38h] BYREF

v1 = (char *)&v32;
do
{
*v1 = z80_acc((unsigned __int16)(a1 - (unsigned __int16)&v32 + (_WORD)v1));
++v1;
}
while ( v1 != &v36 );
v2 = v32;
if ( v32 == 0xCB )
{
v5 = v33;
v6 = v33 >> 6;
if ( v33 >> 6 == 2 )
{
strcpy(dest, "res");
}
else if ( v33 >> 6 == 3 )
{
strcpy(dest, "set");
}
else
{
if ( !v6 )
{
sprintf(dest, "%s ", &oc_shift[4 * (v33 >> 3)]);
goto LABEL_15;
}
if ( v6 == 1 )
strcpy(dest, "bit");
}
sprintf(&dest[3], " %d,", (v33 >> 3) & 7);
LABEL_15:
v7 = strlen(dest);
strcpy(&dest[v7], &oc_op8[5 * (v5 & 7)]);
v8 = -1;
v4 = 2;
goto LABEL_16;
}
if ( (v32 & 0xDF) != 0xDD )
{
strcpy(dest, &oc_base[12 * v32]);
v4 = 1;
LABEL_92:
if ( v2 == 0xED )
{
if ( (char)(v33 - 64) >= 0 )
{
strcpy(dest, &oc_ed[12 * v33 - 768]);
}
else if ( v33 )
{
if ( v33 == 1 )
strcpy(dest, "pti_printf");
else
strcpy(dest, "nop");
}
else
{
strcpy(dest, "pti_dump_registers");
}
++v4;
}
v17 = dest[0];
if ( !dest[0] )
{
if ( (v2 & 0xDF) == 0xDD )
{
v30 = -1;
v8 = -1;
goto LABEL_54;
}
qmemcpy(debug_ins, " ", 9);
goto LABEL_97;
}
v16 = dest;
v8 = -1;
do
{
switch ( v17 )
{
case 'A':
case 'W':
func0((__int64)v16, 4);
v8 = *(&v32 + v4) + (*(&v32 + v4 + 1) << 8);
v4 += 2;
break;
case 'B':
case 'P':
func0((__int64)v16, 2);
v8 = *(&v32 + v4++);
break;
case 'R':
func0((__int64)v16, 4);
v8 = (char)*(&v32 + v4++) + a1 + 2;
break;
default:
break;
}
v17 = *++v16;
}
while ( *v16 );
goto LABEL_16;
}
v3 = v33;
if ( v33 != 0xCB )
{
strcpy(dest, &oc_base[12 * v32]);
if ( (v3 & 0xDF) == 0xDD || v3 == 0xED )
{
v4 = 1;
}
else
{
strcpy(dest, &oc_ix[12 * v3]);
v4 = 2;
}
goto LABEL_92;
}
v12 = v35;
v13 = v35 >> 6;
if ( v35 >> 6 == 2 )
{
strcpy(dest, "res");
}
else if ( v35 >> 6 == 3 )
{
strcpy(dest, "set");
}
else
{
if ( !v13 )
{
sprintf(dest, "%s ", &oc_shift[4 * (v35 >> 3)]);
goto LABEL_30;
}
if ( v13 == 1 )
strcpy(dest, "bit");
}
sprintf(&dest[3], " %d,", (v35 >> 3) & 7);
LABEL_30:
v14 = v12 & 7;
if ( v14 == 6 )
{
*(_DWORD *)&dest[strlen(dest)] = 2709544;
}
else
{
v15 = strlen(dest);
sprintf(&dest[v15], "(X)->%s", &oc_op8[5 * v14]);
}
v8 = -1;
v4 = 2;
LABEL_16:
v9 = dest[0];
v10 = &dest[3];
v11 = 2;
v30 = -1;
if ( !dest[0] )
{
if ( (v2 & 0xDF) == 0xDD )
{
v30 = -1;
goto LABEL_54;
}
qmemcpy(debug_ins, " ", 9);
goto LABEL_19;
}
do
{
if ( v9 == 88 )
{
*(__m128i *)(v10 + 1) = _mm_loadu_si128((const __m128i *)(v10 - 2));
*(v10 - 3) = 105;
*(v10 - 2) = 120;
v28 = *(&v32 + v4);
if ( v28 < 0 )
{
v29 = v11;
*(v10 - 1) = 45;
func0((__int64)v10, 2);
if ( v8 == -1 )
{
v8 = -v28;
LABEL_85:
++v4;
goto LABEL_86;
}
}
else
{
v29 = v11;
*(v10 - 1) = 43;
func0((__int64)v10, 2);
if ( v8 == -1 )
{
v8 = (unsigned __int8)v28;
goto LABEL_85;
}
}
if ( (v34 & 0x80u) != 0 )
{
v8 = -(char)v34;
v30 = v35;
v18 = 45;
}
else
{
v8 = v34;
v30 = v35;
v18 = 43;
}
dest[v29] = v18;
goto LABEL_85;
}
LABEL_86:
v9 = *(v10 - 2);
++v10;
++v11;
}
while ( v9 );
if ( (v2 & 0xDF) == 0xDD )
{
LABEL_54:
if ( v33 == 0xCB )
v4 = 4;
if ( v2 == 0xFD )
{
v19 = dest[0];
if ( dest[0] )
{
for ( i = &dest[1]; ; ++i )
{
if ( v19 == 105 && *i == 120 )
{
*i = 121;
v19 = 121;
}
else
{
v19 = *i;
if ( !*i )
break;
}
}
}
}
}
qmemcpy(debug_ins, " ", 9);
if ( v30 != -1 )
{
sprintf((char *)(unsigned int)((_DWORD)debug_ins + 9), dest, v8, v30);
goto LABEL_68;
}
LABEL_19:
if ( v8 != -1 )
{
sprintf((char *)(unsigned int)((_DWORD)debug_ins + 9), dest, v8);
goto LABEL_68;
}
LABEL_97:
sprintf((char *)(unsigned int)((_DWORD)debug_ins + 9), dest);
LABEL_68:
for ( j = 0LL; v4 > (int)j; ++j )
sprintf(&dest[2 * j], "%02x", *(&v32 + j));
v22 = dest;
v23 = strlen(dest);
v24 = debug_ins;
if ( v23 >= 8 )
{
v27 = 0;
do
{
*(_QWORD *)&debug_ins[v27] = *(_QWORD *)&dest[v27];
v27 += 8;
}
while ( v27 < (v23 & 0xFFFFFFF8) );
v24 = &debug_ins[v27];
v22 = &dest[v27];
}
v25 = 0LL;
if ( (v23 & 4) != 0 )
{
*(_DWORD *)v24 = *(_DWORD *)v22;
v25 = 4LL;
}
if ( (v23 & 2) != 0 )
{
*(_WORD *)&v24[v25] = *(_WORD *)&v22[v25];
v25 += 2LL;
}
if ( (v23 & 1) != 0 )
v24[v25] = v22[v25];
return (unsigned int)v4;
}

original_code:
int opcode_name(WORD adr) {
 BYTE oc[4];
 char tmp[64];
 int i, l = 1, par1 = -1, par2 = -1;
 for (i = 0; i < 4; i++) oc[i] = z80_acc(adr + i);
 if (oc[0] == 0xcb) {
  l = 2;
  switch (oc[1] >> 6) {
   case 0x00: sprintf(tmp, "%s ", oc_shift[(oc[1] >> 3) & 0x07]); break;
   case 0x01: sprintf(tmp, "bit"); break;
   case 0x02: sprintf(tmp, "res"); break;
   case 0x03: sprintf(tmp, "set"); break;
  }
  if (oc[1] >= 0x40) sprintf(tmp + 3, " %d,", (oc[1] >> 3) & 0x07);
  sprintf(tmp + strlen(tmp), "%s", oc_op8[oc[1] & 0x07]);
 } else if (((oc[0] == 0xdd) || (oc[0] == 0xfd)) && (oc[1] == 0xcb)) {
  l = 2;
  switch (oc[3] >> 6) {
   case 0x00: sprintf(tmp, "%s ", oc_shift[(oc[3] >> 3) & 0x07]); break;
   case 0x01: sprintf(tmp, "bit"); break;
   case 0x02: sprintf(tmp, "res"); break;
   case 0x03: sprintf(tmp, "set"); break;
  }
  if (oc[3] >= 0x40) sprintf(tmp + 3, " %d,", (oc[3] >> 3) & 0x07);
  if ((oc[3] & 0x07) == 0x06) sprintf(tmp + strlen(tmp), "(X)");
  else sprintf(tmp + strlen(tmp), "(X)->%s", oc_op8[oc[3] & 0x07]);
 } else {
  strcpy(tmp, oc_base[oc[0]]);
  if (((oc[0] == 0xdd) || (oc[0] == 0xfd)) && (oc[1] != 0xdd) && (oc[1] != 0xed) && (oc[1] != 0xfd)) {
   strcpy(tmp, oc_ix[oc[1]]);
   l++;
  }
  if (oc[0] == 0xed) {
   if ((oc[1] < 0x40) || (oc[1] >= 0xc0))
    switch (oc[1]) {
    case 0x00: strcpy(tmp, "pti_dump_registers"); break;
    case 0x01: strcpy(tmp, "pti_printf"); break;
    default: strcpy(tmp, "nop");
    }
   else strcpy(tmp, oc_ed[oc[1] - 0x40]);
   l++;
  }
  for (i = 0; tmp[i]; i++)
   switch (tmp[i]) {
    case 'A':
     insert_hex(tmp + i, 4);
     par1 = oc[l] + (oc[l + 1] << 8);
     l += 2;
    break;
    case 'B':
     insert_hex(tmp + i, 2);
     par1 = oc[l];
     l += 1;
    break;
    case 'P':
     insert_hex(tmp + i, 2);
     par1 = oc[l];
     l += 1;
    break;
    case 'R':
     insert_hex(tmp + i, 4);
     par1 = adr + 2 + (char)oc[l];
     l += 1;
    break;
    case 'W':
     insert_hex(tmp + i, 4);
     par1 = oc[l] + (oc[l + 1] << 8);
     l += 2;
    break;
   }
 }
 for (i = 0; tmp[i]; i++)
  if (tmp[i] == 'X') {
   memmove(tmp + i + 4, tmp + i + 1, 16);
   tmp[i] = 'i';
   tmp[i + 1] = 'x';
   tmp[i + 2] = ((char)oc[l] < 0) ? '-' : '+';
   insert_hex(tmp + i + 3, 2);
   if (par1 != -1) {
    par1 = ((char)oc[2] < 0) ? -(char)oc[2] : oc[2];
    par2 = oc[3];
    tmp[i + 2] = ((char)oc[2] < 0) ? '-' : '+';
   } else par1 = ((char)oc[l] < 0) ? -(char)oc[l] : oc[l];
   l++;
  }
 if (((oc[0] == 0xdd) || (oc[0] == 0xfd)) && (oc[1] == 0xcb)) l = 4;
 if (oc[0] == 0xfd) for (i = 0; tmp[i]; i++) if ((tmp[i] == 'i') && (tmp[i + 1] == 'x')) tmp[i + 1] = 'y';
 memset(debug_ins, ' ', 9);
 if (par2 != -1) sprintf(debug_ins + 9, tmp, par1, par2);
 else if (par1 != -1) sprintf(debug_ins + 9, tmp, par1);
 else sprintf(debug_ins + 9, tmp);
 for (i = 0; i < l; i++) sprintf(tmp + i * 2, "%02x", oc[i]);
 memcpy(debug_ins, tmp, strlen(tmp));
 return l;
}

canonical_original_code:
int opcode_name(unsigned short adr) {
 unsigned char oc[4];
 char tmp[64];
 int i; int l = 1; int par1 = -1; int par2 = -1;
 for (i = 0; i < 4; i++) oc[i] = z80_acc(adr + i);
 if (oc[0] == 0xcb) {
  l = 2;
  switch (oc[1] >> 6) {
   case 0x00: sprintf(tmp, "%s ", oc_shift[(oc[1] >> 3) & 0x07]); break;
   case 0x01: sprintf(tmp, "bit"); break;
   case 0x02: sprintf(tmp, "res"); break;
   case 0x03: sprintf(tmp, "set"); break;
  }
  if (oc[1] >= 0x40) sprintf(tmp + 3, " %d,", (oc[1] >> 3) & 0x07);
  sprintf(tmp + strlen(tmp), "%s", oc_op8[oc[1] & 0x07]);
 } else if (((oc[0] == 0xdd) || (oc[0] == 0xfd)) && (oc[1] == 0xcb)) {
  l = 2;
  switch (oc[3] >> 6) {
   case 0x00: sprintf(tmp, "%s ", oc_shift[(oc[3] >> 3) & 0x07]); break;
   case 0x01: sprintf(tmp, "bit"); break;
   case 0x02: sprintf(tmp, "res"); break;
   case 0x03: sprintf(tmp, "set"); break;
  }
  if (oc[3] >= 0x40) sprintf(tmp + 3, " %d,", (oc[3] >> 3) & 0x07);
  if ((oc[3] & 0x07) == 0x06) sprintf(tmp + strlen(tmp), "(X)");
  else sprintf(tmp + strlen(tmp), "(X)->%s", oc_op8[oc[3] & 0x07]);
 } else {
  strcpy(tmp, oc_base[oc[0]]);
  if (((oc[0] == 0xdd) || (oc[0] == 0xfd)) && (oc[1] != 0xdd) && (oc[1] != 0xed) && (oc[1] != 0xfd)) {
   strcpy(tmp, oc_ix[oc[1]]);
   l++;
  }
  if (oc[0] == 0xed) {
   if ((oc[1] < 0x40) || (oc[1] >= 0xc0))
    switch (oc[1]) {
    case 0x00: strcpy(tmp, "pti_dump_registers"); break;
    case 0x01: strcpy(tmp, "pti_printf"); break;
    default: strcpy(tmp, "nop");
    }
   else strcpy(tmp, oc_ed[oc[1] - 0x40]);
   l++;
  }
  for (i = 0; tmp[i]; i++)
   switch (tmp[i]) {
    case 'A':
     insert_hex(tmp + i, 4);
     par1 = oc[l] + (oc[l + 1] << 8);
     l += 2;
    break;
    case 'B':
     insert_hex(tmp + i, 2);
     par1 = oc[l];
     l += 1;
    break;
    case 'P':
     insert_hex(tmp + i, 2);
     par1 = oc[l];
     l += 1;
    break;
    case 'R':
     insert_hex(tmp + i, 4);
     par1 = adr + 2 + (char)oc[l];
     l += 1;
    break;
    case 'W':
     insert_hex(tmp + i, 4);
     par1 = oc[l] + (oc[l + 1] << 8);
     l += 2;
    break;
   }
 }
 for (i = 0; tmp[i]; i++)
  if (tmp[i] == 'X') {
   memmove(tmp + i + 4, tmp + i + 1, 16);
   tmp[i] = 'i';
   tmp[i + 1] = 'x';
   tmp[i + 2] = ((char)oc[l] < 0) ? '-' : '+';
   insert_hex(tmp + i + 3, 2);
   if (par1 != -1) {
    par1 = ((char)oc[2] < 0) ? -(char)oc[2] : oc[2];
    par2 = oc[3];
    tmp[i + 2] = ((char)oc[2] < 0) ? '-' : '+';
   } else par1 = ((char)oc[l] < 0) ? -(char)oc[l] : oc[l];
   l++;
  }
 if (((oc[0] == 0xdd) || (oc[0] == 0xfd)) && (oc[1] == 0xcb)) l = 4;
 if (oc[0] == 0xfd) for (i = 0; tmp[i]; i++) if ((tmp[i] == 'i') && (tmp[i + 1] == 'x')) tmp[i + 1] = 'y';
 memset(debug_ins, ' ', 9);
 if (par2 != -1) sprintf(debug_ins + 9, tmp, par1, par2);
 else if (par1 != -1) sprintf(debug_ins + 9, tmp, par1);
 else sprintf(debug_ins + 9, tmp);
 for (i = 0; i < l; i++) sprintf(tmp + i * 2, "%02x", oc[i]);
 memcpy(debug_ins, tmp, strlen(tmp));
 return l;
}

variable_types:
adr
unsigned short

variable_types:
oc
unsigned char [4]

variable_types:
tmp
char [64]

return_type: int


decompiled_code:
__int64 __fastcall prev_address(unsigned __int16 a1)
{
unsigned int i; // ebx

for ( i = a1 - 12; a1 > (int)(i + opcode_name(i)); i += opcode_name(i) )
;
return i;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int16 a1)
{
unsigned int i; // ebx

for ( i = a1 - 12; a1 > (int)(i + func1(i)); i += func1(i) )
;
return i;
}

original_code:
WORD prev_address(WORD a) {
 int i;
 for (i = a - 12; i + opcode_name(i) < a; i += opcode_name(i));
 return i;
}

canonical_original_code:
unsigned short prev_address(unsigned short a) {
 int i;
 for (i = a - 12; i + opcode_name(i) < a; i += opcode_name(i));
 return i;
}

variable_types:
a
unsigned short

return_type: unsigned short


decompiled_code:
__int64 __fastcall calculate_crc32(unsigned int *a1, int a2)
{
__int64 result; // rax
unsigned __int8 *v3; // r8
__int64 v4; // r9
int v5; // edi
int v6; // esi

if ( a2 <= 3 )
return 0xFFFFFFFFLL;
result = ~_byteswap_ulong(*a1);
if ( a2 > 4 )
{
v3 = (unsigned __int8 *)(a1 + 1);
v4 = (__int64)a1 + (unsigned int)(a2 - 5) + 5;
do
{
v5 = *v3;
v6 = 8;
do
{
result = (((unsigned __int8)v5 >> 7) | (2 * (_DWORD)result)) ^ ((int)result >> 31) & 0x4C11DB7u;
v5 *= 2;
--v6;
}
while ( v6 );
++v3;
}
while ( v3 != (unsigned __int8 *)v4 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int *a1, int a2)
{
__int64 result; // rax
unsigned __int8 *v3; // r8
__int64 v4; // r9
int v5; // edi
int v6; // esi

if ( a2 <= 3 )
return 0xFFFFFFFFLL;
result = ~_byteswap_ulong(*a1);
if ( a2 > 4 )
{
v3 = (unsigned __int8 *)(a1 + 1);
v4 = (__int64)a1 + (unsigned int)(a2 - 5) + 5;
do
{
v5 = *v3;
v6 = 8;
do
{
result = (((unsigned __int8)v5 >> 7) | (2 * (_DWORD)result)) ^ ((int)result >> 31) & 0x4C11DB7u;
v5 *= 2;
--v6;
}
while ( v6 );
++v3;
}
while ( v3 != (unsigned __int8 *)v4 );
}
return result;
}

original_code:
int calculate_crc32(unsigned char *tmp, int len) {
 unsigned char tb;
 int i, j, crc;
 if (len < 4) return -1;
 crc = ~((tmp[0] << 24) | (tmp[1] << 16) | (tmp[2] << 8) | tmp[3]);
 for (i = 4; i < len; i++) {
  tb = tmp[i];
  for (j = 0; j < 8; j++) {
   crc = ((crc << 1) | (tb >> 7)) ^ ((crc & 0x80000000) ? 0x04c11db7 : 0);
   tb <<= 1;
  }
 }
 return crc;
}

canonical_original_code:
int calculate_crc32(unsigned char *tmp, int len) {
 unsigned char tb;
 int i; int j; int crc;
 if (len < 4) return -1;
 crc = ~((tmp[0] << 24) | (tmp[1] << 16) | (tmp[2] << 8) | tmp[3]);
 for (i = 4; i < len; i++) {
  tb = tmp[i];
  for (j = 0; j < 8; j++) {
   crc = ((crc << 1) | (tb >> 7)) ^ ((crc & 0x80000000) ? 0x04c11db7 : 0);
   tb <<= 1;
  }
 }
 return crc;
}

variable_types:
tmp
unsigned char *

variable_types:
tb
unsigned char

variable_types: j int

variable_types: crc int


decompiled_code:
__int64 __fastcall flash_reset(int a1, __int64 a2)
{
__int64 result; // rax

*(_DWORD *)flash = 0;
result = flash;
*(_QWORD *)(flash + 8) = a2;
if ( !a1 )
{
*(_QWORD *)(result + 24) = &flash_83p;
*(_BYTE *)(result + 20) = 11;
*(_DWORD *)(result + 16) = 0x7FFFF;
}
return result;
}

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

*(_DWORD *)flash = 0;
result = flash;
*(_QWORD *)(flash + 8) = a2;
if ( !a1 )
{
*(_QWORD *)(result + 24) = &flash_83p;
*(_BYTE *)(result + 20) = 11;
*(_DWORD *)(result + 16) = 0x7FFFF;
}
return result;
}

original_code:
void flash_reset(int type, BYTE *mem) {
 flash->phase = 0x00;
 flash->mem = mem;
 switch (type) {
  case 0x00:
   flash->pages = flash_83p;
   flash->pnum = 11;
   flash->mask = 0x7ffff;
  break;
 }
}

canonical_original_code:
void flash_reset(int type, unsigned char *mem) {
 flash->phase = 0x00;
 flash->mem = mem;
 switch (type) {
  case 0x00:
   flash->pages = flash_83p;
   flash->pnum = 11;
   flash->mask = 0x7ffff;
  break;
 }
}

variable_types: type int

variable_types:
mem
unsigned char *


original_code:
void flash_write(int adr, BYTE val) {
 adr &= flash->mask;
 switch (flash->phase & 0x03) {
  case 0:
   if ((val == 0xaa) && ((adr & 0xfff) == 0xaaa))
    flash->phase++;
   else
    flash->phase = 0x00;
  break;
  case 1:
   if ((val == 0x55) && ((adr & 0xfff) == 0x555))
    flash->phase++;
   else
    flash->phase = 0x00;
  break;
  case 2:
   switch (val) {
    case 0x10:
     if ((flash->phase == 0x22) && ((adr & 0xfff) == 0xaaa)) {
      int i;
      for (i = 0; i < flash->pnum; i++)
       if (!flash->pages[i].flags)
        memset(flash->mem + flash->pages[i].start, 0xff, flash->pages[i].size);
     }
     flash->phase = 0x00;
    break;
    case 0x30:
     if (flash->phase == 0x22) {
      int i;
      for (i = 0; i < flash->pnum; i++)
       if (adr < (flash->pages[i].start + flash->pages[i].size)) {
        if (!flash->pages[i].flags)
         memset(flash->mem + flash->pages[i].start, 0xff, flash->pages[i].size);
        break;
       }
     }
     flash->phase = 0x00;
    break;
    case 0x80:
     flash->phase = 0x20;
    break;
    case 0x90:
     flash->phase = 0x03;
    break;
    case 0xa0:
     flash->phase = 0x13;
    break;
   }
  break;
  case 3:
   if (flash->phase == 0x13)
    flash->mem[adr] &= val;
   flash->phase = 0x00;
  break;
 }
}

canonical_original_code:
void flash_write(int adr, unsigned char val) {
 adr &= flash->mask;
 switch (flash->phase & 0x03) {
  case 0:
   if ((val == 0xaa) && ((adr & 0xfff) == 0xaaa))
    flash->phase++;
   else
    flash->phase = 0x00;
  break;
  case 1:
   if ((val == 0x55) && ((adr & 0xfff) == 0x555))
    flash->phase++;
   else
    flash->phase = 0x00;
  break;
  case 2:
   switch (val) {
    case 0x10:
     if ((flash->phase == 0x22) && ((adr & 0xfff) == 0xaaa)) {
      int i;
      for (i = 0; i < flash->pnum; i++)
       if (!flash->pages[i].flags)
        memset(flash->mem + flash->pages[i].start, 0xff, flash->pages[i].size);
     }
     flash->phase = 0x00;
    break;
    case 0x30:
     if (flash->phase == 0x22) {
      int i;
      for (i = 0; i < flash->pnum; i++)
       if (adr < (flash->pages[i].start + flash->pages[i].size)) {
        if (!flash->pages[i].flags)
         memset(flash->mem + flash->pages[i].start, 0xff, flash->pages[i].size);
        break;
       }
     }
     flash->phase = 0x00;
    break;
    case 0x80:
     flash->phase = 0x20;
    break;
    case 0x90:
     flash->phase = 0x03;
    break;
    case 0xa0:
     flash->phase = 0x13;
    break;
   }
  break;
  case 3:
   if (flash->phase == 0x13)
    flash->mem[adr] &= val;
   flash->phase = 0x00;
  break;
 }
}

variable_types: adr int

variable_types:
val
unsigned char

variable_types: i int


repo: codemonkeyricky/piracast

decompiled_code:
int __fastcall sx_thread_create(pthread_t *a1, void *(*a2)(void *), __int64 a3, int a4)
{
struct sched_param param; // [rsp+Ch] [rbp-14h] BYREF

pthread_create(a1, 0LL, a2, 0LL);
param.__sched_priority = a4;
return pthread_setschedparam(*(int *)a1, 1, &param);
}

canonical_decompiled_code:
int __fastcall func0(pthread_t *a1, void *(*a2)(void *), __int64 a3, int a4)
{
struct sched_param param; // [rsp+Ch] [rbp-14h] BYREF

pthread_create(a1, 0LL, a2, 0LL);
param.__sched_priority = a4;
return pthread_setschedparam(*(int *)a1, 1, &param);
}

original_code:
void sx_thread_create(
    void *thread_id,
    void *thread_func,
    void *thread_arg,
    unsigned int thread_priority
    )
{
    struct sched_param param;
    pthread_create(thread_id, ((void *)0), (void *) thread_func, ((void *)0));
    memset(&param, 0, sizeof(param));
    param.sched_priority = thread_priority;
    pthread_setschedparam(*((int *) thread_id), 1, &param);
}

canonical_original_code:
void sx_thread_create(void *thread_id, void *thread_func, void *thread_arg, unsigned int thread_priority)
{
    struct sched_param param;
    pthread_create(thread_id, ((void *)0), (void *) thread_func, ((void *)0));
    memset(&param, 0, sizeof(param));
    param.sched_priority = thread_priority;
    pthread_setschedparam(*((int *) thread_id), 1, &param);
}

variable_types: struct sched_param { int sched_priority; }


decompiled_code:
void sx_udp_destroy()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void sx_udp_destroy(
    void * id
    )
{
}

canonical_original_code:
void sx_udp_destroy(void *id)
{
}

variable_types:
id
void *


decompiled_code:
char *sx_queue_create()
{
char *v0; // rbx

v0 = (char *)malloc(0x60uLL);
pthread_mutex_init((pthread_mutex_t *)(v0 + 16), 0LL);
sem_init((sem_t *)(v0 + 56), 0, 0);
*(_QWORD *)v0 = 0LL;
*((_QWORD *)v0 + 1) = 0LL;
*((_DWORD *)v0 + 22) = 0;
return v0;
}

canonical_decompiled_code:
char *func4()
{
char *v0; // rbx

v0 = (char *)malloc(0x60uLL);
pthread_mutex_init((pthread_mutex_t *)(v0 + 16), 0LL);
sem_init((sem_t *)(v0 + 56), 0, 0);
*(_QWORD *)v0 = 0LL;
*((_QWORD *)v0 + 1) = 0LL;
*((_DWORD *)v0 + 22) = 0;
return v0;
}

original_code:
void * sx_queue_create(
    void
    )
{
    sQUEUE *queue = malloc(sizeof(sQUEUE));
    pthread_mutex_init(&queue->lock, ((void *)0));
    sem_init(&queue->sem, 0, 0);
    queue->head = ((void *)0);
    queue->tail = ((void *)0);
    queue->len = 0;
    return queue;
}

canonical_original_code:
void *sx_queue_create()
{
    struct sQUEUE *queue = malloc(sizeof(sQUEUE));
    pthread_mutex_init(&queue->lock, ((void *)0));
    sem_init(&queue->sem, 0, 0);
    queue->head = ((void *)0);
    queue->tail = ((void *)0);
    queue->len = 0;
    return queue;
}

variable_types: struct sQUEUE { struct sNODE { struct sNODE *next; void *data; } *head; struct sNODE *tail; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } lock; union sem_t { char __size[32]; long __align; } sem; unsigned int len; unsigned int high_water_mark; } *

user_defined_types: struct sQUEUE { struct sNODE *head; struct sNODE *tail; union pthread_mutex_t lock; union sem_t sem; unsigned int len; unsigned int high_water_mark; }


decompiled_code:
void __fastcall sx_queue_destroy(void *ptr)
{
void **v1; // rbx
void **v2; // r12

v1 = *(void ***)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v2 = v1;
v1 = (void **)*v1;
free(v2[1]);
free(v2);
}
while ( v1 );
}
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 16));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
void **v1; // rbx
void **v2; // r12

v1 = *(void ***)ptr;
if ( *(_QWORD *)ptr )
{
do
{
v2 = v1;
v1 = (void **)*v1;
free(v2[1]);
free(v2);
}
while ( v1 );
}
pthread_mutex_destroy((pthread_mutex_t *)((char *)ptr + 16));
free(ptr);
}

original_code:
void sx_queue_destroy(
    void * queue_id
    )
{
    sQUEUE *queue;
    sNODE *curr;
    sNODE *next;
    queue = queue_id;
    curr = queue->head;
    while(curr)
    {
        next = curr->next;
        free(curr->data);
        free(curr);
        curr = next;
    }
    pthread_mutex_destroy(&queue->lock);
    free(queue);
}

canonical_original_code:
void sx_queue_destroy(void *queue_id)
{
    struct sQUEUE *queue;
    struct sNODE *curr;
    struct sNODE *next;
    queue = queue_id;
    curr = queue->head;
    while(curr)
    {
        next = curr->next;
        free(curr->data);
        free(curr);
        curr = next;
    }
    pthread_mutex_destroy(&queue->lock);
    free(queue);
}

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

user_defined_types: union sem_t { char __size[32]; long __align; }


decompiled_code:
int __fastcall sx_queue_push(__int64 a1, __int64 a2)
{
_QWORD *v2; // r12

if ( !a2 )
_assert_fail("data != NULL", "./src/sx_queue.c", 0x68u, "sx_queue_push");
v2 = malloc(0x10uLL);
v2[1] = a2;
*v2 = 0LL;
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
if ( *(_QWORD *)a1 )
**(_QWORD **)(a1 + 8) = v2;
else
*(_QWORD *)a1 = v2;
*(_QWORD *)(a1 + 8) = v2;
++*(_DWORD *)(a1 + 88);
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1, __int64 a2)
{
_QWORD *v2; // r12

if ( !a2 )
_assert_fail("data != NULL", "./src/sx_queue.c", 0x68u, "sx_queue_push");
v2 = malloc(0x10uLL);
v2[1] = a2;
*v2 = 0LL;
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
if ( *(_QWORD *)a1 )
**(_QWORD **)(a1 + 8) = v2;
else
*(_QWORD *)a1 = v2;
*(_QWORD *)(a1 + 8) = v2;
++*(_DWORD *)(a1 + 88);
return pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
}

original_code:
void sx_queue_push(
    void * queue_id,
    void *data
    )
{
    sNODE *node;
    sQUEUE *queue;
    ((void) sizeof ((data != ((void *)0)) ? 1 : 0), __extension__ ({ if (data != ((void *)0)) ; else __assert_fail ("data != NULL", "./src/sx_queue.c", 104, __extension__ __PRETTY_FUNCTION__); }));
    queue = queue_id;
    node = malloc(sizeof(sNODE));
    node->data = data;
    node->next = ((void *)0);
    pthread_mutex_lock(&queue->lock);
    if(queue->head == ((void *)0))
    {
        queue->head = node;
        queue->tail = node;
        goto cleanup;
    }
    queue->tail->next = node;
    queue->tail = node;
cleanup:
    queue->len++;
    pthread_mutex_unlock(&queue->lock);
}

canonical_original_code:
void sx_queue_push(void *queue_id, void *data)
{
    struct sNODE *node;
    struct sQUEUE *queue;
    ((void) sizeof ((data != ((void *)0)) ? 1 : 0), __extension__ ({ if (data != ((void *)0)) ; else __assert_fail ("data != NULL", "./src/sx_queue.c", 104, __extension__ __PRETTY_FUNCTION__); }));
    queue = queue_id;
    node = malloc(sizeof(sNODE));
    node->data = data;
    node->next = ((void *)0);
    pthread_mutex_lock(&queue->lock);
    if(queue->head == ((void *)0))
    {
        queue->head = node;
        queue->tail = node;
        goto cleanup;
    }
    queue->tail->next = node;
    queue->tail = node;
cleanup:
    queue->len++;
    pthread_mutex_unlock(&queue->lock);
}

variable_types:
data
void *

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


decompiled_code:
__int64 __fastcall sx_queue_pull(__int64 a1)
{
pthread_mutex_t *v2; // r12
__int64 *v3; // rdi
__int64 *v4; // rax
__int64 v5; // r13
__int64 v6; // r14

v2 = (pthread_mutex_t *)(a1 + 16);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
v3 = *(__int64 **)a1;
if ( v3 )
{
v4 = *(__int64 **)(a1 + 8);
if ( v3 == v4 )
{
if ( *v3 )
_assert_fail("queue->head->next == NULL", "./src/sx_queue.c", 0xC6u, "sx_queue_pull");
if ( *v4 )
_assert_fail("queue->tail->next == NULL", "./src/sx_queue.c", 0xC7u, "sx_queue_pull");
v5 = v3[1];
free(v3);
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
}
else
{
v5 = v3[1];
v6 = *v3;
free(v3);
*(_QWORD *)a1 = v6;
}
if ( v5 )
--*(_DWORD *)(a1 + 88);
}
else
{
v5 = *(_QWORD *)(a1 + 8);
if ( v5 )
_assert_fail("queue->tail == NULL", "./src/sx_queue.c", 0xBCu, "sx_queue_pull");
}
pthread_mutex_unlock(v2);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
pthread_mutex_t *v2; // r12
__int64 *v3; // rdi
__int64 *v4; // rax
__int64 v5; // r13
__int64 v6; // r14

v2 = (pthread_mutex_t *)(a1 + 16);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
v3 = *(__int64 **)a1;
if ( v3 )
{
v4 = *(__int64 **)(a1 + 8);
if ( v3 == v4 )
{
if ( *v3 )
_assert_fail("queue->head->next == NULL", "./src/sx_queue.c", 0xC6u, "sx_queue_pull");
if ( *v4 )
_assert_fail("queue->tail->next == NULL", "./src/sx_queue.c", 0xC7u, "sx_queue_pull");
v5 = v3[1];
free(v3);
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
}
else
{
v5 = v3[1];
v6 = *v3;
free(v3);
*(_QWORD *)a1 = v6;
}
if ( v5 )
--*(_DWORD *)(a1 + 88);
}
else
{
v5 = *(_QWORD *)(a1 + 8);
if ( v5 )
_assert_fail("queue->tail == NULL", "./src/sx_queue.c", 0xBCu, "sx_queue_pull");
}
pthread_mutex_unlock(v2);
return v5;
}

original_code:
void * sx_queue_pull(
    void * queue_id
    )
{
    sQUEUE *queue;
    unsigned char *data;
    queue = queue_id;
    pthread_mutex_lock(&queue->lock);
    if(queue->head == ((void *)0))
    {
        ((void) sizeof ((queue->tail == ((void *)0)) ? 1 : 0), __extension__ ({ if (queue->tail == ((void *)0)) ; else __assert_fail ("queue->tail == NULL", "./src/sx_queue.c", 188, __extension__ __PRETTY_FUNCTION__); }));
        data = ((void *)0);
        goto cleanup;
    }
    if(queue->head == queue->tail)
    {
        ((void) sizeof ((queue->head->next == ((void *)0)) ? 1 : 0), __extension__ ({ if (queue->head->next == ((void *)0)) ; else __assert_fail ("queue->head->next == NULL", "./src/sx_queue.c", 198, __extension__ __PRETTY_FUNCTION__); }));
        ((void) sizeof ((queue->tail->next == ((void *)0)) ? 1 : 0), __extension__ ({ if (queue->tail->next == ((void *)0)) ; else __assert_fail ("queue->tail->next == NULL", "./src/sx_queue.c", 199, __extension__ __PRETTY_FUNCTION__); }));
        data = queue->head->data;
        free(queue->head);
        queue->head = ((void *)0);
        queue->tail = ((void *)0);
        goto cleanup;
    }
    data = queue->head->data;
    sNODE *next = queue->head->next;
    free(queue->head);
    queue->head = next;
cleanup:
    if(data != ((void *)0))
    {
        queue->len--;
    }
    pthread_mutex_unlock(&queue->lock);
    return data;
}

canonical_original_code:
void *sx_queue_pull(void *queue_id)
{
    struct sQUEUE *queue;
    unsigned char *data;
    queue = queue_id;
    pthread_mutex_lock(&queue->lock);
    if(queue->head == ((void *)0))
    {
        ((void) sizeof ((queue->tail == ((void *)0)) ? 1 : 0), __extension__ ({ if (queue->tail == ((void *)0)) ; else __assert_fail ("queue->tail == NULL", "./src/sx_queue.c", 188, __extension__ __PRETTY_FUNCTION__); }));
        data = ((void *)0);
        goto cleanup;
    }
    if(queue->head == queue->tail)
    {
        ((void) sizeof ((queue->head->next == ((void *)0)) ? 1 : 0), __extension__ ({ if (queue->head->next == ((void *)0)) ; else __assert_fail ("queue->head->next == NULL", "./src/sx_queue.c", 198, __extension__ __PRETTY_FUNCTION__); }));
        ((void) sizeof ((queue->tail->next == ((void *)0)) ? 1 : 0), __extension__ ({ if (queue->tail->next == ((void *)0)) ; else __assert_fail ("queue->tail->next == NULL", "./src/sx_queue.c", 199, __extension__ __PRETTY_FUNCTION__); }));
        data = queue->head->data;
        free(queue->head);
        queue->head = ((void *)0);
        queue->tail = ((void *)0);
        goto cleanup;
    }
    data = queue->head->data;
    struct sNODE *next = queue->head->next;
    free(queue->head);
    queue->head = next;
cleanup:
    if(data != ((void *)0))
    {
        queue->len--;
    }
    pthread_mutex_unlock(&queue->lock);
    return data;
}

variable_types:
data
unsigned char *


decompiled_code:
__int64 __fastcall sx_queue_len_get(__int64 a1)
{
unsigned int v1; // ebx

pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
v1 = *(_DWORD *)(a1 + 88);
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
unsigned int v1; // ebx

pthread_mutex_lock((pthread_mutex_t *)(a1 + 16));
v1 = *(_DWORD *)(a1 + 88);
pthread_mutex_unlock((pthread_mutex_t *)(a1 + 16));
return v1;
}

original_code:
unsigned int sx_queue_len_get(
    void * queue_id
    )
{
    sQUEUE *queue;
    unsigned int len;
    queue = queue_id;
    pthread_mutex_lock(&queue->lock);
    len = queue->len;
    pthread_mutex_unlock(&queue->lock);
    return len;
}

canonical_original_code:
unsigned int sx_queue_len_get(void *queue_id)
{
    struct sQUEUE *queue;
    unsigned int len;
    queue = queue_id;
    pthread_mutex_lock(&queue->lock);
    len = queue->len;
    pthread_mutex_unlock(&queue->lock);
    return len;
}

variable_types:
len
unsigned int

return_type: unsigned int


decompiled_code:
__int64 slice_get()
{
__int64 result; // rax

result = qword_4E0;
qword_4E0 = 0LL;
qword_4E8 = 0LL;
return result;
}

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

result = qword_4E0;
qword_4E0 = 0LL;
qword_4E8 = 0LL;
return result;
}

original_code:
static sSX_DESC * slice_get(
    void
    )
{
    sSX_DESC *head;
    head = f_cblk.slice_chain.head;
    f_cblk.slice_chain.head = ((void *)0);
    f_cblk.slice_chain.tail = ((void *)0);
    return head;
}

canonical_original_code:
static struct sSX_DESC *slice_get()
{
    struct sSX_DESC *head;
    head = f_cblk.slice_chain.head;
    f_cblk.slice_chain.head = ((void *)0);
    f_cblk.slice_chain.tail = ((void *)0);
    return head;
}

variable_types: struct sSX_DESC { unsigned char *data; unsigned int data_len; struct sSX_DESC *next; } *


decompiled_code:
int slice_drop()
{
sx_desc_put(qword_4E0);
qword_4E0 = 0LL;
qword_4E8 = 0LL;
return puts("slice_dropped() invoked");
}

canonical_decompiled_code:
int func4()
{
sx_desc_put(qword_4E0);
qword_4E0 = 0LL;
qword_4E8 = 0LL;
return puts("slice_dropped() invoked");
}

original_code:
static void slice_drop(
    void
    )
{
    sSX_DESC *curr;
    sSX_DESC *next;
    sx_desc_put(f_cblk.slice_chain.head);
    f_cblk.slice_chain.head = ((void *)0);
    f_cblk.slice_chain.tail = ((void *)0);
    printf("slice_dropped() invoked\n");
}

canonical_original_code:
static void slice_drop()
{
    struct sSX_DESC *curr;
    struct sSX_DESC *next;
    sx_desc_put(f_cblk.slice_chain.head);
    f_cblk.slice_chain.head = ((void *)0);
    f_cblk.slice_chain.tail = ((void *)0);
    printf("slice_dropped() invoked\n");
}

user_defined_types: struct sSX_DESC { unsigned char *data; unsigned int data_len; struct sSX_DESC *next; }


decompiled_code:
__int64 __fastcall audio_decoder_slice_dump(__int64 a1)
{
return sx_pipe_put(6LL, a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return sx_pipe_put(6LL, a1);
}

original_code:
static void audio_decoder_slice_dump(
    sSX_DESC *slice_head
    )
{
    sx_pipe_put(6, slice_head);
}

canonical_original_code:
static void audio_decoder_slice_dump(struct sSX_DESC *slice_head)
{
    sx_pipe_put(6, slice_head);
}


decompiled_code:
void __noreturn audio_decoder_thread()
{
int v0; // eax
int v1; // eax
unsigned int v2; // eax
__int64 v3; // rax
__int64 v4; // rax
__int64 *v5; // rbx
__int64 v6; // r13
int v7; // eax

while ( 1 )
{
while ( 1 )
{
v4 = sx_pipe_get(5LL);
v5 = (__int64 *)v4;
if ( v4 )
break;
usleep(0x3E8u);
}
v6 = *(_QWORD *)v4;
if ( *(_DWORD *)(v4 + 8) <= 0xCu )
break;
if ( *((_DWORD *)&f_cblk + 2) && (unsigned __int8)slice_start_find((__int64 *)v4, dword_4D0) )
{
*((_DWORD *)&f_cblk + 2) = 0;
*((_DWORD *)&f_cblk + 3) = 1;
*((_DWORD *)&f_cblk + 5) = 0;
LOWORD(v0) = ntohs(*(_WORD *)(v6 + 2));
*((_DWORD *)&f_cblk + 6) = v0 - 1;
}
if ( *((_DWORD *)&f_cblk + 3) )
{
LOWORD(v7) = ntohs(*(_WORD *)(v6 + 2));
if ( v7 == *((_DWORD *)&f_cblk + 6) + 1 )
{
LOWORD(v1) = ntohs(*(_WORD *)(v6 + 2));
*((_DWORD *)&f_cblk + 6) = v1;
v2 = *((_DWORD *)&f_cblk + 5) + pes_payload_size(v5);
*((_DWORD *)&f_cblk + 5) = v2;
if ( v2 > *((_DWORD *)&f_cblk + 4) )
_assert_fail(
"f_cblk.pes_curr_byte_count <= f_cblk.pes_len",
"./src/sx_mgmt_audio_decoder.c",
0x15Fu,
"audio_decoder_thread");
slice_pkt_add((__int64)v5);
if ( *((_DWORD *)&f_cblk + 5) == *((_DWORD *)&f_cblk + 4) )
{
*((_DWORD *)&f_cblk + 3) = 0;
*((_DWORD *)&f_cblk + 2) = 1;
v3 = slice_get();
audio_decoder_slice_dump(v3);
}
}
else
{
slice_drop();
*((_DWORD *)&f_cblk + 3) = 0;
*((_DWORD *)&f_cblk + 2) = 1;
}
}
}
_assert_fail("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_audio_decoder.c", 0x133u, "audio_decoder_thread");
}

canonical_decompiled_code:
void __noreturn func6()
{
int v0; // eax
int v1; // eax
unsigned int v2; // eax
__int64 v3; // rax
__int64 v4; // rax
__int64 *v5; // rbx
__int64 v6; // r13
int v7; // eax

while ( 1 )
{
while ( 1 )
{
v4 = sx_pipe_get(5LL);
v5 = (__int64 *)v4;
if ( v4 )
break;
usleep(0x3E8u);
}
v6 = *(_QWORD *)v4;
if ( *(_DWORD *)(v4 + 8) <= 0xCu )
break;
if ( *((_DWORD *)&f_cblk + 2) && (unsigned __int8)func3((__int64 *)v4, dword_4D0) )
{
*((_DWORD *)&f_cblk + 2) = 0;
*((_DWORD *)&f_cblk + 3) = 1;
*((_DWORD *)&f_cblk + 5) = 0;
LOWORD(v0) = ntohs(*(_WORD *)(v6 + 2));
*((_DWORD *)&f_cblk + 6) = v0 - 1;
}
if ( *((_DWORD *)&f_cblk + 3) )
{
LOWORD(v7) = ntohs(*(_WORD *)(v6 + 2));
if ( v7 == *((_DWORD *)&f_cblk + 6) + 1 )
{
LOWORD(v1) = ntohs(*(_WORD *)(v6 + 2));
*((_DWORD *)&f_cblk + 6) = v1;
v2 = *((_DWORD *)&f_cblk + 5) + func1(v5);
*((_DWORD *)&f_cblk + 5) = v2;
if ( v2 > *((_DWORD *)&f_cblk + 4) )
_assert_fail(
"f_cblk.pes_curr_byte_count <= f_cblk.pes_len",
"./src/sx_mgmt_audio_decoder.c",
0x15Fu,
"audio_decoder_thread");
func2((__int64)v5);
if ( *((_DWORD *)&f_cblk + 5) == *((_DWORD *)&f_cblk + 4) )
{
*((_DWORD *)&f_cblk + 3) = 0;
*((_DWORD *)&f_cblk + 2) = 1;
v3 = func0();
func5(v3);
}
}
else
{
func4();
*((_DWORD *)&f_cblk + 3) = 0;
*((_DWORD *)&f_cblk + 2) = 1;
}
}
}
_assert_fail("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_audio_decoder.c", 0x133u, "audio_decoder_thread");
}

original_code:
void audio_decoder_thread(
    void * arg
    )
{
    sSX_DESC *desc;
    unsigned char *curr_ptr;
    unsigned int bytes_left;
    sMPEG2_TS *ts;
    unsigned char eos;
    while(1)
    {
        do
        {
            desc = sx_pipe_get(5);
            if(desc == ((void *)0))
            {
                break;
            }
            curr_ptr = desc->data;
            bytes_left = desc->data_len;
            ((void) sizeof ((bytes_left > sizeof(sRTP_HDR)) ? 1 : 0), __extension__ ({ if (bytes_left > sizeof(sRTP_HDR)) ; else __assert_fail ("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_audio_decoder.c", 307, __extension__ __PRETTY_FUNCTION__); }));
            sRTP_HDR *rtp_hdr = (sRTP_HDR *) curr_ptr;
            if(f_cblk.look_for_new_slice)
            {
                if(slice_start_find(desc, &f_cblk.pes_len))
                {
                    f_cblk.look_for_new_slice = 0;
                    f_cblk.continue_current_slice = 1;
                    f_cblk.pes_curr_byte_count = 0;
                    f_cblk.last_seq_num = ntohs(rtp_hdr->sequence_num) - 1;
                }
            }
            if(f_cblk.continue_current_slice)
            {
                if(ntohs(rtp_hdr->sequence_num) != (f_cblk.last_seq_num + 1))
                {
                    slice_drop();
                    f_cblk.continue_current_slice = 0;
                    f_cblk.look_for_new_slice = 1;
                }
                else
                {
                    f_cblk.last_seq_num = ntohs(rtp_hdr->sequence_num);
                    unsigned int pes_size = pes_payload_size(desc);
                    f_cblk.pes_curr_byte_count += pes_size;
                    ((void) sizeof ((f_cblk.pes_curr_byte_count <= f_cblk.pes_len) ? 1 : 0), __extension__ ({ if (f_cblk.pes_curr_byte_count <= f_cblk.pes_len) ; else __assert_fail ("f_cblk.pes_curr_byte_count <= f_cblk.pes_len", "./src/sx_mgmt_audio_decoder.c", 351, __extension__ __PRETTY_FUNCTION__); }));
                    slice_pkt_add(desc);
                    if(f_cblk.pes_curr_byte_count == f_cblk.pes_len)
                    {
                        f_cblk.continue_current_slice = 0;
                        f_cblk.look_for_new_slice = 1;
                        audio_decoder_slice_dump(slice_get());
                    }
                }
            }
        } while (1);
        usleep(1000);
    }
}

canonical_original_code:
void audio_decoder_thread(void *arg)
{
    struct sSX_DESC *desc;
    unsigned char *curr_ptr;
    unsigned int bytes_left;
    struct sMPEG2_TS *ts;
    unsigned char eos;
    while(1)
    {
        do
        {
            desc = sx_pipe_get(5);
            if(desc == ((void *)0))
            {
                break;
            }
            curr_ptr = desc->data;
            bytes_left = desc->data_len;
            ((void) sizeof ((bytes_left > sizeof(sRTP_HDR)) ? 1 : 0), __extension__ ({ if (bytes_left > sizeof(sRTP_HDR)) ; else __assert_fail ("bytes_left > sizeof(sRTP_HDR)", "./src/sx_mgmt_audio_decoder.c", 307, __extension__ __PRETTY_FUNCTION__); }));
            struct sRTP_HDR *rtp_hdr = (sRTP_HDR *) curr_ptr;
            if(f_cblk.look_for_new_slice)
            {
                if(slice_start_find(desc, &f_cblk.pes_len))
                {
                    f_cblk.look_for_new_slice = 0;
                    f_cblk.continue_current_slice = 1;
                    f_cblk.pes_curr_byte_count = 0;
                    f_cblk.last_seq_num = ntohs(rtp_hdr->sequence_num) - 1;
                }
            }
            if(f_cblk.continue_current_slice)
            {
                if(ntohs(rtp_hdr->sequence_num) != (f_cblk.last_seq_num + 1))
                {
                    slice_drop();
                    f_cblk.continue_current_slice = 0;
                    f_cblk.look_for_new_slice = 1;
                }
                else
                {
                    f_cblk.last_seq_num = ntohs(rtp_hdr->sequence_num);
                    unsigned int pes_size = pes_payload_size(desc);
                    f_cblk.pes_curr_byte_count += pes_size;
                    ((void) sizeof ((f_cblk.pes_curr_byte_count <= f_cblk.pes_len) ? 1 : 0), __extension__ ({ if (f_cblk.pes_curr_byte_count <= f_cblk.pes_len) ; else __assert_fail ("f_cblk.pes_curr_byte_count <= f_cblk.pes_len", "./src/sx_mgmt_audio_decoder.c", 351, __extension__ __PRETTY_FUNCTION__); }));
                    slice_pkt_add(desc);
                    if(f_cblk.pes_curr_byte_count == f_cblk.pes_len)
                    {
                        f_cblk.continue_current_slice = 0;
                        f_cblk.look_for_new_slice = 1;
                        audio_decoder_slice_dump(slice_get());
                    }
                }
            }
        } while (1);
        usleep(1000);
    }
}

variable_types:
arg
void *

variable_types: struct sMPEG2_TS { struct sMPEG2_TS_HDR { unsigned char sync_byte; unsigned char tei_pusi_tp_pid1; unsigned char pid2; unsigned char tsc_afc_cc; } hdr; struct sMPEG2_TS_PAYLOAD { unsigned char payload[184]; } payload; } *

variable_types:
eos
unsigned char

variable_types: struct sRTP_HDR { unsigned char version_p_x_cc; unsigned char m_pt; unsigned short sequence_num; unsigned int timestamp; unsigned int ssrc_id; } *

user_defined_types: struct sMPEG2_TS { struct sMPEG2_TS_HDR hdr; struct sMPEG2_TS_PAYLOAD payload; }


decompiled_code:
__int64 sx_mgmt_audio_decoder_init()
{
return resources_init();
}

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

original_code:
void sx_mgmt_audio_decoder_init(
    void
    )
{
    resources_init();
}

canonical_original_code:
void sx_mgmt_audio_decoder_init()
{
    resources_init();
}


decompiled_code:
__int64 sx_mgmt_audio_decoder_open()
{
return decoder_thread_create();
}

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

original_code:
void sx_mgmt_audio_decoder_open(
    void
    )
{
    decoder_thread_create();
}

canonical_original_code:
void sx_mgmt_audio_decoder_open()
{
    decoder_thread_create();
}


decompiled_code:
void sx_mgmt_audio_decoder_close()
{
;
}

canonical_decompiled_code:
void func11()
{
;
}

original_code:
void sx_mgmt_audio_decoder_close(
    void
    )
{
}

canonical_original_code:
void sx_mgmt_audio_decoder_close()
{
}


decompiled_code:
__int64 pkt_rx_thread_create()
{
return sx_thread_create(&f_cblk, pkt_rx_thread, 0LL, 90LL);
}

canonical_decompiled_code:
__int64 func0()
{
return sx_thread_create(&f_cblk, pkt_rx_thread, 0LL, 90LL);
}

original_code:
static void pkt_rx_thread_create(
    void
    )
{
    sx_thread_create(&f_cblk.pkt_rx_thread, &pkt_rx_thread, ((void *)0), 90);
}

canonical_original_code:
static void pkt_rx_thread_create()
{
    sx_thread_create(&f_cblk.pkt_rx_thread, &pkt_rx_thread, ((void *)0), 90);
}


decompiled_code:
void __fastcall __noreturn pkt_rx_thread()
{
uint16_t v0; // r13
__int64 v1; // rax
_WORD *v2; // rbx
uint16_t v3; // ax
uint16_t v4; // r12
int v5[9]; // [rsp+Ch] [rbp-24h] BYREF

while ( 1 )
{
v2 = malloc(0x5C0uLL);
v5[0] = 1472;
sx_udp_recv(qword_248, v2, v5);
v3 = ntohs(v2[1]);
v4 = v3;
if ( v0 + 1 != v3 )
printf("(mgmt_data): last_seq_num = %u, curr_seq_num = %u, lost %u pkts.\n", v0, v3, v3 - ((unsigned int)v0 + 1));
v1 = sx_desc_get();
*(_QWORD *)v1 = v2;
*(_DWORD *)(v1 + 8) = v5[0];
sx_pipe_put(0LL);
++dword_250;
v0 = v4;
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1()
{
uint16_t v0; // r13
__int64 v1; // rax
_WORD *v2; // rbx
uint16_t v3; // ax
uint16_t v4; // r12
int v5[9]; // [rsp+Ch] [rbp-24h] BYREF

while ( 1 )
{
v2 = malloc(0x5C0uLL);
v5[0] = 1472;
sx_udp_recv(qword_248, v2, v5);
v3 = ntohs(v2[1]);
v4 = v3;
if ( v0 + 1 != v3 )
printf("(mgmt_data): last_seq_num = %u, curr_seq_num = %u, lost %u pkts.\n", v0, v3, v3 - ((unsigned int)v0 + 1));
v1 = sx_desc_get();
*(_QWORD *)v1 = v2;
*(_DWORD *)(v1 + 8) = v5[0];
sx_pipe_put(0LL);
++dword_250;
v0 = v4;
}
}

original_code:
static void pkt_rx_thread(
    void * arg
    )
{
    sPI_PORTAL_PKT *pkt;
    unsigned short last_seq_num;
    unsigned short curr_seq_num;
    while(1)
    {
        pkt = malloc(sizeof(sPI_PORTAL_PKT));
        unsigned int pkt_len = sizeof(sPI_PORTAL_PKT);
        sx_udp_recv(f_cblk.udp_sock, (char *) pkt, &pkt_len);
        sRTP_HDR *rtp_hdr = (sRTP_HDR *) pkt;
        curr_seq_num = ntohs(rtp_hdr->sequence_num);
        if((unsigned short) (last_seq_num + 1) != curr_seq_num)
        {
            printf("(mgmt_data): last_seq_num = %u, curr_seq_num = %u, lost %u pkts.\n",
                    last_seq_num,
                    curr_seq_num,
                    curr_seq_num - (last_seq_num + 1));
        }
        last_seq_num = curr_seq_num;
        sSX_DESC *desc = sx_desc_get();
        desc->data = (void *) pkt;
        desc->data_len = pkt_len;
        sx_pipe_put(0, desc);
        f_cblk.rx_pkt++;
    }
}

canonical_original_code:
static void pkt_rx_thread(void *arg)
{
    struct sPI_PORTAL_PKT *pkt;
    unsigned short last_seq_num;
    unsigned short curr_seq_num;
    while(1)
    {
        pkt = malloc(sizeof(sPI_PORTAL_PKT));
        unsigned int pkt_len = sizeof(sPI_PORTAL_PKT);
        sx_udp_recv(f_cblk.udp_sock, (char *) pkt, &pkt_len);
        struct sRTP_HDR *rtp_hdr = (sRTP_HDR *) pkt;
        curr_seq_num = ntohs(rtp_hdr->sequence_num);
        if((unsigned short) (last_seq_num + 1) != curr_seq_num)
        {
            printf("(mgmt_data): last_seq_num = %u, curr_seq_num = %u, lost %u pkts.\n",
                    last_seq_num,
                    curr_seq_num,
                    curr_seq_num - (last_seq_num + 1));
        }
        last_seq_num = curr_seq_num;
        struct sSX_DESC *desc = sx_desc_get();
        desc->data = (void *) pkt;
        desc->data_len = pkt_len;
        sx_pipe_put(0, desc);
        f_cblk.rx_pkt++;
    }
}

variable_types: struct sPI_PORTAL_PKT { unsigned int hdr; unsigned char payload[-1]; } *

user_defined_types: struct sRTP_HDR { unsigned char version_p_x_cc; unsigned char m_pt; unsigned short sequence_num; unsigned int timestamp; unsigned int ssrc_id; }


decompiled_code:
int sx_mgmt_data_init()
{
qword_248 = sx_udp_create(50000LL);
return puts("(mgmt_data): mgmt_data_init(): Initialized.");
}

canonical_decompiled_code:
int func2()
{
qword_248 = sx_udp_create(50000LL);
return puts("(mgmt_data): mgmt_data_init(): Initialized.");
}

original_code:
void sx_mgmt_data_init(
    void
    )
{
    f_cblk.udp_sock = sx_udp_create(50000);
    printf("(mgmt_data): mgmt_data_init(): Initialized.\n");
}

canonical_original_code:
void sx_mgmt_data_init()
{
    f_cblk.udp_sock = sx_udp_create(50000);
    printf("(mgmt_data): mgmt_data_init(): Initialized.\n");
}


decompiled_code:
int sx_mgmt_data_open()
{
pkt_rx_thread_create();
return puts("(mgmt_data_open): Opened.");
}

canonical_decompiled_code:
int func3()
{
func0();
return puts("(mgmt_data_open): Opened.");
}

original_code:
void sx_mgmt_data_open(
    void
    )
{
    pkt_rx_thread_create();
    printf("(mgmt_data_open): Opened.\n");
}

canonical_original_code:
void sx_mgmt_data_open()
{
    pkt_rx_thread_create();
    printf("(mgmt_data_open): Opened.\n");
}


decompiled_code:
void __noreturn mgmt_sys_thread()
{
while ( 1 )
usleep(0xF4240u);
}

canonical_decompiled_code:
void __noreturn func0()
{
while ( 1 )
usleep(0xF4240u);
}

original_code:
static void mgmt_sys_thread(
    void * arg
    )
{
    do
    {
        usleep(1*1000*1000);
    } while(1);
}

canonical_original_code:
static void mgmt_sys_thread(void *arg)
{
    do
    {
        usleep(1*1000*1000);
    } while(1);
}


decompiled_code:
int wait_forever()
{
return pthread_join(f_mgmt_sys_thread, 0LL);
}

canonical_decompiled_code:
int func1()
{
return pthread_join(f_mgmt_sys_thread, 0LL);
}

original_code:
static void wait_forever(
    void
    )
{
    pthread_join(f_mgmt_sys_thread, ((void *)0));
}

canonical_original_code:
static void wait_forever()
{
    pthread_join(f_mgmt_sys_thread, ((void *)0));
}


decompiled_code:
int mgmt_sys_init()
{
sx_pipe_init();
sx_mgmt_data_init();
sx_mgmt_m2ts_decoder_init();
sx_mgmt_video_decoder_init();
sx_mgmt_video_scheduler_init();
sx_mgmt_audio_decoder_init();
sx_mgmt_audio_scheduler_init();
return puts("(mgmt_sys): (mgmt_sys_init): Done.");
}

canonical_decompiled_code:
int func2()
{
sx_pipe_init();
sx_mgmt_data_init();
sx_mgmt_m2ts_decoder_init();
sx_mgmt_video_decoder_init();
sx_mgmt_video_scheduler_init();
sx_mgmt_audio_decoder_init();
sx_mgmt_audio_scheduler_init();
return puts("(mgmt_sys): (mgmt_sys_init): Done.");
}

original_code:
void mgmt_sys_init(
    void
    )
{
    resource_init();
    sx_pipe_init();
    sx_mgmt_data_init();
    sx_mgmt_m2ts_decoder_init();
    sx_mgmt_video_decoder_init();
    sx_mgmt_video_scheduler_init();
    sx_mgmt_audio_decoder_init();
    sx_mgmt_audio_scheduler_init();
    printf("(mgmt_sys): (mgmt_sys_init): Done.\n");
}

canonical_original_code:
void mgmt_sys_init()
{
    resource_init();
    sx_pipe_init();
    sx_mgmt_data_init();
    sx_mgmt_m2ts_decoder_init();
    sx_mgmt_video_decoder_init();
    sx_mgmt_video_scheduler_init();
    sx_mgmt_audio_decoder_init();
    sx_mgmt_audio_scheduler_init();
    printf("(mgmt_sys): (mgmt_sys_init): Done.\n");
}


decompiled_code:
int mgmt_sys_open()
{
sx_mgmt_data_open();
sx_mgmt_m2ts_decoder_open();
sx_mgmt_video_decoder_open();
sx_mgmt_video_scheduler_open();
sx_mgmt_audio_decoder_open();
sx_mgmt_audio_scheduler_open();
puts("(mgmt_sys): (mgmt_sys_open): Done.");
sx_thread_create(&f_mgmt_sys_thread, mgmt_sys_thread, 0LL, 50LL);
return wait_forever();
}

canonical_decompiled_code:
int func3()
{
sx_mgmt_data_open();
sx_mgmt_m2ts_decoder_open();
sx_mgmt_video_decoder_open();
sx_mgmt_video_scheduler_open();
sx_mgmt_audio_decoder_open();
sx_mgmt_audio_scheduler_open();
puts("(mgmt_sys): (mgmt_sys_open): Done.");
sx_thread_create(&f_mgmt_sys_thread, mgmt_sys_thread, 0LL, 50LL);
return func1();
}

original_code:
void mgmt_sys_open(
    void
    )
{
    sx_mgmt_data_open();
    sx_mgmt_m2ts_decoder_open();
    sx_mgmt_video_decoder_open();
    sx_mgmt_video_scheduler_open();
    sx_mgmt_audio_decoder_open();
    sx_mgmt_audio_scheduler_open();
    printf("(mgmt_sys): (mgmt_sys_open): Done.\n");
    sx_thread_create(&f_mgmt_sys_thread, &mgmt_sys_thread, ((void *)0), 50);
    wait_forever();
}

canonical_original_code:
void mgmt_sys_open()
{
    sx_mgmt_data_open();
    sx_mgmt_m2ts_decoder_open();
    sx_mgmt_video_decoder_open();
    sx_mgmt_video_scheduler_open();
    sx_mgmt_audio_decoder_open();
    sx_mgmt_audio_scheduler_open();
    printf("(mgmt_sys): (mgmt_sys_open): Done.\n");
    sx_thread_create(&f_mgmt_sys_thread, &mgmt_sys_thread, ((void *)0), 50);
    wait_forever();
}


decompiled_code:
__int64 pkt_process_thread_create()
{
return sx_thread_create(&f_cblk, pkt_process_thread, 0LL, 80LL);
}

canonical_decompiled_code:
__int64 func0()
{
return sx_thread_create(&f_cblk, pkt_process_thread, 0LL, 80LL);
}

original_code:
static void pkt_process_thread_create(
    void
    )
{
    sx_thread_create(&f_cblk.pkt_process_thread, &pkt_process_thread, ((void *)0), 80);
}

canonical_original_code:
static void pkt_process_thread_create()
{
    sx_thread_create(&f_cblk.pkt_process_thread, &pkt_process_thread, ((void *)0), 80);
}