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


x86 assembly (clang, -O0):
<already_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    0x2e50(%rip),%rax
or     (%rax),%esi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
mov    0x2e1e(%rip),%rax
or     (%rax),%edx
mov    0x2dfd(%rip),%rax
or     (%rax),%edx
mov    0x2dec(%rip),%rax
or     (%rax),%edx
lea    0xe2b(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1223 <already_running+0x93>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%ecx
lea    0xe08(%rip),%rsi
lea    0xdec(%rip),%rdx
call   1070 <syslog@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x4(%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    0xddd(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x4(%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>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<already_running>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e42(%rip),%rax
mov    0x2e53(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e00(%rip),%rax
mov    0x2e21(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dfe(%rip),%rax
or     (%rax),%edx
mov    0x2ded(%rip),%rax
or     (%rax),%edx
lea    0xe2c(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1224 <already_running+0x94>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe31(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rax,%rdx
call   1090 <sprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <write@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d75(%rip),%rax
mov    (%rax),%ebx
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdd3(%rip),%rsi
lea    0xdb7(%rip),%rdx
mov    %ebx,%edi
mov    %eax,%ecx
call   1070 <syslog@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"/var/run/clacksd.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 i32 0
@.str.2 = [4 x i8] c"%ld\00"
define i32 @already_running() {
%1 = alloca i32
%2 = alloca [16 x i8]
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %3, %4
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRGRP
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IROTH
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %12)
store i32 %13, i32* %1
%14 = load i32, i32* %1
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %0
%17 = load i32, i32* @LOG_ERR
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 @syslog(i32 %17, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %0
%23 = load i32, i32* %1
%24 = call i32 @ftruncate(i32 %23, i32 0)
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%26 = call i64 (...) @getpid()
%27 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 %26)
%28 = load i32, i32* %1
%29 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%30 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%31 = call i64 @strlen(i8* %30)
%32 = add nsw i64 %31, 1
%33 = call i32 @write(i32 %28, i8* %29, i64 %32)
ret i32 0
}

LLVM IR (-O2):
@.str = [21 x i8] c"/var/run/clacksd.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 i32 0
@.str.2 = [4 x i8] c"%ld\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 ([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 %21
15: ; preds = %0
%16 = load i32, i32* @LOG_ERR
%17 = load i32, i32* @errno
%18 = tail call i32 @strerror(i32 %17)
%19 = tail call i32 @syslog(i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %0
%22 = tail call i32 @ftruncate(i32 %13, i32 0)
%23 = tail call i64 (...) @getpid()
%24 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 %23)
%25 = call i64 @strlen(i8* %2)
%26 = add nsw i64 %25, 1
%27 = call i32 @write(i32 %13, i8* %2, i64 %26)
ret i32 0
}