#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 */
void ctohs(char in, char *buf) {
  const char *hexVals = "0123456789ABCDEF";
  size_t sz = sizeof(char);
  for (size_t x = 0; x < sz * 2; x++) {
    size_t val = in & 0xF;
    buf[sz * 2 - x - 1] = hexVals[val];
    in = in >> 4;
  }
}


x86 assembly (clang, -O0):
<ctohs>:
push   %rbp
mov    %rsp,%rbp
mov    %dil,%al
mov    %al,-0x1(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xeeb(%rip),%rax
mov    %rax,-0x18(%rbp)
movq   $0x1,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x1,%rcx
cmp    %rcx,%rax
jae    1188 <ctohs+0x88>
movsbl -0x1(%rbp),%eax
and    $0xf,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x1,%rcx
sub    -0x28(%rbp),%rcx
sub    $0x1,%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x1(%rbp),%eax
sar    $0x4,%eax
mov    %al,-0x1(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1129 <ctohs+0x29>
pop    %rbp
ret

x86 assembly (clang, -O2):
<ctohs>:
movzbl %dil,%eax
and    $0xf,%edi
lea    0xef2(%rip),%rcx
mov    (%rdi,%rcx,1),%dl
mov    %dl,0x1(%rsi)
shr    $0x4,%rax
mov    (%rax,%rcx,1),%al
mov    %al,(%rsi)
ret

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

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789ABCDEF\00"
define void @ctohs(i8 signext %0, i8* nocapture writeonly %1) {
%3 = and i8 %0, 15
%4 = zext i8 %3 to i64
%5 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %4
%6 = load i8, i8* %5
%7 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %6, i8* %7
%8 = lshr i8 %0, 4
%9 = zext i8 %8 to i64
%10 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %9
%11 = load i8, i8* %10
store i8 %11, i8* %1
ret void
}