#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 *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int strlen(char *);
char *hex_decode(char *in) {
  char *out;
  int i, b;
  int len = strlen(in);
  len = len / 2 + 1;
  out = malloc(len * sizeof(char));
  memset(out, 0, len * sizeof(char));
  for (i = 0; i < len - 1; i++) {
    sscanf(in, "%2X", &b);
    out[i] = b;
    in = in + 2;
  }
  return out;
}


x86 assembly (clang, -O0):
<hex_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11e7 <hex_decode+0xa7>
mov    -0x8(%rbp),%rdi
lea    0xe4c(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1060 <sscanf@plt>
mov    -0x18(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1198 <hex_decode+0x58>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_decode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%r12d
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
lea    0x1(%rbp),%r15d
mov    %r15d,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1040 <memset@plt>
cmp    $0x2,%r12d
jl     11c4 <hex_decode+0x84>
mov    %ebp,%r13d
xor    %ebp,%ebp
lea    0xe73(%rip),%r15
lea    0x4(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <sscanf@plt>
movzbl 0x4(%rsp),%eax
mov    %al,(%r14,%rbp,1)
add    $0x2,%rbx
add    $0x1,%rbp
cmp    %rbp,%r13
jne    11a0 <hex_decode+0x60>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%2X\00"
define i8* @hex_decode(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = sdiv i32 %9, 2
%11 = add nsw i32 %10, 1
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = sext i32 %12 to i64
%14 = mul i64 %13, 1
%15 = trunc i64 %14 to i32
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %3
%17 = load i8*, i8** %3
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = mul i64 %19, 1
%21 = trunc i64 %20 to i32
%22 = call i32 @memset(i8* %17, i32 0, i32 %21)
store i32 0, i32* %4
br label %23
23: ; preds = %39, %1
%24 = load i32, i32* %4
%25 = load i32, i32* %6
%26 = sub nsw i32 %25, 1
%27 = icmp slt i32 %24, %26
br i1 %27, label %28, label %42
28: ; preds = %23
%29 = load i8*, i8** %2
%30 = call i32 @sscanf(i8* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %5)
%31 = load i32, i32* %5
%32 = trunc i32 %31 to i8
%33 = load i8*, i8** %3
%34 = load i32, i32* %4
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 %32, i8* %36
%37 = load i8*, i8** %2
%38 = getelementptr inbounds i8, i8* %37, i64 2
store i8* %38, i8** %2
br label %39
39: ; preds = %28
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
store i32 %41, i32* %4
br label %23
42: ; preds = %23
%43 = load i8*, i8** %3
ret i8* %43
}

LLVM IR (-O2):
@.str = [4 x i8] c"%2X\00"
define i8* @hex_decode(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 @strlen(i8* %0)
%5 = sdiv i32 %4, 2
%6 = add nsw i32 %5, 1
%7 = tail call i8* @malloc(i32 %6)
%8 = tail call i32 @memset(i8* %7, i32 0, i32 %6)
%9 = icmp sgt i32 %4, 1
br i1 %9, label %10, label %22
10: ; preds = %1
%11 = zext i32 %5 to i64
br label %12
12: ; preds = %10, %12
%13 = phi i64 [ 0, %10 ], [ %20, %12 ]
%14 = phi i8* [ %0, %10 ], [ %19, %12 ]
%15 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %2)
%16 = load i32, i32* %2
%17 = trunc i32 %16 to i8
%18 = getelementptr inbounds i8, i8* %7, i64 %13
store i8 %17, i8* %18
%19 = getelementptr inbounds i8, i8* %14, i64 2
%20 = add nuw nsw i64 %13, 1
%21 = icmp eq i64 %20, %11
br i1 %21, label %22, label %12
22: ; preds = %12, %1
ret i8* %7
}