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

nr = 1 << dword_FE4;
ng = 1 << dword_FFC;
nb = 1 << dword_FF0;
rr = 8 - dword_FE4;
rl = dword_FE0;
br = 8 - dword_FFC;
result = (unsigned int)dword_FF8;
gr = 8 - dword_FF0;
gl = dword_FEC;
dword_B14 = dword_FF8;
return result;
}

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

nr = 1 << dword_FE4;
ng = 1 << dword_FFC;
nb = 1 << dword_FF0;
rr = 8 - dword_FE4;
rl = dword_FE0;
br = 8 - dword_FFC;
result = (unsigned int)dword_FF8;
gr = 8 - dword_FF0;
gl = dword_FEC;
dword_B14 = dword_FF8;
return result;
}

original_code:
static void init_colors(void)
{
 nr = 1 << vinfo.red.length;
 ng = 1 << vinfo.blue.length;
 nb = 1 << vinfo.green.length;
 rr = 8 - vinfo.red.length;
 rl = vinfo.red.offset;
 gr = 8 - vinfo.green.length;
 gl = vinfo.green.offset;
 br = 8 - vinfo.blue.length;
 bl = vinfo.blue.offset;
}

canonical_original_code:
static void init_colors()
{
 nr = 1 << vinfo.red.length;
 ng = 1 << vinfo.blue.length;
 nb = 1 << vinfo.green.length;
 rr = 8 - vinfo.red.length;
 rl = vinfo.red.offset;
 gr = 8 - vinfo.green.length;
 gl = vinfo.green.offset;
 br = 8 - vinfo.blue.length;
 bl = vinfo.blue.offset;
}


repo: litcave/fbpad

original_code:
static void fb_cmap_save(int save)
{
 static unsigned short red[(1 << 8)], green[(1 << 8)], blue[(1 << 8)];
 struct fb_cmap cmap;
 if (finfo.visual == 2)
  return;
 cmap.start = 0;
 cmap.len = ((nr) > (((ng) > (nb) ? (ng) : (nb))) ? (nr) : (((ng) > (nb) ? (ng) : (nb))));
 cmap.red = red;
 cmap.green = green;
 cmap.blue = blue;
 cmap.transp = ((void *)0);
 ioctl(fd, save ? 0x4604 : 0x4605, &cmap);
}

canonical_original_code:
static void fb_cmap_save(int save)
{
 static unsigned short red[-1]; static unsigned short green[-1]; static unsigned short blue[-1];
 struct fb_cmap cmap;
 if (finfo.visual == 2)
  return;
 cmap.start = 0;
 cmap.len = ((nr) > (((ng) > (nb) ? (ng) : (nb))) ? (nr) : (((ng) > (nb) ? (ng) : (nb))));
 cmap.red = red;
 cmap.green = green;
 cmap.blue = blue;
 cmap.transp = ((void *)0);
 ioctl(fd, save ? 0x4604 : 0x4605, &cmap);
}

variable_types: save int

variable_types:
red
unsigned short [-1]

variable_types:
green
unsigned short [-1]

variable_types:
blue
unsigned short [-1]

variable_types: struct fb_cmap { unsigned int start; unsigned int len; unsigned short *red; unsigned short *green; unsigned short *blue; unsigned short *transp; }

return_type: void


original_code:
void fb_cmap(void)
{
 unsigned short red[(1 << 8)], green[(1 << 8)], blue[(1 << 8)];
 struct fb_cmap cmap;
 int i;
 if (finfo.visual == 2)
  return;
 for (i = 0; i < nr; i++)
  red[i] = (65535 / (nr - 1)) * i;
 for (i = 0; i < ng; i++)
  green[i] = (65535 / (ng - 1)) * i;
 for (i = 0; i < nb; i++)
  blue[i] = (65535 / (nb - 1)) * i;
 cmap.start = 0;
 cmap.len = ((nr) > (((ng) > (nb) ? (ng) : (nb))) ? (nr) : (((ng) > (nb) ? (ng) : (nb))));
 cmap.red = red;
 cmap.green = green;
 cmap.blue = blue;
 cmap.transp = ((void *)0);
 ioctl(fd, 0x4605, &cmap);
}

canonical_original_code:
void fb_cmap()
{
 unsigned short red[-1]; unsigned short green[-1]; unsigned short blue[-1];
 struct fb_cmap cmap;
 int i;
 if (finfo.visual == 2)
  return;
 for (i = 0; i < nr; i++)
  red[i] = (65535 / (nr - 1)) * i;
 for (i = 0; i < ng; i++)
  green[i] = (65535 / (ng - 1)) * i;
 for (i = 0; i < nb; i++)
  blue[i] = (65535 / (nb - 1)) * i;
 cmap.start = 0;
 cmap.len = ((nr) > (((ng) > (nb) ? (ng) : (nb))) ? (nr) : (((ng) > (nb) ? (ng) : (nb))));
 cmap.red = red;
 cmap.green = green;
 cmap.blue = blue;
 cmap.transp = ((void *)0);
 ioctl(fd, 0x4605, &cmap);
}

variable_types: i int

user_defined_types: struct fb_cmap { unsigned int start; unsigned int len; unsigned short *red; unsigned short *green; unsigned short *blue; unsigned short *transp; }


original_code:
unsigned fb_mode(void)
{
 return ((rl < gl) << 22) | ((rl < bl) << 21) | ((gl < bl) << 20) |
  (bpp << 16) | (vinfo.red.length << 8) |
  (vinfo.green.length << 4) | (vinfo.blue.length);
}

canonical_original_code:
unsigned int fb_mode()
{
 return ((rl < gl) << 22) | ((rl < bl) << 21) | ((gl < bl) << 20) |
  (bpp << 16) | (vinfo.red.length << 8) |
  (vinfo.green.length << 4) | (vinfo.blue.length);
}

return_type: unsigned int


decompiled_code:
__int64 __fastcall fb_init(const char *a1)
{
const char *v1; // rbp
char *v2; // rax
int v3; // eax
int v4; // ebp
int v5; // eax
__int64 v6; // r8
size_t v7; // rsi
int v8; // edx
int v9; // ecx
__off_t v10; // r9
__int64 v11; // rdx
__int64 v12; // rcx
char *v13; // r8
__int64 result; // rax

v1 = "/dev/fb0";
if ( a1 )
{
v1 = a1;
v2 = strchr(a1, 58);
if ( v2 )
{
*v2 = 0;
_isoc99_sscanf(v2 + 1, "%dx%d%d%d", &xres, &yres, &xoff, &yoff);
}
}
snprintf(fbdev, 0x400uLL, "%s", v1);
v3 = open(v1, 2);
fd = v3;
v4 = v3;
if ( v3 < 0
|| ioctl(v3, 0x4600uLL, &vinfo) < 0
|| ioctl(v4, 0x4602uLL, &finfo) < 0
|| (v5 = fcntl(v4, 1),
fcntl(fd, 2, v5 | 1u),
bpp = (unsigned int)(dword_FD8 + 7) >> 3,
v7 = (int)fb_len(0LL, 2LL, 3LL, 1LL, v6, 0LL),
fb = mmap(0LL, v7, v8, v9, fd, v10),
fb == (void *)-1LL) )
{
perror("fb_init()");
close(fd);
result = 1LL;
}
else
{
init_colors();
fb_cmap_save(1);
fb_cmap(1LL, v7, v11, v12, v13);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1)
{
const char *v1; // rbp
char *v2; // rax
int v3; // eax
int v4; // ebp
int v5; // eax
__int64 v6; // r8
size_t v7; // rsi
int v8; // edx
int v9; // ecx
__off_t v10; // r9
__int64 v11; // rdx
__int64 v12; // rcx
char *v13; // r8
__int64 result; // rax

v1 = "/dev/fb0";
if ( a1 )
{
v1 = a1;
v2 = strchr(a1, 58);
if ( v2 )
{
*v2 = 0;
_isoc99_sscanf(v2 + 1, "%dx%d%d%d", &xres, &yres, &xoff, &yoff);
}
}
snprintf(fbdev, 0x400uLL, "%s", v1);
v3 = open(v1, 2);
fd = v3;
v4 = v3;
if ( v3 < 0
|| ioctl(v3, 0x4600uLL, &vinfo) < 0
|| ioctl(v4, 0x4602uLL, &finfo) < 0
|| (v5 = fcntl(v4, 1),
fcntl(fd, 2, v5 | 1u),
bpp = (unsigned int)(dword_FD8 + 7) >> 3,
v7 = (int)func0(0LL, 2LL, 3LL, 1LL, v6, 0LL),
fb = mmap(0LL, v7, v8, v9, fd, v10),
fb == (void *)-1LL) )
{
perror("fb_init()");
close(fd);
result = 1LL;
}
else
{
func1();
func2(1);
func3(1LL, v7, v11, v12, v13);
result = 0LL;
}
return result;
}

original_code:
int fb_init(char *dev)
{
 char *path = dev ? dev : "/dev/fb0";
 char *geom = dev ? strchr(dev, ':') : ((void *)0);
 if (geom) {
  *geom = '\0';
  sscanf(geom + 1, "%dx%d%d%d", &xres, &yres, &xoff, &yoff);
 }
 snprintf(fbdev, sizeof(fbdev), "%s", path);
 fd = open(path, 02);
 if (fd < 0)
  goto failed;
 if (ioctl(fd, 0x4600, &vinfo) < 0)
  goto failed;
 if (ioctl(fd, 0x4602, &finfo) < 0)
  goto failed;
 fcntl(fd, 2, fcntl(fd, 1) | 1);
 bpp = (vinfo.bits_per_pixel + 7) >> 3;
 fb = mmap(((void *)0), fb_len(), 0x1 | 0x2, 0x01, fd, 0);
 if (fb == ((void *) -1))
  goto failed;
 init_colors();
 fb_cmap_save(1);
 fb_cmap();
 return 0;
failed:
 perror("fb_init()");
 close(fd);
 return 1;
}

variable_types:
dev
char *

variable_types:
path
char *

variable_types:
geom
char *

return_type: int


decompiled_code:
int __fastcall fb_free(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
__int64 v3; // rcx
__int64 v4; // r8
__int64 v5; // r9
int v6; // eax

fb_cmap_save(0);
v6 = fb_len(0LL, a2, v2, v3, v4, v5);
munmap(fb, v6);
return close(fd);
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
__int64 v3; // rcx
__int64 v4; // r8
__int64 v5; // r9
int v6; // eax

func2(0);
v6 = func0(0LL, a2, v2, v3, v4, v5);
munmap(fb, v6);
return close(fd);
}

original_code:
void fb_free(void)
{
 fb_cmap_save(0);
 munmap(fb, fb_len());
 close(fd);
}

canonical_original_code:
void fb_free()
{
 fb_cmap_save(0);
 munmap(fb, fb_len());
 close(fd);
}


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

result = (unsigned int)yres;
if ( !yres )
result = (unsigned int)dword_FC4;
return result;
}

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

result = (unsigned int)yres;
if ( !yres )
result = (unsigned int)dword_FC4;
return result;
}

original_code:
int fb_rows(void)
{
 return yres ? yres : vinfo.yres;
}

canonical_original_code:
int fb_rows()
{
 return yres ? yres : vinfo.yres;
}


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

result = (unsigned int)xres;
if ( !xres )
result = (unsigned int)vinfo;
return result;
}

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

result = (unsigned int)xres;
if ( !xres )
result = (unsigned int)vinfo;
return result;
}

original_code:
int fb_cols(void)
{
 return xres ? xres : vinfo.xres;
}

canonical_original_code:
int fb_cols()
{
 return xres ? xres : vinfo.xres;
}


decompiled_code:
char *__fastcall fb_mem(int a1)
{
return (char *)fb
+ (unsigned int)(dword_F90 * (yoff + a1 + dword_FD4))
+ (unsigned __int64)(unsigned int)(bpp * (dword_FD0 + xoff));
}

canonical_decompiled_code:
char *__fastcall func9(int a1)
{
return (char *)fb
+ (unsigned int)(dword_F90 * (yoff + a1 + dword_FD4))
+ (unsigned __int64)(unsigned int)(bpp * (dword_FD0 + xoff));
}

original_code:
void *fb_mem(int r)
{
 return fb + (r + vinfo.yoffset + yoff) * finfo.line_length + (vinfo.xoffset + xoff) * bpp;
}

variable_types: r int

return_type: void *


decompiled_code:
__int64 __fastcall fb_val(int a1, int a2, int a3)
{
return (a1 >> rr << rl) | (a3 >> br << dword_B14) | (unsigned int)(a2 >> gr << gl);
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, int a2, int a3)
{
return (a1 >> rr << rl) | (a3 >> br << dword_B14) | (unsigned int)(a2 >> gr << gl);
}

original_code:
unsigned fb_val(int r, int g, int b)
{
 return ((r >> rr) << rl) | ((g >> gr) << gl) | ((b >> br) << bl);
}

canonical_original_code:
unsigned int fb_val(int r, int g, int b)
{
 return ((r >> rr) << rl) | ((g >> gr) << gl) | ((b >> br) << bl);
}

variable_types: g int

variable_types: b int


decompiled_code:
char *fb_dev()
{
return fbdev;
}

canonical_decompiled_code:
char *func11()
{
return fbdev;
}

original_code:
char *fb_dev(void)
{
 return fbdev;
}

canonical_original_code:
char *fb_dev()
{
 return fbdev;
}

return_type: char *


getcount

repo: litcave/fbpdf

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

result = (unsigned int)count;
count = 0;
if ( !(_DWORD)result )
result = a1;
return result;
}

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

result = (unsigned int)count;
count = 0;
if ( !(_DWORD)result )
result = a1;
return result;
}

original_code:
static int getcount(int def)
{
 int result = count ? count : def;
 count = 0;
 return result;
}

variable_types: def int

variable_types: result int


printinfo

decompiled_code:
int printinfo()
{
unsigned int v0; // ebx
unsigned int v1; // eax

printf("\x1B[H");
v0 = zoom;
v1 = doc_pages(doc);
printf("FBPDF: file:%s page:%d(%d) zoom:%d%% \x1B[K\r", filename, (unsigned int)num, v1, v0);
return fflush(stdout);
}

canonical_decompiled_code:
int func1()
{
unsigned int v0; // ebx
unsigned int v1; // eax

printf("\x1B[H");
v0 = zoom;
v1 = doc_pages(doc);
printf("FBPDF: file:%s page:%d(%d) zoom:%d%% \x1B[K\r", filename, (unsigned int)num, v1, v0);
return fflush(stdout);
}

original_code:
static void printinfo(void)
{
 printf("\x1b[H");
 printf("FBPDF:     file:%s  page:%d(%d)  zoom:%d%% \x1b[K\r",
  filename, num, doc_pages(doc), zoom);
 fflush(stdout);
}

canonical_original_code:
static void printinfo()
{
 printf("\x1b[H");
 printf("FBPDF:     file:%s  page:%d(%d)  zoom:%d%% \x1b[K\r",
  filename, num, doc_pages(doc), zoom);
 fflush(stdout);
}


setmark

decompiled_code:
const unsigned __int16 *__fastcall setmark(int a1)
{
const unsigned __int16 *result; // rax

result = *_ctype_b_loc();
if ( (result[a1] & 0x400) != 0 || a1 == 39 || a1 == 96 )
{
mark[a1] = num;
result = (const unsigned __int16 *)(unsigned int)(100 * srow / zoom);
mark_row[a1] = (int)result;
}
return result;
}

canonical_decompiled_code:
const unsigned __int16 *__fastcall func2(int a1)
{
const unsigned __int16 *result; // rax

result = *_ctype_b_loc();
if ( (result[a1] & 0x400) != 0 || a1 == 39 || a1 == 96 )
{
mark[a1] = num;
result = (const unsigned __int16 *)(unsigned int)(100 * srow / zoom);
mark_row[a1] = (int)result;
}
return result;
}

