#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*/ sprintf(char *, char *, double, ...);
int strlen(char *);
int format_double(double v, char *buf, int n) {
  char fmt[15];
  int k;
  sprintf(fmt, "%%%d.2lf", n);
  sprintf(buf, fmt, v);
  for (k = n; strlen(buf) > n; k--) {
    sprintf(fmt, "%%%d.%dg", n, k);
    sprintf(buf, fmt, v);
  }
  return 0;
}


x86 assembly (clang, -O0):
<format_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x23(%rbp),%rdi
cvtsi2sdl -0x14(%rbp),%xmm0
lea    0xebc(%rip),%rsi
mov    $0x1,%al
call   1040 <sprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x23(%rbp),%rsi
movsd  -0x8(%rbp),%xmm0
mov    $0x1,%al
call   1040 <sprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    -0x14(%rbp),%eax
jle    11b3 <format_double+0x93>
lea    -0x23(%rbp),%rdi
cvtsi2sdl -0x14(%rbp),%xmm0
mov    -0x28(%rbp),%edx
lea    0xe7f(%rip),%rsi
mov    $0x1,%al
call   1040 <sprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x23(%rbp),%rsi
movsd  -0x8(%rbp),%xmm0
mov    $0x1,%al
call   1040 <sprintf@plt>
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
jmp    1165 <format_double+0x45>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_double>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r15d
mov    %rdi,%rbx
movsd  %xmm0,(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sd %esi,%xmm0
lea    0xebb(%rip),%rsi
lea    0x11(%rsp),%rbp
mov    %rbp,%rdi
movsd  %xmm0,0x8(%rsp)
mov    $0x1,%al
call   1040 <sprintf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
movsd  (%rsp),%xmm0
mov    $0x1,%al
call   1040 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    %r15d,%eax
jle    11c7 <format_double+0xa7>
lea    0xe89(%rip),%r14
lea    0x11(%rsp),%r12
mov    %r15d,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r14,%rsi
movsd  0x8(%rsp),%xmm0
mov    %ebp,%edx
mov    $0x1,%al
call   1040 <sprintf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
movsd  (%rsp),%xmm0
mov    $0x1,%al
call   1040 <sprintf@plt>
add    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    %r15d,%eax
jg     1190 <format_double+0x70>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"%%%d.2lf\00"
@.str.1 = [9 x i8] c"%%%d.%dg\00"
define i32 @format_double(double %0, i8* %1, i32 %2) {
%4 = alloca double
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [15 x i8]
%8 = alloca i32
store double %0, double* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = getelementptr inbounds [15 x i8], [15 x i8]* %7, i64 0, i64 0
%10 = load i32, i32* %6
%11 = sitofp i32 %10 to double
%12 = call i32 (i8*, i8*, double, ...) @sprintf(i8* %9, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), double %11)
%13 = load i8*, i8** %5
%14 = getelementptr inbounds [15 x i8], [15 x i8]* %7, i64 0, i64 0
%15 = load double, double* %4
%16 = call i32 (i8*, i8*, double, ...) @sprintf(i8* %13, i8* %14, double %15)
%17 = load i32, i32* %6
store i32 %17, i32* %8
br label %18
18: ; preds = %33, %3
%19 = load i8*, i8** %5
%20 = call i32 @strlen(i8* %19)
%21 = load i32, i32* %6
%22 = icmp sgt i32 %20, %21
br i1 %22, label %23, label %36
23: ; preds = %18
%24 = getelementptr inbounds [15 x i8], [15 x i8]* %7, i64 0, i64 0
%25 = load i32, i32* %6
%26 = sitofp i32 %25 to double
%27 = load i32, i32* %8
%28 = call i32 (i8*, i8*, double, ...) @sprintf(i8* %24, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), double %26, i32 %27)
%29 = load i8*, i8** %5
%30 = getelementptr inbounds [15 x i8], [15 x i8]* %7, i64 0, i64 0
%31 = load double, double* %4
%32 = call i32 (i8*, i8*, double, ...) @sprintf(i8* %29, i8* %30, double %31)
br label %33
33: ; preds = %23
%34 = load i32, i32* %8
%35 = add nsw i32 %34, -1
store i32 %35, i32* %8
br label %18
36: ; preds = %18
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"%%%d.2lf\00"
@.str.1 = [9 x i8] c"%%%d.%dg\00"
define i32 @format_double(double %0, i8* %1, i32 %2) {
%4 = alloca [15 x i8]
%5 = getelementptr inbounds [15 x i8], [15 x i8]* %4, i64 0, i64 0
%6 = sitofp i32 %2 to double
%7 = call i32 (i8*, i8*, double, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), double %6)
%8 = call i32 (i8*, i8*, double, ...) @sprintf(i8* dereferenceable(1) %1, i8* %5, double %0)
%9 = tail call i32 @strlen(i8* %1)
%10 = icmp sgt i32 %9, %2
br i1 %10, label %11, label %18
11: ; preds = %3, %11
%12 = phi i32 [ %15, %11 ], [ %2, %3 ]
%13 = call i32 (i8*, i8*, double, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), double %6, i32 %12)
%14 = call i32 (i8*, i8*, double, ...) @sprintf(i8* dereferenceable(1) %1, i8* %5, double %0)
%15 = add nsw i32 %12, -1
%16 = tail call i32 @strlen(i8* %1)
%17 = icmp sgt i32 %16, %2
br i1 %17, label %11, label %18
18: ; preds = %11, %3
ret i32 0
}