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

/* Variables and functions */
int safe_read(int, char *, int);
int safe_write(int, char *, int);
int copy_fd_to_fd(int ifd, int ofd) {
  while (1) {
    ssize_t res, wes;
    char buf[8196];
    res = safe_read(ifd, buf, sizeof(buf));
    if (res < 0)
      return res | 0x08000000;
    else if (res == 0)
      return 0;
    wes = safe_write(ofd, buf, res);
    if (wes < 0)
      return res & (~0x08000000);
    if (res < (ssize_t)sizeof(buf))
      return 0;
  }
}


x86 assembly (clang, -O0):
<copy_fd_to_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x2004,%edx
call   1030 <safe_read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1162 <copy_fd_to_fd+0x42>
mov    -0x10(%rbp),%eax
or     $0x8000000,%eax
mov    %eax,-0x4(%rbp)
jmp    11ca <copy_fd_to_fd+0xaa>
cmpl   $0x0,-0x10(%rbp)
jne    1178 <copy_fd_to_fd+0x58>
movl   $0x0,-0x4(%rbp)
jmp    11ca <copy_fd_to_fd+0xaa>
jmp    117d <copy_fd_to_fd+0x5d>
mov    -0xc(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1040 <safe_write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ac <copy_fd_to_fd+0x8c>
mov    -0x10(%rbp),%eax
and    $0xf7ffffff,%eax
mov    %eax,-0x4(%rbp)
jmp    11ca <copy_fd_to_fd+0xaa>
cmpl   $0x2004,-0x10(%rbp)
jge    11c5 <copy_fd_to_fd+0xa5>
movl   $0x0,-0x4(%rbp)
jmp    11ca <copy_fd_to_fd+0xaa>
jmp    1131 <copy_fd_to_fd+0x11>
mov    -0x4(%rbp),%eax
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_fd_to_fd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2008,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x2004,%edx
call   1030 <safe_read@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1174 <copy_fd_to_fd+0x54>
je     1170 <copy_fd_to_fd+0x50>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <safe_write@plt>
test   %eax,%eax
js     117c <copy_fd_to_fd+0x5c>
cmp    $0x2003,%ebx
ja     1140 <copy_fd_to_fd+0x20>
xor    %ebx,%ebx
jmp    1182 <copy_fd_to_fd+0x62>
or     $0x8000000,%ebx
jmp    1182 <copy_fd_to_fd+0x62>
and    $0xf7ffffff,%ebx
mov    %ebx,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @copy_fd_to_fd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [8196 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
br label %9
9: ; preds = %2, %37
%10 = load i32, i32* %4
%11 = getelementptr inbounds [8196 x i8], [8196 x i8]* %8, i64 0, i64 0
%12 = call i32 @safe_read(i32 %10, i8* %11, i32 8196)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i32, i32* %6
%17 = or i32 %16, 134217728
store i32 %17, i32* %3
br label %38
18: ; preds = %9
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 0, i32* %3
br label %38
22: ; preds = %18
br label %23
23: ; preds = %22
%24 = load i32, i32* %5
%25 = getelementptr inbounds [8196 x i8], [8196 x i8]* %8, i64 0, i64 0
%26 = load i32, i32* %6
%27 = call i32 @safe_write(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = load i32, i32* %6
%32 = and i32 %31, -134217729
store i32 %32, i32* %3
br label %38
33: ; preds = %23
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 8196
br i1 %35, label %36, label %37
36: ; preds = %33
store i32 0, i32* %3
br label %38
37: ; preds = %33
br label %9
38: ; preds = %36, %30, %21, %15
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
define i32 @copy_fd_to_fd(i32 %0, i32 %1) {
%3 = alloca [8196 x i8]
%4 = getelementptr inbounds [8196 x i8], [8196 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %19, %2
%6 = call i32 @safe_read(i32 %0, i8* %4, i32 8196)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = or i32 %6, 134217728
br label %17
10: ; preds = %5
%11 = icmp eq i32 %6, 0
br i1 %11, label %17, label %12
12: ; preds = %10
%13 = call i32 @safe_write(i32 %1, i8* %4, i32 %6)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = and i32 %6, -134217729
br label %17
17: ; preds = %10, %8, %15
%18 = phi i32 [ %16, %15 ], [ %9, %8 ], [ 0, %10 ]
br label %21
19: ; preds = %12
%20 = icmp ugt i32 %6, 8195
br i1 %20, label %5, label %21
21: ; preds = %19, %17
%22 = phi i32 [ %18, %17 ], [ 0, %19 ]
ret i32 %22
}