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

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
struct timeval *malloc(int);
int /*<<< orphan*/ say(int /*<<< orphan*/);
int /*<<< orphan*/ spr_(char *, int, float);
void benchmark(int flag) {
  static int sec;
  static int usec;
  struct timeval *tv = malloc(sizeof *tv);
  if (flag == 0) {
    gettimeofday(tv, NULL);
    sec = tv->tv_sec;
    usec = tv->tv_usec;
  } else {
    gettimeofday(tv, NULL);
    int delta_s = tv->tv_sec - sec;
    int delta_us = tv->tv_usec - usec;
    if (delta_us < 0) {
      delta_s--;
      delta_us += 1e6;
    }
    float delta = delta_s + delta_us / 1e6;
    say(spr_("Benchmark: %f", 50, delta));
    sec = -1;
    usec = -1;
  }
}


x86 assembly (clang, -O0):
<benchmark>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x8,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    118e <benchmark+0x4e>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,0x2ec8(%rip)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,0x2ebf(%rip)
jmp    1239 <benchmark+0xf9>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
sub    0x2e9d(%rip),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%eax
sub    0x2e91(%rip),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11e5 <benchmark+0xa5>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
cvtsi2sdl -0x18(%rbp),%xmm0
movsd  0xe26(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x18(%rbp)
cvtsi2sdl -0x14(%rbp),%xmm0
cvtsi2sdl -0x18(%rbp),%xmm1
movsd  0xe09(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x1c(%rbp)
movss  -0x1c(%rbp),%xmm0
lea    0xdf4(%rip),%rdi
mov    $0x32,%esi
call   1050 <spr_@plt>
mov    %eax,%edi
call   1060 <say@plt>
movl   $0xffffffff,0x2e15(%rip)
ff ff ff
movl   $0xffffffff,0x2e0f(%rip)
ff ff ff
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<benchmark>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0x8,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    (%rbx),%eax
test   %ebp,%ebp
je     11c5 <benchmark+0x85>
sub    0x2edc(%rip),%eax
mov    0x4(%rbx),%ecx
sub    0x2ed7(%rip),%ecx
lea    0xf4240(%rcx),%edx
test   %ecx,%ecx
cmovns %ecx,%edx
sar    $0x1f,%ecx
add    %eax,%ecx
cvtsi2sd %ecx,%xmm0
cvtsi2sd %edx,%xmm1
divsd  0xe6f(%rip),%xmm1
addsd  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtsd2ss %xmm1,%xmm0
lea    0xe65(%rip),%rdi
mov    $0x32,%esi
call   1050 <spr_@plt>
mov    %eax,%edi
call   1060 <say@plt>
movl   $0xffffffff,0x2e86(%rip)
ff ff ff
mov    $0xffffffff,%eax
jmp    11ce <benchmark+0x8e>
mov    %eax,0x2e79(%rip)
mov    0x4(%rbx),%eax
mov    %eax,0x2e74(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@benchmark.sec = internal global i32 0
@benchmark.usec = internal global i32 0
@.str = [14 x i8] c"Benchmark: %f\00"
define void @benchmark(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval*
%4 = alloca i32
%5 = alloca i32
%6 = alloca float
store i32 %0, i32* %2
%7 = call %struct.timeval* @malloc(i32 8)
store %struct.timeval* %7, %struct.timeval** %3
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %1
%11 = load %struct.timeval*, %struct.timeval** %3
%12 = call i32 @gettimeofday(%struct.timeval* %11, i32* null)
%13 = load %struct.timeval*, %struct.timeval** %3
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%15 = load i32, i32* %14
store i32 %15, i32* @benchmark.sec
%16 = load %struct.timeval*, %struct.timeval** %3
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 1
%18 = load i32, i32* %17
store i32 %18, i32* @benchmark.usec
br label %52
19: ; preds = %1
%20 = load %struct.timeval*, %struct.timeval** %3
%21 = call i32 @gettimeofday(%struct.timeval* %20, i32* null)
%22 = load %struct.timeval*, %struct.timeval** %3
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @benchmark.sec
%26 = sub nsw i32 %24, %25
store i32 %26, i32* %4
%27 = load %struct.timeval*, %struct.timeval** %3
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %27, i32 0, i32 1
%29 = load i32, i32* %28
%30 = load i32, i32* @benchmark.usec
%31 = sub nsw i32 %29, %30
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %19
%35 = load i32, i32* %4
%36 = add nsw i32 %35, -1
store i32 %36, i32* %4
%37 = load i32, i32* %5
%38 = sitofp i32 %37 to double
%39 = fadd double %38, 1.000000e+06
%40 = fptosi double %39 to i32
store i32 %40, i32* %5
br label %41
41: ; preds = %34, %19
%42 = load i32, i32* %4
%43 = sitofp i32 %42 to double
%44 = load i32, i32* %5
%45 = sitofp i32 %44 to double
%46 = fdiv double %45, 1.000000e+06
%47 = fadd double %43, %46
%48 = fptrunc double %47 to float
store float %48, float* %6
%49 = load float, float* %6
%50 = call i32 @spr_(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 50, float %49)
%51 = call i32 @say(i32 %50)
store i32 -1, i32* @benchmark.sec
store i32 -1, i32* @benchmark.usec
br label %52
52: ; preds = %41, %10
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@benchmark.sec = internal unnamed_addr global i32 0
@benchmark.usec = internal unnamed_addr global i32 0
@.str = [14 x i8] c"Benchmark: %f\00"
define void @benchmark(i32 %0) {
%2 = tail call %struct.timeval* @malloc(i32 8)
%3 = icmp eq i32 %0, 0
%4 = tail call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%6 = load i32, i32* %5
br i1 %3, label %7, label %10
7: ; preds = %1
store i32 %6, i32* @benchmark.sec
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%9 = load i32, i32* %8
br label %29
10: ; preds = %1
%11 = load i32, i32* @benchmark.sec
%12 = sub i32 %6, %11
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%14 = load i32, i32* %13
%15 = load i32, i32* @benchmark.usec
%16 = sub nsw i32 %14, %15
%17 = icmp slt i32 %16, 0
%18 = add i32 %16, 1000000
%19 = select i1 %17, i32 %18, i32 %16
%20 = ashr i32 %16, 31
%21 = add nsw i32 %12, %20
%22 = sitofp i32 %21 to double
%23 = sitofp i32 %19 to double
%24 = fdiv double %23, 1.000000e+06
%25 = fadd double %24, %22
%26 = fptrunc double %25 to float
%27 = tail call i32 @spr_(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 50, float %26)
%28 = tail call i32 @say(i32 %27)
store i32 -1, i32* @benchmark.sec
br label %29
29: ; preds = %10, %7
%30 = phi i32 [ -1, %10 ], [ %9, %7 ]
store i32 %30, i32* @benchmark.usec
ret void
}