#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int uint64_t;

/* Variables and functions */
int /*<<< orphan*/ print_char(char);
int /*<<< orphan*/ update_cursor();
void print_hex(uint64_t hex) {
  int n;
  for (int i = 15; i >= 0; i--) {
    n = (hex >> (i * 4)) & 0xF;
    if (n < 10) {
      print_char('0' + n);
    } else {
      print_char('a' + n - 10);
    }
  }
  update_cursor();
}


x86 assembly (clang, -O0):
<print_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xf,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     118e <print_hex+0x6e>
mov    -0x4(%rbp),%eax
mov    -0xc(%rbp),%ecx
shl    $0x2,%ecx
sar    %cl,%eax
and    $0xf,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    116a <print_hex+0x4a>
mov    -0x8(%rbp),%eax
add    $0x30,%eax
movsbl %al,%edi
call   1040 <print_char@plt>
jmp    117b <print_hex+0x5b>
mov    -0x8(%rbp),%eax
add    $0x61,%eax
sub    $0xa,%eax
movsbl %al,%edi
call   1040 <print_char@plt>
jmp    1180 <print_hex+0x60>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1132 <print_hex+0x12>
mov    $0x0,%al
call   1030 <update_cursor@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_hex>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    $0x3c,%ebp
nopw   0x0(%rax,%rax,1)
mov    %ebx,%eax
mov    %ebp,%ecx
sar    %cl,%eax
and    $0xf,%eax
lea    0x30(%rax),%ecx
lea    0x57(%rax),%edx
cmp    $0xa,%eax
movzbl %cl,%eax
movzbl %dl,%ecx
cmovb  %eax,%ecx
movzbl %cl,%edi
call   1040 <print_char@plt>
add    $0xfffffffc,%ebp
cmp    $0xfffffffc,%ebp
jne    1130 <print_hex+0x10>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <update_cursor@plt>

LLVM IR (-O0):
define void @print_hex(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
store i32 15, i32* %4
br label %5
5: ; preds = %28, %1
%6 = load i32, i32* %4
%7 = icmp sge i32 %6, 0
br i1 %7, label %8, label %31
8: ; preds = %5
%9 = load i32, i32* %2
%10 = load i32, i32* %4
%11 = mul nsw i32 %10, 4
%12 = ashr i32 %9, %11
%13 = and i32 %12, 15
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 10
br i1 %15, label %16, label %21
16: ; preds = %8
%17 = load i32, i32* %3
%18 = add nsw i32 48, %17
%19 = trunc i32 %18 to i8
%20 = call i32 @print_char(i8 signext %19)
br label %27
21: ; preds = %8
%22 = load i32, i32* %3
%23 = add nsw i32 97, %22
%24 = sub nsw i32 %23, 10
%25 = trunc i32 %24 to i8
%26 = call i32 @print_char(i8 signext %25)
br label %27
27: ; preds = %21, %16
br label %28
28: ; preds = %27
%29 = load i32, i32* %4
%30 = add nsw i32 %29, -1
store i32 %30, i32* %4
br label %5
31: ; preds = %5
%32 = call i32 (...) @update_cursor()
ret void
}

LLVM IR (-O2):
define void @print_hex(i32 %0) {
br label %4
2: ; preds = %4
%3 = tail call i32 (...) @update_cursor()
ret void
4: ; preds = %1, %4
%5 = phi i32 [ 15, %1 ], [ %15, %4 ]
%6 = shl nsw i32 %5, 2
%7 = ashr i32 %0, %6
%8 = and i32 %7, 15
%9 = icmp ult i32 %8, 10
%10 = trunc i32 %8 to i8
%11 = or i8 %10, 48
%12 = add nuw nsw i8 %10, 87
%13 = select i1 %9, i8 %11, i8 %12
%14 = tail call i32 @print_char(i8 signext %13)
%15 = add nsw i32 %5, -1
%16 = icmp eq i32 %5, 0
br i1 %16, label %2, label %4
}