#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 */
long abs(long);
int /*<<< orphan*/ itoa(long, char *, int);
char *ftoa(char *a, double f, int precision) {
  long p[] = {0, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
  char *ret = a;
  long heiltal = (long)f;
  itoa(heiltal, a, 10);
  while (*a != '\0')
    a++;
  *a++ = '.';
  long desimal = abs((long)((f - heiltal) * p[precision]));
  itoa(desimal, a, 10);
  return ret;
}


x86 assembly (clang, -O0):
<ftoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x60(%rbp),%rdi
lea    0xeae(%rip),%rsi
mov    $0x48,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x68(%rbp)
cvttsd2si -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0xa,%edx
call   1050 <itoa@plt>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11a1 <ftoa+0x71>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1180 <ftoa+0x50>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x2e,(%rax)
movsd  -0x10(%rbp),%xmm0
cvtsi2sdq -0x70(%rbp),%xmm1
subsd  %xmm1,%xmm0
movslq -0x14(%rbp),%rax
cvtsi2sdq -0x60(%rbp,%rax,8),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rdi
call   1030 <abs@plt>
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0xa,%edx
call   1050 <itoa@plt>
mov    -0x68(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftoa>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
movsd  %xmm0,0x8(%rsp)
cvttsd2si %xmm0,%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1030 <itoa@plt>
mov    %rbx,%rsi
nopl   0x0(%rax)
cmpb   $0x0,(%rsi)
lea    0x1(%rsi),%rsi
jne    1140 <ftoa+0x30>
xorps  %xmm0,%xmm0
cvtsi2sd %rbp,%xmm0
movslq %r14d,%rax
lea    0xea5(%rip),%rcx
cvtsi2sdq (%rcx,%rax,8),%xmm1
movsd  0x8(%rsp),%xmm2
subsd  %xmm0,%xmm2
mulsd  %xmm2,%xmm1
cvttsd2si %xmm1,%rax
movb   $0x2e,-0x1(%rsi)
mov    %rax,%rdi
neg    %rdi
cmovs  %rax,%rdi
mov    $0xa,%edx
call   1030 <itoa@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.ftoa.p = [9 x i64] [i64 0, i64 10, i64 100, i64 1000, i64 10000, i64 100000, i64 1000000, i64 10000000, i64 100000000]
define i8* @ftoa(i8* %0, double %1, i32 %2) {
%4 = alloca i8*
%5 = alloca double
%6 = alloca i32
%7 = alloca [9 x i64]
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %4
store double %1, double* %5
store i32 %2, i32* %6
%11 = bitcast [9 x i64]* %7 to i8*
%12 = load i8*, i8** %4
store i8* %12, i8** %8
%13 = load double, double* %5
%14 = fptosi double %13 to i64
store i64 %14, i64* %9
%15 = load i64, i64* %9
%16 = load i8*, i8** %4
%17 = call i32 @itoa(i64 %15, i8* %16, i32 10)
br label %18
18: ; preds = %23, %3
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %4
br label %18
26: ; preds = %18
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %4
store i8 46, i8* %27
%29 = load double, double* %5
%30 = load i64, i64* %9
%31 = sitofp i64 %30 to double
%32 = fsub double %29, %31
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [9 x i64], [9 x i64]* %7, i64 0, i64 %34
%36 = load i64, i64* %35
%37 = sitofp i64 %36 to double
%38 = fmul double %32, %37
%39 = fptosi double %38 to i64
%40 = call i64 @abs(i64 %39)
store i64 %40, i64* %10
%41 = load i64, i64* %10
%42 = load i8*, i8** %4
%43 = call i32 @itoa(i64 %41, i8* %42, i32 10)
%44 = load i8*, i8** %8
ret i8* %44
}

LLVM IR (-O2):
@__const.ftoa.p = [9 x i64] [i64 0, i64 10, i64 100, i64 1000, i64 10000, i64 100000, i64 1000000, i64 10000000, i64 100000000]
define i8* @ftoa(i8* returned %0, double %1, i32 %2) {
%4 = fptosi double %1 to i64
%5 = tail call i32 @itoa(i64 %4, i8* %0, i32 10)
br label %6
6: ; preds = %6, %3
%7 = phi i8* [ %0, %3 ], [ %10, %6 ]
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 0
%10 = getelementptr inbounds i8, i8* %7, i64 1
br i1 %9, label %11, label %6
11: ; preds = %6
store i8 46, i8* %7
%12 = sitofp i64 %4 to double
%13 = fsub double %1, %12
%14 = sext i32 %2 to i64
%15 = getelementptr inbounds [9 x i64], [9 x i64]* @__const.ftoa.p, i64 0, i64 %14
%16 = load i64, i64* %15
%17 = sitofp i64 %16 to double
%18 = fmul double %13, %17
%19 = fptosi double %18 to i64
%20 = tail call i64 @llvm.abs.i64(i64 %19, i1 true)
%21 = tail call i32 @itoa(i64 %20, i8* %10, i32 10)
ret i8* %0
}