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

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ ESRCH;
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ LOG_NOTICE;
int /*<<< orphan*/ LogMsg(int /*<<< orphan*/, char *);
int O_CREAT;
int O_EXCL;
int O_RDONLY;
int O_WRONLY;
scalar_t__ atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ kill(scalar_t__, int /*<<< orphan*/);
int open(char const *, int, int);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
scalar_t__ unlink(char const *);
int write(int, char *, int);
int HDBLockFile(const char *LockFile, pid_t LockPid) {
  pid_t Pid;
  int FileDes;
  int N;
  char HDBBuffer[11 + 1];
  char LogStr[255];
  while ((FileDes = open(LockFile, O_CREAT | O_WRONLY | O_EXCL, 0644)) == -1) {
    if ((errno == EEXIST) && ((FileDes = open(LockFile, O_RDONLY, 0)) != -1)) {
      N = read(FileDes, HDBBuffer, 11);
      close(FileDes);
      if (N <= 0) {
        snprintf(LogStr, sizeof(LogStr), "Can't read pid from lock file %s.",
                 LockFile);
        LogStr[sizeof(LogStr) - 1] = '\0';
        LogMsg(LOG_NOTICE, LogStr);
        return (2);
      }
      HDBBuffer[N] = '\0';
      Pid = atoi(HDBBuffer);
      if (Pid == LockPid) {
        snprintf(LogStr, sizeof(LogStr), "Read our pid from lock %s.",
                 LockFile);
        LogStr[sizeof(LogStr) - 1] = '\0';
        LogMsg(LOG_DEBUG, LogStr);
        return (0);
      }
      if ((Pid == 0) || ((kill(Pid, 0) != 0) && (errno == ESRCH)))
        if (unlink(LockFile) == 0) {
          snprintf(LogStr, sizeof(LogStr), "Removed stale lock %s (pid %d).",
                   LockFile, Pid);
          LogStr[sizeof(LogStr) - 1] = '\0';
          LogMsg(LOG_NOTICE, LogStr);
        } else {
          snprintf(LogStr, sizeof(LogStr),
                   "Couldn't remove stale lock %s (pid %d).", LockFile, Pid);
          LogStr[sizeof(LogStr) - 1] = '\0';
          LogMsg(LOG_ERR, LogStr);
          return (2);
        }
      else {
        snprintf(LogStr, sizeof(LogStr), "Lock %s is owned by pid %d.",
                 LockFile, Pid);
        LogStr[sizeof(LogStr) - 1] = '\0';
        LogMsg(LOG_INFO, LogStr);
        return (1);
      }
    } else {
      snprintf(LogStr, sizeof(LogStr), "Can't create lock file %s.", LockFile);
      LogStr[sizeof(LogStr) - 1] = '\0';
      LogMsg(LOG_ERR, LogStr);
      return (2);
    }
  }
  snprintf(HDBBuffer, sizeof(HDBBuffer), "%10d\n", (int)LockPid);
  LogStr[sizeof(HDBBuffer) - 1] = '\0';
  if (write(FileDes, HDBBuffer, 11) != 11) {
    close(FileDes);
    snprintf(LogStr, sizeof(LogStr), "Can't write HDB header to lock file %s.",
             LockFile);
    LogStr[sizeof(LogStr) - 1] = '\0';
    LogMsg(LOG_ERR, LogStr);
    unlink(LockFile);
    return (2);
  }
  close(FileDes);
  return (0);
}


