#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 */
typedef int /*<<< orphan*/ msg;

/* Variables and functions */
scalar_t__ ENOENT;
int LOCK_EX;
int LOCK_NB;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int flock(int, int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int unix_server_trylock() {
  int fd, already_locked;
  fd = open("/tmp/kore-dataserver.lock", O_RDONLY);
  if (fd == -1 && errno != ENOENT) {
    char msg[1024];
    snprintf(msg, sizeof(msg), "datserver: Cannot open lock file %s",
             "/tmp/kore-dataserver.lock");
    perror(msg);
    return -1;
  } else if (fd != -1) {
    already_locked = flock(fd, LOCK_EX | LOCK_NB) == -1;
    close(fd);
    if (already_locked)
      return 0;
  }
  fd = open("/tmp/kore-dataserver.lock", O_CREAT | O_WRONLY,
            S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (fd == -1) {
    perror("dataserver: Cannot create lock file");
    return -1;
  }
  flock(fd, LOCK_EX);
  return 1;
}


x86 assembly (clang, -O0):
<unix_server_trylock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d0 <unix_server_trylock+0x80>
mov    0x2e3a(%rip),%rax
mov    (%rax),%rax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%rax
je     11d0 <unix_server_trylock+0x80>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
lea    0xe6e(%rip),%rdx
lea    0xe4d(%rip),%rcx
call   1030 <snprintf@plt>
lea    -0x410(%rbp),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ab <unix_server_trylock+0x15b>
cmpl   $0xffffffff,-0x8(%rbp)
je     1225 <unix_server_trylock+0xd5>
mov    -0x8(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    0x2d9b(%rip),%rax
or     (%rax),%esi
call   1070 <flock@plt>
cmp    $0xffffffff,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0xc(%rbp)
je     1220 <unix_server_trylock+0xd0>
movl   $0x0,-0x4(%rbp)
jmp    12ab <unix_server_trylock+0x15b>
jmp    1225 <unix_server_trylock+0xd5>
jmp    122a <unix_server_trylock+0xda>
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    0x2d56(%rip),%rax
or     (%rax),%esi
mov    0x2d65(%rip),%rax
mov    (%rax),%edx
mov    0x2d84(%rip),%rax
or     (%rax),%edx
mov    0x2d63(%rip),%rax
or     (%rax),%edx
mov    0x2d52(%rip),%rax
or     (%rax),%edx
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1293 <unix_server_trylock+0x143>
lea    0xdbc(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ab <unix_server_trylock+0x15b>
mov    -0x8(%rbp),%edi
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
call   1070 <flock@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_server_trylock>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11a5 <unix_server_trylock+0x55>
mov    %eax,%ebp
mov    0x2e5d(%rip),%rax
mov    0x2dfe(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebp,%edi
call   1070 <flock@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%ebx
jne    11bb <unix_server_trylock+0x6b>
jmp    121a <unix_server_trylock+0xca>
mov    0x2e14(%rip),%rax
mov    (%rax),%rax
mov    0x2de2(%rip),%rcx
cmp    (%rcx),%rax
jne    1232 <unix_server_trylock+0xe2>
mov    0x2e36(%rip),%rax
mov    0x2dc7(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd4(%rip),%rax
mov    0x2df5(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dd2(%rip),%rax
or     (%rax),%edx
mov    0x2dc1(%rip),%rax
or     (%rax),%edx
lea    0xe08(%rip),%rdi
xor    %eax,%eax
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1229 <unix_server_trylock+0xd9>
mov    0x2dd5(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1070 <flock@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe0e(%rip),%rdi
jmp    1253 <unix_server_trylock+0x103>
lea    0xde1(%rip),%rdx
lea    0xdc0(%rip),%rcx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1030 <snprintf@plt>
mov    %rbx,%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%r14d
jmp    121a <unix_server_trylock+0xca>

LLVM IR (-O0):
@.str = [26 x i8] c"/tmp/kore-dataserver.lock\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [36 x i8] c"datserver: Cannot open lock file %s\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [36 x i8] c"dataserver: Cannot create lock file\00"
define i32 @unix_server_trylock() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = load i32, i32* @O_RDONLY
%6 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %18
9: ; preds = %0
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOENT
%12 = icmp ne i64 %10, %11
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%15 = call i32 @snprintf(i8* %14, i32 1024, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%17 = call i32 @perror(i8* %16)
store i32 -1, i32* %1
br label %56
18: ; preds = %9, %0
%19 = load i32, i32* %2
%20 = icmp ne i32 %19, -1
br i1 %20, label %21, label %35
21: ; preds = %18
%22 = load i32, i32* %2
%23 = load i32, i32* @LOCK_EX
%24 = load i32, i32* @LOCK_NB
%25 = or i32 %23, %24
%26 = call i32 @flock(i32 %22, i32 %25)
%27 = icmp eq i32 %26, -1
%28 = zext i1 %27 to i32
store i32 %28, i32* %3
%29 = load i32, i32* %2
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %3
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %21
store i32 0, i32* %1
br label %56
34: ; preds = %21
br label %35
35: ; preds = %34, %18
br label %36
36: ; preds = %35
%37 = load i32, i32* @O_CREAT
%38 = load i32, i32* @O_WRONLY
%39 = or i32 %37, %38
%40 = load i32, i32* @S_IRUSR
%41 = load i32, i32* @S_IWUSR
%42 = or i32 %40, %41
%43 = load i32, i32* @S_IRGRP
%44 = or i32 %42, %43
%45 = load i32, i32* @S_IROTH
%46 = or i32 %44, %45
%47 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %39, i32 %46)
store i32 %47, i32* %2
%48 = load i32, i32* %2
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %36
%51 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %56
52: ; preds = %36
%53 = load i32, i32* %2
%54 = load i32, i32* @LOCK_EX
%55 = call i32 @flock(i32 %53, i32 %54)
store i32 1, i32* %1
br label %56
56: ; preds = %52, %50, %33, %13
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
@.str = [26 x i8] c"/tmp/kore-dataserver.lock\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [36 x i8] c"datserver: Cannot open lock file %s\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [36 x i8] c"dataserver: Cannot create lock file\00"
define i32 @unix_server_trylock() {
%1 = alloca [1024 x i8]
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %13
5: ; preds = %0
%6 = load i64, i64* @errno
%7 = load i64, i64* @ENOENT
%8 = icmp eq i64 %6, %7
br i1 %8, label %20, label %9
9: ; preds = %5
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%11 = call i32 @snprintf(i8* %10, i32 1024, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @perror(i8* %10)
br label %38
13: ; preds = %0
%14 = load i32, i32* @LOCK_EX
%15 = load i32, i32* @LOCK_NB
%16 = or i32 %15, %14
%17 = tail call i32 @flock(i32 %3, i32 %16)
%18 = icmp eq i32 %17, -1
%19 = tail call i32 @close(i32 %3)
br i1 %18, label %38, label %20
20: ; preds = %5, %13
%21 = load i32, i32* @O_CREAT
%22 = load i32, i32* @O_WRONLY
%23 = or i32 %22, %21
%24 = load i32, i32* @S_IRUSR
%25 = load i32, i32* @S_IWUSR
%26 = or i32 %25, %24
%27 = load i32, i32* @S_IRGRP
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IROTH
%30 = or i32 %28, %29
%31 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %20
%34 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
br label %38
35: ; preds = %20
%36 = load i32, i32* @LOCK_EX
%37 = tail call i32 @flock(i32 %31, i32 %36)
br label %38
38: ; preds = %13, %35, %33, %9
%39 = phi i32 [ -1, %9 ], [ -1, %33 ], [ 1, %35 ], [ 0, %13 ]
ret i32 %39
}