#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_ATTACH;
int /*<<< orphan*/ PTRACE_DETACH;
int /*<<< orphan*/ PTRACE_PEEKDATA;
scalar_t__ errno;
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned int);
int ptrace(int /*<<< orphan*/, int /*<<< orphan*/, void *,
           int /*<<< orphan*/ *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int ReadProcessMemory(pid_t pid, unsigned long address, unsigned long length,
                      unsigned char *buf) {
  int buffer;
  int endAddress = address + length;
  int loopCount = 0;
  if (ptrace(PTRACE_ATTACH, pid, NULL, NULL)) {
    perror("PTRACE_ATTACH");
    return 0;
  }
  wait(NULL);
  for (; address < endAddress; address += sizeof(int)) {
    errno = 0;
    if (((buffer = ptrace(PTRACE_PEEKDATA, pid, (void *)address, NULL)) ==
         -1) &&
        errno) {
      perror("PTRACE_PEEKDATA");
      printf("%02X", (unsigned int)address);
      if (ptrace(PTRACE_DETACH, pid, NULL, NULL))
        perror("PTRACE_DETACH");
      return 0;
    }
    int pos = loopCount * sizeof(int);
    if (pos > 0) {
      buf += sizeof(int);
      memcpy(buf, (unsigned char *)&buffer, sizeof(int));
    } else {
      memcpy(buf, (unsigned char *)&buffer, sizeof(int));
    }
    loopCount++;
  }
  if (ptrace(PTRACE_DETACH, pid, NULL, NULL)) {
    perror("PTRACE_DETACH");
    return (0);
  }
  return 1;
}


x86 assembly (clang, -O0):
<ReadProcessMemory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1050 <ptrace@plt>
cmp    $0x0,%eax
je     11b2 <ReadProcessMemory+0x62>
lea    0xe5f(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    130b <ReadProcessMemory+0x1bb>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <wait@plt>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
cmp    %rcx,%rax
jae    12cb <ReadProcessMemory+0x17b>
mov    0x2dfd(%rip),%rax
movq   $0x0,(%rax)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1050 <ptrace@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1267 <ReadProcessMemory+0x117>
mov    0x2dca(%rip),%rax
cmpq   $0x0,(%rax)
je     1267 <ReadProcessMemory+0x117>
lea    0xdf7(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xdf5(%rip),%rdi
call   1030 <printf@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1050 <ptrace@plt>
cmp    $0x0,%eax
je     125b <ReadProcessMemory+0x10b>
lea    0xdcd(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    130b <ReadProcessMemory+0x1bb>
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jle    129f <ReadProcessMemory+0x14f>
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
jmp    12b1 <ReadProcessMemory+0x161>
mov    -0x20(%rbp),%rdi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x10(%rbp)
jmp    11bb <ReadProcessMemory+0x6b>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1050 <ptrace@plt>
cmp    $0x0,%eax
je     1304 <ReadProcessMemory+0x1b4>
lea    0xd30(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    130b <ReadProcessMemory+0x1bb>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadProcessMemory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %rdx,%r12
mov    %rsi,%r13
mov    %edi,%r14d
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
xor    %r15d,%r15d
mov    %r14d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <ptrace@plt>
test   %eax,%eax
jne    123b <ReadProcessMemory+0xeb>
add    %r13d,%r12d
xor    %edi,%edi
call   1070 <wait@plt>
movslq %r12d,%r12
cmp    %r13,%r12
jbe    120a <ReadProcessMemory+0xba>
xor    %ebx,%ebx
mov    0x2e2e(%rip),%r15
jmp    11d3 <ReadProcessMemory+0x83>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rdi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
lea    (%rbx,%r13,1),%rax
add    $0x4,%rax
add    $0x4,%rbx
cmp    %r12,%rax
jae    120a <ReadProcessMemory+0xba>
lea    (%rbx,%r13,1),%rdx
movq   $0x0,(%r15)
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
mov    %r14d,%esi
xor    %ecx,%ecx
call   1050 <ptrace@plt>
mov    %eax,0x4(%rsp)
cmp    $0xffffffff,%eax
jne    1200 <ReadProcessMemory+0xb0>
cmpq   $0x0,(%r15)
jne    125a <ReadProcessMemory+0x10a>
test   %ebx,%ebx
jle    11b0 <ReadProcessMemory+0x60>
add    $0x4,%rbp
jmp    11b0 <ReadProcessMemory+0x60>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
mov    %r14d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <ptrace@plt>
mov    $0x1,%r15d
test   %eax,%eax
jne    1249 <ReadProcessMemory+0xf9>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbe(%rip),%rdi
call   1060 <perror@plt>
jmp    1229 <ReadProcessMemory+0xd9>
lea    0xdd3(%rip),%rdi
call   1060 <perror@plt>
xor    %r15d,%r15d
jmp    1229 <ReadProcessMemory+0xd9>
lea    0xdad(%rip),%rdi
call   1060 <perror@plt>
add    %ebx,%r13d
lea    0xdae(%rip),%rdi
mov    %r13d,%esi
call   1030 <printf@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
xor    %r15d,%r15d
mov    %r14d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <ptrace@plt>
test   %eax,%eax
je     1229 <ReadProcessMemory+0xd9>
lea    0xd88(%rip),%rdi
call   1060 <perror@plt>
jmp    1229 <ReadProcessMemory+0xd9>

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

LLVM IR (-O2):
@PTRACE_ATTACH = global i32 0
@.str = [14 x i8] c"PTRACE_ATTACH\00"
@errno = global i64 0
@PTRACE_PEEKDATA = global i32 0
@.str.1 = [16 x i8] c"PTRACE_PEEKDATA\00"
@.str.2 = [5 x i8] c"%02X\00"
@PTRACE_DETACH = global i32 0
@.str.3 = [14 x i8] c"PTRACE_DETACH\00"
define i32 @ReadProcessMemory(i32 %0, i64 %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = load i32, i32* @PTRACE_ATTACH
%8 = tail call i32 @ptrace(i32 %7, i32 %0, i8* null, i32* null)
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %4
%11 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %57
12: ; preds = %4
%13 = add i64 %2, %1
%14 = tail call i32 @wait(i32* null)
%15 = shl i64 %13, 32
%16 = ashr exact i64 %15, 32
%17 = icmp ugt i64 %16, %1
br i1 %17, label %18, label %51
18: ; preds = %12, %46
%19 = phi i32 [ %48, %46 ], [ 0, %12 ]
%20 = phi i64 [ %49, %46 ], [ %1, %12 ]
%21 = phi i8* [ %47, %46 ], [ %3, %12 ]
store i64 0, i64* @errno
%22 = load i32, i32* @PTRACE_PEEKDATA
%23 = inttoptr i64 %20 to i8*
%24 = call i32 @ptrace(i32 %22, i32 %0, i8* %23, i32* null)
store i32 %24, i32* %5
%25 = icmp eq i32 %24, -1
%26 = load i64, i64* @errno
%27 = icmp ne i64 %26, 0
%28 = select i1 %25, i1 %27, i1 false
br i1 %28, label %29, label %38
29: ; preds = %18
%30 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%31 = trunc i64 %20 to i32
%32 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @PTRACE_DETACH
%34 = call i32 @ptrace(i32 %33, i32 %0, i8* null, i32* null)
%35 = icmp eq i32 %34, 0
br i1 %35, label %57, label %36
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %57
38: ; preds = %18
%39 = shl i32 %19, 2
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = getelementptr inbounds i8, i8* %21, i64 4
%43 = call i32 @memcpy(i8* %42, i8* %6, i32 4)
br label %46
44: ; preds = %38
%45 = call i32 @memcpy(i8* %21, i8* %6, i32 4)
br label %46
46: ; preds = %44, %41
%47 = phi i8* [ %42, %41 ], [ %21, %44 ]
%48 = add nuw nsw i32 %19, 1
%49 = add i64 %20, 4
%50 = icmp ult i64 %49, %16
br i1 %50, label %18, label %51
51: ; preds = %46, %12
%52 = load i32, i32* @PTRACE_DETACH
%53 = call i32 @ptrace(i32 %52, i32 %0, i8* null, i32* null)
%54 = icmp eq i32 %53, 0
br i1 %54, label %57, label %55
55: ; preds = %51
%56 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %57
57: ; preds = %51, %29, %36, %55, %10
%58 = phi i32 [ 0, %10 ], [ 0, %55 ], [ 0, %36 ], [ 0, %29 ], [ 1, %51 ]
ret i32 %58
}