#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 PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int mprotect(void *, int, int);
int /*<<< orphan*/ perror(char *);
int substitute_function(void *old, void *new) {
  void *addr = (void *)((long)old & (~4095L));
  if (mprotect(addr, 4096, PROT_WRITE | PROT_READ | PROT_EXEC) == -1) {
    perror("mprotect:");
    return -1;
  }
  char instr[12] = {0x48, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xe0};
  *(void **)(instr + 2) = new;
  for (int i = 0; i < 12; i++)
    ((char *)old)[i] = instr[i];
  return 0;
}


x86 assembly (clang, -O0):
<substitute_function>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
and    $0xfffffffffffff000,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e87(%rip),%rax
mov    (%rax),%edx
mov    0x2ea6(%rip),%rax
or     (%rax),%edx
mov    0x2e85(%rip),%rax
or     (%rax),%edx
mov    $0x1000,%esi
call   1030 <mprotect@plt>
cmp    $0xffffffff,%eax
jne    1188 <substitute_function+0x68>
lea    0xe89(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dd <substitute_function+0xbd>
mov    0xe7b(%rip),%rax
mov    %rax,-0x2c(%rbp)
mov    0xe79(%rip),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x2a(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0xc,-0x30(%rbp)
jge    11d6 <substitute_function+0xb6>
movslq -0x30(%rbp),%rax
mov    -0x2c(%rbp,%rax,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11ab <substitute_function+0x8b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<substitute_function>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e9f(%rip),%rax
mov    0x2ec0(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
and    $0xfffffffffffff000,%rdi
mov    0x2e96(%rip),%rax
or     (%rax),%edx
mov    $0x1000,%esi
call   1030 <mprotect@plt>
cmp    $0xffffffff,%eax
je     1174 <substitute_function+0x54>
movw   $0xb848,(%rbx)
mov    %r14,0x2(%rbx)
movw   $0xe0ff,0xa(%rbx)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe85(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
jmp    116c <substitute_function+0x4c>

LLVM IR (-O0):
@PROT_WRITE = global i32 0
@PROT_READ = global i32 0
@PROT_EXEC = global i32 0
@.str = [10 x i8] c"mprotect:\00"
@__const.substitute_function.instr = [12 x i8] c"H\B8\00\00\00\00\00\00\00\00\FF\E0"
define i32 @substitute_function(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [12 x i8]
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = ptrtoint i8* %9 to i64
%11 = and i64 %10, -4096
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %6
%13 = load i8*, i8** %6
%14 = load i32, i32* @PROT_WRITE
%15 = load i32, i32* @PROT_READ
%16 = or i32 %14, %15
%17 = load i32, i32* @PROT_EXEC
%18 = or i32 %16, %17
%19 = call i32 @mprotect(i8* %13, i32 4096, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
23: ; preds = %2
%24 = bitcast [12 x i8]* %7 to i8*
%25 = load i8*, i8** %5
%26 = getelementptr inbounds [12 x i8], [12 x i8]* %7, i64 0, i64 0
%27 = getelementptr inbounds i8, i8* %26, i64 2
%28 = bitcast i8* %27 to i8**
store i8* %25, i8** %28
store i32 0, i32* %8
br label %29
29: ; preds = %41, %23
%30 = load i32, i32* %8
%31 = icmp slt i32 %30, 12
br i1 %31, label %32, label %44
32: ; preds = %29
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [12 x i8], [12 x i8]* %7, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = load i8*, i8** %4
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 %36, i8* %40
br label %41
41: ; preds = %32
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
br label %29
44: ; preds = %29
store i32 0, i32* %3
br label %45
45: ; preds = %44, %21
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@PROT_WRITE = global i32 0
@PROT_READ = global i32 0
@PROT_EXEC = global i32 0
@.str = [10 x i8] c"mprotect:\00"
define i32 @substitute_function(i8* %0, i8* %1) {
%3 = ptrtoint i8* %0 to i64
%4 = and i64 %3, -4096
%5 = inttoptr i64 %4 to i8*
%6 = load i32, i32* @PROT_WRITE
%7 = load i32, i32* @PROT_READ
%8 = or i32 %7, %6
%9 = load i32, i32* @PROT_EXEC
%10 = or i32 %8, %9
%11 = tail call i32 @mprotect(i8* %5, i32 4096, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %21
15: ; preds = %2
%16 = bitcast i8* %0 to i16*
store i16 -18360, i16* %16
%17 = getelementptr inbounds i8, i8* %0, i64 2
%18 = bitcast i8* %17 to i8**
store i8* %1, i8** %18
%19 = getelementptr inbounds i8, i8* %0, i64 10
%20 = bitcast i8* %19 to i16*
store i16 -7937, i16* %20
br label %21
21: ; preds = %15, %13
%22 = phi i32 [ -1, %13 ], [ 0, %15 ]
ret i32 %22
}