#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 /*<<< orphan*/ sscanf(char const *, char *, char *);
int hex_string_to_bin(const char *hex_string, size_t hex_len, char *output,
                      size_t output_size) {
  if (output_size == 0 || hex_len != output_size * 2) {
    return -1;
  }
  for (size_t i = 0; i < output_size; ++i) {
    sscanf(hex_string, "%2hhx", &output[i]);
    hex_string += 2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<hex_string_to_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1148 <hex_string_to_bin+0x38>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
shl    $0x1,%rcx
cmp    %rcx,%rax
je     1154 <hex_string_to_bin+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <hex_string_to_bin+0x96>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    119f <hex_string_to_bin+0x8f>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
add    -0x30(%rbp),%rdx
lea    0xe83(%rip),%rsi
call   1030 <sscanf@plt>
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    115c <hex_string_to_bin+0x4c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_string_to_bin>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%eax
test   %rcx,%rcx
je     115e <hex_string_to_bin+0x4e>
mov    %rcx,%r14
add    %rcx,%rcx
cmp    %rsi,%rcx
jne    115e <hex_string_to_bin+0x4e>
mov    %rdx,%r15
mov    %rdi,%rbx
lea    0xec6(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1030 <sscanf@plt>
add    $0x2,%rbx
add    $0x1,%r15
add    $0xffffffffffffffff,%r14
jne    1140 <hex_string_to_bin+0x30>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%2hhx\00"
define i32 @hex_string_to_bin(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%11 = load i64, i64* %9
%12 = icmp eq i64 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %4
%14 = load i64, i64* %7
%15 = load i64, i64* %9
%16 = mul i64 %15, 2
%17 = icmp ne i64 %14, %16
br i1 %17, label %18, label %19
18: ; preds = %13, %4
store i32 -1, i32* %5
br label %36
19: ; preds = %13
store i64 0, i64* %10
br label %20
20: ; preds = %32, %19
%21 = load i64, i64* %10
%22 = load i64, i64* %9
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %35
24: ; preds = %20
%25 = load i8*, i8** %6
%26 = load i8*, i8** %8
%27 = load i64, i64* %10
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = call i32 @sscanf(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = load i8*, i8** %6
%31 = getelementptr inbounds i8, i8* %30, i64 2
store i8* %31, i8** %6
br label %32
32: ; preds = %24
%33 = load i64, i64* %10
%34 = add i64 %33, 1
store i64 %34, i64* %10
br label %20
35: ; preds = %20
store i32 0, i32* %5
br label %36
36: ; preds = %35, %18
%37 = load i32, i32* %5
ret i32 %37
}

LLVM IR (-O2):
@.str = [6 x i8] c"%2hhx\00"
define i32 @hex_string_to_bin(i8* nocapture %0, i64 %1, i8* %2, i64 %3) {
%5 = icmp ne i64 %3, 0
%6 = shl i64 %3, 1
%7 = icmp eq i64 %6, %1
%8 = and i1 %5, %7
br i1 %8, label %9, label %17
9: ; preds = %4, %9
%10 = phi i64 [ %15, %9 ], [ 0, %4 ]
%11 = phi i8* [ %14, %9 ], [ %0, %4 ]
%12 = getelementptr inbounds i8, i8* %2, i64 %10
%13 = tail call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = getelementptr inbounds i8, i8* %11, i64 2
%15 = add nuw i64 %10, 1
%16 = icmp eq i64 %15, %3
br i1 %16, label %17, label %9
17: ; preds = %9, %4
%18 = phi i32 [ -1, %4 ], [ 0, %9 ]
ret i32 %18
}