#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);
int 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);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4020,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11ab <main+0x4b>
lea    0xe64(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    0x2e3d(%rip),%rax
or     (%rax),%esi
lea    0xe40(%rip),%rdi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11f2 <main+0x92>
lea    0xe21(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
jmp    11f7 <main+0x97>
mov    -0x8(%rbp),%edi
lea    -0x4010(%rbp),%rsi
mov    $0x4000,%edx
call   1050 <read@plt>
mov    %eax,-0x4014(%rbp)
cmp    $0x0,%eax
jle    1234 <main+0xd4>
mov    -0xc(%rbp),%edi
lea    -0x4010(%rbp),%rsi
mov    -0x4014(%rbp),%edx
call   1030 <write@plt>
jmp    11f7 <main+0x97>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
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     120d <main+0xad>
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     1223 <main+0xc3>
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
call   1040 <close@plt>
xor    %eax,%eax
add    $0x4000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdec(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdda(%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 i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [16384 x i8]
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @O_RDONLY
%7 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 420)
store i32 %16, i32* %3
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 2)
unreachable
21: ; preds = %12
br label %22
22: ; preds = %27, %21
%23 = load i32, i32* %2
%24 = getelementptr inbounds [16384 x i8], [16384 x i8]* %4, i64 0, i64 0
%25 = call i32 @read(i32 %23, i8* %24, i32 16384)
store i32 %25, i32* %5
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i32, i32* %3
%29 = getelementptr inbounds [16384 x i8], [16384 x i8]* %4, i64 0, i64 0
%30 = load i32, i32* %5
%31 = call i32 @write(i32 %28, i8* %29, i32 %30)
br label %22
32: ; preds = %22
%33 = load i32, i32* %2
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %3
%36 = call i32 @close(i32 %35)
ret i32 0
}

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 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_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 i32 0
}