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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ PTRACE_PEEKDATA;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
scalar_t__ errno;
long ptrace(int /*<<< orphan*/, int /*<<< orphan*/, void *, int /*<<< orphan*/);
void rescan_heap(pid_t pid, unsigned value, unsigned **map, size_t *length) {
  void *addr;
  long ret;
  unsigned offset;
  size_t new_length = 0, len = *length;
  for (size_t src = 0, dest = 0; src < len; ++src) {
    addr = (void *)((size_t)map[src] & ~(sizeof(long) - 1));
    offset = (size_t)map[src] & (sizeof(long) - 1);
    errno = 0;
    ret = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
    if (errno) {
      err(EXIT_FAILURE, "ptrace (PEEKDATA)");
    } else {
      if (((ret >> offset) & 0x00000000ffffffff) == value) {
        map[(*length)++] = addr + 0;
        map[dest++] = map[src];
        ++new_length;
      }
    }
  }
  *length = new_length;
}


x86 assembly (clang, -O0):
<rescan_heap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x40(%rbp),%rax
jae    126c <rescan_heap+0x14c>
mov    -0x10(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
and    $0xfffffffffffffff8,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
and    $0x7,%rax
mov    %eax,-0x2c(%rbp)
mov    0x2e4b(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x20(%rbp),%rdx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e22(%rip),%rax
cmpq   $0x0,(%rax)
je     11e2 <rescan_heap+0xc2>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
call   1030 <err@plt>
jmp    1256 <rescan_heap+0x136>
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sar    %cl,%rax
movabs $0xffffffff,%rcx
and    %rcx,%rax
mov    -0x8(%rbp),%ecx
cmp    %rcx,%rax
jne    1251 <rescan_heap+0x131>
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rsi
mov    (%rsi),%rcx
mov    %rcx,%rdi
add    $0x1,%rdi
mov    %rdi,(%rsi)
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x10(%rbp),%rax
mov    -0x50(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x50(%rbp)
mov    %rdx,(%rax,%rcx,8)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1256 <rescan_heap+0x136>
jmp    125b <rescan_heap+0x13b>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1159 <rescan_heap+0x39>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rescan_heap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,0x10(%rsp)
mov    %edi,0x14(%rsp)
mov    %rcx,0x18(%rsp)
mov    (%rcx),%r15
test   %r15,%r15
je     11fe <rescan_heap+0xde>
mov    %rdx,%rbx
xor    %eax,%eax
mov    %rax,0x20(%rsp)
mov    0x2e88(%rip),%r14
xor    %r13d,%r13d
xor    %eax,%eax
mov    %rax,0x8(%rsp)
jmp    118e <rescan_heap+0x6e>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
call   1030 <err@plt>
add    $0x1,%r13
cmp    %r13,%r15
je     1205 <rescan_heap+0xe5>
mov    (%rbx,%r13,8),%r12
mov    %r12,%rbp
and    $0xfffffffffffffff8,%rbp
movq   $0x0,(%r14)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    0x14(%rsp),%esi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
cmpq   $0x0,(%r14)
jne    1170 <rescan_heap+0x50>
and    $0x7,%r12b
mov    %r12d,%ecx
sar    %cl,%rax
cmp    0x10(%rsp),%eax
jne    1185 <rescan_heap+0x65>
mov    0x18(%rsp),%rdx
mov    (%rdx),%rax
lea    0x1(%rax),%rcx
mov    %rcx,(%rdx)
mov    %rbp,(%rbx,%rax,8)
mov    (%rbx,%r13,8),%rax
mov    0x20(%rsp),%rcx
mov    %rax,(%rbx,%rcx,8)
add    $0x1,%rcx
mov    %rcx,0x20(%rsp)
addq   $0x1,0x8(%rsp)
jmp    1185 <rescan_heap+0x65>
xor    %eax,%eax
mov    %rax,0x8(%rsp)
mov    0x18(%rsp),%rax
mov    0x8(%rsp),%rcx
mov    %rcx,(%rax)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@PTRACE_PEEKDATA = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [18 x i8] c"ptrace (PEEKDATA)\00"
define void @rescan_heap(i32 %0, i32 %1, i32** %2, i64* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32**
%8 = alloca i64*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32** %2, i32*** %7
store i64* %3, i64** %8
store i64 0, i64* %12
%16 = load i64*, i64** %8
%17 = load i64, i64* %16
store i64 %17, i64* %13
store i64 0, i64* %14
store i64 0, i64* %15
br label %18
18: ; preds = %76, %4
%19 = load i64, i64* %14
%20 = load i64, i64* %13
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %79
22: ; preds = %18
%23 = load i32**, i32*** %7
%24 = load i64, i64* %14
%25 = getelementptr inbounds i32*, i32** %23, i64 %24
%26 = load i32*, i32** %25
%27 = ptrtoint i32* %26 to i64
%28 = and i64 %27, -8
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %9
%30 = load i32**, i32*** %7
%31 = load i64, i64* %14
%32 = getelementptr inbounds i32*, i32** %30, i64 %31
%33 = load i32*, i32** %32
%34 = ptrtoint i32* %33 to i64
%35 = and i64 %34, 7
%36 = trunc i64 %35 to i32
store i32 %36, i32* %11
store i64 0, i64* @errno
%37 = load i32, i32* @PTRACE_PEEKDATA
%38 = load i32, i32* %5
%39 = load i8*, i8** %9
%40 = call i64 @ptrace(i32 %37, i32 %38, i8* %39, i32 0)
store i64 %40, i64* %10
%41 = load i64, i64* @errno
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %22
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @err(i32 %44, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %75
46: ; preds = %22
%47 = load i64, i64* %10
%48 = load i32, i32* %11
%49 = zext i32 %48 to i64
%50 = ashr i64 %47, %49
%51 = and i64 %50, 4294967295
%52 = load i32, i32* %6
%53 = zext i32 %52 to i64
%54 = icmp eq i64 %51, %53
br i1 %54, label %55, label %74
55: ; preds = %46
%56 = load i8*, i8** %9
%57 = getelementptr i8, i8* %56, i64 0
%58 = bitcast i8* %57 to i32*
%59 = load i32**, i32*** %7
%60 = load i64*, i64** %8
%61 = load i64, i64* %60
%62 = add i64 %61, 1
store i64 %62, i64* %60
%63 = getelementptr inbounds i32*, i32** %59, i64 %61
store i32* %58, i32** %63
%64 = load i32**, i32*** %7
%65 = load i64, i64* %14
%66 = getelementptr inbounds i32*, i32** %64, i64 %65
%67 = load i32*, i32** %66
%68 = load i32**, i32*** %7
%69 = load i64, i64* %15
%70 = add i64 %69, 1
store i64 %70, i64* %15
%71 = getelementptr inbounds i32*, i32** %68, i64 %69
store i32* %67, i32** %71
%72 = load i64, i64* %12
%73 = add i64 %72, 1
store i64 %73, i64* %12
br label %74
74: ; preds = %55, %46
br label %75
75: ; preds = %74, %43
br label %76
76: ; preds = %75
%77 = load i64, i64* %14
%78 = add i64 %77, 1
store i64 %78, i64* %14
br label %18
79: ; preds = %18
%80 = load i64, i64* %12
%81 = load i64*, i64** %8
store i64 %80, i64* %81
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@PTRACE_PEEKDATA = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [18 x i8] c"ptrace (PEEKDATA)\00"
define void @rescan_heap(i32 %0, i32 %1, i32** nocapture %2, i64* nocapture %3) {
%5 = load i64, i64* %3
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %39, %4
%8 = phi i64 [ 0, %4 ], [ %40, %39 ]
store i64 %8, i64* %3
ret void
9: ; preds = %4, %39
%10 = phi i64 [ %41, %39 ], [ 0, %4 ]
%11 = phi i64 [ %42, %39 ], [ 0, %4 ]
%12 = phi i64 [ %40, %39 ], [ 0, %4 ]
%13 = getelementptr inbounds i32*, i32** %2, i64 %11
%14 = load i32*, i32** %13
%15 = ptrtoint i32* %14 to i64
%16 = and i64 %15, -8
%17 = inttoptr i64 %16 to i8*
store i64 0, i64* @errno
%18 = load i32, i32* @PTRACE_PEEKDATA
%19 = tail call i64 @ptrace(i32 %18, i32 %0, i8* %17, i32 0)
%20 = load i64, i64* @errno
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %9
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @err(i32 %23, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %39
25: ; preds = %9
%26 = and i64 %15, 7
%27 = ashr i64 %19, %26
%28 = trunc i64 %27 to i32
%29 = icmp eq i32 %28, %1
br i1 %29, label %30, label %39
30: ; preds = %25
%31 = inttoptr i64 %16 to i32*
%32 = load i64, i64* %3
%33 = add i64 %32, 1
store i64 %33, i64* %3
%34 = getelementptr inbounds i32*, i32** %2, i64 %32
store i32* %31, i32** %34
%35 = load i32*, i32** %13
%36 = add i64 %10, 1
%37 = getelementptr inbounds i32*, i32** %2, i64 %10
store i32* %35, i32** %37
%38 = add i64 %12, 1
br label %39
39: ; preds = %22, %30, %25
%40 = phi i64 [ %12, %22 ], [ %38, %30 ], [ %12, %25 ]
%41 = phi i64 [ %10, %22 ], [ %36, %30 ], [ %10, %25 ]
%42 = add nuw i64 %11, 1
%43 = icmp eq i64 %42, %5
br i1 %43, label %7, label %9
}