#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 */
typedef int /*<<< orphan*/ tmp;

/* Variables and functions */
int snprintf(char *, int, char *, double);
int /*<<< orphan*/ strncpy(char *, char *, int);
int json_emit_double(char *buf, int buf_len, double value) {
  char tmp[20];
  int n = snprintf(tmp, sizeof(tmp), "%g", value);
  strncpy(buf, tmp, buf_len > 0 ? buf_len : 0);
  return n;
}


x86 assembly (clang, -O0):
<json_emit_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movsd  %xmm0,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
movsd  -0x18(%rbp),%xmm0
mov    $0x14,%esi
lea    0xeb7(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    1176 <json_emit_double+0x56>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4c(%rbp)
jmp    1180 <json_emit_double+0x60>
xor    %eax,%eax
mov    %eax,-0x4c(%rbp)
jmp    1180 <json_emit_double+0x60>
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdi
mov    -0x4c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x34(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<json_emit_double>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xeca(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x14,%esi
call   1040 <snprintf@plt>
mov    %eax,%ebp
xor    %edx,%edx
test   %ebx,%ebx
cmovns %ebx,%edx
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <strncpy@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%g\00"
define i32 @json_emit_double(i8* %0, i32 %1, double %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca double
%7 = alloca [20 x i8]
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store double %2, double* %6
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%10 = load double, double* %6
%11 = call i32 @snprintf(i8* %9, i32 20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), double %10)
store i32 %11, i32* %8
%12 = load i8*, i8** %4
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%14 = load i32, i32* %5
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = load i32, i32* %5
br label %19
18: ; preds = %3
br label %19
19: ; preds = %18, %16
%20 = phi i32 [ %17, %16 ], [ 0, %18 ]
%21 = call i32 @strncpy(i8* %12, i8* %13, i32 %20)
%22 = load i32, i32* %8
ret i32 %22
}

LLVM IR (-O2):
@.str = [3 x i8] c"%g\00"
define i32 @json_emit_double(i8* %0, i32 %1, double %2) {
%4 = alloca [20 x i8]
%5 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), double %2)
%7 = icmp sgt i32 %1, 0
%8 = select i1 %7, i32 %1, i32 0
%9 = call i32 @strncpy(i8* %0, i8* %5, i32 %8)
ret i32 %6
}