#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 */
typedef int /*<<< orphan*/ val;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ F_LOCK;
int /*<<< orphan*/ F_ULOCK;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int getpid();
scalar_t__ lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, int *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  pid_t pid;
  int fd, lfd, pfd[2];
  if (pipe(pfd) < 0) {
    perror("pfd");
    return 1;
  }
  printf("parent[%d]: tries to locks flock\n", getpid());
  lfd = open(".lock_for_lock.flock", O_WRONLY | O_CREAT, 0600);
  if (fd < 0) {
    perror("open");
    return 1;
  }
  printf("parent[%d]: tries to locks file\n", getpid());
  fd = open(".lock_for_single_process.flock", O_WRONLY | O_CREAT, 0600);
  if (fd < 0) {
    perror("open");
    return 1;
  }
  if (lockf(lfd, F_LOCK, 0) != 0) {
    perror("lockf");
    return 1;
  }
  if (lockf(fd, F_LOCK, 0) != 0) {
    perror("lockf");
    return 1;
  }
  pid = fork();
  if (pid < 0) {
    perror("fork");
    return 1;
  } else if (pid == 0) {
    close(fd);
    close(lfd);
    fd = open(".lock_for_single_process.flock", O_WRONLY | O_CREAT, 0600);
    if (fd < 0) {
      perror("open");
      return 1;
    }
    printf("child[%d]: tries locks file\n", getpid());
    if (lockf(fd, F_LOCK, 0) != 0) {
      perror("child: lock");
      return 1;
    }
    printf("child[%d]: let parent know that child lock file\n", getpid());
    close(pfd[0]);
    close(pfd[1]);
    printf("child: falls asleep\n");
    sleep(20);
    printf("child: unlock file\n");
    if (lockf(fd, F_ULOCK, 0) != 0) {
      perror("child: unlock");
      return 1;
    }
    close(fd);
    return 0;
  } else {
    int val;
    printf("parent[%d]: falls asleep\n", getpid());
    sleep(10);
    printf("parent[%d]: unlocks file\n", getpid());
    if (lockf(fd, F_ULOCK, 0) != 0) {
      perror("parent: unlock");
      return 1;
    }
    printf("parent[%d]: waits for child to lock file\n", getpid());
    close(pfd[1]);
    read(pfd[0], &val, sizeof(val));
    close(pfd[0]);
    printf("parent[%d]: waits for child to exit\n", getpid());
    wait(NULL);
    close(fd);
    close(lfd);
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x20(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11ea <main+0x3a>
lea    0xe27(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1561 <main+0x3b1>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
or     (%rax),%esi
lea    0xe0c(%rip),%rdi
mov    $0x180,%edx
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1249 <main+0x99>
lea    0xe03(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1561 <main+0x3b1>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
mov    0x2d88(%rip),%rax
or     (%rax),%esi
lea    0xde8(%rip),%rdi
mov    $0x180,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12a8 <main+0xf8>
lea    0xda4(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1561 <main+0x3b1>
mov    -0x18(%rbp),%edi
mov    0x2d26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <lockf@plt>
cmp    $0x0,%rax
je     12dd <main+0x12d>
lea    0xdb4(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1561 <main+0x3b1>
mov    -0x14(%rbp),%edi
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <lockf@plt>
cmp    $0x0,%rax
je     1312 <main+0x162>
lea    0xd7f(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1561 <main+0x3b1>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1340 <main+0x190>
lea    0xd57(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1561 <main+0x3b1>
cmpq   $0x0,-0x10(%rbp)
jne    1485 <main+0x2d5>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    0x2c5e(%rip),%rax
mov    (%rax),%esi
mov    0x2c8d(%rip),%rax
or     (%rax),%esi
lea    0xced(%rip),%rdi
mov    $0x180,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    13a3 <main+0x1f3>
lea    0xca9(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1561 <main+0x3b1>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xcd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2c14(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <lockf@plt>
cmp    $0x0,%rax
je     13ef <main+0x23f>
lea    0xcca(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1561 <main+0x3b1>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xcb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
lea    0xcc8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x14,%edi
call   10b0 <sleep@plt>
lea    0xcc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2b9a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <lockf@plt>
cmp    $0x0,%rax
je     1471 <main+0x2c1>
lea    0xcae(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1561 <main+0x3b1>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1561 <main+0x3b1>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xc87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xa,%edi
call   10b0 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xc80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2b19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <lockf@plt>
cmp    $0x0,%rax
je     14f2 <main+0x342>
lea    0xc6f(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1561 <main+0x3b1>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xc5d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xc4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0x4(%rsp),%rdi
call   1080 <pipe@plt>
test   %rax,%rax
js     143c <main+0x27c>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe17(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dc3(%rip),%r14
mov    0x2df4(%rip),%r12
mov    (%r12),%esi
or     (%r14),%esi
lea    0xe14(%rip),%rdi
mov    $0x180,%edx
call   10a0 <open@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe13(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r12),%esi
or     (%r14),%esi
lea    0xe1d(%rip),%rdi
mov    $0x180,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     1433 <main+0x273>
mov    %eax,%ebx
mov    0x2d79(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %r15d,%edi
xor    %edx,%edx
call   1060 <lockf@plt>
test   %rax,%rax
jne    142a <main+0x26a>
mov    0x0(%rbp),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1060 <lockf@plt>
test   %rax,%rax
jne    142a <main+0x26a>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
js     1445 <main+0x285>
je     135f <main+0x19f>
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe42(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe3b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1060 <lockf@plt>
test   %rax,%rax
jne    144e <main+0x28e>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe32(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    0x4(%rsp),%edi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1090 <read@plt>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe20(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   10d0 <wait@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %r15d,%edi
jmp    1423 <main+0x263>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %r15d,%edi
call   1070 <close@plt>
mov    (%r12),%esi
or     (%r14),%esi
lea    0xce5(%rip),%rdi
mov    $0x180,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     1433 <main+0x273>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xced(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x0(%rbp),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1060 <lockf@plt>
test   %rax,%rax
jne    1457 <main+0x297>
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xce7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
lea    0xd96(%rip),%rdi
call   1030 <puts@plt>
mov    $0x14,%edi
call   10c0 <sleep@plt>
lea    0xd94(%rip),%rdi
call   1030 <puts@plt>
mov    0x2bcf(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1060 <lockf@plt>
test   %rax,%rax
jne    1460 <main+0x2a0>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1472 <main+0x2b2>
lea    0xc4f(%rip),%rdi
jmp    1467 <main+0x2a7>
lea    0xc01(%rip),%rdi
jmp    1467 <main+0x2a7>
lea    0xbbd(%rip),%rdi
jmp    1467 <main+0x2a7>
lea    0xc3a(%rip),%rdi
jmp    1467 <main+0x2a7>
lea    0xcd2(%rip),%rdi
jmp    1467 <main+0x2a7>
lea    0xc4a(%rip),%rdi
jmp    1467 <main+0x2a7>
lea    0xc7e(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"pfd\00"
@.str.1 = [34 x i8] c"parent[%d]: tries to locks flock\0A\00"
@.str.2 = [21 x i8] c".lock_for_lock.flock\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [33 x i8] c"parent[%d]: tries to locks file\0A\00"
@.str.5 = [31 x i8] c".lock_for_single_process.flock\00"
@F_LOCK = global i32 0
@.str.6 = [6 x i8] c"lockf\00"
@.str.7 = [5 x i8] c"fork\00"
@.str.8 = [29 x i8] c"child[%d]: tries locks file\0A\00"
@.str.9 = [12 x i8] c"child: lock\00"
@.str.10 = [49 x i8] c"child[%d]: let parent know that child lock file\0A\00"
@.str.11 = [21 x i8] c"child: falls asleep\0A\00"
@.str.12 = [20 x i8] c"child: unlock file\0A\00"
@F_ULOCK = global i32 0
@.str.13 = [14 x i8] c"child: unlock\00"
@.str.14 = [26 x i8] c"parent[%d]: falls asleep\0A\00"
@.str.15 = [26 x i8] c"parent[%d]: unlocks file\0A\00"
@.str.16 = [15 x i8] c"parent: unlock\00"
@.str.17 = [42 x i8] c"parent[%d]: waits for child to lock file\0A\00"
@.str.18 = [37 x i8] c"parent[%d]: waits for child to exit\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2 x i32]
%6 = alloca i32
store i32 0, i32* %1
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %131
12: ; preds = %0
%13 = call i32 (...) @getpid()
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 384)
store i32 %18, i32* %4
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %131
23: ; preds = %12
%24 = call i32 (...) @getpid()
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = load i32, i32* @O_WRONLY
%27 = load i32, i32* @O_CREAT
%28 = or i32 %26, %27
%29 = call i32 @open(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %28, i32 384)
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %23
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %131
34: ; preds = %23
%35 = load i32, i32* %4
%36 = load i32, i32* @F_LOCK
%37 = call i64 @lockf(i32 %35, i32 %36, i32 0)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %131
41: ; preds = %34
%42 = load i32, i32* %3
%43 = load i32, i32* @F_LOCK
%44 = call i64 @lockf(i32 %42, i32 %43, i32 0)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %131
48: ; preds = %41
%49 = call i64 (...) @fork()
store i64 %49, i64* %2
%50 = load i64, i64* %2
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %1
br label %131
54: ; preds = %48
%55 = load i64, i64* %2
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %100
57: ; preds = %54
%58 = load i32, i32* %3
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %4
%61 = call i32 @close(i32 %60)
%62 = load i32, i32* @O_WRONLY
%63 = load i32, i32* @O_CREAT
%64 = or i32 %62, %63
%65 = call i32 @open(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %64, i32 384)
store i32 %65, i32* %3
%66 = load i32, i32* %3
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %57
%69 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %131
70: ; preds = %57
%71 = call i32 (...) @getpid()
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0), i32 %71)
%73 = load i32, i32* %3
%74 = load i32, i32* @F_LOCK
%75 = call i64 @lockf(i32 %73, i32 %74, i32 0)
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %70
%78 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %1
br label %131
79: ; preds = %70
%80 = call i32 (...) @getpid()
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.10, i64 0, i64 0), i32 %80)
%82 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%83 = load i32, i32* %82
%84 = call i32 @close(i32 %83)
%85 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%86 = load i32, i32* %85
%87 = call i32 @close(i32 %86)
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.11, i64 0, i64 0))
%89 = call i32 @sleep(i32 20)
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.12, i64 0, i64 0))
%91 = load i32, i32* %3
%92 = load i32, i32* @F_ULOCK
%93 = call i64 @lockf(i32 %91, i32 %92, i32 0)
%94 = icmp ne i64 %93, 0
br i1 %94, label %95, label %97
95: ; preds = %79
%96 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.13, i64 0, i64 0))
store i32 1, i32* %1
br label %131
97: ; preds = %79
%98 = load i32, i32* %3
%99 = call i32 @close(i32 %98)
store i32 0, i32* %1
br label %131
100: ; preds = %54
%101 = call i32 (...) @getpid()
%102 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.14, i64 0, i64 0), i32 %101)
%103 = call i32 @sleep(i32 10)
%104 = call i32 (...) @getpid()
%105 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.15, i64 0, i64 0), i32 %104)
%106 = load i32, i32* %3
%107 = load i32, i32* @F_ULOCK
%108 = call i64 @lockf(i32 %106, i32 %107, i32 0)
%109 = icmp ne i64 %108, 0
br i1 %109, label %110, label %112
110: ; preds = %100
%111 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.16, i64 0, i64 0))
store i32 1, i32* %1
br label %131
112: ; preds = %100
%113 = call i32 (...) @getpid()
%114 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.17, i64 0, i64 0), i32 %113)
%115 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%116 = load i32, i32* %115
%117 = call i32 @close(i32 %116)
%118 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%119 = load i32, i32* %118
%120 = call i32 @read(i32 %119, i32* %6, i32 4)
%121 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%122 = load i32, i32* %121
%123 = call i32 @close(i32 %122)
%124 = call i32 (...) @getpid()
%125 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.18, i64 0, i64 0), i32 %124)
%126 = call i32 @wait(i32* null)
%127 = load i32, i32* %3
%128 = call i32 @close(i32 %127)
%129 = load i32, i32* %4
%130 = call i32 @close(i32 %129)
store i32 0, i32* %1
br label %131
131: ; preds = %112, %110, %97, %95, %77, %68, %52, %46, %39, %32, %21, %10
%132 = load i32, i32* %1
ret i32 %132
}

