char *itoa_own(unsigned short val) {
  static char buf[6] = {0, 0, 0, 0, 0, 0};
  unsigned char i = sizeof(buf);
  for (; val && i; --i, val /= 10)
    buf[i] = "0123456789abcdef"[val % 10];
  return &buf[i + 1];
}


x86 assembly (clang, -O0):
<itoa_own>:
push   %rbp
mov    %rsp,%rbp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
movb   $0x6,-0x3(%rbp)
movzwl -0x2(%rbp),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x4(%rbp)
je     112e <itoa_own+0x2e>
movzbl -0x3(%rbp),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x4(%rbp)
mov    -0x4(%rbp),%al
test   $0x1,%al
jne    113e <itoa_own+0x3e>
jmp    1184 <itoa_own+0x84>
movzwl -0x2(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
movslq %edx,%rcx
lea    0xeac(%rip),%rax
mov    (%rax,%rcx,1),%dl
movzbl -0x3(%rbp),%eax
mov    %eax,%ecx
lea    0x2ebd(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0x3(%rbp),%al
add    $0xff,%al
mov    %al,-0x3(%rbp)
movzwl -0x2(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %ax,-0x2(%rbp)
jmp    110f <itoa_own+0xf>
movzbl -0x3(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rcx
lea    0x2e8c(%rip),%rax
add    %rcx,%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<itoa_own>:
test   %edi,%edi
je     11f9 <itoa_own+0xf9>
movzwl %di,%eax
imul   $0xcccd,%eax,%esi
shr    $0x13,%esi
lea    (%rsi,%rsi,1),%ecx
lea    (%rcx,%rcx,4),%ecx
mov    %edi,%edx
sub    %ecx,%edx
movzwl %dx,%edx
lea    0xed8(%rip),%rcx
mov    (%rdx,%rcx,1),%dl
mov    %dl,0x2ef6(%rip)
mov    $0x6,%edx
cmp    $0xa,%ax
jb     11fe <itoa_own+0xfe>
movzwl %si,%edx
imul   $0xcccd,%edx,%esi
shr    $0x12,%esi
and    $0xfffffffe,%esi
lea    (%rsi,%rsi,4),%esi
sub    %esi,%edx
movzwl %dx,%edx
mov    (%rdx,%rcx,1),%dl
mov    %dl,0x2ec6(%rip)
mov    $0x5,%edx
cmp    $0x64,%di
jb     11fe <itoa_own+0xfe>
mov    %eax,%edx
shr    $0x2,%edx
imul   $0x147b,%edx,%edx
shr    $0x11,%edx
imul   $0xcccd,%edx,%esi
shr    $0x12,%esi
and    $0xfffffffe,%esi
lea    (%rsi,%rsi,4),%esi
sub    %esi,%edx
movzwl %dx,%edx
mov    (%rdx,%rcx,1),%dl
mov    %dl,0x2e8b(%rip)
mov    $0x4,%edx
cmp    $0x3e8,%eax
jb     11fe <itoa_own+0xfe>
mov    %eax,%edx
shr    $0x3,%edx
imul   $0x20c5,%edx,%edx
shr    $0x14,%edx
imul   $0xcccd,%edx,%esi
shr    $0x12,%esi
and    $0xfffffffe,%esi
lea    (%rsi,%rsi,4),%esi
sub    %esi,%edx
movzwl %dx,%edx
mov    (%rdx,%rcx,1),%dl
mov    %dl,0x2e53(%rip)
mov    $0x3,%edx
cmp    $0x2710,%eax
jb     11fe <itoa_own+0xfe>
shr    $0x4,%eax
imul   $0x347,%eax,%eax
shr    $0x13,%eax
mov    (%rax,%rcx,1),%al
mov    %al,0x2e31(%rip)
mov    $0x2,%edx
jmp    11fe <itoa_own+0xfe>
mov    $0x7,%edx
lea    0x2e1c(%rip),%rax
add    %rdx,%rax
ret

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

LLVM IR (-O2):
@itoa_own.buf = internal global [6 x i8] zeroinitializer
@.str = [17 x i8] c"0123456789abcdef\00"
define i8* @itoa_own(i16 zeroext %0) {
%2 = icmp eq i16 %0, 0
br i1 %2, label %35, label %3
3: ; preds = %1
%4 = urem i16 %0, 10
%5 = udiv i16 %0, 10
%6 = zext i16 %4 to i64
%7 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %6
%8 = load i8, i8* %7
store i8 %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @itoa_own.buf, i64 1, i64 0)
%9 = icmp ugt i16 %0, 9
br i1 %9, label %10, label %35
10: ; preds = %3
%11 = urem i16 %5, 10
%12 = zext i16 %11 to i64
%13 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %12
%14 = load i8, i8* %13
store i8 %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @itoa_own.buf, i64 0, i64 5)
%15 = icmp ugt i16 %0, 99
br i1 %15, label %16, label %35
16: ; preds = %10
%17 = udiv i16 %0, 100
%18 = urem i16 %17, 10
%19 = zext i16 %18 to i64
%20 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %19
%21 = load i8, i8* %20
store i8 %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @itoa_own.buf, i64 0, i64 4)
%22 = icmp ugt i16 %0, 999
br i1 %22, label %23, label %35
23: ; preds = %16
%24 = udiv i16 %0, 1000
%25 = urem i16 %24, 10
%26 = zext i16 %25 to i64
%27 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %26
%28 = load i8, i8* %27
store i8 %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @itoa_own.buf, i64 0, i64 3)
%29 = icmp ugt i16 %0, 9999
br i1 %29, label %30, label %35
30: ; preds = %23
%31 = udiv i16 %0, 10000
%32 = zext i16 %31 to i64
%33 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %32
%34 = load i8, i8* %33
store i8 %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @itoa_own.buf, i64 0, i64 2)
br label %35
35: ; preds = %3, %10, %16, %23, %30, %1
%36 = phi i64 [ 7, %1 ], [ 6, %3 ], [ 5, %10 ], [ 4, %16 ], [ 3, %23 ], [ 2, %30 ]
%37 = getelementptr inbounds [6 x i8], [6 x i8]* @itoa_own.buf, i64 0, i64 %36
ret i8* %37
}