#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
void main() {
  int fin, fout;
  char buf[16384];
  int count;
  if ((fin = open("foo", O_RDONLY)) < 0) {
    perror("foo");
    exit(1);
  }
  if ((fout = open("bar", O_WRONLY | O_CREAT, 0644)) < 0) {
    perror("bar");
    exit(2);
  }
  while ((count = read(fin, buf, 16384)) > 0)
    write(fout, buf, count);
  close(fin);
  close(fout);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4020,%rsp
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jge    11a4 <main+0x44>
lea    0xe6b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    0x2e44(%rip),%rax
or     (%rax),%esi
lea    0xe47(%rip),%rdi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11eb <main+0x8b>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
jmp    11f0 <main+0x90>
mov    -0x4(%rbp),%edi
lea    -0x4010(%rbp),%rsi
mov    $0x4000,%edx
call   1050 <read@plt>
mov    %eax,-0x4014(%rbp)
cmp    $0x0,%eax
jle    122d <main+0xcd>
mov    -0x8(%rbp),%edi
lea    -0x4010(%rbp),%rsi
mov    -0x4014(%rbp),%edx
call   1030 <write@plt>
jmp    11f0 <main+0x90>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
add    $0x4020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x4000,%rsp
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe85(%rip),%rdi
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     120a <main+0xaa>
mov    %eax,%r14d
mov    0x2e34(%rip),%rax
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5e(%rip),%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     1220 <main+0xc0>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x4000,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11f0 <main+0x90>
mov    %rsp,%rbx
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x4000,%edx
call   1050 <read@plt>
test   %eax,%eax
jg     11d0 <main+0x70>
mov    %r14d,%edi
call   1040 <close@plt>
mov    %ebp,%edi
add    $0x4000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <close@plt>
lea    0xdef(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xddd(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"foo\00"
@O_RDONLY = global i32 0
@.str.1 = [4 x i8] c"bar\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define void @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [16384 x i8]
%4 = alloca i32
%5 = load i32, i32* @O_RDONLY
%6 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %1
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 420)
store i32 %15, i32* %2
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 2)
unreachable
20: ; preds = %11
br label %21
21: ; preds = %26, %20
%22 = load i32, i32* %1
%23 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 16384)
store i32 %24, i32* %4
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %21
%27 = load i32, i32* %2
%28 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 0
%29 = load i32, i32* %4
%30 = call i32 @write(i32 %27, i8* %28, i32 %29)
br label %21
31: ; preds = %21
%32 = load i32, i32* %1
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %2
%35 = call i32 @close(i32 %34)
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"foo\00"
@O_RDONLY = global i32 0
@.str.1 = [4 x i8] c"bar\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define void @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_RDONLY
%4 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 420)
%14 = icmp slt i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %9
%16 = call i32 @read(i32 %4, i8* %2, i32 16384)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %21, label %26
18: ; preds = %9
%19 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @exit(i32 2)
unreachable
21: ; preds = %15, %21
%22 = phi i32 [ %24, %21 ], [ %16, %15 ]
%23 = call i32 @write(i32 %13, i8* %2, i32 %22)
%24 = call i32 @read(i32 %4, i8* %2, i32 16384)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %21, label %26
26: ; preds = %21, %15
%27 = tail call i32 @close(i32 %4)
%28 = tail call i32 @close(i32 %13)
ret void
}