#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 unsigned int uint8_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
unsigned int hexdigit_value(char);
scalar_t__ is_hex_digit(char);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, unsigned int);
int /*<<< orphan*/ printf(char *, unsigned int, ...);
void hexstr_to_bytes(unsigned int size_in_bytes, void *dst,
                     const char *const hexstr) {
  unsigned int char_index = 0;
  unsigned int hexdigit_cnt = 0;
  uint8_t *dst_bytes = (uint8_t *)dst;
  memset(dst, 0, size_in_bytes);
  while (hexdigit_cnt < size_in_bytes * 2) {
    char c = hexstr[char_index++];
    if (0 == c) {
      printf("\nERROR: could not find %d hex digits in string '%s'.\n",
             size_in_bytes * 2, hexstr);
      printf("char_index=%d, hexdigit_cnt=%d\n", char_index, hexdigit_cnt);
      exit(-1);
    }
    if (is_hex_digit(c)) {
      unsigned int shift = 4 - 4 * (hexdigit_cnt & 1);
      uint8_t nibble = hexdigit_value(c);
      dst_bytes[hexdigit_cnt / 2] |= nibble << shift;
      hexdigit_cnt++;
    }
  }
}


x86 assembly (clang, -O0):
<hexstr_to_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x20(%rbp),%eax
mov    -0x4(%rbp),%ecx
shl    $0x1,%ecx
cmp    %ecx,%eax
jae    124d <hexstr_to_bytes+0xfd>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
mov    %ecx,%ecx
mov    (%rax,%rcx,1),%al
mov    %al,-0x29(%rbp)
movsbl -0x29(%rbp),%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    11f3 <hexstr_to_bytes+0xa3>
mov    -0x4(%rbp),%esi
shl    $0x1,%esi
mov    -0x18(%rbp),%rdx
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
movsbl -0x29(%rbp),%edi
call   1060 <is_hex_digit@plt>
cmp    $0x0,%rax
je     1248 <hexstr_to_bytes+0xf8>
mov    -0x20(%rbp),%ecx
and    $0x1,%ecx
shl    $0x2,%ecx
mov    $0x4,%eax
sub    %ecx,%eax
mov    %eax,-0x30(%rbp)
movsbl -0x29(%rbp),%edi
call   1050 <hexdigit_value@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edx
mov    -0x30(%rbp),%ecx
shl    %cl,%edx
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%ecx
shr    $0x1,%ecx
mov    %ecx,%ecx
or     (%rax,%rcx,4),%edx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1187 <hexstr_to_bytes+0x37>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexstr_to_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %edi,%r15d
xor    %r13d,%r13d
mov    %rsi,(%rsp)
mov    %rsi,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1040 <memset@plt>
add    %r15d,%r15d
je     11e0 <hexstr_to_bytes+0x90>
mov    $0x1,%ebp
jmp    1198 <hexstr_to_bytes+0x48>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
cmp    %r15d,%r13d
jae    11e0 <hexstr_to_bytes+0x90>
lea    -0x1(%rbp),%eax
movzbl (%r14,%rax,1),%eax
test   %al,%al
je     11ef <hexstr_to_bytes+0x9f>
movsbl %al,%ebx
mov    %ebx,%edi
call   1060 <is_hex_digit@plt>
test   %rax,%rax
je     1190 <hexstr_to_bytes+0x40>
lea    0x0(,%r13,4),%r12d
mov    %ebx,%edi
call   1050 <hexdigit_value@plt>
not    %r12b
and    $0x4,%r12b
mov    %r12d,%ecx
shl    %cl,%eax
mov    %r13d,%ecx
shr    %ecx
mov    (%rsp),%rdx
or     %eax,(%rdx,%rcx,4)
add    $0x1,%r13d
jmp    1190 <hexstr_to_bytes+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0a(%rip),%rdi
mov    %r15d,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe2c(%rip),%rdi
mov    %ebp,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [54 x i8] c"\0AERROR: could not find %d hex digits in string '%s'.\0A\00"
@.str.1 = [32 x i8] c"char_index=%d, hexdigit_cnt=%d\0A\00"
define void @hexstr_to_bytes(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i8
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 0, i32* %7
store i32 0, i32* %8
%13 = load i8*, i8** %5
%14 = bitcast i8* %13 to i32*
store i32* %14, i32** %9
%15 = load i8*, i8** %5
%16 = load i32, i32* %4
%17 = call i32 @memset(i8* %15, i32 0, i32 %16)
br label %18
18: ; preds = %65, %3
%19 = load i32, i32* %8
%20 = load i32, i32* %4
%21 = mul i32 %20, 2
%22 = icmp ult i32 %19, %21
br i1 %22, label %23, label %66
23: ; preds = %18
%24 = load i8*, i8** %6
%25 = load i32, i32* %7
%26 = add i32 %25, 1
store i32 %26, i32* %7
%27 = zext i32 %25 to i64
%28 = getelementptr inbounds i8, i8* %24, i64 %27
%29 = load i8, i8* %28
store i8 %29, i8* %10
%30 = load i8, i8* %10
%31 = sext i8 %30 to i32
%32 = icmp eq i32 0, %31
br i1 %32, label %33, label %42
33: ; preds = %23
%34 = load i32, i32* %4
%35 = mul i32 %34, 2
%36 = load i8*, i8** %6
%37 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %35, i8* %36)
%38 = load i32, i32* %7
%39 = load i32, i32* %8
%40 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %38, i32 %39)
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %23
%43 = load i8, i8* %10
%44 = call i64 @is_hex_digit(i8 signext %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %65
46: ; preds = %42
%47 = load i32, i32* %8
%48 = and i32 %47, 1
%49 = mul i32 4, %48
%50 = sub i32 4, %49
store i32 %50, i32* %11
%51 = load i8, i8* %10
%52 = call i32 @hexdigit_value(i8 signext %51)
store i32 %52, i32* %12
%53 = load i32, i32* %12
%54 = load i32, i32* %11
%55 = shl i32 %53, %54
%56 = load i32*, i32** %9
%57 = load i32, i32* %8
%58 = udiv i32 %57, 2
%59 = zext i32 %58 to i64
%60 = getelementptr inbounds i32, i32* %56, i64 %59
%61 = load i32, i32* %60
%62 = or i32 %61, %55
store i32 %62, i32* %60
%63 = load i32, i32* %8
%64 = add i32 %63, 1
store i32 %64, i32* %8
br label %65
65: ; preds = %46, %42
br label %18
66: ; preds = %18
ret void
}

