#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__ EWOULDBLOCK;
int LOCK_EX;
int LOCK_NB;
int /*<<< orphan*/ LOG_ERR;
int O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ flock(int, int);
int /*<<< orphan*/ ftruncate(int, int /*<<< orphan*/);
scalar_t__ getpid();
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, long);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int already_running(void) {
  int fd;
  char buf[100];
  fd = open("/var/run/daemon.pid", O_RDWR | O_CREAT,
            (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
  if (fd < 0) {
    syslog(LOG_ERR, "open LOCK failed[%ld][%m]", (long)getpid());
    exit(1);
  }
  if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
    if (errno == EWOULDBLOCK) {
      syslog(LOG_ERR, "system is already running");
      return 1;
    }
  }
  ftruncate(fd, 0);
  sprintf(buf, "%ld", (long)getpid());
  write(fd, buf, strlen(buf));
  return 0;
}


x86 assembly (clang, -O0):
<already_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    0x2e4d(%rip),%rax
or     (%rax),%esi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
mov    0x2e13(%rip),%rax
or     (%rax),%edx
mov    0x2dea(%rip),%rax
or     (%rax),%edx
mov    0x2dd9(%rip),%rax
or     (%rax),%edx
lea    0xe28(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    121b <already_running+0x8b>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x74(%rbp),%edi
mov    %rax,%rdx
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
mov    0x2d5a(%rip),%rax
or     (%rax),%esi
call   1090 <flock@plt>
cmp    $0x0,%rax
jge    1281 <already_running+0xf1>
mov    0x2d7a(%rip),%rax
mov    (%rax),%rax
mov    0x2d68(%rip),%rcx
cmp    (%rcx),%rax
jne    127c <already_running+0xec>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e5 <already_running+0x155>
jmp    1281 <already_running+0xf1>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
lea    -0x70(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x90(%rbp),%rdi
mov    %rax,%rdx
lea    0xd9a(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x84(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x80(%rbp)
lea    -0x70(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x84(%rbp),%edi
mov    -0x80(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<already_running>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    0x2e41(%rip),%rax
mov    0x2e52(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2def(%rip),%rax
mov    0x2e18(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2ded(%rip),%rax
or     (%rax),%edx
mov    0x2ddc(%rip),%rax
or     (%rax),%edx
lea    0xe2b(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     127f <already_running+0xef>
mov    %eax,%ebx
mov    0x2ded(%rip),%rax
mov    0x2d96(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebx,%edi
call   1090 <flock@plt>
test   %rax,%rax
jns    1237 <already_running+0xa7>
mov    0x2db7(%rip),%rax
mov    (%rax),%rax
mov    0x2da5(%rip),%rcx
cmp    (%rcx),%rax
jne    1237 <already_running+0xa7>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
xor    %eax,%eax
call   1070 <syslog@plt>
mov    $0x1,%r14d
jmp    1273 <already_running+0xe3>
xor    %r14d,%r14d
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdf7(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %rax,%rdx
call   10a0 <sprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r14d,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d0a(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd7e(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <syslog@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"/var/run/daemon.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 = [26 x i8] c"open LOCK failed[%ld][%m]\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@.str.2 = [26 x i8] c"system is already running\00"
@.str.3 = [4 x i8] c"%ld\00"
define i32 @already_running() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [100 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 ([20 x i8], [20 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 %22
17: ; preds = %0
%18 = load i32, i32* @LOG_ERR
%19 = call i64 (...) @getpid()
%20 = call i32 (i32, i8*, ...) @syslog(i32 %18, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i64 %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %0
%23 = load i32, i32* %2
%24 = load i32, i32* @LOCK_EX
%25 = load i32, i32* @LOCK_NB
%26 = or i32 %24, %25
%27 = call i64 @flock(i32 %23, i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %22
%30 = load i64, i64* @errno
%31 = load i64, i64* @EWOULDBLOCK
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32, i32* @LOG_ERR
%35 = call i32 (i32, i8*, ...) @syslog(i32 %34, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %48
36: ; preds = %29
br label %37
37: ; preds = %36, %22
%38 = load i32, i32* %2
%39 = call i32 @ftruncate(i32 %38, i32 0)
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%41 = call i64 (...) @getpid()
%42 = call i32 @sprintf(i8* %40, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 %41)
%43 = load i32, i32* %2
%44 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%45 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%46 = call i32 @strlen(i8* %45)
%47 = call i32 @write(i32 %43, i8* %44, i32 %46)
store i32 0, i32* %1
br label %48
48: ; preds = %37, %33
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
@.str = [20 x i8] c"/var/run/daemon.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 = [26 x i8] c"open LOCK failed[%ld][%m]\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@.str.2 = [26 x i8] c"system is already running\00"
@.str.3 = [4 x i8] c"%ld\00"
define i32 @already_running() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 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 ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %0
%16 = load i32, i32* @LOG_ERR
%17 = tail call i64 (...) @getpid()
%18 = tail call i32 (i32, i8*, ...) @syslog(i32 %16, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i64 %17)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %0
%21 = load i32, i32* @LOCK_EX
%22 = load i32, i32* @LOCK_NB
%23 = or i32 %22, %21
%24 = tail call i64 @flock(i32 %13, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %20
%27 = load i64, i64* @errno
%28 = load i64, i64* @EWOULDBLOCK
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* @LOG_ERR
%32 = tail call i32 (i32, i8*, ...) @syslog(i32 %31, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %39
33: ; preds = %26, %20
%34 = tail call i32 @ftruncate(i32 %13, i32 0)
%35 = tail call i64 (...) @getpid()
%36 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 %35)
%37 = call i32 @strlen(i8* %2)
%38 = call i32 @write(i32 %13, i8* %2, i32 %37)
br label %39
39: ; preds = %33, %30
%40 = phi i32 [ 1, %30 ], [ 0, %33 ]
ret i32 %40
}