#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;
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_PEEKTEXT;
int /*<<< orphan*/ memcpy(char *, size_t *, int);
size_t ptrace(int /*<<< orphan*/, int /*<<< orphan*/, size_t,
              int /*<<< orphan*/ *);
int peek_text(pid_t pid, size_t addr, char *buf, size_t blen) {
  int i = 0;
  size_t word = 0;
  for (i = 0; i < blen; i += sizeof(size_t)) {
    word = ptrace(PTRACE_PEEKTEXT, pid, addr + i, NULL);
    memcpy(&buf[i], &word, sizeof(word));
  }
  return 0;
}


x86 assembly (clang, -O0):
<peek_text>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11a8 <peek_text+0x88>
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
movslq -0x24(%rbp),%rax
add    %rax,%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
call   1030 <memcpy@plt>
movslq -0x24(%rbp),%rax
add    $0x8,%rax
mov    %eax,-0x24(%rbp)
jmp    114d <peek_text+0x2d>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<peek_text>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movq   $0x0,(%rsp)
test   %rcx,%rcx
je     1186 <peek_text+0x66>
mov    %rcx,%r13
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
mov    %rsp,%r12
nopl   0x0(%rax,%rax,1)
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
lea    (%rbx,%r15,1),%rdx
mov    %ebp,%esi
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %rax,(%rsp)
lea    (%r14,%rbx,1),%rdi
mov    %r12,%rsi
mov    $0x8,%edx
call   1030 <memcpy@plt>
add    $0x8,%ebx
movslq %ebx,%rbx
cmp    %r13,%rbx
jb     1150 <peek_text+0x30>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PTRACE_PEEKTEXT = global i32 0
define i32 @peek_text(i32 %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
store i32 %0, i32* %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i64 %3, i64* %8
store i32 0, i32* %9
store i64 0, i64* %10
store i32 0, i32* %9
br label %11
11: ; preds = %29, %4
%12 = load i32, i32* %9
%13 = sext i32 %12 to i64
%14 = load i64, i64* %8
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %34
16: ; preds = %11
%17 = load i32, i32* @PTRACE_PEEKTEXT
%18 = load i32, i32* %5
%19 = load i64, i64* %6
%20 = load i32, i32* %9
%21 = sext i32 %20 to i64
%22 = add i64 %19, %21
%23 = call i64 @ptrace(i32 %17, i32 %18, i64 %22, i32* null)
store i64 %23, i64* %10
%24 = load i8*, i8** %7
%25 = load i32, i32* %9
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = call i32 @memcpy(i8* %27, i64* %10, i32 8)
br label %29
29: ; preds = %16
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = add i64 %31, 8
%33 = trunc i64 %32 to i32
store i32 %33, i32* %9
br label %11
34: ; preds = %11
ret i32 0
}

LLVM IR (-O2):
@PTRACE_PEEKTEXT = global i32 0
define i32 @peek_text(i32 %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i64
%6 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%7 = icmp eq i64 %3, 0
br i1 %7, label %19, label %8
8: ; preds = %4, %8
%9 = phi i64 [ %17, %8 ], [ 0, %4 ]
%10 = load i32, i32* @PTRACE_PEEKTEXT
%11 = add i64 %9, %1
%12 = call i64 @ptrace(i32 %10, i32 %0, i64 %11, i32* null)
store i64 %12, i64* %5
%13 = getelementptr inbounds i8, i8* %2, i64 %9
%14 = call i32 @memcpy(i8* %13, i64* %5, i32 8)
%15 = shl nsw i64 %9, 32
%16 = add i64 %15, 34359738368
%17 = ashr exact i64 %16, 32
%18 = icmp ult i64 %17, %3
br i1 %18, label %8, label %19
19: ; preds = %8, %4
ret i32 0
}