#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*/ tmpfname;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int ftruncate(int, long);
int getpid();
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int munmap(void *, size_t);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  int rc;
  unsigned long cnt;
  char tmpfname[256];
  long total_size;
  void *pa;
  size_t size;
  int fd;
  total_size = 1024;
  size = total_size;
  snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_10_1_%d", getpid());
  unlink(tmpfname);
  fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    printf("Error at open(): %s\n", strerror(errno));
    return 2;
  }
  unlink(tmpfname);
  if (ftruncate(fd, total_size) == -1) {
    printf("Error at ftruncate(): %s\n", strerror(errno));
    return 2;
  }
  for (cnt = 0; cnt < 100000; cnt++) {
    pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (pa == MAP_FAILED) {
      printf("Test FAILED: Error at mmap: %s\n", strerror(errno));
      return 1;
    }
    if (pa == NULL) {
      printf("Test FAILED:"
             " mmap() map the file to 0 address "
             "without setting MAP_FIXED\n");
      return 1;
    }
    rc = munmap(pa, size);
    if (rc != 0) {
      printf("Error at mnumap(): %s\n", strerror(errno));
      return 2;
    }
  }
  close(fd);
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x400,-0x118(%rbp)
mov    -0x118(%rbp),%rax
mov    %rax,-0x128(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x138(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x138(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xe0b(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    0x2db3(%rip),%rax
or     (%rax),%esi
mov    0x2d92(%rip),%rax
or     (%rax),%esi
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
mov    0x2d90(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x12c(%rbp)
cmpl   $0xffffffff,-0x12c(%rbp)
jne    127d <main+0xdd>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    140c <main+0x26c>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x12c(%rbp),%edi
mov    -0x118(%rbp),%rsi
call   1080 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    12cf <main+0x12f>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    140c <main+0x26c>
movq   $0x0,-0x10(%rbp)
cmpq   $0x186a0,-0x10(%rbp)
jae    13ec <main+0x24c>
mov    -0x128(%rbp),%rsi
mov    0x2cfd(%rip),%rax
mov    (%rax),%edx
mov    0x2cac(%rip),%rax
or     (%rax),%edx
mov    0x2cdb(%rip),%rax
mov    (%rax),%ecx
mov    -0x12c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rax
mov    0x2c69(%rip),%rcx
cmp    (%rcx),%rax
jne    1363 <main+0x1c3>
mov    0x2c71(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xcf5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    140c <main+0x26c>
cmpq   $0x0,-0x120(%rbp)
jne    138b <main+0x1eb>
lea    0xced(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    140c <main+0x26c>
mov    -0x120(%rbp),%rdi
mov    -0x128(%rbp),%rsi
call   10a0 <munmap@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     13d6 <main+0x236>
mov    0x2bfe(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xceb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    140c <main+0x26c>
jmp    13db <main+0x23b>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    12d7 <main+0x137>
mov    -0x12c(%rbp),%edi
call   1090 <close@plt>
lea    0xcc7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe31(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2e08(%rip),%rax
mov    0x2dd9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2db6(%rip),%rax
or     (%rax),%esi
mov    0x2d95(%rip),%rax
mov    0x2db6(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     12d0 <main+0x120>
mov    %eax,%ebp
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    $0x400,%esi
mov    %ebp,%edi
call   1090 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     12e7 <main+0x137>
mov    $0x186a0,%ebx
mov    0x2d9e(%rip),%r14
mov    0x2d4f(%rip),%r15
mov    0x2d80(%rip),%r12
mov    0x2d31(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    (%r15),%edx
or     (%r14),%edx
mov    (%r12),%ecx
mov    $0x400,%esi
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
cmp    0x0(%r13),%rax
je     12fe <main+0x14e>
test   %rax,%rax
je     1324 <main+0x174>
mov    $0x400,%esi
mov    %rax,%rdi
call   10b0 <munmap@plt>
test   %eax,%eax
jne    1337 <main+0x187>
add    $0xffffffffffffffff,%rbx
jne    1270 <main+0xc0>
mov    %ebp,%edi
call   10a0 <close@plt>
lea    0xdb8(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    135b <main+0x1ab>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd31(%rip),%rdi
jmp    134c <main+0x19c>
mov    0x2cc2(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd2f(%rip),%rdi
jmp    134c <main+0x19c>
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd32(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%eax
jmp    135b <main+0x1ab>
lea    0xd5d(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    135b <main+0x1ab>
mov    0x2c72(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd19(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"/tmp/pts_mmap_10_1_%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [21 x i8] c"Error at open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"Error at ftruncate(): %s\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [32 x i8] c"Test FAILED: Error at mmap: %s\0A\00"
@.str.4 = [73 x i8] c"Test FAILED: mmap() map the file to 0 address without setting MAP_FIXED\0A\00"
@.str.5 = [23 x i8] c"Error at mnumap(): %s\0A\00"
@.str.6 = [13 x i8] c"Test PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca [256 x i8]
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32 0, i32* %1
store i64 1024, i64* %5
%9 = load i64, i64* %5
store i64 %9, i64* %7
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%11 = call i32 (...) @getpid()
%12 = call i32 @snprintf(i8* %10, i32 256, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%14 = call i32 @unlink(i8* %13)
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_RDWR
%18 = or i32 %16, %17
%19 = load i32, i32* @O_EXCL
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %15, i32 %20, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %0
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i32 2, i32* %1
br label %84
31: ; preds = %0
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%33 = call i32 @unlink(i8* %32)
%34 = load i32, i32* %8
%35 = load i64, i64* %5
%36 = call i32 @ftruncate(i32 %34, i64 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %31
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %40)
store i32 2, i32* %1
br label %84
42: ; preds = %31
store i64 0, i64* %3
br label %43
43: ; preds = %77, %42
%44 = load i64, i64* %3
%45 = icmp ult i64 %44, 100000
br i1 %45, label %46, label %80
46: ; preds = %43
%47 = load i64, i64* %7
%48 = load i32, i32* @PROT_READ
%49 = load i32, i32* @PROT_WRITE
%50 = or i32 %48, %49
%51 = load i32, i32* @MAP_SHARED
%52 = load i32, i32* %8
%53 = call i8* @mmap(i32* null, i64 %47, i32 %50, i32 %51, i32 %52, i32 0)
store i8* %53, i8** %6
%54 = load i8*, i8** %6
%55 = load i8*, i8** @MAP_FAILED
%56 = icmp eq i8* %54, %55
br i1 %56, label %57, label %61
57: ; preds = %46
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %59)
store i32 1, i32* %1
br label %84
61: ; preds = %46
%62 = load i8*, i8** %6
%63 = icmp eq i8* %62, null
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %84
66: ; preds = %61
%67 = load i8*, i8** %6
%68 = load i64, i64* %7
%69 = call i32 @munmap(i8* %67, i64 %68)
store i32 %69, i32* %2
%70 = load i32, i32* %2
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %76
72: ; preds = %66
%73 = load i32, i32* @errno
%74 = call i8* @strerror(i32 %73)
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %74)
store i32 2, i32* %1
br label %84
76: ; preds = %66
br label %77
77: ; preds = %76
%78 = load i64, i64* %3
%79 = add i64 %78, 1
store i64 %79, i64* %3
br label %43
80: ; preds = %43
%81 = load i32, i32* %8
%82 = call i32 @close(i32 %81)
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %1
br label %84
84: ; preds = %80, %72, %64, %57, %38, %27
%85 = load i32, i32* %1
ret i32 %85
}

LLVM IR (-O2):
@.str = [22 x i8] c"/tmp/pts_mmap_10_1_%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [21 x i8] c"Error at open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [26 x i8] c"Error at ftruncate(): %s\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [32 x i8] c"Test FAILED: Error at mmap: %s\0A\00"
@.str.5 = [23 x i8] c"Error at mnumap(): %s\0A\00"
@str = [12 x i8] c"Test PASSED\00"
@str.7 = [72 x i8] c"Test FAILED: mmap() map the file to 0 address without setting MAP_FIXED\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @getpid()
%4 = call i32 @snprintf(i8* %2, i32 256, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i32 @unlink(i8* %2)
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_RDWR
%8 = or i32 %7, %6
%9 = load i32, i32* @O_EXCL
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = call i32 @open(i8* %2, i32 %10, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %0
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %58
20: ; preds = %0
%21 = call i32 @unlink(i8* %2)
%22 = call i32 @ftruncate(i32 %14, i64 1024)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %31
24: ; preds = %20
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %58
28: ; preds = %48
%29 = add nuw nsw i64 %32, 1
%30 = icmp eq i64 %29, 100000
br i1 %30, label %55, label %31
31: ; preds = %20, %28
%32 = phi i64 [ %29, %28 ], [ 0, %20 ]
%33 = load i32, i32* @PROT_READ
%34 = load i32, i32* @PROT_WRITE
%35 = or i32 %34, %33
%36 = load i32, i32* @MAP_SHARED
%37 = call i8* @mmap(i32* null, i64 1024, i32 %35, i32 %36, i32 %14, i32 0)
%38 = load i8*, i8** @MAP_FAILED
%39 = icmp eq i8* %37, %38
br i1 %39, label %40, label %44
40: ; preds = %31
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %42)
br label %58
44: ; preds = %31
%45 = icmp eq i8* %37, null
br i1 %45, label %46, label %48
46: ; preds = %44
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([72 x i8], [72 x i8]* @str.7, i64 0, i64 0))
br label %58
48: ; preds = %44
%49 = call i32 @munmap(i8* %37, i64 1024)
%50 = icmp eq i32 %49, 0
br i1 %50, label %28, label %51
51: ; preds = %48
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %53)
br label %58
55: ; preds = %28
%56 = call i32 @close(i32 %14)
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %58
58: ; preds = %55, %51, %46, %40, %24, %16
%59 = phi i32 [ 2, %16 ], [ 2, %24 ], [ 1, %40 ], [ 1, %46 ], [ 2, %51 ], [ 0, %55 ]
ret i32 %59
}