#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 */
char *malloc(int);
int /*<<< orphan*/ sscanf(char const *, char *, unsigned int *);
char *hex_to_char(const char *hex, int len) {
  char *_a = malloc(len / 2);
  int num = 0;
  unsigned _b;
  while (num < len) {
    sscanf(&hex[num], "%02X", &_b);
    _a[num / 2] = _b;
    num += 2;
  }
  return _a;
}


x86 assembly (clang, -O0):
<hex_to_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11a9 <hex_to_char+0x89>
mov    -0x8(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
lea    0xe96(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <sscanf@plt>
mov    -0x20(%rbp),%eax
mov    %al,-0x29(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x29(%rbp),%dl
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
jmp    114c <hex_to_char+0x2c>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_to_char>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
mov    %esi,%edi
shr    $0x1f,%edi
add    %esi,%edi
sar    %edi
call   1030 <malloc@plt>
mov    %rax,0x10(%rsp)
test   %ebx,%ebx
jle    1183 <hex_to_char+0x63>
xor    %ebp,%ebp
lea    0xead(%rip),%r12
lea    0xc(%rsp),%r13
mov    0x10(%rsp),%r15
nopl   (%rax)
lea    (%r14,%rbp,1),%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1040 <sscanf@plt>
movzbl 0xc(%rsp),%eax
mov    %al,(%r15)
add    $0x2,%rbp
add    $0x1,%r15
cmp    %ebx,%ebp
jl     1160 <hex_to_char+0x40>
mov    0x10(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%02X\00"
define i8* @hex_to_char(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = sdiv i32 %8, 2
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %5
store i32 0, i32* %6
br label %11
11: ; preds = %15, %2
%12 = load i32, i32* %6
%13 = load i32, i32* %4
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %30
15: ; preds = %11
%16 = load i8*, i8** %3
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %7)
%21 = load i32, i32* %7
%22 = trunc i32 %21 to i8
%23 = load i8*, i8** %5
%24 = load i32, i32* %6
%25 = sdiv i32 %24, 2
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
store i8 %22, i8* %27
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 2
store i32 %29, i32* %6
br label %11
30: ; preds = %11
%31 = load i8*, i8** %5
ret i8* %31
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02X\00"
define noalias i8* @hex_to_char(i8* nocapture %0, i32 %1) {
%3 = alloca i32
%4 = sdiv i32 %1, 2
%5 = tail call i8* @malloc(i32 %4)
%6 = bitcast i32* %3 to i8*
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %19
8: ; preds = %2, %8
%9 = phi i64 [ %16, %8 ], [ 0, %2 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %3)
%12 = load i32, i32* %3
%13 = trunc i32 %12 to i8
%14 = lshr exact i64 %9, 1
%15 = getelementptr inbounds i8, i8* %5, i64 %14
store i8 %13, i8* %15
%16 = add nuw i64 %9, 2
%17 = trunc i64 %16 to i32
%18 = icmp slt i32 %17, %1
br i1 %18, label %8, label %19
19: ; preds = %8, %2
ret i8* %5
}