#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(int);
int /*<<< orphan*/ sscanf(char *, char *, unsigned char *);
int strlen(char *);
unsigned char *hex2bin(char *in, int *len) {
  *len = strlen(in) / 2;
  unsigned char *out = malloc(*len);
  int i;
  for (i = 0; i < *len; i++) {
    sscanf(in + (i * 2), "%2hhx", out + i);
  }
  return out;
}


x86 assembly (clang, -O0):
<hex2bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jge    11b2 <hex2bin+0x82>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x18(%rbp),%rdx
movslq -0x1c(%rbp),%rax
add    %rax,%rdx
lea    0xe61(%rip),%rsi
call   1050 <sscanf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116f <hex2bin+0x3f>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2bin>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %eax,%edi
shr    $0x1f,%edi
add    %eax,%edi
sar    %edi
mov    %edi,(%r14)
call   1040 <malloc@plt>
mov    %rax,%r15
cmp    $0x2,%ebp
jl     118f <hex2bin+0x5f>
xor    %ebp,%ebp
lea    0xe99(%rip),%r12
nopw   0x0(%rax,%rax,1)
lea    (%r15,%rbp,1),%rdx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <sscanf@plt>
add    $0x1,%rbp
movslq (%r14),%rax
add    $0x2,%rbx
cmp    %rax,%rbp
jl     1170 <hex2bin+0x40>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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