#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__ EWOULDBLOCK;
int LOCK_EX;
int LOCK_NB;
int LOCK_UN;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ debug(char *, char const *);
int /*<<< orphan*/ die(char *, char const *);
scalar_t__ errno;
int flock(int, int);
int open_lock(char const *, int /*<<< orphan*/);
void sc_verify_snap_lock(const char *snap_name) {
  int lock_fd, retval;
  lock_fd = open_lock(snap_name, 0);
  debug("trying to verify whether exclusive lock over snap %s is held",
        snap_name);
  retval = flock(lock_fd, LOCK_EX | LOCK_NB);
  if (retval == 0) {
    flock(lock_fd, LOCK_UN);
    close(lock_fd);
    errno = 0;
    die("unexpectedly managed to acquire exclusive lock over snap %s",
        snap_name);
  }
  if (retval < 0 && errno != EWOULDBLOCK) {
    die("cannot verify exclusive lock over snap %s", snap_name);
  }
}


x86 assembly (clang, -O0):
<sc_verify_snap_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1050 <open_lock@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe8b(%rip),%rdi
call   1030 <debug@plt>
mov    -0xc(%rbp),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    0x2e2b(%rip),%rax
or     (%rax),%esi
call   1070 <flock@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    11d8 <sc_verify_snap_lock+0x88>
mov    -0xc(%rbp),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
call   1070 <flock@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    0x2e17(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rsi
lea    0xe6a(%rip),%rdi
call   1060 <die@plt>
cmpl   $0x0,-0x10(%rbp)
jge    120c <sc_verify_snap_lock+0xbc>
mov    0x2def(%rip),%rax
mov    (%rax),%rax
mov    0x2ddd(%rip),%rcx
cmp    (%rcx),%rax
je     120c <sc_verify_snap_lock+0xbc>
mov    -0x8(%rbp),%rsi
lea    0xe72(%rip),%rdi
call   1060 <die@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sc_verify_snap_lock>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %esi,%esi
call   1050 <open_lock@plt>
mov    %eax,%ebp
lea    0xe9a(%rip),%rdi
mov    %rbx,%rsi
call   1030 <debug@plt>
mov    0x2e73(%rip),%rax
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebp,%edi
call   1070 <flock@plt>
test   %eax,%eax
je     11aa <sc_verify_snap_lock+0x5a>
jns    11a3 <sc_verify_snap_lock+0x53>
mov    0x2e44(%rip),%rax
mov    (%rax),%rax
mov    0x2e32(%rip),%rcx
cmp    (%rcx),%rax
jne    11d8 <sc_verify_snap_lock+0x88>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1070 <flock@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x2e10(%rip),%rax
movq   $0x0,(%rax)
lea    0xe67(%rip),%rdi
jmp    11df <sc_verify_snap_lock+0x8f>
lea    0xe9a(%rip),%rdi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <die@plt>

LLVM IR (-O0):
@.str = [61 x i8] c"trying to verify whether exclusive lock over snap %s is held\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@LOCK_UN = global i32 0
@errno = global i64 0
@.str.1 = [60 x i8] c"unexpectedly managed to acquire exclusive lock over snap %s\00"
@EWOULDBLOCK = global i64 0
@.str.2 = [42 x i8] c"cannot verify exclusive lock over snap %s\00"
define void @sc_verify_snap_lock(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @open_lock(i8* %5, i32 0)
store i32 %6, i32* %3
%7 = load i8*, i8** %2
%8 = call i32 @debug(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* %3
%10 = load i32, i32* @LOCK_EX
%11 = load i32, i32* @LOCK_NB
%12 = or i32 %10, %11
%13 = call i32 @flock(i32 %9, i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %1
%17 = load i32, i32* %3
%18 = load i32, i32* @LOCK_UN
%19 = call i32 @flock(i32 %17, i32 %18)
%20 = load i32, i32* %3
%21 = call i32 @close(i32 %20)
store i64 0, i64* @errno
%22 = load i8*, i8** %2
%23 = call i32 @die(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %16, %1
%25 = load i32, i32* %4
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @EWOULDBLOCK
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i8*, i8** %2
%33 = call i32 @die(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %31, %27, %24
ret void
}

LLVM IR (-O2):
@.str = [61 x i8] c"trying to verify whether exclusive lock over snap %s is held\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@LOCK_UN = global i32 0
@errno = global i64 0
@.str.1 = [60 x i8] c"unexpectedly managed to acquire exclusive lock over snap %s\00"
@EWOULDBLOCK = global i64 0
@.str.2 = [42 x i8] c"cannot verify exclusive lock over snap %s\00"
define void @sc_verify_snap_lock(i8* %0) {
%2 = tail call i32 @open_lock(i8* %0, i32 0)
%3 = tail call i32 @debug(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i8* %0)
%4 = load i32, i32* @LOCK_EX
%5 = load i32, i32* @LOCK_NB
%6 = or i32 %5, %4
%7 = tail call i32 @flock(i32 %2, i32 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* @LOCK_UN
%11 = tail call i32 @flock(i32 %2, i32 %10)
%12 = tail call i32 @close(i32 %2)
store i64 0, i64* @errno
br label %19
13: ; preds = %1
%14 = icmp slt i32 %7, 0
br i1 %14, label %15, label %22
15: ; preds = %13
%16 = load i64, i64* @errno
%17 = load i64, i64* @EWOULDBLOCK
%18 = icmp eq i64 %16, %17
br i1 %18, label %22, label %19
19: ; preds = %15, %9
%20 = phi i8* [ getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), %15 ]
%21 = tail call i32 @die(i8* %20, i8* %0)
br label %22
22: ; preds = %19, %15, %13
ret void
}