#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 */
unsigned char hex(int /*<<< orphan*/);
char *hex2mem(char *buf, char *mem, int count) {
  int i;
  unsigned char ch;
  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    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a3 <hex2mem+0x93>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%edi
call   1030 <hex@plt>
movzbl %al,%eax
shl    $0x4,%eax
mov    %al,-0x19(%rbp)
movzbl -0x19(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%edi
call   1030 <hex@plt>
mov    %al,%cl
mov    -0x20(%rbp),%eax
movzbl %cl,%ecx
add    %ecx,%eax
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    112a <hex2mem+0x1a>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2mem>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
test   %edx,%edx
jle    115e <hex2mem+0x4e>
mov    %rdi,%r15
mov    %edx,%r12d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,2),%edi
call   1030 <hex@plt>
mov    %eax,%ebp
shl    $0x4,%bpl
movsbl 0x1(%r15,%rbx,2),%edi
call   1030 <hex@plt>
add    %bpl,%al
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %ebx,%r12d
jne    1130 <hex2mem+0x20>
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, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %9
9: ; preds = %35, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %6
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %38
13: ; preds = %9
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i32 1
store i8* %15, i8** %4
%16 = load i8, i8* %14
%17 = sext i8 %16 to i32
%18 = call zeroext i8 @hex(i32 %17)
%19 = zext i8 %18 to i32
%20 = shl i32 %19, 4
%21 = trunc i32 %20 to i8
store i8 %21, i8* %8
%22 = load i8, i8* %8
%23 = zext i8 %22 to i32
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %4
%26 = load i8, i8* %24
%27 = sext i8 %26 to i32
%28 = call zeroext i8 @hex(i32 %27)
%29 = zext i8 %28 to i32
%30 = add nsw i32 %23, %29
%31 = trunc i32 %30 to i8
store i8 %31, i8* %8
%32 = load i8, i8* %8
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %5
store i8 %32, i8* %33
br label %35
35: ; preds = %13
%36 = load i32, i32* %7
%37 = add nsw i32 %36, 1
store i32 %37, i32* %7
br label %9
38: ; preds = %9
%39 = load i8*, i8** %5
ret i8* %39
}

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