#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 */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
scalar_t__ malloc(size_t);
int /*<<< orphan*/ sscanf(char const *, char *, int /*<<< orphan*/ *);
int strlen(char const *);
uint8_t *hex_string_to_bin(const char *hex_string) {
  size_t i, len = strlen(hex_string) / 2;
  uint8_t *ret = (uint8_t *)malloc(len);
  const char *pos = hex_string;
  for (i = 0; i < len; ++i, pos += 2) {
    sscanf(pos, "%2hhx", &ret[i]);
  }
  return ret;
}


x86 assembly (clang, -O0):
<hex_string_to_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11ba <hex_string_to_bin+0x8a>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe68(%rip),%rsi
call   1050 <sscanf@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
jmp    1170 <hex_string_to_bin+0x40>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_string_to_bin>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r12
call   1030 <strlen@plt>
mov    %eax,%ebp
shr    $0x1f,%eax
add    %ebp,%eax
sar    %eax
movslq %eax,%rbx
mov    %rbx,%rdi
call   1040 <malloc@plt>
mov    %rax,%r14
add    $0x1,%ebp
cmp    $0x3,%ebp
jb     119c <hex_string_to_bin+0x6c>
cmp    $0x2,%rbx
mov    $0x1,%ebp
cmovae %rbx,%rbp
lea    0xe8d(%rip),%r15
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <sscanf@plt>
add    $0x2,%r12
add    $0x4,%rbx
add    $0xffffffffffffffff,%rbp
jne    1180 <hex_string_to_bin+0x50>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"%2hhx\00"
define i32* @hex_string_to_bin(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = sdiv i32 %2, 2
%4 = sext i32 %3 to i64
%5 = tail call i64 @malloc(i64 %4)
%6 = inttoptr i64 %5 to i32*
%7 = add i32 %2, 1
%8 = icmp ult i32 %7, 3
br i1 %8, label %19, label %9
9: ; preds = %1
%10 = call i64 @llvm.umax.i64(i64 %4, i64 1)
br label %11
11: ; preds = %9, %11
%12 = phi i8* [ %17, %11 ], [ %0, %9 ]
%13 = phi i64 [ %16, %11 ], [ 0, %9 ]
%14 = getelementptr inbounds i32, i32* %6, i64 %13
%15 = tail call i32 @sscanf(i8* %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %14)
%16 = add nuw i64 %13, 1
%17 = getelementptr inbounds i8, i8* %12, i64 2
%18 = icmp eq i64 %16, %10
br i1 %18, label %19, label %11
19: ; preds = %11, %1
ret i32* %6
}