#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) {
    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)
je     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   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1190 <lock_check+0x50>
lea    0xe95(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
mov    0x8(%rsp),%r14
xor    %eax,%eax
call   1030 <getuid@plt>
xor    %ebp,%ebp
cmp    %rax,%r14
sete   %bpl
test   %ebx,%ebx
jne    11bb <lock_check+0x7b>
jmp    11c2 <lock_check+0x82>
mov    0x2e39(%rip),%rax
mov    (%rax),%rax
mov    0x2e1f(%rip),%rcx
mov    $0x1,%ebp
cmp    (%rcx),%rax
je     11bb <lock_check+0x7b>
mov    0x2e2e(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%rax
setne  %bpl
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
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 ne 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 %14
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
%13 = zext i1 %12 to i32
br label %22
14: ; preds = %0
%15 = call i32 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%16 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%17 = load i64, i64* %16
%18 = tail call i64 (...) @getuid()
%19 = icmp eq i64 %17, %18
%20 = zext i1 %19 to i32
%21 = icmp eq i32 %4, 0
br i1 %21, label %25, label %22
22: ; preds = %6, %10, %14
%23 = phi i32 [ %20, %14 ], [ %13, %10 ], [ 1, %6 ]
%24 = tail call i32 @close(i32 %4)
br label %25
25: ; preds = %22, %14
%26 = phi i32 [ %23, %22 ], [ %20, %14 ]
ret i32 %26
}