#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[2]);
    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[2]);
    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    0x10(%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    0x10(%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    0x10(%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    0x10(%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 2
%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 2
%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 %104
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 %20
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %11
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %104
20: ; preds = %10
%21 = load i32, i32* @LOCK_EX
%22 = load i32, i32* @LOCK_NB
%23 = or i32 %22, %21
%24 = tail call i32 @flock(i32 %14, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
store i32 %28, i32* %3
br label %102
29: ; preds = %20
%30 = load i8*, i8** %11
%31 = load i32, i32* @O_RDWR
%32 = tail call i32 @open(i8* %30, i32 %31)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %11
%37 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @EXIT_FAILURE
store i32 %38, i32* %3
br label %102
39: ; preds = %29
%40 = load i32, i32* @LOCK_EX
%41 = load i32, i32* @LOCK_NB
%42 = or i32 %41, %40
%43 = tail call i32 @flock(i32 %32, i32 %42)
%44 = icmp sgt i32 %43, -1
br i1 %44, label %45, label %50
45: ; preds = %39
%46 = load i32, i32* @stderr
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
store i32 %48, i32* %3
%49 = tail call i32 @close(i32 %32)
br label %102
50: ; preds = %39
%51 = tail call i32 @close(i32 %32)
%52 = tail call i32 (...) @fork()
switch i32 %52, label %100 [
i32 -1, label %53
i32 0, label %56
]
53: ; preds = %50
%54 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
store i32 %55, i32* %3
br label %102
56: ; preds = %50
%57 = load i8*, i8** %11
%58 = load i32, i32* @O_RDWR
%59 = tail call i32 @open(i8* %57, i32 %58)
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %68
61: ; preds = %56
%62 = load i32, i32* @stderr
%63 = getelementptr inbounds i8*, i8** %1, i64 1
%64 = load i8*, i8** %63
%65 = tail call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %64)
%66 = load i32, i32* @EXIT_FAILURE
%67 = tail call i32 @exit(i32 %66)
unreachable
68: ; preds = %56
%69 = load i32, i32* @LOCK_EX
%70 = load i32, i32* @LOCK_NB
%71 = or i32 %70, %69
%72 = tail call i32 @flock(i32 %59, i32 %71)
%73 = icmp sgt i32 %72, -1
br i1 %73, label %74, label %78
74: ; preds = %68
%75 = load i32, i32* @stderr
%76 = tail call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%77 = load i32, i32* @EXIT_FAILURE
store i32 %77, i32* %3
br label %96
78: ; preds = %68
%79 = load i32, i32* @LOCK_UN
%80 = tail call i32 @flock(i32 %14, i32 %79)
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %86
82: ; preds = %78
%83 = load i32, i32* @stderr
%84 = tail call i32 (i32, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
%85 = load i32, i32* @EXIT_FAILURE
store i32 %85, i32* %3
br label %96
86: ; preds = %78
%87 = load i32, i32* @LOCK_EX
%88 = load i32, i32* @LOCK_NB
%89 = or i32 %88, %87
%90 = tail call i32 @flock(i32 %59, i32 %89)
%91 = icmp eq i32 %90, -1
br i1 %91, label %92, label %96
92: ; preds = %86
%93 = load i32, i32* @stderr
%94 = tail call i32 (i32, i8*, ...) @fprintf(i32 %93, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
%95 = load i32, i32* @EXIT_FAILURE
store i32 %95, i32* %3
br label %96
96: ; preds = %86, %92, %82, %74
%97 = phi i32 [ %5, %86 ], [ %95, %92 ], [ %85, %82 ], [ %77, %74 ]
%98 = tail call i32 @close(i32 %59)
%99 = tail call i32 @exit(i32 %97)
unreachable
100: ; preds = %50
%101 = call i32 @waitpid(i32 %52, i32* %3, i32 0)
br label %102
102: ; preds = %100, %53, %45, %34, %26
%103 = call i32 @close(i32 %14)
br label %104
104: ; preds = %102, %16, %7
%105 = phi i32* [ @EXIT_FAILURE, %7 ], [ @EXIT_FAILURE, %16 ], [ %3, %102 ]
%106 = load i32, i32* %105
ret i32 %106
}