#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 stdlib_ndarray_max_view_buffer_index(int, int *, int *, int);
double tic();
double benchmark() {
  double elapsed;
  int64_t n;
  double t;
  int i;
  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;
    n = stdlib_ndarray_max_view_buffer_index(ndims, shape, strides, offset);
    if (n > 1e10) {
      printf("unexpected result\n");
      break;
    }
  }
  elapsed = tic() - t;
  if (n > 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   1050 <tic@plt>
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0xf4240,-0x1c(%rbp)
jge    1219 <benchmark+0xd9>
mov    $0x0,%al
call   1060 <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
call   1030 <stdlib_ndarray_max_view_buffer_index@plt>
mov    %eax,-0xc(%rbp)
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  0xe15(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1206 <benchmark+0xc6>
lea    0xe2c(%rip),%rdi
call   1040 <printf@plt>
jmp    1219 <benchmark+0xd9>
jmp    120b <benchmark+0xcb>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1191 <benchmark+0x51>
mov    $0x0,%al
call   1050 <tic@plt>
subsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  0xdc9(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    124d <benchmark+0x10d>
lea    0xde0(%rip),%rdi
call   1040 <printf@plt>
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<benchmark>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    0xed2(%rip),%eax
mov    %eax,0x28(%rsp)
mov    0xebf(%rip),%rax
mov    %rax,0x20(%rsp)
mov    0xec8(%rip),%eax
mov    %eax,0x10(%rsp)
mov    0xeb5(%rip),%rax
mov    %rax,0x8(%rsp)
xor    %eax,%eax
call   1040 <tic@plt>
movsd  %xmm0,0x18(%rsp)
mov    $0xf4240,%ebp
lea    0x20(%rsp),%r14
lea    0x8(%rsp),%rbx
xor    %eax,%eax
call   1050 <rand_double@plt>
mov    0x8(%rsp),%eax
mov    %eax,%ecx
neg    %ecx
movsd  0xe69(%rip),%xmm1
ucomisd %xmm0,%xmm1
cmova  %eax,%ecx
mov    %ecx,0x8(%rsp)
mov    $0x3,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    $0x3e8,%ecx
call   1030 <stdlib_ndarray_max_view_buffer_index@plt>
add    $0xffffffff,%ebp
jne    1180 <benchmark+0x50>
xor    %eax,%eax
call   1040 <tic@plt>
subsd  0x18(%rsp),%xmm0
add    $0x30,%rsp
pop    %rbx
pop    %r14
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 i32
%3 = alloca double
%4 = alloca 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* %3
store i32 0, i32* %4
br label %12
12: ; preds = %34, %0
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 1000000
br i1 %14, label %15, label %37
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 = call i32 @stdlib_ndarray_max_view_buffer_index(i32 %23, i32* %24, i32* %25, i32 %26)
store i32 %27, i32* %2
%28 = load i32, i32* %2
%29 = sitofp i32 %28 to double
%30 = fcmp ogt double %29, 1.000000e+10
br i1 %30, label %31, label %33
31: ; preds = %15
%32 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %37
33: ; preds = %15
br label %34
34: ; preds = %33
%35 = load i32, i32* %4
%36 = add nsw i32 %35, 1
store i32 %36, i32* %4
br label %12
37: ; preds = %31, %12
%38 = call double (...) @tic()
%39 = load double, double* %3
%40 = fsub double %38, %39
store double %40, double* %1
%41 = load i32, i32* %2
%42 = sitofp i32 %41 to double
%43 = fcmp ogt double %42, 1.000000e+10
br i1 %43, label %44, label %46
44: ; preds = %37
%45 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %46
46: ; preds = %44, %37
%47 = load double, double* %1
ret double %47
}

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 [3 x i32]
%2 = alloca [3 x i32]
%3 = bitcast [3 x i32]* %1 to i8*
%4 = bitcast [3 x i32]* %2 to i8*
%5 = tail call double (...) @tic()
%6 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 0
%7 = getelementptr inbounds [3 x i32], [3 x i32]* %1, i64 0, i64 0
br label %8
8: ; preds = %0, %8
%9 = phi i32 [ 0, %0 ], [ %16, %8 ]
%10 = call double (...) @rand_double()
%11 = fcmp olt double %10, 5.000000e-01
%12 = load i32, i32* %6
%13 = sub nsw i32 0, %12
%14 = select i1 %11, i32 %12, i32 %13
store i32 %14, i32* %6
%15 = call i32 @stdlib_ndarray_max_view_buffer_index(i32 3, i32* %7, i32* %6, i32 1000)
%16 = add nuw nsw i32 %9, 1
%17 = icmp eq i32 %16, 1000000
br i1 %17, label %18, label %8
18: ; preds = %8
%19 = call double (...) @tic()
%20 = fsub double %19, %5
ret double %20
}