#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);
scalar_t__ ftruncate(int, int /*<<< orphan*/);
scalar_t__ getpid();
scalar_t__ lockfile(int);
int /*<<< orphan*/ logger_stderr(char *, char const *, scalar_t__, ...);
int open(char const *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, long);
int /*<<< orphan*/ 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", pidfile, strerror(errno));
    exit(1);
  }
  if (ftruncate(fd, 0)) {
    logger_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) {
    logger_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    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
mov    0x2df6(%rip),%rax
or     (%rax),%edx
mov    0x2dcd(%rip),%rax
or     (%rax),%edx
mov    0x2dbc(%rip),%rax
or     (%rax),%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    124f <already_running+0x9f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2da9(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1070 <logger_stderr@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1090 <lockfile@plt>
cmp    $0x0,%rax
jge    12df <already_running+0x12f>
mov    0x2d58(%rip),%rax
mov    (%rax),%rax
mov    0x2d5e(%rip),%rcx
cmp    (%rcx),%rax
je     1295 <already_running+0xe5>
mov    0x2d3e(%rip),%rax
mov    (%rax),%rax
mov    0x2d0c(%rip),%rcx
cmp    (%rcx),%rax
jne    12a9 <already_running+0xf9>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13bb <already_running+0x20b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d08(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0x48(%rbp),%rsi
movslq %eax,%rdx
lea    0xd4c(%rip),%rdi
mov    $0x0,%al
call   1070 <logger_stderr@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
cmp    $0x0,%rax
je     1329 <already_running+0x179>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2cbe(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0x50(%rbp),%rsi
movslq %eax,%rdx
lea    0xd14(%rip),%rdi
mov    $0x0,%al
call   1070 <logger_stderr@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x68(%rbp),%rdi
mov    %rax,%rdx
lea    0xcfc(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x5c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    13b4 <already_running+0x204>
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    0x2c33(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0x70(%rbp),%rsi
movslq %eax,%rdx
lea    0xca4(%rip),%rdi
mov    $0x0,%al
call   1070 <logger_stderr@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<already_running>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e1f(%rip),%rax
mov    0x2e30(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd5(%rip),%rax
mov    0x2dfe(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dd3(%rip),%rax
or     (%rax),%edx
mov    %rdi,%r14
mov    0x2dbf(%rip),%rax
or     (%rax),%edx
call   10a0 <open@plt>
test   %eax,%eax
js     12b5 <already_running+0x105>
mov    %eax,%ebp
mov    %eax,%edi
call   1090 <lockfile@plt>
test   %rax,%rax
js     1278 <already_running+0xc8>
xor    %r15d,%r15d
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
test   %rax,%rax
jne    12e7 <already_running+0x137>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe11(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rax,%rdx
call   10b0 <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    12a7 <already_running+0xf7>
mov    0x2d5f(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
movslq %eax,%rdx
lea    0xdd4(%rip),%rdi
jmp    1311 <already_running+0x161>
mov    0x2d41(%rip),%rax
mov    (%rax),%rdi
mov    0x2d47(%rip),%rax
cmp    (%rax),%rdi
je     129a <already_running+0xea>
mov    0x2d03(%rip),%rax
cmp    (%rax),%rdi
jne    1302 <already_running+0x152>
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    0x2d04(%rip),%rax
mov    (%rax),%rbx
mov    %rbx,%rdi
call   10d0 <strerror@plt>
lea    0xd32(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1070 <logger_stderr@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2cd2(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
movslq %eax,%rdx
lea    0xd2c(%rip),%rdi
jmp    1311 <already_running+0x161>
call   10d0 <strerror@plt>
movslq %eax,%rdx
lea    0xd09(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <logger_stderr@plt>
mov    $0x1,%edi
call   10c0 <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 = [18 x i8] c"can't lock %s: %s\00"
@.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 %27
20: ; preds = %1
%21 = load i8*, i8** %3
%22 = load i64, i64* @errno
%23 = load i64, i64* @errno
%24 = call i32 @strerror(i64 %23)
%25 = call i32 (i8*, i8*, i64, ...) @logger_stderr(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %21, i64 %22, i32 %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %1
%28 = load i32, i32* %4
%29 = call i64 @lockfile(i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %49
31: ; preds = %27
%32 = load i64, i64* @errno
%33 = load i64, i64* @EACCES
%34 = icmp eq i64 %32, %33
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = load i64, i64* @errno
%37 = load i64, i64* @EAGAIN
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %35, %31
%40 = load i32, i32* %4
%41 = call i32 @close(i32 %40)
store i32 1, i32* %2
br label %79
42: ; preds = %35
%43 = load i8*, i8** %3
%44 = load i64, i64* @errno
%45 = call i32 @strerror(i64 %44)
%46 = sext i32 %45 to i64
%47 = call i32 (i8*, i8*, i64, ...) @logger_stderr(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %43, i64 %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %27
%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 i8*, i8** %3
%55 = load i64, i64* @errno
%56 = call i32 @strerror(i64 %55)
%57 = sext i32 %56 to i64
%58 = call i32 (i8*, i8*, i64, ...) @logger_stderr(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %54, i64 %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 i8*, i8** %3
%73 = load i64, i64* @errno
%74 = call i32 @strerror(i64 %73)
%75 = sext i32 %74 to i64
%76 = call i32 (i8*, i8*, i64, ...) @logger_stderr(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %72, i64 %75)
%77 = call i32 @exit(i32 1)
unreachable
78: ; preds = %60
store i32 0, i32* %2
br label %79
79: ; preds = %78, %39
%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 = [18 x i8] c"can't lock %s: %s\00"
@.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 %21
16: ; preds = %1
%17 = load i64, i64* @errno
%18 = tail call i32 @strerror(i64 %17)
%19 = tail call i32 (i8*, i8*, i64, ...) @logger_stderr(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %17, i32 %18)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %1
%22 = tail call i64 @lockfile(i32 %14)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %38
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
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = tail call i32 @close(i32 %14)
br label %60
33: ; preds = %24
%34 = tail call i32 @strerror(i64 %25)
%35 = sext i32 %34 to i64
%36 = tail call i32 (i8*, i8*, i64, ...) @logger_stderr(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %0, i64 %35)
%37 = tail call i32 @exit(i32 1)
unreachable
38: ; preds = %21
%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 i64, i64* @errno
%43 = tail call i32 @strerror(i64 %42)
%44 = sext i32 %43 to i64
%45 = tail call i32 (i8*, i8*, i64, ...) @logger_stderr(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0, i64 %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 i64, i64* @errno
%56 = tail call i32 @strerror(i64 %55)
%57 = sext i32 %56 to i64
%58 = tail call i32 (i8*, i8*, i64, ...) @logger_stderr(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %0, i64 %57)
%59 = tail call i32 @exit(i32 1)
unreachable
60: ; preds = %47, %31
%61 = phi i32 [ 1, %31 ], [ 0, %47 ]
ret i32 %61
}