#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 */
unsigned char conv_to_upper_case(unsigned char);
unsigned char ascii_to_hex_nib(unsigned char ch) {
  ch = conv_to_upper_case(ch);
  if (('0' <= ch) && (ch <= '9')) {
    return (ch - '0');
  } else if (('A' <= ch) && (ch <= 'F')) {
    return (ch - 'A' + 10);
  } else {
    return 0xff;
  }
}


x86 assembly (clang, -O0):
<ascii_to_hex_nib>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %dil,%al
mov    %al,-0x2(%rbp)
movzbl -0x2(%rbp),%edi
call   1030 <conv_to_upper_case@plt>
mov    %al,-0x2(%rbp)
movzbl -0x2(%rbp),%ecx
mov    $0x30,%eax
cmp    %ecx,%eax
jg     1157 <ascii_to_hex_nib+0x47>
movzbl -0x2(%rbp),%eax
cmp    $0x39,%eax
jg     1157 <ascii_to_hex_nib+0x47>
movzbl -0x2(%rbp),%eax
sub    $0x30,%eax
mov    %al,-0x1(%rbp)
jmp    118b <ascii_to_hex_nib+0x7b>
movzbl -0x2(%rbp),%ecx
mov    $0x41,%eax
cmp    %ecx,%eax
jg     1187 <ascii_to_hex_nib+0x77>
movzbl -0x2(%rbp),%eax
cmp    $0x46,%eax
jg     1187 <ascii_to_hex_nib+0x77>
movzbl -0x2(%rbp),%eax
sub    $0x41,%eax
add    $0xa,%eax
mov    %al,-0x1(%rbp)
jmp    118b <ascii_to_hex_nib+0x7b>
movb   $0xff,-0x1(%rbp)
movzbl -0x1(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ascii_to_hex_nib>:
push   %rax
call   1030 <conv_to_upper_case@plt>
lea    -0x30(%rax),%ecx
lea    -0x41(%rax),%edx
add    $0xc9,%al
cmp    $0x6,%dl
movzbl %al,%eax
mov    $0xff,%edx
cmovb  %eax,%edx
cmp    $0xa,%cl
movzbl %cl,%eax
cmovae %edx,%eax
pop    %rcx
ret

LLVM IR (-O0):
define zeroext i8 @ascii_to_hex_nib(i8 zeroext %0) {
%2 = alloca i8
%3 = alloca i8
store i8 %0, i8* %3
%4 = load i8, i8* %3
%5 = call zeroext i8 @conv_to_upper_case(i8 zeroext %4)
store i8 %5, i8* %3
%6 = load i8, i8* %3
%7 = zext i8 %6 to i32
%8 = icmp sle i32 48, %7
br i1 %8, label %9, label %18
9: ; preds = %1
%10 = load i8, i8* %3
%11 = zext i8 %10 to i32
%12 = icmp sle i32 %11, 57
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = load i8, i8* %3
%15 = zext i8 %14 to i32
%16 = sub nsw i32 %15, 48
%17 = trunc i32 %16 to i8
store i8 %17, i8* %2
br label %33
18: ; preds = %9, %1
%19 = load i8, i8* %3
%20 = zext i8 %19 to i32
%21 = icmp sle i32 65, %20
br i1 %21, label %22, label %32
22: ; preds = %18
%23 = load i8, i8* %3
%24 = zext i8 %23 to i32
%25 = icmp sle i32 %24, 70
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = load i8, i8* %3
%28 = zext i8 %27 to i32
%29 = sub nsw i32 %28, 65
%30 = add nsw i32 %29, 10
%31 = trunc i32 %30 to i8
store i8 %31, i8* %2
br label %33
32: ; preds = %22, %18
store i8 -1, i8* %2
br label %33
33: ; preds = %32, %26, %13
%34 = load i8, i8* %2
ret i8 %34
}

LLVM IR (-O2):
define zeroext i8 @ascii_to_hex_nib(i8 zeroext %0) {
%2 = tail call zeroext i8 @conv_to_upper_case(i8 zeroext %0)
%3 = add i8 %2, -48
%4 = icmp ult i8 %3, 10
%5 = add i8 %2, -65
%6 = icmp ult i8 %5, 6
%7 = add i8 %2, -55
%8 = select i1 %6, i8 %7, i8 -1
%9 = select i1 %4, i8 %3, i8 %8
ret i8 %9
}