const char *utoa(unsigned long i) {
  static char buf[(sizeof(unsigned long) * 4)];
  char *ptr = buf + (sizeof(unsigned long) * 4);
  if (!i)
    return "0";
  *--ptr = 0;
  while (i) {
    *--ptr = (i % 10) + '0';
    i /= 10;
  }
  return ptr;
}


x86 assembly (clang, -O0):
<utoa>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
lea    0x2f21(%rip),%rax
add    $0x20,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1132 <utoa+0x32>
lea    0xed7(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1195 <utoa+0x95>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,-0x1(%rax)
cmpq   $0x0,-0x10(%rbp)
je     118d <utoa+0x8d>
mov    -0x10(%rbp),%rax
mov    $0xa,%ecx
xor    %edx,%edx
div    %rcx
add    $0x30,%rdx
mov    %dl,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,-0x1(%rax)
mov    -0x10(%rbp),%rax
mov    $0xa,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x10(%rbp)
jmp    1145 <utoa+0x45>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<utoa>:
test   %rdi,%rdi
je     114d <utoa+0x4d>
movb   $0x0,0x2f43(%rip)
lea    0x2f3c(%rip),%rcx
movabs $0xcccccccccccccccd,%r8
cc cc cc
nopl   (%rax)
mov    %rdi,%rax
mul    %r8
shr    $0x3,%rdx
lea    (%rdx,%rdx,1),%eax
lea    (%rax,%rax,4),%eax
mov    %edi,%esi
sub    %eax,%esi
or     $0x30,%sil
mov    %sil,-0x1(%rcx)
add    $0xffffffffffffffff,%rcx
cmp    $0x9,%rdi
mov    %rdx,%rdi
ja     1120 <utoa+0x20>
mov    %rcx,%rax
ret
lea    0xeac(%rip),%rax
ret

LLVM IR (-O0):
@utoa.buf = internal global [32 x i8] zeroinitializer
@.str = [2 x i8] c"0\00"
define i8* @utoa(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
store i64 %0, i64* %3
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @utoa.buf, i64 1, i64 0), i8** %4
%5 = load i64, i64* %3
%6 = icmp ne i64 %5, 0
br i1 %6, label %8, label %7
7: ; preds = %1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2
br label %25
8: ; preds = %1
%9 = load i8*, i8** %4
%10 = getelementptr inbounds i8, i8* %9, i32 -1
store i8* %10, i8** %4
store i8 0, i8* %10
br label %11
11: ; preds = %14, %8
%12 = load i64, i64* %3
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %11
%15 = load i64, i64* %3
%16 = urem i64 %15, 10
%17 = add i64 %16, 48
%18 = trunc i64 %17 to i8
%19 = load i8*, i8** %4
%20 = getelementptr inbounds i8, i8* %19, i32 -1
store i8* %20, i8** %4
store i8 %18, i8* %20
%21 = load i64, i64* %3
%22 = udiv i64 %21, 10
store i64 %22, i64* %3
br label %11
23: ; preds = %11
%24 = load i8*, i8** %4
store i8* %24, i8** %2
br label %25
25: ; preds = %23, %7
%26 = load i8*, i8** %2
ret i8* %26
}

LLVM IR (-O2):
@utoa.buf = internal global [32 x i8] zeroinitializer
@.str = [2 x i8] c"0\00"
define i8* @utoa(i64 %0) {
%2 = icmp eq i64 %0, 0
br i1 %2, label %13, label %3
3: ; preds = %1
store i8 0, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @utoa.buf, i64 0, i64 31)
br label %4
4: ; preds = %3, %4
%5 = phi i8* [ getelementptr inbounds ([32 x i8], [32 x i8]* @utoa.buf, i64 0, i64 31), %3 ], [ %10, %4 ]
%6 = phi i64 [ %0, %3 ], [ %11, %4 ]
%7 = urem i64 %6, 10
%8 = trunc i64 %7 to i8
%9 = or i8 %8, 48
%10 = getelementptr inbounds i8, i8* %5, i64 -1
store i8 %9, i8* %10
%11 = udiv i64 %6, 10
%12 = icmp ult i64 %6, 10
br i1 %12, label %13, label %4
13: ; preds = %4, %1
%14 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %1 ], [ %10, %4 ]
ret i8* %14
}