#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 /*<<< orphan*/ assert(int);
unsigned char hex(int /*<<< orphan*/);
int strlen(char const *);
unsigned char *hex2mem(const char *buf, unsigned char *mem, size_t count) {
  size_t i;
  unsigned char ch;
  assert(strlen(buf) >= (2 * count));
  for (i = 0; i < count; i++) {
    ch = hex(*buf++) << 4;
    ch = ch + hex(*buf++);
    *mem++ = ch;
  }
  return mem;
}


x86 assembly (clang, -O0):
<hex2mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cltq
mov    -0x18(%rbp),%rcx
shl    $0x1,%rcx
cmp    %rcx,%rax
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11ed <hex2mem+0xbd>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%edi
call   1050 <hex@plt>
movzbl %al,%eax
shl    $0x4,%eax
mov    %al,-0x21(%rbp)
movzbl -0x21(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%edi
call   1050 <hex@plt>
mov    %al,%cl
mov    -0x28(%rbp),%eax
movzbl %cl,%ecx
add    %ecx,%eax
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    116f <hex2mem+0x3f>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2mem>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
call   1030 <strlen@plt>
cltq
lea    (%r15,%r15,1),%rcx
xor    %edi,%edi
cmp    %rax,%rcx
setbe  %dil
call   1040 <assert@plt>
test   %r15,%r15
je     119e <hex2mem+0x6e>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl (%r12,%rbx,2),%edi
call   1050 <hex@plt>
mov    %eax,%ebp
shl    $0x4,%bpl
movsbl 0x1(%r12,%rbx,2),%edi
call   1050 <hex@plt>
add    %bpl,%al
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1170 <hex2mem+0x40>
add    %rbx,%r14
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @hex2mem(i8* %0, i8* writeonly %1, i64 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = sext i32 %4 to i64
%6 = shl i64 %2, 1
%7 = icmp ule i64 %6, %5
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp eq i64 %2, 0
br i1 %10, label %28, label %11
11: ; preds = %3, %11
%12 = phi i8* [ %20, %11 ], [ %0, %3 ]
%13 = phi i64 [ %26, %11 ], [ 0, %3 ]
%14 = phi i8* [ %25, %11 ], [ %1, %3 ]
%15 = getelementptr inbounds i8, i8* %12, i64 1
%16 = load i8, i8* %12
%17 = sext i8 %16 to i32
%18 = tail call zeroext i8 @hex(i32 %17)
%19 = shl i8 %18, 4
%20 = getelementptr inbounds i8, i8* %12, i64 2
%21 = load i8, i8* %15
%22 = sext i8 %21 to i32
%23 = tail call zeroext i8 @hex(i32 %22)
%24 = add i8 %23, %19
%25 = getelementptr inbounds i8, i8* %14, i64 1
store i8 %24, i8* %14
%26 = add nuw i64 %13, 1
%27 = icmp eq i64 %26, %2
br i1 %27, label %28, label %11
28: ; preds = %11, %3
%29 = phi i8* [ %1, %3 ], [ %25, %11 ]
ret i8* %29
}