#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 */
struct user_regs_struct {
  int /*<<< orphan*/ rip;
};
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ PTRACE_PEEKTEXT;
scalar_t__ errno;
long ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
            int /*<<< orphan*/);
int strace_peek_instruction(pid_t child, const struct user_regs_struct *regs,
                            long *instrp) {
  long instruction;
  errno = 0;
  instruction = ptrace(PTRACE_PEEKTEXT, child, regs->rip, 0);
  if (instruction == -1 && errno)
    return (-1);
  *instrp = instruction;
  return (0);
}


x86 assembly (clang, -O0):
<strace_peek_instruction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2eb6(%rip),%rax
movq   $0x0,(%rax)
mov    0x2ea0(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1030 <ptrace@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    1176 <strace_peek_instruction+0x66>
mov    0x2e80(%rip),%rax
cmpq   $0x0,(%rax)
je     1176 <strace_peek_instruction+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    1188 <strace_peek_instruction+0x78>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strace_peek_instruction>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %edi,%eax
mov    0x2ec0(%rip),%rbx
movq   $0x0,(%rbx)
mov    0x2eaa(%rip),%rcx
mov    (%rcx),%edi
mov    (%rsi),%edx
mov    %eax,%esi
xor    %ecx,%ecx
call   1030 <ptrace@plt>
cmp    $0xffffffffffffffff,%rax
jne    114c <strace_peek_instruction+0x3c>
mov    $0xffffffff,%ecx
cmpq   $0x0,(%rbx)
jne    1151 <strace_peek_instruction+0x41>
mov    %rax,(%r14)
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.user_regs_struct = type { i32 }
@errno = global i64 0
@PTRACE_PEEKTEXT = global i32 0
define i32 @strace_peek_instruction(i32 %0, %struct.user_regs_struct* nocapture %1, i64* nocapture writeonly %2) {
store i64 0, i64* @errno
%4 = load i32, i32* @PTRACE_PEEKTEXT
%5 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %1, i64 0, i32 0
%6 = load i32, i32* %5
%7 = tail call i64 @ptrace(i32 %4, i32 %0, i32 %6, i32 0)
%8 = icmp eq i64 %7, -1
%9 = load i64, i64* @errno
%10 = icmp ne i64 %9, 0
%11 = select i1 %8, i1 %10, i1 false
br i1 %11, label %13, label %12
12: ; preds = %3
store i64 %7, i64* %2
br label %13
13: ; preds = %3, %12
%14 = phi i32 [ 0, %12 ], [ -1, %3 ]
ret i32 %14
}