#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 */
int /*<<< orphan*/ die(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ errno;
int read(int, char *, int);
int write(int, char *, int);
void copyfd(int out, int in) {
  int n;
  char buf[1024];
  while ((n = read(in, buf, sizeof(buf))) != 0) {
    if (write(out, buf, n) != n) {
      die(3, errno, "write");
    }
  }
}


x86 assembly (clang, -O0):
<copyfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     119b <copyfd+0x6b>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <write@plt>
cmp    -0xc(%rbp),%eax
je     1196 <copyfd+0x66>
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    $0x3,%edi
lea    0xe6f(%rip),%rdx
call   1050 <die@plt>
jmp    1141 <copyfd+0x11>
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyfd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %esi,%r12d
mov    %edi,%r14d
mov    %rsp,%rsi
mov    %r12d,%edi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
je     11aa <copyfd+0x7a>
mov    %eax,%ebx
mov    %rsp,%rbp
mov    0x2e79(%rip),%r13
lea    0xe92(%rip),%r15
jmp    1186 <copyfd+0x56>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11aa <copyfd+0x7a>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     1170 <copyfd+0x40>
mov    0x0(%r13),%esi
mov    $0x3,%edi
mov    %r15,%rdx
call   1050 <die@plt>
jmp    1170 <copyfd+0x40>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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