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


x86 assembly (clang, -O0):
<hexstr_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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    1190 <hexstr_to_string+0x40>
lea    0xe7f(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    $0x2,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <xrealloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
cmp    %rcx,%rax
jge    128a <hexstr_to_string+0x13a>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    -0x18(%rbp),%rsi
movsbl %al,%edi
call   1070 <char_to_4bits@plt>
movsbl %al,%esi
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
or     %esi,%edx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    0x1(%rax,%rcx,1),%al
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
movsbl %al,%edi
call   1070 <char_to_4bits@plt>
movsbl %al,%esi
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
or     %esi,%edx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
jmp    11da <hexstr_to_string+0x8a>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cltq
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexstr_to_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1030 <strlen@plt>
and    $0x80000001,%eax
cmp    $0x1,%eax
je     1236 <hexstr_to_string+0xe6>
mov    (%r15),%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%esi
shr    $0x1f,%esi
add    %eax,%esi
sar    %esi
add    $0x1,%esi
mov    %rbx,%rdi
call   1050 <xrealloc@plt>
mov    %rax,(%r15)
mov    %r14,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    11fb <hexstr_to_string+0xab>
xor    %r12d,%r12d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    (%r15),%rax
movb   $0x0,(%rax,%r12,1)
movsbl (%r14,%rbx,1),%edi
mov    %rbx,%rsi
call   1070 <char_to_4bits@plt>
mov    (%r15),%rcx
or     %al,(%rcx,%r12,1)
mov    %rbx,%rsi
or     $0x1,%rsi
movsbl (%r14,%rsi,1),%edi
call   1070 <char_to_4bits@plt>
mov    (%r15),%rcx
or     %al,(%rcx,%r12,1)
add    $0x1,%r12
add    $0x2,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     11b0 <hexstr_to_string+0x60>
mov    (%r15),%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
movslq %ecx,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
movslq %ecx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdc3(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [59 x i8] c"Error - string length is odd. 2 hex needed to fill a byte!\00"
define i64 @hexstr_to_string(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
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
%15 = load i8**, i8*** %3
%16 = load i8*, i8** %15
%17 = load i8*, i8** %4
%18 = call i32 @strlen(i8* %17)
%19 = sdiv i32 %18, 2
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = mul i64 %21, 1
%23 = trunc i64 %22 to i32
%24 = call i64 @xrealloc(i8* %16, i32 %23)
%25 = inttoptr i64 %24 to i8*
%26 = load i8**, i8*** %3
store i8* %25, i8** %26
store i64 0, i64* %6
store i64 0, i64* %5
br label %27
27: ; preds = %72, %14
%28 = load i64, i64* %5
%29 = load i8*, i8** %4
%30 = call i32 @strlen(i8* %29)
%31 = sext i32 %30 to i64
%32 = icmp slt i64 %28, %31
br i1 %32, label %33, label %75
33: ; preds = %27
%34 = load i8**, i8*** %3
%35 = load i8*, i8** %34
%36 = load i64, i64* %6
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8 0, i8* %37
%38 = load i8*, i8** %4
%39 = load i64, i64* %5
%40 = getelementptr inbounds i8, i8* %38, i64 %39
%41 = load i8, i8* %40
%42 = load i64, i64* %5
%43 = call signext i8 @char_to_4bits(i8 signext %41, i64 %42)
%44 = sext i8 %43 to i32
%45 = load i8**, i8*** %3
%46 = load i8*, i8** %45
%47 = load i64, i64* %6
%48 = getelementptr inbounds i8, i8* %46, i64 %47
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = or i32 %50, %44
%52 = trunc i32 %51 to i8
store i8 %52, i8* %48
%53 = load i8*, i8** %4
%54 = load i64, i64* %5
%55 = add nsw i64 %54, 1
%56 = getelementptr inbounds i8, i8* %53, i64 %55
%57 = load i8, i8* %56
%58 = load i64, i64* %5
%59 = add nsw i64 %58, 1
%60 = call signext i8 @char_to_4bits(i8 signext %57, i64 %59)
%61 = sext i8 %60 to i32
%62 = load i8**, i8*** %3
%63 = load i8*, i8** %62
%64 = load i64, i64* %6
%65 = getelementptr inbounds i8, i8* %63, i64 %64
%66 = load i8, i8* %65
%67 = sext i8 %66 to i32
%68 = or i32 %67, %61
%69 = trunc i32 %68 to i8
store i8 %69, i8* %65
%70 = load i64, i64* %6
%71 = add nsw i64 %70, 1
store i64 %71, i64* %6
br label %72
72: ; preds = %33
%73 = load i64, i64* %5
%74 = add nsw i64 %73, 2
store i64 %74, i64* %5
br label %27
75: ; preds = %27
%76 = load i8**, i8*** %3
%77 = load i8*, i8** %76
%78 = load i8*, i8** %4
%79 = call i32 @strlen(i8* %78)
%80 = sdiv i32 %79, 2
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i8, i8* %77, i64 %81
store i8 0, i8* %82
%83 = load i8*, i8** %4
%84 = call i32 @strlen(i8* %83)
%85 = sdiv i32 %84, 2
%86 = sext i32 %85 to i64
ret i64 %86
}

LLVM IR (-O2):
@.str = [59 x i8] c"Error - string length is odd. 2 hex needed to fill a byte!\00"
define i64 @hexstr_to_string(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 %6, label %9
6: ; preds = %2
%7 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %2
%10 = load i8*, i8** %0
%11 = tail call i32 @strlen(i8* %1)
%12 = sdiv i32 %11, 2
%13 = add nsw i32 %12, 1
%14 = tail call i64 @xrealloc(i8* %10, i32 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %0
%16 = tail call i32 @strlen(i8* %1)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %43
18: ; preds = %9, %18
%19 = phi i64 [ %38, %18 ], [ 0, %9 ]
%20 = phi i64 [ %39, %18 ], [ 0, %9 ]
%21 = load i8*, i8** %0
%22 = getelementptr inbounds i8, i8* %21, i64 %19
store i8 0, i8* %22
%23 = getelementptr inbounds i8, i8* %1, i64 %20
%24 = load i8, i8* %23
%25 = tail call signext i8 @char_to_4bits(i8 signext %24, i64 %20)
%26 = load i8*, i8** %0
%27 = getelementptr inbounds i8, i8* %26, i64 %19
%28 = load i8, i8* %27
%29 = or i8 %28, %25
store i8 %29, i8* %27
%30 = or i64 %20, 1
%31 = getelementptr inbounds i8, i8* %1, i64 %30
%32 = load i8, i8* %31
%33 = tail call signext i8 @char_to_4bits(i8 signext %32, i64 %30)
%34 = load i8*, i8** %0
%35 = getelementptr inbounds i8, i8* %34, i64 %19
%36 = load i8, i8* %35
%37 = or i8 %36, %33
store i8 %37, i8* %35
%38 = add nuw nsw i64 %19, 1
%39 = add nuw nsw i64 %20, 2
%40 = tail call i32 @strlen(i8* %1)
%41 = sext i32 %40 to i64
%42 = icmp slt i64 %39, %41
br i1 %42, label %18, label %43
43: ; preds = %18, %9
%44 = load i8*, i8** %0
%45 = tail call i32 @strlen(i8* %1)
%46 = sdiv i32 %45, 2
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %44, i64 %47
store i8 0, i8* %48
%49 = tail call i32 @strlen(i8* %1)
%50 = sdiv i32 %49, 2
%51 = sext i32 %50 to i64
ret i64 %51
}