#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*/ HexChar(int /*<<< orphan*/);
int /*<<< orphan*/ abs(int);
int /*<<< orphan*/ strreverse(char *, char *);
char *itoa(int value, char *s, int base) {
  if (base < 2 || base > 16) {
    *s = '\0';
    return s;
  }
  int sign = value;
  char *ptr = s;
  do {
    *ptr++ = HexChar(abs(value % base));
    value /= base;
  } while (value);
  if (sign < 0)
    *ptr++ = '-';
  *ptr-- = '\0';
  strreverse(s, ptr);
  return s;
}


x86 assembly (clang, -O0):
<itoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jl     1156 <itoa+0x26>
cmpl   $0x10,-0x1c(%rbp)
jle    116a <itoa+0x3a>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f9 <itoa+0xc9>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
cltd
idivl  -0x1c(%rbp)
mov    %edx,%edi
call   1030 <abs@plt>
mov    %eax,%edi
call   1040 <HexChar@plt>
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
mov    -0x1c(%rbp),%ecx
mov    -0xc(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1178 <itoa+0x48>
cmpl   $0x0,-0x20(%rbp)
jge    11d2 <itoa+0xa2>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x2d,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <strreverse@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itoa>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %rsi,%r14
lea    -0x11(%r15),%eax
cmp    $0xfffffff1,%eax
jae    113d <itoa+0x1d>
movb   $0x0,(%r14)
jmp    118c <itoa+0x6c>
mov    %edi,%r12d
mov    %edi,%ebp
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%eax
cltd
idiv   %r15d
mov    %edx,%edi
neg    %edi
cmovs  %edx,%edi
mov    %eax,%ebp
call   1030 <HexChar@plt>
mov    %al,(%rbx)
add    $0x1,%rbx
test   %ebp,%ebp
jne    1150 <itoa+0x30>
test   %r12d,%r12d
jns    117a <itoa+0x5a>
movb   $0x2d,(%rbx)
add    $0x1,%rbx
movb   $0x0,(%rbx)
add    $0xffffffffffffffff,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strreverse@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @itoa(i32 %0, i8* returned %1, i32 %2) {
%4 = add i32 %2, -17
%5 = icmp ult i32 %4, -15
br i1 %5, label %6, label %7
6: ; preds = %3
store i8 0, i8* %1
br label %25
7: ; preds = %3, %7
%8 = phi i32 [ %15, %7 ], [ %0, %3 ]
%9 = phi i8* [ %14, %7 ], [ %1, %3 ]
%10 = srem i32 %8, %2
%11 = tail call i32 @llvm.abs.i32(i32 %10, i1 true)
%12 = tail call i32 @HexChar(i32 %11)
%13 = trunc i32 %12 to i8
%14 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %13, i8* %9
%15 = sdiv i32 %8, %2
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %7
17: ; preds = %7
%18 = icmp slt i32 %0, 0
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = getelementptr inbounds i8, i8* %9, i64 2
store i8 45, i8* %14
br label %21
21: ; preds = %19, %17
%22 = phi i8* [ %20, %19 ], [ %14, %17 ]
%23 = getelementptr inbounds i8, i8* %22, i64 -1
store i8 0, i8* %22
%24 = tail call i32 @strreverse(i8* %1, i8* %23)
br label %25
25: ; preds = %21, %6
ret i8* %1
}