#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 {
  int dummy;
};

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dsync(int);
scalar_t__ elapsed_ms(struct timeval *, struct timeval *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int open(char *, int);
int /*<<< orphan*/ osync(int);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd;
  struct timeval st, et;
  double total_osync_time = 0;
  double total_dsync_time = 0;
  int i;
  fd = open("/mnt/mydisk/testing2", O_CREAT | O_RDWR);
  for (i = 0; i < 20; i++) {
    gettimeofday(&st, NULL);
    write(fd, "hello\n", 6);
    osync(fd);
    gettimeofday(&et, NULL);
    total_osync_time += elapsed_ms(&st, &et);
  }
  printf("osync() latency in ms: %f\n", total_osync_time / 20);
  for (i = 0; i < 20; i++) {
    gettimeofday(&st, NULL);
    write(fd, "hello again\n", 12);
    dsync(fd);
    gettimeofday(&et, NULL);
    total_dsync_time += elapsed_ms(&st, &et);
  }
  printf("dsync() latency in ms: %f\n", total_dsync_time / 20);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    0x2e31(%rip),%rax
or     (%rax),%esi
lea    0xe50(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x14,-0x2c(%rbp)
jge    1231 <main+0xb1>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
mov    -0x8(%rbp),%edi
lea    0xe35(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1080 <osync@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <elapsed_ms@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11c7 <main+0x47>
movsd  -0x20(%rbp),%xmm0
movsd  0xdc2(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0xddb(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x14,-0x2c(%rbp)
jge    12bf <main+0x13f>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
mov    -0x8(%rbp),%edi
lea    0xdc9(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1050 <dsync@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <elapsed_ms@plt>
cvtsi2sd %rax,%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1255 <main+0xd5>
movsd  -0x28(%rbp),%xmm0
movsd  0xd34(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0xd75(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    0x2e65(%rip),%rax
mov    0x2e46(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe63(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
xorpd  %xmm0,%xmm0
mov    $0x14,%ebp
lea    0x18(%rsp),%r14
lea    0xe5c(%rip),%r15
lea    0x10(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
movsd  %xmm0,0x8(%rsp)
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1080 <osync@plt>
mov    %r12,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1090 <elapsed_ms@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
add    $0xffffffff,%ebp
jne    11d0 <main+0x50>
divsd  0xdca(%rip),%xmm0
lea    0xde7(%rip),%rdi
call   1040 <printf@plt>
xorpd  %xmm0,%xmm0
mov    $0x14,%ebp
lea    0x18(%rsp),%r14
lea    0xde8(%rip),%r15
lea    0x10(%rsp),%r12
nopl   0x0(%rax)
movsd  %xmm0,0x8(%rsp)
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <dsync@plt>
mov    %r12,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1090 <elapsed_ms@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
add    $0xffffffff,%ebp
jne    1260 <main+0xe0>
divsd  0xd3a(%rip),%xmm0
lea    0xd7f(%rip),%rdi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@.str = [21 x i8] c"/mnt/mydisk/testing2\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"hello\0A\00"
@.str.2 = [27 x i8] c"osync() latency in ms: %f\0A\00"
@.str.3 = [13 x i8] c"hello again\0A\00"
@.str.4 = [27 x i8] c"dsync() latency in ms: %f\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca double
%6 = alloca double
%7 = alloca i32
store i32 0, i32* %1
store double 0.000000e+00, double* %5
store double 0.000000e+00, double* %6
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_RDWR
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %2
store i32 0, i32* %7
br label %12
12: ; preds = %26, %0
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 20
br i1 %14, label %15, label %29
15: ; preds = %12
%16 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%17 = load i32, i32* %2
%18 = call i32 @write(i32 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%19 = load i32, i32* %2
%20 = call i32 @osync(i32 %19)
%21 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%22 = call i64 @elapsed_ms(%struct.timeval* %3, %struct.timeval* %4)
%23 = sitofp i64 %22 to double
%24 = load double, double* %5
%25 = fadd double %24, %23
store double %25, double* %5
br label %26
26: ; preds = %15
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
store i32 %28, i32* %7
br label %12
29: ; preds = %12
%30 = load double, double* %5
%31 = fdiv double %30, 2.000000e+01
%32 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), double %31)
store i32 0, i32* %7
br label %33
33: ; preds = %47, %29
%34 = load i32, i32* %7
%35 = icmp slt i32 %34, 20
br i1 %35, label %36, label %50
36: ; preds = %33
%37 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%38 = load i32, i32* %2
%39 = call i32 @write(i32 %38, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 12)
%40 = load i32, i32* %2
%41 = call i32 @dsync(i32 %40)
%42 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%43 = call i64 @elapsed_ms(%struct.timeval* %3, %struct.timeval* %4)
%44 = sitofp i64 %43 to double
%45 = load double, double* %6
%46 = fadd double %45, %44
store double %46, double* %6
br label %47
47: ; preds = %36
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
store i32 %49, i32* %7
br label %33
50: ; preds = %33
%51 = load double, double* %6
%52 = fdiv double %51, 2.000000e+01
%53 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), double %52)
%54 = load i32, i32* %2
%55 = call i32 @close(i32 %54)
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@.str = [21 x i8] c"/mnt/mydisk/testing2\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [7 x i8] c"hello\0A\00"
@.str.2 = [27 x i8] c"osync() latency in ms: %f\0A\00"
@.str.3 = [13 x i8] c"hello again\0A\00"
@.str.4 = [27 x i8] c"dsync() latency in ms: %f\0A\00"
define i32 @main() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %1 to i8*
%4 = bitcast %struct.timeval* %2 to i8*
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDWR
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %9
9: ; preds = %0, %9
%10 = phi i32 [ 0, %0 ], [ %19, %9 ]
%11 = phi double [ 0.000000e+00, %0 ], [ %18, %9 ]
%12 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%13 = call i32 @write(i32 %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%14 = call i32 @osync(i32 %8)
%15 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%16 = call i64 @elapsed_ms(%struct.timeval* %1, %struct.timeval* %2)
%17 = sitofp i64 %16 to double
%18 = fadd double %11, %17
%19 = add nuw nsw i32 %10, 1
%20 = icmp eq i32 %19, 20
br i1 %20, label %21, label %9
21: ; preds = %9
%22 = fdiv double %18, 2.000000e+01
%23 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), double %22)
br label %24
24: ; preds = %21, %24
%25 = phi i32 [ 0, %21 ], [ %34, %24 ]
%26 = phi double [ 0.000000e+00, %21 ], [ %33, %24 ]
%27 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%28 = call i32 @write(i32 %8, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 12)
%29 = call i32 @dsync(i32 %8)
%30 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%31 = call i64 @elapsed_ms(%struct.timeval* %1, %struct.timeval* %2)
%32 = sitofp i64 %31 to double
%33 = fadd double %26, %32
%34 = add nuw nsw i32 %25, 1
%35 = icmp eq i32 %34, 20
br i1 %35, label %36, label %24
36: ; preds = %24
%37 = fdiv double %33, 2.000000e+01
%38 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), double %37)
%39 = call i32 @close(i32 %8)
ret i32 0
}