#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *malloc(size_t);
int /*<<< orphan*/ sscanf(char const *, char *, char *);
size_t strlen(char const *);
char *hex_string_to_bin(const char *hex_string) {
  size_t len = strlen(hex_string);
  char *val = malloc(len);
  if (val == NULL)
    exit(EXIT_FAILURE);
  size_t i;
  for (i = 0; i < len; ++i, hex_string += 2)
    sscanf(hex_string, "%2hhx", &val[i]);
  return val;
}


x86 assembly (clang, -O0):
<hex_string_to_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117f <hex_string_to_bin+0x3f>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11ca <hex_string_to_bin+0x8a>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
add    -0x20(%rbp),%rdx
lea    0xe58(%rip),%rsi
call   1050 <sscanf@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
jmp    1187 <hex_string_to_bin+0x47>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_string_to_bin>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r13
call   1030 <strlen@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     11a9 <hex_string_to_bin+0x69>
mov    %rax,%r15
test   %r14,%r14
je     119c <hex_string_to_bin+0x5c>
lea    0xe90(%rip),%r12
mov    %r15,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1050 <sscanf@plt>
add    $0x2,%r13
add    $0x1,%rbx
add    $0xffffffffffffffff,%r14
jne    1180 <hex_string_to_bin+0x40>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

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

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