#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_strides2offset(int, int *, int *);
double tic();
double benchmark() {
  double elapsed;
  int64_t o;
  double t;
  int i;
  int64_t shape[] = {10, 10, 10};
  int64_t strides[] = {100, 10, 1};
  int64_t ndims = 3;
  t = tic();
  for (i = 0; i < 1000000; i++) {
    strides[0] *= (rand_double() < 0.5) ? -1 : 1;
    o = stdlib_ndarray_strides2offset(ndims, shape, strides);
    if (o > 1e10) {
      printf("unexpected result\n");
      break;
    }
  }
  elapsed = tic() - t;
  if (o > 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   $0x3,-0x38(%rbp)
mov    $0x0,%al
call   1040 <tic@plt>
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0xf4240,-0x1c(%rbp)
jge    120f <benchmark+0xcf>
mov    $0x0,%al
call   1050 <rand_double@plt>
movaps %xmm0,%xmm1
movsd  0xe5f(%rip),%xmm0
mov    $0x1,%eax
mov    $0xffffffff,%ecx
ucomisd %xmm1,%xmm0
cmova  %ecx,%eax
imul   -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1060 <stdlib_ndarray_strides2offset@plt>
mov    %eax,-0xc(%rbp)
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  0xe1f(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11fc <benchmark+0xbc>
lea    0xe36(%rip),%rdi
call   1030 <printf@plt>
jmp    120f <benchmark+0xcf>
jmp    1201 <benchmark+0xc1>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118a <benchmark+0x4a>
mov    $0x0,%al
call   1040 <tic@plt>
subsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  0xdd3(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1243 <benchmark+0x103>
lea    0xdea(%rip),%rdi
call   1030 <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   1030 <tic@plt>
movsd  %xmm0,0x18(%rsp)
mov    $0xf4240,%ebp
lea    0x20(%rsp),%r14
lea    0x8(%rsp),%rbx
xor    %eax,%eax
call   1040 <rand_double@plt>
mov    0x8(%rsp),%eax
mov    %eax,%ecx
neg    %ecx
movsd  0xe69(%rip),%xmm1
ucomisd %xmm0,%xmm1
cmovbe %eax,%ecx
mov    %ecx,0x8(%rsp)
mov    $0x3,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <stdlib_ndarray_strides2offset@plt>
add    $0xffffffff,%ebp
jne    1180 <benchmark+0x50>
xor    %eax,%eax
call   1030 <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 = bitcast [3 x i32]* %5 to i8*
%9 = bitcast [3 x i32]* %6 to i8*
store i32 3, i32* %7
%10 = call double (...) @tic()
store double %10, double* %3
store i32 0, i32* %4
br label %11
11: ; preds = %32, %0
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 1000000
br i1 %13, label %14, label %35
14: ; preds = %11
%15 = call double (...) @rand_double()
%16 = fcmp olt double %15, 5.000000e-01
%17 = zext i1 %16 to i64
%18 = select i1 %16, i32 -1, i32 1
%19 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 0
%20 = load i32, i32* %19
%21 = mul nsw i32 %20, %18
store i32 %21, i32* %19
%22 = load i32, i32* %7
%23 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 0
%24 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 0
%25 = call i32 @stdlib_ndarray_strides2offset(i32 %22, i32* %23, i32* %24)
store i32 %25, i32* %2
%26 = load i32, i32* %2
%27 = sitofp i32 %26 to double
%28 = fcmp ogt double %27, 1.000000e+10
br i1 %28, label %29, label %31
29: ; preds = %14
%30 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %35
31: ; preds = %14
br label %32
32: ; preds = %31
%33 = load i32, i32* %4
%34 = add nsw i32 %33, 1
store i32 %34, i32* %4
br label %11
35: ; preds = %29, %11
%36 = call double (...) @tic()
%37 = load double, double* %3
%38 = fsub double %36, %37
store double %38, double* %1
%39 = load i32, i32* %2
%40 = sitofp i32 %39 to double
%41 = fcmp ogt double %40, 1.000000e+10
br i1 %41, label %42, label %44
42: ; preds = %35
%43 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %44
44: ; preds = %42, %35
%45 = load double, double* %1
ret double %45
}

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 %13, i32 %12
store i32 %14, i32* %6
%15 = call i32 @stdlib_ndarray_strides2offset(i32 3, i32* %7, i32* %6)
%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
}