#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*/ printf(char *, ...);
int sscanf(char *, char *, unsigned int *);
int hex2bin(unsigned char *p, const unsigned char *hexstr, unsigned int len) {
  while (*hexstr && len) {
    char hex_byte[3];
    unsigned int v;
    if (!hexstr[1]) {
      printf("hex2bin str truncated\n");
      return 0;
    }
    hex_byte[0] = hexstr[0];
    hex_byte[1] = hexstr[1];
    hex_byte[2] = 0;
    if (sscanf(hex_byte, "%x", &v) != 1) {
      printf("hex2bin sscanf '%s' failed\n", hex_byte);
      return 0;
    }
    *p = (unsigned char)v;
    p++;
    hexstr += 2;
    len--;
  }
  return ((len == 0) ? 1 : 0);
}


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    -0x18(%rbp),%rax
movzbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x25(%rbp)
je     1152 <hex2bin+0x32>
cmpl   $0x0,-0x1c(%rbp)
setne  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1162 <hex2bin+0x42>
jmp    120d <hex2bin+0xed>
mov    -0x18(%rbp),%rax
cmpb   $0x0,0x1(%rax)
jne    118a <hex2bin+0x6a>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1220 <hex2bin+0x100>
mov    -0x18(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x1f(%rbp)
mov    -0x18(%rbp),%rax
mov    0x1(%rax),%al
mov    %al,-0x1e(%rbp)
movb   $0x0,-0x1d(%rbp)
lea    -0x1f(%rbp),%rdi
lea    0xe6b(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1040 <sscanf@plt>
cmp    $0x1,%eax
je     11dc <hex2bin+0xbc>
lea    -0x1f(%rbp),%rsi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1220 <hex2bin+0x100>
mov    -0x24(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1133 <hex2bin+0x13>
mov    -0x1c(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmove  %ecx,%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    (%rsi),%al
test   %al,%al
je     11c4 <hex2bin+0x94>
test   %edx,%edx
je     11c4 <hex2bin+0x94>
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %edx,0x10(%rsp)
mov    %edx,%r14d
add    $0xffffffffffffffff,%r14
xor    %r15d,%r15d
lea    0xd(%rsp),%r12
lea    0x14(%rsp),%r13
nopw   0x0(%rax,%rax,1)
movzbl 0x1(%rbx,%r15,2),%ecx
test   %cl,%cl
je     11cd <hex2bin+0x9d>
mov    %al,0xd(%rsp)
mov    %cl,0xe(%rsp)
movb   $0x0,0xf(%rsp)
mov    %r12,%rdi
lea    0xe6f(%rip),%rsi
mov    %r13,%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    11dd <hex2bin+0xad>
movzbl 0x14(%rsp),%eax
mov    %al,0x0(%rbp,%r15,1)
movzbl 0x2(%rbx,%r15,2),%eax
lea    0x1(%r15),%rcx
test   %al,%al
je     11be <hex2bin+0x8e>
cmp    %r15d,%r14d
mov    %rcx,%r15
jne    1170 <hex2bin+0x40>
mov    0x10(%rsp),%edx
sub    %ecx,%edx
xor    %ebx,%ebx
test   %edx,%edx
sete   %bl
jmp    11f2 <hex2bin+0xc2>
lea    0xe4b(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    11f2 <hex2bin+0xc2>
lea    0xe1f(%rip),%rdi
xor    %ebx,%ebx
lea    0xd(%rsp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%x\00"
@.str.2 = [28 x i8] c"hex2bin sscanf '%s' failed\0A\00"
@str = [22 x i8] c"hex2bin str truncated\00"
define i32 @hex2bin(i8* nocapture writeonly %0, i8* nocapture %1, i32 %2) {
%4 = alloca [3 x i8]
%5 = alloca i32
%6 = load i8, i8* %1
%7 = icmp ne i8 %6, 0
%8 = icmp ne i32 %2, 0
%9 = and i1 %7, %8
br i1 %9, label %10, label %41
10: ; preds = %3
%11 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%12 = bitcast i32* %5 to i8*
%13 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 1
%14 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 2
br label %15
15: ; preds = %10, %31
%16 = phi i8 [ %6, %10 ], [ %37, %31 ]
%17 = phi i32 [ %2, %10 ], [ %36, %31 ]
%18 = phi i8* [ %1, %10 ], [ %35, %31 ]
%19 = phi i8* [ %0, %10 ], [ %34, %31 ]
%20 = getelementptr inbounds i8, i8* %18, i64 1
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %30
25: ; preds = %15
store i8 %16, i8* %11
store i8 %21, i8* %13
store i8 0, i8* %14
%26 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%27 = icmp eq i32 %26, 1
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %11)
br label %30
30: ; preds = %28, %23
br label %45
31: ; preds = %25
%32 = load i32, i32* %5
%33 = trunc i32 %32 to i8
store i8 %33, i8* %19
%34 = getelementptr inbounds i8, i8* %19, i64 1
%35 = getelementptr inbounds i8, i8* %18, i64 2
%36 = add i32 %17, -1
%37 = load i8, i8* %35
%38 = icmp ne i8 %37, 0
%39 = icmp ne i32 %36, 0
%40 = select i1 %38, i1 %39, i1 false
br i1 %40, label %15, label %41
41: ; preds = %31, %3
%42 = phi i32 [ %2, %3 ], [ %36, %31 ]
%43 = icmp eq i32 %42, 0
%44 = zext i1 %43 to i32
br label %45
45: ; preds = %30, %41
%46 = phi i32 [ %44, %41 ], [ 0, %30 ]
ret i32 %46
}