LLVM IR (-O2):
@.str = [4 x i8] c"pfd\00"
@.str.1 = [34 x i8] c"parent[%d]: tries to locks flock\0A\00"
@.str.2 = [21 x i8] c".lock_for_lock.flock\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [33 x i8] c"parent[%d]: tries to locks file\0A\00"
@.str.5 = [31 x i8] c".lock_for_single_process.flock\00"
@F_LOCK = global i32 0
@.str.6 = [6 x i8] c"lockf\00"
@.str.7 = [5 x i8] c"fork\00"
@.str.8 = [29 x i8] c"child[%d]: tries locks file\0A\00"
@.str.9 = [12 x i8] c"child: lock\00"
@.str.10 = [49 x i8] c"child[%d]: let parent know that child lock file\0A\00"
@F_ULOCK = global i32 0
@.str.13 = [14 x i8] c"child: unlock\00"
@.str.14 = [26 x i8] c"parent[%d]: falls asleep\0A\00"
@.str.15 = [26 x i8] c"parent[%d]: unlocks file\0A\00"
@.str.16 = [15 x i8] c"parent: unlock\00"
@.str.17 = [42 x i8] c"parent[%d]: waits for child to lock file\0A\00"
@.str.18 = [37 x i8] c"parent[%d]: waits for child to exit\0A\00"
@str = [20 x i8] c"child: falls asleep\00"
@str.19 = [19 x i8] c"child: unlock file\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i32
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i64 @pipe(i32* %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %109
9: ; preds = %0
%10 = call i32 (...) @getpid()
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 384)
%16 = call i32 (...) @getpid()
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %16)
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %19, %18
%21 = call i32 @open(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %20, i32 384)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %9
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %109
25: ; preds = %9
%26 = load i32, i32* @F_LOCK
%27 = call i64 @lockf(i32 %15, i32 %26, i32 0)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %109
31: ; preds = %25
%32 = load i32, i32* @F_LOCK
%33 = call i64 @lockf(i32 %21, i32 %32, i32 0)
%34 = icmp eq i64 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %109
37: ; preds = %31
%38 = call i64 (...) @fork()
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
br label %109
42: ; preds = %37
%43 = icmp eq i64 %38, 0
br i1 %43, label %44, label %80
44: ; preds = %42
%45 = call i32 @close(i32 %21)
%46 = call i32 @close(i32 %15)
%47 = load i32, i32* @O_WRONLY
%48 = load i32, i32* @O_CREAT
%49 = or i32 %48, %47
%50 = call i32 @open(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %49, i32 384)
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %44
%53 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %109
54: ; preds = %44
%55 = call i32 (...) @getpid()
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0), i32 %55)
%57 = load i32, i32* @F_LOCK
%58 = call i64 @lockf(i32 %50, i32 %57, i32 0)
%59 = icmp eq i64 %58, 0
br i1 %59, label %62, label %60
60: ; preds = %54
%61 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0))
br label %109
62: ; preds = %54
%63 = call i32 (...) @getpid()
%64 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.10, i64 0, i64 0), i32 %63)
%65 = load i32, i32* %4
%66 = call i32 @close(i32 %65)
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%68 = load i32, i32* %67
%69 = call i32 @close(i32 %68)
%70 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%71 = call i32 @sleep(i32 20)
%72 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.19, i64 0, i64 0))
%73 = load i32, i32* @F_ULOCK
%74 = call i64 @lockf(i32 %50, i32 %73, i32 0)
%75 = icmp eq i64 %74, 0
br i1 %75, label %78, label %76
76: ; preds = %62
%77 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.13, i64 0, i64 0))
br label %109
78: ; preds = %62
%79 = call i32 @close(i32 %50)
br label %109
80: ; preds = %42
%81 = bitcast i32* %2 to i8*
%82 = call i32 (...) @getpid()
%83 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.14, i64 0, i64 0), i32 %82)
%84 = call i32 @sleep(i32 10)
%85 = call i32 (...) @getpid()
%86 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.15, i64 0, i64 0), i32 %85)
%87 = load i32, i32* @F_ULOCK
%88 = call i64 @lockf(i32 %21, i32 %87, i32 0)
%89 = icmp eq i64 %88, 0
br i1 %89, label %92, label %90
90: ; preds = %80
%91 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.16, i64 0, i64 0))
br label %107
92: ; preds = %80
%93 = call i32 (...) @getpid()
%94 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.17, i64 0, i64 0), i32 %93)
%95 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%96 = load i32, i32* %95
%97 = call i32 @close(i32 %96)
%98 = load i32, i32* %4
%99 = call i32 @read(i32 %98, i32* %2, i32 4)
%100 = load i32, i32* %4
%101 = call i32 @close(i32 %100)
%102 = call i32 (...) @getpid()
%103 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.18, i64 0, i64 0), i32 %102)
%104 = call i32 @wait(i32* null)
%105 = call i32 @close(i32 %21)
%106 = call i32 @close(i32 %15)
br label %107
107: ; preds = %92, %90
%108 = phi i32 [ 1, %90 ], [ 0, %92 ]
br label %109
109: ; preds = %107, %78, %76, %60, %52, %40, %35, %29, %23, %7
%110 = phi i32 [ 1, %7 ], [ 1, %23 ], [ 1, %29 ], [ 1, %35 ], [ 1, %40 ], [ 1, %52 ], [ 1, %60 ], [ 1, %76 ], [ 0, %78 ], [ %108, %107 ]
ret i32 %110
}