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

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, double);
int test_gettimeofday(void) {
  int i;
  struct timeval tv_start, tv_end;
  gettimeofday(&tv_start, NULL);
  for (i = 0; i < 100000000; i++) {
    gettimeofday(&tv_end, NULL);
  }
  printf("time = %.6f\n", tv_end.tv_sec - tv_start.tv_sec +
                              (tv_end.tv_usec - tv_start.tv_usec) * 1e-6);
  return 0;
}


x86 assembly (clang, -O0):
<test_gettimeofday>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
movl   $0x0,-0x4(%rbp)
cmpl   $0x5f5e100,-0x4(%rbp)
jge    1164 <test_gettimeofday+0x44>
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    113c <test_gettimeofday+0x1c>
movsd  -0x28(%rbp),%xmm1
subsd  -0x18(%rbp),%xmm1
mov    -0x20(%rbp),%eax
sub    -0x10(%rbp),%eax
cvtsi2sd %eax,%xmm0
movsd  0xe80(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
lea    0xe79(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_gettimeofday>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    $0x5f5e100,%ebp
lea    0x8(%rsp),%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
add    $0xffffffff,%ebp
jne    1140 <test_gettimeofday+0x20>
movsd  0x8(%rsp),%xmm1
subsd  0x18(%rsp),%xmm1
mov    0x10(%rsp),%eax
sub    0x20(%rsp),%eax
cvtsi2sd %eax,%xmm0
mulsd  0xe91(%rip),%xmm0
addsd  %xmm1,%xmm0
lea    0xe8e(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { double, i32 }
@.str = [13 x i8] c"time = %.6f\0A\00"
define i32 @test_gettimeofday() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
store i32 0, i32* %1
br label %5
5: ; preds = %10, %0
%6 = load i32, i32* %1
%7 = icmp slt i32 %6, 100000000
br i1 %7, label %8, label %13
8: ; preds = %5
%9 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
br label %10
10: ; preds = %8
%11 = load i32, i32* %1
%12 = add nsw i32 %11, 1
store i32 %12, i32* %1
br label %5
13: ; preds = %5
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%15 = load double, double* %14
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%17 = load double, double* %16
%18 = fsub double %15, %17
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%22 = load i32, i32* %21
%23 = sub nsw i32 %20, %22
%24 = sitofp i32 %23 to double
%25 = call double @llvm.fmuladd.f64(double %24, double 0x3EB0C6F7A0B5ED8D, double %18)
%26 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), double %25)
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { double, i32 }
@.str = [13 x i8] c"time = %.6f\0A\00"
define i32 @test_gettimeofday() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %1 to i8*
%4 = bitcast %struct.timeval* %2 to i8*
%5 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
br label %6
6: ; preds = %0, %6
%7 = phi i32 [ 0, %0 ], [ %9, %6 ]
%8 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%9 = add nuw nsw i32 %7, 1
%10 = icmp eq i32 %9, 100000000
br i1 %10, label %11, label %6
11: ; preds = %6
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%13 = load double, double* %12
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%15 = load double, double* %14
%16 = fsub double %13, %15
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%20 = load i32, i32* %19
%21 = sub nsw i32 %18, %20
%22 = sitofp i32 %21 to double
%23 = tail call double @llvm.fmuladd.f64(double %22, double 0x3EB0C6F7A0B5ED8D, double %16)
%24 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), double %23)
ret i32 0
}