#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 /*<<< orphan*/ free(char *);
char *itoa(int, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *ftoa(double n, int afterpoint) {
  int left_side_i = (int)n, afterpoint_o = afterpoint;
  char *left_side = itoa(left_side_i, 10);
  char *right_side = (char *)malloc(sizeof(char) * (afterpoint + 2));
  strcpy(right_side, ".");
  n -= (double)left_side_i;
  do {
    n = n * (double)10;
    int n_i = (int)n;
    n -= (double)n_i;
    char *a = itoa(n_i, 10);
    strcat(right_side, a);
    free(a);
  } while (--afterpoint != 0);
  char *res =
      (char *)malloc(sizeof(char) * (strlen(left_side) + afterpoint_o + 2));
  strcpy(res, left_side);
  strcat(res, right_side);
  free(left_side);
  free(right_side);
  return res;
}


x86 assembly (clang, -O0):
<ftoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
cvttsd2si -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
mov    $0xa,%esi
call   1080 <itoa@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1040 <strcpy@plt>
cvtsi2sdl -0x10(%rbp),%xmm1
movsd  -0x8(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  0xe2f(%rip),%xmm0
mulsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
cvttsd2si -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cvtsi2sdl -0x2c(%rbp),%xmm1
movsd  -0x8(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    -0x2c(%rbp),%edi
mov    $0xa,%esi
call   1080 <itoa@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jne    11c9 <ftoa+0x69>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
add    -0x14(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftoa>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movapd %xmm0,(%rsp)
cvttsd2si %xmm0,%edi
mov    $0xa,%esi
call   1080 <itoa@plt>
mov    %rax,%r14
lea    0x2(%rbx),%r12d
mov    %r12d,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
lea    0xe6e(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
movapd (%rsp),%xmm1
cvttpd2dq %xmm1,%xmm0
cvtdq2pd %xmm0,%xmm0
subsd  %xmm0,%xmm1
movapd %xmm1,(%rsp)
nopl   0x0(%rax,%rax,1)
movapd (%rsp),%xmm1
mulsd  0xe33(%rip),%xmm1
cvttsd2si %xmm1,%edi
cvttpd2dq %xmm1,%xmm0
cvtdq2pd %xmm0,%xmm0
subsd  %xmm0,%xmm1
movapd %xmm1,(%rsp)
mov    $0xa,%esi
call   1080 <itoa@plt>
mov    %rax,%rbp
mov    %r15,%rdi
mov    %rax,%rsi
call   1070 <strcat@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
add    $0xffffffff,%ebx
jne    11c0 <ftoa+0x60>
mov    %r14,%rdi
call   1050 <strlen@plt>
add    %eax,%r12d
mov    %r12d,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <strcat@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
define i8* @ftoa(double %0, i32 %1) {
%3 = alloca double
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store double %0, double* %3
store i32 %1, i32* %4
%12 = load double, double* %3
%13 = fptosi double %12 to i32
store i32 %13, i32* %5
%14 = load i32, i32* %4
store i32 %14, i32* %6
%15 = load i32, i32* %5
%16 = call i8* @itoa(i32 %15, i32 10)
store i8* %16, i8** %7
%17 = load i32, i32* %4
%18 = add nsw i32 %17, 2
%19 = sext i32 %18 to i64
%20 = mul i64 1, %19
%21 = trunc i64 %20 to i32
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = call i32 @strcpy(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%26 = load i32, i32* %5
%27 = sitofp i32 %26 to double
%28 = load double, double* %3
%29 = fsub double %28, %27
store double %29, double* %3
br label %30
30: ; preds = %46, %2
%31 = load double, double* %3
%32 = fmul double %31, 1.000000e+01
store double %32, double* %3
%33 = load double, double* %3
%34 = fptosi double %33 to i32
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = sitofp i32 %35 to double
%37 = load double, double* %3
%38 = fsub double %37, %36
store double %38, double* %3
%39 = load i32, i32* %9
%40 = call i8* @itoa(i32 %39, i32 10)
store i8* %40, i8** %10
%41 = load i8*, i8** %8
%42 = load i8*, i8** %10
%43 = call i32 @strcat(i8* %41, i8* %42)
%44 = load i8*, i8** %10
%45 = call i32 @free(i8* %44)
br label %46
46: ; preds = %30
%47 = load i32, i32* %4
%48 = add nsw i32 %47, -1
store i32 %48, i32* %4
%49 = icmp ne i32 %48, 0
br i1 %49, label %30, label %50
50: ; preds = %46
%51 = load i8*, i8** %7
%52 = call i32 @strlen(i8* %51)
%53 = load i32, i32* %6
%54 = add nsw i32 %52, %53
%55 = add nsw i32 %54, 2
%56 = sext i32 %55 to i64
%57 = mul i64 1, %56
%58 = trunc i64 %57 to i32
%59 = call i64 @malloc(i32 %58)
%60 = inttoptr i64 %59 to i8*
store i8* %60, i8** %11
%61 = load i8*, i8** %11
%62 = load i8*, i8** %7
%63 = call i32 @strcpy(i8* %61, i8* %62)
%64 = load i8*, i8** %11
%65 = load i8*, i8** %8
%66 = call i32 @strcat(i8* %64, i8* %65)
%67 = load i8*, i8** %7
%68 = call i32 @free(i8* %67)
%69 = load i8*, i8** %8
%70 = call i32 @free(i8* %69)
%71 = load i8*, i8** %11
ret i8* %71
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define i8* @ftoa(double %0, i32 %1) {
%3 = fptosi double %0 to i32
%4 = tail call i8* @itoa(i32 %3, i32 10)
%5 = add i32 %1, 2
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = tail call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = sitofp i32 %3 to double
%10 = fsub double %0, %9
br label %11
11: ; preds = %11, %2
%12 = phi i32 [ %1, %2 ], [ %21, %11 ]
%13 = phi double [ %10, %2 ], [ %17, %11 ]
%14 = fmul double %13, 1.000000e+01
%15 = fptosi double %14 to i32
%16 = sitofp i32 %15 to double
%17 = fsub double %14, %16
%18 = tail call i8* @itoa(i32 %15, i32 10)
%19 = tail call i32 @strcat(i8* %7, i8* %18)
%20 = tail call i32 @free(i8* %18)
%21 = add nsw i32 %12, -1
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %11
23: ; preds = %11
%24 = tail call i32 @strlen(i8* %4)
%25 = add i32 %5, %24
%26 = tail call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i8*
%28 = tail call i32 @strcpy(i8* %27, i8* %4)
%29 = tail call i32 @strcat(i8* %27, i8* %7)
%30 = tail call i32 @free(i8* %4)
%31 = tail call i32 @free(i8* %7)
ret i8* %27
}