#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 /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ fmt_hex_byte(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) {
    return 0;
  }
  size_t final_len = len >> 1;
  for (size_t i = 0, j = 0; j < final_len; i += 2, j++) {
    out[j] = fmt_hex_byte(hex + i);
  }
  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     115c <fmt_hex_bytes+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    11c5 <fmt_hex_bytes+0xa5>
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    11bd <fmt_hex_bytes+0x9d>
mov    -0x18(%rbp),%rdi
add    -0x30(%rbp),%rdi
call   1030 <fmt_hex_byte@plt>
mov    %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    1178 <fmt_hex_bytes+0x58>
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   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r13
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <fmt_strlen@plt>
test   $0x1,%al
jne    117b <fmt_hex_bytes+0x5b>
mov    %rax,%r14
shr    %r14
cmp    $0x2,%rax
jb     117e <fmt_hex_bytes+0x5e>
cmp    $0x2,%r14
mov    $0x1,%r12d
cmovae %r14,%r12
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1030 <fmt_hex_byte@plt>
mov    %eax,(%r15,%rbx,4)
add    $0x1,%rbx
add    $0x2,%r13
cmp    %rbx,%r12
jne    1160 <fmt_hex_bytes+0x40>
jmp    117e <fmt_hex_bytes+0x5e>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
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 i64 0, i64* %3
br label %38
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 = %31, %16
%20 = load i64, i64* %9
%21 = load i64, i64* %7
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %36
23: ; preds = %19
%24 = load i8*, i8** %5
%25 = load i64, i64* %8
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = call i32 @fmt_hex_byte(i8* %26)
%28 = load i32*, i32** %4
%29 = load i64, i64* %9
%30 = getelementptr inbounds i32, i32* %28, i64 %29
store i32 %27, i32* %30
br label %31
31: ; preds = %23
%32 = load i64, i64* %8
%33 = add i64 %32, 2
store i64 %33, i64* %8
%34 = load i64, i64* %9
%35 = add i64 %34, 1
store i64 %35, i64* %9
br label %19
36: ; preds = %19
%37 = load i64, i64* %7
store i64 %37, i64* %3
br label %38
38: ; preds = %36, %15
%39 = load i64, i64* %3
ret i64 %39
}

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 %20
6: ; preds = %2
%7 = lshr i64 %3, 1
%8 = icmp ult i64 %3, 2
br i1 %8, label %20, 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 [ %18, %11 ], [ 0, %9 ]
%13 = phi i64 [ %17, %11 ], [ 0, %9 ]
%14 = getelementptr inbounds i8, i8* %1, i64 %13
%15 = tail call i32 @fmt_hex_byte(i8* %14)
%16 = getelementptr inbounds i32, i32* %0, i64 %12
store i32 %15, i32* %16
%17 = add nuw i64 %13, 2
%18 = add nuw nsw i64 %12, 1
%19 = icmp eq i64 %18, %10
br i1 %19, label %20, label %11
20: ; preds = %11, %6, %2
%21 = phi i64 [ 0, %2 ], [ %7, %6 ], [ %7, %11 ]
ret i64 %21
}