#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*/);
int /*<<< orphan*/ set_char(int /*<<< orphan*/, unsigned char);
void hex2mem(unsigned char *buf, unsigned long mem, int count) {
  int i;
  unsigned char ch;
  for (i = 0; i < count; i++) {
    ch = hex(*buf++) << 4;
    ch = ch + hex(*buf++);
    set_char(mem++, ch);
  }
}


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    11b9 <hex2mem+0x99>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movzbl (%rax),%edi
call   1040 <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)
movzbl (%rax),%edi
call   1040 <hex@plt>
mov    %al,%cl
mov    -0x20(%rbp),%eax
movzbl %cl,%ecx
add    %ecx,%eax
mov    %al,-0x19(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    %eax,%edi
movzbl -0x19(%rbp),%esi
call   1030 <set_char@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    113a <hex2mem+0x1a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex2mem>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edx,%edx
jle    1173 <hex2mem+0x53>
mov    %rsi,%r14
mov    %rdi,%r15
mov    %edx,%r12d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movzbl (%r15,%rbx,2),%edi
call   1040 <hex@plt>
mov    %eax,%ebp
shl    $0x4,%bpl
movzbl 0x1(%r15,%rbx,2),%edi
call   1040 <hex@plt>
add    %bpl,%al
lea    (%r14,%rbx,1),%edi
movzbl %al,%esi
call   1030 <set_char@plt>
add    $0x1,%rbx
cmp    %ebx,%r12d
jne    1140 <hex2mem+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @hex2mem(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %9
9: ; preds = %37, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %6
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %40
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 = zext 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 = zext 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 i64, i64* %5
%33 = add i64 %32, 1
store i64 %33, i64* %5
%34 = trunc i64 %32 to i32
%35 = load i8, i8* %8
%36 = call i32 @set_char(i32 %34, i8 zeroext %35)
br label %37
37: ; preds = %13
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %9
40: ; preds = %9
ret void
}

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