#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(double n) {
  ;
  int len = n == 0 ? 1 : floor(log10(fabs(n))) + 17;
  if (n < 0)
    len++;
  char *buf = (char *)malloc(len + 1);
  snprintf(buf, len + 1, "%.16f", n);
  int i;
  for (i = len - 1; i > 0; i--) {
    if (buf[i] != '0') {
      if (i != len) {
        buf[i + 1] = '\0';
        break;
      }
      break;
    }
  };
  return buf;
}


x86 assembly (clang, -O0):
<double_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1182 <double_to_string+0x32>
jp     1182 <double_to_string+0x32>
mov    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11a0 <double_to_string+0x50>
movsd  -0x8(%rbp),%xmm0
call   1040 <fabs@plt>
mov    %eax,%edi
call   1060 <log10@plt>
mov    %eax,%edi
call   1070 <floor@plt>
add    $0x11,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0xc(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    11bd <double_to_string+0x6d>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
movsd  -0x8(%rbp),%xmm0
lea    0xe1e(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1249 <double_to_string+0xf9>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x30,%eax
je     1236 <double_to_string+0xe6>
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     1231 <double_to_string+0xe1>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1249 <double_to_string+0xf9>
jmp    1249 <double_to_string+0xf9>
jmp    123b <double_to_string+0xeb>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f0 <double_to_string+0xa0>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<double_to_string>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0x1,%eax
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
movsd  %xmm0,0x8(%rsp)
jne    116f <double_to_string+0x1f>
jnp    1195 <double_to_string+0x45>
movsd  0x8(%rsp),%xmm0
call   1040 <fabs@plt>
mov    %eax,%edi
call   1060 <log10@plt>
mov    %eax,%edi
call   1070 <floor@plt>
xorpd  %xmm1,%xmm1
movsd  0x8(%rsp),%xmm0
add    $0x11,%eax
xor    %ecx,%ecx
ucomisd %xmm0,%xmm1
seta   %cl
mov    %eax,%ebp
add    %ecx,%ebp
lea    (%rax,%rcx,1),%r14d
add    $0x1,%r14d
mov    %r14d,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
lea    0xe44(%rip),%rdx
mov    %rax,%rdi
mov    %r14d,%esi
movsd  0x8(%rsp),%xmm0
call   1030 <snprintf@plt>
xor    %eax,%eax
nop
cmp    $0x2,%ebp
jl     11ef <double_to_string+0x9f>
add    $0x1,%eax
lea    -0x1(%rbp),%rcx
cmpb   $0x30,-0x1(%rbx,%rbp,1)
mov    %rcx,%rbp
je     11d0 <double_to_string+0x80>
test   %eax,%eax
je     11ef <double_to_string+0x9f>
movb   $0x0,0x1(%rbx,%rcx,1)
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%.16f\00"
define i8* @double_to_string(double %0) {
%2 = alloca double
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store double %0, double* %2
%6 = load double, double* %2
%7 = fcmp oeq double %6, 0.000000e+00
br i1 %7, label %8, label %9
8: ; preds = %1
br label %15
9: ; preds = %1
%10 = load double, double* %2
%11 = call i32 @fabs(double %10)
%12 = call i32 @log10(i32 %11)
%13 = call i32 @floor(i32 %12)
%14 = add nsw i32 %13, 17
br label %15
15: ; preds = %9, %8
%16 = phi i32 [ 1, %8 ], [ %14, %9 ]
store i32 %16, i32* %3
%17 = load double, double* %2
%18 = fcmp olt double %17, 0.000000e+00
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
store i32 %21, i32* %3
br label %22
22: ; preds = %19, %15
%23 = load i32, i32* %3
%24 = add nsw i32 %23, 1
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %4
%27 = load i8*, i8** %4
%28 = load i32, i32* %3
%29 = add nsw i32 %28, 1
%30 = load double, double* %2
%31 = call i32 @snprintf(i8* %27, i32 %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double %30)
%32 = load i32, i32* %3
%33 = sub nsw i32 %32, 1
store i32 %33, i32* %5
br label %34
34: ; preds = %57, %22
%35 = load i32, i32* %5
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %60
37: ; preds = %34
%38 = load i8*, i8** %4
%39 = load i32, i32* %5
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 48
br i1 %44, label %45, label %56
45: ; preds = %37
%46 = load i32, i32* %5
%47 = load i32, i32* %3
%48 = icmp ne i32 %46, %47
br i1 %48, label %49, label %55
49: ; preds = %45
%50 = load i8*, i8** %4
%51 = load i32, i32* %5
%52 = add nsw i32 %51, 1
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %50, i64 %53
store i8 0, i8* %54
br label %60
55: ; preds = %45
br label %60
56: ; preds = %37
br label %57
57: ; preds = %56
%58 = load i32, i32* %5
%59 = add nsw i32 %58, -1
store i32 %59, i32* %5
br label %34
60: ; preds = %55, %49, %34
%61 = load i8*, i8** %4
ret i8* %61
}

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