#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__ calloc(unsigned int, int);
int hexToDec(char const);
unsigned int strlen(char const *);
int hexStringToBytes(unsigned char **buffer, const char *hexString) {
  unsigned int length = strlen(hexString);
  unsigned int size;
  int shift;
  int dec;
  unsigned int i;
  if ((length & 0x1) != 0)
    return -1;
  size = length / 2;
  if ((*buffer = (unsigned char *)calloc(size, 1)) == NULL)
    return -1;
  for (i = 0, shift = 4; i < length; i++) {
    if ((dec = hexToDec(hexString[i])) < 0)
      return -1;
    (*buffer)[i / 2] |= dec << shift;
    shift ^= 4;
  }
  return size;
}


x86 assembly (clang, -O0):
<hexStringToBytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1167 <hexStringToBytes+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <hexStringToBytes+0xe9>
mov    -0x1c(%rbp),%eax
shr    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    119a <hexStringToBytes+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <hexStringToBytes+0xe9>
movl   $0x0,-0x2c(%rbp)
movl   $0x4,-0x24(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    1213 <hexStringToBytes+0xe3>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
movsbl (%rax,%rcx,1),%edi
call   1050 <hexToDec@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    11dc <hexStringToBytes+0xac>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <hexStringToBytes+0xe9>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%ecx
shl    %cl,%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x2c(%rbp),%ecx
shr    $0x1,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%edx
or     %esi,%edx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
xor    $0x4,%eax
mov    %eax,-0x24(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a8 <hexStringToBytes+0x78>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexStringToBytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    $0xffffffff,%r14d
test   $0x1,%bpl
jne    11ca <hexStringToBytes+0x9a>
mov    %ebp,%r15d
shr    %r15d
mov    %r15d,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
je     11ca <hexStringToBytes+0x9a>
test   %ebp,%ebp
je     11bf <hexStringToBytes+0x8f>
mov    %ebp,%r13d
mov    $0x4,%ebp
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl (%rbx,%r14,1),%edi
call   1050 <hexToDec@plt>
test   %eax,%eax
js     11c4 <hexStringToBytes+0x94>
mov    %ebp,%ecx
shl    %cl,%eax
mov    (%r12),%rcx
mov    %r14d,%edx
shr    %edx
or     %al,(%rcx,%rdx,1)
xor    $0x4,%ebp
add    $0x1,%r14
cmp    %r14,%r13
jne    1190 <hexStringToBytes+0x60>
mov    %r15d,%r14d
jmp    11ca <hexStringToBytes+0x9a>
xor    %r14d,%r14d
jmp    11ca <hexStringToBytes+0x9a>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @hexStringToBytes(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8** %0, i8*** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %5
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = and i32 %13, 1
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %61
17: ; preds = %2
%18 = load i32, i32* %6
%19 = udiv i32 %18, 2
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = call i64 @calloc(i32 %20, i32 1)
%22 = inttoptr i64 %21 to i8*
%23 = load i8**, i8*** %4
store i8* %22, i8** %23
%24 = icmp eq i8* %22, null
br i1 %24, label %25, label %26
25: ; preds = %17
store i32 -1, i32* %3
br label %61
26: ; preds = %17
store i32 0, i32* %10
store i32 4, i32* %8
br label %27
27: ; preds = %56, %26
%28 = load i32, i32* %10
%29 = load i32, i32* %6
%30 = icmp ult i32 %28, %29
br i1 %30, label %31, label %59
31: ; preds = %27
%32 = load i8*, i8** %5
%33 = load i32, i32* %10
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = call i32 @hexToDec(i8 signext %36)
store i32 %37, i32* %9
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %31
store i32 -1, i32* %3
br label %61
40: ; preds = %31
%41 = load i32, i32* %9
%42 = load i32, i32* %8
%43 = shl i32 %41, %42
%44 = load i8**, i8*** %4
%45 = load i8*, i8** %44
%46 = load i32, i32* %10
%47 = udiv i32 %46, 2
%48 = zext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
%50 = load i8, i8* %49
%51 = zext i8 %50 to i32
%52 = or i32 %51, %43
%53 = trunc i32 %52 to i8
store i8 %53, i8* %49
%54 = load i32, i32* %8
%55 = xor i32 %54, 4
store i32 %55, i32* %8
br label %56
56: ; preds = %40
%57 = load i32, i32* %10
%58 = add i32 %57, 1
store i32 %58, i32* %10
br label %27
59: ; preds = %27
%60 = load i32, i32* %7
store i32 %60, i32* %3
br label %61
61: ; preds = %59, %39, %25, %16
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
define i32 @hexStringToBytes(i8** nocapture %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = and i32 %3, 1
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %34
6: ; preds = %2
%7 = lshr i32 %3, 1
%8 = tail call i64 @calloc(i32 %7, i32 1)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %0
%10 = icmp eq i64 %8, 0
br i1 %10, label %34, label %11
11: ; preds = %6
%12 = icmp eq i32 %3, 0
br i1 %12, label %34, label %13
13: ; preds = %11
%14 = zext i32 %3 to i64
br label %15
15: ; preds = %13, %22
%16 = phi i64 [ 0, %13 ], [ %32, %22 ]
%17 = phi i32 [ 4, %13 ], [ %31, %22 ]
%18 = getelementptr inbounds i8, i8* %1, i64 %16
%19 = load i8, i8* %18
%20 = tail call i32 @hexToDec(i8 signext %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %15
%23 = shl i32 %20, %17
%24 = load i8*, i8** %0
%25 = lshr i64 %16, 1
%26 = and i64 %25, 2147483647
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = trunc i32 %23 to i8
%30 = or i8 %28, %29
store i8 %30, i8* %27
%31 = xor i32 %17, 4
%32 = add nuw nsw i64 %16, 1
%33 = icmp eq i64 %32, %14
br i1 %33, label %34, label %15
34: ; preds = %15, %22, %11, %6, %2
%35 = phi i32 [ -1, %2 ], [ -1, %6 ], [ 0, %11 ], [ -1, %15 ], [ %7, %22 ]
ret i32 %35
}