#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 *, long);
int /*<<< orphan*/ strncpy(char *, char *, int);
int json_emit_long(char *buf, int buf_len, long int value) {
  char tmp[20];
  int n = snprintf(tmp, sizeof(tmp), "%ld", value);
  strncpy(buf, tmp, buf_len > 0 ? buf_len : 0);
  return n;
}


x86 assembly (clang, -O0):
<json_emit_long>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x14,%esi
lea    0xeb9(%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    1174 <json_emit_long+0x54>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4c(%rbp)
jmp    117e <json_emit_long+0x5e>
xor    %eax,%eax
mov    %eax,-0x4c(%rbp)
jmp    117e <json_emit_long+0x5e>
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_long>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rcx
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xec7(%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 = [4 x i8] c"%ld\00"
define i32 @json_emit_long(i8* %0, i32 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca [20 x i8]
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 %2, i64* %6
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%10 = load i64, i64* %6
%11 = call i32 @snprintf(i8* %9, i32 20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %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 = [4 x i8] c"%ld\00"
define i32 @json_emit_long(i8* %0, i32 %1, i64 %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 ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %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
}