#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*/ putc(char);
int /*<<< orphan*/ puts(char *);
void puthex(uint64_t n) {
  int zeros = 1;
  char c;
  int i;
  puts("0x");
  for (i = 15; i >= 0; i--) {
    c = (n >> (4 * i)) & 0xF;
    if (c != 0)
      zeros = 0;
    if (zeros)
      continue;
    if (c < 10)
      putc('0' + c);
    else
      putc('A' + (c - 10));
  }
  if (zeros)
    putc('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   1030 <puts@plt>
movl   $0xf,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     11c9 <puthex+0xa9>
mov    -0x4(%rbp),%eax
mov    -0x10(%rbp),%ecx
shl    $0x2,%ecx
sar    %cl,%eax
and    $0xf,%eax
mov    %al,-0x9(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0x0,%eax
je     1174 <puthex+0x54>
movl   $0x0,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1183 <puthex+0x63>
jmp    11bb <puthex+0x9b>
movsbl -0x9(%rbp),%eax
cmp    $0xa,%eax
jge    11a4 <puthex+0x84>
movsbl -0x9(%rbp),%eax
add    $0x30,%eax
movsbl %al,%edi
call   1040 <putc@plt>
jmp    11b6 <puthex+0x96>
movsbl -0x9(%rbp),%eax
sub    $0xa,%eax
add    $0x41,%eax
movsbl %al,%edi
call   1040 <putc@plt>
jmp    11bb <puthex+0x9b>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1145 <puthex+0x25>
cmpl   $0x0,-0x8(%rbp)
je     11dd <puthex+0xbd>
mov    $0x30,%edi
call   1040 <putc@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   1030 <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   1040 <putc@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    1040 <putc@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 i8
%5 = alloca i32
store i32 %0, i32* %2
store i32 1, i32* %3
%6 = call i32 @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 = %43, %1
%8 = load i32, i32* %5
%9 = icmp sge i32 %8, 0
br i1 %9, label %10, label %46
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
%16 = trunc i32 %15 to i8
store i8 %16, i8* %4
%17 = load i8, i8* %4
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %10
store i32 0, i32* %3
br label %21
21: ; preds = %20, %10
%22 = load i32, i32* %3
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
br label %43
25: ; preds = %21
%26 = load i8, i8* %4
%27 = sext i8 %26 to i32
%28 = icmp slt i32 %27, 10
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = load i8, i8* %4
%31 = sext i8 %30 to i32
%32 = add nsw i32 48, %31
%33 = trunc i32 %32 to i8
%34 = call i32 @putc(i8 signext %33)
br label %42
35: ; preds = %25
%36 = load i8, i8* %4
%37 = sext i8 %36 to i32
%38 = sub nsw i32 %37, 10
%39 = add nsw i32 65, %38
%40 = trunc i32 %39 to i8
%41 = call i32 @putc(i8 signext %40)
br label %42
42: ; preds = %35, %29
br label %43
43: ; preds = %42, %24
%44 = load i32, i32* %5
%45 = add nsw i32 %44, -1
store i32 %45, i32* %5
br label %7
46: ; preds = %7
%47 = load i32, i32* %3
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @putc(i8 signext 48)
br label %51
51: ; preds = %49, %46
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"0x\00"
define void @puthex(i32 %0) {
%2 = tail call i32 @puts(i8* dereferenceable(1) 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 @putc(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 @putc(i8 signext 48)
br label %29
29: ; preds = %27, %26
ret void
}