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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
uint8_t wait_readable_or_stop(int read_fd, int stop_fd) {
  uint8_t done = 0;
  int nfds = 0;
  uint8_t events = 0;
  fd_set read_fds;
  assert(read_fd >= 0);
  assert(stop_fd >= 0);
  nfds = (((read_fd) > (stop_fd)) ? (read_fd) : (stop_fd)) + 1;
  FD_ZERO(&read_fds);
  FD_SET(read_fd, &read_fds);
  FD_SET(stop_fd, &read_fds);
  do {
    if (-1 != select(nfds, &read_fds, NULL, NULL, NULL)) {
      if (FD_ISSET(read_fd, &read_fds)) {
        events |= 1;
      }
      if (FD_ISSET(stop_fd, &read_fds)) {
        events |= 2;
      }
      done = 1;
    } else {
      if (EINTR != errno) {
        perror("select()");
        assert(0);
      }
    }
  } while (!done);
  return events;
}


x86 assembly (clang, -O0):
<wait_readable_or_stop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x4(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpl   $0x0,-0x8(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    11bc <wait_readable_or_stop+0x5c>
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11c2 <wait_readable_or_stop+0x62>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x10(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1060 <select@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     125c <wait_readable_or_stop+0xfc>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1231 <wait_readable_or_stop+0xd1>
mov    -0x14(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1250 <wait_readable_or_stop+0xf0>
mov    -0x14(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0x14(%rbp)
movl   $0x1,-0xc(%rbp)
jmp    128e <wait_readable_or_stop+0x12e>
mov    0x2d75(%rip),%rax
mov    (%rax),%rax
mov    0x2d73(%rip),%rcx
cmp    (%rcx),%rax
je     1289 <wait_readable_or_stop+0x129>
lea    0xd83(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1040 <assert@plt>
jmp    128e <wait_readable_or_stop+0x12e>
jmp    1293 <wait_readable_or_stop+0x133>
cmpl   $0x0,-0xc(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ec <wait_readable_or_stop+0x8c>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_readable_or_stop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r15d
not    %edi
shr    $0x1f,%edi
call   1040 <assert@plt>
mov    %r14d,%edi
not    %edi
shr    $0x1f,%edi
call   1040 <assert@plt>
cmp    %r14d,%r15d
mov    %r14d,%ebx
cmovg  %r15d,%ebx
add    $0x1,%ebx
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <select@plt>
cmp    $0xffffffff,%eax
je     120c <wait_readable_or_stop+0xac>
lea    0x4(%rsp),%rbx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1030 <FD_ISSET@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    %r14d,%edi
mov    %rbx,%rsi
call   1030 <FD_ISSET@plt>
lea    0x2(%rbp),%ecx
test   %rax,%rax
cmove  %ebp,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dc5(%rip),%r13
mov    0x2dc6(%rip),%r12
lea    0x4(%rsp),%rbp
jmp    1246 <wait_readable_or_stop+0xe6>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <select@plt>
cmp    $0xffffffff,%eax
jne    11ce <wait_readable_or_stop+0x6e>
mov    0x0(%r13),%rax
cmp    (%r12),%rax
je     1230 <wait_readable_or_stop+0xd0>
lea    0xda9(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
call   1040 <assert@plt>
jmp    1230 <wait_readable_or_stop+0xd0>

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

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