#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*/ exit(int);
unsigned char *malloc(unsigned int);
int /*<<< orphan*/ printf(char *);
int strlen(unsigned char const *);
unsigned char strtol(char *, char **, int /*<<< orphan*/);
unsigned char *hex2data(unsigned const char *hexstring, unsigned int len) {
  unsigned char *data = malloc((len / 2) * sizeof(char));
  ;
  unsigned const char *pos = hexstring;
  char *endptr;
  size_t count = 0;
  int index = 0;
  if ((hexstring[0] == '\0') || (strlen(hexstring) % 2)) {
    printf("hexstring contains no data or hexstring has an odd length.\n");
    exit(-1);
  }
  for (count = 0; count < len / 2; count++) {
    char buf[5] = {'0', 'x', pos[0], pos[1], 0};
    data[count] = strtol(buf, &endptr, 0);
    pos += 2 * sizeof(char);
    if (endptr[0] != '\0') {
      exit(-1);
    }
  }
  return data;
}


x86 assembly (clang, -O0):
<hex2data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
shr    $0x1,%eax
mov    %eax,%eax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x8(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
je     11b7 <hex2data+0x67>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11cd <hex2data+0x7d>
lea    0xe42(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0xc(%rbp),%ecx
shr    $0x1,%ecx
mov    %ecx,%ecx
cmp    %rcx,%rax
jae    1261 <hex2data+0x111>
movb   $0x30,-0x39(%rbp)
movb   $0x78,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x37(%rbp)
mov    -0x20(%rbp),%rax
mov    0x1(%rax),%al
mov    %al,-0x36(%rbp)
movb   $0x0,-0x35(%rbp)
lea    -0x39(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtol@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     124b <hex2data+0xfb>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    1250 <hex2data+0x100>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11d5 <hex2data+0x85>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    %rdi,%rbx
mov    %esi,%r12d
shr    %r12d
mov    %r12d,%edi
call   1060 <malloc@plt>
cmpb   $0x0,(%rbx)
je     1203 <hex2data+0xb3>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   $0x1,%al
jne    1203 <hex2data+0xb3>
cmp    $0x2,%r15d
jb     11f1 <hex2data+0xa1>
cmp    $0x2,%r12d
mov    $0x1,%r13d
cmovae %r12d,%r13d
xor    %ebp,%ebp
lea    0xb(%rsp),%r15
lea    0x10(%rsp),%r12
nopw   0x0(%rax,%rax,1)
movw   $0x7830,0xb(%rsp)
movzbl (%rbx,%rbp,2),%eax
mov    %al,0xd(%rsp)
movzbl 0x1(%rbx,%rbp,2),%eax
mov    %al,0xe(%rsp)
movb   $0x0,0xf(%rsp)
mov    %r15,%rdi
mov    %r12,%rsi
xor    %edx,%edx
call   1050 <strtol@plt>
mov    %al,(%r14,%rbp,1)
mov    0x10(%rsp),%rax
cmpb   $0x0,(%rax)
jne    120f <hex2data+0xbf>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    11b0 <hex2data+0x60>
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf6(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [60 x i8] c"hexstring contains no data or hexstring has an odd length.\0A\00"
define i8* @hex2data(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca [5 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
%11 = load i32, i32* %4
%12 = udiv i32 %11, 2
%13 = zext i32 %12 to i64
%14 = mul i64 %13, 1
%15 = trunc i64 %14 to i32
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %5
%17 = load i8*, i8** %3
store i8* %17, i8** %6
store i64 0, i64* %8
store i32 0, i32* %9
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i64 0
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = icmp eq i32 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %2
%24 = load i8*, i8** %3
%25 = call i32 @strlen(i8* %24)
%26 = srem i32 %25, 2
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23, %2
%29 = call i32 @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %23
store i64 0, i64* %8
br label %32
32: ; preds = %65, %31
%33 = load i64, i64* %8
%34 = load i32, i32* %4
%35 = udiv i32 %34, 2
%36 = zext i32 %35 to i64
%37 = icmp ult i64 %33, %36
br i1 %37, label %38, label %68
38: ; preds = %32
%39 = getelementptr inbounds [5 x i8], [5 x i8]* %10, i64 0, i64 0
store i8 48, i8* %39
%40 = getelementptr inbounds i8, i8* %39, i64 1
store i8 120, i8* %40
%41 = getelementptr inbounds i8, i8* %40, i64 1
%42 = load i8*, i8** %6
%43 = getelementptr inbounds i8, i8* %42, i64 0
%44 = load i8, i8* %43
store i8 %44, i8* %41
%45 = getelementptr inbounds i8, i8* %41, i64 1
%46 = load i8*, i8** %6
%47 = getelementptr inbounds i8, i8* %46, i64 1
%48 = load i8, i8* %47
store i8 %48, i8* %45
%49 = getelementptr inbounds i8, i8* %45, i64 1
store i8 0, i8* %49
%50 = getelementptr inbounds [5 x i8], [5 x i8]* %10, i64 0, i64 0
%51 = call zeroext i8 @strtol(i8* %50, i8** %7, i32 0)
%52 = load i8*, i8** %5
%53 = load i64, i64* %8
%54 = getelementptr inbounds i8, i8* %52, i64 %53
store i8 %51, i8* %54
%55 = load i8*, i8** %6
%56 = getelementptr inbounds i8, i8* %55, i64 2
store i8* %56, i8** %6
%57 = load i8*, i8** %7
%58 = getelementptr inbounds i8, i8* %57, i64 0
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %38
%63 = call i32 @exit(i32 -1)
unreachable
64: ; preds = %38
br label %65
65: ; preds = %64
%66 = load i64, i64* %8
%67 = add i64 %66, 1
store i64 %67, i64* %8
br label %32
68: ; preds = %32
%69 = load i8*, i8** %5
ret i8* %69
}

LLVM IR (-O2):
@str = [59 x i8] c"hexstring contains no data or hexstring has an odd length.\00"
define noalias i8* @hex2data(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca [5 x i8]
%5 = lshr i32 %1, 1
%6 = tail call i8* @malloc(i32 %5)
%7 = bitcast i8** %3 to i8*
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %24, label %10
10: ; preds = %2
%11 = tail call i32 @strlen(i8* %0)
%12 = and i32 %11, 1
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %10
%15 = icmp ult i32 %1, 2
br i1 %15, label %44, label %16
16: ; preds = %14
%17 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%18 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 1
%19 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 2
%20 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 3
%21 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 4
%22 = call i32 @llvm.umax.i32(i32 %5, i32 1)
%23 = zext i32 %22 to i64
br label %27
24: ; preds = %10, %2
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @str, i64 0, i64 0))
%26 = tail call i32 @exit(i32 -1)
unreachable
27: ; preds = %16, %40
%28 = phi i8* [ %0, %16 ], [ %41, %40 ]
%29 = phi i64 [ 0, %16 ], [ %42, %40 ]
store i8 48, i8* %17
store i8 120, i8* %18
%30 = load i8, i8* %28
store i8 %30, i8* %19
%31 = getelementptr inbounds i8, i8* %28, i64 1
%32 = load i8, i8* %31
store i8 %32, i8* %20
store i8 0, i8* %21
%33 = call zeroext i8 @strtol(i8* %17, i8** %3, i32 0)
%34 = getelementptr inbounds i8, i8* %6, i64 %29
store i8 %33, i8* %34
%35 = load i8*, i8** %3
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %27
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %27
%41 = getelementptr inbounds i8, i8* %28, i64 2
%42 = add nuw nsw i64 %29, 1
%43 = icmp eq i64 %42, %23
br i1 %43, label %44, label %27
44: ; preds = %40, %14
ret i8* %6
}