#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;
unsigned int WriteFile(char *, unsigned char *, unsigned int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
unsigned char *malloc(unsigned int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, unsigned int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(char *, char *);
unsigned int strtol(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ usage(char *);
int main(int argc, char *argv[]) {
  unsigned char *input, *output;
  unsigned int input_size = 0;
  FILE *f;
  int res = 0;
  char *outfile;
  if (argc < 2 || argc > 6) {
    usage(argv[0]);
    return -1;
  }
  if (strcmp(argv[1], "-l") == 0) {
    argv++;
    argc--;
    input_size = strtol(argv[1], NULL, 16);
    printf("Input size: 0x%08X\n", input_size);
    argv++;
    argc--;
  }
  if (input_size < 0x4000)
    input_size = 0x4000;
  f = fopen(argv[1], "rb");
  if (!f) {
    printf("Cannot open %s\n", argv[1]);
    return -1;
  }
  fseek(f, 0, SEEK_SET);
  output = malloc(input_size);
  if (!output) {
    printf("Cannot allocate memory for output buffer.\n");
    return -1;
  }
  memset(output, 0, input_size);
  fread(output, 1, 0x1000, f);
  fclose(f);
  f = fopen(argv[2], "rb");
  if (!f) {
    printf("Cannot open %s\n", argv[2]);
    free(output);
    return -1;
  }
  fseek(f, 0, SEEK_SET);
  fread(output + 0x1000, 1, 0x3000, f);
  fclose(f);
  if (WriteFile(argv[3], output, input_size) != input_size) {
    printf("Error writing file %s.\n", outfile);
    res = -1;
  }
  free(output);
  return res;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11f8 <main+0x38>
cmpl   $0x6,-0x8(%rbp)
jle    1210 <main+0x50>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10d0 <usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    141c <main+0x25c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xde1(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1282 <main+0xc2>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1090 <strtol@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x4000,-0x24(%rbp)
jae    1296 <main+0xd6>
movl   $0x4000,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd72(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12db <main+0x11b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd52(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    141c <main+0x25c>
mov    -0x30(%rbp),%rdi
mov    0x2cfa(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x24(%rbp),%edi
call   10b0 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1320 <main+0x160>
lea    0xd1d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    141c <main+0x25c>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xcba(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    139c <main+0x1dc>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xc9a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    141c <main+0x25c>
mov    -0x30(%rbp),%rdi
mov    0x2c39(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x20(%rbp),%rdi
add    $0x1000,%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3000,%edx
call   1040 <fread@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   10a0 <WriteFile@plt>
cmp    -0x24(%rbp),%eax
je     140d <main+0x24d>
mov    -0x40(%rbp),%rsi
lea    0xc56(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
add    $0xfffffff9,%edi
cmp    $0xfffffffa,%edi
ja     11f3 <main+0x23>
mov    (%rbx),%rdi
call   10e0 <usage@plt>
jmp    1372 <main+0x1a2>
mov    0x8(%rbx),%rdi
lea    0xe02(%rip),%rsi
call   1090 <strcmp@plt>
mov    $0x4000,%r15d
test   %rax,%rax
jne    1244 <main+0x74>
mov    0x10(%rbx),%rdi
add    $0x10,%rbx
xor    %esi,%esi
mov    $0x10,%edx
call   10a0 <strtol@plt>
mov    %eax,%ebp
lea    0xdd8(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
cmp    $0x4001,%ebp
mov    $0x4000,%r15d
cmovae %ebp,%r15d
mov    0x8(%rbx),%rdi
lea    0xdc8(%rip),%rsi
call   10f0 <fopen@plt>
test   %rax,%rax
je     1336 <main+0x166>
mov    %rax,%rbp
mov    0x2d79(%rip),%r13
mov    0x0(%r13),%edx
mov    %rax,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %r15d,%edi
call   10c0 <malloc@plt>
test   %rax,%rax
je     134a <main+0x17a>
mov    %rax,%r14
xor    %r12d,%r12d
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1080 <memset@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rbp,%rcx
call   1050 <fread@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    0x10(%rbx),%rdi
lea    0xd56(%rip),%rsi
call   10f0 <fopen@plt>
test   %rax,%rax
je     1358 <main+0x188>
mov    %rax,%rbp
mov    0x0(%r13),%edx
mov    %rax,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %r14,%rdi
add    $0x1000,%rdi
mov    $0x1,%esi
mov    $0x3000,%edx
mov    %rbp,%rcx
call   1050 <fread@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    0x18(%rbx),%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   10b0 <WriteFile@plt>
cmp    %r15d,%eax
je     132c <main+0x15c>
lea    0xd0b(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%r12d
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1378 <main+0x1a8>
mov    0x8(%rbx),%rsi
lea    0xcd9(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1372 <main+0x1a2>
lea    0xcf1(%rip),%rdi
call   1040 <puts@plt>
jmp    1372 <main+0x1a2>
mov    0x10(%rbx),%rsi
lea    0xcb7(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"-l\00"
@.str.1 = [20 x i8] c"Input size: 0x%08X\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [16 x i8] c"Cannot open %s\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [43 x i8] c"Cannot allocate memory for output buffer.\0A\00"
@.str.5 = [24 x i8] c"Error writing file %s.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
store i32 0, i32* %10
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 6
br i1 %16, label %17, label %22
17: ; preds = %14, %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @usage(i8* %20)
store i32 -1, i32* %3
br label %116
22: ; preds = %14
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %43
28: ; preds = %22
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i32 1
store i8** %30, i8*** %5
%31 = load i32, i32* %4
%32 = add nsw i32 %31, -1
store i32 %32, i32* %4
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32 @strtol(i8* %35, i32* null, i32 16)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i32 1
store i8** %40, i8*** %5
%41 = load i32, i32* %4
%42 = add nsw i32 %41, -1
store i32 %42, i32* %4
br label %43
43: ; preds = %28, %22
%44 = load i32, i32* %8
%45 = icmp ult i32 %44, 16384
br i1 %45, label %46, label %47
46: ; preds = %43
store i32 16384, i32* %8
br label %47
47: ; preds = %46, %43
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = call i32* @fopen(i8* %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %51, i32** %9
%52 = load i32*, i32** %9
%53 = icmp ne i32* %52, null
br i1 %53, label %59, label %54
54: ; preds = %47
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 1
%57 = load i8*, i8** %56
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %57)
store i32 -1, i32* %3
br label %116
59: ; preds = %47
%60 = load i32*, i32** %9
%61 = load i32, i32* @SEEK_SET
%62 = call i32 @fseek(i32* %60, i32 0, i32 %61)
%63 = load i32, i32* %8
%64 = call i8* @malloc(i32 %63)
store i8* %64, i8** %7
%65 = load i8*, i8** %7
%66 = icmp ne i8* %65, null
br i1 %66, label %69, label %67
67: ; preds = %59
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %116
69: ; preds = %59
%70 = load i8*, i8** %7
%71 = load i32, i32* %8
%72 = call i32 @memset(i8* %70, i32 0, i32 %71)
%73 = load i8*, i8** %7
%74 = load i32*, i32** %9
%75 = call i32 @fread(i8* %73, i32 1, i32 4096, i32* %74)
%76 = load i32*, i32** %9
%77 = call i32 @fclose(i32* %76)
%78 = load i8**, i8*** %5
%79 = getelementptr inbounds i8*, i8** %78, i64 2
%80 = load i8*, i8** %79
%81 = call i32* @fopen(i8* %80, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %81, i32** %9
%82 = load i32*, i32** %9
%83 = icmp ne i32* %82, null
br i1 %83, label %91, label %84
84: ; preds = %69
%85 = load i8**, i8*** %5
%86 = getelementptr inbounds i8*, i8** %85, i64 2
%87 = load i8*, i8** %86
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %87)
%89 = load i8*, i8** %7
%90 = call i32 @free(i8* %89)
store i32 -1, i32* %3
br label %116
91: ; preds = %69
%92 = load i32*, i32** %9
%93 = load i32, i32* @SEEK_SET
%94 = call i32 @fseek(i32* %92, i32 0, i32 %93)
%95 = load i8*, i8** %7
%96 = getelementptr inbounds i8, i8* %95, i64 4096
%97 = load i32*, i32** %9
%98 = call i32 @fread(i8* %96, i32 1, i32 12288, i32* %97)
%99 = load i32*, i32** %9
%100 = call i32 @fclose(i32* %99)
%101 = load i8**, i8*** %5
%102 = getelementptr inbounds i8*, i8** %101, i64 3
%103 = load i8*, i8** %102
%104 = load i8*, i8** %7
%105 = load i32, i32* %8
%106 = call i32 @WriteFile(i8* %103, i8* %104, i32 %105)
%107 = load i32, i32* %8
%108 = icmp ne i32 %106, %107
br i1 %108, label %109, label %112
109: ; preds = %91
%110 = load i8*, i8** %11
%111 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %110)
store i32 -1, i32* %10
br label %112
112: ; preds = %109, %91
%113 = load i8*, i8** %7
%114 = call i32 @free(i8* %113)
%115 = load i32, i32* %10
store i32 %115, i32* %3
br label %116
116: ; preds = %112, %84, %67, %54, %17
%117 = load i32, i32* %3
ret i32 %117
}

LLVM IR (-O2):
@.str = [3 x i8] c"-l\00"
@.str.1 = [20 x i8] c"Input size: 0x%08X\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [16 x i8] c"Cannot open %s\0A\00"
@SEEK_SET = global i32 0
@.str.5 = [24 x i8] c"Error writing file %s.\0A\00"
@str = [42 x i8] c"Cannot allocate memory for output buffer.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add i32 %0, -7
%4 = icmp ult i32 %3, -5
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 @usage(i8* %6)
br label %64
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %8
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32 @strtol(i8* nocapture %15, i32* null, i32 16)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = icmp ugt i32 %16, 16384
%19 = select i1 %18, i32 %16, i32 16384
br label %20
20: ; preds = %13, %8
%21 = phi i8** [ %1, %8 ], [ %14, %13 ]
%22 = phi i32 [ 16384, %8 ], [ %19, %13 ]
%23 = getelementptr inbounds i8*, i8** %21, i64 1
%24 = load i8*, i8** %23
%25 = tail call i32* @fopen(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i8*, i8** %23
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %28)
br label %64
30: ; preds = %20
%31 = load i32, i32* @SEEK_SET
%32 = tail call i32 @fseek(i32* %25, i32 0, i32 %31)
%33 = tail call i8* @malloc(i32 %22)
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
br label %64
37: ; preds = %30
%38 = tail call i32 @memset(i8* %33, i32 0, i32 %22)
%39 = tail call i32 @fread(i8* %33, i32 1, i32 4096, i32* %25)
%40 = tail call i32 @fclose(i32* %25)
%41 = getelementptr inbounds i8*, i8** %21, i64 2
%42 = load i8*, i8** %41
%43 = tail call i32* @fopen(i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %49
45: ; preds = %37
%46 = load i8*, i8** %41
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = tail call i32 @free(i8* %33)
br label %64
49: ; preds = %37
%50 = load i32, i32* @SEEK_SET
%51 = tail call i32 @fseek(i32* %43, i32 0, i32 %50)
%52 = getelementptr inbounds i8, i8* %33, i64 4096
%53 = tail call i32 @fread(i8* %52, i32 1, i32 12288, i32* %43)
%54 = tail call i32 @fclose(i32* %43)
%55 = getelementptr inbounds i8*, i8** %21, i64 3
%56 = load i8*, i8** %55
%57 = tail call i32 @WriteFile(i8* %56, i8* %33, i32 %22)
%58 = icmp eq i32 %57, %22
br i1 %58, label %61, label %59
59: ; preds = %49
%60 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* undef)
br label %61
61: ; preds = %59, %49
%62 = phi i32 [ -1, %59 ], [ 0, %49 ]
%63 = tail call i32 @free(i8* %33)
br label %64
64: ; preds = %61, %45, %35, %27, %5
%65 = phi i32 [ -1, %5 ], [ %62, %61 ], [ -1, %45 ], [ -1, %35 ], [ -1, %27 ]
ret i32 %65
}