#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_POKETEXT;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ logs(int, char *, char *, int /*<<< orphan*/);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
scalar_t__ ptrace(int /*<<< orphan*/, int /*<<< orphan*/, size_t, size_t);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int poke_text(pid_t pid, size_t addr, char *buf, size_t blen) {
  int i = 0;
  char *ptr = malloc(blen + blen % sizeof(size_t));
  memcpy(ptr, buf, blen);
  for (i = 0; i < blen; i += sizeof(size_t)) {
    if (ptrace(PTRACE_POKETEXT, pid, addr + i, *(size_t *)&ptr[i]) < 0) {
      logs(2, "%s: %s", "ptrace POKE", strerror(errno));
      exit(1);
    }
  }
  free(ptr);
  return 0;
}


x86 assembly (clang, -O0):
<poke_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)
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rax
and    $0x7,%rax
add    %rax,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1245 <poke_text+0xd5>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
movslq -0x24(%rbp),%rax
add    %rax,%rdx
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,1),%rcx
call   1060 <ptrace@plt>
cmp    $0x0,%rax
jge    1230 <poke_text+0xc0>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%ecx
mov    $0x2,%edi
lea    0xde6(%rip),%rsi
lea    0xde6(%rip),%rdx
call   1070 <logs@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1235 <poke_text+0xc5>
movslq -0x24(%rbp),%rax
add    $0x8,%rax
mov    %eax,-0x24(%rbp)
jmp    11be <poke_text+0x4e>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<poke_text>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r13
mov    %rdx,%r12
mov    %rsi,%r14
mov    %edi,%r15d
mov    %r13d,%edi
and    $0x7,%edi
add    %rcx,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1040 <memcpy@plt>
test   %r13,%r13
je     11e5 <poke_text+0x75>
xor    %ebx,%ebx
mov    0x2e24(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r12),%edi
lea    (%rbx,%r14,1),%rdx
mov    0x0(%rbp,%rbx,1),%rcx
mov    %r15d,%esi
call   1060 <ptrace@plt>
test   %rax,%rax
js     11fe <poke_text+0x8e>
add    $0x8,%ebx
movslq %ebx,%rbx
cmp    %r13,%rbx
jb     11c0 <poke_text+0x50>
mov    %rbp,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xded(%rip),%rsi
lea    0xded(%rip),%rdx
mov    $0x2,%edi
mov    %eax,%ecx
call   1070 <logs@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@PTRACE_POKETEXT = global i32 0
@.str = [7 x i8] c"%s: %s\00"
@.str.1 = [12 x i8] c"ptrace POKE\00"
@errno = global i32 0
define i32 @poke_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 i8*
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
%11 = load i64, i64* %8
%12 = load i64, i64* %8
%13 = urem i64 %12, 8
%14 = add i64 %11, %13
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %10
%16 = load i8*, i8** %10
%17 = load i8*, i8** %7
%18 = load i64, i64* %8
%19 = call i32 @memcpy(i8* %16, i8* %17, i64 %18)
store i32 0, i32* %9
br label %20
20: ; preds = %46, %4
%21 = load i32, i32* %9
%22 = sext i32 %21 to i64
%23 = load i64, i64* %8
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %51
25: ; preds = %20
%26 = load i32, i32* @PTRACE_POKETEXT
%27 = load i32, i32* %5
%28 = load i64, i64* %6
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = add i64 %28, %30
%32 = load i8*, i8** %10
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = bitcast i8* %35 to i64*
%37 = load i64, i64* %36
%38 = call i64 @ptrace(i32 %26, i32 %27, i64 %31, i64 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %25
%41 = load i32, i32* @errno
%42 = call i32 @strerror(i32 %41)
%43 = call i32 @logs(i32 2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %25
br label %46
46: ; preds = %45
%47 = load i32, i32* %9
%48 = sext i32 %47 to i64
%49 = add i64 %48, 8
%50 = trunc i64 %49 to i32
store i32 %50, i32* %9
br label %20
51: ; preds = %20
%52 = load i8*, i8** %10
%53 = call i32 @free(i8* %52)
ret i32 0
}

LLVM IR (-O2):
@PTRACE_POKETEXT = global i32 0
@.str = [7 x i8] c"%s: %s\00"
@.str.1 = [12 x i8] c"ptrace POKE\00"
@errno = global i32 0
define i32 @poke_text(i32 %0, i64 %1, i8* %2, i64 %3) {
%5 = and i64 %3, 7
%6 = add i64 %5, %3
%7 = tail call i8* @malloc(i64 %6)
%8 = tail call i32 @memcpy(i8* %7, i8* %2, i64 %3)
%9 = icmp eq i64 %3, 0
br i1 %9, label %29, label %15
10: ; preds = %15
%11 = shl nsw i64 %16, 32
%12 = add i64 %11, 34359738368
%13 = ashr exact i64 %12, 32
%14 = icmp ult i64 %13, %3
br i1 %14, label %15, label %29
15: ; preds = %4, %10
%16 = phi i64 [ %13, %10 ], [ 0, %4 ]
%17 = load i32, i32* @PTRACE_POKETEXT
%18 = add i64 %16, %1
%19 = getelementptr inbounds i8, i8* %7, i64 %16
%20 = bitcast i8* %19 to i64*
%21 = load i64, i64* %20
%22 = tail call i64 @ptrace(i32 %17, i32 %0, i64 %18, i64 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %10
24: ; preds = %15
%25 = load i32, i32* @errno
%26 = tail call i32 @strerror(i32 %25)
%27 = tail call i32 @logs(i32 2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %10, %4
%30 = tail call i32 @free(i8* %7)
ret i32 0
}