#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*/ perror(char *);
int read(int, char *, int);
int write(int, char *, int);
int fd2fd(int in, size_t size, int out) {
  char buf[4096];
  size_t written = 0;
  int ret;
  while ((written < size) && ((ret = read(in, buf, sizeof buf)) > 0)) {
    if (written + ret > size) {
      ret = size - written;
    }
    if (write(out, buf, ret) < ret) {
      perror("write()");
      return -1;
    }
    written += ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<fd2fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x1028(%rbp)
mov    -0x1028(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x102d(%rbp)
jae    118f <fd2fd+0x5f>
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x102c(%rbp)
cmp    $0x0,%eax
setg   %al
mov    %al,-0x102d(%rbp)
mov    -0x102d(%rbp),%al
test   $0x1,%al
jne    11a2 <fd2fd+0x72>
jmp    1221 <fd2fd+0xf1>
mov    -0x1028(%rbp),%rax
movslq -0x102c(%rbp),%rcx
add    %rcx,%rax
cmp    -0x10(%rbp),%rax
jbe    11ce <fd2fd+0x9e>
mov    -0x10(%rbp),%rax
sub    -0x1028(%rbp),%rax
mov    %eax,-0x102c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x102c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x102c(%rbp),%eax
jge    1207 <fd2fd+0xd7>
lea    0xe0a(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <fd2fd+0xf8>
movslq -0x102c(%rbp),%rax
add    -0x1028(%rbp),%rax
mov    %rax,-0x1028(%rbp)
jmp    1150 <fd2fd+0x20>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd2fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
test   %rsi,%rsi
je     11a0 <fd2fd+0x70>
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%r12d
mov    %rsp,%r13
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11a0 <fd2fd+0x70>
mov    %eax,%ecx
add    %r14,%rcx
mov    %ebx,%ebp
sub    %r14d,%ebp
cmp    %rbx,%rcx
cmovbe %eax,%ebp
mov    %r15d,%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jl     11b4 <fd2fd+0x84>
movslq %ebp,%rax
add    %rax,%r14
cmp    %rbx,%r14
jb     1160 <fd2fd+0x30>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe45(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    11a2 <fd2fd+0x72>

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

LLVM IR (-O2):
@.str = [8 x i8] c"write()\00"
define i32 @fd2fd(i32 %0, i64 %1, i32 %2) {
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%6 = icmp eq i64 %1, 0
br i1 %6, label %26, label %9
7: ; preds = %13
%8 = icmp ult i64 %23, %1
br i1 %8, label %9, label %26
9: ; preds = %3, %7
%10 = phi i64 [ %23, %7 ], [ 0, %3 ]
%11 = call i32 @read(i32 %0, i8* %5, i32 4096)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %26
13: ; preds = %9
%14 = zext i32 %11 to i64
%15 = add i64 %10, %14
%16 = icmp ugt i64 %15, %1
%17 = sub i64 %1, %10
%18 = trunc i64 %17 to i32
%19 = select i1 %16, i32 %18, i32 %11
%20 = call i32 @write(i32 %2, i8* %5, i32 %19)
%21 = icmp slt i32 %20, %19
%22 = sext i32 %19 to i64
%23 = add i64 %10, %22
br i1 %21, label %24, label %7
24: ; preds = %13
%25 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %7, %9, %3, %24
%27 = phi i32 [ -1, %24 ], [ 0, %3 ], [ 0, %9 ], [ 0, %7 ]
ret i32 %27
}