#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 clampi(scalar_t__, int /*<<< orphan*/, int);
int /*<<< orphan*/ log10(double);
int fixed_decimals(double x, int digits) {
  if (x > -1e-10 && x < 1e-10)
    return (((digits - 1) > (0) ? (digits - 1) : (0)));
  if (x < 0)
    x *= -1;
  if (x < 1)
    digits = ((digits - 1) > (0) ? (digits - 1) : (0));
  return (clampi(digits - ceil(log10(x)), 0, digits));
}


x86 assembly (clang, -O0):
<fixed_decimals>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xeb3(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    119c <fixed_decimals+0x6c>
movsd  0xea9(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    119c <fixed_decimals+0x6c>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cmp    $0x0,%eax
jle    1187 <fixed_decimals+0x57>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1191 <fixed_decimals+0x61>
xor    %eax,%eax
mov    %eax,-0x18(%rbp)
jmp    1191 <fixed_decimals+0x61>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1229 <fixed_decimals+0xf9>
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    11bc <fixed_decimals+0x8c>
movsd  0xe5e(%rip),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  0xe54(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    11fc <fixed_decimals+0xcc>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cmp    $0x0,%eax
jle    11ec <fixed_decimals+0xbc>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f6 <fixed_decimals+0xc6>
xor    %eax,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f6 <fixed_decimals+0xc6>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
mov    %rax,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1040 <log10@plt>
mov    %eax,%edi
call   1030 <ceil@plt>
mov    -0x28(%rbp),%rdi
sub    %rax,%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1050 <clampi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fixed_decimals>:
push   %rbx
ucomisd 0xec7(%rip),%xmm0
jbe    1158 <fixed_decimals+0x28>
movsd  0xec5(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    1158 <fixed_decimals+0x28>
test   %edi,%edi
mov    $0x1,%eax
cmovg  %edi,%eax
add    $0xffffffff,%eax
pop    %rbx
ret
movapd 0xec0(%rip),%xmm2
xorpd  %xmm0,%xmm2
xorpd  %xmm3,%xmm3
movapd %xmm0,%xmm1
cmpltsd %xmm3,%xmm1
andpd  %xmm1,%xmm2
andnpd %xmm0,%xmm1
orpd   %xmm2,%xmm1
test   %edi,%edi
mov    $0x1,%eax
cmovg  %edi,%eax
add    $0xffffffff,%eax
movsd  0xe7e(%rip),%xmm0
ucomisd %xmm1,%xmm0
cmovbe %edi,%eax
movslq %eax,%rbx
movapd %xmm1,%xmm0
call   1040 <log10@plt>
mov    %eax,%edi
call   1030 <ceil@plt>
mov    %rbx,%rdi
sub    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
pop    %rbx
jmp    1050 <clampi@plt>

LLVM IR (-O0):
define i32 @fixed_decimals(double %0, i32 %1) {
%3 = alloca i32
%4 = alloca double
%5 = alloca i32
store double %0, double* %4
store i32 %1, i32* %5
%6 = load double, double* %4
%7 = fcmp ogt double %6, -1.000000e-10
br i1 %7, label %8, label %21
8: ; preds = %2
%9 = load double, double* %4
%10 = fcmp olt double %9, 1.000000e-10
br i1 %10, label %11, label %21
11: ; preds = %8
%12 = load i32, i32* %5
%13 = sub nsw i32 %12, 1
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* %5
%17 = sub nsw i32 %16, 1
br label %19
18: ; preds = %11
br label %19
19: ; preds = %18, %15
%20 = phi i32 [ %17, %15 ], [ 0, %18 ]
store i32 %20, i32* %3
br label %49
21: ; preds = %8, %2
%22 = load double, double* %4
%23 = fcmp olt double %22, 0.000000e+00
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load double, double* %4
%26 = fmul double %25, -1.000000e+00
store double %26, double* %4
br label %27
27: ; preds = %24, %21
%28 = load double, double* %4
%29 = fcmp olt double %28, 1.000000e+00
br i1 %29, label %30, label %40
30: ; preds = %27
%31 = load i32, i32* %5
%32 = sub nsw i32 %31, 1
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* %5
%36 = sub nsw i32 %35, 1
br label %38
37: ; preds = %30
br label %38
38: ; preds = %37, %34
%39 = phi i32 [ %36, %34 ], [ 0, %37 ]
store i32 %39, i32* %5
br label %40
40: ; preds = %38, %27
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
%43 = load double, double* %4
%44 = call i32 @log10(double %43)
%45 = call i64 @ceil(i32 %44)
%46 = sub nsw i64 %42, %45
%47 = load i32, i32* %5
%48 = call i32 @clampi(i64 %46, i32 0, i32 %47)
store i32 %48, i32* %3
br label %49
49: ; preds = %40, %19
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
define i32 @fixed_decimals(double %0, i32 %1) {
%3 = fcmp ogt double %0, -1.000000e-10
%4 = fcmp olt double %0, 1.000000e-10
%5 = and i1 %3, %4
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = icmp sgt i32 %1, 1
%8 = select i1 %7, i32 %1, i32 1
%9 = add nsw i32 %8, -1
br label %24
10: ; preds = %2
%11 = fcmp olt double %0, 0.000000e+00
%12 = fneg double %0
%13 = select i1 %11, double %12, double %0
%14 = fcmp olt double %13, 1.000000e+00
%15 = icmp sgt i32 %1, 1
%16 = select i1 %15, i32 %1, i32 1
%17 = add nsw i32 %16, -1
%18 = select i1 %14, i32 %17, i32 %1
%19 = sext i32 %18 to i64
%20 = tail call i32 @log10(double %13)
%21 = tail call i64 @ceil(i32 %20)
%22 = sub nsw i64 %19, %21
%23 = tail call i32 @clampi(i64 %22, i32 0, i32 %18)
br label %24
24: ; preds = %10, %6
%25 = phi i32 [ %9, %6 ], [ %23, %10 ]
ret i32 %25
}