#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 *, char *, unsigned int *);
int /*<<< orphan*/ *strchr(char *, char);
int iso_util_hex_to_bin(char *hex, char *bin, int bin_size, int *bin_count,
                        int flag) {
  static char *allowed = {"0123456789ABCDEFabcdef"};
  char b[3];
  int i;
  unsigned int u;
  b[2] = 0;
  *bin_count = 0;
  for (i = 0; i < bin_size; i++) {
    b[0] = hex[2 * i];
    b[1] = hex[2 * i + 1];
    if (strchr(allowed, b[0]) == NULL || strchr(allowed, b[1]) == NULL)
      break;
    sscanf(b, "%x", &u);
    ((unsigned char *)bin)[i] = u;
    (*bin_count)++;
  }
  return (*bin_count > 0);
}


x86 assembly (clang, -O0):
<iso_util_hex_to_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movb   $0x0,-0x25(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11fb <iso_util_hex_to_bin+0xdb>
mov    -0x8(%rbp),%rax
mov    -0x2c(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x27(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x2c(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x26(%rbp)
mov    0x2ea4(%rip),%rdi
movsbl -0x27(%rbp),%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     11b9 <iso_util_hex_to_bin+0x99>
mov    0x2e8a(%rip),%rdi
movsbl -0x26(%rbp),%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
jne    11be <iso_util_hex_to_bin+0x9e>
jmp    11fb <iso_util_hex_to_bin+0xdb>
lea    -0x27(%rbp),%rdi
lea    0xe4e(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <sscanf@plt>
mov    -0x30(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1150 <iso_util_hex_to_bin+0x30>
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
setg   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iso_util_hex_to_bin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
movb   $0x0,0xb(%rsp)
movl   $0x0,(%rcx)
test   %edx,%edx
jle    11cf <iso_util_hex_to_bin+0xaf>
mov    %rcx,%r15
mov    %rdi,%rbx
mov    %edx,%ebp
xor    %r13d,%r13d
lea    0xea8(%rip),%r12
nopl   0x0(%rax,%rax,1)
movsbl (%rbx,%r13,2),%esi
mov    %sil,0x9(%rsp)
movzbl 0x1(%rbx,%r13,2),%r14d
mov    %r14b,0xa(%rsp)
mov    %r12,%rdi
call   1030 <strchr@plt>
test   %rax,%rax
je     11c4 <iso_util_hex_to_bin+0xa4>
movsbl %r14b,%esi
mov    %r12,%rdi
call   1030 <strchr@plt>
test   %rax,%rax
je     11c4 <iso_util_hex_to_bin+0xa4>
lea    0x9(%rsp),%rdi
lea    0xe78(%rip),%rsi
lea    0xc(%rsp),%rdx
call   1040 <sscanf@plt>
movzbl 0xc(%rsp),%eax
mov    0x10(%rsp),%rcx
mov    %al,(%rcx,%r13,1)
addl   $0x1,(%r15)
add    $0x1,%r13
cmp    %r13,%rbp
jne    1160 <iso_util_hex_to_bin+0x40>
xor    %eax,%eax
cmpl   $0x0,(%r15)
setg   %al
jmp    11d1 <iso_util_hex_to_bin+0xb1>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@iso_util_hex_to_bin.allowed = internal global i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i32 0, i32 0)
@.str = [23 x i8] c"0123456789ABCDEFabcdef\00"
@.str.1 = [3 x i8] c"%x\00"
define i32 @iso_util_hex_to_bin(i8* %0, i8* %1, i32 %2, i32* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca [3 x i8]
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i32 %4, i32* %10
%14 = getelementptr inbounds [3 x i8], [3 x i8]* %11, i64 0, i64 2
store i8 0, i8* %14
%15 = load i32*, i32** %9
store i32 0, i32* %15
store i32 0, i32* %12
br label %16
16: ; preds = %60, %5
%17 = load i32, i32* %12
%18 = load i32, i32* %8
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %63
20: ; preds = %16
%21 = load i8*, i8** %6
%22 = load i32, i32* %12
%23 = mul nsw i32 2, %22
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
%26 = load i8, i8* %25
%27 = getelementptr inbounds [3 x i8], [3 x i8]* %11, i64 0, i64 0
store i8 %26, i8* %27
%28 = load i8*, i8** %6
%29 = load i32, i32* %12
%30 = mul nsw i32 2, %29
%31 = add nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %28, i64 %32
%34 = load i8, i8* %33
%35 = getelementptr inbounds [3 x i8], [3 x i8]* %11, i64 0, i64 1
store i8 %34, i8* %35
%36 = load i8*, i8** @iso_util_hex_to_bin.allowed
%37 = getelementptr inbounds [3 x i8], [3 x i8]* %11, i64 0, i64 0
%38 = load i8, i8* %37
%39 = call i32* @strchr(i8* %36, i8 signext %38)
%40 = icmp eq i32* %39, null
br i1 %40, label %47, label %41
41: ; preds = %20
%42 = load i8*, i8** @iso_util_hex_to_bin.allowed
%43 = getelementptr inbounds [3 x i8], [3 x i8]* %11, i64 0, i64 1
%44 = load i8, i8* %43
%45 = call i32* @strchr(i8* %42, i8 signext %44)
%46 = icmp eq i32* %45, null
br i1 %46, label %47, label %48
47: ; preds = %41, %20
br label %63
48: ; preds = %41
%49 = getelementptr inbounds [3 x i8], [3 x i8]* %11, i64 0, i64 0
%50 = call i32 @sscanf(i8* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %13)
%51 = load i32, i32* %13
%52 = trunc i32 %51 to i8
%53 = load i8*, i8** %7
%54 = load i32, i32* %12
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
store i8 %52, i8* %56
%57 = load i32*, i32** %9
%58 = load i32, i32* %57
%59 = add nsw i32 %58, 1
store i32 %59, i32* %57
br label %60
60: ; preds = %48
%61 = load i32, i32* %12
%62 = add nsw i32 %61, 1
store i32 %62, i32* %12
br label %16
63: ; preds = %47, %16
%64 = load i32*, i32** %9
%65 = load i32, i32* %64
%66 = icmp sgt i32 %65, 0
%67 = zext i1 %66 to i32
ret i32 %67
}

LLVM IR (-O2):
@.str = [23 x i8] c"0123456789ABCDEFabcdef\00"
@.str.1 = [3 x i8] c"%x\00"
define i32 @iso_util_hex_to_bin(i8* nocapture %0, i8* nocapture writeonly %1, i32 %2, i32* nocapture %3, i32 %4) {
%6 = alloca [3 x i8]
%7 = alloca i32
%8 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%9 = bitcast i32* %7 to i8*
%10 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 2
store i8 0, i8* %10
store i32 0, i32* %3
%11 = icmp sgt i32 %2, 0
br i1 %11, label %12, label %41
12: ; preds = %5
%13 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 1
%14 = zext i32 %2 to i64
br label %15
15: ; preds = %12, %28
%16 = phi i64 [ 0, %12 ], [ %35, %28 ]
%17 = shl nuw nsw i64 %16, 1
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
store i8 %19, i8* %8
%20 = or i64 %17, 1
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = load i8, i8* %21
store i8 %22, i8* %13
%23 = call i32* @strchr(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8 signext %19)
%24 = icmp eq i32* %23, null
br i1 %24, label %37, label %25
25: ; preds = %15
%26 = call i32* @strchr(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8 signext %22)
%27 = icmp eq i32* %26, null
br i1 %27, label %37, label %28
28: ; preds = %25
%29 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %7)
%30 = load i32, i32* %7
%31 = trunc i32 %30 to i8
%32 = getelementptr inbounds i8, i8* %1, i64 %16
store i8 %31, i8* %32
%33 = load i32, i32* %3
%34 = add nsw i32 %33, 1
store i32 %34, i32* %3
%35 = add nuw nsw i64 %16, 1
%36 = icmp eq i64 %35, %14
br i1 %36, label %37, label %15
37: ; preds = %15, %25, %28
%38 = load i32, i32* %3
%39 = icmp sgt i32 %38, 0
%40 = zext i1 %39 to i32
br label %41
41: ; preds = %37, %5
%42 = phi i32 [ %40, %37 ], [ 0, %5 ]
ret i32 %42
}