void dump_mem(void *buf, unsigned int start, unsigned int size) {
  unsigned char *ptr;
  int i;
  if (!buf) {
    printf("%s, buf is null\n", __FUNCTION__);
    return;
  }
  ptr = (unsigned char *)buf + start;
  for (i = 0; i < size; i++) {
    if (i % 16 == 0)
      printf("%d: ", start + i);
    printf("%.2x ", *ptr++);
    if (i % 16 == 15)
      printf("\n");
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<dump_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1147 <dump_mem+0x37>
lea    0xecc(%rip),%rdi
lea    0xed6(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f3 <dump_mem+0xe3>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jae    11e5 <dump_mem+0xd5>
mov    -0x1c(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1190 <dump_mem+0x80>
mov    -0xc(%rbp),%esi
add    -0x1c(%rbp),%esi
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movzbl (%rax),%esi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0xf,%edx
jne    11d2 <dump_mem+0xc2>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11d7 <dump_mem+0xc7>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    115c <dump_mem+0x4c>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_mem>:
test   %rdi,%rdi
je     11ba <dump_mem+0x9a>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
je     11a2 <dump_mem+0x82>
mov    %rdi,%rbx
mov    %esi,%r12d
mov    %edx,%r13d
add    %r12,%rbx
lea    0xed4(%rip),%r15
xor    %ebp,%ebp
jmp    1179 <dump_mem+0x59>
nop
lea    (%r12,%rbp,1),%esi
lea    0xebf(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movzbl (%rbx,%rbp,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbp
cmp    %ebp,%r13d
je     11a2 <dump_mem+0x82>
mov    %ebp,%r14d
and    $0xf,%r14d
je     1150 <dump_mem+0x30>
movzbl (%rbx,%rbp,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0xf,%r14d
jne    1170 <dump_mem+0x50>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1170 <dump_mem+0x50>
mov    $0xa,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putchar@plt>
lea    0xe3f(%rip),%rdi
lea    0xe49(%rip),%rsi
xor    %eax,%eax
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"%s, buf is null\0A\00"
@__FUNCTION__.dump_mem = [9 x i8] c"dump_mem\00"
@.str.1 = [5 x i8] c"%d: \00"
@.str.2 = [6 x i8] c"%.2x \00"
@.str.3 = [2 x i8] c"\0A\00"
define void @dump_mem(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @__FUNCTION__.dump_mem, i64 0, i64 0))
br label %48
13: ; preds = %3
%14 = load i8*, i8** %4
%15 = load i32, i32* %5
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
store i8* %17, i8** %7
store i32 0, i32* %8
br label %18
18: ; preds = %43, %13
%19 = load i32, i32* %8
%20 = load i32, i32* %6
%21 = icmp ult i32 %19, %20
br i1 %21, label %22, label %46
22: ; preds = %18
%23 = load i32, i32* %8
%24 = srem i32 %23, 16
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %22
%27 = load i32, i32* %5
%28 = load i32, i32* %8
%29 = add i32 %27, %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %26, %22
%32 = load i8*, i8** %7
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %7
%34 = load i8, i8* %32
%35 = zext i8 %34 to i32
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %8
%38 = srem i32 %37, 16
%39 = icmp eq i32 %38, 15
br i1 %39, label %40, label %42
40: ; preds = %31
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %31
br label %43
43: ; preds = %42
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 1
store i32 %45, i32* %8
br label %18
46: ; preds = %18
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %46, %11
ret void
}

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