#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 {
  scalar_t__ l_len;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EAGAIN;
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep_tenth_second();
int lock_file(FILE *f, int lock_type, int timeout) {
  int fd;
  int lock_success;
  int tenth_seconds;
  struct flock lock;
  fd = fileno(f);
  lock.l_type = (lock_type == 1) ? F_WRLCK : F_RDLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = 0;
  lock.l_len = 0;
  tenth_seconds = 0;
  for (;;) {
    errno = 0;
    lock_success = (fcntl(fd, F_SETLK, &lock) != -1);
    if (lock_success || (errno != EACCES && errno != EAGAIN) ||
        tenth_seconds / 10 >= timeout) {
      break;
    } else {
      sleep_tenth_second();
      tenth_seconds++;
    }
  }
  return (lock_success ? 0 : (tenth_seconds / 10 >= timeout ? 1 : 2));
}


x86 assembly (clang, -O0):
<lock_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    1169 <lock_file+0x39>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
jmp    1175 <lock_file+0x45>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e2b(%rip),%rax
movq   $0x0,(%rax)
mov    -0x14(%rbp),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1221 <lock_file+0xf1>
mov    0x2df0(%rip),%rax
mov    (%rax),%rax
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%rax
je     120d <lock_file+0xdd>
mov    0x2dd6(%rip),%rax
mov    (%rax),%rax
mov    0x2dac(%rip),%rcx
cmp    (%rcx),%rax
jne    1221 <lock_file+0xf1>
mov    -0x1c(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    -0x10(%rbp),%eax
jl     1226 <lock_file+0xf6>
jmp    123b <lock_file+0x10b>
mov    $0x0,%al
call   1050 <sleep_tenth_second@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119e <lock_file+0x6e>
cmpl   $0x0,-0x18(%rbp)
je     124f <lock_file+0x11f>
xor    %eax,%eax
mov    %eax,-0x40(%rbp)
jmp    1271 <lock_file+0x141>
mov    -0x1c(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
mov    -0x10(%rbp),%esi
mov    $0x2,%eax
mov    $0x1,%ecx
cmp    %esi,%edx
cmovge %ecx,%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %esi,%ebx
call   1040 <fileno@plt>
mov    %eax,%r12d
cmp    $0x1,%ebx
jne    1159 <lock_file+0x29>
mov    0x2e69(%rip),%rax
jmp    1160 <lock_file+0x30>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    0x2e4f(%rip),%rbp
movq   $0x0,0x0(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r12d,%edi
call   1030 <fcntl@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
jne    121e <lock_file+0xee>
xor    %r13d,%r13d
mov    $0xcccccccd,%r14d
xor    %ebx,%ebx
nop
mov    0x0(%rbp),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
sete   %cl
mov    0x2de8(%rip),%rdx
cmp    (%rdx),%rax
sete   %dl
or     %cl,%dl
mov    %r13d,%eax
imul   %r14,%rax
shr    $0x23,%rax
cmp    $0x1,%dl
jne    1213 <lock_file+0xe3>
cmp    %r15d,%eax
jge    1213 <lock_file+0xe3>
xor    %eax,%eax
call   1050 <sleep_tenth_second@plt>
add    $0x1,%r13d
movq   $0x0,0x0(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %rsp,%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11b0 <lock_file+0x80>
jmp    121e <lock_file+0xee>
xor    %ebx,%ebx
cmp    %r15d,%eax
setl   %bl
add    $0x1,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i64, i32, i32 }
@F_WRLCK = global i32 0
@F_RDLCK = global i32 0
@SEEK_SET = global i32 0
@errno = global i64 0
@F_SETLK = global i32 0
@EACCES = global i64 0
@EAGAIN = global i64 0
define i32 @lock_file(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.flock
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = load i32*, i32** %4
%12 = call i32 @fileno(i32* %11)
store i32 %12, i32* %7
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = load i32, i32* @F_WRLCK
br label %19
17: ; preds = %3
%18 = load i32, i32* @F_RDLCK
br label %19
19: ; preds = %17, %15
%20 = phi i32 [ %16, %15 ], [ %18, %17 ]
%21 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 3
store i32 %20, i32* %21
%22 = load i32, i32* @SEEK_SET
%23 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 2
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 1
store i64 0, i64* %24
%25 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 0
store i64 0, i64* %25
store i32 0, i32* %9
br label %26
26: ; preds = %52, %19
store i64 0, i64* @errno
%27 = load i32, i32* %7
%28 = load i32, i32* @F_SETLK
%29 = call i32 @fcntl(i32 %27, i32 %28, %struct.flock* %10)
%30 = icmp ne i32 %29, -1
%31 = zext i1 %30 to i32
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp ne i32 %32, 0
br i1 %33, label %47, label %34
34: ; preds = %26
%35 = load i64, i64* @errno
%36 = load i64, i64* @EACCES
%37 = icmp ne i64 %35, %36
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i64, i64* @errno
%40 = load i64, i64* @EAGAIN
%41 = icmp ne i64 %39, %40
br i1 %41, label %47, label %42
42: ; preds = %38, %34
%43 = load i32, i32* %9
%44 = sdiv i32 %43, 10
%45 = load i32, i32* %6
%46 = icmp sge i32 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %42, %38, %26
br label %53
48: ; preds = %42
%49 = call i32 (...) @sleep_tenth_second()
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 1
store i32 %51, i32* %9
br label %52
52: ; preds = %48
br label %26
53: ; preds = %47
%54 = load i32, i32* %8
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %53
br label %64
57: ; preds = %53
%58 = load i32, i32* %9
%59 = sdiv i32 %58, 10
%60 = load i32, i32* %6
%61 = icmp sge i32 %59, %60
%62 = zext i1 %61 to i64
%63 = select i1 %61, i32 1, i32 2
br label %64
64: ; preds = %57, %56
%65 = phi i32 [ 0, %56 ], [ %63, %57 ]
ret i32 %65
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i32, i32 }
@F_WRLCK = global i32 0
@F_RDLCK = global i32 0
@SEEK_SET = global i32 0
@errno = global i64 0
@F_SETLK = global i32 0
@EACCES = global i64 0
@EAGAIN = global i64 0
define i32 @lock_file(i32* nocapture %0, i32 %1, i32 %2) {
%4 = alloca %struct.flock
%5 = bitcast %struct.flock* %4 to i8*
%6 = tail call i32 @fileno(i32* %0)
%7 = icmp eq i32 %1, 1
%8 = load i32, i32* @F_WRLCK
%9 = load i32, i32* @F_RDLCK
%10 = select i1 %7, i32 %8, i32 %9
%11 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 3
store i32 %10, i32* %11
%12 = load i32, i32* @SEEK_SET
%13 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 2
store i32 %12, i32* %13
%14 = bitcast %struct.flock* %4 to i8*
store i64 0, i64* @errno
%15 = load i32, i32* @F_SETLK
%16 = call i32 @fcntl(i32 %6, i32 %15, %struct.flock* %4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %37
18: ; preds = %3, %29
%19 = phi i32 [ %31, %29 ], [ 0, %3 ]
%20 = load i64, i64* @errno
%21 = load i64, i64* @EACCES
%22 = icmp eq i64 %20, %21
%23 = load i64, i64* @EAGAIN
%24 = icmp eq i64 %20, %23
%25 = select i1 %22, i1 true, i1 %24
%26 = udiv i32 %19, 10
%27 = icmp slt i32 %26, %2
%28 = select i1 %25, i1 %27, i1 false
br i1 %28, label %29, label %35
29: ; preds = %18
%30 = call i32 (...) @sleep_tenth_second()
%31 = add nuw nsw i32 %19, 1
store i64 0, i64* @errno
%32 = load i32, i32* @F_SETLK
%33 = call i32 @fcntl(i32 %6, i32 %32, %struct.flock* %4)
%34 = icmp eq i32 %33, -1
br i1 %34, label %18, label %37
35: ; preds = %18
%36 = select i1 %27, i32 2, i32 1
br label %37
37: ; preds = %29, %3, %35
%38 = phi i32 [ %36, %35 ], [ 0, %3 ], [ 0, %29 ]
ret i32 %38
}