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


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    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11a8 <hex_string_to_bin+0x78>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
movslq -0x24(%rbp),%rax
add    %rax,%rdx
lea    0xe77(%rip),%rsi
call   1050 <sscanf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    1165 <hex_string_to_bin+0x35>
mov    -0x18(%rbp),%rax
add    $0x30,%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>
mov    %rax,%r15
test   %r14,%r14
je     117c <hex_string_to_bin+0x4c>
lea    0xea5(%rip),%r12
mov    %r15,%rbx
xchg   %ax,%ax
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1050 <sscanf@plt>
add    $0x2,%r13
add    $0x1,%rbx
add    $0xffffffffffffffff,%r14
jne    1160 <hex_string_to_bin+0x30>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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