#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 /*<<< orphan*/ sprintf(char *, char *, int);
char *fb_hFloat2Str(double val, char *buffer, int digits, int mask) {
  int len, maxlen;
  char *p;
  char fmtstr[16], *fstr;
  if (mask & 0x00000001)
    p = &buffer[1];
  else
    p = buffer;
  switch (digits) {
  case 7:
    fstr = (char *)&"%.7g";
    break;
  case 16:
    fstr = (char *)&"%.16g";
    break;
  default:
    sprintf(fmtstr, "%%.%dg", digits);
    fstr = &fmtstr[0];
  }
  maxlen = 1 + digits + 6 + 1;
  len = snprintf(p, maxlen, fstr, val);
  if (len <= 0 || len >= maxlen) {
    buffer[0] = '\0';
    return 0;
  }
  if (len > 0) {
    if (len > 0)
      if (p[len - 1] == '.')
        p[len - 1] = '\0';
  }
  if ((mask & 0x00000001) > 0) {
    if (p[0] != '-') {
      buffer[0] = ' ';
      return &buffer[0];
    } else
      return p;
  } else
    return p;
}


x86 assembly (clang, -O0):
<fb_hFloat2Str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
mov    %edx,-0x20(%rbp)
mov    -0x20(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1157 <fb_hFloat2Str+0x37>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    115f <fb_hFloat2Str+0x3f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
sub    $0x7,%eax
je     1184 <fb_hFloat2Str+0x64>
jmp    1173 <fb_hFloat2Str+0x53>
mov    -0x4c(%rbp),%eax
sub    $0x10,%eax
je     1194 <fb_hFloat2Str+0x74>
jmp    11a4 <fb_hFloat2Str+0x84>
lea    0xe75(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    11bf <fb_hFloat2Str+0x9f>
lea    0xe6a(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    11bf <fb_hFloat2Str+0x9f>
lea    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xe59(%rip),%rsi
call   1040 <sprintf@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
add    $0x6,%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x48(%rbp),%rdx
movsd  -0x10(%rbp),%xmm0
call   1030 <snprintf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    11fc <fb_hFloat2Str+0xdc>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jl     1210 <fb_hFloat2Str+0xf0>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12a1 <fb_hFloat2Str+0x181>
cmpl   $0x0,-0x24(%rbp)
jle    1259 <fb_hFloat2Str+0x139>
cmpl   $0x0,-0x24(%rbp)
jle    1254 <fb_hFloat2Str+0x134>
mov    -0x30(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
jne    124f <fb_hFloat2Str+0x12f>
mov    -0x30(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1254 <fb_hFloat2Str+0x134>
jmp    1259 <fb_hFloat2Str+0x139>
mov    -0x20(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jle    1299 <fb_hFloat2Str+0x179>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
je     128c <fb_hFloat2Str+0x16c>
mov    -0x18(%rbp),%rax
movb   $0x20,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12a1 <fb_hFloat2Str+0x181>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12a1 <fb_hFloat2Str+0x181>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fb_hFloat2Str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%r14
lea    0x1(%rdi),%r12
test   $0x1,%r15b
mov    %r12,%rbx
cmove  %rdi,%rbx
cmp    $0x7,%esi
je     1158 <fb_hFloat2Str+0x38>
cmp    $0x10,%ebp
jne    1161 <fb_hFloat2Str+0x41>
lea    0xeaf(%rip),%r13
jmp    1183 <fb_hFloat2Str+0x63>
lea    0xea1(%rip),%r13
jmp    1183 <fb_hFloat2Str+0x63>
lea    0x10(%rsp),%r13
lea    0xe9e(%rip),%rsi
mov    %r13,%rdi
mov    %ebp,%edx
movsd  %xmm0,0x8(%rsp)
call   1040 <sprintf@plt>
movsd  0x8(%rsp),%xmm0
add    $0x8,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r13,%rdx
call   1030 <snprintf@plt>
xor    %ecx,%ecx
mov    $0x0,%edx
test   %eax,%eax
jle    11c4 <fb_hFloat2Str+0xa4>
cmp    %ebp,%eax
jge    11c4 <fb_hFloat2Str+0xa4>
add    $0xffffffff,%eax
cmpb   $0x2e,(%rbx,%rax,1)
jne    11af <fb_hFloat2Str+0x8f>
movb   $0x0,(%rbx,%rax,1)
test   $0x1,%r15b
jne    11ba <fb_hFloat2Str+0x9a>
mov    %r14,%r12
jmp    11ca <fb_hFloat2Str+0xaa>
mov    $0x20,%dl
cmpb   $0x2d,(%rbx)
mov    %r14,%rcx
je     11ca <fb_hFloat2Str+0xaa>
mov    %dl,(%r14)
mov    %rcx,%r12
mov    %r12,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%.7g\00"
@.str.1 = [6 x i8] c"%.16g\00"
@.str.2 = [7 x i8] c"%%.%dg\00"
define i8* @fb_hFloat2Str(double %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca double
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca [16 x i8]
%14 = alloca i8*
store double %0, double* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%15 = load i32, i32* %9
%16 = and i32 %15, 1
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %4
%19 = load i8*, i8** %7
%20 = getelementptr inbounds i8, i8* %19, i64 1
store i8* %20, i8** %12
br label %23
21: ; preds = %4
%22 = load i8*, i8** %7
store i8* %22, i8** %12
br label %23
23: ; preds = %21, %18
%24 = load i32, i32* %8
switch i32 %24, label %27 [
i32 7, label %25
i32 16, label %26
]
25: ; preds = %23
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8** %14
br label %32
26: ; preds = %23
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8** %14
br label %32
27: ; preds = %23
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %13, i64 0, i64 0
%29 = load i32, i32* %8
%30 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds [16 x i8], [16 x i8]* %13, i64 0, i64 0
store i8* %31, i8** %14
br label %32
32: ; preds = %27, %26, %25
%33 = load i32, i32* %8
%34 = add nsw i32 1, %33
%35 = add nsw i32 %34, 6
%36 = add nsw i32 %35, 1
store i32 %36, i32* %11
%37 = load i8*, i8** %12
%38 = load i32, i32* %11
%39 = load i8*, i8** %14
%40 = load double, double* %6
%41 = call i32 @snprintf(i8* %37, i32 %38, i8* %39, double %40)
store i32 %41, i32* %10
%42 = load i32, i32* %10
%43 = icmp sle i32 %42, 0
br i1 %43, label %48, label %44
44: ; preds = %32
%45 = load i32, i32* %10
%46 = load i32, i32* %11
%47 = icmp sge i32 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %44, %32
%49 = load i8*, i8** %7
%50 = getelementptr inbounds i8, i8* %49, i64 0
store i8 0, i8* %50
store i8* null, i8** %5
br label %93
51: ; preds = %44
%52 = load i32, i32* %10
%53 = icmp sgt i32 %52, 0
br i1 %53, label %54, label %74
54: ; preds = %51
%55 = load i32, i32* %10
%56 = icmp sgt i32 %55, 0
br i1 %56, label %57, label %73
57: ; preds = %54
%58 = load i8*, i8** %12
%59 = load i32, i32* %10
%60 = sub nsw i32 %59, 1
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %58, i64 %61
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = icmp eq i32 %64, 46
br i1 %65, label %66, label %72
66: ; preds = %57
%67 = load i8*, i8** %12
%68 = load i32, i32* %10
%69 = sub nsw i32 %68, 1
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %67, i64 %70
store i8 0, i8* %71
br label %72
72: ; preds = %66, %57
br label %73
73: ; preds = %72, %54
br label %74
74: ; preds = %73, %51
%75 = load i32, i32* %9
%76 = and i32 %75, 1
%77 = icmp sgt i32 %76, 0
br i1 %77, label %78, label %91
78: ; preds = %74
%79 = load i8*, i8** %12
%80 = getelementptr inbounds i8, i8* %79, i64 0
%81 = load i8, i8* %80
%82 = sext i8 %81 to i32
%83 = icmp ne i32 %82, 45
br i1 %83, label %84, label %89
84: ; preds = %78
%85 = load i8*, i8** %7
%86 = getelementptr inbounds i8, i8* %85, i64 0
store i8 32, i8* %86
%87 = load i8*, i8** %7
%88 = getelementptr inbounds i8, i8* %87, i64 0
store i8* %88, i8** %5
br label %93
89: ; preds = %78
%90 = load i8*, i8** %12
store i8* %90, i8** %5
br label %93
91: ; preds = %74
%92 = load i8*, i8** %12
store i8* %92, i8** %5
br label %93
93: ; preds = %91, %89, %84, %48
%94 = load i8*, i8** %5
ret i8* %94
}

LLVM IR (-O2):
@.str = [5 x i8] c"%.7g\00"
@.str.1 = [6 x i8] c"%.16g\00"
@.str.2 = [7 x i8] c"%%.%dg\00"
define i8* @fb_hFloat2Str(double %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca [16 x i8]
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%7 = and i32 %3, 1
%8 = icmp eq i32 %7, 0
%9 = getelementptr inbounds i8, i8* %1, i64 1
%10 = select i1 %8, i8* %1, i8* %9
switch i32 %2, label %12 [
i32 7, label %14
i32 16, label %11
]
11: ; preds = %4
br label %14
12: ; preds = %4
%13 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %2)
br label %14
14: ; preds = %4, %12, %11
%15 = phi i8* [ %6, %12 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %11 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %4 ]
%16 = add nsw i32 %2, 8
%17 = call i32 @snprintf(i8* %10, i32 %16, i8* %15, double %0)
%18 = icmp sgt i32 %17, 0
%19 = icmp slt i32 %17, %16
%20 = and i1 %18, %19
br i1 %20, label %21, label %32
21: ; preds = %14
%22 = add nsw i32 %17, -1
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %10, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 46
br i1 %26, label %27, label %28
27: ; preds = %21
store i8 0, i8* %24
br label %28
28: ; preds = %27, %21
br i1 %8, label %35, label %29
29: ; preds = %28
%30 = load i8, i8* %10
%31 = icmp eq i8 %30, 45
br i1 %31, label %35, label %32
32: ; preds = %29, %14
%33 = phi i8 [ 0, %14 ], [ 32, %29 ]
%34 = phi i8* [ null, %14 ], [ %1, %29 ]
store i8 %33, i8* %1
br label %35
35: ; preds = %32, %28, %29
%36 = phi i8* [ %9, %29 ], [ %1, %28 ], [ %34, %32 ]
ret i8* %36
}