original_code:
static void setmark(int c)
{
 if ((((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISalpha) || (c) == '\'' || (c) == '`')) {
  mark[c] = num;
  mark_row[c] = srow * 100 / zoom;
 }
}

canonical_original_code:
static void setmark(int c)
{
 if ((((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISalpha) || (c) == '\'' || (c) == '`')) {
  mark[c] = num;
  mark_row[c] = srow * 100 / zoom;
 }
}

variable_types: c int


decompiled_code:
int term_setup()
{
struct termios v1; // [rsp+0h] [rbp-48h] BYREF

tcgetattr(0, &termios);
*(_QWORD *)&v1.c_cc[31] = qword_1210;
*(__m128i *)&v1.c_iflag = _mm_load_si128((const __m128i *)&termios);
*(__m128i *)&v1.c_line = _mm_load_si128((const __m128i *)&xmmword_11F0);
*(__m128i *)&v1.c_cc[15] = _mm_load_si128((const __m128i *)&xmmword_1200);
v1.c_lflag &= 0xFFFFFFF5;
v1.c_ospeed = dword_1218;
tcsetattr(0, 2, &v1);
printf("\x1B[?25l");
printf("\x1B[2J");
return fflush(stdout);
}

canonical_decompiled_code:
int func3()
{
struct termios v1; // [rsp+0h] [rbp-48h] BYREF

tcgetattr(0, &termios);
*(_QWORD *)&v1.c_cc[31] = qword_1210;
*(__m128i *)&v1.c_iflag = _mm_load_si128((const __m128i *)&termios);
*(__m128i *)&v1.c_line = _mm_load_si128((const __m128i *)&xmmword_11F0);
*(__m128i *)&v1.c_cc[15] = _mm_load_si128((const __m128i *)&xmmword_1200);
v1.c_lflag &= 0xFFFFFFF5;
v1.c_ospeed = dword_1218;
tcsetattr(0, 2, &v1);
printf("\x1B[?25l");
printf("\x1B[2J");
return fflush(stdout);
}

original_code:
static void term_setup(void)
{
 struct termios newtermios;
 tcgetattr(0, &termios);
 newtermios = termios;
 newtermios.c_lflag &= ~0000002;
 newtermios.c_lflag &= ~0000010;
 tcsetattr(0, 2, &newtermios);
 printf("\x1b[?25l");
 printf("\x1b[2J");
 fflush(stdout);
}

canonical_original_code:
static void term_setup()
{
 struct termios newtermios;
 tcgetattr(0, &termios);
 newtermios = termios;
 newtermios.c_lflag &= ~0000002;
 newtermios.c_lflag &= ~0000010;
 tcsetattr(0, 2, &newtermios);
 printf("\x1b[?25l");
 printf("\x1b[2J");
 fflush(stdout);
}

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


sigcont

decompiled_code:
// attributes: thunk
void sigcont()
{
term_setup();
}

canonical_decompiled_code:
void func4()
{
func3();
}

original_code:
static void sigcont(int sig)
{
 term_setup();
}

variable_types: sig int


lmargin

decompiled_code:
__int64 lmargin()
{
int v0; // ebp
int i; // er12
int v2; // ebx
int v3; // er13

v0 = 0;
for ( i = pcols; prows > v0; ++v0 )
{
v2 = 0;
if ( i > 0 )
{
do
{
v3 = *((_DWORD *)pbuf + v2 + v0 * pcols);
if ( v3 != (unsigned int)fb_val(255LL, 255LL, 255LL) )
break;
++v2;
}
while ( v2 != i );
}
if ( i > v2 )
i = v2;
}
return (unsigned int)i;
}

canonical_decompiled_code:
__int64 func5()
{
int v0; // ebp
int i; // er12
int v2; // ebx
int v3; // er13

v0 = 0;
for ( i = pcols; prows > v0; ++v0 )
{
v2 = 0;
if ( i > 0 )
{
do
{
v3 = *((_DWORD *)pbuf + v2 + v0 * pcols);
if ( v3 != (unsigned int)fb_val(255LL, 255LL, 255LL) )
break;
++v2;
}
while ( v2 != i );
}
if ( i > v2 )
i = v2;
}
return (unsigned int)i;
}

original_code:
static int lmargin(void)
{
 int ret = pcols;
 int i, j;
 for (i = 0; i < prows; i++) {
  j = 0;
  while (j < ret && pbuf[i * pcols + j] == fb_val((255), (255), (255)))
   j++;
  if (ret > j)
   ret = j;
 }
 return ret;
}

canonical_original_code:
static int lmargin()
{
 int ret = pcols;
 int i; int j;
 for (i = 0; i < prows; i++) {
  j = 0;
  while (j < ret && pbuf[i * pcols + j] == fb_val((255), (255), (255)))
   j++;
  if (ret > j)
   ret = j;
 }
 return ret;
}

variable_types: ret int

variable_types: j int


rmargin

decompiled_code:
__int64 rmargin()
{
int v0; // ebx
int v1; // er12
int v2; // eax
int v3; // ebp
int v4; // er13

if ( prows <= 0 )
return 0LL;
v0 = 0;
v1 = 0;
do
{
v2 = pcols;
v3 = pcols - 1;
if ( pcols - 1 > v1 )
{
while ( 1 )
{
v4 = *((_DWORD *)pbuf + v3 + v0 * v2);
if ( v4 != (unsigned int)fb_val(255LL, 255LL, 255LL) )
break;
if ( --v3 == v1 )
break;
v2 = pcols;
}
}
if ( v1 < v3 )
v1 = v3;
++v0;
}
while ( prows > v0 );
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 func6()
{
int v0; // ebx
int v1; // er12
int v2; // eax
int v3; // ebp
int v4; // er13

if ( prows <= 0 )
return 0LL;
v0 = 0;
v1 = 0;
do
{
v2 = pcols;
v3 = pcols - 1;
if ( pcols - 1 > v1 )
{
while ( 1 )
{
v4 = *((_DWORD *)pbuf + v3 + v0 * v2);
if ( v4 != (unsigned int)fb_val(255LL, 255LL, 255LL) )
break;
if ( --v3 == v1 )
break;
v2 = pcols;
}
}
if ( v1 < v3 )
v1 = v3;
++v0;
}
while ( prows > v0 );
return (unsigned int)v1;
}

original_code:
static int rmargin(void)
{
 int ret = 0;
 int i, j;
 for (i = 0; i < prows; i++) {
  j = pcols - 1;
  while (j > ret && pbuf[i * pcols + j] == fb_val((255), (255), (255)))
   j--;
  if (ret < j)
   ret = j;
 }
 return ret;
}

canonical_original_code:
static int rmargin()
{
 int ret = 0;
 int i; int j;
 for (i = 0; i < prows; i++) {
  j = pcols - 1;
  while (j > ret && pbuf[i * pcols + j] == fb_val((255), (255), (255)))
   j--;
  if (ret < j)
   ret = j;
 }
 return ret;
}


loadpage

decompiled_code:
__int64 __fastcall loadpage(int a1)
{
__int64 result; // rax
void *v2; // rax
int v3; // esi
int v4; // ecx
_DWORD *v5; // rdx
__int64 v6; // rax

if ( a1 <= 0 )
return 1LL;
if ( (int)doc_pages(doc) < a1 )
return 1LL;
prows = 0;
free(pbuf);
v2 = (void *)doc_draw(doc, (unsigned int)a1, (unsigned int)zoom, (unsigned int)rotate, &prows, &pcols);
v3 = prows;
v4 = pcols;
pbuf = v2;
v5 = v2;
if ( invert && prows * pcols > 0 )
{
v6 = 0LL;
do
{
v5[v6] = ~v5[v6];
v3 = prows;
++v6;
v4 = pcols;
}
while ( pcols * prows > (int)v6 );
}
num = a1;
result = 0LL;
prow = v3 / -2;
pcol = v4 / -2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
__int64 result; // rax
void *v2; // rax
int v3; // esi
int v4; // ecx
_DWORD *v5; // rdx
__int64 v6; // rax

if ( a1 <= 0 )
return 1LL;
if ( (int)doc_pages(doc) < a1 )
return 1LL;
prows = 0;
free(pbuf);
v2 = (void *)doc_draw(doc, (unsigned int)a1, (unsigned int)zoom, (unsigned int)rotate, &prows, &pcols);
v3 = prows;
v4 = pcols;
pbuf = v2;
v5 = v2;
if ( invert && prows * pcols > 0 )
{
v6 = 0LL;
do
{
v5[v6] = ~v5[v6];
v3 = prows;
++v6;
v4 = pcols;
}
while ( pcols * prows > (int)v6 );
}
num = a1;
result = 0LL;
prow = v3 / -2;
pcol = v4 / -2;
return result;
}

original_code:
static int loadpage(int p)
{
 int i;
 if (p < 1 || p > doc_pages(doc))
  return 1;
 prows = 0;
 free(pbuf);
 pbuf = doc_draw(doc, p, zoom, rotate, &prows, &pcols);
 if (invert) {
  for (i = 0; i < prows * pcols; i++)
   pbuf[i] = pbuf[i] ^ 0xffffffff;
 }
 prow = -prows / 2;
 pcol = -pcols / 2;
 num = p;
 return 0;
}

variable_types: p int


decompiled_code:
__int64 __fastcall zoom_page(int a1)
{
int v1; // ebx
__int64 result; // rax

v1 = zoom;
if ( a1 <= 0 )
a1 = 1;
if ( a1 > 1000 )
a1 = 1000;
zoom = a1;
result = loadpage(num);
if ( !(_DWORD)result )
{
result = (unsigned int)(zoom * srow / v1);
srow = zoom * srow / v1;
}
return result;
}

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

v1 = zoom;
if ( a1 <= 0 )
a1 = 1;
if ( a1 > 1000 )
a1 = 1000;
zoom = a1;
result = func7(num);
if ( !(_DWORD)result )
{
result = (unsigned int)(zoom * srow / v1);
srow = zoom * srow / v1;
}
return result;
}

variable_types: z int


jmpmark

decompiled_code:
__int64 __fastcall jmpmark(int a1, int a2)
{
const unsigned __int16 **v2; // r8
__int64 result; // rax
int v4; // er12
int v5; // ebx
int v6; // ebp

if ( a1 == 96 )
{
result = 39LL;
v4 = mark[39];
if ( !v4 )
return result;
}
else
{
v2 = _ctype_b_loc();
result = a1;
if ( ((*v2)[a1] & 0x400) == 0 && a1 != 39 )
return result;
v4 = mark[a1];
if ( !v4 )
return result;
}
if ( !a2 )
{
setmark(39);
result = loadpage(v4);
if ( (_DWORD)result )
return result;
result = (unsigned int)prow;
goto LABEL_7;
}
v5 = mark_row[result];
v6 = zoom;
setmark(39);
result = loadpage(v4);
if ( !(_DWORD)result )
{
result = (unsigned int)(v6 * v5 / 100);
LABEL_7:
srow = result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, int a2)
{
const unsigned __int16 **v2; // r8
__int64 result; // rax
int v4; // er12
int v5; // ebx
int v6; // ebp

if ( a1 == 96 )
{
result = 39LL;
v4 = mark[39];
if ( !v4 )
return result;
}
else
{
v2 = _ctype_b_loc();
result = a1;
if ( ((*v2)[a1] & 0x400) == 0 && a1 != 39 )
return result;
v4 = mark[a1];
if ( !v4 )
return result;
}
if ( !a2 )
{
func2(39);
result = func7(v4);
if ( (_DWORD)result )
return result;
result = (unsigned int)prow;
goto LABEL_7;
}
v5 = mark_row[result];
v6 = zoom;
func2(39);
result = func7(v4);
if ( !(_DWORD)result )
{
result = (unsigned int)(v6 * v5 / 100);
LABEL_7:
srow = result;
}
return result;
}

original_code:
static void jmpmark(int c, int offset)
{
 if (c == '`')
  c = '\'';
 if ((((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISalpha) || (c) == '\'' || (c) == '`') && mark[c]) {
  int dst = mark[c];
  int dst_row = offset ? mark_row[c] * zoom / 100 : 0;
  setmark('\'');
  if (!loadpage(dst))
   srow = offset ? dst_row : prow;
 }
}

canonical_original_code:
static void jmpmark(int c, int offset)
{
 if (c == '`')
  c = '\'';
 if ((((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISalpha) || (c) == '\'' || (c) == '`') && mark[c]) {
  int dst = mark[c];
  int dst_row = offset ? mark_row[c] * zoom / 100 : 0;
  setmark('\'');
  if (!loadpage(dst))
   srow = offset ? dst_row : prow;
 }
}

variable_types: offset int

variable_types: dst int


draw

decompiled_code:
void draw()
{
__int64 v0; // rax
int v1; // ebx
char *v2; // rax
int v3; // er14
char *v4; // r13
__int64 v5; // rdx
int v6; // ebp
int v7; // er15
int v8; // er12
int v9; // edi
int v10; // edx
int v11; // edi
void *v12; // rax
__int64 v13; // [rsp+0h] [rbp-48h]
int v14; // [rsp+0h] [rbp-48h]
int v15; // [rsp+Ch] [rbp-3Ch]

v0 = fb_mode();
v13 = scols;
v1 = scols;
v15 = BYTE2(v0) & 0xF;
v2 = (char *)malloc(4LL * scols);
v3 = srow;
v4 = v2;
if ( srows > 0 )
{
v5 = v13;
v6 = srow;
while ( 1 )
{
v7 = pcol;
v8 = scol;
v14 = pcols;
memset(v4, 0, 4 * v5);
if ( prow <= v6 )
{
v9 = v8;
if ( v7 >= v8 )
v9 = v7;
v10 = v7 + v14;
if ( v7 + v14 > v8 + v1 )
v10 = v8 + v1;
if ( prow + prows > v6 && v9 < v10 )
memcpy(
&v4[4 * (v9 - (__int64)v8)],
(char *)pbuf + 4 * v14 * (v6 - prow) + 4 * (v9 - (__int64)v7),
4LL * (v10 - v9));
}
v11 = v6++;
v12 = (void *)fb_mem((unsigned int)(v11 - v3));
memcpy(v12, v4, v15 * v1);
v3 = srow;
if ( srow + srows <= v6 )
break;
v5 = scols;
v1 = scols;
}
}
free(v4);
}

canonical_decompiled_code:
void func10()
{
__int64 v0; // rax
int v1; // ebx
char *v2; // rax
int v3; // er14
char *v4; // r13
__int64 v5; // rdx
int v6; // ebp
int v7; // er15
int v8; // er12
int v9; // edi
int v10; // edx
int v11; // edi
void *v12; // rax
__int64 v13; // [rsp+0h] [rbp-48h]
int v14; // [rsp+0h] [rbp-48h]
int v15; // [rsp+Ch] [rbp-3Ch]

v0 = fb_mode();
v13 = scols;
v1 = scols;
v15 = BYTE2(v0) & 0xF;
v2 = (char *)malloc(4LL * scols);
v3 = srow;
v4 = v2;
if ( srows > 0 )
{
v5 = v13;
v6 = srow;
while ( 1 )
{
v7 = pcol;
v8 = scol;
v14 = pcols;
memset(v4, 0, 4 * v5);
if ( prow <= v6 )
{
v9 = v8;
if ( v7 >= v8 )
v9 = v7;
v10 = v7 + v14;
if ( v7 + v14 > v8 + v1 )
v10 = v8 + v1;
if ( prow + prows > v6 && v9 < v10 )
memcpy(
&v4[4 * (v9 - (__int64)v8)],
(char *)pbuf + 4 * v14 * (v6 - prow) + 4 * (v9 - (__int64)v7),
4LL * (v10 - v9));
}
v11 = v6++;
v12 = (void *)fb_mem((unsigned int)(v11 - v3));
memcpy(v12, v4, v15 * v1);
v3 = srow;
if ( srow + srows <= v6 )
break;
v5 = scols;
v1 = scols;
}
}
free(v4);
}

original_code:
static void draw(void)
{
 int bpp = (((fb_mode()) >> 16) & 0x0f);
 int i;
 fbval_t *rbuf = malloc(scols * sizeof(rbuf[0]));
 for (i = srow; i < srow + srows; i++) {
  int cbeg = ((scol) > (pcol) ? (scol) : (pcol));
  int cend = ((scol + scols) < (pcol + pcols) ? (scol + scols) : (pcol + pcols));
  memset(rbuf, 0, scols * sizeof(rbuf[0]));
  if (i >= prow && i < prow + prows && cbeg < cend) {
   memcpy(rbuf + cbeg - scol,
    pbuf + (i - prow) * pcols + cbeg - pcol,
    (cend - cbeg) * sizeof(rbuf[0]));
  }
  memcpy(fb_mem(i - srow), rbuf, scols * bpp);
 }
 free(rbuf);
}

canonical_original_code:
static void draw()
{
 int bpp = (((fb_mode()) >> 16) & 0x0f);
 int i;
 unsigned int *rbuf = malloc(scols * sizeof(rbuf[0]));
 for (i = srow; i < srow + srows; i++) {
  int cbeg = ((scol) > (pcol) ? (scol) : (pcol));
  int cend = ((scol + scols) < (pcol + pcols) ? (scol + scols) : (pcol + pcols));
  memset(rbuf, 0, scols * sizeof(rbuf[0]));
  if (i >= prow && i < prow + prows && cbeg < cend) {
   memcpy(rbuf + cbeg - scol,
    pbuf + (i - prow) * pcols + cbeg - pcol,
    (cend - cbeg) * sizeof(rbuf[0]));
  }
  memcpy(fb_mem(i - srow), rbuf, scols * bpp);
 }
 free(rbuf);
}

variable_types: bpp int

variable_types: i int

variable_types:
rbuf
unsigned int *

variable_types: cbeg int

variable_types: cend int


reload

decompiled_code:
__int64 reload()
{
__int64 v0; // rax
__int64 result; // rax

doc_close(doc);
v0 = doc_open(filename);
doc = v0;
if ( v0 && (unsigned int)doc_pages(v0) )
{
if ( !(unsigned int)loadpage(num) )
draw();
result = 0LL;
}
else
{
fprintf(stderr, "\nfbpdf: cannot open <%s>\n", filename);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func11()
{
__int64 v0; // rax
__int64 result; // rax

doc_close(doc);
v0 = doc_open(filename);
doc = v0;
if ( v0 && (unsigned int)doc_pages(v0) )
{
if ( !(unsigned int)func7(num) )
func10();
result = 0LL;
}
else
{
fprintf(stderr, "\nfbpdf: cannot open <%s>\n", filename);
result = 1LL;
}
return result;
}

original_code:
static int reload(void)
{
 doc_close(doc);
 doc = doc_open(filename);
 if (!doc || !doc_pages(doc)) {
  fprintf(stderr, "\nfbpdf: cannot open <%s>\n", filename);
  return 1;
 }
 if (!loadpage(num))
  draw();
 return 0;
}

canonical_original_code:
static int reload()
{
 doc_close(doc);
 doc = doc_open(filename);
 if (!doc || !doc_pages(doc)) {
  fprintf(stderr, "\nfbpdf: cannot open <%s>\n", filename);
  return 1;
 }
 if (!loadpage(num))
  draw();
 return 0;
}


readkey

decompiled_code:
__int64 readkey()
{
__int64 result; // rax
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

if ( read(0, buf, 1uLL) <= 0 )
result = 0xFFFFFFFFLL;
else
result = (unsigned __int8)buf[0];
return result;
}

canonical_decompiled_code:
__int64 func12()
{
__int64 result; // rax
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

if ( read(0, buf, 1uLL) <= 0 )
result = 0xFFFFFFFFLL;
else
result = (unsigned __int8)buf[0];
return result;
}

original_code:
static int readkey(void)
{
 unsigned char b;
 if (read(0, &b, 1) <= 0)
  return -1;
 return b;
}

canonical_original_code:
static int readkey()
{
 unsigned char b;
 if (read(0, &b, 1) <= 0)
  return -1;
 return b;
}

variable_types:
b
unsigned char


decompiled_code:
int term_cleanup()
{
tcsetattr(0, 0, &termios);
return puts("\x1B[?25h");
}

canonical_decompiled_code:
int func13()
{
tcsetattr(0, 0, &termios);
return puts("\x1B[?25h");
}

original_code:
static void term_cleanup(void)
{
 tcsetattr(0, 0, &termios);
 printf("\x1b[?25h\n");
}

canonical_original_code:
static void term_cleanup()
{
 tcsetattr(0, 0, &termios);
 printf("\x1b[?25h\n");
}


mainloop

decompiled_code:
int mainloop()
{
int v0; // ebp
int v1; // ebx
int v2; // er12
int v3; // ebx
int v4; // er12
int v5; // ebp
int v6; // eax
int v7; // er15
int v8; // eax
int v9; // edx
int v11; // eax
unsigned int v12; // eax
int v13; // eax
int v14; // eax
int v15; // eax
int v16; // eax
int v17; // eax
int v18; // edx
int v19; // eax
int v20; // eax
int v21; // edx
int v22; // eax
int v23; // edi
int v24; // er15
int v25; // er15
int v26; // eax
int v27; // er15
int v28; // [rsp+Ch] [rbp-3Ch]
int v29; // [rsp+Ch] [rbp-3Ch]

v0 = scols;
v1 = srows + 7;
v2 = scols + 7;
if ( srows >= 0 )
v1 = srows;
v3 = v1 >> 3;
if ( scols >= 0 )
v2 = scols;
term_setup();
signal(18, (__sighandler_t)sigcont);
v4 = v2 >> 3;
loadpage(num);
srow = prow;
scol = scols / -2;
draw();
v5 = v0 / 16;
while ( 1 )
{
v6 = readkey();
v7 = v6;
if ( v6 == 113 )
return term_cleanup();
LABEL_7:
if ( v6 == -1 )
return term_cleanup();
if ( v7 == 101 )
{
if ( (unsigned int)reload() )
return term_cleanup();
if ( (*((_BYTE *)*_ctype_b_loc() + 203) & 8) != 0 )
{
LABEL_19:
count = v7 + 10 * count - 48;
LABEL_20:
switch ( v7 )
{
case 2:
case 75:
v17 = getcount(1u);
if ( (unsigned int)loadpage(v18 - v17) )
goto LABEL_23;
goto LABEL_22;
case 4:
case 32:
srow += srows * getcount(1u) - v3;
goto LABEL_23;
case 6:
case 74:
v16 = getcount(1u);
if ( !(unsigned int)loadpage(num + v16) )
goto LABEL_22;
goto LABEL_23;
case 12:
goto LABEL_23;
case 21:
case 127:
srow -= srows * getcount(1u) - v3;
goto LABEL_23;
case 39:
case 96:
v15 = readkey();
jmpmark(v15, v7 == 96);
goto LABEL_23;
case 67:
scol = scols / -2;
goto LABEL_23;
case 71:
setmark(39);
v19 = doc_pages(doc);
v20 = getcount(v19 - numdiff);
if ( !(unsigned int)loadpage(v21 + v20) )
goto LABEL_22;
goto LABEL_23;
case 72:
goto LABEL_22;
case 73:
invert = invert == 0;
loadpage(num);
goto LABEL_23;
case 76:
srow = prow + prows - srows;
goto LABEL_23;
case 77:
srow = prow + prows / 2 - srows / 2;
goto LABEL_23;
case 79:
v27 = num;
numdiff = v27 - getcount(num);
v29 = numdiff;
setmark(39);
if ( !(unsigned int)loadpage(v27 + v29) )
goto LABEL_22;
goto LABEL_23;
case 87:
v24 = lmargin();
if ( v24 < (int)rmargin() )
{
v28 = zoom * (scols - v4);
v25 = rmargin();
v26 = lmargin();
zoom_page(v28 / (v25 - v26));
}
goto LABEL_23;
case 91:
scol = pcol;
goto LABEL_23;
case 93:
scol = pcol + pcols - scols;
goto LABEL_23;
case 102:
v23 = zoom;
if ( prows )
v23 = srows * zoom / prows;
goto LABEL_49;
case 104:
scol -= v4 * getcount(1u);
goto LABEL_23;
case 106:
srow += v3 * getcount(1u);
goto LABEL_23;
case 107:
srow -= v3 * getcount(1u);
goto LABEL_23;
case 108:
scol += v4 * getcount(1u);
goto LABEL_23;
case 114:
rotate = getcount(0);
if ( !(unsigned int)loadpage(num) )
LABEL_22:
srow = prow;
goto LABEL_23;
case 119:
v23 = zoom;
if ( pcols )
v23 = scols * zoom / pcols;
LABEL_49:
zoom_page(v23);
goto LABEL_23;
case 122:
count *= 10;
v22 = getcount(zoom_def);
zoom_page(v22);
goto LABEL_23;
case 123:
scol = pcol + lmargin() - v5;
goto LABEL_23;
case 125:
scol = v5 + pcol + rmargin() - scols;
LABEL_23:
v13 = srow;
if ( prows + prow <= srow )
v13 = prows + prow - 1;
if ( prow - srows >= v13 )
v13 = prow - srows + 1;
srow = v13;
v14 = scol;
if ( pcols + pcol <= scol )
v14 = pcols + pcol - 1;
if ( pcol - scols >= v14 )
v14 = pcol - scols + 1;
scol = v14;
draw();
break;
default:
continue;
}
}
}
else
{
if ( v7 != 27 )
{
switch ( v7 )
{
case 'Z':
count *= 10;
zoom_def = getcount(zoom);
continue;
case 'd':
v12 = getcount(1u);
sleep(v12);
continue;
case 'i':
printinfo();
continue;
case 'm':
v11 = readkey();
setmark(v11);
continue;
case 'o':
v8 = getcount(num);
numdiff = v9 - v8;
v6 = readkey();
v7 = v6;
if ( v6 != 113 )
goto LABEL_7;
break;
default:
if ( ((*_ctype_b_loc())[v7] & 0x800) != 0 )
goto LABEL_19;
goto LABEL_20;
}
return term_cleanup();
}
count = 0;
}
}
}

canonical_decompiled_code:
int func14()
{
int v0; // ebp
int v1; // ebx
int v2; // er12
int v3; // ebx
int v4; // er12
int v5; // ebp
int v6; // eax
int v7; // er15
int v8; // eax
int v9; // edx
int v11; // eax
unsigned int v12; // eax
int v13; // eax
int v14; // eax
int v15; // eax
int v16; // eax
int v17; // eax
int v18; // edx
int v19; // eax
int v20; // eax
int v21; // edx
int v22; // eax
int v23; // edi
int v24; // er15
int v25; // er15
int v26; // eax
int v27; // er15
int v28; // [rsp+Ch] [rbp-3Ch]
int v29; // [rsp+Ch] [rbp-3Ch]

v0 = scols;
v1 = srows + 7;
v2 = scols + 7;
if ( srows >= 0 )
v1 = srows;
v3 = v1 >> 3;
if ( scols >= 0 )
v2 = scols;
func3();
signal(18, (__sighandler_t)sigcont);
v4 = v2 >> 3;
func7(num);
srow = prow;
scol = scols / -2;
func10();
v5 = v0 / 16;
while ( 1 )
{
v6 = func12();
v7 = v6;
if ( v6 == 113 )
return func13();
LABEL_7:
if ( v6 == -1 )
return func13();
if ( v7 == 101 )
{
if ( (unsigned int)func11() )
return func13();
if ( (*((_BYTE *)*_ctype_b_loc() + 203) & 8) != 0 )
{
LABEL_19:
count = v7 + 10 * count - 48;
LABEL_20:
switch ( v7 )
{
case 2:
case 75:
v17 = func0(1u);
if ( (unsigned int)func7(v18 - v17) )
goto LABEL_23;
goto LABEL_22;
case 4:
case 32:
srow += srows * func0(1u) - v3;
goto LABEL_23;
case 6:
case 74:
v16 = func0(1u);
if ( !(unsigned int)func7(num + v16) )
goto LABEL_22;
goto LABEL_23;
case 12:
goto LABEL_23;
case 21:
case 127:
srow -= srows * func0(1u) - v3;
goto LABEL_23;
case 39:
case 96:
v15 = func12();
func9(v15, v7 == 96);
goto LABEL_23;
case 67:
scol = scols / -2;
goto LABEL_23;
case 71:
func2(39);
v19 = doc_pages(doc);
v20 = func0(v19 - numdiff);
if ( !(unsigned int)func7(v21 + v20) )
goto LABEL_22;
goto LABEL_23;
case 72:
goto LABEL_22;
case 73:
invert = invert == 0;
func7(num);
goto LABEL_23;
case 76:
srow = prow + prows - srows;
goto LABEL_23;
case 77:
srow = prow + prows / 2 - srows / 2;
goto LABEL_23;
case 79:
v27 = num;
numdiff = v27 - func0(num);
v29 = numdiff;
func2(39);
if ( !(unsigned int)func7(v27 + v29) )
goto LABEL_22;
goto LABEL_23;
case 87:
v24 = func5();
if ( v24 < (int)func6() )
{
v28 = zoom * (scols - v4);
v25 = func6();
v26 = func5();
func8(v28 / (v25 - v26));
}
goto LABEL_23;
case 91:
scol = pcol;
goto LABEL_23;
case 93:
scol = pcol + pcols - scols;
goto LABEL_23;
case 102:
v23 = zoom;
if ( prows )
v23 = srows * zoom / prows;
goto LABEL_49;
case 104:
scol -= v4 * func0(1u);
goto LABEL_23;
case 106:
srow += v3 * func0(1u);
goto LABEL_23;
case 107:
srow -= v3 * func0(1u);
goto LABEL_23;
case 108:
scol += v4 * func0(1u);
goto LABEL_23;
case 114:
rotate = func0(0);
if ( !(unsigned int)func7(num) )
LABEL_22:
srow = prow;
goto LABEL_23;
case 119:
v23 = zoom;
if ( pcols )
v23 = scols * zoom / pcols;
LABEL_49:
func8(v23);
goto LABEL_23;
case 122:
count *= 10;
v22 = func0(zoom_def);
func8(v22);
goto LABEL_23;
case 123:
scol = pcol + func5() - v5;
goto LABEL_23;
case 125:
scol = v5 + pcol + func6() - scols;
LABEL_23:
v13 = srow;
if ( prows + prow <= srow )
v13 = prows + prow - 1;
if ( prow - srows >= v13 )
v13 = prow - srows + 1;
srow = v13;
v14 = scol;
if ( pcols + pcol <= scol )
v14 = pcols + pcol - 1;
if ( pcol - scols >= v14 )
v14 = pcol - scols + 1;
scol = v14;
func10();
break;
default:
continue;
}
}
}
else
{
if ( v7 != 27 )
{
switch ( v7 )
{
case 'Z':
count *= 10;
zoom_def = func0(zoom);
continue;
case 'd':
v12 = func0(1u);
sleep(v12);
continue;
case 'i':
func1();
continue;
case 'm':
v11 = func12();
func2(v11);
continue;
case 'o':
v8 = func0(num);
numdiff = v9 - v8;
v6 = func12();
v7 = v6;
if ( v6 != 113 )
goto LABEL_7;
break;
default:
if ( ((*_ctype_b_loc())[v7] & 0x800) != 0 )
goto LABEL_19;
goto LABEL_20;
}
return func13();
}
count = 0;
}
}
}

original_code:
static void mainloop(void)
{
 int step = srows / 8;
 int hstep = scols / 8;
 int c;
 term_setup();
 signal(18, sigcont);
 loadpage(num);
 srow = prow;
 scol = -scols / 2;
 draw();
 while ((c = readkey()) != -1) {
  if (c == 'q')
   break;
  if (c == 'e' && reload())
   break;
  switch (c) {
  case 'o':
   numdiff = num - getcount(num);
   break;
  case 'Z':
   count *= 10;
   zoom_def = getcount(zoom);
   break;
  case 'i':
   printinfo();
   break;
  case 27:
   count = 0;
   break;
  case 'm':
   setmark(readkey());
   break;
  case 'd':
   sleep(getcount(1));
   break;
  default:
   if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISdigit))
    count = count * 10 + c - '0';
  }
  switch (c) {
  case (('f') - 96):
  case 'J':
   if (!loadpage(num + getcount(1)))
    srow = prow;
   break;
  case (('b') - 96):
  case 'K':
   if (!loadpage(num - getcount(1)))
    srow = prow;
   break;
  case 'G':
   setmark('\'');
   if (!loadpage(getcount(doc_pages(doc) - numdiff) + numdiff))
    srow = prow;
   break;
  case 'O':
   numdiff = num - getcount(num);
   setmark('\'');
   if (!loadpage(num + numdiff))
    srow = prow;
   break;
  case 'z':
   count *= 10;
   zoom_page(getcount(zoom_def));
   break;
  case 'w':
   zoom_page(pcols ? zoom * scols / pcols : zoom);
   break;
  case 'W':
   if (lmargin() < rmargin())
    zoom_page(zoom * (scols - hstep) /
     (rmargin() - lmargin()));
   break;
  case 'f':
   zoom_page(prows ? zoom * srows / prows : zoom);
   break;
  case 'r':
   rotate = getcount(0);
   if (!loadpage(num))
    srow = prow;
   break;
  case '`':
  case '\'':
   jmpmark(readkey(), c == '`');
   break;
  case 'j':
   srow += step * getcount(1);
   break;
  case 'k':
   srow -= step * getcount(1);
   break;
  case 'l':
   scol += hstep * getcount(1);
   break;
  case 'h':
   scol -= hstep * getcount(1);
   break;
  case 'H':
   srow = prow;
   break;
  case 'L':
   srow = prow + prows - srows;
   break;
  case 'M':
   srow = prow + prows / 2 - srows / 2;
   break;
  case 'C':
   scol = -scols / 2;
   break;
  case ' ':
  case (('d') - 96):
   srow += srows * getcount(1) - step;
   break;
  case 127:
  case (('u') - 96):
   srow -= srows * getcount(1) - step;
   break;
  case '[':
   scol = pcol;
   break;
  case ']':
   scol = pcol + pcols - scols;
   break;
  case '{':
   scol = pcol + lmargin() - hstep / 2;
   break;
  case '}':
   scol = pcol + rmargin() + hstep / 2 - scols;
   break;
  case (('l') - 96):
   break;
  case 'I':
   invert = !invert;
   loadpage(num);
   break;
  default:
   continue;
  }
  srow = ((prow - srows + 1) > (((prow + prows - 1) < (srow) ? (prow + prows - 1) : (srow))) ? (prow - srows + 1) : (((prow + prows - 1) < (srow) ? (prow + prows - 1) : (srow))));
  scol = ((pcol - scols + 1) > (((pcol + pcols - 1) < (scol) ? (pcol + pcols - 1) : (scol))) ? (pcol - scols + 1) : (((pcol + pcols - 1) < (scol) ? (pcol + pcols - 1) : (scol))));
  draw();
 }
 term_cleanup();
}

canonical_original_code:
static void mainloop()
{
 int step = srows / 8;
 int hstep = scols / 8;
 int c;
 term_setup();
 signal(18, sigcont);
 loadpage(num);
 srow = prow;
 scol = -scols / 2;
 draw();
 while ((c = readkey()) != -1) {
  if (c == 'q')
   break;
  if (c == 'e' && reload())
   break;
  switch (c) {
  case 'o':
   numdiff = num - getcount(num);
   break;
  case 'Z':
   count *= 10;
   zoom_def = getcount(zoom);
   break;
  case 'i':
   printinfo();
   break;
  case 27:
   count = 0;
   break;
  case 'm':
   setmark(readkey());
   break;
  case 'd':
   sleep(getcount(1));
   break;
  default:
   if (((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISdigit))
    count = count * 10 + c - '0';
  }
  switch (c) {
  case (('f') - 96):
  case 'J':
   if (!loadpage(num + getcount(1)))
    srow = prow;
   break;
  case (('b') - 96):
  case 'K':
   if (!loadpage(num - getcount(1)))
    srow = prow;
   break;
  case 'G':
   setmark('\'');
   if (!loadpage(getcount(doc_pages(doc) - numdiff) + numdiff))
    srow = prow;
   break;
  case 'O':
   numdiff = num - getcount(num);
   setmark('\'');
   if (!loadpage(num + numdiff))
    srow = prow;
   break;
  case 'z':
   count *= 10;
   zoom_page(getcount(zoom_def));
   break;
  case 'w':
   zoom_page(pcols ? zoom * scols / pcols : zoom);
   break;
  case 'W':
   if (lmargin() < rmargin())
    zoom_page(zoom * (scols - hstep) /
     (rmargin() - lmargin()));
   break;
  case 'f':
   zoom_page(prows ? zoom * srows / prows : zoom);
   break;
  case 'r':
   rotate = getcount(0);
   if (!loadpage(num))
    srow = prow;
   break;
  case '`':
  case '\'':
   jmpmark(readkey(), c == '`');
   break;
  case 'j':
   srow += step * getcount(1);
   break;
  case 'k':
   srow -= step * getcount(1);
   break;
  case 'l':
   scol += hstep * getcount(1);
   break;
  case 'h':
   scol -= hstep * getcount(1);
   break;
  case 'H':
   srow = prow;
   break;
  case 'L':
   srow = prow + prows - srows;
   break;
  case 'M':
   srow = prow + prows / 2 - srows / 2;
   break;
  case 'C':
   scol = -scols / 2;
   break;
  case ' ':
  case (('d') - 96):
   srow += srows * getcount(1) - step;
   break;
  case 127:
  case (('u') - 96):
   srow -= srows * getcount(1) - step;
   break;
  case '[':
   scol = pcol;
   break;
  case ']':
   scol = pcol + pcols - scols;
   break;
  case '{':
   scol = pcol + lmargin() - hstep / 2;
   break;
  case '}':
   scol = pcol + rmargin() + hstep / 2 - scols;
   break;
  case (('l') - 96):
   break;
  case 'I':
   invert = !invert;
   loadpage(num);
   break;
  default:
   continue;
  }
  srow = ((prow - srows + 1) > (((prow + prows - 1) < (srow) ? (prow + prows - 1) : (srow))) ? (prow - srows + 1) : (((prow + prows - 1) < (srow) ? (prow + prows - 1) : (srow))));
  scol = ((pcol - scols + 1) > (((pcol + pcols - 1) < (scol) ? (pcol + pcols - 1) : (scol))) ? (pcol - scols + 1) : (((pcol + pcols - 1) < (scol) ? (pcol + pcols - 1) : (scol))));
  draw();
 }
 term_cleanup();
}

variable_types: step int

variable_types: hstep int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // eax
const char *v5; // rdi
__int64 v6; // rcx
char v7; // dl
int v8; // er12
const char *v9; // rdi
char *v10; // rax
int v11; // er12
const char *v13; // rdi
const char *v14; // rdi

if ( argc <= 1 )
{
v11 = 1;
puts("usage: fbpdf [-r rotation] [-z zoom x10] [-p page] filename");
return v11;
}
strcpy(filename, argv[argc - 1]);
doc = doc_open(filename);
if ( !doc || !(unsigned int)doc_pages(doc) )
{
v11 = 1;
fprintf(stderr, "fbpdf: cannot open <%s>\n", filename);
return v11;
}
v4 = 1;
while ( 1 )
{
v5 = argv[v4];
v6 = v4;
if ( *v5 != 45 )
break;
v7 = v5[1];
v8 = v4 + 1;
if ( v7 == 114 )
{
if ( v5[2] )
{
v13 = v5 + 2;
}
else
{
v13 = argv[v6 + 1];
v8 = v4 + 2;
}
rotate = atoi(v13);
LABEL_6:
v4 = v8;
if ( argc <= v8 )
break;
}
else
{
if ( v7 != 122 )
{
if ( v7 == 112 )
{
if ( v5[2] )
{
v14 = v5 + 2;
}
else
{
v14 = argv[v6 + 1];
v8 = v4 + 2;
}
num = atoi(v14);
}
goto LABEL_6;
}
if ( v5[2] )
{
v9 = v5 + 2;
}
else
{
v9 = argv[v6 + 1];
v8 = v4 + 2;
}
zoom = 10 * atoi(v9);
v4 = v8;
if ( argc <= v8 )
break;
}
}
printinfo();
v10 = getenv("FBDEV");
v11 = fb_init(v10);
if ( v11 )
return 1;
srows = fb_rows();
scols = fb_cols();
if ( (((unsigned int)fb_mode() >> 16) & 0xF) == 4 )
mainloop();
else
fwrite("fbpdf: fbval_t doesn't match fb depth\n", 1uLL, 0x26uLL, stderr);
fb_free();
free(pbuf);
if ( doc )
doc_close(doc);
return v11;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // eax
const char *v5; // rdi
__int64 v6; // rcx
char v7; // dl
int v8; // er12
const char *v9; // rdi
char *v10; // rax
int v11; // er12
const char *v13; // rdi
const char *v14; // rdi

if ( argc <= 1 )
{
v11 = 1;
puts("usage: fbpdf [-r rotation] [-z zoom x10] [-p page] filename");
return v11;
}
strcpy(filename, argv[argc - 1]);
doc = doc_open(filename);
if ( !doc || !(unsigned int)doc_pages(doc) )
{
v11 = 1;
fprintf(stderr, "fbpdf: cannot open <%s>\n", filename);
return v11;
}
v4 = 1;
while ( 1 )
{
v5 = argv[v4];
v6 = v4;
if ( *v5 != 45 )
break;
v7 = v5[1];
v8 = v4 + 1;
if ( v7 == 114 )
{
if ( v5[2] )
{
v13 = v5 + 2;
}
else
{
v13 = argv[v6 + 1];
v8 = v4 + 2;
}
rotate = atoi(v13);
LABEL_6:
v4 = v8;
if ( argc <= v8 )
break;
}
else
{
if ( v7 != 122 )
{
if ( v7 == 112 )
{
if ( v5[2] )
{
v14 = v5 + 2;
}
else
{
v14 = argv[v6 + 1];
v8 = v4 + 2;
}
num = atoi(v14);
}
goto LABEL_6;
}
if ( v5[2] )
{
v9 = v5 + 2;
}
else
{
v9 = argv[v6 + 1];
v8 = v4 + 2;
}
zoom = 10 * atoi(v9);
v4 = v8;
if ( argc <= v8 )
break;
}
}
func1();
v10 = getenv("FBDEV");
v11 = fb_init(v10);
if ( v11 )
return 1;
srows = fb_rows();
scols = fb_cols();
if ( (((unsigned int)fb_mode() >> 16) & 0xF) == 4 )
func14();
else
fwrite("fbpdf: fbval_t doesn't match fb depth\n", 1uLL, 0x26uLL, stderr);
fb_free();
free(pbuf);
if ( doc )
doc_close(doc);
return v11;
}

original_code:
int main(int argc, char *argv[])
{
 int i = 1;
 if (argc < 2) {
  puts(usage);
  return 1;
 }
 strcpy(filename, argv[argc - 1]);
 doc = doc_open(filename);
 if (!doc || !doc_pages(doc)) {
  fprintf(stderr, "fbpdf: cannot open <%s>\n", filename);
  return 1;
 }
 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
  switch (argv[i][1]) {
  case 'r':
   rotate = atoi(argv[i][2] ? argv[i] + 2 : argv[++i]);
   break;
  case 'z':
   zoom = atoi(argv[i][2] ? argv[i] + 2 : argv[++i]) * 10;
   break;
  case 'p':
   num = atoi(argv[i][2] ? argv[i] + 2 : argv[++i]);
   break;
  }
 }
 printinfo();
 if (fb_init(getenv("FBDEV")))
  return 1;
 srows = fb_rows();
 scols = fb_cols();
 if ((((fb_mode()) >> 16) & 0x0f) != sizeof(fbval_t))
  fprintf(stderr, "fbpdf: fbval_t doesn't match fb depth\n");
 else
  mainloop();
 fb_free();
 free(pbuf);
 if (doc)
  doc_close(doc);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
__int64 fb_len()
{
return (unsigned int)(dword_BAC * dword_B70);
}

canonical_decompiled_code:
__int64 func0()
{
return (unsigned int)(dword_BAC * dword_B70);
}

original_code:
static int fb_len(void)
{
 return finfo.line_length * vinfo.yres_virtual;
}

canonical_original_code:
static int fb_len()
{
 return finfo.line_length * vinfo.yres_virtual;
}


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

nr = 1 << dword_BC4;
ng = 1 << dword_BDC;
nb = 1 << dword_BD0;
rr = 8 - dword_BC4;
rl = dword_BC0;
br = 8 - dword_BDC;
result = (unsigned int)dword_BD8;
gr = 8 - dword_BD0;
gl = dword_BCC;
dword_AF4 = dword_BD8;
return result;
}

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

nr = 1 << dword_BC4;
ng = 1 << dword_BDC;
nb = 1 << dword_BD0;
rr = 8 - dword_BC4;
rl = dword_BC0;
br = 8 - dword_BDC;
result = (unsigned int)dword_BD8;
gr = 8 - dword_BD0;
gl = dword_BCC;
dword_AF4 = dword_BD8;
return result;
}

original_code:
static void init_colors(void)
{
 nr = 1 << vinfo.red.length;
 ng = 1 << vinfo.blue.length;
 nb = 1 << vinfo.green.length;
 rr = 8 - vinfo.red.length;
 rl = vinfo.red.offset;
 gr = 8 - vinfo.green.length;
 gl = vinfo.green.offset;
 br = 8 - vinfo.blue.length;
 bl = vinfo.blue.offset;
}

canonical_original_code:
static void init_colors()
{
 nr = 1 << vinfo.red.length;
 ng = 1 << vinfo.blue.length;
 nb = 1 << vinfo.green.length;
 rr = 8 - vinfo.red.length;
 rl = vinfo.red.offset;
 gr = 8 - vinfo.green.length;
 gl = vinfo.green.offset;
 br = 8 - vinfo.blue.length;
 bl = vinfo.blue.offset;
}


original_code:
static void fb_cmap_save(int save)
{
 static unsigned short red[(1 << 8)], green[(1 << 8)], blue[(1 << 8)];
 struct fb_cmap cmap;
 if (finfo.visual == 2)
  return;
 cmap.start = 0;
 cmap.len = ((nr) > (((ng) > (nb) ? (ng) : (nb))) ? (nr) : (((ng) > (nb) ? (ng) : (nb))));
 cmap.red = red;
 cmap.green = green;
 cmap.blue = blue;
 cmap.transp = ((void *)0);
 ioctl(fd, save ? 0x4604 : 0x4605, &cmap);
}

canonical_original_code:
static void fb_cmap_save(int save)
{
 static unsigned short red[-1]; static unsigned short green[-1]; static unsigned short blue[-1];
 struct fb_cmap cmap;
 if (finfo.visual == 2)
  return;
 cmap.start = 0;
 cmap.len = ((nr) > (((ng) > (nb) ? (ng) : (nb))) ? (nr) : (((ng) > (nb) ? (ng) : (nb))));
 cmap.red = red;
 cmap.green = green;
 cmap.blue = blue;
 cmap.transp = ((void *)0);
 ioctl(fd, save ? 0x4604 : 0x4605, &cmap);
}

variable_types: save int

variable_types:
red
unsigned short [-1]

variable_types:
green
unsigned short [-1]

variable_types:
blue
unsigned short [-1]

variable_types: struct fb_cmap { unsigned int start; unsigned int len; unsigned short *red; unsigned short *green; unsigned short *blue; unsigned short *transp; }


original_code:
void fb_cmap(void)
{
 unsigned short red[(1 << 8)], green[(1 << 8)], blue[(1 << 8)];
 struct fb_cmap cmap;
 int i;
 if (finfo.visual == 2)
  return;
 for (i = 0; i < nr; i++)
  red[i] = (65535 / (nr - 1)) * i;
 for (i = 0; i < ng; i++)
  green[i] = (65535 / (ng - 1)) * i;
 for (i = 0; i < nb; i++)
  blue[i] = (65535 / (nb - 1)) * i;
 cmap.start = 0;
 cmap.len = ((nr) > (((ng) > (nb) ? (ng) : (nb))) ? (nr) : (((ng) > (nb) ? (ng) : (nb))));
 cmap.red = red;
 cmap.green = green;
 cmap.blue = blue;
 cmap.transp = ((void *)0);
 ioctl(fd, 0x4605, &cmap);
}

canonical_original_code:
void fb_cmap()
{
 unsigned short red[-1]; unsigned short green[-1]; unsigned short blue[-1];
 struct fb_cmap cmap;
 int i;
 if (finfo.visual == 2)
  return;
 for (i = 0; i < nr; i++)
  red[i] = (65535 / (nr - 1)) * i;
 for (i = 0; i < ng; i++)
  green[i] = (65535 / (ng - 1)) * i;
 for (i = 0; i < nb; i++)
  blue[i] = (65535 / (nb - 1)) * i;
 cmap.start = 0;
 cmap.len = ((nr) > (((ng) > (nb) ? (ng) : (nb))) ? (nr) : (((ng) > (nb) ? (ng) : (nb))));
 cmap.red = red;
 cmap.green = green;
 cmap.blue = blue;
 cmap.transp = ((void *)0);
 ioctl(fd, 0x4605, &cmap);
}


decompiled_code:
__int64 fb_mode()
{
return ((gl < dword_AF4) << 20) | ((rl < dword_AF4) << 21) | ((rl < gl) << 22) | (bpp << 16) | dword_BDC | (16 * dword_BD0) | (unsigned int)(dword_BC4 << 8);
}

canonical_decompiled_code:
__int64 func4()
{
return ((gl < dword_AF4) << 20) | ((rl < dword_AF4) << 21) | ((rl < gl) << 22) | (bpp << 16) | dword_BDC | (16 * dword_BD0) | (unsigned int)(dword_BC4 << 8);
}

original_code:
unsigned fb_mode(void)
{
 return ((rl < gl) << 22) | ((rl < bl) << 21) | ((gl < bl) << 20) |
  (bpp << 16) | (vinfo.red.length << 8) |
  (vinfo.green.length << 4) | (vinfo.blue.length);
}

canonical_original_code:
unsigned int fb_mode()
{
 return ((rl < gl) << 22) | ((rl < bl) << 21) | ((gl < bl) << 20) |
  (bpp << 16) | (vinfo.red.length << 8) |
  (vinfo.green.length << 4) | (vinfo.blue.length);
}


decompiled_code:
__int64 __fastcall fb_init(const char *a1)
{
const char *v1; // rbp
char *v2; // rax
int v3; // eax
int v4; // ebp
int v5; // eax
__int64 v6; // r8
size_t v7; // rsi
int v8; // edx
int v9; // ecx
__off_t v10; // r9
__int64 v11; // rdx
__int64 v12; // rcx
char *v13; // r8
__int64 result; // rax

v1 = "/dev/fb0";
if ( a1 )
{
v1 = a1;
v2 = strchr(a1, 58);
if ( v2 )
{
*v2 = 0;
_isoc99_sscanf(v2 + 1, "%dx%d%d%d", &xres, &yres, &xoff, &yoff);
}
}
v3 = open(v1, 2);
fd = v3;
v4 = v3;
if ( v3 < 0
|| ioctl(v3, 0x4600uLL, &vinfo) < 0
|| ioctl(v4, 0x4602uLL, &finfo) < 0
|| (v5 = fcntl(v4, 1),
fcntl(fd, 2, v5 | 1u),
bpp = (unsigned int)(dword_BB8 + 7) >> 3,
v7 = (int)fb_len(0LL, 2LL, 3LL, 1LL, v6, 0LL),
fb = mmap(0LL, v7, v8, v9, fd, v10),
fb == (void *)-1LL) )
{
perror("fb_init()");
close(fd);
result = 1LL;
}
else
{
init_colors();
fb_cmap_save(1);
fb_cmap(1LL, v7, v11, v12, v13);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1)
{
const char *v1; // rbp
char *v2; // rax
int v3; // eax
int v4; // ebp
int v5; // eax
__int64 v6; // r8
size_t v7; // rsi
int v8; // edx
int v9; // ecx
__off_t v10; // r9
__int64 v11; // rdx
__int64 v12; // rcx
char *v13; // r8
__int64 result; // rax

v1 = "/dev/fb0";
if ( a1 )
{
v1 = a1;
v2 = strchr(a1, 58);
if ( v2 )
{
*v2 = 0;
_isoc99_sscanf(v2 + 1, "%dx%d%d%d", &xres, &yres, &xoff, &yoff);
}
}
v3 = open(v1, 2);
fd = v3;
v4 = v3;
if ( v3 < 0
|| ioctl(v3, 0x4600uLL, &vinfo) < 0
|| ioctl(v4, 0x4602uLL, &finfo) < 0
|| (v5 = fcntl(v4, 1),
fcntl(fd, 2, v5 | 1u),
bpp = (unsigned int)(dword_BB8 + 7) >> 3,
v7 = (int)func0(0LL, 2LL, 3LL, 1LL, v6, 0LL),
fb = mmap(0LL, v7, v8, v9, fd, v10),
fb == (void *)-1LL) )
{
perror("fb_init()");
close(fd);
result = 1LL;
}
else
{
func1();
func2(1);
func3(1LL, v7, v11, v12, v13);
result = 0LL;
}
return result;
}

original_code:
int fb_init(char *dev)
{
 char *path = dev ? dev : "/dev/fb0";
 char *geom = dev ? strchr(dev, ':') : ((void *)0);
 if (geom) {
  *geom = '\0';
  sscanf(geom + 1, "%dx%d%d%d", &xres, &yres, &xoff, &yoff);
 }
 fd = open(path, 02);
 if (fd < 0)
  goto failed;
 if (ioctl(fd, 0x4600, &vinfo) < 0)
  goto failed;
 if (ioctl(fd, 0x4602, &finfo) < 0)
  goto failed;
 fcntl(fd, 2, fcntl(fd, 1) | 1);
 bpp = (vinfo.bits_per_pixel + 7) >> 3;
 fb = mmap(((void *)0), fb_len(), 0x1 | 0x2, 0x01, fd, 0);
 if (fb == ((void *) -1))
  goto failed;
 init_colors();
 fb_cmap_save(1);
 fb_cmap();
 return 0;
failed:
 perror("fb_init()");
 close(fd);
 return 1;
}

variable_types:
dev
char *

variable_types:
path
char *

variable_types:
geom
char *


decompiled_code:
int __fastcall fb_free(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
__int64 v3; // rcx
__int64 v4; // r8
__int64 v5; // r9
int v6; // eax

fb_cmap_save(0);
v6 = fb_len(0LL, a2, v2, v3, v4, v5);
munmap(fb, v6);
return close(fd);
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1, __int64 a2)
{
__int64 v2; // rdx
__int64 v3; // rcx
__int64 v4; // r8
__int64 v5; // r9
int v6; // eax

func2(0);
v6 = func0(0LL, a2, v2, v3, v4, v5);
munmap(fb, v6);
return close(fd);
}

original_code:
void fb_free(void)
{
 fb_cmap_save(0);
 munmap(fb, fb_len());
 close(fd);
}

canonical_original_code:
void fb_free()
{
 fb_cmap_save(0);
 munmap(fb, fb_len());
 close(fd);
}


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

result = (unsigned int)yres;
if ( !yres )
result = (unsigned int)dword_BA4;
return result;
}

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

result = (unsigned int)yres;
if ( !yres )
result = (unsigned int)dword_BA4;
return result;
}

original_code:
int fb_rows(void)
{
 return yres ? yres : vinfo.yres;
}

canonical_original_code:
int fb_rows()
{
 return yres ? yres : vinfo.yres;
}


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

result = (unsigned int)xres;
if ( !xres )
result = (unsigned int)vinfo;
return result;
}

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

result = (unsigned int)xres;
if ( !xres )
result = (unsigned int)vinfo;
return result;
}

