#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 */
char *format_char(char *, char *, char const);
char *format_hex(char *buf, char *end, unsigned val, int ndigits) {
  int shift = 4 * ndigits;
  static const char hex[16] = "0123456789abcdef";
  while (shift >= 0) {
    buf = format_char(buf, end, hex[(val >> shift) & 15]);
    shift -= 4;
  }
  return buf;
}


x86 assembly (clang, -O0):
<format_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x2,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1172 <format_hex+0x62>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%eax
mov    -0x1c(%rbp),%ecx
shr    %cl,%eax
and    $0xf,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0xea9(%rip),%rax
movsbl (%rax,%rcx,1),%edx
call   1030 <format_char@plt>
mov    %rax,-0x8(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x1c(%rbp)
jmp    112f <format_hex+0x1f>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_hex>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rax
test   %ecx,%ecx
js     1154 <format_hex+0x44>
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %rsi,%rbp
shl    $0x2,%ebx
lea    0xed0(%rip),%r15
mov    %r14d,%edx
mov    %ebx,%ecx
shr    %cl,%edx
and    $0xf,%edx
movsbl (%rdx,%r15,1),%edx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1030 <format_char@plt>
lea    -0x4(%rbx),%ecx
cmp    $0x3,%ebx
mov    %ecx,%ebx
jg     1130 <format_hex+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@format_hex.hex = internal constant [16 x i8] c"0123456789abcdef"
define i8* @format_hex(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%10 = load i32, i32* %8
%11 = mul nsw i32 4, %10
store i32 %11, i32* %9
br label %12
12: ; preds = %15, %4
%13 = load i32, i32* %9
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %28
15: ; preds = %12
%16 = load i8*, i8** %5
%17 = load i8*, i8** %6
%18 = load i32, i32* %7
%19 = load i32, i32* %9
%20 = lshr i32 %18, %19
%21 = and i32 %20, 15
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds [16 x i8], [16 x i8]* @format_hex.hex, i64 0, i64 %22
%24 = load i8, i8* %23
%25 = call i8* @format_char(i8* %16, i8* %17, i8 signext %24)
store i8* %25, i8** %5
%26 = load i32, i32* %9
%27 = sub nsw i32 %26, 4
store i32 %27, i32* %9
br label %12
28: ; preds = %12
%29 = load i8*, i8** %5
ret i8* %29
}

LLVM IR (-O2):
@format_hex.hex = internal unnamed_addr constant [16 x i8] c"0123456789abcdef"
define i8* @format_hex(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %3, -1
br i1 %5, label %6, label %19
6: ; preds = %4
%7 = shl nsw i32 %3, 2
br label %8
8: ; preds = %6, %8
%9 = phi i32 [ %17, %8 ], [ %7, %6 ]
%10 = phi i8* [ %16, %8 ], [ %0, %6 ]
%11 = lshr i32 %2, %9
%12 = and i32 %11, 15
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds [16 x i8], [16 x i8]* @format_hex.hex, i64 0, i64 %13
%15 = load i8, i8* %14
%16 = tail call i8* @format_char(i8* %10, i8* %1, i8 signext %15)
%17 = add nsw i32 %9, -4
%18 = icmp sgt i32 %9, 3
br i1 %18, label %8, label %19
19: ; preds = %8, %4
%20 = phi i8* [ %0, %4 ], [ %16, %8 ]
ret i8* %20
}