#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 */

/* Variables and functions */
int /*<<< orphan*/ AsmJump(void *, void *);
int ChangeMemPermissions(void *, int);
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
int abs(unsigned long);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sscanf(char *, char *, uintptr_t **);
int /*<<< orphan*/ stdin;
int sysconf(int /*<<< orphan*/);
void assemble_jump(void) {
  char str[17];
  uintptr_t *trgt = NULL;
  uintptr_t *dest = NULL;
  printf("\t[aj] Target Address: ");
  fgets(str, 17, stdin);
  sscanf(str, "%p", &trgt);
  printf("\t[aj] Destination Address: ");
  fgets(str, 17, stdin);
  sscanf(str, "%p", &dest);
  if (trgt == NULL || dest == NULL) {
    printf("\t[aj] ERROR: Unable to assemble jump from %p to %p\n", trgt, dest);
    return;
  } else {
    printf("\t[aj] Attempting to assemble jump from %p to %p\n", trgt, dest);
    int PageSize = sysconf(_SC_PAGESIZE);
    unsigned long offset = (unsigned long)trgt - ((unsigned long)dest + 5);
    unsigned long pc = (abs(offset) / PageSize);
    printf("\t[aj] Assuming that with page size of %d, %p is %lu pages away "
           "from %p\n",
           PageSize, trgt, pc, dest);
    if (ChangeMemPermissions((void *)trgt,
                             PROT_READ | PROT_WRITE | PROT_EXEC) == 0) {
      printf("\t[aj] Assembling jump...\n");
      AsmJump((void *)dest, (void *)trgt);
      printf("\t[aj] Fixing page permissions\n");
      if (ChangeMemPermissions((void *)trgt, PROT_READ | PROT_EXEC) == -1) {
        printf("\t[aj] WARN: Jump assembled, however page at %p is still "
               "writable\n",
               trgt);
      }
    } else {
      printf("\t[aj] ERROR: Unable to assemble jump from %p to %p\n", trgt,
             dest);
      return;
    }
  }
  printf("\t[aj] Over and out\n");
}


