int gethex() {
  int c;
  switch (c = getchar()) {
    break;
  case '0' ... '9':;
    return c - '0';
    break;
  case 'A' ... 'F':;
    return c - 'A' + 10;
    break;
  case 'a' ... 'f':;
    return c - 'a' + 10;
  }
  return 0;
}


x86 assembly (clang, -O0):
<gethex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
mov    %ecx,%eax
add    $0xffffffd0,%eax
mov    %ecx,-0xc(%rbp)
sub    $0xa,%eax
jb     1162 <gethex+0x52>
jmp    113a <gethex+0x2a>
mov    -0xc(%rbp),%eax
add    $0xffffffbf,%eax
sub    $0x6,%eax
jb     1170 <gethex+0x60>
jmp    114e <gethex+0x3e>
mov    -0xc(%rbp),%eax
add    $0xffffff9f,%eax
sub    $0x6,%eax
jb     1181 <gethex+0x71>
jmp    1192 <gethex+0x82>
mov    -0x8(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x4(%rbp)
jmp    1199 <gethex+0x89>
mov    -0x8(%rbp),%eax
sub    $0x41,%eax
add    $0xa,%eax
mov    %eax,-0x4(%rbp)
jmp    1199 <gethex+0x89>
mov    -0x8(%rbp),%eax
sub    $0x61,%eax
add    $0xa,%eax
mov    %eax,-0x4(%rbp)
jmp    1199 <gethex+0x89>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gethex>:
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getchar@plt>
add    $0xffffffd0,%eax
cmp    $0x36,%eax
ja     112e <gethex+0x1e>
cltq
lea    0xed5(%rip),%rcx
mov    (%rcx,%rax,4),%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @gethex() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @getchar()
store i32 %3, i32* %2
switch i32 %3, label %15 [
i32 48, label %4
i32 49, label %4
i32 50, label %4
i32 51, label %4
i32 52, label %4
i32 53, label %4
i32 54, label %4
i32 55, label %4
i32 56, label %4
i32 57, label %4
i32 65, label %7
i32 66, label %7
i32 67, label %7
i32 68, label %7
i32 69, label %7
i32 70, label %7
i32 97, label %11
i32 98, label %11
i32 99, label %11
i32 100, label %11
i32 101, label %11
i32 102, label %11
]
4: ; preds = %0, %0, %0, %0, %0, %0, %0, %0, %0, %0
%5 = load i32, i32* %2
%6 = sub nsw i32 %5, 48
store i32 %6, i32* %1
br label %16
7: ; preds = %0, %0, %0, %0, %0, %0
%8 = load i32, i32* %2
%9 = sub nsw i32 %8, 65
%10 = add nsw i32 %9, 10
store i32 %10, i32* %1
br label %16
11: ; preds = %0, %0, %0, %0, %0, %0
%12 = load i32, i32* %2
%13 = sub nsw i32 %12, 97
%14 = add nsw i32 %13, 10
store i32 %14, i32* %1
br label %16
15: ; preds = %0
store i32 0, i32* %1
br label %16
16: ; preds = %15, %11, %7, %4
%17 = load i32, i32* %1
ret i32 %17
}

LLVM IR (-O2):
@switch.table.gethex = [55 x i32] [i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15]
define i32 @gethex() {
%1 = tail call i32 (...) @getchar()
%2 = add i32 %1, -48
%3 = icmp ult i32 %2, 55
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = sext i32 %2 to i64
%6 = getelementptr inbounds [55 x i32], [55 x i32]* @switch.table.gethex, i64 0, i64 %5
%7 = load i32, i32* %6
br label %8
8: ; preds = %4, %0
%9 = phi i32 [ 0, %0 ], [ %7, %4 ]
ret i32 %9
}