#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*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, double);
char *double_to_string_base(double n, int base) {
  ;
  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
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1185 <double_to_string_base+0x35>
jp     1185 <double_to_string_base+0x35>
mov    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a6 <double_to_string_base+0x56>
movsd  -0x8(%rbp),%xmm0
call   1040 <fabs@plt>
mov    %eax,%edi
call   1060 <log10@plt>
mov    %eax,%edi
call   1070 <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    11c3 <double_to_string_base+0x73>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
add    $0x1,%esi
movsd  -0x8(%rbp),%xmm0
lea    0xe18(%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    $0x1,%eax
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
movsd  %xmm0,(%rsp)
jne    116a <double_to_string_base+0x1a>
jnp    1192 <double_to_string_base+0x42>
mov    %edi,%ebx
movsd  (%rsp),%xmm0
call   1040 <fabs@plt>
mov    %eax,%edi
call   1060 <log10@plt>
mov    %eax,%edi
call   1070 <floor@plt>
xorpd  %xmm1,%xmm1
movsd  (%rsp),%xmm0
add    %ebx,%eax
add    $0x2,%eax
xor    %ecx,%ecx
ucomisd %xmm0,%xmm1
seta   %cl
lea    (%rcx,%rax,1),%ebx
add    $0x1,%ebx
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
lea    0xe4e(%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 = fcmp oeq double %7, 0.000000e+00
br i1 %8, label %9, label %10
9: ; preds = %2
br label %18
10: ; preds = %2
%11 = load double, double* %3
%12 = call i32 @fabs(double %11)
%13 = call i32 @log10(i32 %12)
%14 = call i32 @floor(i32 %13)
%15 = load i32, i32* %4
%16 = add nsw i32 %14, %15
%17 = add nsw i32 %16, 2
br label %18
18: ; preds = %10, %9
%19 = phi i32 [ 1, %9 ], [ %17, %10 ]
store i32 %19, i32* %5
%20 = load double, double* %3
%21 = fcmp olt double %20, 0.000000e+00
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
br label %25
25: ; preds = %22, %18
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
%28 = call i64 @malloc(i32 %27)
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %6
%30 = load i8*, i8** %6
%31 = load i32, i32* %5
%32 = add nsw i32 %31, 1
%33 = load double, double* %3
%34 = call i32 @snprintf(i8* %30, i32 %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double %33)
%35 = load i8*, i8** %6
ret i8* %35
}

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