#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 */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
scalar_t__ memcmp(char *, char *, int);
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  void *filememory_1;
  void *filememory_2;
  fd = open("/tmp/zz_temp_mmap_test", O_RDWR | O_CREAT);
  if (fd < 0) {
    fprintf(stderr, "Couldn't open %s for writing\n", "/tmp/zz_temp_mmap_test");
    return (1);
  }
  unlink("/tmp/zz_temp_mmap_test");
  write(fd, "This is a test string", sizeof("This is a test string"));
  lseek(fd, 64 * 1024, SEEK_SET);
  write(fd, "fnord", 5);
  filememory_1 = mmap(NULL, 0x0100, PROT_READ, MAP_PRIVATE, fd, 0);
  if (filememory_1 == (void *)-1) {
    perror("mmap returned error");
    return (1);
  }
  filememory_2 = mmap(NULL, 0x0100, PROT_READ, MAP_PRIVATE, fd + 10, 0);
  if ((filememory_2 != (void *)-1) || (errno != 9)) {
    fprintf(stderr, "mmap allowed a bogus file descriptor...\n");
    return (1);
  }
  if ((*(unsigned char *)filememory_1) != "This is a test string"[0]) {
    fprintf(stderr, "mmap doesn't give expected data...\n");
    return (1);
  }
  {
    char *c = mmap(NULL, 5, PROT_READ, MAP_PRIVATE, fd, 64 * 1024);
    if (c == MAP_FAILED) {
      perror("mmap failed");
      return 1;
    }
    if (memcmp(c, "fnord", 5)) {
      fprintf(stderr, "page offset didn't work");
      return 1;
    }
  }
  close(fd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2e42(%rip),%rax
or     (%rax),%esi
lea    0xe41(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fb <main+0x6b>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
lea    0xe18(%rip),%rdx
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13f0 <main+0x260>
lea    0xdfe(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x14(%rbp),%edi
lea    0xe24(%rip),%rsi
mov    $0x16,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
mov    $0x10000,%esi
call   1060 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    0xe10(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
mov    0x2d5b(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x100,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    1299 <main+0x109>
lea    0xdc9(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13f0 <main+0x260>
mov    0x2d50(%rip),%rax
mov    (%rax),%edx
mov    0x2d07(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
add    $0xa,%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x100,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x28(%rbp)
jne    12e9 <main+0x159>
mov    0x2ce0(%rip),%rax
cmpl   $0x9,(%rax)
je     130c <main+0x17c>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
lea    0xd6c(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13f0 <main+0x260>
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
movsbl 0xd1b(%rip),%ecx
cmp    %ecx,%eax
je     1345 <main+0x1b5>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
lea    0xd5c(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13f0 <main+0x260>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edx
mov    0x2c5b(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x5,%esi
mov    $0x10000,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2c2a(%rip),%rcx
cmp    (%rcx),%rax
jne    139f <main+0x20f>
lea    0xd24(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13f0 <main+0x260>
mov    -0x30(%rbp),%rdi
lea    0xca1(%rip),%rsi
mov    $0x5,%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
je     13e1 <main+0x251>
mov    0x2c03(%rip),%rax
mov    (%rax),%edi
lea    0xcf0(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13f0 <main+0x260>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e39(%rip),%rax
mov    0x2e52(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe4f(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     12d3 <main+0x143>
mov    %eax,%ebx
lea    0xe39(%rip),%rdi
call   1030 <unlink@plt>
lea    0xe62(%rip),%rsi
mov    %ebx,%edi
mov    $0x16,%edx
call   1040 <write@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x10000,%esi
call   1060 <lseek@plt>
lea    0xe50(%rip),%rsi
mov    %ebx,%edi
mov    $0x5,%edx
call   1040 <write@plt>
mov    0x2de2(%rip),%r15
mov    (%r15),%edx
mov    0x2d98(%rip),%r12
mov    (%r12),%ecx
xor    %edi,%edi
mov    $0x100,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     1338 <main+0x1a8>
mov    %rax,%r14
mov    (%r15),%edx
mov    (%r12),%ecx
mov    %ebx,%r8d
add    $0xa,%r8d
xor    %edi,%edi
mov    $0x100,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
jne    12f3 <main+0x163>
mov    0x2d57(%rip),%rax
cmpl   $0x9,(%rax)
jne    12f3 <main+0x163>
cmpb   $0x54,(%r14)
jne    1305 <main+0x175>
mov    (%r15),%edx
mov    (%r12),%ecx
xor    %edi,%edi
mov    $0x5,%esi
mov    %ebx,%r8d
mov    $0x10000,%r9d
call   1050 <mmap@plt>
mov    0x2d09(%rip),%rcx
cmp    (%rcx),%rax
je     1341 <main+0x1b1>
lea    0xd9c(%rip),%rsi
mov    %rax,%rdi
mov    $0x5,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
je     132d <main+0x19d>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
jmp    1315 <main+0x185>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
lea    0xd34(%rip),%rsi
lea    0xd16(%rip),%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    131c <main+0x18c>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
lea    0xd62(%rip),%rsi
jmp    1315 <main+0x185>
mov    0x2cbc(%rip),%rax
mov    (%rax),%edi
lea    0xd79(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
jmp    1321 <main+0x191>
lea    0xd12(%rip),%rdi
jmp    1348 <main+0x1b8>
lea    0xd6a(%rip),%rdi
call   10b0 <perror@plt>
jmp    131c <main+0x18c>

LLVM IR (-O0):
@.str = [23 x i8] c"/tmp/zz_temp_mmap_test\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str.1 = [30 x i8] c"Couldn't open %s for writing\0A\00"
@.str.2 = [22 x i8] c"This is a test string\00"
@SEEK_SET = global i32 0
@.str.3 = [6 x i8] c"fnord\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.4 = [20 x i8] c"mmap returned error\00"
@errno = global i32 0
@.str.5 = [41 x i8] c"mmap allowed a bogus file descriptor...\0A\00"
@.str.6 = [36 x i8] c"mmap doesn't give expected data...\0A\00"
@MAP_FAILED = global i8* null
@.str.7 = [12 x i8] c"mmap failed\00"
@.str.8 = [24 x i8] c"page offset didn't work\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %80
19: ; preds = %2
%20 = call i32 @unlink(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* %6
%22 = call i32 @write(i32 %21, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 22)
%23 = load i32, i32* %6
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @lseek(i32 %23, i32 65536, i32 %24)
%26 = load i32, i32* %6
%27 = call i32 @write(i32 %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%28 = load i32, i32* @PROT_READ
%29 = load i32, i32* @MAP_PRIVATE
%30 = load i32, i32* %6
%31 = call i8* @mmap(i32* null, i32 256, i32 %28, i32 %29, i32 %30, i32 0)
store i8* %31, i8** %7
%32 = load i8*, i8** %7
%33 = icmp eq i8* %32, inttoptr (i64 -1 to i8*)
br i1 %33, label %34, label %36
34: ; preds = %19
%35 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %80
36: ; preds = %19
%37 = load i32, i32* @PROT_READ
%38 = load i32, i32* @MAP_PRIVATE
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 10
%41 = call i8* @mmap(i32* null, i32 256, i32 %37, i32 %38, i32 %40, i32 0)
store i8* %41, i8** %8
%42 = load i8*, i8** %8
%43 = icmp ne i8* %42, inttoptr (i64 -1 to i8*)
br i1 %43, label %47, label %44
44: ; preds = %36
%45 = load i32, i32* @errno
%46 = icmp ne i32 %45, 9
br i1 %46, label %47, label %50
47: ; preds = %44, %36
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %80
50: ; preds = %44
%51 = load i8*, i8** %7
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = load i8, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0)
%55 = sext i8 %54 to i32
%56 = icmp ne i32 %53, %55
br i1 %56, label %57, label %60
57: ; preds = %50
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %80
60: ; preds = %50
%61 = load i32, i32* @PROT_READ
%62 = load i32, i32* @MAP_PRIVATE
%63 = load i32, i32* %6
%64 = call i8* @mmap(i32* null, i32 5, i32 %61, i32 %62, i32 %63, i32 65536)
store i8* %64, i8** %9
%65 = load i8*, i8** %9
%66 = load i8*, i8** @MAP_FAILED
%67 = icmp eq i8* %65, %66
br i1 %67, label %68, label %70
68: ; preds = %60
%69 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %80
70: ; preds = %60
%71 = load i8*, i8** %9
%72 = call i64 @memcmp(i8* %71, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %70
%75 = load i32, i32* @stderr
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %3
br label %80
77: ; preds = %70
%78 = load i32, i32* %6
%79 = call i32 @close(i32 %78)
store i32 0, i32* %3
br label %80
80: ; preds = %77, %74, %68, %57, %47, %34, %16
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
@.str = [23 x i8] c"/tmp/zz_temp_mmap_test\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str.1 = [30 x i8] c"Couldn't open %s for writing\0A\00"
@.str.2 = [22 x i8] c"This is a test string\00"
@SEEK_SET = global i32 0
@.str.3 = [6 x i8] c"fnord\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.4 = [20 x i8] c"mmap returned error\00"
@errno = global i32 0
@.str.5 = [41 x i8] c"mmap allowed a bogus file descriptor...\0A\00"
@.str.6 = [36 x i8] c"mmap doesn't give expected data...\0A\00"
@MAP_FAILED = global i8* null
@.str.7 = [12 x i8] c"mmap failed\00"
@.str.8 = [24 x i8] c"page offset didn't work\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %57
11: ; preds = %2
%12 = tail call i32 @unlink(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 22)
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @lseek(i32 %6, i32 65536, i32 %14)
%16 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @MAP_PRIVATE
%19 = tail call i8* @mmap(i32* null, i32 256, i32 %17, i32 %18, i32 %6, i32 0)
%20 = icmp eq i8* %19, inttoptr (i64 -1 to i8*)
br i1 %20, label %21, label %23
21: ; preds = %11
%22 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %57
23: ; preds = %11
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @MAP_PRIVATE
%26 = add nuw nsw i32 %6, 10
%27 = tail call i8* @mmap(i32* null, i32 256, i32 %24, i32 %25, i32 %26, i32 0)
%28 = icmp ne i8* %27, inttoptr (i64 -1 to i8*)
%29 = load i32, i32* @errno
%30 = icmp ne i32 %29, 9
%31 = select i1 %28, i1 true, i1 %30
br i1 %31, label %32, label %35
32: ; preds = %23
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
br label %57
35: ; preds = %23
%36 = load i8, i8* %19
%37 = icmp eq i8 %36, 84
br i1 %37, label %41, label %38
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
br label %57
41: ; preds = %35
%42 = load i32, i32* @PROT_READ
%43 = load i32, i32* @MAP_PRIVATE
%44 = tail call i8* @mmap(i32* null, i32 5, i32 %42, i32 %43, i32 %6, i32 65536)
%45 = load i8*, i8** @MAP_FAILED
%46 = icmp eq i8* %44, %45
br i1 %46, label %47, label %49
47: ; preds = %41
%48 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
br label %57
49: ; preds = %41
%50 = tail call i64 @memcmp(i8* %44, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%51 = icmp eq i64 %50, 0
br i1 %51, label %55, label %52
52: ; preds = %49
%53 = load i32, i32* @stderr
%54 = tail call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
br label %57
55: ; preds = %49
%56 = tail call i32 @close(i32 %6)
br label %57
57: ; preds = %47, %52, %55, %38, %32, %21, %8
%58 = phi i32 [ 1, %8 ], [ 1, %21 ], [ 1, %32 ], [ 1, %38 ], [ 0, %55 ], [ 1, %52 ], [ 1, %47 ]
ret i32 %58
}