#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 */
char *malloc(int);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int copy_desc_to_desc(int in_fd, int out_fd) {
  int size = 0, now;
  char *buf = malloc(2048);
  while ((now = read(in_fd, buf, 2048)) > 0) {
    size += now;
    write(out_fd, buf, now);
  }
  return size;
}


x86 assembly (clang, -O0):
<copy_desc_to_desc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x800,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x800,%edx
call   1040 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    118d <copy_desc_to_desc+0x5d>
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
jmp    1153 <copy_desc_to_desc+0x23>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_desc_to_desc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r15d
mov    $0x800,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %r15d,%edi
mov    %rax,%rsi
mov    $0x800,%edx
call   1040 <read@plt>
xor    %ebp,%ebp
test   %eax,%eax
jle    1183 <copy_desc_to_desc+0x53>
add    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x800,%edx
call   1040 <read@plt>
test   %eax,%eax
jg     1160 <copy_desc_to_desc+0x30>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @copy_desc_to_desc(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
%8 = call i8* @malloc(i32 2048)
store i8* %8, i8** %7
br label %9
9: ; preds = %14, %2
%10 = load i32, i32* %3
%11 = load i8*, i8** %7
%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 %22
14: ; preds = %9
%15 = load i32, i32* %6
%16 = load i32, i32* %5
%17 = add nsw i32 %16, %15
store i32 %17, i32* %5
%18 = load i32, i32* %4
%19 = load i8*, i8** %7
%20 = load i32, i32* %6
%21 = call i32 @write(i32 %18, i8* %19, i32 %20)
br label %9
22: ; preds = %9
%23 = load i32, i32* %5
ret i32 %23
}

LLVM IR (-O2):
define i32 @copy_desc_to_desc(i32 %0, i32 %1) {
%3 = tail call dereferenceable_or_null(2048) i8* @malloc(i32 2048)
%4 = tail call i32 @read(i32 %0, i8* %3, i32 2048)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %2, %6
%7 = phi i32 [ %11, %6 ], [ %4, %2 ]
%8 = phi i32 [ %9, %6 ], [ 0, %2 ]
%9 = add nsw i32 %7, %8
%10 = tail call i32 @write(i32 %1, i8* %3, i32 %7)
%11 = tail call i32 @read(i32 %0, i8* %3, i32 2048)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %6, label %13
13: ; preds = %6, %2
%14 = phi i32 [ 0, %2 ], [ %9, %6 ]
ret i32 %14
}