#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 /*<<< orphan*/ strrev(char *, unsigned int);
int fast_itoa(int value, char *pBuffer) {
  char *pTmp = pBuffer;
  unsigned int digitCount = 0;
  if (value == 0) {
    *pTmp = '0';
    return 1;
  }
  if (value < 0) {
    value = -value;
    *pTmp++ = '-';
    ++digitCount;
  }
  while (value > 0) {
    *pTmp++ = (value % 10) + '0';
    value /= 10;
    ++digitCount;
  }
  *pTmp = '\0';
  strrev(pBuffer, digitCount);
  return digitCount;
}


x86 assembly (clang, -O0):
<fast_itoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    114b <fast_itoa+0x3b>
mov    -0x18(%rbp),%rax
movb   $0x30,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    11dd <fast_itoa+0xcd>
cmpl   $0x0,-0x8(%rbp)
jge    1178 <fast_itoa+0x68>
xor    %eax,%eax
sub    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x2d,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117d <fast_itoa+0x6d>
cmpl   $0x0,-0x8(%rbp)
jle    11c4 <fast_itoa+0xb4>
mov    -0x8(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%edx
mov    %dl,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117d <fast_itoa+0x6d>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <strrev@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fast_itoa>:
push   %rbp
push   %rbx
push   %rax
test   %edi,%edi
je     1122 <fast_itoa+0x12>
xor    %ebp,%ebp
test   %edi,%edi
js     112c <fast_itoa+0x1c>
mov    %rsi,%rax
jmp    113a <fast_itoa+0x2a>
movb   $0x30,(%rsi)
mov    $0x1,%ebp
jmp    1174 <fast_itoa+0x64>
neg    %edi
lea    0x1(%rsi),%rax
movb   $0x2d,(%rsi)
mov    $0x1,%ebp
mov    $0xcccccccd,%r8d
mov    %edi,%edx
imul   %r8,%rdx
shr    $0x23,%rdx
lea    (%rdx,%rdx,1),%ecx
lea    (%rcx,%rcx,4),%ecx
mov    %edi,%ebx
sub    %ecx,%ebx
or     $0x30,%bl
mov    %bl,(%rax)
add    $0x1,%rax
add    $0x1,%ebp
cmp    $0x9,%edi
mov    %edx,%edi
ja     1140 <fast_itoa+0x30>
movb   $0x0,(%rax)
mov    %rsi,%rdi
mov    %ebp,%esi
call   1030 <strrev@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @fast_itoa(i32 %0, i8* %1) {
%3 = icmp eq i32 %0, 0
br i1 %3, label %4, label %5
4: ; preds = %2
store i8 48, i8* %1
br label %27
5: ; preds = %2
%6 = icmp slt i32 %0, 0
br i1 %6, label %7, label %10
7: ; preds = %5
%8 = sub nsw i32 0, %0
%9 = getelementptr inbounds i8, i8* %1, i64 1
store i8 45, i8* %1
br label %10
10: ; preds = %5, %7
%11 = phi i32 [ 0, %5 ], [ 1, %7 ]
%12 = phi i8* [ %1, %5 ], [ %9, %7 ]
%13 = phi i32 [ %0, %5 ], [ %8, %7 ]
br label %14
14: ; preds = %10, %14
%15 = phi i32 [ %23, %14 ], [ %11, %10 ]
%16 = phi i8* [ %21, %14 ], [ %12, %10 ]
%17 = phi i32 [ %22, %14 ], [ %13, %10 ]
%18 = urem i32 %17, 10
%19 = trunc i32 %18 to i8
%20 = or i8 %19, 48
%21 = getelementptr inbounds i8, i8* %16, i64 1
store i8 %20, i8* %16
%22 = udiv i32 %17, 10
%23 = add i32 %15, 1
%24 = icmp ult i32 %17, 10
br i1 %24, label %25, label %14
25: ; preds = %14
store i8 0, i8* %21
%26 = tail call i32 @strrev(i8* %1, i32 %23)
br label %27
27: ; preds = %25, %4
%28 = phi i32 [ 1, %4 ], [ %23, %25 ]
ret i32 %28
}