#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 */
int /*<<< orphan*/ LOG_INFO;
int O_CREAT;
int O_EXCL;
int O_WRONLY;
scalar_t__ check_lock_status(char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, ...);
char *strerror(int /*<<< orphan*/);
int strlen(char const *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int /*<<< orphan*/ write(int, char *, int);
int fhs_lock(const char *filename, int pid) {
  int fd, j;
  char lockinfo[12], message[80];
  char file[80], *p;
  j = strlen(filename);
  p = (char *)filename + j;
  while (*(p - 1) != '/' && j-- != 1) {
    p--;
  }
  sprintf(file, "%s/LCK..%s", "/var/lock", p);
  if (check_lock_status(filename)) {
    return 1;
  }
  fd = open(file, O_CREAT | O_WRONLY | O_EXCL, 0444);
  if (fd < 0) {
    sprintf(message, "RXTX fhs_lock() Error: creating lock file: %s: %s\n",
            file, strerror(errno));
    syslog(LOG_INFO, message);
    return 1;
  }
  sprintf(lockinfo, "%10d\n", pid);
  sprintf(message, "fhs_lock: creating lockfile: %s\n", lockinfo);
  write(fd, lockinfo, 11);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<fhs_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0xd8(%rbp)
mov    -0xd8(%rbp),%rax
movsbl -0x1(%rax),%ecx
xor    %eax,%eax
cmp    $0x2f,%ecx
mov    %al,-0xd9(%rbp)
je     11e3 <fhs_lock+0x63>
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    $0x1,%eax
setne  %al
mov    %al,-0xd9(%rbp)
mov    -0xd9(%rbp),%al
test   $0x1,%al
jne    11f6 <fhs_lock+0x76>
jmp    120d <fhs_lock+0x8d>
mov    -0xd8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0xd8(%rbp)
jmp    11b0 <fhs_lock+0x30>
lea    -0xd0(%rbp),%rdi
mov    -0xd8(%rbp),%rcx
lea    0xdde(%rip),%rsi
lea    0xde2(%rip),%rdx
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <check_lock_status@plt>
cmp    $0x0,%rax
je     124f <fhs_lock+0xcf>
movl   $0x1,-0x4(%rbp)
jmp    1337 <fhs_lock+0x1b7>
lea    -0xd0(%rbp),%rdi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
mov    0x2d52(%rip),%rax
or     (%rax),%esi
mov    0x2d69(%rip),%rax
or     (%rax),%esi
mov    $0x124,%edx
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12ec <fhs_lock+0x16c>
lea    -0x80(%rbp),%rax
mov    %rax,-0xf0(%rbp)
lea    -0xd0(%rbp),%rax
mov    %rax,-0xe8(%rbp)
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0xf0(%rbp),%rdi
mov    -0xe8(%rbp),%rdx
mov    %rax,%rcx
lea    0xd4e(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
lea    -0x80(%rbp),%rsi
call   1070 <syslog@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <fhs_lock+0x1b7>
lea    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xd4e(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
lea    -0x80(%rbp),%rdi
lea    -0x28(%rbp),%rdx
lea    0xd3e(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xb,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fhs_lock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xb8,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
call   1040 <strlen@plt>
cltq
nopw   0x0(%rax,%rax,1)
lea    -0x1(%rax),%rcx
cmpb   $0x2f,-0x1(%rbx,%rax,1)
je     11b3 <fhs_lock+0x33>
cmp    $0x1,%eax
mov    %rcx,%rax
jne    11a0 <fhs_lock+0x20>
add    %rbx,%rcx
add    $0x1,%rcx
lea    0xe3f(%rip),%rsi
lea    0xe43(%rip),%rdx
lea    0x10(%rsp),%rdi
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <check_lock_status@plt>
mov    $0x1,%ebx
test   %rax,%rax
jne    12a0 <fhs_lock+0x120>
mov    0x2e07(%rip),%rax
mov    0x2dc0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd5(%rip),%rax
or     (%rax),%esi
lea    0x10(%rsp),%rdi
mov    $0x124,%edx
call   1080 <open@plt>
test   %eax,%eax
js     1263 <fhs_lock+0xe3>
mov    %eax,%ebp
lea    0xe27(%rip),%rsi
xor    %ebx,%ebx
lea    0x4(%rsp),%r15
mov    %r15,%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1090 <sprintf@plt>
lea    0xe12(%rip),%rsi
lea    0x60(%rsp),%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xb,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    12a0 <fhs_lock+0x120>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd9d(%rip),%rsi
lea    0x60(%rsp),%rbp
lea    0x10(%rsp),%rdx
mov    %rbp,%rdi
mov    %rax,%rcx
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
mov    %rbp,%rsi
call   1070 <syslog@plt>
mov    %ebx,%eax
add    $0xb8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"%s/LCK..%s\00"
@.str.1 = [10 x i8] c"/var/lock\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@.str.2 = [51 x i8] c"RXTX fhs_lock() Error: creating lock file: %s: %s\0A\00"
@errno = global i32 0
@LOG_INFO = global i32 0
@.str.3 = [6 x i8] c"%10d\0A\00"
@.str.4 = [33 x i8] c"fhs_lock: creating lockfile: %s\0A\00"
define i32 @fhs_lock(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [12 x i8]
%9 = alloca [80 x i8]
%10 = alloca [80 x i8]
%11 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %7
%14 = load i8*, i8** %4
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
store i8* %17, i8** %11
br label %18
18: ; preds = %30, %2
%19 = load i8*, i8** %11
%20 = getelementptr inbounds i8, i8* %19, i64 -1
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 47
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = load i32, i32* %7
%26 = add nsw i32 %25, -1
store i32 %26, i32* %7
%27 = icmp ne i32 %25, 1
br label %28
28: ; preds = %24, %18
%29 = phi i1 [ false, %18 ], [ %27, %24 ]
br i1 %29, label %30, label %33
30: ; preds = %28
%31 = load i8*, i8** %11
%32 = getelementptr inbounds i8, i8* %31, i32 -1
store i8* %32, i8** %11
br label %18
33: ; preds = %28
%34 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%35 = load i8*, i8** %11
%36 = call i32 (i8*, i8*, ...) @sprintf(i8* %34, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = load i8*, i8** %4
%38 = call i64 @check_lock_status(i8* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %33
store i32 1, i32* %3
br label %72
41: ; preds = %33
%42 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%43 = load i32, i32* @O_CREAT
%44 = load i32, i32* @O_WRONLY
%45 = or i32 %43, %44
%46 = load i32, i32* @O_EXCL
%47 = or i32 %45, %46
%48 = call i32 @open(i8* %42, i32 %47, i32 292)
store i32 %48, i32* %6
%49 = load i32, i32* %6
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %60
51: ; preds = %41
%52 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%53 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%54 = load i32, i32* @errno
%55 = call i8* @strerror(i32 %54)
%56 = call i32 (i8*, i8*, ...) @sprintf(i8* %52, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i8* %53, i8* %55)
%57 = load i32, i32* @LOG_INFO
%58 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%59 = call i32 @syslog(i32 %57, i8* %58)
store i32 1, i32* %3
br label %72
60: ; preds = %41
%61 = getelementptr inbounds [12 x i8], [12 x i8]* %8, i64 0, i64 0
%62 = load i32, i32* %5
%63 = call i32 (i8*, i8*, ...) @sprintf(i8* %61, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %62)
%64 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%65 = getelementptr inbounds [12 x i8], [12 x i8]* %8, i64 0, i64 0
%66 = call i32 (i8*, i8*, ...) @sprintf(i8* %64, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %65)
%67 = load i32, i32* %6
%68 = getelementptr inbounds [12 x i8], [12 x i8]* %8, i64 0, i64 0
%69 = call i32 @write(i32 %67, i8* %68, i32 11)
%70 = load i32, i32* %6
%71 = call i32 @close(i32 %70)
store i32 0, i32* %3
br label %72
72: ; preds = %60, %51, %40
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@.str = [11 x i8] c"%s/LCK..%s\00"
@.str.1 = [10 x i8] c"/var/lock\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_EXCL = global i32 0
@.str.2 = [51 x i8] c"RXTX fhs_lock() Error: creating lock file: %s: %s\0A\00"
@errno = global i32 0
@LOG_INFO = global i32 0
@.str.3 = [6 x i8] c"%10d\0A\00"
@.str.4 = [33 x i8] c"fhs_lock: creating lockfile: %s\0A\00"
define i32 @fhs_lock(i8* %0, i32 %1) {
%3 = alloca [12 x i8]
%4 = alloca [80 x i8]
%5 = alloca [80 x i8]
%6 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%9 = tail call i32 @strlen(i8* %0)
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
br label %12
12: ; preds = %12, %2
%13 = phi i32 [ %9, %2 ], [ %20, %12 ]
%14 = phi i8* [ %11, %2 ], [ %15, %12 ]
%15 = getelementptr inbounds i8, i8* %14, i64 -1
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 47
%18 = icmp eq i32 %13, 1
%19 = select i1 %17, i1 true, i1 %18
%20 = add nsw i32 %13, -1
br i1 %19, label %21, label %12
21: ; preds = %12
%22 = call i32 (i8*, i8*, ...) @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%23 = tail call i64 @check_lock_status(i8* %0)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %44
25: ; preds = %21
%26 = load i32, i32* @O_CREAT
%27 = load i32, i32* @O_WRONLY
%28 = or i32 %27, %26
%29 = load i32, i32* @O_EXCL
%30 = or i32 %28, %29
%31 = call i32 @open(i8* %8, i32 %30, i32 292)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %25
%34 = load i32, i32* @errno
%35 = tail call i8* @strerror(i32 %34)
%36 = call i32 (i8*, i8*, ...) @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i8* %8, i8* %35)
%37 = load i32, i32* @LOG_INFO
%38 = call i32 @syslog(i32 %37, i8* %7)
br label %44
39: ; preds = %25
%40 = call i32 (i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %1)
%41 = call i32 (i8*, i8*, ...) @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %6)
%42 = call i32 @write(i32 %31, i8* %6, i32 11)
%43 = call i32 @close(i32 %31)
br label %44
44: ; preds = %21, %39, %33
%45 = phi i32 [ 1, %33 ], [ 0, %39 ], [ 1, %21 ]
ret i32 %45
}