#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*/ errno;
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
int cp_rw(int srcfd, int dstfd, char *buf, int len) {
  int nread;
  while ((nread = read(srcfd, buf, len)) > 0) {
    if (write(dstfd, buf, nread) != nread) {
      printf("write error");
      printf(":%s\n", strerror(errno));
      return -1;
    }
  }
  if (nread == -1) {
    printf("read error");
    printf(":%s\n", strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cp_rw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jle    11c6 <cp_rw+0x86>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <write@plt>
cmp    -0x20(%rbp),%eax
je     11c1 <cp_rw+0x81>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <cp_rw+0xd0>
jmp    1155 <cp_rw+0x15>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1209 <cp_rw+0xc9>
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <cp_rw+0xd0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp_rw>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %rdx,%rbx
mov    %esi,%r12d
mov    %edi,%r13d
lea    0xea2(%rip),%r14
xchg   %ax,%ax
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1187 <cp_rw+0x47>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     1160 <cp_rw+0x20>
jmp    1195 <cp_rw+0x55>
xor    %eax,%eax
cmp    $0xffffffff,%ebp
jne    11c3 <cp_rw+0x83>
lea    0xe7c(%rip),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe58(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"write error\00"
@.str.1 = [5 x i8] c":%s\0A\00"
@errno = global i32 0
@.str.2 = [11 x i8] c"read error\00"
define i32 @cp_rw(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
br label %11
11: ; preds = %29, %4
%12 = load i32, i32* %6
%13 = load i8*, i8** %8
%14 = load i32, i32* %9
%15 = call i32 @read(i32 %12, i8* %13, i32 %14)
store i32 %15, i32* %10
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %30
17: ; preds = %11
%18 = load i32, i32* %7
%19 = load i8*, i8** %8
%20 = load i32, i32* %10
%21 = call i32 @write(i32 %18, i8* %19, i32 %20)
%22 = load i32, i32* %10
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %29
24: ; preds = %17
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %27)
store i32 -1, i32* %5
br label %39
29: ; preds = %17
br label %11
30: ; preds = %11
%31 = load i32, i32* %10
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %36)
store i32 -1, i32* %5
br label %39
38: ; preds = %30
store i32 0, i32* %5
br label %39
39: ; preds = %38, %33, %24
%40 = load i32, i32* %5
ret i32 %40
}

LLVM IR (-O2):
@.str = [12 x i8] c"write error\00"
@.str.1 = [5 x i8] c":%s\0A\00"
@errno = global i32 0
@.str.2 = [11 x i8] c"read error\00"
define i32 @cp_rw(i32 %0, i32 %1, i8* nocapture %2, i32 %3) {
br label %5
5: ; preds = %8, %4
%6 = tail call i32 @read(i32 %0, i8* %2, i32 %3)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = tail call i32 @write(i32 %1, i8* %2, i32 %6)
%10 = icmp eq i32 %9, %6
br i1 %10, label %5, label %13
11: ; preds = %5
%12 = icmp eq i32 %6, -1
br i1 %12, label %13, label %19
13: ; preds = %8, %11
%14 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %11 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %8 ]
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %14)
%16 = load i32, i32* @errno
%17 = tail call i8* @strerror(i32 %16)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %13, %11
%20 = phi i32 [ 0, %11 ], [ -1, %13 ]
ret i32 %20
}