#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 EINVAL;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int sscanf(char const *, char *, unsigned char *);
int /*<<< orphan*/ stderr;
size_t strlen(char const *);
unsigned char *xcalloc(int, int);
unsigned char *xmalloc(size_t);
int hex_to_bytes(const char *hex, unsigned char **bytes) {
  size_t len = strlen(hex);
  if (len % 2 != 0) {
    if (!*bytes)
      *bytes = xcalloc(1, 1);
    **bytes = '\0';
    return -EINVAL;
  }
  if (!*bytes)
    *bytes = xmalloc(len / 2 + 1);
  for (size_t i = 0; i < len / 2; ++i) {
    if (sscanf(&hex[i * 2], "%2hhx", (unsigned char *)(*bytes + i)) != 1) {
      fprintf(stderr, "%s\n", hex);
      **bytes = '\0';
      return -EINVAL;
    }
  }
  (*bytes)[len / 2] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<hex_to_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     11c0 <hex_to_bytes+0x70>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11a3 <hex_to_bytes+0x53>
mov    $0x1,%esi
mov    %esi,%edi
call   1070 <xcalloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movb   $0x0,(%rax)
xor    %eax,%eax
mov    0x2e22(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    129b <hex_to_bytes+0x14b>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11e9 <hex_to_bytes+0x99>
mov    -0x20(%rbp),%rdi
shr    $0x1,%rdi
add    $0x1,%rdi
call   1040 <xmalloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
shr    $0x1,%rcx
cmp    %rcx,%rax
jae    1281 <hex_to_bytes+0x131>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
add    -0x28(%rbp),%rdx
lea    0xdd9(%rip),%rsi
call   1060 <sscanf@plt>
cmp    $0x1,%eax
je     126b <hex_to_bytes+0x11b>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdbd(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movb   $0x0,(%rax)
xor    %eax,%eax
mov    0x2d77(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    129b <hex_to_bytes+0x14b>
jmp    1270 <hex_to_bytes+0x120>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11f1 <hex_to_bytes+0xa1>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
shr    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_to_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    (%r12),%rdx
test   $0x1,%bl
jne    1185 <hex_to_bytes+0x35>
mov    %rbx,%r13
shr    %r13
test   %rdx,%rdx
je     11a8 <hex_to_bytes+0x58>
cmp    $0x2,%rbx
jae    11be <hex_to_bytes+0x6e>
jmp    1204 <hex_to_bytes+0xb4>
test   %rdx,%rdx
jne    11a0 <hex_to_bytes+0x50>
mov    $0x1,%edi
mov    $0x1,%esi
call   1070 <xcalloc@plt>
mov    %rax,%rdx
mov    %rax,(%r12)
movb   $0x0,(%rdx)
jmp    122d <hex_to_bytes+0xdd>
lea    0x1(%r13),%rdi
call   1040 <xmalloc@plt>
mov    %rax,%rdx
mov    %rax,(%r12)
cmp    $0x2,%rbx
jb     1204 <hex_to_bytes+0xb4>
cmp    $0x2,%r13
mov    $0x1,%ebp
cmovae %r13,%rbp
mov    %r14,%rbx
xor    %r14d,%r14d
lea    0xe28(%rip),%r15
mov    %rbx,(%rsp)
nopl   0x0(%rax)
add    %r14,%rdx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    120d <hex_to_bytes+0xbd>
add    $0x1,%r14
mov    (%r12),%rdx
add    $0x2,%rbx
cmp    %r14,%rbp
jne    11e0 <hex_to_bytes+0x90>
movb   $0x0,(%rdx,%r13,1)
xor    %eax,%eax
jmp    1238 <hex_to_bytes+0xe8>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
mov    (%rsp),%rdx
call   1050 <fprintf@plt>
mov    (%r12),%rax
movb   $0x0,(%rax)
mov    0x2da4(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@.str = [6 x i8] c"%2hhx\00"
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @hex_to_bytes(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
%8 = load i8*, i8** %4
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = urem i64 %10, 2
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %25
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = load i8*, i8** %14
%16 = icmp ne i8* %15, null
br i1 %16, label %20, label %17
17: ; preds = %13
%18 = call i8* @xcalloc(i32 1, i32 1)
%19 = load i8**, i8*** %5
store i8* %18, i8** %19
br label %20
20: ; preds = %17, %13
%21 = load i8**, i8*** %5
%22 = load i8*, i8** %21
store i8 0, i8* %22
%23 = load i32, i32* @EINVAL
%24 = sub nsw i32 0, %23
store i32 %24, i32* %3
br label %70
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = load i8*, i8** %26
%28 = icmp ne i8* %27, null
br i1 %28, label %35, label %29
29: ; preds = %25
%30 = load i64, i64* %6
%31 = udiv i64 %30, 2
%32 = add i64 %31, 1
%33 = call i8* @xmalloc(i64 %32)
%34 = load i8**, i8*** %5
store i8* %33, i8** %34
br label %35
35: ; preds = %29, %25
store i64 0, i64* %7
br label %36
36: ; preds = %61, %35
%37 = load i64, i64* %7
%38 = load i64, i64* %6
%39 = udiv i64 %38, 2
%40 = icmp ult i64 %37, %39
br i1 %40, label %41, label %64
41: ; preds = %36
%42 = load i8*, i8** %4
%43 = load i64, i64* %7
%44 = mul i64 %43, 2
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8**, i8*** %5
%47 = load i8*, i8** %46
%48 = load i64, i64* %7
%49 = getelementptr inbounds i8, i8* %47, i64 %48
%50 = call i32 @sscanf(i8* %45, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %49)
%51 = icmp ne i32 %50, 1
br i1 %51, label %52, label %60
52: ; preds = %41
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %4
%55 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %54)
%56 = load i8**, i8*** %5
%57 = load i8*, i8** %56
store i8 0, i8* %57
%58 = load i32, i32* @EINVAL
%59 = sub nsw i32 0, %58
store i32 %59, i32* %3
br label %70
60: ; preds = %41
br label %61
61: ; preds = %60
%62 = load i64, i64* %7
%63 = add i64 %62, 1
store i64 %63, i64* %7
br label %36
64: ; preds = %36
%65 = load i8**, i8*** %5
%66 = load i8*, i8** %65
%67 = load i64, i64* %6
%68 = udiv i64 %67, 2
%69 = getelementptr inbounds i8, i8* %66, i64 %68
store i8 0, i8* %69
store i32 0, i32* %3
br label %70
70: ; preds = %64, %52, %20
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
@EINVAL = global i32 0
@.str = [6 x i8] c"%2hhx\00"
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @hex_to_bytes(i8* %0, i8** nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = and i64 %3, 1
%5 = icmp eq i64 %4, 0
%6 = load i8*, i8** %1
%7 = icmp eq i8* %6, null
br i1 %5, label %15, label %8
8: ; preds = %2
br i1 %7, label %9, label %11
9: ; preds = %8
%10 = tail call i8* @xcalloc(i32 1, i32 1)
store i8* %10, i8** %1
br label %11
11: ; preds = %9, %8
%12 = phi i8* [ %10, %9 ], [ %6, %8 ]
store i8 0, i8* %12
%13 = load i32, i32* @EINVAL
%14 = sub nsw i32 0, %13
br label %46
15: ; preds = %2
%16 = lshr i64 %3, 1
br i1 %7, label %17, label %20
17: ; preds = %15
%18 = add nuw i64 %16, 1
%19 = tail call i8* @xmalloc(i64 %18)
store i8* %19, i8** %1
br label %20
20: ; preds = %15, %17
%21 = phi i8* [ %19, %17 ], [ %6, %15 ]
%22 = icmp ult i64 %3, 2
br i1 %22, label %43, label %23
23: ; preds = %20
%24 = call i64 @llvm.umax.i64(i64 %16, i64 1)
br label %28
25: ; preds = %28
%26 = icmp eq i64 %36, %24
%27 = load i8*, i8** %1
br i1 %26, label %43, label %28
28: ; preds = %25, %23
%29 = phi i8* [ %21, %23 ], [ %27, %25 ]
%30 = phi i64 [ 0, %23 ], [ %36, %25 ]
%31 = shl nuw i64 %30, 1
%32 = getelementptr inbounds i8, i8* %0, i64 %31
%33 = getelementptr inbounds i8, i8* %29, i64 %30
%34 = tail call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %33)
%35 = icmp eq i32 %34, 1
%36 = add nuw nsw i64 %30, 1
br i1 %35, label %25, label %37
37: ; preds = %28
%38 = load i32, i32* @stderr
%39 = tail call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%40 = load i8*, i8** %1
store i8 0, i8* %40
%41 = load i32, i32* @EINVAL
%42 = sub nsw i32 0, %41
br label %46
43: ; preds = %25, %20
%44 = phi i8* [ %21, %20 ], [ %27, %25 ]
%45 = getelementptr inbounds i8, i8* %44, i64 %16
store i8 0, i8* %45
br label %46
46: ; preds = %37, %43, %11
%47 = phi i32 [ %14, %11 ], [ 0, %43 ], [ %42, %37 ]
ret i32 %47
}