#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 */

/* Variables and functions */
int read(int, char *, int);
int write(int, char *, int);
void fdcopy(int dst, int src) {
  char buf[2048];
  int r, w;
  while ((r = read(src, buf, sizeof buf)) > 0) {
    char *p = buf;
    while (r > 0) {
      w = write(dst, p, r);
      if (w < 0)
        return;
      p += w;
      r -= w;
    }
  }
}


x86 assembly (clang, -O0):
<fdcopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x800,%edx
call   1040 <read@plt>
mov    %eax,-0x814(%rbp)
cmp    $0x0,%eax
jle    11d4 <fdcopy+0xb4>
lea    -0x810(%rbp),%rax
mov    %rax,-0x820(%rbp)
cmpl   $0x0,-0x814(%rbp)
jle    11cf <fdcopy+0xaf>
mov    -0x4(%rbp),%edi
mov    -0x820(%rbp),%rsi
mov    -0x814(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x818(%rbp)
cmpl   $0x0,-0x818(%rbp)
jge    119c <fdcopy+0x7c>
jmp    11d4 <fdcopy+0xb4>
mov    -0x818(%rbp),%ecx
mov    -0x820(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x820(%rbp)
mov    -0x818(%rbp),%ecx
mov    -0x814(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x814(%rbp)
jmp    1162 <fdcopy+0x42>
jmp    1131 <fdcopy+0x11>
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fdcopy>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    %esi,%r15d
mov    %edi,%r12d
mov    %rsp,%r14
mov    %esi,%edi
mov    %r14,%rsi
mov    $0x800,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1183 <fdcopy+0x63>
mov    %eax,%ebx
mov    %r14,%rbp
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1183 <fdcopy+0x63>
mov    %eax,%ecx
add    %rcx,%rbp
sub    %eax,%ebx
jg     1150 <fdcopy+0x30>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x800,%edx
call   1040 <read@plt>
mov    %eax,%ebx
mov    %r14,%rbp
test   %eax,%eax
jg     1150 <fdcopy+0x30>
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @fdcopy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2048 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 %0, i32* %3
store i32 %1, i32* %4
br label %9
9: ; preds = %35, %2
%10 = load i32, i32* %4
%11 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 2048)
store i32 %12, i32* %6
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %36
14: ; preds = %9
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
store i8* %15, i8** %8
br label %16
16: ; preds = %27, %14
%17 = load i32, i32* %6
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %35
19: ; preds = %16
%20 = load i32, i32* %3
%21 = load i8*, i8** %8
%22 = load i32, i32* %6
%23 = call i32 @write(i32 %20, i8* %21, i32 %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %19
br label %36
27: ; preds = %19
%28 = load i32, i32* %7
%29 = load i8*, i8** %8
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8* %31, i8** %8
%32 = load i32, i32* %7
%33 = load i32, i32* %6
%34 = sub nsw i32 %33, %32
store i32 %34, i32* %6
br label %16
35: ; preds = %16
br label %9
36: ; preds = %26, %9
ret void
}

LLVM IR (-O2):
define void @fdcopy(i32 %0, i32 %1) {
%3 = alloca [2048 x i8]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%5 = call i32 @read(i32 %1, i8* %4, i32 2048)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %10, label %23
7: ; preds = %15
%8 = call i32 @read(i32 %1, i8* %4, i32 2048)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %20, label %23
10: ; preds = %2, %20
%11 = phi i8* [ %21, %20 ], [ %4, %2 ]
%12 = phi i32 [ %22, %20 ], [ %5, %2 ]
%13 = call i32 @write(i32 %0, i8* %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %10
%16 = zext i32 %13 to i64
%17 = getelementptr inbounds i8, i8* %11, i64 %16
%18 = sub nsw i32 %12, %13
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %7
20: ; preds = %15, %7
%21 = phi i8* [ %17, %15 ], [ %4, %7 ]
%22 = phi i32 [ %18, %15 ], [ %8, %7 ]
br label %10
23: ; preds = %7, %10, %2
ret void
}