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

/* Variables and functions */
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
scalar_t__ close(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char const *);
void do_tests(const char *path) {
  int fd;
  int err;
  struct flock flock;
  fd = open(path, O_RDONLY);
  if (fd < 0) {
    perror(path);
    return;
  }
  do {
    flock.l_type = F_RDLCK;
    flock.l_whence = SEEK_CUR;
    flock.l_start = 0;
    flock.l_len = 0;
    flock.l_pid = 0;
    err = fcntl(fd, F_SETLK, &flock);
    if (err)
      break;
    flock.l_type = F_UNLCK;
    err = fcntl(fd, F_SETLK, &flock);
    if (err)
      break;
  } while (0);
  if (err)
    perror(path);
  if (close(fd) != 0)
    perror(path);
}


x86 assembly (clang, -O0):
<do_tests>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1179 <do_tests+0x39>
mov    -0x8(%rbp),%rdi
call   1060 <perror@plt>
jmp    123b <do_tests+0xfb>
jmp    117e <do_tests+0x3e>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11d5 <do_tests+0x95>
jmp    120d <do_tests+0xcd>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1208 <do_tests+0xc8>
jmp    120d <do_tests+0xcd>
jmp    120d <do_tests+0xcd>
cmpl   $0x0,-0x10(%rbp)
je     1220 <do_tests+0xe0>
mov    -0x8(%rbp),%rdi
call   1060 <perror@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     123b <do_tests+0xfb>
mov    -0x8(%rbp),%rdi
call   1060 <perror@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_tests>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%r14
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     11e5 <do_tests+0xa5>
mov    %eax,%ebp
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2e43(%rip),%rbx
mov    (%rbx),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
test   %eax,%eax
jne    11db <do_tests+0x9b>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    (%rbx),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
test   %eax,%eax
jne    11db <do_tests+0x9b>
mov    %ebp,%edi
call   1040 <close@plt>
test   %rax,%rax
jne    11f5 <do_tests+0xb5>
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %r14,%rdi
call   1060 <perror@plt>
jmp    11c6 <do_tests+0x86>
mov    %r14,%rdi
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <perror@plt>
mov    %r14,%rdi
call   1060 <perror@plt>
jmp    11d2 <do_tests+0x92>

LLVM IR (-O0):
%struct.flock = type { i32, i64, i64, i64, i32 }
@O_RDONLY = global i32 0
@F_RDLCK = global i32 0
@SEEK_CUR = global i32 0
@F_SETLK = global i32 0
@F_UNLCK = global i32 0
define void @do_tests(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.flock
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = call i32 @perror(i8* %12)
br label %52
14: ; preds = %1
br label %15
15: ; preds = %14
%16 = load i32, i32* @F_RDLCK
%17 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @SEEK_CUR
%19 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 4
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 3
store i64 0, i64* %20
%21 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 2
store i64 0, i64* %21
%22 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
store i64 0, i64* %22
%23 = load i32, i32* %3
%24 = load i32, i32* @F_SETLK
%25 = call i32 @fcntl(i32 %23, i32 %24, %struct.flock* %5)
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %15
br label %39
29: ; preds = %15
%30 = load i32, i32* @F_UNLCK
%31 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* %3
%33 = load i32, i32* @F_SETLK
%34 = call i32 @fcntl(i32 %32, i32 %33, %struct.flock* %5)
store i32 %34, i32* %4
%35 = load i32, i32* %4
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %29
br label %39
38: ; preds = %29
br label %39
39: ; preds = %38, %37, %28
%40 = load i32, i32* %4
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i8*, i8** %2
%44 = call i32 @perror(i8* %43)
br label %45
45: ; preds = %42, %39
%46 = load i32, i32* %3
%47 = call i64 @close(i32 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i8*, i8** %2
%51 = call i32 @perror(i8* %50)
br label %52
52: ; preds = %11, %49, %45
ret void
}

LLVM IR (-O2):
%struct.flock = type { i32, i64, i64, i64, i32 }
@O_RDONLY = global i32 0
@F_RDLCK = global i32 0
@SEEK_CUR = global i32 0
@F_SETLK = global i32 0
@F_UNLCK = global i32 0
define void @do_tests(i8* nocapture %0) {
%2 = alloca %struct.flock
%3 = bitcast %struct.flock* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* %0)
br label %31
9: ; preds = %1
%10 = load i32, i32* @F_RDLCK
%11 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @SEEK_CUR
%13 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 4
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
%15 = bitcast i64* %14 to i8*
%16 = load i32, i32* @F_SETLK
%17 = call i32 @fcntl(i32 %5, i32 %16, %struct.flock* %2)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %9
%20 = load i32, i32* @F_UNLCK
store i32 %20, i32* %11
%21 = load i32, i32* @F_SETLK
%22 = call i32 @fcntl(i32 %5, i32 %21, %struct.flock* %2)
%23 = icmp eq i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %9, %19
%25 = call i32 @perror(i8* %0)
br label %26
26: ; preds = %24, %19
%27 = call i64 @close(i32 %5)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = call i32 @perror(i8* %0)
br label %31
31: ; preds = %26, %29, %7
ret void
}