#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*/ fprintf(int /*<<< orphan*/, char *, unsigned char *);
void *mem_heap_hi();
void *mem_heap_lo();
scalar_t__ mem_read(void *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
void hprobe(void *ptr, int offset, size_t count) {
  unsigned char *cptr = (unsigned char *)ptr;
  unsigned char *cptr_lo = cptr + offset;
  unsigned char *cptr_hi = cptr_lo + count - 1;
  unsigned char *iptr;
  if ((void *)cptr_lo < mem_heap_lo()) {
    fprintf(stderr, "Invalid probe.  Address %p is below start of heap\n",
            cptr_lo);
    return;
  }
  if ((void *)cptr_hi > mem_heap_hi()) {
    fprintf(stderr, "Invalid probe.  Address %p is beyond end of heap\n",
            cptr_lo);
    return;
  }
  printf("Bytes %p...%p: 0x", cptr_hi, cptr_lo);
  for (iptr = cptr_hi; iptr >= cptr_lo; iptr--)
    printf("%.2x", (unsigned)mem_read((void *)iptr, 1));
  printf("\n");
}


x86 assembly (clang, -O0):
<hprobe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1050 <mem_heap_lo@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jae    11c7 <hprobe+0x77>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xe43(%rip),%rsi
call   1040 <fprintf@plt>
jmp    126d <hprobe+0x11d>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1070 <mem_heap_hi@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
cmp    %rcx,%rax
jbe    1204 <hprobe+0xb4>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xe39(%rip),%rsi
call   1040 <fprintf@plt>
jmp    126d <hprobe+0x11d>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jb     125f <hprobe+0x10f>
mov    -0x38(%rbp),%rdi
mov    $0x1,%esi
call   1060 <mem_read@plt>
mov    %eax,%esi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x38(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
jmp    1222 <hprobe+0xd2>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hprobe>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
movslq %esi,%r14
add    %rdi,%r14
xor    %eax,%eax
call   1060 <mem_heap_lo@plt>
cmp    %rax,%r14
jae    118c <hprobe+0x2c>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
jmp    11af <hprobe+0x4f>
add    %r14,%rbx
add    $0xffffffffffffffff,%rbx
xor    %eax,%eax
call   1080 <mem_heap_hi@plt>
cmp    %rax,%rbx
jbe    11bc <hprobe+0x5c>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe84(%rip),%rsi
mov    %r14,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <fprintf@plt>
lea    0xea2(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
cmp    %r14,%rbx
jb     1202 <hprobe+0xa2>
lea    0xe9b(%rip),%r15
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
call   1070 <mem_read@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xffffffffffffffff,%rbx
cmp    %r14,%rbx
jae    11e0 <hprobe+0x80>
mov    $0xa,%edi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <putchar@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [51 x i8] c"Invalid probe. Address %p is below start of heap\0A\00"
@.str.1 = [50 x i8] c"Invalid probe. Address %p is beyond end of heap\0A\00"
@.str.2 = [18 x i8] c"Bytes %p...%p: 0x\00"
@.str.3 = [5 x i8] c"%.2x\00"
@.str.4 = [2 x i8] c"\0A\00"
define void @hprobe(i8* %0, i32 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 %2, i64* %6
%11 = load i8*, i8** %4
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = load i32, i32* %5
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
store i8* %15, i8** %8
%16 = load i8*, i8** %8
%17 = load i64, i64* %6
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = getelementptr inbounds i8, i8* %18, i64 -1
store i8* %19, i8** %9
%20 = load i8*, i8** %8
%21 = call i8* (...) @mem_heap_lo()
%22 = icmp ult i8* %20, %21
br i1 %22, label %23, label %27
23: ; preds = %3
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %8
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* %25)
br label %54
27: ; preds = %3
%28 = load i8*, i8** %9
%29 = call i8* (...) @mem_heap_hi()
%30 = icmp ugt i8* %28, %29
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %8
%34 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i8* %33)
br label %54
35: ; preds = %27
%36 = load i8*, i8** %9
%37 = load i8*, i8** %8
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %36, i8* %37)
%39 = load i8*, i8** %9
store i8* %39, i8** %10
br label %40
40: ; preds = %49, %35
%41 = load i8*, i8** %10
%42 = load i8*, i8** %8
%43 = icmp uge i8* %41, %42
br i1 %43, label %44, label %52
44: ; preds = %40
%45 = load i8*, i8** %10
%46 = call i64 @mem_read(i8* %45, i32 1)
%47 = trunc i64 %46 to i32
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %44
%50 = load i8*, i8** %10
%51 = getelementptr inbounds i8, i8* %50, i32 -1
store i8* %51, i8** %10
br label %40
52: ; preds = %40
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %54
54: ; preds = %52, %31, %23
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [51 x i8] c"Invalid probe. Address %p is below start of heap\0A\00"
@.str.1 = [50 x i8] c"Invalid probe. Address %p is beyond end of heap\0A\00"
@.str.2 = [18 x i8] c"Bytes %p...%p: 0x\00"
@.str.3 = [5 x i8] c"%.2x\00"
define void @hprobe(i8* %0, i32 %1, i64 %2) {
%4 = sext i32 %1 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = getelementptr inbounds i8, i8* %5, i64 %2
%7 = getelementptr inbounds i8, i8* %6, i64 -1
%8 = tail call i8* (...) @mem_heap_lo()
%9 = icmp ult i8* %5, %8
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %31
13: ; preds = %3
%14 = tail call i8* (...) @mem_heap_hi()
%15 = icmp ugt i8* %7, %14
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %31
19: ; preds = %13
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %7, i8* %5)
%21 = icmp ult i8* %7, %5
br i1 %21, label %29, label %22
22: ; preds = %19, %22
%23 = phi i8* [ %27, %22 ], [ %7, %19 ]
%24 = tail call i64 @mem_read(i8* %23, i32 1)
%25 = trunc i64 %24 to i32
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = getelementptr inbounds i8, i8* %23, i64 -1
%28 = icmp ult i8* %27, %5
br i1 %28, label %29, label %22
29: ; preds = %22, %19
%30 = tail call i32 @putchar(i32 10)
br label %31
31: ; preds = %29, %16, %10
ret void
}