#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 */
typedef unsigned char int32_t;

/* Variables and functions */
float powf(int, unsigned char);
int /*<<< orphan*/ sprintf(char *, char *, int, unsigned char);
char *dtostrf(double val, signed char width, unsigned char prec, char *sout) {
  char fmt[20];
  int whole = val;
  float mantissa = val - whole;
  int32_t frac = mantissa * powf(10, prec);
  if (frac < 0)
    frac = -frac;
  sprintf(fmt, "%%0%dd.%%0%dd", width, prec);
  sprintf(sout, fmt, whole, frac);
  return sout;
}


x86 assembly (clang, -O0):
<dtostrf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %sil,%al
mov    %dil,%cl
movsd  %xmm0,-0x8(%rbp)
mov    %cl,-0x9(%rbp)
mov    %al,-0xa(%rbp)
mov    %rdx,-0x18(%rbp)
cvttsd2si -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
movsd  -0x8(%rbp),%xmm0
cvtsi2sdl -0x34(%rbp),%xmm1
subsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x38(%rbp)
movss  -0x38(%rbp),%xmm0
movss  %xmm0,-0x40(%rbp)
movzbl -0xa(%rbp),%esi
mov    $0xa,%edi
call   1030 <powf@plt>
movaps %xmm0,%xmm1
movss  -0x40(%rbp),%xmm0
mulss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %al,-0x39(%rbp)
movzbl -0x39(%rbp),%eax
cmp    $0x0,%eax
jge    119f <dtostrf+0x7f>
movzbl -0x39(%rbp),%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %al,-0x39(%rbp)
lea    -0x30(%rbp),%rdi
movsbl -0x9(%rbp),%edx
lea    0xe52(%rip),%rsi
movzbl -0xa(%rbp),%ecx
call   1040 <sprintf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
movzbl -0x39(%rbp),%ecx
call   1040 <sprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dtostrf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r13
mov    %esi,%ebp
mov    %edi,%r14d
movapd %xmm0,(%rsp)
cvttsd2si %xmm0,%r15d
mov    $0xa,%edi
call   1030 <powf@plt>
movapd (%rsp),%xmm2
cvttpd2dq %xmm2,%xmm1
cvtdq2pd %xmm1,%xmm1
subsd  %xmm1,%xmm2
xorps  %xmm1,%xmm1
cvtsd2ss %xmm2,%xmm1
mulss  %xmm0,%xmm1
cvttss2si %xmm1,%ebx
lea    0xe8f(%rip),%rsi
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    %r14d,%edx
mov    %ebp,%ecx
call   1040 <sprintf@plt>
movzbl %bl,%ecx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1040 <sprintf@plt>
mov    %r13,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"%%0%dd.%%0%dd\00"
define i8* @dtostrf(double %0, i8 signext %1, i8 zeroext %2, i8* %3) {
%5 = alloca double
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8*
%9 = alloca [20 x i8]
%10 = alloca i32
%11 = alloca float
%12 = alloca i8
store double %0, double* %5
store i8 %1, i8* %6
store i8 %2, i8* %7
store i8* %3, i8** %8
%13 = load double, double* %5
%14 = fptosi double %13 to i32
store i32 %14, i32* %10
%15 = load double, double* %5
%16 = load i32, i32* %10
%17 = sitofp i32 %16 to double
%18 = fsub double %15, %17
%19 = fptrunc double %18 to float
store float %19, float* %11
%20 = load float, float* %11
%21 = load i8, i8* %7
%22 = call float @powf(i32 10, i8 zeroext %21)
%23 = fmul float %20, %22
%24 = fptoui float %23 to i8
store i8 %24, i8* %12
%25 = load i8, i8* %12
%26 = zext i8 %25 to i32
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %4
%29 = load i8, i8* %12
%30 = zext i8 %29 to i32
%31 = sub nsw i32 0, %30
%32 = trunc i32 %31 to i8
store i8 %32, i8* %12
br label %33
33: ; preds = %28, %4
%34 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%35 = load i8, i8* %6
%36 = sext i8 %35 to i32
%37 = load i8, i8* %7
%38 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %36, i8 zeroext %37)
%39 = load i8*, i8** %8
%40 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%41 = load i32, i32* %10
%42 = load i8, i8* %12
%43 = call i32 @sprintf(i8* %39, i8* %40, i32 %41, i8 zeroext %42)
%44 = load i8*, i8** %8
ret i8* %44
}

LLVM IR (-O2):
@.str = [14 x i8] c"%%0%dd.%%0%dd\00"
define i8* @dtostrf(double %0, i8 signext %1, i8 zeroext %2, i8* returned writeonly %3) {
%5 = alloca [20 x i8]
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%7 = fptosi double %0 to i32
%8 = tail call float @powf(i32 10, i8 zeroext %2)
%9 = sitofp i32 %7 to double
%10 = fsub double %0, %9
%11 = fptrunc double %10 to float
%12 = fmul float %8, %11
%13 = fptoui float %12 to i8
%14 = sext i8 %1 to i32
%15 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %14, i8 zeroext %2)
%16 = call i32 @sprintf(i8* dereferenceable(1) %3, i8* %6, i32 %7, i8 zeroext %13)
ret i8* %3
}