#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 *, ...);
void print_hex(char *str, unsigned char *buf, int len) {
  int i;
  for (i = 0; i < len; i++) {
    if ((i % 16) == 0 && i != 0)
      printf(str);
    printf("%02x", buf[i]);
    if ((i % 4) == 3)
      printf(" ");
    if ((i % 16) == 15)
      printf("\n");
  }
  if ((i % 16) != 0)
    printf("\n");
}


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)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11d0 <print_hex+0xc0>
mov    -0x18(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    115f <print_hex+0x4f>
cmpl   $0x0,-0x18(%rbp)
je     115f <print_hex+0x4f>
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x3,%edx
jne    119b <print_hex+0x8b>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0xf,%edx
jne    11bd <print_hex+0xad>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11c2 <print_hex+0xb2>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    112a <print_hex+0x1a>
mov    -0x18(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11f2 <print_hex+0xe2>
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_hex>:
test   %edx,%edx
jle    11c8 <print_hex+0xa8>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %rdi,%r15
mov    %edx,0x4(%rsp)
mov    %edx,%ebp
lea    0xeba(%rip),%r12
xor    %ebx,%ebx
jmp    1159 <print_hex+0x39>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
je     11a8 <print_hex+0x88>
mov    %ebx,%r14d
and    $0xf,%r14d
test   %rbx,%rbx
je     1174 <print_hex+0x54>
test   %r14d,%r14d
jne    1174 <print_hex+0x54>
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movzbl 0x0(%r13,%rbx,1),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
not    %eax
test   $0x3,%al
jne    1196 <print_hex+0x76>
mov    $0x20,%edi
call   1030 <putchar@plt>
cmp    $0xf,%r14d
jne    1150 <print_hex+0x30>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1150 <print_hex+0x30>
testb  $0xf,0x4(%rsp)
lea    0x8(%rsp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
je     11c8 <print_hex+0xa8>
mov    $0xa,%edi
jmp    1030 <putchar@plt>
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%02x\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [2 x i8] c"\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
store i32 0, i32* %7
br label %8
8: ; preds = %42, %3
%9 = load i32, i32* %7
%10 = load i32, i32* %6
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %45
12: ; preds = %8
%13 = load i32, i32* %7
%14 = srem i32 %13, 16
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i32, i32* %7
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i8*, i8** %4
%21 = call i32 (i8*, ...) @printf(i8* %20)
br label %22
22: ; preds = %19, %16, %12
%23 = load i8*, i8** %5
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %7
%31 = srem i32 %30, 4
%32 = icmp eq i32 %31, 3
br i1 %32, label %33, label %35
33: ; preds = %22
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %22
%36 = load i32, i32* %7
%37 = srem i32 %36, 16
%38 = icmp eq i32 %37, 15
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
br label %42
42: ; preds = %41
%43 = load i32, i32* %7
%44 = add nsw i32 %43, 1
store i32 %44, i32* %7
br label %8
45: ; preds = %8
%46 = load i32, i32* %7
%47 = srem i32 %46, 16
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %49, %45
ret void
}

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