#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ftruncate(int, int);
int getpid();
int /*<<< orphan*/ memset(void *, int, int);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main() {
  int fd, bytes;
  int i;
  char fname[128];
  void *fp_ptr1, *fp_ptr2, *fp_ptr3;
  sprintf(fname, "/tmp/mmap_test.%d", getpid());
  fd = open(fname, O_CREAT | O_RDWR, 0660);
  if (fd < 0) {
    perror("open");
    return 1;
  }
  printf("file desc = %d\n", fd);
  ftruncate(fd, 4096 * 10 / 2);
  fp_ptr1 = mmap(NULL, 4096 * 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (fp_ptr1 == MAP_FAILED) {
    perror("mmap");
    return 2;
  }
  close(fd);
  memset(fp_ptr1, 0xaa, 4096 * 10);
  munmap(fp_ptr1, 4096 * 10);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0xb0(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0xb0(%rbp),%rdi
mov    %eax,%edx
lea    0xe29(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
or     (%rax),%esi
mov    $0x1b0,%edx
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1224 <main+0x84>
lea    0xdff(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d5 <main+0x135>
mov    -0x8(%rbp),%esi
lea    0xde9(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
mov    $0x5000,%esi
call   1060 <ftruncate@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
mov    0x2d70(%rip),%rax
or     (%rax),%edx
mov    0x2d87(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0xa000,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x98(%rbp)
mov    -0x98(%rbp),%rax
mov    0x2d33(%rip),%rcx
cmp    (%rcx),%rax
jne    12a6 <main+0x106>
lea    0xd92(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12d5 <main+0x135>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
mov    -0x98(%rbp),%rdi
mov    $0xaa,%esi
mov    $0xa000,%edx
call   1070 <memset@plt>
mov    -0x98(%rbp),%rdi
mov    $0xa000,%esi
call   1090 <munmap@plt>
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x80,%rsp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe46(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   10c0 <sprintf@plt>
mov    0x2e2a(%rip),%rax
mov    0x2dfb(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1b0,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     127f <main+0xdf>
mov    %eax,%ebx
lea    0xe20(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %ebx,%edi
mov    $0x5000,%esi
call   1060 <ftruncate@plt>
mov    0x2ddf(%rip),%rax
mov    0x2da8(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dbd(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0xa000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r15
mov    0x2d74(%rip),%rax
cmp    (%rax),%r15
je     1293 <main+0xf3>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r15,%rdi
mov    $0xaa,%esi
mov    $0xa000,%edx
call   1070 <memset@plt>
mov    %r15,%rdi
mov    $0xa000,%esi
call   1090 <munmap@plt>
mov    %r14d,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd8c(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%r14d
jmp    126f <main+0xcf>
lea    0xd8d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%r14d
jmp    126f <main+0xcf>

LLVM IR (-O0):
@.str = [18 x i8] c"/tmp/mmap_test.%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [16 x i8] c"file desc = %d\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [128 x i8]
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i32 0, i32* %1
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%10 = call i32 (...) @getpid()
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_RDWR
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %12, i32 %15, i32 432)
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %0
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %44
21: ; preds = %0
%22 = load i32, i32* %2
%23 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %2
%25 = call i32 @ftruncate(i32 %24, i32 20480)
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @PROT_WRITE
%28 = or i32 %26, %27
%29 = load i32, i32* @MAP_SHARED
%30 = load i32, i32* %2
%31 = call i8* @mmap(i32* null, i32 40960, i32 %28, i32 %29, i32 %30, i32 0)
store i8* %31, i8** %6
%32 = load i8*, i8** %6
%33 = load i8*, i8** @MAP_FAILED
%34 = icmp eq i8* %32, %33
br i1 %34, label %35, label %37
35: ; preds = %21
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %44
37: ; preds = %21
%38 = load i32, i32* %2
%39 = call i32 @close(i32 %38)
%40 = load i8*, i8** %6
%41 = call i32 @memset(i8* %40, i32 170, i32 40960)
%42 = load i8*, i8** %6
%43 = call i32 @munmap(i8* %42, i32 40960)
br label %44
44: ; preds = %37, %35, %19
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@.str = [18 x i8] c"/tmp/mmap_test.%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [16 x i8] c"file desc = %d\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
define i32 @main() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @getpid()
%4 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDWR
%7 = or i32 %6, %5
%8 = call i32 @open(i8* %2, i32 %7, i32 432)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %28
12: ; preds = %0
%13 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%14 = tail call i32 @ftruncate(i32 %8, i32 20480)
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %16, %15
%18 = load i32, i32* @MAP_SHARED
%19 = tail call i8* @mmap(i32* null, i32 40960, i32 %17, i32 %18, i32 %8, i32 0)
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %19, %20
br i1 %21, label %22, label %24
22: ; preds = %12
%23 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %28
24: ; preds = %12
%25 = tail call i32 @close(i32 %8)
%26 = tail call i32 @memset(i8* %19, i32 170, i32 40960)
%27 = tail call i32 @munmap(i8* %19, i32 40960)
br label %28
28: ; preds = %24, %22, %10
%29 = phi i32 [ 1, %10 ], [ 2, %22 ], [ 0, %24 ]
ret i32 %29
}