#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 */
struct utsname {
  char *nodename;
};
typedef int /*<<< orphan*/ caddr_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int MAXPATHLEN;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ mmap(int /*<<< orphan*/, int, int /*<<< orphan*/,
                        int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
char *tempnam(char *, char *);
int /*<<< orphan*/ uname(struct utsname *);
int /*<<< orphan*/ unlink(char *);
int write(int, char *, int);
main() {
  int exit_code = 1;
  char buf[MAXPATHLEN];
  struct utsname unamedata;
  int fd;
  caddr_t addr;
  uname(&unamedata);
  sprintf(buf, "/tmp/.%s_mmaplocker", unamedata.nodename);
  fd = open(buf, O_RDONLY);
  if (fd) {
    if (sizeof(addr) == read(fd, (char *)&addr, sizeof(addr)))
      exit_code = 0;
    close(fd);
  }
  if (exit_code) {
    char *name = tempnam("/tmp", "mapper");
    if (name) {
      int unit = open(name, O_RDWR | O_CREAT, 0600);
      if (unit > 0) {
        addr = mmap(0, 2 * 1024 * 1024, PROT_WRITE, MAP_SHARED, unit, 0);
        if (addr != (caddr_t)-1) {
          addr = mmap(0, 160 * 1024 * 1024, PROT_WRITE, MAP_SHARED, unit, 0);
          if (addr != (caddr_t)-1) {
            addr = mmap(0, 160 * 1024 * 1024, PROT_WRITE, MAP_SHARED, unit, 0);
            if (addr != (caddr_t)-1) {
              fd = open(buf, O_RDWR | O_CREAT, 0666);
              if (fd) {
                if (sizeof(addr) == write(fd, (char *)&addr, sizeof(addr)))
                  exit_code = 0;
                close(fd);
              }
            }
          }
        }
        close(unit);
      }
      unlink(name);
    }
    free(name);
  }
  exit(exit_code);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1070 <uname@plt>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xdf6(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x40(%rbp),%rdi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10b0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1261 <main+0xb1>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1090 <read@plt>
movslq %eax,%rcx
mov    $0x4,%eax
cmp    %rcx,%rax
jne    1259 <main+0xa9>
movl   $0x0,-0x8(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
cmpl   $0x0,-0x8(%rbp)
je     13de <main+0x22e>
lea    0xda2(%rip),%rdi
lea    0xda0(%rip),%rsi
call   10a0 <tempnam@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     13d5 <main+0x225>
mov    -0x30(%rbp),%rdi
mov    0x2d40(%rip),%rax
mov    (%rax),%esi
mov    0x2d57(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
mov    $0x0,%al
call   10b0 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    13cc <main+0x21c>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edx
mov    0x2d1c(%rip),%rax
mov    (%rax),%ecx
mov    -0x34(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x200000,%esi
mov    %r9d,%edi
call   1060 <mmap@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
je     13c4 <main+0x214>
mov    0x2cc2(%rip),%rax
mov    (%rax),%edx
mov    0x2ce9(%rip),%rax
mov    (%rax),%ecx
mov    -0x34(%rbp),%r8d
xor    %r9d,%r9d
mov    $0xa000000,%esi
mov    %r9d,%edi
call   1060 <mmap@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
je     13bf <main+0x20f>
mov    0x2c8f(%rip),%rax
mov    (%rax),%edx
mov    0x2cb6(%rip),%rax
mov    (%rax),%ecx
mov    -0x34(%rbp),%r8d
xor    %r9d,%r9d
mov    $0xa000000,%esi
mov    %r9d,%edi
call   1060 <mmap@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
je     13ba <main+0x20a>
mov    -0x40(%rbp),%rdi
mov    0x2c78(%rip),%rax
mov    (%rax),%esi
mov    0x2c8f(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   10b0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     13b5 <main+0x205>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1050 <write@plt>
movslq %eax,%rcx
mov    $0x4,%eax
cmp    %rcx,%rax
jne    13ad <main+0x1fd>
movl   $0x0,-0x8(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
jmp    13ba <main+0x20a>
jmp    13bf <main+0x20f>
jmp    13c4 <main+0x214>
mov    -0x34(%rbp),%edi
call   1080 <close@plt>
mov    -0x30(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    -0x38(%rbp),%rdi
call   1070 <uname@plt>
mov    -0x38(%rbp),%rdx
lea    0xe11(%rip),%rsi
mov    %r13,%rdi
call   10c0 <sprintf@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
mov    %r13,%rdi
xor    %eax,%eax
call   10b0 <open@plt>
test   %eax,%eax
je     1237 <main+0x87>
mov    %eax,%ebx
lea    -0x2c(%rbp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1090 <read@plt>
mov    %eax,%r14d
mov    %ebx,%edi
call   1080 <close@plt>
cmp    $0x4,%r14d
je     1368 <main+0x1b8>
lea    0xdd6(%rip),%rdi
lea    0xdd4(%rip),%rsi
call   10a0 <tempnam@plt>
mov    %rax,%r14
mov    $0x1,%r15d
test   %rax,%rax
je     1360 <main+0x1b0>
mov    0x2d75(%rip),%rax
mov    0x2d8e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x180,%edx
xor    %eax,%eax
call   10b0 <open@plt>
mov    $0x1,%r15d
test   %eax,%eax
jle    1358 <main+0x1a8>
mov    %eax,%r12d
mov    0x2d23(%rip),%rax
mov    (%rax),%edx
mov    0x2d4a(%rip),%rbx
mov    (%rbx),%ecx
xor    %edi,%edi
mov    $0x200000,%esi
mov    %r12d,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    $0x1,%r15d
cmp    $0xffffffff,%eax
je     1350 <main+0x1a0>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edx
mov    (%rbx),%ecx
xor    %edi,%edi
mov    $0xa000000,%esi
mov    %r12d,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
cmp    $0xffffffff,%eax
je     1350 <main+0x1a0>
mov    0x2cce(%rip),%rax
mov    (%rax),%edx
mov    (%rbx),%ecx
xor    %edi,%edi
mov    $0xa000000,%esi
mov    %r12d,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
je     1350 <main+0x1a0>
mov    0x2ce9(%rip),%rax
mov    (%rax),%esi
mov    0x2cc0(%rip),%rax
or     (%rax),%esi
mov    %r13,%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   10b0 <open@plt>
test   %eax,%eax
je     1350 <main+0x1a0>
mov    %eax,%ebx
lea    -0x2c(%rbp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1050 <write@plt>
xor    %r15d,%r15d
cmp    $0x4,%eax
setne  %r15b
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r12d,%edi
call   1080 <close@plt>
mov    %r14,%rdi
call   1040 <unlink@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15d,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.utsname = type { i8* }
@MAXPATHLEN = global i32 0
@.str = [20 x i8] c"/tmp/.%s_mmaplocker\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [7 x i8] c"mapper\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca %struct.utsname
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %2
%10 = load i32, i32* @MAXPATHLEN
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %3
%13 = alloca i8, i64 %11
store i64 %11, i64* %4
%14 = call i32 @uname(%struct.utsname* %5)
%15 = getelementptr inbounds %struct.utsname, %struct.utsname* %5, i32 0, i32 0
%16 = load i8*, i8** %15
%17 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @O_RDONLY
%19 = call i32 (i8*, i32, ...) @open(i8* %13, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %32
22: ; preds = %0
%23 = load i32, i32* %6
%24 = bitcast i32* %7 to i8*
%25 = call i32 @read(i32 %23, i8* %24, i32 4)
%26 = sext i32 %25 to i64
%27 = icmp eq i64 4, %26
br i1 %27, label %28, label %29
28: ; preds = %22
store i32 0, i32* %2
br label %29
29: ; preds = %28, %22
%30 = load i32, i32* %6
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %29, %0
%33 = load i32, i32* %2
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %97
35: ; preds = %32
%36 = call i8* @tempnam(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %94
39: ; preds = %35
%40 = load i8*, i8** %8
%41 = load i32, i32* @O_RDWR
%42 = load i32, i32* @O_CREAT
%43 = or i32 %41, %42
%44 = call i32 (i8*, i32, ...) @open(i8* %40, i32 %43, i32 384)
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %91
47: ; preds = %39
%48 = load i32, i32* @PROT_WRITE
%49 = load i32, i32* @MAP_SHARED
%50 = load i32, i32* %9
%51 = call i32 @mmap(i32 0, i32 2097152, i32 %48, i32 %49, i32 %50, i32 0)
store i32 %51, i32* %7
%52 = load i32, i32* %7
%53 = icmp ne i32 %52, -1
br i1 %53, label %54, label %88
54: ; preds = %47
%55 = load i32, i32* @PROT_WRITE
%56 = load i32, i32* @MAP_SHARED
%57 = load i32, i32* %9
%58 = call i32 @mmap(i32 0, i32 167772160, i32 %55, i32 %56, i32 %57, i32 0)
store i32 %58, i32* %7
%59 = load i32, i32* %7
%60 = icmp ne i32 %59, -1
br i1 %60, label %61, label %87
61: ; preds = %54
%62 = load i32, i32* @PROT_WRITE
%63 = load i32, i32* @MAP_SHARED
%64 = load i32, i32* %9
%65 = call i32 @mmap(i32 0, i32 167772160, i32 %62, i32 %63, i32 %64, i32 0)
store i32 %65, i32* %7
%66 = load i32, i32* %7
%67 = icmp ne i32 %66, -1
br i1 %67, label %68, label %86
68: ; preds = %61
%69 = load i32, i32* @O_RDWR
%70 = load i32, i32* @O_CREAT
%71 = or i32 %69, %70
%72 = call i32 (i8*, i32, ...) @open(i8* %13, i32 %71, i32 438)
store i32 %72, i32* %6
%73 = load i32, i32* %6
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %85
75: ; preds = %68
%76 = load i32, i32* %6
%77 = bitcast i32* %7 to i8*
%78 = call i32 @write(i32 %76, i8* %77, i32 4)
%79 = sext i32 %78 to i64
%80 = icmp eq i64 4, %79
br i1 %80, label %81, label %82
81: ; preds = %75
store i32 0, i32* %2
br label %82
82: ; preds = %81, %75
%83 = load i32, i32* %6
%84 = call i32 @close(i32 %83)
br label %85
85: ; preds = %82, %68
br label %86
86: ; preds = %85, %61
br label %87
87: ; preds = %86, %54
br label %88
88: ; preds = %87, %47
%89 = load i32, i32* %9
%90 = call i32 @close(i32 %89)
br label %91
91: ; preds = %88, %39
%92 = load i8*, i8** %8
%93 = call i32 @unlink(i8* %92)
br label %94
94: ; preds = %91, %35
%95 = load i8*, i8** %8
%96 = call i32 @free(i8* %95)
br label %97
97: ; preds = %94, %32
%98 = load i32, i32* %2
%99 = call i32 @exit(i32 %98)
unreachable
}

LLVM IR (-O2):
%struct.utsname = type { i8* }
@MAXPATHLEN = global i32 0
@.str = [20 x i8] c"/tmp/.%s_mmaplocker\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [7 x i8] c"mapper\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i32 @main() {
%1 = alloca %struct.utsname
%2 = alloca i32
%3 = load i32, i32* @MAXPATHLEN
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = bitcast %struct.utsname* %1 to i8*
%7 = bitcast i32* %2 to i8*
%8 = call i32 @uname(%struct.utsname* %1)
%9 = getelementptr inbounds %struct.utsname, %struct.utsname* %1, i64 0, i32 0
%10 = load i8*, i8** %9
%11 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @O_RDONLY
%13 = call i32 (i8*, i32, ...) @open(i8* %5, i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %0
%16 = call i32 @read(i32 %13, i8* %7, i32 4)
%17 = icmp eq i32 %16, 4
%18 = tail call i32 @close(i32 %13)
br i1 %17, label %63, label %19
19: ; preds = %15, %0
%20 = tail call i8* @tempnam(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i8* %20, null
br i1 %21, label %60, label %22
22: ; preds = %19
%23 = load i32, i32* @O_RDWR
%24 = load i32, i32* @O_CREAT
%25 = or i32 %24, %23
%26 = tail call i32 (i8*, i32, ...) @open(i8* %20, i32 %25, i32 384)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %57
28: ; preds = %22
%29 = load i32, i32* @PROT_WRITE
%30 = load i32, i32* @MAP_SHARED
%31 = tail call i32 @mmap(i32 0, i32 2097152, i32 %29, i32 %30, i32 %26, i32 0)
%32 = icmp eq i32 %31, -1
br i1 %32, label %54, label %33
33: ; preds = %28
%34 = load i32, i32* @PROT_WRITE
%35 = load i32, i32* @MAP_SHARED
%36 = tail call i32 @mmap(i32 0, i32 167772160, i32 %34, i32 %35, i32 %26, i32 0)
%37 = icmp eq i32 %36, -1
br i1 %37, label %54, label %38
38: ; preds = %33
%39 = load i32, i32* @PROT_WRITE
%40 = load i32, i32* @MAP_SHARED
%41 = tail call i32 @mmap(i32 0, i32 167772160, i32 %39, i32 %40, i32 %26, i32 0)
store i32 %41, i32* %2
%42 = icmp eq i32 %41, -1
br i1 %42, label %54, label %43
43: ; preds = %38
%44 = load i32, i32* @O_RDWR
%45 = load i32, i32* @O_CREAT
%46 = or i32 %45, %44
%47 = call i32 (i8*, i32, ...) @open(i8* %5, i32 %46, i32 438)
%48 = icmp eq i32 %47, 0
br i1 %48, label %54, label %49
49: ; preds = %43
%50 = call i32 @write(i32 %47, i8* %7, i32 4)
%51 = icmp ne i32 %50, 4
%52 = zext i1 %51 to i32
%53 = tail call i32 @close(i32 %47)
br label %54
54: ; preds = %33, %43, %49, %38, %28
%55 = phi i32 [ %52, %49 ], [ 1, %43 ], [ 1, %38 ], [ 1, %33 ], [ 1, %28 ]
%56 = tail call i32 @close(i32 %26)
br label %57
57: ; preds = %54, %22
%58 = phi i32 [ %55, %54 ], [ 1, %22 ]
%59 = tail call i32 @unlink(i8* %20)
br label %60
60: ; preds = %57, %19
%61 = phi i32 [ %58, %57 ], [ 1, %19 ]
%62 = tail call i32 @free(i8* %20)
br label %63
63: ; preds = %15, %60
%64 = phi i32 [ %61, %60 ], [ 0, %15 ]
%65 = tail call i32 @exit(i32 %64)
unreachable
}