#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 {
  scalar_t__ l_len;
  scalar_t__ l_start;
  void *l_whence;
  int /*<<< orphan*/ l_type;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ EAGAIN;
#define EWOULDBLOCK 128
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int LOCK_EX;
int LOCK_NB;
int LOCK_UN;
int /*<<< orphan*/ O_RDWR;
void *SEEK_SET;
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int flock(int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int main(int argc, char **argv) {
  struct flock fl = {0};
  int fd, result, flock_lock_succeeded = 0;
  fd = open("config/test-file-to-lock", O_RDWR);
  if (fd < 0) {
    perror("Couldn't open test-file-to-lock");
    return -1;
  }
  fl.l_type = F_WRLCK;
  fl.l_whence = SEEK_SET;
  fl.l_start = 0;
  fl.l_len = 0;
  if (fcntl(fd, F_SETLK, &fl) == -1) {
    perror("Couldn't lock with fcntl in the first place");
    return -1;
  }
  result = flock(fd, LOCK_EX | LOCK_NB);
  switch (result) {
  case 0:
    flock_lock_succeeded = 1;
    if (flock(fd, LOCK_UN) == -1) {
      perror("flock succeeded, but couldn't unlock again with flock");
      return -1;
    }
    break;
  case EWOULDBLOCK:
    flock_lock_succeeded = 0;
    break;
  default:
    if (errno == EAGAIN) {
      flock_lock_succeeded = 0;
      break;
    } else {
      perror("flock failed");
      return -1;
    }
  }
  fl.l_type = F_UNLCK;
  fl.l_whence = SEEK_SET;
  fl.l_start = 0;
  fl.l_len = 0;
  if (fcntl(fd, F_SETLK, &fl) == -1) {
    perror("Couldn't unlock with fcntl afterwards");
    return -1;
  }
  if (flock_lock_succeeded) {
    return 0;
  } else {
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
movl   $0x0,-0x3c(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe73(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11b7 <main+0x67>
lea    0xe73(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135f <main+0x20f>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1217 <main+0xc7>
lea    0xe33(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135f <main+0x20f>
mov    -0x34(%rbp),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
or     %eax,%esi
call   1070 <flock@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x40(%rbp)
test   %eax,%eax
je     125c <main+0x10c>
jmp    1249 <main+0xf9>
mov    -0x40(%rbp),%eax
sub    $0x80,%eax
je     129a <main+0x14a>
jmp    12a6 <main+0x156>
movl   $0x1,-0x3c(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
call   1070 <flock@plt>
cmp    $0xffffffff,%eax
jne    1295 <main+0x145>
lea    0xde1(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135f <main+0x20f>
jmp    12e2 <main+0x192>
movl   $0x0,-0x3c(%rbp)
jmp    12e2 <main+0x192>
mov    0x2d13(%rip),%rax
mov    (%rax),%eax
mov    0x2ce2(%rip),%rcx
cmp    (%rcx),%eax
jne    12ca <main+0x17a>
movl   $0x0,-0x3c(%rbp)
jmp    12e2 <main+0x192>
lea    0xdca(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135f <main+0x20f>
mov    0x2ce7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2cc3(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2cc2(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1342 <main+0x1f2>
lea    0xd77(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135f <main+0x20f>
cmpl   $0x0,-0x3c(%rbp)
je     1358 <main+0x208>
movl   $0x0,-0x4(%rbp)
jmp    135f <main+0x20f>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
movq   $0x0,0x18(%rsp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
lea    0xe9d(%rip),%rdi
call   1040 <open@plt>
test   %eax,%eax
js     124a <main+0x10a>
mov    %eax,%ebx
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e32(%rip),%r14
mov    (%r14),%rax
mov    %rax,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    0x2e3c(%rip),%r15
mov    (%r15),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1253 <main+0x113>
mov    0x2e27(%rip),%rax
mov    0x2dd0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebx,%edi
call   1060 <flock@plt>
mov    $0x1,%ebp
cmp    $0x80,%eax
je     120f <main+0xcf>
test   %eax,%eax
jne    11fb <main+0xbb>
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <flock@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
jne    120f <main+0xcf>
lea    0xe6c(%rip),%rdi
jmp    126c <main+0x12c>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    0x2d8d(%rip),%rcx
cmp    (%rcx),%eax
jne    1265 <main+0x125>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    (%r14),%rax
mov    %rax,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    (%r15),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     125c <main+0x11c>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc8(%rip),%rdi
jmp    126c <main+0x12c>
lea    0xddf(%rip),%rdi
jmp    126c <main+0x12c>
lea    0xe45(%rip),%rdi
jmp    126c <main+0x12c>
lea    0xe2f(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebp
jmp    123d <main+0xfd>

LLVM IR (-O0):
%struct.flock = type { i64, i64, i8*, i32, i32 }
@.str = [25 x i8] c"config/test-file-to-lock\00"
@O_RDWR = global i32 0
@.str.1 = [32 x i8] c"Couldn't open test-file-to-lock\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i8* null
@F_SETLK = global i32 0
@.str.2 = [44 x i8] c"Couldn't lock with fcntl in the first place\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@LOCK_UN = global i32 0
@.str.3 = [54 x i8] c"flock succeeded, but couldn't unlock again with flock\00"
@errno = global i32 0
@EAGAIN = global i32 0
@.str.4 = [13 x i8] c"flock failed\00"
@F_UNLCK = global i32 0
@.str.5 = [38 x i8] c"Couldn't unlock with fcntl afterwards\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.flock
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast %struct.flock* %6 to i8*
store i32 0, i32* %9
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
17: ; preds = %2
%18 = load i32, i32* @F_WRLCK
%19 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 %18, i32* %19
%20 = load i8*, i8** @SEEK_SET
%21 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i8* %20, i8** %21
%22 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i64 0, i64* %22
%23 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i64 0, i64* %23
%24 = load i32, i32* %7
%25 = load i32, i32* @F_SETLK
%26 = call i32 @fcntl(i32 %24, i32 %25, %struct.flock* %6)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
30: ; preds = %17
%31 = load i32, i32* %7
%32 = load i32, i32* @LOCK_EX
%33 = load i32, i32* @LOCK_NB
%34 = or i32 %32, %33
%35 = call i32 @flock(i32 %31, i32 %34)
store i32 %35, i32* %8
%36 = load i32, i32* %8
switch i32 %36, label %46 [
i32 0, label %37
i32 128, label %45
]
37: ; preds = %30
store i32 1, i32* %9
%38 = load i32, i32* %7
%39 = load i32, i32* @LOCK_UN
%40 = call i32 @flock(i32 %38, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
44: ; preds = %37
br label %53
45: ; preds = %30
store i32 0, i32* %9
br label %53
46: ; preds = %30
%47 = load i32, i32* @errno
%48 = load i32, i32* @EAGAIN
%49 = icmp eq i32 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %46
store i32 0, i32* %9
br label %53
51: ; preds = %46
%52 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
53: ; preds = %50, %45, %44
%54 = load i32, i32* @F_UNLCK
%55 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 %54, i32* %55
%56 = load i8*, i8** @SEEK_SET
%57 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i8* %56, i8** %57
%58 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i64 0, i64* %58
%59 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i64 0, i64* %59
%60 = load i32, i32* %7
%61 = load i32, i32* @F_SETLK
%62 = call i32 @fcntl(i32 %60, i32 %61, %struct.flock* %6)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %53
%65 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
66: ; preds = %53
%67 = load i32, i32* %9
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %66
store i32 0, i32* %3
br label %71
70: ; preds = %66
store i32 1, i32* %3
br label %71
71: ; preds = %70, %69, %64, %51, %42, %28, %15
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i8*, i32, i32 }
@.str = [25 x i8] c"config/test-file-to-lock\00"
@O_RDWR = global i32 0
@.str.1 = [32 x i8] c"Couldn't open test-file-to-lock\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i8* null
@F_SETLK = global i32 0
@.str.2 = [44 x i8] c"Couldn't lock with fcntl in the first place\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@LOCK_UN = global i32 0
@.str.3 = [54 x i8] c"flock succeeded, but couldn't unlock again with flock\00"
@errno = global i32 0
@EAGAIN = global i32 0
@.str.4 = [13 x i8] c"flock failed\00"
@F_UNLCK = global i32 0
@.str.5 = [38 x i8] c"Couldn't unlock with fcntl afterwards\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
%6 = bitcast i32* %5 to i64*
store i64 0, i64* %6
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %50
12: ; preds = %2
%13 = load i32, i32* @F_WRLCK
%14 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %13, i32* %14
%15 = load i8*, i8** @SEEK_SET
%16 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i8* %15, i8** %16
%17 = bitcast %struct.flock* %3 to i8*
%18 = load i32, i32* @F_SETLK
%19 = call i32 @fcntl(i32 %8, i32 %18, %struct.flock* %3)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
br label %50
23: ; preds = %12
%24 = load i32, i32* @LOCK_EX
%25 = load i32, i32* @LOCK_NB
%26 = or i32 %25, %24
%27 = call i32 @flock(i32 %8, i32 %26)
switch i32 %27, label %34 [
i32 0, label %28
i32 128, label %40
]
28: ; preds = %23
%29 = load i32, i32* @LOCK_UN
%30 = call i32 @flock(i32 %8, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %40
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0))
br label %50
34: ; preds = %23
%35 = load i32, i32* @errno
%36 = load i32, i32* @EAGAIN
%37 = icmp eq i32 %35, %36
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %50
40: ; preds = %34, %23, %28
%41 = phi i32 [ 0, %28 ], [ 1, %23 ], [ 1, %34 ]
%42 = load i32, i32* @F_UNLCK
store i32 %42, i32* %14
%43 = load i8*, i8** @SEEK_SET
store i8* %43, i8** %16
%44 = bitcast %struct.flock* %3 to i8*
%45 = load i32, i32* @F_SETLK
%46 = call i32 @fcntl(i32 %8, i32 %45, %struct.flock* %3)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %40
%49 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0))
br label %50
50: ; preds = %40, %48, %38, %32, %21, %10
%51 = phi i32 [ -1, %10 ], [ -1, %21 ], [ -1, %48 ], [ -1, %38 ], [ -1, %32 ], [ %41, %40 ]
ret i32 %51
}