#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 */
typedef int uint8_t;

/* Variables and functions */
int ishexvalue(char const *, size_t);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, size_t);
int strlen(char const *);
bool hex2bin(const char *str, uint8_t *bytes, size_t blen) {
  uint8_t pos;
  uint8_t idx0;
  uint8_t idx1;
  uint8_t hashmap[] = {
      0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
      0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  };
  if (ishexvalue(str, blen) == false)
    return false;
  memset(bytes, 0, blen);
  for (pos = 0; ((pos < (blen * 2)) && (pos < strlen(str))); pos += 2) {
    idx0 = ((uint8_t)str[pos + 0] & 0x1F) ^ 0x10;
    idx1 = ((uint8_t)str[pos + 1] & 0x1F) ^ 0x10;
    bytes[pos / 2] = (uint8_t)(hashmap[idx0] << 4) | hashmap[idx1];
  };
  return true;
}


x86 assembly (clang, -O0):
<hex2bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x130(%rbp),%rdi
lea    0xe9b(%rip),%rsi
mov    $0x100,%edx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <ishexvalue@plt>
cmp    $0x0,%eax
jne    1191 <hex2bin+0x51>
movl   $0x0,-0x4(%rbp)
jmp    128f <hex2bin+0x14f>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rcx
mov    -0x20(%rbp),%rdx
shl    $0x1,%rdx
xor    %eax,%eax
cmp    %rdx,%rcx
mov    %al,-0x131(%rbp)
jae    11e9 <hex2bin+0xa9>
mov    -0x24(%rbp),%eax
mov    %eax,-0x138(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x138(%rbp),%eax
cmp    %ecx,%eax
setl   %al
mov    %al,-0x131(%rbp)
mov    -0x131(%rbp),%al
test   $0x1,%al
jne    11fc <hex2bin+0xbc>
jmp    1288 <hex2bin+0x148>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x0,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
and    $0x1f,%eax
xor    $0x10,%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
and    $0x1f,%eax
xor    $0x10,%eax
mov    %eax,-0x2c(%rbp)
movslq -0x28(%rbp),%rax
mov    -0x130(%rbp,%rax,4),%eax
shl    $0x4,%eax
movslq -0x2c(%rbp),%rcx
or     -0x130(%rbp,%rcx,4),%eax
mov    %eax,-0x144(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x140(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x144(%rbp),%edx
mov    %eax,%ecx
mov    -0x140(%rbp),%rax
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x24(%rbp)
jmp    11a7 <hex2bin+0x67>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2bin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rdx,%rsi
call   1050 <ishexvalue@plt>
test   %eax,%eax
je     11c2 <hex2bin+0x92>
mov    %r14,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1040 <memset@plt>
mov    $0x1,%r12d
add    %r15,%r15
je     11c5 <hex2bin+0x95>
xor    %ebp,%ebp
lea    0xe8f(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbp
jge    11c5 <hex2bin+0x95>
movzbl (%rbx,%rbp,1),%eax
and    $0x1f,%eax
xor    $0x10,%rax
movzbl 0x1(%rbx,%rbp,1),%ecx
and    $0x1f,%ecx
xor    $0x10,%rcx
mov    0x0(%r13,%rax,4),%eax
shl    $0x4,%eax
or     0x0(%r13,%rcx,4),%eax
mov    %eax,(%r14,%rbp,2)
add    $0x2,%rbp
cmp    %rbp,%r15
ja     1180 <hex2bin+0x50>
jmp    11c5 <hex2bin+0x95>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.hex2bin.hashmap = <{ [55 x i32], [9 x i32] }> <{ [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], [9 x i32] zeroinitializer }>
define i32 @hex2bin(i8* %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [64 x i32]
store i8* %0, i8** %5
store i32* %1, i32** %6
store i64 %2, i64* %7
%12 = bitcast [64 x i32]* %11 to i8*
%13 = load i8*, i8** %5
%14 = load i64, i64* %7
%15 = call i32 @ishexvalue(i8* %13, i64 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 0, i32* %4
br label %73
18: ; preds = %3
%19 = load i32*, i32** %6
%20 = load i64, i64* %7
%21 = call i32 @memset(i32* %19, i32 0, i64 %20)
store i32 0, i32* %8
br label %22
22: ; preds = %69, %18
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = load i64, i64* %7
%26 = mul i64 %25, 2
%27 = icmp ult i64 %24, %26
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = load i32, i32* %8
%30 = load i8*, i8** %5
%31 = call i32 @strlen(i8* %30)
%32 = icmp slt i32 %29, %31
br label %33
33: ; preds = %28, %22
%34 = phi i1 [ false, %22 ], [ %32, %28 ]
br i1 %34, label %35, label %72
35: ; preds = %33
%36 = load i8*, i8** %5
%37 = load i32, i32* %8
%38 = add nsw i32 %37, 0
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = and i32 %42, 31
%44 = xor i32 %43, 16
store i32 %44, i32* %9
%45 = load i8*, i8** %5
%46 = load i32, i32* %8
%47 = add nsw i32 %46, 1
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = and i32 %51, 31
%53 = xor i32 %52, 16
store i32 %53, i32* %10
%54 = load i32, i32* %9
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [64 x i32], [64 x i32]* %11, i64 0, i64 %55
%57 = load i32, i32* %56
%58 = shl i32 %57, 4
%59 = load i32, i32* %10
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [64 x i32], [64 x i32]* %11, i64 0, i64 %60
%62 = load i32, i32* %61
%63 = or i32 %58, %62
%64 = load i32*, i32** %6
%65 = load i32, i32* %8
%66 = sdiv i32 %65, 2
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i32, i32* %64, i64 %67
store i32 %63, i32* %68
br label %69
69: ; preds = %35
%70 = load i32, i32* %8
%71 = add nsw i32 %70, 2
store i32 %71, i32* %8
br label %22
72: ; preds = %33
store i32 1, i32* %4
br label %73
73: ; preds = %72, %17
%74 = load i32, i32* %4
ret i32 %74
}

LLVM IR (-O2):
@__const.hex2bin.hashmap = <{ [55 x i32], [9 x i32] }> <{ [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], [9 x i32] zeroinitializer }>
define i32 @hex2bin(i8* %0, i32* %1, i64 %2) {
%4 = tail call i32 @ishexvalue(i8* %0, i64 %2)
%5 = icmp eq i32 %4, 0
br i1 %5, label %37, label %6
6: ; preds = %3
%7 = tail call i32 @memset(i32* %1, i32 0, i64 %2)
%8 = shl i64 %2, 1
%9 = icmp eq i64 %8, 0
br i1 %9, label %37, label %10
10: ; preds = %6, %15
%11 = phi i64 [ %35, %15 ], [ 0, %6 ]
%12 = tail call i32 @strlen(i8* %0)
%13 = sext i32 %12 to i64
%14 = icmp slt i64 %11, %13
br i1 %14, label %15, label %37
15: ; preds = %10
%16 = getelementptr inbounds i8, i8* %0, i64 %11
%17 = load i8, i8* %16
%18 = and i8 %17, 31
%19 = xor i8 %18, 16
%20 = or i64 %11, 1
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = load i8, i8* %21
%23 = and i8 %22, 31
%24 = xor i8 %23, 16
%25 = zext i8 %19 to i64
%26 = getelementptr inbounds [64 x i32], [64 x i32]* bitcast (<{ [55 x i32], [9 x i32] }>* @__const.hex2bin.hashmap to [64 x i32]*), i64 0, i64 %25
%27 = load i32, i32* %26
%28 = shl i32 %27, 4
%29 = zext i8 %24 to i64
%30 = getelementptr inbounds [64 x i32], [64 x i32]* bitcast (<{ [55 x i32], [9 x i32] }>* @__const.hex2bin.hashmap to [64 x i32]*), i64 0, i64 %29
%31 = load i32, i32* %30
%32 = or i32 %28, %31
%33 = lshr exact i64 %11, 1
%34 = getelementptr inbounds i32, i32* %1, i64 %33
store i32 %32, i32* %34
%35 = add nuw i64 %11, 2
%36 = icmp ugt i64 %8, %35
br i1 %36, label %10, label %37
37: ; preds = %15, %10, %6, %3
%38 = phi i32 [ 0, %3 ], [ 1, %6 ], [ 1, %10 ], [ 1, %15 ]
ret i32 %38
}