#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 FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *const);
int /*<<< orphan*/ lock_file(int);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char const *const, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int open_lock_and_map(const char *const process_name,
                      const char *const filename) {
  int fd;
  int flags;
  fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  if (fd < 0) {
    perror("open");
    goto err1;
  }
  flags = fcntl(fd, F_GETFD);
  assert(flags >= 0);
  if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
    assert(0);
  fprintf(stderr, "%s: about to lock file for writing.\n", process_name);
  if (!lock_file(fd)) {
    perror("fcntl");
    goto err2;
  }
  fprintf(stderr, "%s: file locking attempt succeeded.\n", process_name);
  if (mmap(NULL, 1, PROT_WRITE, MAP_SHARED, fd, 0) == 0) {
    perror("mmap");
    goto err2;
  }
  goto out;
err2:
  close(fd);
err1:
out:
  return fd;
}


x86 assembly (clang, -O0):
<open_lock_and_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    0x2e54(%rip),%rax
or     (%rax),%esi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
mov    0x2e1a(%rip),%rax
or     (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11db <open_lock_and_map+0x5b>
lea    0xe2f(%rip),%rdi
call   10a0 <perror@plt>
jmp    12d4 <open_lock_and_map+0x154>
mov    -0x14(%rbp),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x14(%rbp),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    0x2d98(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    1231 <open_lock_and_map+0xb1>
xor    %edi,%edi
call   1070 <assert@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdc0(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1050 <lock_file@plt>
cmp    $0x0,%eax
jne    126c <open_lock_and_map+0xec>
lea    0xdc8(%rip),%rdi
call   10a0 <perror@plt>
jmp    12cc <open_lock_and_map+0x14c>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdb0(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%edx
mov    0x2d53(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    $0x0,%rax
jne    12c7 <open_lock_and_map+0x147>
lea    0xd98(%rip),%rdi
call   10a0 <perror@plt>
jmp    12cc <open_lock_and_map+0x14c>
jmp    12d9 <open_lock_and_map+0x159>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
jmp    12d9 <open_lock_and_map+0x159>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_lock_and_map>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rax
mov    %rdi,%r14
mov    0x2e47(%rip),%rcx
mov    0x2e60(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2df5(%rip),%rcx
mov    0x2e26(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %rax,%rdi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     126e <open_lock_and_map+0xee>
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ebp
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1070 <assert@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
mov    0x2dc0(%rip),%rax
or     (%rax),%ebp
mov    %ebx,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
jns    1208 <open_lock_and_map+0x88>
xor    %edi,%edi
call   1070 <assert@plt>
mov    0x2db1(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xdec(%rip),%rsi
mov    %r14,%rdx
call   1080 <fprintf@plt>
mov    %ebx,%edi
call   1050 <lock_file@plt>
test   %eax,%eax
je     127c <open_lock_and_map+0xfc>
mov    0x0(%rbp),%edi
lea    0xdfa(%rip),%rsi
mov    %r14,%rdx
call   1080 <fprintf@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
mov    0x2d9a(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x1,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
test   %rax,%rax
je     1285 <open_lock_and_map+0x105>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd8b(%rip),%rdi
call   10a0 <perror@plt>
jmp    1267 <open_lock_and_map+0xe7>
lea    0xda7(%rip),%rdi
jmp    128c <open_lock_and_map+0x10c>
lea    0xdc9(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1267 <open_lock_and_map+0xe7>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [5 x i8] c"open\00"
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@stderr = global i32 0
@.str.1 = [37 x i8] c"%s: about to lock file for writing.\0A\00"
@.str.2 = [6 x i8] c"fcntl\00"
@.str.3 = [37 x i8] c"%s: file locking attempt succeeded.\0A\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.4 = [5 x i8] c"mmap\00"
define i32 @open_lock_and_map(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %7, i32 %10, i32 %13)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %60
19: ; preds = %2
%20 = load i32, i32* %5
%21 = load i32, i32* @F_GETFD
%22 = call i32 (i32, i32, ...) @fcntl(i32 %20, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp sge i32 %23, 0
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = load i32, i32* %5
%28 = load i32, i32* @F_SETFD
%29 = load i32, i32* %6
%30 = load i32, i32* @FD_CLOEXEC
%31 = or i32 %29, %30
%32 = call i32 (i32, i32, ...) @fcntl(i32 %27, i32 %28, i32 %31)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %19
%35 = call i32 @assert(i32 0)
br label %36
36: ; preds = %34, %19
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %3
%39 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %38)
%40 = load i32, i32* %5
%41 = call i32 @lock_file(i32 %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %45, label %43
43: ; preds = %36
%44 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %57
45: ; preds = %36
%46 = load i32, i32* @stderr
%47 = load i8*, i8** %3
%48 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %47)
%49 = load i32, i32* @PROT_WRITE
%50 = load i32, i32* @MAP_SHARED
%51 = load i32, i32* %5
%52 = call i64 @mmap(i32* null, i32 1, i32 %49, i32 %50, i32 %51, i32 0)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %45
%55 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %57
56: ; preds = %45
br label %61
57: ; preds = %54, %43
%58 = load i32, i32* %5
%59 = call i32 @close(i32 %58)
br label %60
60: ; preds = %57, %17
br label %61
61: ; preds = %60, %56
%62 = load i32, i32* %5
ret i32 %62
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [5 x i8] c"open\00"
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@stderr = global i32 0
@.str.1 = [37 x i8] c"%s: about to lock file for writing.\0A\00"
@.str.2 = [6 x i8] c"fcntl\00"
@.str.3 = [37 x i8] c"%s: file locking attempt succeeded.\0A\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.4 = [5 x i8] c"mmap\00"
define i32 @open_lock_and_map(i8* %0, i8* nocapture %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* %1, i32 %5, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %42
13: ; preds = %2
%14 = load i32, i32* @F_GETFD
%15 = tail call i32 (i32, i32, ...) @fcntl(i32 %9, i32 %14)
%16 = xor i32 %15, -1
%17 = lshr i32 %16, 31
%18 = tail call i32 @assert(i32 %17)
%19 = load i32, i32* @F_SETFD
%20 = load i32, i32* @FD_CLOEXEC
%21 = or i32 %20, %15
%22 = tail call i32 (i32, i32, ...) @fcntl(i32 %9, i32 %19, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %13
%25 = tail call i32 @assert(i32 0)
br label %26
26: ; preds = %24, %13
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%29 = tail call i32 @lock_file(i32 %9)
%30 = icmp eq i32 %29, 0
br i1 %30, label %38, label %31
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = tail call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%34 = load i32, i32* @PROT_WRITE
%35 = load i32, i32* @MAP_SHARED
%36 = tail call i64 @mmap(i32* null, i32 1, i32 %34, i32 %35, i32 %9, i32 0)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %31, %26
%39 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), %26 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), %31 ]
%40 = tail call i32 @perror(i8* %39)
%41 = tail call i32 @close(i32 %9)
br label %42
42: ; preds = %11, %38, %31
ret i32 %9
}