#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 */

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int errno;
int /*<<< orphan*/ exit(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int write(int, char *, int);
int main(void) {
  int fdWrite, fdRead, n;
  char buf[16384];
  if ((fdWrite = open("bar", O_CREAT | O_WRONLY,
                      S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
    printf("open error with errno = %d\n", errno);
    perror("bar");
    exit(1);
  }
  if ((fdRead = open("foo", O_RDONLY)) == -1) {
    printf("open error with errno = %d\n", errno);
    perror("foo");
    exit(1);
  }
  while ((n = read(fdRead, buf, 16384)) > 0) {
    if ((n = write(fdWrite, buf, n)) == -1) {
      printf("write error with errno = %d\n", errno);
      perror("bar");
      exit(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4010,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    0x2e1e(%rip),%rax
or     (%rax),%esi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
mov    0x2e44(%rip),%rax
or     (%rax),%edx
mov    0x2e23(%rip),%rax
or     (%rax),%edx
mov    0x2e12(%rip),%rax
or     (%rax),%edx
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11ed <main+0x8d>
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
lea    0xe32(%rip),%rdi
call   1040 <printf@plt>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    123b <main+0xdb>
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
lea    0xde4(%rip),%rdi
call   1040 <printf@plt>
lea    0xdf4(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1240 <main+0xe0>
mov    -0xc(%rbp),%edi
lea    -0x4010(%rbp),%rsi
mov    $0x4000,%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    12ae <main+0x14e>
mov    -0x8(%rbp),%edi
lea    -0x4010(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    12a9 <main+0x149>
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
lea    0xd96(%rip),%rdi
call   1040 <printf@plt>
lea    0xd66(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1240 <main+0xe0>
xor    %eax,%eax
add    $0x4010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x4000,%rsp
mov    0x2e86(%rip),%rax
mov    0x2e27(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2c(%rip),%rax
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e2a(%rip),%rax
or     (%rax),%edx
mov    0x2e19(%rip),%rax
or     (%rax),%edx
lea    0xe58(%rip),%rdi
xor    %eax,%eax
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     123e <main+0xde>
mov    %eax,%r14d
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
xor    %eax,%eax
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1250 <main+0xf0>
mov    %eax,%ebp
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4000,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1230 <main+0xd0>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11e0 <main+0x80>
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
lea    0xe0f(%rip),%rdi
call   1040 <printf@plt>
lea    0xddf(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x4000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
lea    0xdb6(%rip),%rdi
jmp    1215 <main+0xb5>
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
lea    0xda4(%rip),%rdi
call   1040 <printf@plt>
lea    0xdb4(%rip),%rdi
jmp    1221 <main+0xc1>

LLVM IR (-O0):
@.str = [4 x i8] c"bar\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [28 x i8] c"open error with errno = %d\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"foo\00"
@O_RDONLY = global i32 0
@.str.3 = [29 x i8] c"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 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_WRONLY
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRGRP
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IROTH
%15 = or i32 %13, %14
%16 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %15)
store i32 %16, i32* %2
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %0
%19 = load i32, i32* @errno
%20 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %0
%24 = load i32, i32* @O_RDONLY
%25 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %24)
store i32 %25, i32* %3
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i32, i32* @errno
%29 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %23
br label %33
33: ; preds = %49, %32
%34 = load i32, i32* %3
%35 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 16384)
store i32 %36, i32* %4
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %50
38: ; preds = %33
%39 = load i32, i32* %2
%40 = getelementptr inbounds [16384 x i8], [16384 x i8]* %5, i64 0, i64 0
%41 = load i32, i32* %4
%42 = call i32 @write(i32 %39, i8* %40, i32 %41)
store i32 %42, i32* %4
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %49
44: ; preds = %38
%45 = load i32, i32* @errno
%46 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %45)
%47 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %38
br label %33
50: ; preds = %33
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"bar\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [28 x i8] c"open error with errno = %d\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"foo\00"
@O_RDONLY = global i32 0
@.str.3 = [29 x i8] c"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 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_WRONLY
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRGRP
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IROTH
%12 = or i32 %10, %11
%13 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %0
%16 = load i32, i32* @errno
%17 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %0
%21 = load i32, i32* @O_RDONLY
%22 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32, i32* @errno
%26 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %20, %32
%30 = call i32 @read(i32 %22, i8* %2, i32 16384)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %40
32: ; preds = %29
%33 = call i32 @write(i32 %13, i8* %2, i32 %30)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %29
35: ; preds = %32
%36 = load i32, i32* @errno
%37 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%39 = tail call i32 @exit(i32 1)
unreachable
40: ; preds = %29
ret i32 0
}