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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
void *malloc(int);
int /*<<< orphan*/ perror(char *);
int sread(int, void *, unsigned int);
int swrite(int, void *, unsigned int);
int transfer_file_copy(int out_fd, int in_fd, off_t size_remain,
                       void (*progress)(double percent)) {
  void *buf = malloc(65536);
  if (!buf) {
    perror("malloc error");
    exit(1);
  }
  off_t orig_size = size_remain;
  while (size_remain > 0) {
    unsigned int send_this_time;
    if (size_remain > 65536) {
      send_this_time = 65536;
    } else {
      send_this_time = (unsigned int)size_remain;
    }
    if (sread(in_fd, buf, send_this_time) == -1)
      return -1;
    if (swrite(out_fd, buf, send_this_time) == -1)
      return -1;
    size_remain -= send_this_time;
    if (progress != NULL)
      progress((orig_size - size_remain) / (double)orig_size);
  }
  return 0;
}


x86 assembly (clang, -O0):
<transfer_file_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    $0x10000,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1195 <transfer_file_copy+0x45>
lea    0xe7a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    124e <transfer_file_copy+0xfe>
cmpq   $0x10000,-0x18(%rbp)
jle    11c2 <transfer_file_copy+0x72>
movl   $0x10000,-0x34(%rbp)
jmp    11c9 <transfer_file_copy+0x79>
mov    -0x18(%rbp),%rax
mov    %eax,-0x34(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <sread@plt>
cmp    $0xffffffff,%eax
jne    11ed <transfer_file_copy+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <transfer_file_copy+0x105>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1060 <swrite@plt>
cmp    $0xffffffff,%eax
jne    1211 <transfer_file_copy+0xc1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <transfer_file_copy+0x105>
mov    -0x34(%rbp),%eax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1249 <transfer_file_copy+0xf9>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    -0x18(%rbp),%rcx
cvtsi2sd %rcx,%xmm0
cvtsi2sdq -0x30(%rbp),%xmm1
divsd  %xmm1,%xmm0
call   *%rax
jmp    119d <transfer_file_copy+0x4d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<transfer_file_copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r13
mov    %rdx,%r15
mov    %esi,%r12d
mov    %edi,0x4(%rsp)
mov    $0x10000,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     1212 <transfer_file_copy+0xc2>
test   %r15,%r15
jle    11fd <transfer_file_copy+0xad>
mov    %rax,%rbp
cvtsi2sd %r15,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %r15,0x10(%rsp)
mov    $0xffffffff,%r14d
jmp    11a5 <transfer_file_copy+0x55>
xchg   %ax,%ax
test   %r15,%r15
jle    11fd <transfer_file_copy+0xad>
cmp    $0x10000,%r15
mov    $0x10000,%ebx
cmovb  %r15,%rbx
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <sread@plt>
cmp    $0xffffffff,%eax
je     1200 <transfer_file_copy+0xb0>
mov    0x4(%rsp),%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1060 <swrite@plt>
cmp    $0xffffffff,%eax
je     1200 <transfer_file_copy+0xb0>
sub    %rbx,%r15
test   %r13,%r13
je     11a0 <transfer_file_copy+0x50>
mov    0x10(%rsp),%rax
sub    %r15,%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  0x8(%rsp),%xmm0
call   *%r13
jmp    11a0 <transfer_file_copy+0x50>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde7(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"malloc error\00"
define i32 @transfer_file_copy(i32 %0, i32 %1, i64 %2, void (double)* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca void (double)*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i64 %2, i64* %8
store void (double)* %3, void (double)** %9
%13 = call i8* @malloc(i32 65536)
store i8* %13, i8** %10
%14 = load i8*, i8** %10
%15 = icmp ne i8* %14, null
br i1 %15, label %19, label %16
16: ; preds = %4
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %4
%20 = load i64, i64* %8
store i64 %20, i64* %11
br label %21
21: ; preds = %61, %19
%22 = load i64, i64* %8
%23 = icmp sgt i64 %22, 0
br i1 %23, label %24, label %62
24: ; preds = %21
%25 = load i64, i64* %8
%26 = icmp sgt i64 %25, 65536
br i1 %26, label %27, label %28
27: ; preds = %24
store i32 65536, i32* %12
br label %31
28: ; preds = %24
%29 = load i64, i64* %8
%30 = trunc i64 %29 to i32
store i32 %30, i32* %12
br label %31
31: ; preds = %28, %27
%32 = load i32, i32* %7
%33 = load i8*, i8** %10
%34 = load i32, i32* %12
%35 = call i32 @sread(i32 %32, i8* %33, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %38
37: ; preds = %31
store i32 -1, i32* %5
br label %63
38: ; preds = %31
%39 = load i32, i32* %6
%40 = load i8*, i8** %10
%41 = load i32, i32* %12
%42 = call i32 @swrite(i32 %39, i8* %40, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %38
store i32 -1, i32* %5
br label %63
45: ; preds = %38
%46 = load i32, i32* %12
%47 = zext i32 %46 to i64
%48 = load i64, i64* %8
%49 = sub nsw i64 %48, %47
store i64 %49, i64* %8
%50 = load void (double)*, void (double)** %9
%51 = icmp ne void (double)* %50, null
br i1 %51, label %52, label %61
52: ; preds = %45
%53 = load void (double)*, void (double)** %9
%54 = load i64, i64* %11
%55 = load i64, i64* %8
%56 = sub nsw i64 %54, %55
%57 = sitofp i64 %56 to double
%58 = load i64, i64* %11
%59 = sitofp i64 %58 to double
%60 = fdiv double %57, %59
br label %61
61: ; preds = %52, %45
br label %21
62: ; preds = %21
store i32 0, i32* %5
br label %63
63: ; preds = %62, %44, %37
%64 = load i32, i32* %5
ret i32 %64
}

LLVM IR (-O2):
@.str = [13 x i8] c"malloc error\00"
define i32 @transfer_file_copy(i32 %0, i32 %1, i64 %2, void (double)* %3) {
%5 = tail call dereferenceable_or_null(65536) i8* @malloc(i32 65536)
%6 = icmp eq i8* %5, null
br i1 %6, label %12, label %7
7: ; preds = %4
%8 = icmp sgt i64 %2, 0
br i1 %8, label %9, label %33
9: ; preds = %7
%10 = icmp eq void (double)* %3, null
%11 = sitofp i64 %2 to double
br label %15
12: ; preds = %4
%13 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %9, %31
%16 = phi i64 [ %2, %9 ], [ %26, %31 ]
%17 = icmp ult i64 %16, 65536
%18 = select i1 %17, i64 %16, i64 65536
%19 = trunc i64 %18 to i32
%20 = tail call i32 @sread(i32 %1, i8* %5, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %33, label %22
22: ; preds = %15
%23 = tail call i32 @swrite(i32 %0, i8* %5, i32 %19)
%24 = icmp eq i32 %23, -1
br i1 %24, label %33, label %25
25: ; preds = %22
%26 = sub nsw i64 %16, %18
br i1 %10, label %31, label %27
27: ; preds = %25
%28 = sub nsw i64 %2, %26
%29 = sitofp i64 %28 to double
%30 = fdiv double %29, %11
tail call void %3(double %30)
br label %31
31: ; preds = %25, %27
%32 = icmp sgt i64 %26, 0
br i1 %32, label %15, label %33
33: ; preds = %31, %15, %22, %7
%34 = phi i32 [ 0, %7 ], [ -1, %22 ], [ -1, %15 ], [ 0, %31 ]
ret i32 %34
}