#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_IRUSR;
int S_IWUSR;
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  char c;
  int in, out;
  in = open("file.in", O_RDONLY);
  if (in == -1)
    perror("open failed.");
  out = open("file.out", O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR);
  if (out == -1)
    perror("open to write failed.");
  while (read(in, &c, 1) == 1)
    write(out, &c, 1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    117f <main+0x3f>
lea    0xe8e(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e69(%rip),%rax
or     (%rax),%esi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
mov    0x2e27(%rip),%rax
or     (%rax),%edx
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11ca <main+0x8a>
lea    0xe59(%rip),%rdi
call   1060 <perror@plt>
jmp    11cf <main+0x8f>
mov    -0xc(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    11ff <main+0xbf>
mov    -0x10(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    11cf <main+0x8f>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
xor    %eax,%eax
call   1050 <open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     11ef <main+0xaf>
mov    0x2e46(%rip),%rax
mov    0x2e7f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e54(%rip),%rax
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe7f(%rip),%rdi
xor    %eax,%eax
call   1050 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1200 <main+0xc0>
lea    0xf(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    11e4 <main+0xa4>
lea    0xf(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     11c0 <main+0x80>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe12(%rip),%rdi
call   1060 <perror@plt>
jmp    116b <main+0x2b>
lea    0xe17(%rip),%rdi
call   1060 <perror@plt>
jmp    11a4 <main+0x64>

LLVM IR (-O0):
@.str = [8 x i8] c"file.in\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"open failed.\00"
@.str.2 = [9 x i8] c"file.out\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@.str.3 = [22 x i8] c"open to write failed.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_RDONLY
%6 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IWUSR
%16 = load i32, i32* @S_IRUSR
%17 = or i32 %15, %16
%18 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %11
%22 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %11
br label %24
24: ; preds = %28, %23
%25 = load i32, i32* %3
%26 = call i32 @read(i32 %25, i8* %2, i32 1)
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* %4
%30 = call i32 @write(i32 %29, i8* %2, i32 1)
br label %24
31: ; preds = %24
ret i32 0
}

LLVM IR (-O2):
@.str = [8 x i8] c"file.in\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"open failed.\00"
@.str.2 = [9 x i8] c"file.out\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@.str.3 = [22 x i8] c"open to write failed.\00"
define i32 @main() {
%1 = alloca i8
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IWUSR
%12 = load i32, i32* @S_IRUSR
%13 = or i32 %12, %11
%14 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %10, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %7
%17 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %16, %7
%19 = call i32 @read(i32 %3, i8* %1, i32 1)
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %25
21: ; preds = %18, %21
%22 = call i32 @write(i32 %14, i8* %1, i32 1)
%23 = call i32 @read(i32 %3, i8* %1, i32 1)
%24 = icmp eq i32 %23, 1
br i1 %24, label %21, label %25
25: ; preds = %21, %18
ret i32 0
}