#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*/ F_GETLK;
int F_RDLCK;
int /*<<< orphan*/ F_SETLK;
int F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errx(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ str_type(int);
int /*<<< orphan*/ try_lock(int /*<<< orphan*/, int, int /*<<< orphan*/, long,
                            long);
void lock(int type, long int start, long int len) {
  assert(type == F_RDLCK || type == F_WRLCK);
  if (!try_lock(F_GETLK, type, SEEK_SET, start, len) ||
      !try_lock(F_SETLK, type, SEEK_SET, start, len))
    errx(1, "setting %s failed", str_type(type));
}


x86 assembly (clang, -O0):
<lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%ecx
mov    $0x1,%al
mov    0x2e59(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x19(%rbp)
je     117c <lock+0x3c>
mov    -0x4(%rbp),%eax
mov    0x2e54(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
call   1030 <try_lock@plt>
cmp    $0x0,%eax
je     11df <lock+0x9f>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
call   1030 <try_lock@plt>
cmp    $0x0,%eax
jne    11fa <lock+0xba>
mov    -0x4(%rbp),%edi
call   1050 <str_type@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe0b(%rip),%rsi
call   1040 <errx@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e67(%rip),%rax
cmp    %edi,(%rax)
sete   %al
mov    0x2e6b(%rip),%rcx
cmp    %edi,(%rcx)
mov    %rdx,%r14
mov    %rsi,%r15
sete   %cl
or     %al,%cl
movzbl %cl,%edi
call   1060 <assert@plt>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    0x2e4e(%rip),%r12
mov    (%r12),%edx
mov    %ebx,%esi
mov    %r15,%rcx
mov    %r14,%r8
call   1030 <try_lock@plt>
test   %eax,%eax
je     11c1 <lock+0x81>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    (%r12),%edx
mov    %ebx,%esi
mov    %r15,%rcx
mov    %r14,%r8
call   1030 <try_lock@plt>
test   %eax,%eax
je     11c1 <lock+0x81>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %ebx,%edi
call   1050 <str_type@plt>
lea    0xe31(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <errx@plt>

LLVM IR (-O0):
@F_RDLCK = global i32 0
@F_WRLCK = global i32 0
@F_GETLK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str = [18 x i8] c"setting %s failed\00"
define void @lock(i32 %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
store i32 %0, i32* %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%7 = load i32, i32* %4
%8 = load i32, i32* @F_RDLCK
%9 = icmp eq i32 %7, %8
br i1 %9, label %14, label %10
10: ; preds = %3
%11 = load i32, i32* %4
%12 = load i32, i32* @F_WRLCK
%13 = icmp eq i32 %11, %12
br label %14
14: ; preds = %10, %3
%15 = phi i1 [ true, %3 ], [ %13, %10 ]
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i32, i32* @F_GETLK
%19 = load i32, i32* %4
%20 = load i32, i32* @SEEK_SET
%21 = load i64, i64* %5
%22 = load i64, i64* %6
%23 = call i32 @try_lock(i32 %18, i32 %19, i32 %20, i64 %21, i64 %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %14
%26 = load i32, i32* @F_SETLK
%27 = load i32, i32* %4
%28 = load i32, i32* @SEEK_SET
%29 = load i64, i64* %5
%30 = load i64, i64* %6
%31 = call i32 @try_lock(i32 %26, i32 %27, i32 %28, i64 %29, i64 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %25, %14
%34 = load i32, i32* %4
%35 = call i32 @str_type(i32 %34)
%36 = call i32 @errx(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %33, %25
ret void
}

LLVM IR (-O2):
@F_RDLCK = global i32 0
@F_WRLCK = global i32 0
@F_GETLK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str = [18 x i8] c"setting %s failed\00"
define void @lock(i32 %0, i64 %1, i64 %2) {
%4 = load i32, i32* @F_RDLCK
%5 = icmp eq i32 %4, %0
%6 = load i32, i32* @F_WRLCK
%7 = icmp eq i32 %6, %0
%8 = select i1 %5, i1 true, i1 %7
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = load i32, i32* @F_GETLK
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @try_lock(i32 %11, i32 %0, i32 %12, i64 %1, i64 %2)
%14 = icmp eq i32 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %3
%16 = load i32, i32* @F_SETLK
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @try_lock(i32 %16, i32 %0, i32 %17, i64 %1, i64 %2)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15, %3
%21 = tail call i32 @str_type(i32 %0)
%22 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %20, %15
ret void
}