int hex_decode(unsigned char *out, const unsigned char *in, int maxlen) {
  static const char hexval[] = {
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  -1, -1, -1, -1, -1, -1,
      -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  int len = 0;
  int empty = 1;
  for (; len < maxlen;) {
    if (*in > sizeof(hexval) || hexval[*in] == -1)
      break;
    if (empty) {
      *out = (unsigned char)hexval[*in] << 4;
    } else {
      *out |= (unsigned char)hexval[*in];
      ++out;
      ++len;
    }
    ++in;
    empty = !empty;
  }
  return (len);
}


x86 assembly (clang, -O0):
<hex_decode>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11e0 <hex_decode+0xe0>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x70,%rax
ja     1157 <hex_decode+0x57>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
mov    %eax,%ecx
lea    0xeb6(%rip),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0xffffffff,%eax
jne    115c <hex_decode+0x5c>
jmp    11e0 <hex_decode+0xe0>
cmpl   $0x0,-0x1c(%rbp)
je     118a <hex_decode+0x8a>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
mov    %eax,%ecx
lea    0xe8a(%rip),%rax
movzbl (%rax,%rcx,1),%eax
shl    $0x4,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
jmp    11be <hex_decode+0xbe>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
mov    %eax,%ecx
lea    0xe66(%rip),%rax
movzbl (%rax,%rcx,1),%edx
mov    -0x8(%rbp),%rax
movzbl (%rax),%ecx
or     %edx,%ecx
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x1c(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1c(%rbp)
jmp    111d <hex_decode+0x1d>
mov    -0x18(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_decode>:
xor    %eax,%eax
test   %edx,%edx
jle    1154 <hex_decode+0x54>
mov    $0x1,%r9d
lea    0xeed(%rip),%r8
jmp    1135 <hex_decode+0x35>
cs nopw 0x0(%rax,%rax,1)
nop
or     %cl,(%rdi)
add    $0x1,%rdi
add    $0x1,%eax
add    $0x1,%rsi
xor    $0x1,%r9d
cmp    %edx,%eax
jge    1154 <hex_decode+0x54>
movzbl (%rsi),%ecx
cmp    $0x70,%rcx
ja     1154 <hex_decode+0x54>
movzbl (%rcx,%r8,1),%ecx
cmp    $0xff,%cl
je     1154 <hex_decode+0x54>
test   %r9d,%r9d
je     1120 <hex_decode+0x20>
shl    $0x4,%cl
mov    %cl,(%rdi)
jmp    1129 <hex_decode+0x29>
ret

LLVM IR (-O0):
@hex_decode.hexval = internal constant [112 x i8] c"\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\00\01\02\03\04\05\06\07\08\09\FF\FF\FF\FF\FF\FF\FF\0A\0B\0C\0D\0E\0F\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\0A\0B\0C\0D\0E\0F\FF\FF\FF\FF\FF\FF\FF\FF\FF"
define i32 @hex_decode(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i32 1, i32* %8
br label %9
9: ; preds = %56, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %6
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %63
13: ; preds = %9
%14 = load i8*, i8** %5
%15 = load i8, i8* %14
%16 = zext i8 %15 to i64
%17 = icmp ugt i64 %16, 112
br i1 %17, label %26, label %18
18: ; preds = %13
%19 = load i8*, i8** %5
%20 = load i8, i8* %19
%21 = zext i8 %20 to i64
%22 = getelementptr inbounds [112 x i8], [112 x i8]* @hex_decode.hexval, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %18, %13
br label %63
27: ; preds = %18
%28 = load i32, i32* %8
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %40
30: ; preds = %27
%31 = load i8*, i8** %5
%32 = load i8, i8* %31
%33 = zext i8 %32 to i64
%34 = getelementptr inbounds [112 x i8], [112 x i8]* @hex_decode.hexval, i64 0, i64 %33
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = shl i32 %36, 4
%38 = trunc i32 %37 to i8
%39 = load i8*, i8** %4
store i8 %38, i8* %39
br label %56
40: ; preds = %27
%41 = load i8*, i8** %5
%42 = load i8, i8* %41
%43 = zext i8 %42 to i64
%44 = getelementptr inbounds [112 x i8], [112 x i8]* @hex_decode.hexval, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = zext i8 %45 to i32
%47 = load i8*, i8** %4
%48 = load i8, i8* %47
%49 = zext i8 %48 to i32
%50 = or i32 %49, %46
%51 = trunc i32 %50 to i8
store i8 %51, i8* %47
%52 = load i8*, i8** %4
%53 = getelementptr inbounds i8, i8* %52, i32 1
store i8* %53, i8** %4
%54 = load i32, i32* %7
%55 = add nsw i32 %54, 1
store i32 %55, i32* %7
br label %56
56: ; preds = %40, %30
%57 = load i8*, i8** %5
%58 = getelementptr inbounds i8, i8* %57, i32 1
store i8* %58, i8** %5
%59 = load i32, i32* %8
%60 = icmp ne i32 %59, 0
%61 = xor i1 %60, true
%62 = zext i1 %61 to i32
store i32 %62, i32* %8
br label %9
63: ; preds = %26, %9
%64 = load i32, i32* %7
ret i32 %64
}

LLVM IR (-O2):
@hex_decode.hexval = internal unnamed_addr constant [112 x i8] c"\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\00\01\02\03\04\05\06\07\08\09\FF\FF\FF\FF\FF\FF\FF\0A\0B\0C\0D\0E\0F\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\0A\0B\0C\0D\0E\0F\FF\FF\FF\FF\FF\FF\FF\FF\FF"
define i32 @hex_decode(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %32
5: ; preds = %3, %26
%6 = phi i32 [ %30, %26 ], [ 1, %3 ]
%7 = phi i32 [ %28, %26 ], [ 0, %3 ]
%8 = phi i8* [ %27, %26 ], [ %0, %3 ]
%9 = phi i8* [ %29, %26 ], [ %1, %3 ]
%10 = load i8, i8* %9
%11 = icmp ugt i8 %10, 112
br i1 %11, label %32, label %12
12: ; preds = %5
%13 = zext i8 %10 to i64
%14 = getelementptr inbounds [112 x i8], [112 x i8]* @hex_decode.hexval, i64 0, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, -1
br i1 %16, label %32, label %17
17: ; preds = %12
%18 = icmp eq i32 %6, 0
br i1 %18, label %21, label %19
19: ; preds = %17
%20 = shl i8 %15, 4
store i8 %20, i8* %8
br label %26
21: ; preds = %17
%22 = load i8, i8* %8
%23 = or i8 %22, %15
store i8 %23, i8* %8
%24 = getelementptr inbounds i8, i8* %8, i64 1
%25 = add nsw i32 %7, 1
br label %26
26: ; preds = %21, %19
%27 = phi i8* [ %8, %19 ], [ %24, %21 ]
%28 = phi i32 [ %7, %19 ], [ %25, %21 ]
%29 = getelementptr inbounds i8, i8* %9, i64 1
%30 = xor i32 %6, 1
%31 = icmp slt i32 %28, %2
br i1 %31, label %5, label %32
32: ; preds = %26, %12, %5, %3
%33 = phi i32 [ 0, %3 ], [ %7, %5 ], [ %7, %12 ], [ %28, %26 ]
ret i32 %33
}