#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_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
scalar_t__ munmap(void *, int);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ tst_exit();
int main(int argc, char *argv[]) {
  int fd;
  void *file;
  char *filename;
  if (argc != 2)
    printf("%s: filename\n", argv[1]);
  filename = argv[1];
  fd = open(filename, O_CREAT | O_RDWR, S_IRWXU);
  if (fd < 0) {
    perror("open");
    return (-1);
  }
  file = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (file == (void *)-1) {
    perror("mmap");
    return (-1);
  }
  close(fd);
  sleep(30);
  if (munmap(file, 1024) < 0) {
    perror("unmap");
    return (-1);
  }
  tst_exit();
}


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)
cmpl   $0x2,-0x8(%rbp)
je     11b4 <main+0x34>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe51(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    0x2df4(%rip),%rax
or     (%rax),%esi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1209 <main+0x89>
lea    0xe16(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <main+0x12f>
mov    0x2de0(%rip),%rax
mov    (%rax),%edx
mov    0x2d9f(%rip),%rax
or     (%rax),%edx
mov    0x2dbe(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x400,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    1266 <main+0xe6>
lea    0xdbe(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <main+0x12f>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    $0x1e,%edi
call   10a0 <sleep@plt>
mov    -0x20(%rbp),%rdi
mov    $0x400,%esi
call   1070 <munmap@plt>
cmp    $0x0,%rax
jge    12a8 <main+0x128>
lea    0xd81(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <main+0x12f>
mov    $0x0,%al
call   1060 <tst_exit@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
je     119c <main+0x1c>
mov    0x8(%rbx),%rsi
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
mov    0x8(%rbx),%rdi
mov    0x2e51(%rip),%rax
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
call   1080 <open@plt>
test   %eax,%eax
js     1232 <main+0xb2>
mov    %eax,%ebx
mov    0x2e23(%rip),%rax
mov    0x2de4(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e01(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x400,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     123b <main+0xbb>
mov    %rax,%r14
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0x1e,%edi
call   10a0 <sleep@plt>
mov    %r14,%rdi
mov    $0x400,%esi
call   1070 <munmap@plt>
test   %rax,%rax
js     1244 <main+0xc4>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <tst_exit@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdd5(%rip),%rdi
jmp    124b <main+0xcb>
lea    0xdd1(%rip),%rdi
jmp    124b <main+0xcb>
lea    0xdcd(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1228 <main+0xa8>

LLVM IR (-O0):
@.str = [14 x i8] c"%s: filename\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [6 x i8] c"unmap\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* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %11, %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %8
%20 = load i8*, i8** %8
%21 = load i32, i32* @O_CREAT
%22 = load i32, i32* @O_RDWR
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRWXU
%25 = call i32 @open(i8* %20, i32 %23, i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %16
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %52
30: ; preds = %16
%31 = load i32, i32* @PROT_READ
%32 = load i32, i32* @PROT_WRITE
%33 = or i32 %31, %32
%34 = load i32, i32* @MAP_SHARED
%35 = load i32, i32* %6
%36 = call i8* @mmap(i32* null, i32 1024, i32 %33, i32 %34, i32 %35, i32 0)
store i8* %36, i8** %7
%37 = load i8*, i8** %7
%38 = icmp eq i8* %37, inttoptr (i64 -1 to i8*)
br i1 %38, label %39, label %41
39: ; preds = %30
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %52
41: ; preds = %30
%42 = load i32, i32* %6
%43 = call i32 @close(i32 %42)
%44 = call i32 @sleep(i32 30)
%45 = load i8*, i8** %7
%46 = call i64 @munmap(i8* %45, i32 1024)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %52
50: ; preds = %41
%51 = call i32 (...) @tst_exit()
br label %52
52: ; preds = %50, %48, %39, %28
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@.str = [14 x i8] c"%s: filename\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [6 x i8] c"unmap\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %8
8: ; preds = %4, %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_RDWR
%13 = or i32 %12, %11
%14 = load i32, i32* @S_IRWXU
%15 = tail call i32 @open(i8* %10, i32 %13, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %8
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %37
19: ; preds = %8
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @PROT_WRITE
%22 = or i32 %21, %20
%23 = load i32, i32* @MAP_SHARED
%24 = tail call i8* @mmap(i32* null, i32 1024, i32 %22, i32 %23, i32 %15, i32 0)
%25 = icmp eq i8* %24, inttoptr (i64 -1 to i8*)
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %37
28: ; preds = %19
%29 = tail call i32 @close(i32 %15)
%30 = tail call i32 @sleep(i32 30)
%31 = tail call i64 @munmap(i8* %24, i32 1024)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %37
35: ; preds = %28
%36 = tail call i32 (...) @tst_exit()
br label %37
37: ; preds = %35, %33, %26, %17
%38 = phi i32 [ -1, %17 ], [ -1, %26 ], [ -1, %33 ], [ 0, %35 ]
ret i32 %38
}