#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 flock {
  scalar_t__ l_len;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
};
typedef int /*<<< orphan*/ pid_buff;

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EAGAIN;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
scalar_t__ errno;
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
scalar_t__ ftruncate(int, int /*<<< orphan*/);
scalar_t__ getpid();
int open(char *, int, int);
int snprintf(char *, int, char *, long);
int write(int, char *, int);
int already_running(void) {
  static char pid_buff[32];
  int lockfile =
      open("/var/run/"
           "remotelogind"
           ".pid",
           O_RDWR | O_CREAT, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
  if (lockfile < 0) {
    return -1;
  }
  struct flock flock;
  flock.l_type = F_WRLCK;
  flock.l_whence = SEEK_SET;
  flock.l_start = 0;
  flock.l_len = 0;
  if (fcntl(lockfile, F_SETLK, &flock) < 0) {
    if (errno == EACCES || errno == EAGAIN) {
      return 1;
    }
    return -1;
  }
  if (ftruncate(lockfile, 0) < 0) {
    return -1;
  }
  int len = snprintf(pid_buff, sizeof(pid_buff), "%ld\n", (long)getpid());
  if (write(lockfile, pid_buff, len) != len) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<already_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e80(%rip),%rax
or     (%rax),%esi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    0x2e46(%rip),%rax
or     (%rax),%edx
mov    0x2e15(%rip),%rax
or     (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
lea    0xe5b(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c3 <already_running+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <already_running+0x169>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1256 <already_running+0xf6>
mov    0x2da7(%rip),%rax
mov    (%rax),%rax
mov    0x2dad(%rip),%rcx
cmp    (%rcx),%rax
je     123e <already_running+0xde>
mov    0x2d8d(%rip),%rax
mov    (%rax),%rax
mov    0x2d4b(%rip),%rcx
cmp    (%rcx),%rax
jne    124a <already_running+0xea>
movl   $0x1,-0x4(%rbp)
jmp    12c9 <already_running+0x169>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <already_running+0x169>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1070 <ftruncate@plt>
cmp    $0x0,%rax
jge    1276 <already_running+0x116>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <already_running+0x169>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %rax,%rcx
lea    0x2dd9(%rip),%rdi
mov    $0x20,%esi
lea    0xd87(%rip),%rdx
call   1060 <snprintf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x24(%rbp),%edx
lea    0x2db8(%rip),%rsi
call   1040 <write@plt>
cmp    -0x24(%rbp),%eax
je     12c2 <already_running+0x162>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <already_running+0x169>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<already_running>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e71(%rip),%rax
mov    0x2e82(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e17(%rip),%rax
mov    0x2e48(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e15(%rip),%rax
or     (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
lea    0xe5b(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     123b <already_running+0xdb>
mov    %eax,%ebx
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
test   %rax,%rax
js     1242 <already_running+0xe2>
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <ftruncate@plt>
test   %rax,%rax
js     123b <already_running+0xdb>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0x2e57(%rip),%r14
lea    0xe0a(%rip),%rdx
mov    %r14,%rdi
mov    $0x20,%esi
mov    %rax,%rcx
call   1060 <snprintf@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ebp,%ecx
setne  %al
neg    %eax
jmp    1270 <already_running+0x110>
mov    $0xffffffff,%eax
jmp    1270 <already_running+0x110>
mov    0x2d6f(%rip),%rax
mov    (%rax),%rax
mov    0x2d75(%rip),%rcx
cmp    (%rcx),%rax
sete   %cl
mov    0x2d20(%rip),%rdx
cmp    (%rdx),%rax
sete   %al
or     %cl,%al
movzbl %al,%eax
add    %eax,%eax
add    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i64, i32, i32 }
@already_running.pid_buff = internal global [32 x i8] zeroinitializer
@.str = [26 x i8] c"/var/run/remotelogind.pid\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.1 = [5 x i8] c"%ld\0A\00"
define i32 @already_running() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.flock
%4 = alloca i32
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IROTH
%14 = or i32 %12, %13
%15 = call i32 @open(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %14)
store i32 %15, i32* %2
%16 = load i32, i32* %2
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %0
store i32 -1, i32* %1
br label %55
19: ; preds = %0
%20 = load i32, i32* @F_WRLCK
%21 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 3
store i32 %20, i32* %21
%22 = load i32, i32* @SEEK_SET
%23 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 2
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 1
store i64 0, i64* %24
%25 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 0
store i64 0, i64* %25
%26 = load i32, i32* %2
%27 = load i32, i32* @F_SETLK
%28 = call i64 @fcntl(i32 %26, i32 %27, %struct.flock* %3)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %40
30: ; preds = %19
%31 = load i64, i64* @errno
%32 = load i64, i64* @EACCES
%33 = icmp eq i64 %31, %32
br i1 %33, label %38, label %34
34: ; preds = %30
%35 = load i64, i64* @errno
%36 = load i64, i64* @EAGAIN
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %34, %30
store i32 1, i32* %1
br label %55
39: ; preds = %34
store i32 -1, i32* %1
br label %55
40: ; preds = %19
%41 = load i32, i32* %2
%42 = call i64 @ftruncate(i32 %41, i32 0)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %40
store i32 -1, i32* %1
br label %55
45: ; preds = %40
%46 = call i64 (...) @getpid()
%47 = call i32 @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @already_running.pid_buff, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %46)
store i32 %47, i32* %4
%48 = load i32, i32* %2
%49 = load i32, i32* %4
%50 = call i32 @write(i32 %48, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @already_running.pid_buff, i64 0, i64 0), i32 %49)
%51 = load i32, i32* %4
%52 = icmp ne i32 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %45
store i32 -1, i32* %1
br label %55
54: ; preds = %45
store i32 0, i32* %1
br label %55
55: ; preds = %54, %53, %44, %39, %38, %18
%56 = load i32, i32* %1
ret i32 %56
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i32, i32 }
@already_running.pid_buff = internal global [32 x i8] zeroinitializer
@.str = [26 x i8] c"/var/run/remotelogind.pid\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.1 = [5 x i8] c"%ld\0A\00"
define i32 @already_running() {
%1 = alloca %struct.flock
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = load i32, i32* @S_IRUSR
%6 = load i32, i32* @S_IWUSR
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IRGRP
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IROTH
%11 = or i32 %9, %10
%12 = tail call i32 @open(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %43, label %14
14: ; preds = %0
%15 = bitcast %struct.flock* %1 to i8*
%16 = load i32, i32* @F_WRLCK
%17 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 3
store i32 %16, i32* %17
%18 = load i32, i32* @SEEK_SET
%19 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 2
store i32 %18, i32* %19
%20 = bitcast %struct.flock* %1 to i8*
%21 = load i32, i32* @F_SETLK
%22 = call i64 @fcntl(i32 %12, i32 %21, %struct.flock* %1)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %14
%25 = load i64, i64* @errno
%26 = load i64, i64* @EACCES
%27 = icmp eq i64 %25, %26
%28 = load i64, i64* @EAGAIN
%29 = icmp eq i64 %25, %28
%30 = select i1 %27, i1 true, i1 %29
%31 = select i1 %30, i32 1, i32 -1
br label %41
32: ; preds = %14
%33 = call i64 @ftruncate(i32 %12, i32 0)
%34 = icmp slt i64 %33, 0
br i1 %34, label %41, label %35
35: ; preds = %32
%36 = call i64 (...) @getpid()
%37 = call i32 @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @already_running.pid_buff, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %36)
%38 = call i32 @write(i32 %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @already_running.pid_buff, i64 0, i64 0), i32 %37)
%39 = icmp ne i32 %38, %37
%40 = sext i1 %39 to i32
br label %41
41: ; preds = %24, %32, %35
%42 = phi i32 [ %40, %35 ], [ -1, %32 ], [ %31, %24 ]
br label %43
43: ; preds = %0, %41
%44 = phi i32 [ %42, %41 ], [ -1, %0 ]
ret i32 %44
}