#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 off_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *malloc(int);
int read(int, char *, int);
int write(int, char *, int);
int fd_copy(int from, int to, off_t count) {
  char *buf = malloc(4096);
  int reads, wrote;
  int retval = 0;
  while (count > 0 &&
         (reads = read(from, buf, 4096 < count ? 4096 : count)) > 0) {
    count -= reads;
    wrote = write(to, buf, reads);
    if (wrote != reads) {
      retval = 1;
      break;
    }
  }
  free(buf);
  return retval;
}


x86 assembly (clang, -O0):
<fd_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0x25(%rbp)
jle    11bf <fd_copy+0x7f>
mov    -0x4(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x1000,%eax
cmp    -0xc(%rbp),%eax
jge    119e <fd_copy+0x5e>
mov    $0x1000,%eax
mov    %eax,-0x38(%rbp)
jmp    11a4 <fd_copy+0x64>
mov    -0xc(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edi
mov    -0x38(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
setg   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    11cf <fd_copy+0x8f>
jmp    1209 <fd_copy+0xc9>
mov    -0x1c(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1204 <fd_copy+0xc4>
movl   $0x1,-0x24(%rbp)
jmp    1209 <fd_copy+0xc9>
jmp    1166 <fd_copy+0x26>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r14d
mov    %edi,%r15d
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %ebp,%ebp
jle    11ac <fd_copy+0x6c>
cmp    $0x1000,%ebp
mov    $0x1000,%edx
cmovb  %ebp,%edx
mov    %r15d,%edi
mov    %r12,%rsi
call   1050 <read@plt>
test   %eax,%eax
jle    11ac <fd_copy+0x6c>
mov    %eax,%ebx
sub    %eax,%ebp
mov    %r14d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    %ebx,%eax
je     1170 <fd_copy+0x30>
mov    $0x1,%r13d
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fd_copy(i32 %0, i32 %1, i32 %2) {
%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
store i32 %2, i32* %6
%11 = call i8* @malloc(i32 4096)
store i8* %11, i8** %7
store i32 0, i32* %10
br label %12
12: ; preds = %41, %3
%13 = load i32, i32* %6
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %27
15: ; preds = %12
%16 = load i32, i32* %4
%17 = load i8*, i8** %7
%18 = load i32, i32* %6
%19 = icmp slt i32 4096, %18
br i1 %19, label %20, label %21
20: ; preds = %15
br label %23
21: ; preds = %15
%22 = load i32, i32* %6
br label %23
23: ; preds = %21, %20
%24 = phi i32 [ 4096, %20 ], [ %22, %21 ]
%25 = call i32 @read(i32 %16, i8* %17, i32 %24)
store i32 %25, i32* %8
%26 = icmp sgt i32 %25, 0
br label %27
27: ; preds = %23, %12
%28 = phi i1 [ false, %12 ], [ %26, %23 ]
br i1 %28, label %29, label %42
29: ; preds = %27
%30 = load i32, i32* %8
%31 = load i32, i32* %6
%32 = sub nsw i32 %31, %30
store i32 %32, i32* %6
%33 = load i32, i32* %5
%34 = load i8*, i8** %7
%35 = load i32, i32* %8
%36 = call i32 @write(i32 %33, i8* %34, i32 %35)
store i32 %36, i32* %9
%37 = load i32, i32* %9
%38 = load i32, i32* %8
%39 = icmp ne i32 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %29
store i32 1, i32* %10
br label %42
41: ; preds = %29
br label %12
42: ; preds = %40, %27
%43 = load i8*, i8** %7
%44 = call i32 @free(i8* %43)
%45 = load i32, i32* %10
ret i32 %45
}

LLVM IR (-O2):
define i32 @fd_copy(i32 %0, i32 %1, i32 %2) {
%4 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
br label %5
5: ; preds = %13, %3
%6 = phi i32 [ %2, %3 ], [ %14, %13 ]
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %17
8: ; preds = %5
%9 = icmp ult i32 %6, 4096
%10 = select i1 %9, i32 %6, i32 4096
%11 = tail call i32 @read(i32 %0, i8* %4, i32 %10)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = sub nsw i32 %6, %11
%15 = tail call i32 @write(i32 %1, i8* %4, i32 %11)
%16 = icmp eq i32 %15, %11
br i1 %16, label %5, label %17
17: ; preds = %13, %5, %8
%18 = phi i32 [ 0, %8 ], [ 0, %5 ], [ 1, %13 ]
%19 = tail call i32 @free(i8* %4)
ret i32 %18
}