#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 LOCK_EX;
int LOCK_NB;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ exit(int);
scalar_t__ flock(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int fd, fd2;
  if (argc != 2) {
    fprintf(stderr, "Usage: tflock2 file\n");
    exit(1);
  }
  if ((fd = open(argv[1], O_RDWR)) < 0) {
    perror(argv[1]);
    exit(1);
  }
  fprintf(stderr, "fd: open\n");
  if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
    perror("flock");
    exit(1);
  }
  fprintf(stderr, "fd: write-locked\n");
  if ((fd2 = open(argv[1], O_RDWR)) < 0) {
    perror(argv[1]);
    exit(1);
  }
  fprintf(stderr, "fd2: open\n");
  if (flock(fd2, LOCK_EX | LOCK_NB) < 0)
    perror("second flock failed as it should - PASS");
  else
    fprintf(stderr, "fd: write-locked - FAIL\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     118f <main+0x3f>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11c8 <main+0x78>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x14(%rbp),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    0x2dd0(%rip),%rax
or     (%rax),%esi
call   1060 <flock@plt>
cmp    $0x0,%rax
jge    1217 <main+0xc7>
lea    0xe17(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1265 <main+0x115>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x18(%rbp),%edi
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
mov    0x2d33(%rip),%rax
or     (%rax),%esi
call   1060 <flock@plt>
cmp    $0x0,%rax
jge    12af <main+0x15f>
lea    0xd9d(%rip),%rdi
call   1050 <perror@plt>
jmp    12c4 <main+0x174>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
lea    0xdab(%rip),%rsi
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1180 <main+0x30>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e7a(%rip),%r15
mov    (%r15),%esi
call   1040 <open@plt>
test   %eax,%eax
jns    119f <main+0x4f>
mov    0x8(%rbx),%rdi
jmp    11e0 <main+0x90>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    %eax,%ebp
mov    0x2e28(%rip),%r14
mov    (%r14),%edi
lea    0xe63(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e22(%rip),%r12
mov    0x2dfb(%rip),%r13
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %ebp,%edi
call   1060 <flock@plt>
test   %rax,%rax
jns    11ef <main+0x9f>
lea    0xe3f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    (%r14),%edi
lea    0xe2c(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x8(%rbx),%rdi
mov    (%r15),%esi
call   1040 <open@plt>
test   %eax,%eax
js     117a <main+0x2a>
mov    %eax,%ebp
mov    (%r14),%edi
lea    0xe19(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %ebp,%edi
call   1060 <flock@plt>
test   %rax,%rax
jns    124a <main+0xfa>
lea    0xe04(%rip),%rdi
call   1050 <perror@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    (%r14),%edi
lea    0xe16(%rip),%rsi
call   1030 <fprintf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"Usage: tflock2 file\0A\00"
@O_RDWR = global i32 0
@.str.1 = [10 x i8] c"fd: open\0A\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.2 = [6 x i8] c"flock\00"
@.str.3 = [18 x i8] c"fd: write-locked\0A\00"
@.str.4 = [11 x i8] c"fd2: open\0A\00"
@.str.5 = [40 x i8] c"second flock failed as it should - PASS\00"
@.str.6 = [25 x i8] c"fd: write-locked - FAIL\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %14
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @perror(i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %14
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* %6
%31 = load i32, i32* @LOCK_EX
%32 = load i32, i32* @LOCK_NB
%33 = or i32 %31, %32
%34 = call i64 @flock(i32 %30, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %27
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %27
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%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 %54
48: ; preds = %39
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8*, i8** %50
%52 = call i32 @perror(i8* %51)
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %39
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%57 = load i32, i32* %7
%58 = load i32, i32* @LOCK_EX
%59 = load i32, i32* @LOCK_NB
%60 = or i32 %58, %59
%61 = call i64 @flock(i32 %57, i32 %60)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %54
%64 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
br label %68
65: ; preds = %54
%66 = load i32, i32* @stderr
%67 = call i32 @fprintf(i32 %66, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
br label %68
68: ; preds = %65, %63
%69 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"Usage: tflock2 file\0A\00"
@O_RDWR = global i32 0
@.str.1 = [10 x i8] c"fd: open\0A\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.2 = [6 x i8] c"flock\00"
@.str.3 = [18 x i8] c"fd: write-locked\0A\00"
@.str.4 = [11 x i8] c"fd2: open\0A\00"
@.str.5 = [40 x i8] c"second flock failed as it should - PASS\00"
@.str.6 = [25 x i8] c"fd: write-locked - FAIL\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = load i8*, i8** %9
%16 = tail call i32 @perror(i8* %15)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %8
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @LOCK_EX
%22 = load i32, i32* @LOCK_NB
%23 = or i32 %22, %21
%24 = tail call i64 @flock(i32 %12, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %18
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%32 = load i8*, i8** %9
%33 = load i32, i32* @O_RDWR
%34 = tail call i32 @open(i8* %32, i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = load i8*, i8** %9
%38 = tail call i32 @perror(i8* %37)
%39 = tail call i32 @exit(i32 1)
unreachable
40: ; preds = %29
%41 = load i32, i32* @stderr
%42 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%43 = load i32, i32* @LOCK_EX
%44 = load i32, i32* @LOCK_NB
%45 = or i32 %44, %43
%46 = tail call i64 @flock(i32 %34, i32 %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %40
%49 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
br label %53
50: ; preds = %40
%51 = load i32, i32* @stderr
%52 = tail call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
br label %53
53: ; preds = %50, %48
%54 = tail call i32 @exit(i32 0)
unreachable
}