#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
int MAP_ANONYMOUS;
int MAP_PRIVATE;
int MAP_SHARED;
int /*<<< orphan*/ MCL_CURRENT;
int /*<<< orphan*/ MS_SYNC;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ mlock(void *, int);
int /*<<< orphan*/ mlockall(int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ mprotect(void *, int, int);
void *mremap(void *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ msync(void *, int, int /*<<< orphan*/);
int /*<<< orphan*/ munlock(void *, int);
int /*<<< orphan*/ munlockall();
int /*<<< orphan*/ munmap(void *, int);
int open(char *, int, ...);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd, ret;
  struct stat fs;
  void *r;
  fd = open("foobar", O_WRONLY | O_CREAT | O_TRUNC, 0600);
  lseek(fd, 1024, SEEK_SET);
  write(fd, "abcdef", 6);
  close(fd);
  fd = open("foobar", O_RDONLY);
  ret = fstat(fd, &fs);
  r = mmap(NULL, fs.st_size, PROT_READ, MAP_SHARED, fd, 0);
  close(fd);
  mlock(r, fs.st_size);
  msync(r, fs.st_size, MS_SYNC);
  munlock(r, fs.st_size);
  mlockall(MCL_CURRENT);
  munlockall();
  munmap(r, fs.st_size);
  r = mmap(NULL, 12288, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1,
           0);
  mprotect(r, 4096, PROT_READ);
  munmap(r, 12288);
  r = mmap(NULL, 8192, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1,
           0);
  r = mremap(r, 8192, 4096, 0);
  munmap(r, 4096);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
mov    0x2df9(%rip),%rax
or     (%rax),%esi
mov    0x2de8(%rip),%rax
or     (%rax),%esi
lea    0xdef(%rip),%rdi
mov    $0x180,%edx
mov    $0x0,%al
call   10e0 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1080 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    0xdc7(%rip),%rsi
mov    $0x6,%edx
call   1050 <write@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   10e0 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1100 <fstat@plt>
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%esi
mov    0x2d63(%rip),%rax
mov    (%rax),%edx
mov    0x2d4a(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1040 <mlock@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    0x2ce2(%rip),%rax
mov    (%rax),%edx
call   1060 <msync@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1030 <munlock@plt>
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
call   10c0 <mlockall@plt>
mov    $0x0,%al
call   10f0 <munlockall@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   10b0 <munmap@plt>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edx
mov    0x2c8e(%rip),%rax
or     (%rax),%edx
mov    0x2c7d(%rip),%rax
mov    (%rax),%ecx
mov    0x2ca4(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x3000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2ca4(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   10d0 <mprotect@plt>
mov    -0x18(%rbp),%rdi
mov    $0x3000,%esi
call   10b0 <munmap@plt>
mov    0x2c83(%rip),%rax
mov    (%rax),%edx
mov    0x2c2a(%rip),%rax
or     (%rax),%edx
mov    0x2c19(%rip),%rax
mov    (%rax),%ecx
mov    0x2c40(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x2000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2000,%esi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   10a0 <mremap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x1000,%esi
call   10b0 <munmap@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2d8d(%rip),%rax
mov    0x2dfe(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2deb(%rip),%rax
or     (%rax),%esi
lea    0xdf2(%rip),%rbx
mov    %rbx,%rdi
mov    $0x180,%edx
xor    %eax,%eax
call   10e0 <open@plt>
mov    %eax,%ebp
mov    0x2d8a(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    $0x400,%esi
call   1080 <lseek@plt>
lea    0xdcc(%rip),%rsi
mov    %ebp,%edi
mov    $0x6,%edx
call   1050 <write@plt>
mov    %ebp,%edi
call   1090 <close@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   10e0 <open@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1100 <fstat@plt>
mov    0x8(%rsp),%r14d
mov    0x2d6d(%rip),%r15
mov    (%r15),%edx
mov    0x2d53(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1090 <close@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <mlock@plt>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1060 <msync@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <munlock@plt>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
call   10c0 <mlockall@plt>
xor    %eax,%eax
call   10f0 <munlockall@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   10b0 <munmap@plt>
mov    0x2ca6(%rip),%r12
mov    (%r12),%edx
or     (%r15),%edx
mov    0x2c90(%rip),%r14
mov    0x2cb9(%rip),%rbp
mov    0x0(%rbp),%ecx
or     (%r14),%ecx
xor    %edi,%edi
mov    $0x3000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,%rbx
mov    (%r15),%edx
mov    %rax,%rdi
mov    $0x1000,%esi
call   10d0 <mprotect@plt>
mov    %rbx,%rdi
mov    $0x3000,%esi
call   10b0 <munmap@plt>
mov    (%r12),%edx
or     (%r15),%edx
mov    0x0(%rbp),%ecx
or     (%r14),%ecx
xor    %edi,%edi
mov    $0x2000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,%rdi
mov    $0x2000,%esi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   10a0 <mremap@plt>
mov    %rax,%rdi
mov    $0x1000,%esi
call   10b0 <munmap@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [7 x i8] c"foobar\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [7 x i8] c"abcdef\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MS_SYNC = global i32 0
@MCL_CURRENT = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i8*
store i32 0, i32* %1
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %10, i32 384)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = load i32, i32* @SEEK_SET
%14 = call i32 @lseek(i32 %12, i32 1024, i32 %13)
%15 = load i32, i32* %2
%16 = call i32 @write(i32 %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%17 = load i32, i32* %2
%18 = call i32 @close(i32 %17)
%19 = load i32, i32* @O_RDONLY
%20 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %19)
store i32 %20, i32* %2
%21 = load i32, i32* %2
%22 = call i32 @fstat(i32 %21, %struct.stat* %4)
store i32 %22, i32* %3
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @MAP_SHARED
%27 = load i32, i32* %2
%28 = call i8* @mmap(i32* null, i32 %24, i32 %25, i32 %26, i32 %27, i32 0)
store i8* %28, i8** %5
%29 = load i32, i32* %2
%30 = call i32 @close(i32 %29)
%31 = load i8*, i8** %5
%32 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @mlock(i8* %31, i32 %33)
%35 = load i8*, i8** %5
%36 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%37 = load i32, i32* %36
%38 = load i32, i32* @MS_SYNC
%39 = call i32 @msync(i8* %35, i32 %37, i32 %38)
%40 = load i8*, i8** %5
%41 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @munlock(i8* %40, i32 %42)
%44 = load i32, i32* @MCL_CURRENT
%45 = call i32 @mlockall(i32 %44)
%46 = call i32 (...) @munlockall()
%47 = load i8*, i8** %5
%48 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 @munmap(i8* %47, i32 %49)
%51 = load i32, i32* @PROT_READ
%52 = load i32, i32* @PROT_WRITE
%53 = or i32 %51, %52
%54 = load i32, i32* @MAP_PRIVATE
%55 = load i32, i32* @MAP_ANONYMOUS
%56 = or i32 %54, %55
%57 = call i8* @mmap(i32* null, i32 12288, i32 %53, i32 %56, i32 -1, i32 0)
store i8* %57, i8** %5
%58 = load i8*, i8** %5
%59 = load i32, i32* @PROT_READ
%60 = call i32 @mprotect(i8* %58, i32 4096, i32 %59)
%61 = load i8*, i8** %5
%62 = call i32 @munmap(i8* %61, i32 12288)
%63 = load i32, i32* @PROT_READ
%64 = load i32, i32* @PROT_WRITE
%65 = or i32 %63, %64
%66 = load i32, i32* @MAP_PRIVATE
%67 = load i32, i32* @MAP_ANONYMOUS
%68 = or i32 %66, %67
%69 = call i8* @mmap(i32* null, i32 8192, i32 %65, i32 %68, i32 -1, i32 0)
store i8* %69, i8** %5
%70 = load i8*, i8** %5
%71 = call i8* @mremap(i8* %70, i32 8192, i32 4096, i32 0)
store i8* %71, i8** %5
%72 = load i8*, i8** %5
%73 = call i32 @munmap(i8* %72, i32 4096)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [7 x i8] c"foobar\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [7 x i8] c"abcdef\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MS_SYNC = global i32 0
@MCL_CURRENT = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
define i32 @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %7, i32 384)
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @lseek(i32 %8, i32 1024, i32 %9)
%11 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%12 = tail call i32 @close(i32 %8)
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = call i32 @fstat(i32 %14, %struct.stat* %1)
%16 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @MAP_SHARED
%20 = tail call i8* @mmap(i32* null, i32 %17, i32 %18, i32 %19, i32 %14, i32 0)
%21 = tail call i32 @close(i32 %14)
%22 = tail call i32 @mlock(i8* %20, i32 %17)
%23 = load i32, i32* @MS_SYNC
%24 = tail call i32 @msync(i8* %20, i32 %17, i32 %23)
%25 = tail call i32 @munlock(i8* %20, i32 %17)
%26 = load i32, i32* @MCL_CURRENT
%27 = tail call i32 @mlockall(i32 %26)
%28 = tail call i32 (...) @munlockall()
%29 = tail call i32 @munmap(i8* %20, i32 %17)
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @PROT_WRITE
%32 = or i32 %31, %30
%33 = load i32, i32* @MAP_PRIVATE
%34 = load i32, i32* @MAP_ANONYMOUS
%35 = or i32 %34, %33
%36 = tail call i8* @mmap(i32* null, i32 12288, i32 %32, i32 %35, i32 -1, i32 0)
%37 = load i32, i32* @PROT_READ
%38 = tail call i32 @mprotect(i8* %36, i32 4096, i32 %37)
%39 = tail call i32 @munmap(i8* %36, i32 12288)
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* @PROT_WRITE
%42 = or i32 %41, %40
%43 = load i32, i32* @MAP_PRIVATE
%44 = load i32, i32* @MAP_ANONYMOUS
%45 = or i32 %44, %43
%46 = tail call i8* @mmap(i32* null, i32 8192, i32 %42, i32 %45, i32 -1, i32 0)
%47 = tail call i8* @mremap(i8* %46, i32 8192, i32 4096, i32 0)
%48 = tail call i32 @munmap(i8* %47, i32 4096)
ret i32 0
}