#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*/);
size_t fmt_bytes_hex(char *out, const uint8_t *ptr, size_t n) {
  size_t len = n * 2;
  if (out) {
    while (n--) {
      out += fmt_byte_hex(out, *ptr++);
    }
  }
  return len;
}


x86 assembly (clang, -O0):
<fmt_bytes_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     118d <fmt_bytes_hex+0x7d>
jmp    1140 <fmt_bytes_hex+0x30>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
cmp    $0x0,%rax
je     1188 <fmt_bytes_hex+0x78>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%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    1140 <fmt_bytes_hex+0x30>
jmp    118d <fmt_bytes_hex+0x7d>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmt_bytes_hex>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
test   %rdi,%rdi
je     114a <fmt_bytes_hex+0x3a>
test   %r15,%r15
je     114a <fmt_bytes_hex+0x3a>
mov    %rsi,%r14
mov    %rdi,%r12
xor    %ebx,%ebx
nopl   (%rax)
mov    (%r14,%rbx,4),%esi
mov    %r12,%rdi
call   1030 <fmt_byte_hex@plt>
cltq
add    %rax,%r12
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1130 <fmt_bytes_hex+0x20>
add    %r15,%r15
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @fmt_bytes_hex(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
%9 = mul i64 %8, 2
store i64 %9, i64* %7
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %27
12: ; preds = %3
br label %13
13: ; preds = %17, %12
%14 = load i64, i64* %6
%15 = add i64 %14, -1
store i64 %15, i64* %6
%16 = icmp ne i64 %14, 0
br i1 %16, label %17, label %26
17: ; preds = %13
%18 = load i8*, i8** %4
%19 = load i32*, i32** %5
%20 = getelementptr inbounds i32, i32* %19, i32 1
store i32* %20, i32** %5
%21 = load i32, i32* %19
%22 = call i32 @fmt_byte_hex(i8* %18, i32 %21)
%23 = load i8*, i8** %4
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8* %25, i8** %4
br label %13
26: ; preds = %13
br label %27
27: ; preds = %26, %3
%28 = load i64, i64* %7
ret i64 %28
}

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