#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 */
int INT_MAX;
int abs(int);
double exp(int);
int fabs(double);
int floor(double);
int log(int);
int log10(double);
int rint(int);
double tick_interval(double x) {
  int low_log10 = rint(log10(x));
  double low_PoT = exp(low_log10 * log(10));
  int divisors[] = {2, 4, 5, 10};
  int preferred_num_tick_intervals[] = {4, 5};
  int penalty = INT_MAX;
  double best_tick_interval = -1;
  int worst_penalty = INT_MAX;
  int i = -1;
  for (i = 0; i < 4; i++) {
    double tick_interval = low_PoT / divisors[i];
    int num_tick_intervals = floor(x / tick_interval);
    double remainder = x - (num_tick_intervals * tick_interval);
    int relative_error = rint(100 * fabs(remainder / x));
    int j = -1;
    for (j = 0; j < 2; j++) {
      int tick_diff = abs(num_tick_intervals - preferred_num_tick_intervals[j]);
      penalty = 10 * tick_diff + relative_error;
      if (penalty < worst_penalty) {
        worst_penalty = penalty;
        best_tick_interval = tick_interval;
      }
    }
  }
  return best_tick_interval;
}


x86 assembly (clang, -O0):
<tick_interval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1080 <log10@plt>
mov    %eax,%edi
call   1050 <rint@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x78(%rbp)
mov    $0xa,%edi
call   1070 <log@plt>
mov    -0x78(%rbp),%edi
imul   %eax,%edi
call   1060 <exp@plt>
movsd  %xmm0,-0x18(%rbp)
mov    0xe55(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe52(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe37(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
movsd  0xe17(%rip),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
movl   $0xffffffff,-0x50(%rbp)
movl   $0x0,-0x50(%rbp)
cmpl   $0x4,-0x50(%rbp)
jge    1300 <tick_interval+0x190>
movsd  -0x18(%rbp),%xmm0
movslq -0x50(%rbp),%rax
cvtsi2sdl -0x30(%rbp,%rax,4),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x8(%rbp),%xmm0
divsd  -0x58(%rbp),%xmm0
call   1090 <floor@plt>
mov    %eax,-0x5c(%rbp)
movsd  -0x8(%rbp),%xmm1
cvtsi2sdl -0x5c(%rbp),%xmm0
movsd  -0x58(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x68(%rbp)
movsd  -0x68(%rbp),%xmm0
divsd  -0x8(%rbp),%xmm0
call   1040 <fabs@plt>
imul   $0x64,%eax,%edi
call   1050 <rint@plt>
mov    %eax,-0x6c(%rbp)
movl   $0xffffffff,-0x70(%rbp)
movl   $0x0,-0x70(%rbp)
cmpl   $0x2,-0x70(%rbp)
jge    12ed <tick_interval+0x17d>
mov    -0x5c(%rbp),%edi
movslq -0x70(%rbp),%rax
sub    -0x38(%rbp,%rax,4),%edi
call   1030 <abs@plt>
mov    %eax,-0x74(%rbp)
imul   $0xa,-0x74(%rbp),%eax
add    -0x6c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x4c(%rbp),%eax
jge    12da <tick_interval+0x16a>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
movsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
jmp    12df <tick_interval+0x16f>
mov    -0x70(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x70(%rbp)
jmp    1297 <tick_interval+0x127>
jmp    12f2 <tick_interval+0x182>
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    1208 <tick_interval+0x98>
movsd  -0x48(%rbp),%xmm0
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tick_interval>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
movsd  %xmm0,0x8(%rsp)
call   1070 <log10@plt>
mov    %eax,%edi
call   1040 <rint@plt>
mov    %eax,%ebx
mov    $0xa,%edi
call   1060 <log@plt>
imul   %eax,%ebx
mov    %ebx,%edi
call   1050 <exp@plt>
movsd  %xmm0,0x18(%rsp)
mov    0x2e49(%rip),%rax
mov    (%rax),%r14d
movsd  0xe56(%rip),%xmm1
xor    %ebp,%ebp
lea    0xe5d(%rip),%r15
jmp    11d1 <tick_interval+0x71>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x4,%rbp
movaps %xmm0,%xmm1
cmp    $0x10,%rbp
je     127b <tick_interval+0x11b>
movsd  %xmm1,0x20(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sdl 0x0(%rbp,%r15,1),%xmm0
movsd  0x18(%rsp),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,0x10(%rsp)
movsd  0x8(%rsp),%xmm0
divsd  %xmm1,%xmm0
call   1080 <floor@plt>
mov    %eax,%ebx
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
mulsd  0x10(%rsp),%xmm1
movsd  0x8(%rsp),%xmm2
movapd %xmm2,%xmm0
subsd  %xmm1,%xmm0
divsd  %xmm2,%xmm0
call   1030 <fabs@plt>
imul   $0x64,%eax,%edi
call   1040 <rint@plt>
movsd  0x10(%rsp),%xmm0
lea    -0x4(%rbx),%ecx
mov    %ecx,%edx
neg    %edx
cmovs  %ecx,%edx
lea    (%rdx,%rdx,4),%edx
add    $0xfffffffb,%ebx
mov    %ebx,%ecx
neg    %ecx
cmovs  %ebx,%ecx
lea    (%rax,%rdx,2),%edx
movaps %xmm0,%xmm1
cmp    %r14d,%edx
cmovl  %edx,%r14d
jl     1260 <tick_interval+0x100>
movsd  0x20(%rsp),%xmm1
lea    (%rcx,%rcx,4),%ecx
lea    (%rax,%rcx,2),%eax
cmp    %r14d,%eax
cmovl  %eax,%r14d
jl     11c0 <tick_interval+0x60>
movaps %xmm1,%xmm0
jmp    11c0 <tick_interval+0x60>
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.tick_interval.divisors = [4 x i32] [i32 2, i32 4, i32 5, i32 10]
@__const.tick_interval.preferred_num_tick_intervals = [2 x i32] [i32 4, i32 5]
@INT_MAX = global i32 0
define double @tick_interval(double %0) {
%2 = alloca double
%3 = alloca i32
%4 = alloca double
%5 = alloca [4 x i32]
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca double
%9 = alloca i32
%10 = alloca i32
%11 = alloca double
%12 = alloca i32
%13 = alloca double
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store double %0, double* %2
%17 = load double, double* %2
%18 = call i32 @log10(double %17)
%19 = call i32 @rint(i32 %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = call i32 @log(i32 10)
%22 = mul nsw i32 %20, %21
%23 = call double @exp(i32 %22)
store double %23, double* %4
%24 = bitcast [4 x i32]* %5 to i8*
%25 = bitcast [2 x i32]* %6 to i8*
%26 = load i32, i32* @INT_MAX
store i32 %26, i32* %7
store double -1.000000e+00, double* %8
%27 = load i32, i32* @INT_MAX
store i32 %27, i32* %9
store i32 -1, i32* %10
store i32 0, i32* %10
br label %28
28: ; preds = %81, %1
%29 = load i32, i32* %10
%30 = icmp slt i32 %29, 4
br i1 %30, label %31, label %84
31: ; preds = %28
%32 = load double, double* %4
%33 = load i32, i32* %10
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [4 x i32], [4 x i32]* %5, i64 0, i64 %34
%36 = load i32, i32* %35
%37 = sitofp i32 %36 to double
%38 = fdiv double %32, %37
store double %38, double* %11
%39 = load double, double* %2
%40 = load double, double* %11
%41 = fdiv double %39, %40
%42 = call i32 @floor(double %41)
store i32 %42, i32* %12
%43 = load double, double* %2
%44 = load i32, i32* %12
%45 = sitofp i32 %44 to double
%46 = load double, double* %11
%47 = fneg double %45
%48 = call double @llvm.fmuladd.f64(double %47, double %46, double %43)
store double %48, double* %13
%49 = load double, double* %13
%50 = load double, double* %2
%51 = fdiv double %49, %50
%52 = call i32 @fabs(double %51)
%53 = mul nsw i32 100, %52
%54 = call i32 @rint(i32 %53)
store i32 %54, i32* %14
store i32 -1, i32* %15
store i32 0, i32* %15
br label %55
55: ; preds = %77, %31
%56 = load i32, i32* %15
%57 = icmp slt i32 %56, 2
br i1 %57, label %58, label %80
58: ; preds = %55
%59 = load i32, i32* %12
%60 = load i32, i32* %15
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 %61
%63 = load i32, i32* %62
%64 = sub nsw i32 %59, %63
%65 = call i32 @abs(i32 %64)
store i32 %65, i32* %16
%66 = load i32, i32* %16
%67 = mul nsw i32 10, %66
%68 = load i32, i32* %14
%69 = add nsw i32 %67, %68
store i32 %69, i32* %7
%70 = load i32, i32* %7
%71 = load i32, i32* %9
%72 = icmp slt i32 %70, %71
br i1 %72, label %73, label %76
73: ; preds = %58
%74 = load i32, i32* %7
store i32 %74, i32* %9
%75 = load double, double* %11
store double %75, double* %8
br label %76
76: ; preds = %73, %58
br label %77
77: ; preds = %76
%78 = load i32, i32* %15
%79 = add nsw i32 %78, 1
store i32 %79, i32* %15
br label %55
80: ; preds = %55
br label %81
81: ; preds = %80
%82 = load i32, i32* %10
%83 = add nsw i32 %82, 1
store i32 %83, i32* %10
br label %28
84: ; preds = %28
%85 = load double, double* %8
ret double %85
}

LLVM IR (-O2):
@__const.tick_interval.divisors = [4 x i32] [i32 2, i32 4, i32 5, i32 10]
@INT_MAX = global i32 0
define double @tick_interval(double %0) {
%2 = tail call i32 @log10(double %0)
%3 = tail call i32 @rint(i32 %2)
%4 = tail call i32 @log(i32 10)
%5 = mul nsw i32 %4, %3
%6 = tail call double @exp(i32 %5)
%7 = load i32, i32* @INT_MAX
br label %8
8: ; preds = %1, %8
%9 = phi i64 [ 0, %1 ], [ %39, %8 ]
%10 = phi double [ -1.000000e+00, %1 ], [ %38, %8 ]
%11 = phi i32 [ %7, %1 ], [ %36, %8 ]
%12 = getelementptr inbounds [4 x i32], [4 x i32]* @__const.tick_interval.divisors, i64 0, i64 %9
%13 = load i32, i32* %12
%14 = sitofp i32 %13 to double
%15 = fdiv double %6, %14
%16 = fdiv double %0, %15
%17 = tail call i32 @floor(double %16)
%18 = sitofp i32 %17 to double
%19 = fneg double %18
%20 = tail call double @llvm.fmuladd.f64(double %19, double %15, double %0)
%21 = fdiv double %20, %0
%22 = tail call i32 @fabs(double %21)
%23 = mul nsw i32 %22, 100
%24 = tail call i32 @rint(i32 %23)
%25 = add nsw i32 %17, -4
%26 = tail call i32 @llvm.abs.i32(i32 %25, i1 true)
%27 = mul nsw i32 %26, 10
%28 = add nsw i32 %27, %24
%29 = icmp slt i32 %28, %11
%30 = select i1 %29, i32 %28, i32 %11
%31 = add nsw i32 %17, -5
%32 = tail call i32 @llvm.abs.i32(i32 %31, i1 true)
%33 = mul nsw i32 %32, 10
%34 = add nsw i32 %33, %24
%35 = icmp slt i32 %34, %30
%36 = select i1 %35, i32 %34, i32 %30
%37 = or i1 %35, %29
%38 = select i1 %37, double %15, double %10
%39 = add nuw nsw i64 %9, 1
%40 = icmp eq i64 %39, 4
br i1 %40, label %41, label %8
41: ; preds = %8
ret double %38
}