#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ stderr;
int MightBeTrueType(FILE *binary, int pos, int dlen, char *filename) {
  FILE *out = fopen(filename, "w");
  char *buffer = malloc(8192);
  int len;
  if (out == NULL) {
    fprintf(stderr, "Could not open %s for writing\n", filename);
    exit(1);
  }
  fseek(binary, pos, SEEK_SET);
  while (dlen > 0) {
    len = dlen > 8192 ? 8192 : dlen;
    len = fread(buffer, 1, dlen > 8192 ? 8192 : dlen, binary);
    if (len == 0)
      break;
    fwrite(buffer, 1, len, out);
    dlen -= len;
  }
  fclose(out);
  free(buffer);
  return (1);
}


x86 assembly (clang, -O0):
<MightBeTrueType>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe4f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    $0x2000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f6 <MightBeTrueType+0x66>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe1b(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
cmpl   $0x0,-0x10(%rbp)
jle    12af <MightBeTrueType+0x11f>
cmpl   $0x2000,-0x10(%rbp)
jle    122f <MightBeTrueType+0x9f>
mov    $0x2000,%eax
mov    %eax,-0x30(%rbp)
jmp    1235 <MightBeTrueType+0xa5>
mov    -0x10(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x2000,-0x10(%rbp)
jle    125d <MightBeTrueType+0xcd>
mov    $0x2000,%eax
mov    %eax,-0x3c(%rbp)
jmp    1263 <MightBeTrueType+0xd3>
mov    -0x10(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    128a <MightBeTrueType+0xfa>
jmp    12af <MightBeTrueType+0x11f>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
jmp    120b <MightBeTrueType+0x7b>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MightBeTrueType>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r13
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xe54(%rip),%rsi
mov    %rcx,%rdi
call   1090 <fopen@plt>
mov    %rax,%r15
mov    $0x2000,%edi
call   1070 <malloc@plt>
test   %r15,%r15
je     123e <MightBeTrueType+0xae>
mov    %rax,%r12
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %ebp,%esi
call   1080 <fseek@plt>
test   %ebx,%ebx
jle    121a <MightBeTrueType+0x8a>
cmp    $0x2000,%ebx
mov    $0x2000,%edx
cmovb  %ebx,%edx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1040 <fread@plt>
test   %eax,%eax
je     121a <MightBeTrueType+0x8a>
mov    %eax,%ebp
mov    %r12,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r15,%rcx
call   10b0 <fwrite@plt>
sub    %ebp,%ebx
jg     11e0 <MightBeTrueType+0x50>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    0xdb4(%rip),%rsi
mov    %r13,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"Could not open %s for writing\0A\00"
@SEEK_SET = global i32 0
define i32 @MightBeTrueType(i32* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%12 = load i8*, i8** %8
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %9
%14 = call i8* @malloc(i32 8192)
store i8* %14, i8** %10
%15 = load i32*, i32** %9
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %22
17: ; preds = %4
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %8
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %4
%23 = load i32*, i32** %5
%24 = load i32, i32* %6
%25 = load i32, i32* @SEEK_SET
%26 = call i32 @fseek(i32* %23, i32 %24, i32 %25)
br label %27
27: ; preds = %51, %22
%28 = load i32, i32* %7
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %59
30: ; preds = %27
%31 = load i32, i32* %7
%32 = icmp sgt i32 %31, 8192
br i1 %32, label %33, label %34
33: ; preds = %30
br label %36
34: ; preds = %30
%35 = load i32, i32* %7
br label %36
36: ; preds = %34, %33
%37 = phi i32 [ 8192, %33 ], [ %35, %34 ]
store i32 %37, i32* %11
%38 = load i8*, i8** %10
%39 = load i32, i32* %7
%40 = icmp sgt i32 %39, 8192
br i1 %40, label %41, label %42
41: ; preds = %36
br label %44
42: ; preds = %36
%43 = load i32, i32* %7
br label %44
44: ; preds = %42, %41
%45 = phi i32 [ 8192, %41 ], [ %43, %42 ]
%46 = load i32*, i32** %5
%47 = call i32 @fread(i8* %38, i32 1, i32 %45, i32* %46)
store i32 %47, i32* %11
%48 = load i32, i32* %11
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %44
br label %59
51: ; preds = %44
%52 = load i8*, i8** %10
%53 = load i32, i32* %11
%54 = load i32*, i32** %9
%55 = call i32 @fwrite(i8* %52, i32 1, i32 %53, i32* %54)
%56 = load i32, i32* %11
%57 = load i32, i32* %7
%58 = sub nsw i32 %57, %56
store i32 %58, i32* %7
br label %27
59: ; preds = %50, %27
%60 = load i32*, i32** %9
%61 = call i32 @fclose(i32* %60)
%62 = load i8*, i8** %10
%63 = call i32 @free(i8* %62)
ret i32 1
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"Could not open %s for writing\0A\00"
@SEEK_SET = global i32 0
define i32 @MightBeTrueType(i32* nocapture %0, i32 %1, i32 %2, i8* %3) {
%5 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = tail call dereferenceable_or_null(8192) i8* @malloc(i32 8192)
%7 = icmp eq i32* %5, null
br i1 %7, label %8, label %12
8: ; preds = %4
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %4
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @fseek(i32* %0, i32 %1, i32 %13)
%15 = icmp sgt i32 %2, 0
br i1 %15, label %16, label %26
16: ; preds = %12, %22
%17 = phi i32 [ %24, %22 ], [ %2, %12 ]
%18 = icmp ult i32 %17, 8192
%19 = select i1 %18, i32 %17, i32 8192
%20 = tail call i32 @fread(i8* %6, i32 1, i32 %19, i32* %0)
%21 = icmp eq i32 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %16
%23 = tail call i32 @fwrite(i8* %6, i32 1, i32 %20, i32* %5)
%24 = sub nsw i32 %17, %20
%25 = icmp sgt i32 %24, 0
br i1 %25, label %16, label %26
26: ; preds = %22, %16, %12
%27 = tail call i32 @fclose(i32* %5)
%28 = tail call i32 @free(i8* %6)
ret i32 1
}