char *hexc(char *p, unsigned x) {
  static char hex[16] = "0123456789abcdef";
  if (x > 0x0fff)
    *p++ = hex[(x >> 12) & 15];
  if (x > 0x00ff)
    *p++ = hex[(x >> 8) & 15];
  if (x > 0x000f)
    *p++ = hex[(x >> 4) & 15];
  *p++ = hex[x & 15];
  return p;
}


x86 assembly (clang, -O0):
<hexc>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0xfff,-0xc(%rbp)
jbe    1140 <hexc+0x40>
mov    -0xc(%rbp),%eax
shr    $0xc,%eax
and    $0xf,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0x2f04(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
cmpl   $0xff,-0xc(%rbp)
jbe    1175 <hexc+0x75>
mov    -0xc(%rbp),%eax
shr    $0x8,%eax
and    $0xf,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0x2ecf(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
cmpl   $0xf,-0xc(%rbp)
jbe    11a7 <hexc+0xa7>
mov    -0xc(%rbp),%eax
shr    $0x4,%eax
and    $0xf,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0x2e9d(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0xc(%rbp),%eax
and    $0xf,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0x2e78(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexc>:
mov    %rdi,%rax
cmp    $0x1000,%esi
jb     1125 <hexc+0x25>
mov    %esi,%ecx
shr    $0xc,%ecx
and    $0xf,%ecx
lea    0xee6(%rip),%rdx
mov    (%rcx,%rdx,1),%cl
mov    %cl,(%rax)
add    $0x1,%rax
jmp    112d <hexc+0x2d>
cmp    $0x100,%esi
jb     1171 <hexc+0x71>
mov    %esi,%ecx
shr    $0x8,%ecx
and    $0xf,%ecx
lea    0xec4(%rip),%rdx
mov    (%rcx,%rdx,1),%cl
mov    %cl,(%rax)
add    $0x1,%rax
mov    %esi,%ecx
shr    $0x4,%ecx
and    $0xf,%ecx
lea    0xeac(%rip),%rdx
mov    (%rcx,%rdx,1),%cl
mov    %cl,(%rax)
add    $0x1,%rax
and    $0xf,%esi
lea    0xe99(%rip),%rcx
mov    (%rsi,%rcx,1),%cl
mov    %cl,(%rax)
add    $0x1,%rax
ret
cmp    $0x10,%esi
jae    1145 <hexc+0x45>
jmp    115d <hexc+0x5d>

LLVM IR (-O0):
@hexc.hex = internal global [16 x i8] c"0123456789abcdef"
define i8* @hexc(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%5 = load i32, i32* %4
%6 = icmp ugt i32 %5, 4095
br i1 %6, label %7, label %16
7: ; preds = %2
%8 = load i32, i32* %4
%9 = lshr i32 %8, 12
%10 = and i32 %9, 15
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds [16 x i8], [16 x i8]* @hexc.hex, i64 0, i64 %11
%13 = load i8, i8* %12
%14 = load i8*, i8** %3
%15 = getelementptr inbounds i8, i8* %14, i32 1
store i8* %15, i8** %3
store i8 %13, i8* %14
br label %16
16: ; preds = %7, %2
%17 = load i32, i32* %4
%18 = icmp ugt i32 %17, 255
br i1 %18, label %19, label %28
19: ; preds = %16
%20 = load i32, i32* %4
%21 = lshr i32 %20, 8
%22 = and i32 %21, 15
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds [16 x i8], [16 x i8]* @hexc.hex, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = load i8*, i8** %3
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %3
store i8 %25, i8* %26
br label %28
28: ; preds = %19, %16
%29 = load i32, i32* %4
%30 = icmp ugt i32 %29, 15
br i1 %30, label %31, label %40
31: ; preds = %28
%32 = load i32, i32* %4
%33 = lshr i32 %32, 4
%34 = and i32 %33, 15
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds [16 x i8], [16 x i8]* @hexc.hex, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = load i8*, i8** %3
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %3
store i8 %37, i8* %38
br label %40
40: ; preds = %31, %28
%41 = load i32, i32* %4
%42 = and i32 %41, 15
%43 = zext i32 %42 to i64
%44 = getelementptr inbounds [16 x i8], [16 x i8]* @hexc.hex, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = load i8*, i8** %3
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %3
store i8 %45, i8* %46
%48 = load i8*, i8** %3
ret i8* %48
}

LLVM IR (-O2):
@hexc.hex = internal unnamed_addr constant [16 x i8] c"0123456789abcdef"
define i8* @hexc(i8* writeonly %0, i32 %1) {
%3 = icmp ugt i32 %1, 4095
br i1 %3, label %4, label %11
4: ; preds = %2
%5 = lshr i32 %1, 12
%6 = and i32 %5, 15
%7 = zext i32 %6 to i64
%8 = getelementptr inbounds [16 x i8], [16 x i8]* @hexc.hex, i64 0, i64 %7
%9 = load i8, i8* %8
%10 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %9, i8* %0
br label %13
11: ; preds = %2
%12 = icmp ugt i32 %1, 255
br i1 %12, label %13, label %21
13: ; preds = %11, %4
%14 = phi i8* [ %10, %4 ], [ %0, %11 ]
%15 = lshr i32 %1, 8
%16 = and i32 %15, 15
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds [16 x i8], [16 x i8]* @hexc.hex, i64 0, i64 %17
%19 = load i8, i8* %18
%20 = getelementptr inbounds i8, i8* %14, i64 1
store i8 %19, i8* %14
br label %23
21: ; preds = %11
%22 = icmp ugt i32 %1, 15
br i1 %22, label %23, label %31
23: ; preds = %13, %21
%24 = phi i8* [ %20, %13 ], [ %0, %21 ]
%25 = lshr i32 %1, 4
%26 = and i32 %25, 15
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds [16 x i8], [16 x i8]* @hexc.hex, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = getelementptr inbounds i8, i8* %24, i64 1
store i8 %29, i8* %24
br label %31
31: ; preds = %23, %21
%32 = phi i8* [ %30, %23 ], [ %0, %21 ]
%33 = and i32 %1, 15
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds [16 x i8], [16 x i8]* @hexc.hex, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = getelementptr inbounds i8, i8* %32, i64 1
store i8 %36, i8* %32
ret i8* %37
}