#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 int int32_t;

/* Variables and functions */
float powf(int, unsigned char);
int /*<<< orphan*/ sprintf(char *, char *, int, int);
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(sout, "%d.%d", 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)
mov    $0xa,%edi
movzbl -0xa(%rbp),%esi
call   1030 <powf@plt>
movaps %xmm0,%xmm1
movss  -0x40(%rbp),%xmm0
mulss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1199 <dtostrf+0x79>
xor    %eax,%eax
sub    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x3c(%rbp),%ecx
lea    0xe56(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dtostrf>:
push   %rbp
push   %rbx
push   %rax
cvttsd2si %xmm0,%ebp
cvttpd2dq %xmm0,%xmm1
cvtdq2pd %xmm1,%xmm1
mov    %rdx,%rbx
subsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,0x4(%rsp)
mov    $0xa,%edi
call   1030 <powf@plt>
mulss  0x4(%rsp),%xmm0
cvttss2si %xmm0,%eax
mov    %eax,%ecx
neg    %ecx
cmovs  %eax,%ecx
lea    0xe9e(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1040 <sprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%d.%d\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 i32
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 = fptosi float %23 to i32
store i32 %24, i32* %12
%25 = load i32, i32* %12
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %4
%28 = load i32, i32* %12
%29 = sub nsw i32 0, %28
store i32 %29, i32* %12
br label %30
30: ; preds = %27, %4
%31 = load i8*, i8** %8
%32 = load i32, i32* %10
%33 = load i32, i32* %12
%34 = call i32 @sprintf(i8* %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %32, i32 %33)
%35 = load i8*, i8** %8
ret i8* %35
}

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