int util_hex_to_nib(char ch) {
  int val;
  switch (ch) {
  case 'a':
  case 'A':
    val = 10;
    break;
  case 'b':
  case 'B':
    val = 11;
    break;
  case 'c':
  case 'C':
    val = 12;
    break;
  case 'd':
  case 'D':
    val = 13;
    break;
  case 'e':
  case 'E':
    val = 14;
    break;
  case 'f':
  case 'F':
    val = 15;
    break;
  default:
    val = (int)ch - (int)'0';
    if ((val < 0) || (val > 9)) {
      val = -1;
    }
    break;
  }
  return val;
}


x86 assembly (clang, -O0):
<util_hex_to_nib>:
push   %rbp
mov    %rsp,%rbp
mov    %dil,%al
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0xffffffbf,%eax
mov    %eax,%ecx
mov    %rcx,-0x10(%rbp)
sub    $0x25,%eax
ja     117c <util_hex_to_nib+0x7c>
mov    -0x10(%rbp),%rax
lea    0xed5(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0xa,-0x8(%rbp)
jmp    11a6 <util_hex_to_nib+0xa6>
movl   $0xb,-0x8(%rbp)
jmp    11a6 <util_hex_to_nib+0xa6>
movl   $0xc,-0x8(%rbp)
jmp    11a6 <util_hex_to_nib+0xa6>
movl   $0xd,-0x8(%rbp)
jmp    11a6 <util_hex_to_nib+0xa6>
movl   $0xe,-0x8(%rbp)
jmp    11a6 <util_hex_to_nib+0xa6>
movl   $0xf,-0x8(%rbp)
jmp    11a6 <util_hex_to_nib+0xa6>
movsbl -0x1(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     119a <util_hex_to_nib+0x9a>
cmpl   $0x9,-0x8(%rbp)
jle    11a1 <util_hex_to_nib+0xa1>
movl   $0xffffffff,-0x8(%rbp)
jmp    11a6 <util_hex_to_nib+0xa6>
mov    -0x8(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_hex_to_nib>:
lea    -0x41(%rdi),%ecx
cmp    $0x25,%ecx
ja     113b <util_hex_to_nib+0x3b>
mov    $0xa,%eax
lea    0xeec(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
mov    $0xb,%eax
ret
mov    $0xc,%eax
ret
mov    $0xd,%eax
ret
mov    $0xe,%eax
ret
mov    $0xf,%eax
ret
mov    %edi,%ecx
add    $0xffffffd0,%ecx
lea    -0x3a(%rdi),%eax
cmp    $0xf6,%al
mov    $0xffffffff,%eax
cmovae %ecx,%eax
ret

LLVM IR (-O0):
define i32 @util_hex_to_nib(i8 signext %0) {
%2 = alloca i8
%3 = alloca i32
store i8 %0, i8* %2
%4 = load i8, i8* %2
%5 = sext i8 %4 to i32
switch i32 %5, label %12 [
i32 97, label %6
i32 65, label %6
i32 98, label %7
i32 66, label %7
i32 99, label %8
i32 67, label %8
i32 100, label %9
i32 68, label %9
i32 101, label %10
i32 69, label %10
i32 102, label %11
i32 70, label %11
]
6: ; preds = %1, %1
store i32 10, i32* %3
br label %23
7: ; preds = %1, %1
store i32 11, i32* %3
br label %23
8: ; preds = %1, %1
store i32 12, i32* %3
br label %23
9: ; preds = %1, %1
store i32 13, i32* %3
br label %23
10: ; preds = %1, %1
store i32 14, i32* %3
br label %23
11: ; preds = %1, %1
store i32 15, i32* %3
br label %23
12: ; preds = %1
%13 = load i8, i8* %2
%14 = sext i8 %13 to i32
%15 = sub nsw i32 %14, 48
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp slt i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %12
%19 = load i32, i32* %3
%20 = icmp sgt i32 %19, 9
br i1 %20, label %21, label %22
21: ; preds = %18, %12
store i32 -1, i32* %3
br label %22
22: ; preds = %21, %18
br label %23
23: ; preds = %22, %11, %10, %9, %8, %7, %6
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
define i32 @util_hex_to_nib(i8 signext %0) {
%2 = sext i8 %0 to i32
switch i32 %2, label %8 [
i32 97, label %13
i32 65, label %13
i32 98, label %3
i32 66, label %3
i32 99, label %4
i32 67, label %4
i32 100, label %5
i32 68, label %5
i32 101, label %6
i32 69, label %6
i32 102, label %7
i32 70, label %7
]
3: ; preds = %1, %1
br label %13
4: ; preds = %1, %1
br label %13
5: ; preds = %1, %1
br label %13
6: ; preds = %1, %1
br label %13
7: ; preds = %1, %1
br label %13
8: ; preds = %1
%9 = add nsw i32 %2, -48
%10 = add i8 %0, -58
%11 = icmp ult i8 %10, -10
%12 = select i1 %11, i32 -1, i32 %9
br label %13
13: ; preds = %1, %1, %8, %7, %6, %5, %4, %3
%14 = phi i32 [ %12, %8 ], [ 15, %7 ], [ 14, %6 ], [ 13, %5 ], [ 12, %4 ], [ 11, %3 ], [ 10, %1 ], [ 10, %1 ]
ret i32 %14
}