#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 char uint8_t;
typedef int uint32_t;

/* Variables and functions */
int Hex_decodeByte(char const, char const);
int Hex_decode(uint8_t *output, const uint32_t outLength, const uint8_t *hex,
               const uint32_t length) {
  if (length % 1) {
    return -1;
  } else if (outLength < (length / 2)) {
    return -2;
  } else if (outLength > (length / 2)) {
    output[length / 2] = '\0';
  }
  for (uint32_t i = 0; i < length; i += 2) {
    int byte = Hex_decodeByte(hex[i], hex[i + 1]);
    if (byte == -1) {
      return -1;
    }
    output[i / 2] = (uint8_t)byte;
  }
  return length / 2;
}


x86 assembly (clang, -O0):
<Hex_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x1,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1146 <Hex_decode+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    124c <Hex_decode+0x13c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jge    1170 <Hex_decode+0x60>
movl   $0xfffffffe,-0x4(%rbp)
jmp    124c <Hex_decode+0x13c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
jle    11ae <Hex_decode+0x9e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11b3 <Hex_decode+0xa3>
jmp    11b8 <Hex_decode+0xa8>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    123e <Hex_decode+0x12e>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsbl %dl,%edi
movsbl (%rax,%rcx,1),%esi
call   1030 <Hex_decodeByte@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1208 <Hex_decode+0xf8>
movl   $0xffffffff,-0x4(%rbp)
jmp    124c <Hex_decode+0x13c>
mov    -0x2c(%rbp),%eax
mov    %al,-0x49(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x49(%rbp),%dl
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x28(%rbp)
jmp    11bf <Hex_decode+0xaf>
mov    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Hex_decode>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r15d
shr    $0x1f,%r15d
add    %ecx,%r15d
sar    %r15d
mov    $0xfffffffe,%eax
cmp    %esi,%r15d
jg     117f <Hex_decode+0x6f>
mov    %ecx,%r14d
mov    %rdx,%r12
mov    %rdi,%rbp
jge    1142 <Hex_decode+0x32>
movslq %r15d,%rax
movb   $0x0,0x0(%rbp,%rax,1)
test   %r14d,%r14d
jle    1175 <Hex_decode+0x65>
xor    %ebx,%ebx
nopl   0x0(%rax)
movsbl (%r12,%rbx,1),%edi
movsbl 0x1(%r12,%rbx,1),%esi
call   1030 <Hex_decodeByte@plt>
cmp    $0xffffffff,%eax
je     117a <Hex_decode+0x6a>
mov    %al,0x0(%rbp)
add    $0x2,%rbx
add    $0x1,%rbp
cmp    %r14d,%ebx
jl     1150 <Hex_decode+0x40>
mov    %r15d,%eax
jmp    117f <Hex_decode+0x6f>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @Hex_decode(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i32, i32* %9
%13 = srem i32 %12, 1
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %4
store i32 -1, i32* %5
br label %70
16: ; preds = %4
%17 = load i32, i32* %7
%18 = load i32, i32* %9
%19 = sdiv i32 %18, 2
%20 = icmp slt i32 %17, %19
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 -2, i32* %5
br label %70
22: ; preds = %16
%23 = load i32, i32* %7
%24 = load i32, i32* %9
%25 = sdiv i32 %24, 2
%26 = icmp sgt i32 %23, %25
br i1 %26, label %27, label %33
27: ; preds = %22
%28 = load i8*, i8** %6
%29 = load i32, i32* %9
%30 = sdiv i32 %29, 2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
store i8 0, i8* %32
br label %33
33: ; preds = %27, %22
br label %34
34: ; preds = %33
br label %35
35: ; preds = %34
store i32 0, i32* %10
br label %36
36: ; preds = %64, %35
%37 = load i32, i32* %10
%38 = load i32, i32* %9
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %67
40: ; preds = %36
%41 = load i8*, i8** %8
%42 = load i32, i32* %10
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i8, i8* %44
%46 = load i8*, i8** %8
%47 = load i32, i32* %10
%48 = add nsw i32 %47, 1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
%51 = load i8, i8* %50
%52 = call i32 @Hex_decodeByte(i8 signext %45, i8 signext %51)
store i32 %52, i32* %11
%53 = load i32, i32* %11
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %56
55: ; preds = %40
store i32 -1, i32* %5
br label %70
56: ; preds = %40
%57 = load i32, i32* %11
%58 = trunc i32 %57 to i8
%59 = load i8*, i8** %6
%60 = load i32, i32* %10
%61 = sdiv i32 %60, 2
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %59, i64 %62
store i8 %58, i8* %63
br label %64
64: ; preds = %56
%65 = load i32, i32* %10
%66 = add nsw i32 %65, 2
store i32 %66, i32* %10
br label %36
67: ; preds = %36
%68 = load i32, i32* %9
%69 = sdiv i32 %68, 2
store i32 %69, i32* %5
br label %70
70: ; preds = %67, %55, %21, %15
%71 = load i32, i32* %5
ret i32 %71
}

LLVM IR (-O2):
define i32 @Hex_decode(i8* nocapture writeonly %0, i32 %1, i8* nocapture %2, i32 %3) {
%5 = sdiv i32 %3, 2
%6 = icmp sgt i32 %5, %1
br i1 %6, label %30, label %7
7: ; preds = %4
%8 = icmp slt i32 %5, %1
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = sext i32 %5 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 0, i8* %11
br label %12
12: ; preds = %9, %7
%13 = icmp sgt i32 %3, 0
br i1 %13, label %14, label %30
14: ; preds = %12, %23
%15 = phi i64 [ %27, %23 ], [ 0, %12 ]
%16 = getelementptr inbounds i8, i8* %2, i64 %15
%17 = load i8, i8* %16
%18 = or i64 %15, 1
%19 = getelementptr inbounds i8, i8* %2, i64 %18
%20 = load i8, i8* %19
%21 = tail call i32 @Hex_decodeByte(i8 signext %17, i8 signext %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %30, label %23
23: ; preds = %14
%24 = trunc i32 %21 to i8
%25 = lshr exact i64 %15, 1
%26 = getelementptr inbounds i8, i8* %0, i64 %25
store i8 %24, i8* %26
%27 = add nuw i64 %15, 2
%28 = trunc i64 %27 to i32
%29 = icmp slt i32 %28, %3
br i1 %29, label %14, label %30
30: ; preds = %14, %23, %12, %4
%31 = phi i32 [ -2, %4 ], [ %5, %12 ], [ -1, %14 ], [ %5, %23 ]
ret i32 %31
}