original_code:
int fb_cols(void)
{
 return xres ? xres : vinfo.xres;
}

canonical_original_code:
int fb_cols()
{
 return xres ? xres : vinfo.xres;
}


decompiled_code:
char *__fastcall fb_mem(int a1)
{
return (char *)fb
+ (unsigned int)(dword_B70 * (yoff + a1 + dword_BB4))
+ (unsigned __int64)(unsigned int)(bpp * (dword_BB0 + xoff));
}

canonical_decompiled_code:
char *__fastcall func9(int a1)
{
return (char *)fb
+ (unsigned int)(dword_B70 * (yoff + a1 + dword_BB4))
+ (unsigned __int64)(unsigned int)(bpp * (dword_BB0 + xoff));
}

original_code:
void *fb_mem(int r)
{
 return fb + (r + vinfo.yoffset + yoff) * finfo.line_length + (vinfo.xoffset + xoff) * bpp;
}

variable_types: r int


decompiled_code:
__int64 __fastcall fb_val(int a1, int a2, int a3)
{
return (a1 >> rr << rl) | (a3 >> br << dword_AF4) | (unsigned int)(a2 >> gr << gl);
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, int a2, int a3)
{
return (a1 >> rr << rl) | (a3 >> br << dword_AF4) | (unsigned int)(a2 >> gr << gl);
}

