const char *toHex(unsigned short u) {
  static char hexVal[5];
  int i = 3;
  while (i >= 0) {
    unsigned short hex = (u & 0x000f);
    if (hex < 0x0a)
      hexVal[i] = '0' + hex;
    else
      hexVal[i] = 'A' + (hex - 0x0a);
    i--;
  }
  hexVal[4] = '\0';
  return hexVal;
}


x86 assembly (clang, -O0):
<toHex>:
push   %rbp
mov    %rsp,%rbp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
movl   $0x3,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1178 <toHex+0x78>
movzwl -0x2(%rbp),%eax
and    $0xf,%eax
mov    %ax,-0xa(%rbp)
movzwl -0xa(%rbp),%eax
cmp    $0xa,%eax
jge    1150 <toHex+0x50>
movzwl -0xa(%rbp),%eax
add    $0x30,%eax
mov    %al,%dl
movslq -0x8(%rbp),%rcx
lea    0x2ed9(%rip),%rax
mov    %dl,(%rax,%rcx,1)
jmp    116a <toHex+0x6a>
movzwl -0xa(%rbp),%eax
sub    $0xa,%eax
add    $0x41,%eax
mov    %al,%dl
movslq -0x8(%rbp),%rcx
lea    0x2eba(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    1112 <toHex+0x12>
movb   $0x0,0x2ea6(%rip)
lea    0x2e9b(%rip),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<toHex>:
and    $0xf,%edi
lea    0x37(%rdi),%eax
mov    %edi,%ecx
or     $0x30,%cl
cmp    $0xa,%di
movzbl %cl,%ecx
movzbl %al,%edx
cmovb  %ecx,%edx
mov    %dl,0x2f06(%rip)
lea    0x2efc(%rip),%rax
mov    %dl,0x2ef8(%rip)
mov    %dl,0x2ef1(%rip)
mov    %dl,0x2eea(%rip)
movb   $0x0,0x2ee7(%rip)
ret

LLVM IR (-O0):
@toHex.hexVal = internal global [5 x i8] zeroinitializer
define i8* @toHex(i16 zeroext %0) {
%2 = alloca i16
%3 = alloca i32
%4 = alloca i16
store i16 %0, i16* %2
store i32 3, i32* %3
br label %5
5: ; preds = %33, %1
%6 = load i32, i32* %3
%7 = icmp sge i32 %6, 0
br i1 %7, label %8, label %36
8: ; preds = %5
%9 = load i16, i16* %2
%10 = zext i16 %9 to i32
%11 = and i32 %10, 15
%12 = trunc i32 %11 to i16
store i16 %12, i16* %4
%13 = load i16, i16* %4
%14 = zext i16 %13 to i32
%15 = icmp slt i32 %14, 10
br i1 %15, label %16, label %24
16: ; preds = %8
%17 = load i16, i16* %4
%18 = zext i16 %17 to i32
%19 = add nsw i32 48, %18
%20 = trunc i32 %19 to i8
%21 = load i32, i32* %3
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [5 x i8], [5 x i8]* @toHex.hexVal, i64 0, i64 %22
store i8 %20, i8* %23
br label %33
24: ; preds = %8
%25 = load i16, i16* %4
%26 = zext i16 %25 to i32
%27 = sub nsw i32 %26, 10
%28 = add nsw i32 65, %27
%29 = trunc i32 %28 to i8
%30 = load i32, i32* %3
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [5 x i8], [5 x i8]* @toHex.hexVal, i64 0, i64 %31
store i8 %29, i8* %32
br label %33
33: ; preds = %24, %16
%34 = load i32, i32* %3
%35 = add nsw i32 %34, -1
store i32 %35, i32* %3
br label %5
36: ; preds = %5
store i8 0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @toHex.hexVal, i64 0, i64 4)
ret i8* getelementptr inbounds ([5 x i8], [5 x i8]* @toHex.hexVal, i64 0, i64 0)
}

LLVM IR (-O2):
@toHex.hexVal = internal global [5 x i8] zeroinitializer
define i8* @toHex(i16 zeroext %0) {
%2 = and i16 %0, 15
%3 = icmp ult i16 %2, 10
%4 = trunc i16 %2 to i8
%5 = add nuw nsw i8 %4, 55
%6 = or i8 %4, 48
%7 = select i1 %3, i8 %6, i8 %5
store i8 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @toHex.hexVal, i64 0, i64 3)
store i8 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @toHex.hexVal, i64 0, i64 2)
store i8 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @toHex.hexVal, i64 0, i64 1)
store i8 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @toHex.hexVal, i64 0, i64 0)
store i8 0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @toHex.hexVal, i64 0, i64 4)
ret i8* getelementptr inbounds ([5 x i8], [5 x i8]* @toHex.hexVal, i64 0, i64 0)
}