#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 */
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ ESRCH;
int O_CREAT;
int O_EXCL;
int O_WRONLY;
int /*<<< orphan*/ __read_pidfile(char const *);
int /*<<< orphan*/ __update_pidfile(int);
scalar_t__ errno;
int /*<<< orphan*/ isns_debug_general(char *, char const *);
int /*<<< orphan*/ isns_fatal(char *, ...);
scalar_t__ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int, int);
int /*<<< orphan*/ unlink(char const *);
void isns_write_pidfile(const char *filename) {
  int fd;
  pid_t pid;
  fd = open(filename, O_CREAT | O_EXCL | O_WRONLY, 0644);
  if (fd >= 0) {
    __update_pidfile(fd);
    return;
  }
  if (errno != EEXIST)
    isns_fatal("Error creating pid file %s: %m\n", filename);
  if ((pid = __read_pidfile(filename)) > 0 && kill(pid, 0) < 0 &&
      errno == ESRCH) {
    isns_debug_general("Removing stale PID file %s\n", filename);
    unlink(filename);
  }
  fd = open(filename, O_CREAT | O_EXCL | O_WRONLY, 0644);
  if (fd < 0)
    isns_fatal("PID file exists; another daemon "
               "seems to be running\n");
  __update_pidfile(fd);
}


