#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 */
scalar_t__ EEXIST;
int /*<<< orphan*/ F_OK;
int /*<<< orphan*/ __ploop_err(scalar_t__, char *);
scalar_t__ access(char *, int /*<<< orphan*/);
scalar_t__ create_file(char *);
int do_lock(char *, int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ mkdir(char *, int);
int ploop_global_lock(void) {
  if (access("/var/lock/ploop"
             "/ploop.lck",
             F_OK)) {
    if (access("/var/lock/ploop", F_OK) && mkdir("/var/lock/ploop", 0700) &&
        errno != EEXIST) {
      __ploop_err(errno, "Failed to create "
                         "/var/lock/ploop");
      return -1;
    }
    if (create_file("/var/lock/ploop"
                    "/ploop.lck"))
      return -1;
  }
  return do_lock("/var/lock/ploop"
                 "/ploop.lck",
                 0);
}


x86 assembly (clang, -O0):
<ploop_global_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1050 <access@plt>
cmp    $0x0,%rax
je     1214 <ploop_global_lock+0xc4>
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0xea4(%rip),%rdi
call   1050 <access@plt>
cmp    $0x0,%rax
je     11ed <ploop_global_lock+0x9d>
lea    0xe8e(%rip),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     11ed <ploop_global_lock+0x9d>
mov    0x2e20(%rip),%rax
mov    (%rax),%rax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%rax
je     11ed <ploop_global_lock+0x9d>
mov    0x2e06(%rip),%rax
mov    (%rax),%rdi
lea    0xe3e(%rip),%rsi
call   1070 <__ploop_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <ploop_global_lock+0xd5>
lea    0xe0c(%rip),%rdi
call   1060 <create_file@plt>
cmp    $0x0,%rax
je     120f <ploop_global_lock+0xbf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <ploop_global_lock+0xd5>
jmp    1214 <ploop_global_lock+0xc4>
lea    0xde5(%rip),%rdi
xor    %esi,%esi
call   1040 <do_lock@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ploop_global_lock>:
push   %rbx
mov    0x2e70(%rip),%rbx
mov    (%rbx),%esi
lea    0xe9f(%rip),%rdi
call   1050 <access@plt>
test   %rax,%rax
je     11bb <ploop_global_lock+0x6b>
mov    (%rbx),%esi
lea    0xeb7(%rip),%rdi
call   1050 <access@plt>
test   %rax,%rax
je     11aa <ploop_global_lock+0x5a>
lea    0xea6(%rip),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     11aa <ploop_global_lock+0x5a>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rdi
mov    0x2e3b(%rip),%rax
cmp    (%rax),%rdi
jne    11ca <ploop_global_lock+0x7a>
lea    0xe4f(%rip),%rdi
call   1060 <create_file@plt>
test   %rax,%rax
jne    11d6 <ploop_global_lock+0x86>
lea    0xe3e(%rip),%rdi
xor    %esi,%esi
pop    %rbx
jmp    1040 <do_lock@plt>
lea    0xe49(%rip),%rsi
call   1070 <__ploop_err@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [26 x i8] c"/var/lock/ploop/ploop.lck\00"
@F_OK = global i32 0
@.str.1 = [16 x i8] c"/var/lock/ploop\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.2 = [33 x i8] c"Failed to create /var/lock/ploop\00"
define i32 @ploop_global_lock() {
%1 = alloca i32
%2 = load i32, i32* @F_OK
%3 = call i64 @access(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp ne i64 %3, 0
br i1 %4, label %5, label %24
5: ; preds = %0
%6 = load i32, i32* @F_OK
%7 = call i64 @access(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %19
9: ; preds = %5
%10 = call i64 @mkdir(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 448)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @EEXIST
%15 = icmp ne i64 %13, %14
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i64, i64* @errno
%18 = call i32 @__ploop_err(i64 %17, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %26
19: ; preds = %12, %9, %5
%20 = call i64 @create_file(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 -1, i32* %1
br label %26
23: ; preds = %19
br label %24
24: ; preds = %23, %0
%25 = call i32 @do_lock(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %25, i32* %1
br label %26
26: ; preds = %24, %22, %16
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@.str = [26 x i8] c"/var/lock/ploop/ploop.lck\00"
@F_OK = global i32 0
@.str.1 = [16 x i8] c"/var/lock/ploop\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.2 = [33 x i8] c"Failed to create /var/lock/ploop\00"
define i32 @ploop_global_lock() {
%1 = load i32, i32* @F_OK
%2 = tail call i64 @access(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i64 %2, 0
br i1 %3, label %20, label %4
4: ; preds = %0
%5 = load i32, i32* @F_OK
%6 = tail call i64 @access(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %4
%9 = tail call i64 @mkdir(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 448)
%10 = icmp eq i64 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @EEXIST
%14 = icmp eq i64 %12, %13
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = tail call i32 @__ploop_err(i64 %12, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %22
17: ; preds = %11, %8, %4
%18 = tail call i64 @create_file(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17, %0
%21 = tail call i32 @do_lock(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 0)
br label %22
22: ; preds = %17, %20, %15
%23 = phi i32 [ -1, %15 ], [ %21, %20 ], [ -1, %17 ]
ret i32 %23
}