original_code:
unsigned fb_val(int r, int g, int b)
{
 return ((r >> rr) << rl) | ((g >> gr) << gl) | ((b >> br) << bl);
}

canonical_original_code:
unsigned int fb_val(int r, int g, int b)
{
 return ((r >> rr) << rl) | ((g >> gr) << gl) | ((b >> br) << bl);
}

variable_types: g int

variable_types: b int


repo: litcave/neatcc

original_code:
static void mem_extend(struct mem *mem)
{
 char *s = mem->s;
 mem->sz = mem->sz ? mem->sz + mem->sz : 512;
 mem->s = malloc(mem->sz);
 if (mem->n)
  memcpy(mem->s, s, mem->n);
 free(s);
}

variable_types: struct mem { char *s; long sz; long n; } *

variable_types:
s
char *


decompiled_code:
void __fastcall mem_init(__int64 a1)
{
*(_QWORD *)(a1 + 16) = 0LL;
*(_OWORD *)a1 = 0LL;
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
*(_QWORD *)(a1 + 16) = 0LL;
*(_OWORD *)a1 = 0LL;
}

original_code:
void mem_init(struct mem *mem)
{
 memset(mem, 0, sizeof(*mem));
}

user_defined_types: struct mem { char *s; long sz; long n; }


decompiled_code:
void __fastcall mem_done(__int64 a1)
{
free(*(void **)a1);
*(_QWORD *)(a1 + 16) = 0LL;
*(_OWORD *)a1 = 0LL;
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1)
{
free(*(void **)a1);
*(_QWORD *)(a1 + 16) = 0LL;
*(_OWORD *)a1 = 0LL;
}

