#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*/ printf(char *, ...);
void dump_hex(char *buf, int nb) {
  int i = 0, n = 0;
  for (; nb > 0; nb--) {
    printf("%02hx ", (unsigned char)buf[i]);
    i++;
    n++;
    if (n == 16) {
      printf("\n");
      n = 0;
    }
  }
  if (n != 0) {
    printf("\n");
  }
}


x86 assembly (clang, -O0):
<dump_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    1195 <dump_hex+0x85>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xeb6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x10,-0x14(%rbp)
jne    1182 <dump_hex+0x72>
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x14(%rbp)
jmp    1187 <dump_hex+0x77>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    112d <dump_hex+0x1d>
cmpl   $0x0,-0x14(%rbp)
je     11ad <dump_hex+0x9d>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_hex>:
test   %esi,%esi
jle    1184 <dump_hex+0x64>
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
mov    %esi,%r12d
lea    0xec7(%rip),%r14
xor    %ebx,%ebx
xor    %ebp,%ebp
jmp    1145 <dump_hex+0x25>
nop
cmp    %rbx,%r12
je     116e <dump_hex+0x4e>
movzbl (%r15,%rbx,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
add    $0x1,%ebp
cmp    $0x10,%ebp
jne    1140 <dump_hex+0x20>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %ebp,%ebp
jmp    1140 <dump_hex+0x20>
test   %ebp,%ebp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
je     1184 <dump_hex+0x64>
mov    $0xa,%edi
jmp    1030 <putchar@plt>
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%02hx \00"
@.str.1 = [2 x i8] c"\0A\00"
define void @dump_hex(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
br label %7
7: ; preds = %27, %2
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %30
10: ; preds = %7
%11 = load i8*, i8** %3
%12 = load i32, i32* %5
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %5
%19 = add nsw i32 %18, 1
store i32 %19, i32* %5
%20 = load i32, i32* %6
%21 = add nsw i32 %20, 1
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, 16
br i1 %23, label %24, label %26
24: ; preds = %10
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %6
br label %26
26: ; preds = %24, %10
br label %27
27: ; preds = %26
%28 = load i32, i32* %4
%29 = add nsw i32 %28, -1
store i32 %29, i32* %4
br label %7
30: ; preds = %7
%31 = load i32, i32* %6
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %30
ret void
}

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