x86 assembly (clang, -O0):
<assemble_jump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x20(%rbp),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    $0x11,%esi
call   1070 <fgets@plt>
lea    -0x20(%rbp),%rdi
lea    0xe5f(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1080 <sscanf@plt>
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x20(%rbp),%rdi
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
mov    $0x11,%esi
call   1070 <fgets@plt>
lea    -0x20(%rbp),%rdi
lea    0xe26(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1080 <sscanf@plt>
cmpq   $0x0,-0x28(%rbp)
je     1210 <assemble_jump+0xa0>
cmpq   $0x0,-0x30(%rbp)
jne    122b <assemble_jump+0xbb>
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1354 <assemble_jump+0x1e4>
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
add    $0x5,%rcx
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <abs@plt>
cltd
idivl  -0x34(%rbp)
cltq
mov    %rax,-0x48(%rbp)
mov    -0x34(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x48(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
mov    0x2d17(%rip),%rax
or     (%rax),%esi
mov    0x2d1e(%rip),%rax
or     (%rax),%esi
call   1050 <ChangeMemPermissions@plt>
cmp    $0x0,%eax
jne    1326 <assemble_jump+0x1b6>
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <AsmJump@plt>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d02(%rip),%rax
mov    (%rax),%esi
mov    0x2cd1(%rip),%rax
or     (%rax),%esi
call   1050 <ChangeMemPermissions@plt>
cmp    $0xffffffff,%eax
jne    1321 <assemble_jump+0x1b1>
mov    -0x28(%rbp),%rsi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1341 <assemble_jump+0x1d1>
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    0xd01(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1354 <assemble_jump+0x1e4>
jmp    1346 <assemble_jump+0x1d6>
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<assemble_jump>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe5f(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2e31(%rip),%r15
mov    (%r15),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x11,%esi
call   1080 <fgets@plt>
lea    0xe4c(%rip),%r14
mov    %rsp,%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <sscanf@plt>
lea    0xe3a(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x11,%esi
call   1080 <fgets@plt>
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <sscanf@plt>
mov    (%rsp),%rsi
mov    0x8(%rsp),%rdx
test   %rsi,%rsi
je     12a3 <assemble_jump+0x123>
test   %rdx,%rdx
je     12a3 <assemble_jump+0x123>
lea    0xe41(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   10a0 <sysconf@plt>
mov    %eax,%ebx
mov    (%rsp),%rdi
sub    0x8(%rsp),%rdi
add    $0xfffffffffffffffb,%rdi
call   1050 <abs@plt>
cltd
idiv   %ebx
movslq %eax,%rcx
mov    (%rsp),%rdx
mov    0x8(%rsp),%r8
lea    0xe33(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2d80(%rip),%r14
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    (%rsp),%rdi
mov    0x2d41(%rip),%rbx
or     (%rbx),%esi
call   1060 <ChangeMemPermissions@plt>
test   %eax,%eax
je     12bb <assemble_jump+0x13b>
mov    (%rsp),%rsi
mov    0x8(%rsp),%rdx
lea    0xd8c(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe76(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%rdi
mov    (%rsp),%rsi
call   1040 <AsmJump@plt>
lea    0xe75(%rip),%rdi
call   1030 <puts@plt>
mov    (%rsp),%rdi
mov    (%rbx),%esi
or     (%r14),%esi
call   1060 <ChangeMemPermissions@plt>
cmp    $0xffffffff,%eax
jne    1306 <assemble_jump+0x186>
mov    (%rsp),%rsi
lea    0xde4(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe18(%rip),%rdi
call   1030 <puts@plt>
jmp    12b1 <assemble_jump+0x131>

LLVM IR (-O0):
@.str = [23 x i8] c"\09[aj] Target Address: \00"
@stdin = global i32 0
@.str.1 = [3 x i8] c"%p\00"
@.str.2 = [28 x i8] c"\09[aj] Destination Address: \00"
@.str.3 = [52 x i8] c"\09[aj] ERROR: Unable to assemble jump from %p to %p\0A\00"
@.str.4 = [49 x i8] c"\09[aj] Attempting to assemble jump from %p to %p\0A\00"
@_SC_PAGESIZE = global i32 0
@.str.5 = [72 x i8] c"\09[aj] Assuming that with page size of %d, %p is %lu pages away from %p\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@.str.6 = [26 x i8] c"\09[aj] Assembling jump...\0A\00"
@.str.7 = [31 x i8] c"\09[aj] Fixing page permissions\0A\00"
@.str.8 = [66 x i8] c"\09[aj] WARN: Jump assembled, however page at %p is still writable\0A\00"
@.str.9 = [20 x i8] c"\09[aj] Over and out\0A\00"
define void @assemble_jump() {
%1 = alloca [17 x i8]
%2 = alloca i64*
%3 = alloca i64*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
store i64* null, i64** %2
store i64* null, i64** %3
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [17 x i8], [17 x i8]* %1, i64 0, i64 0
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %8, i32 17, i32 %9)
%11 = getelementptr inbounds [17 x i8], [17 x i8]* %1, i64 0, i64 0
%12 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i64** %2)
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%14 = getelementptr inbounds [17 x i8], [17 x i8]* %1, i64 0, i64 0
%15 = load i32, i32* @stdin
%16 = call i32 @fgets(i8* %14, i32 17, i32 %15)
%17 = getelementptr inbounds [17 x i8], [17 x i8]* %1, i64 0, i64 0
%18 = call i32 @sscanf(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i64** %3)
%19 = load i64*, i64** %2
%20 = icmp eq i64* %19, null
br i1 %20, label %24, label %21
21: ; preds = %0
%22 = load i64*, i64** %3
%23 = icmp eq i64* %22, null
br i1 %23, label %24, label %28
24: ; preds = %21, %0
%25 = load i64*, i64** %2
%26 = load i64*, i64** %3
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i64* %25, i64* %26)
br label %85
28: ; preds = %21
%29 = load i64*, i64** %2
%30 = load i64*, i64** %3
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i64* %29, i64* %30)
%32 = load i32, i32* @_SC_PAGESIZE
%33 = call i32 @sysconf(i32 %32)
store i32 %33, i32* %4
%34 = load i64*, i64** %2
%35 = ptrtoint i64* %34 to i64
%36 = load i64*, i64** %3
%37 = ptrtoint i64* %36 to i64
%38 = add i64 %37, 5
%39 = sub i64 %35, %38
store i64 %39, i64* %5
%40 = load i64, i64* %5
%41 = call i32 @abs(i64 %40)
%42 = load i32, i32* %4
%43 = sdiv i32 %41, %42
%44 = sext i32 %43 to i64
store i64 %44, i64* %6
%45 = load i32, i32* %4
%46 = load i64*, i64** %2
%47 = load i64, i64* %6
%48 = load i64*, i64** %3
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.5, i64 0, i64 0), i32 %45, i64* %46, i64 %47, i64* %48)
%50 = load i64*, i64** %2
%51 = bitcast i64* %50 to i8*
%52 = load i32, i32* @PROT_READ
%53 = load i32, i32* @PROT_WRITE
%54 = or i32 %52, %53
%55 = load i32, i32* @PROT_EXEC
%56 = or i32 %54, %55
%57 = call i32 @ChangeMemPermissions(i8* %51, i32 %56)
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %78
59: ; preds = %28
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%61 = load i64*, i64** %3
%62 = bitcast i64* %61 to i8*
%63 = load i64*, i64** %2
%64 = bitcast i64* %63 to i8*
%65 = call i32 @AsmJump(i8* %62, i8* %64)
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0))
%67 = load i64*, i64** %2
%68 = bitcast i64* %67 to i8*
%69 = load i32, i32* @PROT_READ
%70 = load i32, i32* @PROT_EXEC
%71 = or i32 %69, %70
%72 = call i32 @ChangeMemPermissions(i8* %68, i32 %71)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %77
74: ; preds = %59
%75 = load i64*, i64** %2
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.8, i64 0, i64 0), i64* %75)
br label %77
77: ; preds = %74, %59
br label %82
78: ; preds = %28
%79 = load i64*, i64** %2
%80 = load i64*, i64** %3
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i64* %79, i64* %80)
br label %85
82: ; preds = %77
br label %83
83: ; preds = %82
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0))
br label %85
85: ; preds = %83, %78, %24
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c"\09[aj] Target Address: \00"
@stdin = global i32 0
@.str.1 = [3 x i8] c"%p\00"
@.str.2 = [28 x i8] c"\09[aj] Destination Address: \00"
@.str.3 = [52 x i8] c"\09[aj] ERROR: Unable to assemble jump from %p to %p\0A\00"
@.str.4 = [49 x i8] c"\09[aj] Attempting to assemble jump from %p to %p\0A\00"
@_SC_PAGESIZE = global i32 0
@.str.5 = [72 x i8] c"\09[aj] Assuming that with page size of %d, %p is %lu pages away from %p\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@.str.8 = [66 x i8] c"\09[aj] WARN: Jump assembled, however page at %p is still writable\0A\00"
@str = [19 x i8] c"\09[aj] Over and out\00"
@str.10 = [25 x i8] c"\09[aj] Assembling jump...\00"
@str.11 = [30 x i8] c"\09[aj] Fixing page permissions\00"
define void @assemble_jump() {
%1 = alloca [17 x i8]
%2 = alloca i64*
%3 = alloca i64*
%4 = getelementptr inbounds [17 x i8], [17 x i8]* %1, i64 0, i64 0
%5 = bitcast i64** %2 to i8*
store i64* null, i64** %2
%6 = bitcast i64** %3 to i8*
store i64* null, i64** %3
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @stdin
%9 = call i32 @fgets(i8* %4, i32 17, i32 %8)
%10 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i64** %2)
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%12 = load i32, i32* @stdin
%13 = call i32 @fgets(i8* %4, i32 17, i32 %12)
%14 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i64** %3)
%15 = load i64*, i64** %2
%16 = icmp eq i64* %15, null
%17 = load i64*, i64** %3
%18 = icmp eq i64* %17, null
%19 = select i1 %16, i1 true, i1 %18
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i64* %15, i64* %17)
br label %69
22: ; preds = %0
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i64* %15, i64* %17)
%24 = load i32, i32* @_SC_PAGESIZE
%25 = call i32 @sysconf(i32 %24)
%26 = load i64*, i64** %2
%27 = ptrtoint i64* %26 to i64
%28 = load i64*, i64** %3
%29 = ptrtoint i64* %28 to i64
%30 = add i64 %27, -5
%31 = sub i64 %30, %29
%32 = call i32 @abs(i64 %31)
%33 = sdiv i32 %32, %25
%34 = sext i32 %33 to i64
%35 = load i64*, i64** %2
%36 = load i64*, i64** %3
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([72 x i8], [72 x i8]* @.str.5, i64 0, i64 0), i32 %25, i64* %35, i64 %34, i64* %36)
%38 = bitcast i64** %2 to i8**
%39 = load i8*, i8** %38
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* @PROT_WRITE
%42 = or i32 %41, %40
%43 = load i32, i32* @PROT_EXEC
%44 = or i32 %42, %43
%45 = call i32 @ChangeMemPermissions(i8* %39, i32 %44)
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %63
47: ; preds = %22
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.10, i64 0, i64 0))
%49 = bitcast i64** %3 to i8**
%50 = load i8*, i8** %49
%51 = load i8*, i8** %38
%52 = call i32 @AsmJump(i8* %50, i8* %51)
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.11, i64 0, i64 0))
%54 = load i8*, i8** %38
%55 = load i32, i32* @PROT_READ
%56 = load i32, i32* @PROT_EXEC
%57 = or i32 %56, %55
%58 = call i32 @ChangeMemPermissions(i8* %54, i32 %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %67
60: ; preds = %47
%61 = load i64*, i64** %2
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str.8, i64 0, i64 0), i64* %61)
br label %67
63: ; preds = %22
%64 = load i64*, i64** %2
%65 = load i64*, i64** %3
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i64* %64, i64* %65)
br label %69
67: ; preds = %60, %47
%68 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %69
69: ; preds = %63, %67, %20
ret void
}