#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*/ kputc(char);
void kphex(int n, unsigned int len, char pad) {
  char hex[] = "0123456789abcdef";
  int d;
  int nibble;
  int lz = 1;
  int slen;
  slen = len < 3 ? 3 : len;
  for (d = slen; d >= 0; d--) {
    nibble = (n >> (d << 2)) & 0x000fU;
    if (nibble)
      lz = 0;
    if (lz && d) {
      if (d < len)
        kputc(pad);
    } else {
      kputc(hex[nibble]);
    }
  }
}


x86 assembly (clang, -O0):
<kphex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dl,%al
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    0xed6(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xed3(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xed1(%rip),%al
mov    %al,-0x10(%rbp)
movl   $0x1,-0x2c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jae    1160 <kphex+0x50>
mov    $0x3,%eax
mov    %eax,-0x34(%rbp)
jmp    1166 <kphex+0x56>
mov    -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     11ed <kphex+0xdd>
mov    -0x4(%rbp),%eax
mov    -0x24(%rbp),%ecx
shl    $0x2,%ecx
sar    %cl,%eax
and    $0xf,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     119e <kphex+0x8e>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11cc <kphex+0xbc>
cmpl   $0x0,-0x24(%rbp)
je     11cc <kphex+0xbc>
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    11c7 <kphex+0xb7>
movsbl -0x9(%rbp),%edi
call   1030 <kputc@plt>
jmp    11da <kphex+0xca>
movslq -0x28(%rbp),%rax
movsbl -0x20(%rbp,%rax,1),%edi
call   1030 <kputc@plt>
jmp    11df <kphex+0xcf>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    1172 <kphex+0x62>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kphex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,0x4(%rsp)
cmp    $0x4,%esi
mov    $0x3,%ebx
cmovae %esi,%ebx
test   %ebx,%ebx
js     1192 <kphex+0x82>
mov    %esi,%r15d
mov    %edi,%r12d
lea    0x0(,%rbx,4),%ebp
mov    $0x1,%r13d
xor    %r14d,%r14d
jmp    116f <kphex+0x5f>
cs nopw 0x0(%rax,%rax,1)
lea    0xea9(%rip),%rcx
movzbl (%rax,%rcx,1),%eax
movsbl %al,%edi
call   1030 <kputc@plt>
lea    -0x1(%rbx),%eax
add    $0xfffffffc,%ebp
test   %ebx,%ebx
mov    %eax,%ebx
jle    1192 <kphex+0x82>
mov    %r12d,%eax
mov    %ebp,%ecx
sar    %cl,%eax
and    $0xf,%rax
cmovne %r14d,%r13d
test   %r13d,%r13d
je     1150 <kphex+0x40>
test   %ebx,%ebx
je     1150 <kphex+0x40>
mov    0x4(%rsp),%eax
cmp    %r15d,%ebx
jb     115b <kphex+0x4b>
jmp    1163 <kphex+0x53>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.kphex.hex = [17 x i8] c"0123456789abcdef\00"
define void @kphex(i32 %0, i32 %1, i8 signext %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca [17 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8 %2, i8* %6
%12 = bitcast [17 x i8]* %7 to i8*
store i32 1, i32* %10
%13 = load i32, i32* %5
%14 = icmp ult i32 %13, 3
br i1 %14, label %15, label %16
15: ; preds = %3
br label %18
16: ; preds = %3
%17 = load i32, i32* %5
br label %18
18: ; preds = %16, %15
%19 = phi i32 [ 3, %15 ], [ %17, %16 ]
store i32 %19, i32* %11
%20 = load i32, i32* %11
store i32 %20, i32* %8
br label %21
21: ; preds = %54, %18
%22 = load i32, i32* %8
%23 = icmp sge i32 %22, 0
br i1 %23, label %24, label %57
24: ; preds = %21
%25 = load i32, i32* %4
%26 = load i32, i32* %8
%27 = shl i32 %26, 2
%28 = ashr i32 %25, %27
%29 = and i32 %28, 15
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %24
store i32 0, i32* %10
br label %33
33: ; preds = %32, %24
%34 = load i32, i32* %10
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %47
36: ; preds = %33
%37 = load i32, i32* %8
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %36
%40 = load i32, i32* %8
%41 = load i32, i32* %5
%42 = icmp ult i32 %40, %41
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i8, i8* %6
%45 = call i32 @kputc(i8 signext %44)
br label %46
46: ; preds = %43, %39
br label %53
47: ; preds = %36, %33
%48 = load i32, i32* %9
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 %49
%51 = load i8, i8* %50
%52 = call i32 @kputc(i8 signext %51)
br label %53
53: ; preds = %47, %46
br label %54
54: ; preds = %53
%55 = load i32, i32* %8
%56 = add nsw i32 %55, -1
store i32 %56, i32* %8
br label %21
57: ; preds = %21
ret void
}

LLVM IR (-O2):
@__const.kphex.hex = [17 x i8] c"0123456789abcdef\00"
define void @kphex(i32 %0, i32 %1, i8 signext %2) {
%4 = icmp ugt i32 %1, 3
%5 = select i1 %4, i32 %1, i32 3
%6 = icmp sgt i32 %5, -1
br i1 %6, label %7, label %30
7: ; preds = %3, %27
%8 = phi i32 [ %28, %27 ], [ %5, %3 ]
%9 = phi i32 [ %14, %27 ], [ 1, %3 ]
%10 = shl i32 %8, 2
%11 = ashr i32 %0, %10
%12 = and i32 %11, 15
%13 = icmp eq i32 %12, 0
%14 = select i1 %13, i32 %9, i32 0
%15 = icmp ne i32 %14, 0
%16 = icmp ne i32 %8, 0
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %18, label %20
18: ; preds = %7
%19 = icmp ult i32 %8, %1
br i1 %19, label %24, label %27
20: ; preds = %7
%21 = zext i32 %12 to i64
%22 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.kphex.hex, i64 0, i64 %21
%23 = load i8, i8* %22
br label %24
24: ; preds = %18, %20
%25 = phi i8 [ %23, %20 ], [ %2, %18 ]
%26 = tail call i32 @kputc(i8 signext %25)
br label %27
27: ; preds = %24, %18
%28 = add nsw i32 %8, -1
%29 = icmp sgt i32 %8, 0
br i1 %29, label %7, label %30
30: ; preds = %27, %3
ret void
}