#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 hex_dump_limit_len(char *str, unsigned char *pSrcBufVA,
                        unsigned int SrcBufLen, unsigned int limit_len) {
  unsigned char *pt;
  int x;
  unsigned int print_len = SrcBufLen;
  pt = pSrcBufVA;
  if (print_len > limit_len) {
    print_len = limit_len;
  }
  printf("%s: %p, len = %d, dump lenght is limited to %u\n\r", str, pSrcBufVA,
         SrcBufLen, limit_len);
  for (x = 0; x < print_len; x++) {
    if (x % 16 == 0) {
      printf("0x%04x : ", x);
    }
    printf("%02x ", ((unsigned char)pt[x]));
    if (x % 16 == 15) {
      printf("\n\r");
    }
  }
  printf("\n\r");
}


x86 assembly (clang, -O0):
<hex_dump_limit_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
jbe    1146 <hex_dump_limit_len+0x36>
mov    -0x18(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%r8d
lea    0xea4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jae    11ea <hex_dump_limit_len+0xda>
mov    -0x24(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    119b <hex_dump_limit_len+0x8b>
mov    -0x24(%rbp),%esi
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xe8d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0xf,%edx
jne    11d7 <hex_dump_limit_len+0xc7>
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11dc <hex_dump_limit_len+0xcc>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    116a <hex_dump_limit_len+0x5a>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_dump_limit_len>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r8d
mov    %edx,%ecx
mov    %rsi,%r13
mov    %rdi,%rsi
cmp    %r8d,%edx
mov    %edx,%ebx
cmova  %r8d,%ebx
lea    0xeca(%rip),%rdi
mov    %r13,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebx,%ebx
je     11b4 <hex_dump_limit_len+0xa4>
mov    %ebx,%ebp
lea    0xee4(%rip),%r14
lea    0xee7(%rip),%r15
xor    %ebx,%ebx
jmp    1185 <hex_dump_limit_len+0x75>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
movzbl 0x0(%r13,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
je     11b4 <hex_dump_limit_len+0xa4>
mov    %ebx,%r12d
and    $0xf,%r12d
je     1160 <hex_dump_limit_len+0x50>
movzbl 0x0(%r13,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
cmp    $0xf,%r12d
jne    117c <hex_dump_limit_len+0x6c>
lea    0xe83(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    117c <hex_dump_limit_len+0x6c>
lea    0xe73(%rip),%rdi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>

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

LLVM IR (-O2):
@.str = [49 x i8] c"%s: %p, len = %d, dump lenght is limited to %u\0A\0D\00"
@.str.1 = [10 x i8] c"0x%04x : \00"
@.str.2 = [6 x i8] c"%02x \00"
@.str.3 = [3 x i8] c"\0A\0D\00"
define void @hex_dump_limit_len(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = icmp ugt i32 %2, %3
%6 = select i1 %5, i32 %3, i32 %2
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i32 %2, i32 %3)
%8 = icmp eq i32 %6, 0
br i1 %8, label %33, label %9
9: ; preds = %4
%10 = zext i32 %6 to i64
br label %11
11: ; preds = %9, %30
%12 = phi i64 [ 0, %9 ], [ %31, %30 ]
%13 = trunc i64 %12 to i32
%14 = and i32 %13, 15
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%18 = getelementptr inbounds i8, i8* %1, i64 %12
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %20)
br label %30
22: ; preds = %11
%23 = getelementptr inbounds i8, i8* %1, i64 %12
%24 = load i8, i8* %23
%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 %14, 15
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %16, %22, %28
%31 = add nuw nsw i64 %12, 1
%32 = icmp eq i64 %31, %10
br i1 %32, label %33, label %11
33: ; preds = %30, %4
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
ret void
}