#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 EXIT_SUCCESS;
int /*<<< orphan*/ attach(int /*<<< orphan*/);
int /*<<< orphan*/ detach(int /*<<< orphan*/);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
scalar_t__ errno;
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *);
unsigned int **malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rescan_heap(int /*<<< orphan*/, unsigned int,
                               unsigned int **, size_t *);
int /*<<< orphan*/ scan_heap(int /*<<< orphan*/, unsigned int, unsigned int **,
                             size_t *, size_t);
int /*<<< orphan*/ scanf(char *, unsigned int *);
int /*<<< orphan*/ strtol(char *, char **, int);
int /*<<< orphan*/ write_heap(int /*<<< orphan*/, unsigned int, unsigned int *);
int main(int argc, char **argv) {
  if (argc != 2)
    errx(EXIT_FAILURE, "missing argument");
  char *end = NULL;
  pid_t pid;
  size_t length = 0, max_length = 1024 * 1024;
  unsigned **map = malloc(sizeof(unsigned *) * max_length);
  unsigned value;
  errno = 0;
  pid = strtol(argv[1], &end, 10);
  if (errno)
    err(EXIT_FAILURE, "strtol");
  else if (argv[1] == end || *end)
    errx(EXIT_FAILURE, "strtol");
  printf("Enter current value: ");
  scanf("%u", &value);
  printf("Scanning memory for %u...\n", value);
  attach(pid);
  scan_heap(pid, value, map, &length, max_length);
  detach(pid);
  printf("Found %u %zu times.\n", value, length);
  for (; length > 1;) {
    printf("Enter current value: ");
    scanf("%u", &value);
    attach(pid);
    printf("Re-scanning memory for %u...\n", value);
    rescan_heap(pid, value, map, &length);
    printf("Found %u %zu times.\n", value, length);
    if (length == 1) {
      printf("Enter desired value: ");
      scanf("%u", &value);
      write_heap(pid, value, map[0]);
    }
    detach(pid);
    printf("\n");
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e5 <main+0x35>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1050 <errx@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x100000,-0x30(%rbp)
mov    -0x30(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    0x2dc9(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2da2(%rip),%rax
cmpq   $0x0,(%rax)
je     1262 <main+0xb2>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xdb9(%rip),%rsi
call   1090 <err@plt>
jmp    129e <main+0xee>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
cmp    -0x18(%rbp),%rax
je     1284 <main+0xd4>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1299 <main+0xe9>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    0xd7d(%rip),%rsi
call   1050 <errx@plt>
jmp    129e <main+0xee>
lea    0xd73(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xd7b(%rip),%rdi
lea    -0x3c(%rbp),%rsi
call   10c0 <scanf@plt>
mov    -0x3c(%rbp),%esi
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <attach@plt>
mov    -0x1c(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%r8
lea    -0x28(%rbp),%rcx
call   1030 <scan_heap@plt>
mov    -0x1c(%rbp),%edi
call   10d0 <detach@plt>
mov    -0x3c(%rbp),%esi
mov    -0x28(%rbp),%rdx
lea    0xd4a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpq   $0x1,-0x28(%rbp)
jbe    13c9 <main+0x219>
lea    0xcfd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xd05(%rip),%rdi
lea    -0x3c(%rbp),%rsi
call   10c0 <scanf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <attach@plt>
mov    -0x3c(%rbp),%esi
lea    0xd1d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1070 <rescan_heap@plt>
mov    -0x3c(%rbp),%esi
mov    -0x28(%rbp),%rdx
lea    0xce0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpq   $0x1,-0x28(%rbp)
jne    13ae <main+0x1fe>
lea    0xcfa(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xc9b(%rip),%rdi
lea    -0x3c(%rbp),%rsi
call   10c0 <scanf@plt>
mov    -0x1c(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%rdx
call   10b0 <write_heap@plt>
mov    -0x1c(%rbp),%edi
call   10d0 <detach@plt>
lea    0xc8d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1309 <main+0x159>
mov    0x2c00(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
je     11eb <main+0x2b>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1060 <errx@plt>
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    $0x800000,%edi
call   10b0 <malloc@plt>
mov    %rax,%rbx
mov    0x2dcf(%rip),%r15
movq   $0x0,(%r15)
mov    0x8(%r14),%rdi
lea    0x10(%rsp),%rsi
mov    $0xa,%edx
call   1090 <strtol@plt>
mov    %eax,%ebp
cmpq   $0x0,(%r15)
je     124a <main+0x8a>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xdce(%rip),%rsi
call   10a0 <err@plt>
jmp    126f <main+0xaf>
mov    0x10(%rsp),%rax
cmp    %rax,0x8(%r14)
je     125a <main+0x9a>
cmpb   $0x0,(%rax)
je     126f <main+0xaf>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    0xda7(%rip),%rsi
call   1060 <errx@plt>
lea    0xda2(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xdaa(%rip),%rdi
lea    0x4(%rsp),%rsi
call   10d0 <scanf@plt>
mov    0x4(%rsp),%esi
lea    0xd98(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%edi
call   1050 <attach@plt>
mov    0x4(%rsp),%esi
lea    0x8(%rsp),%rcx
mov    $0x100000,%r8d
mov    %ebp,%edi
mov    %rbx,%rdx
call   1040 <scan_heap@plt>
mov    %ebp,%edi
call   10e0 <detach@plt>
mov    0x4(%rsp),%esi
mov    0x8(%rsp),%rdx
lea    0xd75(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
cmpq   $0x2,0x8(%rsp)
jb     13b7 <main+0x1f7>
lea    0xd3d(%rip),%r15
lea    0x4(%rsp),%r12
lea    0x8(%rsp),%r14
lea    0xd4a(%rip),%r13
jmp    132d <main+0x16d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
call   10e0 <detach@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
cmpq   $0x1,0x8(%rsp)
jbe    13b7 <main+0x1f7>
lea    0xce4(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   10d0 <scanf@plt>
mov    %ebp,%edi
call   1050 <attach@plt>
mov    0x4(%rsp),%esi
lea    0xd09(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x4(%rsp),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1080 <rescan_heap@plt>
mov    0x4(%rsp),%esi
mov    0x8(%rsp),%rdx
mov    %r13,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
cmpq   $0x1,0x8(%rsp)
jne    1310 <main+0x150>
lea    0xced(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   10d0 <scanf@plt>
mov    0x4(%rsp),%esi
mov    (%rbx),%rdx
mov    %ebp,%edi
call   10c0 <write_heap@plt>
jmp    1310 <main+0x150>
mov    0x2c12(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [17 x i8] c"missing argument\00"
@errno = global i64 0
@.str.1 = [7 x i8] c"strtol\00"
@.str.2 = [22 x i8] c"Enter current value: \00"
@.str.3 = [3 x i8] c"%u\00"
@.str.4 = [27 x i8] c"Scanning memory for %u...\0A\00"
@.str.5 = [21 x i8] c"Found %u %zu times.\0A\00"
@.str.6 = [30 x i8] c"Re-scanning memory for %u...\0A\00"
@.str.7 = [22 x i8] c"Enter desired value: \00"
@.str.8 = [2 x i8] c"\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32**
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @errx(i32 %15, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %14, %2
store i8* null, i8** %6
store i64 0, i64* %8
store i64 1048576, i64* %9
%18 = load i64, i64* %9
%19 = mul i64 8, %18
%20 = trunc i64 %19 to i32
%21 = call i32** @malloc(i32 %20)
store i32** %21, i32*** %10
store i64 0, i64* @errno
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @strtol(i8* %24, i8** %6, i32 10)
store i32 %25, i32* %7
%26 = load i64, i64* @errno
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @err(i32 %29, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %46
31: ; preds = %17
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = load i8*, i8** %6
%36 = icmp eq i8* %34, %35
br i1 %36, label %42, label %37
37: ; preds = %31
%38 = load i8*, i8** %6
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37, %31
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @errx(i32 %43, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %45
45: ; preds = %42, %37
br label %46
46: ; preds = %45, %28
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%48 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %11)
%49 = load i32, i32* %11
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %49)
%51 = load i32, i32* %7
%52 = call i32 @attach(i32 %51)
%53 = load i32, i32* %7
%54 = load i32, i32* %11
%55 = load i32**, i32*** %10
%56 = load i64, i64* %9
%57 = call i32 @scan_heap(i32 %53, i32 %54, i32** %55, i64* %8, i64 %56)
%58 = load i32, i32* %7
%59 = call i32 @detach(i32 %58)
%60 = load i32, i32* %11
%61 = load i64, i64* %8
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %60, i64 %61)
br label %63
63: ; preds = %91, %46
%64 = load i64, i64* %8
%65 = icmp ugt i64 %64, 1
br i1 %65, label %66, label %95
66: ; preds = %63
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%68 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %11)
%69 = load i32, i32* %7
%70 = call i32 @attach(i32 %69)
%71 = load i32, i32* %11
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i32 %71)
%73 = load i32, i32* %7
%74 = load i32, i32* %11
%75 = load i32**, i32*** %10
%76 = call i32 @rescan_heap(i32 %73, i32 %74, i32** %75, i64* %8)
%77 = load i32, i32* %11
%78 = load i64, i64* %8
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %77, i64 %78)
%80 = load i64, i64* %8
%81 = icmp eq i64 %80, 1
br i1 %81, label %82, label %91
82: ; preds = %66
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
%84 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %11)
%85 = load i32, i32* %7
%86 = load i32, i32* %11
%87 = load i32**, i32*** %10
%88 = getelementptr inbounds i32*, i32** %87, i64 0
%89 = load i32*, i32** %88
%90 = call i32 @write_heap(i32 %85, i32 %86, i32* %89)
br label %91
91: ; preds = %82, %66
%92 = load i32, i32* %7
%93 = call i32 @detach(i32 %92)
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
br label %63
95: ; preds = %63
%96 = load i32, i32* @EXIT_SUCCESS
ret i32 %96
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [17 x i8] c"missing argument\00"
@errno = global i64 0
@.str.1 = [7 x i8] c"strtol\00"
@.str.2 = [22 x i8] c"Enter current value: \00"
@.str.3 = [3 x i8] c"%u\00"
@.str.4 = [27 x i8] c"Scanning memory for %u...\0A\00"
@.str.5 = [21 x i8] c"Found %u %zu times.\0A\00"
@.str.6 = [30 x i8] c"Re-scanning memory for %u...\0A\00"
@.str.7 = [22 x i8] c"Enter desired value: \00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = icmp eq i32 %0, 2
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @errx(i32 %8, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %7, %2
%11 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%12 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%13 = tail call i32** @malloc(i32 8388608)
%14 = bitcast i32* %5 to i8*
store i64 0, i64* @errno
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @strtol(i8* %16, i8** %3, i32 10)
%18 = load i64, i64* @errno
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %10
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @err(i32 %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %33
23: ; preds = %10
%24 = load i8*, i8** %15
%25 = load i8*, i8** %3
%26 = icmp eq i8* %24, %25
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = load i8, i8* %25
%29 = icmp eq i8 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %27, %23
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @errx(i32 %31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %27, %30, %20
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%36 = load i32, i32* %5
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %36)
%38 = call i32 @attach(i32 %17)
%39 = load i32, i32* %5
%40 = call i32 @scan_heap(i32 %17, i32 %39, i32** %13, i64* %4, i64 1048576)
%41 = call i32 @detach(i32 %17)
%42 = load i32, i32* %5
%43 = load i64, i64* %4
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %42, i64 %43)
%45 = load i64, i64* %4
%46 = icmp ugt i64 %45, 1
br i1 %46, label %47, label %71
47: ; preds = %33, %66
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%50 = call i32 @attach(i32 %17)
%51 = load i32, i32* %5
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i32 %51)
%53 = load i32, i32* %5
%54 = call i32 @rescan_heap(i32 %17, i32 %53, i32** %13, i64* %4)
%55 = load i32, i32* %5
%56 = load i64, i64* %4
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %55, i64 %56)
%58 = load i64, i64* %4
%59 = icmp eq i64 %58, 1
br i1 %59, label %60, label %66
60: ; preds = %47
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
%62 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%63 = load i32, i32* %5
%64 = load i32*, i32** %13
%65 = call i32 @write_heap(i32 %17, i32 %63, i32* %64)
br label %66
66: ; preds = %60, %47
%67 = call i32 @detach(i32 %17)
%68 = call i32 @putchar(i32 10)
%69 = load i64, i64* %4
%70 = icmp ugt i64 %69, 1
br i1 %70, label %47, label %71
71: ; preds = %66, %33
%72 = load i32, i32* @EXIT_SUCCESS
ret i32 %72
}