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

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ atoi(char *);
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ strlen(char *);
extern pid_t singleton_get_lock() {
  FILE *lock = fopen("/var/pushr/.lock", "r");
  char buffer[10];
  if (lock == NULL) {
    if (errno == ENOENT) {
      return 0;
    }
    return -1;
  }
  (void)fgets(buffer, 10, lock);
  fclose(lock);
  if (strlen(buffer) > 0) {
    return (pid_t)atoi(buffer);
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<singleton_get_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rdi
lea    0xeab(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ac <singleton_get_lock+0x5c>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%rax
jne    11a0 <singleton_get_lock+0x50>
movl   $0x0,-0x4(%rbp)
jmp    11f2 <singleton_get_lock+0xa2>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <singleton_get_lock+0xa2>
lea    -0x1a(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xa,%esi
call   1050 <fgets@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x1a(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jle    11eb <singleton_get_lock+0x9b>
lea    -0x1a(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x4(%rbp)
jmp    11f2 <singleton_get_lock+0xa2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<singleton_get_lock>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xea2(%rip),%rdi
lea    0xeac(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11a8 <singleton_get_lock+0x58>
mov    %rax,%rbx
lea    0xe(%rsp),%r14
mov    %r14,%rdi
mov    $0xa,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
jle    11c5 <singleton_get_lock+0x75>
lea    0xe(%rsp),%rdi
call   1070 <atoi@plt>
jmp    11c7 <singleton_get_lock+0x77>
mov    0x2e31(%rip),%rax
mov    (%rax),%rcx
mov    0x2e17(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
neg    %eax
jmp    11c7 <singleton_get_lock+0x77>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/var/pushr/.lock\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i64 0
@ENOENT = global i64 0
define i32 @singleton_get_lock() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [10 x i8]
%4 = 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* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %13
7: ; preds = %0
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOENT
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %12
11: ; preds = %7
store i32 0, i32* %1
br label %26
12: ; preds = %7
store i32 -1, i32* %1
br label %26
13: ; preds = %0
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%15 = load i32*, i32** %2
%16 = call i32 @fgets(i8* %14, i32 10, i32* %15)
%17 = load i32*, i32** %2
%18 = call i32 @fclose(i32* %17)
%19 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%20 = call i64 @strlen(i8* %19)
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %13
%23 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %1
br label %26
25: ; preds = %13
store i32 0, i32* %1
br label %26
26: ; preds = %25, %22, %12, %11
%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"r\00"
@errno = global i64 0
@ENOENT = global i64 0
define i32 @singleton_get_lock() {
%1 = alloca [10 x i8]
%2 = 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))
%3 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%4 = icmp eq i32* %2, null
br i1 %4, label %5, label %10
5: ; preds = %0
%6 = load i64, i64* @errno
%7 = load i64, i64* @ENOENT
%8 = icmp ne i64 %6, %7
%9 = sext i1 %8 to i32
br label %17
10: ; preds = %0
%11 = call i32 @fgets(i8* %3, i32 10, i32* %2)
%12 = call i32 @fclose(i32* %2)
%13 = call i64 @strlen(i8* %3)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @atoi(i8* %3)
br label %17
17: ; preds = %10, %5, %15
%18 = phi i32 [ %16, %15 ], [ %9, %5 ], [ 0, %10 ]
ret i32 %18
}