#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 */
scalar_t__ isinf(double);
scalar_t__ isnan(double);
size_t snprintf(char *, size_t, char *, ...);
int d2string(char *buf, size_t len, double value) {
  if (isnan(value)) {
    len = snprintf(buf, len, "nan");
  } else if (isinf(value)) {
    if (value < 0)
      len = snprintf(buf, len, "-inf");
    else
      len = snprintf(buf, len, "inf");
  } else if (value == 0) {
    if (1.0 / value < 0)
      len = snprintf(buf, len, "-0");
    else
      len = snprintf(buf, len, "0");
  } else {
    len = snprintf(buf, len, "%.17g", value);
  }
  return len;
}


x86 assembly (clang, -O0):
<d2string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1040 <isnan@plt>
cmp    $0x0,%rax
je     1178 <d2string+0x48>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    0xea0(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %rax,-0x10(%rbp)
jmp    1271 <d2string+0x141>
movsd  -0x18(%rbp),%xmm0
call   1050 <isinf@plt>
cmp    $0x0,%rax
je     11d8 <d2string+0xa8>
xorps  %xmm0,%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    11b9 <d2string+0x89>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    0xe63(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %rax,-0x10(%rbp)
jmp    11d3 <d2string+0xa3>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    0xe45(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %rax,-0x10(%rbp)
jmp    126c <d2string+0x13c>
movsd  -0x18(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1248 <d2string+0x118>
jp     1248 <d2string+0x118>
movsd  0xe08(%rip),%xmm1
divsd  -0x18(%rbp),%xmm1
xorps  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    1229 <d2string+0xf9>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    0xdf8(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %rax,-0x10(%rbp)
jmp    1243 <d2string+0x113>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    0xdda(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %rax,-0x10(%rbp)
jmp    1267 <d2string+0x137>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movsd  -0x18(%rbp),%xmm0
lea    0xdb8(%rip),%rdx
mov    $0x1,%al
call   1030 <snprintf@plt>
mov    %rax,-0x10(%rbp)
jmp    126c <d2string+0x13c>
jmp    1271 <d2string+0x141>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<d2string>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
movsd  %xmm0,(%rsp)
call   1040 <isnan@plt>
test   %rax,%rax
je     1165 <d2string+0x35>
lea    0xeb8(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <snprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
movsd  (%rsp),%xmm0
call   1050 <isinf@plt>
xorpd  %xmm0,%xmm0
test   %rax,%rax
je     1188 <d2string+0x58>
ucomisd (%rsp),%xmm0
jbe    11b1 <d2string+0x81>
lea    0xe86(%rip),%rdx
jmp    1150 <d2string+0x20>
movsd  (%rsp),%xmm1
ucomisd %xmm0,%xmm1
jne    11ba <d2string+0x8a>
jp     11ba <d2string+0x8a>
movsd  0xe63(%rip),%xmm1
divsd  (%rsp),%xmm1
ucomisd %xmm1,%xmm0
jbe    11d5 <d2string+0xa5>
lea    0xe62(%rip),%rdx
jmp    1150 <d2string+0x20>
lea    0xe55(%rip),%rdx
jmp    1150 <d2string+0x20>
lea    0xe53(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
movsd  (%rsp),%xmm0
mov    $0x1,%al
call   1030 <snprintf@plt>
jmp    115d <d2string+0x2d>
lea    0xe36(%rip),%rdx
jmp    1150 <d2string+0x20>

LLVM IR (-O0):
@.str = [4 x i8] c"nan\00"
@.str.1 = [5 x i8] c"-inf\00"
@.str.2 = [4 x i8] c"inf\00"
@.str.3 = [3 x i8] c"-0\00"
@.str.4 = [2 x i8] c"0\00"
@.str.5 = [6 x i8] c"%.17g\00"
define i32 @d2string(i8* %0, i64 %1, double %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca double
store i8* %0, i8** %4
store i64 %1, i64* %5
store double %2, double* %6
%7 = load double, double* %6
%8 = call i64 @isnan(double %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = load i8*, i8** %4
%12 = load i64, i64* %5
%13 = call i64 (i8*, i64, i8*, ...) @snprintf(i8* %11, i64 %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i64 %13, i64* %5
br label %53
14: ; preds = %3
%15 = load double, double* %6
%16 = call i64 @isinf(double %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %14
%19 = load double, double* %6
%20 = fcmp olt double %19, 0.000000e+00
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i8*, i8** %4
%23 = load i64, i64* %5
%24 = call i64 (i8*, i64, i8*, ...) @snprintf(i8* %22, i64 %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i64 %24, i64* %5
br label %29
25: ; preds = %18
%26 = load i8*, i8** %4
%27 = load i64, i64* %5
%28 = call i64 (i8*, i64, i8*, ...) @snprintf(i8* %26, i64 %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i64 %28, i64* %5
br label %29
29: ; preds = %25, %21
br label %52
30: ; preds = %14
%31 = load double, double* %6
%32 = fcmp oeq double %31, 0.000000e+00
br i1 %32, label %33, label %46
33: ; preds = %30
%34 = load double, double* %6
%35 = fdiv double 1.000000e+00, %34
%36 = fcmp olt double %35, 0.000000e+00
br i1 %36, label %37, label %41
37: ; preds = %33
%38 = load i8*, i8** %4
%39 = load i64, i64* %5
%40 = call i64 (i8*, i64, i8*, ...) @snprintf(i8* %38, i64 %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i64 %40, i64* %5
br label %45
41: ; preds = %33
%42 = load i8*, i8** %4
%43 = load i64, i64* %5
%44 = call i64 (i8*, i64, i8*, ...) @snprintf(i8* %42, i64 %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i64 %44, i64* %5
br label %45
45: ; preds = %41, %37
br label %51
46: ; preds = %30
%47 = load i8*, i8** %4
%48 = load i64, i64* %5
%49 = load double, double* %6
%50 = call i64 (i8*, i64, i8*, ...) @snprintf(i8* %47, i64 %48, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), double %49)
store i64 %50, i64* %5
br label %51
51: ; preds = %46, %45
br label %52
52: ; preds = %51, %29
br label %53
53: ; preds = %52, %10
%54 = load i64, i64* %5
%55 = trunc i64 %54 to i32
ret i32 %55
}

LLVM IR (-O2):
@.str = [4 x i8] c"nan\00"
@.str.1 = [5 x i8] c"-inf\00"
@.str.2 = [4 x i8] c"inf\00"
@.str.3 = [3 x i8] c"-0\00"
@.str.4 = [2 x i8] c"0\00"
@.str.5 = [6 x i8] c"%.17g\00"
define i32 @d2string(i8* %0, i64 %1, double %2) {
%4 = tail call i64 @isnan(double %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i64 (i8*, i64, i8*, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %28
8: ; preds = %3
%9 = tail call i64 @isinf(double %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %8
%12 = fcmp olt double %2, 0.000000e+00
br i1 %12, label %13, label %15
13: ; preds = %11
%14 = tail call i64 (i8*, i64, i8*, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %28
15: ; preds = %11
%16 = tail call i64 (i8*, i64, i8*, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %28
17: ; preds = %8
%18 = fcmp oeq double %2, 0.000000e+00
br i1 %18, label %19, label %26
19: ; preds = %17
%20 = fdiv double 1.000000e+00, %2
%21 = fcmp olt double %20, 0.000000e+00
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i64 (i8*, i64, i8*, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %28
24: ; preds = %19
%25 = tail call i64 (i8*, i64, i8*, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %28
26: ; preds = %17
%27 = tail call i64 (i8*, i64, i8*, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), double %2)
br label %28
28: ; preds = %15, %13, %22, %24, %26, %6
%29 = phi i64 [ %7, %6 ], [ %14, %13 ], [ %16, %15 ], [ %23, %22 ], [ %25, %24 ], [ %27, %26 ]
%30 = trunc i64 %29 to i32
ret i32 %30
}