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

/* Variables and functions */
void puthex(uint16_t word, char **posx) {
  const char *digits = "0123456789abcdef";
  if (word >= 0x1000)
    *(*posx)++ = digits[(word >> 12) & 0xf];
  if (word >= 0x100)
    *(*posx)++ = digits[(word >> 8) & 0xf];
  if (word >= 0x10)
    *(*posx)++ = digits[(word >> 4) & 0xf];
  *(*posx)++ = digits[word & 0xf];
}


x86 assembly (clang, -O0):
<puthex>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xeee(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x1000,-0x4(%rbp)
jl     1149 <puthex+0x49>
mov    -0x18(%rbp),%rax
mov    -0x4(%rbp),%ecx
sar    $0xc,%ecx
and    $0xf,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x10(%rbp),%rdx
mov    (%rdx),%rax
mov    %rax,%rsi
add    $0x1,%rsi
mov    %rsi,(%rdx)
mov    %cl,(%rax)
cmpl   $0x100,-0x4(%rbp)
jl     117c <puthex+0x7c>
mov    -0x18(%rbp),%rax
mov    -0x4(%rbp),%ecx
sar    $0x8,%ecx
and    $0xf,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x10(%rbp),%rdx
mov    (%rdx),%rax
mov    %rax,%rsi
add    $0x1,%rsi
mov    %rsi,(%rdx)
mov    %cl,(%rax)
cmpl   $0x10,-0x4(%rbp)
jl     11ac <puthex+0xac>
mov    -0x18(%rbp),%rax
mov    -0x4(%rbp),%ecx
sar    $0x4,%ecx
and    $0xf,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x10(%rbp),%rdx
mov    (%rdx),%rax
mov    %rax,%rsi
add    $0x1,%rsi
mov    %rsi,(%rdx)
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
mov    -0x4(%rbp),%ecx
and    $0xf,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x10(%rbp),%rdx
mov    (%rdx),%rax
mov    %rax,%rsi
add    $0x1,%rsi
mov    %rsi,(%rdx)
mov    %cl,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<puthex>:
cmp    $0x1000,%edi
jl     1128 <puthex+0x28>
mov    %edi,%eax
shr    $0xc,%eax
and    $0xf,%eax
lea    0xee9(%rip),%rcx
mov    (%rax,%rcx,1),%al
mov    (%rsi),%rcx
lea    0x1(%rcx),%rdx
mov    %rdx,(%rsi)
mov    %al,(%rcx)
jmp    1130 <puthex+0x30>
cmp    $0x100,%edi
jl     1186 <puthex+0x86>
mov    %edi,%eax
shr    $0x8,%eax
and    $0xf,%eax
lea    0xec1(%rip),%rcx
mov    (%rax,%rcx,1),%al
mov    (%rsi),%rcx
lea    0x1(%rcx),%rdx
mov    %rdx,(%rsi)
mov    %al,(%rcx)
mov    %edi,%eax
shr    $0x4,%eax
and    $0xf,%eax
lea    0xea3(%rip),%rcx
mov    (%rax,%rcx,1),%al
mov    (%rsi),%rcx
lea    0x1(%rcx),%rdx
mov    %rdx,(%rsi)
mov    %al,(%rcx)
and    $0xf,%edi
lea    0xe8a(%rip),%rax
mov    (%rdi,%rax,1),%al
mov    (%rsi),%rcx
lea    0x1(%rcx),%rdx
mov    %rdx,(%rsi)
mov    %al,(%rcx)
ret
cmp    $0x10,%edi
jge    114e <puthex+0x4e>
jmp    116c <puthex+0x6c>

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

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789abcdef\00"
define void @puthex(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 4095
br i1 %3, label %4, label %12
4: ; preds = %2
%5 = lshr i32 %0, 12
%6 = and i32 %5, 15
%7 = zext i32 %6 to i64
%8 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %7
%9 = load i8, i8* %8
%10 = load i8*, i8** %1
%11 = getelementptr inbounds i8, i8* %10, i64 1
store i8* %11, i8** %1
store i8 %9, i8* %10
br label %14
12: ; preds = %2
%13 = icmp sgt i32 %0, 255
br i1 %13, label %14, label %22
14: ; preds = %12, %4
%15 = lshr i32 %0, 8
%16 = and i32 %15, 15
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %17
%19 = load i8, i8* %18
%20 = load i8*, i8** %1
%21 = getelementptr inbounds i8, i8* %20, i64 1
store i8* %21, i8** %1
store i8 %19, i8* %20
br label %24
22: ; preds = %12
%23 = icmp sgt i32 %0, 15
br i1 %23, label %24, label %32
24: ; preds = %14, %22
%25 = lshr i32 %0, 4
%26 = and i32 %25, 15
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = load i8*, i8** %1
%31 = getelementptr inbounds i8, i8* %30, i64 1
store i8* %31, i8** %1
store i8 %29, i8* %30
br label %32
32: ; preds = %24, %22
%33 = and i32 %0, 15
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = load i8*, i8** %1
%38 = getelementptr inbounds i8, i8* %37, i64 1
store i8* %38, i8** %1
store i8 %36, i8* %37
ret void
}