#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;
typedef int int8_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int parseHexDigit(char const);
int /*<<< orphan*/ stderr;
int hexstring_parse(const char *hexstr, uint8_t *buf, size_t *buflen) {
  size_t hexstrlen = *buflen * 2;
  size_t i;
  if (hexstrlen & 0x1) {
    fprintf(stderr, "hexstring_parse: not even\n");
    return 1;
  }
  if (*buflen < hexstrlen / 2) {
    fprintf(stderr, "hexstring_parse: buffer too small %zu < %zu\n", *buflen,
            hexstrlen / 2);
    return 1;
  }
  for (i = 0; i < hexstrlen; i += 2) {
    int8_t a, b;
    if (-1 == (a = parseHexDigit(hexstr[i]))) {
      fprintf(stderr, "hexstring_parse: bad digit 0x%02X\n", hexstr[i]);
      return 1;
    }
    if (-1 == (b = parseHexDigit(hexstr[i + 1]))) {
      fprintf(stderr, "hexstring_parse: bad digit 0x%02X\n", hexstr[i + 1]);
      return 1;
    }
    *buf++ = (a << 4) | b;
  }
  *buflen = hexstrlen / 2;
  return 0;
}


x86 assembly (clang, -O0):
<hexstring_parse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     1178 <hexstring_parse+0x58>
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
lea    0xe9b(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bf <hexstring_parse+0x19f>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
shr    $0x1,%rcx
cmp    %rcx,%rax
jae    11c2 <hexstring_parse+0xa2>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x28(%rbp),%rcx
shr    $0x1,%rcx
lea    0xe6c(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bf <hexstring_parse+0x19f>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    12a9 <hexstring_parse+0x189>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <parseHexDigit@plt>
mov    %eax,%ecx
mov    %ecx,-0x34(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    122a <hexstring_parse+0x10a>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
lea    0xe31(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bf <hexstring_parse+0x19f>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl 0x1(%rax,%rcx,1),%edi
call   1040 <parseHexDigit@plt>
mov    %eax,%ecx
mov    %ecx,-0x38(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    127e <hexstring_parse+0x15e>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl 0x1(%rax,%rcx,1),%edx
lea    0xddd(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bf <hexstring_parse+0x19f>
mov    -0x34(%rbp),%ecx
shl    $0x4,%ecx
or     -0x38(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x18(%rbp)
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x30(%rbp)
jmp    11ca <hexstring_parse+0xaa>
mov    -0x28(%rbp),%rcx
shr    $0x1,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexstring_parse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
mov    (%rdx),%r14
mov    %r14,%r12
add    %r14,%r12
je     1184 <hexstring_parse+0x64>
mov    %rsi,%r15
mov    %rdi,%r13
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl 0x0(%r13,%rbx,1),%edi
call   1040 <parseHexDigit@plt>
cmp    $0xffffffff,%eax
je     119c <hexstring_parse+0x7c>
mov    %eax,%ebp
movsbl 0x1(%r13,%rbx,1),%edi
call   1040 <parseHexDigit@plt>
cmp    $0xffffffff,%eax
je     11ad <hexstring_parse+0x8d>
shl    $0x4,%ebp
or     %ebp,%eax
mov    %eax,(%r15,%rbx,2)
add    $0x2,%rbx
cmp    %r12,%rbx
jb     1150 <hexstring_parse+0x30>
movabs $0x7fffffffffffffff,%rax
ff ff 7f
and    %rax,%r14
mov    (%rsp),%rax
mov    %r14,(%rax)
xor    %eax,%eax
jmp    11cf <hexstring_parse+0xaf>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
movsbl 0x0(%r13,%rbx,1),%edx
jmp    11bc <hexstring_parse+0x9c>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
movsbl 0x1(%r13,%rbx,1),%edx
lea    0xe3d(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"hexstring_parse: not even\0A\00"
@.str.1 = [45 x i8] c"hexstring_parse: buffer too small %zu < %zu\0A\00"
@.str.2 = [35 x i8] c"hexstring_parse: bad digit 0x%02X\0A\00"
define i32 @hexstring_parse(i8* %0, i32* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i64*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i64* %2, i64** %7
%12 = load i64*, i64** %7
%13 = load i64, i64* %12
%14 = mul i64 %13, 2
store i64 %14, i64* %8
%15 = load i64, i64* %8
%16 = and i64 %15, 1
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %85
21: ; preds = %3
%22 = load i64*, i64** %7
%23 = load i64, i64* %22
%24 = load i64, i64* %8
%25 = udiv i64 %24, 2
%26 = icmp ult i64 %23, %25
br i1 %26, label %27, label %34
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = load i64*, i64** %7
%30 = load i64, i64* %29
%31 = load i64, i64* %8
%32 = udiv i64 %31, 2
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i64 %30, i64 %32)
store i32 1, i32* %4
br label %85
34: ; preds = %21
store i64 0, i64* %9
br label %35
35: ; preds = %78, %34
%36 = load i64, i64* %9
%37 = load i64, i64* %8
%38 = icmp ult i64 %36, %37
br i1 %38, label %39, label %81
39: ; preds = %35
%40 = load i8*, i8** %5
%41 = load i64, i64* %9
%42 = getelementptr inbounds i8, i8* %40, i64 %41
%43 = load i8, i8* %42
%44 = call i32 @parseHexDigit(i8 signext %43)
store i32 %44, i32* %10
%45 = icmp eq i32 -1, %44
br i1 %45, label %46, label %54
46: ; preds = %39
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %5
%49 = load i64, i64* %9
%50 = getelementptr inbounds i8, i8* %48, i64 %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %52)
store i32 1, i32* %4
br label %85
54: ; preds = %39
%55 = load i8*, i8** %5
%56 = load i64, i64* %9
%57 = add i64 %56, 1
%58 = getelementptr inbounds i8, i8* %55, i64 %57
%59 = load i8, i8* %58
%60 = call i32 @parseHexDigit(i8 signext %59)
store i32 %60, i32* %11
%61 = icmp eq i32 -1, %60
br i1 %61, label %62, label %71
62: ; preds = %54
%63 = load i32, i32* @stderr
%64 = load i8*, i8** %5
%65 = load i64, i64* %9
%66 = add i64 %65, 1
%67 = getelementptr inbounds i8, i8* %64, i64 %66
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %69)
store i32 1, i32* %4
br label %85
71: ; preds = %54
%72 = load i32, i32* %10
%73 = shl i32 %72, 4
%74 = load i32, i32* %11
%75 = or i32 %73, %74
%76 = load i32*, i32** %6
%77 = getelementptr inbounds i32, i32* %76, i32 1
store i32* %77, i32** %6
store i32 %75, i32* %76
br label %78
78: ; preds = %71
%79 = load i64, i64* %9
%80 = add i64 %79, 2
store i64 %80, i64* %9
br label %35
81: ; preds = %35
%82 = load i64, i64* %8
%83 = udiv i64 %82, 2
%84 = load i64*, i64** %7
store i64 %83, i64* %84
store i32 0, i32* %4
br label %85
85: ; preds = %81, %62, %46, %27, %18
%86 = load i32, i32* %4
ret i32 %86
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.2 = [35 x i8] c"hexstring_parse: bad digit 0x%02X\0A\00"
define i32 @hexstring_parse(i8* nocapture %0, i32* nocapture writeonly %1, i64* nocapture %2) {
%4 = load i64, i64* %2
%5 = shl i64 %4, 1
%6 = and i64 %4, 9223372036854775807
%7 = icmp eq i64 %5, 0
br i1 %7, label %39, label %8
8: ; preds = %3, %33
%9 = phi i32* [ %36, %33 ], [ %1, %3 ]
%10 = phi i64 [ %37, %33 ], [ 0, %3 ]
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = tail call i32 @parseHexDigit(i8 signext %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %21
15: ; preds = %8
%16 = getelementptr inbounds i8, i8* %0, i64 %10
%17 = load i32, i32* @stderr
%18 = load i8, i8* %16
%19 = sext i8 %18 to i32
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %40
21: ; preds = %8
%22 = or i64 %10, 1
%23 = getelementptr inbounds i8, i8* %0, i64 %22
%24 = load i8, i8* %23
%25 = tail call i32 @parseHexDigit(i8 signext %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = getelementptr inbounds i8, i8* %0, i64 %22
%29 = load i32, i32* @stderr
%30 = load i8, i8* %28
%31 = sext i8 %30 to i32
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %31)
br label %40
33: ; preds = %21
%34 = shl i32 %13, 4
%35 = or i32 %25, %34
%36 = getelementptr inbounds i32, i32* %9, i64 1
store i32 %35, i32* %9
%37 = add nuw i64 %10, 2
%38 = icmp ult i64 %37, %5
br i1 %38, label %8, label %39
39: ; preds = %33, %3
store i64 %6, i64* %2
br label %40
40: ; preds = %15, %27, %39
%41 = phi i32 [ 0, %39 ], [ 1, %27 ], [ 1, %15 ]
ret i32 %41
}