#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 /*<<< orphan*/ hex;

/* Variables and functions */
int /*<<< orphan*/ kprint(char *);
int /*<<< orphan*/ putchar(char);
void print_hex(int hex) {
  kprint("0x");
  for (int i = 2 * sizeof(hex) - 1; i >= 0; i--) {
    putchar("0123456789abcdef"[((hex >> i * 4) & 0x0F)]);
  }
}


x86 assembly (clang, -O0):
<print_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    0xece(%rip),%rdi
call   1040 <kprint@plt>
movl   $0x7,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1177 <print_hex+0x57>
mov    -0x4(%rbp),%eax
mov    -0x8(%rbp),%ecx
shl    $0x2,%ecx
sar    %cl,%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0xea3(%rip),%rax
movsbl (%rax,%rcx,1),%edi
call   1030 <putchar@plt>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    113e <print_hex+0x1e>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_hex>:
push   %r15
push   %r14
push   %rbx
mov    %edi,%ebx
lea    0xed2(%rip),%rdi
call   1040 <kprint@plt>
mov    %ebx,%r14d
mov    %r14,%rax
shr    $0x1c,%rax
lea    0xebf(%rip),%r15
movsbl (%rax,%r15,1),%edi
call   1030 <putchar@plt>
mov    %ebx,%eax
shr    $0x18,%eax
and    $0xf,%eax
movsbl (%rax,%r15,1),%edi
call   1030 <putchar@plt>
mov    %ebx,%eax
shr    $0x14,%eax
and    $0xf,%eax
movsbl (%rax,%r15,1),%edi
call   1030 <putchar@plt>
mov    %ebx,%eax
shr    $0x10,%eax
and    $0xf,%eax
movsbl (%rax,%r15,1),%edi
call   1030 <putchar@plt>
mov    %ebx,%eax
shr    $0xc,%eax
and    $0xf,%eax
movsbl (%rax,%r15,1),%edi
call   1030 <putchar@plt>
mov    %ebx,%eax
shr    $0x8,%eax
and    $0xf,%eax
movsbl (%rax,%r15,1),%edi
call   1030 <putchar@plt>
shr    $0x4,%ebx
and    $0xf,%ebx
movsbl (%rbx,%r15,1),%edi
call   1030 <putchar@plt>
and    $0xf,%r14d
movsbl (%r14,%r15,1),%edi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <putchar@plt>

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

LLVM IR (-O2):
@.str = [3 x i8] c"0x\00"
@.str.1 = [17 x i8] c"0123456789abcdef\00"
define void @print_hex(i32 %0) {
%2 = tail call i32 @kprint(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = lshr i32 %0, 28
%4 = zext i32 %3 to i64
%5 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.1, i64 0, i64 %4
%6 = load i8, i8* %5
%7 = tail call i32 @putchar(i8 signext %6)
%8 = lshr i32 %0, 24
%9 = and i32 %8, 15
%10 = zext i32 %9 to i64
%11 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.1, i64 0, i64 %10
%12 = load i8, i8* %11
%13 = tail call i32 @putchar(i8 signext %12)
%14 = lshr i32 %0, 20
%15 = and i32 %14, 15
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.1, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = tail call i32 @putchar(i8 signext %18)
%20 = lshr i32 %0, 16
%21 = and i32 %20, 15
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.1, i64 0, i64 %22
%24 = load i8, i8* %23
%25 = tail call i32 @putchar(i8 signext %24)
%26 = lshr i32 %0, 12
%27 = and i32 %26, 15
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.1, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = tail call i32 @putchar(i8 signext %30)
%32 = lshr i32 %0, 8
%33 = and i32 %32, 15
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.1, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = tail call i32 @putchar(i8 signext %36)
%38 = lshr i32 %0, 4
%39 = and i32 %38, 15
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.1, i64 0, i64 %40
%42 = load i8, i8* %41
%43 = tail call i32 @putchar(i8 signext %42)
%44 = and i32 %0, 15
%45 = zext i32 %44 to i64
%46 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.1, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = tail call i32 @putchar(i8 signext %47)
ret void
}