#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_PEEKDATA;
size_t SSIZE_MAX;
scalar_t__ errno;
int /*<<< orphan*/ memcpy(void *, long *, size_t);
int /*<<< orphan*/ perror(char *);
long ptrace(int /*<<< orphan*/, int /*<<< orphan*/, void *,
            int /*<<< orphan*/ *);
ssize_t read_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) {
    errno = 0;
    word = ptrace(PTRACE_PEEKDATA, pid, addr + i, NULL);
    if (errno)
      break;
    rem = size - i < sizeof(word) ? size - i : sizeof(word);
    memcpy(buf + i, &word, rem);
    i += rem;
  }
  if (errno && errno != EIO) {
    perror("read_memory: ptrace");
    goto ret;
  }
  r = (ssize_t)i;
ret:
  return r;
}


x86 assembly (clang, -O0):
<read_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    0x2e8f(%rip),%rcx
cmp    (%rcx),%rax
jbe    1167 <read_memory+0x37>
jmp    1264 <read_memory+0x134>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1221 <read_memory+0xf1>
mov    0x2e4c(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
add    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x38(%rbp)
mov    0x2e1d(%rip),%rax
cmpq   $0x0,(%rax)
je     11c2 <read_memory+0x92>
jmp    1221 <read_memory+0xf1>
mov    -0x20(%rbp),%rax
sub    -0x28(%rbp),%rax
cmp    $0x8,%rax
jae    11e5 <read_memory+0xb5>
mov    -0x20(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    11f3 <read_memory+0xc3>
mov    $0x8,%eax
mov    %rax,-0x48(%rbp)
jmp    11f3 <read_memory+0xc3>
mov    -0x48(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rsi
call   1030 <memcpy@plt>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    116f <read_memory+0x3f>
mov    0x2da8(%rip),%rax
cmpq   $0x0,(%rax)
je     125d <read_memory+0x12d>
mov    0x2d97(%rip),%rax
mov    (%rax),%rax
mov    0x2d95(%rip),%rcx
cmp    (%rcx),%rax
je     125d <read_memory+0x12d>
lea    0xdad(%rip),%rdi
call   1050 <perror@plt>
jmp    1264 <read_memory+0x134>
mov    -0x28(%rbp),%rax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_memory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,0x8(%rsp)
mov    0x2e9b(%rip),%rcx
mov    $0xffffffff,%eax
cmp    %r14,(%rcx)
jb     11ec <read_memory+0xbc>
test   %r14,%r14
je     11cb <read_memory+0x9b>
mov    %rsi,%r12
mov    %edi,%ebp
xor    %r13d,%r13d
mov    0x2e61(%rip),%r15
nop
movq   $0x0,(%r15)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
lea    (%r12,%r13,1),%rdx
mov    %ebp,%esi
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %rax,0x10(%rsp)
cmpq   $0x0,(%r15)
jne    11ce <read_memory+0x9e>
mov    %r14,%rbx
sub    %r13,%rbx
cmp    $0x8,%rbx
mov    $0x8,%eax
cmovae %rax,%rbx
mov    0x8(%rsp),%rax
lea    (%rax,%r13,1),%rdi
lea    0x10(%rsp),%rsi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
add    %rbx,%r13
cmp    %r14,%r13
jb     1170 <read_memory+0x40>
jmp    11ce <read_memory+0x9e>
xor    %r13d,%r13d
mov    0x2dfb(%rip),%rax
mov    (%rax),%rax
test   %rax,%rax
je     11e9 <read_memory+0xb9>
mov    0x2df4(%rip),%rcx
cmp    (%rcx),%rax
jne    11fb <read_memory+0xcb>
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfe(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    11ec <read_memory+0xbc>

LLVM IR (-O0):
@SSIZE_MAX = global i64 0
@errno = global i64 0
@PTRACE_PEEKDATA = global i32 0
@EIO = global i64 0
@.str = [20 x i8] c"read_memory: ptrace\00"
define i32 @read_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 %64
17: ; preds = %4
store i64 0, i64* %9
br label %18
18: ; preds = %42, %17
%19 = load i64, i64* %9
%20 = load i64, i64* %8
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %52
22: ; preds = %18
store i64 0, i64* @errno
%23 = load i32, i32* @PTRACE_PEEKDATA
%24 = load i32, i32* %5
%25 = load i8*, i8** %6
%26 = load i64, i64* %9
%27 = getelementptr i8, i8* %25, i64 %26
%28 = call i64 @ptrace(i32 %23, i32 %24, i8* %27, i32* null)
store i64 %28, i64* %11
%29 = load i64, i64* @errno
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %22
br label %52
32: ; preds = %22
%33 = load i64, i64* %8
%34 = load i64, i64* %9
%35 = sub i64 %33, %34
%36 = icmp ult i64 %35, 8
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = load i64, i64* %8
%39 = load i64, i64* %9
%40 = sub i64 %38, %39
br label %42
41: ; preds = %32
br label %42
42: ; preds = %41, %37
%43 = phi i64 [ %40, %37 ], [ 8, %41 ]
store i64 %43, i64* %10
%44 = load i8*, i8** %7
%45 = load i64, i64* %9
%46 = getelementptr i8, i8* %44, i64 %45
%47 = load i64, i64* %10
%48 = call i32 @memcpy(i8* %46, i64* %11, i64 %47)
%49 = load i64, i64* %10
%50 = load i64, i64* %9
%51 = add i64 %50, %49
store i64 %51, i64* %9
br label %18
52: ; preds = %31, %18
%53 = load i64, i64* @errno
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %52
%56 = load i64, i64* @errno
%57 = load i64, i64* @EIO
%58 = icmp ne i64 %56, %57
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %64
61: ; preds = %55, %52
%62 = load i64, i64* %9
%63 = trunc i64 %62 to i32
store i32 %63, i32* %12
br label %64
64: ; preds = %61, %59, %16
%65 = load i32, i32* %12
ret i32 %65
}

LLVM IR (-O2):
@SSIZE_MAX = global i64 0
@errno = global i64 0
@PTRACE_PEEKDATA = global i32 0
@EIO = global i64 0
@.str = [20 x i8] c"read_memory: ptrace\00"
define i32 @read_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 %37, label %9
9: ; preds = %4
%10 = icmp eq i64 %3, 0
br i1 %10, label %26, label %11
11: ; preds = %9, %18
%12 = phi i64 [ %24, %18 ], [ 0, %9 ]
store i64 0, i64* @errno
%13 = load i32, i32* @PTRACE_PEEKDATA
%14 = getelementptr i8, i8* %1, i64 %12
%15 = call i64 @ptrace(i32 %13, i32 %0, i8* %14, i32* null)
store i64 %15, i64* %5
%16 = load i64, i64* @errno
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %11
%19 = sub i64 %3, %12
%20 = icmp ult i64 %19, 8
%21 = select i1 %20, i64 %19, i64 8
%22 = getelementptr i8, i8* %2, i64 %12
%23 = call i32 @memcpy(i8* %22, i64* %5, i64 %21)
%24 = add i64 %21, %12
%25 = icmp ult i64 %24, %3
br i1 %25, label %11, label %26
26: ; preds = %18, %11, %9
%27 = phi i64 [ 0, %9 ], [ %12, %11 ], [ %24, %18 ]
%28 = load i64, i64* @errno
%29 = icmp eq i64 %28, 0
%30 = load i64, i64* @EIO
%31 = icmp eq i64 %28, %30
%32 = select i1 %29, i1 true, i1 %31
br i1 %32, label %35, label %33
33: ; preds = %26
%34 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %37
35: ; preds = %26
%36 = trunc i64 %27 to i32
br label %37
37: ; preds = %4, %35, %33
%38 = phi i32 [ -1, %4 ], [ -1, %33 ], [ %36, %35 ]
ret i32 %38
}