char *xtoa(unsigned long num) {
  static char buf[12];
  int i, digit;
  buf[8] = 0;
  for (i = 7; i >= 0; --i) {
    digit = num & 0xf;
    buf[i] = digit + (digit < 10 ? '0' : 'A' - 10);
    num >>= 4;
  }
  return buf;
}


x86 assembly (clang, -O0):
<xtoa>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
movb   $0x0,0x2f1a(%rip)
movl   $0x7,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     116d <xtoa+0x6d>
mov    -0x8(%rbp),%rax
and    $0xf,%rax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x10(%rbp),%esi
mov    $0x37,%ecx
mov    $0x30,%edx
cmp    $0xa,%esi
cmovl  %edx,%ecx
add    %ecx,%eax
mov    %al,%dl
movslq -0xc(%rbp),%rcx
lea    0x2ed1(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
shr    $0x4,%rax
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1116 <xtoa+0x16>
lea    0x2ead(%rip),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<xtoa>:
movb   $0x0,0x2f22(%rip)
lea    0x2f13(%rip),%rax
mov    %edi,%ecx
shr    $0x4,%ecx
mov    %edi,%edx
shr    $0x8,%edx
mov    %edi,%esi
shr    $0xc,%esi
movd   %edi,%xmm0
movd   %ecx,%xmm1
punpckldq %xmm0,%xmm1
movd   %edx,%xmm0
movd   %esi,%xmm2
punpckldq %xmm0,%xmm2
punpcklqdq %xmm1,%xmm2
pand   0xebf(%rip),%xmm2
movdqa 0xec7(%rip),%xmm0
pcmpgtd %xmm2,%xmm0
movdqa %xmm0,%xmm1
pandn  0xec7(%rip),%xmm1
pand   0xecf(%rip),%xmm0
por    %xmm1,%xmm0
paddd  %xmm2,%xmm0
packuswb %xmm0,%xmm0
packuswb %xmm0,%xmm0
movd   %xmm0,0x2eac(%rip)
mov    %edi,%ecx
shr    $0x10,%ecx
and    $0xf,%ecx
lea    0x37(%rcx),%edx
lea    0x30(%rcx),%esi
cmp    $0xa,%ecx
cmovae %edx,%esi
mov    %sil,0x2e90(%rip)
mov    %edi,%ecx
shr    $0x14,%ecx
and    $0xf,%ecx
lea    0x37(%rcx),%edx
lea    0x30(%rcx),%esi
cmp    $0xa,%ecx
cmovae %edx,%esi
mov    %sil,0x2e74(%rip)
mov    %edi,%ecx
shr    $0x18,%ecx
and    $0xf,%ecx
lea    0x37(%rcx),%edx
lea    0x30(%rcx),%esi
cmp    $0xa,%ecx
cmovae %edx,%esi
mov    %sil,0x2e58(%rip)
mov    %edi,%ecx
shr    $0x1c,%ecx
lea    0x37(%rcx),%edx
or     $0x30,%ecx
cmp    $0xa0000000,%edi
cmovae %edx,%ecx
mov    %cl,0x2e3d(%rip)
ret

LLVM IR (-O0):
@xtoa.buf = internal global [12 x i8] zeroinitializer
define i8* @xtoa(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
store i64 %0, i64* %2
store i8 0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @xtoa.buf, i64 0, i64 8)
store i32 7, i32* %3
br label %5
5: ; preds = %24, %1
%6 = load i32, i32* %3
%7 = icmp sge i32 %6, 0
br i1 %7, label %8, label %27
8: ; preds = %5
%9 = load i64, i64* %2
%10 = and i64 %9, 15
%11 = trunc i64 %10 to i32
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 10
%15 = zext i1 %14 to i64
%16 = select i1 %14, i32 48, i32 55
%17 = add nsw i32 %12, %16
%18 = trunc i32 %17 to i8
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [12 x i8], [12 x i8]* @xtoa.buf, i64 0, i64 %20
store i8 %18, i8* %21
%22 = load i64, i64* %2
%23 = lshr i64 %22, 4
store i64 %23, i64* %2
br label %24
24: ; preds = %8
%25 = load i32, i32* %3
%26 = add nsw i32 %25, -1
store i32 %26, i32* %3
br label %5
27: ; preds = %5
ret i8* getelementptr inbounds ([12 x i8], [12 x i8]* @xtoa.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@xtoa.buf = internal global [12 x i8] zeroinitializer
define i8* @xtoa(i64 %0) {
store i8 0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @xtoa.buf, i64 0, i64 8)
%2 = trunc i64 %0 to i32
%3 = trunc i64 %0 to i32
%4 = lshr i32 %3, 4
%5 = trunc i64 %0 to i32
%6 = lshr i32 %5, 8
%7 = trunc i64 %0 to i32
%8 = lshr i32 %7, 12
%9 = insertelement <4 x i32> poison, i32 %8, i64 0
%10 = insertelement <4 x i32> %9, i32 %6, i64 1
%11 = insertelement <4 x i32> %10, i32 %4, i64 2
%12 = insertelement <4 x i32> %11, i32 %2, i64 3
%13 = and <4 x i32> %12, <i32 15, i32 15, i32 15, i32 15>
%14 = icmp ult <4 x i32> %13, <i32 10, i32 10, i32 10, i32 10>
%15 = select <4 x i1> %14, <4 x i32> <i32 48, i32 48, i32 48, i32 48>, <4 x i32> <i32 55, i32 55, i32 55, i32 55>
%16 = add nuw nsw <4 x i32> %15, %13
%17 = trunc <4 x i32> %16 to <4 x i8>
store <4 x i8> %17, <4 x i8>* bitcast (i8* getelementptr inbounds ([12 x i8], [12 x i8]* @xtoa.buf, i64 0, i64 4) to <4 x i8>*)
%18 = trunc i64 %0 to i32
%19 = lshr i32 %18, 16
%20 = and i32 %19, 15
%21 = icmp ult i32 %20, 10
%22 = select i1 %21, i32 48, i32 55
%23 = add nuw nsw i32 %22, %20
%24 = trunc i32 %23 to i8
store i8 %24, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @xtoa.buf, i64 0, i64 3)
%25 = trunc i64 %0 to i32
%26 = lshr i32 %25, 20
%27 = and i32 %26, 15
%28 = icmp ult i32 %27, 10
%29 = select i1 %28, i32 48, i32 55
%30 = add nuw nsw i32 %29, %27
%31 = trunc i32 %30 to i8
store i8 %31, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @xtoa.buf, i64 0, i64 2)
%32 = trunc i64 %0 to i32
%33 = lshr i32 %32, 24
%34 = and i32 %33, 15
%35 = icmp ult i32 %34, 10
%36 = select i1 %35, i32 48, i32 55
%37 = add nuw nsw i32 %36, %34
%38 = trunc i32 %37 to i8
store i8 %38, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @xtoa.buf, i64 0, i64 1)
%39 = trunc i64 %0 to i32
%40 = lshr i32 %39, 28
%41 = icmp ult i32 %39, -1610612736
%42 = select i1 %41, i32 48, i32 55
%43 = add nuw nsw i32 %42, %40
%44 = trunc i32 %43 to i8
store i8 %44, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @xtoa.buf, i64 0, i64 0)
ret i8* getelementptr inbounds ([12 x i8], [12 x i8]* @xtoa.buf, i64 0, i64 0)
}