original_code:
void mem_done(struct mem *mem)
{
 free(mem->s);
 memset(mem, 0, sizeof(*mem));
}


original_code:
void mem_cut(struct mem *mem, long pos)
{
 mem->n = pos < mem->n ? pos : mem->n;
}

variable_types: pos long


decompiled_code:
void *__fastcall mem_cpy(_QWORD *a1, __int64 a2, const void *a3, size_t a4)
{
while ( (signed __int64)(a4 + a2 + a1[2] + 1) >= a1[1] )
mem_extend((__int64)a1);
return memcpy((void *)(a2 + *a1), a3, a4);
}

canonical_decompiled_code:
void *__fastcall func4(_QWORD *a1, __int64 a2, const void *a3, size_t a4)
{
while ( (signed __int64)(a4 + a2 + a1[2] + 1) >= a1[1] )
func0((__int64)a1);
return memcpy((void *)(a2 + *a1), a3, a4);
}

original_code:
void mem_cpy(struct mem *mem, long off, void *buf, long len)
{
 while (mem->n + off + len + 1 >= mem->sz)
  mem_extend(mem);
 memcpy(mem->s + off, buf, len);
}

variable_types: off long

variable_types:
buf
void *

variable_types: len long


decompiled_code:
void *__fastcall mem_buf(_QWORD *a1)
{
if ( !*a1 )
return &unk_24E;
*(_BYTE *)(*a1 + a1[2]) = 0;
return (void *)*a1;
}

canonical_decompiled_code:
void *__fastcall func8(_QWORD *a1)
{
if ( !*a1 )
return &unk_24E;
*(_BYTE *)(*a1 + a1[2]) = 0;
return (void *)*a1;
}

original_code:
void *mem_buf(struct mem *mem)
{
 if (!mem->s)
  return "";
 mem->s[mem->n] = '\0';
 return mem->s;
}


decompiled_code:
__int64 __fastcall mem_len(__int64 a1)
{
return *(_QWORD *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
return *(_QWORD *)(a1 + 16);
}

original_code:
long mem_len(struct mem *mem)
{
 return mem->n;
}

return_type: long


decompiled_code:
void __fastcall mem_get(_QWORD *a1)
{
if ( !*a1 )
mem_extend((__int64)a1);
mem_init((__int64)a1);
}

canonical_decompiled_code:
void __fastcall func10(_QWORD *a1)
{
if ( !*a1 )
func0((__int64)a1);
func1((__int64)a1);
}

original_code:
void *mem_get(struct mem *mem)
{
 void *ret;
 if (!mem->s)
  mem_extend(mem);
 ret = mem->s;
 mem_init(mem);
 return ret;
}

variable_types:
ret
void *


variable_types:
r
char *

variable_types: i int


decompiled_code:
__int64 __fastcall id_char(int a1)
{
return ((a1 == 95) | (unsigned __int8)((*_ctype_b_loc())[a1] >> 3)) & 1;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
return ((a1 == 95) | (unsigned __int8)((*_ctype_b_loc())[a1] >> 3)) & 1;
}

original_code:
static int id_char(int c)
{
 return ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISalnum) || c == '_';
}

