#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 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*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
scalar_t__ ftruncate(int, int /*<<< orphan*/);
scalar_t__ getpid();
scalar_t__ lockfile(int);
int /*<<< orphan*/ logger_stderr(char *, char const *, char *, ...);
int open(char const *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, long);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
scalar_t__ strlen(char *);
int write(int, char *, scalar_t__);
int already_running(const char *pidfile) {
  int fd;
  char buf[16];
  fd = open(pidfile, O_RDWR | O_CREAT, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
  if (fd < 0) {
    logger_stderr("open \"%s\" failed (%d: %s)", pidfile, errno,
                  strerror(errno));
    exit(1);
  }
  if (lockfile(fd) < 0) {
    if (errno == EACCES || errno == EAGAIN) {
      close(fd);
      return (1);
    }
    logger_stderr("can't lock %s: %s\n", pidfile, strerror(errno));
    exit(1);
  }
  if (ftruncate(fd, 0)) {
    fprintf(stderr, "can't truncate %s: %s", pidfile, strerror(errno));
    exit(1);
  }
  sprintf(buf, "%ld\n", (long)getpid());
  if (write(fd, buf, strlen(buf) + 1) == -1) {
    fprintf(stderr, "can't write %s: %s", pidfile, strerror(errno));
    exit(1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<already_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
or     (%rax),%esi
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
mov    0x2de3(%rip),%rax
or     (%rax),%edx
mov    0x2db2(%rip),%rax
or     (%rax),%edx
mov    0x2da1(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1263 <already_running+0xa3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d80(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1070 <logger_stderr@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
call   10a0 <lockfile@plt>
cmp    $0x0,%rax
jge    12f3 <already_running+0x133>
mov    0x2d3c(%rip),%rax
mov    (%rax),%rax
mov    0x2d4a(%rip),%rcx
cmp    (%rcx),%rax
je     12a9 <already_running+0xe9>
mov    0x2d22(%rip),%rax
mov    (%rax),%rax
mov    0x2cf0(%rip),%rcx
cmp    (%rcx),%rax
jne    12bd <already_running+0xfd>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e9 <already_running+0x229>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2cec(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
lea    0xd38(%rip),%rdi
mov    $0x0,%al
call   1070 <logger_stderr@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
cmp    $0x0,%rax
je     134a <already_running+0x18a>
mov    0x2cb2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2c96(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xcf2(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x70(%rbp),%rdi
mov    %rax,%rdx
lea    0xcdc(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x64(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    13e2 <already_running+0x222>
mov    0x2c1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    0x2bfe(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rdx
mov    %rax,%rcx
lea    0xc75(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<already_running>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e0f(%rip),%rax
mov    0x2e20(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dbd(%rip),%rax
mov    0x2dee(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dbb(%rip),%rax
or     (%rax),%edx
mov    %rdi,%r14
mov    0x2da7(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
test   %eax,%eax
js     12cf <already_running+0x10f>
mov    %eax,%ebp
mov    %eax,%edi
call   10a0 <lockfile@plt>
test   %rax,%rax
js     128e <already_running+0xce>
xor    %r15d,%r15d
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
test   %rax,%rax
jne    1302 <already_running+0x142>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe02(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rax,%rdx
call   10c0 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    12c1 <already_running+0x101>
mov    0x2d4f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3e(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
lea    0xdbf(%rip),%rsi
jmp    1321 <already_running+0x161>
mov    0x2d23(%rip),%rax
mov    (%rax),%rdi
mov    0x2d31(%rip),%rax
cmp    (%rax),%rdi
je     12b4 <already_running+0xf4>
mov    0x2ce5(%rip),%rax
cmp    (%rax),%rdi
jne    1338 <already_running+0x178>
mov    %ebp,%edi
call   1080 <close@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ce2(%rip),%rax
mov    (%rax),%rbx
mov    %rbx,%rdi
call   10e0 <strerror@plt>
lea    0xd18(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <logger_stderr@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2cb7(%rip),%rax
mov    (%rax),%ebx
mov    0x2ca6(%rip),%rax
mov    (%rax),%rdi
call   10e0 <strerror@plt>
lea    0xd0c(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
call   10e0 <strerror@plt>
lea    0xcd6(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <logger_stderr@plt>
mov    $0x1,%edi
call   10d0 <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
@.str = [26 x i8] c"open \22%s\22 failed (%d: %s)\00"
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.1 = [19 x i8] c"can't lock %s: %s\0A\00"
@stderr = global i32 0
@.str.2 = [22 x i8] c"can't truncate %s: %s\00"
@.str.3 = [5 x i8] c"%ld\0A\00"
@.str.4 = [19 x i8] c"can't write %s: %s\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 %28
20: ; preds = %1
%21 = load i8*, i8** %3
%22 = load i64, i64* @errno
%23 = inttoptr i64 %22 to i8*
%24 = load i64, i64* @errno
%25 = call i8* @strerror(i64 %24)
%26 = call i32 (i8*, i8*, i8*, ...) @logger_stderr(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %21, i8* %23, i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %1
%29 = load i32, i32* %4
%30 = call i64 @lockfile(i32 %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %49
32: ; preds = %28
%33 = load i64, i64* @errno
%34 = load i64, i64* @EACCES
%35 = icmp eq i64 %33, %34
br i1 %35, label %40, label %36
36: ; preds = %32
%37 = load i64, i64* @errno
%38 = load i64, i64* @EAGAIN
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %43
40: ; preds = %36, %32
%41 = load i32, i32* %4
%42 = call i32 @close(i32 %41)
store i32 1, i32* %2
br label %79
43: ; preds = %36
%44 = load i8*, i8** %3
%45 = load i64, i64* @errno
%46 = call i8* @strerror(i64 %45)
%47 = call i32 (i8*, i8*, i8*, ...) @logger_stderr(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %44, i8* %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %28
%50 = load i32, i32* %4
%51 = call i64 @ftruncate(i32 %50, i32 0)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %60
53: ; preds = %49
%54 = load i32, i32* @stderr
%55 = load i8*, i8** %3
%56 = load i64, i64* @errno
%57 = call i8* @strerror(i64 %56)
%58 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %55, i8* %57)
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %49
%61 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%62 = call i64 (...) @getpid()
%63 = call i32 @sprintf(i8* %61, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %62)
%64 = load i32, i32* %4
%65 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%66 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%67 = call i64 @strlen(i8* %66)
%68 = add nsw i64 %67, 1
%69 = call i32 @write(i32 %64, i8* %65, i64 %68)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %78
71: ; preds = %60
%72 = load i32, i32* @stderr
%73 = load i8*, i8** %3
%74 = load i64, i64* @errno
%75 = call i8* @strerror(i64 %74)
%76 = call i32 @fprintf(i32 %72, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %73, i8* %75)
%77 = call i32 @exit(i32 1)
unreachable
78: ; preds = %60
store i32 0, i32* %2
br label %79
79: ; preds = %78, %40
%80 = load i32, i32* %2
ret i32 %80
}

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
@.str = [26 x i8] c"open \22%s\22 failed (%d: %s)\00"
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.1 = [19 x i8] c"can't lock %s: %s\0A\00"
@stderr = global i32 0
@.str.2 = [22 x i8] c"can't truncate %s: %s\00"
@.str.3 = [5 x i8] c"%ld\0A\00"
@.str.4 = [19 x i8] c"can't write %s: %s\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 i64, i64* @errno
%18 = inttoptr i64 %17 to i8*
%19 = tail call i8* @strerror(i64 %17)
%20 = tail call i32 (i8*, i8*, i8*, ...) @logger_stderr(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %18, i8* %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 %38
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 %60
34: ; preds = %25
%35 = tail call i8* @strerror(i64 %26)
%36 = tail call i32 (i8*, i8*, i8*, ...) @logger_stderr(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %35)
%37 = tail call i32 @exit(i32 1)
unreachable
38: ; preds = %22
%39 = tail call i64 @ftruncate(i32 %14, i32 0)
%40 = icmp eq i64 %39, 0
br i1 %40, label %47, label %41
41: ; preds = %38
%42 = load i32, i32* @stderr
%43 = load i64, i64* @errno
%44 = tail call i8* @strerror(i64 %43)
%45 = tail call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %44)
%46 = tail call i32 @exit(i32 1)
unreachable
47: ; preds = %38
%48 = tail call i64 (...) @getpid()
%49 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %48)
%50 = call i64 @strlen(i8* %3)
%51 = add nsw i64 %50, 1
%52 = call i32 @write(i32 %14, i8* %3, i64 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %60
54: ; preds = %47
%55 = load i32, i32* @stderr
%56 = load i64, i64* @errno
%57 = tail call i8* @strerror(i64 %56)
%58 = tail call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %57)
%59 = tail call i32 @exit(i32 1)
unreachable
60: ; preds = %47, %32
%61 = phi i32 [ 1, %32 ], [ 0, %47 ]
ret i32 %61
}