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

/* Variables and functions */
scalar_t__ EIO;
int /*<<< orphan*/ PTRACE_POKEDATA;
size_t SSIZE_MAX;
scalar_t__ errno;
int /*<<< orphan*/ memcpy(long *, void *, size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ ptrace(int /*<<< orphan*/, int /*<<< orphan*/, void *, long);
int read_memory(int /*<<< orphan*/, void *, long *, int);
ssize_t write_memory(pid_t pid, void *addr, void *buf, size_t size) {
  size_t i, rem;
  long word;
  ssize_t r = -1;
  if (size > SSIZE_MAX)
    goto ret;
  i = 0;
  while (i < size) {
    rem = size - i < sizeof(word) ? size - i : sizeof(word);
    if (rem < sizeof(word) &&
        read_memory(pid, addr + i, &word, sizeof(word)) != sizeof(word))
      goto ret;
    memcpy(&word, buf + i, rem);
    errno = 0;
    ptrace(PTRACE_POKEDATA, pid, addr + i, word);
    if (errno)
      break;
    i += rem;
  }
  if (errno && errno != EIO) {
    perror("write_memory: ptrace");
    goto ret;
  }
  r = (ssize_t)i;
ret:
  return r;
}


x86 assembly (clang, -O0):
<write_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e7f(%rip),%rcx
cmp    (%rcx),%rax
jbe    1177 <write_memory+0x37>
jmp    12a5 <write_memory+0x165>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1262 <write_memory+0x122>
mov    -0x20(%rbp),%rax
sub    -0x28(%rbp),%rax
cmp    $0x8,%rax
jae    11b0 <write_memory+0x70>
mov    -0x20(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    11be <write_memory+0x7e>
mov    $0x8,%eax
mov    %rax,-0x48(%rbp)
jmp    11be <write_memory+0x7e>
mov    -0x48(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x8,-0x30(%rbp)
jae    11fb <write_memory+0xbb>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
lea    -0x38(%rbp),%rdx
mov    $0x8,%ecx
call   1050 <read_memory@plt>
cltq
cmp    $0x8,%rax
je     11fb <write_memory+0xbb>
jmp    12a5 <write_memory+0x165>
mov    -0x18(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rdi
call   1030 <memcpy@plt>
mov    0x2db1(%rip),%rax
movq   $0x0,(%rax)
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
add    -0x28(%rbp),%rdx
mov    -0x38(%rbp),%rcx
call   1040 <ptrace@plt>
mov    0x2d86(%rip),%rax
cmpq   $0x0,(%rax)
je     1251 <write_memory+0x111>
jmp    1262 <write_memory+0x122>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    117f <write_memory+0x3f>
mov    0x2d5f(%rip),%rax
cmpq   $0x0,(%rax)
je     129e <write_memory+0x15e>
mov    0x2d4e(%rip),%rax
mov    (%rax),%rax
mov    0x2d54(%rip),%rcx
cmp    (%rcx),%rax
je     129e <write_memory+0x15e>
lea    0xd6c(%rip),%rdi
call   1060 <perror@plt>
jmp    12a5 <write_memory+0x165>
mov    -0x28(%rbp),%rax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_memory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    0x2e90(%rip),%rcx
mov    $0xffffffff,%eax
cmp    %r14,(%rcx)
jb     122c <write_memory+0xec>
test   %r14,%r14
je     120b <write_memory+0xcb>
mov    %rdx,%r15
mov    %edi,0xc(%rsp)
mov    %rsi,%r12
xor    %r13d,%r13d
nopl   0x0(%rax)
mov    %r14,%rbx
sub    %r13,%rbx
cmp    $0x8,%rbx
mov    $0x8,%eax
cmovae %rax,%rbx
jae    11b5 <write_memory+0x75>
lea    (%r12,%r13,1),%rsi
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rdx
mov    $0x8,%ecx
call   1050 <read_memory@plt>
cmp    $0x8,%eax
jne    123b <write_memory+0xfb>
mov    %r15,%rbp
lea    (%r15,%r13,1),%rsi
lea    0x10(%rsp),%rdi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
mov    0x2df8(%rip),%r15
movq   $0x0,(%r15)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    (%r12,%r13,1),%rdx
mov    0x10(%rsp),%rcx
mov    0xc(%rsp),%esi
call   1040 <ptrace@plt>
mov    (%r15),%rax
test   %rax,%rax
jne    1218 <write_memory+0xd8>
add    %rbx,%r13
cmp    %r14,%r13
mov    %rbp,%r15
jb     1180 <write_memory+0x40>
jmp    1229 <write_memory+0xe9>
mov    0x2db6(%rip),%rax
mov    (%rax),%rax
xor    %r13d,%r13d
test   %rax,%rax
je     1229 <write_memory+0xe9>
mov    0x2db4(%rip),%rcx
cmp    (%rcx),%rax
jne    1242 <write_memory+0x102>
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    122c <write_memory+0xec>
lea    0xdb7(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    122c <write_memory+0xec>

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

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