#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);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(unsigned char *, int, int, int /*<<< orphan*/);
int fwrite(unsigned char *, int, size_t, int /*<<< orphan*/ *);
char *getenv(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int main(int argc, char *argv[]) {
  unsigned char buf[4096];
  const char *fname;
  size_t len;
  FILE *f;
  fname = getenv("BASTILE_IMAGE_FILE");
  if (!fname) {
    fprintf(stderr,
            "bastile-xloadimage: specify output file in $BASTILE_IMAGE_FILE\n");
    exit(2);
  }
  f = fopen(fname, "wb");
  if (!f) {
    fprintf(stderr, "bastile-xloadimage: couldn't open output file: %s", fname);
    exit(1);
  }
  do {
    len = fread(buf, sizeof(unsigned char), 4096, stdin);
  } while (fwrite(buf, sizeof(unsigned char), len, f) == 4096);
  if (ferror(f)) {
    fprintf(stderr, "bastile-xloadimage: couldn't write to output file: %s",
            fname);
    exit(1);
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe60(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x1018(%rbp)
cmpq   $0x0,-0x1018(%rbp)
jne    11db <main+0x5b>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    -0x1018(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jne    122b <main+0xab>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    -0x1018(%rbp),%rdx
lea    0xe3c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1230 <main+0xb0>
lea    -0x1010(%rbp),%rdi
mov    0x2daa(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1050 <fread@plt>
mov    %rax,-0x1020(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x1020(%rbp),%rdx
mov    -0x1028(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
cmp    $0x1000,%eax
je     1230 <main+0xb0>
mov    -0x1028(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     12be <main+0x13e>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
mov    -0x1018(%rbp),%rdx
lea    0xddb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1028(%rbp),%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1008,%rsp
lea    0xe6b(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1226 <main+0xa6>
mov    %rax,%r14
lea    0xea6(%rip),%rsi
mov    %rax,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1247 <main+0xc7>
mov    %rax,%r15
mov    0x2e20(%rip),%r12
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    (%r12),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
call   1050 <fread@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
cmp    $0x1000,%eax
je     11d0 <main+0x50>
mov    %r15,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
jne    1259 <main+0xd9>
mov    %r15,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xdff(%rip),%rsi
jmp    1269 <main+0xe9>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"BASTILE_IMAGE_FILE\00"
@stderr = global i32 0
@.str.1 = [64 x i8] c"bastile-xloadimage: specify output file in $BASTILE_IMAGE_FILE\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [50 x i8] c"bastile-xloadimage: couldn't open output file: %s\00"
@stdin = global i32 0
@.str.4 = [54 x i8] c"bastile-xloadimage: couldn't write to output file: %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [4096 x i8]
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i8* @getenv(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp ne i8* %11, null
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 2)
unreachable
17: ; preds = %2
%18 = load i8*, i8** %7
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %19, i32** %9
%20 = load i32*, i32** %9
%21 = icmp ne i32* %20, null
br i1 %21, label %27, label %22
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %7
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %17
br label %28
28: ; preds = %32, %27
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%30 = load i32, i32* @stdin
%31 = call i64 @fread(i8* %29, i32 1, i32 4096, i32 %30)
store i64 %31, i64* %8
br label %32
32: ; preds = %28
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%34 = load i64, i64* %8
%35 = load i32*, i32** %9
%36 = call i32 @fwrite(i8* %33, i32 1, i64 %34, i32* %35)
%37 = icmp eq i32 %36, 4096
br i1 %37, label %28, label %38
38: ; preds = %32
%39 = load i32*, i32** %9
%40 = call i64 @ferror(i32* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %38
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %7
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i8* %44)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %38
%48 = load i32*, i32** %9
%49 = call i32 @fclose(i32* %48)
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"BASTILE_IMAGE_FILE\00"
@stderr = global i32 0
@.str.1 = [64 x i8] c"bastile-xloadimage: specify output file in $BASTILE_IMAGE_FILE\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [50 x i8] c"bastile-xloadimage: couldn't open output file: %s\00"
@stdin = global i32 0
@.str.4 = [54 x i8] c"bastile-xloadimage: couldn't write to output file: %s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 2)
unreachable
11: ; preds = %2
%12 = tail call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11, %18
%19 = load i32, i32* @stdin
%20 = call i64 @fread(i8* %4, i32 1, i32 4096, i32 %19)
%21 = call i32 @fwrite(i8* %4, i32 1, i64 %20, i32* %12)
%22 = icmp eq i32 %21, 4096
br i1 %22, label %18, label %23
23: ; preds = %18
%24 = call i64 @ferror(i32* %12)
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i8* %5)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %23
%31 = call i32 @fclose(i32* %12)
ret i32 0
}