#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 */
unsigned char char_to_4bits(char, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *);
int strlen(char *);
void hexstr_to_bytes(unsigned char *buffer, char *input) {
  if (strlen(input) % 2 > 0) {
    printf("Error - string length is odd. 2 hex needed to fill a byte!");
    exit(-1);
  }
  int a = 0;
  int i = 0;
  while (input[a] != '\0') {
    buffer[i] |= char_to_4bits(input[a], a);
    a++;
    if (a % 2 == 0 && a != 0)
      i++;
  }
}


x86 assembly (clang, -O0):
<hexstr_to_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jle    1180 <hexstr_to_bytes+0x40>
lea    0xe8f(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1202 <hexstr_to_bytes+0xc2>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    -0x14(%rbp),%esi
movsbl %al,%edi
call   1060 <char_to_4bits@plt>
movzbl %al,%esi
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
or     %esi,%edx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11fd <hexstr_to_bytes+0xbd>
cmpl   $0x0,-0x14(%rbp)
je     11fd <hexstr_to_bytes+0xbd>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    118e <hexstr_to_bytes+0x4e>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexstr_to_bytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <strlen@plt>
and    $0x80000001,%eax
cmp    $0x1,%eax
je     11a3 <hexstr_to_bytes+0x63>
mov    (%r15),%al
test   %al,%al
je     1198 <hexstr_to_bytes+0x58>
xor    %ebx,%ebx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
movsbl %al,%edi
mov    %ebx,%esi
call   1060 <char_to_4bits@plt>
mov    %ebp,%ecx
or     %al,(%r14,%rcx,1)
lea    0x1(%rbx),%rcx
movzbl 0x1(%r15,%rbx,1),%eax
mov    %rcx,%rbx
not    %ecx
and    $0x1,%ecx
add    %ecx,%ebp
test   %al,%al
jne    1170 <hexstr_to_bytes+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe56(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [59 x i8] c"Error - string length is odd. 2 hex needed to fill a byte!\00"
define void @hexstr_to_bytes(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = call i32 @strlen(i8* %7)
%9 = srem i32 %8, 2
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %2
store i32 0, i32* %5
store i32 0, i32* %6
br label %15
15: ; preds = %51, %14
%16 = load i8*, i8** %4
%17 = load i32, i32* %5
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %52
23: ; preds = %15
%24 = load i8*, i8** %4
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = load i32, i32* %5
%30 = call zeroext i8 @char_to_4bits(i8 signext %28, i32 %29)
%31 = zext i8 %30 to i32
%32 = load i8*, i8** %3
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = or i32 %37, %31
%39 = trunc i32 %38 to i8
store i8 %39, i8* %35
%40 = load i32, i32* %5
%41 = add nsw i32 %40, 1
store i32 %41, i32* %5
%42 = load i32, i32* %5
%43 = srem i32 %42, 2
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %23
%46 = load i32, i32* %5
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* %6
%50 = add nsw i32 %49, 1
store i32 %50, i32* %6
br label %51
51: ; preds = %48, %45, %23
br label %15
52: ; preds = %15
ret void
}

LLVM IR (-O2):
@.str = [59 x i8] c"Error - string length is odd. 2 hex needed to fill a byte!\00"
define void @hexstr_to_bytes(i8* nocapture %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = and i32 %3, -2147483647
%5 = icmp eq i32 %4, 1
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8, i8* %1
%8 = icmp eq i8 %7, 0
br i1 %8, label %30, label %12
9: ; preds = %2
%10 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %6, %12
%13 = phi i64 [ %22, %12 ], [ 0, %6 ]
%14 = phi i8 [ %28, %12 ], [ %7, %6 ]
%15 = phi i32 [ %26, %12 ], [ 0, %6 ]
%16 = trunc i64 %13 to i32
%17 = tail call zeroext i8 @char_to_4bits(i8 signext %14, i32 %16)
%18 = zext i32 %15 to i64
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = load i8, i8* %19
%21 = or i8 %20, %17
store i8 %21, i8* %19
%22 = add nuw i64 %13, 1
%23 = trunc i64 %22 to i32
%24 = and i32 %23, 1
%25 = xor i32 %24, 1
%26 = add nuw nsw i32 %25, %15
%27 = getelementptr inbounds i8, i8* %1, i64 %22
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
br i1 %29, label %30, label %12
30: ; preds = %12, %6
ret void
}