#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 {
  int len;
};
typedef int /*<<< orphan*/ event_buf;

/* Variables and functions */
scalar_t__ EINTR;
int call_notify_handler(struct inotify_event *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int handle_inotify_event(int nfd) {
  int res;
  int wake_up = 0;
  struct inotify_event *event;
  char event_buf[512];
  int event_pos = 0;
  res = read(nfd, event_buf, sizeof(event_buf));
  if (res < (int)sizeof(*event)) {
    if (errno == EINTR)
      return 0;
    fprintf(stderr, "could not get event, %s\n", strerror(errno));
    return 0;
  }
  printf("got %d bytes of event information\n", res);
  while (res >= (int)sizeof(*event)) {
    int event_size;
    event = (struct inotify_event *)(event_buf + event_pos);
    wake_up |= call_notify_handler(event);
    event_size = sizeof(*event) + event->len;
    res -= event_size;
    event_pos += event_size;
  }
  return wake_up;
}


x86 assembly (clang, -O0):
<handle_inotify_event>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x224(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x4,-0xc(%rbp)
jge    11f5 <handle_inotify_event+0xa5>
mov    0x2e41(%rip),%rax
mov    (%rax),%rax
mov    0x2e2f(%rip),%rcx
cmp    (%rcx),%rax
jne    11b6 <handle_inotify_event+0x66>
movl   $0x0,-0x4(%rbp)
jmp    126e <handle_inotify_event+0x11e>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x22c(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x22c(%rbp),%edi
mov    %rax,%rdx
lea    0xe1c(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    126e <handle_inotify_event+0x11e>
mov    -0xc(%rbp),%esi
lea    0xe1a(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x4,-0xc(%rbp)
jl     1268 <handle_inotify_event+0x118>
lea    -0x220(%rbp),%rax
movslq -0x224(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <call_notify_handler@plt>
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
add    $0x4,%rax
mov    %eax,-0x228(%rbp)
mov    -0x228(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0x228(%rbp),%eax
add    -0x224(%rbp),%eax
mov    %eax,-0x224(%rbp)
jmp    1204 <handle_inotify_event+0xb4>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_inotify_event>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %rsp,%rsi
mov    $0x200,%edx
call   1040 <read@plt>
cmp    $0x3,%eax
jg     11a7 <handle_inotify_event+0x57>
mov    0x2e64(%rip),%rax
mov    (%rax),%rdi
mov    0x2e52(%rip),%rax
xor    %r14d,%r14d
cmp    (%rax),%rdi
je     11e1 <handle_inotify_event+0x91>
mov    0x2e53(%rip),%rax
mov    (%rax),%ebp
call   1070 <strerror@plt>
lea    0xe65(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
jmp    11e1 <handle_inotify_event+0x91>
mov    %eax,%ebp
lea    0xe69(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%eax
xor    %r14d,%r14d
nopl   0x0(%rax)
movslq %eax,%rbx
lea    (%rsp,%rbx,1),%rdi
call   1060 <call_notify_handler@plt>
or     %eax,%r14d
mov    (%rsp,%rbx,1),%eax
lea    0x4(%rax),%ecx
sub    %ecx,%ebp
add    %ebx,%eax
add    $0x4,%eax
cmp    $0x3,%ebp
jg     11c0 <handle_inotify_event+0x70>
mov    %r14d,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.inotify_event = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [25 x i8] c"could not get event, %s\0A\00"
@.str.1 = [35 x i8] c"got %d bytes of event information\0A\00"
define i32 @handle_inotify_event(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.inotify_event*
%7 = alloca [512 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %5
store i32 0, i32* %8
%10 = load i32, i32* %3
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 512)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 4
br i1 %14, label %15, label %25
15: ; preds = %1
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 0, i32* %2
br label %55
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = load i64, i64* @errno
%23 = call i8* @strerror(i64 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %23)
store i32 0, i32* %2
br label %55
25: ; preds = %1
%26 = load i32, i32* %4
%27 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %31, %25
%29 = load i32, i32* %4
%30 = icmp sge i32 %29, 4
br i1 %30, label %31, label %53
31: ; preds = %28
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = bitcast i8* %35 to %struct.inotify_event*
store %struct.inotify_event* %36, %struct.inotify_event** %6
%37 = load %struct.inotify_event*, %struct.inotify_event** %6
%38 = call i32 @call_notify_handler(%struct.inotify_event* %37)
%39 = load i32, i32* %5
%40 = or i32 %39, %38
store i32 %40, i32* %5
%41 = load %struct.inotify_event*, %struct.inotify_event** %6
%42 = getelementptr inbounds %struct.inotify_event, %struct.inotify_event* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = sext i32 %43 to i64
%45 = add i64 4, %44
%46 = trunc i64 %45 to i32
store i32 %46, i32* %9
%47 = load i32, i32* %9
%48 = load i32, i32* %4
%49 = sub nsw i32 %48, %47
store i32 %49, i32* %4
%50 = load i32, i32* %9
%51 = load i32, i32* %8
%52 = add nsw i32 %51, %50
store i32 %52, i32* %8
br label %28
53: ; preds = %28
%54 = load i32, i32* %5
store i32 %54, i32* %2
br label %55
55: ; preds = %53, %20, %19
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
%struct.inotify_event = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [25 x i8] c"could not get event, %s\0A\00"
@.str.1 = [35 x i8] c"got %d bytes of event information\0A\00"
define i32 @handle_inotify_event(i32 %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%4 = call i32 @read(i32 %0, i8* %3, i32 512)
%5 = icmp slt i32 %4, 4
br i1 %5, label %6, label %14
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINTR
%9 = icmp eq i64 %7, %8
br i1 %9, label %31, label %10
10: ; preds = %6
%11 = load i32, i32* @stderr
%12 = tail call i8* @strerror(i64 %7)
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %31
14: ; preds = %1
%15 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %4)
br label %16
16: ; preds = %14, %16
%17 = phi i32 [ 0, %14 ], [ %29, %16 ]
%18 = phi i32 [ %4, %14 ], [ %28, %16 ]
%19 = phi i32 [ 0, %14 ], [ %24, %16 ]
%20 = sext i32 %17 to i64
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %20
%22 = bitcast i8* %21 to %struct.inotify_event*
%23 = call i32 @call_notify_handler(%struct.inotify_event* %22)
%24 = or i32 %23, %19
%25 = bitcast i8* %21 to i32*
%26 = load i32, i32* %25
%27 = add i32 %26, 4
%28 = sub nsw i32 %18, %27
%29 = add nsw i32 %27, %17
%30 = icmp sgt i32 %28, 3
br i1 %30, label %16, label %31
31: ; preds = %16, %6, %10
%32 = phi i32 [ 0, %10 ], [ 0, %6 ], [ %24, %16 ]
ret i32 %32
}