#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 snprintf(char *, int, char *, double);
int format_double(char *buf, int buflen, double num) {
  int l = snprintf(buf, buflen, "%f", num);
  while (l > 0 && buf[l - 1] == '0')
    l--;
  return l;
}


x86 assembly (clang, -O0):
<format_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movsd  %xmm0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
movsd  -0x18(%rbp),%xmm0
lea    0xec9(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
jle    1168 <format_double+0x58>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x30,%eax
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1178 <format_double+0x68>
jmp    1186 <format_double+0x76>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    113f <format_double+0x2f>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_double>:
push   %rbx
mov    %rdi,%rbx
lea    0xee5(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,%ecx
mov    %eax,%edx
xor    %eax,%eax
test   %ecx,%ecx
cmovs  %ecx,%eax
nopl   0x0(%rax,%rax,1)
test   %edx,%edx
jle    1147 <format_double+0x37>
lea    -0x1(%rdx),%rcx
cmpb   $0x30,-0x1(%rbx,%rdx,1)
mov    %rcx,%rdx
je     1130 <format_double+0x20>
add    $0x1,%ecx
mov    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%f\00"
define i32 @format_double(i8* %0, i32 %1, double %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca double
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store double %2, double* %6
%8 = load i8*, i8** %4
%9 = load i32, i32* %5
%10 = load double, double* %6
%11 = call i32 @snprintf(i8* %8, i32 %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), double %10)
store i32 %11, i32* %7
br label %12
12: ; preds = %26, %3
%13 = load i32, i32* %7
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = load i8*, i8** %4
%17 = load i32, i32* %7
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %16, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 48
br label %24
24: ; preds = %15, %12
%25 = phi i1 [ false, %12 ], [ %23, %15 ]
br i1 %25, label %26, label %29
26: ; preds = %24
%27 = load i32, i32* %7
%28 = add nsw i32 %27, -1
store i32 %28, i32* %7
br label %12
29: ; preds = %24
%30 = load i32, i32* %7
ret i32 %30
}

LLVM IR (-O2):
@.str = [3 x i8] c"%f\00"
define i32 @format_double(i8* %0, i32 %1, double %2) {
%4 = tail call i32 @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), double %2)
%5 = zext i32 %4 to i64
%6 = call i32 @llvm.smin.i32(i32 %4, i32 0)
br label %7
7: ; preds = %11, %3
%8 = phi i64 [ %12, %11 ], [ %5, %3 ]
%9 = trunc i64 %8 to i32
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %7
%12 = add nsw i64 %8, -1
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 48
br i1 %15, label %7, label %16
16: ; preds = %11
%17 = trunc i64 %8 to i32
br label %18
18: ; preds = %7, %16
%19 = phi i32 [ %17, %16 ], [ %6, %7 ]
ret i32 %19
}