#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*/ exit(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  FILE *in, *out;
  char buf[32768];
  if (argc != 3) {
    fprintf(stderr, "%s image dockfile.\n", argv[0]);
    exit(1);
  }
  in = fopen(argv[1], "r");
  if (in == NULL) {
    perror(argv[1]);
    exit(1);
  }
  out = fopen(argv[2], "w");
  if (out == NULL) {
    perror(argv[2]);
    exit(1);
  }
  fputc(0, out);
  fputc(0x1, out);
  fputc(0x1, out);
  fputc(0x1, out);
  fputc(0x1, out);
  fputc(0x2, out);
  fputc(0x2, out);
  fputc(0x2, out);
  fputc(0x2, out);
  if (fread(buf, 32768, 1, in) != 1 || fread(buf, 32768, 1, in) != 1) {
    fprintf(stderr, "%s: image '%s' is too short.\n", argv[0], argv[1]);
    exit(1);
  }
  if (fwrite(buf, 32768, 1, out) != 1 || fclose(out)) {
    fprintf(stderr, "%s: short write to '%s'.\n", argv[0], argv[2]);
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11cb <main+0x4b>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe3a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1205 <main+0x85>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe02(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123f <main+0xbf>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rsi
xor    %edi,%edi
call   1050 <fputc@plt>
mov    -0x20(%rbp),%rsi
mov    $0x1,%edi
call   1050 <fputc@plt>
mov    -0x20(%rbp),%rsi
mov    $0x1,%edi
call   1050 <fputc@plt>
mov    -0x20(%rbp),%rsi
mov    $0x1,%edi
call   1050 <fputc@plt>
mov    -0x20(%rbp),%rsi
mov    $0x1,%edi
call   1050 <fputc@plt>
mov    -0x20(%rbp),%rsi
mov    $0x2,%edi
call   1050 <fputc@plt>
mov    -0x20(%rbp),%rsi
mov    $0x2,%edi
call   1050 <fputc@plt>
mov    -0x20(%rbp),%rsi
mov    $0x2,%edi
call   1050 <fputc@plt>
mov    -0x20(%rbp),%rsi
mov    $0x2,%edi
call   1050 <fputc@plt>
lea    -0x8020(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x8000,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1300 <main+0x180>
lea    -0x8020(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x8000,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1330 <main+0x1b0>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xcf9(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x8020(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x8000,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
cmp    $0x1,%eax
jne    1366 <main+0x1e6>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     1396 <main+0x216>
mov    0x2c73(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xcb1(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %eax,%eax
add    $0x8020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x8000,%rsp
mov    %rsi,%r14
cmp    $0x3,%edi
jne    12ed <main+0x16d>
mov    0x8(%r14),%rdi
lea    0xe71(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1311 <main+0x191>
mov    %rax,%r15
mov    0x10(%r14),%rdi
lea    0xe57(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1317 <main+0x197>
mov    %rax,%rbx
xor    %edi,%edi
mov    %rax,%rsi
call   1050 <fputc@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    %rsp,%rdi
mov    $0x8000,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    12ac <main+0x12c>
mov    %rsp,%rdi
mov    $0x8000,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    12ac <main+0x12c>
mov    %rsp,%rdi
mov    $0x8000,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10a0 <fwrite@plt>
cmp    $0x1,%eax
jne    12c5 <main+0x145>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    12c5 <main+0x145>
xor    %eax,%eax
add    $0x8000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
mov    0x8(%r14),%rcx
lea    0xd55(%rip),%rsi
jmp    12dc <main+0x15c>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
mov    0x10(%r14),%rcx
lea    0xd5a(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd00(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x8(%r14),%rdi
jmp    131b <main+0x19b>
mov    0x10(%r14),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"%s image dockfile.\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [30 x i8] c"%s: image '%s' is too short.\0A\00"
@.str.4 = [26 x i8] c"%s: short write to '%s'.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [32768 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 3
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %31
25: ; preds = %18
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @perror(i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %18
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = call i32* @fopen(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %35, i32** %7
%36 = load i32*, i32** %7
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %44
38: ; preds = %31
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 2
%41 = load i8*, i8** %40
%42 = call i32 @perror(i8* %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %31
%45 = load i32*, i32** %7
%46 = call i32 @fputc(i32 0, i32* %45)
%47 = load i32*, i32** %7
%48 = call i32 @fputc(i32 1, i32* %47)
%49 = load i32*, i32** %7
%50 = call i32 @fputc(i32 1, i32* %49)
%51 = load i32*, i32** %7
%52 = call i32 @fputc(i32 1, i32* %51)
%53 = load i32*, i32** %7
%54 = call i32 @fputc(i32 1, i32* %53)
%55 = load i32*, i32** %7
%56 = call i32 @fputc(i32 2, i32* %55)
%57 = load i32*, i32** %7
%58 = call i32 @fputc(i32 2, i32* %57)
%59 = load i32*, i32** %7
%60 = call i32 @fputc(i32 2, i32* %59)
%61 = load i32*, i32** %7
%62 = call i32 @fputc(i32 2, i32* %61)
%63 = getelementptr inbounds [32768 x i8], [32768 x i8]* %8, i64 0, i64 0
%64 = load i32*, i32** %6
%65 = call i32 @fread(i8* %63, i32 32768, i32 1, i32* %64)
%66 = icmp ne i32 %65, 1
br i1 %66, label %72, label %67
67: ; preds = %44
%68 = getelementptr inbounds [32768 x i8], [32768 x i8]* %8, i64 0, i64 0
%69 = load i32*, i32** %6
%70 = call i32 @fread(i8* %68, i32 32768, i32 1, i32* %69)
%71 = icmp ne i32 %70, 1
br i1 %71, label %72, label %82
72: ; preds = %67, %44
%73 = load i32, i32* @stderr
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 0
%76 = load i8*, i8** %75
%77 = load i8**, i8*** %5
%78 = getelementptr inbounds i8*, i8** %77, i64 1
%79 = load i8*, i8** %78
%80 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %76, i8* %79)
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %67
%83 = getelementptr inbounds [32768 x i8], [32768 x i8]* %8, i64 0, i64 0
%84 = load i32*, i32** %7
%85 = call i32 @fwrite(i8* %83, i32 32768, i32 1, i32* %84)
%86 = icmp ne i32 %85, 1
br i1 %86, label %91, label %87
87: ; preds = %82
%88 = load i32*, i32** %7
%89 = call i64 @fclose(i32* %88)
%90 = icmp ne i64 %89, 0
br i1 %90, label %91, label %101
91: ; preds = %87, %82
%92 = load i32, i32* @stderr
%93 = load i8**, i8*** %5
%94 = getelementptr inbounds i8*, i8** %93, i64 0
%95 = load i8*, i8** %94
%96 = load i8**, i8*** %5
%97 = getelementptr inbounds i8*, i8** %96, i64 2
%98 = load i8*, i8** %97
%99 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %92, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %95, i8* %98)
%100 = call i32 @exit(i32 1)
unreachable
101: ; preds = %87
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"%s image dockfile.\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [30 x i8] c"%s: image '%s' is too short.\0A\00"
@.str.4 = [26 x i8] c"%s: short write to '%s'.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [32768 x i8]
%4 = getelementptr inbounds [32768 x i8], [32768 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 3
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i8*, i8** %12
%18 = tail call i32 @perror(i8* %17)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %11
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = tail call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i8*, i8** %21
%27 = tail call i32 @perror(i8* %26)
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = tail call i32 @fputc(i32 0, i32* %23)
%31 = tail call i32 @fputc(i32 1, i32* %23)
%32 = tail call i32 @fputc(i32 1, i32* %23)
%33 = tail call i32 @fputc(i32 1, i32* %23)
%34 = tail call i32 @fputc(i32 1, i32* %23)
%35 = tail call i32 @fputc(i32 2, i32* %23)
%36 = tail call i32 @fputc(i32 2, i32* %23)
%37 = tail call i32 @fputc(i32 2, i32* %23)
%38 = tail call i32 @fputc(i32 2, i32* %23)
%39 = call i32 @fread(i8* %4, i32 32768, i32 1, i32* %14)
%40 = icmp eq i32 %39, 1
br i1 %40, label %41, label %44
41: ; preds = %29
%42 = call i32 @fread(i8* %4, i32 32768, i32 1, i32* %14)
%43 = icmp eq i32 %42, 1
br i1 %43, label %50, label %44
44: ; preds = %41, %29
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %1
%47 = load i8*, i8** %12
%48 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %46, i8* %47)
%49 = tail call i32 @exit(i32 1)
unreachable
50: ; preds = %41
%51 = call i32 @fwrite(i8* %4, i32 32768, i32 1, i32* %23)
%52 = icmp eq i32 %51, 1
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = tail call i64 @fclose(i32* %23)
%55 = icmp eq i64 %54, 0
br i1 %55, label %62, label %56
56: ; preds = %53, %50
%57 = load i32, i32* @stderr
%58 = load i8*, i8** %1
%59 = load i8*, i8** %21
%60 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %58, i8* %59)
%61 = tail call i32 @exit(i32 1)
unreachable
62: ; preds = %53
ret i32 0
}