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

/* Variables and functions */
int /*<<< orphan*/ PTRACE_GETREGS;
int /*<<< orphan*/ PTRACE_SETREGS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ logs(int, char *, char *, int /*<<< orphan*/);
scalar_t__ ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/ *,
                  struct user_regs_struct *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void remote_jmp(pid_t pid, void *addr) {
  struct user_regs_struct regs;
  if (ptrace(PTRACE_GETREGS, pid, NULL, &regs) < 0) {
    logs(2, "%s: %s", "ptrace GETREGS", strerror(errno));
    exit(1);
  }
  regs.rip = (uint64_t)addr;
  if (ptrace(PTRACE_SETREGS, pid, NULL, &regs) < 0) {
    logs(2, "%s: %s", "ptrace SETREGS", strerror(errno));
    exit(1);
  }
}


x86 assembly (clang, -O0):
<remote_jmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x18(%rbp),%rcx
call   1030 <ptrace@plt>
cmp    $0x0,%rax
jge    11a4 <remote_jmp+0x64>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %eax,%ecx
mov    $0x2,%edi
lea    0xe72(%rip),%rsi
lea    0xe72(%rip),%rdx
call   1040 <logs@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x18(%rbp),%rcx
call   1030 <ptrace@plt>
cmp    $0x0,%rax
jge    1201 <remote_jmp+0xc1>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %eax,%ecx
mov    $0x2,%edi
lea    0xe15(%rip),%rsi
lea    0xe24(%rip),%rdx
call   1040 <logs@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remote_jmp>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rcx
mov    %ebx,%esi
xor    %edx,%edx
call   1030 <ptrace@plt>
test   %rax,%rax
js     1189 <remote_jmp+0x49>
mov    %r14,(%rsp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rcx
mov    %ebx,%esi
xor    %edx,%edx
call   1030 <ptrace@plt>
test   %rax,%rax
js     11a7 <remote_jmp+0x67>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe62(%rip),%rsi
lea    0xe62(%rip),%rdx
jmp    11c3 <remote_jmp+0x83>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe44(%rip),%rsi
lea    0xe53(%rip),%rdx
mov    $0x2,%edi
mov    %eax,%ecx
call   1040 <logs@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.user_regs_struct = type { i64 }
@PTRACE_GETREGS = global i32 0
@.str = [7 x i8] c"%s: %s\00"
@.str.1 = [15 x i8] c"ptrace GETREGS\00"
@errno = global i32 0
@PTRACE_SETREGS = global i32 0
@.str.2 = [15 x i8] c"ptrace SETREGS\00"
define void @remote_jmp(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.user_regs_struct
store i32 %0, i32* %3
store i8* %1, i8** %4
%6 = load i32, i32* @PTRACE_GETREGS
%7 = load i32, i32* %3
%8 = call i64 @ptrace(i32 %6, i32 %7, i32* null, %struct.user_regs_struct* %5)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = call i32 @strerror(i32 %11)
%13 = call i32 @logs(i32 2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = ptrtoint i8* %16 to i64
%18 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %5, i32 0, i32 0
store i64 %17, i64* %18
%19 = load i32, i32* @PTRACE_SETREGS
%20 = load i32, i32* %3
%21 = call i64 @ptrace(i32 %19, i32 %20, i32* null, %struct.user_regs_struct* %5)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %15
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = call i32 @logs(i32 2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %15
ret void
}

LLVM IR (-O2):
%struct.user_regs_struct = type { i64 }
@PTRACE_GETREGS = global i32 0
@.str = [7 x i8] c"%s: %s\00"
@.str.1 = [15 x i8] c"ptrace GETREGS\00"
@errno = global i32 0
@PTRACE_SETREGS = global i32 0
@.str.2 = [15 x i8] c"ptrace SETREGS\00"
define void @remote_jmp(i32 %0, i8* %1) {
%3 = alloca %struct.user_regs_struct
%4 = bitcast %struct.user_regs_struct* %3 to i8*
%5 = load i32, i32* @PTRACE_GETREGS
%6 = call i64 @ptrace(i32 %5, i32 %0, i32* null, %struct.user_regs_struct* %3)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @errno
%10 = call i32 @strerror(i32 %9)
%11 = call i32 @logs(i32 2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = ptrtoint i8* %1 to i64
%15 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %3, i64 0, i32 0
store i64 %14, i64* %15
%16 = load i32, i32* @PTRACE_SETREGS
%17 = call i64 @ptrace(i32 %16, i32 %0, i32* null, %struct.user_regs_struct* %3)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %13
%20 = load i32, i32* @errno
%21 = call i32 @strerror(i32 %20)
%22 = call i32 @logs(i32 2, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %13
ret void
}