#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 */
scalar_t__ decode_hex(unsigned char, unsigned char *);
int hex_to_bin(const char *hex, unsigned char *bin, int length) {
  int i;
  unsigned char ch_lower = 0, ch_upper = 0, *hex_pos = (unsigned char *)hex - 1,
                *bin_pos = bin - 1;
  for (i = 0; i < length; i++) {
    if (decode_hex(*(++hex_pos), &ch_upper) ||
        decode_hex(*(++hex_pos), &ch_lower)) {
      return -1;
    }
    *(++bin_pos) = (ch_upper << 4) + ch_lower;
  }
  return 0;
}


x86 assembly (clang, -O0):
<hex_to_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movb   $0x0,-0x21(%rbp)
movb   $0x0,-0x22(%rbp)
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11dd <hex_to_bin+0xcd>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
lea    -0x22(%rbp),%rsi
movzbl 0x1(%rax),%edi
call   1030 <decode_hex@plt>
cmp    $0x0,%rax
jne    11a2 <hex_to_bin+0x92>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
lea    -0x21(%rbp),%rsi
movzbl 0x1(%rax),%edi
call   1030 <decode_hex@plt>
cmp    $0x0,%rax
je     11ae <hex_to_bin+0x9e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <hex_to_bin+0xd4>
movzbl -0x22(%rbp),%eax
shl    $0x4,%eax
movzbl -0x21(%rbp),%ecx
add    %ecx,%eax
mov    %al,%cl
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x38(%rbp)
mov    %cl,0x1(%rax)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    114a <hex_to_bin+0x3a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_to_bin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movb   $0x0,0x7(%rsp)
movb   $0x0,0x6(%rsp)
test   %edx,%edx
jle    1180 <hex_to_bin+0x70>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %edx,%r13d
xor    %r12d,%r12d
lea    0x6(%rsp),%r15
mov    $0xffffffff,%ebp
nop
movzbl (%rbx,%r12,2),%edi
mov    %r15,%rsi
call   1030 <decode_hex@plt>
test   %rax,%rax
jne    1182 <hex_to_bin+0x72>
movzbl 0x1(%rbx,%r12,2),%edi
lea    0x7(%rsp),%rsi
call   1030 <decode_hex@plt>
test   %rax,%rax
jne    1182 <hex_to_bin+0x72>
movzbl 0x6(%rsp),%eax
shl    $0x4,%al
add    0x7(%rsp),%al
mov    %al,(%r14,%r12,1)
add    $0x1,%r12
cmp    %r12d,%r13d
jne    1140 <hex_to_bin+0x30>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @hex_to_bin(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca i8
%11 = alloca i8*
%12 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8 0, i8* %9
store i8 0, i8* %10
%13 = load i8*, i8** %5
%14 = getelementptr inbounds i8, i8* %13, i64 -1
store i8* %14, i8** %11
%15 = load i8*, i8** %6
%16 = getelementptr inbounds i8, i8* %15, i64 -1
store i8* %16, i8** %12
store i32 0, i32* %8
br label %17
17: ; preds = %44, %3
%18 = load i32, i32* %8
%19 = load i32, i32* %7
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %47
21: ; preds = %17
%22 = load i8*, i8** %11
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %11
%24 = load i8, i8* %23
%25 = call i64 @decode_hex(i8 zeroext %24, i8* %10)
%26 = icmp ne i64 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %21
%28 = load i8*, i8** %11
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %11
%30 = load i8, i8* %29
%31 = call i64 @decode_hex(i8 zeroext %30, i8* %9)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %27, %21
store i32 -1, i32* %4
br label %48
34: ; preds = %27
%35 = load i8, i8* %10
%36 = zext i8 %35 to i32
%37 = shl i32 %36, 4
%38 = load i8, i8* %9
%39 = zext i8 %38 to i32
%40 = add nsw i32 %37, %39
%41 = trunc i32 %40 to i8
%42 = load i8*, i8** %12
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %12
store i8 %41, i8* %43
br label %44
44: ; preds = %34
%45 = load i32, i32* %8
%46 = add nsw i32 %45, 1
store i32 %46, i32* %8
br label %17
47: ; preds = %17
store i32 0, i32* %4
br label %48
48: ; preds = %47, %33
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
define i32 @hex_to_bin(i8* nocapture %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca i8
%5 = alloca i8
store i8 0, i8* %4
store i8 0, i8* %5
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %31
7: ; preds = %3
%8 = getelementptr inbounds i8, i8* %1, i64 -1
%9 = getelementptr inbounds i8, i8* %0, i64 -1
br label %10
10: ; preds = %7, %23
%11 = phi i8* [ %28, %23 ], [ %8, %7 ]
%12 = phi i8* [ %19, %23 ], [ %9, %7 ]
%13 = phi i32 [ %29, %23 ], [ 0, %7 ]
%14 = getelementptr inbounds i8, i8* %12, i64 1
%15 = load i8, i8* %14
%16 = call i64 @decode_hex(i8 zeroext %15, i8* %5)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %31
18: ; preds = %10
%19 = getelementptr inbounds i8, i8* %12, i64 2
%20 = load i8, i8* %19
%21 = call i64 @decode_hex(i8 zeroext %20, i8* %4)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %18
%24 = load i8, i8* %5
%25 = shl i8 %24, 4
%26 = load i8, i8* %4
%27 = add i8 %25, %26
%28 = getelementptr inbounds i8, i8* %11, i64 1
store i8 %27, i8* %28
%29 = add nuw nsw i32 %13, 1
%30 = icmp eq i32 %29, %2
br i1 %30, label %31, label %10
31: ; preds = %18, %10, %23, %3
%32 = phi i32 [ 0, %3 ], [ 0, %23 ], [ -1, %10 ], [ -1, %18 ]
ret i32 %32
}