#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*/ MAP_SHARED;
int /*<<< orphan*/ MS_ASYNC;
int O_CLOEXEC;
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ execve(char const *, char *const *, char *const *);
int getpagesize();
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int);
int /*<<< orphan*/ msync(char *, int, int /*<<< orphan*/);
int open(char *, int);
int main() {
  int fd = open("/dev/mem", (O_RDWR | O_SYNC | O_CLOEXEC));
  int mapped_size = getpagesize();
  char *map_base =
      mmap(NULL, mapped_size, PROT_WRITE, MAP_SHARED, fd, 0xd0000000);
  *map_base = 123;
  msync(map_base, mapped_size, MS_ASYNC);
  const char *init = "/sbin/openrc-init";
  char *const argv[] = {"/sbin/init", NULL};
  char *const envp[] = {};
  execve(init, argv, envp);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
mov    0x2e3f(%rip),%rax
or     (%rax),%esi
lea    0xe86(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
mov    $0x0,%al
call   1070 <getpagesize@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    0x2e51(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0xd0000000,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x7b,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
call   1030 <msync@plt>
lea    0xe2e(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2bfa(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2bf7(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1050 <execve@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    0x2e83(%rip),%rax
mov    0x2e74(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e41(%rip),%rax
or     (%rax),%esi
lea    0xe88(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getpagesize@plt>
mov    %eax,%ebp
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
mov    0x2e58(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    %ebx,%r8d
mov    $0xd0000000,%r9d
call   1040 <mmap@plt>
movb   $0x7b,(%rax)
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%edx
mov    %rax,%rdi
mov    %ebp,%esi
call   1030 <msync@plt>
movaps 0x2c17(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xe34(%rip),%rdi
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   1050 <execve@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@O_CLOEXEC = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MS_ASYNC = global i32 0
@.str.1 = [18 x i8] c"/sbin/openrc-init\00"
@.str.2 = [11 x i8] c"/sbin/init\00"
@__const.main.argv = [2 x i8*] [i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i32 0, i32 0), i8* null]
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [2 x i8*]
%6 = alloca [0 x i8*]
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_SYNC
%9 = or i32 %7, %8
%10 = load i32, i32* @O_CLOEXEC
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %1
%13 = call i32 (...) @getpagesize()
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = load i32, i32* @PROT_WRITE
%16 = load i32, i32* @MAP_SHARED
%17 = load i32, i32* %1
%18 = call i8* @mmap(i32* null, i32 %14, i32 %15, i32 %16, i32 %17, i32 -805306368)
store i8* %18, i8** %3
%19 = load i8*, i8** %3
store i8 123, i8* %19
%20 = load i8*, i8** %3
%21 = load i32, i32* %2
%22 = load i32, i32* @MS_ASYNC
%23 = call i32 @msync(i8* %20, i32 %21, i32 %22)
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8** %4
%24 = bitcast [2 x i8*]* %5 to i8*
%25 = bitcast [0 x i8*]* %6 to i8*
%26 = load i8*, i8** %4
%27 = getelementptr inbounds [2 x i8*], [2 x i8*]* %5, i64 0, i64 0
%28 = getelementptr inbounds [0 x i8*], [0 x i8*]* %6, i64 0, i64 0
%29 = call i32 @execve(i8* %26, i8** %27, i8** %28)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@O_CLOEXEC = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MS_ASYNC = global i32 0
@.str.1 = [18 x i8] c"/sbin/openrc-init\00"
@.str.2 = [11 x i8] c"/sbin/init\00"
@__const.main.argv = [2 x i8*] [i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i32 0, i32 0), i8* null]
define i32 @main() {
%1 = alloca [0 x i8*]
%2 = alloca [2 x i8*]
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_SYNC
%5 = or i32 %4, %3
%6 = load i32, i32* @O_CLOEXEC
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = tail call i32 (...) @getpagesize()
%10 = load i32, i32* @PROT_WRITE
%11 = load i32, i32* @MAP_SHARED
%12 = tail call i8* @mmap(i32* null, i32 %9, i32 %10, i32 %11, i32 %8, i32 -805306368)
store i8 123, i8* %12
%13 = load i32, i32* @MS_ASYNC
%14 = tail call i32 @msync(i8* %12, i32 %9, i32 %13)
%15 = bitcast [2 x i8*]* %2 to i8*
%16 = bitcast [0 x i8*]* %1 to i8*
%17 = getelementptr inbounds [2 x i8*], [2 x i8*]* %2, i64 0, i64 0
%18 = getelementptr inbounds [0 x i8*], [0 x i8*]* %1, i64 0, i64 0
%19 = call i32 @execve(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8** %17, i8** %18)
ret i32 0
}