#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 */
struct riku_devfs_node {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ x86vga_putch(struct riku_devfs_node *, int);
int /*<<< orphan*/ x86vga_puts(struct riku_devfs_node *, char *, int);
void x86vga_puthex(struct riku_devfs_node *self, uintptr_t n) {
  int tmp;
  x86vga_puts(self, "0x", 2);
  char noZeroes = 1;
  int i;
  for (i = 60; i > 0; i -= 4) {
    tmp = (n >> i) & 0xF;
    if (tmp == 0 && noZeroes != 0) {
      continue;
    }
    if (tmp >= 0xA) {
      noZeroes = 0;
      x86vga_putch(self, tmp - 0xA + 'A');
    } else {
      noZeroes = 0;
      x86vga_putch(self, tmp + '0');
    }
  }
  tmp = n & 0xF;
  if (tmp >= 0xA) {
    x86vga_putch(self, tmp - 0xA + 'A');
  } else {
    x86vga_putch(self, tmp + '0');
  }
}


x86 assembly (clang, -O0):
<x86vga_puthex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xec5(%rip),%rsi
mov    $0x2,%edx
call   1040 <x86vga_puts@plt>
movb   $0x1,-0x15(%rbp)
movl   $0x3c,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11d2 <x86vga_puthex+0xb2>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shr    %cl,%rax
and    $0xf,%rax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1187 <x86vga_puthex+0x67>
movsbl -0x15(%rbp),%eax
cmp    $0x0,%eax
je     1187 <x86vga_puthex+0x67>
jmp    11c4 <x86vga_puthex+0xa4>
cmpl   $0xa,-0x14(%rbp)
jl     11ac <x86vga_puthex+0x8c>
movb   $0x0,-0x15(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
sub    $0xa,%esi
add    $0x41,%esi
call   1030 <x86vga_putch@plt>
jmp    11bf <x86vga_puthex+0x9f>
movb   $0x0,-0x15(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x30,%esi
call   1030 <x86vga_putch@plt>
jmp    11c4 <x86vga_puthex+0xa4>
mov    -0x1c(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x1c(%rbp)
jmp    1150 <x86vga_puthex+0x30>
mov    -0x10(%rbp),%rax
and    $0xf,%rax
mov    %eax,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jl     11fe <x86vga_puthex+0xde>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
sub    $0xa,%esi
add    $0x41,%esi
call   1030 <x86vga_putch@plt>
jmp    120d <x86vga_puthex+0xed>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x30,%esi
call   1030 <x86vga_putch@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<x86vga_puthex>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xece(%rip),%rsi
mov    $0x2,%edx
call   1040 <x86vga_puts@plt>
mov    $0x1,%al
mov    $0x40,%ebx
jmp    1172 <x86vga_puthex+0x52>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x37(%rdx),%eax
mov    %edx,%esi
or     $0x30,%esi
cmp    $0xa,%edx
cmovae %eax,%esi
mov    %r14,%rdi
call   1030 <x86vga_putch@plt>
xor    %eax,%eax
add    $0xfffffffffffffffc,%rbx
cmp    $0x4,%rbx
jbe    1186 <x86vga_puthex+0x66>
lea    -0x4(%rbx),%ecx
mov    %r15,%rdx
shr    %cl,%rdx
and    $0xf,%edx
jne    1150 <x86vga_puthex+0x30>
test   %al,%al
jne    1168 <x86vga_puthex+0x48>
jmp    1150 <x86vga_puthex+0x30>
and    $0xf,%r15d
lea    0x30(%r15),%eax
lea    0x37(%r15),%esi
cmp    $0xa,%r15d
cmovb  %eax,%esi
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <x86vga_putch@plt>

LLVM IR (-O0):
%struct.riku_devfs_node = type { i32 }
@.str = [3 x i8] c"0x\00"
define void @x86vga_puthex(%struct.riku_devfs_node* %0, i64 %1) {
%3 = alloca %struct.riku_devfs_node*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8
%7 = alloca i32
store %struct.riku_devfs_node* %0, %struct.riku_devfs_node** %3
store i64 %1, i64* %4
%8 = load %struct.riku_devfs_node*, %struct.riku_devfs_node** %3
%9 = call i32 @x86vga_puts(%struct.riku_devfs_node* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
store i8 1, i8* %6
store i32 60, i32* %7
br label %10
10: ; preds = %42, %2
%11 = load i32, i32* %7
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %45
13: ; preds = %10
%14 = load i64, i64* %4
%15 = load i32, i32* %7
%16 = zext i32 %15 to i64
%17 = lshr i64 %14, %16
%18 = and i64 %17, 15
%19 = trunc i64 %18 to i32
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %13
%23 = load i8, i8* %6
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
br label %42
27: ; preds = %22, %13
%28 = load i32, i32* %5
%29 = icmp sge i32 %28, 10
br i1 %29, label %30, label %36
30: ; preds = %27
store i8 0, i8* %6
%31 = load %struct.riku_devfs_node*, %struct.riku_devfs_node** %3
%32 = load i32, i32* %5
%33 = sub nsw i32 %32, 10
%34 = add nsw i32 %33, 65
%35 = call i32 @x86vga_putch(%struct.riku_devfs_node* %31, i32 %34)
br label %41
36: ; preds = %27
store i8 0, i8* %6
%37 = load %struct.riku_devfs_node*, %struct.riku_devfs_node** %3
%38 = load i32, i32* %5
%39 = add nsw i32 %38, 48
%40 = call i32 @x86vga_putch(%struct.riku_devfs_node* %37, i32 %39)
br label %41
41: ; preds = %36, %30
br label %42
42: ; preds = %41, %26
%43 = load i32, i32* %7
%44 = sub nsw i32 %43, 4
store i32 %44, i32* %7
br label %10
45: ; preds = %10
%46 = load i64, i64* %4
%47 = and i64 %46, 15
%48 = trunc i64 %47 to i32
store i32 %48, i32* %5
%49 = load i32, i32* %5
%50 = icmp sge i32 %49, 10
br i1 %50, label %51, label %57
51: ; preds = %45
%52 = load %struct.riku_devfs_node*, %struct.riku_devfs_node** %3
%53 = load i32, i32* %5
%54 = sub nsw i32 %53, 10
%55 = add nsw i32 %54, 65
%56 = call i32 @x86vga_putch(%struct.riku_devfs_node* %52, i32 %55)
br label %62
57: ; preds = %45
%58 = load %struct.riku_devfs_node*, %struct.riku_devfs_node** %3
%59 = load i32, i32* %5
%60 = add nsw i32 %59, 48
%61 = call i32 @x86vga_putch(%struct.riku_devfs_node* %58, i32 %60)
br label %62
62: ; preds = %57, %51
ret void
}

LLVM IR (-O2):
%struct.riku_devfs_node = type { i32 }
@.str = [3 x i8] c"0x\00"
define void @x86vga_puthex(%struct.riku_devfs_node* %0, i64 %1) {
%3 = tail call i32 @x86vga_puts(%struct.riku_devfs_node* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
br label %4
4: ; preds = %2, %19
%5 = phi i64 [ 60, %2 ], [ %21, %19 ]
%6 = phi i8 [ 1, %2 ], [ %20, %19 ]
%7 = lshr i64 %1, %5
%8 = trunc i64 %7 to i32
%9 = and i32 %8, 15
%10 = icmp eq i32 %9, 0
%11 = icmp ne i8 %6, 0
%12 = select i1 %10, i1 %11, i1 false
br i1 %12, label %19, label %13
13: ; preds = %4
%14 = icmp ugt i32 %9, 9
%15 = add nuw nsw i32 %9, 55
%16 = or i32 %9, 48
%17 = select i1 %14, i32 %15, i32 %16
%18 = tail call i32 @x86vga_putch(%struct.riku_devfs_node* %0, i32 %17)
br label %19
19: ; preds = %13, %4
%20 = phi i8 [ %6, %4 ], [ 0, %13 ]
%21 = add nsw i64 %5, -4
%22 = icmp ugt i64 %5, 4
br i1 %22, label %4, label %23
23: ; preds = %19
%24 = trunc i64 %1 to i32
%25 = and i32 %24, 15
%26 = icmp ugt i32 %25, 9
%27 = or i32 %25, 48
%28 = add nuw nsw i32 %25, 55
%29 = select i1 %26, i32 %28, i32 %27
%30 = tail call i32 @x86vga_putch(%struct.riku_devfs_node* %0, i32 %29)
ret void
}