#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 calc_sanitize_decimal_number(char *);
int /*<<< orphan*/ isdigit(unsigned char);
int /*<<< orphan*/ snprintf(char *, int, char *, double);
int strlen(char *);
void calc_format_result(double value, char *result, int max_size) {
  int i, has_decimal;
  snprintf(result, max_size, "%.10f", (value == -0.0) ? 0.0 : value);
  has_decimal = calc_sanitize_decimal_number(result);
  i = strlen(result) - 1;
  while (i >= 0) {
    if (!isdigit((unsigned char)result[i]) && (result[i] != '-')) {
      result[i] = '\0';
      break;
    }
    if (has_decimal && (result[i] == '0')) {
      result[i] = '\0';
      i--;
    } else
      break;
  }
}


x86 assembly (clang, -O0):
<calc_format_result>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  0xe91(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    118c <calc_format_result+0x4c>
jp     118c <calc_format_result+0x4c>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    1196 <calc_format_result+0x56>
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x28(%rbp),%rdi
movsd  -0x30(%rbp),%xmm0
lea    0xe5f(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <calc_sanitize_decimal_number@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1256 <calc_format_result+0x116>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    1213 <calc_format_result+0xd3>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2d,%eax
je     1213 <calc_format_result+0xd3>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1256 <calc_format_result+0x116>
cmpl   $0x0,-0x1c(%rbp)
je     124c <calc_format_result+0x10c>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x30,%eax
jne    124c <calc_format_result+0x10c>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1251 <calc_format_result+0x111>
jmp    1256 <calc_format_result+0x116>
jmp    11c9 <calc_format_result+0x89>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_format_result>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
xorpd  %xmm1,%xmm1
cmpeqsd %xmm0,%xmm1
andnpd %xmm0,%xmm1
lea    0xea2(%rip),%rdx
movapd %xmm1,%xmm0
call   1050 <snprintf@plt>
mov    %r15,%rdi
call   1060 <calc_sanitize_decimal_number@plt>
mov    %eax,%r14d
mov    %r15,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    11ca <calc_format_result+0x8a>
mov    %eax,%ebp
add    $0x1,%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    -0x2(%rbp),%ebx
movzbl (%r15,%rbx,1),%edi
call   1030 <isdigit@plt>
test   %eax,%eax
jne    11a8 <calc_format_result+0x68>
cmpb   $0x2d,(%r15,%rbx,1)
jne    11c5 <calc_format_result+0x85>
test   %r14d,%r14d
je     11ca <calc_format_result+0x8a>
cmpb   $0x30,(%r15,%rbx,1)
jne    11ca <calc_format_result+0x8a>
movb   $0x0,(%r15,%rbx,1)
add    $0xffffffffffffffff,%rbp
cmp    $0x1,%rbp
ja     1190 <calc_format_result+0x50>
jmp    11ca <calc_format_result+0x8a>
movb   $0x0,(%r15,%rbx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%.10f\00"
define void @calc_format_result(double %0, i8* %1, i32 %2) {
%4 = alloca double
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store double %0, double* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %5
%10 = load i32, i32* %6
%11 = load double, double* %4
%12 = fcmp oeq double %11, -0.000000e+00
br i1 %12, label %13, label %14
13: ; preds = %3
br label %16
14: ; preds = %3
%15 = load double, double* %4
br label %16
16: ; preds = %14, %13
%17 = phi double [ 0.000000e+00, %13 ], [ %15, %14 ]
%18 = call i32 @snprintf(i8* %9, i32 %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double %17)
%19 = load i8*, i8** %5
%20 = call i32 @calc_sanitize_decimal_number(i8* %19)
store i32 %20, i32* %8
%21 = load i8*, i8** %5
%22 = call i32 @strlen(i8* %21)
%23 = sub nsw i32 %22, 1
store i32 %23, i32* %7
br label %24
24: ; preds = %67, %16
%25 = load i32, i32* %7
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %68
27: ; preds = %24
%28 = load i8*, i8** %5
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8, i8* %31
%33 = call i32 @isdigit(i8 zeroext %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %48, label %35
35: ; preds = %27
%36 = load i8*, i8** %5
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp ne i32 %41, 45
br i1 %42, label %43, label %48
43: ; preds = %35
%44 = load i8*, i8** %5
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
br label %68
48: ; preds = %35, %27
%49 = load i32, i32* %8
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %66
51: ; preds = %48
%52 = load i8*, i8** %5
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp eq i32 %57, 48
br i1 %58, label %59, label %66
59: ; preds = %51
%60 = load i8*, i8** %5
%61 = load i32, i32* %7
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8 0, i8* %63
%64 = load i32, i32* %7
%65 = add nsw i32 %64, -1
store i32 %65, i32* %7
br label %67
66: ; preds = %51, %48
br label %68
67: ; preds = %59
br label %24
68: ; preds = %66, %43, %24
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"%.10f\00"
define void @calc_format_result(double %0, i8* %1, i32 %2) {
%4 = fcmp oeq double %0, 0.000000e+00
%5 = select i1 %4, double 0.000000e+00, double %0
%6 = tail call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double %5)
%7 = tail call i32 @calc_sanitize_decimal_number(i8* %1)
%8 = tail call i32 @strlen(i8* %1)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %31
10: ; preds = %3
%11 = icmp eq i32 %7, 0
%12 = zext i32 %8 to i64
br label %13
13: ; preds = %10, %29
%14 = phi i64 [ %12, %10 ], [ %15, %29 ]
%15 = add nsw i64 %14, -1
%16 = and i64 %15, 4294967295
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = load i8, i8* %17
%19 = tail call i32 @isdigit(i8 zeroext %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %13
%22 = load i8, i8* %17
%23 = icmp eq i8 %22, 45
br i1 %23, label %25, label %24
24: ; preds = %21
store i8 0, i8* %17
br label %31
25: ; preds = %21, %13
br i1 %11, label %31, label %26
26: ; preds = %25
%27 = load i8, i8* %17
%28 = icmp eq i8 %27, 48
br i1 %28, label %29, label %31
29: ; preds = %26
store i8 0, i8* %17
%30 = icmp ugt i64 %14, 1
br i1 %30, label %13, label %31
31: ; preds = %29, %26, %25, %3, %24
ret void
}