#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 */
struct flock {
  int l_type;
  int l_whence;
  long l_start;
  long l_len;
};

/* Variables and functions */
int F_GETLK;
scalar_t__ F_UNLCK;
int /*<<< orphan*/ assert(int);
int try_fcntl(int, struct flock *);
bool try_lock(int cmd, int type, int whence, long int start, long int len) {
  struct flock fl = {
      .l_type = type,
      .l_whence = whence,
      .l_start = start,
      .l_len = len,
  };
  int ret = try_fcntl(cmd, &fl);
  if (cmd == F_GETLK) {
    assert(ret == 0);
    return fl.l_type == F_UNLCK;
  } else {
    return ret == 0;
  }
}


x86 assembly (clang, -O0):
<try_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1030 <try_fcntl@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%eax
jne    11a5 <try_lock+0x85>
cmpl   $0x0,-0x3c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movslq -0x38(%rbp),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    11b4 <try_lock+0x94>
cmpl   $0x0,-0x3c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_lock>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    %esi,0x8(%rsp)
mov    %edx,0xc(%rsp)
mov    %rcx,0x10(%rsp)
mov    %r8,0x18(%rsp)
lea    0x8(%rsp),%rsi
call   1030 <try_fcntl@plt>
mov    0x2e9e(%rip),%rdx
test   %eax,%eax
sete   %cl
cmp    %ebx,(%rdx)
jne    1172 <try_lock+0x52>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
movslq 0x8(%rsp),%rax
mov    0x2e74(%rip),%rcx
cmp    %rax,(%rcx)
sete   %cl
movzbl %cl,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i64, i64 }
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
define i32 @try_lock(i32 %0, i32 %1, i32 %2, i64 %3, i64 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
%12 = alloca %struct.flock
%13 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i64 %3, i64* %10
store i64 %4, i64* %11
%14 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 0
%15 = load i32, i32* %8
store i32 %15, i32* %14
%16 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 1
%17 = load i32, i32* %9
store i32 %17, i32* %16
%18 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 2
%19 = load i64, i64* %10
store i64 %19, i64* %18
%20 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 3
%21 = load i64, i64* %11
store i64 %21, i64* %20
%22 = load i32, i32* %7
%23 = call i32 @try_fcntl(i32 %22, %struct.flock* %12)
store i32 %23, i32* %13
%24 = load i32, i32* %7
%25 = load i32, i32* @F_GETLK
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %38
27: ; preds = %5
%28 = load i32, i32* %13
%29 = icmp eq i32 %28, 0
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 0
%33 = load i32, i32* %32
%34 = sext i32 %33 to i64
%35 = load i64, i64* @F_UNLCK
%36 = icmp eq i64 %34, %35
%37 = zext i1 %36 to i32
store i32 %37, i32* %6
br label %42
38: ; preds = %5
%39 = load i32, i32* %13
%40 = icmp eq i32 %39, 0
%41 = zext i1 %40 to i32
store i32 %41, i32* %6
br label %42
42: ; preds = %38, %27
%43 = load i32, i32* %6
ret i32 %43
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i64, i64 }
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
define i32 @try_lock(i32 %0, i32 %1, i32 %2, i64 %3, i64 %4) {
%6 = alloca %struct.flock
%7 = bitcast %struct.flock* %6 to i8*
%8 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 0
store i32 %1, i32* %8
%9 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 1
store i32 %2, i32* %9
%10 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 2
store i64 %3, i64* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 3
store i64 %4, i64* %11
%12 = call i32 @try_fcntl(i32 %0, %struct.flock* %6)
%13 = load i32, i32* @F_GETLK
%14 = icmp eq i32 %13, %0
%15 = icmp eq i32 %12, 0
br i1 %14, label %16, label %23
16: ; preds = %5
%17 = zext i1 %15 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i32, i32* %8
%20 = sext i32 %19 to i64
%21 = load i64, i64* @F_UNLCK
%22 = icmp eq i64 %21, %20
br label %23
23: ; preds = %5, %16
%24 = phi i1 [ %22, %16 ], [ %15, %5 ]
%25 = zext i1 %24 to i32
ret i32 %25
}