#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 strlen(unsigned char const *);
unsigned char strtol(char *, char **, int);
int hex2data(unsigned char *data, const unsigned char *hexstring,
             unsigned int len) {
  unsigned const char *pos = hexstring;
  char *endptr;
  size_t count = 0;
  if ((hexstring[0] == '\0') || (strlen(hexstring) % 2)) {
    return -1;
  }
  for (count = 0; count < len; count++) {
    char buf[3] = {pos[0], pos[1], 0};
    data[count] = strtol(buf, &endptr, 16);
    pos += 2 * sizeof(char);
    if (endptr[0] != '\0') {
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<hex2data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
je     116d <hex2data+0x4d>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1179 <hex2data+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <hex2data+0xec>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x1c(%rbp),%ecx
cmp    %rcx,%rax
jae    1205 <hex2data+0xe5>
mov    -0x28(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x3b(%rbp)
mov    -0x28(%rbp),%rax
mov    0x1(%rax),%al
mov    %al,-0x3a(%rbp)
movb   $0x0,-0x39(%rbp)
lea    -0x3b(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11ef <hex2data+0xcf>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <hex2data+0xec>
jmp    11f4 <hex2data+0xd4>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1181 <hex2data+0x61>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
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    $0xffffffff,%ebp
cmpb   $0x0,(%rsi)
je     11b9 <hex2data+0x99>
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r13
mov    %rsi,%rdi
call   1030 <strlen@plt>
test   $0x1,%al
jne    11b9 <hex2data+0x99>
test   %r15d,%r15d
je     11b0 <hex2data+0x90>
mov    %r15d,%ebp
xor    %r14d,%r14d
lea    0xd(%rsp),%r15
lea    0x10(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
movzbl (%rbx,%r14,2),%eax
mov    %al,0xd(%rsp)
movzbl 0x1(%rbx,%r14,2),%eax
mov    %al,0xe(%rsp)
movb   $0x0,0xf(%rsp)
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %al,0x0(%r13,%r14,1)
mov    0x10(%rsp),%rax
cmpb   $0x0,(%rax)
jne    11b4 <hex2data+0x94>
add    $0x1,%r14
cmp    %r14,%rbp
jne    1170 <hex2data+0x50>
xor    %ebp,%ebp
jmp    11b9 <hex2data+0x99>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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