#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;

/* Variables and functions */
int LOCK_EX;
int LOCK_NB;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ flock(int, int);
scalar_t__ getpid();
int open(char const *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
extern int lockpidfile(const char *runfile) {
  int lck_fd = 0;
  char pidstr[12];
  pid_t mypid;
  mypid = getpid();
  sprintf(pidstr, "%i\n", (int)mypid);
  if (runfile && ((lck_fd = open(runfile, O_RDWR | O_CREAT, 0640)) > 0) &&
      (!flock(lck_fd, LOCK_EX | LOCK_NB))) {
    if (write(lck_fd, pidstr, strlen(pidstr)) < 0) {
      close(lck_fd);
      lck_fd = -1;
    }
  } else if (runfile && lck_fd) {
    close(lck_fd);
    lck_fd = -1;
  }
  return (lck_fd);
}


x86 assembly (clang, -O0):
<lockpidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x20(%rbp)
lea    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
lea    0xe61(%rip),%rsi
call   1090 <sprintf@plt>
cmpq   $0x0,-0x8(%rbp)
je     1241 <lockpidfile+0xd1>
mov    -0x8(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
mov    $0x1a0,%edx
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    1241 <lockpidfile+0xd1>
mov    -0xc(%rbp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
or     (%rax),%esi
call   1080 <flock@plt>
cmp    $0x0,%eax
jne    1241 <lockpidfile+0xd1>
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    123c <lockpidfile+0xcc>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0xc(%rbp)
jmp    126a <lockpidfile+0xfa>
cmpq   $0x0,-0x8(%rbp)
je     1265 <lockpidfile+0xf5>
cmpl   $0x0,-0xc(%rbp)
je     1265 <lockpidfile+0xf5>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0xc(%rbp)
jmp    126a <lockpidfile+0xfa>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lockpidfile>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbp
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe77(%rip),%rsi
lea    0xc(%rsp),%rdi
mov    %eax,%edx
call   1090 <sprintf@plt>
test   %rbp,%rbp
je     120c <lockpidfile+0x9c>
mov    0x2e3f(%rip),%rax
mov    0x2e50(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
mov    $0x1a0,%edx
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11fe <lockpidfile+0x8e>
mov    0x2e12(%rip),%rax
mov    0x2df3(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebp,%edi
call   1080 <flock@plt>
test   %eax,%eax
jne    1200 <lockpidfile+0x90>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
js     1200 <lockpidfile+0x90>
mov    %ebp,%ebx
jmp    120c <lockpidfile+0x9c>
je     120c <lockpidfile+0x9c>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%i\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
define i32 @lockpidfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [12 x i8]
%5 = alloca i64
store i8* %0, i8** %2
store i32 0, i32* %3
%6 = call i64 (...) @getpid()
store i64 %6, i64* %5
%7 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%8 = load i64, i64* %5
%9 = trunc i64 %8 to i32
%10 = call i32 @sprintf(i8* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i8*, i8** %2
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %38
13: ; preds = %1
%14 = load i8*, i8** %2
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %14, i32 %17, i32 416)
store i32 %18, i32* %3
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %38
20: ; preds = %13
%21 = load i32, i32* %3
%22 = load i32, i32* @LOCK_EX
%23 = load i32, i32* @LOCK_NB
%24 = or i32 %22, %23
%25 = call i32 @flock(i32 %21, i32 %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %38, label %27
27: ; preds = %20
%28 = load i32, i32* %3
%29 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%30 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = call i64 @write(i32 %28, i8* %29, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = load i32, i32* %3
%36 = call i32 @close(i32 %35)
store i32 -1, i32* %3
br label %37
37: ; preds = %34, %27
br label %48
38: ; preds = %20, %13, %1
%39 = load i8*, i8** %2
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %47
41: ; preds = %38
%42 = load i32, i32* %3
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i32, i32* %3
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %3
br label %47
47: ; preds = %44, %41, %38
br label %48
48: ; preds = %47, %37
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str = [4 x i8] c"%i\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
define i32 @lockpidfile(i8* %0) {
%2 = alloca [12 x i8]
%3 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%4 = tail call i64 (...) @getpid()
%5 = trunc i64 %4 to i32
%6 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i8* %0, null
br i1 %7, label %30, label %8
8: ; preds = %1
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %0, i32 %11, i32 416)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %26
14: ; preds = %8
%15 = load i32, i32* @LOCK_EX
%16 = load i32, i32* @LOCK_NB
%17 = or i32 %16, %15
%18 = tail call i32 @flock(i32 %12, i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %14
%21 = call i32 @strlen(i8* %3)
%22 = call i64 @write(i32 %12, i8* %3, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = call i32 @close(i32 %12)
br label %30
26: ; preds = %8
%27 = icmp eq i32 %12, 0
br i1 %27, label %30, label %28
28: ; preds = %14, %26
%29 = tail call i32 @close(i32 %12)
br label %30
30: ; preds = %1, %26, %28, %20, %24
%31 = phi i32 [ -1, %28 ], [ 0, %26 ], [ -1, %24 ], [ %12, %20 ], [ 0, %1 ]
ret i32 %31
}