#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 */
char *strchr(char *, char const);
char const tolower(char const);
int unhex(const char *src, char *dst, size_t dstlen) {
  char *p;
  unsigned byte;
  static char hex[] = "0123456789abcdef";
  if (dstlen < 1)
    return (-2);
  p = strchr(hex, *src);
  if (p == NULL)
    p = strchr(hex, tolower(*src));
  if (p == NULL)
    return (-4);
  byte = (p - hex) << 4;
  src++;
  p = strchr(hex, *src);
  if (p == NULL)
    p = strchr(hex, tolower(*src));
  if (p == NULL)
    return (-5);
  byte |= (p - hex);
  *dst = byte;
  return 1;
}


x86 assembly (clang, -O0):
<unhex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x1,-0x20(%rbp)
jae    114b <unhex+0x2b>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1245 <unhex+0x125>
mov    -0x10(%rbp),%rax
lea    0x2eea(%rip),%rdi
movsbl (%rax),%esi
call   1030 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118c <unhex+0x6c>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <tolower@plt>
lea    0x2ec0(%rip),%rdi
movsbl %al,%esi
call   1030 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a3 <unhex+0x83>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1245 <unhex+0x125>
mov    -0x28(%rbp),%rax
lea    0x2e92(%rip),%rcx
sub    %rcx,%rax
shl    $0x4,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
lea    0x2e71(%rip),%rdi
movsbl (%rax),%esi
call   1030 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1205 <unhex+0xe5>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <tolower@plt>
lea    0x2e47(%rip),%rdi
movsbl %al,%esi
call   1030 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    121c <unhex+0xfc>
movl   $0xfffffffb,-0x4(%rbp)
jmp    1245 <unhex+0x125>
mov    -0x28(%rbp),%rcx
lea    0x2e19(%rip),%rax
sub    %rax,%rcx
mov    -0x2c(%rbp),%eax
or     %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unhex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     11c3 <unhex+0xa3>
mov    %rsi,%r14
mov    %rdi,%r15
movsbl (%rdi),%ebp
lea    0x2eff(%rip),%rdi
mov    %ebp,%esi
call   1030 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    116e <unhex+0x4e>
mov    %ebp,%edi
call   1040 <tolower@plt>
movsbl %al,%esi
lea    0x2edf(%rip),%rdi
call   1030 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11ca <unhex+0xaa>
lea    0x2ecb(%rip),%r12
movsbl 0x1(%r15),%ebp
mov    %r12,%rdi
mov    %ebp,%esi
call   1030 <strchr@plt>
test   %rax,%rax
jne    11a4 <unhex+0x84>
mov    %ebp,%edi
call   1040 <tolower@plt>
movsbl %al,%esi
lea    0x2ea6(%rip),%rdi
call   1030 <strchr@plt>
test   %rax,%rax
je     11d1 <unhex+0xb1>
mov    %r12d,%ecx
sub    %rcx,%rbx
shl    $0x4,%rbx
lea    0x2e8b(%rip),%rcx
sub    %ecx,%eax
or     %eax,%ebx
mov    %bl,(%r14)
mov    $0x1,%eax
jmp    11d6 <unhex+0xb6>
mov    $0xfffffffe,%eax
jmp    11d6 <unhex+0xb6>
mov    $0xfffffffc,%eax
jmp    11d6 <unhex+0xb6>
mov    $0xfffffffb,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@unhex.hex = internal global [17 x i8] c"0123456789abcdef\00"
define i32 @unhex(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i64, i64* %7
%11 = icmp ult i64 %10, 1
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 -2, i32* %4
br label %61
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = load i8, i8* %14
%16 = call i8* @strchr(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @unhex.hex, i64 0, i64 0), i8 signext %15)
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %24
19: ; preds = %13
%20 = load i8*, i8** %5
%21 = load i8, i8* %20
%22 = call signext i8 @tolower(i8 signext %21)
%23 = call i8* @strchr(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @unhex.hex, i64 0, i64 0), i8 signext %22)
store i8* %23, i8** %8
br label %24
24: ; preds = %19, %13
%25 = load i8*, i8** %8
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %24
store i32 -4, i32* %4
br label %61
28: ; preds = %24
%29 = load i8*, i8** %8
%30 = ptrtoint i8* %29 to i64
%31 = sub i64 %30, ptrtoint ([17 x i8]* @unhex.hex to i64)
%32 = shl i64 %31, 4
%33 = trunc i64 %32 to i32
store i32 %33, i32* %9
%34 = load i8*, i8** %5
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %5
%36 = load i8*, i8** %5
%37 = load i8, i8* %36
%38 = call i8* @strchr(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @unhex.hex, i64 0, i64 0), i8 signext %37)
store i8* %38, i8** %8
%39 = load i8*, i8** %8
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %46
41: ; preds = %28
%42 = load i8*, i8** %5
%43 = load i8, i8* %42
%44 = call signext i8 @tolower(i8 signext %43)
%45 = call i8* @strchr(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @unhex.hex, i64 0, i64 0), i8 signext %44)
store i8* %45, i8** %8
br label %46
46: ; preds = %41, %28
%47 = load i8*, i8** %8
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %50
49: ; preds = %46
store i32 -5, i32* %4
br label %61
50: ; preds = %46
%51 = load i8*, i8** %8
%52 = ptrtoint i8* %51 to i64
%53 = sub i64 %52, ptrtoint ([17 x i8]* @unhex.hex to i64)
%54 = load i32, i32* %9
%55 = zext i32 %54 to i64
%56 = or i64 %55, %53
%57 = trunc i64 %56 to i32
store i32 %57, i32* %9
%58 = load i32, i32* %9
%59 = trunc i32 %58 to i8
%60 = load i8*, i8** %6
store i8 %59, i8* %60
store i32 1, i32* %4
br label %61
61: ; preds = %50, %49, %27, %12
%62 = load i32, i32* %4
ret i32 %62
}

