#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 int /*<<< orphan*/ buf;

/* Variables and functions */
int decode_intern(char const);
int strlen(char const *);
char *hexdecode(const char *in) {
  static char buf[1024];
  const char *c;
  char *o = buf;
  if (strlen(in) > sizeof(buf) * 2) {
    return NULL;
  }
  for (c = in; *c; c += 2) {
    *o = (decode_intern(*c) << 4 | decode_intern(*(c + 1)));
    o++;
  }
  *o = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<hexdecode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0x2f0d(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cltq
cmp    $0x800,%rax
jbe    115b <hexdecode+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    11cd <hexdecode+0xad>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     11bb <hexdecode+0x9b>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1040 <decode_intern@plt>
shl    $0x4,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%edi
call   1040 <decode_intern@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
or     %ecx,%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
jmp    1163 <hexdecode+0x43>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
lea    0x2e77(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexdecode>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
call   1030 <strlen@plt>
cmp    $0x800,%eax
jbe    113b <hexdecode+0x1b>
xor    %r14d,%r14d
jmp    118e <hexdecode+0x6e>
mov    (%r15),%al
lea    0x2efb(%rip),%r14
mov    %r14,%rbx
test   %al,%al
je     118b <hexdecode+0x6b>
add    $0x2,%r15
lea    0x2ee9(%rip),%rbx
nopw   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1040 <decode_intern@plt>
mov    %eax,%ebp
shl    $0x4,%ebp
movsbl -0x1(%r15),%edi
call   1040 <decode_intern@plt>
or     %ebp,%eax
mov    %al,(%rbx)
add    $0x1,%rbx
movzbl (%r15),%eax
add    $0x2,%r15
test   %al,%al
jne    1160 <hexdecode+0x40>
movb   $0x0,(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@hexdecode.buf = internal global [1024 x i8] zeroinitializer
define i8* @hexdecode(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @hexdecode.buf, i64 0, i64 0), i8** %5
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
%8 = sext i32 %7 to i64
%9 = icmp ugt i64 %8, 2048
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* null, i8** %2
br label %36
11: ; preds = %1
%12 = load i8*, i8** %3
store i8* %12, i8** %4
br label %13
13: ; preds = %31, %11
%14 = load i8*, i8** %4
%15 = load i8, i8* %14
%16 = icmp ne i8 %15, 0
br i1 %16, label %17, label %34
17: ; preds = %13
%18 = load i8*, i8** %4
%19 = load i8, i8* %18
%20 = call i32 @decode_intern(i8 signext %19)
%21 = shl i32 %20, 4
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = load i8, i8* %23
%25 = call i32 @decode_intern(i8 signext %24)
%26 = or i32 %21, %25
%27 = trunc i32 %26 to i8
%28 = load i8*, i8** %5
store i8 %27, i8* %28
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %5
br label %31
31: ; preds = %17
%32 = load i8*, i8** %4
%33 = getelementptr inbounds i8, i8* %32, i64 2
store i8* %33, i8** %4
br label %13
34: ; preds = %13
%35 = load i8*, i8** %5
store i8 0, i8* %35
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @hexdecode.buf, i64 0, i64 0), i8** %2
br label %36
36: ; preds = %34, %10
%37 = load i8*, i8** %2
ret i8* %37
}

LLVM IR (-O2):
@hexdecode.buf = internal global [1024 x i8] zeroinitializer
define i8* @hexdecode(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp ugt i32 %2, 2048
br i1 %3, label %24, label %4
4: ; preds = %1
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %22, label %7
7: ; preds = %4, %7
%8 = phi i8 [ %20, %7 ], [ %5, %4 ]
%9 = phi i8* [ %18, %7 ], [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @hexdecode.buf, i64 0, i64 0), %4 ]
%10 = phi i8* [ %19, %7 ], [ %0, %4 ]
%11 = tail call i32 @decode_intern(i8 signext %8)
%12 = shl i32 %11, 4
%13 = getelementptr inbounds i8, i8* %10, i64 1
%14 = load i8, i8* %13
%15 = tail call i32 @decode_intern(i8 signext %14)
%16 = or i32 %15, %12
%17 = trunc i32 %16 to i8
store i8 %17, i8* %9
%18 = getelementptr inbounds i8, i8* %9, i64 1
%19 = getelementptr inbounds i8, i8* %10, i64 2
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
br i1 %21, label %22, label %7
22: ; preds = %7, %4
%23 = phi i8* [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @hexdecode.buf, i64 0, i64 0), %4 ], [ %18, %7 ]
store i8 0, i8* %23
br label %24
24: ; preds = %1, %22
%25 = phi i8* [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @hexdecode.buf, i64 0, i64 0), %22 ], [ null, %1 ]
ret i8* %25
}