#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 /*<<< orphan*/ LOG_WARNING;
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);
scalar_t__ ftruncate(int, int /*<<< orphan*/);
scalar_t__ getpid();
scalar_t__ lockfile(int);
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, long);
scalar_t__ strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
scalar_t__ write(int, char *, scalar_t__);
int already_running(void) {
  int fd;
  char buf[32];
  fd = open("/var/run/ulccmmd.pid", O_RDWR | O_CREAT,
            (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
  if (fd < 0) {
    syslog(LOG_ERR, "cannot open %s: %m", "/var/run/ulccmmd.pid");
    exit(1);
  }
  if (lockfile(fd) < 0) {
    if (errno == EACCES || errno == EAGAIN) {
      close(fd);
      return (1);
    }
    syslog(LOG_ERR, "cannot lock %s: %m", "/var/run/ulccmmd.pid");
    exit(1);
  }
  if (ftruncate(fd, 0) < 0) {
    syslog(LOG_WARNING, "cannont truncate the lock file to zero: %m");
  }
  sprintf(buf, "%ld", (long)getpid());
  if (write(fd, buf, strlen(buf) + 1) < 0) {
    syslog(LOG_WARNING, "cannot write process id into the lock file: %m");
  }
  return 0;
}


x86 assembly (clang, -O0):
<already_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
mov    0x2e0e(%rip),%rax
or     (%rax),%edx
mov    0x2ddd(%rip),%rax
or     (%rax),%edx
mov    0x2dcc(%rip),%rax
or     (%rax),%edx
lea    0xe1b(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    121f <already_running+0x7f>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
lea    0xdf2(%rip),%rdx
mov    $0x0,%al
call   1080 <syslog@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1090 <lockfile@plt>
cmp    $0x0,%rax
jge    12a1 <already_running+0x101>
mov    0x2d80(%rip),%rax
mov    (%rax),%rax
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
je     1265 <already_running+0xc5>
mov    0x2d66(%rip),%rax
mov    (%rax),%rax
mov    0x2d2c(%rip),%rcx
cmp    (%rcx),%rax
jne    1279 <already_running+0xd9>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1340 <already_running+0x1a0>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
lea    0xd9f(%rip),%rsi
lea    0xd70(%rip),%rdx
mov    $0x0,%al
call   1080 <syslog@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
cmp    $0x0,%rax
jge    12cc <already_running+0x12c>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
lea    0xd76(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rdx
lea    0xd7d(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1339 <already_running+0x199>
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
lea    0xd38(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<already_running>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e31(%rip),%rax
mov    0x2e42(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2ddf(%rip),%rax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2ddd(%rip),%rax
or     (%rax),%edx
mov    0x2dcc(%rip),%rax
or     (%rax),%edx
lea    0xe1b(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     12b6 <already_running+0x116>
mov    %eax,%ebx
mov    %eax,%edi
call   1090 <lockfile@plt>
test   %rax,%rax
js     127b <already_running+0xdb>
xor    %r14d,%r14d
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
test   %rax,%rax
jns    1228 <already_running+0x88>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
xor    %eax,%eax
call   1080 <syslog@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe30(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %rax,%rdx
call   10b0 <sprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <write@plt>
test   %rax,%rax
jns    12aa <already_running+0x10a>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1080 <syslog@plt>
jmp    12aa <already_running+0x10a>
mov    0x2d36(%rip),%rax
mov    (%rax),%rax
mov    0x2d3c(%rip),%rcx
cmp    (%rcx),%rax
je     129d <already_running+0xfd>
mov    0x2cf0(%rip),%rcx
cmp    (%rcx),%rax
jne    12c8 <already_running+0x128>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
lea    0xd4f(%rip),%rsi
jmp    12d8 <already_running+0x138>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
lea    0xd50(%rip),%rsi
lea    0xd21(%rip),%rdx
xor    %eax,%eax
call   1080 <syslog@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

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

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