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

/* Variables and functions */
unsigned int snhexdump(char *, int, unsigned char const *, unsigned int);
int /*<<< orphan*/ snprintf(char *, int, char *);
void hexdump_buffer(const unsigned char *buffer, unsigned int buffer_size,
                    void (*printrow)(const char *text),
                    unsigned int bytes_per_row) {
  unsigned int left = buffer_size;
  static char buffer_txt[1024];
  while (left > 0) {
    unsigned int row = left < bytes_per_row ? left : bytes_per_row;
    unsigned int rowlen =
        snhexdump(buffer_txt, sizeof(buffer_txt) - 1, buffer, row);
    snprintf(buffer_txt + rowlen, sizeof(buffer_txt) - rowlen - 1, "\n");
    printrow(buffer_txt);
    buffer += row;
    left -= row;
  }
}


x86 assembly (clang, -O0):
<hexdump_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jbe    11da <hexdump_buffer+0xba>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    115d <hexdump_buffer+0x3d>
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    1163 <hexdump_buffer+0x43>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdx
mov    -0x24(%rbp),%ecx
lea    0x2ec9(%rip),%rdi
mov    $0x3ff,%esi
call   1040 <snhexdump@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
lea    0x2eb2(%rip),%rdi
add    %rax,%rdi
mov    -0x28(%rbp),%eax
mov    %eax,%ecx
mov    $0x400,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%esi
lea    0xe55(%rip),%rdx
call   1030 <snprintf@plt>
lea    0x2e89(%rip),%rdi
call   *-0x18(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
jmp    113c <hexdump_buffer+0x1c>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexdump_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
je     1193 <hexdump_buffer+0x73>
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x2eff(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    %r15d,%ebp
mov    %r15d,%r13d
cmovb  %ebp,%r13d
mov    %r12,%rdi
mov    $0x3ff,%esi
mov    %rbx,%rdx
mov    %r13d,%ecx
call   1040 <snhexdump@plt>
mov    %eax,%edi
add    %r12,%rdi
mov    $0x3ff,%esi
sub    %eax,%esi
lea    0xe80(%rip),%rdx
call   1030 <snprintf@plt>
mov    %r12,%rdi
call   *%r14
add    %r13,%rbx
sub    %r13d,%ebp
jne    1150 <hexdump_buffer+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@hexdump_buffer.buffer_txt = internal global [1024 x i8] zeroinitializer
@.str = [2 x i8] c"\0A\00"
define void @hexdump_buffer(i8* %0, i32 %1, void (i8*)* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca void (i8*)*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store void (i8*)* %2, void (i8*)** %7
store i32 %3, i32* %8
%12 = load i32, i32* %6
store i32 %12, i32* %9
br label %13
13: ; preds = %24, %4
%14 = load i32, i32* %9
%15 = icmp ugt i32 %14, 0
br i1 %15, label %16, label %46
16: ; preds = %13
%17 = load i32, i32* %9
%18 = load i32, i32* %8
%19 = icmp ult i32 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = load i32, i32* %9
br label %24
22: ; preds = %16
%23 = load i32, i32* %8
br label %24
24: ; preds = %22, %20
%25 = phi i32 [ %21, %20 ], [ %23, %22 ]
store i32 %25, i32* %10
%26 = load i8*, i8** %5
%27 = load i32, i32* %10
%28 = call i32 @snhexdump(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @hexdump_buffer.buffer_txt, i64 0, i64 0), i32 1023, i8* %26, i32 %27)
store i32 %28, i32* %11
%29 = load i32, i32* %11
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @hexdump_buffer.buffer_txt, i64 0, i64 0), i64 %30
%32 = load i32, i32* %11
%33 = zext i32 %32 to i64
%34 = sub i64 1024, %33
%35 = sub i64 %34, 1
%36 = trunc i64 %35 to i32
%37 = call i32 @snprintf(i8* %31, i32 %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%38 = load void (i8*)*, void (i8*)** %7
%39 = load i32, i32* %10
%40 = load i8*, i8** %5
%41 = zext i32 %39 to i64
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8* %42, i8** %5
%43 = load i32, i32* %10
%44 = load i32, i32* %9
%45 = sub i32 %44, %43
store i32 %45, i32* %9
br label %13
46: ; preds = %13
ret void
}

LLVM IR (-O2):
@hexdump_buffer.buffer_txt = internal global [1024 x i8] zeroinitializer
@.str = [2 x i8] c"\0A\00"
define void @hexdump_buffer(i8* %0, i32 %1, void (i8*)* nocapture %2, i32 %3) {
%5 = icmp eq i32 %1, 0
br i1 %5, label %20, label %6
6: ; preds = %4, %6
%7 = phi i8* [ %17, %6 ], [ %0, %4 ]
%8 = phi i32 [ %18, %6 ], [ %1, %4 ]
%9 = icmp ult i32 %8, %3
%10 = select i1 %9, i32 %8, i32 %3
%11 = tail call i32 @snhexdump(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @hexdump_buffer.buffer_txt, i64 0, i64 0), i32 1023, i8* %7, i32 %10)
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* @hexdump_buffer.buffer_txt, i64 0, i64 %12
%14 = sub i32 1023, %11
%15 = tail call i32 @snprintf(i8* %13, i32 %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
tail call void %2(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @hexdump_buffer.buffer_txt, i64 0, i64 0))
%16 = zext i32 %10 to i64
%17 = getelementptr inbounds i8, i8* %7, i64 %16
%18 = sub i32 %8, %10
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %6
20: ; preds = %6, %4
ret void
}