#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 */
char *itoa2(unsigned int, char *, int);
double powi(int, int);
void dtoa(double n, char *buf, int p) {
  if (n < 0) {
    *buf = '-';
    buf++;
    n *= -1;
  }
  n += .5 / (powi(10, p));
  int pe = (int)n;
  n -= pe;
  buf = itoa2(pe, buf, 0);
  if (p > 0) {
    *buf = '.';
    buf++;
  }
  while (p > 0 && n > 0) {
    if (p <= 9) {
      unsigned int fp = n * powi(10, p);
      buf = itoa2(fp, buf, p);
      p = 0;
    } else {
      unsigned int fp = n * powi(10, 9);
      buf = itoa2(fp, buf, 9);
      n = n * powi(10, 9) - fp;
      p -= 9;
    }
  }
  *buf = '\0';
}


x86 assembly (clang, -O0):
<dtoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    1167 <dtoa+0x47>
mov    -0x10(%rbp),%rax
movb   $0x2d,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
movsd  0xea3(%rip),%xmm0
mulsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    -0x14(%rbp),%esi
mov    $0xa,%edi
call   1030 <powi@plt>
movaps %xmm0,%xmm1
movsd  0xe89(%rip),%xmm0
divsd  %xmm1,%xmm0
addsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
cvttsd2si -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
cvtsi2sdl -0x18(%rbp),%xmm1
movsd  -0x8(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1040 <itoa2@plt>
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11d7 <dtoa+0xb7>
mov    -0x10(%rbp),%rax
movb   $0x2e,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11dc <dtoa+0xbc>
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x21(%rbp)
jle    11fd <dtoa+0xdd>
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
seta   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    120d <dtoa+0xed>
jmp    12fe <dtoa+0x1de>
cmpl   $0x9,-0x14(%rbp)
jg     1261 <dtoa+0x141>
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x14(%rbp),%esi
mov    $0xa,%edi
call   1030 <powi@plt>
movaps %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <itoa2@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
jmp    12f9 <dtoa+0x1d9>
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    $0xa,%edi
mov    %edi,-0x40(%rbp)
mov    $0x9,%esi
mov    %esi,-0x3c(%rbp)
call   1030 <powi@plt>
mov    -0x3c(%rbp),%edx
movaps %xmm0,%xmm1
movsd  -0x48(%rbp),%xmm0
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <itoa2@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    %rax,-0x10(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
call   1030 <powi@plt>
movaps %xmm0,%xmm2
movsd  -0x38(%rbp),%xmm0
mov    -0x20(%rbp),%eax
cvtsi2sd %rax,%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x9,%eax
mov    %eax,-0x14(%rbp)
jmp    11dc <dtoa+0xbc>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dtoa>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jbe    1147 <dtoa+0x27>
movb   $0x2d,(%r14)
add    $0x1,%r14
xorpd  0xeb9(%rip),%xmm0
movapd %xmm0,(%rsp)
mov    $0xa,%edi
mov    %ebp,%esi
call   1030 <powi@plt>
movsd  0xeb0(%rip),%xmm1
divsd  %xmm0,%xmm1
addsd  (%rsp),%xmm1
movapd %xmm1,(%rsp)
cvttsd2si %xmm1,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <itoa2@plt>
mov    %rax,%r14
test   %ebp,%ebp
jle    1248 <dtoa+0x128>
movapd (%rsp),%xmm1
cvttpd2dq %xmm1,%xmm0
cvtdq2pd %xmm0,%xmm0
subsd  %xmm0,%xmm1
movb   $0x2e,(%r14)
add    $0x1,%r14
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
jbe    1248 <dtoa+0x128>
movapd %xmm1,%xmm0
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movapd %xmm0,(%rsp)
mov    $0xa,%edi
cmp    $0x9,%ebp
jbe    1226 <dtoa+0x106>
mov    $0x9,%esi
call   1030 <powi@plt>
mulsd  (%rsp),%xmm0
cvttsd2si %xmm0,%rbx
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x9,%edx
call   1040 <itoa2@plt>
mov    %rax,%r14
mov    $0xa,%edi
mov    $0x9,%esi
call   1030 <powi@plt>
mov    %ebx,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
mulsd  (%rsp),%xmm0
subsd  %xmm1,%xmm0
xorpd  %xmm1,%xmm1
add    $0xfffffff7,%ebp
ucomisd %xmm1,%xmm0
ja     11c0 <dtoa+0xa0>
jmp    1248 <dtoa+0x128>
mov    %ebp,%esi
call   1030 <powi@plt>
movapd (%rsp),%xmm1
mulsd  %xmm0,%xmm1
cvttsd2si %xmm1,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <itoa2@plt>
mov    %rax,%r14
movb   $0x0,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @dtoa(double %0, i8* %1, i32 %2) {
%4 = alloca double
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store double %0, double* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load double, double* %4
%11 = fcmp olt double %10, 0.000000e+00
br i1 %11, label %12, label %18
12: ; preds = %3
%13 = load i8*, i8** %5
store i8 45, i8* %13
%14 = load i8*, i8** %5
%15 = getelementptr inbounds i8, i8* %14, i32 1
store i8* %15, i8** %5
%16 = load double, double* %4
%17 = fmul double %16, -1.000000e+00
store double %17, double* %4
br label %18
18: ; preds = %12, %3
%19 = load i32, i32* %6
%20 = call double @powi(i32 10, i32 %19)
%21 = fdiv double 5.000000e-01, %20
%22 = load double, double* %4
%23 = fadd double %22, %21
store double %23, double* %4
%24 = load double, double* %4
%25 = fptosi double %24 to i32
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = sitofp i32 %26 to double
%28 = load double, double* %4
%29 = fsub double %28, %27
store double %29, double* %4
%30 = load i32, i32* %7
%31 = load i8*, i8** %5
%32 = call i8* @itoa2(i32 %30, i8* %31, i32 0)
store i8* %32, i8** %5
%33 = load i32, i32* %6
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %18
%36 = load i8*, i8** %5
store i8 46, i8* %36
%37 = load i8*, i8** %5
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %5
br label %39
39: ; preds = %35, %18
br label %40
40: ; preds = %77, %39
%41 = load i32, i32* %6
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load double, double* %4
%45 = fcmp ogt double %44, 0.000000e+00
br label %46
46: ; preds = %43, %40
%47 = phi i1 [ false, %40 ], [ %45, %43 ]
br i1 %47, label %48, label %78
48: ; preds = %46
%49 = load i32, i32* %6
%50 = icmp sle i32 %49, 9
br i1 %50, label %51, label %61
51: ; preds = %48
%52 = load double, double* %4
%53 = load i32, i32* %6
%54 = call double @powi(i32 10, i32 %53)
%55 = fmul double %52, %54
%56 = fptoui double %55 to i32
store i32 %56, i32* %8
%57 = load i32, i32* %8
%58 = load i8*, i8** %5
%59 = load i32, i32* %6
%60 = call i8* @itoa2(i32 %57, i8* %58, i32 %59)
store i8* %60, i8** %5
store i32 0, i32* %6
br label %77
61: ; preds = %48
%62 = load double, double* %4
%63 = call double @powi(i32 10, i32 9)
%64 = fmul double %62, %63
%65 = fptoui double %64 to i32
store i32 %65, i32* %9
%66 = load i32, i32* %9
%67 = load i8*, i8** %5
%68 = call i8* @itoa2(i32 %66, i8* %67, i32 9)
store i8* %68, i8** %5
%69 = load double, double* %4
%70 = call double @powi(i32 10, i32 9)
%71 = load i32, i32* %9
%72 = uitofp i32 %71 to double
%73 = fneg double %72
%74 = call double @llvm.fmuladd.f64(double %69, double %70, double %73)
store double %74, double* %4
%75 = load i32, i32* %6
%76 = sub nsw i32 %75, 9
store i32 %76, i32* %6
br label %77
77: ; preds = %61, %51
br label %40
78: ; preds = %46
%79 = load i8*, i8** %5
store i8 0, i8* %79
ret void
}

LLVM IR (-O2):
define void @dtoa(double %0, i8* %1, i32 %2) {
%4 = fcmp olt double %0, 0.000000e+00
br i1 %4, label %5, label %8
5: ; preds = %3
store i8 45, i8* %1
%6 = getelementptr inbounds i8, i8* %1, i64 1
%7 = fneg double %0
br label %8
8: ; preds = %5, %3
%9 = phi i8* [ %6, %5 ], [ %1, %3 ]
%10 = phi double [ %7, %5 ], [ %0, %3 ]
%11 = tail call double @powi(i32 10, i32 %2)
%12 = fdiv double 5.000000e-01, %11
%13 = fadd double %10, %12
%14 = fptosi double %13 to i32
%15 = sitofp i32 %14 to double
%16 = fsub double %13, %15
%17 = tail call i8* @itoa2(i32 %14, i8* %9, i32 0)
%18 = icmp sgt i32 %2, 0
br i1 %18, label %19, label %43
19: ; preds = %8
store i8 46, i8* %17
%20 = getelementptr inbounds i8, i8* %17, i64 1
%21 = fcmp ogt double %16, 0.000000e+00
br i1 %21, label %22, label %43
22: ; preds = %19, %32
%23 = phi double [ %40, %32 ], [ %16, %19 ]
%24 = phi i8* [ %36, %32 ], [ %20, %19 ]
%25 = phi i32 [ %41, %32 ], [ %2, %19 ]
%26 = icmp ult i32 %25, 10
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = tail call double @powi(i32 10, i32 %25)
%29 = fmul double %23, %28
%30 = fptoui double %29 to i32
%31 = tail call i8* @itoa2(i32 %30, i8* %24, i32 %25)
br label %43
32: ; preds = %22
%33 = tail call double @powi(i32 10, i32 9)
%34 = fmul double %23, %33
%35 = fptoui double %34 to i32
%36 = tail call i8* @itoa2(i32 %35, i8* %24, i32 9)
%37 = tail call double @powi(i32 10, i32 9)
%38 = uitofp i32 %35 to double
%39 = fneg double %38
%40 = tail call double @llvm.fmuladd.f64(double %23, double %37, double %39)
%41 = add nsw i32 %25, -9
%42 = fcmp ogt double %40, 0.000000e+00
br i1 %42, label %22, label %43
43: ; preds = %32, %8, %27, %19
%44 = phi i8* [ %20, %19 ], [ %31, %27 ], [ %17, %8 ], [ %36, %32 ]
store i8 0, i8* %44
ret void
}