#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_type;
  int /*<<< orphan*/ l_pid;
  scalar_t__ l_len;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
};

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
scalar_t__ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
scalar_t__ F_UNLCK;
int /*<<< orphan*/ SEEK_SET;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fopen(char *, char *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ printf(char *);
int isOnlyInstance() {
  struct flock fl;
  char *lockFile = "/tmp/.daboxsrv.lck";
  int fd = fileno(fopen(lockFile, "r"));
  if (fd == -1) {
    printf("Could not open /tmp/.daboxsrv.lck for reading.");
    return -1;
  }
  if (fcntl(fd, F_GETLK, &fl) == -1) {
    printf("Could not read lock state of /tmp/.daboxsrv.lck");
    return -1;
  }
  if (fl.l_type != F_UNLCK) {
    return 0;
  } else {
    fl.l_type = F_RDLCK;
    fl.l_whence = SEEK_SET;
    fl.l_start = 0;
    fl.l_len = 0;
    fl.l_pid = getpid();
    if (fcntl(fd, F_SETLK, &fl) == -1) {
      printf("Could not set read lock on /tmp/.daboxsrv.lck");
      return 1;
    }
    return 1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<isOnlyInstance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xeef(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1070 <fopen@plt>
mov    %eax,%edi
call   1060 <fileno@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    119f <isOnlyInstance+0x4f>
lea    0xe74(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <isOnlyInstance+0x116>
mov    -0x3c(%rbp),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11d5 <isOnlyInstance+0x85>
lea    0xe6d(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <isOnlyInstance+0x116>
mov    -0x30(%rbp),%rax
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%rax
je     11f5 <isOnlyInstance+0xa5>
movl   $0x0,-0x4(%rbp)
jmp    1266 <isOnlyInstance+0x116>
mov    0x2dbc(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    -0x3c(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    125f <isOnlyInstance+0x10f>
lea    0xe13(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1266 <isOnlyInstance+0x116>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isOnlyInstance>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
lea    0xef1(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1070 <fopen@plt>
mov    %eax,%edi
call   1060 <fileno@plt>
cmp    $0xffffffff,%eax
je     11fe <isOnlyInstance+0xae>
mov    %eax,%ebx
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1207 <isOnlyInstance+0xb7>
mov    (%rsp),%rax
mov    0x2e3a(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%rax
jne    1218 <isOnlyInstance+0xc8>
mov    0x2e0c(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rsp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0x8(%rsp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
mov    $0x1,%ebp
cmp    $0xffffffff,%eax
jne    1218 <isOnlyInstance+0xc8>
lea    0xe6a(%rip),%rdi
call   1050 <printf@plt>
jmp    1218 <isOnlyInstance+0xc8>
lea    0xdfd(%rip),%rdi
jmp    120e <isOnlyInstance+0xbe>
lea    0xe23(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i32, i64, i64, i32 }
@.str = [19 x i8] c"/tmp/.daboxsrv.lck\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [47 x i8] c"Could not open /tmp/.daboxsrv.lck for reading.\00"
@F_GETLK = global i32 0
@.str.3 = [48 x i8] c"Could not read lock state of /tmp/.daboxsrv.lck\00"
@F_UNLCK = global i64 0
@F_RDLCK = global i64 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str.4 = [46 x i8] c"Could not set read lock on /tmp/.daboxsrv.lck\00"
define i32 @isOnlyInstance() {
%1 = alloca i32
%2 = alloca %struct.flock
%3 = alloca i8*
%4 = alloca i32
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = call i32 @fileno(i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %41
12: ; preds = %0
%13 = load i32, i32* %4
%14 = load i32, i32* @F_GETLK
%15 = call i32 @fcntl(i32 %13, i32 %14, %struct.flock* %2)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %41
19: ; preds = %12
%20 = getelementptr inbounds %struct.flock, %struct.flock* %2, i32 0, i32 0
%21 = load i64, i64* %20
%22 = load i64, i64* @F_UNLCK
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 0, i32* %1
br label %41
25: ; preds = %19
%26 = load i64, i64* @F_RDLCK
%27 = getelementptr inbounds %struct.flock, %struct.flock* %2, i32 0, i32 0
store i64 %26, i64* %27
%28 = load i32, i32* @SEEK_SET
%29 = getelementptr inbounds %struct.flock, %struct.flock* %2, i32 0, i32 4
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.flock, %struct.flock* %2, i32 0, i32 3
store i64 0, i64* %30
%31 = getelementptr inbounds %struct.flock, %struct.flock* %2, i32 0, i32 2
store i64 0, i64* %31
%32 = call i32 (...) @getpid()
%33 = getelementptr inbounds %struct.flock, %struct.flock* %2, i32 0, i32 1
store i32 %32, i32* %33
%34 = load i32, i32* %4
%35 = load i32, i32* @F_SETLK
%36 = call i32 @fcntl(i32 %34, i32 %35, %struct.flock* %2)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %25
%39 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %41
40: ; preds = %25
store i32 1, i32* %1
br label %41
41: ; preds = %40, %38, %24, %17, %10
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
%struct.flock = type { i64, i32, i64, i64, i32 }
@.str = [19 x i8] c"/tmp/.daboxsrv.lck\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [47 x i8] c"Could not open /tmp/.daboxsrv.lck for reading.\00"
@F_GETLK = global i32 0
@.str.3 = [48 x i8] c"Could not read lock state of /tmp/.daboxsrv.lck\00"
@F_UNLCK = global i64 0
@F_RDLCK = global i64 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str.4 = [46 x i8] c"Could not set read lock on /tmp/.daboxsrv.lck\00"
define i32 @isOnlyInstance() {
%1 = alloca %struct.flock
%2 = bitcast %struct.flock* %1 to i8*
%3 = tail call i32 @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32 @fileno(i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
br label %32
8: ; preds = %0
%9 = load i32, i32* @F_GETLK
%10 = call i32 @fcntl(i32 %4, i32 %9, %struct.flock* %1)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
br label %32
14: ; preds = %8
%15 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 0
%16 = load i64, i64* %15
%17 = load i64, i64* @F_UNLCK
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %32
19: ; preds = %14
%20 = load i64, i64* @F_RDLCK
store i64 %20, i64* %15
%21 = load i32, i32* @SEEK_SET
%22 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 4
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 2
%24 = bitcast i64* %23 to i8*
%25 = call i32 (...) @getpid()
%26 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 1
store i32 %25, i32* %26
%27 = load i32, i32* @F_SETLK
%28 = call i32 @fcntl(i32 %4, i32 %27, %struct.flock* %1)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %19
%31 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %19, %14, %30, %12, %6
%33 = phi i32 [ -1, %6 ], [ -1, %12 ], [ 1, %30 ], [ 0, %14 ], [ 1, %19 ]
ret i32 %33
}