char *itoa(int val, char *ret, int radix) {
  if (radix == 10)
    snprintf(ret, 32 - 1, "%d", val);
  else if (radix == 16)
    snprintf(ret, 32 - 1, "%x", val);
  else
    hu_log(6, "hu_uti", __func__, "radix != 10 && != 16: %d", radix);
  return (ret);
}


x86 assembly (clang, -O0):
<itoa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jne    115b <itoa+0x3b>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x1f,%esi
lea    0xed6(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    11ae <itoa+0x8e>
cmpl   $0x10,-0x14(%rbp)
jne    1184 <itoa+0x64>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x1f,%esi
lea    0xe88(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    11a9 <itoa+0x89>
mov    -0x14(%rbp),%r8d
mov    $0x6,%edi
lea    0xe6f(%rip),%rsi
lea    0xe6f(%rip),%rdx
lea    0xe6d(%rip),%rcx
mov    $0x0,%al
call   1040 <hu_log@plt>
jmp    11ae <itoa+0x8e>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itoa>:
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ecx
cmp    $0x10,%edx
je     113c <itoa+0x1c>
mov    %edx,%r8d
cmp    $0xa,%edx
jne    1154 <itoa+0x34>
lea    0xeeb(%rip),%rdx
jmp    1143 <itoa+0x23>
lea    0xebd(%rip),%rdx
mov    $0x1f,%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    1175 <itoa+0x55>
lea    0xea8(%rip),%rsi
lea    0xea8(%rip),%rdx
lea    0xea6(%rip),%rcx
mov    $0x6,%edi
xor    %eax,%eax
call   1040 <hu_log@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [3 x i8] c"%x\00"
@.str.2 = [7 x i8] c"hu_uti\00"
@__func__.itoa = [5 x i8] c"itoa\00"
@.str.3 = [25 x i8] c"radix != 10 && != 16: %d\00"
define i8* @itoa(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%7 = load i32, i32* %6
%8 = icmp eq i32 %7, 10
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = load i8*, i8** %5
%11 = load i32, i32* %4
%12 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %10, i64 31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %24
13: ; preds = %3
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, 16
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i8*, i8** %5
%18 = load i32, i32* %4
%19 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %17, i64 31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %23
20: ; preds = %13
%21 = load i32, i32* %6
%22 = call i32 (i32, i8*, i8*, i8*, i32, ...) bitcast (i32 (...)* @hu_log to i32 (i32, i8*, i8*, i8*, i32, ...)*)(i32 6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @__func__.itoa, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %20, %16
br label %24
24: ; preds = %23, %9
%25 = load i8*, i8** %5
ret i8* %25
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [3 x i8] c"%x\00"
@.str.2 = [7 x i8] c"hu_uti\00"
@__func__.itoa = [5 x i8] c"itoa\00"
@.str.3 = [25 x i8] c"radix != 10 && != 16: %d\00"
define i8* @itoa(i32 %0, i8* returned writeonly %1, i32 %2) {
switch i32 %2, label %8 [
i32 10, label %4
i32 16, label %6
]
4: ; preds = %3
%5 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* dereferenceable(1) %1, i64 31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %10
6: ; preds = %3
%7 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* dereferenceable(1) %1, i64 31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %10
8: ; preds = %3
%9 = tail call i32 (i32, i8*, i8*, i8*, i32, ...) bitcast (i32 (...)* @hu_log to i32 (i32, i8*, i8*, i8*, i32, ...)*)(i32 6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @__func__.itoa, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %2)
br label %10
10: ; preds = %6, %8, %4
ret i8* %1
}