#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 */
size_t abs(long);
int /*<<< orphan*/ strrev(char *);
char *imms_itoa(long value, char *result, int base) {
  char *sz = result;
  long quotient = value;
  if (base < 2 || base > 16) {
    *result = 0;
    return result;
  }
  do {
    *sz++ = "0123456789abcdef"[abs(quotient % base)];
    quotient /= base;
  } while (quotient);
  if (base == 16) {
    *sz++ = 'x';
    *sz++ = '0';
  }
  if (value < 0 && base == 10)
    *sz++ = '-';
  *sz = 0;
  strrev(result);
  return result;
}


x86 assembly (clang, -O0):
<imms_itoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jl     1157 <imms_itoa+0x37>
cmpl   $0x10,-0x1c(%rbp)
jle    116b <imms_itoa+0x4b>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    122c <imms_itoa+0x10c>
jmp    1170 <imms_itoa+0x50>
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cqto
idiv   %rcx
mov    %rdx,%rdi
call   1030 <abs@plt>
mov    %rax,%rcx
lea    0xe71(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
movslq -0x1c(%rbp),%rcx
mov    -0x30(%rbp),%rax
cqto
idiv   %rcx
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1170 <imms_itoa+0x50>
cmpl   $0x10,-0x1c(%rbp)
jne    11ed <imms_itoa+0xcd>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x78,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x30,(%rax)
cmpq   $0x0,-0x10(%rbp)
jge    1214 <imms_itoa+0xf4>
cmpl   $0xa,-0x1c(%rbp)
jne    1214 <imms_itoa+0xf4>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x2d,(%rax)
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   1040 <strrev@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<imms_itoa>:
push   %r14
push   %rbx
push   %rax
mov    %edx,%r8d
mov    %rsi,%r14
lea    -0x11(%r8),%eax
cmp    $0xfffffff1,%eax
jae    1129 <imms_itoa+0x19>
movb   $0x0,(%r14)
jmp    119c <imms_itoa+0x8c>
mov    %r8d,%ebx
lea    0xecd(%rip),%r9
mov    %r14,%rsi
mov    %rdi,%rax
jmp    115f <imms_itoa+0x4f>
nopl   0x0(%rax,%rax,1)
cqto
idiv   %rbx
mov    %rdx,%rcx
neg    %rcx
cmovs  %rdx,%rcx
movzbl (%rcx,%r9,1),%ecx
mov    %cl,(%rsi)
add    $0x1,%rsi
test   %rax,%rax
je     116e <imms_itoa+0x5e>
mov    %rax,%rdx
shr    $0x20,%rdx
jne    1140 <imms_itoa+0x30>
xor    %edx,%edx
div    %ebx
jmp    1145 <imms_itoa+0x35>
cmp    $0x10,%r8d
jne    117f <imms_itoa+0x6f>
movw   $0x3078,(%rsi)
add    $0x2,%rsi
jmp    1191 <imms_itoa+0x81>
test   %rdi,%rdi
jns    1191 <imms_itoa+0x81>
cmp    $0xa,%r8d
jne    1191 <imms_itoa+0x81>
movb   $0x2d,(%rsi)
add    $0x1,%rsi
movb   $0x0,(%rsi)
mov    %r14,%rdi
call   1030 <strrev@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [17 x i8] c"0123456789abcdef\00"
define i8* @imms_itoa(i64 %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
store i64 %0, i64* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
store i8* %10, i8** %8
%11 = load i64, i64* %5
store i64 %11, i64* %9
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 2
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i32, i32* %7
%16 = icmp sgt i32 %15, 16
br i1 %16, label %17, label %20
17: ; preds = %14, %3
%18 = load i8*, i8** %6
store i8 0, i8* %18
%19 = load i8*, i8** %6
store i8* %19, i8** %4
br label %60
20: ; preds = %14
br label %21
21: ; preds = %35, %20
%22 = load i64, i64* %9
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = srem i64 %22, %24
%26 = call i64 @abs(i64 %25)
%27 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = load i8*, i8** %8
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %8
store i8 %28, i8* %29
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = load i64, i64* %9
%34 = sdiv i64 %33, %32
store i64 %34, i64* %9
br label %35
35: ; preds = %21
%36 = load i64, i64* %9
%37 = icmp ne i64 %36, 0
br i1 %37, label %21, label %38
38: ; preds = %35
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, 16
br i1 %40, label %41, label %46
41: ; preds = %38
%42 = load i8*, i8** %8
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %8
store i8 120, i8* %42
%44 = load i8*, i8** %8
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %8
store i8 48, i8* %44
br label %46
46: ; preds = %41, %38
%47 = load i64, i64* %5
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %55
49: ; preds = %46
%50 = load i32, i32* %7
%51 = icmp eq i32 %50, 10
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = load i8*, i8** %8
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %8
store i8 45, i8* %53
br label %55
55: ; preds = %52, %49, %46
%56 = load i8*, i8** %8
store i8 0, i8* %56
%57 = load i8*, i8** %6
%58 = call i32 @strrev(i8* %57)
%59 = load i8*, i8** %6
store i8* %59, i8** %4
br label %60
60: ; preds = %55, %17
%61 = load i8*, i8** %4
ret i8* %61
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789abcdef\00"
define i8* @imms_itoa(i64 %0, i8* returned %1, i32 %2) {
%4 = add i32 %2, -17
%5 = icmp ult i32 %4, -15
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = zext i32 %2 to i64
br label %9
8: ; preds = %3
store i8 0, i8* %1
br label %33
9: ; preds = %6, %9
%10 = phi i8* [ %16, %9 ], [ %1, %6 ]
%11 = phi i64 [ %17, %9 ], [ %0, %6 ]
%12 = srem i64 %11, %7
%13 = tail call i64 @llvm.abs.i64(i64 %12, i1 true)
%14 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %13
%15 = load i8, i8* %14
%16 = getelementptr inbounds i8, i8* %10, i64 1
store i8 %15, i8* %10
%17 = sdiv i64 %11, %7
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %9
19: ; preds = %9
%20 = icmp eq i32 %2, 16
br i1 %20, label %21, label %24
21: ; preds = %19
%22 = getelementptr inbounds i8, i8* %10, i64 2
store i8 120, i8* %16
%23 = getelementptr inbounds i8, i8* %10, i64 3
store i8 48, i8* %22
br label %30
24: ; preds = %19
%25 = icmp slt i64 %0, 0
%26 = icmp eq i32 %2, 10
%27 = and i1 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = getelementptr inbounds i8, i8* %10, i64 2
store i8 45, i8* %16
br label %30
30: ; preds = %21, %28, %24
%31 = phi i8* [ %29, %28 ], [ %16, %24 ], [ %23, %21 ]
store i8 0, i8* %31
%32 = tail call i32 @strrev(i8* %1)
br label %33
33: ; preds = %30, %8
ret i8* %1
}