#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 stat {
  scalar_t__ st_uid;
};

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ ENOENT;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ getuid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stat(char *, struct stat *);
int lock_check(void) {
  struct stat buf;
  int lock_handle = -1;
  int result = 0;
  char *filepath = "LIKWIDLOCK";
  if ((lock_handle = open(filepath, O_RDONLY)) == -1) {
    if (errno == ENOENT) {
      result = 1;
    } else if (errno == EACCES) {
      result = 0;
    } else {
      result = 1;
    }
  } else {
    stat(filepath, &buf);
    if (buf.st_uid == getuid()) {
      result = 1;
    } else {
      result = 0;
    }
  }
  if (lock_handle > 0) {
    close(lock_handle);
  }
  return result;
}


x86 assembly (clang, -O0):
<lock_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0xffffffff,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    0xea3(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11dc <lock_check+0x9c>
mov    0x2e4a(%rip),%rax
mov    (%rax),%rax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%rax
jne    11a5 <lock_check+0x65>
movl   $0x1,-0x10(%rbp)
jmp    11d7 <lock_check+0x97>
mov    0x2e24(%rip),%rax
mov    (%rax),%rax
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%rax
jne    11cb <lock_check+0x8b>
movl   $0x0,-0x10(%rbp)
jmp    11d2 <lock_check+0x92>
movl   $0x1,-0x10(%rbp)
jmp    11d7 <lock_check+0x97>
jmp    1220 <lock_check+0xe0>
mov    -0x18(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <stat@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
jne    1214 <lock_check+0xd4>
movl   $0x1,-0x10(%rbp)
jmp    121b <lock_check+0xdb>
movl   $0x0,-0x10(%rbp)
jmp    1220 <lock_check+0xe0>
cmpl   $0x0,-0xc(%rbp)
jle    1232 <lock_check+0xf2>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_check>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
lea    0xead(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     118d <lock_check+0x4d>
mov    %eax,%ebx
lea    0xe9a(%rip),%rdi
mov    %rsp,%rsi
call   1050 <stat@plt>
mov    (%rsp),%rbp
xor    %eax,%eax
call   1030 <getuid@plt>
cmp    %rax,%rbp
sete   %bpl
test   %ebx,%ebx
jle    11b4 <lock_check+0x74>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    11b4 <lock_check+0x74>
mov    0x2e3c(%rip),%rax
mov    (%rax),%rax
mov    0x2e22(%rip),%rcx
mov    $0x1,%bpl
cmp    (%rcx),%rax
je     11b4 <lock_check+0x74>
mov    0x2e33(%rip),%rcx
cmp    (%rcx),%rax
setne  %bpl
movzbl %bpl,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [11 x i8] c"LIKWIDLOCK\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@EACCES = global i64 0
define i32 @lock_check() {
%1 = alloca %struct.stat
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 -1, i32* %2
store i32 0, i32* %3
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %4
%5 = load i8*, i8** %4
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %22
9: ; preds = %0
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOENT
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %14
13: ; preds = %9
store i32 1, i32* %3
br label %21
14: ; preds = %9
%15 = load i64, i64* @errno
%16 = load i64, i64* @EACCES
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 0, i32* %3
br label %20
19: ; preds = %14
store i32 1, i32* %3
br label %20
20: ; preds = %19, %18
br label %21
21: ; preds = %20, %13
br label %32
22: ; preds = %0
%23 = load i8*, i8** %4
%24 = call i32 @stat(i8* %23, %struct.stat* %1)
%25 = getelementptr inbounds %struct.stat, %struct.stat* %1, i32 0, i32 0
%26 = load i64, i64* %25
%27 = call i64 (...) @getuid()
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %22
store i32 1, i32* %3
br label %31
30: ; preds = %22
store i32 0, i32* %3
br label %31
31: ; preds = %30, %29
br label %32
32: ; preds = %31, %21
%33 = load i32, i32* %2
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i32, i32* %2
%37 = call i32 @close(i32 %36)
br label %38
38: ; preds = %35, %32
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [11 x i8] c"LIKWIDLOCK\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@EACCES = global i64 0
define i32 @lock_check() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %13
6: ; preds = %0
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %7, %8
br i1 %9, label %22, label %10
10: ; preds = %6
%11 = load i64, i64* @EACCES
%12 = icmp ne i64 %7, %11
br label %22
13: ; preds = %0
%14 = call i32 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%15 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%16 = load i64, i64* %15
%17 = tail call i64 (...) @getuid()
%18 = icmp eq i64 %16, %17
%19 = icmp sgt i32 %4, 0
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = tail call i32 @close(i32 %4)
br label %22
22: ; preds = %6, %10, %20, %13
%23 = phi i1 [ %18, %20 ], [ %18, %13 ], [ %12, %10 ], [ true, %6 ]
%24 = zext i1 %23 to i32
ret i32 %24
}