#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__ INT_MAX;
scalar_t__ fabs(double);
int /*<<< orphan*/ fatal_error(char *, char *);
double floor(double);
scalar_t__ isinf(double);
int best_scaled_value(double val, int *scale_factor, int *scale_value) {
  int n;
  if (isinf(val)) {
    fatal_error("best_scaled_value: encountered an infinite value", "");
  }
  if (val == 0.0) {
    *scale_factor = *scale_value = 0;
    return 0;
  }
  n = 0;
  if (fabs(val) > INT_MAX) {
    n = 0;
    while (fabs(val) > INT_MAX) {
      val *= 0.1;
      n--;
    }
    *scale_factor = n;
    *scale_value = floor(val + 0.5);
    return 0;
  }
  while (fabs(val * 10.0) < INT_MAX && (val - floor(val)) != 0.0) {
    n++;
    val *= 10.0;
  }
  *scale_factor = n;
  *scale_value = floor(val + 0.5);
  return 0;
}


x86 assembly (clang, -O0):
<best_scaled_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
mov    %rsi,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1060 <isinf@plt>
cmp    $0x0,%rax
je     117c <best_scaled_value+0x3c>
lea    0xea8(%rip),%rdi
lea    0xed1(%rip),%rsi
call   1040 <fatal_error@plt>
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11b4 <best_scaled_value+0x74>
jp     11b4 <best_scaled_value+0x74>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1304 <best_scaled_value+0x1c4>
movl   $0x0,-0x24(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1030 <fabs@plt>
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
jle    1247 <best_scaled_value+0x107>
movl   $0x0,-0x24(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1030 <fabs@plt>
mov    0x2dfb(%rip),%rcx
cmp    (%rcx),%rax
jle    1216 <best_scaled_value+0xd6>
movsd  0xe12(%rip),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    11dc <best_scaled_value+0x9c>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movsd  0xde1(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
call   1050 <floor@plt>
cvttsd2si %xmm0,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1304 <best_scaled_value+0x1c4>
jmp    124c <best_scaled_value+0x10c>
movsd  0xdac(%rip),%xmm0
mulsd  -0x10(%rbp),%xmm0
call   1030 <fabs@plt>
mov    %rax,%rcx
xor    %eax,%eax
mov    0x2d7e(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x25(%rbp)
jge    12a8 <best_scaled_value+0x168>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1050 <floor@plt>
movaps %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
subsd  %xmm1,%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
setne  %al
setp   %cl
or     %cl,%al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    12b8 <best_scaled_value+0x178>
jmp    12d8 <best_scaled_value+0x198>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
movsd  0xd37(%rip),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    124c <best_scaled_value+0x10c>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movsd  0xd1f(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
call   1050 <floor@plt>
cvttsd2si %xmm0,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<best_scaled_value>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
movsd  %xmm0,(%rsp)
call   1060 <isinf@plt>
test   %rax,%rax
je     1172 <best_scaled_value+0x32>
lea    0xeba(%rip),%rdi
lea    0xee3(%rip),%rsi
call   1040 <fatal_error@plt>
xorpd  %xmm1,%xmm1
movsd  (%rsp),%xmm0
ucomisd %xmm1,%xmm0
jne    1196 <best_scaled_value+0x56>
jp     1196 <best_scaled_value+0x56>
movl   $0x0,(%r14)
movl   $0x0,(%r15)
jmp    12b2 <best_scaled_value+0x172>
call   1030 <fabs@plt>
mov    0x2e46(%rip),%rbx
cmp    (%rbx),%rax
jle    11f7 <best_scaled_value+0xb7>
movsd  (%rsp),%xmm0
call   1030 <fabs@plt>
xor    %ebp,%ebp
cmp    (%rbx),%rax
jle    1278 <best_scaled_value+0x138>
xor    %ebp,%ebp
movsd  (%rsp),%xmm0
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mulsd  0xe38(%rip),%xmm0
movsd  %xmm0,(%rsp)
add    $0xffffffff,%ebp
call   1030 <fabs@plt>
movsd  (%rsp),%xmm0
cmp    (%rbx),%rax
jg     11d0 <best_scaled_value+0x90>
mov    %ebp,(%r15)
jmp    129e <best_scaled_value+0x15e>
movsd  0xe01(%rip),%xmm0
mulsd  (%rsp),%xmm0
movsd  %xmm0,0x10(%rsp)
call   1030 <fabs@plt>
xor    %ebp,%ebp
cmp    (%rbx),%rax
jge    1282 <best_scaled_value+0x142>
xor    %ebp,%ebp
movsd  (%rsp),%xmm0
nopl   (%rax)
movsd  %xmm0,(%rsp)
movsd  0x10(%rsp),%xmm1
movsd  %xmm1,0x8(%rsp)
call   1050 <floor@plt>
movsd  (%rsp),%xmm2
movapd %xmm2,%xmm1
subsd  %xmm0,%xmm1
ucomisd 0xdcd(%rip),%xmm1
jne    124f <best_scaled_value+0x10f>
jnp    128f <best_scaled_value+0x14f>
add    $0x1,%ebp
movsd  0x8(%rsp),%xmm0
mulsd  0xda0(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
call   1030 <fabs@plt>
movsd  0x8(%rsp),%xmm0
cmp    (%rbx),%rax
jl     1220 <best_scaled_value+0xe0>
jmp    1295 <best_scaled_value+0x155>
movsd  (%rsp),%xmm0
mov    %ebp,(%r15)
jmp    129e <best_scaled_value+0x15e>
movsd  (%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
jmp    1295 <best_scaled_value+0x155>
movsd  %xmm2,0x8(%rsp)
mov    %ebp,(%r15)
movsd  0x8(%rsp),%xmm0
addsd  0xd62(%rip),%xmm0
call   1050 <floor@plt>
cvttsd2si %xmm0,%eax
mov    %eax,(%r14)
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [49 x i8] c"best_scaled_value: encountered an infinite value\00"
@.str.1 = [1 x i8] zeroinitializer
@INT_MAX = global i64 0
define i32 @best_scaled_value(double %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca double
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
store double %0, double* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%9 = load double, double* %5
%10 = call i64 @isinf(double %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 @fatal_error(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %3
%15 = load double, double* %5
%16 = fcmp oeq double %15, 0.000000e+00
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32*, i32** %7
store i32 0, i32* %18
%19 = load i32*, i32** %6
store i32 0, i32* %19
store i32 0, i32* %4
br label %72
20: ; preds = %14
store i32 0, i32* %8
%21 = load double, double* %5
%22 = call i64 @fabs(double %21)
%23 = load i64, i64* @INT_MAX
%24 = icmp sgt i64 %22, %23
br i1 %24, label %25, label %44
25: ; preds = %20
store i32 0, i32* %8
br label %26
26: ; preds = %31, %25
%27 = load double, double* %5
%28 = call i64 @fabs(double %27)
%29 = load i64, i64* @INT_MAX
%30 = icmp sgt i64 %28, %29
br i1 %30, label %31, label %36
31: ; preds = %26
%32 = load double, double* %5
%33 = fmul double %32, 1.000000e-01
store double %33, double* %5
%34 = load i32, i32* %8
%35 = add nsw i32 %34, -1
store i32 %35, i32* %8
br label %26
36: ; preds = %26
%37 = load i32, i32* %8
%38 = load i32*, i32** %6
store i32 %37, i32* %38
%39 = load double, double* %5
%40 = fadd double %39, 5.000000e-01
%41 = call double @llvm.floor.f64(double %40)
%42 = fptosi double %41 to i32
%43 = load i32*, i32** %7
store i32 %42, i32* %43
store i32 0, i32* %4
br label %72
44: ; preds = %20
br label %45
45: ; preds = %59, %44
%46 = load double, double* %5
%47 = fmul double %46, 1.000000e+01
%48 = call i64 @fabs(double %47)
%49 = load i64, i64* @INT_MAX
%50 = icmp slt i64 %48, %49
br i1 %50, label %51, label %57
51: ; preds = %45
%52 = load double, double* %5
%53 = load double, double* %5
%54 = call double @llvm.floor.f64(double %53)
%55 = fsub double %52, %54
%56 = fcmp une double %55, 0.000000e+00
br label %57
57: ; preds = %51, %45
%58 = phi i1 [ false, %45 ], [ %56, %51 ]
br i1 %58, label %59, label %64
59: ; preds = %57
%60 = load i32, i32* %8
%61 = add nsw i32 %60, 1
store i32 %61, i32* %8
%62 = load double, double* %5
%63 = fmul double %62, 1.000000e+01
store double %63, double* %5
br label %45
64: ; preds = %57
%65 = load i32, i32* %8
%66 = load i32*, i32** %6
store i32 %65, i32* %66
%67 = load double, double* %5
%68 = fadd double %67, 5.000000e-01
%69 = call double @llvm.floor.f64(double %68)
%70 = fptosi double %69 to i32
%71 = load i32*, i32** %7
store i32 %70, i32* %71
store i32 0, i32* %4
br label %72
72: ; preds = %64, %36, %17
%73 = load i32, i32* %4
ret i32 %73
}

LLVM IR (-O2):
@.str = [49 x i8] c"best_scaled_value: encountered an infinite value\00"
@.str.1 = [1 x i8] zeroinitializer
@INT_MAX = global i64 0
define i32 @best_scaled_value(double %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = tail call i64 @isinf(double %0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i32 @fatal_error(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %3
%9 = fcmp oeq double %0, 0.000000e+00
br i1 %9, label %10, label %11
10: ; preds = %8
store i32 0, i32* %2
store i32 0, i32* %1
br label %57
11: ; preds = %8
%12 = tail call i64 @fabs(double %0)
%13 = load i64, i64* @INT_MAX
%14 = icmp sgt i64 %12, %13
br i1 %14, label %20, label %15
15: ; preds = %11
%16 = fmul double %0, 1.000000e+01
%17 = tail call i64 @fabs(double %16)
%18 = load i64, i64* @INT_MAX
%19 = icmp slt i64 %17, %18
br i1 %19, label %38, label %51
20: ; preds = %11
%21 = tail call i64 @fabs(double %0)
%22 = load i64, i64* @INT_MAX
%23 = icmp sgt i64 %21, %22
br i1 %23, label %24, label %32
24: ; preds = %20, %24
%25 = phi i32 [ %28, %24 ], [ 0, %20 ]
%26 = phi double [ %27, %24 ], [ %0, %20 ]
%27 = fmul double %26, 1.000000e-01
%28 = add nsw i32 %25, -1
%29 = tail call i64 @fabs(double %27)
%30 = load i64, i64* @INT_MAX
%31 = icmp sgt i64 %29, %30
br i1 %31, label %24, label %32
32: ; preds = %24, %20
%33 = phi double [ %0, %20 ], [ %27, %24 ]
%34 = phi i32 [ 0, %20 ], [ %28, %24 ]
store i32 %34, i32* %1
%35 = fadd double %33, 5.000000e-01
%36 = tail call double @llvm.floor.f64(double %35)
%37 = fptosi double %36 to i32
store i32 %37, i32* %2
br label %57
38: ; preds = %15, %45
%39 = phi double [ %47, %45 ], [ %16, %15 ]
%40 = phi i32 [ %46, %45 ], [ 0, %15 ]
%41 = phi double [ %39, %45 ], [ %0, %15 ]
%42 = tail call double @llvm.floor.f64(double %41)
%43 = fsub double %41, %42
%44 = fcmp une double %43, 0.000000e+00
br i1 %44, label %45, label %51
45: ; preds = %38
%46 = add nuw nsw i32 %40, 1
%47 = fmul double %39, 1.000000e+01
%48 = tail call i64 @fabs(double %47)
%49 = load i64, i64* @INT_MAX
%50 = icmp slt i64 %48, %49
br i1 %50, label %38, label %51
51: ; preds = %38, %45, %15
%52 = phi double [ %0, %15 ], [ %39, %45 ], [ %41, %38 ]
%53 = phi i32 [ 0, %15 ], [ %46, %45 ], [ %40, %38 ]
store i32 %53, i32* %1
%54 = fadd double %52, 5.000000e-01
%55 = tail call double @llvm.floor.f64(double %54)
%56 = fptosi double %55 to i32
store i32 %56, i32* %2
br label %57
57: ; preds = %51, %32, %10
ret i32 0
}