#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 */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ fmt_byte_hex(char *, int /*<<< orphan*/ const);
size_t fmt_bytes_hex_reverse(char *out, const uint8_t *ptr, size_t n) {
  size_t i = n;
  while (i--) {
    out += fmt_byte_hex(out, ptr[i]);
  }
  return (n << 1);
}


x86 assembly (clang, -O0):
<fmt_bytes_hex_reverse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
cmp    $0x0,%rax
je     116e <fmt_bytes_hex_reverse+0x5e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
call   1030 <fmt_byte_hex@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    112c <fmt_bytes_hex_reverse+0x1c>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmt_bytes_hex_reverse>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdx,%rdx
je     114b <fmt_bytes_hex_reverse+0x3b>
mov    %rsi,%r15
mov    %rdi,%r12
mov    %r14,%rbx
nopl   0x0(%rax)
mov    -0x4(%r15,%rbx,4),%esi
add    $0xffffffffffffffff,%rbx
mov    %r12,%rdi
call   1030 <fmt_byte_hex@plt>
cltq
add    %rax,%r12
test   %rbx,%rbx
jne    1130 <fmt_bytes_hex_reverse+0x20>
add    %r14,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @fmt_bytes_hex_reverse(i8* %0, i32* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i32* %1, i32** %5
store i64 %2, i64* %6
%8 = load i64, i64* %6
store i64 %8, i64* %7
br label %9
9: ; preds = %13, %3
%10 = load i64, i64* %7
%11 = add i64 %10, -1
store i64 %11, i64* %7
%12 = icmp ne i64 %10, 0
br i1 %12, label %13, label %23
13: ; preds = %9
%14 = load i8*, i8** %4
%15 = load i32*, i32** %5
%16 = load i64, i64* %7
%17 = getelementptr inbounds i32, i32* %15, i64 %16
%18 = load i32, i32* %17
%19 = call i32 @fmt_byte_hex(i8* %14, i32 %18)
%20 = load i8*, i8** %4
%21 = sext i32 %19 to i64
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8* %22, i8** %4
br label %9
23: ; preds = %9
%24 = load i64, i64* %6
%25 = shl i64 %24, 1
ret i64 %25
}

LLVM IR (-O2):
define i64 @fmt_bytes_hex_reverse(i8* %0, i32* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %15, label %5
5: ; preds = %3, %5
%6 = phi i64 [ %8, %5 ], [ %2, %3 ]
%7 = phi i8* [ %13, %5 ], [ %0, %3 ]
%8 = add i64 %6, -1
%9 = getelementptr inbounds i32, i32* %1, i64 %8
%10 = load i32, i32* %9
%11 = tail call i32 @fmt_byte_hex(i8* %7, i32 %10)
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %7, i64 %12
%14 = icmp eq i64 %8, 0
br i1 %14, label %15, label %5
15: ; preds = %5, %3
%16 = shl i64 %2, 1
ret i64 %16
}