#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 */
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
scalar_t__ read(int, char *, int);
scalar_t__ write(int, char *, scalar_t__);
ssize_t fd_to_fd(int outfd, int infd) {
  ssize_t total = 0;
  char buffer[4096];
  for (;;) {
    ssize_t nread, nwritten = 0;
    nread = read(infd, buffer, sizeof(buffer));
    if (nread < 0)
      return -1;
    if (!nread)
      break;
    do {
      ssize_t n = write(outfd, buffer + nwritten, nread - nwritten);
      if (n < 0)
        return -1;
      nwritten += n;
    } while (nwritten < nread);
    total += nwritten;
  }
  return total;
}


x86 assembly (clang, -O0):
<fd_to_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x1030(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jge    117a <fd_to_fd+0x5a>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    121d <fd_to_fd+0xfd>
cmpq   $0x0,-0x1028(%rbp)
jne    118d <fd_to_fd+0x6d>
jmp    1215 <fd_to_fd+0xf5>
jmp    1192 <fd_to_fd+0x72>
mov    -0xc(%rbp),%edi
lea    -0x1020(%rbp),%rsi
add    -0x1030(%rbp),%rsi
mov    -0x1028(%rbp),%rdx
sub    -0x1030(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1038(%rbp)
jge    11d8 <fd_to_fd+0xb8>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    121d <fd_to_fd+0xfd>
mov    -0x1038(%rbp),%rax
add    -0x1030(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    -0x1030(%rbp),%rax
cmp    -0x1028(%rbp),%rax
jl     1192 <fd_to_fd+0x72>
mov    -0x1030(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1139 <fd_to_fd+0x19>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_to_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %esi,%r12d
mov    %edi,%ebx
mov    %rsp,%rsi
mov    %r12d,%edi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    $0xffffffffffffffff,%r14
test   %rax,%rax
js     11a6 <fd_to_fd+0x86>
mov    %rax,%rbp
xor    %r13d,%r13d
test   %rbp,%rbp
je     11a3 <fd_to_fd+0x83>
xor    %ecx,%ecx
nop
mov    %rcx,%r15
lea    (%rsp,%rcx,1),%rsi
mov    %rbp,%rdx
sub    %rcx,%rdx
mov    %ebx,%edi
call   1030 <write@plt>
lea    (%rax,%r15,1),%rcx
test   %rax,%rax
cmovns %rcx,%r15
js     11a6 <fd_to_fd+0x86>
cmp    %rbp,%r15
jl     1160 <fd_to_fd+0x40>
add    %r15,%r13
mov    %r12d,%edi
mov    %rsp,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %rax,%rbp
test   %rax,%rax
jns    1158 <fd_to_fd+0x38>
jmp    11a6 <fd_to_fd+0x86>
mov    %r13,%r14
mov    %r14,%rax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i64 @fd_to_fd(i32 %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i64 @read(i32 %1, i8* %4, i32 4096)
%6 = icmp slt i64 %5, 0
br i1 %6, label %25, label %7
7: ; preds = %2, %21
%8 = phi i64 [ %23, %21 ], [ %5, %2 ]
%9 = phi i64 [ %22, %21 ], [ 0, %2 ]
%10 = icmp eq i64 %8, 0
br i1 %10, label %25, label %11
11: ; preds = %7, %19
%12 = phi i64 [ %17, %19 ], [ 0, %7 ]
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %12
%14 = sub nsw i64 %8, %12
%15 = call i64 @write(i32 %0, i8* %13, i64 %14)
%16 = icmp slt i64 %15, 0
%17 = add nsw i64 %15, %12
%18 = select i1 %16, i64 %12, i64 %17
br i1 %16, label %25, label %19
19: ; preds = %11
%20 = icmp slt i64 %18, %8
br i1 %20, label %11, label %21
21: ; preds = %19
%22 = add nsw i64 %18, %9
%23 = call i64 @read(i32 %1, i8* %4, i32 4096)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %7
25: ; preds = %7, %21, %11, %2
%26 = phi i64 [ -1, %2 ], [ -1, %11 ], [ %9, %7 ], [ -1, %21 ]
ret i64 %26
}