#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 */
int /*<<< orphan*/ EILSEQ;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int hex_decode(char *dest, unsigned char *src, size_t len) {
  static const char hex2dec[256] = {
      -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, -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, -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, -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};
  char *ptr = dest;
  size_t i = 0;
  if (len % 2) {
    errno = EINVAL;
    return -1;
  }
  for (; i < len; i += 2) {
    if (hex2dec[src[i]] == -1 || hex2dec[src[i + 1]] == -1) {
      errno = EILSEQ;
      return -1;
    }
    *ptr++ = hex2dec[src[i]] << 4 | hex2dec[src[i + 1]];
  }
  return 0;
}


x86 assembly (clang, -O0):
<hex_decode>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     1150 <hex_decode+0x50>
mov    0x2e97(%rip),%rax
mov    (%rax),%ecx
mov    0x2e96(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1229 <hex_decode+0x129>
jmp    1155 <hex_decode+0x55>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1222 <hex_decode+0x122>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,%ecx
lea    0xe88(%rip),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0xffffffff,%eax
je     11a8 <hex_decode+0xa8>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movzbl 0x1(%rax,%rcx,1),%eax
mov    %eax,%ecx
lea    0xe65(%rip),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0xffffffff,%eax
jne    11c6 <hex_decode+0xc6>
mov    0x2e41(%rip),%rax
mov    (%rax),%ecx
mov    0x2e20(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1229 <hex_decode+0x129>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,%ecx
lea    0xe25(%rip),%rax
movsbl (%rax,%rcx,1),%eax
shl    $0x4,%eax
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%rdx
movzbl 0x1(%rcx,%rdx,1),%ecx
mov    %ecx,%edx
lea    0xe08(%rip),%rcx
movsbl (%rcx,%rdx,1),%ecx
or     %ecx,%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
mov    -0x30(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x30(%rbp)
jmp    1155 <hex_decode+0x55>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_decode>:
test   $0x1,%dl
jne    1155 <hex_decode+0x55>
test   %rdx,%rdx
je     1152 <hex_decode+0x52>
xor    %r10d,%r10d
lea    0xeec(%rip),%r9
mov    0x2ed5(%rip),%r8
nopl   0x0(%rax,%rax,1)
movzbl (%rsi,%r10,1),%eax
movzbl (%rax,%r9,1),%eax
cmp    $0xff,%al
je     115c <hex_decode+0x5c>
movzbl 0x1(%rsi,%r10,1),%ecx
movzbl (%rcx,%r9,1),%ecx
cmp    $0xff,%cl
je     115c <hex_decode+0x5c>
shl    $0x4,%al
or     %al,%cl
mov    %cl,(%rdi)
add    $0x1,%rdi
add    $0x2,%r10
cmp    %rdx,%r10
jb     1120 <hex_decode+0x20>
xor    %eax,%eax
ret
mov    0x2e74(%rip),%r8
mov    (%r8),%eax
mov    0x2e72(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
ret

LLVM IR (-O0):
@hex_decode.hex2dec = internal constant [256 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\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\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\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"
@EINVAL = global i32 0
@errno = global i32 0
@EILSEQ = global i32 0
define i32 @hex_decode(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
store i8* %10, i8** %8
store i64 0, i64* %9
%11 = load i64, i64* %7
%12 = urem i64 %11, 2
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = load i32, i32* @EINVAL
store i32 %15, i32* @errno
store i32 -1, i32* %4
br label %71
16: ; preds = %3
br label %17
17: ; preds = %67, %16
%18 = load i64, i64* %9
%19 = load i64, i64* %7
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %70
21: ; preds = %17
%22 = load i8*, i8** %6
%23 = load i64, i64* %9
%24 = getelementptr inbounds i8, i8* %22, i64 %23
%25 = load i8, i8* %24
%26 = zext i8 %25 to i64
%27 = getelementptr inbounds [256 x i8], [256 x i8]* @hex_decode.hex2dec, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, -1
br i1 %30, label %42, label %31
31: ; preds = %21
%32 = load i8*, i8** %6
%33 = load i64, i64* %9
%34 = add i64 %33, 1
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = zext i8 %36 to i64
%38 = getelementptr inbounds [256 x i8], [256 x i8]* @hex_decode.hex2dec, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %31, %21
%43 = load i32, i32* @EILSEQ
store i32 %43, i32* @errno
store i32 -1, i32* %4
br label %71
44: ; preds = %31
%45 = load i8*, i8** %6
%46 = load i64, i64* %9
%47 = getelementptr inbounds i8, i8* %45, i64 %46
%48 = load i8, i8* %47
%49 = zext i8 %48 to i64
%50 = getelementptr inbounds [256 x i8], [256 x i8]* @hex_decode.hex2dec, i64 0, i64 %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = shl i32 %52, 4
%54 = load i8*, i8** %6
%55 = load i64, i64* %9
%56 = add i64 %55, 1
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i8, i8* %57
%59 = zext i8 %58 to i64
%60 = getelementptr inbounds [256 x i8], [256 x i8]* @hex_decode.hex2dec, i64 0, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = or i32 %53, %62
%64 = trunc i32 %63 to i8
%65 = load i8*, i8** %8
%66 = getelementptr inbounds i8, i8* %65, i32 1
store i8* %66, i8** %8
store i8 %64, i8* %65
br label %67
67: ; preds = %44
%68 = load i64, i64* %9
%69 = add i64 %68, 2
store i64 %69, i64* %9
br label %17
70: ; preds = %17
store i32 0, i32* %4
br label %71
71: ; preds = %70, %42, %14
%72 = load i32, i32* %4
ret i32 %72
}

LLVM IR (-O2):
@hex_decode.hex2dec = internal unnamed_addr constant [256 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\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\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\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"
@EINVAL = global i32 0
@errno = global i32 0
@EILSEQ = global i32 0
define i32 @hex_decode(i8* nocapture writeonly %0, i8* nocapture %1, i64 %2) {
%4 = and i64 %2, 1
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %31
6: ; preds = %3
%7 = icmp eq i64 %2, 0
br i1 %7, label %34, label %8
8: ; preds = %6, %25
%9 = phi i64 [ %29, %25 ], [ 0, %6 ]
%10 = phi i8* [ %28, %25 ], [ %0, %6 ]
%11 = getelementptr inbounds i8, i8* %1, i64 %9
%12 = load i8, i8* %11
%13 = zext i8 %12 to i64
%14 = getelementptr inbounds [256 x i8], [256 x i8]* @hex_decode.hex2dec, i64 0, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, -1
br i1 %16, label %31, label %17
17: ; preds = %8
%18 = or i64 %9, 1
%19 = getelementptr inbounds i8, i8* %1, i64 %18
%20 = load i8, i8* %19
%21 = zext i8 %20 to i64
%22 = getelementptr inbounds [256 x i8], [256 x i8]* @hex_decode.hex2dec, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, -1
br i1 %24, label %31, label %25
25: ; preds = %17
%26 = shl i8 %15, 4
%27 = or i8 %23, %26
%28 = getelementptr inbounds i8, i8* %10, i64 1
store i8 %27, i8* %10
%29 = add i64 %9, 2
%30 = icmp ult i64 %29, %2
br i1 %30, label %8, label %34
31: ; preds = %8, %17, %3
%32 = phi i32* [ @EINVAL, %3 ], [ @EILSEQ, %17 ], [ @EILSEQ, %8 ]
%33 = load i32, i32* %32
store i32 %33, i32* @errno
br label %34
34: ; preds = %25, %31, %6
%35 = phi i32 [ 0, %6 ], [ -1, %31 ], [ 0, %25 ]
ret i32 %35
}