char *fmt_ul(unsigned long u) {
  static char strnum[48];
  char *hex = "0123456789abcdef";
  char *s = strnum + 44;
  *s = 0;
  do {
    *--s = hex[(u % 16)];
    u /= 16;
  } while (u);
  *--s = 'x';
  *--s = '0';
  return s;
}


x86 assembly (clang, -O0):
<fmt_ul>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
lea    0xef1(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0x2f16(%rip),%rax
add    $0x2c,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
and    $0xf,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,-0x1(%rax)
mov    -0x8(%rbp),%rax
shr    $0x4,%rax
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1129 <fmt_ul+0x29>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x78,-0x1(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x30,-0x1(%rax)
mov    -0x18(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmt_ul>:
movb   $0x0,0x2f55(%rip)
lea    0x2f4c(%rip),%rax
lea    0xeeb(%rip),%r8
mov    %rdi,%rdx
nopl   0x0(%rax,%rax,1)
mov    %edi,%esi
and    $0xf,%esi
movzbl (%rsi,%r8,1),%ecx
mov    %cl,0x1(%rax)
shr    $0x4,%rdx
add    $0xffffffffffffffff,%rax
cmp    $0xf,%rdi
mov    %rdx,%rdi
ja     1120 <fmt_ul+0x20>
movw   $0x7830,(%rax)
ret

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

LLVM IR (-O2):
@fmt_ul.strnum = internal global [48 x i8] zeroinitializer
@.str = [17 x i8] c"0123456789abcdef\00"
define i8* @fmt_ul(i64 %0) {
store i8 0, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @fmt_ul.strnum, i64 0, i64 44)
br label %2
2: ; preds = %2, %1
%3 = phi i64 [ %0, %1 ], [ %9, %2 ]
%4 = phi i8* [ getelementptr inbounds ([48 x i8], [48 x i8]* @fmt_ul.strnum, i64 0, i64 44), %1 ], [ %8, %2 ]
%5 = and i64 %3, 15
%6 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %5
%7 = load i8, i8* %6
%8 = getelementptr inbounds i8, i8* %4, i64 -1
store i8 %7, i8* %8
%9 = lshr i64 %3, 4
%10 = icmp ult i64 %3, 16
br i1 %10, label %11, label %2
11: ; preds = %2
%12 = getelementptr inbounds i8, i8* %4, i64 -2
store i8 120, i8* %12
%13 = getelementptr inbounds i8, i8* %4, i64 -3
store i8 48, i8* %13
ret i8* %13
}