#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 int ssize_t;
typedef int /*<<< orphan*/ read_buf;
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
long long current_timestamp();
int /*<<< orphan*/ error_exit(char *);
int /*<<< orphan*/ fsync(int const);
int read(int const, char *, int);
int /*<<< orphan*/ write_to_file(int const, char *, int);
off_t cp_for_time(const int fd_src, const int fd_target, const int msec) {
  long long start_time = current_timestamp();
  off_t cp_count = 0;
  char read_buf[1024 * 80] = "";
  for (;;) {
    ssize_t read_ret = read(fd_src, read_buf, sizeof(read_buf));
    if (read_ret == -1) {
      error_exit("read error");
    }
    if (read_ret == 0) {
      fsync(fd_target);
      return 0;
    }
    ssize_t len = read_ret;
    cp_count += len;
    write_to_file(fd_target, read_buf, len);
    long long cur_time = current_timestamp();
    if ((cur_time - start_time) > msec) {
      fsync(fd_target);
      return cp_count;
    }
  }
}


x86 assembly (clang, -O0):
<cp_for_time>:
push   %rbp
mov    %rsp,%rbp
sub    $0x14030,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    $0x0,%al
call   1030 <current_timestamp@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x14020(%rbp),%rdi
xor    %esi,%esi
mov    $0x14000,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x14020(%rbp),%rsi
mov    $0x14000,%edx
call   1070 <read@plt>
mov    %eax,-0x14024(%rbp)
cmpl   $0xffffffff,-0x14024(%rbp)
jne    11cc <cp_for_time+0x6c>
lea    0xe39(%rip),%rdi
call   1060 <error_exit@plt>
cmpl   $0x0,-0x14024(%rbp)
jne    11ed <cp_for_time+0x8d>
mov    -0xc(%rbp),%edi
call   1080 <fsync@plt>
movl   $0x0,-0x4(%rbp)
jmp    1258 <cp_for_time+0xf8>
mov    -0x14024(%rbp),%eax
mov    %eax,-0x14028(%rbp)
mov    -0x14028(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x14020(%rbp),%rsi
mov    -0x14028(%rbp),%edx
call   1040 <write_to_file@plt>
mov    $0x0,%al
call   1030 <current_timestamp@plt>
mov    %rax,-0x14030(%rbp)
mov    -0x14030(%rbp),%rax
sub    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
cmp    %rcx,%rax
jle    1253 <cp_for_time+0xf3>
mov    -0xc(%rbp),%edi
call   1080 <fsync@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1258 <cp_for_time+0xf8>
jmp    1199 <cp_for_time+0x39>
mov    -0x4(%rbp),%eax
add    $0x14030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp_for_time>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x14018,%rsp
mov    %edx,%ebx
mov    %esi,0xc(%rsp)
mov    %edi,%r15d
xor    %eax,%eax
call   1030 <current_timestamp@plt>
mov    %rax,%r12
lea    0x10(%rsp),%rbp
mov    $0x14000,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
movslq %ebx,%r14
xor    %r13d,%r13d
jmp    11cc <cp_for_time+0x6c>
lea    0xe59(%rip),%rdi
call   1060 <error_exit@plt>
add    %ebx,%r13d
mov    0xc(%rsp),%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <write_to_file@plt>
xor    %eax,%eax
call   1030 <current_timestamp@plt>
sub    %r12,%rax
cmp    %r14,%rax
jg     11ea <cp_for_time+0x8a>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x14000,%edx
call   1070 <read@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11a0 <cp_for_time+0x40>
test   %ebx,%ebx
jne    11ac <cp_for_time+0x4c>
mov    %ebx,%r13d
mov    0xc(%rsp),%edi
call   1080 <fsync@plt>
mov    %r13d,%eax
add    $0x14018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"read error\00"
define i32 @cp_for_time(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
%10 = alloca [81920 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%14 = call i64 (...) @current_timestamp()
store i64 %14, i64* %8
store i32 0, i32* %9
%15 = bitcast [81920 x i8]* %10 to i8*
br label %16
16: ; preds = %50, %3
%17 = load i32, i32* %5
%18 = getelementptr inbounds [81920 x i8], [81920 x i8]* %10, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 81920)
store i32 %19, i32* %11
%20 = load i32, i32* %11
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @error_exit(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
%25 = load i32, i32* %11
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* %6
%29 = call i32 @fsync(i32 %28)
store i32 0, i32* %4
br label %51
30: ; preds = %24
%31 = load i32, i32* %11
store i32 %31, i32* %12
%32 = load i32, i32* %12
%33 = load i32, i32* %9
%34 = add nsw i32 %33, %32
store i32 %34, i32* %9
%35 = load i32, i32* %6
%36 = getelementptr inbounds [81920 x i8], [81920 x i8]* %10, i64 0, i64 0
%37 = load i32, i32* %12
%38 = call i32 @write_to_file(i32 %35, i8* %36, i32 %37)
%39 = call i64 (...) @current_timestamp()
store i64 %39, i64* %13
%40 = load i64, i64* %13
%41 = load i64, i64* %8
%42 = sub nsw i64 %40, %41
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = icmp sgt i64 %42, %44
br i1 %45, label %46, label %50
46: ; preds = %30
%47 = load i32, i32* %6
%48 = call i32 @fsync(i32 %47)
%49 = load i32, i32* %9
store i32 %49, i32* %4
br label %51
50: ; preds = %30
br label %16
51: ; preds = %46, %27
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@.str = [11 x i8] c"read error\00"
define i32 @cp_for_time(i32 %0, i32 %1, i32 %2) {
%4 = alloca [81920 x i8]
%5 = tail call i64 (...) @current_timestamp()
%6 = getelementptr inbounds [81920 x i8], [81920 x i8]* %4, i64 0, i64 0
%7 = sext i32 %2 to i64
br label %8
8: ; preds = %13, %3
%9 = phi i32 [ 0, %3 ], [ %14, %13 ]
%10 = call i32 @read(i32 %0, i8* %6, i32 81920)
switch i32 %10, label %13 [
i32 -1, label %11
i32 0, label %19
]
11: ; preds = %8
%12 = call i32 @error_exit(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = add nsw i32 %10, %9
%15 = call i32 @write_to_file(i32 %1, i8* %6, i32 %10)
%16 = call i64 (...) @current_timestamp()
%17 = sub nsw i64 %16, %5
%18 = icmp sgt i64 %17, %7
br i1 %18, label %19, label %8
19: ; preds = %13, %8
%20 = phi i32 [ %10, %8 ], [ %14, %13 ]
%21 = call i32 @fsync(i32 %1)
ret i32 %20
}