#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;
typedef enum __ptrace_request {
  ____Placeholder___ptrace_request
} __ptrace_request;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ EIO;
scalar_t__ errno;
int /*<<< orphan*/ msg_warn(char *, ...);
long ptrace(int, int /*<<< orphan*/, void *, void *);
long xptrace(int request, pid_t pid, void *addr, void *data) {
  long ret;
  errno = 0;
  do {
    ret = ptrace((enum __ptrace_request)request, pid, addr, data);
  } while (ret == -1 && errno == EINTR);
  if (ret == -1 && errno) {
    if (errno == EIO)
      msg_warn("Cannot access memory at address %p", addr);
    else
      msg_warn("ptrace");
    errno = 0;
  }
  return ret;
}


x86 assembly (clang, -O0):
<xptrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    0x2e9b(%rip),%rax
movq   $0x0,(%rax)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <ptrace@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0xffffffffffffffff,-0x20(%rbp)
mov    %al,-0x21(%rbp)
jne    1185 <xptrace+0x65>
mov    0x2e66(%rip),%rax
mov    (%rax),%rax
mov    0x2e54(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1144 <xptrace+0x24>
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11f9 <xptrace+0xd9>
mov    0x2e36(%rip),%rax
cmpq   $0x0,(%rax)
je     11f9 <xptrace+0xd9>
mov    0x2e25(%rip),%rax
mov    (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
jne    11dd <xptrace+0xbd>
mov    -0x10(%rbp),%rsi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <msg_warn@plt>
jmp    11eb <xptrace+0xcb>
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1040 <msg_warn@plt>
mov    0x2de6(%rip),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xptrace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r14
mov    %esi,%r12d
mov    %edi,%ebp
mov    0x2e9b(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e85(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r12d,%esi
mov    %r14,%rdx
mov    %r15,%rcx
call   1030 <ptrace@plt>
mov    %rax,%rbx
mov    0x2e6e(%rip),%rax
mov    (%rax),%rax
cmp    $0xffffffffffffffff,%rbx
jne    1179 <xptrace+0x59>
cmp    0x0(%r13),%rax
je     1150 <xptrace+0x30>
cmp    $0xffffffffffffffff,%rbx
jne    11bf <xptrace+0x9f>
test   %rax,%rax
je     11bf <xptrace+0x9f>
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%rax
jne    11a3 <xptrace+0x83>
lea    0xe69(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <msg_warn@plt>
jmp    11b1 <xptrace+0x91>
lea    0xe79(%rip),%rdi
xor    %eax,%eax
call   1040 <msg_warn@plt>
mov    0x2e20(%rip),%rax
movq   $0x0,(%rax)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EIO = global i64 0
@.str = [35 x i8] c"Cannot access memory at address %p\00"
@.str.1 = [7 x i8] c"ptrace\00"
define i64 @xptrace(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
store i64 0, i64* @errno
br label %10
10: ; preds = %23, %4
%11 = load i32, i32* %5
%12 = load i32, i32* %6
%13 = load i8*, i8** %7
%14 = load i8*, i8** %8
%15 = call i64 @ptrace(i32 %11, i32 %12, i8* %13, i8* %14)
store i64 %15, i64* %9
br label %16
16: ; preds = %10
%17 = load i64, i64* %9
%18 = icmp eq i64 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br label %23
23: ; preds = %19, %16
%24 = phi i1 [ false, %16 ], [ %22, %19 ]
br i1 %24, label %10, label %25
25: ; preds = %23
%26 = load i64, i64* %9
%27 = icmp eq i64 %26, -1
br i1 %27, label %28, label %41
28: ; preds = %25
%29 = load i64, i64* @errno
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %41
31: ; preds = %28
%32 = load i64, i64* @errno
%33 = load i64, i64* @EIO
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i8*, i8** %7
%37 = call i32 (i8*, ...) @msg_warn(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %36)
br label %40
38: ; preds = %31
%39 = call i32 (i8*, ...) @msg_warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %38, %35
store i64 0, i64* @errno
br label %41
41: ; preds = %40, %28, %25
%42 = load i64, i64* %9
ret i64 %42
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EIO = global i64 0
@.str = [35 x i8] c"Cannot access memory at address %p\00"
@.str.1 = [7 x i8] c"ptrace\00"
define i64 @xptrace(i32 %0, i32 %1, i8* %2, i8* %3) {
store i64 0, i64* @errno
br label %5
5: ; preds = %5, %4
%6 = tail call i64 @ptrace(i32 %0, i32 %1, i8* %2, i8* %3)
%7 = icmp eq i64 %6, -1
%8 = load i64, i64* @errno
%9 = load i64, i64* @EINTR
%10 = icmp eq i64 %8, %9
%11 = select i1 %7, i1 %10, i1 false
br i1 %11, label %5, label %12
12: ; preds = %5
%13 = icmp ne i64 %8, 0
%14 = select i1 %7, i1 %13, i1 false
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = load i64, i64* @EIO
%17 = icmp eq i64 %8, %16
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 (i8*, ...) @msg_warn(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %22
20: ; preds = %15
%21 = tail call i32 (i8*, ...) @msg_warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %18
store i64 0, i64* @errno
br label %23
23: ; preds = %22, %12
ret i64 %6
}