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


x86 assembly (clang, -O0):
<print_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119f <print_hex+0x7f>
cmpl   $0x0,-0x18(%rbp)
je     1179 <print_hex+0x59>
mov    -0x18(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1179 <print_hex+0x59>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xe74(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1143 <print_hex+0x23>
mov    -0x14(%rbp),%esi
lea    0xe5d(%rip),%rdi
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_hex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r12
call   1040 <puts@plt>
test   %r14d,%r14d
jle    1191 <print_hex+0x61>
mov    %r14d,%ebp
xor    %ebx,%ebx
lea    0xeac(%rip),%r15
jmp    1176 <print_hex+0x46>
cs nopw 0x0(%rax,%rax,1)
movzbl (%r12,%rbx,1),%esi
mov    %r15,%rdi
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
je     1191 <print_hex+0x61>
test   %rbx,%rbx
je     1160 <print_hex+0x30>
mov    %ebx,%eax
and    $0xf,%eax
test   %rax,%rax
jne    1160 <print_hex+0x30>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1160 <print_hex+0x30>
lea    0xe6e(%rip),%rdi
mov    %r14d,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [6 x i8] c"%02x \00"
@.str.2 = [19 x i8] c" %d (octets)\0A\0A\00"
define void @print_hex(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = call i32 @puts(i8* %8)
store i32 0, i32* %7
br label %10
10: ; preds = %31, %3
%11 = load i32, i32* %7
%12 = load i32, i32* %6
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %34
14: ; preds = %10
%15 = load i32, i32* %7
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = load i32, i32* %7
%19 = srem i32 %18, 16
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %17, %14
%24 = load i8*, i8** %5
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %23
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %10
34: ; preds = %10
%35 = load i32, i32* %6
%36 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %35)
ret void
}

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