#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*/ test_sort(void (*)(int *, int), int);
double benchmark_sort(void (*sort_fn)(int[], int), int size, int iterations) {
  struct timeval start;
  gettimeofday(&start, 0);
  for (int i = 0; i < iterations; i++) {
    test_sort(sort_fn, size);
  }
  struct timeval end;
  gettimeofday(&end, 0);
  return end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec) / 1e6;
}


x86 assembly (clang, -O0):
<benchmark_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    116a <benchmark_sort+0x4a>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <test_sort@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1144 <benchmark_sort+0x24>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movsd  -0x38(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
mov    -0x30(%rbp),%eax
sub    -0x18(%rbp),%eax
cvtsi2sd %eax,%xmm1
movsd  0xe6f(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<benchmark_sort>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edx,%ebx
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %ebx,%ebx
jle    1150 <benchmark_sort+0x30>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1040 <test_sort@plt>
add    $0xffffffff,%ebx
jne    1140 <benchmark_sort+0x20>
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movsd  (%rsp),%xmm1
subsd  0x10(%rsp),%xmm1
mov    0x8(%rsp),%eax
sub    0x18(%rsp),%eax
cvtsi2sd %eax,%xmm0
divsd  0xe87(%rip),%xmm0
addsd  %xmm1,%xmm0
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { double, i32 }
define double @benchmark_sort(void (i32*, i32)* %0, i32 %1, i32 %2) {
%4 = alloca void (i32*, i32)*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = alloca %struct.timeval
store void (i32*, i32)* %0, void (i32*, i32)** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = call i32 @gettimeofday(%struct.timeval* %7, i32 0)
store i32 0, i32* %8
br label %11
11: ; preds = %19, %3
%12 = load i32, i32* %8
%13 = load i32, i32* %6
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %22
15: ; preds = %11
%16 = load void (i32*, i32)*, void (i32*, i32)** %4
%17 = load i32, i32* %5
%18 = call i32 @test_sort(void (i32*, i32)* %16, i32 %17)
br label %19
19: ; preds = %15
%20 = load i32, i32* %8
%21 = add nsw i32 %20, 1
store i32 %21, i32* %8
br label %11
22: ; preds = %11
%23 = call i32 @gettimeofday(%struct.timeval* %9, i32 0)
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%25 = load double, double* %24
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%27 = load double, double* %26
%28 = fsub double %25, %27
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%32 = load i32, i32* %31
%33 = sub nsw i32 %30, %32
%34 = sitofp i32 %33 to double
%35 = fdiv double %34, 1.000000e+06
%36 = fadd double %28, %35
ret double %36
}

LLVM IR (-O2):
%struct.timeval = type { double, i32 }
define double @benchmark_sort(void (i32*, i32)* %0, i32 %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = bitcast %struct.timeval* %4 to i8*
%7 = call i32 @gettimeofday(%struct.timeval* %4, i32 0)
%8 = icmp sgt i32 %2, 0
br i1 %8, label %25, label %9
9: ; preds = %25, %3
%10 = bitcast %struct.timeval* %5 to i8*
%11 = call i32 @gettimeofday(%struct.timeval* %5, i32 0)
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%13 = load double, double* %12
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%15 = load double, double* %14
%16 = fsub double %13, %15
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%20 = load i32, i32* %19
%21 = sub nsw i32 %18, %20
%22 = sitofp i32 %21 to double
%23 = fdiv double %22, 1.000000e+06
%24 = fadd double %16, %23
ret double %24
25: ; preds = %3, %25
%26 = phi i32 [ %28, %25 ], [ 0, %3 ]
%27 = call i32 @test_sort(void (i32*, i32)* %0, i32 %1)
%28 = add nuw nsw i32 %26, 1
%29 = icmp eq i32 %28, %2
br i1 %29, label %9, label %25
}