#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 int /*<<< orphan*/ off_t;
typedef int mode_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_SET;
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
int /*<<< orphan*/ close(int);
int getpid();
int getppid();
int /*<<< orphan*/ lseek(int, size_t, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
size_t sprintf(char *, char *, int, int, unsigned long);
int /*<<< orphan*/ sync();
int /*<<< orphan*/ write(int, char *, int);
int lxc_mmap() {
  int fd = -1;
  void *map = NULL;
  char *ptr;
  size_t length = 8192 * 5;
  off_t off = 0;
  size_t wsize;
  unsigned long slot = 0;
  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
  fd = open("./mmap.file", O_RDWR | O_CREAT, mode);
  if (fd == -1) {
    printf("open mmap file failed.\n");
    return -1;
  }
  lseek(fd, length + 1, SEEK_SET);
  write(fd, "*\n", 2);
  lseek(fd, 0, SEEK_SET);
  map = mmap(NULL, length, PROT_WRITE | PROT_READ, MAP_SHARED, fd, off);
  if (!map) {
    close(fd);
    printf("map file failed.\n");
    return -2;
  }
  ptr = (char *)map;
  while (1) {
    wsize = sprintf(ptr, "I am process %d from process %d, Now it's %ld\n",
                    getpid(), getppid(), slot);
    printf("---I am process %d from process %d, Now it's %ld\n", getpid(),
           getppid(), slot);
    slot++;
    sleep(20);
    ptr += wsize;
  }
  sync();
  munmap(map, length);
  return 0;
}


x86 assembly (clang, -O0):
<lxc_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0xffffffff,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0xa000,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    0x2dda(%rip),%rcx
or     (%rcx),%eax
mov    0x2df9(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    0x2dfc(%rip),%rax
or     (%rax),%esi
mov    -0x3c(%rbp),%edx
lea    0xdf8(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1234 <lxc_mmap+0x94>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136c <lxc_mmap+0x1cc>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
mov    0x2d62(%rip),%rax
mov    (%rax),%edx
call   1070 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    0xdcd(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
mov    0x2d3d(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <lseek@plt>
mov    -0x20(%rbp),%rsi
mov    0x2d1f(%rip),%rax
mov    (%rax),%edx
mov    0x2d66(%rip),%rax
or     (%rax),%edx
mov    0x2d45(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
mov    -0x24(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1050 <mmap@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12d7 <lxc_mmap+0x137>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    136c <lxc_mmap+0x1cc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x58(%rbp)
mov    $0x0,%al
call   10b0 <getppid@plt>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%edx
mov    %eax,%ecx
mov    -0x38(%rbp),%r8
lea    0xd1e(%rip),%rsi
call   10a0 <sprintf@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x54(%rbp)
mov    $0x0,%al
call   10b0 <getppid@plt>
mov    -0x54(%rbp),%esi
mov    %eax,%edx
mov    -0x38(%rbp),%rcx
lea    0xcf1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
mov    %rax,-0x38(%rbp)
mov    $0x14,%edi
call   10c0 <sleep@plt>
mov    -0x30(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    %rax,-0x18(%rbp)
jmp    12df <lxc_mmap+0x13f>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lxc_mmap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e06(%rip),%rax
mov    0x2def(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0c(%rip),%rax
or     (%rax),%edx
mov    0x2de3(%rip),%rax
mov    0x2e14(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe11(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12d9 <lxc_mmap+0x129>
mov    %eax,%ebp
mov    0x2da2(%rip),%rbx
mov    (%rbx),%edx
mov    $0xa001,%esi
mov    %eax,%edi
call   1080 <lseek@plt>
lea    0xdf1(%rip),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1040 <write@plt>
mov    (%rbx),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1080 <lseek@plt>
mov    0x2d67(%rip),%rax
mov    0x2db0(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d8d(%rip),%rax
mov    (%rax),%ecx
mov    $0xa000,%esi
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
test   %rax,%rax
je     12e7 <lxc_mmap+0x137>
mov    %rax,%r12
lea    0xda0(%rip),%r14
lea    0xd96(%rip),%r15
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10c0 <getppid@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
mov    %eax,%ecx
mov    %rbp,%r8
call   10b0 <sprintf@plt>
mov    %rax,%r13
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10c0 <getppid@plt>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %eax,%edx
mov    %rbp,%rcx
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%rbp
mov    $0x14,%edi
call   10d0 <sleep@plt>
add    %r13,%r12
jmp    1280 <lxc_mmap+0xd0>
mov    $0xffffffff,%ebx
lea    0xd5c(%rip),%rdi
jmp    12fa <lxc_mmap+0x14a>
mov    %ebp,%edi
call   1090 <close@plt>
mov    $0xfffffffe,%ebx
lea    0xd4d(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str = [12 x i8] c"./mmap.file\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [24 x i8] c"open mmap file failed.\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [3 x i8] c"*\0A\00"
@PROT_WRITE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [18 x i8] c"map file failed.\0A\00"
@.str.4 = [47 x i8] c"I am process %d from process %d, Now it's %ld\0A\00"
@.str.5 = [50 x i8] c"---I am process %d from process %d, Now it's %ld\0A\00"
define i32 @lxc_mmap() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i32 -1, i32* %2
store i8* null, i8** %3
store i64 40960, i64* %5
store i32 0, i32* %6
store i64 0, i64* %8
%10 = load i32, i32* @S_IRWXU
%11 = load i32, i32* @S_IRWXG
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRWXO
%14 = or i32 %12, %13
store i32 %14, i32* %9
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = load i32, i32* %9
%19 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %18)
store i32 %19, i32* %2
%20 = load i32, i32* %2
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %0
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %67
24: ; preds = %0
%25 = load i32, i32* %2
%26 = load i64, i64* %5
%27 = add i64 %26, 1
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @lseek(i32 %25, i64 %27, i32 %28)
%30 = load i32, i32* %2
%31 = call i32 @write(i32 %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%32 = load i32, i32* %2
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @lseek(i32 %32, i64 0, i32 %33)
%35 = load i64, i64* %5
%36 = load i32, i32* @PROT_WRITE
%37 = load i32, i32* @PROT_READ
%38 = or i32 %36, %37
%39 = load i32, i32* @MAP_SHARED
%40 = load i32, i32* %2
%41 = load i32, i32* %6
%42 = call i8* @mmap(i32* null, i64 %35, i32 %38, i32 %39, i32 %40, i32 %41)
store i8* %42, i8** %3
%43 = load i8*, i8** %3
%44 = icmp ne i8* %43, null
br i1 %44, label %49, label %45
45: ; preds = %24
%46 = load i32, i32* %2
%47 = call i32 @close(i32 %46)
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
store i32 -2, i32* %1
br label %67
49: ; preds = %24
%50 = load i8*, i8** %3
store i8* %50, i8** %4
br label %51
51: ; preds = %49, %51
%52 = load i8*, i8** %4
%53 = call i32 (...) @getpid()
%54 = call i32 (...) @getppid()
%55 = load i64, i64* %8
%56 = call i64 @sprintf(i8* %52, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %53, i32 %54, i64 %55)
store i64 %56, i64* %7
%57 = call i32 (...) @getpid()
%58 = call i32 (...) @getppid()
%59 = load i64, i64* %8
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0), i32 %57, i32 %58, i64 %59)
%61 = load i64, i64* %8
%62 = add i64 %61, 1
store i64 %62, i64* %8
%63 = call i32 @sleep(i32 20)
%64 = load i64, i64* %7
%65 = load i8*, i8** %4
%66 = getelementptr inbounds i8, i8* %65, i64 %64
store i8* %66, i8** %4
br label %51
67: ; preds = %45, %22
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str = [12 x i8] c"./mmap.file\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [3 x i8] c"*\0A\00"
@PROT_WRITE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.4 = [47 x i8] c"I am process %d from process %d, Now it's %ld\0A\00"
@.str.5 = [50 x i8] c"---I am process %d from process %d, Now it's %ld\0A\00"
@str = [17 x i8] c"map file failed.\00"
@str.6 = [23 x i8] c"open mmap file failed.\00"
define i32 @lxc_mmap() {
%1 = load i32, i32* @S_IRWXU
%2 = load i32, i32* @S_IRWXG
%3 = or i32 %2, %1
%4 = load i32, i32* @S_IRWXO
%5 = or i32 %3, %4
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %5)
%10 = icmp eq i32 %9, -1
br i1 %10, label %37, label %11
11: ; preds = %0
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @lseek(i32 %9, i64 40961, i32 %12)
%14 = tail call i32 @write(i32 %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @lseek(i32 %9, i64 0, i32 %15)
%17 = load i32, i32* @PROT_WRITE
%18 = load i32, i32* @PROT_READ
%19 = or i32 %18, %17
%20 = load i32, i32* @MAP_SHARED
%21 = tail call i8* @mmap(i32* null, i64 40960, i32 %19, i32 %20, i32 %9, i32 0)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %11
%24 = tail call i32 @close(i32 %9)
br label %37
25: ; preds = %11, %25
%26 = phi i8* [ %36, %25 ], [ %21, %11 ]
%27 = phi i64 [ %34, %25 ], [ 0, %11 ]
%28 = tail call i32 (...) @getpid()
%29 = tail call i32 (...) @getppid()
%30 = tail call i64 @sprintf(i8* %26, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %28, i32 %29, i64 %27)
%31 = tail call i32 (...) @getpid()
%32 = tail call i32 (...) @getppid()
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0), i32 %31, i32 %32, i64 %27)
%34 = add i64 %27, 1
%35 = tail call i32 @sleep(i32 20)
%36 = getelementptr inbounds i8, i8* %26, i64 %30
br label %25
37: ; preds = %0, %23
%38 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0), %23 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @str.6, i64 0, i64 0), %0 ]
%39 = phi i32 [ -2, %23 ], [ -1, %0 ]
%40 = tail call i32 @puts(i8* dereferenceable(1) %38)
ret i32 %39
}