#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__ EACCES;
scalar_t__ EAGAIN;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
scalar_t__ errno;
int lockit(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ poll(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int mytrylock(int fd) {
  int backoff = 50;
  int retries = 10;
  while (lockit(fd, F_SETLK, F_WRLCK) == -1) {
    if (errno != EACCES && errno != EAGAIN) {
      return -1;
    }
    if (--retries == 0) {
      return 0;
    }
    backoff += 50;
    poll(NULL, 0, backoff);
  }
  return 1;
}


x86 assembly (clang, -O0):
<mytrylock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x32,-0xc(%rbp)
movl   $0xa,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea5(%rip),%rax
mov    (%rax),%esi
mov    0x2e7c(%rip),%rax
mov    (%rax),%edx
call   1030 <lockit@plt>
cmp    $0xffffffff,%eax
jne    11d6 <mytrylock+0xb6>
mov    0x2e6d(%rip),%rax
mov    (%rax),%rax
mov    0x2e73(%rip),%rcx
cmp    (%rcx),%rax
je     119c <mytrylock+0x7c>
mov    0x2e53(%rip),%rax
mov    (%rax),%rax
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%rax
je     119c <mytrylock+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dd <mytrylock+0xbd>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jne    11ba <mytrylock+0x9a>
movl   $0x0,-0x4(%rbp)
jmp    11dd <mytrylock+0xbd>
mov    -0xc(%rbp),%eax
add    $0x32,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edx
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1040 <poll@plt>
jmp    1139 <mytrylock+0x19>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mytrylock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2eb4(%rip),%r15
mov    (%r15),%esi
mov    0x2e8a(%rip),%rbx
mov    (%rbx),%edx
call   1030 <lockit@plt>
mov    $0x1,%ecx
cmp    $0xffffffff,%eax
je     1160 <mytrylock+0x40>
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x64,%edx
mov    0x2e64(%rip),%r12
mov    0x2e6d(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1196 <mytrylock+0x76>
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
jne    11c7 <mytrylock+0xa7>
lea    0x32(%rdx),%r14d
cmp    $0x258,%r14d
je     11ce <mytrylock+0xae>
xor    %edi,%edi
xor    %esi,%esi
call   1040 <poll@plt>
mov    (%r15),%esi
mov    (%rbx),%edx
mov    %ebp,%edi
call   1030 <lockit@plt>
mov    %r14d,%edx
cmp    $0xffffffff,%eax
je     1180 <mytrylock+0x60>
mov    $0x1,%ecx
jmp    114f <mytrylock+0x2f>
mov    $0xffffffff,%ecx
jmp    114f <mytrylock+0x2f>
xor    %ecx,%ecx
jmp    114f <mytrylock+0x2f>

LLVM IR (-O0):
@F_SETLK = global i32 0
@F_WRLCK = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
define i32 @mytrylock(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 50, i32* %4
store i32 10, i32* %5
br label %6
6: ; preds = %26, %1
%7 = load i32, i32* %3
%8 = load i32, i32* @F_SETLK
%9 = load i32, i32* @F_WRLCK
%10 = call i32 @lockit(i32 %7, i32 %8, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %31
12: ; preds = %6
%13 = load i64, i64* @errno
%14 = load i64, i64* @EACCES
%15 = icmp ne i64 %13, %14
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i64, i64* @errno
%18 = load i64, i64* @EAGAIN
%19 = icmp ne i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 -1, i32* %2
br label %32
21: ; preds = %16, %12
%22 = load i32, i32* %5
%23 = add nsw i32 %22, -1
store i32 %23, i32* %5
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 0, i32* %2
br label %32
26: ; preds = %21
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 50
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = call i32 @poll(i32* null, i32 0, i32 %29)
br label %6
31: ; preds = %6
store i32 1, i32* %2
br label %32
32: ; preds = %31, %25, %20
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@F_SETLK = global i32 0
@F_WRLCK = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
define i32 @mytrylock(i32 %0) {
%2 = load i32, i32* @F_SETLK
%3 = load i32, i32* @F_WRLCK
%4 = tail call i32 @lockit(i32 %0, i32 %2, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %25
6: ; preds = %1, %18
%7 = phi i32 [ %16, %18 ], [ 10, %1 ]
%8 = phi i32 [ %19, %18 ], [ 50, %1 ]
%9 = load i64, i64* @errno
%10 = load i64, i64* @EACCES
%11 = icmp eq i64 %9, %10
%12 = load i64, i64* @EAGAIN
%13 = icmp eq i64 %9, %12
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %15, label %25
15: ; preds = %6
%16 = add nsw i32 %7, -1
%17 = icmp eq i32 %16, 0
br i1 %17, label %25, label %18
18: ; preds = %15
%19 = add nuw nsw i32 %8, 50
%20 = tail call i32 @poll(i32* null, i32 0, i32 %19)
%21 = load i32, i32* @F_SETLK
%22 = load i32, i32* @F_WRLCK
%23 = tail call i32 @lockit(i32 %0, i32 %21, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %6, label %25
25: ; preds = %6, %15, %18, %1
%26 = phi i32 [ 1, %1 ], [ 1, %18 ], [ 0, %15 ], [ -1, %6 ]
ret i32 %26
}