#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 */
double Round(double, int);
scalar_t__ floor(double);
int Ndrgtb(double n, int b) {
  double base = b;
  int i;
  n = n < 0.0 ? -n : n;
  i = 0;
  while (i < 20) {
    n -= floor(n);
    if (1E-12 >= n)
      break;
    i++;
    n = Round(n * base, 13);
  }
  return (i);
}


x86 assembly (clang, -O0):
<Ndrgtb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    116e <Ndrgtb+0x4e>
movsd  -0x8(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    1178 <Ndrgtb+0x58>
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x14,-0x1c(%rbp)
jge    11ef <Ndrgtb+0xcf>
movsd  -0x8(%rbp),%xmm0
call   1040 <floor@plt>
cvtsi2sd %rax,%xmm1
movsd  -0x8(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  0xe48(%rip),%xmm0
ucomisd -0x8(%rbp),%xmm0
jb     11c8 <Ndrgtb+0xa8>
jmp    11ef <Ndrgtb+0xcf>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movsd  -0x8(%rbp),%xmm0
mulsd  -0x18(%rbp),%xmm0
mov    $0xd,%edi
call   1030 <Round@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    1189 <Ndrgtb+0x69>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Ndrgtb>:
push   %rbx
sub    $0x20,%rsp
cvtsi2sd %edi,%xmm1
movsd  %xmm1,0x8(%rsp)
movapd 0xec9(%rip),%xmm1
xorpd  %xmm0,%xmm1
xorpd  %xmm2,%xmm2
movapd %xmm0,%xmm3
cmpltsd %xmm2,%xmm3
andpd  %xmm3,%xmm1
andnpd %xmm0,%xmm3
orpd   %xmm1,%xmm3
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movapd %xmm3,0x10(%rsp)
movapd %xmm3,%xmm0
call   1040 <floor@plt>
movapd 0x10(%rsp),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
subsd  %xmm1,%xmm0
movsd  0xe87(%rip),%xmm1
ucomisd %xmm0,%xmm1
jae    11b0 <Ndrgtb+0x90>
add    $0x1,%ebx
mulsd  0x8(%rsp),%xmm0
mov    $0xd,%edi
call   1030 <Round@plt>
movapd %xmm0,%xmm3
cmp    $0x14,%ebx
jne    1160 <Ndrgtb+0x40>
mov    $0x14,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @Ndrgtb(double %0, i32 %1) {
%3 = alloca double
%4 = alloca i32
%5 = alloca double
%6 = alloca i32
store double %0, double* %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = sitofp i32 %7 to double
store double %8, double* %5
%9 = load double, double* %3
%10 = fcmp olt double %9, 0.000000e+00
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load double, double* %3
%13 = fneg double %12
br label %16
14: ; preds = %2
%15 = load double, double* %3
br label %16
16: ; preds = %14, %11
%17 = phi double [ %13, %11 ], [ %15, %14 ]
store double %17, double* %3
store i32 0, i32* %6
br label %18
18: ; preds = %30, %16
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 20
br i1 %20, label %21, label %37
21: ; preds = %18
%22 = load double, double* %3
%23 = call i64 @floor(double %22)
%24 = sitofp i64 %23 to double
%25 = load double, double* %3
%26 = fsub double %25, %24
store double %26, double* %3
%27 = load double, double* %3
%28 = fcmp oge double 0x3D719799812DEA11, %27
br i1 %28, label %29, label %30
29: ; preds = %21
br label %37
30: ; preds = %21
%31 = load i32, i32* %6
%32 = add nsw i32 %31, 1
store i32 %32, i32* %6
%33 = load double, double* %3
%34 = load double, double* %5
%35 = fmul double %33, %34
%36 = call double @Round(double %35, i32 13)
store double %36, double* %3
br label %18
37: ; preds = %29, %18
%38 = load i32, i32* %6
ret i32 %38
}

LLVM IR (-O2):
define i32 @Ndrgtb(double %0, i32 %1) {
%3 = sitofp i32 %1 to double
%4 = fcmp olt double %0, 0.000000e+00
%5 = fneg double %0
%6 = select i1 %4, double %5, double %0
br label %7
7: ; preds = %2, %14
%8 = phi i32 [ 0, %2 ], [ %15, %14 ]
%9 = phi double [ %6, %2 ], [ %17, %14 ]
%10 = tail call i64 @floor(double %9)
%11 = sitofp i64 %10 to double
%12 = fsub double %9, %11
%13 = fcmp ugt double %12, 0x3D719799812DEA11
br i1 %13, label %14, label %19
14: ; preds = %7
%15 = add nuw nsw i32 %8, 1
%16 = fmul double %12, %3
%17 = tail call double @Round(double %16, i32 13)
%18 = icmp eq i32 %15, 20
br i1 %18, label %19, label %7
19: ; preds = %7, %14
%20 = phi i32 [ %8, %7 ], [ 20, %14 ]
ret i32 %20
}