#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*/ fd_set;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int sock_mwait(int *socklist, int socklistlen, fd_set *fset) {
  int i;
  FD_ZERO(fset);
  int max = 0;
  for (i = 0; i < socklistlen; i++) {
    if (socklist[i] <= 0)
      continue;
    FD_SET(socklist[i], fset);
    if (socklist[i] > max)
      max = socklist[i];
  }
  int n;
  while ((n = select(max + 1, fset, NULL, NULL, NULL)) < 0) {
    if (errno != EINTR)
      return n;
  }
  return n;
}


x86 assembly (clang, -O0):
<sock_mwait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <FD_ZERO@plt>
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c6 <sock_mwait+0x96>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jg     117d <sock_mwait+0x4d>
jmp    11b8 <sock_mwait+0x88>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    -0x20(%rbp),%rsi
call   1050 <FD_SET@plt>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x28(%rbp),%eax
jle    11b3 <sock_mwait+0x83>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x28(%rbp)
jmp    11b8 <sock_mwait+0x88>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    115a <sock_mwait+0x2a>
jmp    11cb <sock_mwait+0x9b>
mov    -0x28(%rbp),%edi
add    $0x1,%edi
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1040 <select@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    121b <sock_mwait+0xeb>
mov    0x2de8(%rip),%rax
mov    (%rax),%rax
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%rax
je     1216 <sock_mwait+0xe6>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1221 <sock_mwait+0xf1>
jmp    11cb <sock_mwait+0x9b>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_mwait>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r12
mov    %rdx,%rdi
call   1030 <FD_ZERO@plt>
test   %ebp,%ebp
jle    1184 <sock_mwait+0x54>
mov    %ebp,%r15d
xor    %ebx,%ebx
xor    %ebp,%ebp
jmp    1169 <sock_mwait+0x39>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%rbx
cmp    %rbx,%r15
je     1186 <sock_mwait+0x56>
mov    (%r12,%rbx,4),%edi
test   %edi,%edi
jle    1160 <sock_mwait+0x30>
mov    %r14,%rsi
call   1050 <FD_SET@plt>
mov    (%r12,%rbx,4),%eax
cmp    %ebp,%eax
cmovg  %eax,%ebp
jmp    1160 <sock_mwait+0x30>
xor    %ebp,%ebp
add    $0x1,%ebp
mov    0x2e50(%rip),%r15
mov    0x2e41(%rip),%rbx
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1040 <select@plt>
test   %eax,%eax
jns    11bd <sock_mwait+0x8d>
mov    (%r15),%rcx
cmp    (%rbx),%rcx
je     11a0 <sock_mwait+0x70>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @sock_mwait(i32* nocapture %0, i32 %1, i32* %2) {
%4 = tail call i32 @FD_ZERO(i32* %2)
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = zext i32 %1 to i64
br label %11
8: ; preds = %22, %3
%9 = phi i32 [ 0, %3 ], [ %23, %22 ]
%10 = add nsw i32 %9, 1
br label %26
11: ; preds = %6, %22
%12 = phi i64 [ 0, %6 ], [ %24, %22 ]
%13 = phi i32 [ 0, %6 ], [ %23, %22 ]
%14 = getelementptr inbounds i32, i32* %0, i64 %12
%15 = load i32, i32* %14
%16 = icmp slt i32 %15, 1
br i1 %16, label %22, label %17
17: ; preds = %11
%18 = tail call i32 @FD_SET(i32 %15, i32* %2)
%19 = load i32, i32* %14
%20 = icmp sgt i32 %19, %13
%21 = select i1 %20, i32 %19, i32 %13
br label %22
22: ; preds = %17, %11
%23 = phi i32 [ %13, %11 ], [ %21, %17 ]
%24 = add nuw nsw i64 %12, 1
%25 = icmp eq i64 %24, %7
br i1 %25, label %8, label %11
26: ; preds = %8, %29
%27 = tail call i32 @select(i32 %10, i32* %2, i32* null, i32* null, i32* null)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %26, label %33
33: ; preds = %26, %29
ret i32 %27
}