#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 */
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int write(int, char *, int);
int copyData(int fromFd, int toFd, char *buf, ssize_t len) {
  int retVal = -1;
  ssize_t rdSize = read(fromFd, buf, len);
  if (rdSize > 0) {
    ssize_t wrSize = write(toFd, buf, rdSize);
    if (wrSize == -1) {
      fprintf(stderr, "fd-proxy: write failure errno=%d %s\n", errno,
              strerror(errno));
    } else if (wrSize != rdSize) {
      fprintf(stderr, "fd-proxy: wrote only %lu of %lu bytes.", wrSize, rdSize);
    } else if (wrSize > 0) {
      retVal = 0;
    }
  } else if (rdSize == -1) {
    fprintf(stderr, "fd-proxy: read failure errno=%d %s\n", errno,
            strerror(errno));
  }
  return retVal;
}


x86 assembly (clang, -O0):
<copyData>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1224 <copyData+0xe4>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11d6 <copyData+0x96>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%edx
mov    %rax,%rcx
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    121f <copyData+0xdf>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1204 <copyData+0xc4>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xe2d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    121a <copyData+0xda>
cmpl   $0x0,-0x20(%rbp)
jle    1215 <copyData+0xd5>
movl   $0x0,-0x18(%rbp)
jmp    121a <copyData+0xda>
jmp    121f <copyData+0xdf>
jmp    1270 <copyData+0x130>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    126b <copyData+0x12b>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%edx
mov    %rax,%rcx
lea    0xde8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1270 <copyData+0x130>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyData>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdx,%rsi
mov    %ecx,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1193 <copyData+0x53>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11cf <copyData+0x8f>
xor    %ebx,%ebx
cmp    %ebp,%eax
je     1202 <copyData+0xc2>
mov    0x2e63(%rip),%rcx
mov    (%rcx),%edi
lea    0xe9f(%rip),%rsi
mov    %eax,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11fd <copyData+0xbd>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%ebp
jne    1202 <copyData+0xc2>
mov    0x2e3c(%rip),%rax
mov    (%rax),%r14d
mov    0x2e2a(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1060 <strerror@plt>
lea    0xe8e(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1202 <copyData+0xc2>
mov    0x2e0a(%rip),%rax
mov    (%rax),%ebx
mov    0x2df9(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1060 <strerror@plt>
lea    0xe11(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"fd-proxy: write failure errno=%d %s\0A\00"
@errno = global i32 0
@.str.1 = [39 x i8] c"fd-proxy: wrote only %lu of %lu bytes.\00"
@.str.2 = [36 x i8] c"fd-proxy: read failure errno=%d %s\0A\00"
define i32 @copyData(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 -1, i32* %9
%12 = load i32, i32* %5
%13 = load i8*, i8** %7
%14 = load i32, i32* %8
%15 = call i32 @read(i32 %12, i8* %13, i32 %14)
store i32 %15, i32* %10
%16 = load i32, i32* %10
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %47
18: ; preds = %4
%19 = load i32, i32* %6
%20 = load i8*, i8** %7
%21 = load i32, i32* %10
%22 = call i32 @write(i32 %19, i8* %20, i32 %21)
store i32 %22, i32* %11
%23 = load i32, i32* %11
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %31
25: ; preds = %18
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %27, i8* %29)
br label %46
31: ; preds = %18
%32 = load i32, i32* %11
%33 = load i32, i32* %10
%34 = icmp ne i32 %32, %33
br i1 %34, label %35, label %40
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = load i32, i32* %11
%38 = load i32, i32* %10
%39 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %37, i32 %38)
br label %45
40: ; preds = %31
%41 = load i32, i32* %11
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %40
store i32 0, i32* %9
br label %44
44: ; preds = %43, %40
br label %45
45: ; preds = %44, %35
br label %46
46: ; preds = %45, %25
br label %57
47: ; preds = %4
%48 = load i32, i32* %10
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %56
50: ; preds = %47
%51 = load i32, i32* @stderr
%52 = load i32, i32* @errno
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %52, i8* %54)
br label %56
56: ; preds = %50, %47
br label %57
57: ; preds = %56, %46
%58 = load i32, i32* %9
ret i32 %58
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"fd-proxy: write failure errno=%d %s\0A\00"
@errno = global i32 0
@.str.1 = [39 x i8] c"fd-proxy: wrote only %lu of %lu bytes.\00"
@.str.2 = [36 x i8] c"fd-proxy: read failure errno=%d %s\0A\00"
define i32 @copyData(i32 %0, i32 %1, i8* nocapture %2, i32 %3) {
%5 = tail call i32 @read(i32 %0, i8* %2, i32 %3)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %20
7: ; preds = %4
%8 = tail call i32 @write(i32 %1, i8* %2, i32 %5)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %13)
br label %27
15: ; preds = %7
%16 = icmp eq i32 %8, %5
br i1 %16, label %27, label %17
17: ; preds = %15
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %5)
br label %27
20: ; preds = %4
%21 = icmp eq i32 %5, -1
br i1 %21, label %22, label %27
22: ; preds = %20
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = tail call i8* @strerror(i32 %24)
%26 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %24, i8* %25)
br label %27
27: ; preds = %10, %17, %15, %20, %22
%28 = phi i32 [ -1, %22 ], [ -1, %20 ], [ -1, %10 ], [ -1, %17 ], [ 0, %15 ]
ret i32 %28
}