#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_ino;
};

/* Variables and functions */
int O_CREAT;
int O_NOCTTY;
int O_RDONLY;
int bee_flock_close(int);
int bee_flock_fd(int, int, int /*<<< orphan*/);
int close(int);
int open_and_stat(char *, int, int, struct stat *);
int /*<<< orphan*/ perror(char *);
int bee_flock(char *filename, int operation, int flags) {
  int res;
  int fd, fd2;
  struct stat statbuf_pre, statbuf_post;
  fd = open_and_stat(filename, O_RDONLY | O_CREAT | O_NOCTTY, 0666,
                     &statbuf_pre);
  if (fd < 0)
    return -1;
  while (1) {
    res = bee_flock_fd(fd, operation, 0);
    if (res < 0)
      return -1;
    fd2 = open_and_stat(filename, O_RDONLY | O_CREAT | O_NOCTTY, 0666,
                        &statbuf_post);
    if (fd2 < 0) {
      bee_flock_close(fd);
      return -1;
    }
    if (statbuf_pre.st_ino == statbuf_post.st_ino) {
      res = close(fd2);
      if (res < 0) {
        perror("bee_flock: close");
        bee_flock_close(fd);
        return -1;
      }
      return fd;
    }
    res = bee_flock_close(fd);
    if (res < 0) {
      res = close(fd2);
      if (res < 0)
        perror("bee_flock: close");
      return -1;
    }
    fd = fd2;
    statbuf_pre = statbuf_post;
  }
  return -1;
}


