#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*/ PTRACE_PEEKTEXT;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned int, unsigned int);
int /*<<< orphan*/ procmsg(char *, int /*<<< orphan*/, unsigned int,
                           unsigned int);
unsigned int ptrace(int /*<<< orphan*/, int /*<<< orphan*/, unsigned int,
                    int /*<<< orphan*/);
int dump_process_memory(pid_t pid, unsigned from_addr, int size, char *mem) {
  procmsg("Dump of %d's memory [0x%08X : 0x%08X]\n", pid, from_addr,
          from_addr + size);
  for (int i = 0; i < size; i++) {
    unsigned word;
    if ((int)(word = ptrace(PTRACE_PEEKTEXT, pid, from_addr + i, 0)) == -1 &&
        errno != 0) {
      perror("ptrace3");
      return -1;
    }
    printf("  0x%08X:  %02x\n", from_addr + size, word & 0xFF);
    mem[i] = word & 0xFF;
  }
  return 0;
}


x86 assembly (clang, -O0):
<dump_process_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0xc(%rbp),%ecx
add    -0x10(%rbp),%ecx
lea    0xe98(%rip),%rdi
call   1030 <procmsg@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    120c <dump_process_memory+0xcc>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
add    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1050 <ptrace@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11ce <dump_process_memory+0x8e>
mov    0x2e34(%rip),%rax
cmpq   $0x0,(%rax)
je     11ce <dump_process_memory+0x8e>
lea    0xe6a(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <dump_process_memory+0xd3>
mov    -0xc(%rbp),%esi
add    -0x10(%rbp),%esi
mov    -0x20(%rbp),%edx
and    $0xff,%edx
lea    0xe4b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
and    $0xff,%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1174 <dump_process_memory+0x34>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_process_memory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x10(%rsp)
mov    %edx,%r12d
mov    %esi,%ebx
mov    %edi,%r15d
lea    (%r12,%rbx,1),%r13d
lea    0xe9a(%rip),%rdi
mov    %r15d,%esi
mov    %ebx,%edx
mov    %r13d,%ecx
call   1030 <procmsg@plt>
test   %r12d,%r12d
jle    1227 <dump_process_memory+0xe7>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    %r15d,%esi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1050 <ptrace@plt>
mov    0x2e48(%rip),%rcx
cmp    $0xffffffff,%eax
jne    11a7 <dump_process_memory+0x67>
cmpq   $0x0,(%rcx)
jne    123d <dump_process_memory+0xfd>
mov    %r12d,%ecx
add    $0x1,%ebx
mov    %rcx,0x8(%rsp)
lea    -0x1(%rcx),%r14
xor    %r12d,%r12d
nopl   0x0(%rax)
movzbl %al,%ebp
lea    0xe65(%rip),%rdi
mov    %r13d,%esi
mov    %ebp,%edx
call   1040 <printf@plt>
mov    0x10(%rsp),%rax
mov    %bpl,(%rax,%r12,1)
cmp    %r12,%r14
je     1219 <dump_process_memory+0xd9>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    (%rbx,%r12,1),%edx
mov    %r15d,%esi
xor    %ecx,%ecx
call   1050 <ptrace@plt>
add    $0x1,%r12
cmp    $0xffffffff,%eax
jne    11c0 <dump_process_memory+0x80>
mov    0x2dd7(%rip),%rcx
cmpq   $0x0,(%rcx)
je     11c0 <dump_process_memory+0x80>
cmp    0x8(%rsp),%r12
setb   %bl
jmp    123f <dump_process_memory+0xff>
add    $0x1,%r12
cmp    0x8(%rsp),%r12
setb   %bl
jmp    1229 <dump_process_memory+0xe9>
xor    %ebx,%ebx
movzbl %bl,%eax
neg    %eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%bl
lea    0xde1(%rip),%rdi
call   1060 <perror@plt>
jmp    1229 <dump_process_memory+0xe9>

LLVM IR (-O0):
@.str = [39 x i8] c"Dump of %d's memory [0x%08X : 0x%08X]\0A\00"
@PTRACE_PEEKTEXT = global i32 0
@errno = global i64 0
@.str.1 = [8 x i8] c"ptrace3\00"
@.str.2 = [17 x i8] c" 0x%08X: %02x\0A\00"
define i32 @dump_process_memory(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%12 = load i32, i32* %6
%13 = load i32, i32* %7
%14 = load i32, i32* %7
%15 = load i32, i32* %8
%16 = add i32 %14, %15
%17 = call i32 @procmsg(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13, i32 %16)
store i32 0, i32* %10
br label %18
18: ; preds = %49, %4
%19 = load i32, i32* %10
%20 = load i32, i32* %8
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %52
22: ; preds = %18
%23 = load i32, i32* @PTRACE_PEEKTEXT
%24 = load i32, i32* %6
%25 = load i32, i32* %7
%26 = load i32, i32* %10
%27 = add i32 %25, %26
%28 = call i32 @ptrace(i32 %23, i32 %24, i32 %27, i32 0)
store i32 %28, i32* %11
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %22
%31 = load i64, i64* @errno
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %53
35: ; preds = %30, %22
%36 = load i32, i32* %7
%37 = load i32, i32* %8
%38 = add i32 %36, %37
%39 = load i32, i32* %11
%40 = and i32 %39, 255
%41 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %38, i32 %40)
%42 = load i32, i32* %11
%43 = and i32 %42, 255
%44 = trunc i32 %43 to i8
%45 = load i8*, i8** %9
%46 = load i32, i32* %10
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 %44, i8* %48
br label %49
49: ; preds = %35
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %10
br label %18
52: ; preds = %18
store i32 0, i32* %5
br label %53
53: ; preds = %52, %33
%54 = load i32, i32* %5
ret i32 %54
}

