#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_PEEKDATA;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, scalar_t__,
                           char *, ...);
scalar_t__ ptrace(int /*<<< orphan*/, int /*<<< orphan*/, void *,
                  int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int ptrace_readmem(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",
              244, errno, errno == 0 ? "None" : strerror(errno));
      errno = 0;
      goto error;
    }
  } while (0);
  size_t wordlen = len / sizeof(void *);
  void **wordbuf = (void **)buf;
  errno = 0;
  for (size_t i = 0; i < wordlen; i++) {
    wordbuf[i] = (void *)ptrace(PTRACE_PEEKDATA, (pid_t)pid,
                                addr + (i * sizeof(void *)), NULL);
    do {
      if (!(errno == 0)) {
        fprintf(stderr,
                "(%s:%d: error: %d [%s]) "
                "Failed to read memory of 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",
                252, 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_readmem>:
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_readmem+0xb2>
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e5b(%rip),%rax
cmpq   $0x0,(%rax)
jne    1197 <ptrace_readmem+0x67>
lea    0xf46(%rip),%rax
mov    %rax,-0x50(%rbp)
jmp    11aa <ptrace_readmem+0x7a>
mov    0x2e3a(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%r8
mov    -0x44(%rbp),%edi
mov    -0x50(%rbp),%r9
lea    0xe44(%rip),%rsi
lea    0xe84(%rip),%rdx
mov    $0xf4,%ecx
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2e02(%rip),%rax
movq   $0x0,(%rax)
jmp    132c <ptrace_readmem+0x1fc>
jmp    11e7 <ptrace_readmem+0xb7>
mov    -0x20(%rbp),%rax
shr    $0x3,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dd6(%rip),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    1320 <ptrace_readmem+0x1f0>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x38(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    %rax,%rdx
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    0x2d7f(%rip),%rax
cmpq   $0x0,(%rax)
je     1305 <ptrace_readmem+0x1d5>
mov    0x2d76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2d62(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    0x2d54(%rip),%rax
cmpq   $0x0,(%rax)
jne    129e <ptrace_readmem+0x16e>
lea    0xe3f(%rip),%rax
mov    %rax,-0x68(%rbp)
jmp    12b1 <ptrace_readmem+0x181>
mov    0x2d33(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %rax,-0x68(%rbp)
mov    -0x58(%rbp),%r8
mov    -0x5c(%rbp),%edi
mov    -0x68(%rbp),%r9
mov    -0x8(%rbp),%r10d
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
lea    0xe03(%rip),%rsi
lea    0xd6a(%rip),%rdx
mov    $0xfc,%ecx
mov    %r10d,(%rsp)
mov    %rax,0x8(%rsp)
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2cdf(%rip),%rax
movq   $0x0,(%rax)
jmp    132c <ptrace_readmem+0x1fc>
jmp    130a <ptrace_readmem+0x1da>
jmp    130f <ptrace_readmem+0x1df>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1211 <ptrace_readmem+0xe1>
movl   $0x1,-0x4(%rbp)
jmp    1333 <ptrace_readmem+0x203>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptrace_readmem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
test   $0x7,%r15b
je     116d <ptrace_readmem+0x3d>
mov    0x2e95(%rip),%rax
mov    (%rax),%ebp
mov    0x2e84(%rip),%rax
mov    (%rax),%rbx
test   %rbx,%rbx
je     11dd <ptrace_readmem+0xad>
mov    %rbx,%rdi
call   1050 <strerror@plt>
mov    %rax,%r9
jmp    11e4 <ptrace_readmem+0xb4>
mov    0x2e64(%rip),%rax
movq   $0x0,(%rax)
mov    $0x1,%eax
cmp    $0x8,%r15
jb     1213 <ptrace_readmem+0xe3>
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %edi,%r14d
shr    $0x3,%r15
xor    %r13d,%r13d
nopw   0x0(%rax,%rax,1)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    %r14d,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %rax,(%r12,%r13,8)
mov    0x2e17(%rip),%rax
mov    (%rax),%rbp
test   %rbp,%rbp
jne    1222 <ptrace_readmem+0xf2>
add    $0x1,%r13
add    $0x8,%rbx
cmp    %r13,%r15
jne    11a0 <ptrace_readmem+0x70>
mov    $0x1,%eax
jmp    1213 <ptrace_readmem+0xe3>
lea    0xef0(%rip),%r9
lea    0xe15(%rip),%rsi
lea    0xe55(%rip),%rdx
mov    %ebp,%edi
mov    $0xf4,%ecx
mov    %rbx,%r8
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x2dce(%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    0x2db7(%rip),%rax
mov    (%rax),%r15d
mov    %rbp,%rdi
call   1050 <strerror@plt>
lea    0xe9e(%rip),%rsi
lea    0xe05(%rip),%rdx
mov    %r15d,%edi
mov    $0xfc,%ecx
mov    %rbp,%r8
mov    %rax,%r9
xor    %eax,%eax
push   %rbx
push   %r14
call   1030 <fprintf@plt>
add    $0x10,%rsp
jmp    1203 <ptrace_readmem+0xd3>

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_PEEKDATA = global i32 0
@.str.3 = [83 x i8] c"(%s:%d: error: %d [%s]) Failed to read memory of target process %d at location %p\0A\00"
define i32 @ptrace_readmem(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
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
br label %13
13: ; preds = %4
%14 = load i64, i64* %9
%15 = urem i64 %14, 8
%16 = icmp eq i64 %15, 0
br i1 %16, label %29, label %17
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = load i64, i64* @errno
%20 = load i64, i64* @errno
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %17
br label %26
23: ; preds = %17
%24 = load i64, i64* @errno
%25 = call i8* @strerror(i64 %24)
br label %26
26: ; preds = %23, %22
%27 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %22 ], [ %25, %23 ]
%28 = call i32 (i32, i8*, i8*, i32, i64, i8*, ...) @fprintf(i32 %18, 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 244, i64 %19, i8* %27)
store i64 0, i64* @errno
br label %77
29: ; preds = %13
br label %30
30: ; preds = %29
%31 = load i64, i64* %9
%32 = udiv i64 %31, 8
store i64 %32, i64* %10
%33 = load i8*, i8** %8
%34 = bitcast i8* %33 to i8**
store i8** %34, i8*** %11
store i64 0, i64* @errno
store i64 0, i64* %12
br label %35
35: ; preds = %73, %30
%36 = load i64, i64* %12
%37 = load i64, i64* %10
%38 = icmp ult i64 %36, %37
br i1 %38, label %39, label %76
39: ; preds = %35
%40 = load i32, i32* @PTRACE_PEEKDATA
%41 = load i32, i32* %6
%42 = load i8*, i8** %7
%43 = load i64, i64* %12
%44 = mul i64 %43, 8
%45 = getelementptr i8, i8* %42, i64 %44
%46 = call i64 @ptrace(i32 %40, i32 %41, i8* %45, i32* null)
%47 = inttoptr i64 %46 to i8*
%48 = load i8**, i8*** %11
%49 = load i64, i64* %12
%50 = getelementptr inbounds i8*, i8** %48, i64 %49
store i8* %47, i8** %50
br label %51
51: ; preds = %39
%52 = load i64, i64* @errno
%53 = icmp eq i64 %52, 0
br i1 %53, label %71, label %54
54: ; preds = %51
%55 = load i32, i32* @stderr
%56 = load i64, i64* @errno
%57 = load i64, i64* @errno
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %54
br label %63
60: ; preds = %54
%61 = load i64, i64* @errno
%62 = call i8* @strerror(i64 %61)
br label %63
63: ; preds = %60, %59
%64 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %59 ], [ %62, %60 ]
%65 = load i32, i32* %6
%66 = load i8*, i8** %7
%67 = load i64, i64* %12
%68 = mul i64 %67, 8
%69 = getelementptr i8, i8* %66, i64 %68
%70 = call i32 (i32, i8*, i8*, i32, i64, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([141 x i8], [141 x i8]* @.str.1, i64 0, i64 0), i32 252, i64 %56, i8* %64, i32 %65, i8* %69)
store i64 0, i64* @errno
br label %77
71: ; preds = %51
br label %72
72: ; preds = %71
br label %73
73: ; preds = %72
%74 = load i64, i64* %12
%75 = add i64 %74, 1
store i64 %75, i64* %12
br label %35
76: ; preds = %35
store i32 1, i32* %5
br label %78
77: ; preds = %63, %26
store i32 0, i32* %5
br label %78
78: ; preds = %77, %76
%79 = load i32, i32* %5
ret i32 %79
}

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_PEEKDATA = global i32 0
@.str.3 = [83 x i8] c"(%s:%d: error: %d [%s]) Failed to read memory of target process %d at location %p\0A\00"
define i32 @ptrace_readmem(i32 %0, i8* %1, i8* nocapture writeonly %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 244, i64 %9, i8* %14)
br label %38
16: ; preds = %4
%17 = bitcast i8* %2 to i8**
store i64 0, i64* @errno
%18 = icmp ult i64 %3, 8
br i1 %18, label %39, label %19
19: ; preds = %16
%20 = lshr i64 %3, 3
br label %23
21: ; preds = %23
%22 = icmp eq i64 %33, %20
br i1 %22, label %39, label %23
23: ; preds = %19, %21
%24 = phi i64 [ %33, %21 ], [ 0, %19 ]
%25 = load i32, i32* @PTRACE_PEEKDATA
%26 = shl i64 %24, 3
%27 = getelementptr i8, i8* %1, i64 %26
%28 = tail call i64 @ptrace(i32 %25, i32 %0, i8* %27, i32* null)
%29 = inttoptr i64 %28 to i8*
%30 = getelementptr inbounds i8*, i8** %17, i64 %24
store i8* %29, i8** %30
%31 = load i64, i64* @errno
%32 = icmp eq i64 %31, 0
%33 = add nuw nsw i64 %24, 1
br i1 %32, label %21, label %34
34: ; preds = %23
%35 = load i32, i32* @stderr
%36 = tail call i8* @strerror(i64 %31)
%37 = tail call i32 (i32, i8*, i8*, i32, i64, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([141 x i8], [141 x i8]* @.str.1, i64 0, i64 0), i32 252, i64 %31, i8* %36, i32 %0, i8* %27)
br label %38
38: ; preds = %34, %13
store i64 0, i64* @errno
br label %39
39: ; preds = %21, %38, %16
%40 = phi i32 [ 1, %16 ], [ 0, %38 ], [ 1, %21 ]
ret i32 %40
}