#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 */
int /*<<< orphan*/ MAP_PRIVATE;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
scalar_t__ 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);
  }
  write(fd, "This is a test string", sizeof("This is a test string"));
  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);
  }
  close(fd);
  if ((*(unsigned char *)filememory_1) != "This is a test string"[0]) {
    fprintf(stderr, "mmap doesn't give expected data...\n");
    return (1);
  }
  if (unlink("/tmp/zz_temp_mmap_test") != 0) {
    fprintf(stderr, "Unexpected problem deleting the tempfile... ?\n");
    return (1);
  }
  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    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    0x2e62(%rip),%rax
or     (%rax),%esi
lea    0xe61(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11db <main+0x6b>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
lea    0xe38(%rip),%rdx
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <main+0x1c7>
mov    -0x14(%rbp),%edi
lea    0xe50(%rip),%rsi
mov    $0x16,%edx
call   1040 <write@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
mov    0x2db9(%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    1243 <main+0xd3>
lea    0xe19(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <main+0x1c7>
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
mov    0x2d65(%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    1293 <main+0x123>
mov    0x2d36(%rip),%rax
cmpl   $0x9,(%rax)
je     12b6 <main+0x146>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <main+0x1c7>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
movsbl 0xd69(%rip),%ecx
cmp    %ecx,%eax
je     12f7 <main+0x187>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <main+0x1c7>
lea    0xd02(%rip),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
je     1330 <main+0x1c0>
mov    0x2cb4(%rip),%rax
mov    (%rax),%edi
lea    0xd8f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <main+0x1c7>
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    0x2e59(%rip),%rax
mov    0x2e72(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe6f(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1246 <main+0xd6>
mov    %eax,%ebx
lea    0xe8e(%rip),%rsi
mov    %eax,%edi
mov    $0x16,%edx
call   1040 <write@plt>
mov    0x2e36(%rip),%r15
mov    (%r15),%edx
mov    0x2df4(%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     12a4 <main+0x134>
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    1266 <main+0xf6>
mov    0x2daf(%rip),%rax
cmpl   $0x9,(%rax)
jne    1266 <main+0xf6>
mov    %ebx,%edi
call   1060 <close@plt>
cmpb   $0x54,(%r14)
jne    1278 <main+0x108>
lea    0xdd6(%rip),%rdi
call   1030 <unlink@plt>
test   %rax,%rax
je     12a0 <main+0x130>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
jmp    1288 <main+0x118>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%rip),%rsi
lea    0xda3(%rip),%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    128f <main+0x11f>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
jmp    1288 <main+0x118>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %eax,%eax
jmp    1294 <main+0x124>
lea    0xda0(%rip),%rdi
call   1090 <perror@plt>
jmp    128f <main+0x11f>

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"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.3 = [20 x i8] c"mmap returned error\00"
@errno = global i32 0
@.str.4 = [41 x i8] c"mmap allowed a bogus file descriptor...\0A\00"
@.str.5 = [36 x i8] c"mmap doesn't give expected data...\0A\00"
@.str.6 = [47 x i8] c"Unexpected problem deleting the tempfile... ?\0A\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*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, 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 %62
18: ; preds = %2
%19 = load i32, i32* %6
%20 = call i32 @write(i32 %19, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 22)
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @MAP_PRIVATE
%23 = load i32, i32* %6
%24 = call i8* @mmap(i32* null, i32 256, i32 %21, i32 %22, i32 %23, i32 0)
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = icmp eq i8* %25, inttoptr (i64 -1 to i8*)
br i1 %26, label %27, label %29
27: ; preds = %18
%28 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %62
29: ; preds = %18
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @MAP_PRIVATE
%32 = load i32, i32* %6
%33 = add nsw i32 %32, 10
%34 = call i8* @mmap(i32* null, i32 256, i32 %30, i32 %31, i32 %33, i32 0)
store i8* %34, i8** %8
%35 = load i8*, i8** %8
%36 = icmp ne i8* %35, inttoptr (i64 -1 to i8*)
br i1 %36, label %40, label %37
37: ; preds = %29
%38 = load i32, i32* @errno
%39 = icmp ne i32 %38, 9
br i1 %39, label %40, label %43
40: ; preds = %37, %29
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %62
43: ; preds = %37
%44 = load i32, i32* %6
%45 = call i32 @close(i32 %44)
%46 = load i8*, i8** %7
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = load i8, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0)
%50 = sext i8 %49 to i32
%51 = icmp ne i32 %48, %50
br i1 %51, label %52, label %55
52: ; preds = %43
%53 = load i32, i32* @stderr
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %62
55: ; preds = %43
%56 = call i64 @unlink(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = load i32, i32* @stderr
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %62
61: ; preds = %55
store i32 0, i32* %3
br label %62
62: ; preds = %61, %58, %52, %40, %27, %15
%63 = load i32, i32* %3
ret i32 %63
}

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"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.3 = [20 x i8] c"mmap returned error\00"
@errno = global i32 0
@.str.4 = [41 x i8] c"mmap allowed a bogus file descriptor...\0A\00"
@.str.5 = [36 x i8] c"mmap doesn't give expected data...\0A\00"
@.str.6 = [47 x i8] c"Unexpected problem deleting the tempfile... ?\0A\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 %44
11: ; preds = %2
%12 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 22)
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @MAP_PRIVATE
%15 = tail call i8* @mmap(i32* null, i32 256, i32 %13, i32 %14, i32 %6, i32 0)
%16 = icmp eq i8* %15, inttoptr (i64 -1 to i8*)
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %44
19: ; preds = %11
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @MAP_PRIVATE
%22 = add nuw nsw i32 %6, 10
%23 = tail call i8* @mmap(i32* null, i32 256, i32 %20, i32 %21, i32 %22, i32 0)
%24 = icmp ne i8* %23, inttoptr (i64 -1 to i8*)
%25 = load i32, i32* @errno
%26 = icmp ne i32 %25, 9
%27 = select i1 %24, i1 true, i1 %26
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = load i32, i32* @stderr
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %44
31: ; preds = %19
%32 = tail call i32 @close(i32 %6)
%33 = load i8, i8* %15
%34 = icmp eq i8 %33, 84
br i1 %34, label %38, label %35
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = tail call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
br label %44
38: ; preds = %31
%39 = tail call i64 @unlink(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %38
%42 = load i32, i32* @stderr
%43 = tail call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.6, i64 0, i64 0))
br label %44
44: ; preds = %38, %41, %35, %28, %17, %8
%45 = phi i32 [ 1, %8 ], [ 1, %17 ], [ 1, %28 ], [ 1, %35 ], [ 1, %41 ], [ 0, %38 ]
ret i32 %45
}