#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 timeval {
  double tv_sec;
  double tv_usec;
};
typedef int ssize_t;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_WRONLY;
scalar_t__ calloc(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_global_performance(double, int, int);
int /*<<< orphan*/ printf(char *, int, double);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int measure_lazy_write(const char *testfile_name, int request_size,
                       int requests_number) {
  ssize_t bytes_written, local_request_size, local_requests_number,
      total_size = 0;
  int i, testfile_descriptor;
  char *buffer;
  struct timeval start_timer;
  struct timeval stop_timer;
  double time_elapsed, total_time_elapsed = 0;
  if ((local_request_size = request_size * 1024 * sizeof(char)) <= 0)
    local_request_size = 64 * 1024 * sizeof(char);
  if ((local_requests_number = requests_number) <= 0)
    local_requests_number = 50;
  testfile_descriptor = open(testfile_name, O_WRONLY);
  if (testfile_descriptor < 0)
    return (EXIT_FAILURE);
  buffer = (char *)calloc(local_request_size, sizeof(char));
  for (i = 0; i < local_requests_number; i++) {
    gettimeofday(&start_timer, NULL);
    bytes_written =
        write(testfile_descriptor, buffer, local_request_size * sizeof(char));
    gettimeofday(&stop_timer, NULL);
    if (bytes_written > 0) {
      time_elapsed = (stop_timer.tv_sec - start_timer.tv_sec) * 1000.0;
      time_elapsed += (stop_timer.tv_usec - start_timer.tv_usec) / 1000.0;
      total_time_elapsed += time_elapsed;
      total_size += bytes_written;
      printf("%d\t%f\n", i, time_elapsed);
    } else {
      perror("write");
      fprintf(stderr, "Writing problem, abandoning writing measurment\n");
      break;
    }
  }
  print_global_performance(total_time_elapsed, total_size, request_size);
  close(testfile_descriptor);
  free(buffer);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<measure_lazy_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x68(%rbp)
mov    -0x14(%rbp),%eax
shl    $0xa,%eax
cltq
shl    $0x0,%rax
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jg     11e0 <measure_lazy_write+0x40>
movl   $0x10000,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jg     11f6 <measure_lazy_write+0x56>
movl   $0x32,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1226 <measure_lazy_write+0x86>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1359 <measure_lazy_write+0x1b9>
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    132c <measure_lazy_write+0x18c>
lea    -0x48(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
mov    -0x30(%rbp),%edi
mov    -0x38(%rbp),%rsi
movslq -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x58(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    12f3 <measure_lazy_write+0x153>
movsd  -0x58(%rbp),%xmm0
subsd  -0x48(%rbp),%xmm0
movsd  0xd67(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x50(%rbp),%xmm0
subsd  -0x40(%rbp),%xmm0
movsd  0xd4c(%rip),%xmm1
divsd  %xmm1,%xmm0
addsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x60(%rbp),%xmm0
addsd  -0x68(%rbp),%xmm0
movsd  %xmm0,-0x68(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%esi
movsd  -0x60(%rbp),%xmm0
lea    0xd1f(%rip),%rdi
call   1050 <printf@plt>
jmp    1319 <measure_lazy_write+0x179>
lea    0xd15(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
lea    0xd06(%rip),%rsi
call   1090 <fprintf@plt>
jmp    132c <measure_lazy_write+0x18c>
jmp    131e <measure_lazy_write+0x17e>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    123e <measure_lazy_write+0x9e>
movsd  -0x68(%rbp),%xmm0
mov    -0x28(%rbp),%edi
mov    -0x14(%rbp),%esi
call   10c0 <print_global_performance@plt>
mov    -0x30(%rbp),%edi
call   1070 <close@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    0x2c7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<measure_lazy_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebx
mov    %esi,%eax
shl    $0xa,%eax
test   %esi,%esi
mov    $0x10000,%r13d
cmovg  %eax,%r13d
test   %edx,%edx
mov    $0x32,%r15d
cmovg  %edx,%r15d
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
test   %eax,%eax
js     12c6 <measure_lazy_write+0x126>
mov    %eax,%r12d
mov    %ebx,0xc(%rsp)
mov    %r13d,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,0x10(%rsp)
xor    %ebx,%ebx
xorpd  %xmm0,%xmm0
movsd  %xmm0,(%rsp)
xor    %ebp,%ebp
nopl   0x0(%rax)
lea    0x28(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    %r12d,%edi
mov    0x10(%rsp),%rsi
mov    %r13d,%edx
call   1040 <write@plt>
mov    %eax,%r14d
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
test   %r14d,%r14d
jle    12de <measure_lazy_write+0x13e>
movsd  0x18(%rsp),%xmm1
subsd  0x28(%rsp),%xmm1
movsd  0x20(%rsp),%xmm0
movsd  0xda2(%rip),%xmm2
mulsd  %xmm2,%xmm1
subsd  0x30(%rsp),%xmm0
divsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
add    %r14d,%ebp
lea    0xd80(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
add    $0x1,%ebx
cmp    %ebx,%r15d
jne    1210 <measure_lazy_write+0x70>
movsd  (%rsp),%xmm0
mov    %ebp,%edi
mov    0xc(%rsp),%esi
call   10c0 <print_global_performance@plt>
mov    %r12d,%edi
call   1070 <close@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    0x2d0c(%rip),%rax
jmp    12cd <measure_lazy_write+0x12d>
mov    0x2d0b(%rip),%rax
mov    (%rax),%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd2a(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
lea    0xd1b(%rip),%rsi
call   1090 <fprintf@plt>
jmp    129b <measure_lazy_write+0xfb>

LLVM IR (-O0):
%struct.timeval = type { double, double }
@O_WRONLY = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [7 x i8] c"%d\09%f\0A\00"
@.str.1 = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.2 = [48 x i8] c"Writing problem, abandoning writing measurment\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @measure_lazy_write(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
%15 = alloca %struct.timeval
%16 = alloca %struct.timeval
%17 = alloca double
%18 = alloca double
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %11
store double 0.000000e+00, double* %18
%19 = load i32, i32* %6
%20 = mul nsw i32 %19, 1024
%21 = sext i32 %20 to i64
%22 = mul i64 %21, 1
%23 = trunc i64 %22 to i32
store i32 %23, i32* %9
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %3
store i32 65536, i32* %9
br label %26
26: ; preds = %25, %3
%27 = load i32, i32* %7
store i32 %27, i32* %10
%28 = icmp sle i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 50, i32* %10
br label %30
30: ; preds = %29, %26
%31 = load i8*, i8** %5
%32 = load i32, i32* @O_WRONLY
%33 = call i32 @open(i8* %31, i32 %32)
store i32 %33, i32* %13
%34 = load i32, i32* %13
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = load i32, i32* @EXIT_FAILURE
store i32 %37, i32* %4
br label %100
38: ; preds = %30
%39 = load i32, i32* %9
%40 = call i64 @calloc(i32 %39, i32 1)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %14
store i32 0, i32* %12
br label %42
42: ; preds = %87, %38
%43 = load i32, i32* %12
%44 = load i32, i32* %10
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %90
46: ; preds = %42
%47 = call i32 @gettimeofday(%struct.timeval* %15, i32* null)
%48 = load i32, i32* %13
%49 = load i8*, i8** %14
%50 = load i32, i32* %9
%51 = sext i32 %50 to i64
%52 = mul i64 %51, 1
%53 = trunc i64 %52 to i32
%54 = call i32 @write(i32 %48, i8* %49, i32 %53)
store i32 %54, i32* %8
%55 = call i32 @gettimeofday(%struct.timeval* %16, i32* null)
%56 = load i32, i32* %8
%57 = icmp sgt i32 %56, 0
br i1 %57, label %58, label %82
58: ; preds = %46
%59 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
%60 = load double, double* %59
%61 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
%62 = load double, double* %61
%63 = fsub double %60, %62
%64 = fmul double %63, 1.000000e+03
store double %64, double* %17
%65 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 1
%66 = load double, double* %65
%67 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
%68 = load double, double* %67
%69 = fsub double %66, %68
%70 = fdiv double %69, 1.000000e+03
%71 = load double, double* %17
%72 = fadd double %71, %70
store double %72, double* %17
%73 = load double, double* %17
%74 = load double, double* %18
%75 = fadd double %74, %73
store double %75, double* %18
%76 = load i32, i32* %8
%77 = load i32, i32* %11
%78 = add nsw i32 %77, %76
store i32 %78, i32* %11
%79 = load i32, i32* %12
%80 = load double, double* %17
%81 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %79, double %80)
br label %86
82: ; preds = %46
%83 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%84 = load i32, i32* @stderr
%85 = call i32 @fprintf(i32 %84, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0))
br label %90
86: ; preds = %58
br label %87
87: ; preds = %86
%88 = load i32, i32* %12
%89 = add nsw i32 %88, 1
store i32 %89, i32* %12
br label %42
90: ; preds = %82, %42
%91 = load double, double* %18
%92 = load i32, i32* %11
%93 = load i32, i32* %6
%94 = call i32 @print_global_performance(double %91, i32 %92, i32 %93)
%95 = load i32, i32* %13
%96 = call i32 @close(i32 %95)
%97 = load i8*, i8** %14
%98 = call i32 @free(i8* %97)
%99 = load i32, i32* @EXIT_SUCCESS
store i32 %99, i32* %4
br label %100
100: ; preds = %90, %36
%101 = load i32, i32* %4
ret i32 %101
}

LLVM IR (-O2):
%struct.timeval = type { double, double }
@O_WRONLY = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [7 x i8] c"%d\09%f\0A\00"
@.str.1 = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.2 = [48 x i8] c"Writing problem, abandoning writing measurment\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @measure_lazy_write(i8* nocapture %0, i32 %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = bitcast %struct.timeval* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = shl nsw i32 %1, 10
%9 = icmp slt i32 %1, 1
%10 = select i1 %9, i32 65536, i32 %8
%11 = icmp slt i32 %2, 1
%12 = select i1 %11, i32 50, i32 %2
%13 = load i32, i32* @O_WRONLY
%14 = tail call i32 @open(i8* %0, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %56, label %16
16: ; preds = %3
%17 = tail call i64 @calloc(i32 %10, i32 1)
%18 = inttoptr i64 %17 to i8*
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
br label %23
23: ; preds = %16, %31
%24 = phi double [ 0.000000e+00, %16 ], [ %41, %31 ]
%25 = phi i32 [ 0, %16 ], [ %44, %31 ]
%26 = phi i32 [ 0, %16 ], [ %42, %31 ]
%27 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%28 = tail call i32 @write(i32 %14, i8* %18, i32 %10)
%29 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%30 = icmp sgt i32 %28, 0
br i1 %30, label %31, label %46
31: ; preds = %23
%32 = load double, double* %19
%33 = load double, double* %20
%34 = fsub double %32, %33
%35 = fmul double %34, 1.000000e+03
%36 = load double, double* %21
%37 = load double, double* %22
%38 = fsub double %36, %37
%39 = fdiv double %38, 1.000000e+03
%40 = fadd double %35, %39
%41 = fadd double %24, %40
%42 = add nsw i32 %28, %26
%43 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %25, double %40)
%44 = add nuw nsw i32 %25, 1
%45 = icmp eq i32 %44, %12
br i1 %45, label %50, label %23
46: ; preds = %23
%47 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%48 = load i32, i32* @stderr
%49 = tail call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %31, %46
%51 = phi i32 [ %26, %46 ], [ %42, %31 ]
%52 = phi double [ %24, %46 ], [ %41, %31 ]
%53 = tail call i32 @print_global_performance(double %52, i32 %51, i32 %1)
%54 = tail call i32 @close(i32 %14)
%55 = tail call i32 @free(i8* %18)
br label %56
56: ; preds = %3, %50
%57 = phi i32* [ @EXIT_SUCCESS, %50 ], [ @EXIT_FAILURE, %3 ]
%58 = load i32, i32* %57
ret i32 %58
}