#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 */
typedef int /*<<< orphan*/ uint64_t;
typedef int /*<<< orphan*/ result;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ ENOENT;
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ W_OK;
int access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int read(int, int /*<<< orphan*/ *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int detect_oom(int event_fd, const char *event_control_path) {
  fd_set rfds;
  int rv;
  FD_ZERO(&rfds);
  FD_SET(event_fd, &rfds);
  do {
    rv = select(event_fd + 1, &rfds, NULL, NULL, NULL);
  } while (rv == -1 && errno == EINTR);
  if (!FD_ISSET(event_fd, &rfds)) {
    return -1;
  }
  uint64_t result;
  do {
    rv = read(event_fd, &result, sizeof(result));
  } while (rv == -1 && errno == EINTR);
  if (rv == -1) {
    perror("read");
    return -1;
  }
  assert(rv == sizeof(result));
  rv = access(event_control_path, W_OK);
  if (rv == -1 && errno == ENOENT) {
    perror("access");
    return -1;
  }
  if (rv == -1) {
    perror("access");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<detect_oom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1070 <select@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x18(%rbp)
mov    %al,-0x1d(%rbp)
jne    11ea <detect_oom+0x6a>
mov    0x2e01(%rip),%rax
mov    (%rax),%rax
mov    0x2def(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11a4 <detect_oom+0x24>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    1216 <detect_oom+0x96>
movl   $0xffffffff,-0x4(%rbp)
jmp    1314 <detect_oom+0x194>
jmp    121b <detect_oom+0x9b>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x18(%rbp)
mov    %al,-0x1e(%rbp)
jne    1258 <detect_oom+0xd8>
mov    0x2d93(%rip),%rax
mov    (%rax),%rax
mov    0x2d81(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1e(%rbp)
mov    -0x1e(%rbp),%al
test   $0x1,%al
jne    121b <detect_oom+0x9b>
cmpl   $0xffffffff,-0x18(%rbp)
jne    1285 <detect_oom+0x105>
lea    0xd8c(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1314 <detect_oom+0x194>
movslq -0x18(%rbp),%rax
cmp    $0x4,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12eb <detect_oom+0x16b>
mov    0x2d18(%rip),%rax
mov    (%rax),%rax
mov    0x2cf6(%rip),%rcx
cmp    (%rcx),%rax
jne    12eb <detect_oom+0x16b>
lea    0xd2b(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1314 <detect_oom+0x194>
cmpl   $0xffffffff,-0x18(%rbp)
jne    130d <detect_oom+0x18d>
lea    0xd09(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1314 <detect_oom+0x194>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detect_oom>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
mov    %rsp,%r12
mov    %r12,%rdi
call   1060 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %r12,%rsi
call   10a0 <FD_SET@plt>
lea    0x1(%r15),%ebp
mov    0x2e26(%rip),%r13
mov    0x2e17(%rip),%rbx
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <select@plt>
cmp    $0xffffffff,%eax
jne    11df <detect_oom+0x5f>
mov    (%rbx),%rax
cmp    %rax,0x0(%r13)
je     11c0 <detect_oom+0x40>
mov    %rsp,%rsi
mov    %r15d,%edi
call   1030 <FD_ISSET@plt>
test   %eax,%eax
je     1260 <detect_oom+0xe0>
lea    0x4(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    121e <detect_oom+0x9e>
mov    (%rbx),%rcx
cmp    %rcx,0x0(%r13)
je     1200 <detect_oom+0x80>
cmp    $0xffffffff,%eax
je     1254 <detect_oom+0xd4>
xor    %edi,%edi
cmp    $0x4,%eax
sete   %dil
call   1050 <assert@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1080 <access@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    1265 <detect_oom+0xe5>
lea    0xdb3(%rip),%rdi
jmp    125b <detect_oom+0xdb>
lea    0xda5(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"read\00"
@W_OK = global i32 0
@ENOENT = global i64 0
@.str.1 = [7 x i8] c"access\00"
define i32 @detect_oom(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = call i32 @FD_ZERO(i32* %6)
%10 = load i32, i32* %4
%11 = call i32 @FD_SET(i32 %10, i32* %6)
br label %12
12: ; preds = %23, %2
%13 = load i32, i32* %4
%14 = add nsw i32 %13, 1
%15 = call i32 @select(i32 %14, i32* %6, i32* null, i32* null, i32* null)
store i32 %15, i32* %7
br label %16
16: ; preds = %12
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br label %23
23: ; preds = %19, %16
%24 = phi i1 [ false, %16 ], [ %22, %19 ]
br i1 %24, label %12, label %25
25: ; preds = %23
%26 = load i32, i32* %4
%27 = call i32 @FD_ISSET(i32 %26, i32* %6)
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %25
store i32 -1, i32* %3
br label %71
30: ; preds = %25
br label %31
31: ; preds = %41, %30
%32 = load i32, i32* %4
%33 = call i32 @read(i32 %32, i32* %8, i32 4)
store i32 %33, i32* %7
br label %34
34: ; preds = %31
%35 = load i32, i32* %7
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINTR
%40 = icmp eq i64 %38, %39
br label %41
41: ; preds = %37, %34
%42 = phi i1 [ false, %34 ], [ %40, %37 ]
br i1 %42, label %31, label %43
43: ; preds = %41
%44 = load i32, i32* %7
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
48: ; preds = %43
%49 = load i32, i32* %7
%50 = sext i32 %49 to i64
%51 = icmp eq i64 %50, 4
%52 = zext i1 %51 to i32
%53 = call i32 @assert(i32 %52)
%54 = load i8*, i8** %5
%55 = load i32, i32* @W_OK
%56 = call i32 @access(i8* %54, i32 %55)
store i32 %56, i32* %7
%57 = load i32, i32* %7
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %65
59: ; preds = %48
%60 = load i64, i64* @errno
%61 = load i64, i64* @ENOENT
%62 = icmp eq i64 %60, %61
br i1 %62, label %63, label %65
63: ; preds = %59
%64 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
65: ; preds = %59, %48
%66 = load i32, i32* %7
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %70
68: ; preds = %65
%69 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
70: ; preds = %65
store i32 0, i32* %3
br label %71
71: ; preds = %70, %68, %63, %46, %29
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"read\00"
@W_OK = global i32 0
@ENOENT = global i64 0
@.str.1 = [7 x i8] c"access\00"
define i32 @detect_oom(i32 %0, i8* nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = call i32 @FD_ZERO(i32* %3)
%7 = call i32 @FD_SET(i32 %0, i32* %3)
%8 = add nsw i32 %0, 1
br label %9
9: ; preds = %9, %2
%10 = call i32 @select(i32 %8, i32* %3, i32* null, i32* null, i32* null)
%11 = icmp eq i32 %10, -1
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
%15 = select i1 %11, i1 %14, i1 false
br i1 %15, label %9, label %16
16: ; preds = %9
%17 = call i32 @FD_ISSET(i32 %0, i32* %3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %41, label %19
19: ; preds = %16
%20 = bitcast i32* %4 to i8*
br label %21
21: ; preds = %21, %19
%22 = call i32 @read(i32 %0, i32* %4, i32 4)
%23 = icmp eq i32 %22, -1
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
%27 = select i1 %23, i1 %26, i1 false
br i1 %27, label %21, label %28
28: ; preds = %21
br i1 %23, label %36, label %29
29: ; preds = %28
%30 = icmp eq i32 %22, 4
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = load i32, i32* @W_OK
%34 = call i32 @access(i8* %1, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %29, %28
%37 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %28 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %29 ]
%38 = call i32 @perror(i8* %37)
br label %39
39: ; preds = %36, %29
%40 = phi i32 [ 0, %29 ], [ -1, %36 ]
br label %41
41: ; preds = %16, %39
%42 = phi i32 [ %40, %39 ], [ -1, %16 ]
ret i32 %42
}