#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_PEEKTEXT;
int /*<<< orphan*/ PTRACE_POKETEXT;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
scalar_t__ ptrace(int /*<<< orphan*/, int /*<<< orphan*/, unsigned int,
                  unsigned int);
int change_process_memory(pid_t pid, unsigned from_addr, int size, char *mem) {
  for (int i = 0; i < size; i++) {
    unsigned word;
    if ((int)(word = ptrace(PTRACE_PEEKTEXT, pid, from_addr + i, 0)) == -1 &&
        errno != 0) {
      perror("ptrace3");
      return -1;
    }
    if (ptrace(PTRACE_POKETEXT, pid, from_addr + i, (word & ~(0xFF)) | mem[i]) <
        0) {
      perror("ptrace");
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<change_process_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11fa <change_process_memory+0xda>
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
add    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <ptrace@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1196 <change_process_memory+0x76>
mov    0x2e6c(%rip),%rax
cmpq   $0x0,(%rax)
je     1196 <change_process_memory+0x76>
lea    0xe7b(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1201 <change_process_memory+0xe1>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
add    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
and    $0xffffff00,%ecx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%r8
movsbl (%rax,%r8,1),%eax
or     %eax,%ecx
call   1030 <ptrace@plt>
cmp    $0x0,%rax
jge    11e7 <change_process_memory+0xc7>
lea    0xe32(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1201 <change_process_memory+0xe1>
jmp    11ec <change_process_memory+0xcc>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    113c <change_process_memory+0x1c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_process_memory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    1196 <change_process_memory+0x76>
mov    %rcx,%r15
mov    %edi,%ebp
mov    %edx,%r12d
mov    %esi,%r13d
xor    %r14d,%r14d
nopl   (%rax)
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
lea    (%r14,%r13,1),%rbx
mov    %ebp,%esi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1030 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    116a <change_process_memory+0x4a>
mov    0x2e7c(%rip),%rcx
cmpq   $0x0,(%rcx)
jne    11a7 <change_process_memory+0x87>
mov    0x2e67(%rip),%rcx
mov    (%rcx),%edi
and    $0xffffff00,%eax
movsbl (%r15,%r14,1),%ecx
or     %eax,%ecx
mov    %ebp,%esi
mov    %ebx,%edx
call   1030 <ptrace@plt>
test   %rax,%rax
js     11b0 <change_process_memory+0x90>
add    $0x1,%r14
cmp    %r14,%r12
jne    1140 <change_process_memory+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe52(%rip),%rdi
jmp    11b7 <change_process_memory+0x97>
lea    0xe51(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
jmp    1198 <change_process_memory+0x78>

LLVM IR (-O0):
@PTRACE_PEEKTEXT = global i32 0
@errno = global i64 0
@.str = [8 x i8] c"ptrace3\00"
@PTRACE_POKETEXT = global i32 0
@.str.1 = [7 x i8] c"ptrace\00"
define i32 @change_process_memory(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 0, i32* %10
br label %12
12: ; preds = %50, %4
%13 = load i32, i32* %10
%14 = load i32, i32* %8
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %53
16: ; preds = %12
%17 = load i32, i32* @PTRACE_PEEKTEXT
%18 = load i32, i32* %6
%19 = load i32, i32* %7
%20 = load i32, i32* %10
%21 = add i32 %19, %20
%22 = call i64 @ptrace(i32 %17, i32 %18, i32 %21, i32 0)
%23 = trunc i64 %22 to i32
store i32 %23, i32* %11
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %16
%26 = load i64, i64* @errno
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %54
30: ; preds = %25, %16
%31 = load i32, i32* @PTRACE_POKETEXT
%32 = load i32, i32* %6
%33 = load i32, i32* %7
%34 = load i32, i32* %10
%35 = add i32 %33, %34
%36 = load i32, i32* %11
%37 = and i32 %36, -256
%38 = load i8*, i8** %9
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = or i32 %37, %43
%45 = call i64 @ptrace(i32 %31, i32 %32, i32 %35, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %30
%48 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %54
49: ; preds = %30
br label %50
50: ; preds = %49
%51 = load i32, i32* %10
%52 = add nsw i32 %51, 1
store i32 %52, i32* %10
br label %12
53: ; preds = %12
store i32 0, i32* %5
br label %54
54: ; preds = %53, %47, %28
%55 = load i32, i32* %5
ret i32 %55
}

LLVM IR (-O2):
@PTRACE_PEEKTEXT = global i32 0
@errno = global i64 0
@.str = [8 x i8] c"ptrace3\00"
@PTRACE_POKETEXT = global i32 0
@.str.1 = [7 x i8] c"ptrace\00"
define i32 @change_process_memory(i32 %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %34
6: ; preds = %4
%7 = zext i32 %2 to i64
br label %10
8: ; preds = %21
%9 = icmp eq i64 %30, %7
br i1 %9, label %34, label %10
10: ; preds = %6, %8
%11 = phi i64 [ 0, %6 ], [ %30, %8 ]
%12 = load i32, i32* @PTRACE_PEEKTEXT
%13 = trunc i64 %11 to i32
%14 = add i32 %13, %1
%15 = tail call i64 @ptrace(i32 %12, i32 %0, i32 %14, i32 0)
%16 = trunc i64 %15 to i32
%17 = icmp eq i32 %16, -1
%18 = load i64, i64* @errno
%19 = icmp ne i64 %18, 0
%20 = select i1 %17, i1 %19, i1 false
br i1 %20, label %31, label %21
21: ; preds = %10
%22 = load i32, i32* @PTRACE_POKETEXT
%23 = and i32 %16, -256
%24 = getelementptr inbounds i8, i8* %3, i64 %11
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = or i32 %23, %26
%28 = tail call i64 @ptrace(i32 %22, i32 %0, i32 %14, i32 %27)
%29 = icmp slt i64 %28, 0
%30 = add nuw nsw i64 %11, 1
br i1 %29, label %31, label %8
31: ; preds = %21, %10
%32 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %10 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %21 ]
%33 = tail call i32 @perror(i8* %32)
br label %34
34: ; preds = %8, %31, %4
%35 = phi i32 [ 0, %4 ], [ -1, %31 ], [ 0, %8 ]
ret i32 %35
}