#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*/ memcpy(char *, char *, int);
unsigned char strtol(char *, int /*<<< orphan*/ *, int);
char *hex_decode(char *str, unsigned char *out) {
  int i = 0, j = 0;
  char buf[3];
  buf[2] = 0;
  while (str[i] && str[i + 1]) {
    memcpy(buf, str + i, 2);
    out[j++] = strtol(buf, NULL, 16);
    i += 2;
  }
  return out;
}


x86 assembly (clang, -O0):
<hex_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movb   $0x0,-0x19(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1c(%rbp)
je     1176 <hex_decode+0x56>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x1c(%rbp)
mov    -0x1c(%rbp),%al
test   $0x1,%al
jne    1186 <hex_decode+0x66>
jmp    11d6 <hex_decode+0xb6>
lea    -0x1b(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
lea    -0x1b(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1030 <strtol@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
jmp    1142 <hex_decode+0x22>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_decode>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
movb   $0x0,0x7(%rsp)
cmpb   $0x0,(%rdi)
je     117a <hex_decode+0x5a>
mov    %rdi,%r12
lea    0x5(%rsp),%r15
mov    %r14,%rbx
cmpb   $0x0,0x1(%r12)
je     117a <hex_decode+0x5a>
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <strtol@plt>
mov    %al,(%rbx)
add    $0x1,%rbx
cmpb   $0x0,0x2(%r12)
lea    0x2(%r12),%r12
jne    1140 <hex_decode+0x20>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @hex_decode(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [3 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
store i32 0, i32* %6
%8 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 2
store i8 0, i8* %8
br label %9
9: ; preds = %28, %2
%10 = load i8*, i8** %3
%11 = load i32, i32* %5
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %9
%18 = load i8*, i8** %3
%19 = load i32, i32* %5
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %18, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 0
br label %26
26: ; preds = %17, %9
%27 = phi i1 [ false, %9 ], [ %25, %17 ]
br i1 %27, label %28, label %44
28: ; preds = %26
%29 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%30 = load i8*, i8** %3
%31 = load i32, i32* %5
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = call i32 @memcpy(i8* %29, i8* %33, i32 2)
%35 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%36 = call zeroext i8 @strtol(i8* %35, i32* null, i32 16)
%37 = load i8*, i8** %4
%38 = load i32, i32* %6
%39 = add nsw i32 %38, 1
store i32 %39, i32* %6
%40 = sext i32 %38 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 %36, i8* %41
%42 = load i32, i32* %5
%43 = add nsw i32 %42, 2
store i32 %43, i32* %5
br label %9
44: ; preds = %26
%45 = load i8*, i8** %4
ret i8* %45
}

LLVM IR (-O2):
define i8* @hex_decode(i8* %0, i8* returned 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 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %25, label %8
8: ; preds = %2, %16
%9 = phi i64 [ %21, %16 ], [ 0, %2 ]
%10 = phi i64 [ %19, %16 ], [ 0, %2 ]
%11 = phi i8* [ %22, %16 ], [ %0, %2 ]
%12 = or i64 %9, 1
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %25, label %16
16: ; preds = %8
%17 = call i32 @memcpy(i8* %4, i8* %11, i32 2)
%18 = call zeroext i8 @strtol(i8* nocapture %4, i32* null, i32 16)
%19 = add nuw i64 %10, 1
%20 = getelementptr inbounds i8, i8* %1, i64 %10
store i8 %18, i8* %20
%21 = add nuw i64 %9, 2
%22 = getelementptr inbounds i8, i8* %0, i64 %21
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 0
br i1 %24, label %25, label %8
25: ; preds = %8, %16, %2
ret i8* %1
}