#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 */

/* Variables and functions */
int /*<<< orphan*/ console_print(char *);
int /*<<< orphan*/ console_printchr(char);
void console_printhex(int i) {
  char buf[8];
  char *template = "0123456789ABCDEF";
  int j = i;
  int c = 0;
  console_print("0x");
  do {
    buf[c++] = template[j % 16];
    j /= 16;
  } while (j > 0);
  while (c--) {
    console_printchr(buf[c]);
  }
}


x86 assembly (clang, -O0):
<console_printhex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
lea    0xece(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    0xec7(%rip),%rdi
call   1030 <console_print@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    -0x28(%rbp),%rax
movslq %edx,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x20(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x20(%rbp)
cltq
mov    %cl,-0xc(%rbp,%rax,1)
mov    -0x1c(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     114f <console_printhex+0x2f>
jmp    119a <console_printhex+0x7a>
mov    -0x20(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x20(%rbp)
cmp    $0x0,%eax
je     11c1 <console_printhex+0xa1>
movslq -0x20(%rbp),%rax
movsbl -0xc(%rbp,%rax,1),%edi
call   1040 <console_printchr@plt>
jmp    119a <console_printhex+0x7a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<console_printhex>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
lea    0xee3(%rip),%rdi
call   1030 <console_print@plt>
movabs $0xffffffff00000000,%rbx
ff ff ff
mov    %rsp,%rax
lea    0xeb9(%rip),%r8
movabs $0x100000000,%rdx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0xf(%r14),%esi
test   %r14d,%r14d
cmovns %r14d,%esi
mov    %esi,%edi
and    $0xfffffff0,%edi
mov    %r14d,%ecx
sub    %edi,%ecx
movslq %ecx,%rcx
movzbl (%rcx,%r8,1),%ecx
mov    %cl,(%rax)
sar    $0x4,%esi
add    %rdx,%rbx
add    $0x1,%rax
cmp    $0xf,%r14d
mov    %esi,%r14d
jg     1160 <console_printhex+0x40>
sar    $0x20,%rbx
cs nopw 0x0(%rax,%rax,1)
movsbl (%rsp,%rbx,1),%edi
call   1040 <console_printchr@plt>
add    $0xffffffffffffffff,%rbx
jb     11a0 <console_printhex+0x80>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [17 x i8] c"0123456789ABCDEF\00"
@.str.1 = [3 x i8] c"0x\00"
define void @console_printhex(i32 %0) {
%2 = alloca i32
%3 = alloca [8 x i8]
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %4
%7 = load i32, i32* %2
store i32 %7, i32* %5
store i32 0, i32* %6
%8 = call i32 @console_print(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %22, %1
%10 = load i8*, i8** %4
%11 = load i32, i32* %5
%12 = srem i32 %11, 16
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %10, i64 %13
%15 = load i8, i8* %14
%16 = load i32, i32* %6
%17 = add nsw i32 %16, 1
store i32 %17, i32* %6
%18 = sext i32 %16 to i64
%19 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 %18
store i8 %15, i8* %19
%20 = load i32, i32* %5
%21 = sdiv i32 %20, 16
store i32 %21, i32* %5
br label %22
22: ; preds = %9
%23 = load i32, i32* %5
%24 = icmp sgt i32 %23, 0
br i1 %24, label %9, label %25
25: ; preds = %22
br label %26
26: ; preds = %30, %25
%27 = load i32, i32* %6
%28 = add nsw i32 %27, -1
store i32 %28, i32* %6
%29 = icmp ne i32 %27, 0
br i1 %29, label %30, label %36
30: ; preds = %26
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 %32
%34 = load i8, i8* %33
%35 = call i32 @console_printchr(i8 signext %34)
br label %26
36: ; preds = %26
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789ABCDEF\00"
@.str.1 = [3 x i8] c"0x\00"
define void @console_printhex(i32 %0) {
%2 = alloca [8 x i8]
%3 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @console_print(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %5
5: ; preds = %5, %1
%6 = phi i64 [ %12, %5 ], [ 0, %1 ]
%7 = phi i32 [ %14, %5 ], [ %0, %1 ]
%8 = srem i32 %7, 16
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %9
%11 = load i8, i8* %10
%12 = add nuw i64 %6, 1
%13 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 %6
store i8 %11, i8* %13
%14 = sdiv i32 %7, 16
%15 = icmp sgt i32 %7, 15
br i1 %15, label %5, label %16
16: ; preds = %5
%17 = shl i64 %6, 32
%18 = ashr exact i64 %17, 32
br label %19
19: ; preds = %16, %19
%20 = phi i64 [ %18, %16 ], [ %24, %19 ]
%21 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = tail call i32 @console_printchr(i8 signext %22)
%24 = add nsw i64 %20, -1
%25 = icmp eq i64 %20, 0
br i1 %25, label %26, label %19
26: ; preds = %19
ret void
}