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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x5020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x501c(%rbp)
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x5014(%rbp)
cmpl   $0x0,-0x5014(%rbp)
jge    11c3 <main+0x63>
lea    0xe54(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    0x2e25(%rip),%rax
or     (%rax),%esi
lea    0xe3d(%rip),%rdi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x5018(%rbp)
cmpl   $0x0,-0x5018(%rbp)
jge    1211 <main+0xb1>
lea    0xe27(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1216 <main+0xb6>
mov    -0x5014(%rbp),%edi
lea    -0x5010(%rbp),%rsi
mov    $0x5000,%edx
call   1050 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x501c(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jge    125c <main+0xfc>
mov    -0x5018(%rbp),%edi
lea    -0x5010(%rbp),%rsi
mov    -0x501c(%rbp),%edx
call   1030 <write@plt>
jmp    1216 <main+0xb6>
lea    0xdd8(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x5020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x5000,%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    0xe73(%rip),%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     1213 <main+0xb3>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x5000,%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   1040 <write@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x5000,%edx
call   1050 <read@plt>
test   %eax,%eax
jg     11d0 <main+0x70>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x5000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf7(%rip),%rdi
jmp    121a <main+0xba>
lea    0xe0f(%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 = [16 x i8] c"file_out_by_buf\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 [20480 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
%10 = load i32, i32* @O_RDONLY
%11 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %20, i32 420)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %17
br label %28
28: ; preds = %33, %27
%29 = load i32, i32* %7
%30 = getelementptr inbounds [20480 x i8], [20480 x i8]* %6, i64 0, i64 0
%31 = call i32 @read(i32 %29, i8* %30, i32 20480)
store i32 %31, i32* %9
%32 = icmp slt i32 0, %31
br i1 %32, label %33, label %38
33: ; preds = %28
%34 = load i32, i32* %8
%35 = getelementptr inbounds [20480 x i8], [20480 x i8]* %6, i64 0, i64 0
%36 = load i32, i32* %9
%37 = call i32 @write(i32 %34, i8* %35, i32 %36)
br label %28
38: ; preds = %28
%39 = 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 = [16 x i8] c"file_out_by_buf\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 [20480 x i8]
%4 = getelementptr inbounds [20480 x i8], [20480 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 420)
%16 = icmp slt i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %11
%18 = call i32 @read(i32 %6, i8* %4, i32 20480)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %23, label %28
20: ; preds = %11
%21 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %17, %23
%24 = phi i32 [ %26, %23 ], [ %18, %17 ]
%25 = call i32 @write(i32 %15, i8* %4, i32 %24)
%26 = call i32 @read(i32 %6, i8* %4, i32 20480)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %23, label %28
28: ; preds = %23, %17
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
ret i32 0
}