char *TOHEXSTR(char *Result, short Num) {
  static char the_chars[16] = "0123456789ABCDEF";
  short I;
  Result[4] = '\0';
  for (I = 0; I <= 3; I++)
    Result[3 - I] = the_chars[(((unsigned)Num) >> (I * 4)) & 0xf];
  return Result;
}


x86 assembly (clang, -O0):
<TOHEXSTR>:
push   %rbp
mov    %rsp,%rbp
mov    %si,%ax
mov    %rdi,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,0x4(%rax)
movw   $0x0,-0xc(%rbp)
movswl -0xc(%rbp),%eax
cmp    $0x3,%eax
jg     116e <TOHEXSTR+0x6e>
movswl -0xa(%rbp),%eax
movswl -0xc(%rbp),%ecx
shl    $0x2,%ecx
shr    %cl,%eax
and    $0xf,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0x2eeb(%rip),%rax
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
movswl -0xc(%rbp),%esi
mov    $0x3,%ecx
sub    %esi,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0xc(%rbp)
jmp    111d <TOHEXSTR+0x1d>
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<TOHEXSTR>:
mov    %rdi,%rax
movb   $0x0,0x4(%rdi)
movzwl %si,%ecx
movzwl %si,%edi
and    $0xf,%esi
lea    0xee9(%rip),%r8
mov    (%rsi,%r8,1),%dl
mov    %dl,0x3(%rax)
mov    %ecx,%edx
shr    $0x4,%edx
and    $0xf,%edx
mov    (%rdx,%r8,1),%dl
mov    %dl,0x2(%rax)
shr    $0x8,%ecx
and    $0xf,%ecx
mov    (%rcx,%r8,1),%cl
mov    %cl,0x1(%rax)
shr    $0xc,%rdi
mov    (%rdi,%r8,1),%cl
mov    %cl,(%rax)
ret

LLVM IR (-O0):
@TOHEXSTR.the_chars = internal global [16 x i8] c"0123456789ABCDEF"
define i8* @TOHEXSTR(i8* %0, i16 signext %1) {
%3 = alloca i8*
%4 = alloca i16
%5 = alloca i16
store i8* %0, i8** %3
store i16 %1, i16* %4
%6 = load i8*, i8** %3
%7 = getelementptr inbounds i8, i8* %6, i64 4
store i8 0, i8* %7
store i16 0, i16* %5
br label %8
8: ; preds = %29, %2
%9 = load i16, i16* %5
%10 = sext i16 %9 to i32
%11 = icmp sle i32 %10, 3
br i1 %11, label %12, label %32
12: ; preds = %8
%13 = load i16, i16* %4
%14 = sext i16 %13 to i32
%15 = load i16, i16* %5
%16 = sext i16 %15 to i32
%17 = mul nsw i32 %16, 4
%18 = lshr i32 %14, %17
%19 = and i32 %18, 15
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds [16 x i8], [16 x i8]* @TOHEXSTR.the_chars, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = load i8*, i8** %3
%24 = load i16, i16* %5
%25 = sext i16 %24 to i32
%26 = sub nsw i32 3, %25
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %23, i64 %27
store i8 %22, i8* %28
br label %29
29: ; preds = %12
%30 = load i16, i16* %5
%31 = add i16 %30, 1
store i16 %31, i16* %5
br label %8
32: ; preds = %8
%33 = load i8*, i8** %3
ret i8* %33
}

LLVM IR (-O2):
@TOHEXSTR.the_chars = internal unnamed_addr constant [16 x i8] c"0123456789ABCDEF"
define i8* @TOHEXSTR(i8* returned writeonly %0, i16 signext %1) {
%3 = getelementptr inbounds i8, i8* %0, i64 4
store i8 0, i8* %3
%4 = zext i16 %1 to i32
%5 = and i32 %4, 15
%6 = zext i32 %5 to i64
%7 = getelementptr inbounds [16 x i8], [16 x i8]* @TOHEXSTR.the_chars, i64 0, i64 %6
%8 = load i8, i8* %7
%9 = getelementptr inbounds i8, i8* %0, i64 3
store i8 %8, i8* %9
%10 = lshr i32 %4, 4
%11 = and i32 %10, 15
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds [16 x i8], [16 x i8]* @TOHEXSTR.the_chars, i64 0, i64 %12
%14 = load i8, i8* %13
%15 = getelementptr inbounds i8, i8* %0, i64 2
store i8 %14, i8* %15
%16 = lshr i32 %4, 8
%17 = and i32 %16, 15
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds [16 x i8], [16 x i8]* @TOHEXSTR.the_chars, i64 0, i64 %18
%20 = load i8, i8* %19
%21 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %20, i8* %21
%22 = lshr i32 %4, 12
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds [16 x i8], [16 x i8]* @TOHEXSTR.the_chars, i64 0, i64 %23
%25 = load i8, i8* %24
store i8 %25, i8* %0
ret i8* %0
}