#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 uint8_t;
typedef int uint32_t;
typedef int uint16_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
void print_hex(void *p, int size, int unit) {
  uint8_t *p8 = p;
  uint16_t *p16 = p;
  uint32_t *p32 = p;
  for (int i = 0; i < size; i += unit, p8++, p16++, p32++) {
    if (i != 0 && (i % 16) == 0)
      printf("\n");
    if (unit == 1)
      printf(" %02x", *p8);
    else if (unit == 2)
      printf(" %04x", *p16);
    else
      printf(" %08x", *p32);
  }
}


x86 assembly (clang, -O0):
<print_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    120f <print_hex+0xff>
cmpl   $0x0,-0x2c(%rbp)
je     1179 <print_hex+0x69>
mov    -0x2c(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1179 <print_hex+0x69>
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x1,-0x10(%rbp)
jne    119c <print_hex+0x8c>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11d8 <print_hex+0xc8>
cmpl   $0x2,-0x10(%rbp)
jne    11bf <print_hex+0xaf>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11d3 <print_hex+0xc3>
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11d8 <print_hex+0xc8>
jmp    11dd <print_hex+0xcd>
mov    -0x10(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
jmp    1141 <print_hex+0x31>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_hex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %esi,%esi
jle    1190 <print_hex+0x70>
mov    %edx,%r15d
mov    %esi,%r14d
mov    %rdi,%rbp
cmp    $0x2,%edx
lea    0xec7(%rip),%rax
lea    0xec6(%rip),%rcx
cmove  %rax,%rcx
cmp    $0x1,%edx
lea    0xeac(%rip),%r12
cmovne %rcx,%r12
xor    %ebx,%ebx
jmp    1179 <print_hex+0x59>
nopl   0x0(%rax)
mov    0x0(%rbp),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    %r15d,%ebx
add    $0x4,%rbp
cmp    %r14d,%ebx
jge    1190 <print_hex+0x70>
test   %ebx,%ebx
je     1160 <print_hex+0x40>
mov    %ebx,%eax
and    $0xf,%eax
jne    1160 <print_hex+0x40>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1160 <print_hex+0x40>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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