#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 */
int /*<<< orphan*/ assert(int);
int read(int, unsigned char *, int const);
int write(int, unsigned char *, int);
int copy_file_by_fd(int src, int dst) {
  assert(src >= 0);
  assert(dst >= 0);
  ssize_t len;
  static const int BUFLEN = 1024;
  unsigned char buf[BUFLEN];
  while ((len = read(src, buf, BUFLEN)) > 0) {
    int done = 0;
    while (done != len) {
      int rv = write(dst, buf + done, len - done);
      if (rv == -1)
        return -1;
      done += rv;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy_file_by_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpl   $0x0,-0xc(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    %rsp,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    11ec <copy_file_by_fd+0xbc>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     11e7 <copy_file_by_fd+0xb7>
mov    -0xc(%rbp),%edi
movslq -0x1c(%rbp),%rax
lea    -0x430(%rbp),%rsi
add    %rax,%rsi
mov    -0x10(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11d9 <copy_file_by_fd+0xa9>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    11fa <copy_file_by_fd+0xca>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1191 <copy_file_by_fd+0x61>
jmp    116a <copy_file_by_fd+0x3a>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file_by_fd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %esi,%r12d
mov    %edi,%r15d
not    %edi
shr    $0x1f,%edi
call   1050 <assert@plt>
mov    %r12d,%edi
not    %edi
shr    $0x1f,%edi
call   1050 <assert@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11b6 <copy_file_by_fd+0x86>
mov    %eax,%ebp
mov    %rsp,%r14
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edx
sub    %ebx,%edx
je     11a0 <copy_file_by_fd+0x70>
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
mov    %r12d,%edi
call   1030 <write@plt>
add    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1180 <copy_file_by_fd+0x50>
jmp    11ba <copy_file_by_fd+0x8a>
xchg   %ax,%ax
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1175 <copy_file_by_fd+0x45>
xor    %eax,%eax
jmp    11bf <copy_file_by_fd+0x8f>
mov    $0xffffffff,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@copy_file_by_fd.BUFLEN = internal constant i32 1024
define i32 @copy_file_by_fd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = load i32, i32* %4
%12 = icmp sge i32 %11, 0
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* %5
%16 = icmp sge i32 %15, 0
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %7
%20 = alloca i8, i64 1024
br label %21
21: ; preds = %46, %2
%22 = load i32, i32* %4
%23 = call i32 @read(i32 %22, i8* %20, i32 1024)
store i32 %23, i32* %6
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %47
25: ; preds = %21
store i32 0, i32* %8
br label %26
26: ; preds = %42, %25
%27 = load i32, i32* %8
%28 = load i32, i32* %6
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %46
30: ; preds = %26
%31 = load i32, i32* %5
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %20, i64 %33
%35 = load i32, i32* %6
%36 = load i32, i32* %8
%37 = sub nsw i32 %35, %36
%38 = call i32 @write(i32 %31, i8* %34, i32 %37)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %42
41: ; preds = %30
store i32 -1, i32* %3
store i32 1, i32* %10
br label %48
42: ; preds = %30
%43 = load i32, i32* %9
%44 = load i32, i32* %8
%45 = add nsw i32 %44, %43
store i32 %45, i32* %8
br label %26
46: ; preds = %26
br label %21
47: ; preds = %21
store i32 0, i32* %3
store i32 1, i32* %10
br label %48
48: ; preds = %47, %41
%49 = load i8*, i8** %7
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
define i32 @copy_file_by_fd(i32 %0, i32 %1) {
%3 = xor i32 %0, -1
%4 = lshr i32 %3, 31
%5 = tail call i32 @assert(i32 %4)
%6 = xor i32 %1, -1
%7 = lshr i32 %6, 31
%8 = tail call i32 @assert(i32 %7)
%9 = alloca [1024 x i8]
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%11 = call i32 @read(i32 %0, i8* %10, i32 1024)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %16, label %28
13: ; preds = %18
%14 = call i32 @read(i32 %0, i8* %10, i32 1024)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %28
16: ; preds = %2, %13
%17 = phi i32 [ %14, %13 ], [ %11, %2 ]
br label %18
18: ; preds = %16, %21
%19 = phi i32 [ %27, %21 ], [ 0, %16 ]
%20 = icmp eq i32 %17, %19
br i1 %20, label %13, label %21
21: ; preds = %18
%22 = sext i32 %19 to i64
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %22
%24 = sub nsw i32 %17, %19
%25 = call i32 @write(i32 %1, i8* %23, i32 %24)
%26 = icmp eq i32 %25, -1
%27 = add nsw i32 %25, %19
br i1 %26, label %28, label %18
28: ; preds = %13, %21, %2
%29 = phi i32 [ 0, %2 ], [ -1, %21 ], [ 0, %13 ]
ret i32 %29
}