#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*/ free(char *);
char *malloc(unsigned int);
int /*<<< orphan*/ memmove(char *, char *, unsigned int);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
unsigned long strtoul(char *, int /*<<< orphan*/ *, int);
unsigned int hex2bytes(const char *hex, char **bytes) {
  const char *p;
  unsigned int i;
  char hexbyte[3];
  unsigned long num;
  char *buf;
  unsigned int bytesize;
  int byte;
  hexbyte[2] = '\0';
  if (hex == NULL)
    return 0;
  bytesize = strlen(hex) / 2;
  if (bytesize == 0)
    return 0;
  if (strlen(hex) % 2 == 0) {
    buf = (char *)malloc(bytesize);
    for (i = 0, p = hex; i < bytesize; i++, p += 2) {
      strncpy(hexbyte, p, 2);
      num = strtoul(hexbyte, NULL, 16);
      byte = (char)(num & 0xFF);
      buf[i] = byte;
    }
    *bytes = malloc(bytesize);
    memmove(*bytes, buf, bytesize);
    free(buf);
  } else {
    return 0;
  }
  return bytesize;
}


x86 assembly (clang, -O0):
<hex2bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movb   $0x0,-0x25(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118b <hex2bytes+0x2b>
movl   $0x0,-0x4(%rbp)
jmp    129c <hex2bytes+0x13c>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    11b5 <hex2bytes+0x55>
movl   $0x0,-0x4(%rbp)
jmp    129c <hex2bytes+0x13c>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    128a <hex2bytes+0x12a>
mov    -0x3c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jae    1257 <hex2bytes+0xf7>
lea    -0x27(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x2,%edx
call   1040 <strncpy@plt>
lea    -0x27(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
and    $0xff,%rax
movsbl %al,%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    %al,%dl
mov    -0x38(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    11ea <hex2bytes+0x8a>
mov    -0x3c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1070 <memmove@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    1296 <hex2bytes+0x136>
movl   $0x0,-0x4(%rbp)
jmp    129c <hex2bytes+0x13c>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movb   $0x0,0xf(%rsp)
xor    %r15d,%r15d
test   %rdi,%rdi
je     1236 <hex2bytes+0xd6>
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <strlen@plt>
mov    %eax,%ebp
lea    0x1(%rbp),%eax
cmp    $0x3,%eax
jb     1236 <hex2bytes+0xd6>
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   $0x1,%al
jne    1236 <hex2bytes+0xd6>
mov    %r14,0x10(%rsp)
mov    %ebp,%r15d
shr    $0x1f,%r15d
add    %ebp,%r15d
sar    %r15d
mov    %r15d,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
cmp    $0x2,%r15d
mov    $0x1,%r13d
cmovae %r15d,%r13d
xor    %r14d,%r14d
lea    0xd(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1040 <strncpy@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
mov    %al,(%r12,%r14,1)
add    $0x1,%r14
add    $0x2,%rbx
cmp    %r14,%r13
jne    11e0 <hex2bytes+0x80>
mov    %r15d,%edi
call   1060 <malloc@plt>
mov    0x10(%rsp),%rcx
mov    %rax,(%rcx)
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1070 <memmove@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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