#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*/ FILE;

/* Variables and functions */
scalar_t__ EEXIST;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ LOG_NOTICE;
scalar_t__ errno;
scalar_t__ fchmod(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int getpid();
scalar_t__ kill(int, int /*<<< orphan*/);
scalar_t__ link(char *, char const *);
int mkstemp(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ strerror(scalar_t__);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *, ...);
int /*<<< orphan*/ unlink(char const *);
int lockfile(const char *path) {
  FILE *f = fopen(path, "r");
  if (f) {
    int pid = -1;
    fscanf(f, "%d", &pid);
    fclose(f);
    if (pid > 0 && kill(pid, 0) == 0) {
      syslog(LOG_NOTICE, "lock (%s) is taken", path);
      return 0;
    }
    syslog(LOG_NOTICE, "lock (%s) is stale, removing", path);
    unlink(path);
  }
  char tmp[1024];
  snprintf(tmp, sizeof tmp, "%s.tmp.XXXXXX", path);
  int fd = mkstemp(tmp);
  if (fd < 0) {
    syslog(LOG_ERR, "could not create lock tmpfile %s: %s", tmp,
           strerror(errno));
    return 0;
  }
  if (fchmod(fd, 0644) < 0) {
    syslog(LOG_ERR, "could not chmod lock tmpfile %s: %s", tmp,
           strerror(errno));
    unlink(tmp);
    return 0;
  }
  f = fdopen(fd, "w");
  if (!f) {
    syslog(LOG_ERR, "error wrapping tmpfile %s: %s", tmp, strerror(errno));
    unlink(tmp);
    return 0;
  }
  fprintf(f, "%10d\n", getpid());
  fflush(f);
  if (fchmod(fd, 0644) < 0) {
    syslog(LOG_ERR, "could not chmod lock tmpfile %s: %s", tmp,
           strerror(errno));
    unlink(tmp);
    return 0;
  }
  fclose(f);
  if (link(tmp, path) < 0) {
    if (errno == EEXIST)
      syslog(LOG_NOTICE, "someone beat us to create %s", path);
    else
      syslog(LOG_NOTICE, "could not create %s: %s", path, strerror(errno));
    unlink(tmp);
    return 0;
  }
  syslog(LOG_INFO, "got lock %s", path);
  unlink(tmp);
  return 1;
}


x86 assembly (clang, -O0):
<lockfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12ab <lockfile+0xbb>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdd2(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1070 <fscanf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    1287 <lockfile+0x97>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1090 <kill@plt>
cmp    $0x0,%rax
jne    1287 <lockfile+0x97>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd91(%rip),%rsi
mov    $0x0,%al
call   10b0 <syslog@plt>
movl   $0x0,-0x4(%rbp)
jmp    15cc <lockfile+0x3dc>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd7d(%rip),%rsi
mov    $0x0,%al
call   10b0 <syslog@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x400,%esi
lea    0xd73(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x420(%rbp),%rdi
call   10c0 <mkstemp@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jge    133b <lockfile+0x14b>
mov    0x2ccb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x434(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    0x2cce(%rip),%rax
mov    (%rax),%rdi
call   1110 <strerror@plt>
mov    -0x434(%rbp),%edi
mov    -0x430(%rbp),%rdx
mov    %eax,%ecx
lea    0xd1b(%rip),%rsi
mov    $0x0,%al
call   10b0 <syslog@plt>
movl   $0x0,-0x4(%rbp)
jmp    15cc <lockfile+0x3dc>
mov    -0x424(%rbp),%edi
mov    $0x1a4,%esi
call   10e0 <fchmod@plt>
cmp    $0x0,%rax
jge    13b6 <lockfile+0x1c6>
mov    0x2c5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x444(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    0x2c5f(%rip),%rax
mov    (%rax),%rdi
call   1110 <strerror@plt>
mov    -0x444(%rbp),%edi
mov    -0x440(%rbp),%rdx
mov    %eax,%ecx
lea    0xcd1(%rip),%rsi
mov    $0x0,%al
call   10b0 <syslog@plt>
lea    -0x420(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    15cc <lockfile+0x3dc>
mov    -0x424(%rbp),%edi
lea    0xcc9(%rip),%rsi
call   10d0 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1438 <lockfile+0x248>
mov    0x2bda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x454(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    0x2bdd(%rip),%rax
mov    (%rax),%rdi
call   1110 <strerror@plt>
mov    -0x454(%rbp),%edi
mov    -0x450(%rbp),%rdx
mov    %eax,%ecx
lea    0xc75(%rip),%rsi
mov    $0x0,%al
call   10b0 <syslog@plt>
lea    -0x420(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    15cc <lockfile+0x3dc>
mov    -0x18(%rbp),%rax
mov    %rax,-0x460(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x460(%rbp),%rdi
mov    %eax,%edx
lea    0xc52(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <fflush@plt>
mov    -0x424(%rbp),%edi
mov    $0x1a4,%esi
call   10e0 <fchmod@plt>
cmp    $0x0,%rax
jge    14e3 <lockfile+0x2f3>
mov    0x2b2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x46c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x468(%rbp)
mov    0x2b32(%rip),%rax
mov    (%rax),%rdi
call   1110 <strerror@plt>
mov    -0x46c(%rbp),%edi
mov    -0x468(%rbp),%rdx
mov    %eax,%ecx
lea    0xba4(%rip),%rsi
mov    $0x0,%al
call   10b0 <syslog@plt>
lea    -0x420(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    15cc <lockfile+0x3dc>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1100 <link@plt>
cmp    $0x0,%rax
jge    159e <lockfile+0x3ae>
mov    0x2acb(%rip),%rax
mov    (%rax),%rax
mov    0x2ac9(%rip),%rcx
cmp    (%rcx),%rax
jne    1540 <lockfile+0x350>
mov    0x2aa9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xb7e(%rip),%rsi
mov    $0x0,%al
call   10b0 <syslog@plt>
jmp    1586 <lockfile+0x396>
mov    0x2a89(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x47c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x478(%rbp)
mov    0x2a77(%rip),%rax
mov    (%rax),%rdi
call   1110 <strerror@plt>
mov    -0x47c(%rbp),%edi
mov    -0x478(%rbp),%rdx
mov    %eax,%ecx
lea    0xb50(%rip),%rsi
mov    $0x0,%al
call   10b0 <syslog@plt>
lea    -0x420(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    15cc <lockfile+0x3dc>
mov    0x2a23(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xb35(%rip),%rsi
mov    $0x0,%al
call   10b0 <syslog@plt>
lea    -0x420(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x480,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lockfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r15
lea    0xdf9(%rip),%rsi
call   10f0 <fopen@plt>
test   %rax,%rax
je     1271 <lockfile+0x81>
mov    %rax,%rbx
movl   $0xffffffff,(%rsp)
lea    0xde0(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1070 <fscanf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    (%rsp),%edi
test   %edi,%edi
jle    124f <lockfile+0x5f>
xor    %r14d,%r14d
xor    %esi,%esi
call   1090 <kill@plt>
test   %rax,%rax
je     13a0 <lockfile+0x1b0>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
lea    0xdb9(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   10b0 <syslog@plt>
mov    %r15,%rdi
call   1030 <unlink@plt>
lea    0xdbd(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rcx
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   10c0 <mkstemp@plt>
test   %eax,%eax
js     134d <lockfile+0x15d>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x1a4,%esi
call   10e0 <fchmod@plt>
test   %rax,%rax
js     137f <lockfile+0x18f>
lea    0xdd3(%rip),%rsi
mov    %ebp,%edi
call   10d0 <fdopen@plt>
test   %rax,%rax
je     13bc <lockfile+0x1cc>
mov    %rax,%rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdcf(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   10a0 <fflush@plt>
mov    %ebp,%edi
mov    $0x1a4,%esi
call   10e0 <fchmod@plt>
test   %rax,%rax
js     1408 <lockfile+0x218>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %rsp,%rdi
mov    %r15,%rsi
call   1100 <link@plt>
test   %rax,%rax
js     1429 <lockfile+0x239>
mov    0x2ca1(%rip),%rax
mov    (%rax),%edi
lea    0xdb7(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   10b0 <syslog@plt>
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    $0x1,%r14d
jmp    13f7 <lockfile+0x207>
mov    0x2c64(%rip),%rax
mov    (%rax),%ebx
mov    0x2c7b(%rip),%rax
mov    (%rax),%rdi
call   1110 <strerror@plt>
lea    0xcd7(%rip),%rsi
xor    %r14d,%r14d
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %eax,%ecx
xor    %eax,%eax
call   10b0 <syslog@plt>
jmp    13f7 <lockfile+0x207>
mov    0x2c32(%rip),%rax
mov    (%rax),%ebx
mov    0x2c49(%rip),%rax
mov    (%rax),%rdi
call   1110 <strerror@plt>
lea    0xcca(%rip),%rsi
jmp    13db <lockfile+0x1eb>
mov    0x2c29(%rip),%rax
mov    (%rax),%edi
lea    0xc55(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   10b0 <syslog@plt>
jmp    13f7 <lockfile+0x207>
mov    0x2bf5(%rip),%rax
mov    (%rax),%ebx
mov    0x2c0c(%rip),%rax
mov    (%rax),%rdi
call   1110 <strerror@plt>
lea    0xcb3(%rip),%rsi
xor    %r14d,%r14d
mov    %rsp,%rbp
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   10b0 <syslog@plt>
mov    %rbp,%rdi
call   1030 <unlink@plt>
mov    %r14d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ba9(%rip),%rax
mov    (%rax),%ebx
mov    0x2bc0(%rip),%rax
mov    (%rax),%rdi
call   1110 <strerror@plt>
lea    0xc41(%rip),%rsi
jmp    13de <lockfile+0x1ee>
mov    0x2ba8(%rip),%rax
mov    (%rax),%rdi
mov    0x2ba6(%rip),%rax
mov    0x2b8f(%rip),%rcx
mov    (%rcx),%ebx
cmp    (%rax),%rdi
jne    145d <lockfile+0x26d>
lea    0xc63(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
xor    %eax,%eax
call   10b0 <syslog@plt>
jmp    1477 <lockfile+0x287>
call   1110 <strerror@plt>
lea    0xc66(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   10b0 <syslog@plt>
mov    %rsp,%rdi
call   1030 <unlink@plt>
xor    %r14d,%r14d
jmp    13f7 <lockfile+0x207>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
@LOG_NOTICE = global i32 0
@.str.2 = [19 x i8] c"lock (%s) is taken\00"
@.str.3 = [29 x i8] c"lock (%s) is stale, removing\00"
@.str.4 = [14 x i8] c"%s.tmp.XXXXXX\00"
@LOG_ERR = global i32 0
@.str.5 = [37 x i8] c"could not create lock tmpfile %s: %s\00"
@errno = global i64 0
@.str.6 = [36 x i8] c"could not chmod lock tmpfile %s: %s\00"
@.str.7 = [2 x i8] c"w\00"
@.str.8 = [30 x i8] c"error wrapping tmpfile %s: %s\00"
@.str.9 = [6 x i8] c"%10d\0A\00"
@EEXIST = global i64 0
@.str.10 = [29 x i8] c"someone beat us to create %s\00"
@.str.11 = [24 x i8] c"could not create %s: %s\00"
@LOG_INFO = global i32 0
@.str.12 = [12 x i8] c"got lock %s\00"
define i32 @lockfile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %33
12: ; preds = %1
store i32 -1, i32* %5
%13 = load i32*, i32** %4
%14 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%15 = load i32*, i32** %4
%16 = call i32 @fclose(i32* %15)
%17 = load i32, i32* %5
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %12
%20 = load i32, i32* %5
%21 = call i64 @kill(i32 %20, i32 0)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i32, i32* @LOG_NOTICE
%25 = load i8*, i8** %3
%26 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %24, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %25)
store i32 0, i32* %2
br label %119
27: ; preds = %19, %12
%28 = load i32, i32* @LOG_NOTICE
%29 = load i8*, i8** %3
%30 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %3
%32 = call i32 @unlink(i8* %31)
br label %33
33: ; preds = %27, %1
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%35 = load i8*, i8** %3
%36 = call i32 @snprintf(i8* %34, i32 1024, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %35)
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%38 = call i32 @mkstemp(i8* %37)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %33
%42 = load i32, i32* @LOG_ERR
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%44 = load i64, i64* @errno
%45 = call i32 @strerror(i64 %44)
%46 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %42, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %43, i32 %45)
store i32 0, i32* %2
br label %119
47: ; preds = %33
%48 = load i32, i32* %7
%49 = call i64 @fchmod(i32 %48, i32 420)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %59
51: ; preds = %47
%52 = load i32, i32* @LOG_ERR
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%54 = load i64, i64* @errno
%55 = call i32 @strerror(i64 %54)
%56 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %52, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i8* %53, i32 %55)
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%58 = call i32 @unlink(i8* %57)
store i32 0, i32* %2
br label %119
59: ; preds = %47
%60 = load i32, i32* %7
%61 = call i32* @fdopen(i32 %60, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
store i32* %61, i32** %4
%62 = load i32*, i32** %4
%63 = icmp ne i32* %62, null
br i1 %63, label %72, label %64
64: ; preds = %59
%65 = load i32, i32* @LOG_ERR
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%67 = load i64, i64* @errno
%68 = call i32 @strerror(i64 %67)
%69 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %65, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0), i8* %66, i32 %68)
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%71 = call i32 @unlink(i8* %70)
store i32 0, i32* %2
br label %119
72: ; preds = %59
%73 = load i32*, i32** %4
%74 = call i32 (...) @getpid()
%75 = call i32 @fprintf(i32* %73, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i32 %74)
%76 = load i32*, i32** %4
%77 = call i32 @fflush(i32* %76)
%78 = load i32, i32* %7
%79 = call i64 @fchmod(i32 %78, i32 420)
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %89
81: ; preds = %72
%82 = load i32, i32* @LOG_ERR
%83 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%84 = load i64, i64* @errno
%85 = call i32 @strerror(i64 %84)
%86 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %82, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i8* %83, i32 %85)
%87 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%88 = call i32 @unlink(i8* %87)
store i32 0, i32* %2
br label %119
89: ; preds = %72
%90 = load i32*, i32** %4
%91 = call i32 @fclose(i32* %90)
%92 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%93 = load i8*, i8** %3
%94 = call i64 @link(i8* %92, i8* %93)
%95 = icmp slt i64 %94, 0
br i1 %95, label %96, label %113
96: ; preds = %89
%97 = load i64, i64* @errno
%98 = load i64, i64* @EEXIST
%99 = icmp eq i64 %97, %98
br i1 %99, label %100, label %104
100: ; preds = %96
%101 = load i32, i32* @LOG_NOTICE
%102 = load i8*, i8** %3
%103 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %101, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.10, i64 0, i64 0), i8* %102)
br label %110
104: ; preds = %96
%105 = load i32, i32* @LOG_NOTICE
%106 = load i8*, i8** %3
%107 = load i64, i64* @errno
%108 = call i32 @strerror(i64 %107)
%109 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %105, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.11, i64 0, i64 0), i8* %106, i32 %108)
br label %110
110: ; preds = %104, %100
%111 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%112 = call i32 @unlink(i8* %111)
store i32 0, i32* %2
br label %119
113: ; preds = %89
%114 = load i32, i32* @LOG_INFO
%115 = load i8*, i8** %3
%116 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %114, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.12, i64 0, i64 0), i8* %115)
%117 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%118 = call i32 @unlink(i8* %117)
store i32 1, i32* %2
br label %119
119: ; preds = %113, %110, %81, %64, %51, %41, %23
%120 = load i32, i32* %2
ret i32 %120
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
@LOG_NOTICE = global i32 0
@.str.2 = [19 x i8] c"lock (%s) is taken\00"
@.str.3 = [29 x i8] c"lock (%s) is stale, removing\00"
@.str.4 = [14 x i8] c"%s.tmp.XXXXXX\00"
@LOG_ERR = global i32 0
@.str.5 = [37 x i8] c"could not create lock tmpfile %s: %s\00"
@errno = global i64 0
@.str.6 = [36 x i8] c"could not chmod lock tmpfile %s: %s\00"
@.str.7 = [2 x i8] c"w\00"
@.str.8 = [30 x i8] c"error wrapping tmpfile %s: %s\00"
@.str.9 = [6 x i8] c"%10d\0A\00"
@EEXIST = global i64 0
@.str.10 = [29 x i8] c"someone beat us to create %s\00"
@.str.11 = [24 x i8] c"could not create %s: %s\00"
@LOG_INFO = global i32 0
@.str.12 = [12 x i8] c"got lock %s\00"
define i32 @lockfile(i8* %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %22, label %6
6: ; preds = %1
%7 = bitcast i32* %2 to i8*
store i32 -1, i32* %2
%8 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%9 = call i32 @fclose(i32* %4)
%10 = load i32, i32* %2
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %6
%13 = call i64 @kill(i32 %10, i32 0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @LOG_NOTICE
%17 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %16, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %84
18: ; preds = %12, %6
%19 = load i32, i32* @LOG_NOTICE
%20 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %19, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%21 = call i32 @unlink(i8* %0)
br label %22
22: ; preds = %18, %1
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%24 = call i32 @snprintf(i8* %23, i32 1024, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%25 = call i32 @mkstemp(i8* %23)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i32, i32* @LOG_ERR
%29 = load i64, i64* @errno
%30 = call i32 @strerror(i64 %29)
%31 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %28, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %23, i32 %30)
br label %82
32: ; preds = %22
%33 = call i64 @fchmod(i32 %25, i32 420)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = load i32, i32* @LOG_ERR
%37 = load i64, i64* @errno
%38 = call i32 @strerror(i64 %37)
%39 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %36, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i8* %23, i32 %38)
%40 = call i32 @unlink(i8* %23)
br label %82
41: ; preds = %32
%42 = call i32* @fdopen(i32 %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %50
44: ; preds = %41
%45 = load i32, i32* @LOG_ERR
%46 = load i64, i64* @errno
%47 = call i32 @strerror(i64 %46)
%48 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %45, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0), i8* %23, i32 %47)
%49 = call i32 @unlink(i8* %23)
br label %82
50: ; preds = %41
%51 = call i32 (...) @getpid()
%52 = call i32 @fprintf(i32* %42, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i32 %51)
%53 = call i32 @fflush(i32* %42)
%54 = call i64 @fchmod(i32 %25, i32 420)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %62
56: ; preds = %50
%57 = load i32, i32* @LOG_ERR
%58 = load i64, i64* @errno
%59 = call i32 @strerror(i64 %58)
%60 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %57, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i8* %23, i32 %59)
%61 = call i32 @unlink(i8* %23)
br label %82
62: ; preds = %50
%63 = call i32 @fclose(i32* %42)
%64 = call i64 @link(i8* %23, i8* %0)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %78
66: ; preds = %62
%67 = load i64, i64* @errno
%68 = load i64, i64* @EEXIST
%69 = icmp eq i64 %67, %68
%70 = load i32, i32* @LOG_NOTICE
br i1 %69, label %71, label %73
71: ; preds = %66
%72 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %70, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.10, i64 0, i64 0), i8* %0)
br label %76
73: ; preds = %66
%74 = call i32 @strerror(i64 %67)
%75 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %70, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.11, i64 0, i64 0), i8* %0, i32 %74)
br label %76
76: ; preds = %73, %71
%77 = call i32 @unlink(i8* %23)
br label %82
78: ; preds = %62
%79 = load i32, i32* @LOG_INFO
%80 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %79, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.12, i64 0, i64 0), i8* %0)
%81 = call i32 @unlink(i8* %23)
br label %82
82: ; preds = %78, %76, %56, %44, %35, %27
%83 = phi i32 [ 0, %27 ], [ 0, %35 ], [ 0, %56 ], [ 0, %76 ], [ 1, %78 ], [ 0, %44 ]
br label %84
84: ; preds = %15, %82
%85 = phi i32 [ %83, %82 ], [ 0, %15 ]
ret i32 %85
}