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

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


x86 assembly (clang, -O0):
<ftrace_peek_instruction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    0x2e6f(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1040 <ptrace@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11be <ftrace_peek_instruction+0x9e>
mov    0x2e38(%rip),%rax
cmpq   $0x0,(%rax)
je     11be <ftrace_peek_instruction+0x9e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <ftrace_peek_instruction+0xb0>
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):
<ftrace_peek_instruction>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1030 <assert@plt>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1030 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1030 <assert@plt>
mov    0x2e7e(%rip),%r12
movq   $0x0,(%r12)
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%edx
mov    %r15,%rsi
xor    %ecx,%ecx
call   1040 <ptrace@plt>
cmp    $0xffffffffffffffff,%rax
jne    1191 <ftrace_peek_instruction+0x71>
mov    $0xffffffff,%ecx
cmpq   $0x0,(%r12)
jne    1196 <ftrace_peek_instruction+0x76>
mov    %rax,(%r14)
xor    %ecx,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.user_regs_struct = type { i32 }
@errno = global i64 0
@PTRACE_PEEKTEXT = global i32 0
define i32 @ftrace_peek_instruction(i64 %0, %struct.user_regs_struct* %1, i64* %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca %struct.user_regs_struct*
%7 = alloca i64*
%8 = alloca i64
store i64 %0, i64* %5
store %struct.user_regs_struct* %1, %struct.user_regs_struct** %6
store i64* %2, i64** %7
%9 = load i64, i64* %5
%10 = icmp ne i64 %9, 0
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load %struct.user_regs_struct*, %struct.user_regs_struct** %6
%14 = icmp ne %struct.user_regs_struct* %13, null
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i64*, i64** %7
%18 = icmp ne i64* %17, null
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
store i64 0, i64* @errno
%21 = load i32, i32* @PTRACE_PEEKTEXT
%22 = load i64, i64* %5
%23 = load %struct.user_regs_struct*, %struct.user_regs_struct** %6
%24 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @ptrace(i32 %21, i64 %22, i32 %25, i32 0)
store i64 %26, i64* %8
%27 = load i64, i64* %8
%28 = icmp eq i64 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %3
%30 = load i64, i64* @errno
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 -1, i32* %4
br label %36
33: ; preds = %29, %3
%34 = load i64, i64* %8
%35 = load i64*, i64** %7
store i64 %34, i64* %35
store i32 0, i32* %4
br label %36
36: ; preds = %33, %32
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
%struct.user_regs_struct = type { i32 }
@errno = global i64 0
@PTRACE_PEEKTEXT = global i32 0
define i32 @ftrace_peek_instruction(i64 %0, %struct.user_regs_struct* %1, i64* writeonly %2) {
%4 = icmp ne i64 %0, 0
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp ne %struct.user_regs_struct* %1, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp ne i64* %2, null
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
store i64 0, i64* @errno
%13 = load i32, i32* @PTRACE_PEEKTEXT
%14 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %1, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i64 @ptrace(i32 %13, i64 %0, i32 %15, i32 0)
%17 = icmp eq i64 %16, -1
%18 = load i64, i64* @errno
%19 = icmp ne i64 %18, 0
%20 = select i1 %17, i1 %19, i1 false
br i1 %20, label %22, label %21
21: ; preds = %3
store i64 %16, i64* %2
br label %22
22: ; preds = %3, %21
%23 = phi i32 [ 0, %21 ], [ -1, %3 ]
ret i32 %23
}