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

/* Variables and functions */
int /*<<< orphan*/ PTRACE_PEEKTEXT;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
long ptrace(int /*<<< orphan*/, int, unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void ptrace_read(int pid, unsigned long addr, void *vptr, int len) {
  int bytesRead = 0;
  int i = 0;
  long word = 0;
  long *ptr = (long *)vptr;
  while (bytesRead < len) {
    word = ptrace(PTRACE_PEEKTEXT, pid, addr + bytesRead, NULL);
    if (word == -1) {
      fprintf(stderr, "ptrace(PTRACE_PEEKTEXT) failed\n");
      exit(1);
    }
    bytesRead += sizeof(word);
    ptr[i++] = word;
  }
}


x86 assembly (clang, -O0):
<ptrace_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11e8 <ptrace_read+0xb8>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
movslq -0x20(%rbp),%rax
add    %rax,%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    11be <ptrace_read+0x8e>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
movslq -0x20(%rbp),%rax
add    $0x8,%rax
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    1164 <ptrace_read+0x34>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptrace_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %ecx,%ecx
jle    1186 <ptrace_read+0x56>
mov    %ecx,%r12d
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
mov    0x2e85(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x0(%r13),%edi
movslq %ebx,%rdx
add    %r15,%rdx
mov    %ebp,%esi
xor    %ecx,%ecx
call   1040 <ptrace@plt>
cmp    $0xffffffffffffffff,%rax
je     1195 <ptrace_read+0x65>
mov    %rax,(%r14,%rbx,1)
add    $0x8,%rbx
cmp    %r12d,%ebx
jl     1160 <ptrace_read+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@PTRACE_PEEKTEXT = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"ptrace(PTRACE_PEEKTEXT) failed\0A\00"
define void @ptrace_read(i32 %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64*
store i32 %0, i32* %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
store i64 0, i64* %11
%13 = load i8*, i8** %7
%14 = bitcast i8* %13 to i64*
store i64* %14, i64** %12
br label %15
15: ; preds = %33, %4
%16 = load i32, i32* %9
%17 = load i32, i32* %8
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %44
19: ; preds = %15
%20 = load i32, i32* @PTRACE_PEEKTEXT
%21 = load i32, i32* %5
%22 = load i64, i64* %6
%23 = load i32, i32* %9
%24 = sext i32 %23 to i64
%25 = add i64 %22, %24
%26 = call i64 @ptrace(i32 %20, i32 %21, i64 %25, i32* null)
store i64 %26, i64* %11
%27 = load i64, i64* %11
%28 = icmp eq i64 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %19
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %19
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = add i64 %35, 8
%37 = trunc i64 %36 to i32
store i32 %37, i32* %9
%38 = load i64, i64* %11
%39 = load i64*, i64** %12
%40 = load i32, i32* %10
%41 = add nsw i32 %40, 1
store i32 %41, i32* %10
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i64, i64* %39, i64 %42
store i64 %38, i64* %43
br label %15
44: ; preds = %15
ret void
}

LLVM IR (-O2):
@PTRACE_PEEKTEXT = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"ptrace(PTRACE_PEEKTEXT) failed\0A\00"
define void @ptrace_read(i32 %0, i64 %1, i8* nocapture writeonly %2, i32 %3) {
%5 = bitcast i8* %2 to i64*
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %24
7: ; preds = %4, %19
%8 = phi i64 [ %21, %19 ], [ 0, %4 ]
%9 = phi i32 [ %20, %19 ], [ 0, %4 ]
%10 = load i32, i32* @PTRACE_PEEKTEXT
%11 = sext i32 %9 to i64
%12 = add i64 %11, %1
%13 = tail call i64 @ptrace(i32 %10, i32 %0, i64 %12, i32* null)
%14 = icmp eq i64 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %7
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %7
%20 = add i32 %9, 8
%21 = add nuw i64 %8, 1
%22 = getelementptr inbounds i64, i64* %5, i64 %8
store i64 %13, i64* %22
%23 = icmp slt i32 %20, %3
br i1 %23, label %7, label %24
24: ; preds = %19, %4
ret void
}