#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*/ ERROR(int /*<<< orphan*/);
int charIndex(unsigned char *, unsigned char);
int /*<<< orphan*/ printf(char *);
int destructiveHexDecode(unsigned char *encoded) {
  unsigned char *hexdict = (unsigned char *)"0123456789abcdef";
  unsigned i, c;
  int first, second;
  c = 0;
  for (i = 0; encoded[i] != '\0'; i += 2) {
    if (encoded[i + 1] == '\0') {
      return -1;
    }
    first = charIndex(hexdict, encoded[i]);
    second = charIndex(hexdict, encoded[i + 1]);
    if (first != -1 && second != -1) {
      encoded[c] = first * 16 + second;
    } else {
      ERROR(printf("Input contains invalid characters.\n"));
      return -1;
    }
    c++;
  }
  encoded[c] = 0;
  return c;
}


x86 assembly (clang, -O0):
<destructiveHexDecode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xebd(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1225 <destructiveHexDecode+0xf5>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    118e <destructiveHexDecode+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <destructiveHexDecode+0x106>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movzbl (%rax,%rcx,1),%esi
call   1040 <charIndex@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%esi
call   1040 <charIndex@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
je     11ef <destructiveHexDecode+0xbf>
cmpl   $0xffffffff,-0x28(%rbp)
je     11ef <destructiveHexDecode+0xbf>
mov    -0x24(%rbp),%eax
shl    $0x4,%eax
add    -0x28(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
jmp    120e <destructiveHexDecode+0xde>
lea    0xe1b(%rip),%rdi
call   1030 <printf@plt>
mov    %eax,%edi
call   1050 <ERROR@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <destructiveHexDecode+0x106>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
jmp    1155 <destructiveHexDecode+0x25>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<destructiveHexDecode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
mov    (%rdi),%cl
xor    %r13d,%r13d
test   %cl,%cl
je     11ad <destructiveHexDecode+0x7d>
mov    $0x2,%ebp
nopl   0x0(%rax)
lea    -0x1(%rbp),%r14d
mov    $0xffffffff,%eax
cmpb   $0x0,(%r12,%r14,1)
je     11d2 <destructiveHexDecode+0xa2>
movzbl %cl,%esi
lea    0xe96(%rip),%rdi
mov    %r12,%r15
call   1040 <charIndex@plt>
mov    %eax,%ebx
movzbl (%r12,%r14,1),%esi
lea    0xe80(%rip),%rdi
call   1040 <charIndex@plt>
cmp    $0xffffffff,%ebx
je     11ba <destructiveHexDecode+0x8a>
cmp    $0xffffffff,%eax
je     11ba <destructiveHexDecode+0x8a>
shl    $0x4,%ebx
add    %ebx,%eax
mov    %r13d,%ecx
mov    %al,(%r12,%rcx,1)
add    $0x1,%r13d
mov    %ebp,%eax
movzbl (%r12,%rax,1),%ecx
add    $0x2,%ebp
test   %cl,%cl
jne    1150 <destructiveHexDecode+0x20>
mov    %r13d,%eax
movb   $0x0,(%r12,%rax,1)
mov    %r13d,%eax
jmp    11d2 <destructiveHexDecode+0xa2>
lea    0xe50(%rip),%rdi
call   1030 <printf@plt>
mov    %eax,%edi
call   1050 <ERROR@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"0123456789abcdef\00"
@.str.1 = [36 x i8] c"Input contains invalid characters.\0A\00"
define i32 @destructiveHexDecode(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %4
store i32 0, i32* %6
store i32 0, i32* %5
br label %9
9: ; preds = %64, %1
%10 = load i8*, i8** %3
%11 = load i32, i32* %5
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %67
17: ; preds = %9
%18 = load i8*, i8** %3
%19 = load i32, i32* %5
%20 = add i32 %19, 1
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %18, i64 %21
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %17
store i32 -1, i32* %2
br label %73
27: ; preds = %17
%28 = load i8*, i8** %4
%29 = load i8*, i8** %3
%30 = load i32, i32* %5
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i8, i8* %32
%34 = call i32 @charIndex(i8* %28, i8 zeroext %33)
store i32 %34, i32* %7
%35 = load i8*, i8** %4
%36 = load i8*, i8** %3
%37 = load i32, i32* %5
%38 = add i32 %37, 1
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
%41 = load i8, i8* %40
%42 = call i32 @charIndex(i8* %35, i8 zeroext %41)
store i32 %42, i32* %8
%43 = load i32, i32* %7
%44 = icmp ne i32 %43, -1
br i1 %44, label %45, label %58
45: ; preds = %27
%46 = load i32, i32* %8
%47 = icmp ne i32 %46, -1
br i1 %47, label %48, label %58
48: ; preds = %45
%49 = load i32, i32* %7
%50 = mul nsw i32 %49, 16
%51 = load i32, i32* %8
%52 = add nsw i32 %50, %51
%53 = trunc i32 %52 to i8
%54 = load i8*, i8** %3
%55 = load i32, i32* %6
%56 = zext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
store i8 %53, i8* %57
br label %61
58: ; preds = %45, %27
%59 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%60 = call i32 @ERROR(i32 %59)
store i32 -1, i32* %2
br label %73
61: ; preds = %48
%62 = load i32, i32* %6
%63 = add i32 %62, 1
store i32 %63, i32* %6
br label %64
64: ; preds = %61
%65 = load i32, i32* %5
%66 = add i32 %65, 2
store i32 %66, i32* %5
br label %9
67: ; preds = %9
%68 = load i8*, i8** %3
%69 = load i32, i32* %6
%70 = zext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
store i8 0, i8* %71
%72 = load i32, i32* %6
store i32 %72, i32* %2
br label %73
73: ; preds = %67, %58, %26
%74 = load i32, i32* %2
ret i32 %74
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789abcdef\00"
@.str.1 = [36 x i8] c"Input contains invalid characters.\0A\00"
define i32 @destructiveHexDecode(i8* nocapture %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %35, label %4
4: ; preds = %1, %20
%5 = phi i8 [ %30, %20 ], [ %2, %1 ]
%6 = phi i32 [ %27, %20 ], [ 0, %1 ]
%7 = phi i32 [ %26, %20 ], [ 0, %1 ]
%8 = or i32 %6, 1
%9 = zext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 0
br i1 %12, label %39, label %13
13: ; preds = %4
%14 = tail call i32 @charIndex(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8 zeroext %5)
%15 = load i8, i8* %10
%16 = tail call i32 @charIndex(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8 zeroext %15)
%17 = icmp ne i32 %14, -1
%18 = icmp ne i32 %16, -1
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %20, label %32
20: ; preds = %13
%21 = shl nsw i32 %14, 4
%22 = add nsw i32 %16, %21
%23 = trunc i32 %22 to i8
%24 = zext i32 %7 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
store i8 %23, i8* %25
%26 = add i32 %7, 1
%27 = add i32 %6, 2
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %0, i64 %28
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %35, label %4
32: ; preds = %13
%33 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%34 = tail call i32 @ERROR(i32 %33)
br label %39
35: ; preds = %20, %1
%36 = phi i32 [ 0, %1 ], [ %26, %20 ]
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %0, i64 %37
store i8 0, i8* %38
br label %39
39: ; preds = %4, %35, %32
%40 = phi i32 [ -1, %32 ], [ %36, %35 ], [ -1, %4 ]
ret i32 %40
}