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

/* Variables and functions */
scalar_t__ check(int /*<<< orphan*/, char *);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ read(int, char *, int);
scalar_t__ write(int, char *, scalar_t__);
void copy_fd(int in, int out) {
  ssize_t read_cnt;
  char buf[4096];
  while ((read_cnt = check(read(in, buf, sizeof(buf)), "read(copy_fd)")) > 0) {
    if (write(out, buf, read_cnt) != read_cnt) {
      err(1, "write(copy_fd)");
    }
  }
}


x86 assembly (clang, -O0):
<copy_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,%edi
lea    0xe92(%rip),%rsi
call   1060 <check@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jle    11b4 <copy_fd+0x74>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x10(%rbp),%rax
je     11af <copy_fd+0x6f>
mov    $0x1,%edi
lea    0xe64(%rip),%rsi
call   1050 <err@plt>
jmp    1151 <copy_fd+0x11>
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %esi,%r14d
mov    %edi,%r13d
mov    %rsp,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
lea    0xe95(%rip),%rsi
mov    %eax,%edi
call   1060 <check@plt>
test   %rax,%rax
jle    11d4 <copy_fd+0x94>
mov    %rax,%rbp
mov    %rsp,%rbx
lea    0xe7c(%rip),%r15
lea    0xe83(%rip),%r12
jmp    11b2 <copy_fd+0x72>
nopl   (%rax)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,%edi
mov    %r15,%rsi
call   1060 <check@plt>
mov    %rax,%rbp
test   %rax,%rax
jle    11d4 <copy_fd+0x94>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
cmp    %rbp,%rax
je     1190 <copy_fd+0x50>
mov    $0x1,%edi
mov    %r12,%rsi
call   1050 <err@plt>
jmp    1190 <copy_fd+0x50>
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"read(copy_fd)\00"
@.str.1 = [15 x i8] c"write(copy_fd)\00"
define void @copy_fd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca [4096 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
br label %7
7: ; preds = %22, %2
%8 = load i32, i32* %3
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%10 = call i32 @read(i32 %8, i8* %9, i32 4096)
%11 = call i64 @check(i32 %10, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i64 %11, i64* %5
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %7
%14 = load i32, i32* %4
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%16 = load i64, i64* %5
%17 = call i64 @write(i32 %14, i8* %15, i64 %16)
%18 = load i64, i64* %5
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %13
br label %7
23: ; preds = %7
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"read(copy_fd)\00"
@.str.1 = [15 x i8] c"write(copy_fd)\00"
define void @copy_fd(i32 %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i32 @read(i32 %0, i8* %4, i32 4096)
%6 = tail call i64 @check(i32 %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%7 = icmp sgt i64 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %2, %14
%9 = phi i64 [ %16, %14 ], [ %6, %2 ]
%10 = call i64 @write(i32 %1, i8* %4, i64 %9)
%11 = icmp eq i64 %10, %9
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = call i32 @read(i32 %0, i8* %4, i32 4096)
%16 = tail call i64 @check(i32 %15, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%17 = icmp sgt i64 %16, 0
br i1 %17, label %8, label %18
18: ; preds = %14, %2
ret void
}