#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 int ssize_t;

/* Variables and functions */
#define EAGAIN 129
#define EINTR 128
int /*<<< orphan*/ EPOLLIN;
int /*<<< orphan*/ block(int, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int, char *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int read(int, char *, size_t);
int write(int, char *, int);
int fd_copy(int ifd, int ofd, size_t bs) {
  if (ifd < 0 || ofd < 0)
    return -1;
  char *buf = malloc(bs * sizeof(char)), *pos;
  ssize_t brd, bwr;
  while ((brd = read(ifd, buf, bs))) {
    if (brd == -1)
      return -1;
    pos = buf;
    while (brd > 0) {
      if ((bwr = write(ofd, buf, brd)) == -1) {
        switch (errno) {
        case EAGAIN:
          block(ifd, EPOLLIN);
          break;
        case EINTR:
          continue;
        default:
          error(0, errno, "write error");
          free(buf);
          return -1;
        }
      }
      brd -= bwr;
      pos += bwr;
    }
  }
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<fd_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1186 <fd_copy+0x26>
cmpl   $0x0,-0xc(%rbp)
jge    1192 <fd_copy+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b3 <fd_copy+0x153>
mov    -0x18(%rbp),%rdi
shl    $0x0,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
je     12a3 <fd_copy+0x143>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11d5 <fd_copy+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b3 <fd_copy+0x153>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    129e <fd_copy+0x13e>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x30(%rbp)
cmp    $0xffffffff,%eax
jne    127d <fd_copy+0x11d>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
sub    $0x80,%eax
je     1247 <fd_copy+0xe7>
jmp    121e <fd_copy+0xbe>
mov    -0x34(%rbp),%eax
sub    $0x81,%eax
jne    124c <fd_copy+0xec>
jmp    1231 <fd_copy+0xd1>
mov    -0x8(%rbp),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
call   1080 <block@plt>
jmp    1278 <fd_copy+0x118>
jmp    11dd <fd_copy+0x7d>
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xda2(%rip),%rdx
call   1070 <error@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b3 <fd_copy+0x153>
jmp    127d <fd_copy+0x11d>
mov    -0x30(%rbp),%ecx
mov    -0x2c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    11dd <fd_copy+0x7d>
jmp    11a3 <fd_copy+0x43>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%eax
or     %edi,%eax
js     1217 <fd_copy+0xb7>
mov    %esi,%r13d
mov    %edi,%r15d
mov    %rdx,(%rsp)
mov    %rdx,%rdi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    0x2e47(%rip),%r14
mov    %r15d,%edi
mov    %r12,%rsi
mov    (%rsp),%rdx
call   1050 <read@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     121c <fd_copy+0xbc>
test   %ebx,%ebx
je     120d <fd_copy+0xad>
jg     11c0 <fd_copy+0x60>
jmp    1191 <fd_copy+0x31>
sub    %ebp,%ebx
jle    1191 <fd_copy+0x31>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13d,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11af <fd_copy+0x4f>
mov    (%r14),%esi
cmp    $0x80,%esi
je     11c0 <fd_copy+0x60>
cmp    $0x81,%esi
jne    11fa <fd_copy+0x9a>
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1080 <block@plt>
jmp    11af <fd_copy+0x4f>
lea    0xdff(%rip),%rdx
xor    %edi,%edi
call   1070 <error@plt>
mov    $0xffffffff,%ebx
mov    %r12,%rdi
call   1030 <free@plt>
jmp    121c <fd_copy+0xbc>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EPOLLIN = global i32 0
@.str = [12 x i8] c"write error\00"
define i32 @fd_copy(i32 %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64 %2, i64* %7
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %14, %3
store i32 -1, i32* %4
br label %68
18: ; preds = %14
%19 = load i64, i64* %7
%20 = mul i64 %19, 1
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %8
br label %22
22: ; preds = %64, %18
%23 = load i32, i32* %5
%24 = load i8*, i8** %8
%25 = load i64, i64* %7
%26 = call i32 @read(i32 %23, i8* %24, i64 %25)
store i32 %26, i32* %10
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %65
28: ; preds = %22
%29 = load i32, i32* %10
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 -1, i32* %4
br label %68
32: ; preds = %28
%33 = load i8*, i8** %8
store i8* %33, i8** %9
br label %34
34: ; preds = %56, %49, %32
%35 = load i32, i32* %10
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %64
37: ; preds = %34
%38 = load i32, i32* %6
%39 = load i8*, i8** %8
%40 = load i32, i32* %10
%41 = call i32 @write(i32 %38, i8* %39, i32 %40)
store i32 %41, i32* %11
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %56
43: ; preds = %37
%44 = load i32, i32* @errno
switch i32 %44, label %50 [
i32 129, label %45
i32 128, label %49
]
45: ; preds = %43
%46 = load i32, i32* %5
%47 = load i32, i32* @EPOLLIN
%48 = call i32 @block(i32 %46, i32 %47)
br label %55
49: ; preds = %43
br label %34
50: ; preds = %43
%51 = load i32, i32* @errno
%52 = call i32 @error(i32 0, i32 %51, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%53 = load i8*, i8** %8
%54 = call i32 @free(i8* %53)
store i32 -1, i32* %4
br label %68
55: ; preds = %45
br label %56
56: ; preds = %55, %37
%57 = load i32, i32* %11
%58 = load i32, i32* %10
%59 = sub nsw i32 %58, %57
store i32 %59, i32* %10
%60 = load i32, i32* %11
%61 = load i8*, i8** %9
%62 = sext i32 %60 to i64
%63 = getelementptr inbounds i8, i8* %61, i64 %62
store i8* %63, i8** %9
br label %34
64: ; preds = %34
br label %22
65: ; preds = %22
%66 = load i8*, i8** %8
%67 = call i32 @free(i8* %66)
store i32 0, i32* %4
br label %68
68: ; preds = %65, %50, %31, %17
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
@errno = global i32 0
@EPOLLIN = global i32 0
@.str = [12 x i8] c"write error\00"
define i32 @fd_copy(i32 %0, i32 %1, i64 %2) {
%4 = or i32 %1, %0
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %31
6: ; preds = %3
%7 = tail call i8* @malloc(i64 %2)
br label %8
8: ; preds = %12, %6
%9 = tail call i32 @read(i32 %0, i8* %7, i64 %2)
switch i32 %9, label %10 [
i32 0, label %28
i32 -1, label %31
]
10: ; preds = %8
%11 = icmp sgt i32 %9, 0
br i1 %11, label %13, label %12
12: ; preds = %25, %10
br label %8
13: ; preds = %10, %25
%14 = phi i32 [ %26, %25 ], [ %9, %10 ]
br label %15
15: ; preds = %13, %18
%16 = tail call i32 @write(i32 %1, i8* %7, i32 %14)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = load i32, i32* @errno
switch i32 %19, label %23 [
i32 129, label %20
i32 128, label %15
]
20: ; preds = %18
%21 = load i32, i32* @EPOLLIN
%22 = tail call i32 @block(i32 %0, i32 %21)
br label %25
23: ; preds = %18
%24 = tail call i32 @error(i32 0, i32 %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %28
25: ; preds = %15, %20
%26 = sub nsw i32 %14, %16
%27 = icmp sgt i32 %26, 0
br i1 %27, label %13, label %12
28: ; preds = %8, %23
%29 = phi i32 [ -1, %23 ], [ %9, %8 ]
%30 = tail call i32 @free(i8* %7)
br label %31
31: ; preds = %8, %28, %3
%32 = phi i32 [ -1, %3 ], [ %29, %28 ], [ %9, %8 ]
ret i32 %32
}