#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*/ exit(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  char buf;
  int fd_in;
  int fd_out;
  fd_in = open("file_in", O_RDONLY);
  if (fd_in < 0) {
    perror("Open failed!(in)");
    exit(1);
  }
  fd_out = open("file_out_by_char", O_WRONLY | O_CREAT, 0644);
  if (fd_out < 0) {
    perror("Open failed!(out)");
    exit(1);
  }
  while (0 != read(fd_in, &buf, 1)) {
    write(fd_out, &buf, 1);
  }
  printf("copy finished\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11b0 <main+0x50>
lea    0xe67(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
lea    0xe50(%rip),%rdi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11f8 <main+0x98>
lea    0xe41(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11fd <main+0x9d>
mov    -0x18(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1232 <main+0xd2>
mov    -0x1c(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    11fd <main+0x9d>
lea    0xe03(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     121b <main+0xbb>
mov    %eax,%r14d
mov    0x2e37(%rip),%rax
mov    0x2e60(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe76(%rip),%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     1224 <main+0xc4>
mov    %eax,%ebp
lea    0xf(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
je     1204 <main+0xa4>
lea    0xf(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jne    11e0 <main+0x80>
lea    0xe31(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xde6(%rip),%rdi
jmp    122b <main+0xcb>
lea    0xdff(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

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