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

/* Variables and functions */
int EEXIST;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int getpid();
scalar_t__ kill(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ op_file_readable(char const *);
int /*<<< orphan*/ op_read_lock_file(char const *);
int /*<<< orphan*/ stderr;
int unlink(char const *);
int op_write_lock_file(char const *file) {
  FILE *fp;
  if (op_file_readable(file)) {
    pid_t pid = op_read_lock_file(file);
    if (kill(pid, 0)) {
      int err = unlink(file);
      fprintf(stderr, "Removing stale lock file %s\n", file);
      if (err)
        return err;
    } else {
      return EEXIST;
    }
  }
  fp = fopen(file, "w");
  if (!fp)
    return errno;
  fprintf(fp, "%d", getpid());
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<op_write_lock_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <op_file_readable@plt>
cmp    $0x0,%rax
je     1217 <op_write_lock_file+0x97>
mov    -0x10(%rbp),%rdi
call   10a0 <op_read_lock_file@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1080 <kill@plt>
cmp    $0x0,%rax
je     1201 <op_write_lock_file+0x81>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e06(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe20(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
cmpl   $0x0,-0x20(%rbp)
je     11fc <op_write_lock_file+0x7c>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    127a <op_write_lock_file+0xfa>
jmp    1212 <op_write_lock_file+0x92>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127a <op_write_lock_file+0xfa>
jmp    1217 <op_write_lock_file+0x97>
mov    -0x10(%rbp),%rdi
lea    0xdfb(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1247 <op_write_lock_file+0xc7>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127a <op_write_lock_file+0xfa>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
lea    0xdbc(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<op_write_lock_file>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1070 <op_file_readable@plt>
test   %rax,%rax
je     11cf <op_write_lock_file+0x4f>
mov    %rbx,%rdi
call   10a0 <op_read_lock_file@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1080 <kill@plt>
test   %rax,%rax
je     120c <op_write_lock_file+0x8c>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %eax,%ebp
mov    0x2e21(%rip),%rax
movslq (%rax),%rdi
lea    0xe3f(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %ebp,%ebp
jne    121e <op_write_lock_file+0x9e>
lea    0xe47(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1215 <op_write_lock_file+0x95>
mov    %rax,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe29(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    121e <op_write_lock_file+0x9e>
mov    0x2dcd(%rip),%rax
jmp    121c <op_write_lock_file+0x9c>
mov    0x2db4(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"Removing stale lock file %s\0A\00"
@EEXIST = global i32 0
@.str.1 = [2 x i8] c"w\00"
@errno = global i32 0
@.str.2 = [3 x i8] c"%d\00"
define i32 @op_write_lock_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @op_file_readable(i8* %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %32
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @op_read_lock_file(i8* %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = call i64 @kill(i32 %13, i32 0)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %10
%17 = load i8*, i8** %3
%18 = call i32 @unlink(i8* %17)
store i32 %18, i32* %6
%19 = load i32, i32* @stderr
%20 = sext i32 %19 to i64
%21 = inttoptr i64 %20 to i32*
%22 = load i8*, i8** %3
%23 = call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = load i32, i32* %6
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = load i32, i32* %6
store i32 %27, i32* %2
br label %45
28: ; preds = %16
br label %31
29: ; preds = %10
%30 = load i32, i32* @EEXIST
store i32 %30, i32* %2
br label %45
31: ; preds = %28
br label %32
32: ; preds = %31, %1
%33 = load i8*, i8** %3
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %34, i32** %4
%35 = load i32*, i32** %4
%36 = icmp ne i32* %35, null
br i1 %36, label %39, label %37
37: ; preds = %32
%38 = load i32, i32* @errno
store i32 %38, i32* %2
br label %45
39: ; preds = %32
%40 = load i32*, i32** %4
%41 = call i32 (...) @getpid()
%42 = call i32 (i32*, i8*, ...) @fprintf(i32* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %41)
%43 = load i32*, i32** %4
%44 = call i32 @fclose(i32* %43)
store i32 0, i32* %2
br label %45
45: ; preds = %39, %37, %29, %26
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"Removing stale lock file %s\0A\00"
@EEXIST = global i32 0
@.str.1 = [2 x i8] c"w\00"
@errno = global i32 0
@.str.2 = [3 x i8] c"%d\00"
define i32 @op_write_lock_file(i8* %0) {
%2 = tail call i64 @op_file_readable(i8* %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %17, label %4
4: ; preds = %1
%5 = tail call i32 @op_read_lock_file(i8* %0)
%6 = tail call i64 @kill(i32 %5, i32 0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %15, label %8
8: ; preds = %4
%9 = tail call i32 @unlink(i8* %0)
%10 = load i32, i32* @stderr
%11 = sext i32 %10 to i64
%12 = inttoptr i64 %11 to i32*
%13 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0)
%14 = icmp eq i32 %9, 0
br i1 %14, label %17, label %26
15: ; preds = %4
%16 = load i32, i32* @EEXIST
br label %26
17: ; preds = %8, %1
%18 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = load i32, i32* @errno
br label %26
22: ; preds = %17
%23 = tail call i32 (...) @getpid()
%24 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = tail call i32 @fclose(i32* %18)
br label %26
26: ; preds = %15, %8, %22, %20
%27 = phi i32 [ 0, %22 ], [ %21, %20 ], [ %16, %15 ], [ %9, %8 ]
ret i32 %27
}