#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 /*<<< orphan*/ fabs(double);
int floor(int /*<<< orphan*/);
int /*<<< orphan*/ log10(int /*<<< orphan*/);
int /*<<< orphan*/ log_debug(char *, double);
scalar_t__ malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, double);
char *double_to_string_base(double n, int base) {
  log_debug("%.17f", n);
  int len = n == 0 ? 1 : floor(log10(fabs(n))) + base + 2;
  if (n < 0)
    len++;
  char *buf = (char *)malloc(len + 1);
  snprintf(buf, len + 1, "%.17f", n);
  return buf;
}


x86 assembly (clang, -O0):
<double_to_string_base>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
movsd  -0x8(%rbp),%xmm0
lea    0xe84(%rip),%rdi
call   1040 <log_debug@plt>
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11a6 <double_to_string_base+0x46>
jp     11a6 <double_to_string_base+0x46>
mov    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c7 <double_to_string_base+0x67>
movsd  -0x8(%rbp),%xmm0
call   1050 <fabs@plt>
mov    %eax,%edi
call   1070 <log10@plt>
mov    %eax,%edi
call   1080 <floor@plt>
add    -0xc(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x10(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    11e4 <double_to_string_base+0x84>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
add    $0x1,%esi
movsd  -0x8(%rbp),%xmm0
lea    0xdf7(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<double_to_string_base>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
movsd  %xmm0,(%rsp)
lea    0xe8e(%rip),%rdi
call   1040 <log_debug@plt>
mov    $0x1,%eax
xorpd  %xmm0,%xmm0
movsd  (%rsp),%xmm1
ucomisd %xmm0,%xmm1
jne    118d <double_to_string_base+0x2d>
jnp    11ae <double_to_string_base+0x4e>
movsd  (%rsp),%xmm0
call   1050 <fabs@plt>
mov    %eax,%edi
call   1070 <log10@plt>
mov    %eax,%edi
call   1080 <floor@plt>
xorpd  %xmm0,%xmm0
add    %ebx,%eax
add    $0x2,%eax
xor    %ecx,%ecx
ucomisd (%rsp),%xmm0
seta   %cl
lea    (%rcx,%rax,1),%ebx
add    $0x1,%ebx
mov    %ebx,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
lea    0xe31(%rip),%rdx
mov    %rax,%rdi
mov    %ebx,%esi
movsd  (%rsp),%xmm0
call   1030 <snprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%.17f\00"
define i8* @double_to_string_base(double %0, i32 %1) {
%3 = alloca double
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store double %0, double* %3
store i32 %1, i32* %4
%7 = load double, double* %3
%8 = call i32 @log_debug(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double %7)
%9 = load double, double* %3
%10 = fcmp oeq double %9, 0.000000e+00
br i1 %10, label %11, label %12
11: ; preds = %2
br label %20
12: ; preds = %2
%13 = load double, double* %3
%14 = call i32 @fabs(double %13)
%15 = call i32 @log10(i32 %14)
%16 = call i32 @floor(i32 %15)
%17 = load i32, i32* %4
%18 = add nsw i32 %16, %17
%19 = add nsw i32 %18, 2
br label %20
20: ; preds = %12, %11
%21 = phi i32 [ 1, %11 ], [ %19, %12 ]
store i32 %21, i32* %5
%22 = load double, double* %3
%23 = fcmp olt double %22, 0.000000e+00
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
store i32 %26, i32* %5
br label %27
27: ; preds = %24, %20
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
%30 = call i64 @malloc(i32 %29)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %6
%32 = load i8*, i8** %6
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 1
%35 = load double, double* %3
%36 = call i32 @snprintf(i8* %32, i32 %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double %35)
%37 = load i8*, i8** %6
ret i8* %37
}

LLVM IR (-O2):
@.str = [6 x i8] c"%.17f\00"
define i8* @double_to_string_base(double %0, i32 %1) {
%3 = tail call i32 @log_debug(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double %0)
%4 = fcmp oeq double %0, 0.000000e+00
br i1 %4, label %11, label %5
5: ; preds = %2
%6 = tail call i32 @fabs(double %0)
%7 = tail call i32 @log10(i32 %6)
%8 = tail call i32 @floor(i32 %7)
%9 = add i32 %1, 2
%10 = add i32 %9, %8
br label %11
11: ; preds = %2, %5
%12 = phi i32 [ %10, %5 ], [ 1, %2 ]
%13 = fcmp olt double %0, 0.000000e+00
%14 = select i1 %13, i32 2, i32 1
%15 = add i32 %14, %12
%16 = tail call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
%18 = tail call i32 @snprintf(i8* %17, i32 %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double %0)
ret i8* %17
}