#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 ENOSYS;
int /*<<< orphan*/ PTRACE_PEEKUSER;
int RAX;
scalar_t__ errno;
int /*<<< orphan*/ printf(char *);
int ptrace(int /*<<< orphan*/, int /*<<< orphan*/ const, int,
           int /*<<< orphan*/ *);
int syscall_get_return(const pid_t pid, int *rc) {
  errno = 0;
  *rc = ptrace(PTRACE_PEEKUSER, pid, sizeof(long) * RAX, NULL);
  if (errno)
    return -1;
  if (*rc == -ENOSYS) {
    printf("got unexpected SYSCALL entry\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<syscall_get_return>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e9a(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    0x2e90(%rip),%rax
movslq (%rax),%rax
shl    $0x3,%rax
mov    %eax,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <ptrace@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e5f(%rip),%rax
cmpq   $0x0,(%rax)
je     1187 <syscall_get_return+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bf <syscall_get_return+0x9f>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
xor    %ecx,%ecx
mov    0x2e52(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    11b8 <syscall_get_return+0x98>
lea    0xe59(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bf <syscall_get_return+0x9f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<syscall_get_return>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%esi
mov    0x2e9f(%rip),%r15
movq   $0x0,(%r15)
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
mov    0x2e98(%rip),%rax
mov    (%rax),%edx
shl    $0x3,%edx
xor    %ebx,%ebx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %eax,(%r14)
cmpq   $0x0,(%r15)
jne    1176 <syscall_get_return+0x56>
mov    0x2e82(%rip),%rcx
add    (%rcx),%eax
jne    117b <syscall_get_return+0x5b>
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@PTRACE_PEEKUSER = global i32 0
@RAX = global i32 0
@ENOSYS = global i32 0
@.str = [30 x i8] c"got unexpected SYSCALL entry\0A\00"
define i32 @syscall_get_return(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
store i32 %0, i32* %4
store i32* %1, i32** %5
store i64 0, i64* @errno
%6 = load i32, i32* @PTRACE_PEEKUSER
%7 = load i32, i32* %4
%8 = load i32, i32* @RAX
%9 = sext i32 %8 to i64
%10 = mul i64 8, %9
%11 = trunc i64 %10 to i32
%12 = call i32 @ptrace(i32 %6, i32 %7, i32 %11, i32* null)
%13 = load i32*, i32** %5
store i32 %12, i32* %13
%14 = load i64, i64* @errno
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %26
17: ; preds = %2
%18 = load i32*, i32** %5
%19 = load i32, i32* %18
%20 = load i32, i32* @ENOSYS
%21 = sub nsw i32 0, %20
%22 = icmp eq i32 %19, %21
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %26
25: ; preds = %17
store i32 0, i32* %3
br label %26
26: ; preds = %25, %23, %16
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
@errno = global i64 0
@PTRACE_PEEKUSER = global i32 0
@RAX = global i32 0
@ENOSYS = global i32 0
@str = [29 x i8] c"got unexpected SYSCALL entry\00"
define i32 @syscall_get_return(i32 %0, i32* nocapture writeonly %1) {
store i64 0, i64* @errno
%3 = load i32, i32* @PTRACE_PEEKUSER
%4 = load i32, i32* @RAX
%5 = shl i32 %4, 3
%6 = tail call i32 @ptrace(i32 %3, i32 %0, i32 %5, i32* null)
store i32 %6, i32* %1
%7 = load i64, i64* @errno
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = load i32, i32* @ENOSYS
%11 = sub nsw i32 0, %10
%12 = icmp eq i32 %6, %11
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %15
15: ; preds = %9, %2, %13
%16 = phi i32 [ -1, %13 ], [ -1, %2 ], [ 0, %9 ]
ret i32 %16
}