#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 errno;
int /*<<< orphan*/ exit(int);
int ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int main(void) {
  FILE *fdWrite, *fdRead;
  int n;
  char buf[16384];
  if ((fdWrite = fopen("bar", "w")) == NULL) {
    printf("open error with errno = %d\n", errno);
    perror("bar");
    exit(1);
  }
  if ((fdRead = fopen("foo", "r")) == NULL) {
    printf("open error with errno = %d\n", errno);
    perror("foo");
    exit(1);
  }
  while ((n = fread(buf, 1, 16384, fdRead)) > 0) {
    if ((n = fwrite(buf, 1, n, fdWrite)) == 0) {
      if (ferror(fdWrite)) {
        printf("bar write error with errno = %d\n", ferror(fdWrite));
        exit(1);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4020,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rdi
lea    0xe74(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11ce <main+0x5e>
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    0xe53(%rip),%rdi
call   1050 <printf@plt>
lea    0xe41(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe4d(%rip),%rdi
lea    0xe26(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    121a <main+0xaa>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    0xe07(%rip),%rdi
call   1050 <printf@plt>
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    121f <main+0xaf>
lea    -0x4020(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4000,%edx
call   1040 <fread@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    12a6 <main+0x136>
lea    -0x4020(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    12a1 <main+0x131>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%eax
je     129c <main+0x12c>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
mov    %eax,%esi
lea    0xd99(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    12a1 <main+0x131>
jmp    121f <main+0xaf>
xor    %eax,%eax
add    $0x4020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x4000,%rsp
lea    0xe7d(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1242 <main+0xd2>
mov    %rax,%r15
lea    0xe80(%rip),%rdi
lea    0xe59(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1260 <main+0xf0>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x4000,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    1233 <main+0xc3>
mov    %rsp,%rbx
jmp    11f9 <main+0x89>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x4000,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    1233 <main+0xc3>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r15,%rcx
call   1090 <fwrite@plt>
test   %eax,%eax
jne    11e0 <main+0x70>
mov    %r15,%rdi
call   1030 <ferror@plt>
test   %eax,%eax
je     11e0 <main+0x70>
lea    0xe04(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x4000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
lea    0xdb4(%rip),%rdi
call   1050 <printf@plt>
lea    0xda2(%rip),%rdi
jmp    127c <main+0x10c>
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
lea    0xd96(%rip),%rdi
call   1050 <printf@plt>
lea    0xda6(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"bar\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [28 x i8] c"open error with errno = %d\0A\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"foo\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [33 x i8] c"bar write error with errno = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca [16384 x i8]
store i32 0, i32* %1
%6 = call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %13
8: ; preds = %0
%9 = load i32, i32* @errno
%10 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %14, i32** %3
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i32, i32* @errno
%18 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %13
br label %22
22: ; preds = %43, %21
%23 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%24 = load i32*, i32** %3
%25 = call i32 @fread(i8* %23, i32 1, i32 16384, i32* %24)
store i32 %25, i32* %4
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %44
27: ; preds = %22
%28 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%29 = load i32, i32* %4
%30 = load i32*, i32** %2
%31 = call i32 @fwrite(i8* %28, i32 1, i32 %29, i32* %30)
store i32 %31, i32* %4
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %43
33: ; preds = %27
%34 = load i32*, i32** %2
%35 = call i32 @ferror(i32* %34)
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = load i32*, i32** %2
%39 = call i32 @ferror(i32* %38)
%40 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %39)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %33
br label %43
43: ; preds = %42, %27
br label %22
44: ; preds = %22
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"bar\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [28 x i8] c"open error with errno = %d\0A\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"foo\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [33 x i8] c"bar write error with errno = %d\0A\00"
define i32 @main() {
%1 = alloca [16384 x i8]
%2 = getelementptr inbounds [16384 x i8], [16384 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %0
%6 = load i32, i32* @errno
%7 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %6)
%8 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = call i32 @fread(i8* %2, i32 1, i32 16384, i32* %11)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %21, label %34
16: ; preds = %10
%17 = load i32, i32* @errno
%18 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %13, %31
%22 = phi i32 [ %32, %31 ], [ %14, %13 ]
%23 = call i32 @fwrite(i8* %2, i32 1, i32 %22, i32* %3)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = tail call i32 @ferror(i32* %3)
%27 = icmp eq i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %25, %21
%32 = call i32 @fread(i8* %2, i32 1, i32 16384, i32* %11)
%33 = icmp sgt i32 %32, 0
br i1 %33, label %21, label %34
34: ; preds = %31, %13
ret i32 0
}