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

/* Variables and functions */
int /*<<< orphan*/ PTRACE_PEEKDATA;
int /*<<< orphan*/ PTRACE_POKEDATA;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ memcpy(intptr_t *, int /*<<< orphan*/ const *, size_t);
int /*<<< orphan*/ perror(char *);
int ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/ *, ...);
int memcpy_to_pid(pid_t pid, void *dst, const void *src, size_t size) {
  uint8_t *pdst = (uint8_t *)dst;
  const uint8_t *psrc = (const uint8_t *)src;
  size_t i;
  for (i = 0; i + sizeof(intptr_t) <= size; i += sizeof(intptr_t)) {
    intptr_t bytes;
    memcpy(&bytes, &psrc[i], sizeof(intptr_t));
    if (ptrace(PTRACE_POKEDATA, pid, &pdst[i], bytes) == -1) {
      perror("ptrace(POKEDATA)");
      return -1;
    }
  }
  if (i < size) {
    intptr_t bytes;
    if (ptrace(PTRACE_PEEKDATA, pid, &pdst[i], &bytes) == -1) {
      perror("ptrace(PEEKDATA)");
      return -1;
    }
    assert(i + sizeof(intptr_t) > size);
    memcpy(&bytes, &psrc[i], size - i);
    if (ptrace(PTRACE_POKEDATA, pid, &pdst[i], bytes) == -1) {
      perror("ptrace(POKEDATA)");
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<memcpy_to_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0x8,%rax
cmp    -0x20(%rbp),%rax
ja     11fb <memcpy_to_pid+0xbb>
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    -0x40(%rbp),%rdi
mov    $0x8,%edx
call   1040 <memcpy@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x38(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
mov    -0x40(%rbp),%rcx
mov    $0x0,%al
call   1050 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    11e5 <memcpy_to_pid+0xa5>
lea    0xe2c(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <memcpy_to_pid+0x19c>
jmp    11ea <memcpy_to_pid+0xaa>
mov    -0x38(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
jmp    116f <memcpy_to_pid+0x2f>
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    12d5 <memcpy_to_pid+0x195>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x38(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    -0x48(%rbp),%rcx
mov    $0x0,%al
call   1050 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    1250 <memcpy_to_pid+0x110>
lea    0xdd2(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <memcpy_to_pid+0x19c>
mov    -0x38(%rbp),%rax
add    $0x8,%rax
cmp    -0x20(%rbp),%rax
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
sub    -0x38(%rbp),%rdx
lea    -0x48(%rbp),%rdi
call   1040 <memcpy@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x38(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
mov    -0x48(%rbp),%rcx
mov    $0x0,%al
call   1050 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    12d0 <memcpy_to_pid+0x190>
lea    0xd41(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <memcpy_to_pid+0x19c>
jmp    12d5 <memcpy_to_pid+0x195>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memcpy_to_pid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbx
mov    %edi,%ebp
cmp    $0x8,%rcx
mov    %rdx,0x8(%rsp)
mov    %rsi,0x10(%rsp)
jae    1168 <memcpy_to_pid+0x28>
xor    %r13d,%r13d
jmp    11ca <memcpy_to_pid+0x8a>
mov    %rsi,%r12
mov    %rdx,%r14
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x8,%edx
mov    %rsp,%rdi
mov    %r14,%rsi
call   1040 <memcpy@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    (%rsp),%rcx
mov    %ebp,%esi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <ptrace@plt>
cmp    $0xffffffff,%eax
je     124f <memcpy_to_pid+0x10f>
lea    0x8(%r15),%r13
add    $0x10,%r15
add    $0x20,%r14
add    $0x20,%r12
cmp    %rbx,%r15
mov    %r13,%r15
jbe    1180 <memcpy_to_pid+0x40>
cmp    %rbx,%r13
mov    $0x0,%r15d
jae    123d <memcpy_to_pid+0xfd>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rsp),%rax
lea    (%rax,%r13,4),%r14
mov    %rsp,%rcx
mov    %ebp,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <ptrace@plt>
cmp    $0xffffffff,%eax
je     1258 <memcpy_to_pid+0x118>
mov    $0x1,%edi
call   1030 <assert@plt>
mov    0x8(%rsp),%rax
lea    (%rax,%r13,4),%rsi
sub    %r13,%rbx
mov    %rsp,%rdi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    (%rsp),%rcx
xor    %r15d,%r15d
mov    %ebp,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <ptrace@plt>
cmp    $0xffffffff,%eax
je     124f <memcpy_to_pid+0x10f>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdaa(%rip),%rdi
jmp    125f <memcpy_to_pid+0x11f>
lea    0xdb2(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%r15d
jmp    123d <memcpy_to_pid+0xfd>

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

LLVM IR (-O2):
@PTRACE_POKEDATA = global i32 0
@.str = [17 x i8] c"ptrace(POKEDATA)\00"
@PTRACE_PEEKDATA = global i32 0
@.str.1 = [17 x i8] c"ptrace(PEEKDATA)\00"
define i32 @memcpy_to_pid(i32 %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca i64
%6 = alloca i64
%7 = bitcast i8* %1 to i32*
%8 = bitcast i8* %2 to i32*
%9 = icmp ult i64 %3, 8
br i1 %9, label %27, label %10
10: ; preds = %4
%11 = bitcast i64* %5 to i8*
br label %12
12: ; preds = %10, %24
%13 = phi i64 [ 8, %10 ], [ %25, %24 ]
%14 = phi i64 [ 0, %10 ], [ %13, %24 ]
%15 = getelementptr inbounds i32, i32* %8, i64 %14
%16 = call i32 @memcpy(i64* %5, i32* %15, i64 8)
%17 = load i32, i32* @PTRACE_POKEDATA
%18 = getelementptr inbounds i32, i32* %7, i64 %14
%19 = load i64, i64* %5
%20 = call i32 (i32, i32, i32*, ...) @ptrace(i32 %17, i32 %0, i32* %18, i64 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %12
%23 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %50
24: ; preds = %12
%25 = add i64 %13, 8
%26 = icmp ugt i64 %25, %3
br i1 %26, label %27, label %12
27: ; preds = %24, %4
%28 = phi i64 [ 0, %4 ], [ %13, %24 ]
%29 = icmp ult i64 %28, %3
br i1 %29, label %30, label %50
30: ; preds = %27
%31 = bitcast i64* %6 to i8*
%32 = load i32, i32* @PTRACE_PEEKDATA
%33 = getelementptr inbounds i32, i32* %7, i64 %28
%34 = call i32 (i32, i32, i32*, ...) @ptrace(i32 %32, i32 %0, i32* %33, i64* %6)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %50
38: ; preds = %30
%39 = call i32 @assert(i32 1)
%40 = getelementptr inbounds i32, i32* %8, i64 %28
%41 = sub i64 %3, %28
%42 = call i32 @memcpy(i64* %6, i32* %40, i64 %41)
%43 = load i32, i32* @PTRACE_POKEDATA
%44 = load i64, i64* %6
%45 = call i32 (i32, i32, i32*, ...) @ptrace(i32 %43, i32 %0, i32* %33, i64 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %38
%48 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %50
49: ; preds = %38
br label %50
50: ; preds = %22, %27, %49, %36, %47
%51 = phi i32 [ -1, %22 ], [ -1, %47 ], [ -1, %36 ], [ 0, %49 ], [ 0, %27 ]
ret i32 %51
}