void print_hex(n) int n;
{
  static char chars[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  int tmp;
  int i;
  for (i = 12; i >= 0; i -= 4) {
    tmp = (n >> i) & 0xF;
    print_char(chars[tmp]);
  }
}


x86 assembly (clang, -O0):
<print_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xc,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     115e <print_hex+0x4e>
mov    -0x4(%rbp),%eax
mov    -0xc(%rbp),%ecx
sar    %cl,%eax
and    $0xf,%eax
mov    %eax,-0x8(%rbp)
movslq -0x8(%rbp),%rcx
lea    0x2eeb(%rip),%rax
movsbl (%rax,%rcx,1),%edi
mov    $0x0,%al
call   1030 <print_char@plt>
mov    -0xc(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0xc(%rbp)
jmp    1122 <print_hex+0x12>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_hex>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %edi,%eax
shr    $0xc,%eax
and    $0xf,%eax
lea    0xedb(%rip),%r14
movsbl (%rax,%r14,1),%edi
xor    %eax,%eax
call   1030 <print_char@plt>
mov    %ebx,%eax
shr    $0x8,%eax
and    $0xf,%eax
movsbl (%rax,%r14,1),%edi
xor    %eax,%eax
call   1030 <print_char@plt>
mov    %ebx,%eax
shr    $0x4,%eax
and    $0xf,%eax
movsbl (%rax,%r14,1),%edi
xor    %eax,%eax
call   1030 <print_char@plt>
and    $0xf,%ebx
movsbl (%rbx,%r14,1),%edi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <print_char@plt>

LLVM IR (-O0):
@print_hex.chars = internal global [16 x i8] c"0123456789abcdef"
define void @print_hex(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
store i32 12, i32* %4
br label %5
5: ; preds = %19, %1
%6 = load i32, i32* %4
%7 = icmp sge i32 %6, 0
br i1 %7, label %8, label %22
8: ; preds = %5
%9 = load i32, i32* %2
%10 = load i32, i32* %4
%11 = ashr i32 %9, %10
%12 = and i32 %11, 15
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [16 x i8], [16 x i8]* @print_hex.chars, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = call i32 (i32, ...) bitcast (i32 (...)* @print_char to i32 (i32, ...)*)(i32 %17)
br label %19
19: ; preds = %8
%20 = load i32, i32* %4
%21 = sub nsw i32 %20, 4
store i32 %21, i32* %4
br label %5
22: ; preds = %5
ret void
}

LLVM IR (-O2):
@print_hex.chars = internal unnamed_addr constant [16 x i8] c"0123456789abcdef"
define void @print_hex(i32 %0) {
%2 = lshr i32 %0, 12
%3 = and i32 %2, 15
%4 = zext i32 %3 to i64
%5 = getelementptr inbounds [16 x i8], [16 x i8]* @print_hex.chars, i64 0, i64 %4
%6 = load i8, i8* %5
%7 = sext i8 %6 to i32
%8 = tail call i32 (i32, ...) bitcast (i32 (...)* @print_char to i32 (i32, ...)*)(i32 %7)
%9 = lshr i32 %0, 8
%10 = and i32 %9, 15
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds [16 x i8], [16 x i8]* @print_hex.chars, i64 0, i64 %11
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = tail call i32 (i32, ...) bitcast (i32 (...)* @print_char to i32 (i32, ...)*)(i32 %14)
%16 = lshr i32 %0, 4
%17 = and i32 %16, 15
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds [16 x i8], [16 x i8]* @print_hex.chars, i64 0, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = tail call i32 (i32, ...) bitcast (i32 (...)* @print_char to i32 (i32, ...)*)(i32 %21)
%23 = and i32 %0, 15
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds [16 x i8], [16 x i8]* @print_hex.chars, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = tail call i32 (i32, ...) bitcast (i32 (...)* @print_char to i32 (i32, ...)*)(i32 %27)
ret void
}