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

/* Variables and functions */
int _hex_nib(char const);
size_t fmt_strlen(char const *);
size_t fmt_hex_bytes(uint8_t *out, const char *hex) {
  size_t len = fmt_strlen(hex);
  if (len & 1) {
    out = NULL;
    return 0;
  }
  size_t final_len = len >> 1;
  for (size_t i = 0, j = 0; j < final_len; i += 2, j++) {
    out[j] = (_hex_nib(hex[i]) << 4) | _hex_nib(hex[i + 1]);
  }
  return final_len;
}


x86 assembly (clang, -O0):
<fmt_hex_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fmt_strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     1164 <fmt_hex_bytes+0x44>
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x8(%rbp)
jmp    11ec <fmt_hex_bytes+0xcc>
mov    -0x20(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11e4 <fmt_hex_bytes+0xc4>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <_hex_nib@plt>
shl    $0x4,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl 0x1(%rax,%rcx,1),%edi
call   1030 <_hex_nib@plt>
mov    -0x3c(%rbp),%edx
or     %eax,%edx
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1180 <fmt_hex_bytes+0x60>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmt_hex_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <fmt_strlen@plt>
test   $0x1,%al
jne    118c <fmt_hex_bytes+0x6c>
mov    %rax,%r14
shr    %r14
cmp    $0x2,%rax
jb     118f <fmt_hex_bytes+0x6f>
cmp    $0x2,%r14
mov    $0x1,%r12d
cmovae %r14,%r12
xor    %ebx,%ebx
nopl   0x0(%rax)
movsbl 0x0(%r13,%rbx,2),%edi
call   1030 <_hex_nib@plt>
mov    %eax,%ebp
shl    $0x4,%ebp
movsbl 0x1(%r13,%rbx,2),%edi
call   1030 <_hex_nib@plt>
or     %ebp,%eax
mov    %eax,(%r15,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1160 <fmt_hex_bytes+0x40>
jmp    118f <fmt_hex_bytes+0x6f>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @fmt_hex_bytes(i32* %0, i8* %1) {
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32* %0, i32** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = call i64 @fmt_strlen(i8* %10)
store i64 %11, i64* %6
%12 = load i64, i64* %6
%13 = and i64 %12, 1
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32* null, i32** %4
store i64 0, i64* %3
br label %47
16: ; preds = %2
%17 = load i64, i64* %6
%18 = lshr i64 %17, 1
store i64 %18, i64* %7
store i64 0, i64* %8
store i64 0, i64* %9
br label %19
19: ; preds = %40, %16
%20 = load i64, i64* %9
%21 = load i64, i64* %7
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %45
23: ; preds = %19
%24 = load i8*, i8** %5
%25 = load i64, i64* %8
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = load i8, i8* %26
%28 = call i32 @_hex_nib(i8 signext %27)
%29 = shl i32 %28, 4
%30 = load i8*, i8** %5
%31 = load i64, i64* %8
%32 = add i64 %31, 1
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8, i8* %33
%35 = call i32 @_hex_nib(i8 signext %34)
%36 = or i32 %29, %35
%37 = load i32*, i32** %4
%38 = load i64, i64* %9
%39 = getelementptr inbounds i32, i32* %37, i64 %38
store i32 %36, i32* %39
br label %40
40: ; preds = %23
%41 = load i64, i64* %8
%42 = add i64 %41, 2
store i64 %42, i64* %8
%43 = load i64, i64* %9
%44 = add i64 %43, 1
store i64 %44, i64* %9
br label %19
45: ; preds = %19
%46 = load i64, i64* %7
store i64 %46, i64* %3
br label %47
47: ; preds = %45, %15
%48 = load i64, i64* %3
ret i64 %48
}

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