#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 nibble_from_char(unsigned char);
int strlen(char const *);
void hex_str_to_bytes(const char *in, unsigned char *out) {
  unsigned char *p;
  int len, i;
  len = strlen(in) / 2;
  for (i = 0, p = (unsigned char *)in; i < len; i++) {
    out[i] = (nibble_from_char(*p) << 4) | nibble_from_char(*(p + 1));
    p += 2;
  }
  out[len] = 0;
}


x86 assembly (clang, -O0):
<hex_str_to_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11ac <hex_str_to_bytes+0x8c>
mov    -0x18(%rbp),%rax
movzbl (%rax),%edi
call   1040 <nibble_from_char@plt>
shl    $0x4,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
movzbl 0x1(%rax),%edi
call   1040 <nibble_from_char@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
or     %ecx,%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1153 <hex_str_to_bytes+0x33>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_str_to_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r13
call   1030 <strlen@plt>
mov    %eax,%r15d
shr    $0x1f,%r15d
add    %eax,%r15d
sar    %r15d
cmp    $0x2,%eax
jl     117a <hex_str_to_bytes+0x5a>
mov    %r15d,%r12d
xor    %ebx,%ebx
nopl   (%rax)
movzbl 0x0(%r13,%rbx,2),%edi
call   1040 <nibble_from_char@plt>
mov    %eax,%ebp
shl    $0x4,%ebp
movzbl 0x1(%r13,%rbx,2),%edi
call   1040 <nibble_from_char@plt>
or     %ebp,%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1150 <hex_str_to_bytes+0x30>
movslq %r15d,%rax
movb   $0x0,(%r14,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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