#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*/ uid_t;

/* Variables and functions */
int /*<<< orphan*/ LOCK_EX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ debug(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ die(char *, char *, int /*<<< orphan*/);
scalar_t__ flock(int, int /*<<< orphan*/);
int open_lock(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ sc_disable_sanity_timeout();
int /*<<< orphan*/ sc_enable_sanity_timeout();
int sc_lock_generic(const char *scope, uid_t uid) {
  int lock_fd = open_lock(scope, uid);
  sc_enable_sanity_timeout();
  debug("acquiring exclusive lock (scope %s, uid %d)", scope ?: "(global)",
        uid);
  if (flock(lock_fd, LOCK_EX) < 0) {
    sc_disable_sanity_timeout();
    close(lock_fd);
    die("cannot acquire exclusive lock (scope %s, uid %d)", scope ?: "(global)",
        uid);
  } else {
    sc_disable_sanity_timeout();
  }
  return lock_fd;
}


x86 assembly (clang, -O0):
<sc_lock_generic>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <open_lock@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1070 <sc_enable_sanity_timeout@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11b4 <sc_lock_generic+0x44>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11c4 <sc_lock_generic+0x54>
lea    0xe71(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11c4 <sc_lock_generic+0x54>
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xe2e(%rip),%rdi
call   1030 <debug@plt>
mov    -0x10(%rbp),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
call   1090 <flock@plt>
cmp    $0x0,%rax
jge    1248 <sc_lock_generic+0xd8>
mov    $0x0,%al
call   1060 <sc_disable_sanity_timeout@plt>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1220 <sc_lock_generic+0xb0>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1230 <sc_lock_generic+0xc0>
lea    0xe05(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    1230 <sc_lock_generic+0xc0>
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xdf7(%rip),%rdi
call   1080 <die@plt>
jmp    124f <sc_lock_generic+0xdf>
mov    $0x0,%al
call   1060 <sc_disable_sanity_timeout@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sc_lock_generic>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbp
call   1050 <open_lock@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1070 <sc_enable_sanity_timeout@plt>
test   %rbp,%rbp
lea    0xe96(%rip),%rbx
cmovne %rbp,%rbx
lea    0xe5f(%rip),%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <debug@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1090 <flock@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1060 <sc_disable_sanity_timeout@plt>
test   %rbp,%rbp
jns    11e6 <sc_lock_generic+0x76>
mov    %r15d,%edi
call   1040 <close@plt>
lea    0xe5a(%rip),%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1080 <die@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [44 x i8] c"acquiring exclusive lock (scope %s, uid %d)\00"
@.str.1 = [9 x i8] c"(global)\00"
@LOCK_EX = global i32 0
@.str.2 = [49 x i8] c"cannot acquire exclusive lock (scope %s, uid %d)\00"
define i32 @sc_lock_generic(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
%7 = load i32, i32* %4
%8 = call i32 @open_lock(i8* %6, i32 %7)
store i32 %8, i32* %5
%9 = call i32 (...) @sc_enable_sanity_timeout()
%10 = load i8*, i8** %3
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
br label %14
13: ; preds = %2
br label %14
14: ; preds = %13, %12
%15 = phi i8* [ %10, %12 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), %13 ]
%16 = load i32, i32* %4
%17 = call i32 @debug(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %15, i32 %16)
%18 = load i32, i32* %5
%19 = load i32, i32* @LOCK_EX
%20 = call i64 @flock(i32 %18, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %34
22: ; preds = %14
%23 = call i32 (...) @sc_disable_sanity_timeout()
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
%26 = load i8*, i8** %3
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %29
28: ; preds = %22
br label %30
29: ; preds = %22
br label %30
30: ; preds = %29, %28
%31 = phi i8* [ %26, %28 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), %29 ]
%32 = load i32, i32* %4
%33 = call i32 @die(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i8* %31, i32 %32)
br label %36
34: ; preds = %14
%35 = call i32 (...) @sc_disable_sanity_timeout()
br label %36
36: ; preds = %34, %30
%37 = load i32, i32* %5
ret i32 %37
}

LLVM IR (-O2):
@.str = [44 x i8] c"acquiring exclusive lock (scope %s, uid %d)\00"
@.str.1 = [9 x i8] c"(global)\00"
@LOCK_EX = global i32 0
@.str.2 = [49 x i8] c"cannot acquire exclusive lock (scope %s, uid %d)\00"
define i32 @sc_lock_generic(i8* %0, i32 %1) {
%3 = tail call i32 @open_lock(i8* %0, i32 %1)
%4 = tail call i32 (...) @sc_enable_sanity_timeout()
%5 = icmp eq i8* %0, null
%6 = select i1 %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %0
%7 = tail call i32 @debug(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %6, i32 %1)
%8 = load i32, i32* @LOCK_EX
%9 = tail call i64 @flock(i32 %3, i32 %8)
%10 = icmp slt i64 %9, 0
%11 = tail call i32 (...) @sc_disable_sanity_timeout()
br i1 %10, label %12, label %15
12: ; preds = %2
%13 = tail call i32 @close(i32 %3)
%14 = tail call i32 @die(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i8* %6, i32 %1)
br label %15
15: ; preds = %2, %12
ret i32 %3
}