#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 int int64_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *);
double rand_double();
int /*<<< orphan*/ stdlib_ndarray_minmax_view_buffer_index(int, int *, int *,
                                                           int, int *);
double tic();
double benchmark() {
  double elapsed;
  double t;
  int i;
  int64_t out[2];
  int64_t shape[] = {10, 10, 10};
  int64_t strides[] = {100, 10, 1};
  int64_t offset = 1000;
  int64_t ndims = 3;
  t = tic();
  for (i = 0; i < 1000000; i++) {
    strides[0] *= (rand_double() < 0.5) ? 1 : -1;
    stdlib_ndarray_minmax_view_buffer_index(ndims, shape, strides, offset, out);
    if (out[0] > 1e10) {
      printf("unexpected result\n");
      break;
    }
  }
  elapsed = tic() - t;
  if (out[0] > 1e10) {
    printf("unexpected result\n");
  }
  return elapsed;
}


x86 assembly (clang, -O0):
<benchmark>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0xec1(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xebf(%rip),%eax
mov    %eax,-0x20(%rbp)
mov    0xeb9(%rip),%rax
mov    %rax,-0x34(%rbp)
mov    0xeb7(%rip),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x3e8,-0x38(%rbp)
movl   $0x3,-0x3c(%rbp)
mov    $0x0,%al
call   1040 <tic@plt>
movsd  %xmm0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0xf4240,-0x14(%rbp)
jge    121a <benchmark+0xda>
mov    $0x0,%al
call   1050 <rand_double@plt>
movaps %xmm0,%xmm1
movsd  0xe58(%rip),%xmm0
mov    $0xffffffff,%eax
mov    $0x1,%ecx
ucomisd %xmm1,%xmm0
cmova  %ecx,%eax
imul   -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x3c(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x34(%rbp),%rdx
mov    -0x38(%rbp),%ecx
lea    -0x1c(%rbp),%r8
call   1060 <stdlib_ndarray_minmax_view_buffer_index@plt>
cvtsi2sdl -0x1c(%rbp),%xmm0
movsd  0xe14(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1207 <benchmark+0xc7>
lea    0xe2b(%rip),%rdi
call   1030 <printf@plt>
jmp    121a <benchmark+0xda>
jmp    120c <benchmark+0xcc>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1191 <benchmark+0x51>
mov    $0x0,%al
call   1040 <tic@plt>
subsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
cvtsi2sdl -0x1c(%rbp),%xmm0
movsd  0xdc8(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    124e <benchmark+0x10e>
lea    0xddf(%rip),%rdi
call   1030 <printf@plt>
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<benchmark>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    0xed0(%rip),%eax
mov    %eax,0x28(%rsp)
mov    0xebd(%rip),%rax
mov    %rax,0x20(%rsp)
mov    0xec6(%rip),%eax
mov    %eax,0x10(%rsp)
mov    0xeb3(%rip),%rax
mov    %rax,0x8(%rsp)
xor    %eax,%eax
call   1030 <tic@plt>
movsd  %xmm0,0x18(%rsp)
mov    $0xf4240,%ebp
lea    0x20(%rsp),%r14
lea    0x8(%rsp),%r15
lea    0x30(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <rand_double@plt>
mov    0x8(%rsp),%eax
mov    %eax,%ecx
neg    %ecx
movsd  0xe59(%rip),%xmm1
ucomisd %xmm0,%xmm1
cmova  %eax,%ecx
mov    %ecx,0x8(%rsp)
mov    $0x3,%edi
mov    %r14,%rsi
mov    %r15,%rdx
mov    $0x3e8,%ecx
mov    %rbx,%r8
call   1050 <stdlib_ndarray_minmax_view_buffer_index@plt>
add    $0xffffffff,%ebp
jne    1190 <benchmark+0x60>
xor    %eax,%eax
call   1030 <tic@plt>
subsd  0x18(%rsp),%xmm0
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.benchmark.shape = [3 x i32] [i32 10, i32 10, i32 10]
@__const.benchmark.strides = [3 x i32] [i32 100, i32 10, i32 1]
@.str = [19 x i8] c"unexpected result\0A\00"
define double @benchmark() {
%1 = alloca double
%2 = alloca double
%3 = alloca i32
%4 = alloca [2 x i32]
%5 = alloca [3 x i32]
%6 = alloca [3 x i32]
%7 = alloca i32
%8 = alloca i32
%9 = bitcast [3 x i32]* %5 to i8*
%10 = bitcast [3 x i32]* %6 to i8*
store i32 1000, i32* %7
store i32 3, i32* %8
%11 = call double (...) @tic()
store double %11, double* %2
store i32 0, i32* %3
br label %12
12: ; preds = %36, %0
%13 = load i32, i32* %3
%14 = icmp slt i32 %13, 1000000
br i1 %14, label %15, label %39
15: ; preds = %12
%16 = call double (...) @rand_double()
%17 = fcmp olt double %16, 5.000000e-01
%18 = zext i1 %17 to i64
%19 = select i1 %17, i32 1, i32 -1
%20 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 0
%21 = load i32, i32* %20
%22 = mul nsw i32 %21, %19
store i32 %22, i32* %20
%23 = load i32, i32* %8
%24 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 0
%25 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 0
%26 = load i32, i32* %7
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%28 = call i32 @stdlib_ndarray_minmax_view_buffer_index(i32 %23, i32* %24, i32* %25, i32 %26, i32* %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%30 = load i32, i32* %29
%31 = sitofp i32 %30 to double
%32 = fcmp ogt double %31, 1.000000e+10
br i1 %32, label %33, label %35
33: ; preds = %15
%34 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %39
35: ; preds = %15
br label %36
36: ; preds = %35
%37 = load i32, i32* %3
%38 = add nsw i32 %37, 1
store i32 %38, i32* %3
br label %12
39: ; preds = %33, %12
%40 = call double (...) @tic()
%41 = load double, double* %2
%42 = fsub double %40, %41
store double %42, double* %1
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%44 = load i32, i32* %43
%45 = sitofp i32 %44 to double
%46 = fcmp ogt double %45, 1.000000e+10
br i1 %46, label %47, label %49
47: ; preds = %39
%48 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %49
49: ; preds = %47, %39
%50 = load double, double* %1
ret double %50
}

LLVM IR (-O2):
@__const.benchmark.shape = [3 x i32] [i32 10, i32 10, i32 10]
@__const.benchmark.strides = [3 x i32] [i32 100, i32 10, i32 1]
define double @benchmark() {
%1 = alloca [2 x i32]
%2 = alloca [3 x i32]
%3 = alloca [3 x i32]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = bitcast [3 x i32]* %2 to i8*
%6 = bitcast [3 x i32]* %3 to i8*
%7 = tail call double (...) @tic()
%8 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 0
%9 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 0
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
br label %11
11: ; preds = %0, %11
%12 = phi i32 [ 0, %0 ], [ %19, %11 ]
%13 = call double (...) @rand_double()
%14 = fcmp olt double %13, 5.000000e-01
%15 = load i32, i32* %8
%16 = sub nsw i32 0, %15
%17 = select i1 %14, i32 %15, i32 %16
store i32 %17, i32* %8
%18 = call i32 @stdlib_ndarray_minmax_view_buffer_index(i32 3, i32* %9, i32* %8, i32 1000, i32* %10)
%19 = add nuw nsw i32 %12, 1
%20 = icmp eq i32 %19, 1000000
br i1 %20, label %21, label %11
21: ; preds = %11
%22 = call double (...) @tic()
%23 = fsub double %22, %7
ret double %23
}