#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 flock {
  int l_start;
  int l_len;
  int /*<<< orphan*/ l_type;
  int /*<<< orphan*/ l_whence;
};
typedef int pid_t;

/* Variables and functions */
scalar_t__ EDEADLK;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int TEMP_FAILURE_RETRY(int);
int create_temp_file(char *, int /*<<< orphan*/ *);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sleep(int);
char *strerror(scalar_t__);
int waitpid(int, int *, int /*<<< orphan*/);
int do_test(void) {
  int fd = create_temp_file("tst-deadlk-", NULL);
  if (fd == -1) {
    puts("create_temp_file failed");
    return 1;
  }
  pid_t pid1, pid2;
  struct flock fl;
  fl.l_type = F_WRLCK;
  fl.l_whence = SEEK_SET;
  fl.l_start = 0;
  fl.l_len = 2;
  if (TEMP_FAILURE_RETRY(fcntl(fd, F_SETLK, &fl)) == -1) {
    perror("fcntl failed");
    return 1;
  }
  if ((pid1 = fork())) {
    if ((pid2 = fork())) {
      fl.l_type = F_UNLCK;
      fl.l_len = 2;
      sleep(2);
      printf("Parent %d is unlocking lock...\n", getpid());
      if (TEMP_FAILURE_RETRY(fcntl(fd, F_SETLKW, &fl)) == -1) {
        perror("fcntl failed");
        return 1;
      }
      int status1, status2;
      if (TEMP_FAILURE_RETRY(waitpid(pid1, &status1, 0)) != pid1) {
        perror("waitpid failed");
        return 1;
      }
      if (TEMP_FAILURE_RETRY(waitpid(pid2, &status2, 0)) != pid2) {
        perror("waitpid failed");
        return 1;
      }
      return status1 ? status1 : status2 ? status2 : 0;
    }
  }
  if (pid1 == -1 || pid2 == -1) {
    perror("fork failed");
    return 1;
  }
  if (!pid1 || !pid2) {
    printf("Child %d started\n", getpid());
    int offset0 = (pid1 ? 0 : 1);
    int offset1 = (pid1 ? 1 : 0);
    fl.l_len = 1;
    fl.l_start = offset0;
    printf("Child %d locking byte %d...\n", getpid(), offset0);
    if (TEMP_FAILURE_RETRY(fcntl(fd, F_SETLKW, &fl) == -1)) {
      printf("Child %d fcntl failed (%s)", getpid(), strerror(errno));
      return errno == EDEADLK ? 0 : 1;
    }
    sleep(1);
    printf("Child %d locking byte %d...\n", getpid(), offset1);
    fl.l_start = offset1;
    if (TEMP_FAILURE_RETRY(fcntl(fd, F_SETLKW, &fl) == -1)) {
      printf("Child %d fcntl failed (%s)\n", getpid(), strerror(errno));
      return errno == EDEADLK ? 0 : 1;
    }
    printf("Child %d terminating (and releasing its lock)\n", getpid());
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xe41(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <create_temp_file@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11ed <do_test+0x3d>
lea    0xe30(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1591 <do_test+0x3e1>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x2,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <fcntl@plt>
mov    %eax,%edi
call   10a0 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
jne    1250 <do_test+0xa0>
lea    0xde5(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1591 <do_test+0x3e1>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     139d <do_test+0x1ed>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
je     1398 <do_test+0x1e8>
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0x2,-0x1c(%rbp)
mov    $0x2,%edi
call   10c0 <sleep@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <fcntl@plt>
mov    %eax,%edi
call   10a0 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
jne    12e7 <do_test+0x137>
lea    0xd4e(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1591 <do_test+0x3e1>
mov    -0xc(%rbp),%edi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    %eax,%edi
call   10a0 <TEMP_FAILURE_RETRY@plt>
cmp    -0xc(%rbp),%eax
je     131d <do_test+0x16d>
lea    0xd45(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1591 <do_test+0x3e1>
mov    -0x10(%rbp),%edi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    %eax,%edi
call   10a0 <TEMP_FAILURE_RETRY@plt>
cmp    -0x10(%rbp),%eax
je     1353 <do_test+0x1a3>
lea    0xd0f(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1591 <do_test+0x3e1>
cmpl   $0x0,-0x24(%rbp)
je     1368 <do_test+0x1b8>
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    138d <do_test+0x1dd>
cmpl   $0x0,-0x28(%rbp)
je     137d <do_test+0x1cd>
mov    -0x28(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    1387 <do_test+0x1d7>
xor    %eax,%eax
mov    %eax,-0x38(%rbp)
jmp    1387 <do_test+0x1d7>
mov    -0x38(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1591 <do_test+0x3e1>
jmp    139d <do_test+0x1ed>
cmpl   $0xffffffff,-0xc(%rbp)
je     13b1 <do_test+0x201>
cmpl   $0xffffffff,-0x10(%rbp)
jne    13c9 <do_test+0x219>
lea    0xca8(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1591 <do_test+0x3e1>
cmpl   $0x0,-0xc(%rbp)
je     13dd <do_test+0x22d>
cmpl   $0x0,-0x10(%rbp)
jne    158a <do_test+0x3da>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xc7f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xc(%rbp),%edx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x30(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
mov    -0x2c(%rbp),%edx
lea    0xc44(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2b9d(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   10a0 <TEMP_FAILURE_RETRY@plt>
cmp    $0x0,%eax
je     14c5 <do_test+0x315>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2b40(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x3c(%rbp),%esi
mov    %rax,%rdx
lea    0xc06(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2b1d(%rip),%rax
mov    (%rax),%rdx
mov    0x2b23(%rip),%rax
mov    (%rax),%rsi
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1591 <do_test+0x3e1>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
mov    -0x30(%rbp),%edx
lea    0xb9c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2aef(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   10a0 <TEMP_FAILURE_RETRY@plt>
cmp    $0x0,%eax
je     1573 <do_test+0x3c3>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x40(%rbp)
mov    0x2a92(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x40(%rbp),%esi
mov    %rax,%rdx
lea    0xb73(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2a6f(%rip),%rax
mov    (%rax),%rdx
mov    0x2a75(%rip),%rax
mov    (%rax),%rsi
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1591 <do_test+0x3e1>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xb4f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xe3f(%rip),%rdi
xor    %esi,%esi
call   1070 <create_temp_file@plt>
cmp    $0xffffffff,%eax
je     12ee <do_test+0x13e>
mov    %eax,%r15d
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
movabs $0x200000000,%rax
mov    %rax,(%rsp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r15d,%edi
call   1040 <fcntl@plt>
mov    %eax,%edi
call   10a0 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
je     1449 <do_test+0x299>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
je     130d <do_test+0x15d>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     1304 <do_test+0x154>
mov    %eax,%r14d
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
movl   $0x2,0x4(%rsp)
mov    $0x2,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xdc1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r15d,%edi
call   1040 <fcntl@plt>
mov    %eax,%edi
call   10a0 <TEMP_FAILURE_RETRY@plt>
cmp    $0xffffffff,%eax
je     1449 <do_test+0x299>
lea    0x14(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    %eax,%edi
call   10a0 <TEMP_FAILURE_RETRY@plt>
cmp    %ebp,%eax
jne    1465 <do_test+0x2b5>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    %eax,%edi
call   10a0 <TEMP_FAILURE_RETRY@plt>
cmp    %r14d,%eax
jne    1465 <do_test+0x2b5>
mov    0x14(%rsp),%ebp
test   %ebp,%ebp
jne    143c <do_test+0x28c>
mov    0x10(%rsp),%ebp
jmp    143c <do_test+0x28c>
lea    0xd17(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
jmp    143c <do_test+0x28c>
cmp    $0xffffffff,%ebp
je     146e <do_test+0x2be>
xor    %r14d,%r14d
xor    %ebx,%ebx
test   %ebp,%ebp
setne  %bpl
sete   %bl
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd43(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
movl   $0x1,0x4(%rsp)
mov    %ebx,(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xd33(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2c8b(%rip),%rbx
mov    (%rbx),%esi
mov    %rsp,%rdx
mov    %r15d,%edi
call   1040 <fcntl@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
call   10a0 <TEMP_FAILURE_RETRY@plt>
test   %eax,%eax
je     13ae <do_test+0x1fe>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebx
mov    0x2c34(%rip),%rbp
mov    0x0(%rbp),%rdi
call   10b0 <strerror@plt>
lea    0xcff(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x0(%rbp),%rax
jmp    142c <do_test+0x27c>
mov    %bpl,%r14b
mov    $0x1,%edi
call   10c0 <sleep@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xcb3(%rip),%rdi
mov    %eax,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14d,(%rsp)
mov    (%rbx),%esi
mov    %rsp,%rdx
mov    %r15d,%edi
call   1040 <fcntl@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
call   10a0 <TEMP_FAILURE_RETRY@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%r14d
test   %ebx,%ebx
je     1452 <do_test+0x2a2>
mov    0x2bb3(%rip),%rbx
mov    (%rbx),%rdi
call   10b0 <strerror@plt>
lea    0xc9a(%rip),%rdi
mov    %r14d,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rbx),%rax
mov    0x2b9d(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%rax
setne  %bpl
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xbd4(%rip),%rdi
jmp    1475 <do_test+0x2c5>
lea    0xc79(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    143c <do_test+0x28c>
lea    0xbe5(%rip),%rdi
jmp    1475 <do_test+0x2c5>
lea    0xbeb(%rip),%rdi
call   1090 <perror@plt>
jmp    12fa <do_test+0x14a>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i32 }
@.str = [12 x i8] c"tst-deadlk-\00"
@.str.1 = [24 x i8] c"create_temp_file failed\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str.2 = [13 x i8] c"fcntl failed\00"
@F_UNLCK = global i32 0
@.str.3 = [32 x i8] c"Parent %d is unlocking lock...\0A\00"
@F_SETLKW = global i32 0
@.str.4 = [15 x i8] c"waitpid failed\00"
@.str.5 = [12 x i8] c"fork failed\00"
@.str.6 = [18 x i8] c"Child %d started\0A\00"
@.str.7 = [29 x i8] c"Child %d locking byte %d...\0A\00"
@.str.8 = [27 x i8] c"Child %d fcntl failed (%s)\00"
@errno = global i64 0
@EDEADLK = global i64 0
@.str.9 = [28 x i8] c"Child %d fcntl failed (%s)\0A\00"
@.str.10 = [47 x i8] c"Child %d terminating (and releasing its lock)\0A\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.flock
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = call i32 @create_temp_file(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32* null)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %157
15: ; preds = %0
%16 = load i32, i32* @F_WRLCK
%17 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @SEEK_SET
%19 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 3
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i32 0, i32* %20
%21 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
store i32 2, i32* %21
%22 = load i32, i32* %2
%23 = load i32, i32* @F_SETLK
%24 = call i32 @fcntl(i32 %22, i32 %23, %struct.flock* %5)
%25 = call i32 @TEMP_FAILURE_RETRY(i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %15
%28 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %157
29: ; preds = %15
%30 = call i32 (...) @fork()
store i32 %30, i32* %3
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %81
32: ; preds = %29
%33 = call i32 (...) @fork()
store i32 %33, i32* %4
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %80
35: ; preds = %32
%36 = load i32, i32* @F_UNLCK
%37 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 2
store i32 %36, i32* %37
%38 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
store i32 2, i32* %38
%39 = call i32 @sleep(i32 2)
%40 = call i32 (...) @getpid()
%41 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %2
%43 = load i32, i32* @F_SETLKW
%44 = call i32 @fcntl(i32 %42, i32 %43, %struct.flock* %5)
%45 = call i32 @TEMP_FAILURE_RETRY(i32 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %35
%48 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %157
49: ; preds = %35
%50 = load i32, i32* %3
%51 = call i32 @waitpid(i32 %50, i32* %6, i32 0)
%52 = call i32 @TEMP_FAILURE_RETRY(i32 %51)
%53 = load i32, i32* %3
%54 = icmp ne i32 %52, %53
br i1 %54, label %55, label %57
55: ; preds = %49
%56 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %157
57: ; preds = %49
%58 = load i32, i32* %4
%59 = call i32 @waitpid(i32 %58, i32* %7, i32 0)
%60 = call i32 @TEMP_FAILURE_RETRY(i32 %59)
%61 = load i32, i32* %4
%62 = icmp ne i32 %60, %61
br i1 %62, label %63, label %65
63: ; preds = %57
%64 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %157
65: ; preds = %57
%66 = load i32, i32* %6
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %65
%69 = load i32, i32* %6
br label %78
70: ; preds = %65
%71 = load i32, i32* %7
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %70
%74 = load i32, i32* %7
br label %76
75: ; preds = %70
br label %76
76: ; preds = %75, %73
%77 = phi i32 [ %74, %73 ], [ 0, %75 ]
br label %78
78: ; preds = %76, %68
%79 = phi i32 [ %69, %68 ], [ %77, %76 ]
store i32 %79, i32* %1
br label %157
80: ; preds = %32
br label %81
81: ; preds = %80, %29
%82 = load i32, i32* %3
%83 = icmp eq i32 %82, -1
br i1 %83, label %87, label %84
84: ; preds = %81
%85 = load i32, i32* %4
%86 = icmp eq i32 %85, -1
br i1 %86, label %87, label %89
87: ; preds = %84, %81
%88 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %157
89: ; preds = %84
%90 = load i32, i32* %3
%91 = icmp ne i32 %90, 0
br i1 %91, label %92, label %95
92: ; preds = %89
%93 = load i32, i32* %4
%94 = icmp ne i32 %93, 0
br i1 %94, label %156, label %95
95: ; preds = %92, %89
%96 = call i32 (...) @getpid()
%97 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %96)
%98 = load i32, i32* %3
%99 = icmp ne i32 %98, 0
%100 = zext i1 %99 to i64
%101 = select i1 %99, i32 0, i32 1
store i32 %101, i32* %8
%102 = load i32, i32* %3
%103 = icmp ne i32 %102, 0
%104 = zext i1 %103 to i64
%105 = select i1 %103, i32 1, i32 0
store i32 %105, i32* %9
%106 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
store i32 1, i32* %106
%107 = load i32, i32* %8
%108 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i32 %107, i32* %108
%109 = call i32 (...) @getpid()
%110 = load i32, i32* %8
%111 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %109, i32 %110)
%112 = load i32, i32* %2
%113 = load i32, i32* @F_SETLKW
%114 = call i32 @fcntl(i32 %112, i32 %113, %struct.flock* %5)
%115 = icmp eq i32 %114, -1
%116 = zext i1 %115 to i32
%117 = call i32 @TEMP_FAILURE_RETRY(i32 %116)
%118 = icmp ne i32 %117, 0
br i1 %118, label %119, label %129
119: ; preds = %95
%120 = call i32 (...) @getpid()
%121 = load i64, i64* @errno
%122 = call i8* @strerror(i64 %121)
%123 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i32 %120, i8* %122)
%124 = load i64, i64* @errno
%125 = load i64, i64* @EDEADLK
%126 = icmp eq i64 %124, %125
%127 = zext i1 %126 to i64
%128 = select i1 %126, i32 0, i32 1
store i32 %128, i32* %1
br label %157
129: ; preds = %95
%130 = call i32 @sleep(i32 1)
%131 = call i32 (...) @getpid()
%132 = load i32, i32* %9
%133 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %131, i32 %132)
%134 = load i32, i32* %9
%135 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i32 %134, i32* %135
%136 = load i32, i32* %2
%137 = load i32, i32* @F_SETLKW
%138 = call i32 @fcntl(i32 %136, i32 %137, %struct.flock* %5)
%139 = icmp eq i32 %138, -1
%140 = zext i1 %139 to i32
%141 = call i32 @TEMP_FAILURE_RETRY(i32 %140)
%142 = icmp ne i32 %141, 0
br i1 %142, label %143, label %153
143: ; preds = %129
%144 = call i32 (...) @getpid()
%145 = load i64, i64* @errno
%146 = call i8* @strerror(i64 %145)
%147 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i32 %144, i8* %146)
%148 = load i64, i64* @errno
%149 = load i64, i64* @EDEADLK
%150 = icmp eq i64 %148, %149
%151 = zext i1 %150 to i64
%152 = select i1 %150, i32 0, i32 1
store i32 %152, i32* %1
br label %157
153: ; preds = %129
%154 = call i32 (...) @getpid()
%155 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.10, i64 0, i64 0), i32 %154)
br label %156
156: ; preds = %153, %92
store i32 0, i32* %1
br label %157
157: ; preds = %156, %143, %119, %87, %78, %63, %55, %47, %27, %13
%158 = load i32, i32* %1
ret i32 %158
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i32 }
@.str = [12 x i8] c"tst-deadlk-\00"
@.str.1 = [24 x i8] c"create_temp_file failed\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str.2 = [13 x i8] c"fcntl failed\00"
@F_UNLCK = global i32 0
@.str.3 = [32 x i8] c"Parent %d is unlocking lock...\0A\00"
@F_SETLKW = global i32 0
@.str.4 = [15 x i8] c"waitpid failed\00"
@.str.5 = [12 x i8] c"fork failed\00"
@.str.6 = [18 x i8] c"Child %d started\0A\00"
@.str.7 = [29 x i8] c"Child %d locking byte %d...\0A\00"
@.str.8 = [27 x i8] c"Child %d fcntl failed (%s)\00"
@errno = global i64 0
@EDEADLK = global i64 0
@.str.9 = [28 x i8] c"Child %d fcntl failed (%s)\0A\00"
@.str.10 = [47 x i8] c"Child %d terminating (and releasing its lock)\0A\00"
define i32 @do_test() {
%1 = alloca %struct.flock
%2 = alloca i32
%3 = alloca i32
%4 = tail call i32 @create_temp_file(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32* null)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %110
8: ; preds = %0
%9 = bitcast %struct.flock* %1 to i8*
%10 = load i32, i32* @F_WRLCK
%11 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* @SEEK_SET
%13 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 3
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 0
store i32 0, i32* %14
%15 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 1
store i32 2, i32* %15
%16 = load i32, i32* @F_SETLK
%17 = call i32 @fcntl(i32 %4, i32 %16, %struct.flock* %1)
%18 = call i32 @TEMP_FAILURE_RETRY(i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %8
%21 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %108
22: ; preds = %8
%23 = call i32 (...) @fork()
%24 = icmp eq i32 %23, 0
br i1 %24, label %64, label %25
25: ; preds = %22
%26 = call i32 (...) @fork()
%27 = icmp eq i32 %26, 0
br i1 %27, label %60, label %28
28: ; preds = %25
%29 = load i32, i32* @F_UNLCK
store i32 %29, i32* %11
store i32 2, i32* %15
%30 = call i32 @sleep(i32 2)
%31 = call i32 (...) @getpid()
%32 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @F_SETLKW
%34 = call i32 @fcntl(i32 %4, i32 %33, %struct.flock* %1)
%35 = call i32 @TEMP_FAILURE_RETRY(i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %28
%38 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %108
39: ; preds = %28
%40 = bitcast i32* %2 to i8*
%41 = bitcast i32* %3 to i8*
%42 = call i32 @waitpid(i32 %23, i32* %2, i32 0)
%43 = call i32 @TEMP_FAILURE_RETRY(i32 %42)
%44 = icmp eq i32 %43, %23
br i1 %44, label %47, label %45
45: ; preds = %39
%46 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %58
47: ; preds = %39
%48 = call i32 @waitpid(i32 %26, i32* %3, i32 0)
%49 = call i32 @TEMP_FAILURE_RETRY(i32 %48)
%50 = icmp eq i32 %49, %26
br i1 %50, label %53, label %51
51: ; preds = %47
%52 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %58
53: ; preds = %47
%54 = load i32, i32* %2
%55 = icmp eq i32 %54, 0
%56 = load i32, i32* %3
%57 = select i1 %55, i32 %56, i32 %54
br label %58
58: ; preds = %53, %51, %45
%59 = phi i32 [ 1, %45 ], [ 1, %51 ], [ %57, %53 ]
br label %108
60: ; preds = %25
%61 = icmp eq i32 %23, -1
br i1 %61, label %62, label %64
62: ; preds = %60
%63 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %108
64: ; preds = %22, %60
%65 = call i32 (...) @getpid()
%66 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %65)
%67 = zext i1 %24 to i32
%68 = xor i1 %24, true
%69 = zext i1 %68 to i32
store i32 1, i32* %15
store i32 %67, i32* %14
%70 = call i32 (...) @getpid()
%71 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %70, i32 %67)
%72 = load i32, i32* @F_SETLKW
%73 = call i32 @fcntl(i32 %4, i32 %72, %struct.flock* %1)
%74 = icmp eq i32 %73, -1
%75 = zext i1 %74 to i32
%76 = call i32 @TEMP_FAILURE_RETRY(i32 %75)
%77 = icmp eq i32 %76, 0
br i1 %77, label %87, label %78
78: ; preds = %64
%79 = call i32 (...) @getpid()
%80 = load i64, i64* @errno
%81 = call i8* @strerror(i64 %80)
%82 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i32 %79, i8* %81)
%83 = load i64, i64* @errno
%84 = load i64, i64* @EDEADLK
%85 = icmp ne i64 %83, %84
%86 = zext i1 %85 to i32
br label %108
87: ; preds = %64
%88 = call i32 @sleep(i32 1)
%89 = call i32 (...) @getpid()
%90 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %89, i32 %69)
store i32 %69, i32* %14
%91 = load i32, i32* @F_SETLKW
%92 = call i32 @fcntl(i32 %4, i32 %91, %struct.flock* %1)
%93 = icmp eq i32 %92, -1
%94 = zext i1 %93 to i32
%95 = call i32 @TEMP_FAILURE_RETRY(i32 %94)
%96 = icmp eq i32 %95, 0
%97 = call i32 (...) @getpid()
br i1 %96, label %106, label %98
98: ; preds = %87
%99 = load i64, i64* @errno
%100 = call i8* @strerror(i64 %99)
%101 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i32 %97, i8* %100)
%102 = load i64, i64* @errno
%103 = load i64, i64* @EDEADLK
%104 = icmp ne i64 %102, %103
%105 = zext i1 %104 to i32
br label %108
106: ; preds = %87
%107 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.10, i64 0, i64 0), i32 %97)
br label %108
108: ; preds = %78, %98, %106, %62, %58, %37, %20
%109 = phi i32 [ 1, %20 ], [ 1, %37 ], [ %59, %58 ], [ 1, %62 ], [ 0, %106 ], [ %86, %78 ], [ %105, %98 ]
br label %110
110: ; preds = %108, %6
%111 = phi i32 [ 1, %6 ], [ %109, %108 ]
ret i32 %111
}