#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 sendfile(int, int, int /*<<< orphan*/ *, unsigned int);
int transfer_file_sys(int out_fd, int in_fd, off_t size_remain,
                      void (*progress)(double percent)) {
  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;
    }
    int ret = sendfile(out_fd, in_fd, NULL, send_this_time);
    if (ret == -1 || ret == 0)
      return -1;
    size_remain -= ret;
    if (progress != NULL)
      progress((orig_size - size_remain) / (double)orig_size);
  }
  return 0;
}


x86 assembly (clang, -O0):
<transfer_file_sys>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    11cb <transfer_file_sys+0xbb>
cmpq   $0x10000,-0x18(%rbp)
jle    1153 <transfer_file_sys+0x43>
movl   $0x10000,-0x2c(%rbp)
jmp    115a <transfer_file_sys+0x4a>
mov    -0x18(%rbp),%rax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x2c(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sendfile@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
je     1183 <transfer_file_sys+0x73>
cmpl   $0x0,-0x30(%rbp)
jne    118f <transfer_file_sys+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <transfer_file_sys+0xc2>
movslq -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11c6 <transfer_file_sys+0xb6>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    -0x18(%rbp),%rcx
cvtsi2sd %rcx,%xmm0
cvtsi2sdq -0x28(%rbp),%xmm1
divsd  %xmm1,%xmm0
call   *%rax
jmp    112e <transfer_file_sys+0x1e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<transfer_file_sys>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %rdx,%rdx
jle    118b <transfer_file_sys+0x7b>
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%r12d
cvtsi2sd %rdx,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %rdx,%rbp
jmp    1145 <transfer_file_sys+0x35>
nopl   (%rax)
test   %rbp,%rbp
jle    118b <transfer_file_sys+0x7b>
cmp    $0x10000,%rbp
mov    $0x10000,%ecx
cmovb  %ebp,%ecx
mov    %r12d,%edi
mov    %r15d,%esi
xor    %edx,%edx
call   1030 <sendfile@plt>
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jb     118f <transfer_file_sys+0x7f>
cltq
sub    %rax,%rbp
test   %rbx,%rbx
je     1140 <transfer_file_sys+0x30>
mov    %r14,%rax
sub    %rbp,%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  0x8(%rsp),%xmm0
call   *%rbx
jmp    1140 <transfer_file_sys+0x30>
xor    %eax,%eax
jmp    1194 <transfer_file_sys+0x84>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @transfer_file_sys(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 i64
%11 = alloca i32
%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 = load i64, i64* %8
store i64 %13, i64* %10
br label %14
14: ; preds = %51, %4
%15 = load i64, i64* %8
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %52
17: ; preds = %14
%18 = load i64, i64* %8
%19 = icmp sgt i64 %18, 65536
br i1 %19, label %20, label %21
20: ; preds = %17
store i32 65536, i32* %11
br label %24
21: ; preds = %17
%22 = load i64, i64* %8
%23 = trunc i64 %22 to i32
store i32 %23, i32* %11
br label %24
24: ; preds = %21, %20
%25 = load i32, i32* %6
%26 = load i32, i32* %7
%27 = load i32, i32* %11
%28 = call i32 @sendfile(i32 %25, i32 %26, i32* null, i32 %27)
store i32 %28, i32* %12
%29 = load i32, i32* %12
%30 = icmp eq i32 %29, -1
br i1 %30, label %34, label %31
31: ; preds = %24
%32 = load i32, i32* %12
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %31, %24
store i32 -1, i32* %5
br label %53
35: ; preds = %31
%36 = load i32, i32* %12
%37 = sext i32 %36 to i64
%38 = load i64, i64* %8
%39 = sub nsw i64 %38, %37
store i64 %39, i64* %8
%40 = load void (double)*, void (double)** %9
%41 = icmp ne void (double)* %40, null
br i1 %41, label %42, label %51
42: ; preds = %35
%43 = load void (double)*, void (double)** %9
%44 = load i64, i64* %10
%45 = load i64, i64* %8
%46 = sub nsw i64 %44, %45
%47 = sitofp i64 %46 to double
%48 = load i64, i64* %10
%49 = sitofp i64 %48 to double
%50 = fdiv double %47, %49
br label %51
51: ; preds = %42, %35
br label %14
52: ; preds = %14
store i32 0, i32* %5
br label %53
53: ; preds = %52, %34
%54 = load i32, i32* %5
ret i32 %54
}

LLVM IR (-O2):
define i32 @transfer_file_sys(i32 %0, i32 %1, i64 %2, void (double)* %3) {
%5 = icmp sgt i64 %2, 0
br i1 %5, label %6, label %26
6: ; preds = %4
%7 = icmp eq void (double)* %3, null
%8 = sitofp i64 %2 to double
br label %9
9: ; preds = %6, %24
%10 = phi i64 [ %2, %6 ], [ %19, %24 ]
%11 = icmp ult i64 %10, 65536
%12 = select i1 %11, i64 %10, i64 65536
%13 = trunc i64 %12 to i32
%14 = tail call i32 @sendfile(i32 %0, i32 %1, i32* null, i32 %13)
%15 = add i32 %14, 1
%16 = icmp ult i32 %15, 2
br i1 %16, label %26, label %17
17: ; preds = %9
%18 = sext i32 %14 to i64
%19 = sub nsw i64 %10, %18
br i1 %7, label %24, label %20
20: ; preds = %17
%21 = sub nsw i64 %2, %19
%22 = sitofp i64 %21 to double
%23 = fdiv double %22, %8
tail call void %3(double %23)
br label %24
24: ; preds = %17, %20
%25 = icmp sgt i64 %19, 0
br i1 %25, label %9, label %26
26: ; preds = %24, %9, %4
%27 = phi i32 [ 0, %4 ], [ -1, %9 ], [ 0, %24 ]
ret i32 %27
}