#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*/ memcpy(unsigned char *, char *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ reverse_order(unsigned char *, size_t);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
void convert_to_hex(unsigned char *in, unsigned char *out, size_t len,
                    const unsigned int eflag) {
  unsigned int i;
  char ch[3];
  if (len > 64)
    len = 64;
  memset(out, 0, 129);
  if (eflag)
    reverse_order(in, len);
  for (i = 0; i < len; i++) {
    sprintf(ch, "%02x", (unsigned char)in[i]);
    memcpy(&out[i * 2], ch, 2);
  }
}


x86 assembly (clang, -O0):
<convert_to_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpq   $0x40,-0x18(%rbp)
jbe    116a <convert_to_hex+0x2a>
movq   $0x40,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x81,%edx
call   1030 <memset@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1191 <convert_to_hex+0x51>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <reverse_order@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%rax
jae    11eb <convert_to_hex+0xab>
lea    -0x23(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%ecx
lea    0xe49(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1060 <sprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%eax
shl    $0x1,%eax
mov    %eax,%eax
add    %rax,%rdi
lea    -0x23(%rbp),%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1198 <convert_to_hex+0x58>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_to_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
cmp    $0x40,%rdx
mov    $0x40,%r15d
cmovb  %rdx,%r15
mov    %rsi,%rdi
xor    %esi,%esi
mov    $0x81,%edx
call   1030 <memset@plt>
test   %ebp,%ebp
je     117f <convert_to_hex+0x3f>
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <reverse_order@plt>
test   %r15,%r15
je     11cd <convert_to_hex+0x8d>
xor    %ebp,%ebp
lea    0xe73(%rip),%r12
lea    0x5(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl (%r14,%rbp,1),%edx
mov    %r13,%rdi
mov    %r12,%rsi
call   1060 <sprintf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
add    $0x1,%rbp
add    $0x2,%rbx
cmp    %rbp,%r15
jne    11a0 <convert_to_hex+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%02x\00"
define void @convert_to_hex(i8* %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca [3 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
%11 = load i64, i64* %7
%12 = icmp ugt i64 %11, 64
br i1 %12, label %13, label %14
13: ; preds = %4
store i64 64, i64* %7
br label %14
14: ; preds = %13, %4
%15 = load i8*, i8** %6
%16 = call i32 @memset(i8* %15, i32 0, i32 129)
%17 = load i32, i32* %8
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i8*, i8** %5
%21 = load i64, i64* %7
%22 = call i32 @reverse_order(i8* %20, i64 %21)
br label %23
23: ; preds = %19, %14
store i32 0, i32* %9
br label %24
24: ; preds = %44, %23
%25 = load i32, i32* %9
%26 = zext i32 %25 to i64
%27 = load i64, i64* %7
%28 = icmp ult i64 %26, %27
br i1 %28, label %29, label %47
29: ; preds = %24
%30 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 0
%31 = load i8*, i8** %5
%32 = load i32, i32* %9
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = call i32 @sprintf(i8* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %35)
%37 = load i8*, i8** %6
%38 = load i32, i32* %9
%39 = mul i32 %38, 2
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
%42 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 0
%43 = call i32 @memcpy(i8* %41, i8* %42, i32 2)
br label %44
44: ; preds = %29
%45 = load i32, i32* %9
%46 = add i32 %45, 1
store i32 %46, i32* %9
br label %24
47: ; preds = %24
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define void @convert_to_hex(i8* %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca [3 x i8]
%6 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%7 = icmp ult i64 %2, 64
%8 = select i1 %7, i64 %2, i64 64
%9 = tail call i32 @memset(i8* %1, i32 0, i32 129)
%10 = icmp eq i32 %3, 0
br i1 %10, label %13, label %11
11: ; preds = %4
%12 = tail call i32 @reverse_order(i8* %0, i64 %8)
br label %13
13: ; preds = %11, %4
%14 = icmp eq i64 %8, 0
br i1 %14, label %25, label %15
15: ; preds = %13, %15
%16 = phi i64 [ %23, %15 ], [ 0, %13 ]
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = load i8, i8* %17
%19 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %18)
%20 = shl nuw i64 %16, 1
%21 = getelementptr inbounds i8, i8* %1, i64 %20
%22 = call i32 @memcpy(i8* %21, i8* %6, i32 2)
%23 = add nuw nsw i64 %16, 1
%24 = icmp eq i64 %23, %8
br i1 %24, label %25, label %15
25: ; preds = %15, %13
ret void
}