#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 const *, int, double);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
int strlen(char *);
void libconfig_format_double(double val, int precision, int sci_ok, char *buf,
                             size_t buflen) {
  const char *fmt = sci_ok ? "%.*g" : "%.*f";
  char *p, *q;
  snprintf(buf, buflen - 3, fmt, 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):
<libconfig_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    -0x10(%rbp),%edx
lea    0xea0(%rip),%rax
lea    0xe94(%rip),%rcx
cmp    $0x0,%edx
cmovne %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
sub    $0x3,%rsi
mov    -0x28(%rbp),%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,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11b6 <libconfig_format_double+0x76>
jmp    125e <libconfig_format_double+0x11e>
mov    -0x18(%rbp),%rdi
mov    $0x2e,%esi
call   1040 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e8 <libconfig_format_double+0xa8>
mov    -0x18(%rbp),%rdi
lea    0xe2c(%rip),%rsi
call   1060 <strcat@plt>
jmp    125e <libconfig_format_double+0x11e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jbe    1259 <libconfig_format_double+0x119>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jne    123e <libconfig_format_double+0xfe>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
jmp    1243 <libconfig_format_double+0x103>
jmp    1259 <libconfig_format_double+0x119>
jmp    1248 <libconfig_format_double+0x108>
mov    -0x38(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
jmp    120d <libconfig_format_double+0xcd>
jmp    125e <libconfig_format_double+0x11e>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libconfig_format_double>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %edi,%eax
test   %esi,%esi
lea    0xeb3(%rip),%rsi
lea    0xea7(%rip),%rdx
cmove  %rsi,%rdx
lea    -0x3(%rcx),%rsi
mov    %r14,%rdi
mov    %eax,%ecx
call   1050 <snprintf@plt>
mov    %r14,%rdi
mov    $0x65,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1185 <libconfig_format_double+0x45>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rdi
mov    $0x2e,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11d3 <libconfig_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    117d <libconfig_format_double+0x3d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmpb   $0x30,(%rax)
jne    117d <libconfig_format_double+0x3d>
movb   $0x0,(%rax)
add    $0xffffffffffffffff,%rax
cmp    %rbx,%rax
ja     11c0 <libconfig_format_double+0x80>
jmp    117d <libconfig_format_double+0x3d>
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 @libconfig_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*
%13 = 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
%14 = load i32, i32* %8
%15 = icmp ne i32 %14, 0
%16 = zext i1 %15 to i64
%17 = select i1 %15, 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)
store i8* %17, i8** %11
%18 = load i8*, i8** %9
%19 = load i64, i64* %10
%20 = sub i64 %19, 3
%21 = load i8*, i8** %11
%22 = load i32, i32* %7
%23 = load double, double* %6
%24 = call i32 @snprintf(i8* %18, i64 %20, i8* %21, i32 %22, double %23)
%25 = load i8*, i8** %9
%26 = call i8* @strchr(i8* %25, i8 signext 101)
store i8* %26, i8** %12
%27 = load i8*, i8** %12
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %5
br label %63
30: ; preds = %5
%31 = load i8*, i8** %9
%32 = call i8* @strchr(i8* %31, i8 signext 46)
store i8* %32, i8** %12
%33 = load i8*, i8** %12
%34 = icmp ne i8* %33, null
br i1 %34, label %38, label %35
35: ; preds = %30
%36 = load i8*, i8** %9
%37 = call i32 @strcat(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %63
38: ; preds = %30
%39 = load i8*, i8** %9
%40 = load i8*, i8** %9
%41 = call i32 @strlen(i8* %40)
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %39, i64 %42
%44 = getelementptr inbounds i8, i8* %43, i64 -1
store i8* %44, i8** %13
br label %45
45: ; preds = %59, %38
%46 = load i8*, i8** %13
%47 = load i8*, i8** %12
%48 = getelementptr inbounds i8, i8* %47, i64 1
%49 = icmp ugt i8* %46, %48
br i1 %49, label %50, label %62
50: ; preds = %45
%51 = load i8*, i8** %13
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 48
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = load i8*, i8** %13
store i8 0, i8* %56
br label %58
57: ; preds = %50
br label %62
58: ; preds = %55
br label %59
59: ; preds = %58
%60 = load i8*, i8** %13
%61 = getelementptr inbounds i8, i8* %60, i32 -1
store i8* %61, i8** %13
br label %45
62: ; preds = %57, %45
br label %63
63: ; preds = %29, %62, %35
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 @libconfig_format_double(double %0, i32 %1, i32 %2, i8* %3, i64 %4) {
%6 = icmp eq i32 %2, 0
%7 = select i1 %6, 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)
%8 = add i64 %4, -3
%9 = tail call i32 @snprintf(i8* %3, i64 %8, i8* %7, 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
}