#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*/ printf(char *, size_t const, ...);
int /*<<< orphan*/ puts(char const *);
void print_hex(const char *message, const unsigned char *x, const size_t len) {
  puts(message);
  const size_t clamped_len = (len <= 1024) ? len : 1024;
  for (size_t i = 0; i < clamped_len; i++) {
    if ((i != 0) && (i % 16 == 0))
      puts("");
    printf("%02x ", x[i]);
  }
  if (clamped_len == len) {
    printf("     %zu (octets)\n\n", len);
  } else {
    printf("     %zu/%zu (octets)\n\n", clamped_len, len);
  }
}


x86 assembly (clang, -O0):
<print_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <puts@plt>
cmpq   $0x400,-0x18(%rbp)
ja     1158 <print_hex+0x38>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1166 <print_hex+0x46>
mov    $0x400,%eax
mov    %rax,-0x30(%rbp)
jmp    1166 <print_hex+0x46>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11da <print_hex+0xba>
cmpq   $0x0,-0x28(%rbp)
je     11ad <print_hex+0x8d>
mov    -0x28(%rbp),%rax
and    $0xf,%rax
cmp    $0x0,%rax
jne    11ad <print_hex+0x8d>
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,%esi
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1176 <print_hex+0x56>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11ff <print_hex+0xdf>
mov    -0x18(%rbp),%rsi
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1215 <print_hex+0xf5>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_hex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
call   1040 <puts@plt>
cmp    $0x401,%r14
mov    $0x400,%r13d
cmovb  %r14,%r13
test   %r13,%r13
je     11a3 <print_hex+0x73>
lea    0xe9f(%rip),%r12
xor    %ebx,%ebx
jmp    1188 <print_hex+0x58>
cs nopw 0x0(%rax,%rax,1)
nop
movzbl (%r15,%rbx,1),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
je     11a3 <print_hex+0x73>
test   %rbx,%rbx
je     1170 <print_hex+0x40>
mov    %ebx,%eax
and    $0xf,%eax
test   %rax,%rax
jne    1170 <print_hex+0x40>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1170 <print_hex+0x40>
cmp    $0x401,%r14
jae    11c6 <print_hex+0x96>
lea    0xe53(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1050 <printf@plt>
lea    0xe4d(%rip),%rdi
mov    %r13,%rsi
mov    %r14,%rdx
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1050 <printf@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [6 x i8] c"%02x \00"
@.str.2 = [20 x i8] c" %zu (octets)\0A\0A\00"
@.str.3 = [24 x i8] c" %zu/%zu (octets)\0A\0A\00"
define void @print_hex(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %4
%10 = call i32 @puts(i8* %9)
%11 = load i64, i64* %6
%12 = icmp ule i64 %11, 1024
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = load i64, i64* %6
br label %16
15: ; preds = %3
br label %16
16: ; preds = %15, %13
%17 = phi i64 [ %14, %13 ], [ 1024, %15 ]
store i64 %17, i64* %7
store i64 0, i64* %8
br label %18
18: ; preds = %38, %16
%19 = load i64, i64* %8
%20 = load i64, i64* %7
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %41
22: ; preds = %18
%23 = load i64, i64* %8
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %22
%26 = load i64, i64* %8
%27 = urem i64 %26, 16
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %29, %25, %22
%32 = load i8*, i8** %5
%33 = load i64, i64* %8
%34 = getelementptr inbounds i8, i8* %32, i64 %33
%35 = load i8, i8* %34
%36 = zext i8 %35 to i64
%37 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %36)
br label %38
38: ; preds = %31
%39 = load i64, i64* %8
%40 = add i64 %39, 1
store i64 %40, i64* %8
br label %18
41: ; preds = %18
%42 = load i64, i64* %7
%43 = load i64, i64* %6
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i64, i64* %6
%47 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i64 %46)
br label %52
48: ; preds = %41
%49 = load i64, i64* %7
%50 = load i64, i64* %6
%51 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i64 %49, i64 %50)
br label %52
52: ; preds = %48, %45
ret void
}

LLVM IR (-O2):
@.str.1 = [6 x i8] c"%02x \00"
@.str.2 = [20 x i8] c" %zu (octets)\0A\0A\00"
@.str.3 = [24 x i8] c" %zu/%zu (octets)\0A\0A\00"
define void @print_hex(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32 @puts(i8* dereferenceable(1) %0)
%5 = icmp ult i64 %2, 1025
%6 = select i1 %5, i64 %2, i64 1024
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %17, %3
br i1 %5, label %24, label %26
9: ; preds = %3, %17
%10 = phi i64 [ %22, %17 ], [ 0, %3 ]
%11 = icmp ne i64 %10, 0
%12 = and i64 %10, 15
%13 = icmp eq i64 %12, 0
%14 = and i1 %11, %13
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = tail call i32 @putchar(i32 10)
br label %17
17: ; preds = %15, %9
%18 = getelementptr inbounds i8, i8* %1, i64 %10
%19 = load i8, i8* %18
%20 = zext i8 %19 to i64
%21 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %20)
%22 = add nuw i64 %10, 1
%23 = icmp eq i64 %22, %6
br i1 %23, label %8, label %9
24: ; preds = %8
%25 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i64 %2)
br label %28
26: ; preds = %8
%27 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i64 %6, i64 %2)
br label %28
28: ; preds = %26, %24
ret void
}