#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 hex_to_char(char const);
char *malloc(int);
char *unhex(const char *hex) {
  char *buffer = malloc(17 * sizeof(char));
  buffer[16] = 0;
  for (int i = 0; i < 16; i++) {
    buffer[i] = hex_to_char(hex[i * 2]) | (hex_to_char(hex[i * 2 + 1]) << 4);
  }
  return buffer;
}


x86 assembly (clang, -O0):
<unhex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x11,%edi
call   1030 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,0x10(%rax)
movl   $0x0,-0x14(%rbp)
cmpl   $0x10,-0x14(%rbp)
jge    11aa <unhex+0x8a>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <hex_to_char@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <hex_to_char@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
shl    $0x4,%ecx
or     %ecx,%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1149 <unhex+0x29>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unhex>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
mov    $0x11,%edi
call   1030 <malloc@plt>
mov    %rax,%r14
movb   $0x0,0x10(%rax)
xor    %ebx,%ebx
nopl   (%rax)
movsbl (%r15,%rbx,2),%edi
call   1040 <hex_to_char@plt>
mov    %eax,%ebp
movsbl 0x1(%r15,%rbx,2),%edi
call   1040 <hex_to_char@plt>
shl    $0x4,%eax
or     %ebp,%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    $0x10,%rbx
jne    1140 <unhex+0x20>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @unhex(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %2
%5 = call i8* @malloc(i32 17)
store i8* %5, i8** %3
%6 = load i8*, i8** %3
%7 = getelementptr inbounds i8, i8* %6, i64 16
store i8 0, i8* %7
store i32 0, i32* %4
br label %8
8: ; preds = %34, %1
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 16
br i1 %10, label %11, label %37
11: ; preds = %8
%12 = load i8*, i8** %2
%13 = load i32, i32* %4
%14 = mul nsw i32 %13, 2
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %12, i64 %15
%17 = load i8, i8* %16
%18 = call i32 @hex_to_char(i8 signext %17)
%19 = load i8*, i8** %2
%20 = load i32, i32* %4
%21 = mul nsw i32 %20, 2
%22 = add nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %19, i64 %23
%25 = load i8, i8* %24
%26 = call i32 @hex_to_char(i8 signext %25)
%27 = shl i32 %26, 4
%28 = or i32 %18, %27
%29 = trunc i32 %28 to i8
%30 = load i8*, i8** %3
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 %29, i8* %33
br label %34
34: ; preds = %11
%35 = load i32, i32* %4
%36 = add nsw i32 %35, 1
store i32 %36, i32* %4
br label %8
37: ; preds = %8
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
define noalias i8* @unhex(i8* nocapture %0) {
%2 = tail call dereferenceable_or_null(17) i8* @malloc(i32 17)
%3 = getelementptr inbounds i8, i8* %2, i64 16
store i8 0, i8* %3
br label %5
4: ; preds = %5
ret i8* %2
5: ; preds = %1, %5
%6 = phi i64 [ 0, %1 ], [ %19, %5 ]
%7 = shl nuw nsw i64 %6, 1
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = load i8, i8* %8
%10 = tail call i32 @hex_to_char(i8 signext %9)
%11 = or i64 %7, 1
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = tail call i32 @hex_to_char(i8 signext %13)
%15 = shl i32 %14, 4
%16 = or i32 %15, %10
%17 = trunc i32 %16 to i8
%18 = getelementptr inbounds i8, i8* %2, i64 %6
store i8 %17, i8* %18
%19 = add nuw nsw i64 %6, 1
%20 = icmp eq i64 %19, 16
br i1 %20, label %4, label %5
}