#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*/ uart_put(char);
int /*<<< orphan*/ uart_puts(char *);
void puthex(uint64_t n) {
  int zeros = 1;
  int c;
  int i;
  uart_puts("0x");
  for (i = 15; i >= 0; i--) {
    c = (n >> (4 * i)) & 0xF;
    if (c != 0)
      zeros = 0;
    if (zeros)
      continue;
    if (c < 10)
      uart_put('0' + c);
    else
      uart_put('A' + (c - 10));
  }
  if (zeros)
    uart_put('0');
}


x86 assembly (clang, -O0):
<puthex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
lea    0xec7(%rip),%rdi
call   1040 <uart_puts@plt>
movl   $0xf,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     11c1 <puthex+0xa1>
mov    -0x4(%rbp),%eax
mov    -0x10(%rbp),%ecx
shl    $0x2,%ecx
sar    %cl,%eax
and    $0xf,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1171 <puthex+0x51>
movl   $0x0,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1180 <puthex+0x60>
jmp    11b3 <puthex+0x93>
cmpl   $0xa,-0xc(%rbp)
jge    119d <puthex+0x7d>
mov    -0xc(%rbp),%eax
add    $0x30,%eax
movsbl %al,%edi
call   1030 <uart_put@plt>
jmp    11ae <puthex+0x8e>
mov    -0xc(%rbp),%eax
sub    $0xa,%eax
add    $0x41,%eax
movsbl %al,%edi
call   1030 <uart_put@plt>
jmp    11b3 <puthex+0x93>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1145 <puthex+0x25>
cmpl   $0x0,-0x8(%rbp)
je     11d5 <puthex+0xb5>
mov    $0x30,%edi
call   1030 <uart_put@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<puthex>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
lea    0xed2(%rip),%rdi
call   1040 <uart_puts@plt>
mov    $0x1,%eax
mov    $0xf,%ecx
mov    $0x1,%ebp
sub    %ecx,%ebp
shl    $0x2,%ecx
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edx
sar    %cl,%edx
mov    %eax,%ebx
and    $0xf,%edx
je     115e <puthex+0x3e>
xor    %ebx,%ebx
test   %ebx,%ebx
je     1170 <puthex+0x50>
add    $0x1,%ebp
add    $0xfffffffc,%ecx
cmp    $0x2,%ebp
jne    1150 <puthex+0x30>
jmp    119a <puthex+0x7a>
nop
mov    %edx,%eax
or     $0x30,%al
lea    0x37(%rdx),%ecx
cmp    $0xa,%edx
movzbl %al,%eax
movzbl %cl,%ecx
cmovb  %eax,%ecx
movsbl %cl,%edi
call   1030 <uart_put@plt>
mov    %ebp,%ecx
neg    %ecx
mov    %ebx,%eax
cmp    $0x1,%ebp
jne    113d <puthex+0x1d>
test   %ebx,%ebx
je     11a8 <puthex+0x88>
mov    $0x30,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <uart_put@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"0x\00"
define void @puthex(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
store i32 1, i32* %3
%6 = call i32 @uart_puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 15, i32* %5
br label %7
7: ; preds = %38, %1
%8 = load i32, i32* %5
%9 = icmp sge i32 %8, 0
br i1 %9, label %10, label %41
10: ; preds = %7
%11 = load i32, i32* %2
%12 = load i32, i32* %5
%13 = mul nsw i32 4, %12
%14 = ashr i32 %11, %13
%15 = and i32 %14, 15
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %10
store i32 0, i32* %3
br label %19
19: ; preds = %18, %10
%20 = load i32, i32* %3
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19
br label %38
23: ; preds = %19
%24 = load i32, i32* %4
%25 = icmp slt i32 %24, 10
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i32, i32* %4
%28 = add nsw i32 48, %27
%29 = trunc i32 %28 to i8
%30 = call i32 @uart_put(i8 signext %29)
br label %37
31: ; preds = %23
%32 = load i32, i32* %4
%33 = sub nsw i32 %32, 10
%34 = add nsw i32 65, %33
%35 = trunc i32 %34 to i8
%36 = call i32 @uart_put(i8 signext %35)
br label %37
37: ; preds = %31, %26
br label %38
38: ; preds = %37, %22
%39 = load i32, i32* %5
%40 = add nsw i32 %39, -1
store i32 %40, i32* %5
br label %7
41: ; preds = %7
%42 = load i32, i32* %3
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 @uart_put(i8 signext 48)
br label %46
46: ; preds = %44, %41
ret void
}

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