#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 floor(float);
int log10(float);
float pow(float, int);
char *strdup(char *);
char *ftoa(float num) {
  int i = 0;
  char fstr[80];
  int m = log10(num);
  int digit;
  float tolerance = .0001f;
  float precision = 0.00000000000001;
  while (num > 0 + precision) {
    float weight = pow(10.0f, m);
    digit = floor(num / weight);
    num -= (digit * weight);
    fstr[i++] = '0' + digit;
    if (m == 0)
      fstr[i++] = '.';
    m--;
  }
  fstr[i] = '\0';
  return strdup(fstr);
}


x86 assembly (clang, -O0):
<ftoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movss  %xmm0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movss  -0x4(%rbp),%xmm0
call   1040 <log10@plt>
mov    %eax,-0x64(%rbp)
movss  0xe98(%rip),%xmm0
movss  %xmm0,-0x6c(%rbp)
movss  0xe87(%rip),%xmm0
movss  %xmm0,-0x70(%rbp)
movss  -0x4(%rbp),%xmm0
xorps  %xmm1,%xmm1
addss  -0x70(%rbp),%xmm1
ucomiss %xmm1,%xmm0
jbe    1227 <ftoa+0xe7>
mov    -0x64(%rbp),%edi
movss  0xe69(%rip),%xmm0
call   1030 <pow@plt>
movss  %xmm0,-0x74(%rbp)
movss  -0x4(%rbp),%xmm0
divss  -0x74(%rbp),%xmm0
call   1060 <floor@plt>
mov    %eax,-0x68(%rbp)
cvtsi2ssl -0x68(%rbp),%xmm0
movss  -0x74(%rbp),%xmm2
movss  -0x4(%rbp),%xmm1
movd   %xmm0,%eax
xor    $0x80000000,%eax
movd   %eax,%xmm0
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x4(%rbp)
mov    -0x68(%rbp),%eax
add    $0x30,%eax
mov    %al,%cl
mov    -0x8(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x8(%rbp)
cltq
mov    %cl,-0x60(%rbp,%rax,1)
cmpl   $0x0,-0x64(%rbp)
jne    1219 <ftoa+0xd9>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x8(%rbp)
cltq
movb   $0x2e,-0x60(%rbp,%rax,1)
mov    -0x64(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x64(%rbp)
jmp    117e <ftoa+0x3e>
movslq -0x8(%rbp),%rax
movb   $0x0,-0x60(%rbp,%rax,1)
lea    -0x60(%rbp),%rdi
call   1050 <strdup@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftoa>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
movss  %xmm0,0x8(%rsp)
call   1040 <log10@plt>
movss  0x8(%rsp),%xmm0
xor    %ecx,%ecx
ucomiss 0xea0(%rip),%xmm0
jbe    11e1 <ftoa+0xa1>
mov    %eax,%ebx
xor    %ebp,%ebp
jmp    1195 <ftoa+0x55>
nopl   0x0(%rax,%rax,1)
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
mulss  0xc(%rsp),%xmm0
movss  0x8(%rsp),%xmm1
subss  %xmm0,%xmm1
ucomiss 0xe72(%rip),%xmm1
mov    %ecx,%ebp
movaps %xmm1,%xmm0
jbe    11e1 <ftoa+0xa1>
movss  %xmm0,0x8(%rsp)
movss  0xe61(%rip),%xmm0
mov    %ebx,%edi
call   1030 <pow@plt>
movaps %xmm0,%xmm1
movss  0x8(%rsp),%xmm0
movss  %xmm1,0xc(%rsp)
divss  %xmm1,%xmm0
call   1060 <floor@plt>
lea    0x30(%rax),%edx
movslq %ebp,%rcx
mov    %dl,0x10(%rsp,%rcx,1)
add    $0x1,%rcx
sub    $0x1,%ebx
jae    1170 <ftoa+0x30>
add    $0x2,%ebp
movb   $0x2e,0x10(%rsp,%rcx,1)
mov    %ebp,%ecx
jmp    1170 <ftoa+0x30>
movslq %ecx,%rax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x10(%rsp),%rdi
call   1050 <strdup@plt>
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i8* @ftoa(float %0) {
%2 = alloca float
%3 = alloca i32
%4 = alloca [80 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca float
%8 = alloca float
%9 = alloca float
store float %0, float* %2
store i32 0, i32* %3
%10 = load float, float* %2
%11 = call i32 @log10(float %10)
store i32 %11, i32* %5
store float 0x3F1A36E2E0000000, float* %7
store float 0x3D06849B80000000, float* %8
br label %12
12: ; preds = %44, %1
%13 = load float, float* %2
%14 = load float, float* %8
%15 = fadd float 0.000000e+00, %14
%16 = fcmp ogt float %13, %15
br i1 %16, label %17, label %47
17: ; preds = %12
%18 = load i32, i32* %5
%19 = call float @pow(float 1.000000e+01, i32 %18)
store float %19, float* %9
%20 = load float, float* %2
%21 = load float, float* %9
%22 = fdiv float %20, %21
%23 = call i32 @floor(float %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = sitofp i32 %24 to float
%26 = load float, float* %9
%27 = load float, float* %2
%28 = fneg float %25
%29 = call float @llvm.fmuladd.f32(float %28, float %26, float %27)
store float %29, float* %2
%30 = load i32, i32* %6
%31 = add nsw i32 48, %30
%32 = trunc i32 %31 to i8
%33 = load i32, i32* %3
%34 = add nsw i32 %33, 1
store i32 %34, i32* %3
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 %35
store i8 %32, i8* %36
%37 = load i32, i32* %5
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %17
%40 = load i32, i32* %3
%41 = add nsw i32 %40, 1
store i32 %41, i32* %3
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 %42
store i8 46, i8* %43
br label %44
44: ; preds = %39, %17
%45 = load i32, i32* %5
%46 = add nsw i32 %45, -1
store i32 %46, i32* %5
br label %12
47: ; preds = %12
%48 = load i32, i32* %3
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 %49
store i8 0, i8* %50
%51 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%52 = call i8* @strdup(i8* %51)
ret i8* %52
}

LLVM IR (-O2):
define noalias i8* @ftoa(float %0) {
%2 = alloca [80 x i8]
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @log10(float %0)
%5 = fcmp ogt float %0, 0x3D06849B80000000
br i1 %5, label %6, label %30
6: ; preds = %1, %26
%7 = phi float [ %15, %26 ], [ %0, %1 ]
%8 = phi i32 [ %27, %26 ], [ 0, %1 ]
%9 = phi i32 [ %28, %26 ], [ %4, %1 ]
%10 = tail call float @pow(float 1.000000e+01, i32 %9)
%11 = fdiv float %7, %10
%12 = tail call i32 @floor(float %11)
%13 = sitofp i32 %12 to float
%14 = fneg float %13
%15 = tail call float @llvm.fmuladd.f32(float %14, float %10, float %7)
%16 = trunc i32 %12 to i8
%17 = add i8 %16, 48
%18 = add nsw i32 %8, 1
%19 = sext i32 %8 to i64
%20 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 %19
store i8 %17, i8* %20
%21 = icmp eq i32 %9, 0
br i1 %21, label %22, label %26
22: ; preds = %6
%23 = add nsw i32 %8, 2
%24 = sext i32 %18 to i64
%25 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 %24
store i8 46, i8* %25
br label %26
26: ; preds = %22, %6
%27 = phi i32 [ %23, %22 ], [ %18, %6 ]
%28 = add nsw i32 %9, -1
%29 = fcmp ogt float %15, 0x3D06849B80000000
br i1 %29, label %6, label %30
30: ; preds = %26, %1
%31 = phi i32 [ 0, %1 ], [ %27, %26 ]
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 %32
store i8 0, i8* %33
%34 = call i8* @strdup(i8* %3)
ret i8* %34
}