#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_PEEKDATA;
scalar_t__ errno;
unsigned long ptrace(int /*<<< orphan*/, scalar_t__, void *,
                     int /*<<< orphan*/);
int read_word(pid_t pid, void *addr, unsigned long *dest) {
  if (pid <= 0) {
    return 1;
  }
  errno = 0;
  unsigned long ret;
  ret = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
  if (errno) {
    return 1;
  }
  *dest = ret;
  return 0;
}


x86 assembly (clang, -O0):
<read_word>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jg     113b <read_word+0x2b>
movl   $0x1,-0x4(%rbp)
jmp    1194 <read_word+0x84>
mov    0x2e9e(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <ptrace@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e74(%rip),%rax
cmpq   $0x0,(%rax)
je     1182 <read_word+0x72>
movl   $0x1,-0x4(%rbp)
jmp    1194 <read_word+0x84>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_word>:
push   %r15
push   %r14
push   %rbx
mov    $0x1,%ebx
test   %rdi,%rdi
jle    1151 <read_word+0x41>
mov    %rdx,%r14
mov    %rsi,%rdx
mov    %rdi,%rsi
mov    0x2eb1(%rip),%r15
movq   $0x0,(%r15)
mov    0x2e9b(%rip),%rax
mov    (%rax),%edi
xor    %ecx,%ecx
call   1030 <ptrace@plt>
cmpq   $0x0,(%r15)
jne    1151 <read_word+0x41>
mov    %rax,(%r14)
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@PTRACE_PEEKDATA = global i32 0
define i32 @read_word(i64 %0, i8* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64*
%8 = alloca i64
store i64 %0, i64* %5
store i8* %1, i8** %6
store i64* %2, i64** %7
%9 = load i64, i64* %5
%10 = icmp sle i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %3
store i32 1, i32* %4
br label %23
12: ; preds = %3
store i64 0, i64* @errno
%13 = load i32, i32* @PTRACE_PEEKDATA
%14 = load i64, i64* %5
%15 = load i8*, i8** %6
%16 = call i64 @ptrace(i32 %13, i64 %14, i8* %15, i32 0)
store i64 %16, i64* %8
%17 = load i64, i64* @errno
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %12
store i32 1, i32* %4
br label %23
20: ; preds = %12
%21 = load i64, i64* %8
%22 = load i64*, i64** %7
store i64 %21, i64* %22
store i32 0, i32* %4
br label %23
23: ; preds = %20, %19, %11
%24 = load i32, i32* %4
ret i32 %24
}

LLVM IR (-O2):
@errno = global i64 0
@PTRACE_PEEKDATA = global i32 0
define i32 @read_word(i64 %0, i8* %1, i64* nocapture writeonly %2) {
%4 = icmp slt i64 %0, 1
br i1 %4, label %11, label %5
5: ; preds = %3
store i64 0, i64* @errno
%6 = load i32, i32* @PTRACE_PEEKDATA
%7 = tail call i64 @ptrace(i32 %6, i64 %0, i8* %1, i32 0)
%8 = load i64, i64* @errno
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %5
store i64 %7, i64* %2
br label %11
11: ; preds = %10, %5, %3
%12 = phi i32 [ 1, %3 ], [ 0, %10 ], [ 1, %5 ]
ret i32 %12
}