#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 */
scalar_t__ EINTR;
int PIPE_BUF;
scalar_t__ errno;
int read(int, char *, int);
int write_chunk(int, char *, int);
int fdcopy(int src, int dst) {
  char buf[PIPE_BUF];
  ssize_t size;
  while ((size = read(src, buf, sizeof buf)) != 0) {
    if (size == -1) {
      if (errno == EINTR)
        continue;
      return -1;
    }
    if (write_chunk(dst, buf, size) == -1)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<fdcopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rax
mov    -0x8(%rbp),%edi
mov    %eax,%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     11e8 <fdcopy+0xc8>
cmpl   $0xffffffff,-0x24(%rbp)
jne    11b8 <fdcopy+0x98>
mov    0x2e53(%rip),%rax
mov    (%rax),%rax
mov    0x2e41(%rip),%rcx
cmp    (%rcx),%rax
jne    11a5 <fdcopy+0x85>
jmp    115e <fdcopy+0x3e>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    11f6 <fdcopy+0xd6>
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    -0x24(%rbp),%edx
call   1040 <write_chunk@plt>
cmp    $0xffffffff,%eax
jne    11e3 <fdcopy+0xc3>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    11f6 <fdcopy+0xd6>
jmp    115e <fdcopy+0x3e>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fdcopy>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r15d
mov    0x2e95(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
mov    %r12,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e7f(%rip),%r13
jmp    1170 <fdcopy+0x50>
nopl   0x0(%rax,%rax,1)
mov    0x2e79(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
jne    1199 <fdcopy+0x79>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     1160 <fdcopy+0x40>
test   %eax,%eax
je     119e <fdcopy+0x7e>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write_chunk@plt>
cmp    $0xffffffff,%eax
jne    1170 <fdcopy+0x50>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PIPE_BUF = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @fdcopy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* @PIPE_BUF
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
br label %14
14: ; preds = %34, %26, %2
%15 = load i32, i32* %4
%16 = trunc i64 %11 to i32
%17 = call i32 @read(i32 %15, i8* %13, i32 %16)
store i32 %17, i32* %8
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %35
19: ; preds = %14
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
br label %14
27: ; preds = %22
store i32 -1, i32* %3
store i32 1, i32* %9
br label %36
28: ; preds = %19
%29 = load i32, i32* %5
%30 = load i32, i32* %8
%31 = call i32 @write_chunk(i32 %29, i8* %13, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %28
store i32 -1, i32* %3
store i32 1, i32* %9
br label %36
34: ; preds = %28
br label %14
35: ; preds = %14
store i32 0, i32* %3
store i32 1, i32* %9
br label %36
36: ; preds = %35, %33, %27
%37 = load i8*, i8** %6
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@PIPE_BUF = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @fdcopy(i32 %0, i32 %1) {
%3 = load i32, i32* @PIPE_BUF
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
br label %6
6: ; preds = %12, %2
%7 = call i32 @read(i32 %0, i8* %5, i32 %3)
switch i32 %7, label %13 [
i32 0, label %16
i32 -1, label %8
]
8: ; preds = %6
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %16
12: ; preds = %8, %13
br label %6
13: ; preds = %6
%14 = call i32 @write_chunk(i32 %1, i8* %5, i32 %7)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %12
16: ; preds = %6, %13, %8
%17 = phi i32 [ -1, %8 ], [ -1, %13 ], [ %7, %6 ]
ret i32 %17
}