canonical_original_code:
static int id_char(int c)
{
 return ((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISalnum) || c == '_';
}

variable_types: c int


original_code:
static int esc_char(int *c, char *s)
{
 if (*s != '\\') {
  *c = (unsigned char) *s;
  return 1;
 }
 if (strchr(esc_code, (unsigned char) s[1])) {
  *c = esc[strchr(esc_code, (unsigned char) s[1]) - esc_code];
  return 2;
 }
 if (((*__ctype_b_loc ())[(int) ((s[1]))] & (unsigned short int) _ISdigit) || s[1] == 'x') {
  int ret = 0;
  int base = 8;
  int i = 1;
  char *d;
  if (s[1] == 'x') {
   base = 16;
   i++;
  }
  while ((d = memchr(digs, tolower(s[i]), base))) {
   ret *= base;
   ret += d - digs;
   i++;
  }
  *c = ret;
  return i;
 }
 *c = (unsigned char) s[1];
 return 2;
}

canonical_original_code:
static int esc_char(int *c, char *s)
{
 if (*s != '\\') {
  *c = (unsigned char) *s;
  return 1;
 }
 if (strchr(esc_code, (unsigned char) s[1])) {
  *c = esc[strchr(esc_code, (unsigned char) s[1]) - esc_code];
  return 2;
 }
 if (((*__ctype_b_loc ())[(int) ((s[1]))] & (unsigned short) _ISdigit) || s[1] == 'x') {
  int ret = 0;
  int base = 8;
  int i = 1;
  char *d;
  if (s[1] == 'x') {
   base = 16;
   i++;
  }
  while ((d = memchr(digs, tolower(s[i]), base))) {
   ret *= base;
   ret += d - digs;
   i++;
  }
  *c = ret;
  return i;
 }
 *c = (unsigned char) s[1];
 return 2;
}

variable_types:
c
int *

variable_types: ret int

variable_types: base int

variable_types:
d
char *


skipws

decompiled_code:
__int64 skipws()
{
__int64 v0; // rbp
__int64 v1; // rbx
bool v2; // cc
const unsigned __int16 **v3; // rax
__int64 v4; // rcx
char v5; // si
const unsigned __int16 *v6; // rdi
__int64 v7; // rdx
char v8; // al
char v9; // al
char v11; // al
__int64 v12; // rdx
__int64 v13; // rdx
__int64 v14; // rax
__int64 v15; // [rsp+0h] [rbp-28h] BYREF
__int64 v16[4]; // [rsp+8h] [rbp-20h] BYREF

v0 = len;
v1 = off;
LABEL_2:
v2 = v0 <= v1;
if ( v0 != v1 )
goto LABEL_3;
do
{
v15 = 0LL;
do
{
if ( (unsigned int)cpp_read(v16, &v15) )
return 1LL;
}
while ( !v15 );
mem_put(&tok_mem, v16[0]);
buf = mem_buf(&tok_mem);
len = mem_len(&tok_mem);
v0 = len;
v14 = off;
v1 = off;
if ( len > off )
goto LABEL_4;
if ( len == off )
{
LABEL_28:
v1 = v14;
goto LABEL_2;
}
LABEL_24:
v4 = buf;
v8 = *(_BYTE *)(buf + v1);
if ( v8 == 92 )
goto LABEL_25;
LABEL_10:
if ( v8 != 47 )
return 0LL;
v9 = *(_BYTE *)(v4 + v1 + 1);
v1 = off;
if ( v9 == 47 )
{
while ( 1 )
{
v13 = v1;
if ( ++v1 >= v0 )
break;
v11 = *(_BYTE *)(v4 + v1);
if ( v11 == 10 )
break;
v12 = v13 + 2;
if ( v11 == 92 )
v1 = v12;
}
}
else
{
if ( v9 != 42 )
return 0LL;
v1 = off + 1;
off = v1;
if ( v1 >= v0 )
goto LABEL_2;
if ( *(_BYTE *)(v4 + v1) == 42 )
goto LABEL_33;
while ( ++v1 != v0 )
{
if ( *(_BYTE *)(v4 + v1) == 42 )
{
LABEL_33:
if ( *(_BYTE *)(v4 + v1 + 1) == 47 )
{
LABEL_26:
v1 += 2LL;
off = v1;
goto LABEL_2;
}
}
}
}
off = v1;
v2 = v0 <= v1;
}
while ( v0 == v1 );
LABEL_3:
if ( v2 )
goto LABEL_24;
LABEL_4:
v3 = _ctype_b_loc();
v4 = buf;
v5 = 0;
v6 = *v3;
while ( 1 )
{
v7 = *(char *)(buf + v1);
v8 = *(_BYTE *)(buf + v1);
if ( (v6[v7] & 0x2000) == 0 )
break;
++v1;
v5 = 1;
if ( v1 == v0 )
{
off = v0;
v14 = v0;
goto LABEL_28;
}
}
if ( v5 )
off = v1;
if ( (_BYTE)v7 != 92 )
goto LABEL_10;
LABEL_25:
if ( *(_BYTE *)(v4 + v1 + 1) == 10 )
goto LABEL_26;
return 0LL;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // rbp
__int64 v1; // rbx
bool v2; // cc
const unsigned __int16 **v3; // rax
__int64 v4; // rcx
char v5; // si
const unsigned __int16 *v6; // rdi
__int64 v7; // rdx
char v8; // al
char v9; // al
char v11; // al
__int64 v12; // rdx
__int64 v13; // rdx
__int64 v14; // rax
__int64 v15; // [rsp+0h] [rbp-28h] BYREF
__int64 v16[4]; // [rsp+8h] [rbp-20h] BYREF

v0 = len;
v1 = off;
LABEL_2:
v2 = v0 <= v1;
if ( v0 != v1 )
goto LABEL_3;
do
{
v15 = 0LL;
do
{
if ( (unsigned int)cpp_read(v16, &v15) )
return 1LL;
}
while ( !v15 );
mem_put(&tok_mem, v16[0]);
buf = mem_buf(&tok_mem);
len = mem_len(&tok_mem);
v0 = len;
v14 = off;
v1 = off;
if ( len > off )
goto LABEL_4;
if ( len == off )
{
LABEL_28:
v1 = v14;
goto LABEL_2;
}
LABEL_24:
v4 = buf;
v8 = *(_BYTE *)(buf + v1);
if ( v8 == 92 )
goto LABEL_25;
LABEL_10:
if ( v8 != 47 )
return 0LL;
v9 = *(_BYTE *)(v4 + v1 + 1);
v1 = off;
if ( v9 == 47 )
{
while ( 1 )
{
v13 = v1;
if ( ++v1 >= v0 )
break;
v11 = *(_BYTE *)(v4 + v1);
if ( v11 == 10 )
break;
v12 = v13 + 2;
if ( v11 == 92 )
v1 = v12;
}
}
else
{
if ( v9 != 42 )
return 0LL;
v1 = off + 1;
off = v1;
if ( v1 >= v0 )
goto LABEL_2;
if ( *(_BYTE *)(v4 + v1) == 42 )
goto LABEL_33;
while ( ++v1 != v0 )
{
if ( *(_BYTE *)(v4 + v1) == 42 )
{
LABEL_33:
if ( *(_BYTE *)(v4 + v1 + 1) == 47 )
{
LABEL_26:
v1 += 2LL;
off = v1;
goto LABEL_2;
}
}
}
}
off = v1;
v2 = v0 <= v1;
}
while ( v0 == v1 );
LABEL_3:
if ( v2 )
goto LABEL_24;
LABEL_4:
v3 = _ctype_b_loc();
v4 = buf;
v5 = 0;
v6 = *v3;
while ( 1 )
{
v7 = *(char *)(buf + v1);
v8 = *(_BYTE *)(buf + v1);
if ( (v6[v7] & 0x2000) == 0 )
break;
++v1;
v5 = 1;
if ( v1 == v0 )
{
off = v0;
v14 = v0;
goto LABEL_28;
}
}
if ( v5 )
off = v1;
if ( (_BYTE)v7 != 92 )
goto LABEL_10;
LABEL_25:
if ( *(_BYTE *)(v4 + v1 + 1) == 10 )
goto LABEL_26;
return 0LL;
}

original_code:
static int skipws(void)
{
 long clen;
 char *cbuf;
 while (1) {
  if (off == len) {
   clen = 0;
   while (!clen)
    if (cpp_read(&cbuf, &clen))
     return 1;
   mem_put(&tok_mem, cbuf, clen);
   buf = mem_buf(&tok_mem);
   len = mem_len(&tok_mem);
  }
  while (off < len && ((*__ctype_b_loc ())[(int) ((buf[off]))] & (unsigned short int) _ISspace))
   off++;
  if (off == len)
   continue;
  if (buf[off] == '\\' && buf[off + 1] == '\n') {
   off += 2;
   continue;
  }
  if (buf[off] == '/' && buf[off + 1] == '/') {
   while (++off < len && buf[off] != '\n')
    if (buf[off] == '\\')
     off++;
   continue;
  }
  if (buf[off] == '/' && buf[off + 1] == '*') {
   while (++off < len) {
    if (buf[off] == '*' && buf[off + 1] == '/') {
     off += 2;
     break;
    }
   }
   continue;
  }
  break;
 }
 return 0;
}

canonical_original_code:
static int skipws()
{
 long clen;
 char *cbuf;
 while (1) {
  if (off == len) {
   clen = 0;
   while (!clen)
    if (cpp_read(&cbuf, &clen))
     return 1;
   mem_put(&tok_mem, cbuf, clen);
   buf = mem_buf(&tok_mem);
   len = mem_len(&tok_mem);
  }
  while (off < len && ((*__ctype_b_loc ())[(int) ((buf[off]))] & (unsigned short) _ISspace))
   off++;
  if (off == len)
   continue;
  if (buf[off] == '\\' && buf[off + 1] == '\n') {
   off += 2;
   continue;
  }
  if (buf[off] == '/' && buf[off + 1] == '/') {
   while (++off < len && buf[off] != '\n')
    if (buf[off] == '\\')
     off++;
   continue;
  }
  if (buf[off] == '/' && buf[off + 1] == '*') {
   while (++off < len) {
    if (buf[off] == '*' && buf[off + 1] == '/') {
     off += 2;
     break;
    }
   }
   continue;
  }
  break;
 }
 return 0;
}

variable_types: clen long

variable_types:
cbuf
char *


decompiled_code:
__int64 tok_read()
{
__int64 v0; // rbx
__int64 v1; // r15
unsigned int v2; // er12
_BYTE *v3; // r14
__int64 v4; // rbp
unsigned int v5; // er13
const __int32_t **v7; // r14
__int64 v8; // r13
unsigned __int8 v9; // bp
unsigned int v10; // ebp
char *v11; // rax
char *v12; // r14
__int64 v13; // r8
__int64 v14; // rbx
unsigned __int8 *v15; // r8
__int64 v16; // rsi
__int64 v17; // rax
__int64 v18; // r13
int v19; // er14
int v20; // ebx
__int64 v21; // rsi
int v22[15]; // [rsp+Ch] [rbp-3Ch] BYREF

off_pre = off;
mem_cut(&tok, 0LL);
if ( (unsigned int)skipws() )
return 1;
v0 = off;
v1 = buf;
v2 = 0;
v3 = (_BYTE *)(buf + off);
v4 = off;
v5 = *(unsigned __int8 *)(buf + off);
if ( (_BYTE)v5 == 34 )
{
mem_putc(&tok, 34LL);
while ( 1 )
{
v13 = buf;
if ( *(_BYTE *)(buf + off) != 34 )
break;
v14 = off + 1;
off = v14;
if ( v14 < len )
{
while ( 1 )
{
v15 = (unsigned __int8 *)(v14 + v13);
v16 = *v15;
if ( (_BYTE)v16 == 34 )
break;
if ( (_BYTE)v16 == 92 )
{
off = (int)esc_char(v22, v15) + v14;
mem_putc(&tok, (unsigned int)v22[0]);
}
else
{
off = v14 + 1;
mem_putc(&tok, v16);
}
v14 = off;
if ( off >= len )
return 1;
v13 = buf;
}
off = v14 + 1;
if ( !(unsigned int)skipws() )
continue;
}
return 1;
}
mem_putc(&tok, 34LL);
}
else if ( ((*_ctype_b_loc())[(unsigned __int8)v5] & 0x800) != 0 )
{
if ( (_BYTE)v5 == 48 && (*(_BYTE *)(v1 + v0 + 1) & 0xDF) == 88 )
{
off = v0 + 1;
mem_putc(&tok, 48LL);
v17 = off++;
mem_putc(&tok, *(unsigned __int8 *)(buf + v17));
v0 = off;
}
if ( len > v0 )
{
v7 = _ctype_tolower_loc();
while ( 1 )
{
v8 = buf;
v9 = *(_BYTE *)(buf + v0);
if ( !strchr("0123456789abcdef", (*v7)[v9]) )
break;
off = v0 + 1;
mem_putc(&tok, v9);
v0 = off;
if ( off >= len )
return v2;
}
if ( len > v0 )
{
while ( 1 )
{
v10 = *(unsigned __int8 *)(v8 + v0);
if ( !strchr("uUlL", v10) )
break;
off = v0 + 1;
mem_putc(&tok, v10);
v0 = off;
if ( off >= len )
break;
v8 = buf;
}
}
}
}
else if ( (_BYTE)v5 == 39 )
{
v18 = v0 + 1;
v19 = esc_char(v22, (unsigned __int8 *)(v1 + v0 + 1)) + 2;
if ( v19 > 0 )
{
v20 = 0;
while ( 1 )
{
v21 = *(unsigned __int8 *)(v1 + v4);
++v20;
off = v18;
mem_putc(&tok, v21);
if ( v19 == v20 )
break;
v4 = off;
v1 = buf;
v18 = off + 1;
}
}
}
else
{
if ( !(unsigned int)id_char(v5) )
{
if ( v0 + 1 < len )
{
v11 = find_tok3(v3);
v12 = v11;
if ( v11 )
{
off = strlen(v11) + v0;
mem_put(&tok, v12);
return v2;
}
}
if ( strchr(";,{}()[]<>*&!=+-/%?:|^~.", v5) )
{
off = v0 + 1;
v2 = 0;
mem_putc(&tok, v5);
return v2;
}
return 1;
}
if ( v0 < len )
{
do
{
off = v4 + 1;
mem_putc(&tok, v5);
v4 = off;
if ( off >= len )
break;
v5 = *(unsigned __int8 *)(buf + off);
}
while ( (unsigned int)id_char(v5) );
}
}
return v2;
}

canonical_decompiled_code:
__int64 func4()
{
__int64 v0; // rbx
__int64 v1; // r15
unsigned int v2; // er12
_BYTE *v3; // r14
__int64 v4; // rbp
unsigned int v5; // er13
const __int32_t **v7; // r14
__int64 v8; // r13
unsigned __int8 v9; // bp
unsigned int v10; // ebp
char *v11; // rax
char *v12; // r14
__int64 v13; // r8
__int64 v14; // rbx
unsigned __int8 *v15; // r8
__int64 v16; // rsi
__int64 v17; // rax
__int64 v18; // r13
int v19; // er14
int v20; // ebx
__int64 v21; // rsi
int v22[15]; // [rsp+Ch] [rbp-3Ch] BYREF

off_pre = off;
mem_cut(&tok, 0LL);
if ( (unsigned int)func3() )
return 1;
v0 = off;
v1 = buf;
v2 = 0;
v3 = (_BYTE *)(buf + off);
v4 = off;
v5 = *(unsigned __int8 *)(buf + off);
if ( (_BYTE)v5 == 34 )
{
mem_putc(&tok, 34LL);
while ( 1 )
{
v13 = buf;
if ( *(_BYTE *)(buf + off) != 34 )
break;
v14 = off + 1;
off = v14;
if ( v14 < len )
{
while ( 1 )
{
v15 = (unsigned __int8 *)(v14 + v13);
v16 = *v15;
if ( (_BYTE)v16 == 34 )
break;
if ( (_BYTE)v16 == 92 )
{
off = (int)func2(v22, v15) + v14;
mem_putc(&tok, (unsigned int)v22[0]);
}
else
{
off = v14 + 1;
mem_putc(&tok, v16);
}
v14 = off;
if ( off >= len )
return 1;
v13 = buf;
}
off = v14 + 1;
if ( !(unsigned int)func3() )
continue;
}
return 1;
}
mem_putc(&tok, 34LL);
}
else if ( ((*_ctype_b_loc())[(unsigned __int8)v5] & 0x800) != 0 )
{
if ( (_BYTE)v5 == 48 && (*(_BYTE *)(v1 + v0 + 1) & 0xDF) == 88 )
{
off = v0 + 1;
mem_putc(&tok, 48LL);
v17 = off++;
mem_putc(&tok, *(unsigned __int8 *)(buf + v17));
v0 = off;
}
if ( len > v0 )
{
v7 = _ctype_tolower_loc();
while ( 1 )
{
v8 = buf;
v9 = *(_BYTE *)(buf + v0);
if ( !strchr("0123456789abcdef", (*v7)[v9]) )
break;
off = v0 + 1;
mem_putc(&tok, v9);
v0 = off;
if ( off >= len )
return v2;
}
if ( len > v0 )
{
while ( 1 )
{
v10 = *(unsigned __int8 *)(v8 + v0);
if ( !strchr("uUlL", v10) )
break;
off = v0 + 1;
mem_putc(&tok, v10);
v0 = off;
if ( off >= len )
break;
v8 = buf;
}
}
}
}
else if ( (_BYTE)v5 == 39 )
{
v18 = v0 + 1;
v19 = func2(v22, (unsigned __int8 *)(v1 + v0 + 1)) + 2;
if ( v19 > 0 )
{
v20 = 0;
while ( 1 )
{
v21 = *(unsigned __int8 *)(v1 + v4);
++v20;
off = v18;
mem_putc(&tok, v21);
if ( v19 == v20 )
break;
v4 = off;
v1 = buf;
v18 = off + 1;
}
}
}
else
{
if ( !(unsigned int)func1(v5) )
{
if ( v0 + 1 < len )
{
v11 = func0(v3);
v12 = v11;
if ( v11 )
{
off = strlen(v11) + v0;
mem_put(&tok, v12);
return v2;
}
}
if ( strchr(";,{}()[]<>*&!=+-/%?:|^~.", v5) )
{
off = v0 + 1;
v2 = 0;
mem_putc(&tok, v5);
return v2;
}
return 1;
}
if ( v0 < len )
{
do
{
off = v4 + 1;
mem_putc(&tok, v5);
v4 = off;
if ( off >= len )
break;
v5 = *(unsigned __int8 *)(buf + off);
}
while ( (unsigned int)func1(v5) );
}
}
return v2;
}

original_code:
static int tok_read(void)
{
 char *t3;
 int c;
 off_pre = off;
 mem_cut(&tok, 0);
 if (skipws())
  return 1;
 if (buf[off] == '"') {
  mem_putc(&tok, '"');
  while (buf[off] == '"') {
   off++;
   while (off < len && buf[off] != '"') {
    if (buf[off] == '\\') {
     off += esc_char(&c, buf + off);
     mem_putc(&tok, c);
    } else {
     mem_putc(&tok, (unsigned char) buf[off++]);
    }
   }
   if (off >= len || buf[off++] != '"')
    return 1;
   if (skipws())
    return 1;
  }
  mem_putc(&tok, '"');
  return 0;
 }
 if (((*__ctype_b_loc ())[(int) (((unsigned char) buf[off]))] & (unsigned short int) _ISdigit)) {
  if (buf[off] == '0' && (buf[off + 1] == 'x' || buf[off + 1] == 'X')) {
   mem_putc(&tok, (unsigned char) buf[off++]);
   mem_putc(&tok, (unsigned char) buf[off++]);
  }
  while (off < len && strchr(digs, tolower((unsigned char) buf[off])))
   mem_putc(&tok, (unsigned char) buf[off++]);
  while (off < len && strchr("uUlL", (unsigned char) buf[off]))
   mem_putc(&tok, (unsigned char) buf[off++]);
  return 0;
 }
 if (buf[off] == '\'') {
  int c, i;
  int n = esc_char(&c, buf + off + 1) + 1 + 1;
  for (i = 0; i < n; i++)
   mem_putc(&tok, (unsigned char) buf[off++]);
  return 0;
 }
 if (id_char((unsigned char) buf[off])) {
  while (off < len && id_char((unsigned char) buf[off]))
   mem_putc(&tok, (unsigned char) buf[off++]);
  return 0;
 }
 if (off + 2 <= len && (t3 = find_tok3(buf + off))) {
  off += strlen(t3);
  mem_put(&tok, t3, strlen(t3));
  return 0;
 }
 if (strchr(";,{}()[]<>*&!=+-/%?:|^~.", (unsigned char) buf[off])) {
  mem_putc(&tok, (unsigned char) buf[off++]);
  return 0;
 }
 return 1;
}

canonical_original_code:
static int tok_read()
{
 char *t3;
 int c;
 off_pre = off;
 mem_cut(&tok, 0);
 if (skipws())
  return 1;
 if (buf[off] == '"') {
  mem_putc(&tok, '"');
  while (buf[off] == '"') {
   off++;
   while (off < len && buf[off] != '"') {
    if (buf[off] == '\\') {
     off += esc_char(&c, buf + off);
     mem_putc(&tok, c);
    } else {
     mem_putc(&tok, (unsigned char) buf[off++]);
    }
   }
   if (off >= len || buf[off++] != '"')
    return 1;
   if (skipws())
    return 1;
  }
  mem_putc(&tok, '"');
  return 0;
 }
 if (((*__ctype_b_loc ())[(int) (((unsigned char) buf[off]))] & (unsigned short) _ISdigit)) {
  if (buf[off] == '0' && (buf[off + 1] == 'x' || buf[off + 1] == 'X')) {
   mem_putc(&tok, (unsigned char) buf[off++]);
   mem_putc(&tok, (unsigned char) buf[off++]);
  }
  while (off < len && strchr(digs, tolower((unsigned char) buf[off])))
   mem_putc(&tok, (unsigned char) buf[off++]);
  while (off < len && strchr("uUlL", (unsigned char) buf[off]))
   mem_putc(&tok, (unsigned char) buf[off++]);
  return 0;
 }
 if (buf[off] == '\'') {
  int c; int i;
  int n = esc_char(&c, buf + off + 1) + 1 + 1;
  for (i = 0; i < n; i++)
   mem_putc(&tok, (unsigned char) buf[off++]);
  return 0;
 }
 if (id_char((unsigned char) buf[off])) {
  while (off < len && id_char((unsigned char) buf[off]))
   mem_putc(&tok, (unsigned char) buf[off++]);
  return 0;
 }
 if (off + 2 <= len && (t3 = find_tok3(buf + off))) {
  off += strlen(t3);
  mem_put(&tok, t3, strlen(t3));
  return 0;
 }
 if (strchr(";,{}()[]<>*&!=+-/%?:|^~.", (unsigned char) buf[off])) {
  mem_putc(&tok, (unsigned char) buf[off++]);
  return 0;
 }
 return 1;
}

variable_types: n int


decompiled_code:
void *tok_get()
{
if ( !tok_set && (unsigned int)tok_read() )
return &unk_B3D;
tok_set = 0;
return (void *)mem_buf(&tok);
}

canonical_decompiled_code:
void *func6()
{
if ( !tok_set && (unsigned int)func4() )
return &unk_B3D;
tok_set = 0;
return (void *)mem_buf(&tok);
}

original_code:
char *tok_get(void)
{
 if (!tok_set)
  if (tok_read())
   return "";
 tok_set = 0;
 return mem_buf(&tok);
}

canonical_original_code:
char *tok_get()
{
 if (!tok_set)
  if (tok_read())
   return "";
 tok_set = 0;
 return mem_buf(&tok);
}


decompiled_code:
void *tok_see()
{
if ( !tok_set && (unsigned int)tok_read() )
return &unk_B3D;
tok_set = 1;
return (void *)mem_buf(&tok);
}

canonical_decompiled_code:
void *func7()
{
if ( !tok_set && (unsigned int)func4() )
return &unk_B3D;
tok_set = 1;
return (void *)mem_buf(&tok);
}

original_code:
char *tok_see(void)
{
 if (!tok_set)
  if (tok_read())
   return "";
 tok_set = 1;
 return mem_buf(&tok);
}

canonical_original_code:
char *tok_see()
{
 if (!tok_set)
  if (tok_read())
   return "";
 tok_set = 1;
 return mem_buf(&tok);
}


decompiled_code:
__int64 tok_len()
{
return mem_len(&tok);
}

canonical_decompiled_code:
__int64 func8()
{
return mem_len(&tok);
}

original_code:
long tok_len(void)
{
 return mem_len(&tok);
}

canonical_original_code:
long tok_len()
{
 return mem_len(&tok);
}


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

result = off_pre;
if ( !tok_set )
result = off;
return result;
}

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

