#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 */
scalar_t__ EAGAIN;
scalar_t__ errno;
int read(int, char *, size_t);
int write(int, char *, int);
int copy(int src_fd, int dest_fd, char *buf, size_t buf_len) {
  int n = read(src_fd, buf, buf_len);
  if (n == -1 && errno == EAGAIN) {
    n = 0;
  } else if (n > 0) {
    n = write(dest_fd, buf, n);
  }
  return n;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1179 <copy+0x59>
mov    0x2e86(%rip),%rax
mov    (%rax),%rax
mov    0x2e6c(%rip),%rcx
cmp    (%rcx),%rax
jne    1179 <copy+0x59>
movl   $0x0,-0x1c(%rbp)
jmp    119a <copy+0x7a>
cmpl   $0x0,-0x1c(%rbp)
jle    1195 <copy+0x75>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
jmp    119a <copy+0x7a>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdx,%rsi
mov    %rcx,%rdx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     114e <copy+0x2e>
test   %eax,%eax
jle    1169 <copy+0x49>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <write@plt>
mov    0x2e8b(%rip),%rax
mov    (%rax),%rcx
mov    0x2e71(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
neg    %eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @copy(i32 %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%10 = load i32, i32* %5
%11 = load i8*, i8** %7
%12 = load i64, i64* %8
%13 = call i32 @read(i32 %10, i8* %11, i64 %12)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %4
%17 = load i64, i64* @errno
%18 = load i64, i64* @EAGAIN
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 0, i32* %9
br label %30
21: ; preds = %16, %4
%22 = load i32, i32* %9
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i32, i32* %6
%26 = load i8*, i8** %7
%27 = load i32, i32* %9
%28 = call i32 @write(i32 %25, i8* %26, i32 %27)
store i32 %28, i32* %9
br label %29
29: ; preds = %24, %21
br label %30
30: ; preds = %29, %20
%31 = load i32, i32* %9
ret i32 %31
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @copy(i32 %0, i32 %1, i8* nocapture %2, i64 %3) {
%5 = tail call i32 @read(i32 %0, i8* %2, i64 %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %4
%8 = load i64, i64* @errno
%9 = load i64, i64* @EAGAIN
%10 = icmp ne i64 %8, %9
%11 = sext i1 %10 to i32
br label %16
12: ; preds = %4
%13 = icmp sgt i32 %5, 0
br i1 %13, label %14, label %16
14: ; preds = %12
%15 = tail call i32 @write(i32 %1, i8* %2, i32 %5)
br label %16
16: ; preds = %7, %12, %14
%17 = phi i32 [ %15, %14 ], [ %5, %12 ], [ %11, %7 ]
ret i32 %17
}