LLVM IR (-O2):
@.str = [39 x i8] c"Dump of %d's memory [0x%08X : 0x%08X]\0A\00"
@PTRACE_PEEKTEXT = global i32 0
@errno = global i64 0
@.str.1 = [8 x i8] c"ptrace3\00"
@.str.2 = [17 x i8] c" 0x%08X: %02x\0A\00"
define i32 @dump_process_memory(i32 %0, i32 %1, i32 %2, i8* nocapture writeonly %3) {
%5 = add i32 %2, %1
%6 = tail call i32 @procmsg(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1, i32 %5)
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %42
8: ; preds = %4
%9 = zext i32 %2 to i64
%10 = zext i32 %2 to i64
%11 = load i32, i32* @PTRACE_PEEKTEXT
%12 = tail call i32 @ptrace(i32 %11, i32 %0, i32 %1, i32 0)
%13 = icmp eq i32 %12, -1
%14 = load i64, i64* @errno
%15 = icmp ne i64 %14, 0
%16 = select i1 %13, i1 %15, i1 false
br i1 %16, label %28, label %31
17: ; preds = %31
%18 = load i32, i32* @PTRACE_PEEKTEXT
%19 = trunc i64 %38 to i32
%20 = add i32 %19, %1
%21 = tail call i32 @ptrace(i32 %18, i32 %0, i32 %20, i32 0)
%22 = icmp eq i32 %21, -1
%23 = load i64, i64* @errno
%24 = icmp ne i64 %23, 0
%25 = select i1 %22, i1 %24, i1 false
br i1 %25, label %26, label %31
26: ; preds = %17
%27 = icmp ult i64 %38, %9
br label %28
28: ; preds = %26, %8
%29 = phi i1 [ true, %8 ], [ %27, %26 ]
%30 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %42
31: ; preds = %8, %17
%32 = phi i32 [ %21, %17 ], [ %12, %8 ]
%33 = phi i64 [ %38, %17 ], [ 0, %8 ]
%34 = and i32 %32, 255
%35 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %5, i32 %34)
%36 = trunc i32 %32 to i8
%37 = getelementptr inbounds i8, i8* %3, i64 %33
store i8 %36, i8* %37
%38 = add nuw nsw i64 %33, 1
%39 = icmp eq i64 %38, %10
br i1 %39, label %40, label %17
40: ; preds = %31
%41 = icmp ult i64 %38, %9
br label %42
42: ; preds = %40, %4, %28
%43 = phi i1 [ %29, %28 ], [ false, %4 ], [ %41, %40 ]
%44 = sext i1 %43 to i32
ret i32 %44
}