#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 ULONG_MAX;
int /*<<< orphan*/ sprintf(char *, char *, unsigned long);
char const *strhex(unsigned long number) {
  static char buf[20];
  if (sizeof number > 4 && number > 0xFFFFFFFFL) {
    sprintf(buf, number > (ULONG_MAX >> 4) ? "0x%016lX" : "0x%012lX", number);
  } else {
    sprintf(buf,
            number > 0x00FFFFFFL   ? "0x%08lX"
            : number > 0x0000FFFFL ? "0x%06lX"
            : number > 0x000000FFL ? "0x%04lX"
            : number > 0x0000000FL ? "0x%lX"
                                   : "%lu",
            number);
  }
  return buf;
}


x86 assembly (clang, -O0):
<strhex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    %rax,-0x8(%rbp)
jbe    1168 <strhex+0x58>
mov    -0x8(%rbp),%rcx
mov    0x2ea2(%rip),%rax
mov    (%rax),%eax
sar    $0x4,%eax
movslq %eax,%rdx
lea    0xec4(%rip),%rsi
lea    0xeb4(%rip),%rax
cmp    %rdx,%rcx
cmova  %rax,%rsi
mov    -0x8(%rbp),%rdx
lea    0x2ee2(%rip),%rdi
call   1030 <sprintf@plt>
jmp    1204 <strhex+0xf4>
cmpq   $0xffffff,-0x8(%rbp)
jbe    1186 <strhex+0x76>
lea    0xe95(%rip),%rax
mov    %rax,-0x10(%rbp)
jmp    11f0 <strhex+0xe0>
cmpq   $0xffff,-0x8(%rbp)
jbe    11a4 <strhex+0x94>
lea    0xe7f(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    11e8 <strhex+0xd8>
cmpq   $0xff,-0x8(%rbp)
jbe    11c2 <strhex+0xb2>
lea    0xe69(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11e0 <strhex+0xd0>
mov    -0x8(%rbp),%rdx
lea    0xe63(%rip),%rax
lea    0xe56(%rip),%rcx
cmp    $0xf,%rdx
cmova  %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    0x2e41(%rip),%rdi
call   1030 <sprintf@plt>
lea    0x2e35(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strhex>:
push   %rbx
mov    %rdi,%rdx
mov    %rdi,%rax
shr    $0x20,%rax
je     1142 <strhex+0x32>
mov    0x2eb4(%rip),%rax
mov    (%rax),%eax
sar    $0x4,%eax
cltq
cmp    %rdx,%rax
lea    0xecb(%rip),%rax
lea    0xecd(%rip),%rsi
cmovb  %rax,%rsi
jmp    118e <strhex+0x7e>
cmp    $0xffffff,%rdx
jbe    1154 <strhex+0x44>
lea    0xec0(%rip),%rsi
jmp    118e <strhex+0x7e>
cmp    $0xffff,%rdx
jbe    1166 <strhex+0x56>
lea    0xeb6(%rip),%rsi
jmp    118e <strhex+0x7e>
cmp    $0xff,%rdx
jbe    1178 <strhex+0x68>
lea    0xeac(%rip),%rsi
jmp    118e <strhex+0x7e>
cmp    $0x10,%rdx
lea    0xea7(%rip),%rax
lea    0xea6(%rip),%rsi
cmovae %rax,%rsi
lea    0x2eab(%rip),%rbx
mov    %rbx,%rdi
call   1030 <sprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@strhex.buf = internal global [20 x i8] zeroinitializer
@ULONG_MAX = global i32 0
@.str = [9 x i8] c"0x%016lX\00"
@.str.1 = [9 x i8] c"0x%012lX\00"
@.str.2 = [8 x i8] c"0x%08lX\00"
@.str.3 = [8 x i8] c"0x%06lX\00"
@.str.4 = [8 x i8] c"0x%04lX\00"
@.str.5 = [6 x i8] c"0x%lX\00"
@.str.6 = [4 x i8] c"%lu\00"
define i8* @strhex(i64 %0) {
%2 = alloca i64
store i64 %0, i64* %2
%3 = load i64, i64* %2
%4 = icmp ugt i64 %3, 4294967295
br i1 %4, label %5, label %15
5: ; preds = %1
%6 = load i64, i64* %2
%7 = load i32, i32* @ULONG_MAX
%8 = ashr i32 %7, 4
%9 = sext i32 %8 to i64
%10 = icmp ugt i64 %6, %9
%11 = zext i1 %10 to i64
%12 = select i1 %10, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0)
%13 = load i64, i64* %2
%14 = call i32 @sprintf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @strhex.buf, i64 0, i64 0), i8* %12, i64 %13)
br label %40
15: ; preds = %1
%16 = load i64, i64* %2
%17 = icmp ugt i64 %16, 16777215
br i1 %17, label %18, label %19
18: ; preds = %15
br label %36
19: ; preds = %15
%20 = load i64, i64* %2
%21 = icmp ugt i64 %20, 65535
br i1 %21, label %22, label %23
22: ; preds = %19
br label %34
23: ; preds = %19
%24 = load i64, i64* %2
%25 = icmp ugt i64 %24, 255
br i1 %25, label %26, label %27
26: ; preds = %23
br label %32
27: ; preds = %23
%28 = load i64, i64* %2
%29 = icmp ugt i64 %28, 15
%30 = zext i1 %29 to i64
%31 = select i1 %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0)
br label %32
32: ; preds = %27, %26
%33 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), %26 ], [ %31, %27 ]
br label %34
34: ; preds = %32, %22
%35 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), %22 ], [ %33, %32 ]
br label %36
36: ; preds = %34, %18
%37 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), %18 ], [ %35, %34 ]
%38 = load i64, i64* %2
%39 = call i32 @sprintf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @strhex.buf, i64 0, i64 0), i8* %37, i64 %38)
br label %40
40: ; preds = %36, %5
ret i8* getelementptr inbounds ([20 x i8], [20 x i8]* @strhex.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@strhex.buf = internal global [20 x i8] zeroinitializer
@ULONG_MAX = global i32 0
@.str = [9 x i8] c"0x%016lX\00"
@.str.1 = [9 x i8] c"0x%012lX\00"
@.str.2 = [8 x i8] c"0x%08lX\00"
@.str.3 = [8 x i8] c"0x%06lX\00"
@.str.4 = [8 x i8] c"0x%04lX\00"
@.str.5 = [6 x i8] c"0x%lX\00"
@.str.6 = [4 x i8] c"%lu\00"
define i8* @strhex(i64 %0) {
%2 = icmp ugt i64 %0, 4294967295
br i1 %2, label %3, label %9
3: ; preds = %1
%4 = load i32, i32* @ULONG_MAX
%5 = ashr i32 %4, 4
%6 = sext i32 %5 to i64
%7 = icmp ult i64 %6, %0
%8 = select i1 %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0)
br label %18
9: ; preds = %1
%10 = icmp ugt i64 %0, 16777215
br i1 %10, label %18, label %11
11: ; preds = %9
%12 = icmp ugt i64 %0, 65535
br i1 %12, label %18, label %13
13: ; preds = %11
%14 = icmp ugt i64 %0, 255
br i1 %14, label %18, label %15
15: ; preds = %13
%16 = icmp ugt i64 %0, 15
%17 = select i1 %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0)
br label %18
18: ; preds = %9, %15, %13, %11, %3
%19 = phi i8* [ %8, %3 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), %9 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), %11 ], [ %17, %15 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), %13 ]
%20 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @strhex.buf, i64 0, i64 0), i8* dereferenceable(1) %19, i64 %0)
ret i8* getelementptr inbounds ([20 x i8], [20 x i8]* @strhex.buf, i64 0, i64 0)
}