#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int LOCK_EX;
int LOCK_NB;
int LOCK_UN;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int flock(int, int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int t3(int argc, char *argv[]) {
  int fd, fd2;
  int pid;
  int rc = EXIT_SUCCESS;
  if (argc != 3) {
    fprintf(stderr, "Usage: ./flocks_test 3 filename\n");
    return EXIT_FAILURE;
  }
  if ((fd = open(argv[2], O_RDWR)) < 0) {
    fprintf(stderr, "Couldn't open file: %s\n", argv[1]);
    return EXIT_FAILURE;
  }
  if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
    perror("first flock failed");
    rc = EXIT_FAILURE;
    goto out;
  }
  if ((fd2 = open(argv[2], O_RDWR)) < 0) {
    fprintf(stderr, "Couldn't open file: %s\n", argv[1]);
    rc = EXIT_FAILURE;
    goto out;
  }
  if (flock(fd2, LOCK_EX | LOCK_NB) >= 0) {
    fprintf(stderr, "Second flock succeeded - FAIL\n");
    rc = EXIT_FAILURE;
    close(fd2);
    goto out;
  }
  close(fd2);
  pid = fork();
  if (pid == -1) {
    perror("fork");
    rc = EXIT_FAILURE;
    goto out;
  }
  if (pid == 0) {
    if ((fd2 = open(argv[2], O_RDWR)) < 0) {
      fprintf(stderr, "Couldn't open file: %s\n", argv[1]);
      rc = EXIT_FAILURE;
      exit(rc);
    }
    if (flock(fd2, LOCK_EX | LOCK_NB) >= 0) {
      fprintf(stderr, "Second flock succeeded - FAIL\n");
      rc = EXIT_FAILURE;
      goto out_child;
    }
    if (flock(fd, LOCK_UN) == -1) {
      fprintf(stderr, "Child unlock on parent fd failed\n");
      rc = EXIT_FAILURE;
      goto out_child;
    }
    if (flock(fd2, LOCK_EX | LOCK_NB) == -1) {
      fprintf(stderr, "Relock after parent unlock failed!\n");
      rc = EXIT_FAILURE;
      goto out_child;
    }
  out_child:
    close(fd2);
    exit(rc);
  }
  waitpid(pid, &rc, 0);
out:
  close(fd);
  return rc;
}


