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

/* Variables and functions */
int /*<<< orphan*/ PTRACE_PEEKTEXT;
int /*<<< orphan*/ PTRACE_POKETEXT;
scalar_t__ errno;
int /*<<< orphan*/ memmove(void *, long *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
long ptrace(int /*<<< orphan*/, int /*<<< orphan*/, void *, void *);
int poke_text(pid_t pid, void *where, void *new_text, void *old_text,
              size_t len) {
  if (len % sizeof(void *) != 0) {
    printf("invalid len, not a multiple of %zd\n", sizeof(void *));
    return -1;
  }
  long poke_data;
  for (size_t copied = 0; copied < len; copied += sizeof(poke_data)) {
    memmove(&poke_data, new_text + copied, sizeof(poke_data));
    if (old_text != NULL) {
      errno = 0;
      long peek_data = ptrace(PTRACE_PEEKTEXT, pid, where + copied, NULL);
      if (peek_data == -1 && errno) {
        perror("PTRACE_PEEKTEXT");
        return -1;
      }
      memmove(old_text + copied, &peek_data, sizeof(peek_data));
    }
    if (ptrace(PTRACE_POKETEXT, pid, where + copied, (void *)poke_data) < 0) {
      perror("PTRACE_POKETEXT");
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<poke_text>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x28(%rbp),%rax
and    $0x7,%rax
cmp    $0x0,%rax
je     118a <poke_text+0x4a>
lea    0xe8c(%rip),%rdi
mov    $0x8,%esi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <poke_text+0x156>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    128f <poke_text+0x14f>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    -0x38(%rbp),%rsi
mov    $0x8,%edx
call   1050 <memmove@plt>
cmpq   $0x0,-0x20(%rbp)
je     123a <poke_text+0xfa>
mov    0x2e18(%rip),%rax
movq   $0x0,(%rax)
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
add    -0x38(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0xffffffffffffffff,-0x40(%rbp)
jne    1224 <poke_text+0xe4>
mov    0x2dde(%rip),%rax
cmpq   $0x0,(%rax)
je     1224 <poke_text+0xe4>
lea    0xe11(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <poke_text+0x156>
mov    -0x20(%rbp),%rdi
add    -0x38(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x8,%edx
call   1050 <memmove@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
add    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%rcx
call   1040 <ptrace@plt>
cmp    $0x0,%rax
jge    1279 <poke_text+0x139>
lea    0xdcc(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <poke_text+0x156>
jmp    127e <poke_text+0x13e>
mov    -0x38(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x38(%rbp)
jmp    1192 <poke_text+0x52>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<poke_text>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%rbp
test   $0x7,%bpl
je     1172 <poke_text+0x32>
lea    0xea2(%rip),%rdi
mov    $0x8,%esi
call   1030 <printf@plt>
mov    $0xffffffff,%eax
jmp    122a <poke_text+0xea>
test   %rbp,%rbp
je     1228 <poke_text+0xe8>
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%r14
mov    %edi,%ebx
xor    %r13d,%r13d
mov    %rsi,(%rsp)
nopl   (%rax)
lea    (%r12,%r13,1),%rsi
lea    0x10(%rsp),%rdi
mov    $0x8,%edx
call   1050 <memmove@plt>
test   %r15,%r15
je     11fd <poke_text+0xbd>
mov    %r14,%rax
mov    %ebx,%esi
mov    %r15,%r14
mov    0x2e29(%rip),%r15
movq   $0x0,(%r15)
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%edi
lea    (%rax,%r13,1),%rdx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %rax,0x8(%rsp)
cmp    $0xffffffffffffffff,%rax
jne    11e3 <poke_text+0xa3>
cmpq   $0x0,(%r15)
jne    1242 <poke_text+0x102>
mov    %r14,%r15
lea    (%r14,%r13,1),%rdi
lea    0x8(%rsp),%rsi
mov    $0x8,%edx
call   1050 <memmove@plt>
mov    (%rsp),%r14
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    (%r14,%r13,1),%rdx
mov    0x10(%rsp),%rcx
mov    %ebx,%esi
call   1040 <ptrace@plt>
test   %rax,%rax
js     1239 <poke_text+0xf9>
add    $0x8,%r13
cmp    %rbp,%r13
jb     1190 <poke_text+0x50>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf4(%rip),%rdi
jmp    1249 <poke_text+0x109>
lea    0xddb(%rip),%rdi
call   1060 <perror@plt>
jmp    1168 <poke_text+0x28>

LLVM IR (-O0):
@.str = [36 x i8] c"invalid len, not a multiple of %zd\0A\00"
@errno = global i64 0
@PTRACE_PEEKTEXT = global i32 0
@.str.1 = [16 x i8] c"PTRACE_PEEKTEXT\00"
@PTRACE_POKETEXT = global i32 0
@.str.2 = [16 x i8] c"PTRACE_POKETEXT\00"
define i32 @poke_text(i32 %0, i8* %1, i8* %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
store i32 %0, i32* %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i64 %4, i64* %11
%15 = load i64, i64* %11
%16 = urem i64 %15, 8
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %5
%19 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 8)
store i32 -1, i32* %6
br label %70
20: ; preds = %5
store i64 0, i64* %13
br label %21
21: ; preds = %66, %20
%22 = load i64, i64* %13
%23 = load i64, i64* %11
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %69
25: ; preds = %21
%26 = bitcast i64* %12 to i8*
%27 = load i8*, i8** %9
%28 = load i64, i64* %13
%29 = getelementptr i8, i8* %27, i64 %28
%30 = bitcast i8* %29 to i64*
%31 = call i32 @memmove(i8* %26, i64* %30, i32 8)
%32 = load i8*, i8** %10
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %53
34: ; preds = %25
store i64 0, i64* @errno
%35 = load i32, i32* @PTRACE_PEEKTEXT
%36 = load i32, i32* %7
%37 = load i8*, i8** %8
%38 = load i64, i64* %13
%39 = getelementptr i8, i8* %37, i64 %38
%40 = call i64 @ptrace(i32 %35, i32 %36, i8* %39, i8* null)
store i64 %40, i64* %14
%41 = load i64, i64* %14
%42 = icmp eq i64 %41, -1
br i1 %42, label %43, label %48
43: ; preds = %34
%44 = load i64, i64* @errno
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %70
48: ; preds = %43, %34
%49 = load i8*, i8** %10
%50 = load i64, i64* %13
%51 = getelementptr i8, i8* %49, i64 %50
%52 = call i32 @memmove(i8* %51, i64* %14, i32 8)
br label %53
53: ; preds = %48, %25
%54 = load i32, i32* @PTRACE_POKETEXT
%55 = load i32, i32* %7
%56 = load i8*, i8** %8
%57 = load i64, i64* %13
%58 = getelementptr i8, i8* %56, i64 %57
%59 = load i64, i64* %12
%60 = inttoptr i64 %59 to i8*
%61 = call i64 @ptrace(i32 %54, i32 %55, i8* %58, i8* %60)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %53
%64 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %6
br label %70
65: ; preds = %53
br label %66
66: ; preds = %65
%67 = load i64, i64* %13
%68 = add i64 %67, 8
store i64 %68, i64* %13
br label %21
69: ; preds = %21
store i32 0, i32* %6
br label %70
70: ; preds = %69, %63, %46, %18
%71 = load i32, i32* %6
ret i32 %71
}

LLVM IR (-O2):
@.str = [36 x i8] c"invalid len, not a multiple of %zd\0A\00"
@errno = global i64 0
@PTRACE_PEEKTEXT = global i32 0
@.str.1 = [16 x i8] c"PTRACE_PEEKTEXT\00"
@PTRACE_POKETEXT = global i32 0
@.str.2 = [16 x i8] c"PTRACE_POKETEXT\00"
define i32 @poke_text(i32 %0, i8* %1, i8* %2, i8* %3, i64 %4) {
%6 = alloca i64
%7 = alloca i64
%8 = and i64 %4, 7
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %5
%11 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 8)
br label %50
12: ; preds = %5
%13 = bitcast i64* %6 to i8*
%14 = icmp eq i64 %4, 0
br i1 %14, label %48, label %15
15: ; preds = %12
%16 = icmp eq i8* %3, null
%17 = bitcast i64* %7 to i8*
br label %20
18: ; preds = %38
%19 = icmp ult i64 %45, %4
br i1 %19, label %20, label %48
20: ; preds = %15, %18
%21 = phi i64 [ 0, %15 ], [ %45, %18 ]
%22 = getelementptr i8, i8* %2, i64 %21
%23 = bitcast i8* %22 to i64*
%24 = call i32 @memmove(i8* %13, i64* %23, i32 8)
br i1 %16, label %38, label %25
25: ; preds = %20
store i64 0, i64* @errno
%26 = load i32, i32* @PTRACE_PEEKTEXT
%27 = getelementptr i8, i8* %1, i64 %21
%28 = call i64 @ptrace(i32 %26, i32 %0, i8* %27, i8* null)
store i64 %28, i64* %7
%29 = icmp eq i64 %28, -1
%30 = load i64, i64* @errno
%31 = icmp ne i64 %30, 0
%32 = select i1 %29, i1 %31, i1 false
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %48
35: ; preds = %25
%36 = getelementptr i8, i8* %3, i64 %21
%37 = call i32 @memmove(i8* %36, i64* %7, i32 8)
br label %38
38: ; preds = %35, %20
%39 = load i32, i32* @PTRACE_POKETEXT
%40 = getelementptr i8, i8* %1, i64 %21
%41 = load i64, i64* %6
%42 = inttoptr i64 %41 to i8*
%43 = call i64 @ptrace(i32 %39, i32 %0, i8* %40, i8* %42)
%44 = icmp slt i64 %43, 0
%45 = add i64 %21, 8
br i1 %44, label %46, label %18
46: ; preds = %38
%47 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %18, %12, %33, %46
%49 = phi i32 [ -1, %46 ], [ -1, %33 ], [ 0, %12 ], [ 0, %18 ]
br label %50
50: ; preds = %48, %10
%51 = phi i32 [ -1, %10 ], [ %49, %48 ]
ret i32 %51
}