#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 inotify_event {
  scalar_t__ len;
  int mask;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ ib;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int IN_MODIFY;
scalar_t__ errno;
scalar_t__ read(int, char *, int);
int wait_for_inotify(int fd) {
  char ib[4 * sizeof(struct inotify_event)];
  struct inotify_event *ev;
  ssize_t len, i;
  len = read(fd, ib, sizeof(ib));
  if (len < 0) {
    if (errno == EINTR || errno == EAGAIN) {
      return 0;
    }
    return -2;
  }
  for (i = 0; i < len; i += sizeof(struct inotify_event) + ev->len) {
    ev = (struct inotify_event *)(ib + i);
    if (ev->mask & ~IN_MODIFY) {
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<wait_for_inotify>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x40,%edx
call   1030 <read@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jge    1187 <wait_for_inotify+0x77>
mov    0x2e96(%rip),%rax
mov    (%rax),%rax
mov    0x2e84(%rip),%rcx
cmp    (%rcx),%rax
je     116f <wait_for_inotify+0x5f>
mov    0x2e7c(%rip),%rax
mov    (%rax),%rax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%rax
jne    117b <wait_for_inotify+0x6b>
movl   $0x0,-0x4(%rbp)
jmp    11f7 <wait_for_inotify+0xe7>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11f7 <wait_for_inotify+0xe7>
movq   $0x0,-0x68(%rbp)
mov    -0x68(%rbp),%rax
cmp    -0x60(%rbp),%rax
jge    11f0 <wait_for_inotify+0xe0>
lea    -0x50(%rbp),%rax
add    -0x68(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%eax
mov    0x2e39(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11d3 <wait_for_inotify+0xc3>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f7 <wait_for_inotify+0xe7>
jmp    11d8 <wait_for_inotify+0xc8>
mov    -0x58(%rbp),%rax
mov    (%rax),%rax
add    $0x10,%rax
add    -0x68(%rbp),%rax
mov    %rax,-0x68(%rbp)
jmp    118f <wait_for_inotify+0x7f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_inotify>:
sub    $0x48,%rsp
mov    %rsp,%rsi
mov    $0x40,%edx
call   1030 <read@plt>
test   %rax,%rax
js     115d <wait_for_inotify+0x4d>
je     1156 <wait_for_inotify+0x46>
mov    0x2ec1(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
nop
test   %ecx,0x8(%rsp,%rdx,1)
jne    1190 <wait_for_inotify+0x80>
mov    (%rsp,%rdx,1),%rsi
add    %rsi,%rdx
add    $0x10,%rdx
cmp    %rax,%rdx
jl     1140 <wait_for_inotify+0x30>
xor    %eax,%eax
add    $0x48,%rsp
ret
mov    0x2e74(%rip),%rax
mov    (%rax),%rax
mov    0x2e62(%rip),%rcx
cmp    (%rcx),%rax
sete   %cl
mov    0x2e45(%rip),%rdx
cmp    (%rdx),%rax
sete   %al
or     %cl,%al
movzbl %al,%eax
add    %eax,%eax
add    $0xfffffffe,%eax
add    $0x48,%rsp
ret
mov    $0xffffffff,%eax
add    $0x48,%rsp
ret

LLVM IR (-O0):
%struct.inotify_event = type { i64, i32 }
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@IN_MODIFY = global i32 0
define i32 @wait_for_inotify(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [64 x i8]
%5 = alloca %struct.inotify_event*
%6 = alloca i64
%7 = alloca i64
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%10 = call i64 @read(i32 %8, i8* %9, i32 64)
store i64 %10, i64* %6
%11 = load i64, i64* %6
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %1
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = load i64, i64* @errno
%19 = load i64, i64* @EAGAIN
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17, %13
store i32 0, i32* %2
br label %50
22: ; preds = %17
store i32 -2, i32* %2
br label %50
23: ; preds = %1
store i64 0, i64* %7
br label %24
24: ; preds = %42, %23
%25 = load i64, i64* %7
%26 = load i64, i64* %6
%27 = icmp slt i64 %25, %26
br i1 %27, label %28, label %49
28: ; preds = %24
%29 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%30 = load i64, i64* %7
%31 = getelementptr inbounds i8, i8* %29, i64 %30
%32 = bitcast i8* %31 to %struct.inotify_event*
store %struct.inotify_event* %32, %struct.inotify_event** %5
%33 = load %struct.inotify_event*, %struct.inotify_event** %5
%34 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %33, i32 0, i32 1
%35 = load i32, i32* %34
%36 = load i32, i32* @IN_MODIFY
%37 = xor i32 %36, -1
%38 = and i32 %35, %37
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %28
store i32 -1, i32* %2
br label %50
41: ; preds = %28
br label %42
42: ; preds = %41
%43 = load %struct.inotify_event*, %struct.inotify_event** %5
%44 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %43, i32 0, i32 0
%45 = load i64, i64* %44
%46 = add i64 16, %45
%47 = load i64, i64* %7
%48 = add i64 %47, %46
store i64 %48, i64* %7
br label %24
49: ; preds = %24
store i32 0, i32* %2
br label %50
50: ; preds = %49, %40, %22, %21
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@IN_MODIFY = global i32 0
define i32 @wait_for_inotify(i32 %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = call i64 @read(i32 %0, i8* %3, i32 64)
%5 = icmp slt i64 %4, 0
br i1 %5, label %11, label %6
6: ; preds = %1
%7 = icmp eq i64 %4, 0
br i1 %7, label %33, label %8
8: ; preds = %6
%9 = load i32, i32* @IN_MODIFY
%10 = xor i32 %9, -1
br label %19
11: ; preds = %1
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
%15 = load i64, i64* @EAGAIN
%16 = icmp eq i64 %12, %15
%17 = select i1 %14, i1 true, i1 %16
%18 = select i1 %17, i32 0, i32 -2
br label %33
19: ; preds = %8, %27
%20 = phi i64 [ 0, %8 ], [ %31, %27 ]
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 %20
%22 = getelementptr inbounds i8, i8* %21, i64 8
%23 = bitcast i8* %22 to i32*
%24 = load i32, i32* %23
%25 = and i32 %24, %10
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %19
%28 = bitcast i8* %21 to i64*
%29 = load i64, i64* %28
%30 = add i64 %20, 16
%31 = add i64 %30, %29
%32 = icmp slt i64 %31, %4
br i1 %32, label %19, label %33
33: ; preds = %19, %27, %6, %11
%34 = phi i32 [ %18, %11 ], [ 0, %6 ], [ -1, %19 ], [ 0, %27 ]
ret i32 %34
}