#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_info(char *, ...);
scalar_t__ malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, double);
char *double_to_string(double n) {
  log_info("%f", 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;
    }
  }
  log_info("%s", buf);
  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
lea    0xe87(%rip),%rdi
mov    $0x1,%al
call   1030 <log_info@plt>
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11a5 <double_to_string+0x45>
jp     11a5 <double_to_string+0x45>
mov    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11c3 <double_to_string+0x63>
movsd  -0x8(%rbp),%xmm0
call   1050 <fabs@plt>
mov    %eax,%edi
call   1070 <log10@plt>
mov    %eax,%edi
call   1080 <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    11e0 <double_to_string+0x80>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
movsd  -0x8(%rbp),%xmm0
lea    0xdfe(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    126c <double_to_string+0x10c>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x30,%eax
je     1259 <double_to_string+0xf9>
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     1254 <double_to_string+0xf4>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    126c <double_to_string+0x10c>
jmp    126c <double_to_string+0x10c>
jmp    125e <double_to_string+0xfe>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1213 <double_to_string+0xb3>
mov    -0x18(%rbp),%rsi
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1030 <log_info@plt>
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
movsd  %xmm0,0x8(%rsp)
lea    0xe8b(%rip),%rdi
mov    $0x1,%al
call   1030 <log_info@plt>
mov    $0x1,%eax
xorpd  %xmm0,%xmm0
movsd  0x8(%rsp),%xmm1
ucomisd %xmm0,%xmm1
jne    1193 <double_to_string+0x33>
jnp    11b3 <double_to_string+0x53>
movsd  0x8(%rsp),%xmm0
call   1050 <fabs@plt>
mov    %eax,%edi
call   1070 <log10@plt>
mov    %eax,%edi
call   1080 <floor@plt>
xorpd  %xmm0,%xmm0
add    $0x11,%eax
xor    %ecx,%ecx
ucomisd 0x8(%rsp),%xmm0
seta   %cl
mov    %eax,%ebp
add    %ecx,%ebp
lea    (%rax,%rcx,1),%r14d
add    $0x1,%r14d
mov    %r14d,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0xe27(%rip),%rdx
mov    %rax,%rdi
mov    %r14d,%esi
movsd  0x8(%rsp),%xmm0
call   1040 <snprintf@plt>
xor    %eax,%eax
nop
cmp    $0x2,%ebp
jl     120f <double_to_string+0xaf>
add    $0x1,%eax
lea    -0x1(%rbp),%rcx
cmpb   $0x30,-0x1(%rbx,%rbp,1)
mov    %rcx,%rbp
je     11f0 <double_to_string+0x90>
test   %eax,%eax
je     120f <double_to_string+0xaf>
movb   $0x0,0x1(%rbx,%rcx,1)
lea    0xdf3(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <log_info@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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