#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*/ SEEK_SET;
scalar_t__ atoi(char *);
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int,
                           int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/, int /*<<< orphan*/,
                           int /*<<< orphan*/);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int getpid();
pid_t singleton_read_and_write_lock() {
  FILE *lock = fopen("/var/pushr/.lock", "r+");
  char buffer[10];
  pid_t read_lock;
  pid_t me = getpid();
  if (lock == NULL) {
    if (errno == ENOENT) {
      read_lock = 0;
      lock = fopen("/var/pushr/.lock", "w");
      if (lock == NULL) {
        return 0;
      }
    }
  } else {
    (void)fgets(buffer, 10, lock);
    read_lock = (pid_t)atoi(buffer);
  }
  fseek(lock, 0L, SEEK_SET);
  fprintf(lock, "%d%c%c%c%c%c", me, 0, 0, 0, 0, 0);
  fclose(lock);
  return read_lock;
}


x86 assembly (clang, -O0):
<singleton_read_and_write_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xe81(%rip),%rdi
lea    0xe8b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11fd <singleton_read_and_write_lock+0x8d>
mov    0x2e35(%rip),%rax
mov    (%rax),%rax
mov    0x2e13(%rip),%rcx
cmp    (%rcx),%rax
jne    11f8 <singleton_read_and_write_lock+0x88>
movl   $0x0,-0x20(%rbp)
lea    0xe34(%rip),%rdi
lea    0xe41(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f3 <singleton_read_and_write_lock+0x83>
movl   $0x0,-0x4(%rbp)
jmp    126b <singleton_read_and_write_lock+0xfb>
jmp    11f8 <singleton_read_and_write_lock+0x88>
jmp    121b <singleton_read_and_write_lock+0xab>
lea    -0x1a(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xa,%esi
call   1050 <fgets@plt>
lea    -0x1a(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2db2(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xdd7(%rip),%rsi
xor    %r9d,%r9d
mov    %r9d,%ecx
mov    %r9d,%r8d
movl   $0x0,(%rsp)
movl   $0x0,0x8(%rsp)
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<singleton_read_and_write_lock>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe81(%rip),%rdi
lea    0xe8b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r14d
test   %rbx,%rbx
je     11bf <singleton_read_and_write_lock+0x4f>
lea    0x6(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xa,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
mov    %rbp,%rdi
call   1090 <atoi@plt>
mov    %rax,%rbp
jmp    11f6 <singleton_read_and_write_lock+0x86>
mov    0x2e1a(%rip),%rax
mov    (%rax),%rax
mov    0x2df8(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%rax
jne    11f4 <singleton_read_and_write_lock+0x84>
lea    0xe22(%rip),%rdi
lea    0xe2f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11f6 <singleton_read_and_write_lock+0x86>
jmp    1233 <singleton_read_and_write_lock+0xc3>
xor    %ebx,%ebx
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
lea    0xe06(%rip),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   $0x0
push   $0x0
call   1060 <fprintf@plt>
add    $0x10,%rsp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/var/pushr/.lock\00"
@.str.1 = [3 x i8] c"r+\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [2 x i8] c"w\00"
@SEEK_SET = global i32 0
@.str.3 = [13 x i8] c"%d%c%c%c%c%c\00"
define i32 @singleton_read_and_write_lock() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [10 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = call i32 (...) @getpid()
store i32 %7, i32* %5
%8 = load i32*, i32** %2
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %21
10: ; preds = %0
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOENT
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %20
14: ; preds = %10
store i32 0, i32* %4
%15 = 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.2, i64 0, i64 0))
store i32* %15, i32** %2
%16 = load i32*, i32** %2
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 0, i32* %1
br label %38
19: ; preds = %14
br label %20
20: ; preds = %19, %10
br label %28
21: ; preds = %0
%22 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%23 = load i32*, i32** %2
%24 = call i32 @fgets(i8* %22, i32 10, i32* %23)
%25 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%26 = call i64 @atoi(i8* %25)
%27 = trunc i64 %26 to i32
store i32 %27, i32* %4
br label %28
28: ; preds = %21, %20
%29 = load i32*, i32** %2
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @fseek(i32* %29, i64 0, i32 %30)
%32 = load i32*, i32** %2
%33 = load i32, i32* %5
%34 = call i32 @fprintf(i32* %32, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %33, i32 0, i32 0, i32 0, i32 0, i32 0)
%35 = load i32*, i32** %2
%36 = call i32 @fclose(i32* %35)
%37 = load i32, i32* %4
store i32 %37, i32* %1
br label %38
38: ; preds = %28, %18
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str = [17 x i8] c"/var/pushr/.lock\00"
@.str.1 = [3 x i8] c"r+\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [2 x i8] c"w\00"
@SEEK_SET = global i32 0
@.str.3 = [13 x i8] c"%d%c%c%c%c%c\00"
define i32 @singleton_read_and_write_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 ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%3 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%4 = tail call i32 (...) @getpid()
%5 = icmp eq i32* %2, null
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 %10, label %17
10: ; preds = %6
%11 = 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.2, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %24, label %17
13: ; preds = %0
%14 = call i32 @fgets(i8* %3, i32 10, i32* %2)
%15 = call i64 @atoi(i8* %3)
%16 = trunc i64 %15 to i32
br label %17
17: ; preds = %6, %10, %13
%18 = phi i32* [ %11, %10 ], [ null, %6 ], [ %2, %13 ]
%19 = phi i32 [ 0, %10 ], [ undef, %6 ], [ %16, %13 ]
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @fseek(i32* %18, i64 0, i32 %20)
%22 = call i32 @fprintf(i32* %18, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %4, i32 0, i32 0, i32 0, i32 0, i32 0)
%23 = call i32 @fclose(i32* %18)
br label %24
24: ; preds = %10, %17
%25 = phi i32 [ %19, %17 ], [ 0, %10 ]
ret i32 %25
}