#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 const, int);
int __attribute__((optimize("-O3"))) __attribute__((hot))
putdouble(char *str, const double val, const int base, const int width) {
  const double v = val + 0.005;
  int n = width - 3;
  if (n < 1)
    n = 1;
  (void)putint(str, n, (int)v, false);
  str[n] = '.';
  (void)putint(str + n + 1, 2, v * (double)base - (double)((int)v * base),
               true);
  str[width + 1] = '\0';
  return width;
}


x86 assembly (clang, -O0):
<putdouble>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movsd  0xed1(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jge    1153 <putdouble+0x43>
movl   $0x1,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
cvttsd2si -0x20(%rbp),%eax
cvtsi2sd %eax,%xmm0
xor    %edx,%edx
call   1030 <putint@plt>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x2e,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
movsd  -0x20(%rbp),%xmm0
cvtsi2sdl -0x14(%rbp),%xmm2
cvttsd2si -0x20(%rbp),%eax
imul   -0x14(%rbp),%eax
cvtsi2sd %eax,%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mov    $0x2,%esi
mov    $0x1,%edx
call   1030 <putint@plt>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putdouble>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
addsd  0xf99(%rip),%xmm0
movapd %xmm0,(%rsp)
cmp    $0x5,%edx
mov    $0x4,%ebp
cmovge %edx,%ebp
mov    %esi,%r15d
mov    %rdi,%r12
add    $0xfffffffd,%ebp
cvttsd2si %xmm0,%ebx
cvttpd2dq %xmm0,%xmm1
cvtdq2pd %xmm1,%xmm0
mov    %ebp,%esi
xor    %edx,%edx
call   1030 <putint@plt>
movb   $0x2e,(%r12,%rbp,1)
lea    (%r12,%rbp,1),%rdi
add    $0x1,%rdi
xorps  %xmm0,%xmm0
cvtsi2sd %r15d,%xmm0
imul   %r15d,%ebx
xorps  %xmm1,%xmm1
cvtsi2sd %ebx,%xmm1
mulsd  (%rsp),%xmm0
subsd  %xmm1,%xmm0
mov    $0x2,%esi
mov    $0x1,%edx
call   1030 <putint@plt>
movslq %r14d,%rax
movb   $0x0,0x1(%rax,%r12,1)
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)

LLVM IR (-O0):
define i32 @putdouble(i8* %0, double %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca double
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca i32
store i8* %0, i8** %5
store double %1, double* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load double, double* %6
%12 = fadd double %11, 5.000000e-03
store double %12, double* %9
%13 = load i32, i32* %8
%14 = sub nsw i32 %13, 3
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 1, i32* %10
br label %18
18: ; preds = %17, %4
%19 = load i8*, i8** %5
%20 = load i32, i32* %10
%21 = load double, double* %9
%22 = fptosi double %21 to i32
%23 = sitofp i32 %22 to double
%24 = call i32 @putint(i8* %19, i32 %20, double %23, i32 0)
%25 = load i8*, i8** %5
%26 = load i32, i32* %10
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 46, i8* %28
%29 = load i8*, i8** %5
%30 = load i32, i32* %10
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = load double, double* %9
%35 = load i32, i32* %7
%36 = sitofp i32 %35 to double
%37 = load double, double* %9
%38 = fptosi double %37 to i32
%39 = load i32, i32* %7
%40 = mul nsw i32 %38, %39
%41 = sitofp i32 %40 to double
%42 = fneg double %41
%43 = call double @llvm.fmuladd.f64(double %34, double %36, double %42)
%44 = call i32 @putint(i8* %33, i32 2, double %43, i32 1)
%45 = load i8*, i8** %5
%46 = load i32, i32* %8
%47 = add nsw i32 %46, 1
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
store i8 0, i8* %49
%50 = load i32, i32* %8
ret i32 %50
}

LLVM IR (-O2):
define i32 @putdouble(i8* %0, double %1, i32 %2, i32 returned %3) {
%5 = fadd double %1, 5.000000e-03
%6 = icmp sgt i32 %3, 4
%7 = select i1 %6, i32 %3, i32 4
%8 = add nsw i32 %7, -3
%9 = fptosi double %5 to i32
%10 = sitofp i32 %9 to double
%11 = tail call i32 @putint(i8* %0, i32 %8, double %10, i32 0)
%12 = zext i32 %8 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 46, i8* %13
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = sitofp i32 %2 to double
%16 = mul nsw i32 %9, %2
%17 = sitofp i32 %16 to double
%18 = fneg double %17
%19 = tail call double @llvm.fmuladd.f64(double %5, double %15, double %18)
%20 = tail call i32 @putint(i8* %14, i32 2, double %19, i32 1)
%21 = add nsw i32 %3, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %0, i64 %22
store i8 0, i8* %23
ret i32 %3
}