LLVM IR (-O2):
@.str = [54 x i8] c"\0AERROR: could not find %d hex digits in string '%s'.\0A\00"
@.str.1 = [32 x i8] c"char_index=%d, hexdigit_cnt=%d\0A\00"
define void @hexstr_to_bytes(i32 %0, i8* %1, i8* %2) {
%4 = bitcast i8* %1 to i32*
%5 = tail call i32 @memset(i8* %1, i32 0, i32 %0)
%6 = shl i32 %0, 1
%7 = icmp eq i32 %6, 0
br i1 %7, label %38, label %8
8: ; preds = %3, %35
%9 = phi i32 [ %11, %35 ], [ 0, %3 ]
%10 = phi i32 [ %36, %35 ], [ 0, %3 ]
%11 = add i32 %9, 1
%12 = zext i32 %9 to i64
%13 = getelementptr inbounds i8, i8* %2, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %8
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %6, i8* %2)
%18 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %10)
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %8
%21 = tail call i64 @is_hex_digit(i8 signext %14)
%22 = icmp eq i64 %21, 0
br i1 %22, label %35, label %23
23: ; preds = %20
%24 = shl i32 %10, 2
%25 = and i32 %24, 4
%26 = xor i32 %25, 4
%27 = tail call i32 @hexdigit_value(i8 signext %14)
%28 = shl i32 %27, %26
%29 = lshr i32 %10, 1
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %4, i64 %30
%32 = load i32, i32* %31
%33 = or i32 %32, %28
store i32 %33, i32* %31
%34 = add nuw i32 %10, 1
br label %35
35: ; preds = %23, %20
%36 = phi i32 [ %34, %23 ], [ %10, %20 ]
%37 = icmp ult i32 %36, %6
br i1 %37, label %8, label %38
38: ; preds = %35, %3
ret void
}