void hexdmp(char *ptr, int length) {
  char hex[16];
  char ch;
  int count;
  printf("base address: %x     length: %d\n", ptr, length);
  clear(hex);
  count = 0;
  while (length > 0) {
    ch = *ptr++;
    hex[count] = ch;
    length--;
    count++;
    if (count == 16) {
      hprint(hex);
      vprint(hex);
      count = 0;
      clear(hex);
    }
  };
  if (count) {
    hprint(hex);
    vprint(hex);
  }
}


x86 assembly (clang, -O0):
<hexdmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1060 <clear@plt>
movl   $0x0,-0x28(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11e8 <hexdmp+0xa8>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%cl
movslq -0x28(%rbp),%rax
mov    %cl,-0x20(%rbp,%rax,1)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x10,-0x28(%rbp)
jne    11e3 <hexdmp+0xa3>
lea    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1040 <hprint@plt>
lea    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1050 <vprint@plt>
movl   $0x0,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1060 <clear@plt>
jmp    1176 <hexdmp+0x36>
cmpl   $0x0,-0x28(%rbp)
je     1208 <hexdmp+0xc8>
lea    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1040 <hprint@plt>
lea    -0x20(%rbp),%rdi
mov    $0x0,%al
call   1050 <vprint@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexdmp>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xea9(%rip),%rdi
xor    %r12d,%r12d
mov    %r14,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rsp,%rdi
xor    %eax,%eax
call   1060 <clear@plt>
test   %ebx,%ebx
jle    11e0 <hexdmp+0xa0>
add    $0x1,%ebx
mov    %rsp,%r15
jmp    118c <hexdmp+0x4c>
nopl   0x0(%rax)
add    $0x1,%r14
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
jbe    11c4 <hexdmp+0x84>
movzbl (%r14),%eax
movslq %r12d,%r12
mov    %al,(%rsp,%r12,1)
add    $0x1,%r12d
cmp    $0x10,%r12d
jne    1180 <hexdmp+0x40>
xor    %r12d,%r12d
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <hprint@plt>
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <vprint@plt>
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <clear@plt>
jmp    1180 <hexdmp+0x40>
test   %r12d,%r12d
je     11e0 <hexdmp+0xa0>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <hprint@plt>
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <vprint@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [33 x i8] c"base address: %x length: %d\0A\00"
define void @hexdmp(i8* %0, i32 %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%6 = call i32 (i8*, ...) bitcast (i32 (...)* @clear to i32 (i8*, ...)*)(i8* %4)
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %31
8: ; preds = %2, %23
%9 = phi i32 [ %24, %23 ], [ 0, %2 ]
%10 = phi i8* [ %12, %23 ], [ %0, %2 ]
%11 = phi i32 [ %16, %23 ], [ %1, %2 ]
%12 = getelementptr inbounds i8, i8* %10, i64 1
%13 = load i8, i8* %10
%14 = sext i32 %9 to i64
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 %14
store i8 %13, i8* %15
%16 = add nsw i32 %11, -1
%17 = add nsw i32 %9, 1
%18 = icmp eq i32 %17, 16
br i1 %18, label %19, label %23
19: ; preds = %8
%20 = call i32 (i8*, ...) bitcast (i32 (...)* @hprint to i32 (i8*, ...)*)(i8* %4)
%21 = call i32 (i8*, ...) bitcast (i32 (...)* @vprint to i32 (i8*, ...)*)(i8* %4)
%22 = call i32 (i8*, ...) bitcast (i32 (...)* @clear to i32 (i8*, ...)*)(i8* %4)
br label %23
23: ; preds = %19, %8
%24 = phi i32 [ 0, %19 ], [ %17, %8 ]
%25 = icmp ugt i32 %11, 1
br i1 %25, label %8, label %26
26: ; preds = %23
%27 = icmp eq i32 %24, 0
br i1 %27, label %31, label %28
28: ; preds = %26
%29 = call i32 (i8*, ...) bitcast (i32 (...)* @hprint to i32 (i8*, ...)*)(i8* %4)
%30 = call i32 (i8*, ...) bitcast (i32 (...)* @vprint to i32 (i8*, ...)*)(i8* %4)
br label %31
31: ; preds = %2, %28, %26
ret void
}