#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 FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_WRLCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ errExit(char *, char const *);
scalar_t__ errno;
int /*<<< orphan*/ fatal(char *, char const *, char const *);
int fcntl(int, int /*<<< orphan*/, ...);
int ftruncate(int, int /*<<< orphan*/);
int lockRegion(int, int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
               int /*<<< orphan*/);
int open(char const *, int, int);
int createPidFile(const char *progName, const char *pidFile, int flags) {
  int fd;
  fd = open(pidFile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  if (fd == -1)
    errExit("Could not open PID file %s", pidFile);
  if (flags & 1) {
    flags = fcntl(fd, F_GETFD);
    if (flags == -1)
      errExit("Could not get flags for PID file %s", pidFile);
    flags |= FD_CLOEXEC;
    if (fcntl(fd, F_SETFD, flags) == -1)
      errExit("Could not set flags for PID file %s", pidFile);
  }
  if (lockRegion(fd, F_WRLCK, SEEK_SET, 0, 0) == -1) {
    if (errno == EAGAIN || errno == EACCES)
      fatal("PID file '%s' is locked; probably "
            "'%s' is already running",
            pidFile, progName);
    else
      errExit("Unable to lock PID file '%s'", pidFile);
  }
  if (ftruncate(fd, 0) == -1)
    errExit("Could not truncate PID file '%s'", pidFile);
  return fd;
}


x86 assembly (clang, -O0):
<createPidFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    0x2e71(%rip),%rax
or     (%rax),%esi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
mov    0x2e3f(%rip),%rax
or     (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11bd <createPidFile+0x5d>
mov    -0x10(%rbp),%rsi
lea    0xe48(%rip),%rdi
call   1040 <errExit@plt>
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     123f <createPidFile+0xdf>
mov    -0x18(%rbp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11fc <createPidFile+0x9c>
mov    -0x10(%rbp),%rsi
lea    0xe24(%rip),%rdi
call   1040 <errExit@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    123a <createPidFile+0xda>
mov    -0x10(%rbp),%rsi
lea    0xe0a(%rip),%rdi
call   1040 <errExit@plt>
jmp    123f <createPidFile+0xdf>
mov    -0x18(%rbp),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
mov    0x2d56(%rip),%rax
mov    (%rax),%edx
xor    %r8d,%r8d
mov    %r8d,%ecx
call   1070 <lockRegion@plt>
cmp    $0xffffffff,%eax
jne    12ca <createPidFile+0x16a>
mov    0x2d41(%rip),%rax
mov    (%rax),%rax
mov    0x2d07(%rip),%rcx
cmp    (%rcx),%rax
je     129c <createPidFile+0x13c>
mov    0x2d27(%rip),%rax
mov    (%rax),%rax
mov    0x2d3d(%rip),%rcx
cmp    (%rcx),%rax
jne    12b5 <createPidFile+0x155>
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    0xdb8(%rip),%rdi
call   1080 <fatal@plt>
jmp    12c5 <createPidFile+0x165>
mov    -0x10(%rbp),%rsi
lea    0xddd(%rip),%rdi
call   1040 <errExit@plt>
jmp    12ca <createPidFile+0x16a>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    12ed <createPidFile+0x18d>
mov    -0x10(%rbp),%rsi
lea    0xdd2(%rip),%rdi
call   1040 <errExit@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createPidFile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e6a(%rip),%rax
mov    0x2e7b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e10(%rip),%rax
mov    0x2e49(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r15,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11a9 <createPidFile+0x49>
test   $0x1,%bl
jne    11bd <createPidFile+0x5d>
jmp    1216 <createPidFile+0xb6>
lea    0xe50(%rip),%rdi
mov    %r15,%rsi
call   1040 <errExit@plt>
test   $0x1,%bl
je     1216 <createPidFile+0xb6>
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11e5 <createPidFile+0x85>
lea    0xe3e(%rip),%rdi
mov    %r15,%rsi
call   1040 <errExit@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
or     %ebx,%edx
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1216 <createPidFile+0xb6>
lea    0xe31(%rip),%rdi
mov    %r15,%rsi
call   1040 <errExit@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
mov    0x2d82(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <lockRegion@plt>
cmp    $0xffffffff,%eax
jne    127e <createPidFile+0x11e>
mov    0x2d70(%rip),%rax
mov    (%rax),%rax
mov    0x2d36(%rip),%rcx
cmp    (%rcx),%rax
je     126c <createPidFile+0x10c>
mov    0x2d7a(%rip),%rcx
cmp    (%rcx),%rax
je     126c <createPidFile+0x10c>
lea    0xe3b(%rip),%rdi
mov    %r15,%rsi
call   1040 <errExit@plt>
jmp    127e <createPidFile+0x11e>
lea    0xdf0(%rip),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1080 <fatal@plt>
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    129b <createPidFile+0x13b>
lea    0xe27(%rip),%rdi
mov    %r15,%rsi
call   1040 <errExit@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [27 x i8] c"Could not open PID file %s\00"
@F_GETFD = global i32 0
@.str.1 = [36 x i8] c"Could not get flags for PID file %s\00"
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@.str.2 = [36 x i8] c"Could not set flags for PID file %s\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EACCES = global i64 0
@.str.3 = [58 x i8] c"PID file '%s' is locked; probably '%s' is already running\00"
@.str.4 = [29 x i8] c"Unable to lock PID file '%s'\00"
@.str.5 = [33 x i8] c"Could not truncate PID file '%s'\00"
define i32 @createPidFile(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %5
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %8, i32 %11, i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = load i8*, i8** %5
%20 = call i32 @errExit(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %18, %3
%22 = load i32, i32* %6
%23 = and i32 %22, 1
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %47
25: ; preds = %21
%26 = load i32, i32* %7
%27 = load i32, i32* @F_GETFD
%28 = call i32 (i32, i32, ...) @fcntl(i32 %26, i32 %27)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = load i8*, i8** %5
%33 = call i32 @errExit(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %31, %25
%35 = load i32, i32* @FD_CLOEXEC
%36 = load i32, i32* %6
%37 = or i32 %36, %35
store i32 %37, i32* %6
%38 = load i32, i32* %7
%39 = load i32, i32* @F_SETFD
%40 = load i32, i32* %6
%41 = call i32 (i32, i32, ...) @fcntl(i32 %38, i32 %39, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %34
%44 = load i8*, i8** %5
%45 = call i32 @errExit(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %43, %34
br label %47
47: ; preds = %46, %21
%48 = load i32, i32* %7
%49 = load i32, i32* @F_WRLCK
%50 = load i32, i32* @SEEK_SET
%51 = call i32 @lockRegion(i32 %48, i32 %49, i32 %50, i32 0, i32 0)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %69
53: ; preds = %47
%54 = load i64, i64* @errno
%55 = load i64, i64* @EAGAIN
%56 = icmp eq i64 %54, %55
br i1 %56, label %61, label %57
57: ; preds = %53
%58 = load i64, i64* @errno
%59 = load i64, i64* @EACCES
%60 = icmp eq i64 %58, %59
br i1 %60, label %61, label %65
61: ; preds = %57, %53
%62 = load i8*, i8** %5
%63 = load i8*, i8** %4
%64 = call i32 @fatal(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0), i8* %62, i8* %63)
br label %68
65: ; preds = %57
%66 = load i8*, i8** %5
%67 = call i32 @errExit(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %66)
br label %68
68: ; preds = %65, %61
br label %69
69: ; preds = %68, %47
%70 = load i32, i32* %7
%71 = call i32 @ftruncate(i32 %70, i32 0)
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %76
73: ; preds = %69
%74 = load i8*, i8** %5
%75 = call i32 @errExit(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %74)
br label %76
76: ; preds = %73, %69
%77 = load i32, i32* %7
ret i32 %77
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [27 x i8] c"Could not open PID file %s\00"
@F_GETFD = global i32 0
@.str.1 = [36 x i8] c"Could not get flags for PID file %s\00"
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@.str.2 = [36 x i8] c"Could not set flags for PID file %s\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EACCES = global i64 0
@.str.3 = [58 x i8] c"PID file '%s' is locked; probably '%s' is already running\00"
@.str.4 = [29 x i8] c"Unable to lock PID file '%s'\00"
@.str.5 = [33 x i8] c"Could not truncate PID file '%s'\00"
define i32 @createPidFile(i8* %0, i8* %1, i32 %2) {
%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 = tail call i32 @open(i8* %1, i32 %6, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = tail call i32 @errExit(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %14
14: ; preds = %12, %3
%15 = and i32 %2, 1
%16 = icmp eq i32 %15, 0
br i1 %16, label %31, label %17
17: ; preds = %14
%18 = load i32, i32* @F_GETFD
%19 = tail call i32 (i32, i32, ...) @fcntl(i32 %10, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @errExit(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %23
23: ; preds = %21, %17
%24 = load i32, i32* @FD_CLOEXEC
%25 = or i32 %24, %19
%26 = load i32, i32* @F_SETFD
%27 = tail call i32 (i32, i32, ...) @fcntl(i32 %10, i32 %26, i32 %25)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = tail call i32 @errExit(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %31
31: ; preds = %23, %29, %14
%32 = load i32, i32* @F_WRLCK
%33 = load i32, i32* @SEEK_SET
%34 = tail call i32 @lockRegion(i32 %10, i32 %32, i32 %33, i32 0, i32 0)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %47
36: ; preds = %31
%37 = load i64, i64* @errno
%38 = load i64, i64* @EAGAIN
%39 = icmp eq i64 %37, %38
%40 = load i64, i64* @EACCES
%41 = icmp eq i64 %37, %40
%42 = select i1 %39, i1 true, i1 %41
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = tail call i32 @fatal(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0), i8* %1, i8* %0)
br label %47
45: ; preds = %36
%46 = tail call i32 @errExit(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %1)
br label %47
47: ; preds = %43, %45, %31
%48 = tail call i32 @ftruncate(i32 %10, i32 0)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = tail call i32 @errExit(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %1)
br label %52
52: ; preds = %50, %47
ret i32 %10
}