#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 /*<<< orphan*/ call_error(char *);
int read(int, char *, int);
int write(int, char *, int);
int file_copy(int fd_src, int fd_target, int size) {
  int n, r_size;
  char buf[8192];
  while (size > 0) {
    r_size = (8192 < size ? 8192 : size);
    n = read(fd_src, buf, r_size);
    if (n == -1) {
      call_error("read");
      return 1;
    }
    if (n == 0)
      break;
    if (write(fd_target, buf, n) != n) {
      call_error("write");
      return 1;
    }
    size -= n;
  }
  return 0;
}


x86 assembly (clang, -O0):
<file_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1207 <file_copy+0xd7>
mov    $0x2000,%eax
cmp    -0x10(%rbp),%eax
jge    116c <file_copy+0x3c>
mov    $0x2000,%eax
mov    %eax,-0x2024(%rbp)
jmp    1175 <file_copy+0x45>
mov    -0x10(%rbp),%eax
mov    %eax,-0x2024(%rbp)
mov    -0x2024(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b5 <file_copy+0x85>
lea    0xe5c(%rip),%rdi
call   1050 <call_error@plt>
movl   $0x1,-0x4(%rbp)
jmp    120e <file_copy+0xde>
cmpl   $0x0,-0x14(%rbp)
jne    11c4 <file_copy+0x94>
jmp    1207 <file_copy+0xd7>
mov    -0xc(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
cmp    -0x14(%rbp),%eax
je     11f7 <file_copy+0xc7>
lea    0xe1f(%rip),%rdi
call   1050 <call_error@plt>
movl   $0x1,-0x4(%rbp)
jmp    120e <file_copy+0xde>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
jmp    1144 <file_copy+0x14>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2008,%rsp
xor    %r12d,%r12d
test   %edx,%edx
jle    11c0 <file_copy+0x90>
mov    %edx,%ebp
mov    %esi,%r15d
mov    %edi,%ebx
mov    %rsp,%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x2000,%ebp
mov    $0x2000,%edx
cmovb  %ebp,%edx
mov    %ebx,%edi
mov    %r13,%rsi
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     11a0 <file_copy+0x70>
mov    %eax,%r14d
test   %eax,%eax
je     11a9 <file_copy+0x79>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
cmp    %r14d,%eax
jne    11ae <file_copy+0x7e>
sub    %r14d,%ebp
test   %ebp,%ebp
jg     1160 <file_copy+0x30>
jmp    11c0 <file_copy+0x90>
lea    0xe59(%rip),%rdi
jmp    11b5 <file_copy+0x85>
mov    %r14d,%r12d
jmp    11c0 <file_copy+0x90>
lea    0xe50(%rip),%rdi
call   1050 <call_error@plt>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@.str.1 = [6 x i8] c"write\00"
define i32 @file_copy(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [8192 x i8]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
br label %11
11: ; preds = %43, %3
%12 = load i32, i32* %7
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %47
14: ; preds = %11
%15 = load i32, i32* %7
%16 = icmp slt i32 8192, %15
br i1 %16, label %17, label %18
17: ; preds = %14
br label %20
18: ; preds = %14
%19 = load i32, i32* %7
br label %20
20: ; preds = %18, %17
%21 = phi i32 [ 8192, %17 ], [ %19, %18 ]
store i32 %21, i32* %9
%22 = load i32, i32* %5
%23 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
%24 = load i32, i32* %9
%25 = call i32 @read(i32 %22, i8* %23, i32 %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = call i32 @call_error(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %48
30: ; preds = %20
%31 = load i32, i32* %8
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
br label %47
34: ; preds = %30
%35 = load i32, i32* %6
%36 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
%37 = load i32, i32* %8
%38 = call i32 @write(i32 %35, i8* %36, i32 %37)
%39 = load i32, i32* %8
%40 = icmp ne i32 %38, %39
br i1 %40, label %41, label %43
41: ; preds = %34
%42 = call i32 @call_error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %48
43: ; preds = %34
%44 = load i32, i32* %8
%45 = load i32, i32* %7
%46 = sub nsw i32 %45, %44
store i32 %46, i32* %7
br label %11
47: ; preds = %33, %11
store i32 0, i32* %4
br label %48
48: ; preds = %47, %41, %28
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@.str.1 = [6 x i8] c"write\00"
define i32 @file_copy(i32 %0, i32 %1, i32 %2) {
%4 = alloca [8192 x i8]
%5 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%6 = icmp sgt i32 %2, 0
br i1 %6, label %9, label %21
7: ; preds = %14
%8 = icmp sgt i32 %17, 0
br i1 %8, label %9, label %21
9: ; preds = %3, %7
%10 = phi i32 [ %17, %7 ], [ %2, %3 ]
%11 = icmp ult i32 %10, 8192
%12 = select i1 %11, i32 %10, i32 8192
%13 = call i32 @read(i32 %0, i8* %5, i32 %12)
switch i32 %13, label %14 [
i32 -1, label %18
i32 0, label %21
]
14: ; preds = %9
%15 = call i32 @write(i32 %1, i8* %5, i32 %13)
%16 = icmp eq i32 %15, %13
%17 = sub nsw i32 %10, %13
br i1 %16, label %7, label %18
18: ; preds = %14, %9
%19 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %9 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %14 ]
%20 = tail call i32 @call_error(i8* %19)
br label %21
21: ; preds = %9, %7, %18, %3
%22 = phi i32 [ 0, %3 ], [ 1, %18 ], [ %13, %9 ], [ 0, %7 ]
ret i32 %22
}