#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__ emit_digits(char *, int, char *, int, int, int);
int filter_special(double, char *);
int get_dbits(double);
int grisu2(double, char *, int *);
int fpconv_dtoa(double d, char dest[24], int is_decimal) {
  char digits[18];
  int str_len = 0;
  bool neg = false;
  bool decimal = (is_decimal != 0);
  if (get_dbits(d) & 0x8000000000000000U) {
    dest[0] = '-';
    str_len++;
    neg = true;
  }
  int spec = filter_special(d, dest + str_len);
  if (spec) {
    return str_len + spec;
  }
  int K = 0;
  int ndigits = grisu2(d, digits, &K);
  str_len += emit_digits(digits, ndigits, dest + str_len, K, neg, decimal);
  return str_len;
}


x86 assembly (clang, -O0):
<fpconv_dtoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
cmpl   $0x0,-0x1c(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x3c(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1050 <get_dbits@plt>
cltq
movabs $0x8000000000000000,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
je     11ab <fpconv_dtoa+0x6b>
mov    -0x18(%rbp),%rax
movb   $0x2d,(%rax)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
movl   $0x1,-0x38(%rbp)
movsd  -0x10(%rbp),%xmm0
mov    -0x18(%rbp),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
call   1030 <filter_special@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
je     11db <fpconv_dtoa+0x9b>
mov    -0x34(%rbp),%eax
add    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122c <fpconv_dtoa+0xec>
movl   $0x0,-0x44(%rbp)
movsd  -0x10(%rbp),%xmm0
lea    -0x30(%rbp),%rdi
lea    -0x44(%rbp),%rsi
call   1040 <grisu2@plt>
mov    %eax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x48(%rbp),%esi
mov    -0x18(%rbp),%rdx
movslq -0x34(%rbp),%rax
add    %rax,%rdx
mov    -0x44(%rbp),%ecx
mov    -0x38(%rbp),%r8d
mov    -0x3c(%rbp),%r9d
call   1060 <emit_digits@plt>
mov    %rax,%rcx
movslq -0x34(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fpconv_dtoa>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
xor    %r14d,%r14d
test   %esi,%esi
setne  %r15b
movsd  %xmm0,0x8(%rsp)
call   1050 <get_dbits@plt>
xor    %ebp,%ebp
test   %eax,%eax
jns    116f <fpconv_dtoa+0x2f>
movb   $0x2d,(%rbx)
mov    $0x1,%ebp
mov    %ebp,%eax
add    %rax,%rbx
movsd  0x8(%rsp),%xmm0
mov    %rbx,%rdi
call   1030 <filter_special@plt>
test   %eax,%eax
jne    11c0 <fpconv_dtoa+0x80>
mov    %r15b,%r14b
movl   $0x0,0x4(%rsp)
lea    0x10(%rsp),%r15
lea    0x4(%rsp),%rsi
movsd  0x8(%rsp),%xmm0
mov    %r15,%rdi
call   1040 <grisu2@plt>
mov    0x4(%rsp),%ecx
mov    %r15,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
mov    %ebp,%r8d
mov    %r14d,%r9d
call   1060 <emit_digits@plt>
add    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fpconv_dtoa(double %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca double
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [18 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store double %0, double* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
store i32 0, i32* %10
%15 = load i32, i32* %7
%16 = icmp ne i32 %15, 0
%17 = zext i1 %16 to i32
store i32 %17, i32* %11
%18 = load double, double* %5
%19 = call i32 @get_dbits(double %18)
%20 = sext i32 %19 to i64
%21 = and i64 %20, -9223372036854775808
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %3
%24 = load i8*, i8** %6
%25 = getelementptr inbounds i8, i8* %24, i64 0
store i8 45, i8* %25
%26 = load i32, i32* %9
%27 = add nsw i32 %26, 1
store i32 %27, i32* %9
store i32 1, i32* %10
br label %28
28: ; preds = %23, %3
%29 = load double, double* %5
%30 = load i8*, i8** %6
%31 = load i32, i32* %9
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = call i32 @filter_special(double %29, i8* %33)
store i32 %34, i32* %12
%35 = load i32, i32* %12
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %28
%38 = load i32, i32* %9
%39 = load i32, i32* %12
%40 = add nsw i32 %38, %39
store i32 %40, i32* %4
br label %60
41: ; preds = %28
store i32 0, i32* %13
%42 = load double, double* %5
%43 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
%44 = call i32 @grisu2(double %42, i8* %43, i32* %13)
store i32 %44, i32* %14
%45 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
%46 = load i32, i32* %14
%47 = load i8*, i8** %6
%48 = load i32, i32* %9
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = load i32, i32* %13
%52 = load i32, i32* %10
%53 = load i32, i32* %11
%54 = call i64 @emit_digits(i8* %45, i32 %46, i8* %50, i32 %51, i32 %52, i32 %53)
%55 = load i32, i32* %9
%56 = sext i32 %55 to i64
%57 = add nsw i64 %56, %54
%58 = trunc i64 %57 to i32
store i32 %58, i32* %9
%59 = load i32, i32* %9
store i32 %59, i32* %4
br label %60
60: ; preds = %41, %37
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
define i32 @fpconv_dtoa(double %0, i8* %1, i32 %2) {
%4 = alloca [18 x i8]
%5 = alloca i32
%6 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 0
%7 = icmp ne i32 %2, 0
%8 = zext i1 %7 to i32
%9 = tail call i32 @get_dbits(double %0)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %12, label %11
11: ; preds = %3
store i8 45, i8* %1
br label %12
12: ; preds = %11, %3
%13 = phi i32 [ 1, %11 ], [ 0, %3 ]
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %1, i64 %14
%16 = tail call i32 @filter_special(double %0, i8* %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %12
%19 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%20 = call i32 @grisu2(double %0, i8* %6, i32* %5)
%21 = load i32, i32* %5
%22 = call i64 @emit_digits(i8* %6, i32 %20, i8* %15, i32 %21, i32 %13, i32 %8)
%23 = trunc i64 %22 to i32
br label %24
24: ; preds = %12, %18
%25 = phi i32 [ %23, %18 ], [ %16, %12 ]
%26 = add i32 %25, %13
ret i32 %26
}