#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*/ printf(char *, char *);
char *itoa(int val, int base) {
  static char buf[32] = {0};
  int i = 30;
  for (; val && i; --i, val /= base)
    buf[i] = "0123456789abcdef"[val % base];
  printf("%s \n", buf);
  return &buf[i + 1];
}


x86 assembly (clang, -O0):
<itoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x1e,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x4(%rbp)
mov    %al,-0xd(%rbp)
je     113e <itoa+0x2e>
cmpl   $0x0,-0xc(%rbp)
setne  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    114e <itoa+0x3e>
jmp    118a <itoa+0x7a>
mov    -0x4(%rbp),%eax
cltd
idivl  -0x8(%rbp)
movslq %edx,%rcx
lea    0xea1(%rip),%rax
mov    (%rax,%rcx,1),%dl
movslq -0xc(%rbp),%rcx
lea    0x2ed3(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%ecx
mov    -0x4(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
jmp    1125 <itoa+0x15>
lea    0xe80(%rip),%rdi
lea    0x2ea8(%rip),%rsi
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rcx
lea    0x2e93(%rip),%rax
add    %rcx,%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itoa>:
push   %r14
push   %rbx
push   %rax
test   %edi,%edi
je     1152 <itoa+0x42>
mov    %edi,%eax
mov    $0x1f,%edi
lea    0xeda(%rip),%r8
lea    0x2f13(%rip),%rcx
nopl   (%rax)
cltd
idiv   %esi
movslq %edx,%rdx
movzbl (%rdx,%r8,1),%edx
mov    %dl,-0x1(%rdi,%rcx,1)
lea    -0x1(%rdi),%rbx
test   %eax,%eax
je     1157 <itoa+0x47>
cmp    $0x2,%rdi
mov    %rbx,%rdi
jne    1130 <itoa+0x20>
jmp    1157 <itoa+0x47>
mov    $0x1f,%ebx
lea    0xeb3(%rip),%rdi
lea    0x2edb(%rip),%r14
mov    %r14,%rsi
call   1030 <printf@plt>
add    %r14,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@itoa.buf = internal global [32 x i8] zeroinitializer
@.str = [17 x i8] c"0123456789abcdef\00"
@.str.1 = [5 x i8] c"%s \0A\00"
define i8* @itoa(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 30, i32* %5
br label %6
6: ; preds = %24, %2
%7 = load i32, i32* %3
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, 0
br label %12
12: ; preds = %9, %6
%13 = phi i1 [ false, %6 ], [ %11, %9 ]
br i1 %13, label %14, label %30
14: ; preds = %12
%15 = load i32, i32* %3
%16 = load i32, i32* %4
%17 = srem i32 %15, %16
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %18
%20 = load i8, i8* %19
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [32 x i8], [32 x i8]* @itoa.buf, i64 0, i64 %22
store i8 %20, i8* %23
br label %24
24: ; preds = %14
%25 = load i32, i32* %5
%26 = add nsw i32 %25, -1
store i32 %26, i32* %5
%27 = load i32, i32* %4
%28 = load i32, i32* %3
%29 = sdiv i32 %28, %27
store i32 %29, i32* %3
br label %6
30: ; preds = %12
%31 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @itoa.buf, i64 0, i64 0))
%32 = load i32, i32* %5
%33 = add nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [32 x i8], [32 x i8]* @itoa.buf, i64 0, i64 %34
ret i8* %35
}

LLVM IR (-O2):
@itoa.buf = internal global [32 x i8] zeroinitializer
@.str = [17 x i8] c"0123456789abcdef\00"
@.str.1 = [5 x i8] c"%s \0A\00"
define i8* @itoa(i32 %0, i32 %1) {
%3 = icmp eq i32 %0, 0
br i1 %3, label %20, label %4
4: ; preds = %2, %4
%5 = phi i64 [ %12, %4 ], [ 30, %2 ]
%6 = phi i32 [ %13, %4 ], [ %0, %2 ]
%7 = srem i32 %6, %1
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %8
%10 = load i8, i8* %9
%11 = getelementptr inbounds [32 x i8], [32 x i8]* @itoa.buf, i64 0, i64 %5
store i8 %10, i8* %11
%12 = add nsw i64 %5, -1
%13 = sdiv i32 %6, %1
%14 = icmp ne i32 %13, 0
%15 = icmp ne i64 %12, 0
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %4, label %17
17: ; preds = %4
%18 = shl i64 %5, 32
%19 = ashr exact i64 %18, 32
br label %20
20: ; preds = %17, %2
%21 = phi i64 [ 31, %2 ], [ %19, %17 ]
%22 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @itoa.buf, i64 0, i64 0))
%23 = getelementptr inbounds [32 x i8], [32 x i8]* @itoa.buf, i64 0, i64 %21
ret i8* %23
}