#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*/ putchar(int);
int printhex(long v, int w) {
  int i;
  int c;
  for (i = 4 * (w - 1); i >= 0; i -= 4) {
    c = (v >> i) & 0xf;
    putchar((c <= 9) ? c + '0' : c - 0xa + 'a');
  }
  return 0;
}


x86 assembly (clang, -O0):
<printhex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
shl    $0x2,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     1180 <printhex+0x70>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sar    %cl,%rax
and    $0xf,%rax
mov    %eax,-0x14(%rbp)
cmpl   $0x9,-0x14(%rbp)
jg     115e <printhex+0x4e>
mov    -0x14(%rbp),%eax
add    $0x30,%eax
mov    %eax,-0x18(%rbp)
jmp    116a <printhex+0x5a>
mov    -0x14(%rbp),%eax
sub    $0xa,%eax
add    $0x61,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <putchar@plt>
mov    -0x10(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x10(%rbp)
jmp    112b <printhex+0x1b>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printhex>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
shl    $0x2,%ebx
mov    %ebx,%eax
add    $0xfffffffc,%eax
js     1154 <printhex+0x44>
mov    %rdi,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0xfffffffc,%ebx
mov    %r14,%rax
mov    %ebx,%ecx
sar    %cl,%rax
and    $0xf,%eax
lea    0x30(%rax),%ecx
lea    0x57(%rax),%edi
cmp    $0xa,%eax
cmovb  %ecx,%edi
call   1030 <putchar@plt>
cmp    $0x3,%ebx
jg     1130 <printhex+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @printhex(i64 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i64 %0, i64* %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = sub nsw i32 %7, 1
%9 = mul nsw i32 4, %8
store i32 %9, i32* %5
br label %10
10: ; preds = %32, %2
%11 = load i32, i32* %5
%12 = icmp sge i32 %11, 0
br i1 %12, label %13, label %35
13: ; preds = %10
%14 = load i64, i64* %3
%15 = load i32, i32* %5
%16 = zext i32 %15 to i64
%17 = ashr i64 %14, %16
%18 = and i64 %17, 15
%19 = trunc i64 %18 to i32
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp sle i32 %20, 9
br i1 %21, label %22, label %25
22: ; preds = %13
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 48
br label %29
25: ; preds = %13
%26 = load i32, i32* %6
%27 = sub nsw i32 %26, 10
%28 = add nsw i32 %27, 97
br label %29
29: ; preds = %25, %22
%30 = phi i32 [ %24, %22 ], [ %28, %25 ]
%31 = call i32 @putchar(i32 %30)
br label %32
32: ; preds = %29
%33 = load i32, i32* %5
%34 = sub nsw i32 %33, 4
store i32 %34, i32* %5
br label %10
35: ; preds = %10
ret i32 0
}

LLVM IR (-O2):
define i32 @printhex(i64 %0, i32 %1) {
%3 = shl i32 %1, 2
%4 = add i32 %3, -4
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %19
6: ; preds = %2, %6
%7 = phi i32 [ %17, %6 ], [ %4, %2 ]
%8 = zext i32 %7 to i64
%9 = ashr i64 %0, %8
%10 = trunc i64 %9 to i32
%11 = and i32 %10, 15
%12 = icmp ult i32 %11, 10
%13 = or i32 %11, 48
%14 = add nuw nsw i32 %11, 87
%15 = select i1 %12, i32 %13, i32 %14
%16 = tail call i32 @putchar(i32 %15)
%17 = add nsw i32 %7, -4
%18 = icmp sgt i32 %7, 3
br i1 %18, label %6, label %19
19: ; preds = %6, %2
ret i32 0
}