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

/* Variables and functions */
int /*<<< orphan*/ PTRACE_ATTACH;
int /*<<< orphan*/ PTRACE_DETACH;
char *calloc(int, int);
scalar_t__ errno;
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, unsigned int *,
                          unsigned int *, char *);
int /*<<< orphan*/ perror(char *);
int ptrace(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ readMem(int, unsigned int, unsigned int,
                           int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ strstr(char *, char *);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
void pullPidMemory(FILE *out_file, int target_pid) {
  FILE *mem_maps_file;
  char *maps_path = calloc(1, 256);
  char *name = calloc(1, 256);
  unsigned int mem_low = 0;
  unsigned int mem_high = 0;
  int i;
  errno = 0;
  i = ptrace(PTRACE_ATTACH, target_pid, 0, 0);
  if (i == -1 && errno) {
    free(maps_path);
    free(name);
    return;
  }
  waitpid(target_pid, NULL, 0);
  snprintf(maps_path, 255, "/proc/%d/maps", target_pid);
  mem_maps_file = fopen(maps_path, "r");
  if (mem_maps_file == NULL) {
    perror("Error opening memory map for this PID");
    goto leave;
  }
  while (!feof(mem_maps_file)) {
    fscanf(mem_maps_file, "%x-%x %[^\n]", &mem_low, &mem_high, name);
    if (strstr(name, "[heap]") || strstr(name, "[stack]") ||
        strstr(name, "deleted")) {
      readMem(target_pid, mem_low, mem_high - mem_low, out_file);
    }
  }
leave:
  ptrace(PTRACE_DETACH, target_pid, 0, 0);
  free(maps_path);
  free(name);
}


x86 assembly (clang, -O0):
<pullPidMemory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x1,%edi
mov    $0x100,%esi
call   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x1,%edi
mov    $0x100,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    0x2dd6(%rip),%rax
movq   $0x0,(%rax)
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1090 <ptrace@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    124b <pullPidMemory+0x9b>
mov    0x2da6(%rip),%rax
cmpq   $0x0,(%rax)
je     124b <pullPidMemory+0x9b>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    1374 <pullPidMemory+0x1c4>
mov    -0xc(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0xff,%esi
lea    0xd94(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xd92(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12a1 <pullPidMemory+0xf1>
lea    0xd79(%rip),%rdi
call   10c0 <perror@plt>
jmp    134d <pullPidMemory+0x19d>
jmp    12a6 <pullPidMemory+0xf6>
mov    -0x18(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12c4 <pullPidMemory+0x114>
jmp    1348 <pullPidMemory+0x198>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%r8
lea    0xd63(%rip),%rsi
lea    -0x2c(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1050 <fscanf@plt>
mov    -0x28(%rbp),%rdi
lea    0xd57(%rip),%rsi
call   10d0 <strstr@plt>
cmp    $0x0,%rax
jne    132e <pullPidMemory+0x17e>
mov    -0x28(%rbp),%rdi
lea    0xd44(%rip),%rsi
call   10d0 <strstr@plt>
cmp    $0x0,%rax
jne    132e <pullPidMemory+0x17e>
mov    -0x28(%rbp),%rdi
lea    0xd32(%rip),%rsi
call   10d0 <strstr@plt>
cmp    $0x0,%rax
je     1343 <pullPidMemory+0x193>
mov    -0xc(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x30(%rbp),%edx
sub    -0x2c(%rbp),%edx
mov    -0x8(%rbp),%rcx
call   1080 <readMem@plt>
jmp    12a6 <pullPidMemory+0xf6>
jmp    134d <pullPidMemory+0x19d>
mov    0x2c94(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1090 <ptrace@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pullPidMemory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r12d
mov    %rdi,%r15
mov    $0x1,%edi
mov    $0x100,%esi
call   1060 <calloc@plt>
mov    %rax,%rbp
mov    $0x1,%edi
mov    $0x100,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
mov    0x2dd1(%rip),%r14
movq   $0x0,(%r14)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    %r12d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    1244 <pullPidMemory+0x94>
cmpq   $0x0,(%r14)
je     1244 <pullPidMemory+0x94>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <waitpid@plt>
lea    0xda9(%rip),%rdx
mov    %rbp,%rdi
mov    $0xff,%esi
mov    %r12d,%ecx
call   1040 <snprintf@plt>
lea    0xda0(%rip),%rsi
mov    %rbp,0x10(%rsp)
mov    %rbp,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     136b <pullPidMemory+0x1bb>
mov    %rax,%rbp
mov    %rax,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1335 <pullPidMemory+0x185>
lea    0x8(%rsp),%r13
lea    0xd9f(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
lea    0xd7c(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %r13,%rcx
mov    %rbx,%r8
call   1050 <fscanf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10d0 <strstr@plt>
test   %rax,%rax
jne    1310 <pullPidMemory+0x160>
mov    %rbx,%rdi
lea    0xd65(%rip),%rsi
call   10d0 <strstr@plt>
test   %rax,%rax
jne    1310 <pullPidMemory+0x160>
mov    %rbx,%rdi
lea    0xd59(%rip),%rsi
call   10d0 <strstr@plt>
test   %rax,%rax
je     1325 <pullPidMemory+0x175>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%edx
sub    %esi,%edx
mov    %r12d,%edi
mov    %r15,%rcx
call   1080 <readMem@plt>
mov    %rbp,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     12b0 <pullPidMemory+0x100>
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
mov    %r12d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <ptrace@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc9e(%rip),%rdi
call   10c0 <perror@plt>
jmp    1335 <pullPidMemory+0x185>

LLVM IR (-O0):
@errno = global i64 0
@PTRACE_ATTACH = global i32 0
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [38 x i8] c"Error opening memory map for this PID\00"
@.str.3 = [12 x i8] c"%x-%x %[^\0A]\00"
@.str.4 = [7 x i8] c"[heap]\00"
@.str.5 = [8 x i8] c"[stack]\00"
@.str.6 = [8 x i8] c"deleted\00"
@PTRACE_DETACH = global i32 0
define void @pullPidMemory(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%11 = call i8* @calloc(i32 1, i32 256)
store i8* %11, i8** %6
%12 = call i8* @calloc(i32 1, i32 256)
store i8* %12, i8** %7
store i32 0, i32* %8
store i32 0, i32* %9
store i64 0, i64* @errno
%13 = load i32, i32* @PTRACE_ATTACH
%14 = load i32, i32* %4
%15 = call i32 @ptrace(i32 %13, i32 %14, i32 0, i32 0)
store i32 %15, i32* %10
%16 = load i32, i32* %10
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %26
18: ; preds = %2
%19 = load i64, i64* @errno
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i8*, i8** %6
%23 = call i32 @free(i8* %22)
%24 = load i8*, i8** %7
%25 = call i32 @free(i8* %24)
br label %77
26: ; preds = %18, %2
%27 = load i32, i32* %4
%28 = call i32 @waitpid(i32 %27, i32* null, i32 0)
%29 = load i8*, i8** %6
%30 = load i32, i32* %4
%31 = call i32 @snprintf(i8* %29, i32 255, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = load i8*, i8** %6
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %33, i32** %5
%34 = load i32*, i32** %5
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %38
36: ; preds = %26
%37 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %69
38: ; preds = %26
br label %39
39: ; preds = %67, %38
%40 = load i32*, i32** %5
%41 = call i32 @feof(i32* %40)
%42 = icmp ne i32 %41, 0
%43 = xor i1 %42, true
br i1 %43, label %44, label %68
44: ; preds = %39
%45 = load i32*, i32** %5
%46 = load i8*, i8** %7
%47 = call i32 @fscanf(i32* %45, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32* %8, i32* %9, i8* %46)
%48 = load i8*, i8** %7
%49 = call i64 @strstr(i8* %48, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%50 = icmp ne i64 %49, 0
br i1 %50, label %59, label %51
51: ; preds = %44
%52 = load i8*, i8** %7
%53 = call i64 @strstr(i8* %52, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%54 = icmp ne i64 %53, 0
br i1 %54, label %59, label %55
55: ; preds = %51
%56 = load i8*, i8** %7
%57 = call i64 @strstr(i8* %56, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %67
59: ; preds = %55, %51, %44
%60 = load i32, i32* %4
%61 = load i32, i32* %8
%62 = load i32, i32* %9
%63 = load i32, i32* %8
%64 = sub i32 %62, %63
%65 = load i32*, i32** %3
%66 = call i32 @readMem(i32 %60, i32 %61, i32 %64, i32* %65)
br label %67
67: ; preds = %59, %55
br label %39
68: ; preds = %39
br label %69
69: ; preds = %68, %36
%70 = load i32, i32* @PTRACE_DETACH
%71 = load i32, i32* %4
%72 = call i32 @ptrace(i32 %70, i32 %71, i32 0, i32 0)
%73 = load i8*, i8** %6
%74 = call i32 @free(i8* %73)
%75 = load i8*, i8** %7
%76 = call i32 @free(i8* %75)
br label %77
77: ; preds = %69, %21
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@PTRACE_ATTACH = global i32 0
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [38 x i8] c"Error opening memory map for this PID\00"
@.str.3 = [12 x i8] c"%x-%x %[^\0A]\00"
@.str.4 = [7 x i8] c"[heap]\00"
@.str.5 = [8 x i8] c"[stack]\00"
@.str.6 = [8 x i8] c"deleted\00"
@PTRACE_DETACH = global i32 0
define void @pullPidMemory(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = tail call dereferenceable_or_null(256) i8* @calloc(i32 1, i32 256)
%6 = tail call dereferenceable_or_null(256) i8* @calloc(i32 1, i32 256)
%7 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%8 = bitcast i32* %4 to i8*
store i32 0, i32* %4
store i64 0, i64* @errno
%9 = load i32, i32* @PTRACE_ATTACH
%10 = tail call i32 @ptrace(i32 %9, i32 %1, i32 0, i32 0)
%11 = icmp eq i32 %10, -1
%12 = load i64, i64* @errno
%13 = icmp ne i64 %12, 0
%14 = select i1 %11, i1 %13, i1 false
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = tail call i32 @free(i8* %5)
%17 = tail call i32 @free(i8* %6)
br label %51
18: ; preds = %2
%19 = tail call i32 @waitpid(i32 %1, i32* null, i32 0)
%20 = tail call i32 @snprintf(i8* %5, i32 255, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %1)
%21 = tail call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %26, label %23
23: ; preds = %18
%24 = call i32 @feof(i32* %21)
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %46
26: ; preds = %18
%27 = tail call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %46
28: ; preds = %23, %43
%29 = call i32 @fscanf(i32* %21, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32* %3, i32* %4, i8* %6)
%30 = call i64 @strstr(i8* %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %28
%33 = call i64 @strstr(i8* %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = call i64 @strstr(i8* %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %35, %32, %28
%39 = load i32, i32* %3
%40 = load i32, i32* %4
%41 = sub i32 %40, %39
%42 = call i32 @readMem(i32 %1, i32 %39, i32 %41, i32* %0)
br label %43
43: ; preds = %38, %35
%44 = call i32 @feof(i32* %21)
%45 = icmp eq i32 %44, 0
br i1 %45, label %28, label %46
46: ; preds = %43, %23, %26
%47 = load i32, i32* @PTRACE_DETACH
%48 = call i32 @ptrace(i32 %47, i32 %1, i32 0, i32 0)
%49 = call i32 @free(i8* %5)
%50 = call i32 @free(i8* %6)
br label %51
51: ; preds = %46, %15
ret void
}