LLVM IR (-O2):
@unhex.hex = internal global [17 x i8] c"0123456789abcdef\00"
define i32 @unhex(i8* nocapture %0, i8* nocapture writeonly %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %32, label %5
5: ; preds = %3
%6 = load i8, i8* %0
%7 = tail call i8* @strchr(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @unhex.hex, i64 0, i64 0), i8 signext %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %5
%10 = tail call signext i8 @tolower(i8 signext %6)
%11 = tail call i8* @strchr(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @unhex.hex, i64 0, i64 0), i8 signext %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %32, label %13
13: ; preds = %5, %9
%14 = phi i8* [ %11, %9 ], [ %7, %5 ]
%15 = ptrtoint i8* %14 to i64
%16 = sub i64 %15, zext (i32 ptrtoint ([17 x i8]* @unhex.hex to i32) to i64)
%17 = shl i64 %16, 4
%18 = getelementptr inbounds i8, i8* %0, i64 1
%19 = load i8, i8* %18
%20 = tail call i8* @strchr(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @unhex.hex, i64 0, i64 0), i8 signext %19)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %26
22: ; preds = %13
%23 = tail call signext i8 @tolower(i8 signext %19)
%24 = tail call i8* @strchr(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @unhex.hex, i64 0, i64 0), i8 signext %23)
%25 = icmp eq i8* %24, null
br i1 %25, label %32, label %26
26: ; preds = %13, %22
%27 = phi i8* [ %24, %22 ], [ %20, %13 ]
%28 = ptrtoint i8* %27 to i64
%29 = sub i64 %28, ptrtoint ([17 x i8]* @unhex.hex to i64)
%30 = or i64 %29, %17
%31 = trunc i64 %30 to i8
store i8 %31, i8* %1
br label %32
32: ; preds = %22, %9, %3, %26
%33 = phi i32 [ 1, %26 ], [ -2, %3 ], [ -4, %9 ], [ -5, %22 ]
ret i32 %33
}