x86 assembly (clang, -O0):
<isns_write_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
mov    0x2e1f(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11bf <isns_write_pidfile+0x4f>
mov    -0xc(%rbp),%edi
call   1090 <__update_pidfile@plt>
jmp    1293 <isns_write_pidfile+0x123>
mov    0x2e02(%rip),%rax
mov    (%rax),%rax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%rax
je     11eb <isns_write_pidfile+0x7b>
mov    -0x8(%rbp),%rsi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1030 <isns_fatal@plt>
mov    -0x8(%rbp),%rdi
call   1040 <__read_pidfile@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    1247 <isns_write_pidfile+0xd7>
mov    -0x10(%rbp),%edi
xor    %esi,%esi
call   1070 <kill@plt>
cmp    $0x0,%rax
jge    1247 <isns_write_pidfile+0xd7>
mov    0x2dad(%rip),%rax
mov    (%rax),%rax
mov    0x2d8b(%rip),%rcx
cmp    (%rcx),%rax
jne    1247 <isns_write_pidfile+0xd7>
mov    -0x8(%rbp),%rsi
lea    0xde7(%rip),%rdi
call   1060 <isns_debug_general@plt>
mov    -0x8(%rbp),%rdi
call   1050 <unlink@plt>
mov    -0x8(%rbp),%rdi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
mov    0x2d75(%rip),%rax
or     (%rax),%esi
mov    0x2d54(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    128b <isns_write_pidfile+0x11b>
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1030 <isns_fatal@plt>
mov    -0xc(%rbp),%edi
call   1090 <__update_pidfile@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isns_write_pidfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e79(%rip),%r14
mov    0x2e4a(%rip),%r15
mov    (%r15),%esi
or     (%r14),%esi
mov    %rdi,%rbx
mov    0x2e22(%rip),%r12
or     (%r12),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1242 <isns_write_pidfile+0xd2>
mov    0x2e13(%rip),%rbp
mov    0x0(%rbp),%rax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
je     11d6 <isns_write_pidfile+0x66>
lea    0xe34(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <isns_fatal@plt>
mov    %rbx,%rdi
call   1040 <__read_pidfile@plt>
test   %eax,%eax
jle    1217 <isns_write_pidfile+0xa7>
mov    %eax,%edi
xor    %esi,%esi
call   1070 <kill@plt>
test   %rax,%rax
jns    1217 <isns_write_pidfile+0xa7>
mov    0x0(%rbp),%rax
mov    0x2db5(%rip),%rcx
cmp    (%rcx),%rax
jne    1217 <isns_write_pidfile+0xa7>
lea    0xe19(%rip),%rdi
mov    %rbx,%rsi
call   1060 <isns_debug_general@plt>
mov    %rbx,%rdi
call   1050 <unlink@plt>
mov    (%r15),%esi
or     (%r14),%esi
or     (%r12),%esi
mov    %rbx,%rdi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1242 <isns_write_pidfile+0xd2>
lea    0xe01(%rip),%rdi
xor    %eax,%eax
call   1030 <isns_fatal@plt>
mov    %ebp,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <__update_pidfile@plt>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str = [32 x i8] c"Error creating pid file %s: %m\0A\00"
@ESRCH = global i64 0
@.str.1 = [28 x i8] c"Removing stale PID file %s\0A\00"
@.str.2 = [53 x i8] c"PID file exists; another daemon seems to be running\0A\00"
define void @isns_write_pidfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_EXCL
%8 = or i32 %6, %7
%9 = load i32, i32* @O_WRONLY
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %5, i32 %10, i32 420)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32, i32* %3
%16 = call i32 @__update_pidfile(i32 %15)
br label %56
17: ; preds = %1
%18 = load i64, i64* @errno
%19 = load i64, i64* @EEXIST
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i8*, i8** %2
%23 = call i32 (i8*, ...) @isns_fatal(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %21, %17
%25 = load i8*, i8** %2
%26 = call i32 @__read_pidfile(i8* %25)
store i32 %26, i32* %4
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %41
28: ; preds = %24
%29 = load i32, i32* %4
%30 = call i64 @kill(i32 %29, i32 0)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %41
32: ; preds = %28
%33 = load i64, i64* @errno
%34 = load i64, i64* @ESRCH
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = load i8*, i8** %2
%38 = call i32 @isns_debug_general(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = load i8*, i8** %2
%40 = call i32 @unlink(i8* %39)
br label %41
41: ; preds = %36, %32, %28, %24
%42 = load i8*, i8** %2
%43 = load i32, i32* @O_CREAT
%44 = load i32, i32* @O_EXCL
%45 = or i32 %43, %44
%46 = load i32, i32* @O_WRONLY
%47 = or i32 %45, %46
%48 = call i32 @open(i8* %42, i32 %47, i32 420)
store i32 %48, i32* %3
%49 = load i32, i32* %3
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %41
%52 = call i32 (i8*, ...) @isns_fatal(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0))
br label %53
53: ; preds = %51, %41
%54 = load i32, i32* %3
%55 = call i32 @__update_pidfile(i32 %54)
br label %56
56: ; preds = %53, %14
ret void
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str = [32 x i8] c"Error creating pid file %s: %m\0A\00"
@ESRCH = global i64 0
@.str.1 = [28 x i8] c"Removing stale PID file %s\0A\00"
@.str.2 = [53 x i8] c"PID file exists; another daemon seems to be running\0A\00"
define void @isns_write_pidfile(i8* %0) {
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @O_EXCL
%4 = or i32 %3, %2
%5 = load i32, i32* @O_WRONLY
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* %0, i32 %6, i32 420)
%8 = icmp sgt i32 %7, -1
br i1 %8, label %38, label %9
9: ; preds = %1
%10 = load i64, i64* @errno
%11 = load i64, i64* @EEXIST
%12 = icmp eq i64 %10, %11
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = tail call i32 (i8*, ...) @isns_fatal(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %15
15: ; preds = %13, %9
%16 = tail call i32 @__read_pidfile(i8* %0)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %28
18: ; preds = %15
%19 = tail call i64 @kill(i32 %16, i32 0)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %18
%22 = load i64, i64* @errno
%23 = load i64, i64* @ESRCH
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = tail call i32 @isns_debug_general(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%27 = tail call i32 @unlink(i8* %0)
br label %28
28: ; preds = %25, %21, %18, %15
%29 = load i32, i32* @O_CREAT
%30 = load i32, i32* @O_EXCL
%31 = or i32 %30, %29
%32 = load i32, i32* @O_WRONLY
%33 = or i32 %31, %32
%34 = tail call i32 @open(i8* %0, i32 %33, i32 420)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %28
%37 = tail call i32 (i8*, ...) @isns_fatal(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %28, %36, %1
%39 = phi i32 [ %7, %1 ], [ %34, %36 ], [ %34, %28 ]
%40 = tail call i32 @__update_pidfile(i32 %39)
ret void
}