result = off_pre;
if ( !tok_set )
result = off;
return result;
}

original_code:
long tok_addr(void)
{
 return tok_set ? off_pre : off;
}

canonical_original_code:
long tok_addr()
{
 return tok_set ? off_pre : off;
}


decompiled_code:
void __fastcall tok_jump(__int64 a1)
{
off = a1;
off_pre = -1LL;
tok_set = 0;
}

canonical_decompiled_code:
void __fastcall func10(__int64 a1)
{
off = a1;
off_pre = -1LL;
tok_set = 0;
}

original_code:
void tok_jump(long addr)
{
 off = addr;
 off_pre = -1;
 tok_set = 0;
}

variable_types: addr long


decompiled_code:
__int64 tok_done()
{
mem_done(&tok);
return mem_done(&tok_mem);
}

canonical_decompiled_code:
__int64 func11()
{
mem_done(&tok);
return mem_done(&tok_mem);
}

original_code:
void tok_done(void)
{
 mem_done(&tok);
 mem_done(&tok_mem);
}

canonical_original_code:
void tok_done()
{
 mem_done(&tok);
 mem_done(&tok_mem);
}


repo: litcave/neatpost

decompiled_code:
void __fastcall sbuf_extend(__int64 a1, int a2)
{
void *v2; // rbp
void *v3; // rax
size_t v4; // rdx

*(_DWORD *)(a1 + 12) = a2;
v2 = *(void **)a1;
v3 = malloc(a2);
v4 = *(int *)(a1 + 8);
*(_QWORD *)a1 = v3;
if ( (_DWORD)v4 )
memcpy(v3, v2, v4);
free(v2);
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, int a2)
{
void *v2; // rbp
void *v3; // rax
size_t v4; // rdx

*(_DWORD *)(a1 + 12) = a2;
v2 = *(void **)a1;
v3 = malloc(a2);
v4 = *(int *)(a1 + 8);
*(_QWORD *)a1 = v3;
if ( (_DWORD)v4 )
memcpy(v3, v2, v4);
free(v2);
}

original_code:
static void sbuf_extend(struct sbuf *sbuf, int newsz)
{
 char *s = sbuf->s;
 sbuf->s_sz = newsz;
 sbuf->s = malloc(sbuf->s_sz);
 if (sbuf->s_n)
  memcpy(sbuf->s, s, sbuf->s_n);
 free(s);
}

variable_types: struct sbuf { char *s; int s_n; int s_sz; } *

variable_types: newsz int

variable_types:
s
char *


decompiled_code:
void *sbuf_make()
{
return calloc(0x10uLL, 1uLL);
}

canonical_decompiled_code:
void *func1()
{
return calloc(0x10uLL, 1uLL);
}

original_code:
struct sbuf *sbuf_make(void)
{
 struct sbuf *sb = malloc(sizeof(*sb));
 memset(sb, 0, sizeof(*sb));
 return sb;
}

canonical_original_code:
struct sbuf *sbuf_make()
{
 struct sbuf *sb = malloc(sizeof(*sb));
 memset(sb, 0, sizeof(*sb));
 return sb;
}

return_type: struct sbuf { char *s; int s_n; int s_sz; } *


decompiled_code:
__int64 __fastcall sbuf_buf(int *a1)
{
if ( !*(_QWORD *)a1 )
sbuf_extend((__int64)a1, 1);
*(_BYTE *)(*(_QWORD *)a1 + a1[2]) = 0;
return *(_QWORD *)a1;
}

original_code:
char *sbuf_buf(struct sbuf *sb)
{
 if (!sb->s)
  sbuf_extend(sb, 1);
 sb->s[sb->s_n] = '\0';
 return sb->s;
}

user_defined_types: struct sbuf { char *s; int s_n; int s_sz; }


decompiled_code:
__int64 __fastcall sbuf_done(void *ptr)
{
__int64 v1; // r12

v1 = sbuf_buf((int *)ptr);
free(ptr);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func3(void *ptr)
{
__int64 v1; // r12

v1 = func2((int *)ptr);
free(ptr);
return v1;
}

original_code:
char *sbuf_done(struct sbuf *sb)
{
 char *s = sbuf_buf(sb);
 free(sb);
 return s;
}


decompiled_code:
void __fastcall sbuf_free(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
void sbuf_free(struct sbuf *sb)
{
 free(sb->s);
 free(sb);
}


decompiled_code:
void *__fastcall sbuf_mem(__int64 a1, const void *a2, int a3)
{
__int64 v6; // rdi
int v7; // eax
void *result; // rax
int v9; // esi
int v10; // eax

v6 = *(int *)(a1 + 8);
v7 = *(_DWORD *)(a1 + 12);
if ( (int)v6 + a3 + 1 >= v7 )
{
v9 = v7 + a3 + 1;
v10 = 2 * v7;
if ( v9 < v10 )
v9 = v10;
sbuf_extend(a1, (v9 + 127) & 0xFFFFFF80);
v6 = *(int *)(a1 + 8);
}
result = memcpy((void *)(*(_QWORD *)a1 + v6), a2, a3);
*(_DWORD *)(a1 + 8) += a3;
return result;
}

original_code:
void sbuf_mem(struct sbuf *sbuf, char *s, int len)
{
 if (sbuf->s_n + len + 1 >= sbuf->s_sz)
  sbuf_extend(sbuf, ((((((sbuf->s_sz) * 2) < ((sbuf->s_sz) + (len + 1)) ? ((sbuf->s_sz) + (len + 1)) : ((sbuf->s_sz) * 2))) + (128) - 1) & ~((128) - 1)));
 memcpy(sbuf->s + sbuf->s_n, s, len);
 sbuf->s_n += len;
}

variable_types: len int


decompiled_code:
void *__fastcall sbuf_str(__int64 a1, const char *a2)
{
int v2; // eax

v2 = strlen(a2);
return sbuf_mem(a1, a2, v2);
}

canonical_decompiled_code:
void *__fastcall func7(__int64 a1, const char *a2)
{
int v2; // eax

v2 = strlen(a2);
return func6(a1, a2, v2);
}

original_code:
void sbuf_str(struct sbuf *sbuf, char *s)
{
 sbuf_mem(sbuf, s, strlen(s));
}


decompiled_code:
__int64 __fastcall sbuf_len(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

original_code:
int sbuf_len(struct sbuf *sbuf)
{
 return sbuf->s_n;
}


decompiled_code:
void __fastcall sbuf_cut(__int64 a1, int a2)
{
if ( *(_DWORD *)(a1 + 8) > a2 )
*(_DWORD *)(a1 + 8) = a2;
}

original_code:
void sbuf_cut(struct sbuf *sb, int len)
{
 if (sb->s_n > len)
  sb->s_n = len;
}


decompiled_code:
void *sbuf_printf(__int64 a1, const char *a2, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-1D0h] BYREF
char s[256]; // [rsp+20h] [rbp-1B8h] BYREF

va_start(arg, a2);
vsnprintf(s, 0x100uLL, a2, arg);
return sbuf_str(a1, s);
}

canonical_decompiled_code:
void *func10(__int64 a1, const char *a2, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-1D0h] BYREF
char s[256]; // [rsp+20h] [rbp-1B8h] BYREF

va_start(arg, a2);
vsnprintf(s, 0x100uLL, a2, arg);
return func7(a1, s);
}

original_code:
void sbuf_printf(struct sbuf *sbuf, char *s, ...)
{
 char buf[256];
 va_list ap;
 __builtin_va_start(ap,s);
 vsnprintf(buf, sizeof(buf), s, ap);
 __builtin_va_end(ap);
 sbuf_str(sbuf, buf);
}

canonical_original_code:
void sbuf_printf(struct sbuf *sbuf, char *s, ...)
{
 char buf[256];
 __builtin_va_list ap;
 __builtin_va_start(ap,s);
 vsnprintf(buf, sizeof(buf), s, ap);
 __builtin_va_end(ap);
 sbuf_str(sbuf, buf);
}

variable_types:
buf
char [256]


original_code:
static void iset_extend(struct iset *iset, int cnt)
{
 iset->set = mextend(iset->set, iset->cnt, cnt, sizeof(iset->set[0]));
 iset->sz = mextend(iset->sz, iset->cnt, cnt, sizeof(iset->sz[0]));
 iset->len = mextend(iset->len, iset->cnt, cnt, sizeof(iset->len[0]));
 iset->cnt = cnt;
}

variable_types: struct iset { int **set; int *sz; int *len; int cnt; } *

variable_types: cnt int


decompiled_code:
void *iset_make()
{
void *v0; // r12

v0 = calloc(0x20uLL, 1uLL);
iset_extend((__int64)v0, 1024);
return v0;
}

canonical_decompiled_code:
void *func1()
{
void *v0; // r12

v0 = calloc(0x20uLL, 1uLL);
func0((__int64)v0, 1024);
return v0;
}

original_code:
struct iset *iset_make(void)
{
 struct iset *iset = malloc(sizeof(*iset));
 memset(iset, 0, sizeof(*iset));
 iset_extend(iset, (1 << 10));
 return iset;
}

canonical_original_code:
struct iset *iset_make()
{
 struct iset *iset = malloc(sizeof(*iset));
 memset(iset, 0, sizeof(*iset));
 iset_extend(iset, (1 << 10));
 return iset;
}

return_type: struct iset { int **set; int *sz; int *len; int cnt; } *


decompiled_code:
void __fastcall iset_free(void *ptr)
{
__int64 v2; // rbx
void *v3; // rdi

if ( *((int *)ptr + 6) > 0 )
{
v2 = 0LL;
do
{
v3 = *(void **)(*(_QWORD *)ptr + 8 * v2++);
free(v3);
}
while ( *((_DWORD *)ptr + 6) > (int)v2 );
}
free(*(void **)ptr);
free(*((void **)ptr + 2));
free(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
__int64 v2; // rbx
void *v3; // rdi

if ( *((int *)ptr + 6) > 0 )
{
v2 = 0LL;
do
{
v3 = *(void **)(*(_QWORD *)ptr + 8 * v2++);
free(v3);
}
while ( *((_DWORD *)ptr + 6) > (int)v2 );
}
free(*(void **)ptr);
free(*((void **)ptr + 2));
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void iset_free(struct iset *iset)
{
 int i;
 for (i = 0; i < iset->cnt; i++)
  free(iset->set[i]);
 free(iset->set);
 free(iset->len);
 free(iset->sz);
 free(iset);
}

variable_types: i int

user_defined_types: struct iset { int **set; int *sz; int *len; int cnt; }


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

result = 0LL;
if ( a2 >= 0 && *(_DWORD *)(a1 + 24) > a2 )
result = *(_QWORD *)(*(_QWORD *)a1 + 8LL * a2);
return result;
}

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

result = 0LL;
if ( a2 >= 0 && *(_DWORD *)(a1 + 24) > a2 )
result = *(_QWORD *)(*(_QWORD *)a1 + 8LL * a2);
return result;
}

original_code:
int *iset_get(struct iset *iset, int key)
{
 return key >= 0 && key < iset->cnt ? iset->set[key] : ((void *)0);
}

variable_types: key int

return_type: int *


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

result = 0LL;
if ( a2 >= 0 && *(_DWORD *)(a1 + 24) > a2 )
result = *(unsigned int *)(*(_QWORD *)(a1 + 16) + 4LL * a2);
return result;
}

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

result = 0LL;
if ( a2 >= 0 && *(_DWORD *)(a1 + 24) > a2 )
result = *(unsigned int *)(*(_QWORD *)(a1 + 16) + 4LL * a2);
return result;
}

original_code:
int iset_len(struct iset *iset, int key)
{
 return key >= 0 && key < iset->cnt ? iset->len[key] : 0;
}


original_code:
void iset_put(struct iset *iset, int key, int ent)
{
 if (key < 0 || key >= (1 << 20))
  return;
 if (key >= iset->cnt)
  iset_extend(iset, (((key + 1) + ((1 << 10)) - 1) & ~(((1 << 10)) - 1)));
 if (key >= 0 && key < iset->cnt && iset->len[key] + 1 >= iset->sz[key]) {
  int olen = iset->sz[key];
  int nlen = iset->sz[key] * 2 + 8;
  void *nset = malloc(nlen * sizeof(iset->set[key][0]));
  if (iset->set[key]) {
   memcpy(nset, iset->set[key],
    olen * sizeof(iset->set[key][0]));
   free(iset->set[key]);
  }
  iset->sz[key] = nlen;
  iset->set[key] = nset;
 }
 iset->set[key][iset->len[key]++] = ent;
 iset->set[key][iset->len[key]] = -1;
}

