#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*/ highhex(int);
int /*<<< orphan*/ lowhex(int);
char *mem2hex(char *mem, char *buf, int count) {
  int i;
  int ch;
  for (i = 0; i < count; i++) {
    ch = *mem++;
    *buf++ = highhex(ch);
    *buf++ = lowhex(ch);
  }
  *buf = 0;
  return (buf);
}


x86 assembly (clang, -O0):
<mem2hex>:
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    119f <mem2hex+0x7f>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <highhex@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x1c(%rbp),%edi
call   1030 <lowhex@plt>
mov    %al,%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    113a <mem2hex+0x1a>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem2hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
test   %edx,%edx
jle    116e <mem2hex+0x4e>
mov    %rdi,%r14
mov    %edx,%r12d
xor    %ebx,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax)
movsbl (%r14,%rbp,1),%r13d
mov    %r13d,%edi
call   1040 <highhex@plt>
mov    %al,(%r15,%rbp,2)
mov    %r13d,%edi
call   1030 <lowhex@plt>
mov    %al,0x1(%r15,%rbp,2)
add    $0x1,%rbp
add    $0xfffffffffffffffe,%rbx
cmp    %ebp,%r12d
jne    1140 <mem2hex+0x20>
sub    %rbx,%r15
movb   $0x0,(%r15)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @mem2hex(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
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 = %28, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %6
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %31
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
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = call i32 @highhex(i32 %18)
%20 = trunc i32 %19 to i8
%21 = load i8*, i8** %5
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %5
store i8 %20, i8* %21
%23 = load i32, i32* %8
%24 = call i32 @lowhex(i32 %23)
%25 = trunc i32 %24 to i8
%26 = load i8*, i8** %5
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %5
store i8 %25, i8* %26
br label %28
28: ; preds = %13
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 1
store i32 %30, i32* %7
br label %9
31: ; preds = %9
%32 = load i8*, i8** %5
store i8 0, i8* %32
%33 = load i8*, i8** %5
ret i8* %33
}

LLVM IR (-O2):
define i8* @mem2hex(i8* nocapture %0, i8* writeonly %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %20
5: ; preds = %3, %5
%6 = phi i8* [ %9, %5 ], [ %0, %3 ]
%7 = phi i32 [ %18, %5 ], [ 0, %3 ]
%8 = phi i8* [ %17, %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 i32 @highhex(i32 %11)
%13 = trunc i32 %12 to i8
%14 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %13, i8* %8
%15 = tail call i32 @lowhex(i32 %11)
%16 = trunc i32 %15 to i8
%17 = getelementptr inbounds i8, i8* %8, i64 2
store i8 %16, i8* %14
%18 = add nuw nsw i32 %7, 1
%19 = icmp eq i32 %18, %2
br i1 %19, label %20, label %5
20: ; preds = %5, %3
%21 = phi i8* [ %1, %3 ], [ %17, %5 ]
store i8 0, i8* %21
ret i8* %21
}