#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 */

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EAGAIN;
int /*<<< orphan*/ LOG_ERR;
int O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ftruncate(int, int /*<<< orphan*/);
scalar_t__ getpid();
scalar_t__ lockfile(int);
int open(char const *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, long);
int /*<<< orphan*/ strerror(scalar_t__);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *,
                          int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
bool already_running(const char *pid_file) {
  int fd;
  char buf[16];
  fd = open(pid_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (fd < 0) {
    syslog(LOG_ERR, "Can't open %s, %s", pid_file, strerror(errno));
    exit(1);
  }
  if (lockfile(fd) < 0) {
    if (errno == EACCES || errno == EAGAIN) {
      close(fd);
      return true;
    }
    syslog(LOG_ERR, "Can't lock %s, %s", pid_file, strerror(errno));
    exit(1);
  }
  ftruncate(fd, 0);
  sprintf(buf, "%ld", (long)getpid());
  write(fd, buf, strlen(buf));
  return false;
}


x86 assembly (clang, -O0):
<already_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
mov    0x2df6(%rip),%rax
or     (%rax),%edx
mov    0x2dcd(%rip),%rax
or     (%rax),%edx
mov    0x2dbc(%rip),%rax
or     (%rax),%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    124a <already_running+0x9a>
mov    0x2d89(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
lea    0xdc5(%rip),%rsi
call   1080 <syslog@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1090 <lockfile@plt>
cmp    $0x0,%rax
jge    12e6 <already_running+0x136>
mov    0x2d5d(%rip),%rax
mov    (%rax),%rax
mov    0x2d63(%rip),%rcx
cmp    (%rcx),%rax
je     1290 <already_running+0xe0>
mov    0x2d43(%rip),%rax
mov    (%rax),%rax
mov    0x2d09(%rip),%rcx
cmp    (%rcx),%rax
jne    12a4 <already_running+0xf4>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    133e <already_running+0x18e>
mov    0x2ced(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d01(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %eax,%ecx
lea    0xd3b(%rip),%rsi
call   1080 <syslog@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x68(%rbp),%rdi
mov    %rax,%rdx
lea    0xd17(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x5c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<already_running>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e21(%rip),%rax
mov    0x2e32(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd7(%rip),%rax
mov    0x2e00(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dd5(%rip),%rax
or     (%rax),%edx
mov    %rdi,%rbx
mov    0x2dc1(%rip),%rax
or     (%rax),%edx
call   10a0 <open@plt>
test   %eax,%eax
js     1285 <already_running+0xd5>
mov    %eax,%ebp
mov    %eax,%edi
call   1090 <lockfile@plt>
test   %rax,%rax
js     124a <already_running+0x9a>
xor    %r14d,%r14d
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdfe(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rax,%rdx
call   10b0 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1279 <already_running+0xc9>
mov    0x2d6f(%rip),%rax
mov    (%rax),%rdi
mov    0x2d75(%rip),%rax
cmp    (%rax),%rdi
je     126c <already_running+0xbc>
mov    0x2d29(%rip),%rax
cmp    (%rax),%rdi
jne    12a6 <already_running+0xf6>
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d0c(%rip),%rax
mov    (%rax),%ebp
mov    0x2d2b(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
lea    0xd5c(%rip),%rsi
jmp    12bb <already_running+0x10b>
mov    0x2ceb(%rip),%rax
mov    (%rax),%ebp
call   10d0 <strerror@plt>
lea    0xd57(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1080 <syslog@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@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
@LOG_ERR = global i32 0
@.str = [18 x i8] c"Can't open %s, %s\00"
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.1 = [18 x i8] c"Can't lock %s, %s\00"
@.str.2 = [4 x i8] c"%ld\00"
define i32 @already_running(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [16 x i8]
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRUSR
%11 = load i32, i32* @S_IWUSR
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRGRP
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IROTH
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %6, i32 %9, i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %1
%21 = load i32, i32* @LOG_ERR
%22 = load i8*, i8** %3
%23 = load i64, i64* @errno
%24 = call i32 @strerror(i64 %23)
%25 = call i32 @syslog(i32 %21, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %22, i32 %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %1
%28 = load i32, i32* %4
%29 = call i64 @lockfile(i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %49
31: ; preds = %27
%32 = load i64, i64* @errno
%33 = load i64, i64* @EACCES
%34 = icmp eq i64 %32, %33
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = load i64, i64* @errno
%37 = load i64, i64* @EAGAIN
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %35, %31
%40 = load i32, i32* %4
%41 = call i32 @close(i32 %40)
store i32 1, i32* %2
br label %60
42: ; preds = %35
%43 = load i32, i32* @LOG_ERR
%44 = load i8*, i8** %3
%45 = load i64, i64* @errno
%46 = call i32 @strerror(i64 %45)
%47 = call i32 @syslog(i32 %43, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %44, i32 %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %27
%50 = load i32, i32* %4
%51 = call i32 @ftruncate(i32 %50, i32 0)
%52 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%53 = call i64 (...) @getpid()
%54 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 %53)
%55 = load i32, i32* %4
%56 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%57 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%58 = call i32 @strlen(i8* %57)
%59 = call i32 @write(i32 %55, i8* %56, i32 %58)
store i32 0, i32* %2
br label %60
60: ; preds = %49, %39
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
@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
@LOG_ERR = global i32 0
@.str = [18 x i8] c"Can't open %s, %s\00"
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.1 = [18 x i8] c"Can't lock %s, %s\00"
@.str.2 = [4 x i8] c"%ld\00"
define i32 @already_running(i8* %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %8, %7
%10 = load i32, i32* @S_IRGRP
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IROTH
%13 = or i32 %11, %12
%14 = tail call i32 @open(i8* %0, i32 %6, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %1
%17 = load i32, i32* @LOG_ERR
%18 = load i64, i64* @errno
%19 = tail call i32 @strerror(i64 %18)
%20 = tail call i32 @syslog(i32 %17, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %19)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %1
%23 = tail call i64 @lockfile(i32 %14)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %39
25: ; preds = %22
%26 = load i64, i64* @errno
%27 = load i64, i64* @EACCES
%28 = icmp eq i64 %26, %27
%29 = load i64, i64* @EAGAIN
%30 = icmp eq i64 %26, %29
%31 = select i1 %28, i1 true, i1 %30
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = tail call i32 @close(i32 %14)
br label %45
34: ; preds = %25
%35 = load i32, i32* @LOG_ERR
%36 = tail call i32 @strerror(i64 %26)
%37 = tail call i32 @syslog(i32 %35, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %36)
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %22
%40 = tail call i32 @ftruncate(i32 %14, i32 0)
%41 = tail call i64 (...) @getpid()
%42 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 %41)
%43 = call i32 @strlen(i8* %3)
%44 = call i32 @write(i32 %14, i8* %3, i32 %43)
br label %45
45: ; preds = %39, %32
%46 = phi i32 [ 1, %32 ], [ 0, %39 ]
ret i32 %46
}