#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 uint32_t;

/* Variables and functions */
int itoa_hex(uint32_t x, char *buffer) {
  static const char hex_tbl[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  char *w = buffer + 8;
  *w = '\0';
  do {
    *--w = hex_tbl[x & 0x0f];
    x >>= 4;
  } while (w > buffer);
  return 8;
}


x86 assembly (clang, -O0):
<itoa_hex>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x4(%rbp),%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0xed2(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,-0x1(%rax)
mov    -0x4(%rbp),%eax
sar    $0x4,%eax
mov    %eax,-0x4(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
ja     111e <itoa_hex+0x1e>
mov    $0x8,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<itoa_hex>:
movb   $0x0,0x8(%rsi)
mov    %edi,%eax
mov    %eax,%edx
and    $0xf,%edx
lea    0xeee(%rip),%rcx
mov    (%rdx,%rcx,1),%dl
mov    %dl,0x7(%rsi)
mov    %edi,%edx
shr    $0x4,%edx
and    $0xf,%edx
mov    (%rdx,%rcx,1),%dl
mov    %dl,0x6(%rsi)
mov    %edi,%edx
shr    $0x8,%edx
and    $0xf,%edx
mov    (%rdx,%rcx,1),%dl
mov    %dl,0x5(%rsi)
mov    %edi,%edx
shr    $0xc,%edx
and    $0xf,%edx
mov    (%rdx,%rcx,1),%dl
mov    %dl,0x4(%rsi)
mov    %edi,%edx
shr    $0x10,%edx
and    $0xf,%edx
mov    (%rdx,%rcx,1),%dl
mov    %dl,0x3(%rsi)
mov    %edi,%edx
shr    $0x14,%edx
and    $0xf,%edx
mov    (%rdx,%rcx,1),%dl
mov    %dl,0x2(%rsi)
shr    $0x18,%edi
and    $0xf,%edi
mov    (%rdi,%rcx,1),%dl
mov    %dl,0x1(%rsi)
shr    $0x1c,%rax
mov    (%rax,%rcx,1),%al
mov    %al,(%rsi)
mov    $0x8,%eax
ret

LLVM IR (-O0):
@itoa_hex.hex_tbl = internal constant [16 x i8] c"0123456789abcdef"
define i32 @itoa_hex(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
%7 = getelementptr inbounds i8, i8* %6, i64 8
store i8* %7, i8** %5
%8 = load i8*, i8** %5
store i8 0, i8* %8
br label %9
9: ; preds = %19, %2
%10 = load i32, i32* %3
%11 = and i32 %10, 15
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [16 x i8], [16 x i8]* @itoa_hex.hex_tbl, i64 0, i64 %12
%14 = load i8, i8* %13
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i32 -1
store i8* %16, i8** %5
store i8 %14, i8* %16
%17 = load i32, i32* %3
%18 = ashr i32 %17, 4
store i32 %18, i32* %3
br label %19
19: ; preds = %9
%20 = load i8*, i8** %5
%21 = load i8*, i8** %4
%22 = icmp ugt i8* %20, %21
br i1 %22, label %9, label %23
23: ; preds = %19
ret i32 8
}

LLVM IR (-O2):
@itoa_hex.hex_tbl = internal unnamed_addr constant [16 x i8] c"0123456789abcdef"
define i32 @itoa_hex(i32 %0, i8* writeonly %1) {
%3 = getelementptr inbounds i8, i8* %1, i64 8
store i8 0, i8* %3
%4 = and i32 %0, 15
%5 = zext i32 %4 to i64
%6 = getelementptr inbounds [16 x i8], [16 x i8]* @itoa_hex.hex_tbl, i64 0, i64 %5
%7 = load i8, i8* %6
%8 = getelementptr inbounds i8, i8* %1, i64 7
store i8 %7, i8* %8
%9 = lshr i32 %0, 4
%10 = and i32 %9, 15
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds [16 x i8], [16 x i8]* @itoa_hex.hex_tbl, i64 0, i64 %11
%13 = load i8, i8* %12
%14 = getelementptr inbounds i8, i8* %1, i64 6
store i8 %13, i8* %14
%15 = lshr i32 %0, 8
%16 = and i32 %15, 15
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds [16 x i8], [16 x i8]* @itoa_hex.hex_tbl, i64 0, i64 %17
%19 = load i8, i8* %18
%20 = getelementptr inbounds i8, i8* %1, i64 5
store i8 %19, i8* %20
%21 = lshr i32 %0, 12
%22 = and i32 %21, 15
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds [16 x i8], [16 x i8]* @itoa_hex.hex_tbl, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = getelementptr inbounds i8, i8* %1, i64 4
store i8 %25, i8* %26
%27 = lshr i32 %0, 16
%28 = and i32 %27, 15
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds [16 x i8], [16 x i8]* @itoa_hex.hex_tbl, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %31, i8* %32
%33 = lshr i32 %0, 20
%34 = and i32 %33, 15
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds [16 x i8], [16 x i8]* @itoa_hex.hex_tbl, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %37, i8* %38
%39 = lshr i32 %0, 24
%40 = and i32 %39, 15
%41 = zext i32 %40 to i64
%42 = getelementptr inbounds [16 x i8], [16 x i8]* @itoa_hex.hex_tbl, i64 0, i64 %41
%43 = load i8, i8* %42
%44 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %43, i8* %44
%45 = lshr i32 %0, 28
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds [16 x i8], [16 x i8]* @itoa_hex.hex_tbl, i64 0, i64 %46
%48 = load i8, i8* %47
store i8 %48, i8* %1
ret i32 8
}