#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 unsigned int uint8_t;

/* Variables and functions */
void ntohex(uint8_t *o, uint8_t d) {
  unsigned int rb;
  unsigned int rc;
  if (o == NULL)
    return;
  rb = 4;
  while (1) {
    rb -= 4;
    rc = (d >> rb) & 0xF;
    if (rc > 9)
      rc += 0x37;
    else
      rc += 0x30;
    *(o++) = rc;
    if (rb == 0)
      break;
  }
}


x86 assembly (clang, -O0):
<ntohex>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    111b <ntohex+0x1b>
jmp    1182 <ntohex+0x82>
movl   $0x4,-0x10(%rbp)
mov    -0x10(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x10(%rbp),%ecx
shr    %cl,%eax
and    $0xf,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x9,-0x14(%rbp)
jbe    1151 <ntohex+0x51>
mov    -0x14(%rbp),%eax
add    $0x37,%eax
mov    %eax,-0x14(%rbp)
jmp    115a <ntohex+0x5a>
mov    -0x14(%rbp),%eax
add    $0x30,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x8(%rbp)
mov    %ecx,(%rax)
cmpl   $0x0,-0x10(%rbp)
jne    117d <ntohex+0x7d>
jmp    1182 <ntohex+0x82>
jmp    1122 <ntohex+0x22>
pop    %rbp
ret

x86 assembly (clang, -O2):
<ntohex>:
test   %rdi,%rdi
je     1116 <ntohex+0x16>
and    $0xf,%esi
lea    0x37(%rsi),%eax
lea    0x30(%rsi),%ecx
cmp    $0xa,%esi
cmovae %eax,%ecx
mov    %ecx,(%rdi)
ret

LLVM IR (-O0):
define void @ntohex(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = load i32*, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %10
9: ; preds = %2
br label %34
10: ; preds = %2
store i32 4, i32* %5
br label %11
11: ; preds = %10, %33
%12 = load i32, i32* %5
%13 = sub i32 %12, 4
store i32 %13, i32* %5
%14 = load i32, i32* %4
%15 = load i32, i32* %5
%16 = lshr i32 %14, %15
%17 = and i32 %16, 15
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp ugt i32 %18, 9
br i1 %19, label %20, label %23
20: ; preds = %11
%21 = load i32, i32* %6
%22 = add i32 %21, 55
store i32 %22, i32* %6
br label %26
23: ; preds = %11
%24 = load i32, i32* %6
%25 = add i32 %24, 48
store i32 %25, i32* %6
br label %26
26: ; preds = %23, %20
%27 = load i32, i32* %6
%28 = load i32*, i32** %3
%29 = getelementptr inbounds i32, i32* %28, i32 1
store i32* %29, i32** %3
store i32 %27, i32* %28
%30 = load i32, i32* %5
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %26
br label %34
33: ; preds = %26
br label %11
34: ; preds = %9, %32
ret void
}

LLVM IR (-O2):
define void @ntohex(i32* writeonly %0, i32 %1) {
%3 = icmp eq i32* %0, null
br i1 %3, label %10, label %4
4: ; preds = %2
%5 = and i32 %1, 15
%6 = icmp ugt i32 %5, 9
%7 = add nuw nsw i32 %5, 55
%8 = or i32 %5, 48
%9 = select i1 %6, i32 %7, i32 %8
store i32 %9, i32* %0
br label %10
10: ; preds = %2, %4
ret void
}