#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 */
int /*<<< orphan*/ *malloc(size_t);
int /*<<< orphan*/ sscanf(char *, char *, int /*<<< orphan*/ *);
int strlen(char *);
uint8_t *hex_string_to_bin(char *hex_string) {
  size_t i, len = strlen(hex_string) / 2;
  uint8_t *ret = malloc(len);
  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 i32* @malloc(i64 %11)
store i32* %12, i32** %5
%13 = load i8*, i8** %2
store i8* %13, i8** %6
store i64 0, i64* %3
br label %14
14: ; preds = %24, %1
%15 = load i64, i64* %3
%16 = load i64, i64* %4
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %29
18: ; preds = %14
%19 = load i8*, i8** %6
%20 = load i32*, i32** %5
%21 = load i64, i64* %3
%22 = getelementptr inbounds i32, i32* %20, i64 %21
%23 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %22)
br label %24
24: ; preds = %18
%25 = load i64, i64* %3
%26 = add i64 %25, 1
store i64 %26, i64* %3
%27 = load i8*, i8** %6
%28 = getelementptr inbounds i8, i8* %27, i64 2
store i8* %28, i8** %6
br label %14
29: ; preds = %14
%30 = load i32*, i32** %5
ret i32* %30
}

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 i32* @malloc(i64 %4)
%6 = add i32 %2, 1
%7 = icmp ult i32 %6, 3
br i1 %7, label %18, label %8
8: ; preds = %1
%9 = call i64 @llvm.umax.i64(i64 %4, i64 1)
br label %10
10: ; preds = %8, %10
%11 = phi i8* [ %16, %10 ], [ %0, %8 ]
%12 = phi i64 [ %15, %10 ], [ 0, %8 ]
%13 = getelementptr inbounds i32, i32* %5, i64 %12
%14 = tail call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %13)
%15 = add nuw i64 %12, 1
%16 = getelementptr inbounds i8, i8* %11, i64 2
%17 = icmp eq i64 %15, %9
br i1 %17, label %18, label %10
18: ; preds = %10, %1
ret i32* %5
}