#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);
int strlen(char const *);
void bbp_parse_hex(uint8_t *v, const char *str) {
  const size_t count = strlen(str) / 2;
  size_t i;
  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;
  }
}


x86 assembly (clang, -O0):
<bbp_parse_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11b9 <bbp_parse_hex+0x99>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x1,%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <bbp_hex2byte@plt>
mov    %al,-0x21(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x1,%rcx
movsbl 0x1(%rax,%rcx,1),%edi
call   1030 <bbp_hex2byte@plt>
mov    %al,-0x22(%rbp)
movsbl -0x21(%rbp),%eax
shl    $0x4,%eax
movsbl -0x22(%rbp),%ecx
add    %ecx,%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    114f <bbp_parse_hex+0x2f>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bbp_parse_hex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%ecx
cmp    $0x3,%ecx
jb     118b <bbp_parse_hex+0x6b>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
cmp    $0x2,%ecx
mov    $0x1,%eax
cmovae %ecx,%eax
movslq %eax,%r12
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,2),%edi
call   1030 <bbp_hex2byte@plt>
mov    %eax,%ebp
movsbl 0x1(%r15,%rbx,2),%edi
call   1030 <bbp_hex2byte@plt>
shl    $0x4,%bpl
add    %al,%bpl
mov    %bpl,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1160 <bbp_parse_hex+0x40>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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