#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 stat {
  scalar_t__ st_dev;
  scalar_t__ st_ino;
};
struct flock {
  int l_len;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
struct flock *FD_CLOEXEC;
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_WRLCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, struct flock *);
scalar_t__ fstat(int, struct stat *);
int open(char const *, int, int);
scalar_t__ stat(char const *, struct stat *);
int u_lock(const char *path, int excl, int dowait) {
  int h;
  h = open(path, O_RDWR | O_CREAT, 0666);
  if (!((h) >= 0))
    return h;
  fcntl(h, F_SETFD, FD_CLOEXEC);
  for (;;) {
    int success;
    struct flock lck = {0};
    lck.l_type = excl ? F_WRLCK : F_RDLCK;
    lck.l_whence = SEEK_SET;
    lck.l_len = 1;
    success = !fcntl(h, dowait ? F_SETLKW : F_SETLK, &lck);
    if (!success)
      break;
    struct stat s1, s2;
    if (fstat(h, &s1) || stat(path, &s2))
      break;
    success = s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino;
    if (success)
      return h;
    close(h);
  }
  close(h);
  return -1;
}


x86 assembly (clang, -O0):
<u_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    0x2e72(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11aa <u_lock+0x4a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ff <u_lock+0x19f>
mov    -0x1c(%rbp),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    0x2dfb(%rip),%rax
mov    (%rax),%rdx
call   1030 <fcntl@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
cmpl   $0x0,-0x14(%rbp)
je     11f0 <u_lock+0x90>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
jmp    11fc <u_lock+0x9c>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1236 <u_lock+0xd6>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
jmp    1242 <u_lock+0xe2>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x58(%rbp),%edi
mov    -0x5c(%rbp),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1270 <u_lock+0x110>
jmp    12f0 <u_lock+0x190>
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
jne    129d <u_lock+0x13d>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     12a2 <u_lock+0x142>
jmp    12f0 <u_lock+0x190>
mov    -0x40(%rbp),%rcx
xor    %eax,%eax
cmp    -0x50(%rbp),%rcx
mov    %al,-0x5d(%rbp)
jne    12c3 <u_lock+0x163>
mov    -0x38(%rbp),%rax
cmp    -0x48(%rbp),%rax
sete   %al
mov    %al,-0x5d(%rbp)
mov    -0x5d(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     12e3 <u_lock+0x183>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ff <u_lock+0x19f>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
jmp    11c5 <u_lock+0x65>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<u_lock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbp
mov    0x2e72(%rip),%rax
mov    0x2e83(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     126f <u_lock+0x11f>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    0x2e1b(%rip),%rax
mov    (%rax),%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
lea    0x8(%rsp),%r12
lea    0x28(%rsp),%r13
jmp    11c7 <u_lock+0x77>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2dda(%rip),%rax
test   %r15d,%r15d
jne    11da <u_lock+0x8a>
mov    0x2dbe(%rip),%rax
movl   $0x0,0x14(%rsp)
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
movl   $0x1,0x8(%rsp)
mov    0x2dd4(%rip),%rax
test   %r14d,%r14d
jne    1210 <u_lock+0xc0>
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r12,%rdx
call   1030 <fcntl@plt>
test   %eax,%eax
jne    1263 <u_lock+0x113>
mov    %ebx,%edi
mov    %r13,%rsi
call   1070 <fstat@plt>
test   %rax,%rax
jne    1263 <u_lock+0x113>
mov    %rbp,%rdi
lea    0x18(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
jne    1263 <u_lock+0x113>
mov    0x28(%rsp),%rax
cmp    0x18(%rsp),%rax
jne    11c0 <u_lock+0x70>
mov    0x20(%rsp),%rax
cmp    %rax,0x30(%rsp)
jne    11c0 <u_lock+0x70>
jmp    126f <u_lock+0x11f>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i32 }
%struct.stat = type { i64, i64 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global %struct.flock* null
@F_WRLCK = global i32 0
@F_RDLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@F_SETLK = global i32 0
define i32 @u_lock(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.flock
%11 = alloca %struct.stat
%12 = alloca %struct.stat
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%13 = load i8*, i8** %5
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %13, i32 %16, i32 438)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp sge i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %3
%21 = load i32, i32* %8
store i32 %21, i32* %4
br label %91
22: ; preds = %3
%23 = load i32, i32* %8
%24 = load i32, i32* @F_SETFD
%25 = load %struct.flock*, %struct.flock** @FD_CLOEXEC
%26 = call i32 @fcntl(i32 %23, i32 %24, %struct.flock* %25)
br label %27
27: ; preds = %85, %22
%28 = bitcast %struct.flock* %10 to i8*
%29 = load i32, i32* %6
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i32, i32* @F_WRLCK
br label %35
33: ; preds = %27
%34 = load i32, i32* @F_RDLCK
br label %35
35: ; preds = %33, %31
%36 = phi i32 [ %32, %31 ], [ %34, %33 ]
%37 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 2
store i32 %36, i32* %37
%38 = load i32, i32* @SEEK_SET
%39 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 1
store i32 %38, i32* %39
%40 = getelementptr inbounds %struct.flock, %struct.flock* %10, i32 0, i32 0
store i32 1, i32* %40
%41 = load i32, i32* %8
%42 = load i32, i32* %7
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %35
%45 = load i32, i32* @F_SETLKW
br label %48
46: ; preds = %35
%47 = load i32, i32* @F_SETLK
br label %48
48: ; preds = %46, %44
%49 = phi i32 [ %45, %44 ], [ %47, %46 ]
%50 = call i32 @fcntl(i32 %41, i32 %49, %struct.flock* %10)
%51 = icmp ne i32 %50, 0
%52 = xor i1 %51, true
%53 = zext i1 %52 to i32
store i32 %53, i32* %9
%54 = load i32, i32* %9
%55 = icmp ne i32 %54, 0
br i1 %55, label %57, label %56
56: ; preds = %48
br label %88
57: ; preds = %48
%58 = load i32, i32* %8
%59 = call i64 @fstat(i32 %58, %struct.stat* %11)
%60 = icmp ne i64 %59, 0
br i1 %60, label %65, label %61
61: ; preds = %57
%62 = load i8*, i8** %5
%63 = call i64 @stat(i8* %62, %struct.stat* %12)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %61, %57
br label %88
66: ; preds = %61
%67 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%68 = load i64, i64* %67
%69 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%70 = load i64, i64* %69
%71 = icmp eq i64 %68, %70
br i1 %71, label %72, label %78
72: ; preds = %66
%73 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 1
%74 = load i64, i64* %73
%75 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 1
%76 = load i64, i64* %75
%77 = icmp eq i64 %74, %76
br label %78
78: ; preds = %72, %66
%79 = phi i1 [ false, %66 ], [ %77, %72 ]
%80 = zext i1 %79 to i32
store i32 %80, i32* %9
%81 = load i32, i32* %9
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %78
%84 = load i32, i32* %8
store i32 %84, i32* %4
br label %91
85: ; preds = %78
%86 = load i32, i32* %8
%87 = call i32 @close(i32 %86)
br label %27
88: ; preds = %65, %56
%89 = load i32, i32* %8
%90 = call i32 @close(i32 %89)
store i32 -1, i32* %4
br label %91
91: ; preds = %88, %83, %20
%92 = load i32, i32* %4
ret i32 %92
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i32 }
%struct.stat = type { i64, i64 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global %struct.flock* null
@F_WRLCK = global i32 0
@F_RDLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@F_SETLK = global i32 0
define i32 @u_lock(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.flock
%5 = alloca %struct.stat
%6 = alloca %struct.stat
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* %0, i32 %9, i32 438)
%11 = icmp sgt i32 %10, -1
br i1 %11, label %12, label %60
12: ; preds = %3
%13 = load i32, i32* @F_SETFD
%14 = load %struct.flock*, %struct.flock** @FD_CLOEXEC
%15 = tail call i32 @fcntl(i32 %10, i32 %13, %struct.flock* %14)
%16 = bitcast %struct.flock* %4 to i8*
%17 = icmp eq i32 %1, 0
%18 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 2
%19 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 1
%20 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 0
%21 = icmp eq i32 %2, 0
%22 = bitcast %struct.stat* %5 to i8*
%23 = bitcast %struct.stat* %6 to i8*
%24 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%25 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%26 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%27 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 1
%28 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 3
br label %29
29: ; preds = %56, %12
store i32 0, i32* %28
%30 = load i32, i32* @F_WRLCK
%31 = load i32, i32* @F_RDLCK
%32 = select i1 %17, i32 %31, i32 %30
store i32 %32, i32* %18
%33 = load i32, i32* @SEEK_SET
store i32 %33, i32* %19
store i32 1, i32* %20
%34 = load i32, i32* @F_SETLKW
%35 = load i32, i32* @F_SETLK
%36 = select i1 %21, i32 %35, i32 %34
%37 = call i32 @fcntl(i32 %10, i32 %36, %struct.flock* %4)
%38 = icmp eq i32 %37, 0
br i1 %38, label %40, label %39
39: ; preds = %29
br label %58
40: ; preds = %29
%41 = call i64 @fstat(i32 %10, %struct.stat* %5)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %55
43: ; preds = %40
%44 = call i64 @stat(i8* %0, %struct.stat* %6)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %55
46: ; preds = %43
%47 = load i64, i64* %24
%48 = load i64, i64* %25
%49 = icmp eq i64 %47, %48
%50 = load i64, i64* %26
%51 = load i64, i64* %27
%52 = icmp eq i64 %50, %51
%53 = select i1 %49, i1 %52, i1 false
br i1 %53, label %54, label %56
54: ; preds = %46
br label %60
55: ; preds = %43, %40
br label %58
56: ; preds = %46
%57 = call i32 @close(i32 %10)
br label %29
58: ; preds = %55, %39
%59 = call i32 @close(i32 %10)
br label %60
60: ; preds = %54, %3, %58
%61 = phi i32 [ -1, %58 ], [ %10, %3 ], [ %10, %54 ]
ret i32 %61
}