#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*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ sscanf(char *, char *, unsigned int *);
int strlen(char const *);
int hex2bin(const char *hex, unsigned char *raw_out, int raw_max) {
  int hex_len = strlen(hex);
  if (!hex_len || hex_len % 2)
    return 0;
  int raw_len = hex_len / 2;
  if (raw_len > raw_max)
    raw_len = raw_max;
  char sub[3] = {0};
  unsigned int x;
  for (int i = 0; i < raw_len; i++) {
    memcpy(sub, hex + i * 2, 2);
    sscanf(sub, "%X", &x);
    raw_out[i] = (x & 0xFF);
  }
  return raw_len;
}


x86 assembly (clang, -O0):
<hex2bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     117d <hex2bin+0x3d>
mov    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1189 <hex2bin+0x49>
movl   $0x0,-0x4(%rbp)
jmp    1226 <hex2bin+0xe6>
mov    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    11a9 <hex2bin+0x69>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    -0x27(%rbp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1040 <memset@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1220 <hex2bin+0xe0>
lea    -0x27(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
lea    -0x27(%rbp),%rdi
lea    0xe0c(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1060 <sscanf@plt>
mov    -0x2c(%rbp),%eax
and    $0xff,%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11c0 <hex2bin+0x80>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2bin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%rbp
call   1030 <strlen@plt>
xor    %edx,%edx
test   %eax,%eax
je     11cc <hex2bin+0x9c>
mov    %eax,%ecx
and    $0x1,%ecx
jne    11cc <hex2bin+0x9c>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
cmp    %ebx,%ecx
cmovg  %ebx,%ecx
movb   $0x0,0xe(%rsp)
movw   $0x0,0xc(%rsp)
mov    %ecx,0x10(%rsp)
test   %ecx,%ecx
jle    11c8 <hex2bin+0x98>
mov    0x10(%rsp),%r14d
xor    %r12d,%r12d
lea    0xc(%rsp),%rbx
lea    0x14(%rsp),%r13
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
lea    0xe56(%rip),%rsi
mov    %r13,%rdx
call   1050 <sscanf@plt>
movzbl 0x14(%rsp),%eax
mov    %al,(%r15,%r12,1)
add    $0x1,%r12
add    $0x2,%rbp
cmp    %r12,%r14
jne    1190 <hex2bin+0x60>
mov    0x10(%rsp),%edx
mov    %edx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%X\00"
define i32 @hex2bin(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [3 x i8]
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%13 = load i8*, i8** %5
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %3
%18 = load i32, i32* %8
%19 = srem i32 %18, 2
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17, %3
store i32 0, i32* %4
br label %58
22: ; preds = %17
%23 = load i32, i32* %8
%24 = sdiv i32 %23, 2
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = load i32, i32* %7
%27 = icmp sgt i32 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = load i32, i32* %7
store i32 %29, i32* %9
br label %30
30: ; preds = %28, %22
%31 = bitcast [3 x i8]* %10 to i8*
store i32 0, i32* %12
br label %32
32: ; preds = %53, %30
%33 = load i32, i32* %12
%34 = load i32, i32* %9
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %56
36: ; preds = %32
%37 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 0
%38 = load i8*, i8** %5
%39 = load i32, i32* %12
%40 = mul nsw i32 %39, 2
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
%43 = call i32 @memcpy(i8* %37, i8* %42, i32 2)
%44 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 0
%45 = call i32 @sscanf(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %11)
%46 = load i32, i32* %11
%47 = and i32 %46, 255
%48 = trunc i32 %47 to i8
%49 = load i8*, i8** %6
%50 = load i32, i32* %12
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 %48, i8* %52
br label %53
53: ; preds = %36
%54 = load i32, i32* %12
%55 = add nsw i32 %54, 1
store i32 %55, i32* %12
br label %32
56: ; preds = %32
%57 = load i32, i32* %9
store i32 %57, i32* %4
br label %58
58: ; preds = %56, %21
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@.str = [3 x i8] c"%X\00"
define i32 @hex2bin(i8* %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca [3 x i8]
%5 = alloca i32
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp ne i32 %6, 0
%8 = and i32 %6, 1
%9 = icmp eq i32 %8, 0
%10 = and i1 %7, %9
br i1 %10, label %11, label %32
11: ; preds = %3
%12 = sdiv i32 %6, 2
%13 = icmp sgt i32 %12, %2
%14 = select i1 %13, i32 %2, i32 %12
%15 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%16 = bitcast i32* %5 to i8*
%17 = icmp sgt i32 %14, 0
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = zext i32 %14 to i64
br label %21
20: ; preds = %21, %11
br label %32
21: ; preds = %18, %21
%22 = phi i64 [ 0, %18 ], [ %30, %21 ]
%23 = shl nuw nsw i64 %22, 1
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = call i32 @memcpy(i8* %15, i8* %24, i32 2)
%26 = call i32 @sscanf(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %5)
%27 = load i32, i32* %5
%28 = trunc i32 %27 to i8
%29 = getelementptr inbounds i8, i8* %1, i64 %22
store i8 %28, i8* %29
%30 = add nuw nsw i64 %22, 1
%31 = icmp eq i64 %30, %19
br i1 %31, label %20, label %21
32: ; preds = %3, %20
%33 = phi i32 [ %14, %20 ], [ 0, %3 ]
ret i32 %33
}