#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_POKETEXT;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memcpy(long *, void *, int);
long ptrace(int /*<<< orphan*/, int, unsigned long, long);
int /*<<< orphan*/ stderr;
void ptrace_write(int pid, unsigned long addr, void *vptr, int len) {
  int byteCount = 0;
  long word = 0;
  while (byteCount < len) {
    memcpy(&word, vptr + byteCount, sizeof(word));
    word = ptrace(PTRACE_POKETEXT, pid, addr + byteCount, word);
    if (word == -1) {
      fprintf(stderr, "ptrace(PTRACE_POKETEXT) failed\n");
      exit(1);
    }
    byteCount += sizeof(word);
  }
}


x86 assembly (clang, -O0):
<ptrace_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11e8 <ptrace_write+0xa8>
mov    -0x18(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
lea    -0x28(%rbp),%rdi
mov    $0x8,%edx
call   1040 <memcpy@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
movslq -0x20(%rbp),%rax
add    %rax,%rdx
mov    -0x28(%rbp),%rcx
call   1050 <ptrace@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11d8 <ptrace_write+0x98>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xe37(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
movslq -0x20(%rbp),%rax
add    $0x8,%rax
mov    %eax,-0x20(%rbp)
jmp    1165 <ptrace_write+0x25>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptrace_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movq   $0x0,(%rsp)
test   %ecx,%ecx
jle    11b2 <ptrace_write+0x72>
mov    %ecx,%ebx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r13d
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nop
movslq %r12d,%rbp
lea    (%r14,%rbp,1),%rsi
mov    %rsp,%rdi
mov    $0x8,%edx
call   1040 <memcpy@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
add    %r15,%rbp
mov    (%rsp),%rcx
mov    %r13d,%esi
mov    %rbp,%rdx
call   1050 <ptrace@plt>
mov    %rax,(%rsp)
cmp    $0xffffffffffffffff,%rax
je     11c1 <ptrace_write+0x81>
add    $0x8,%r12d
cmp    %ebx,%r12d
jl     1170 <ptrace_write+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

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

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