x86 assembly (clang, -O0):
<HDBLockFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    0x2de1(%rip),%rax
or     (%rax),%esi
mov    0x2e08(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    144f <HDBLockFile+0x2bf>
mov    0x2de1(%rip),%rax
mov    (%rax),%rax
mov    0x2de7(%rip),%rcx
cmp    (%rcx),%rax
jne    1407 <HDBLockFile+0x277>
mov    -0x10(%rbp),%rdi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     1407 <HDBLockFile+0x277>
mov    -0x24(%rbp),%edi
lea    -0x34(%rbp),%rsi
mov    $0xb,%edx
call   1080 <read@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
cmpl   $0x0,-0x28(%rbp)
jg     127b <HDBLockFile+0xeb>
lea    -0x140(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xff,%esi
lea    0xdb1(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
movb   $0x0,-0x42(%rbp)
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    -0x140(%rbp),%rsi
call   1060 <LogMsg@plt>
movl   $0x2,-0x4(%rbp)
jmp    14f0 <HDBLockFile+0x360>
movslq -0x28(%rbp),%rax
movb   $0x0,-0x34(%rbp,%rax,1)
lea    -0x34(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    12e2 <HDBLockFile+0x152>
lea    -0x140(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xff,%esi
lea    0xd6c(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
movb   $0x0,-0x42(%rbp)
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
lea    -0x140(%rbp),%rsi
call   1060 <LogMsg@plt>
movl   $0x0,-0x4(%rbp)
jmp    14f0 <HDBLockFile+0x360>
cmpq   $0x0,-0x20(%rbp)
je     131c <HDBLockFile+0x18c>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1090 <kill@plt>
cmp    $0x0,%rax
je     13bb <HDBLockFile+0x22b>
mov    0x2cb7(%rip),%rax
mov    (%rax),%rax
mov    0x2c75(%rip),%rcx
cmp    (%rcx),%rax
jne    13bb <HDBLockFile+0x22b>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jne    136f <HDBLockFile+0x1df>
lea    -0x140(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    $0xff,%esi
lea    0xcf3(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
movb   $0x0,-0x42(%rbp)
mov    0x2c5c(%rip),%rax
mov    (%rax),%edi
lea    -0x140(%rbp),%rsi
call   1060 <LogMsg@plt>
jmp    13b6 <HDBLockFile+0x226>
lea    -0x140(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    $0xff,%esi
lea    0xcd3(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
movb   $0x0,-0x42(%rbp)
mov    0x2bf4(%rip),%rax
mov    (%rax),%edi
lea    -0x140(%rbp),%rsi
call   1060 <LogMsg@plt>
movl   $0x2,-0x4(%rbp)
jmp    14f0 <HDBLockFile+0x360>
jmp    1402 <HDBLockFile+0x272>
lea    -0x140(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    $0xff,%esi
lea    0xcaf(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
movb   $0x0,-0x42(%rbp)
mov    0x2bc0(%rip),%rax
mov    (%rax),%edi
lea    -0x140(%rbp),%rsi
call   1060 <LogMsg@plt>
movl   $0x1,-0x4(%rbp)
jmp    14f0 <HDBLockFile+0x360>
jmp    144a <HDBLockFile+0x2ba>
lea    -0x140(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xff,%esi
lea    0xc83(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
movb   $0x0,-0x42(%rbp)
mov    0x2b60(%rip),%rax
mov    (%rax),%edi
lea    -0x140(%rbp),%rsi
call   1060 <LogMsg@plt>
movl   $0x2,-0x4(%rbp)
jmp    14f0 <HDBLockFile+0x360>
jmp    11a3 <HDBLockFile+0x13>
lea    -0x34(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    %eax,%ecx
mov    $0xc,%esi
lea    0xc57(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
movb   $0x0,-0x135(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x34(%rbp),%rsi
mov    $0xb,%edx
call   1040 <write@plt>
cmp    $0xb,%eax
je     14e1 <HDBLockFile+0x351>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
lea    -0x140(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xff,%esi
lea    0xc16(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
movb   $0x0,-0x42(%rbp)
mov    0x2ad2(%rip),%rax
mov    (%rax),%edi
lea    -0x140(%rbp),%rsi
call   1060 <LogMsg@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    14f0 <HDBLockFile+0x360>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HDBLockFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
mov    0x2e50(%rip),%rbx
mov    0x2de9(%rip),%r15
mov    (%r15),%eax
or     (%rbx),%eax
mov    %rsi,%rbp
mov    %rdi,%r14
mov    0x2e07(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
je     1267 <HDBLockFile+0xd7>
lea    0xeda(%rip),%rdx
xor    %r15d,%r15d
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <snprintf@plt>
movb   $0x0,0x2b(%rsp)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0xb,%edx
call   1040 <write@plt>
mov    %eax,%ebx
mov    %r12d,%edi
call   1070 <close@plt>
cmp    $0xb,%ebx
je     14af <HDBLockFile+0x31f>
lea    0xe98(%rip),%rdx
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
movb   $0x0,0x11e(%rsp)
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1060 <LogMsg@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
jmp    14a9 <HDBLockFile+0x319>
mov    %rbp,0x8(%rsp)
mov    0x2d4d(%rip),%rax
mov    (%rax),%rax
mov    0x2d53(%rip),%rcx
cmp    (%rcx),%rax
jne    139c <HDBLockFile+0x20c>
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
xor    %edx,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     139c <HDBLockFile+0x20c>
mov    %eax,%ebp
mov    %rbx,%r12
mov    %eax,%edi
lea    0x14(%rsp),%r13
mov    %r13,%rsi
mov    $0xb,%edx
call   1080 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1070 <close@plt>
test   %ebx,%ebx
jle    1404 <HDBLockFile+0x274>
mov    %ebx,%eax
movb   $0x0,0x14(%rsp,%rax,1)
mov    %r13,%rdi
call   10b0 <atoi@plt>
cmp    0x8(%rsp),%rax
je     1433 <HDBLockFile+0x2a3>
mov    %rax,%rbp
test   %rax,%rax
je     131b <HDBLockFile+0x18b>
mov    %rbp,%rdi
xor    %esi,%esi
call   1090 <kill@plt>
test   %rax,%rax
je     13bf <HDBLockFile+0x22f>
mov    0x2cb8(%rip),%rax
mov    (%rax),%rax
mov    0x2c76(%rip),%rcx
cmp    (%rcx),%rax
jne    13bf <HDBLockFile+0x22f>
mov    %r14,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
jne    146f <HDBLockFile+0x2df>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
lea    0xcfd(%rip),%rdx
mov    %r14,%rcx
mov    %rbp,%r8
xor    %eax,%eax
call   1050 <snprintf@plt>
movb   $0x0,0x11e(%rsp)
mov    0x2c5c(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1060 <LogMsg@plt>
mov    (%r15),%esi
mov    %r12,%rbx
or     (%r12),%esi
mov    0x2c51(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x1a4,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     126c <HDBLockFile+0xdc>
mov    %eax,%r12d
mov    0x8(%rsp),%rbp
jmp    11db <HDBLockFile+0x4b>
lea    0xcfe(%rip),%rdx
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
jmp    1490 <HDBLockFile+0x300>
lea    0xcbf(%rip),%rdx
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r14,%rcx
mov    %rbp,%r8
xor    %eax,%eax
call   1050 <snprintf@plt>
movb   $0x0,0x11e(%rsp)
mov    0x2bb9(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1060 <LogMsg@plt>
mov    $0x1,%r15d
jmp    14af <HDBLockFile+0x31f>
lea    0xbf5(%rip),%rdx
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
movb   $0x0,0x11e(%rsp)
mov    0x2b87(%rip),%rax
jmp    149f <HDBLockFile+0x30f>
lea    0xbe8(%rip),%rdx
xor    %r15d,%r15d
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
movb   $0x0,0x11e(%rsp)
mov    0x2b4d(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1060 <LogMsg@plt>
jmp    14af <HDBLockFile+0x31f>
lea    0xbe7(%rip),%rdx
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r14,%rcx
mov    %rbp,%r8
xor    %eax,%eax
call   1050 <snprintf@plt>
movb   $0x0,0x11e(%rsp)
mov    0x2af1(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1060 <LogMsg@plt>
mov    $0x2,%r15d
mov    %r15d,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@O_RDONLY = global i32 0
@.str = [34 x i8] c"Can't read pid from lock file %s.\00"
@LOG_NOTICE = global i32 0
@.str.1 = [27 x i8] c"Read our pid from lock %s.\00"
@LOG_DEBUG = global i32 0
@ESRCH = global i64 0
@.str.2 = [32 x i8] c"Removed stale lock %s (pid %d).\00"
@.str.3 = [40 x i8] c"Couldn't remove stale lock %s (pid %d).\00"
@LOG_ERR = global i32 0
@.str.4 = [28 x i8] c"Lock %s is owned by pid %d.\00"
@LOG_INFO = global i32 0
@.str.5 = [27 x i8] c"Can't create lock file %s.\00"
@.str.6 = [6 x i8] c"%10d\0A\00"
@.str.7 = [40 x i8] c"Can't write HDB header to lock file %s.\00"
define i32 @HDBLockFile(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca [12 x i8]
%10 = alloca [255 x i8]
store i8* %0, i8** %4
store i64 %1, i64* %5
br label %11
11: ; preds = %114, %2
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_WRONLY
%15 = or i32 %13, %14
%16 = load i32, i32* @O_EXCL
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %12, i32 %17, i32 420)
store i32 %18, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %115
20: ; preds = %11
%21 = load i64, i64* @errno
%22 = load i64, i64* @EEXIST
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %106
24: ; preds = %20
%25 = load i8*, i8** %4
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* %25, i32 %26, i32 0)
store i32 %27, i32* %7
%28 = icmp ne i32 %27, -1
br i1 %28, label %29, label %106
29: ; preds = %24
%30 = load i32, i32* %7
%31 = getelementptr inbounds [12 x i8], [12 x i8]* %9, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 11)
store i32 %32, i32* %8
%33 = load i32, i32* %7
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %8
%36 = icmp sle i32 %35, 0
br i1 %36, label %37, label %45
37: ; preds = %29
%38 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%39 = load i8*, i8** %4
%40 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %38, i32 255, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %39)
%41 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 254
store i8 0, i8* %41
%42 = load i32, i32* @LOG_NOTICE
%43 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%44 = call i32 @LogMsg(i32 %42, i8* %43)
store i32 2, i32* %3
br label %140
45: ; preds = %29
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [12 x i8], [12 x i8]* %9, i64 0, i64 %47
store i8 0, i8* %48
%49 = getelementptr inbounds [12 x i8], [12 x i8]* %9, i64 0, i64 0
%50 = call i64 @atoi(i8* %49)
store i64 %50, i64* %6
%51 = load i64, i64* %6
%52 = load i64, i64* %5
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %62
54: ; preds = %45
%55 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%56 = load i8*, i8** %4
%57 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %55, i32 255, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %56)
%58 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 254
store i8 0, i8* %58
%59 = load i32, i32* @LOG_DEBUG
%60 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%61 = call i32 @LogMsg(i32 %59, i8* %60)
store i32 0, i32* %3
br label %140
62: ; preds = %45
%63 = load i64, i64* %6
%64 = icmp eq i64 %63, 0
br i1 %64, label %73, label %65
65: ; preds = %62
%66 = load i64, i64* %6
%67 = call i64 @kill(i64 %66, i32 0)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %96
69: ; preds = %65
%70 = load i64, i64* @errno
%71 = load i64, i64* @ESRCH
%72 = icmp eq i64 %70, %71
br i1 %72, label %73, label %96
73: ; preds = %69, %62
%74 = load i8*, i8** %4
%75 = call i64 @unlink(i8* %74)
%76 = icmp eq i64 %75, 0
br i1 %76, label %77, label %86
77: ; preds = %73
%78 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%79 = load i8*, i8** %4
%80 = load i64, i64* %6
%81 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %78, i32 255, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %79, i64 %80)
%82 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 254
store i8 0, i8* %82
%83 = load i32, i32* @LOG_NOTICE
%84 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%85 = call i32 @LogMsg(i32 %83, i8* %84)
br label %95
86: ; preds = %73
%87 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%88 = load i8*, i8** %4
%89 = load i64, i64* %6
%90 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %87, i32 255, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %88, i64 %89)
%91 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 254
store i8 0, i8* %91
%92 = load i32, i32* @LOG_ERR
%93 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%94 = call i32 @LogMsg(i32 %92, i8* %93)
store i32 2, i32* %3
br label %140
95: ; preds = %77
br label %105
96: ; preds = %69, %65
%97 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%98 = load i8*, i8** %4
%99 = load i64, i64* %6
%100 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %97, i32 255, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %98, i64 %99)
%101 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 254
store i8 0, i8* %101
%102 = load i32, i32* @LOG_INFO
%103 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%104 = call i32 @LogMsg(i32 %102, i8* %103)
store i32 1, i32* %3
br label %140
105: ; preds = %95
br label %114
106: ; preds = %24, %20
%107 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%108 = load i8*, i8** %4
%109 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %107, i32 255, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %108)
%110 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 254
store i8 0, i8* %110
%111 = load i32, i32* @LOG_ERR
%112 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%113 = call i32 @LogMsg(i32 %111, i8* %112)
store i32 2, i32* %3
br label %140
114: ; preds = %105
br label %11
115: ; preds = %11
%116 = getelementptr inbounds [12 x i8], [12 x i8]* %9, i64 0, i64 0
%117 = load i64, i64* %5
%118 = trunc i64 %117 to i32
%119 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %116, i32 12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %118)
%120 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 11
store i8 0, i8* %120
%121 = load i32, i32* %7
%122 = getelementptr inbounds [12 x i8], [12 x i8]* %9, i64 0, i64 0
%123 = call i32 @write(i32 %121, i8* %122, i32 11)
%124 = icmp ne i32 %123, 11
br i1 %124, label %125, label %137
125: ; preds = %115
%126 = load i32, i32* %7
%127 = call i32 @close(i32 %126)
%128 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%129 = load i8*, i8** %4
%130 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %128, i32 255, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0), i8* %129)
%131 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 254
store i8 0, i8* %131
%132 = load i32, i32* @LOG_ERR
%133 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%134 = call i32 @LogMsg(i32 %132, i8* %133)
%135 = load i8*, i8** %4
%136 = call i64 @unlink(i8* %135)
store i32 2, i32* %3
br label %140
137: ; preds = %115
%138 = load i32, i32* %7
%139 = call i32 @close(i32 %138)
store i32 0, i32* %3
br label %140
140: ; preds = %137, %125, %106, %96, %86, %54, %37
%141 = load i32, i32* %3
ret i32 %141
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@O_RDONLY = global i32 0
@.str = [34 x i8] c"Can't read pid from lock file %s.\00"
@LOG_NOTICE = global i32 0
@.str.1 = [27 x i8] c"Read our pid from lock %s.\00"
@LOG_DEBUG = global i32 0
@ESRCH = global i64 0
@.str.2 = [32 x i8] c"Removed stale lock %s (pid %d).\00"
@.str.3 = [40 x i8] c"Couldn't remove stale lock %s (pid %d).\00"
@LOG_ERR = global i32 0
@.str.4 = [28 x i8] c"Lock %s is owned by pid %d.\00"
@LOG_INFO = global i32 0
@.str.5 = [27 x i8] c"Can't create lock file %s.\00"
@.str.6 = [6 x i8] c"%10d\0A\00"
@.str.7 = [40 x i8] c"Can't write HDB header to lock file %s.\00"
define i32 @HDBLockFile(i8* %0, i64 %1) {
%3 = alloca [12 x i8]
%4 = alloca [255 x i8]
%5 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_WRONLY
%9 = or i32 %8, %7
%10 = load i32, i32* @O_EXCL
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %0, i32 %11, i32 420)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %76
14: ; preds = %2
%15 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 254
br label %16
16: ; preds = %14, %53
%17 = load i64, i64* @errno
%18 = load i64, i64* @EEXIST
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %72
20: ; preds = %16
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %0, i32 %21, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %72, label %24
24: ; preds = %20
%25 = call i32 @read(i32 %22, i8* %5, i32 11)
%26 = call i32 @close(i32 %22)
%27 = icmp slt i32 %25, 1
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0)
store i8 0, i8* %15
%30 = load i32, i32* @LOG_NOTICE
%31 = call i32 @LogMsg(i32 %30, i8* %6)
br label %90
32: ; preds = %24
%33 = zext i32 %25 to i64
%34 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 %33
store i8 0, i8* %34
%35 = call i64 @atoi(i8* %5)
%36 = icmp eq i64 %35, %1
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
store i8 0, i8* %15
%39 = load i32, i32* @LOG_DEBUG
%40 = call i32 @LogMsg(i32 %39, i8* %6)
br label %90
41: ; preds = %32
%42 = icmp eq i64 %35, 0
br i1 %42, label %50, label %43
43: ; preds = %41
%44 = call i64 @kill(i64 %35, i32 0)
%45 = icmp eq i64 %44, 0
br i1 %45, label %68, label %46
46: ; preds = %43
%47 = load i64, i64* @errno
%48 = load i64, i64* @ESRCH
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %68
50: ; preds = %46, %41
%51 = call i64 @unlink(i8* %0)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %64
53: ; preds = %50
%54 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %0, i64 %35)
store i8 0, i8* %15
%55 = load i32, i32* @LOG_NOTICE
%56 = call i32 @LogMsg(i32 %55, i8* %6)
%57 = load i32, i32* @O_CREAT
%58 = load i32, i32* @O_WRONLY
%59 = or i32 %58, %57
%60 = load i32, i32* @O_EXCL
%61 = or i32 %59, %60
%62 = call i32 @open(i8* %0, i32 %61, i32 420)
%63 = icmp eq i32 %62, -1
br i1 %63, label %16, label %76
64: ; preds = %50
%65 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %0, i64 %35)
store i8 0, i8* %15
%66 = load i32, i32* @LOG_ERR
%67 = call i32 @LogMsg(i32 %66, i8* %6)
br label %90
68: ; preds = %46, %43
%69 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %0, i64 %35)
store i8 0, i8* %15
%70 = load i32, i32* @LOG_INFO
%71 = call i32 @LogMsg(i32 %70, i8* %6)
br label %90
72: ; preds = %20, %16
%73 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %0)
store i8 0, i8* %15
%74 = load i32, i32* @LOG_ERR
%75 = call i32 @LogMsg(i32 %74, i8* %6)
br label %90
76: ; preds = %53, %2
%77 = phi i32 [ %12, %2 ], [ %62, %53 ]
%78 = trunc i64 %1 to i32
%79 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %5, i32 12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %78)
%80 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 11
store i8 0, i8* %80
%81 = call i32 @write(i32 %77, i8* %5, i32 11)
%82 = icmp eq i32 %81, 11
%83 = call i32 @close(i32 %77)
br i1 %82, label %90, label %84
84: ; preds = %76
%85 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0), i8* %0)
%86 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 254
store i8 0, i8* %86
%87 = load i32, i32* @LOG_ERR
%88 = call i32 @LogMsg(i32 %87, i8* %6)
%89 = call i64 @unlink(i8* %0)
br label %90
90: ; preds = %76, %84, %72, %68, %64, %37, %28
%91 = phi i32 [ 2, %28 ], [ 0, %37 ], [ 2, %64 ], [ 1, %68 ], [ 2, %72 ], [ 2, %84 ], [ 0, %76 ]
ret i32 %91
}