#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 /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ perror_wrapper(char *);
int read(int, char *, int);
int /*<<< orphan*/ write_all(int, char *, int);
int copy_fd_all(int fdout, int fdin) {
  int ret;
  char buf[4096];
  for (;;) {
    ret = read(fdin, buf, sizeof(buf));
    if (ret == -1 && errno == EINTR)
      continue;
    if (!ret)
      break;
    if (ret < 0) {
      perror_wrapper("read");
      return 0;
    }
    if (!write_all(fdout, buf, ret)) {
      perror_wrapper("write");
      return 0;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<copy_fd_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1181 <copy_fd_all+0x51>
mov    0x2e77(%rip),%rax
mov    (%rax),%rax
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%rax
jne    1181 <copy_fd_all+0x51>
jmp    1141 <copy_fd_all+0x11>
cmpl   $0x0,-0x10(%rbp)
jne    1190 <copy_fd_all+0x60>
jmp    11ea <copy_fd_all+0xba>
cmpl   $0x0,-0x10(%rbp)
jge    11b2 <copy_fd_all+0x82>
lea    0xe5f(%rip),%rdi
call   1030 <perror_wrapper@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <copy_fd_all+0xc1>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1050 <write_all@plt>
cmp    $0x0,%eax
jne    11e5 <copy_fd_all+0xb5>
lea    0xe31(%rip),%rdi
call   1030 <perror_wrapper@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <copy_fd_all+0xc1>
jmp    1141 <copy_fd_all+0x11>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_fd_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %esi,%ebp
mov    %edi,%r15d
mov    %rsp,%rbx
mov    0x2e90(%rip),%r12
mov    0x2e81(%rip),%r13
lea    0xea2(%rip),%r14
jmp    116a <copy_fd_all+0x3a>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    119c <copy_fd_all+0x6c>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     1160 <copy_fd_all+0x30>
test   %eax,%eax
je     11a8 <copy_fd_all+0x78>
js     119c <copy_fd_all+0x6c>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write_all@plt>
test   %eax,%eax
jne    116a <copy_fd_all+0x3a>
lea    0xe69(%rip),%r14
mov    %r14,%rdi
call   1030 <perror_wrapper@plt>
xor    %eax,%eax
jmp    11ad <copy_fd_all+0x7d>
mov    $0x1,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"read\00"
@.str.1 = [6 x i8] c"write\00"
define i32 @copy_fd_all(i32 %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %11, %2
%6 = call i32 @read(i32 %1, i8* %4, i32 4096)
switch i32 %6, label %12 [
i32 -1, label %7
i32 0, label %20
]
7: ; preds = %5
%8 = load i64, i64* @errno
%9 = load i64, i64* @EINTR
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %17
11: ; preds = %7, %14
br label %5
12: ; preds = %5
%13 = icmp slt i32 %6, 0
br i1 %13, label %17, label %14
14: ; preds = %12
%15 = call i32 @write_all(i32 %0, i8* %4, i32 %6)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %11
17: ; preds = %14, %12, %7
%18 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %7 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %12 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %14 ]
%19 = call i32 @perror_wrapper(i8* %18)
br label %20
20: ; preds = %5, %17
%21 = phi i32 [ 0, %17 ], [ 1, %5 ]
ret i32 %21
}