#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 scalar_t__ uint64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_ATTACH;
int /*<<< orphan*/ PTRACE_CONT;
int /*<<< orphan*/ PTRACE_DETACH;
int /*<<< orphan*/ dump_memory_region(int /*<<< orphan*/ *, scalar_t__,
                                      scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ ptrace(int /*<<< orphan*/, unsigned int, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ sscanf(char *, char *, scalar_t__ *, scalar_t__ *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
void dump_ram(unsigned int pid, FILE *out_file) {
  uint64_t ptraceResult = ptrace(PTRACE_ATTACH, pid, NULL, NULL);
  if (ptraceResult < 0) {
    printf("ramdump: unable to attach to the pid specified\n");
    return;
  }
  wait(NULL);
  char mapsFilename[1024];
  char proc_str[6];
  sprintf(proc_str, "%d", pid);
  sprintf(mapsFilename, "/proc/%s/maps", proc_str);
  FILE *pMapsFile = fopen(mapsFilename, "r");
  char memFilename[1024];
  sprintf(memFilename, "/proc/%s/mem", proc_str);
  FILE *pMemFile = fopen(memFilename, "r");
  char line[256];
  while (fgets(line, 256, pMapsFile) != NULL) {
    uint64_t start_address;
    uint64_t end_address;
    sscanf(line, "%08lx-%08lx\n", &start_address, &end_address);
    dump_memory_region(pMemFile, start_address, end_address - start_address,
                       out_file);
  }
  fclose(pMapsFile);
  fclose(pMemFile);
  ptrace(PTRACE_CONT, pid, NULL, NULL);
  ptrace(PTRACE_DETACH, pid, NULL, NULL);
}


x86 assembly (clang, -O0):
<dump_ram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x950,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1080 <ptrace@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11da <dump_ram+0x4a>
lea    0xe30(%rip),%rdi
call   1040 <printf@plt>
jmp    131c <dump_ram+0x18c>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
lea    -0x426(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe3c(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
lea    -0x420(%rbp),%rdi
lea    -0x426(%rbp),%rdx
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
lea    -0x420(%rbp),%rdi
lea    0xe1c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x430(%rbp)
lea    -0x830(%rbp),%rdi
lea    -0x426(%rbp),%rdx
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
lea    -0x830(%rbp),%rdi
lea    0xde6(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x838(%rbp)
lea    -0x940(%rbp),%rdi
mov    -0x430(%rbp),%rdx
mov    $0x100,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     12d4 <dump_ram+0x144>
lea    -0x940(%rbp),%rdi
lea    0xdb9(%rip),%rsi
lea    -0x948(%rbp),%rdx
lea    -0x950(%rbp),%rcx
call   1070 <sscanf@plt>
mov    -0x838(%rbp),%rdi
mov    -0x948(%rbp),%rsi
mov    -0x950(%rbp),%rdx
sub    -0x948(%rbp),%rdx
mov    -0x10(%rbp),%rcx
call   1060 <dump_memory_region@plt>
jmp    1267 <dump_ram+0xd7>
mov    -0x430(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x838(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1080 <ptrace@plt>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1080 <ptrace@plt>
add    $0x950,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_ram>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x928,%rsp
mov    %rsi,%r12
mov    %edi,%ebp
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <ptrace@plt>
test   %rax,%rax
js     1300 <dump_ram+0x170>
xor    %edi,%edi
call   10b0 <wait@plt>
lea    0xe2f(%rip),%rsi
lea    0x1a(%rsp),%rbx
mov    %rbx,%rdi
mov    %ebp,0x4(%rsp)
mov    %ebp,%edx
xor    %eax,%eax
call   10a0 <sprintf@plt>
lea    0xe16(%rip),%rsi
lea    0x520(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   10a0 <sprintf@plt>
lea    0xe08(%rip),%r14
mov    %rbp,%rdi
mov    %r14,%rsi
call   1090 <fopen@plt>
mov    %rax,%r13
lea    0xdf5(%rip),%rsi
lea    0x120(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   10a0 <sprintf@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
lea    0x20(%rsp),%rdi
mov    $0x100,%esi
mov    %r13,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     12b2 <dump_ram+0x122>
lea    0x20(%rsp),%rbp
lea    0x10(%rsp),%r14
lea    0x8(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
lea    0xda6(%rip),%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1070 <sscanf@plt>
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%rdx
sub    %rsi,%rdx
mov    %rbx,%rdi
mov    %r12,%rcx
call   1060 <dump_memory_region@plt>
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %r13,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    1270 <dump_ram+0xe0>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
mov    0x4(%rsp),%ebx
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <ptrace@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <ptrace@plt>
add    $0x928,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd26(%rip),%rdi
add    $0x928,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
@PTRACE_ATTACH = global i32 0
@.str = [48 x i8] c"ramdump: unable to attach to the pid specified\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [14 x i8] c"/proc/%s/maps\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [13 x i8] c"/proc/%s/mem\00"
@.str.5 = [13 x i8] c"%08lx-%08lx\0A\00"
@PTRACE_CONT = global i32 0
@PTRACE_DETACH = global i32 0
define void @dump_ram(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64
%6 = alloca [1024 x i8]
%7 = alloca [6 x i8]
%8 = alloca i32*
%9 = alloca [1024 x i8]
%10 = alloca i32*
%11 = alloca [256 x i8]
%12 = alloca i64
%13 = alloca i64
store i32 %0, i32* %3
store i32* %1, i32** %4
%14 = load i32, i32* @PTRACE_ATTACH
%15 = load i32, i32* %3
%16 = call i64 @ptrace(i32 %14, i32 %15, i32* null, i32* null)
store i64 %16, i64* %5
%17 = load i64, i64* %5
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
br label %62
21: ; preds = %2
%22 = call i32 @wait(i32* null)
%23 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%24 = load i32, i32* %3
%25 = call i32 (i8*, i8*, ...) @sprintf(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%27 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%28 = call i32 (i8*, i8*, ...) @sprintf(i8* %26, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %30, i32** %8
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%32 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%33 = call i32 (i8*, i8*, ...) @sprintf(i8* %31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %32)
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%35 = call i32* @fopen(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %35, i32** %10
br label %36
36: ; preds = %41, %21
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%38 = load i32*, i32** %8
%39 = call i32* @fgets(i8* %37, i32 256, i32* %38)
%40 = icmp ne i32* %39, null
br i1 %40, label %41, label %51
41: ; preds = %36
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%43 = call i32 @sscanf(i8* %42, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i64* %12, i64* %13)
%44 = load i32*, i32** %10
%45 = load i64, i64* %12
%46 = load i64, i64* %13
%47 = load i64, i64* %12
%48 = sub nsw i64 %46, %47
%49 = load i32*, i32** %4
%50 = call i32 @dump_memory_region(i32* %44, i64 %45, i64 %48, i32* %49)
br label %36
51: ; preds = %36
%52 = load i32*, i32** %8
%53 = call i32 @fclose(i32* %52)
%54 = load i32*, i32** %10
%55 = call i32 @fclose(i32* %54)
%56 = load i32, i32* @PTRACE_CONT
%57 = load i32, i32* %3
%58 = call i64 @ptrace(i32 %56, i32 %57, i32* null, i32* null)
%59 = load i32, i32* @PTRACE_DETACH
%60 = load i32, i32* %3
%61 = call i64 @ptrace(i32 %59, i32 %60, i32* null, i32* null)
br label %62
62: ; preds = %51, %19
ret void
}

LLVM IR (-O2):
@PTRACE_ATTACH = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [14 x i8] c"/proc/%s/maps\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [13 x i8] c"/proc/%s/mem\00"
@.str.5 = [13 x i8] c"%08lx-%08lx\0A\00"
@PTRACE_CONT = global i32 0
@PTRACE_DETACH = global i32 0
@str = [47 x i8] c"ramdump: unable to attach to the pid specified\00"
define void @dump_ram(i32 %0, i32* %1) {
%3 = alloca [1024 x i8]
%4 = alloca [6 x i8]
%5 = alloca [1024 x i8]
%6 = alloca [256 x i8]
%7 = alloca i64
%8 = alloca i64
%9 = load i32, i32* @PTRACE_ATTACH
%10 = tail call i64 @ptrace(i32 %9, i32 %0, i32* null, i32* null)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
br label %45
14: ; preds = %2
%15 = tail call i32 @wait(i32* null)
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%17 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%18 = call i32 (i8*, i8*, ...) @sprintf(i8* %17, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%19 = call i32 (i8*, i8*, ...) @sprintf(i8* %16, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%20 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%22 = call i32 (i8*, i8*, ...) @sprintf(i8* %21, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %17)
%23 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%25 = call i32* @fgets(i8* %24, i32 256, i32* %20)
%26 = icmp eq i32* %25, null
br i1 %26, label %38, label %27
27: ; preds = %14
%28 = bitcast i64* %7 to i8*
%29 = bitcast i64* %8 to i8*
br label %30
30: ; preds = %27, %30
%31 = call i32 @sscanf(i8* %24, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i64* %7, i64* %8)
%32 = load i64, i64* %7
%33 = load i64, i64* %8
%34 = sub nsw i64 %33, %32
%35 = call i32 @dump_memory_region(i32* %23, i64 %32, i64 %34, i32* %1)
%36 = call i32* @fgets(i8* %24, i32 256, i32* %20)
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %30
38: ; preds = %30, %14
%39 = call i32 @fclose(i32* %20)
%40 = call i32 @fclose(i32* %23)
%41 = load i32, i32* @PTRACE_CONT
%42 = call i64 @ptrace(i32 %41, i32 %0, i32* null, i32* null)
%43 = load i32, i32* @PTRACE_DETACH
%44 = call i64 @ptrace(i32 %43, i32 %0, i32* null, i32* null)
br label %45
45: ; preds = %38, %12
ret void
}