#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 */
int write_hex16(char *out, uint16_t val) {
  const char *hex = "0123456789ABCDEF";
  *out++ = hex[(val >> 12) & 0xF];
  *out++ = hex[(val >> 8) & 0xF];
  *out++ = hex[(val >> 4) & 0xF];
  *out++ = hex[val & 0xF];
  return 4;
}


x86 assembly (clang, -O0):
<write_hex16>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xeee(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sar    $0xc,%ecx
and    $0xf,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sar    $0x8,%ecx
and    $0xf,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sar    $0x4,%ecx
and    $0xf,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
and    $0xf,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    $0x4,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_hex16>:
mov    %esi,%eax
shr    $0xc,%eax
and    $0xf,%eax
lea    0xef1(%rip),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,(%rdi)
mov    %esi,%eax
shr    $0x8,%eax
and    $0xf,%eax
mov    (%rax,%rcx,1),%al
mov    %al,0x1(%rdi)
mov    %esi,%eax
shr    $0x4,%eax
and    $0xf,%eax
mov    (%rax,%rcx,1),%al
mov    %al,0x2(%rdi)
and    $0xf,%esi
mov    (%rsi,%rcx,1),%al
mov    %al,0x3(%rdi)
mov    $0x4,%eax
ret

LLVM IR (-O0):
@.str = [17 x i8] c"0123456789ABCDEF\00"
define i32 @write_hex16(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = load i8*, i8** %5
%7 = load i32, i32* %4
%8 = ashr i32 %7, 12
%9 = and i32 %8, 15
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %6, i64 %10
%12 = load i8, i8* %11
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %3
store i8 %12, i8* %13
%15 = load i8*, i8** %5
%16 = load i32, i32* %4
%17 = ashr i32 %16, 8
%18 = and i32 %17, 15
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %15, i64 %19
%21 = load i8, i8* %20
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %3
store i8 %21, i8* %22
%24 = load i8*, i8** %5
%25 = load i32, i32* %4
%26 = ashr i32 %25, 4
%27 = and i32 %26, 15
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %24, i64 %28
%30 = load i8, i8* %29
%31 = load i8*, i8** %3
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %3
store i8 %30, i8* %31
%33 = load i8*, i8** %5
%34 = load i32, i32* %4
%35 = and i32 %34, 15
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %33, i64 %36
%38 = load i8, i8* %37
%39 = load i8*, i8** %3
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %3
store i8 %38, i8* %39
ret i32 4
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789ABCDEF\00"
define i32 @write_hex16(i8* nocapture writeonly %0, i32 %1) {
%3 = lshr i32 %1, 12
%4 = and i32 %3, 15
%5 = zext i32 %4 to i64
%6 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %5
%7 = load i8, i8* %6
%8 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %7, i8* %0
%9 = lshr i32 %1, 8
%10 = and i32 %9, 15
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %11
%13 = load i8, i8* %12
%14 = getelementptr inbounds i8, i8* %0, i64 2
store i8 %13, i8* %8
%15 = lshr i32 %1, 4
%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 = getelementptr inbounds i8, i8* %0, i64 3
store i8 %19, i8* %14
%21 = and i32 %1, 15
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %22
%24 = load i8, i8* %23
store i8 %24, i8* %20
ret i32 4
}