#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 unsigned long off_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
void *mmap(int /*<<< orphan*/, unsigned long, int, int /*<<< orphan*/, int,
           unsigned long);
int /*<<< orphan*/ munmap(void *, unsigned long);
void set_stack_pc(int fd, unsigned int stack, unsigned int pc) {
  off_t target = (off_t)0x007F8000;
  unsigned long read_result;
  void *map_base, *virt_addr;
  map_base =
      mmap(0, 16UL, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t)target);
  virt_addr = (unsigned char *)map_base + (target & (4096UL - 1));
  *((unsigned long *)virt_addr) = stack;
  virt_addr = (unsigned char *)map_base + ((target + 0x4) & (4096UL - 1));
  *((unsigned long *)virt_addr) = pc;
  munmap(map_base, 16UL);
}


x86 assembly (clang, -O0):
<set_stack_pc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movq   $0x7f8000,-0x18(%rbp)
mov    0x2eb8(%rip),%rax
mov    (%rax),%edx
mov    0x2e87(%rip),%rax
or     (%rax),%edx
mov    0x2e96(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
mov    -0x18(%rbp),%r9
xor    %edi,%edi
mov    $0x10,%esi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
and    $0xfff,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
add    $0x4,%rcx
and    $0xfff,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
mov    $0x10,%esi
call   1040 <munmap@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_stack_pc>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%ebp
mov    %edi,%r8d
mov    0x2ec7(%rip),%rax
mov    0x2e98(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2ea5(%rip),%rax
mov    (%rax),%ecx
mov    $0x10,%esi
mov    $0x7f8000,%r9d
xor    %edi,%edi
call   1030 <mmap@plt>
mov    %ebp,%ecx
mov    %rcx,(%rax)
mov    %ebx,%ecx
mov    %rcx,0x4(%rax)
mov    $0x10,%esi
mov    %rax,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <munmap@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define void @set_stack_pc(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i64 8355840, i64* %7
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %11, %12
%14 = load i32, i32* @MAP_SHARED
%15 = load i32, i32* %4
%16 = load i64, i64* %7
%17 = call i8* @mmap(i32 0, i64 16, i32 %13, i32 %14, i32 %15, i64 %16)
store i8* %17, i8** %9
%18 = load i8*, i8** %9
%19 = load i64, i64* %7
%20 = and i64 %19, 4095
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8* %21, i8** %10
%22 = load i32, i32* %5
%23 = zext i32 %22 to i64
%24 = load i8*, i8** %10
%25 = bitcast i8* %24 to i64*
store i64 %23, i64* %25
%26 = load i8*, i8** %9
%27 = load i64, i64* %7
%28 = add i64 %27, 4
%29 = and i64 %28, 4095
%30 = getelementptr inbounds i8, i8* %26, i64 %29
store i8* %30, i8** %10
%31 = load i32, i32* %6
%32 = zext i32 %31 to i64
%33 = load i8*, i8** %10
%34 = bitcast i8* %33 to i64*
store i64 %32, i64* %34
%35 = load i8*, i8** %9
%36 = call i32 @munmap(i8* %35, i64 16)
ret void
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define void @set_stack_pc(i32 %0, i32 %1, i32 %2) {
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %5, %4
%7 = load i32, i32* @MAP_SHARED
%8 = tail call i8* @mmap(i32 0, i64 16, i32 %6, i32 %7, i32 %0, i64 8355840)
%9 = zext i32 %1 to i64
%10 = bitcast i8* %8 to i64*
store i64 %9, i64* %10
%11 = getelementptr inbounds i8, i8* %8, i64 4
%12 = zext i32 %2 to i64
%13 = bitcast i8* %11 to i64*
store i64 %12, i64* %13
%14 = tail call i32 @munmap(i8* %8, i64 16)
ret void
}