x86 assembly (clang, -O0):
<t3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11cd <t3+0x4d>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    14a5 <t3+0x325>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    121f <t3+0x9f>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    14a5 <t3+0x325>
mov    -0x14(%rbp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
mov    0x2d76(%rip),%rax
or     (%rax),%esi
call   1080 <flock@plt>
cmp    $0x0,%eax
jge    125f <t3+0xdf>
lea    0xdf0(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    1497 <t3+0x317>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12b1 <t3+0x131>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd88(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    1497 <t3+0x317>
mov    -0x18(%rbp),%edi
mov    0x2d25(%rip),%rax
mov    (%rax),%esi
mov    0x2ce4(%rip),%rax
or     (%rax),%esi
call   1080 <flock@plt>
cmp    $0x0,%eax
jl     1304 <t3+0x184>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
lea    0xd68(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2cd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
jmp    1497 <t3+0x317>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    133d <t3+0x1bd>
lea    0xd44(%rip),%rdi
call   1070 <perror@plt>
mov    0x2c95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    1497 <t3+0x317>
cmpl   $0x0,-0x1c(%rbp)
jne    1489 <t3+0x309>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2c92(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    139c <t3+0x21c>
mov    0x2c60(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xca0(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2c39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2c3a(%rip),%rax
mov    (%rax),%esi
mov    0x2bf9(%rip),%rax
or     (%rax),%esi
call   1080 <flock@plt>
cmp    $0x0,%eax
jl     13e7 <t3+0x267>
mov    0x2c0a(%rip),%rax
mov    (%rax),%edi
lea    0xc7d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2beb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    1479 <t3+0x2f9>
mov    -0x14(%rbp),%edi
mov    0x2bc7(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
cmp    $0xffffffff,%eax
jne    1429 <t3+0x2a9>
mov    0x2bc8(%rip),%rax
mov    (%rax),%edi
lea    0xc5f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2ba9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    1479 <t3+0x2f9>
mov    -0x18(%rbp),%edi
mov    0x2bad(%rip),%rax
mov    (%rax),%esi
mov    0x2b6c(%rip),%rax
or     (%rax),%esi
call   1080 <flock@plt>
cmp    $0xffffffff,%eax
jne    1474 <t3+0x2f4>
mov    0x2b7d(%rip),%rax
mov    (%rax),%edi
lea    0xc36(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2b5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    1479 <t3+0x2f9>
jmp    1479 <t3+0x2f9>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<t3>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e2e(%rip),%rax
mov    (%rax),%r12d
mov    %r12d,0x4(%rsp)
cmp    $0x3,%edi
jne    124a <t3+0xca>
mov    %rsi,%rbx
mov    0x10(%rsi),%rdi
mov    0x2e37(%rip),%r13
mov    0x0(%r13),%esi
call   1060 <open@plt>
test   %eax,%eax
js     126d <t3+0xed>
mov    %eax,%r14d
mov    0x2e14(%rip),%rax
mov    0x2dd5(%rip),%r15
mov    (%r15),%esi
or     (%rax),%esi
mov    %r14d,%edi
call   1080 <flock@plt>
test   %eax,%eax
js     12fe <t3+0x17e>
mov    0x10(%rbx),%rdi
mov    0x0(%r13),%esi
call   1060 <open@plt>
test   %eax,%eax
js     1291 <t3+0x111>
mov    %eax,%ebp
mov    (%r15),%esi
mov    0x2dd7(%rip),%rax
or     (%rax),%esi
mov    %ebp,%edi
call   1080 <flock@plt>
test   %eax,%eax
js     12bb <t3+0x13b>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
call   1030 <close@plt>
jmp    12e0 <t3+0x160>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xda6(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2d60(%rip),%rax
jmp    12ed <t3+0x16d>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xda0(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2d39(%rip),%rax
jmp    12ed <t3+0x16d>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xd7c(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2d15(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
jmp    12e0 <t3+0x160>
mov    %ebp,%edi
call   1030 <close@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     130c <t3+0x18c>
test   %eax,%eax
je     1315 <t3+0x195>
lea    0x4(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %r14d,%edi
call   1030 <close@plt>
lea    0x4(%rsp),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd34(%rip),%rdi
call   1070 <perror@plt>
jmp    12ac <t3+0x12c>
lea    0xd58(%rip),%rdi
jmp    1305 <t3+0x185>
mov    0x10(%rbx),%rdi
mov    0x0(%r13),%esi
call   1060 <open@plt>
test   %eax,%eax
jns    134f <t3+0x1cf>
mov    0x2ca3(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xce7(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2c80(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    %eax,%ebp
mov    (%r15),%esi
mov    0x2c85(%rip),%rax
or     (%rax),%esi
mov    %ebp,%edi
call   1080 <flock@plt>
test   %eax,%eax
js     137a <t3+0x1fa>
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
lea    0xcd4(%rip),%rsi
jmp    13ca <t3+0x24a>
mov    0x2c37(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1080 <flock@plt>
cmp    $0xffffffff,%eax
jne    13a2 <t3+0x222>
mov    0x2c39(%rip),%rax
mov    (%rax),%edi
lea    0xcd0(%rip),%rsi
jmp    13ca <t3+0x24a>
mov    (%r15),%esi
mov    0x2c34(%rip),%rax
or     (%rax),%esi
mov    %ebp,%edi
call   1080 <flock@plt>
cmp    $0xffffffff,%eax
jne    13e0 <t3+0x260>
mov    0x2c0f(%rip),%rax
mov    (%rax),%edi
lea    0xcc8(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2bf0(%rip),%rax
mov    (%rax),%r12d
mov    %r12d,0x4(%rsp)
mov    %ebp,%edi
call   1030 <close@plt>
mov    %r12d,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"Usage: ./flocks_test 3 filename\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@.str.1 = [24 x i8] c"Couldn't open file: %s\0A\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.2 = [19 x i8] c"first flock failed\00"
@.str.3 = [31 x i8] c"Second flock succeeded - FAIL\0A\00"
@.str.4 = [5 x i8] c"fork\00"
@LOCK_UN = global i32 0
@.str.5 = [34 x i8] c"Child unlock on parent fd failed\0A\00"
@.str.6 = [36 x i8] c"Relock after parent unlock failed!\0A\00"
define i32 @t3(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @EXIT_SUCCESS
store i32 %10, i32* %9
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
store i32 %16, i32* %3
br label %140
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 2
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
store i32 %30, i32* %3
br label %140
31: ; preds = %17
%32 = load i32, i32* %6
%33 = load i32, i32* @LOCK_EX
%34 = load i32, i32* @LOCK_NB
%35 = or i32 %33, %34
%36 = call i32 @flock(i32 %32, i32 %35)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
store i32 %40, i32* %9
br label %136
41: ; preds = %31
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 2
%44 = load i8*, i8** %43
%45 = load i32, i32* @O_RDWR
%46 = call i32 @open(i8* %44, i32 %45)
store i32 %46, i32* %7
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %41
%49 = load i32, i32* @stderr
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 1
%52 = load i8*, i8** %51
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %52)
%54 = load i32, i32* @EXIT_FAILURE
store i32 %54, i32* %9
br label %136
55: ; preds = %41
%56 = load i32, i32* %7
%57 = load i32, i32* @LOCK_EX
%58 = load i32, i32* @LOCK_NB
%59 = or i32 %57, %58
%60 = call i32 @flock(i32 %56, i32 %59)
%61 = icmp sge i32 %60, 0
br i1 %61, label %62, label %68
62: ; preds = %55
%63 = load i32, i32* @stderr
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
store i32 %65, i32* %9
%66 = load i32, i32* %7
%67 = call i32 @close(i32 %66)
br label %136
68: ; preds = %55
%69 = load i32, i32* %7
%70 = call i32 @close(i32 %69)
%71 = call i32 (...) @fork()
store i32 %71, i32* %8
%72 = load i32, i32* %8
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %77
74: ; preds = %68
%75 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%76 = load i32, i32* @EXIT_FAILURE
store i32 %76, i32* %9
br label %136
77: ; preds = %68
%78 = load i32, i32* %8
%79 = icmp eq i32 %78, 0
br i1 %79, label %80, label %133
80: ; preds = %77
%81 = load i8**, i8*** %5
%82 = getelementptr inbounds i8*, i8** %81, i64 2
%83 = load i8*, i8** %82
%84 = load i32, i32* @O_RDWR
%85 = call i32 @open(i8* %83, i32 %84)
store i32 %85, i32* %7
%86 = icmp slt i32 %85, 0
br i1 %86, label %87, label %96
87: ; preds = %80
%88 = load i32, i32* @stderr
%89 = load i8**, i8*** %5
%90 = getelementptr inbounds i8*, i8** %89, i64 1
%91 = load i8*, i8** %90
%92 = call i32 (i32, i8*, ...) @fprintf(i32 %88, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %91)
%93 = load i32, i32* @EXIT_FAILURE
store i32 %93, i32* %9
%94 = load i32, i32* %9
%95 = call i32 @exit(i32 %94)
unreachable
96: ; preds = %80
%97 = load i32, i32* %7
%98 = load i32, i32* @LOCK_EX
%99 = load i32, i32* @LOCK_NB
%100 = or i32 %98, %99
%101 = call i32 @flock(i32 %97, i32 %100)
%102 = icmp sge i32 %101, 0
br i1 %102, label %103, label %107
103: ; preds = %96
%104 = load i32, i32* @stderr
%105 = call i32 (i32, i8*, ...) @fprintf(i32 %104, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%106 = load i32, i32* @EXIT_FAILURE
store i32 %106, i32* %9
br label %128
107: ; preds = %96
%108 = load i32, i32* %6
%109 = load i32, i32* @LOCK_UN
%110 = call i32 @flock(i32 %108, i32 %109)
%111 = icmp eq i32 %110, -1
br i1 %111, label %112, label %116
112: ; preds = %107
%113 = load i32, i32* @stderr
%114 = call i32 (i32, i8*, ...) @fprintf(i32 %113, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
%115 = load i32, i32* @EXIT_FAILURE
store i32 %115, i32* %9
br label %128
116: ; preds = %107
%117 = load i32, i32* %7
%118 = load i32, i32* @LOCK_EX
%119 = load i32, i32* @LOCK_NB
%120 = or i32 %118, %119
%121 = call i32 @flock(i32 %117, i32 %120)
%122 = icmp eq i32 %121, -1
br i1 %122, label %123, label %127
123: ; preds = %116
%124 = load i32, i32* @stderr
%125 = call i32 (i32, i8*, ...) @fprintf(i32 %124, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
%126 = load i32, i32* @EXIT_FAILURE
store i32 %126, i32* %9
br label %128
127: ; preds = %116
br label %128
128: ; preds = %127, %123, %112, %103
%129 = load i32, i32* %7
%130 = call i32 @close(i32 %129)
%131 = load i32, i32* %9
%132 = call i32 @exit(i32 %131)
unreachable
133: ; preds = %77
%134 = load i32, i32* %8
%135 = call i32 @waitpid(i32 %134, i32* %9, i32 0)
br label %136
136: ; preds = %133, %74, %62, %48, %38
%137 = load i32, i32* %6
%138 = call i32 @close(i32 %137)
%139 = load i32, i32* %9
store i32 %139, i32* %3
br label %140
140: ; preds = %136, %24, %13
%141 = load i32, i32* %3
ret i32 %141
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"Usage: ./flocks_test 3 filename\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDWR = global i32 0
@.str.1 = [24 x i8] c"Couldn't open file: %s\0A\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.2 = [19 x i8] c"first flock failed\00"
@.str.3 = [31 x i8] c"Second flock succeeded - FAIL\0A\00"
@.str.4 = [5 x i8] c"fork\00"
@LOCK_UN = global i32 0
@.str.5 = [34 x i8] c"Child unlock on parent fd failed\0A\00"
@.str.6 = [36 x i8] c"Relock after parent unlock failed!\0A\00"
define i32 @t3(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @EXIT_SUCCESS
store i32 %5, i32* %3
%6 = icmp eq i32 %0, 3
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %106
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %106
21: ; preds = %10
%22 = load i32, i32* @LOCK_EX
%23 = load i32, i32* @LOCK_NB
%24 = or i32 %23, %22
%25 = tail call i32 @flock(i32 %14, i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
store i32 %29, i32* %3
br label %104
30: ; preds = %21
%31 = load i8*, i8** %11
%32 = load i32, i32* @O_RDWR
%33 = tail call i32 @open(i8* %31, i32 %32)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = getelementptr inbounds i8*, i8** %1, i64 1
%38 = load i8*, i8** %37
%39 = tail call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %38)
%40 = load i32, i32* @EXIT_FAILURE
store i32 %40, i32* %3
br label %104
41: ; preds = %30
%42 = load i32, i32* @LOCK_EX
%43 = load i32, i32* @LOCK_NB
%44 = or i32 %43, %42
%45 = tail call i32 @flock(i32 %33, i32 %44)
%46 = icmp sgt i32 %45, -1
br i1 %46, label %47, label %52
47: ; preds = %41
%48 = load i32, i32* @stderr
%49 = tail call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
store i32 %50, i32* %3
%51 = tail call i32 @close(i32 %33)
br label %104
52: ; preds = %41
%53 = tail call i32 @close(i32 %33)
%54 = tail call i32 (...) @fork()
switch i32 %54, label %102 [
i32 -1, label %55
i32 0, label %58
]
55: ; preds = %52
%56 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%57 = load i32, i32* @EXIT_FAILURE
store i32 %57, i32* %3
br label %104
58: ; preds = %52
%59 = load i8*, i8** %11
%60 = load i32, i32* @O_RDWR
%61 = tail call i32 @open(i8* %59, i32 %60)
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %70
63: ; preds = %58
%64 = load i32, i32* @stderr
%65 = getelementptr inbounds i8*, i8** %1, i64 1
%66 = load i8*, i8** %65
%67 = tail call i32 (i32, i8*, ...) @fprintf(i32 %64, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %66)
%68 = load i32, i32* @EXIT_FAILURE
%69 = tail call i32 @exit(i32 %68)
unreachable
70: ; preds = %58
%71 = load i32, i32* @LOCK_EX
%72 = load i32, i32* @LOCK_NB
%73 = or i32 %72, %71
%74 = tail call i32 @flock(i32 %61, i32 %73)
%75 = icmp sgt i32 %74, -1
br i1 %75, label %76, label %80
76: ; preds = %70
%77 = load i32, i32* @stderr
%78 = tail call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%79 = load i32, i32* @EXIT_FAILURE
store i32 %79, i32* %3
br label %98
80: ; preds = %70
%81 = load i32, i32* @LOCK_UN
%82 = tail call i32 @flock(i32 %14, i32 %81)
%83 = icmp eq i32 %82, -1
br i1 %83, label %84, label %88
84: ; preds = %80
%85 = load i32, i32* @stderr
%86 = tail call i32 (i32, i8*, ...) @fprintf(i32 %85, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
%87 = load i32, i32* @EXIT_FAILURE
store i32 %87, i32* %3
br label %98
88: ; preds = %80
%89 = load i32, i32* @LOCK_EX
%90 = load i32, i32* @LOCK_NB
%91 = or i32 %90, %89
%92 = tail call i32 @flock(i32 %61, i32 %91)
%93 = icmp eq i32 %92, -1
br i1 %93, label %94, label %98
94: ; preds = %88
%95 = load i32, i32* @stderr
%96 = tail call i32 (i32, i8*, ...) @fprintf(i32 %95, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
%97 = load i32, i32* @EXIT_FAILURE
store i32 %97, i32* %3
br label %98
98: ; preds = %88, %94, %84, %76
%99 = phi i32 [ %5, %88 ], [ %97, %94 ], [ %87, %84 ], [ %79, %76 ]
%100 = tail call i32 @close(i32 %61)
%101 = tail call i32 @exit(i32 %99)
unreachable
102: ; preds = %52
%103 = call i32 @waitpid(i32 %54, i32* %3, i32 0)
br label %104
104: ; preds = %102, %55, %47, %35, %27
%105 = call i32 @close(i32 %14)
br label %106
106: ; preds = %104, %16, %7
%107 = phi i32* [ @EXIT_FAILURE, %7 ], [ @EXIT_FAILURE, %16 ], [ %3, %104 ]
%108 = load i32, i32* %107
ret i32 %108
}