void dump_buffer_hex(int indent, const unsigned char *data, int size) {
  int address = 0;
  int i;
  while (size > 0) {
    int howmany = (size < 16) ? size : 16;
    printf("%*s0x%04X: ", indent, "", address);
    for (i = 0; i < howmany; i++)
      printf("%02X ", data[i]);
    printf("\n");
    SERIAL_flush();
    printf("%*s        ", indent, "");
    for (i = 0; i < howmany; i++)
      printf(" %c ", isprint(data[i]) ? data[i] : '.');
    printf("\n");
    SERIAL_flush();
    size -= howmany;
    data += howmany;
    address += howmany;
  }
}


x86 assembly (clang, -O0):
<dump_buffer_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    12aa <dump_buffer_hex+0x17a>
cmpl   $0x10,-0x14(%rbp)
jge    1168 <dump_buffer_hex+0x38>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1175 <dump_buffer_hex+0x45>
mov    $0x10,%eax
mov    %eax,-0x24(%rbp)
jmp    1175 <dump_buffer_hex+0x45>
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x18(%rbp),%ecx
lea    0xe78(%rip),%rdi
lea    0xe84(%rip),%rdx
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11d1 <dump_buffer_hex+0xa1>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119d <dump_buffer_hex+0x6d>
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <SERIAL_flush@plt>
mov    -0x4(%rbp),%esi
lea    0xe24(%rip),%rdi
lea    0xe1c(%rip),%rdx
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    126b <dump_buffer_hex+0x13b>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1040 <isprint@plt>
cmp    $0x0,%eax
je     123f <dump_buffer_hex+0x10f>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x28(%rbp)
jmp    124c <dump_buffer_hex+0x11c>
mov    $0x2e,%eax
mov    %eax,-0x28(%rbp)
jmp    124c <dump_buffer_hex+0x11c>
mov    -0x28(%rbp),%esi
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1205 <dump_buffer_hex+0xd5>
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <SERIAL_flush@plt>
mov    -0x20(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1149 <dump_buffer_hex+0x19>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_buffer_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edx,%edx
jle    1255 <dump_buffer_hex+0x115>
mov    %rsi,%rbx
mov    %edi,%eax
mov    $0x2e,%r12d
lea    0xeb6(%rip),%r15
xor    %ecx,%ecx
mov    %edi,0xc(%rsp)
xchg   %ax,%ax
cmp    $0x10,%edx
mov    $0x10,%r13d
mov    %edx,0x14(%rsp)
cmovb  %edx,%r13d
lea    0xe78(%rip),%rdi
mov    %eax,%esi
lea    0xe8c(%rip),%rdx
mov    %ecx,0x10(%rsp)
xor    %eax,%eax
call   1040 <printf@plt>
xor    %ebp,%ebp
lea    0xe67(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
movzbl (%rbx,%rbp,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    11b0 <dump_buffer_hex+0x70>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
call   1060 <SERIAL_flush@plt>
lea    0xe33(%rip),%rdi
mov    0xc(%rsp),%esi
lea    0xe33(%rip),%rdx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl (%rbx,%rbp,1),%r14d
mov    %r14d,%edi
call   1050 <isprint@plt>
test   %eax,%eax
cmove  %r12d,%r14d
mov    %r15,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1200 <dump_buffer_hex+0xc0>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
call   1060 <SERIAL_flush@plt>
add    %r13,%rbx
mov    0x10(%rsp),%ecx
add    %r13d,%ecx
mov    0x14(%rsp),%edx
sub    %r13d,%edx
mov    0xc(%rsp),%eax
jg     1170 <dump_buffer_hex+0x30>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [12 x i8] c"%*s0x%04X: \00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [6 x i8] c"%02X \00"
@.str.4 = [12 x i8] c"%*s \00"
@.str.5 = [5 x i8] c" %c \00"
define void @dump_buffer_hex(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %44
5: ; preds = %3, %36
%6 = phi i8* [ %41, %36 ], [ %1, %3 ]
%7 = phi i32 [ %42, %36 ], [ 0, %3 ]
%8 = phi i32 [ %39, %36 ], [ %2, %3 ]
%9 = icmp ult i32 %8, 16
%10 = select i1 %9, i32 %8, i32 16
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%12 = zext i32 %10 to i64
br label %13
13: ; preds = %5, %13
%14 = phi i64 [ 0, %5 ], [ %19, %13 ]
%15 = getelementptr inbounds i8, i8* %6, i64 %14
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = add nuw nsw i64 %14, 1
%20 = icmp eq i64 %19, %12
br i1 %20, label %21, label %13
21: ; preds = %13
%22 = tail call i32 @putchar(i32 10)
%23 = tail call i32 (...) @SERIAL_flush()
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %21, %25
%26 = phi i64 [ 0, %21 ], [ %34, %25 ]
%27 = getelementptr inbounds i8, i8* %6, i64 %26
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = tail call i32 @isprint(i32 %29)
%31 = icmp eq i32 %30, 0
%32 = select i1 %31, i32 46, i32 %29
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %32)
%34 = add nuw nsw i64 %26, 1
%35 = icmp eq i64 %34, %12
br i1 %35, label %36, label %25
36: ; preds = %25
%37 = tail call i32 @putchar(i32 10)
%38 = tail call i32 (...) @SERIAL_flush()
%39 = sub nsw i32 %8, %10
%40 = zext i32 %10 to i64
%41 = getelementptr inbounds i8, i8* %6, i64 %40
%42 = add nuw nsw i32 %10, %7
%43 = icmp sgt i32 %39, 0
br i1 %43, label %5, label %44
44: ; preds = %36, %3
ret void
}