#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 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 {
      (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    0xecd(%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     120e <destructiveHexDecode+0xee>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    117e <destructiveHexDecode+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <destructiveHexDecode+0xff>
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     11df <destructiveHexDecode+0xbf>
cmpl   $0xffffffff,-0x28(%rbp)
je     11df <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    11f7 <destructiveHexDecode+0xd7>
lea    0xe2b(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <destructiveHexDecode+0xff>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
jmp    1145 <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     119d <destructiveHexDecode+0x7d>
mov    $0x2,%ebp
nopl   0x0(%rax)
lea    -0x1(%rbp),%r14d
mov    $0xffffffff,%eax
cmpb   $0x0,(%r12,%r14,1)
je     11bb <destructiveHexDecode+0x9b>
movzbl %cl,%esi
lea    0xea6(%rip),%rdi
mov    %r12,%r15
call   1040 <charIndex@plt>
mov    %eax,%ebx
movzbl (%r12,%r14,1),%esi
lea    0xe90(%rip),%rdi
call   1040 <charIndex@plt>
cmp    $0xffffffff,%ebx
je     11aa <destructiveHexDecode+0x8a>
cmp    $0xffffffff,%eax
je     11aa <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    1140 <destructiveHexDecode+0x20>
mov    %r13d,%eax
movb   $0x0,(%r12,%rax,1)
mov    %r13d,%eax
jmp    11bb <destructiveHexDecode+0x9b>
lea    0xe60(%rip),%rdi
call   1030 <puts@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 = %63, %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 %66
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 %72
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 %60
58: ; preds = %45, %27
%59 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %72
60: ; preds = %48
%61 = load i32, i32* %6
%62 = add i32 %61, 1
store i32 %62, i32* %6
br label %63
63: ; preds = %60
%64 = load i32, i32* %5
%65 = add i32 %64, 2
store i32 %65, i32* %5
br label %9
66: ; preds = %9
%67 = load i8*, i8** %3
%68 = load i32, i32* %6
%69 = zext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %67, i64 %69
store i8 0, i8* %70
%71 = load i32, i32* %6
store i32 %71, i32* %2
br label %72
72: ; preds = %66, %58, %26
%73 = load i32, i32* %2
ret i32 %73
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789abcdef\00"
@str = [35 x i8] c"Input contains invalid characters.\00"
define i32 @destructiveHexDecode(i8* nocapture %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %34, 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 %38, 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 %34, label %4
32: ; preds = %13
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %38
34: ; preds = %20, %1
%35 = phi i32 [ 0, %1 ], [ %26, %20 ]
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %0, i64 %36
store i8 0, i8* %37
br label %38
38: ; preds = %4, %34, %32
%39 = phi i32 [ -1, %32 ], [ %35, %34 ], [ -1, %4 ]
ret i32 %39
}