#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_PRIVATE;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ _SC_PAGE_SIZE;
int /*<<< orphan*/ ftruncate(int, int);
char getchar();
unsigned long *malloc(int);
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int sysconf(int /*<<< orphan*/);
int main(void) {
  char c;
  int fd = open("1.bat", O_RDWR | O_CREAT | O_TRUNC, 0777);
  ftruncate(fd, 5000);
  int pagesize = sysconf(_SC_PAGE_SIZE);
  printf("pagesize = %d\n", pagesize);
  char *addr_mmap_p = mmap(NULL, pagesize, PROT_WRITE, MAP_PRIVATE, fd, 0);
  char *addr_mmap_s = mmap(NULL, pagesize, PROT_WRITE, MAP_SHARED, fd, 0);
  unsigned long *addr_malloc = malloc(sizeof(unsigned long));
  printf("addr_mmap_p: %p, addr_mmap_s: %p, addr_malloc: %p\n", addr_mmap_p,
         addr_mmap_s, addr_malloc);
  c = getchar();
  addr_mmap_p[pagesize - 1] = 0;
  addr_mmap_s[pagesize - 1] = 0;
  *addr_malloc = 0;
  c = getchar();
  printf("addr_mmap_p: %p, addr_mmap_s: %p, addr_malloc: %p\n", addr_mmap_p,
         addr_mmap_s, addr_malloc);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    0x2e69(%rip),%rax
or     (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
lea    0xe5f(%rip),%rdi
mov    $0x1ff,%edx
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x1388,%esi
call   1050 <ftruncate@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%esi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    0x2dc0(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%esi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
mov    0x2dc7(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    $0x8,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %al,-0x5(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %al,-0x5(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e51(%rip),%rax
mov    0x2e72(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e5f(%rip),%rax
or     (%rax),%esi
lea    0xe66(%rip),%rdi
mov    $0x1ff,%edx
call   1080 <open@plt>
mov    %eax,%r14d
mov    %eax,%edi
mov    $0x1388,%esi
call   1050 <ftruncate@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %eax,%r12d
lea    0xe3b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2de5(%rip),%rbx
mov    (%rbx),%edx
mov    0x2dcc(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r12d,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r15
mov    (%rbx),%edx
mov    0x2dde(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r12d,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    $0x8,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0xdea(%rip),%r14
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
movslq %r12d,%rax
movb   $0x0,-0x1(%r15,%rax,1)
movb   $0x0,-0x1(%rbp,%rax,1)
movq   $0x0,(%rbx)
xor    %eax,%eax
call   1060 <getchar@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"1.bat\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@_SC_PAGE_SIZE = global i32 0
@.str.1 = [15 x i8] c"pagesize = %d\0A\00"
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [51 x i8] c"addr_mmap_p: %p, addr_mmap_s: %p, addr_malloc: %p\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64*
store i32 0, i32* %1
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %12, i32 511)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = call i32 @ftruncate(i32 %14, i32 5000)
%16 = load i32, i32* @_SC_PAGE_SIZE
%17 = call i32 @sysconf(i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %4
%21 = load i32, i32* @PROT_WRITE
%22 = load i32, i32* @MAP_PRIVATE
%23 = load i32, i32* %3
%24 = call i8* @mmap(i32* null, i32 %20, i32 %21, i32 %22, i32 %23, i32 0)
store i8* %24, i8** %5
%25 = load i32, i32* %4
%26 = load i32, i32* @PROT_WRITE
%27 = load i32, i32* @MAP_SHARED
%28 = load i32, i32* %3
%29 = call i8* @mmap(i32* null, i32 %25, i32 %26, i32 %27, i32 %28, i32 0)
store i8* %29, i8** %6
%30 = call i64* @malloc(i32 8)
store i64* %30, i64** %7
%31 = load i8*, i8** %5
%32 = load i8*, i8** %6
%33 = load i64*, i64** %7
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i8* %31, i8* %32, i64* %33)
%35 = call signext i8 (...) @getchar()
store i8 %35, i8* %2
%36 = load i8*, i8** %5
%37 = load i32, i32* %4
%38 = sub nsw i32 %37, 1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %6
%42 = load i32, i32* %4
%43 = sub nsw i32 %42, 1
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %41, i64 %44
store i8 0, i8* %45
%46 = load i64*, i64** %7
store i64 0, i64* %46
%47 = call signext i8 (...) @getchar()
store i8 %47, i8* %2
%48 = load i8*, i8** %5
%49 = load i8*, i8** %6
%50 = load i64*, i64** %7
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i8* %48, i8* %49, i64* %50)
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"1.bat\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@_SC_PAGE_SIZE = global i32 0
@.str.1 = [15 x i8] c"pagesize = %d\0A\00"
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [51 x i8] c"addr_mmap_p: %p, addr_mmap_s: %p, addr_malloc: %p\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @O_TRUNC
%5 = or i32 %3, %4
%6 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %5, i32 511)
%7 = tail call i32 @ftruncate(i32 %6, i32 5000)
%8 = load i32, i32* @_SC_PAGE_SIZE
%9 = tail call i32 @sysconf(i32 %8)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @PROT_WRITE
%12 = load i32, i32* @MAP_PRIVATE
%13 = tail call i8* @mmap(i32* null, i32 %9, i32 %11, i32 %12, i32 %6, i32 0)
%14 = load i32, i32* @PROT_WRITE
%15 = load i32, i32* @MAP_SHARED
%16 = tail call i8* @mmap(i32* null, i32 %9, i32 %14, i32 %15, i32 %6, i32 0)
%17 = tail call i64* @malloc(i32 8)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i8* %13, i8* %16, i64* %17)
%19 = tail call signext i8 (...) @getchar()
%20 = add nsw i32 %9, -1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %13, i64 %21
store i8 0, i8* %22
%23 = getelementptr inbounds i8, i8* %16, i64 %21
store i8 0, i8* %23
store i64 0, i64* %17
%24 = tail call signext i8 (...) @getchar()
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i8* %13, i8* %16, i64* %17)
ret i32 0
}