char *inthex(unsigned val) {
  static char hexdigit[20] = "0123456789abcdef";
  static char string[12];
  int i;
  for (i = 0; i < 8; i++)
    string[i] = hexdigit[(val >> ((7 - i) * 4)) & 0xf];
  string[i] = 0;
  return string;
}


x86 assembly (clang, -O0):
<inthex>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x8,-0x8(%rbp)
jge    1155 <inthex+0x55>
mov    -0x4(%rbp),%eax
mov    $0x7,%ecx
sub    -0x8(%rbp),%ecx
shl    $0x2,%ecx
shr    %cl,%eax
and    $0xf,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0x2efa(%rip),%rax
mov    (%rax,%rcx,1),%dl
movslq -0x8(%rbp),%rcx
lea    0x2f01(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    110e <inthex+0xe>
movslq -0x8(%rbp),%rcx
lea    0x2ee5(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2eda(%rip),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<inthex>:
mov    %edi,%ecx
mov    %rcx,%rax
shr    $0x1c,%rax
lea    0xef0(%rip),%r8
mov    (%rax,%r8,1),%al
mov    %al,0x2f07(%rip)
lea    0x2f00(%rip),%rax
mov    %edi,%esi
shr    $0x18,%esi
and    $0xf,%esi
mov    (%rsi,%r8,1),%dl
mov    %dl,0x2eef(%rip)
mov    %edi,%edx
shr    $0x14,%edx
and    $0xf,%edx
mov    (%rdx,%r8,1),%dl
mov    %dl,0x2ede(%rip)
mov    %edi,%edx
shr    $0x10,%edx
and    $0xf,%edx
mov    (%rdx,%r8,1),%dl
mov    %dl,0x2ecd(%rip)
mov    %edi,%edx
shr    $0xc,%edx
and    $0xf,%edx
mov    (%rdx,%r8,1),%dl
mov    %dl,0x2ebc(%rip)
mov    %edi,%edx
shr    $0x8,%edx
and    $0xf,%edx
mov    (%rdx,%r8,1),%dl
mov    %dl,0x2eab(%rip)
shr    $0x4,%edi
and    $0xf,%edi
mov    (%rdi,%r8,1),%dl
mov    %dl,0x2e9c(%rip)
and    $0xf,%ecx
mov    (%rcx,%r8,1),%cl
mov    %cl,0x2e90(%rip)
movb   $0x0,0x2e8a(%rip)
ret

LLVM IR (-O0):
@inthex.hexdigit = internal global [20 x i8] c"0123456789abcdef\00\00\00\00"
@inthex.string = internal global [12 x i8] zeroinitializer
define i8* @inthex(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
br label %4
4: ; preds = %20, %1
%5 = load i32, i32* %3
%6 = icmp slt i32 %5, 8
br i1 %6, label %7, label %23
7: ; preds = %4
%8 = load i32, i32* %2
%9 = load i32, i32* %3
%10 = sub nsw i32 7, %9
%11 = mul nsw i32 %10, 4
%12 = lshr i32 %8, %11
%13 = and i32 %12, 15
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds [20 x i8], [20 x i8]* @inthex.hexdigit, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [12 x i8], [12 x i8]* @inthex.string, i64 0, i64 %18
store i8 %16, i8* %19
br label %20
20: ; preds = %7
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %4
23: ; preds = %4
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [12 x i8], [12 x i8]* @inthex.string, i64 0, i64 %25
store i8 0, i8* %26
ret i8* getelementptr inbounds ([12 x i8], [12 x i8]* @inthex.string, i64 0, i64 0)
}

LLVM IR (-O2):
@inthex.hexdigit = internal unnamed_addr constant [20 x i8] c"0123456789abcdef\00\00\00\00"
@inthex.string = internal global [12 x i8] zeroinitializer
define i8* @inthex(i32 %0) {
%2 = lshr i32 %0, 28
%3 = zext i32 %2 to i64
%4 = getelementptr inbounds [20 x i8], [20 x i8]* @inthex.hexdigit, i64 0, i64 %3
%5 = load i8, i8* %4
store i8 %5, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @inthex.string, i64 0, i64 0)
%6 = lshr i32 %0, 24
%7 = and i32 %6, 15
%8 = zext i32 %7 to i64
%9 = getelementptr inbounds [20 x i8], [20 x i8]* @inthex.hexdigit, i64 0, i64 %8
%10 = load i8, i8* %9
store i8 %10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @inthex.string, i64 0, i64 1)
%11 = lshr i32 %0, 20
%12 = and i32 %11, 15
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds [20 x i8], [20 x i8]* @inthex.hexdigit, i64 0, i64 %13
%15 = load i8, i8* %14
store i8 %15, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @inthex.string, i64 0, i64 2)
%16 = lshr i32 %0, 16
%17 = and i32 %16, 15
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds [20 x i8], [20 x i8]* @inthex.hexdigit, i64 0, i64 %18
%20 = load i8, i8* %19
store i8 %20, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @inthex.string, i64 0, i64 3)
%21 = lshr i32 %0, 12
%22 = and i32 %21, 15
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds [20 x i8], [20 x i8]* @inthex.hexdigit, i64 0, i64 %23
%25 = load i8, i8* %24
store i8 %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @inthex.string, i64 0, i64 4)
%26 = lshr i32 %0, 8
%27 = and i32 %26, 15
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds [20 x i8], [20 x i8]* @inthex.hexdigit, i64 0, i64 %28
%30 = load i8, i8* %29
store i8 %30, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @inthex.string, i64 0, i64 5)
%31 = lshr i32 %0, 4
%32 = and i32 %31, 15
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds [20 x i8], [20 x i8]* @inthex.hexdigit, i64 0, i64 %33
%35 = load i8, i8* %34
store i8 %35, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @inthex.string, i64 0, i64 6)
%36 = and i32 %0, 15
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds [20 x i8], [20 x i8]* @inthex.hexdigit, i64 0, i64 %37
%39 = load i8, i8* %38
store i8 %39, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @inthex.string, i64 0, i64 7)
store i8 0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @inthex.string, i64 0, i64 8)
ret i8* getelementptr inbounds ([12 x i8], [12 x i8]* @inthex.string, i64 0, i64 0)
}