#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*/ snprintf(char *, size_t, char *, int, double);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
int strlen(char *);
void format_double(double val, int precision, int sci_ok, char *buf,
                   size_t buflen) {
  char *p, *q;
  snprintf(buf, buflen - 3, sci_ok ? "%.*g" : "%.*f", precision, val);
  p = strchr(buf, 'e');
  if (p)
    return;
  p = strchr(buf, '.');
  if (!p) {
    strcat(buf, ".0");
  } else {
    for (q = buf + strlen(buf) - 1; q > p + 1; --q) {
      if (*q == '0')
        *q = '\0';
      else
        break;
    }
  }
}


x86 assembly (clang, -O0):
<format_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
sub    $0x3,%rsi
mov    -0x10(%rbp),%ecx
lea    0xe94(%rip),%rdx
lea    0xe88(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
mov    -0xc(%rbp),%ecx
movsd  -0x8(%rbp),%xmm0
call   1050 <snprintf@plt>
mov    -0x18(%rbp),%rdi
mov    $0x65,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11ae <format_double+0x6e>
jmp    1256 <format_double+0x116>
mov    -0x18(%rbp),%rdi
mov    $0x2e,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e0 <format_double+0xa0>
mov    -0x18(%rbp),%rdi
lea    0xe34(%rip),%rsi
call   1060 <strcat@plt>
jmp    1256 <format_double+0x116>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jbe    1251 <format_double+0x111>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jne    1236 <format_double+0xf6>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
jmp    123b <format_double+0xfb>
jmp    1251 <format_double+0x111>
jmp    1240 <format_double+0x100>
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
jmp    1205 <format_double+0xc5>
jmp    1256 <format_double+0x116>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_double>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %edi,%eax
add    $0xfffffffffffffffd,%rcx
test   %esi,%esi
lea    0xeaf(%rip),%rsi
lea    0xea3(%rip),%rdx
cmove  %rsi,%rdx
mov    %r14,%rdi
mov    %rcx,%rsi
mov    %eax,%ecx
call   1050 <snprintf@plt>
mov    %r14,%rdi
mov    $0x65,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1188 <format_double+0x48>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rdi
mov    $0x2e,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11d3 <format_double+0x93>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
add    $0x1,%rbx
add    %r14,%rax
add    $0xffffffffffffffff,%rax
cmp    %rbx,%rax
jbe    1180 <format_double+0x40>
nopw   0x0(%rax,%rax,1)
cmpb   $0x30,(%rax)
jne    1180 <format_double+0x40>
movb   $0x0,(%rax)
add    $0xffffffffffffffff,%rax
cmp    %rbx,%rax
ja     11c0 <format_double+0x80>
jmp    1180 <format_double+0x40>
lea    0xe30(%rip),%rsi
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <strcat@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"%.*g\00"
@.str.1 = [5 x i8] c"%.*f\00"
@.str.2 = [3 x i8] c".0\00"
define void @format_double(double %0, i32 %1, i32 %2, i8* %3, i64 %4) {
%6 = alloca double
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
store double %0, double* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i64 %4, i64* %10
%13 = load i8*, i8** %9
%14 = load i64, i64* %10
%15 = sub i64 %14, 3
%16 = load i32, i32* %8
%17 = icmp ne i32 %16, 0
%18 = zext i1 %17 to i64
%19 = select i1 %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0)
%20 = load i32, i32* %7
%21 = load double, double* %6
%22 = call i32 @snprintf(i8* %13, i64 %15, i8* %19, i32 %20, double %21)
%23 = load i8*, i8** %9
%24 = call i8* @strchr(i8* %23, i8 signext 101)
store i8* %24, i8** %11
%25 = load i8*, i8** %11
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %5
br label %61
28: ; preds = %5
%29 = load i8*, i8** %9
%30 = call i8* @strchr(i8* %29, i8 signext 46)
store i8* %30, i8** %11
%31 = load i8*, i8** %11
%32 = icmp ne i8* %31, null
br i1 %32, label %36, label %33
33: ; preds = %28
%34 = load i8*, i8** %9
%35 = call i32 @strcat(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %61
36: ; preds = %28
%37 = load i8*, i8** %9
%38 = load i8*, i8** %9
%39 = call i32 @strlen(i8* %38)
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
%42 = getelementptr inbounds i8, i8* %41, i64 -1
store i8* %42, i8** %12
br label %43
43: ; preds = %57, %36
%44 = load i8*, i8** %12
%45 = load i8*, i8** %11
%46 = getelementptr inbounds i8, i8* %45, i64 1
%47 = icmp ugt i8* %44, %46
br i1 %47, label %48, label %60
48: ; preds = %43
%49 = load i8*, i8** %12
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 48
br i1 %52, label %53, label %55
53: ; preds = %48
%54 = load i8*, i8** %12
store i8 0, i8* %54
br label %56
55: ; preds = %48
br label %60
56: ; preds = %53
br label %57
57: ; preds = %56
%58 = load i8*, i8** %12
%59 = getelementptr inbounds i8, i8* %58, i32 -1
store i8* %59, i8** %12
br label %43
60: ; preds = %55, %43
br label %61
61: ; preds = %27, %60, %33
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%.*g\00"
@.str.1 = [5 x i8] c"%.*f\00"
@.str.2 = [3 x i8] c".0\00"
define void @format_double(double %0, i32 %1, i32 %2, i8* %3, i64 %4) {
%6 = add i64 %4, -3
%7 = icmp eq i32 %2, 0
%8 = select i1 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0)
%9 = tail call i32 @snprintf(i8* %3, i64 %6, i8* %8, i32 %1, double %0)
%10 = tail call i8* @strchr(i8* dereferenceable(1) %3, i8 signext 101)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %31
12: ; preds = %5
%13 = tail call i8* @strchr(i8* dereferenceable(1) %3, i8 signext 46)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @strcat(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %31
17: ; preds = %12
%18 = tail call i32 @strlen(i8* %3)
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %3, i64 %19
%21 = getelementptr inbounds i8, i8* %13, i64 1
%22 = getelementptr inbounds i8, i8* %20, i64 -1
%23 = icmp ugt i8* %22, %21
br i1 %23, label %24, label %31
24: ; preds = %17, %28
%25 = phi i8* [ %29, %28 ], [ %22, %17 ]
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 48
br i1 %27, label %28, label %31
28: ; preds = %24
store i8 0, i8* %25
%29 = getelementptr inbounds i8, i8* %25, i64 -1
%30 = icmp ugt i8* %29, %21
br i1 %30, label %24, label %31
31: ; preds = %28, %24, %17, %15, %5
ret void
}