#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 scalar_t__ ssize_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ LogError(char *, ...);
int O_CREAT;
int O_RDONLY;
int O_RDWR;
int O_TRUNC;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
unsigned long atol(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ getpid();
scalar_t__ kill(unsigned long, int /*<<< orphan*/);
int open(char *, int, int);
scalar_t__ read(int, void *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned long);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ unlink(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int CheckRunningOnce(char *pidfile) {
  int pidf;
  pid_t pid;
  char pidstr[32];
  pidf = open(pidfile, O_RDONLY, 0);
  if (pidf > 0) {
    char s[32];
    ssize_t len;
    len = read(pidf, (void *)s, 31);
    close(pidf);
    s[31] = '\0';
    if (len < 0) {
      LogError("read() error existing pid file: %s\n", strerror(errno));
      return 0;
    } else {
      unsigned long pid = atol(s);
      if (pid == 0) {
        unlink(pidfile);
      } else {
        if (kill(pid, 0) == 0) {
          LogError("An nftrack process with pid %lu is already running!\n",
                   pid);
          return 0;
        } else {
          LogError("The nftrack process with pid %lu died unexpectedly!\n",
                   pid);
          unlink(pidfile);
        }
      }
    }
  }
  pid = getpid();
  pidf = open(pidfile, O_RDWR | O_TRUNC | O_CREAT,
              S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (pidf == -1) {
    LogError("Error opening nftrack pid file: '%s' %s", pidfile,
             strerror(errno));
    return 0;
  }
  snprintf(pidstr, 31, "%lu\n", (unsigned long)pid);
  if (write(pidf, pidstr, strlen(pidstr)) <= 0) {
    LogError("Error write nftrack pid file: '%s' %s", pidfile, strerror(errno));
  }
  close(pidf);
  return 1;
}


x86 assembly (clang, -O0):
<CheckRunningOnce>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    12c9 <CheckRunningOnce+0x109>
mov    -0x14(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x1f,%edx
call   1090 <read@plt>
mov    %rax,-0x68(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movb   $0x0,-0x41(%rbp)
cmpq   $0x0,-0x68(%rbp)
jge    1246 <CheckRunningOnce+0x86>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %eax,%esi
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   10a0 <LogError@plt>
movl   $0x0,-0x4(%rbp)
jmp    13ec <CheckRunningOnce+0x22c>
lea    -0x60(%rbp),%rdi
call   10c0 <atol@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
jne    126c <CheckRunningOnce+0xac>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
jmp    12bf <CheckRunningOnce+0xff>
mov    -0x70(%rbp),%rdi
xor    %esi,%esi
call   10b0 <kill@plt>
cmp    $0x0,%rax
jne    129f <CheckRunningOnce+0xdf>
mov    -0x70(%rbp),%rsi
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   10a0 <LogError@plt>
movl   $0x0,-0x4(%rbp)
jmp    13ec <CheckRunningOnce+0x22c>
mov    -0x70(%rbp),%rsi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   10a0 <LogError@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
jmp    12bf <CheckRunningOnce+0xff>
jmp    12c4 <CheckRunningOnce+0x104>
jmp    12c9 <CheckRunningOnce+0x109>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2cf9(%rip),%rax
mov    (%rax),%esi
mov    0x2d08(%rip),%rax
or     (%rax),%esi
mov    0x2d07(%rip),%rax
or     (%rax),%esi
mov    0x2ca6(%rip),%rax
mov    (%rax),%edx
mov    0x2cc5(%rip),%rax
or     (%rax),%edx
mov    0x2ca4(%rip),%rax
or     (%rax),%edx
mov    0x2c93(%rip),%rax
or     (%rax),%edx
call   10d0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    135f <CheckRunningOnce+0x19f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    0x2c80(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x78(%rbp),%rsi
mov    %eax,%edx
lea    0xd42(%rip),%rdi
mov    $0x0,%al
call   10a0 <LogError@plt>
movl   $0x0,-0x4(%rbp)
jmp    13ec <CheckRunningOnce+0x22c>
lea    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1f,%esi
lea    0xd43(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x84(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x80(%rbp)
lea    -0x40(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x84(%rbp),%edi
mov    -0x80(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jg     13dd <CheckRunningOnce+0x21d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    0x2bf9(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x90(%rbp),%rsi
mov    %eax,%edx
lea    0xce5(%rip),%rdi
mov    $0x0,%al
call   10a0 <LogError@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CheckRunningOnce>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <open@plt>
test   %eax,%eax
jle    1246 <CheckRunningOnce+0x86>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1f,%edx
call   1090 <read@plt>
mov    %rax,%rbp
mov    %ebx,%edi
call   1080 <close@plt>
movb   $0x0,0x1f(%rsp)
test   %rbp,%rbp
js     1325 <CheckRunningOnce+0x165>
mov    %rsp,%rdi
call   10c0 <atol@plt>
test   %rax,%rax
je     123e <CheckRunningOnce+0x7e>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
call   10b0 <kill@plt>
test   %rax,%rax
je     1347 <CheckRunningOnce+0x187>
lea    0xe25(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   10a0 <LogError@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <getpid@plt>
mov    0x2d82(%rip),%rcx
mov    0x2d93(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2d90(%rip),%rcx
or     (%rcx),%esi
mov    0x2d2f(%rip),%rcx
mov    0x2d50(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2d2d(%rip),%rcx
or     (%rcx),%edx
mov    %rax,%r15
mov    0x2d19(%rip),%rax
or     (%rax),%edx
mov    %r14,%rdi
call   10d0 <open@plt>
cmp    $0xffffffff,%eax
je     1302 <CheckRunningOnce+0x142>
mov    %eax,%ebx
lea    0xe0f(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x1f,%esi
mov    %r15,%rcx
call   1070 <snprintf@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %rax,%rax
jg     12f4 <CheckRunningOnce+0x134>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xdd3(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10a0 <LogError@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    $0x1,%ebp
jmp    135a <CheckRunningOnce+0x19a>
mov    0x2caf(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd77(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10a0 <LogError@plt>
jmp    135a <CheckRunningOnce+0x19a>
mov    0x2c8c(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xcc6(%rip),%rdi
xor    %ebp,%ebp
mov    %eax,%esi
xor    %eax,%eax
call   10a0 <LogError@plt>
jmp    135a <CheckRunningOnce+0x19a>
lea    0xcd6(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   10a0 <LogError@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [36 x i8] c"read() error existing pid file: %s\0A\00"
@errno = global i32 0
@.str.1 = [53 x i8] c"An nftrack process with pid %lu is already running!\0A\00"
@.str.2 = [53 x i8] c"The nftrack process with pid %lu died unexpectedly!\0A\00"
@O_RDWR = global i32 0
@O_TRUNC = 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.3 = [40 x i8] c"Error opening nftrack pid file: '%s' %s\00"
@.str.4 = [5 x i8] c"%lu\0A\00"
@.str.5 = [38 x i8] c"Error write nftrack pid file: '%s' %s\00"
define i32 @CheckRunningOnce(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca [32 x i8]
%7 = alloca [32 x i8]
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11, i32 0)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %51
15: ; preds = %1
%16 = load i32, i32* %4
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%18 = call i64 @read(i32 %16, i8* %17, i32 31)
store i64 %18, i64* %8
%19 = load i32, i32* %4
%20 = call i32 @close(i32 %19)
%21 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 31
store i8 0, i8* %21
%22 = load i64, i64* %8
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %15
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 (i8*, ...) @LogError(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %26)
store i32 0, i32* %2
br label %92
28: ; preds = %15
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%30 = call i64 @atol(i8* %29)
store i64 %30, i64* %9
%31 = load i64, i64* %9
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i8*, i8** %3
%35 = call i32 @unlink(i8* %34)
br label %49
36: ; preds = %28
%37 = load i64, i64* %9
%38 = call i64 @kill(i64 %37, i32 0)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i64, i64* %9
%42 = call i32 (i8*, ...) @LogError(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i64 %41)
store i32 0, i32* %2
br label %92
43: ; preds = %36
%44 = load i64, i64* %9
%45 = call i32 (i8*, ...) @LogError(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i64 %44)
%46 = load i8*, i8** %3
%47 = call i32 @unlink(i8* %46)
br label %48
48: ; preds = %43
br label %49
49: ; preds = %48, %33
br label %50
50: ; preds = %49
br label %51
51: ; preds = %50, %1
%52 = call i64 (...) @getpid()
store i64 %52, i64* %5
%53 = load i8*, i8** %3
%54 = load i32, i32* @O_RDWR
%55 = load i32, i32* @O_TRUNC
%56 = or i32 %54, %55
%57 = load i32, i32* @O_CREAT
%58 = or i32 %56, %57
%59 = load i32, i32* @S_IRUSR
%60 = load i32, i32* @S_IWUSR
%61 = or i32 %59, %60
%62 = load i32, i32* @S_IRGRP
%63 = or i32 %61, %62
%64 = load i32, i32* @S_IROTH
%65 = or i32 %63, %64
%66 = call i32 @open(i8* %53, i32 %58, i32 %65)
store i32 %66, i32* %4
%67 = load i32, i32* %4
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %74
69: ; preds = %51
%70 = load i8*, i8** %3
%71 = load i32, i32* @errno
%72 = call i32 @strerror(i32 %71)
%73 = call i32 (i8*, ...) @LogError(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %70, i32 %72)
store i32 0, i32* %2
br label %92
74: ; preds = %51
%75 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%76 = load i64, i64* %5
%77 = call i32 @snprintf(i8* %75, i32 31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i64 %76)
%78 = load i32, i32* %4
%79 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%80 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%81 = call i32 @strlen(i8* %80)
%82 = call i64 @write(i32 %78, i8* %79, i32 %81)
%83 = icmp sle i64 %82, 0
br i1 %83, label %84, label %89
84: ; preds = %74
%85 = load i8*, i8** %3
%86 = load i32, i32* @errno
%87 = call i32 @strerror(i32 %86)
%88 = call i32 (i8*, ...) @LogError(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* %85, i32 %87)
br label %89
89: ; preds = %84, %74
%90 = load i32, i32* %4
%91 = call i32 @close(i32 %90)
store i32 1, i32* %2
br label %92
92: ; preds = %89, %69, %40, %24
%93 = load i32, i32* %2
ret i32 %93
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [36 x i8] c"read() error existing pid file: %s\0A\00"
@errno = global i32 0
@.str.1 = [53 x i8] c"An nftrack process with pid %lu is already running!\0A\00"
@.str.2 = [53 x i8] c"The nftrack process with pid %lu died unexpectedly!\0A\00"
@O_RDWR = global i32 0
@O_TRUNC = 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.3 = [40 x i8] c"Error opening nftrack pid file: '%s' %s\00"
@.str.4 = [5 x i8] c"%lu\0A\00"
@.str.5 = [38 x i8] c"Error write nftrack pid file: '%s' %s\00"
define i32 @CheckRunningOnce(i8* %0) {
%2 = alloca [32 x i8]
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5, i32 0)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %31
8: ; preds = %1
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%10 = call i64 @read(i32 %6, i8* %9, i32 31)
%11 = tail call i32 @close(i32 %6)
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 31
store i8 0, i8* %12
%13 = icmp slt i64 %10, 0
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 (i8*, ...) @LogError(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %30
18: ; preds = %8
%19 = call i64 @atol(i8* %9)
%20 = icmp eq i64 %19, 0
br i1 %20, label %28, label %21
21: ; preds = %18
%22 = tail call i64 @kill(i64 %19, i32 0)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 (i8*, ...) @LogError(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i64 %19)
br label %30
26: ; preds = %21
%27 = tail call i32 (i8*, ...) @LogError(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i64 %19)
br label %28
28: ; preds = %18, %26
%29 = tail call i32 @unlink(i8* %0)
br label %31
30: ; preds = %24, %14
br label %62
31: ; preds = %28, %1
%32 = tail call i64 (...) @getpid()
%33 = load i32, i32* @O_RDWR
%34 = load i32, i32* @O_TRUNC
%35 = or i32 %34, %33
%36 = load i32, i32* @O_CREAT
%37 = or i32 %35, %36
%38 = load i32, i32* @S_IRUSR
%39 = load i32, i32* @S_IWUSR
%40 = or i32 %39, %38
%41 = load i32, i32* @S_IRGRP
%42 = or i32 %40, %41
%43 = load i32, i32* @S_IROTH
%44 = or i32 %42, %43
%45 = tail call i32 @open(i8* %0, i32 %37, i32 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %51
47: ; preds = %31
%48 = load i32, i32* @errno
%49 = tail call i32 @strerror(i32 %48)
%50 = tail call i32 (i8*, ...) @LogError(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %49)
br label %62
51: ; preds = %31
%52 = call i32 @snprintf(i8* %4, i32 31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i64 %32)
%53 = call i32 @strlen(i8* %4)
%54 = call i64 @write(i32 %45, i8* %4, i32 %53)
%55 = icmp slt i64 %54, 1
br i1 %55, label %56, label %60
56: ; preds = %51
%57 = load i32, i32* @errno
%58 = call i32 @strerror(i32 %57)
%59 = call i32 (i8*, ...) @LogError(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* %0, i32 %58)
br label %60
60: ; preds = %56, %51
%61 = call i32 @close(i32 %45)
br label %62
62: ; preds = %30, %60, %47
%63 = phi i32 [ 0, %47 ], [ 1, %60 ], [ 0, %30 ]
ret i32 %63
}