#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_POKEDATA;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, scalar_t__,
                           char *, ...);
long ptrace(int /*<<< orphan*/, int /*<<< orphan*/, void *, void *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int ptrace_writemem(int pid, void *addr, unsigned char *buf, size_t len) {
  do {
    if (!(len % sizeof(void *) == 0)) {
      fprintf(stderr,
              "(%s:%d: error: %d [%s]) "
              "Length of memory to read must be word-aligned"
              "\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/dismantl/linux-injector/refs/heads/"
              "master/ptrace.c",
              265, errno, errno == 0 ? "None" : strerror(errno));
      errno = 0;
      goto error;
    }
  } while (0);
  size_t wordlen = len / sizeof(void *);
  void **wordbuf = (void **)buf;
  for (size_t i = 0; i < wordlen; i++) {
    long result = ptrace(PTRACE_POKEDATA, (pid_t)pid,
                         addr + (i * sizeof(void *)), wordbuf[i]);
    do {
      if (!(result == 0)) {
        fprintf(stderr,
                "(%s:%d: error: %d [%s]) "
                "Failed to write memory to target process %d at location %p"
                "\n",
                "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/dismantl/linux-injector/"
                "refs/heads/master/ptrace.c",
                272, errno, errno == 0 ? "None" : strerror(errno), pid,
                addr + (i * sizeof(void *)));
        errno = 0;
        goto error;
      }
    } while (0);
  }
  return 1;
error:
  return 0;
}


x86 assembly (clang, -O0):
<ptrace_writemem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
and    $0x7,%rax
cmp    $0x0,%rax
je     11e2 <ptrace_writemem+0xb2>
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2e53(%rip),%rax
cmpq   $0x0,(%rax)
jne    1197 <ptrace_writemem+0x67>
lea    0xf46(%rip),%rax
mov    %rax,-0x58(%rbp)
jmp    11aa <ptrace_writemem+0x7a>
mov    0x2e32(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%r8
mov    -0x4c(%rbp),%edi
mov    -0x58(%rbp),%r9
lea    0xe44(%rip),%rsi
lea    0xe84(%rip),%rdx
mov    $0x109,%ecx
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2dfa(%rip),%rax
movq   $0x0,(%rax)
jmp    1315 <ptrace_writemem+0x1e5>
jmp    11e7 <ptrace_writemem+0xb7>
mov    -0x20(%rbp),%rax
shr    $0x3,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    1309 <ptrace_writemem+0x1d9>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x38(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdx
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
call   1040 <ptrace@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12ee <ptrace_writemem+0x1be>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2d71(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x60(%rbp)
mov    0x2d63(%rip),%rax
cmpq   $0x0,(%rax)
jne    1287 <ptrace_writemem+0x157>
lea    0xe56(%rip),%rax
mov    %rax,-0x70(%rbp)
jmp    129a <ptrace_writemem+0x16a>
mov    0x2d42(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %rax,-0x70(%rbp)
mov    -0x60(%rbp),%r8
mov    -0x64(%rbp),%edi
mov    -0x70(%rbp),%r9
mov    -0x8(%rbp),%r10d
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
lea    0xe1a(%rip),%rsi
lea    0xd81(%rip),%rdx
mov    $0x110,%ecx
mov    %r10d,(%rsp)
mov    %rax,0x8(%rsp)
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2cee(%rip),%rax
movq   $0x0,(%rax)
jmp    1315 <ptrace_writemem+0x1e5>
jmp    12f3 <ptrace_writemem+0x1c3>
jmp    12f8 <ptrace_writemem+0x1c8>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1203 <ptrace_writemem+0xd3>
movl   $0x1,-0x4(%rbp)
jmp    131c <ptrace_writemem+0x1ec>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptrace_writemem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r13
test   $0x7,%r13b
je     1169 <ptrace_writemem+0x39>
mov    0x2e95(%rip),%rax
mov    (%rax),%ebp
mov    0x2e7c(%rip),%rax
mov    (%rax),%rbx
test   %rbx,%rbx
je     11bb <ptrace_writemem+0x8b>
mov    %rbx,%rdi
call   1050 <strerror@plt>
mov    %rax,%r9
jmp    11c2 <ptrace_writemem+0x92>
mov    $0x1,%eax
cmp    $0x8,%r13
jb     11f1 <ptrace_writemem+0xc1>
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %edi,%r14d
shr    $0x3,%r13
xor    %ebp,%ebp
mov    0x2e4e(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r15),%edi
mov    (%r12,%rbp,8),%rcx
mov    %r14d,%esi
mov    %rbx,%rdx
call   1040 <ptrace@plt>
test   %rax,%rax
jne    1200 <ptrace_writemem+0xd0>
add    $0x1,%rbp
add    $0x8,%rbx
cmp    %rbp,%r13
jne    1190 <ptrace_writemem+0x60>
mov    $0x1,%eax
jmp    11f1 <ptrace_writemem+0xc1>
lea    0xf12(%rip),%r9
lea    0xe37(%rip),%rsi
lea    0xe77(%rip),%rdx
mov    %ebp,%edi
mov    $0x109,%ecx
mov    %rbx,%r8
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x2de8(%rip),%rax
movq   $0x0,(%rax)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd9(%rip),%rax
mov    (%rax),%r15d
mov    0x2dbf(%rip),%rax
mov    (%rax),%rbp
test   %rbp,%rbp
je     1226 <ptrace_writemem+0xf6>
mov    %rbp,%rdi
call   1050 <strerror@plt>
mov    %rax,%r9
jmp    122d <ptrace_writemem+0xfd>
lea    0xea7(%rip),%r9
lea    0xea5(%rip),%rsi
lea    0xe0c(%rip),%rdx
mov    %r15d,%edi
mov    $0x110,%ecx
mov    %rbp,%r8
xor    %eax,%eax
push   %rbx
push   %r14
call   1030 <fprintf@plt>
add    $0x10,%rsp
jmp    11e1 <ptrace_writemem+0xb1>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [71 x i8] c"(%s:%d: error: %d [%s]) Length of memory to read must be word-aligned\0A\00"
@.str.1 = [141 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/dismantl/linux-injector/refs/heads/master/ptrace.c\00"
@errno = global i64 0
@.str.2 = [5 x i8] c"None\00"
@PTRACE_POKEDATA = global i32 0
@.str.3 = [84 x i8] c"(%s:%d: error: %d [%s]) Failed to write memory to target process %d at location %p\0A\00"
define i32 @ptrace_writemem(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 i64
%11 = alloca i8**
%12 = alloca i64
%13 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
br label %14
14: ; preds = %4
%15 = load i64, i64* %9
%16 = urem i64 %15, 8
%17 = icmp eq i64 %16, 0
br i1 %17, label %30, label %18
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = load i64, i64* @errno
%21 = load i64, i64* @errno
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
br label %27
24: ; preds = %18
%25 = load i64, i64* @errno
%26 = call i8* @strerror(i64 %25)
br label %27
27: ; preds = %24, %23
%28 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %23 ], [ %26, %24 ]
%29 = call i32 (i32, i8*, i8*, i32, i64, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([141 x i8], [141 x i8]* @.str.1, i64 0, i64 0), i32 265, i64 %20, i8* %28)
store i64 0, i64* @errno
br label %78
30: ; preds = %14
br label %31
31: ; preds = %30
%32 = load i64, i64* %9
%33 = udiv i64 %32, 8
store i64 %33, i64* %10
%34 = load i8*, i8** %8
%35 = bitcast i8* %34 to i8**
store i8** %35, i8*** %11
store i64 0, i64* %12
br label %36
36: ; preds = %74, %31
%37 = load i64, i64* %12
%38 = load i64, i64* %10
%39 = icmp ult i64 %37, %38
br i1 %39, label %40, label %77
40: ; preds = %36
%41 = load i32, i32* @PTRACE_POKEDATA
%42 = load i32, i32* %6
%43 = load i8*, i8** %7
%44 = load i64, i64* %12
%45 = mul i64 %44, 8
%46 = getelementptr i8, i8* %43, i64 %45
%47 = load i8**, i8*** %11
%48 = load i64, i64* %12
%49 = getelementptr inbounds i8*, i8** %47, i64 %48
%50 = load i8*, i8** %49
%51 = call i64 @ptrace(i32 %41, i32 %42, i8* %46, i8* %50)
store i64 %51, i64* %13
br label %52
52: ; preds = %40
%53 = load i64, i64* %13
%54 = icmp eq i64 %53, 0
br i1 %54, label %72, label %55
55: ; preds = %52
%56 = load i32, i32* @stderr
%57 = load i64, i64* @errno
%58 = load i64, i64* @errno
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %55
br label %64
61: ; preds = %55
%62 = load i64, i64* @errno
%63 = call i8* @strerror(i64 %62)
br label %64
64: ; preds = %61, %60
%65 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %60 ], [ %63, %61 ]
%66 = load i32, i32* %6
%67 = load i8*, i8** %7
%68 = load i64, i64* %12
%69 = mul i64 %68, 8
%70 = getelementptr i8, i8* %67, i64 %69
%71 = call i32 (i32, i8*, i8*, i32, i64, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([84 x i8], [84 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([141 x i8], [141 x i8]* @.str.1, i64 0, i64 0), i32 272, i64 %57, i8* %65, i32 %66, i8* %70)
store i64 0, i64* @errno
br label %78
72: ; preds = %52
br label %73
73: ; preds = %72
br label %74
74: ; preds = %73
%75 = load i64, i64* %12
%76 = add i64 %75, 1
store i64 %76, i64* %12
br label %36
77: ; preds = %36
store i32 1, i32* %5
br label %79
78: ; preds = %64, %27
store i32 0, i32* %5
br label %79
79: ; preds = %78, %77
%80 = load i32, i32* %5
ret i32 %80
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [71 x i8] c"(%s:%d: error: %d [%s]) Length of memory to read must be word-aligned\0A\00"
@.str.1 = [141 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/dismantl/linux-injector/refs/heads/master/ptrace.c\00"
@errno = global i64 0
@.str.2 = [5 x i8] c"None\00"
@PTRACE_POKEDATA = global i32 0
@.str.3 = [84 x i8] c"(%s:%d: error: %d [%s]) Failed to write memory to target process %d at location %p\0A\00"
define i32 @ptrace_writemem(i32 %0, i8* %1, i8* nocapture %2, i64 %3) {
%5 = and i64 %3, 7
%6 = icmp eq i64 %5, 0
br i1 %6, label %16, label %7
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = load i64, i64* @errno
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i8* @strerror(i64 %9)
br label %13
13: ; preds = %7, %11
%14 = phi i8* [ %12, %11 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %7 ]
%15 = tail call i32 (i32, i8*, i8*, i32, i64, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([141 x i8], [141 x i8]* @.str.1, i64 0, i64 0), i32 265, i64 %9, i8* %14)
br label %42
16: ; preds = %4
%17 = bitcast i8* %2 to i8**
%18 = icmp ult i64 %3, 8
br i1 %18, label %43, label %19
19: ; preds = %16
%20 = lshr i64 %3, 3
br label %23
21: ; preds = %23
%22 = icmp eq i64 %32, %20
br i1 %22, label %43, label %23
23: ; preds = %19, %21
%24 = phi i64 [ %32, %21 ], [ 0, %19 ]
%25 = load i32, i32* @PTRACE_POKEDATA
%26 = shl i64 %24, 3
%27 = getelementptr i8, i8* %1, i64 %26
%28 = getelementptr inbounds i8*, i8** %17, i64 %24
%29 = load i8*, i8** %28
%30 = tail call i64 @ptrace(i32 %25, i32 %0, i8* %27, i8* %29)
%31 = icmp eq i64 %30, 0
%32 = add nuw nsw i64 %24, 1
br i1 %31, label %21, label %33
33: ; preds = %23
%34 = load i32, i32* @stderr
%35 = load i64, i64* @errno
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %33
%38 = tail call i8* @strerror(i64 %35)
br label %39
39: ; preds = %33, %37
%40 = phi i8* [ %38, %37 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %33 ]
%41 = tail call i32 (i32, i8*, i8*, i32, i64, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([84 x i8], [84 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([141 x i8], [141 x i8]* @.str.1, i64 0, i64 0), i32 272, i64 %35, i8* %40, i32 %0, i8* %27)
br label %42
42: ; preds = %39, %13
store i64 0, i64* @errno
br label %43
43: ; preds = %21, %42, %16
%44 = phi i32 [ 1, %16 ], [ 0, %42 ], [ 1, %21 ]
ret i32 %44
}