#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 hex_to_nibble(char hex) {
  if (hex >= '0' && hex <= '9') {
    return (unsigned char)hex - '0';
  } else if (hex >= 'a' && hex <= 'f') {
    return (unsigned char)10 + hex - 'a';
  } else if (hex >= 'A' && hex <= 'F') {
    return (unsigned char)10 + hex - 'A';
  }
  ((char *)NULL)[0] = hex;
  return (unsigned char)hex;
}


x86 assembly (clang, -O0):
<hex_to_nibble>:
push   %rbp
mov    %rsp,%rbp
mov    %dil,%al
mov    %al,-0x2(%rbp)
movsbl -0x2(%rbp),%eax
cmp    $0x30,%eax
jl     1133 <hex_to_nibble+0x33>
movsbl -0x2(%rbp),%eax
cmp    $0x39,%eax
jg     1133 <hex_to_nibble+0x33>
movzbl -0x2(%rbp),%eax
sub    $0x30,%eax
mov    %al,-0x1(%rbp)
jmp    11a2 <hex_to_nibble+0xa2>
movsbl -0x2(%rbp),%eax
cmp    $0x61,%eax
jl     115f <hex_to_nibble+0x5f>
movsbl -0x2(%rbp),%eax
cmp    $0x66,%eax
jg     115f <hex_to_nibble+0x5f>
movsbl -0x2(%rbp),%eax
add    $0xa,%eax
sub    $0x61,%eax
mov    %al,-0x1(%rbp)
jmp    11a2 <hex_to_nibble+0xa2>
movsbl -0x2(%rbp),%eax
cmp    $0x41,%eax
jl     118b <hex_to_nibble+0x8b>
movsbl -0x2(%rbp),%eax
cmp    $0x46,%eax
jg     118b <hex_to_nibble+0x8b>
movsbl -0x2(%rbp),%eax
add    $0xa,%eax
sub    $0x41,%eax
mov    %al,-0x1(%rbp)
jmp    11a2 <hex_to_nibble+0xa2>
jmp    1190 <hex_to_nibble+0x90>
jmp    1195 <hex_to_nibble+0x95>
mov    -0x2(%rbp),%cl
xor    %eax,%eax
mov    %cl,(%rax)
mov    -0x2(%rbp),%al
mov    %al,-0x1(%rbp)
movzbl -0x1(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_to_nibble>:
lea    -0x30(%rdi),%eax
cmp    $0xa,%al
jb     111a <hex_to_nibble+0x1a>
lea    -0x61(%rdi),%eax
cmp    $0x5,%al
ja     1114 <hex_to_nibble+0x14>
add    $0xa9,%dil
jmp    1118 <hex_to_nibble+0x18>
add    $0xc9,%dil
mov    %edi,%eax
ret

LLVM IR (-O0):
define zeroext i8 @hex_to_nibble(i8 signext %0) {
%2 = alloca i8
%3 = alloca i8
store i8 %0, i8* %3
%4 = load i8, i8* %3
%5 = sext i8 %4 to i32
%6 = icmp sge i32 %5, 48
br i1 %6, label %7, label %16
7: ; preds = %1
%8 = load i8, i8* %3
%9 = sext i8 %8 to i32
%10 = icmp sle i32 %9, 57
br i1 %10, label %11, label %16
11: ; preds = %7
%12 = load i8, i8* %3
%13 = zext i8 %12 to i32
%14 = sub nsw i32 %13, 48
%15 = trunc i32 %14 to i8
store i8 %15, i8* %2
br label %49
16: ; preds = %7, %1
%17 = load i8, i8* %3
%18 = sext i8 %17 to i32
%19 = icmp sge i32 %18, 97
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = load i8, i8* %3
%22 = sext i8 %21 to i32
%23 = icmp sle i32 %22, 102
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load i8, i8* %3
%26 = sext i8 %25 to i32
%27 = add nsw i32 10, %26
%28 = sub nsw i32 %27, 97
%29 = trunc i32 %28 to i8
store i8 %29, i8* %2
br label %49
30: ; preds = %20, %16
%31 = load i8, i8* %3
%32 = sext i8 %31 to i32
%33 = icmp sge i32 %32, 65
br i1 %33, label %34, label %44
34: ; preds = %30
%35 = load i8, i8* %3
%36 = sext i8 %35 to i32
%37 = icmp sle i32 %36, 70
br i1 %37, label %38, label %44
38: ; preds = %34
%39 = load i8, i8* %3
%40 = sext i8 %39 to i32
%41 = add nsw i32 10, %40
%42 = sub nsw i32 %41, 65
%43 = trunc i32 %42 to i8
store i8 %43, i8* %2
br label %49
44: ; preds = %34, %30
br label %45
45: ; preds = %44
br label %46
46: ; preds = %45
%47 = load i8, i8* %3
store i8 %47, i8* null
%48 = load i8, i8* %3
store i8 %48, i8* %2
br label %49
49: ; preds = %46, %38, %24, %11
%50 = load i8, i8* %2
ret i8 %50
}

LLVM IR (-O2):
define zeroext i8 @hex_to_nibble(i8 signext %0) {
%2 = add i8 %0, -48
%3 = icmp ult i8 %2, 10
br i1 %3, label %13, label %4
4: ; preds = %1
%5 = add i8 %0, -97
%6 = icmp ult i8 %5, 6
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = add nsw i8 %0, -87
br label %13
9: ; preds = %4
%10 = add i8 %0, -65
%11 = icmp ult i8 %10, 6
tail call void @llvm.assume(i1 %11)
%12 = add nsw i8 %0, -55
br label %13
13: ; preds = %1, %9, %7
%14 = phi i8 [ %8, %7 ], [ %12, %9 ], [ %2, %1 ]
ret i8 %14
}