x86 assembly (clang, -O0):
<bee_flock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    0x2e82(%rip),%rax
or     (%rax),%esi
mov    0x2e51(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
lea    -0x30(%rbp),%rcx
call   1040 <open_and_stat@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11a8 <bee_flock+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <bee_flock+0x174>
jmp    11ad <bee_flock+0x5d>
mov    -0x20(%rbp),%edi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   1060 <bee_flock_fd@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d3 <bee_flock+0x83>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <bee_flock+0x174>
mov    -0x10(%rbp),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    0x2e11(%rip),%rax
or     (%rax),%esi
mov    0x2de0(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
lea    -0x38(%rbp),%rcx
call   1040 <open_and_stat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1221 <bee_flock+0xd1>
mov    -0x20(%rbp),%edi
call   1030 <bee_flock_close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <bee_flock+0x174>
mov    -0x30(%rbp),%rax
cmp    -0x38(%rbp),%rax
jne    126f <bee_flock+0x11f>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1264 <bee_flock+0x114>
lea    0xdb5(%rip),%rdi
call   1070 <perror@plt>
mov    -0x20(%rbp),%edi
call   1030 <bee_flock_close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <bee_flock+0x174>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c4 <bee_flock+0x174>
mov    -0x20(%rbp),%edi
call   1030 <bee_flock_close@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12b1 <bee_flock+0x161>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12a5 <bee_flock+0x155>
lea    0xd60(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <bee_flock+0x174>
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11ad <bee_flock+0x5d>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bee_flock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e7b(%rip),%rcx
mov    0x2e8c(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %esi,%r15d
mov    %rdi,%r13
mov    0x2e53(%rip),%r14
or     (%r14),%eax
lea    0x10(%rsp),%rcx
mov    %eax,%esi
mov    $0x1b6,%edx
call   1040 <open_and_stat@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     1260 <bee_flock+0x110>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15d,%esi
xor    %edx,%edx
call   1060 <bee_flock_fd@plt>
test   %eax,%eax
js     1260 <bee_flock+0x110>
lea    0x8(%rsp),%r12
nopl   0x0(%rax)
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e10(%rip),%rax
or     (%rax),%esi
or     (%r14),%esi
mov    %r13,%rdi
mov    $0x1b6,%edx
mov    %r12,%rcx
call   1040 <open_and_stat@plt>
test   %eax,%eax
js     1254 <bee_flock+0x104>
mov    %eax,%ebp
mov    0x10(%rsp),%rax
cmp    0x8(%rsp),%rax
je     1220 <bee_flock+0xd0>
mov    %ebx,%edi
call   1030 <bee_flock_close@plt>
test   %eax,%eax
js     122f <bee_flock+0xdf>
mov    0x8(%rsp),%rax
mov    %rax,0x10(%rsp)
mov    %ebp,%edi
mov    %r15d,%esi
xor    %edx,%edx
call   1060 <bee_flock_fd@plt>
mov    %ebp,%ebx
test   %eax,%eax
jns    11c0 <bee_flock+0x70>
jmp    125b <bee_flock+0x10b>
mov    %ebp,%edi
call   1050 <close@plt>
test   %eax,%eax
js     1248 <bee_flock+0xf8>
mov    %ebx,%ebp
jmp    1260 <bee_flock+0x110>
mov    %ebp,%edi
call   1050 <close@plt>
test   %eax,%eax
jns    125b <bee_flock+0x10b>
lea    0xdbf(%rip),%rdi
call   1070 <perror@plt>
jmp    125b <bee_flock+0x10b>
lea    0xdb1(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1030 <bee_flock_close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_NOCTTY = global i32 0
@.str = [17 x i8] c"bee_flock: close\00"
define i32 @bee_flock(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 i32
%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_RDONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = load i32, i32* @O_NOCTTY
%18 = or i32 %16, %17
%19 = call i32 @open_and_stat(i8* %13, i32 %18, i32 438, %struct.stat* %11)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %3
store i32 -1, i32* %4
br label %78
23: ; preds = %3
br label %24
24: ; preds = %23, %74
%25 = load i32, i32* %9
%26 = load i32, i32* %6
%27 = call i32 @bee_flock_fd(i32 %25, i32 %26, i32 0)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
store i32 -1, i32* %4
br label %78
31: ; preds = %24
%32 = load i8*, i8** %5
%33 = load i32, i32* @O_RDONLY
%34 = load i32, i32* @O_CREAT
%35 = or i32 %33, %34
%36 = load i32, i32* @O_NOCTTY
%37 = or i32 %35, %36
%38 = call i32 @open_and_stat(i8* %32, i32 %37, i32 438, %struct.stat* %12)
store i32 %38, i32* %10
%39 = load i32, i32* %10
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %31
%42 = load i32, i32* %9
%43 = call i32 @bee_flock_close(i32 %42)
store i32 -1, i32* %4
br label %78
44: ; preds = %31
%45 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%46 = load i64, i64* %45
%47 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%48 = load i64, i64* %47
%49 = icmp eq i64 %46, %48
br i1 %49, label %50, label %61
50: ; preds = %44
%51 = load i32, i32* %10
%52 = call i32 @close(i32 %51)
store i32 %52, i32* %8
%53 = load i32, i32* %8
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %50
%56 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%57 = load i32, i32* %9
%58 = call i32 @bee_flock_close(i32 %57)
store i32 -1, i32* %4
br label %78
59: ; preds = %50
%60 = load i32, i32* %9
store i32 %60, i32* %4
br label %78
61: ; preds = %44
%62 = load i32, i32* %9
%63 = call i32 @bee_flock_close(i32 %62)
store i32 %63, i32* %8
%64 = load i32, i32* %8
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %74
66: ; preds = %61
%67 = load i32, i32* %10
%68 = call i32 @close(i32 %67)
store i32 %68, i32* %8
%69 = load i32, i32* %8
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %66
%72 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %73
73: ; preds = %71, %66
store i32 -1, i32* %4
br label %78
74: ; preds = %61
%75 = load i32, i32* %10
store i32 %75, i32* %9
%76 = bitcast %struct.stat* %11 to i8*
%77 = bitcast %struct.stat* %12 to i8*
br label %24
78: ; preds = %73, %59, %55, %41, %30, %22
%79 = load i32, i32* %4
ret i32 %79
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_NOCTTY = global i32 0
@.str = [17 x i8] c"bee_flock: close\00"
define i32 @bee_flock(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.stat
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %4 to i8*
%7 = bitcast %struct.stat* %5 to i8*
%8 = load i32, i32* @O_RDONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %9, %8
%11 = load i32, i32* @O_NOCTTY
%12 = or i32 %10, %11
%13 = call i32 @open_and_stat(i8* %0, i32 %12, i32 438, %struct.stat* %4)
%14 = icmp slt i32 %13, 0
br i1 %14, label %54, label %15
15: ; preds = %3
%16 = call i32 @bee_flock_fd(i32 %13, i32 %1, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %54, label %18
18: ; preds = %15
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%20 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
br label %21
21: ; preds = %18, %50
%22 = phi i32 [ %13, %18 ], [ %28, %50 ]
%23 = load i32, i32* @O_RDONLY
%24 = load i32, i32* @O_CREAT
%25 = or i32 %24, %23
%26 = load i32, i32* @O_NOCTTY
%27 = or i32 %25, %26
%28 = call i32 @open_and_stat(i8* %0, i32 %27, i32 438, %struct.stat* %5)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = call i32 @bee_flock_close(i32 %22)
br label %54
32: ; preds = %21
%33 = load i64, i64* %19
%34 = load i64, i64* %20
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = call i32 @close(i32 %28)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %54
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%41 = call i32 @bee_flock_close(i32 %22)
br label %54
42: ; preds = %32
%43 = call i32 @bee_flock_close(i32 %22)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %42
%46 = call i32 @close(i32 %28)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %45
%49 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %54
50: ; preds = %42
%51 = load i64, i64* %20
store i64 %51, i64* %19
%52 = call i32 @bee_flock_fd(i32 %28, i32 %1, i32 0)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %21
54: ; preds = %50, %15, %45, %48, %36, %3, %39, %30
%55 = phi i32 [ -1, %30 ], [ -1, %39 ], [ -1, %3 ], [ %22, %36 ], [ -1, %48 ], [ -1, %45 ], [ -1, %15 ], [ -1, %50 ]
ret i32 %55
}