#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 /*<<< orphan*/ buf;

/* Variables and functions */
int print_buf_pad(char *, int, char *, int, char);
int print_hex(unsigned long val, int width, char pad) {
  char buf[32];
  char *p = buf + sizeof(buf);
  if (val == 0)
    *--p = '0';
  else {
    do {
      int d = val % 16;
      *--p = (d < 10 ? '0' : 'a' - 10) + d;
      val /= 16;
    } while (val);
  }
  return print_buf_pad(buf, sizeof(buf), p, width, pad);
}


x86 assembly (clang, -O0):
<print_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %al,-0xd(%rbp)
lea    -0x30(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1153 <print_hex+0x43>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x30,-0x1(%rax)
jmp    11a9 <print_hex+0x99>
jmp    1158 <print_hex+0x48>
mov    -0x8(%rbp),%rax
and    $0xf,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edx
mov    $0x57,%eax
mov    $0x30,%ecx
cmp    $0xa,%edx
cmovl  %ecx,%eax
add    -0x3c(%rbp),%eax
mov    %al,%cl
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x38(%rbp)
mov    %cl,-0x1(%rax)
mov    -0x8(%rbp),%rax
shr    $0x4,%rax
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1158 <print_hex+0x48>
jmp    11a9 <print_hex+0x99>
lea    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0xc(%rbp),%ecx
mov    $0x20,%esi
movsbl -0xd(%rbp),%r8d
call   1030 <print_buf_pad@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_hex>:
sub    $0x28,%rsp
mov    %esi,%ecx
test   %rdi,%rdi
je     115b <print_hex+0x4b>
lea    0x20(%rsp),%r9
mov    %rdi,%rsi
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %edi,%eax
and    $0xf,%eax
lea    0x57(%rax),%r8d
lea    0x30(%rax),%r10d
cmp    $0xa,%eax
cmovae %r8d,%r10d
mov    %r10b,-0x1(%r9)
add    $0xffffffffffffffff,%r9
shr    $0x4,%rsi
cmp    $0xf,%rdi
mov    %rsi,%rdi
ja     1130 <print_hex+0x20>
jmp    1165 <print_hex+0x55>
lea    0x1f(%rsp),%r9
movb   $0x30,0x1f(%rsp)
movsbl %dl,%r8d
mov    %rsp,%rdi
mov    $0x20,%esi
mov    %r9,%rdx
call   1030 <print_buf_pad@plt>
add    $0x28,%rsp
ret

LLVM IR (-O0):
define i32 @print_hex(i64 %0, i32 %1, i8 signext %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8
%7 = alloca [32 x i8]
%8 = alloca i8*
%9 = alloca i32
store i64 %0, i64* %4
store i32 %1, i32* %5
store i8 %2, i8* %6
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%11 = getelementptr inbounds i8, i8* %10, i64 32
store i8* %11, i8** %8
%12 = load i64, i64* %4
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i8*, i8** %8
%16 = getelementptr inbounds i8, i8* %15, i32 -1
store i8* %16, i8** %8
store i8 48, i8* %16
br label %37
17: ; preds = %3
br label %18
18: ; preds = %33, %17
%19 = load i64, i64* %4
%20 = urem i64 %19, 16
%21 = trunc i64 %20 to i32
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 10
%24 = zext i1 %23 to i64
%25 = select i1 %23, i32 48, i32 87
%26 = load i32, i32* %9
%27 = add nsw i32 %25, %26
%28 = trunc i32 %27 to i8
%29 = load i8*, i8** %8
%30 = getelementptr inbounds i8, i8* %29, i32 -1
store i8* %30, i8** %8
store i8 %28, i8* %30
%31 = load i64, i64* %4
%32 = udiv i64 %31, 16
store i64 %32, i64* %4
br label %33
33: ; preds = %18
%34 = load i64, i64* %4
%35 = icmp ne i64 %34, 0
br i1 %35, label %18, label %36
36: ; preds = %33
br label %37
37: ; preds = %36, %14
%38 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%39 = load i8*, i8** %8
%40 = load i32, i32* %5
%41 = load i8, i8* %6
%42 = call i32 @print_buf_pad(i8* %38, i32 32, i8* %39, i32 %40, i8 signext %41)
ret i32 %42
}

LLVM IR (-O2):
define i32 @print_hex(i64 %0, i32 %1, i8 signext %2) {
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%6 = icmp eq i64 %0, 0
br i1 %6, label %9, label %7
7: ; preds = %3
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 32
br label %11
9: ; preds = %3
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 31
store i8 48, i8* %10
br label %23
11: ; preds = %7, %11
%12 = phi i8* [ %20, %11 ], [ %8, %7 ]
%13 = phi i64 [ %21, %11 ], [ %0, %7 ]
%14 = trunc i64 %13 to i32
%15 = and i32 %14, 15
%16 = icmp ult i32 %15, 10
%17 = select i1 %16, i32 48, i32 87
%18 = add nuw nsw i32 %17, %15
%19 = trunc i32 %18 to i8
%20 = getelementptr inbounds i8, i8* %12, i64 -1
store i8 %19, i8* %20
%21 = lshr i64 %13, 4
%22 = icmp ult i64 %13, 16
br i1 %22, label %23, label %11
23: ; preds = %11, %9
%24 = phi i8* [ %10, %9 ], [ %20, %11 ]
%25 = call i32 @print_buf_pad(i8* %5, i32 32, i8* %24, i32 %1, i8 signext %2)
ret i32 %25
}