#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 */
struct string {
  int dummy;
};

/* Variables and functions */
int object_to_buf_write_char(struct string *, char);
int object_to_buf_write_string(struct string *, char *, int);
int object_to_buf_write_value(struct string *buf, signed long sr) {
  char temp[20] = {0}, *p;
  int ret;
  if (sr == 0)
    return object_to_buf_write_char(buf, '0');
  if (sr < 0) {
    ret = object_to_buf_write_char(buf, '-');
    if (ret < 0)
      return -1;
    sr = -sr;
  }
  p = temp + 20;
  while (sr != 0) {
    *(--p) = (char)('0' + sr % 10);
    sr /= 10;
  }
  return object_to_buf_write_string(buf, p, (int)(20 - (p - temp)));
}


x86 assembly (clang, -O0):
<object_to_buf_write_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1171 <object_to_buf_write_value+0x41>
mov    -0x10(%rbp),%rdi
mov    $0x30,%esi
call   1050 <object_to_buf_write_char@plt>
mov    %eax,-0x4(%rbp)
jmp    1226 <object_to_buf_write_value+0xf6>
cmpq   $0x0,-0x18(%rbp)
jge    11ad <object_to_buf_write_value+0x7d>
mov    -0x10(%rbp),%rdi
mov    $0x2d,%esi
call   1050 <object_to_buf_write_char@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11a3 <object_to_buf_write_value+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <object_to_buf_write_value+0xf6>
xor    %eax,%eax
sub    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x30(%rbp),%rax
add    $0x14,%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1201 <object_to_buf_write_value+0xd1>
mov    -0x18(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
add    $0x30,%rdx
mov    %dl,%cl
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x38(%rbp)
mov    %cl,-0x1(%rax)
mov    -0x18(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
mov    %rax,-0x18(%rbp)
jmp    11b9 <object_to_buf_write_value+0x89>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x38(%rbp),%rcx
lea    -0x30(%rbp),%rax
sub    %rax,%rcx
mov    $0x14,%eax
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <object_to_buf_write_string@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<object_to_buf_write_value>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movl   $0x0,0x10(%rsp)
test   %rsi,%rsi
je     11bc <object_to_buf_write_value+0x9c>
mov    %rsi,%rbx
jns    1157 <object_to_buf_write_value+0x37>
mov    %r14,%rdi
mov    $0x2d,%esi
call   1040 <object_to_buf_write_char@plt>
test   %eax,%eax
js     11d0 <object_to_buf_write_value+0xb0>
neg    %rbx
xor    %ecx,%ecx
lea    0x14(%rsp),%rsi
movabs $0x6666666666666667,%r8
66 66 66
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rax
imul   %r8
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x2,%rdx
add    %rax,%rdx
lea    (%rdx,%rdx,1),%eax
lea    (%rax,%rax,4),%eax
mov    %ebx,%edi
sub    %eax,%edi
add    $0x30,%dil
mov    %dil,-0x1(%rsi)
add    $0xffffffffffffffff,%rsi
add    $0x9,%rbx
add    $0x1,%ecx
cmp    $0x12,%rbx
mov    %rdx,%rbx
ja     1170 <object_to_buf_write_value+0x50>
mov    %r14,%rdi
mov    %ecx,%edx
call   1030 <object_to_buf_write_string@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rdi
mov    $0x30,%esi
add    $0x18,%rsp
pop    %rbx
pop    %r14
jmp    1040 <object_to_buf_write_char@plt>
mov    $0xffffffff,%eax
jmp    11b4 <object_to_buf_write_value+0x94>

LLVM IR (-O0):
%struct.string = type { i32 }
define i32 @object_to_buf_write_value(%struct.string* %0, i64 %1) {
%3 = alloca i32
%4 = alloca %struct.string*
%5 = alloca i64
%6 = alloca [20 x i8]
%7 = alloca i8*
%8 = alloca i32
store %struct.string* %0, %struct.string** %4
store i64 %1, i64* %5
%9 = bitcast [20 x i8]* %6 to i8*
%10 = load i64, i64* %5
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load %struct.string*, %struct.string** %4
%14 = call i32 @object_to_buf_write_char(%struct.string* %13, i8 signext 48)
store i32 %14, i32* %3
br label %53
15: ; preds = %2
%16 = load i64, i64* %5
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %15
%19 = load %struct.string*, %struct.string** %4
%20 = call i32 @object_to_buf_write_char(%struct.string* %19, i8 signext 45)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 -1, i32* %3
br label %53
24: ; preds = %18
%25 = load i64, i64* %5
%26 = sub nsw i64 0, %25
store i64 %26, i64* %5
br label %27
27: ; preds = %24, %15
%28 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%29 = getelementptr inbounds i8, i8* %28, i64 20
store i8* %29, i8** %7
br label %30
30: ; preds = %33, %27
%31 = load i64, i64* %5
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %42
33: ; preds = %30
%34 = load i64, i64* %5
%35 = srem i64 %34, 10
%36 = add nsw i64 48, %35
%37 = trunc i64 %36 to i8
%38 = load i8*, i8** %7
%39 = getelementptr inbounds i8, i8* %38, i32 -1
store i8* %39, i8** %7
store i8 %37, i8* %39
%40 = load i64, i64* %5
%41 = sdiv i64 %40, 10
store i64 %41, i64* %5
br label %30
42: ; preds = %30
%43 = load %struct.string*, %struct.string** %4
%44 = load i8*, i8** %7
%45 = load i8*, i8** %7
%46 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%47 = ptrtoint i8* %45 to i64
%48 = ptrtoint i8* %46 to i64
%49 = sub i64 %47, %48
%50 = sub nsw i64 20, %49
%51 = trunc i64 %50 to i32
%52 = call i32 @object_to_buf_write_string(%struct.string* %43, i8* %44, i32 %51)
store i32 %52, i32* %3
br label %53
53: ; preds = %42, %23, %12
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
%struct.string = type { i32 }
define i32 @object_to_buf_write_value(%struct.string* %0, i64 %1) {
%3 = alloca [20 x i8]
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%5 = icmp eq i64 %1, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @object_to_buf_write_char(%struct.string* %0, i8 signext 48)
br label %35
8: ; preds = %2
%9 = icmp slt i64 %1, 0
br i1 %9, label %10, label %15
10: ; preds = %8
%11 = tail call i32 @object_to_buf_write_char(%struct.string* %0, i8 signext 45)
%12 = icmp slt i32 %11, 0
br i1 %12, label %35, label %13
13: ; preds = %10
%14 = sub nsw i64 0, %1
br label %15
15: ; preds = %8, %13
%16 = phi i64 [ %14, %13 ], [ %1, %8 ]
%17 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 20
br label %18
18: ; preds = %15, %18
%19 = phi i8* [ %24, %18 ], [ %17, %15 ]
%20 = phi i64 [ %25, %18 ], [ %16, %15 ]
%21 = srem i64 %20, 10
%22 = trunc i64 %21 to i8
%23 = add nsw i8 %22, 48
%24 = getelementptr inbounds i8, i8* %19, i64 -1
store i8 %23, i8* %24
%25 = sdiv i64 %20, 10
%26 = add i64 %20, 9
%27 = icmp ult i64 %26, 19
br i1 %27, label %28, label %18
28: ; preds = %18
%29 = ptrtoint i8* %24 to i64
%30 = ptrtoint [20 x i8]* %3 to i64
%31 = sub i64 %30, %29
%32 = trunc i64 %31 to i32
%33 = add i32 %32, 20
%34 = call i32 @object_to_buf_write_string(%struct.string* %0, i8* %24, i32 %33)
br label %35
35: ; preds = %10, %28, %6
%36 = phi i32 [ %7, %6 ], [ %34, %28 ], [ -1, %10 ]
ret i32 %36
}