#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;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int O_CREAT;
int /*<<< orphan*/ O_NONBLOCK;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ SEEK_SET;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_ISGID;
int S_IWUSR;
int S_IXGRP;
int /*<<< orphan*/ TELL_CHILD(scalar_t__);
int /*<<< orphan*/ TELL_WAIT();
int /*<<< orphan*/ WAIT_PARENT();
int errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fchmod(int, int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fstat(int, struct stat *);
int lock_reg(int, int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
             int /*<<< orphan*/, int /*<<< orphan*/);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ set_fl(int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  pid_t pid;
  char buf[5];
  struct stat statbuf;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s filename\n", argv[0]);
    exit(1);
  }
  if ((fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC,
                 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
    fprintf(stderr, "open error\n");
    exit(1);
  }
  if (write(fd, "abcdef", 6) != 6) {
    fprintf(stderr, "write error\n");
    exit(1);
  }
  if (fstat(fd, &statbuf) < 0) {
    fprintf(stderr, "fstat error\n");
    exit(1);
  }
  if (fchmod(fd, (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 0) {
    fprintf(stderr, "fchmod error\n");
    exit(1);
  }
  TELL_WAIT();
  if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error\n");
    exit(1);
  } else if (pid > 0) {
    if (lock_reg((fd), F_SETLK, F_WRLCK, (0), (SEEK_SET), (0)) < 0) {
      fprintf(stderr, "write_lock error\n");
      exit(1);
    }
    TELL_CHILD(pid);
    if (waitpid(pid, NULL, 0) < 0) {
      fprintf(stderr, "waitpid error\n");
      exit(1);
    }
  } else {
    WAIT_PARENT();
    set_fl(fd, O_NONBLOCK);
    if (lock_reg((fd), F_SETLK, F_RDLCK, (0), (SEEK_SET), (0)) != -1) {
      fprintf(stderr, "child: read_lock succeeded");
      exit(1);
    }
    printf("read_lock of already-locked region returns %d\n", errno);
    if (lseek(fd, 0, SEEK_SET) == -1) {
      fprintf(stderr, "lseek error\n");
      exit(1);
    }
    if (read(fd, buf, 2) < 0) {
      printf("read failed (mandatory locking works)\n");
      return 1;
    } else {
      printf("read OK (no mandatory locking), buf = %2.2s\n", buf);
    }
  }
  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)
cmpl   $0x2,-0x8(%rbp)
je     1248 <main+0x48>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
mov    0x2d98(%rip),%rax
or     (%rax),%esi
mov    0x2d87(%rip),%rax
or     (%rax),%esi
mov    0x2d06(%rip),%rax
mov    (%rax),%edx
mov    0x2d45(%rip),%rax
or     (%rax),%edx
mov    0x2d04(%rip),%rax
or     (%rax),%edx
mov    0x2cf3(%rip),%rax
or     (%rax),%edx
call   10e0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    12c1 <main+0xc1>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
lea    0xd64(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    0xd55(%rip),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
cmp    $0x6,%eax
je     12ff <main+0xff>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
lea    0xd39(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1110 <fstat@plt>
cmp    $0x0,%rax
jge    1336 <main+0x136>
mov    0x2c8c(%rip),%rax
mov    (%rax),%edi
lea    0xd0f(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%esi
mov    0x2c75(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%esi
mov    0x2c4f(%rip),%rax
or     (%rax),%esi
call   10c0 <fchmod@plt>
cmp    $0x0,%rax
jge    1383 <main+0x183>
mov    0x2c3f(%rip),%rax
mov    (%rax),%edi
lea    0xccf(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    $0x0,%al
call   1080 <TELL_WAIT@plt>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    13c0 <main+0x1c0>
mov    0x2c02(%rip),%rax
mov    (%rax),%edi
lea    0xca0(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jle    1467 <main+0x267>
mov    -0x14(%rbp),%edi
mov    0x2bf3(%rip),%rax
mov    (%rax),%esi
mov    0x2b92(%rip),%rax
mov    (%rax),%edx
mov    0x2ba9(%rip),%rax
mov    (%rax),%r8d
xor    %r9d,%r9d
mov    %r9d,%ecx
call   1060 <lock_reg@plt>
cmp    $0x0,%eax
jge    141f <main+0x21f>
mov    0x2ba3(%rip),%rax
mov    (%rax),%edi
lea    0xc4d(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1050 <TELL_CHILD@plt>
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10d0 <waitpid@plt>
cmp    $0x0,%rax
jge    1462 <main+0x262>
mov    0x2b60(%rip),%rax
mov    (%rax),%edi
lea    0xc1c(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
jmp    1573 <main+0x373>
mov    $0x0,%al
call   1100 <WAIT_PARENT@plt>
mov    -0x14(%rbp),%edi
mov    0x2b58(%rip),%rax
mov    (%rax),%esi
call   1030 <set_fl@plt>
mov    -0x14(%rbp),%edi
mov    0x2b3f(%rip),%rax
mov    (%rax),%esi
mov    0x2ace(%rip),%rax
mov    (%rax),%edx
mov    0x2af5(%rip),%rax
mov    (%rax),%r8d
xor    %r9d,%r9d
mov    %r9d,%ecx
call   1060 <lock_reg@plt>
cmp    $0xffffffff,%eax
je     14d3 <main+0x2d3>
mov    0x2aef(%rip),%rax
mov    (%rax),%edi
lea    0xbba(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    0x2abe(%rip),%rax
mov    (%rax),%esi
lea    0xbb4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2a9c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1527 <main+0x327>
mov    0x2a9b(%rip),%rax
mov    (%rax),%edi
lea    0xbb0(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x25(%rbp),%rsi
mov    $0x2,%edx
call   10a0 <read@plt>
cmp    $0x0,%rax
jge    155c <main+0x35c>
lea    0xb8a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    157f <main+0x37f>
lea    -0x25(%rbp),%rsi
lea    0xb93(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1573 <main+0x373>
jmp    1578 <main+0x378>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    140b <main+0x1fb>
mov    0x2db0(%rip),%rcx
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2db6(%rip),%rcx
or     (%rcx),%eax
mov    0x2d35(%rip),%rcx
mov    0x2d76(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2d33(%rip),%rcx
or     (%rcx),%edx
mov    0x8(%rsi),%rdi
mov    0x2d1e(%rip),%rcx
or     (%rcx),%edx
mov    %eax,%esi
call   10f0 <open@plt>
test   %eax,%eax
js     142f <main+0x21f>
mov    %eax,%ebp
lea    0xda4(%rip),%rsi
mov    %eax,%edi
mov    $0x6,%edx
call   1050 <write@plt>
cmp    $0x6,%eax
jne    1441 <main+0x231>
mov    %rsp,%rsi
mov    %ebp,%edi
call   1120 <fstat@plt>
test   %rax,%rax
js     1453 <main+0x243>
mov    0x2d0d(%rip),%rax
mov    (%rax),%esi
not    %esi
and    (%rsp),%esi
mov    0x2ce7(%rip),%rax
or     (%rax),%esi
mov    %ebp,%edi
call   10d0 <fchmod@plt>
test   %rax,%rax
js     1465 <main+0x255>
xor    %eax,%eax
call   1090 <TELL_WAIT@plt>
xor    %eax,%eax
call   1130 <fork@plt>
test   %rax,%rax
js     1477 <main+0x267>
je     134c <main+0x13c>
mov    %rax,%rbx
mov    0x2cda(%rip),%rax
mov    (%rax),%esi
mov    0x2c79(%rip),%rax
mov    (%rax),%edx
mov    0x2c90(%rip),%rax
mov    (%rax),%r8d
xor    %r14d,%r14d
mov    %ebp,%edi
xor    %ecx,%ecx
xor    %r9d,%r9d
call   1070 <lock_reg@plt>
test   %eax,%eax
js     1489 <main+0x279>
mov    %rbx,%rdi
call   1060 <TELL_CHILD@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <waitpid@plt>
test   %rax,%rax
jns    13ff <main+0x1ef>
mov    0x2c6a(%rip),%rax
mov    (%rax),%edi
lea    0xd26(%rip),%rsi
jmp    14bd <main+0x2ad>
xor    %eax,%eax
call   1110 <WAIT_PARENT@plt>
mov    0x2c76(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <set_fl@plt>
mov    0x2c5e(%rip),%rax
mov    (%rax),%esi
mov    0x2bed(%rip),%rax
mov    (%rax),%edx
mov    0x2c14(%rip),%rbx
mov    (%rbx),%r8d
mov    %ebp,%edi
xor    %ecx,%ecx
xor    %r9d,%r9d
call   1070 <lock_reg@plt>
cmp    $0xffffffff,%eax
jne    149b <main+0x28b>
mov    0x2bfd(%rip),%rax
mov    (%rax),%esi
lea    0xcf3(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    (%rbx),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   10a0 <lseek@plt>
cmp    $0xffffffff,%eax
je     14ad <main+0x29d>
lea    0xb(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   10b0 <read@plt>
test   %rax,%rax
js     13ed <main+0x1dd>
lea    0xcf7(%rip),%rdi
xor    %r14d,%r14d
lea    0xb(%rsp),%rsi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    13ff <main+0x1ef>
lea    0xd0c(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2b96(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xbe2(%rip),%rsi
xor    %eax,%eax
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2b72(%rip),%rax
mov    (%rax),%edi
lea    0xbd5(%rip),%rsi
jmp    14bd <main+0x2ad>
mov    0x2b60(%rip),%rax
mov    (%rax),%edi
lea    0xbd6(%rip),%rsi
jmp    14bd <main+0x2ad>
mov    0x2b4e(%rip),%rax
mov    (%rax),%edi
lea    0xbd1(%rip),%rsi
jmp    14bd <main+0x2ad>
mov    0x2b3c(%rip),%rax
mov    (%rax),%edi
lea    0xbcc(%rip),%rsi
jmp    14bd <main+0x2ad>
mov    0x2b2a(%rip),%rax
mov    (%rax),%edi
lea    0xbc8(%rip),%rsi
jmp    14bd <main+0x2ad>
mov    0x2b18(%rip),%rax
mov    (%rax),%edi
lea    0xbc2(%rip),%rsi
jmp    14bd <main+0x2ad>
mov    0x2b06(%rip),%rax
mov    (%rax),%edi
lea    0xbd1(%rip),%rsi
jmp    14bd <main+0x2ad>
mov    0x2af4(%rip),%rax
mov    (%rax),%edi
lea    0xc09(%rip),%rsi
xor    %eax,%eax
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [20 x i8] c"Usage: %s filename\0A\00"
@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 = [12 x i8] c"open error\0A\00"
@.str.2 = [7 x i8] c"abcdef\00"
@.str.3 = [13 x i8] c"write error\0A\00"
@.str.4 = [13 x i8] c"fstat error\0A\00"
@S_IXGRP = global i32 0
@S_ISGID = global i32 0
@.str.5 = [14 x i8] c"fchmod error\0A\00"
@.str.6 = [12 x i8] c"fork error\0A\00"
@F_SETLK = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@.str.7 = [18 x i8] c"write_lock error\0A\00"
@.str.8 = [15 x i8] c"waitpid error\0A\00"
@O_NONBLOCK = global i32 0
@F_RDLCK = global i32 0
@.str.9 = [27 x i8] c"child: read_lock succeeded\00"
@.str.10 = [47 x i8] c"read_lock of already-locked region returns %d\0A\00"
@errno = global i32 0
@.str.11 = [13 x i8] c"lseek error\0A\00"
@.str.12 = [39 x i8] c"read failed (mandatory locking works)\0A\00"
@.str.13 = [45 x i8] c"read OK (no mandatory locking), buf = %2.2s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca [5 x i8]
%9 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDWR
%24 = load i32, i32* @O_CREAT
%25 = or i32 %23, %24
%26 = load i32, i32* @O_TRUNC
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IRUSR
%29 = load i32, i32* @S_IWUSR
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IRGRP
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IROTH
%34 = or i32 %32, %33
%35 = call i32 @open(i8* %22, i32 %27, i32 %34)
store i32 %35, i32* %6
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %19
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %19
%42 = load i32, i32* %6
%43 = call i32 @write(i32 %42, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%44 = icmp ne i32 %43, 6
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %41
%50 = load i32, i32* %6
%51 = call i64 @fstat(i32 %50, %struct.stat* %9)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %49
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %49
%58 = load i32, i32* %6
%59 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%60 = load i32, i32* %59
%61 = load i32, i32* @S_IXGRP
%62 = xor i32 %61, -1
%63 = and i32 %60, %62
%64 = load i32, i32* @S_ISGID
%65 = or i32 %63, %64
%66 = call i64 @fchmod(i32 %58, i32 %65)
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %72
68: ; preds = %57
%69 = load i32, i32* @stderr
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %57
%73 = call i32 (...) @TELL_WAIT()
%74 = call i64 (...) @fork()
store i64 %74, i64* %7
%75 = icmp slt i64 %74, 0
br i1 %75, label %76, label %80
76: ; preds = %72
%77 = load i32, i32* @stderr
%78 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %72
%81 = load i64, i64* %7
%82 = icmp sgt i64 %81, 0
br i1 %82, label %83, label %105
83: ; preds = %80
%84 = load i32, i32* %6
%85 = load i32, i32* @F_SETLK
%86 = load i32, i32* @F_WRLCK
%87 = load i32, i32* @SEEK_SET
%88 = call i32 @lock_reg(i32 %84, i32 %85, i32 %86, i32 0, i32 %87, i32 0)
%89 = icmp slt i32 %88, 0
br i1 %89, label %90, label %94
90: ; preds = %83
%91 = load i32, i32* @stderr
%92 = call i32 (i32, i8*, ...) @fprintf(i32 %91, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
%93 = call i32 @exit(i32 1)
unreachable
94: ; preds = %83
%95 = load i64, i64* %7
%96 = call i32 @TELL_CHILD(i64 %95)
%97 = load i64, i64* %7
%98 = call i64 @waitpid(i64 %97, i32* null, i32 0)
%99 = icmp slt i64 %98, 0
br i1 %99, label %100, label %104
100: ; preds = %94
%101 = load i32, i32* @stderr
%102 = call i32 (i32, i8*, ...) @fprintf(i32 %101, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
%103 = call i32 @exit(i32 1)
unreachable
104: ; preds = %94
br label %142
105: ; preds = %80
%106 = call i32 (...) @WAIT_PARENT()
%107 = load i32, i32* %6
%108 = load i32, i32* @O_NONBLOCK
%109 = call i32 @set_fl(i32 %107, i32 %108)
%110 = load i32, i32* %6
%111 = load i32, i32* @F_SETLK
%112 = load i32, i32* @F_RDLCK
%113 = load i32, i32* @SEEK_SET
%114 = call i32 @lock_reg(i32 %110, i32 %111, i32 %112, i32 0, i32 %113, i32 0)
%115 = icmp ne i32 %114, -1
br i1 %115, label %116, label %120
116: ; preds = %105
%117 = load i32, i32* @stderr
%118 = call i32 (i32, i8*, ...) @fprintf(i32 %117, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0))
%119 = call i32 @exit(i32 1)
unreachable
120: ; preds = %105
%121 = load i32, i32* @errno
%122 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.10, i64 0, i64 0), i32 %121)
%123 = load i32, i32* %6
%124 = load i32, i32* @SEEK_SET
%125 = call i32 @lseek(i32 %123, i32 0, i32 %124)
%126 = icmp eq i32 %125, -1
br i1 %126, label %127, label %131
127: ; preds = %120
%128 = load i32, i32* @stderr
%129 = call i32 (i32, i8*, ...) @fprintf(i32 %128, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.11, i64 0, i64 0))
%130 = call i32 @exit(i32 1)
unreachable
131: ; preds = %120
%132 = load i32, i32* %6
%133 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%134 = call i64 @read(i32 %132, i8* %133, i32 2)
%135 = icmp slt i64 %134, 0
br i1 %135, label %136, label %138
136: ; preds = %131
%137 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.12, i64 0, i64 0))
store i32 1, i32* %3
br label %144
138: ; preds = %131
%139 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%140 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.13, i64 0, i64 0), i8* %139)
br label %141
141: ; preds = %138
br label %142
142: ; preds = %141, %104
br label %143
143: ; preds = %142
store i32 0, i32* %3
br label %144
144: ; preds = %143, %136
%145 = load i32, i32* %3
ret i32 %145
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [20 x i8] c"Usage: %s filename\0A\00"
@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 = [12 x i8] c"open error\0A\00"
@.str.2 = [7 x i8] c"abcdef\00"
@.str.3 = [13 x i8] c"write error\0A\00"
@.str.4 = [13 x i8] c"fstat error\0A\00"
@S_IXGRP = global i32 0
@S_ISGID = global i32 0
@.str.5 = [14 x i8] c"fchmod error\0A\00"
@.str.6 = [12 x i8] c"fork error\0A\00"
@F_SETLK = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@.str.7 = [18 x i8] c"write_lock error\0A\00"
@.str.8 = [15 x i8] c"waitpid error\0A\00"
@O_NONBLOCK = global i32 0
@F_RDLCK = global i32 0
@.str.9 = [27 x i8] c"child: read_lock succeeded\00"
@.str.10 = [47 x i8] c"read_lock of already-locked region returns %d\0A\00"
@errno = global i32 0
@.str.11 = [13 x i8] c"lseek error\0A\00"
@.str.13 = [45 x i8] c"read OK (no mandatory locking), buf = %2.2s\0A\00"
@str = [38 x i8] c"read failed (mandatory locking works)\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [5 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
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 %34
30: ; preds = %13
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %13
%35 = tail call i32 @write(i32 %28, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%36 = icmp eq i32 %35, 6
br i1 %36, label %41, label %37
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = tail call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %34
%42 = call i64 @fstat(i32 %28, %struct.stat* %4)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %41
%45 = load i32, i32* @stderr
%46 = tail call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%47 = tail call i32 @exit(i32 1)
unreachable
48: ; preds = %41
%49 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%50 = load i32, i32* %49
%51 = load i32, i32* @S_IXGRP
%52 = xor i32 %51, -1
%53 = and i32 %50, %52
%54 = load i32, i32* @S_ISGID
%55 = or i32 %53, %54
%56 = tail call i64 @fchmod(i32 %28, i32 %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %48
%59 = load i32, i32* @stderr
%60 = tail call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%61 = tail call i32 @exit(i32 1)
unreachable
62: ; preds = %48
%63 = tail call i32 (...) @TELL_WAIT()
%64 = tail call i64 (...) @fork()
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %62
%67 = load i32, i32* @stderr
%68 = tail call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%69 = tail call i32 @exit(i32 1)
unreachable
70: ; preds = %62
%71 = icmp eq i64 %64, 0
br i1 %71, label %90, label %72
72: ; preds = %70
%73 = load i32, i32* @F_SETLK
%74 = load i32, i32* @F_WRLCK
%75 = load i32, i32* @SEEK_SET
%76 = tail call i32 @lock_reg(i32 %28, i32 %73, i32 %74, i32 0, i32 %75, i32 0)
%77 = icmp slt i32 %76, 0
br i1 %77, label %78, label %82
78: ; preds = %72
%79 = load i32, i32* @stderr
%80 = tail call i32 (i32, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
%81 = tail call i32 @exit(i32 1)
unreachable
82: ; preds = %72
%83 = tail call i32 @TELL_CHILD(i64 %64)
%84 = tail call i64 @waitpid(i64 %64, i32* null, i32 0)
%85 = icmp slt i64 %84, 0
br i1 %85, label %86, label %120
86: ; preds = %82
%87 = load i32, i32* @stderr
%88 = tail call i32 (i32, i8*, ...) @fprintf(i32 %87, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
%89 = tail call i32 @exit(i32 1)
unreachable
90: ; preds = %70
%91 = tail call i32 (...) @WAIT_PARENT()
%92 = load i32, i32* @O_NONBLOCK
%93 = tail call i32 @set_fl(i32 %28, i32 %92)
%94 = load i32, i32* @F_SETLK
%95 = load i32, i32* @F_RDLCK
%96 = load i32, i32* @SEEK_SET
%97 = tail call i32 @lock_reg(i32 %28, i32 %94, i32 %95, i32 0, i32 %96, i32 0)
%98 = icmp eq i32 %97, -1
br i1 %98, label %103, label %99
99: ; preds = %90
%100 = load i32, i32* @stderr
%101 = tail call i32 (i32, i8*, ...) @fprintf(i32 %100, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0))
%102 = tail call i32 @exit(i32 1)
unreachable
103: ; preds = %90
%104 = load i32, i32* @errno
%105 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.10, i64 0, i64 0), i32 %104)
%106 = load i32, i32* @SEEK_SET
%107 = tail call i32 @lseek(i32 %28, i32 0, i32 %106)
%108 = icmp eq i32 %107, -1
br i1 %108, label %109, label %113
109: ; preds = %103
%110 = load i32, i32* @stderr
%111 = tail call i32 (i32, i8*, ...) @fprintf(i32 %110, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.11, i64 0, i64 0))
%112 = tail call i32 @exit(i32 1)
unreachable
113: ; preds = %103
%114 = call i64 @read(i32 %28, i8* %5, i32 2)
%115 = icmp slt i64 %114, 0
br i1 %115, label %116, label %118
116: ; preds = %113
%117 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %120
118: ; preds = %113
%119 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.13, i64 0, i64 0), i8* %5)
br label %120
120: ; preds = %82, %118, %116
%121 = phi i32 [ 1, %116 ], [ 0, %118 ], [ 0, %82 ]
ret i32 %121
}