#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 stat {
  int st_mode;
};
struct flock {
  scalar_t__ l_len;
  scalar_t__ l_start;
  void *l_whence;
  int /*<<< orphan*/ l_type;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ readbuff;
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ file_contents;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ NOTIFY(scalar_t__);
int /*<<< orphan*/ NOTIFY_INIT();
int O_CREAT;
struct flock *O_NONBLOCK;
int O_RDWR;
int O_TRUNC;
void *SEEK_SET;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_ISGID;
int S_IWUSR;
int S_IXGRP;
int /*<<< orphan*/ WAIT_NOTIFY();
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fchmod(int, int);
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fstat(int, struct stat *);
scalar_t__ lseek(int, int /*<<< orphan*/, void *);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
size_t read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
scalar_t__ write(int, char const *, int);
int main(int argc, char *argv[]) {
  static const char file_contents[] = "abcdefg";
  static char readbuff[sizeof(file_contents)];
  if (argc < 2) {
    fprintf(stderr, "Usage: %s filename\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  int fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC,
                (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
  if (fd < 0) {
    fprintf(stderr, "Couldn't open %s: %s\n", argv[1], strerror(errno));
    exit(EXIT_FAILURE);
  }
  struct stat statbuf;
  if (fstat(fd, &statbuf) < 0) {
    fprintf(stderr, "stat(2) failed for file %s: %s\n", argv[1],
            strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (fchmod(fd, (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 0) {
    perror("Couldn't enable mandatory locking");
    exit(EXIT_FAILURE);
  }
  if (write(fd, file_contents, sizeof(file_contents) - 1) < 0) {
    perror("write(2) error");
    exit(EXIT_FAILURE);
  }
  NOTIFY_INIT();
  pid_t f;
  if ((f = fork()) < 0) {
    perror("fork(2) failed");
    exit(EXIT_FAILURE);
  }
  if (f == 0) {
    WAIT_NOTIFY();
    if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
      perror("fcntl(2) failed");
      exit(EXIT_FAILURE);
    }
    struct flock lock;
    lock.l_type = F_RDLCK;
    lock.l_whence = SEEK_SET;
    lock.l_start = 0;
    lock.l_len = 0;
    if (fcntl(fd, F_SETLK, &lock) < 0) {
      printf("Read lock denied, error = %d (%s)\n", errno, strerror(errno));
    } else {
      fprintf(
          stderr,
          "Oops! Read lock acquired when parent process has write lock.. ?!\n");
      exit(EXIT_FAILURE);
    }
    if (lseek(fd, 0, SEEK_SET) < 0) {
      perror("lseek(2) failed");
      exit(EXIT_FAILURE);
    }
    ssize_t r;
    if ((r = read(fd, readbuff, sizeof(readbuff) - 1)) < 0) {
      printf("read(2) returned with error %d (%s); mandatory locking seems to "
             "work.\n",
             errno, strerror(errno));
    } else {
      readbuff[r] = '\0';
      printf("read(2) returned success: %s; mandatory locking doesn't work.\n",
             readbuff);
    }
  } else {
    struct flock lock;
    lock.l_type = F_WRLCK;
    lock.l_whence = SEEK_SET;
    lock.l_start = 0;
    lock.l_len = 0;
    if (fcntl(fd, F_SETLK, &lock) < 0) {
      perror("fcntl(2) failed");
      NOTIFY(f);
      exit(EXIT_FAILURE);
    }
    NOTIFY(f);
    wait(NULL);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    125f <main+0x4f>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdbe(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
mov    0x2d81(%rip),%rax
or     (%rax),%esi
mov    0x2d70(%rip),%rax
or     (%rax),%esi
mov    0x2cdf(%rip),%rax
mov    (%rax),%edx
mov    0x2d2e(%rip),%rax
or     (%rax),%edx
mov    0x2ce5(%rip),%rax
or     (%rax),%edx
mov    0x2cd4(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1304 <main+0xf4>
mov    0x2ce9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x70(%rbp)
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x74(%rbp),%edi
mov    -0x70(%rbp),%rdx
mov    %rax,%rcx
lea    0xd2d(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2c73(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1100 <fstat@plt>
cmp    $0x0,%rax
jge    136c <main+0x15c>
mov    0x2c87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x84(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x80(%rbp)
mov    0x2c54(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x84(%rbp),%edi
mov    -0x80(%rbp),%rdx
mov    %rax,%rcx
lea    0xcdb(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2c0b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    0x2c3f(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%esi
mov    0x2c11(%rip),%rax
or     (%rax),%esi
call   10a0 <fchmod@plt>
cmp    $0x0,%rax
jge    13b2 <main+0x1a2>
lea    0xcb3(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2bc5(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    0xc44(%rip),%rsi
mov    $0x7,%edx
call   1050 <write@plt>
cmp    $0x0,%rax
jge    13ea <main+0x1da>
lea    0xc9d(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2b8d(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10f0 <NOTIFY_INIT@plt>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    1420 <main+0x210>
lea    0xc76(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2b57(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    15d4 <main+0x3c4>
mov    $0x0,%al
call   1130 <WAIT_NOTIFY@plt>
mov    -0x14(%rbp),%edi
mov    0x2b64(%rip),%rax
mov    (%rax),%esi
mov    0x2b8b(%rip),%rax
mov    (%rax),%rdx
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jge    1471 <main+0x261>
lea    0xc34(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2b06(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2ad8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2b04(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2b23(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jge    14f3 <main+0x2e3>
mov    0x2acf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x88(%rbp)
mov    0x2ac0(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x88(%rbp),%esi
mov    %rax,%rdx
lea    0xbbb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1518 <main+0x308>
mov    0x2aae(%rip),%rax
mov    (%rax),%edi
lea    0xbc2(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2a5f(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2a66(%rip),%rax
mov    (%rax),%rdx
xor    %esi,%esi
call   1070 <lseek@plt>
cmp    $0x0,%rax
jge    1550 <main+0x340>
lea    0xbca(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2a27(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    0x2b56(%rip),%rsi
mov    $0x7,%edx
call   1080 <read@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jae    15ab <main+0x39b>
mov    0x2a17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8c(%rbp)
mov    0x2a08(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x8c(%rbp),%esi
mov    %rax,%rdx
lea    0xb78(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    15cf <main+0x3bf>
mov    -0x48(%rbp),%rcx
lea    0x2afa(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0xb9d(%rip),%rdi
lea    0x2ae8(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1652 <main+0x442>
mov    0x2985(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x29a1(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
movq   $0x0,-0x60(%rbp)
movq   $0x0,-0x68(%rbp)
mov    -0x14(%rbp),%edi
mov    0x29c0(%rip),%rax
mov    (%rax),%esi
lea    -0x68(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jge    1640 <main+0x430>
lea    0xa6e(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1030 <NOTIFY@plt>
mov    0x2937(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1030 <NOTIFY@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1110 <wait@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r14
cmp    $0x1,%edi
jle    141b <main+0x20b>
mov    0x2dad(%rip),%rax
mov    0x2dc6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2db3(%rip),%rax
or     (%rax),%esi
mov    0x2d22(%rip),%rax
mov    0x2d73(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d28(%rip),%rax
or     (%rax),%edx
mov    0x8(%r14),%rdi
mov    0x2d13(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
test   %eax,%eax
js     143a <main+0x22a>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1100 <fstat@plt>
test   %rax,%rax
js     145e <main+0x24e>
mov    0x2d26(%rip),%rax
mov    (%rax),%esi
not    %esi
and    0x8(%rsp),%esi
mov    0x2cf7(%rip),%rax
or     (%rax),%esi
mov    %ebp,%edi
call   10a0 <fchmod@plt>
test   %rax,%rax
js     1491 <main+0x281>
lea    0xd46(%rip),%rsi
mov    %ebp,%edi
mov    $0x7,%edx
call   1050 <write@plt>
test   %rax,%rax
js     149a <main+0x28a>
xor    %eax,%eax
call   10f0 <NOTIFY_INIT@plt>
xor    %eax,%eax
call   1120 <fork@plt>
test   %rax,%rax
js     14a3 <main+0x293>
je     1341 <main+0x131>
mov    %rax,%rbx
mov    0x2c6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2c89(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x20(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2cb2(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1040 <fcntl@plt>
test   %rax,%rax
js     14ac <main+0x29c>
mov    %rbx,%rdi
call   1030 <NOTIFY@plt>
xor    %edi,%edi
call   1110 <wait@plt>
jmp    1410 <main+0x200>
xor    %eax,%eax
call   1130 <WAIT_NOTIFY@plt>
mov    0x2c51(%rip),%rax
mov    (%rax),%esi
mov    0x2c78(%rip),%rax
mov    (%rax),%rdx
mov    %ebp,%edi
call   1040 <fcntl@plt>
test   %rax,%rax
js     14c2 <main+0x2b2>
mov    0x2bde(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2c09(%rip),%r14
mov    (%r14),%rax
mov    %rax,0x20(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2c32(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1040 <fcntl@plt>
test   %rax,%rax
jns    14cb <main+0x2bb>
mov    0x2bdc(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10e0 <strerror@plt>
lea    0xcde(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%r14),%rdx
mov    %ebp,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
test   %rax,%rax
js     14e4 <main+0x2d4>
lea    0x2cc4(%rip),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x7,%edx
call   1080 <read@plt>
movb   $0x0,(%rax,%rbx,1)
lea    0xd11(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2b86(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xbda(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    14f0 <main+0x2e0>
mov    0x2b67(%rip),%rax
mov    (%rax),%ebx
mov    0x8(%r14),%rbp
mov    0x2b42(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xbc0(%rip),%rsi
jmp    1480 <main+0x270>
mov    0x2b43(%rip),%rax
mov    (%rax),%ebx
mov    0x8(%r14),%rbp
mov    0x2b1e(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xbb2(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    14f0 <main+0x2e0>
lea    0xbba(%rip),%rdi
jmp    14eb <main+0x2db>
lea    0xbd3(%rip),%rdi
jmp    14eb <main+0x2db>
lea    0xbd9(%rip),%rdi
jmp    14eb <main+0x2db>
lea    0xbdf(%rip),%rdi
call   10c0 <perror@plt>
mov    %rbx,%rdi
call   1030 <NOTIFY@plt>
jmp    14f0 <main+0x2e0>
lea    0xbc9(%rip),%rdi
jmp    14eb <main+0x2db>
mov    0x2ad6(%rip),%rax
mov    (%rax),%edi
lea    0xbea(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    14f0 <main+0x2e0>
lea    0xc1c(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2a79(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i64, i64, i8*, i32 }
%struct.stat = type { i32 }
@main.file_contents = internal constant [8 x i8] c"abcdefg\00"
@main.readbuff = internal global [8 x i8] zeroinitializer
@stderr = global i32 0
@.str = [20 x i8] c"Usage: %s filename\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [22 x i8] c"Couldn't open %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [32 x i8] c"stat(2) failed for file %s: %s\0A\00"
@S_IXGRP = global i32 0
@S_ISGID = global i32 0
@.str.3 = [34 x i8] c"Couldn't enable mandatory locking\00"
@.str.4 = [15 x i8] c"write(2) error\00"
@.str.5 = [15 x i8] c"fork(2) failed\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global %struct.flock* null
@.str.6 = [16 x i8] c"fcntl(2) failed\00"
@F_RDLCK = global i32 0
@SEEK_SET = global i8* null
@F_SETLK = global i32 0
@.str.7 = [35 x i8] c"Read lock denied, error = %d (%s)\0A\00"
@.str.8 = [66 x i8] c"Oops! Read lock acquired when parent process has write lock.. ?!\0A\00"
@.str.9 = [16 x i8] c"lseek(2) failed\00"
@.str.10 = [71 x i8] c"read(2) returned with error %d (%s); mandatory locking seems to work.\0A\00"
@.str.11 = [63 x i8] c"read(2) returned success: %s; mandatory locking doesn't work.\0A\00"
@F_WRLCK = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i64
%9 = alloca %struct.flock
%10 = alloca i64
%11 = alloca %struct.flock
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %22
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = load i32, i32* @O_RDWR
%27 = load i32, i32* @O_CREAT
%28 = or i32 %26, %27
%29 = load i32, i32* @O_TRUNC
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IRUSR
%32 = load i32, i32* @S_IWUSR
%33 = or i32 %31, %32
%34 = load i32, i32* @S_IRGRP
%35 = or i32 %33, %34
%36 = load i32, i32* @S_IROTH
%37 = or i32 %35, %36
%38 = call i32 @open(i8* %25, i32 %30, i32 %37)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %51
41: ; preds = %22
%42 = load i32, i32* @stderr
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %45, i8* %47)
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %22
%52 = load i32, i32* %6
%53 = call i64 @fstat(i32 %52, %struct.stat* %7)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %65
55: ; preds = %51
%56 = load i32, i32* @stderr
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 1
%59 = load i8*, i8** %58
%60 = load i32, i32* @errno
%61 = call i8* @strerror(i32 %60)
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %59, i8* %61)
%63 = load i32, i32* @EXIT_FAILURE
%64 = call i32 @exit(i32 %63)
unreachable
65: ; preds = %51
%66 = load i32, i32* %6
%67 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%68 = load i32, i32* %67
%69 = load i32, i32* @S_IXGRP
%70 = xor i32 %69, -1
%71 = and i32 %68, %70
%72 = load i32, i32* @S_ISGID
%73 = or i32 %71, %72
%74 = call i64 @fchmod(i32 %66, i32 %73)
%75 = icmp slt i64 %74, 0
br i1 %75, label %76, label %80
76: ; preds = %65
%77 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%78 = load i32, i32* @EXIT_FAILURE
%79 = call i32 @exit(i32 %78)
unreachable
80: ; preds = %65
%81 = load i32, i32* %6
%82 = call i64 @write(i32 %81, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @main.file_contents, i64 0, i64 0), i32 7)
%83 = icmp slt i64 %82, 0
br i1 %83, label %84, label %88
84: ; preds = %80
%85 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%86 = load i32, i32* @EXIT_FAILURE
%87 = call i32 @exit(i32 %86)
unreachable
88: ; preds = %80
%89 = call i32 (...) @NOTIFY_INIT()
%90 = call i64 (...) @fork()
store i64 %90, i64* %8
%91 = icmp slt i64 %90, 0
br i1 %91, label %92, label %96
92: ; preds = %88
%93 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%94 = load i32, i32* @EXIT_FAILURE
%95 = call i32 @exit(i32 %94)
unreachable
96: ; preds = %88
%97 = load i64, i64* %8
%98 = icmp eq i64 %97, 0
br i1 %98, label %99, label %154
99: ; preds = %96
%100 = call i32 (...) @WAIT_NOTIFY()
%101 = load i32, i32* %6
%102 = load i32, i32* @F_SETFL
%103 = load %struct.flock*, %struct.flock** @O_NONBLOCK
%104 = call i64 @fcntl(i32 %101, i32 %102, %struct.flock* %103)
%105 = icmp slt i64 %104, 0
br i1 %105, label %106, label %110
106: ; preds = %99
%107 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%108 = load i32, i32* @EXIT_FAILURE
%109 = call i32 @exit(i32 %108)
unreachable
110: ; preds = %99
%111 = load i32, i32* @F_RDLCK
%112 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 3
store i32 %111, i32* %112
%113 = load i8*, i8** @SEEK_SET
%114 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 2
store i8* %113, i8** %114
%115 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 1
store i64 0, i64* %115
%116 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 0
store i64 0, i64* %116
%117 = load i32, i32* %6
%118 = load i32, i32* @F_SETLK
%119 = call i64 @fcntl(i32 %117, i32 %118, %struct.flock* %9)
%120 = icmp slt i64 %119, 0
br i1 %120, label %121, label %126
121: ; preds = %110
%122 = load i32, i32* @errno
%123 = load i32, i32* @errno
%124 = call i8* @strerror(i32 %123)
%125 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %122, i8* %124)
br label %131
126: ; preds = %110
%127 = load i32, i32* @stderr
%128 = call i32 (i32, i8*, ...) @fprintf(i32 %127, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.8, i64 0, i64 0))
%129 = load i32, i32* @EXIT_FAILURE
%130 = call i32 @exit(i32 %129)
unreachable
131: ; preds = %121
%132 = load i32, i32* %6
%133 = load i8*, i8** @SEEK_SET
%134 = call i64 @lseek(i32 %132, i32 0, i8* %133)
%135 = icmp slt i64 %134, 0
br i1 %135, label %136, label %140
136: ; preds = %131
%137 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
%138 = load i32, i32* @EXIT_FAILURE
%139 = call i32 @exit(i32 %138)
unreachable
140: ; preds = %131
%141 = load i32, i32* %6
%142 = call i64 @read(i32 %141, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @main.readbuff, i64 0, i64 0), i32 7)
store i64 %142, i64* %10
%143 = icmp ult i64 %142, 0
br i1 %143, label %144, label %149
144: ; preds = %140
%145 = load i32, i32* @errno
%146 = load i32, i32* @errno
%147 = call i8* @strerror(i32 %146)
%148 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.10, i64 0, i64 0), i32 %145, i8* %147)
br label %153
149: ; preds = %140
%150 = load i64, i64* %10
%151 = getelementptr inbounds [8 x i8], [8 x i8]* @main.readbuff, i64 0, i64 %150
store i8 0, i8* %151
%152 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @main.readbuff, i64 0, i64 0))
br label %153
153: ; preds = %149, %144
br label %175
154: ; preds = %96
%155 = load i32, i32* @F_WRLCK
%156 = getelementptr inbounds %struct.flock, %struct.flock* %11, i32 0, i32 3
store i32 %155, i32* %156
%157 = load i8*, i8** @SEEK_SET
%158 = getelementptr inbounds %struct.flock, %struct.flock* %11, i32 0, i32 2
store i8* %157, i8** %158
%159 = getelementptr inbounds %struct.flock, %struct.flock* %11, i32 0, i32 1
store i64 0, i64* %159
%160 = getelementptr inbounds %struct.flock, %struct.flock* %11, i32 0, i32 0
store i64 0, i64* %160
%161 = load i32, i32* %6
%162 = load i32, i32* @F_SETLK
%163 = call i64 @fcntl(i32 %161, i32 %162, %struct.flock* %11)
%164 = icmp slt i64 %163, 0
br i1 %164, label %165, label %171
165: ; preds = %154
%166 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%167 = load i64, i64* %8
%168 = call i32 @NOTIFY(i64 %167)
%169 = load i32, i32* @EXIT_FAILURE
%170 = call i32 @exit(i32 %169)
unreachable
171: ; preds = %154
%172 = load i64, i64* %8
%173 = call i32 @NOTIFY(i64 %172)
%174 = call i32 @wait(i32* null)
br label %175
175: ; preds = %171, %153
ret i32 0
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i8*, i32 }
%struct.stat = type { i32 }
@main.file_contents = internal constant [8 x i8] c"abcdefg\00"
@main.readbuff = internal global [8 x i8] zeroinitializer
@stderr = global i32 0
@.str = [20 x i8] c"Usage: %s filename\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [22 x i8] c"Couldn't open %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [32 x i8] c"stat(2) failed for file %s: %s\0A\00"
@S_IXGRP = global i32 0
@S_ISGID = global i32 0
@.str.3 = [34 x i8] c"Couldn't enable mandatory locking\00"
@.str.4 = [15 x i8] c"write(2) error\00"
@.str.5 = [15 x i8] c"fork(2) failed\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global %struct.flock* null
@.str.6 = [16 x i8] c"fcntl(2) failed\00"
@F_RDLCK = global i32 0
@SEEK_SET = global i8* null
@F_SETLK = global i32 0
@.str.7 = [35 x i8] c"Read lock denied, error = %d (%s)\0A\00"
@.str.8 = [66 x i8] c"Oops! Read lock acquired when parent process has write lock.. ?!\0A\00"
@.str.9 = [16 x i8] c"lseek(2) failed\00"
@.str.11 = [63 x i8] c"read(2) returned success: %s; mandatory locking doesn't work.\0A\00"
@F_WRLCK = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.flock
%5 = alloca %struct.flock
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %13
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDWR
%17 = load i32, i32* @O_CREAT
%18 = or i32 %17, %16
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %22, %21
%24 = load i32, i32* @S_IRGRP
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IROTH
%27 = or i32 %25, %26
%28 = tail call i32 @open(i8* %15, i32 %20, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %13
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %14
%33 = load i32, i32* @errno
%34 = tail call i8* @strerror(i32 %33)
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %32, i8* %34)
%36 = load i32, i32* @EXIT_FAILURE
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %13
%39 = bitcast %struct.stat* %3 to i8*
%40 = call i64 @fstat(i32 %28, %struct.stat* %3)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %38
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %14
%45 = load i32, i32* @errno
%46 = tail call i8* @strerror(i32 %45)
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %44, i8* %46)
%48 = load i32, i32* @EXIT_FAILURE
%49 = tail call i32 @exit(i32 %48)
unreachable
50: ; preds = %38
%51 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%52 = load i32, i32* %51
%53 = load i32, i32* @S_IXGRP
%54 = xor i32 %53, -1
%55 = and i32 %52, %54
%56 = load i32, i32* @S_ISGID
%57 = or i32 %55, %56
%58 = tail call i64 @fchmod(i32 %28, i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %50
%61 = tail call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%62 = load i32, i32* @EXIT_FAILURE
%63 = tail call i32 @exit(i32 %62)
unreachable
64: ; preds = %50
%65 = tail call i64 @write(i32 %28, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @main.file_contents, i64 0, i64 0), i32 7)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %64
%68 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%69 = load i32, i32* @EXIT_FAILURE
%70 = tail call i32 @exit(i32 %69)
unreachable
71: ; preds = %64
%72 = tail call i32 (...) @NOTIFY_INIT()
%73 = tail call i64 (...) @fork()
%74 = icmp slt i64 %73, 0
br i1 %74, label %75, label %79
75: ; preds = %71
%76 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%77 = load i32, i32* @EXIT_FAILURE
%78 = tail call i32 @exit(i32 %77)
unreachable
79: ; preds = %71
%80 = icmp eq i64 %73, 0
br i1 %80, label %81, label %121
81: ; preds = %79
%82 = tail call i32 (...) @WAIT_NOTIFY()
%83 = load i32, i32* @F_SETFL
%84 = load %struct.flock*, %struct.flock** @O_NONBLOCK
%85 = tail call i64 @fcntl(i32 %28, i32 %83, %struct.flock* %84)
%86 = icmp slt i64 %85, 0
br i1 %86, label %87, label %91
87: ; preds = %81
%88 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%89 = load i32, i32* @EXIT_FAILURE
%90 = tail call i32 @exit(i32 %89)
unreachable
91: ; preds = %81
%92 = bitcast %struct.flock* %4 to i8*
%93 = load i32, i32* @F_RDLCK
%94 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 3
store i32 %93, i32* %94
%95 = load i8*, i8** @SEEK_SET
%96 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 2
store i8* %95, i8** %96
%97 = bitcast %struct.flock* %4 to i8*
%98 = load i32, i32* @F_SETLK
%99 = call i64 @fcntl(i32 %28, i32 %98, %struct.flock* %4)
%100 = icmp slt i64 %99, 0
br i1 %100, label %101, label %108
101: ; preds = %91
%102 = load i32, i32* @errno
%103 = call i8* @strerror(i32 %102)
%104 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %102, i8* %103)
%105 = load i8*, i8** @SEEK_SET
%106 = call i64 @lseek(i32 %28, i32 0, i8* %105)
%107 = icmp slt i64 %106, 0
br i1 %107, label %113, label %117
108: ; preds = %91
%109 = load i32, i32* @stderr
%110 = call i32 (i32, i8*, ...) @fprintf(i32 %109, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.8, i64 0, i64 0))
%111 = load i32, i32* @EXIT_FAILURE
%112 = call i32 @exit(i32 %111)
unreachable
113: ; preds = %101
%114 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
%115 = load i32, i32* @EXIT_FAILURE
%116 = call i32 @exit(i32 %115)
unreachable
117: ; preds = %101
%118 = call i64 @read(i32 %28, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @main.readbuff, i64 0, i64 0), i32 7)
%119 = getelementptr inbounds [8 x i8], [8 x i8]* @main.readbuff, i64 0, i64 %118
store i8 0, i8* %119
%120 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([63 x i8], [63 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @main.readbuff, i64 0, i64 0))
br label %139
121: ; preds = %79
%122 = bitcast %struct.flock* %5 to i8*
%123 = load i32, i32* @F_WRLCK
%124 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 3
store i32 %123, i32* %124
%125 = load i8*, i8** @SEEK_SET
%126 = getelementptr inbounds %struct.flock, %struct.flock* %5, i64 0, i32 2
store i8* %125, i8** %126
%127 = bitcast %struct.flock* %5 to i8*
%128 = load i32, i32* @F_SETLK
%129 = call i64 @fcntl(i32 %28, i32 %128, %struct.flock* %5)
%130 = icmp slt i64 %129, 0
br i1 %130, label %131, label %136
131: ; preds = %121
%132 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%133 = call i32 @NOTIFY(i64 %73)
%134 = load i32, i32* @EXIT_FAILURE
%135 = call i32 @exit(i32 %134)
unreachable
136: ; preds = %121
%137 = call i32 @NOTIFY(i64 %73)
%138 = call i32 @wait(i32* null)
br label %139
139: ; preds = %136, %117
ret i32 0
}