#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 scalar_t__ pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, scalar_t__);
scalar_t__ getpid();
scalar_t__ singleton_read_and_write_lock();
scalar_t__ singleton_validate_lock(scalar_t__);
extern int singleton_check() {
  pid_t lock = singleton_read_and_write_lock();
  pid_t me = getpid();
  if (lock == 0 || lock == me) {
    return 0;
  } else {
    if (singleton_validate_lock(lock)) {
      FILE *lock_file = fopen("/var/pushr/.lock", "w");
      fprintf(lock_file, "%d", lock);
      fclose(lock_file);
      return 1;
    } else {
      return 0;
    }
  }
}


x86 assembly (clang, -O0):
<singleton_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1080 <singleton_read_and_write_lock@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1197 <singleton_check+0x37>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11a3 <singleton_check+0x43>
movl   $0x0,-0x4(%rbp)
jmp    11fd <singleton_check+0x9d>
mov    -0x10(%rbp),%rdi
call   1030 <singleton_validate_lock@plt>
cmp    $0x0,%rax
je     11f6 <singleton_check+0x96>
lea    0xe43(%rip),%rdi
lea    0xe4d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe37(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    11fd <singleton_check+0x9d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<singleton_check>:
push   %rbp
push   %rbx
push   %rax
xor    %ebp,%ebp
xor    %eax,%eax
call   1080 <singleton_read_and_write_lock@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
test   %rbx,%rbx
je     11c2 <singleton_check+0x62>
cmp    %rax,%rbx
je     11c2 <singleton_check+0x62>
mov    %rbx,%rdi
call   1030 <singleton_validate_lock@plt>
test   %rax,%rax
je     11c2 <singleton_check+0x62>
lea    0xe6c(%rip),%rdi
lea    0xe76(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbp
lea    0xe69(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/var/pushr/.lock\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @singleton_check() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32*
%5 = call i64 (...) @singleton_read_and_write_lock()
store i64 %5, i64* %2
%6 = call i64 (...) @getpid()
store i64 %6, i64* %3
%7 = load i64, i64* %2
%8 = icmp eq i64 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %0
%10 = load i64, i64* %2
%11 = load i64, i64* %3
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %14
13: ; preds = %9, %0
store i32 0, i32* %1
br label %26
14: ; preds = %9
%15 = load i64, i64* %2
%16 = call i64 @singleton_validate_lock(i64 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %4
%20 = load i32*, i32** %4
%21 = load i64, i64* %2
%22 = call i32 @fprintf(i32* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i64 %21)
%23 = load i32*, i32** %4
%24 = call i32 @fclose(i32* %23)
store i32 1, i32* %1
br label %26
25: ; preds = %14
store i32 0, i32* %1
br label %26
26: ; preds = %25, %18, %13
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@.str = [17 x i8] c"/var/pushr/.lock\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @singleton_check() {
%1 = tail call i64 (...) @singleton_read_and_write_lock()
%2 = tail call i64 (...) @getpid()
%3 = icmp eq i64 %1, 0
%4 = icmp eq i64 %1, %2
%5 = select i1 %3, i1 true, i1 %4
br i1 %5, label %13, label %6
6: ; preds = %0
%7 = tail call i64 @singleton_validate_lock(i64 %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @fprintf(i32* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i64 %1)
%12 = tail call i32 @fclose(i32* %10)
br label %13
13: ; preds = %6, %0, %9
%14 = phi i32 [ 1, %9 ], [ 0, %0 ], [ 0, %6 ]
ret i32 %14
}