#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 EOF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int putchar(int);
int /*<<< orphan*/ puts(char *);
main() {
  FILE *infile;
  FILE *outfile;
  int c;
  puts("Starting input only");
  if ((infile = fopen("b2test.dat", "r")) == NULL) {
    puts("could not open input file");
    exit(1);
  }
  while (putchar(fgetc(infile)) != EOF)
    ;
  puts("end of input file");
  fclose(infile);
  puts("starting copy");
  if ((infile = fopen("b2test.dat", "r")) == NULL) {
    puts("could not open input file for copy");
    exit(1);
  }
  if ((outfile = fopen("b2test.out", "w")) == NULL) {
    puts("could not open output file");
    exit(1);
  }
  while ((c = fgetc(infile)) != EOF) {
    fputc(c, outfile);
  }
  puts("finished output file");
  fclose(infile);
  fclose(outfile);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
call   1040 <puts@plt>
lea    0xe82(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11c2 <main+0x52>
lea    0xe6e(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11c7 <main+0x57>
mov    -0x10(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,%edi
call   1030 <putchar@plt>
mov    0x2e0a(%rip),%rcx
cmp    (%rcx),%eax
je     11eb <main+0x7b>
jmp    11c7 <main+0x57>
lea    0xe49(%rip),%rdi
call   1040 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xe46(%rip),%rdi
call   1040 <puts@plt>
lea    0xe01(%rip),%rdi
lea    0xe05(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1243 <main+0xd3>
lea    0xe27(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe34(%rip),%rdi
lea    0xe38(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    127a <main+0x10a>
lea    0xe20(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    127f <main+0x10f>
mov    -0x10(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d56(%rip),%rcx
cmp    (%rcx),%eax
je     12ab <main+0x13b>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1070 <fputc@plt>
jmp    127f <main+0x10f>
lea    0xdf4(%rip),%rdi
call   1040 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
lea    0xe84(%rip),%rdi
call   1040 <puts@plt>
lea    0xe8c(%rip),%rdi
lea    0xe90(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     125d <main+0xed>
mov    %rax,%rbx
mov    0x2e41(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <fgetc@plt>
mov    %eax,%edi
call   1030 <putchar@plt>
cmp    (%r15),%eax
jne    11b0 <main+0x40>
lea    0xe70(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe6e(%rip),%rdi
call   1040 <puts@plt>
lea    0xe29(%rip),%rdi
lea    0xe2d(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1266 <main+0xf6>
mov    %rax,%r14
lea    0xe78(%rip),%rdi
lea    0xe7c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     126f <main+0xff>
mov    %rax,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1060 <fgetc@plt>
cmp    (%r15),%eax
je     1239 <main+0xc9>
mov    %eax,%edi
mov    %rbx,%rsi
call   1070 <fputc@plt>
jmp    1220 <main+0xb0>
lea    0xe66(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdbd(%rip),%rdi
jmp    1276 <main+0x106>
lea    0xdee(%rip),%rdi
jmp    1276 <main+0x106>
lea    0xe15(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"Starting input only\00"
@.str.1 = [11 x i8] c"b2test.dat\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [26 x i8] c"could not open input file\00"
@EOF = global i32 0
@.str.4 = [18 x i8] c"end of input file\00"
@.str.5 = [14 x i8] c"starting copy\00"
@.str.6 = [35 x i8] c"could not open input file for copy\00"
@.str.7 = [11 x i8] c"b2test.out\00"
@.str.8 = [2 x i8] c"w\00"
@.str.9 = [27 x i8] c"could not open output file\00"
@.str.10 = [21 x i8] c"finished output file\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%6 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %6, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
br label %12
12: ; preds = %18, %11
%13 = load i32*, i32** %2
%14 = call i32 @fgetc(i32* %13)
%15 = call i32 @putchar(i32 %14)
%16 = load i32, i32* @EOF
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %12
br label %12
19: ; preds = %12
%20 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%21 = load i32*, i32** %2
%22 = call i32 @fclose(i32* %21)
%23 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%24 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %24, i32** %2
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = call i32 @puts(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %19
%30 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
store i32* %30, i32** %3
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = call i32 @puts(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %29
br label %36
36: ; preds = %41, %35
%37 = load i32*, i32** %2
%38 = call i32 @fgetc(i32* %37)
store i32 %38, i32* %4
%39 = load i32, i32* @EOF
%40 = icmp ne i32 %38, %39
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = load i32, i32* %4
%43 = load i32*, i32** %3
%44 = call i32 @fputc(i32 %42, i32* %43)
br label %36
45: ; preds = %36
%46 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0))
%47 = load i32*, i32** %2
%48 = call i32 @fclose(i32* %47)
%49 = load i32*, i32** %3
%50 = call i32 @fclose(i32* %49)
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
@.str = [20 x i8] c"Starting input only\00"
@.str.1 = [11 x i8] c"b2test.dat\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [26 x i8] c"could not open input file\00"
@EOF = global i32 0
@.str.4 = [18 x i8] c"end of input file\00"
@.str.5 = [14 x i8] c"starting copy\00"
@.str.6 = [35 x i8] c"could not open input file for copy\00"
@.str.7 = [11 x i8] c"b2test.out\00"
@.str.8 = [2 x i8] c"w\00"
@.str.9 = [27 x i8] c"could not open output file\00"
@.str.10 = [21 x i8] c"finished output file\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0, %7
%8 = tail call i32 @fgetc(i32* %2)
%9 = tail call i32 @putchar(i32 %8)
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %7
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%14 = tail call i32 @fclose(i32* %2)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%16 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %28, label %24
24: ; preds = %21
%25 = tail call i32 @fgetc(i32* %16)
%26 = load i32, i32* @EOF
%27 = icmp eq i32 %25, %26
br i1 %27, label %37, label %31
28: ; preds = %21
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %24, %31
%32 = phi i32 [ %34, %31 ], [ %25, %24 ]
%33 = tail call i32 @fputc(i32 %32, i32* %22)
%34 = tail call i32 @fgetc(i32* %16)
%35 = load i32, i32* @EOF
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %31
37: ; preds = %31, %24
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0))
%39 = tail call i32 @fclose(i32* %16)
%40 = tail call i32 @fclose(i32* %22)
ret i32 0
}