#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 */
struct stat {
  scalar_t__ st_size;
};
typedef scalar_t__ off_t;

/* Variables and functions */
scalar_t__ cp_for_time(int const, int const, int);
long long current_timestamp();
int /*<<< orphan*/ printf(char *, long, long, int);
int /*<<< orphan*/ usleep(int);
void do_cp(const int fd_src, const int fd_target,
           const struct stat *pstat_buf) {
  int each_cp_time = 500 * 0.5;
  const off_t file_size = pstat_buf->st_size;
  off_t all_cp_count = 0;
  for (;;) {
    long long start_time = current_timestamp();
    off_t copied_count = cp_for_time(fd_src, fd_target, each_cp_time);
    if (copied_count == 0)
      return;
    long long end_cur_cp_time = current_timestamp();
    if ((end_cur_cp_time - start_time) > 500)
      each_cp_time *= 0.8;
    else
      each_cp_time *= 1.1;
    all_cp_count += copied_count;
    printf(
        "%ld%% cur_cp_time milliseconds: %ld  sleep: 500  next read_time:%d\n",
        (long)(all_cp_count * 100 / file_size),
        (long)(end_cur_cp_time - start_time), each_cp_time);
    usleep(500 * 1000);
  }
}


x86 assembly (clang, -O0):
<do_cp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
movl   $0xfa,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1030 <current_timestamp@plt>
mov    %rax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1040 <cp_for_time@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    119a <do_cp+0x5a>
add    $0x40,%rsp
pop    %rbp
ret
mov    $0x0,%al
call   1030 <current_timestamp@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
sub    -0x30(%rbp),%rax
cmp    $0x1f4,%rax
jle    11d6 <do_cp+0x96>
cvtsi2sdl -0x14(%rbp),%xmm0
movsd  0xe42(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x14(%rbp)
jmp    11ee <do_cp+0xae>
cvtsi2sdl -0x14(%rbp),%xmm0
movsd  0xe1d(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x14(%rbp)
mov    -0x38(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
imul   $0x64,-0x28(%rbp),%rax
cqto
idivq  -0x20(%rbp)
mov    %rax,%rsi
mov    -0x40(%rbp),%rdx
sub    -0x30(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xdf6(%rip),%rdi
call   1050 <printf@plt>
mov    $0x7a120,%edi
call   1060 <usleep@plt>
jmp    116c <do_cp+0x2c>

x86 assembly (clang, -O2):
<do_cp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%ebx
mov    (%rdx),%r15
xor    %r12d,%r12d
xor    %eax,%eax
call   1030 <current_timestamp@plt>
mov    %rax,%rbp
mov    $0xfa,%r13d
mov    %ebx,(%rsp)
mov    %ebx,%edi
mov    %r14d,0x4(%rsp)
mov    %r14d,%esi
mov    $0xfa,%edx
call   1040 <cp_for_time@plt>
test   %rax,%rax
je     1234 <do_cp+0xf4>
mov    %rax,%rbx
jmp    11f1 <do_cp+0xb1>
nopl   0x0(%rax,%rax,1)
cqto
idiv   %r15
mov    %rax,%rsi
xorps  %xmm0,%xmm0
cvtsi2sd %r13d,%xmm0
mov    %r8b,%dil
lea    0xe56(%rip),%rax
mulsd  (%rax,%rdi,8),%xmm0
cvttsd2si %xmm0,%r13d
lea    0xe55(%rip),%rdi
mov    %rcx,%rdx
mov    %r13d,%ecx
call   1050 <printf@plt>
mov    $0x7a120,%edi
call   1060 <usleep@plt>
xor    %eax,%eax
call   1030 <current_timestamp@plt>
mov    %rax,%rbp
mov    (%rsp),%edi
mov    0x4(%rsp),%esi
mov    %r13d,%edx
call   1040 <cp_for_time@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1234 <do_cp+0xf4>
mov    %r12,%r14
xor    %eax,%eax
call   1030 <current_timestamp@plt>
mov    %rax,%rcx
sub    %rbp,%rcx
xor    %edi,%edi
cmp    $0x1f5,%rcx
setge  %r8b
mov    %rbx,%r12
add    %r14,%r12
imul   $0x64,%r12,%rax
mov    %rax,%rdx
or     %r15,%rdx
shr    $0x20,%rdx
jne    1190 <do_cp+0x50>
xor    %edx,%edx
div    %r15d
mov    %eax,%esi
jmp    1198 <do_cp+0x58>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [68 x i8] c"%ld%% cur_cp_time milliseconds: %ld sleep: 500 next read_time:%d\0A\00"
define void @do_cp(i32 %0, i32 %1, %struct.stat* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.stat*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
store %struct.stat* %2, %struct.stat** %6
store i32 250, i32* %7
%13 = load %struct.stat*, %struct.stat** %6
%14 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%15 = load i64, i64* %14
store i64 %15, i64* %8
store i64 0, i64* %9
br label %16
16: ; preds = %41, %3
%17 = call i64 (...) @current_timestamp()
store i64 %17, i64* %10
%18 = load i32, i32* %4
%19 = load i32, i32* %5
%20 = load i32, i32* %7
%21 = call i64 @cp_for_time(i32 %18, i32 %19, i32 %20)
store i64 %21, i64* %11
%22 = load i64, i64* %11
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %16
ret void
25: ; preds = %16
%26 = call i64 (...) @current_timestamp()
store i64 %26, i64* %12
%27 = load i64, i64* %12
%28 = load i64, i64* %10
%29 = sub nsw i64 %27, %28
%30 = icmp sgt i64 %29, 500
br i1 %30, label %31, label %36
31: ; preds = %25
%32 = load i32, i32* %7
%33 = sitofp i32 %32 to double
%34 = fmul double %33, 8.000000e-01
%35 = fptosi double %34 to i32
store i32 %35, i32* %7
br label %41
36: ; preds = %25
%37 = load i32, i32* %7
%38 = sitofp i32 %37 to double
%39 = fmul double %38, 1.100000e+00
%40 = fptosi double %39 to i32
store i32 %40, i32* %7
br label %41
41: ; preds = %36, %31
%42 = load i64, i64* %11
%43 = load i64, i64* %9
%44 = add nsw i64 %43, %42
store i64 %44, i64* %9
%45 = load i64, i64* %9
%46 = mul nsw i64 %45, 100
%47 = load i64, i64* %8
%48 = sdiv i64 %46, %47
%49 = load i64, i64* %12
%50 = load i64, i64* %10
%51 = sub nsw i64 %49, %50
%52 = load i32, i32* %7
%53 = call i32 @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str, i64 0, i64 0), i64 %48, i64 %51, i32 %52)
%54 = call i32 @usleep(i32 500000)
br label %16
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [68 x i8] c"%ld%% cur_cp_time milliseconds: %ld sleep: 500 next read_time:%d\0A\00"
define void @do_cp(i32 %0, i32 %1, %struct.stat* nocapture %2) {
%4 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%5 = load i64, i64* %4
%6 = tail call i64 (...) @current_timestamp()
%7 = tail call i64 @cp_for_time(i32 %0, i32 %1, i32 250)
%8 = icmp eq i64 %7, 0
br i1 %8, label %29, label %9
9: ; preds = %3, %9
%10 = phi i64 [ %27, %9 ], [ %7, %3 ]
%11 = phi i64 [ %26, %9 ], [ %6, %3 ]
%12 = phi i32 [ %20, %9 ], [ 250, %3 ]
%13 = phi i64 [ %21, %9 ], [ 0, %3 ]
%14 = tail call i64 (...) @current_timestamp()
%15 = sub nsw i64 %14, %11
%16 = icmp sgt i64 %15, 500
%17 = sitofp i32 %12 to double
%18 = select i1 %16, double 8.000000e-01, double 1.100000e+00
%19 = fmul double %18, %17
%20 = fptosi double %19 to i32
%21 = add nsw i64 %10, %13
%22 = mul nsw i64 %21, 100
%23 = sdiv i64 %22, %5
%24 = tail call i32 @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str, i64 0, i64 0), i64 %23, i64 %15, i32 %20)
%25 = tail call i32 @usleep(i32 500000)
%26 = tail call i64 (...) @current_timestamp()
%27 = tail call i64 @cp_for_time(i32 %0, i32 %1, i32 %20)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %9
29: ; preds = %9, %3
ret void
}