#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 *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, long);
int /*<<< orphan*/ strerror(scalar_t__);
scalar_t__ strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int already_running(void) {
  int fd = 0;
  char buf[16] = {0};
  fd = open("/var/run/daemon.pid", O_RDWR | O_CREAT,
            (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
  if (fd < 0) {
    syslog(LOG_ERR, "can't open %s: %s", "/var/run/daemon.pid",
           strerror(errno));
    exit(1);
  }
  if (lockfile(fd) < 0) {
    if (errno == EACCES || errno == EAGAIN) {
      syslog(LOG_ERR, "daemon is already_running");
      close(fd);
      return 1;
    }
    syslog(LOG_ERR, "can't lock %s: %s", "/var/run/daemon.pid",
           strerror(errno));
    exit(1);
  }
  ftruncate(fd, 0);
  sprintf(buf, "%ld", (long)getpid());
  write(fd, buf, strlen(buf) + 1);
  syslog(LOG_ERR, "write /var/run/daemon.pid: %s", buf);
  return 0;
}


x86 assembly (clang, -O0):
<already_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    0x2e09(%rip),%rax
or     (%rax),%esi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
mov    0x2dd7(%rip),%rax
or     (%rax),%edx
mov    0x2dae(%rip),%rax
or     (%rax),%edx
mov    0x2d9d(%rip),%rax
or     (%rax),%edx
lea    0xde4(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    126d <already_running+0xad>
mov    0x2d63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d7f(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%ecx
lea    0xdbf(%rip),%rsi
lea    0xda4(%rip),%rdx
mov    $0x0,%al
call   1090 <syslog@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x8(%rbp),%edi
call   10a0 <lockfile@plt>
cmp    $0x0,%rax
jge    131d <already_running+0x15d>
mov    0x2d3a(%rip),%rax
mov    (%rax),%rax
mov    0x2d40(%rip),%rcx
cmp    (%rcx),%rax
je     12b3 <already_running+0xf3>
mov    0x2d20(%rip),%rax
mov    (%rax),%rax
mov    0x2ce6(%rip),%rcx
cmp    (%rcx),%rax
jne    12de <already_running+0x11e>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
lea    0xd63(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1395 <already_running+0x1d5>
mov    0x2cb3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2ccf(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%ecx
lea    0xd3b(%rip),%rsi
lea    0xcf4(%rip),%rdx
mov    $0x0,%al
call   1090 <syslog@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
lea    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rdx
lea    0xd0e(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    0x2c1e(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rdx
lea    0xccf(%rip),%rsi
mov    $0x0,%al
call   1090 <syslog@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%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
xorps  %xmm0,%xmm0
mov    0x2dd2(%rip),%rax
or     (%rax),%edx
movaps %xmm0,(%rsp)
mov    0x2dbd(%rip),%rax
or     (%rax),%edx
lea    0xe04(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     12c5 <already_running+0x115>
mov    %eax,%ebx
mov    %eax,%edi
call   1090 <lockfile@plt>
test   %rax,%rax
js     1271 <already_running+0xc1>
xor    %r14d,%r14d
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe21(%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>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1080 <syslog@plt>
jmp    12b9 <already_running+0x109>
mov    0x2d48(%rip),%rax
mov    (%rax),%rdi
mov    0x2d4e(%rip),%rax
mov    0x2d0f(%rip),%rcx
mov    (%rcx),%ebp
cmp    (%rax),%rdi
je     129c <already_running+0xec>
mov    0x2cf9(%rip),%rax
cmp    (%rax),%rdi
jne    12ef <already_running+0x13f>
lea    0xd83(%rip),%rsi
mov    %ebp,%edi
xor    %eax,%eax
call   1080 <syslog@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2ccc(%rip),%rax
mov    (%rax),%ebx
mov    0x2ceb(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
lea    0xd30(%rip),%rsi
lea    0xd15(%rip),%rdx
mov    %ebx,%edi
jmp    1304 <already_running+0x154>
call   10d0 <strerror@plt>
lea    0xd45(%rip),%rsi
lea    0xcfe(%rip),%rdx
mov    %ebp,%edi
mov    %eax,%ecx
xor    %eax,%eax
call   1080 <syslog@plt>
mov    $0x1,%edi
call   10c0 <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 = [18 x i8] c"can't open %s: %s\00"
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.2 = [26 x i8] c"daemon is already_running\00"
@.str.3 = [18 x i8] c"can't lock %s: %s\00"
@.str.4 = [4 x i8] c"%ld\00"
@.str.5 = [30 x i8] c"write /var/run/daemon.pid: %s\00"
define i32 @already_running() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [16 x i8]
store i32 0, i32* %2
%4 = bitcast [16 x i8]* %3 to i8*
%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 ([20 x i8], [20 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 %24
18: ; preds = %0
%19 = load i32, i32* @LOG_ERR
%20 = load i64, i64* @errno
%21 = call i32 @strerror(i64 %20)
%22 = call i32 (i32, i8*, ...) @syslog(i32 %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %0
%25 = load i32, i32* %2
%26 = call i64 @lockfile(i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %47
28: ; preds = %24
%29 = load i64, i64* @errno
%30 = load i64, i64* @EACCES
%31 = icmp eq i64 %29, %30
br i1 %31, label %36, label %32
32: ; preds = %28
%33 = load i64, i64* @errno
%34 = load i64, i64* @EAGAIN
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %41
36: ; preds = %32, %28
%37 = load i32, i32* @LOG_ERR
%38 = call i32 (i32, i8*, ...) @syslog(i32 %37, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
store i32 1, i32* %1
br label %62
41: ; preds = %32
%42 = load i32, i32* @LOG_ERR
%43 = load i64, i64* @errno
%44 = call i32 @strerror(i64 %43)
%45 = call i32 (i32, i8*, ...) @syslog(i32 %42, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %44)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %24
%48 = load i32, i32* %2
%49 = call i32 @ftruncate(i32 %48, i32 0)
%50 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%51 = call i64 (...) @getpid()
%52 = call i32 @sprintf(i8* %50, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i64 %51)
%53 = load i32, i32* %2
%54 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%55 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%56 = call i64 @strlen(i8* %55)
%57 = add nsw i64 %56, 1
%58 = call i32 @write(i32 %53, i8* %54, i64 %57)
%59 = load i32, i32* @LOG_ERR
%60 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%61 = call i32 (i32, i8*, ...) @syslog(i32 %59, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %60)
store i32 0, i32* %1
br label %62
62: ; preds = %47, %36
%63 = load i32, i32* %1
ret i32 %63
}

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 = [18 x i8] c"can't open %s: %s\00"
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.2 = [26 x i8] c"daemon is already_running\00"
@.str.3 = [18 x i8] c"can't lock %s: %s\00"
@.str.4 = [4 x i8] c"%ld\00"
@.str.5 = [30 x i8] c"write /var/run/daemon.pid: %s\00"
define i32 @already_running() {
%1 = alloca [16 x i8]
%2 = getelementptr inbounds [16 x i8], [16 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 %21
15: ; preds = %0
%16 = load i32, i32* @LOG_ERR
%17 = load i64, i64* @errno
%18 = tail call i32 @strerror(i64 %17)
%19 = tail call i32 (i32, i8*, ...) @syslog(i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %0
%22 = tail call i64 @lockfile(i32 %13)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %39
24: ; preds = %21
%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 = load i32, i32* @LOG_ERR
br i1 %30, label %32, label %35
32: ; preds = %24
%33 = tail call i32 (i32, i8*, ...) @syslog(i32 %31, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%34 = tail call i32 @close(i32 %13)
br label %48
35: ; preds = %24
%36 = tail call i32 @strerror(i64 %25)
%37 = tail call i32 (i32, i8*, ...) @syslog(i32 %31, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %36)
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %21
%40 = tail call i32 @ftruncate(i32 %13, i32 0)
%41 = tail call i64 (...) @getpid()
%42 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i64 %41)
%43 = call i64 @strlen(i8* %2)
%44 = add nsw i64 %43, 1
%45 = call i32 @write(i32 %13, i8* %2, i64 %44)
%46 = load i32, i32* @LOG_ERR
%47 = call i32 (i32, i8*, ...) @syslog(i32 %46, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %2)
br label %48
48: ; preds = %39, %32
%49 = phi i32 [ 1, %32 ], [ 0, %39 ]
ret i32 %49
}