#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*/ uint32_t;
typedef scalar_t__ int32_t;

/* Variables and functions */
scalar_t__ WriteDecUint32(int /*<<< orphan*/, scalar_t__, char *);
int /*<<< orphan*/ assert(int);
int32_t WriteDecInt32(int32_t value, int32_t max_length, char *buf) {
  assert(max_length >= 0);
  assert(buf != NULL);
  int32_t length;
  if (value >= 0) {
    length = WriteDecUint32((uint32_t)value, max_length, buf);
  } else if (max_length) {
    buf[0] = '-';
    length = WriteDecUint32((uint32_t)(-value), max_length - 1, &buf[1]) + 1;
  } else {
    length = 0;
  }
  return length;
}


x86 assembly (clang, -O0):
<WriteDecInt32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpq   $0x0,-0x8(%rbp)
jl     117f <WriteDecInt32+0x5f>
mov    -0x8(%rbp),%rax
mov    %eax,%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <WriteDecUint32@plt>
mov    %rax,-0x20(%rbp)
jmp    11c8 <WriteDecInt32+0xa8>
cmpq   $0x0,-0x10(%rbp)
je     11bb <WriteDecInt32+0x9b>
mov    -0x18(%rbp),%rax
movb   $0x2d,(%rax)
xor    %eax,%eax
sub    -0x8(%rbp),%rax
mov    %eax,%edi
mov    -0x10(%rbp),%rsi
sub    $0x1,%rsi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
call   1040 <WriteDecUint32@plt>
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11c3 <WriteDecInt32+0xa3>
movq   $0x0,-0x20(%rbp)
jmp    11c8 <WriteDecInt32+0xa8>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteDecInt32>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsi,%rdi
shr    $0x3f,%rdi
xor    $0x1,%edi
call   1030 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1030 <assert@plt>
test   %r15,%r15
js     1163 <WriteDecInt32+0x43>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <WriteDecUint32@plt>
test   %rbx,%rbx
je     118b <WriteDecInt32+0x6b>
movb   $0x2d,(%r14)
neg    %r15d
add    $0xffffffffffffffff,%rbx
add    $0x1,%r14
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <WriteDecUint32@plt>
add    $0x1,%rax
jmp    118d <WriteDecInt32+0x6d>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @WriteDecInt32(i64 %0, i64 %1, i8* %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i64 %0, i64* %4
store i64 %1, i64* %5
store i8* %2, i8** %6
%8 = load i64, i64* %5
%9 = icmp sge i64 %8, 0
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i64, i64* %4
%17 = icmp sge i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %3
%19 = load i64, i64* %4
%20 = trunc i64 %19 to i32
%21 = load i64, i64* %5
%22 = load i8*, i8** %6
%23 = call i64 @WriteDecUint32(i32 %20, i64 %21, i8* %22)
store i64 %23, i64* %7
br label %41
24: ; preds = %3
%25 = load i64, i64* %5
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %39
27: ; preds = %24
%28 = load i8*, i8** %6
%29 = getelementptr inbounds i8, i8* %28, i64 0
store i8 45, i8* %29
%30 = load i64, i64* %4
%31 = sub nsw i64 0, %30
%32 = trunc i64 %31 to i32
%33 = load i64, i64* %5
%34 = sub nsw i64 %33, 1
%35 = load i8*, i8** %6
%36 = getelementptr inbounds i8, i8* %35, i64 1
%37 = call i64 @WriteDecUint32(i32 %32, i64 %34, i8* %36)
%38 = add nsw i64 %37, 1
store i64 %38, i64* %7
br label %40
39: ; preds = %24
store i64 0, i64* %7
br label %40
40: ; preds = %39, %27
br label %41
41: ; preds = %40, %18
%42 = load i64, i64* %7
ret i64 %42
}

LLVM IR (-O2):
define i64 @WriteDecInt32(i64 %0, i64 %1, i8* %2) {
%4 = lshr i64 %1, 63
%5 = trunc i64 %4 to i32
%6 = xor i32 %5, 1
%7 = tail call i32 @assert(i32 %6)
%8 = icmp ne i8* %2, null
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = icmp sgt i64 %0, -1
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = trunc i64 %0 to i32
%14 = tail call i64 @WriteDecUint32(i32 %13, i64 %1, i8* %2)
br label %24
15: ; preds = %3
%16 = icmp eq i64 %1, 0
br i1 %16, label %24, label %17
17: ; preds = %15
store i8 45, i8* %2
%18 = trunc i64 %0 to i32
%19 = sub i32 0, %18
%20 = add nsw i64 %1, -1
%21 = getelementptr inbounds i8, i8* %2, i64 1
%22 = tail call i64 @WriteDecUint32(i32 %19, i64 %20, i8* %21)
%23 = add nsw i64 %22, 1
br label %24
24: ; preds = %15, %17, %12
%25 = phi i64 [ %14, %12 ], [ %23, %17 ], [ 0, %15 ]
ret i64 %25
}