#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 char uint8_t;

/* Variables and functions */
char bbp_hex2byte(char const);
char *malloc(size_t const);
int strlen(char const *);
uint8_t *bbp_alloc_hex(const char *str, size_t *len) {
  const size_t count = strlen(str) / 2;
  size_t i;
  uint8_t *v = malloc(count);
  for (i = 0; i < count; ++i) {
    const char hi = bbp_hex2byte(str[i * 2]);
    const char lo = bbp_hex2byte(str[i * 2 + 1]);
    v[i] = hi * 16 + lo;
  }
  *len = count;
  return v;
}


x86 assembly (clang, -O0):
<bbp_alloc_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11d6 <bbp_alloc_hex+0xa6>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x1,%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <bbp_hex2byte@plt>
mov    %al,-0x29(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x1,%rcx
movsbl 0x1(%rax,%rcx,1),%edi
call   1030 <bbp_hex2byte@plt>
mov    %al,-0x2a(%rbp)
movsbl -0x29(%rbp),%eax
shl    $0x4,%eax
movsbl -0x2a(%rbp),%ecx
add    %ecx,%eax
mov    %al,%dl
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    116c <bbp_alloc_hex+0x3c>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbp_alloc_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,(%rsp)
mov    %rdi,%rbp
call   1040 <strlen@plt>
mov    %eax,%ebx
shr    $0x1f,%eax
add    %ebx,%eax
sar    %eax
movslq %eax,%r15
mov    %r15,%rdi
call   1050 <malloc@plt>
mov    %rax,%r12
add    $0x1,%ebx
cmp    $0x3,%ebx
jb     11aa <bbp_alloc_hex+0x7a>
cmp    $0x2,%r15
mov    $0x1,%r13d
cmovae %r15,%r13
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
movsbl 0x0(%rbp,%r14,2),%edi
call   1030 <bbp_hex2byte@plt>
mov    %eax,%ebx
movsbl 0x1(%rbp,%r14,2),%edi
call   1030 <bbp_hex2byte@plt>
shl    $0x4,%bl
add    %al,%bl
mov    %bl,(%r12,%r14,1)
add    $0x1,%r14
cmp    %r14,%r13
jne    1180 <bbp_alloc_hex+0x50>
mov    (%rsp),%rax
mov    %r15,(%rax)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @bbp_alloc_hex(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8
%9 = alloca i8
store i8* %0, i8** %3
store i64* %1, i64** %4
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = sdiv i32 %11, 2
%13 = sext i32 %12 to i64
store i64 %13, i64* %5
%14 = load i64, i64* %5
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %7
store i64 0, i64* %6
br label %16
16: ; preds = %44, %2
%17 = load i64, i64* %6
%18 = load i64, i64* %5
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %47
20: ; preds = %16
%21 = load i8*, i8** %3
%22 = load i64, i64* %6
%23 = mul i64 %22, 2
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = call signext i8 @bbp_hex2byte(i8 signext %25)
store i8 %26, i8* %8
%27 = load i8*, i8** %3
%28 = load i64, i64* %6
%29 = mul i64 %28, 2
%30 = add i64 %29, 1
%31 = getelementptr inbounds i8, i8* %27, i64 %30
%32 = load i8, i8* %31
%33 = call signext i8 @bbp_hex2byte(i8 signext %32)
store i8 %33, i8* %9
%34 = load i8, i8* %8
%35 = sext i8 %34 to i32
%36 = mul nsw i32 %35, 16
%37 = load i8, i8* %9
%38 = sext i8 %37 to i32
%39 = add nsw i32 %36, %38
%40 = trunc i32 %39 to i8
%41 = load i8*, i8** %7
%42 = load i64, i64* %6
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8 %40, i8* %43
br label %44
44: ; preds = %20
%45 = load i64, i64* %6
%46 = add i64 %45, 1
store i64 %46, i64* %6
br label %16
47: ; preds = %16
%48 = load i64, i64* %5
%49 = load i64*, i64** %4
store i64 %48, i64* %49
%50 = load i8*, i8** %7
ret i8* %50
}

LLVM IR (-O2):
define noalias i8* @bbp_alloc_hex(i8* %0, i64* nocapture writeonly %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = sdiv i32 %3, 2
%5 = sext i32 %4 to i64
%6 = tail call i8* @malloc(i64 %5)
%7 = add i32 %3, 1
%8 = icmp ult i32 %7, 3
br i1 %8, label %26, label %9
9: ; preds = %2
%10 = call i64 @llvm.umax.i64(i64 %5, i64 1)
br label %11
11: ; preds = %9, %11
%12 = phi i64 [ %24, %11 ], [ 0, %9 ]
%13 = shl i64 %12, 1
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = tail call signext i8 @bbp_hex2byte(i8 signext %15)
%17 = or i64 %13, 1
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
%20 = tail call signext i8 @bbp_hex2byte(i8 signext %19)
%21 = shl i8 %16, 4
%22 = add i8 %20, %21
%23 = getelementptr inbounds i8, i8* %6, i64 %12
store i8 %22, i8* %23
%24 = add nuw i64 %12, 1
%25 = icmp eq i64 %24, %10
br i1 %25, label %26, label %11
26: ; preds = %11, %2
store i64 %5, i64* %1
ret i8* %6
}