#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 errno;
long fd_aio_read(int, char *, int);
long fd_aio_write(int, char *, long);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, int);
long int calipso_aio_sendfile(int out_fd, int in_fd, size_t size) {
  char *buf;
  long int cc, ww;
  long int total;
  total = 0;
  while (size > 0) {
    buf = malloc((1024 * 4));
    if ((cc = fd_aio_read(in_fd, buf, (1024 * 4))) < 0 ||
        (ww = fd_aio_write(out_fd, buf, cc)) < 0) {
      return -1;
    } else if (cc == 0 || out_fd < 0) {
      printf("AIO errno %d\n", errno);
      break;
    }
    total += cc;
    size -= cc;
    free(buf);
  }
  return total;
}


x86 assembly (clang, -O0):
<calipso_aio_sendfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    122a <calipso_aio_sendfile+0xda>
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <fd_aio_read@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jl     11c0 <calipso_aio_sendfile+0x70>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1070 <fd_aio_write@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jge    11cd <calipso_aio_sendfile+0x7d>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1232 <calipso_aio_sendfile+0xe2>
cmpq   $0x0,-0x28(%rbp)
je     11e2 <calipso_aio_sendfile+0x92>
cmpl   $0x0,-0xc(%rbp)
jge    11fc <calipso_aio_sendfile+0xac>
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
lea    0xe0e(%rip),%rdi
call   1050 <printf@plt>
jmp    122a <calipso_aio_sendfile+0xda>
jmp    1201 <calipso_aio_sendfile+0xb1>
mov    -0x28(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    116a <calipso_aio_sendfile+0x1a>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calipso_aio_sendfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,0x4(%rsp)
test   %rdx,%rdx
je     11cf <calipso_aio_sendfile+0x7f>
mov    %rdx,%r12
mov    %edi,%r15d
xor    %r14d,%r14d
nopl   (%rax)
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,%r13
mov    0x4(%rsp),%edi
mov    %rax,%rsi
mov    $0x1000,%edx
call   1040 <fd_aio_read@plt>
mov    $0xffffffffffffffff,%rbx
test   %rax,%rax
js     11eb <calipso_aio_sendfile+0x9b>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %r13,%rsi
mov    %rax,%rdx
call   1070 <fd_aio_write@plt>
test   %rax,%rax
js     11eb <calipso_aio_sendfile+0x9b>
test   %r15d,%r15d
js     11d3 <calipso_aio_sendfile+0x83>
test   %rbp,%rbp
je     11d3 <calipso_aio_sendfile+0x83>
add    %rbp,%r14
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r14,%rbx
sub    %rbp,%r12
jne    1170 <calipso_aio_sendfile+0x20>
jmp    11eb <calipso_aio_sendfile+0x9b>
xor    %ebx,%ebx
jmp    11eb <calipso_aio_sendfile+0x9b>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
lea    0xe1d(%rip),%rdi
call   1050 <printf@plt>
mov    %r14,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"AIO errno %d\0A\00"
@errno = global i32 0
define i64 @calipso_aio_sendfile(i32 %0, i32 %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64 %2, i64* %7
store i64 0, i64* %11
br label %12
12: ; preds = %38, %3
%13 = load i64, i64* %7
%14 = icmp ugt i64 %13, 0
br i1 %14, label %15, label %47
15: ; preds = %12
%16 = call i8* @malloc(i32 4096)
store i8* %16, i8** %8
%17 = load i32, i32* %6
%18 = load i8*, i8** %8
%19 = call i64 @fd_aio_read(i32 %17, i8* %18, i32 4096)
store i64 %19, i64* %9
%20 = icmp slt i64 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %15
%22 = load i32, i32* %5
%23 = load i8*, i8** %8
%24 = load i64, i64* %9
%25 = call i64 @fd_aio_write(i32 %22, i8* %23, i64 %24)
store i64 %25, i64* %10
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21, %15
store i64 -1, i64* %4
br label %49
28: ; preds = %21
%29 = load i64, i64* %9
%30 = icmp eq i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = load i32, i32* %5
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31, %28
%35 = load i32, i32* @errno
%36 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %35)
br label %47
37: ; preds = %31
br label %38
38: ; preds = %37
%39 = load i64, i64* %9
%40 = load i64, i64* %11
%41 = add nsw i64 %40, %39
store i64 %41, i64* %11
%42 = load i64, i64* %9
%43 = load i64, i64* %7
%44 = sub i64 %43, %42
store i64 %44, i64* %7
%45 = load i8*, i8** %8
%46 = call i32 @free(i8* %45)
br label %12
47: ; preds = %34, %12
%48 = load i64, i64* %11
store i64 %48, i64* %4
br label %49
49: ; preds = %47, %27
%50 = load i64, i64* %4
ret i64 %50
}

LLVM IR (-O2):
@.str = [14 x i8] c"AIO errno %d\0A\00"
@errno = global i32 0
define i64 @calipso_aio_sendfile(i32 %0, i32 %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %27, label %5
5: ; preds = %3
%6 = icmp slt i32 %0, 0
br label %7
7: ; preds = %5, %22
%8 = phi i64 [ 0, %5 ], [ %23, %22 ]
%9 = phi i64 [ %2, %5 ], [ %24, %22 ]
%10 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%11 = tail call i64 @fd_aio_read(i32 %1, i8* %10, i32 4096)
%12 = icmp slt i64 %11, 0
br i1 %12, label %27, label %13
13: ; preds = %7
%14 = tail call i64 @fd_aio_write(i32 %0, i8* %10, i64 %11)
%15 = icmp slt i64 %14, 0
br i1 %15, label %27, label %16
16: ; preds = %13
%17 = icmp eq i64 %11, 0
%18 = or i1 %6, %17
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @errno
%21 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %27
22: ; preds = %16
%23 = add nsw i64 %11, %8
%24 = sub i64 %9, %11
%25 = tail call i32 @free(i8* %10)
%26 = icmp eq i64 %24, 0
br i1 %26, label %27, label %7
27: ; preds = %13, %7, %22, %3, %19
%28 = phi i64 [ %8, %19 ], [ 0, %3 ], [ -1, %13 ], [ -1, %7 ], [ %23, %22 ]
ret i64 %28
}