#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 uint8_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
scalar_t__ malloc(size_t);
int parse_hex_digit(char);
size_t strlen(char *);
uint8_t *hex2bin(char *hex) {
  size_t len = strlen(hex);
  assert(len % 2 == 0);
  uint8_t *bin = (uint8_t *)malloc(len / 2);
  size_t i;
  for (i = 0; i < len / 2; i++) {
    uint8_t hi = parse_hex_digit(hex[i * 2]);
    uint8_t lo = parse_hex_digit(hex[i * 2 + 1]);
    bin[i] = hi * 16 + lo;
  }
  return bin;
}


x86 assembly (clang, -O0):
<hex2bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
shr    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
shr    $0x1,%rcx
cmp    %rcx,%rax
jae    11f6 <hex2bin+0xb6>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x1,%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <parse_hex_digit@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x1,%rcx
movsbl 0x1(%rax,%rcx,1),%edi
call   1030 <parse_hex_digit@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edx
shl    $0x4,%edx
add    -0x28(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    118b <hex2bin+0x4b>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2bin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r13
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %r12d,%edi
not    %edi
and    $0x1,%edi
call   1050 <assert@plt>
mov    %r12,%r15
shr    %r15
mov    %r15,%rdi
call   1060 <malloc@plt>
mov    %rax,%r14
cmp    $0x2,%r12
jb     11ba <hex2bin+0x7a>
cmp    $0x2,%r15
mov    $0x1,%r12d
cmovae %r15,%r12
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movsbl 0x0(%r13,%rbx,2),%edi
call   1030 <parse_hex_digit@plt>
mov    %eax,%ebp
movsbl 0x1(%r13,%rbx,2),%edi
call   1030 <parse_hex_digit@plt>
shl    $0x4,%ebp
add    %eax,%ebp
mov    %ebp,(%r14,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1190 <hex2bin+0x50>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @hex2bin(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %3
%10 = load i64, i64* %3
%11 = urem i64 %10, 2
%12 = icmp eq i64 %11, 0
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i64, i64* %3
%16 = udiv i64 %15, 2
%17 = call i64 @malloc(i64 %16)
%18 = inttoptr i64 %17 to i32*
store i32* %18, i32** %4
store i64 0, i64* %5
br label %19
19: ; preds = %45, %1
%20 = load i64, i64* %5
%21 = load i64, i64* %3
%22 = udiv i64 %21, 2
%23 = icmp ult i64 %20, %22
br i1 %23, label %24, label %48
24: ; preds = %19
%25 = load i8*, i8** %2
%26 = load i64, i64* %5
%27 = mul i64 %26, 2
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = call i32 @parse_hex_digit(i8 signext %29)
store i32 %30, i32* %6
%31 = load i8*, i8** %2
%32 = load i64, i64* %5
%33 = mul i64 %32, 2
%34 = add i64 %33, 1
%35 = getelementptr inbounds i8, i8* %31, i64 %34
%36 = load i8, i8* %35
%37 = call i32 @parse_hex_digit(i8 signext %36)
store i32 %37, i32* %7
%38 = load i32, i32* %6
%39 = mul nsw i32 %38, 16
%40 = load i32, i32* %7
%41 = add nsw i32 %39, %40
%42 = load i32*, i32** %4
%43 = load i64, i64* %5
%44 = getelementptr inbounds i32, i32* %42, i64 %43
store i32 %41, i32* %44
br label %45
45: ; preds = %24
%46 = load i64, i64* %5
%47 = add i64 %46, 1
store i64 %47, i64* %5
br label %19
48: ; preds = %19
%49 = load i32*, i32** %4
ret i32* %49
}

LLVM IR (-O2):
define i32* @hex2bin(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = trunc i64 %2 to i32
%4 = and i32 %3, 1
%5 = xor i32 %4, 1
%6 = tail call i32 @assert(i32 %5)
%7 = lshr i64 %2, 1
%8 = tail call i64 @malloc(i64 %7)
%9 = inttoptr i64 %8 to i32*
%10 = icmp ult i64 %2, 2
br i1 %10, label %28, label %11
11: ; preds = %1
%12 = call i64 @llvm.umax.i64(i64 %7, i64 1)
br label %13
13: ; preds = %11, %13
%14 = phi i64 [ %26, %13 ], [ 0, %11 ]
%15 = shl nuw i64 %14, 1
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = tail call i32 @parse_hex_digit(i8 signext %17)
%19 = or i64 %15, 1
%20 = getelementptr inbounds i8, i8* %0, i64 %19
%21 = load i8, i8* %20
%22 = tail call i32 @parse_hex_digit(i8 signext %21)
%23 = shl nsw i32 %18, 4
%24 = add nsw i32 %22, %23
%25 = getelementptr inbounds i32, i32* %9, i64 %14
store i32 %24, i32* %25
%26 = add nuw nsw i64 %14, 1
%27 = icmp eq i64 %26, %12
br i1 %27, label %28, label %13
28: ; preds = %13, %1
ret i32* %9
}