#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*/ putint(char *, int, double, int);
int /*<<< orphan*/ putuint(char *, int);
int __attribute__((optimize("-O3"))) __attribute__((hot))
putdouble_decpl(char *const str, double v, int decpl) {
  char *ptr = str;
  static const double scales[] = {1.0, 10.0, 100.0, 1000.0, 10000.0};
  double scale;
  decpl = decpl > 4 ? 4 : decpl;
  scale = scales[decpl];
  v += 0.5 / scale;
  ptr += putuint(ptr, (int)v);
  *(ptr++) = '.';
  ptr += putint(ptr, decpl, scale * v - (double)((int)v * scale), true);
  *ptr = '\0';
  return ptr - str;
}


x86 assembly (clang, -O0):
<putdouble_decpl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x4,-0x14(%rbp)
jle    1153 <putdouble_decpl+0x33>
mov    $0x4,%eax
mov    %eax,-0x2c(%rbp)
jmp    1159 <putdouble_decpl+0x39>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rcx
lea    0xea6(%rip),%rax
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0xe84(%rip),%xmm0
divsd  -0x28(%rbp),%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x20(%rbp),%rdi
cvttsd2si -0x10(%rbp),%esi
call   1040 <putuint@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x2e,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
movsd  -0x28(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
cvttsd2si -0x10(%rbp),%eax
cvtsi2sd %eax,%xmm1
mulsd  -0x28(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    $0x1,%edx
call   1030 <putint@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putdouble_decpl>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
cmp    $0x4,%esi
mov    $0x4,%eax
cmovl  %esi,%eax
movslq %eax,%r14
lea    0xf8f(%rip),%rax
movsd  (%rax,%r14,8),%xmm2
movsd  %xmm2,0x8(%rsp)
movsd  0xf6b(%rip),%xmm1
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movapd %xmm0,0x10(%rsp)
cvttsd2si %xmm0,%esi
call   1040 <putuint@plt>
cltq
lea    (%rbx,%rax,1),%r15
lea    (%rbx,%rax,1),%rdi
add    $0x1,%rdi
movapd 0x10(%rsp),%xmm1
cvttpd2dq %xmm1,%xmm0
cvtdq2pd %xmm0,%xmm0
movb   $0x2e,(%rbx,%rax,1)
movsd  0x8(%rsp),%xmm2
mulsd  %xmm2,%xmm0
mulsd  %xmm2,%xmm1
subsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
mov    %r14d,%esi
mov    $0x1,%edx
call   1030 <putint@plt>
movslq %eax,%rcx
lea    (%rcx,%r15,1),%rax
add    $0x1,%rax
movb   $0x0,0x1(%rcx,%r15,1)
sub    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
nopl   0x0(%rax)

LLVM IR (-O0):
@putdouble_decpl.scales = internal constant [5 x double] [double 1.000000e+00, double 1.000000e+01, double 1.000000e+02, double 1.000000e+03, double 1.000000e+04]
define i32 @putdouble_decpl(i8* %0, double %1, i32 %2) {
%4 = alloca i8*
%5 = alloca double
%6 = alloca i32
%7 = alloca i8*
%8 = alloca double
store i8* %0, i8** %4
store double %1, double* %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
store i8* %9, i8** %7
%10 = load i32, i32* %6
%11 = icmp sgt i32 %10, 4
br i1 %11, label %12, label %13
12: ; preds = %3
br label %15
13: ; preds = %3
%14 = load i32, i32* %6
br label %15
15: ; preds = %13, %12
%16 = phi i32 [ 4, %12 ], [ %14, %13 ]
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [5 x double], [5 x double]* @putdouble_decpl.scales, i64 0, i64 %18
%20 = load double, double* %19
store double %20, double* %8
%21 = load double, double* %8
%22 = fdiv double 5.000000e-01, %21
%23 = load double, double* %5
%24 = fadd double %23, %22
store double %24, double* %5
%25 = load i8*, i8** %7
%26 = load double, double* %5
%27 = fptosi double %26 to i32
%28 = call i32 @putuint(i8* %25, i32 %27)
%29 = load i8*, i8** %7
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8* %31, i8** %7
%32 = load i8*, i8** %7
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %7
store i8 46, i8* %32
%34 = load i8*, i8** %7
%35 = load i32, i32* %6
%36 = load double, double* %8
%37 = load double, double* %5
%38 = load double, double* %5
%39 = fptosi double %38 to i32
%40 = sitofp i32 %39 to double
%41 = load double, double* %8
%42 = fmul double %40, %41
%43 = fneg double %42
%44 = call double @llvm.fmuladd.f64(double %36, double %37, double %43)
%45 = call i32 @putint(i8* %34, i32 %35, double %44, i32 1)
%46 = load i8*, i8** %7
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8* %48, i8** %7
%49 = load i8*, i8** %7
store i8 0, i8* %49
%50 = load i8*, i8** %7
%51 = load i8*, i8** %4
%52 = ptrtoint i8* %50 to i64
%53 = ptrtoint i8* %51 to i64
%54 = sub i64 %52, %53
%55 = trunc i64 %54 to i32
ret i32 %55
}

LLVM IR (-O2):
@putdouble_decpl.scales = internal unnamed_addr constant [5 x double] [double 1.000000e+00, double 1.000000e+01, double 1.000000e+02, double 1.000000e+03, double 1.000000e+04]
define i32 @putdouble_decpl(i8* %0, double %1, i32 %2) {
%4 = icmp slt i32 %2, 4
%5 = select i1 %4, i32 %2, i32 4
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds [5 x double], [5 x double]* @putdouble_decpl.scales, i64 0, i64 %6
%8 = load double, double* %7
%9 = fdiv double 5.000000e-01, %8
%10 = fadd double %9, %1
%11 = fptosi double %10 to i32
%12 = tail call i32 @putuint(i8* %0, i32 %11)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = getelementptr inbounds i8, i8* %14, i64 1
store i8 46, i8* %14
%16 = sitofp i32 %11 to double
%17 = fneg double %8
%18 = fmul double %17, %16
%19 = tail call double @llvm.fmuladd.f64(double %8, double %10, double %18)
%20 = tail call i32 @putint(i8* %15, i32 %5, double %19, i32 1)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %15, i64 %21
store i8 0, i8* %22
%23 = ptrtoint i8* %22 to i64
%24 = ptrtoint i8* %0 to i64
%25 = sub i64 %23, %24
%26 = trunc i64 %25 to i32
ret i32 %26
}