#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 */
typedef scalar_t__ int64_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *);
double rand_double();
scalar_t__ stdlib_ndarray_clamp_index(scalar_t__, scalar_t__);
double tic();
double benchmark() {
  double elapsed;
  int64_t idx;
  int64_t max;
  double t;
  int i;
  t = tic();
  for (i = 0; i < 1000000; i++) {
    idx = (int64_t)(rand_double() * 100.0);
    max = (int64_t)(rand_double() * 100.0);
    idx = stdlib_ndarray_clamp_index(idx, max);
    if (idx < 0) {
      printf("unexpected result\n");
      break;
    }
  }
  elapsed = tic() - t;
  if (idx < 0) {
    printf("unexpected result\n");
  }
  return elapsed;
}


x86 assembly (clang, -O0):
<benchmark>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x0,%al
call   1040 <tic@plt>
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0xf4240,-0x24(%rbp)
jge    11e0 <benchmark+0xa0>
mov    $0x0,%al
call   1060 <rand_double@plt>
movsd  0xe89(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1060 <rand_double@plt>
movsd  0xe6d(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <stdlib_ndarray_clamp_index@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11cd <benchmark+0x8d>
lea    0xe45(%rip),%rdi
call   1030 <printf@plt>
jmp    11e0 <benchmark+0xa0>
jmp    11d2 <benchmark+0x92>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    115b <benchmark+0x1b>
mov    $0x0,%al
call   1040 <tic@plt>
subsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1208 <benchmark+0xc8>
lea    0xe05(%rip),%rdi
call   1030 <printf@plt>
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<benchmark>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1040 <tic@plt>
movsd  %xmm0,0x10(%rsp)
mov    $0xf4240,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1060 <rand_double@plt>
movsd  0xe91(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rbx
xor    %eax,%eax
call   1060 <rand_double@plt>
mulsd  0xe79(%rip),%xmm0
cvttsd2si %xmm0,%rsi
mov    %rbx,%rdi
call   1050 <stdlib_ndarray_clamp_index@plt>
test   %rax,%rax
js     11a7 <benchmark+0x67>
add    $0xffffffff,%ebp
jne    1160 <benchmark+0x20>
xor    %eax,%eax
call   1040 <tic@plt>
jmp    11d1 <benchmark+0x91>
lea    0xe5a(%rip),%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <tic@plt>
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
call   1030 <puts@plt>
movsd  0x8(%rsp),%xmm0
subsd  0x10(%rsp),%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"unexpected result\0A\00"
define double @benchmark() {
%1 = alloca double
%2 = alloca i64
%3 = alloca i64
%4 = alloca double
%5 = alloca i32
%6 = call double (...) @tic()
store double %6, double* %4
store i32 0, i32* %5
br label %7
7: ; preds = %25, %0
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 1000000
br i1 %9, label %10, label %28
10: ; preds = %7
%11 = call double (...) @rand_double()
%12 = fmul double %11, 1.000000e+02
%13 = fptosi double %12 to i64
store i64 %13, i64* %2
%14 = call double (...) @rand_double()
%15 = fmul double %14, 1.000000e+02
%16 = fptosi double %15 to i64
store i64 %16, i64* %3
%17 = load i64, i64* %2
%18 = load i64, i64* %3
%19 = call i64 @stdlib_ndarray_clamp_index(i64 %17, i64 %18)
store i64 %19, i64* %2
%20 = load i64, i64* %2
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %10
%23 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %28
24: ; preds = %10
br label %25
25: ; preds = %24
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
br label %7
28: ; preds = %22, %7
%29 = call double (...) @tic()
%30 = load double, double* %4
%31 = fsub double %29, %30
store double %31, double* %1
%32 = load i64, i64* %2
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %36
36: ; preds = %34, %28
%37 = load double, double* %1
ret double %37
}

LLVM IR (-O2):
@str.1 = [18 x i8] c"unexpected result\00"
define double @benchmark() {
%1 = tail call double (...) @tic()
br label %5
2: ; preds = %5
%3 = add nuw nsw i32 %6, 1
%4 = icmp eq i32 %3, 1000000
br i1 %4, label %15, label %5
5: ; preds = %0, %2
%6 = phi i32 [ 0, %0 ], [ %3, %2 ]
%7 = tail call double (...) @rand_double()
%8 = fmul double %7, 1.000000e+02
%9 = fptosi double %8 to i64
%10 = tail call double (...) @rand_double()
%11 = fmul double %10, 1.000000e+02
%12 = fptosi double %11 to i64
%13 = tail call i64 @stdlib_ndarray_clamp_index(i64 %9, i64 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %17, label %2
15: ; preds = %2
%16 = tail call double (...) @tic()
br label %21
17: ; preds = %5
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.1, i64 0, i64 0))
%19 = tail call double (...) @tic()
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.1, i64 0, i64 0))
br label %21
21: ; preds = %15, %17
%22 = phi double [ %19, %17 ], [ %16, %15 ]
%23 = fsub double %22, %1
ret double %23
}