#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 */

/* Variables and functions */
scalar_t__ ceil(int /*<<< orphan*/);
int /*<<< orphan*/ log2(double const);
double matnorm_1(double const *, int const);
int matexp_scale_factor(const double *x, const int n) {
  int i;
  const double theta[] = {1.5e-2, 2.5e-1, 9.5e-1, 2.1e0, 5.4e0};
  const double x_1 = matnorm_1(x, n);
  for (i = 0; i < 5; i++) {
    if (x_1 <= theta[i])
      return 0;
  }
  i = (int)ceil(log2(x_1 / theta[4]));
  return 1 << i;
}


x86 assembly (clang, -O0):
<matexp_scale_factor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
movsd  0xeb9(%rip),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  0xea4(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  0xe8f(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xe7a(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0xe65(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <matnorm_1@plt>
movsd  %xmm0,-0x48(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x5,-0x18(%rbp)
jge    11fa <matexp_scale_factor+0xba>
movsd  -0x48(%rbp),%xmm1
movslq -0x18(%rbp),%rax
movsd  -0x40(%rbp,%rax,8),%xmm0
ucomisd %xmm1,%xmm0
jb     11e7 <matexp_scale_factor+0xa7>
movl   $0x0,-0x4(%rbp)
jmp    1220 <matexp_scale_factor+0xe0>
jmp    11ec <matexp_scale_factor+0xac>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11b8 <matexp_scale_factor+0x78>
movsd  -0x48(%rbp),%xmm0
divsd  -0x20(%rbp),%xmm0
call   1030 <log2@plt>
mov    %eax,%edi
call   1040 <ceil@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<matexp_scale_factor>:
push   %rax
call   1050 <matnorm_1@plt>
xor    %eax,%eax
movsd  0xec0(%rip),%xmm1
ucomisd %xmm0,%xmm1
jae    119c <matexp_scale_factor+0x6c>
movsd  0xeba(%rip),%xmm1
ucomisd %xmm0,%xmm1
jae    119c <matexp_scale_factor+0x6c>
movsd  0xeb4(%rip),%xmm1
ucomisd %xmm0,%xmm1
jae    119c <matexp_scale_factor+0x6c>
movsd  0xeae(%rip),%xmm1
ucomisd %xmm0,%xmm1
jae    119c <matexp_scale_factor+0x6c>
movsd  0xea8(%rip),%xmm1
ucomisd %xmm0,%xmm1
jae    119c <matexp_scale_factor+0x6c>
divsd  0xe9a(%rip),%xmm0
call   1030 <log2@plt>
mov    %eax,%edi
call   1040 <ceil@plt>
mov    %rax,%rcx
mov    $0x1,%eax
shl    %cl,%eax
pop    %rcx
ret

LLVM IR (-O0):
define i32 @matexp_scale_factor(double* %0, i32 %1) {
%3 = alloca i32
%4 = alloca double*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [5 x double]
%8 = alloca double
store double* %0, double** %4
store i32 %1, i32* %5
%9 = bitcast [5 x double]* %7 to i8*
%10 = bitcast i8* %9 to [5 x double]*
%11 = getelementptr inbounds [5 x double], [5 x double]* %10, i32 0, i32 0
store double 1.500000e-02, double* %11
%12 = getelementptr inbounds [5 x double], [5 x double]* %10, i32 0, i32 1
store double 2.500000e-01, double* %12
%13 = getelementptr inbounds [5 x double], [5 x double]* %10, i32 0, i32 2
store double 0x3FEE666666666666, double* %13
%14 = getelementptr inbounds [5 x double], [5 x double]* %10, i32 0, i32 3
store double 2.100000e+00, double* %14
%15 = getelementptr inbounds [5 x double], [5 x double]* %10, i32 0, i32 4
store double 5.400000e+00, double* %15
%16 = load double*, double** %4
%17 = load i32, i32* %5
%18 = call double @matnorm_1(double* %16, i32 %17)
store double %18, double* %8
store i32 0, i32* %6
br label %19
19: ; preds = %31, %2
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 5
br i1 %21, label %22, label %34
22: ; preds = %19
%23 = load double, double* %8
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [5 x double], [5 x double]* %7, i64 0, i64 %25
%27 = load double, double* %26
%28 = fcmp ole double %23, %27
br i1 %28, label %29, label %30
29: ; preds = %22
store i32 0, i32* %3
br label %44
30: ; preds = %22
br label %31
31: ; preds = %30
%32 = load i32, i32* %6
%33 = add nsw i32 %32, 1
store i32 %33, i32* %6
br label %19
34: ; preds = %19
%35 = load double, double* %8
%36 = getelementptr inbounds [5 x double], [5 x double]* %7, i64 0, i64 4
%37 = load double, double* %36
%38 = fdiv double %35, %37
%39 = call i32 @log2(double %38)
%40 = call i64 @ceil(i32 %39)
%41 = trunc i64 %40 to i32
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = shl i32 1, %42
store i32 %43, i32* %3
br label %44
44: ; preds = %34, %29
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
define i32 @matexp_scale_factor(double* %0, i32 %1) {
%3 = tail call double @matnorm_1(double* %0, i32 %1)
%4 = fcmp ugt double %3, 1.500000e-02
%5 = fcmp ugt double %3, 2.500000e-01
%6 = and i1 %4, %5
%7 = fcmp ugt double %3, 0x3FEE666666666666
%8 = and i1 %6, %7
%9 = fcmp ugt double %3, 2.100000e+00
%10 = and i1 %8, %9
%11 = fcmp ugt double %3, 5.400000e+00
%12 = and i1 %10, %11
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = fdiv double %3, 5.400000e+00
%15 = tail call i32 @log2(double %14)
%16 = tail call i64 @ceil(i32 %15)
%17 = trunc i64 %16 to i32
%18 = shl nuw i32 1, %17
br label %19
19: ; preds = %2, %13
%20 = phi i32 [ %18, %13 ], [ 0, %2 ]
ret i32 %20
}