#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 /*<<< orphan*/ FILE;

/* Variables and functions */
char ascii(unsigned char);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ print_chars(int /*<<< orphan*/ *, char *, int);
void mem_dump(FILE *log, char *title, void *mem, int len) {
  int i, index;
  char s[16 + 1];
  unsigned char *p, b;
  p = mem;
  index = 0;
  s[0] = '\0';
  if (len && log)
    fprintf(log, "memdump of %s:\n", title);
  for (i = 0; i < len; i++) {
    b = p[i];
    if ((index == 0) || (index == 16)) {
      print_chars(log, s, index);
      if (log)
        fprintf(log, "%.8X :", (unsigned int)i);
      index = 0;
    }
    if (log)
      fprintf(log, " %.2X", (unsigned int)b);
    s[index++] = ascii(b);
  }
  print_chars(log, s, index);
  (void)fflush(log);
}


x86 assembly (clang, -O0):
<mem_dump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x24(%rbp)
movb   $0x0,-0x40(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1195 <mem_dump+0x55>
cmpq   $0x0,-0x8(%rbp)
je     1195 <mem_dump+0x55>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe72(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    124c <mem_dump+0x10c>
mov    -0x48(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x49(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11ca <mem_dump+0x8a>
cmpl   $0x10,-0x24(%rbp)
jne    1201 <mem_dump+0xc1>
mov    -0x8(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <print_chars@plt>
cmpq   $0x0,-0x8(%rbp)
je     11fa <mem_dump+0xba>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xe1d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1222 <mem_dump+0xe2>
mov    -0x8(%rbp),%rdi
movzbl -0x49(%rbp),%edx
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movzbl -0x49(%rbp),%edi
call   1040 <ascii@plt>
mov    %al,%cl
mov    -0x24(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x24(%rbp)
cltq
mov    %cl,-0x40(%rbp,%rax,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    119c <mem_dump+0x5c>
mov    -0x8(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <print_chars@plt>
mov    -0x8(%rbp),%rdi
call   1060 <fflush@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_dump>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rdi,%rbx
movb   $0x0,(%rsp)
test   %rdi,%rdi
je     1177 <mem_dump+0x37>
test   %ebp,%ebp
je     1177 <mem_dump+0x37>
mov    %rsi,%rdx
lea    0xe93(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %ebp,%ebp
jle    120b <mem_dump+0xcb>
mov    %ebp,%r13d
xor    %ebp,%ebp
xor    %r12d,%r12d
jmp    11bf <mem_dump+0x7f>
nopl   0x0(%rax)
mov    %rbx,%rdi
lea    0xe7d(%rip),%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r15d,%edi
call   1040 <ascii@plt>
movslq %r12d,%rcx
add    $0x1,%r12d
mov    %al,(%rsp,%rcx,1)
add    $0x1,%rbp
cmp    %rbp,%r13
je     120e <mem_dump+0xce>
movzbl (%r14,%rbp,1),%r15d
test   $0xffffffef,%r12d
je     11e0 <mem_dump+0xa0>
test   %rbx,%rbx
jne    1190 <mem_dump+0x50>
jmp    11a4 <mem_dump+0x64>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %rsp,%rsi
mov    %r12d,%edx
call   1030 <print_chars@plt>
xor    %r12d,%r12d
test   %rbx,%rbx
je     11a4 <mem_dump+0x64>
mov    %rbx,%rdi
lea    0xe10(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1190 <mem_dump+0x50>
xor    %r12d,%r12d
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    %r12d,%edx
call   1030 <print_chars@plt>
mov    %rbx,%rdi
call   1060 <fflush@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"memdump of %s:\0A\00"
@.str.1 = [7 x i8] c"%.8X :\00"
@.str.2 = [6 x i8] c" %.2X\00"
define void @mem_dump(i32* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [17 x i8]
%12 = alloca i8*
%13 = alloca i8
store i32* %0, i32** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%14 = load i8*, i8** %7
store i8* %14, i8** %12
store i32 0, i32* %10
%15 = getelementptr inbounds [17 x i8], [17 x i8]* %11, i64 0, i64 0
store i8 0, i8* %15
%16 = load i32, i32* %8
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %4
%19 = load i32*, i32** %5
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32*, i32** %5
%23 = load i8*, i8** %6
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %21, %18, %4
store i32 0, i32* %9
br label %26
26: ; preds = %68, %25
%27 = load i32, i32* %9
%28 = load i32, i32* %8
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %71
30: ; preds = %26
%31 = load i8*, i8** %12
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
store i8 %35, i8* %13
%36 = load i32, i32* %10
%37 = icmp eq i32 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %30
%39 = load i32, i32* %10
%40 = icmp eq i32 %39, 16
br i1 %40, label %41, label %53
41: ; preds = %38, %30
%42 = load i32*, i32** %5
%43 = getelementptr inbounds [17 x i8], [17 x i8]* %11, i64 0, i64 0
%44 = load i32, i32* %10
%45 = call i32 @print_chars(i32* %42, i8* %43, i32 %44)
%46 = load i32*, i32** %5
%47 = icmp ne i32* %46, null
br i1 %47, label %48, label %52
48: ; preds = %41
%49 = load i32*, i32** %5
%50 = load i32, i32* %9
%51 = call i32 (i32*, i8*, ...) @fprintf(i32* %49, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %50)
br label %52
52: ; preds = %48, %41
store i32 0, i32* %10
br label %53
53: ; preds = %52, %38
%54 = load i32*, i32** %5
%55 = icmp ne i32* %54, null
br i1 %55, label %56, label %61
56: ; preds = %53
%57 = load i32*, i32** %5
%58 = load i8, i8* %13
%59 = zext i8 %58 to i32
%60 = call i32 (i32*, i8*, ...) @fprintf(i32* %57, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %59)
br label %61
61: ; preds = %56, %53
%62 = load i8, i8* %13
%63 = call signext i8 @ascii(i8 zeroext %62)
%64 = load i32, i32* %10
%65 = add nsw i32 %64, 1
store i32 %65, i32* %10
%66 = sext i32 %64 to i64
%67 = getelementptr inbounds [17 x i8], [17 x i8]* %11, i64 0, i64 %66
store i8 %63, i8* %67
br label %68
68: ; preds = %61
%69 = load i32, i32* %9
%70 = add nsw i32 %69, 1
store i32 %70, i32* %9
br label %26
71: ; preds = %26
%72 = load i32*, i32** %5
%73 = getelementptr inbounds [17 x i8], [17 x i8]* %11, i64 0, i64 0
%74 = load i32, i32* %10
%75 = call i32 @print_chars(i32* %72, i8* %73, i32 %74)
%76 = load i32*, i32** %5
%77 = call i32 @fflush(i32* %76)
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"memdump of %s:\0A\00"
@.str.1 = [7 x i8] c"%.8X :\00"
@.str.2 = [6 x i8] c" %.2X\00"
define void @mem_dump(i32* %0, i8* %1, i8* nocapture %2, i32 %3) {
%5 = alloca [17 x i8]
%6 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 0
store i8 0, i8* %6
%7 = icmp ne i32 %3, 0
%8 = icmp ne i32* %0, null
%9 = and i1 %8, %7
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %12
12: ; preds = %10, %4
%13 = icmp sgt i32 %3, 0
br i1 %13, label %14, label %41
14: ; preds = %12
%15 = zext i32 %3 to i64
br label %16
16: ; preds = %14, %33
%17 = phi i64 [ 0, %14 ], [ %39, %33 ]
%18 = phi i32 [ 0, %14 ], [ %36, %33 ]
%19 = getelementptr inbounds i8, i8* %2, i64 %17
%20 = load i8, i8* %19
%21 = and i32 %18, -17
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %16
%24 = call i32 @print_chars(i32* %0, i8* %6, i32 %18)
br i1 %8, label %25, label %33
25: ; preds = %23
%26 = trunc i64 %17 to i32
%27 = call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %26)
br label %29
28: ; preds = %16
br i1 %8, label %29, label %33
29: ; preds = %25, %28
%30 = phi i32 [ 0, %25 ], [ %18, %28 ]
%31 = zext i8 %20 to i32
%32 = call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %23, %29, %28
%34 = phi i32 [ %30, %29 ], [ %18, %28 ], [ 0, %23 ]
%35 = call signext i8 @ascii(i8 zeroext %20)
%36 = add nsw i32 %34, 1
%37 = sext i32 %34 to i64
%38 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 %37
store i8 %35, i8* %38
%39 = add nuw nsw i64 %17, 1
%40 = icmp eq i64 %39, %15
br i1 %40, label %41, label %16
41: ; preds = %33, %12
%42 = phi i32 [ 0, %12 ], [ %36, %33 ]
%43 = call i32 @print_chars(i32* %0, i8* %6, i32 %42)
%44 = call i32 @fflush(i32* %0)
ret void
}