variable_types: ent int

variable_types: olen int

variable_types: nlen int

variable_types:
nset
void *


startswith

decompiled_code:
__int64 __fastcall startswith(__int64 a1, __int64 a2)
{
__int64 v2; // rax
char v3; // cl
char v4; // dl

v2 = 0LL;
do
{
v4 = *(_BYTE *)(a1 + v2);
if ( !v4 )
return 1LL;
v3 = *(_BYTE *)(a2 + v2);
if ( !v3 )
return 1LL;
++v2;
}
while ( v4 == v3 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 v2; // rax
char v3; // cl
char v4; // dl

v2 = 0LL;
do
{
v4 = *(_BYTE *)(a1 + v2);
if ( !v4 )
return 1LL;
v3 = *(_BYTE *)(a2 + v2);
if ( !v3 )
return 1LL;
++v2;
}
while ( v4 == v3 );
return 0LL;
}

original_code:
static int startswith(char *s, char *t)
{
 while (*s && *t)
  if (*s++ != *t++)
   return 0;
 return 1;
}

variable_types:
t
char *


nextline

decompiled_code:
__int64 __fastcall nextline(__int64 a1, int a2, int a3)
{
void *v3; // rax
__int64 result; // rax

v3 = memchr((const void *)(a3 + a1), 10, a2 - a3);
if ( v3 )
result = (unsigned int)((_DWORD)v3 - a1 + 1);
else
result = 0xFFFFFFFFLL;
return result;
}

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

v3 = memchr((const void *)(a3 + a1), 10, a2 - a3);
if ( v3 )
result = (unsigned int)((_DWORD)v3 - a1 + 1);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
static int nextline(char *pdf, int len, int off)
{
 char *nl = memchr(pdf + off, '\n', len - off);
 if (nl)
  return nl - pdf + 1;
 return -1;
}

variable_types:
pdf
char *

variable_types: off int

variable_types:
nl
char *


decompiled_code:
__int64 __fastcall pdf_xref(_BYTE *a1, int a2)
{
int v2; // eax
int v3; // eax
__int64 result; // rax
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = prevline_constprop_0(a1, a2);
v3 = prevline_constprop_0(a1, v2);
v5[0] = v3;
if ( v3 >= 0 && (unsigned int)_isoc99_sscanf(&a1[v3], &unk_AAF, v5) == 1 && v5[0] >= 0 && v5[0] < a2 )
result = nextline((__int64)a1, a2, v5[0]);
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_BYTE *a1, int a2)
{
int v2; // eax
int v3; // eax
__int64 result; // rax
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = func2(a1, a2);
v3 = func2(a1, v2);
v5[0] = v3;
if ( v3 >= 0 && (unsigned int)_isoc99_sscanf(&a1[v3], &unk_AAF, v5) == 1 && v5[0] >= 0 && v5[0] < a2 )
result = func1((__int64)a1, a2, v5[0]);
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
static int pdf_xref(char *pdf, int len)
{
 int pos = prevline(pdf, len, len);
 if ((pos = prevline(pdf, len, pos)) < 0)
  return -1;
 if (sscanf(pdf + pos, "%d", &pos) != 1 || pos >= len || pos < 0)
  return -1;
 return nextline(pdf, len, pos);
}

variable_types: pos int


decompiled_code:
__int64 __fastcall pdf_ws(__int64 a1, int a2, int a3)
{
const unsigned __int16 *v4; // rcx
unsigned __int8 *v5; // rdi
int v6; // eax

if ( a3 >= a2 )
return 0LL;
v4 = *_ctype_b_loc();
v5 = (unsigned __int8 *)(a1 + a3);
v6 = a3;
do
{
if ( (v4[*v5] & 0x2000) == 0 )
return (unsigned int)(v6 - a3);
++v6;
++v5;
}
while ( a2 != v6 );
return (unsigned int)(a2 - a3);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2, int a3)
{
const unsigned __int16 *v4; // rcx
unsigned __int8 *v5; // rdi
int v6; // eax

if ( a3 >= a2 )
return 0LL;
v4 = *_ctype_b_loc();
v5 = (unsigned __int8 *)(a1 + a3);
v6 = a3;
do
{
if ( (v4[*v5] & 0x2000) == 0 )
return (unsigned int)(v6 - a3);
++v6;
++v5;
}
while ( a2 != v6 );
return (unsigned int)(a2 - a3);
}

original_code:
int pdf_ws(char *pdf, int len, int pos)
{
 int i = pos;
 while (i < len && ((*__ctype_b_loc ())[(int) (((unsigned char) pdf[i]))] & (unsigned short int) _ISspace))
  i++;
 return i - pos;
}

canonical_original_code:
int pdf_ws(char *pdf, int len, int pos)
{
 int i = pos;
 while (i < len && ((*__ctype_b_loc ())[(int) (((unsigned char) pdf[i]))] & (unsigned short) _ISspace))
  i++;
 return i - pos;
}


original_code:
int pdf_type(char *pdf, int len, int pos)
{
 pos += pdf_ws(pdf, len, pos);
 if (pdf[pos] == '/')
  return '/';
 if (pdf[pos] == '(')
  return 's';
 if (pdf[pos] == '<' && pdf[pos + 1] != '<')
  return 's';
 if (pdf[pos] == '<' && pdf[pos + 1] == '<')
  return 'd';
 if (pdf[pos] == '[')
  return 'l';
 if (strchr("0123456789+-.", (unsigned char) pdf[pos])) {
  if (!((*__ctype_b_loc ())[(int) (((unsigned char) pdf[pos]))] & (unsigned short int) _ISdigit))
   return 'n';
  while (pos < len && ((*__ctype_b_loc ())[(int) (((unsigned char) pdf[pos]))] & (unsigned short int) _ISdigit))
   pos++;
  pos += pdf_ws(pdf, len, pos);
  if (!((*__ctype_b_loc ())[(int) (((unsigned char) pdf[pos]))] & (unsigned short int) _ISdigit))
   return 'n';
  while (pos < len && ((*__ctype_b_loc ())[(int) (((unsigned char) pdf[pos]))] & (unsigned short int) _ISdigit))
   pos++;
  pos += pdf_ws(pdf, len, pos);
  return pos < len && pdf[pos] == 'R' ? 'r' : 'n';
 }
 return -1;
}

canonical_original_code:
int pdf_type(char *pdf, int len, int pos)
{
 pos += pdf_ws(pdf, len, pos);
 if (pdf[pos] == '/')
  return '/';
 if (pdf[pos] == '(')
  return 's';
 if (pdf[pos] == '<' && pdf[pos + 1] != '<')
  return 's';
 if (pdf[pos] == '<' && pdf[pos + 1] == '<')
  return 'd';
 if (pdf[pos] == '[')
  return 'l';
 if (strchr("0123456789+-.", (unsigned char) pdf[pos])) {
  if (!((*__ctype_b_loc ())[(int) (((unsigned char) pdf[pos]))] & (unsigned short) _ISdigit))
   return 'n';
  while (pos < len && ((*__ctype_b_loc ())[(int) (((unsigned char) pdf[pos]))] & (unsigned short) _ISdigit))
   pos++;
  pos += pdf_ws(pdf, len, pos);
  if (!((*__ctype_b_loc ())[(int) (((unsigned char) pdf[pos]))] & (unsigned short) _ISdigit))
   return 'n';
  while (pos < len && ((*__ctype_b_loc ())[(int) (((unsigned char) pdf[pos]))] & (unsigned short) _ISdigit))
   pos++;
  pos += pdf_ws(pdf, len, pos);
  return pos < len && pdf[pos] == 'R' ? 'r' : 'n';
 }
 return -1;
}


original_code:
int pdf_len(char *pdf, int len, int pos)
{
 int c;
 int old = pos;
 if (pos >= len)
  return 0;
 pos += pdf_ws(pdf, len, pos);
 c = (unsigned char) pdf[pos];
 if (strchr("0123456789+-.", c)) {
  if (pdf_type(pdf, len, pos) == 'r') {
   char *r = memchr(pdf + pos, 'R', len - pos);
   return r - (pdf + old) + 1;
  }
  pos++;
  while (pos < len && strchr("0123456789.", (unsigned char) pdf[pos]))
   pos++;
 }
 if (c == '(') {
  int depth = 1;
  pos++;
  while (pos < len && depth > 0) {
   if (pdf[pos] == '(')
    depth++;
   if (pdf[pos] == ')')
    depth--;
   if (pdf[pos] == '\\')
    pos++;
   pos++;
  }
 }
 if (c == '<' && pos + 1 < len && pdf[pos + 1] == '<') {
  pos += 2;
  while (pos + 2 < len && (pdf[pos] != '>' || pdf[pos + 1] != '>')) {
   pos += pdf_len(pdf, len, pos);
   pos += pdf_len(pdf, len, pos);
   pos += pdf_ws(pdf, len, pos);
  }
  if (pos + 2 < len)
   pos += 2;
 } else if (c == '<') {
  while (pos < len && pdf[pos] != '>')
   pos++;
  if (pos < len)
   pos++;
 }
 if (c == '/') {
  pos++;
  while (pos < len && !strchr(" \t\r\n\f()<>[]{}/%",
     (unsigned char) pdf[pos]))
   pos++;
 }
 if (c == '[') {
  pos++;
  while (pos < len && pdf[pos] != ']') {
   pos += pdf_len(pdf, len, pos);
   pos += pdf_ws(pdf, len, pos);
  }
  pos++;
 }
 return pos - old;
}

variable_types: c int

variable_types: old int

variable_types:
r
char *

variable_types: depth int


decompiled_code:
__int64 __fastcall pdf_obj(__int64 a1, int a2, int a3, int *a4, int *a5)
{
int v8; // eax

if ( (unsigned int)pdf_type(a1, a2, a3) != 114 )
return 0xFFFFFFFFLL;
*a4 = atoi((const char *)(a1 + a3));
v8 = pdf_len(a1, a2, a3);
*a5 = atoi((const char *)(a1 + a3 + v8));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, int a2, int a3, int *a4, int *a5)
{
int v8; // eax

if ( (unsigned int)func5(a1, a2, a3) != 114 )
return 0xFFFFFFFFLL;
*a4 = atoi((const char *)(a1 + a3));
v8 = func6(a1, a2, a3);
*a5 = atoi((const char *)(a1 + a3 + v8));
return 0LL;
}

original_code:
int pdf_obj(char *pdf, int len, int pos, int *obj, int *rev)
{
 if (pdf_type(pdf, len, pos) != 'r')
  return -1;
 *obj = atoi(pdf + pos);
 pos += pdf_len(pdf, len, pos);
 *rev = atoi(pdf + pos);
 return 0;
}

variable_types: len int

variable_types:
obj
int *

variable_types:
rev
int *


decompiled_code:
__int64 __fastcall pdf_dval(__int64 a1, int a2, int a3, const char *a4)
{
int v5; // ebx
int v6; // ebx
int v7; // ebx
int v8; // er14

v5 = a3 + 2;
if ( a2 > a3 + 4 )
{
while ( *(_BYTE *)(a1 + v5) != 62 || *(_BYTE *)(a1 + v5 + 1) != 62 )
{
v7 = pdf_ws(a1, a2, v5) + v5;
v8 = pdf_len(a1, a2, v7);
if ( v8 == strlen(a4) && (unsigned int)startswith((__int64)a4, a1 + v7) )
return v8 + v7 + (unsigned int)pdf_ws(a1, a2, v8 + v7);
v6 = pdf_len(a1, a2, v8 + v7) + v8 + v7;
v5 = pdf_ws(a1, a2, v6) + v6;
if ( v5 + 2 >= a2 )
return 0xFFFFFFFFLL;
}
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, int a2, int a3, const char *a4)
{
int v5; // ebx
int v6; // ebx
int v7; // ebx
int v8; // er14

v5 = a3 + 2;
if ( a2 > a3 + 4 )
{
while ( *(_BYTE *)(a1 + v5) != 62 || *(_BYTE *)(a1 + v5 + 1) != 62 )
{
v7 = func4(a1, a2, v5) + v5;
v8 = func6(a1, a2, v7);
if ( v8 == strlen(a4) && (unsigned int)func0((__int64)a4, a1 + v7) )
return v8 + v7 + (unsigned int)func4(a1, a2, v8 + v7);
v6 = func6(a1, a2, v8 + v7) + v8 + v7;
v5 = func4(a1, a2, v6) + v6;
if ( v5 + 2 >= a2 )
return 0xFFFFFFFFLL;
}
}
return 0xFFFFFFFFLL;
}

original_code:
int pdf_dval(char *pdf, int len, int pos, char *key)
{
 pos += 2;
 while (pos + 2 < len && (pdf[pos] != '>' || pdf[pos + 1] != '>')) {
  pos += pdf_ws(pdf, len, pos);
  if (pdf_len(pdf, len, pos) == strlen(key) && startswith(key, pdf + pos)) {
   pos += pdf_len(pdf, len, pos);
   pos += pdf_ws(pdf, len, pos);
   return pos;
  }
  pos += pdf_len(pdf, len, pos);
  pos += pdf_len(pdf, len, pos);
  pos += pdf_ws(pdf, len, pos);
 }
 return -1;
}

variable_types:
key
char *


original_code:
int pdf_dkey(char *pdf, int len, int pos, int key)
{
 int i = 0;
 pos += 2;
 while (pos + 2 < len && (pdf[pos] != '>' || pdf[pos + 1] != '>')) {
  pos += pdf_ws(pdf, len, pos);
  if (i++ == key)
   return pos;
  pos += pdf_len(pdf, len, pos);
  pos += pdf_len(pdf, len, pos);
  pos += pdf_ws(pdf, len, pos);
 }
 return -1;
}

variable_types: key int

variable_types: i int


decompiled_code:
__int64 __fastcall pdf_lval(__int64 a1, int a2, int a3, int a4)
{
int v4; // er12
int v6; // er14
int v7; // er12

v4 = a3 + 1;
if ( a3 + 1 < a2 )
{
v6 = 0;
do
{
if ( *(_BYTE *)(a1 + v4) == 93 )
break;
if ( a4 == v6 )
return (unsigned int)v4;
++v6;
v7 = pdf_len(a1, a2, v4) + v4;
v4 = pdf_ws(a1, a2, v7) + v7;
}
while ( a2 > v4 );
}
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, int a2, int a3, int a4)
{
int v4; // er12
int v6; // er14
int v7; // er12

v4 = a3 + 1;
if ( a3 + 1 < a2 )
{
v6 = 0;
do
{
if ( *(_BYTE *)(a1 + v4) == 93 )
break;
if ( a4 == v6 )
return (unsigned int)v4;
++v6;
v7 = func6(a1, a2, v4) + v4;
v4 = func4(a1, a2, v7) + v7;
}
while ( a2 > v4 );
}
return (unsigned int)-1;
}

original_code:
int pdf_lval(char *pdf, int len, int pos, int idx)
{
 int i = 0;
 pos++;
 while (pos < len && pdf[pos] != ']') {
  if (i++ == idx)
   return pos;
  pos += pdf_len(pdf, len, pos);
  pos += pdf_ws(pdf, len, pos);
 }
 return -1;
}

variable_types: idx int


decompiled_code:
__int64 __fastcall pdf_trailer(_BYTE *a1, int a2)
{
int v2; // er8
__int64 v3; // r9
int v4; // er8
_BYTE *v5; // r9
int v6; // er10

v2 = prevline_constprop_0(a1, a2);
do
{
if ( (unsigned int)startswith(v3 + v2, (__int64)"trailer") )
return nextline((__int64)v5, v6, v4);
v2 = prevline_constprop_0(v5, v4);
}
while ( v2 >= 0 );
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func11(_BYTE *a1, int a2)
{
int v2; // er8
__int64 v3; // r9
int v4; // er8
_BYTE *v5; // r9
int v6; // er10

v2 = func2(a1, a2);
do
{
if ( (unsigned int)func0(v3 + v2, (__int64)"trailer") )
return func1((__int64)v5, v6, v4);
v2 = func2(v5, v4);
}
while ( v2 >= 0 );
return 0xFFFFFFFFLL;
}

original_code:
int pdf_trailer(char *pdf, int len)
{
 int pos = prevline(pdf, len, len);
 while (!startswith(pdf + pos, "trailer"))
  if ((pos = prevline(pdf, len, pos)) < 0)
   return -1;
 return nextline(pdf, len, pos);
}