#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*/ printk(char *, ...);
int hexdump_helper(void *ptr, int len, int format_offset) {
  int i, newlined = 0, format_val;
  char *data = (char *)ptr;
  for (i = 0, format_val = format_offset; i < len; i++, format_val++) {
    printk("%02X ", (unsigned char)data[i]);
    newlined = 0;
    if (format_val > 0) {
      int mod = format_val % (16);
      if (mod == 0) {
        printk("\n");
        newlined = 1;
      } else if (mod == (16) / 2) {
        printk("- ");
      }
    }
  }
  return format_val;
}


x86 assembly (clang, -O0):
<hexdump_helper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11e5 <hexdump_helper+0xd5>
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1030 <printk@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11c9 <hexdump_helper+0xb9>
mov    -0x1c(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %edx,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11a7 <hexdump_helper+0x97>
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1030 <printk@plt>
movl   $0x1,-0x18(%rbp)
jmp    11c4 <hexdump_helper+0xb4>
cmpl   $0x8,-0x2c(%rbp)
jne    11bf <hexdump_helper+0xaf>
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1030 <printk@plt>
jmp    11c4 <hexdump_helper+0xb4>
jmp    11c9 <hexdump_helper+0xb9>
jmp    11ce <hexdump_helper+0xbe>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    113e <hexdump_helper+0x2e>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexdump_helper>:
mov    %edx,%eax
test   %esi,%esi
jle    119a <hexdump_helper+0x8a>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r13
mov    %esi,(%rsp)
mov    %esi,%ebx
mov    %eax,0x4(%rsp)
mov    %eax,%ebp
lea    0xec6(%rip),%r12
xor    %r15d,%r15d
jmp    1150 <hexdump_helper+0x40>
nop
xor    %eax,%eax
call   1030 <printk@plt>
add    $0x1,%r15
cmp    %r15,%rbx
je     1185 <hexdump_helper+0x75>
lea    (%r15,%rbp,1),%r14
movzbl 0x0(%r13,%r15,1),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1030 <printk@plt>
test   %r14d,%r14d
jle    1147 <hexdump_helper+0x37>
lea    0xe96(%rip),%rdi
and    $0xf,%r14d
je     1140 <hexdump_helper+0x30>
cmp    $0x8,%r14d
jne    1147 <hexdump_helper+0x37>
lea    0xe85(%rip),%rdi
jmp    1140 <hexdump_helper+0x30>
mov    0x4(%rsp),%eax
add    (%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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