#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 */
int EINTR;
int FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ LOG_NOTICE;
int errno;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int iTermSelect(int *fds, int count, int *results) {
  int result;
  int theError;
  fd_set readset;
  do {
    FD_ZERO(&readset);
    int max = 0;
    for (int i = 0; i < count; i++) {
      if (fds[i] > max) {
        max = fds[i];
      }
      FD_SET(fds[i], &readset);
    }
    syslog(LOG_NOTICE, "Calling select...");
    result = select(max + 1, &readset, NULL, NULL, NULL);
    theError = errno;
    syslog(LOG_NOTICE, "select returned %d, error = %s", result,
           strerror(theError));
  } while (result == -1 && theError == EINTR);
  int n = 0;
  for (int i = 0; i < count; i++) {
    results[i] = FD_ISSET(fds[i], &readset);
    if (results[i]) {
      n++;
    }
  }
  return n;
}


x86 assembly (clang, -O0):
<iTermSelect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x24(%rbp),%rdi
call   1040 <FD_ZERO@plt>
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11da <iTermSelect+0x7a>
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x28(%rbp),%eax
jle    11b8 <iTermSelect+0x58>
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x24(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    118a <iTermSelect+0x2a>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    -0x28(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1050 <select@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%edi
call   1070 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%edx
mov    %eax,%ecx
lea    0xdcf(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
xor    %eax,%eax
cmpl   $0xffffffff,-0x1c(%rbp)
mov    %al,-0x3d(%rbp)
jne    126b <iTermSelect+0x10b>
mov    -0x20(%rbp),%eax
mov    0x2d75(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    1173 <iTermSelect+0x13>
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12df <iTermSelect+0x17f>
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x24(%rbp),%rsi
call   1030 <FD_ISSET@plt>
mov    %eax,%edx
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
je     12cc <iTermSelect+0x16c>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    12d1 <iTermSelect+0x171>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1284 <iTermSelect+0x124>
mov    -0x30(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iTermSelect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,0x10(%rsp)
mov    %rdi,%rbx
mov    %esi,0x8(%rsp)
mov    %esi,%r15d
lea    0xc(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
mov    $0x0,%r14d
cmpl   $0x0,0x8(%rsp)
jle    11cb <iTermSelect+0x6b>
xor    %ebp,%ebp
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    (%rbx,%rbp,4),%edi
cmp    %r14d,%edi
cmovg  %edi,%r14d
mov    %r12,%rsi
call   1080 <FD_SET@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    11b0 <iTermSelect+0x50>
mov    0x2dfe(%rip),%r13
mov    0x0(%r13),%edi
lea    0xe23(%rip),%rsi
xor    %eax,%eax
call   1060 <syslog@plt>
add    $0x1,%r14d
mov    %r14d,%edi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <select@plt>
mov    %eax,%r14d
mov    0x2ddc(%rip),%rax
mov    (%rax),%ebp
mov    0x0(%r13),%r13d
mov    %ebp,%edi
call   1070 <strerror@plt>
mov    %r13d,%edi
lea    0xdf7(%rip),%rsi
mov    %r14d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <syslog@plt>
cmp    $0xffffffff,%r14d
jne    123c <iTermSelect+0xdc>
mov    0x2da4(%rip),%rax
cmp    (%rax),%ebp
je     1190 <iTermSelect+0x30>
cmpl   $0x0,0x8(%rsp)
jle    1282 <iTermSelect+0x122>
xor    %ebp,%ebp
lea    0xc(%rsp),%r14
xor    %r12d,%r12d
mov    0x10(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rbx,%rbp,4),%edi
mov    %r14,%rsi
call   1030 <FD_ISSET@plt>
mov    %eax,0x0(%r13,%rbp,4)
cmp    $0x1,%eax
sbb    $0xffffffff,%r12d
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1260 <iTermSelect+0x100>
jmp    1285 <iTermSelect+0x125>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@LOG_NOTICE = global i32 0
@.str = [18 x i8] c"Calling select...\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"select returned %d, error = %s\00"
@EINTR = global i32 0
define i32 @iTermSelect(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
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
br label %14
14: ; preds = %63, %3
%15 = call i32 @FD_ZERO(i32* %9)
store i32 0, i32* %10
store i32 0, i32* %11
br label %16
16: ; preds = %41, %14
%17 = load i32, i32* %11
%18 = load i32, i32* %5
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %44
20: ; preds = %16
%21 = load i32*, i32** %4
%22 = load i32, i32* %11
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32, i32* %24
%26 = load i32, i32* %10
%27 = icmp sgt i32 %25, %26
br i1 %27, label %28, label %34
28: ; preds = %20
%29 = load i32*, i32** %4
%30 = load i32, i32* %11
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
%33 = load i32, i32* %32
store i32 %33, i32* %10
br label %34
34: ; preds = %28, %20
%35 = load i32*, i32** %4
%36 = load i32, i32* %11
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
%39 = load i32, i32* %38
%40 = call i32 @FD_SET(i32 %39, i32* %9)
br label %41
41: ; preds = %34
%42 = load i32, i32* %11
%43 = add nsw i32 %42, 1
store i32 %43, i32* %11
br label %16
44: ; preds = %16
%45 = load i32, i32* @LOG_NOTICE
%46 = call i32 (i32, i8*, ...) @syslog(i32 %45, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%47 = load i32, i32* %10
%48 = add nsw i32 %47, 1
%49 = call i32 @select(i32 %48, i32* %9, i32* null, i32* null, i32* null)
store i32 %49, i32* %7
%50 = load i32, i32* @errno
store i32 %50, i32* %8
%51 = load i32, i32* @LOG_NOTICE
%52 = load i32, i32* %7
%53 = load i32, i32* %8
%54 = call i32 @strerror(i32 %53)
%55 = call i32 (i32, i8*, ...) @syslog(i32 %51, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %52, i32 %54)
br label %56
56: ; preds = %44
%57 = load i32, i32* %7
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %63
59: ; preds = %56
%60 = load i32, i32* %8
%61 = load i32, i32* @EINTR
%62 = icmp eq i32 %60, %61
br label %63
63: ; preds = %59, %56
%64 = phi i1 [ false, %56 ], [ %62, %59 ]
br i1 %64, label %14, label %65
65: ; preds = %63
store i32 0, i32* %12
store i32 0, i32* %13
br label %66
66: ; preds = %91, %65
%67 = load i32, i32* %13
%68 = load i32, i32* %5
%69 = icmp slt i32 %67, %68
br i1 %69, label %70, label %94
70: ; preds = %66
%71 = load i32*, i32** %4
%72 = load i32, i32* %13
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %71, i64 %73
%75 = load i32, i32* %74
%76 = call i32 @FD_ISSET(i32 %75, i32* %9)
%77 = load i32*, i32** %6
%78 = load i32, i32* %13
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i32, i32* %77, i64 %79
store i32 %76, i32* %80
%81 = load i32*, i32** %6
%82 = load i32, i32* %13
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i32, i32* %81, i64 %83
%85 = load i32, i32* %84
%86 = icmp ne i32 %85, 0
br i1 %86, label %87, label %90
87: ; preds = %70
%88 = load i32, i32* %12
%89 = add nsw i32 %88, 1
store i32 %89, i32* %12
br label %90
90: ; preds = %87, %70
br label %91
91: ; preds = %90
%92 = load i32, i32* %13
%93 = add nsw i32 %92, 1
store i32 %93, i32* %13
br label %66
94: ; preds = %66
%95 = load i32, i32* %12
ret i32 %95
}

LLVM IR (-O2):
@LOG_NOTICE = global i32 0
@.str = [18 x i8] c"Calling select...\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"select returned %d, error = %s\00"
@EINTR = global i32 0
define i32 @iTermSelect(i32* nocapture %0, i32 %1, i32* nocapture writeonly %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp sgt i32 %1, 0
%7 = zext i32 %1 to i64
br label %8
8: ; preds = %10, %3
%9 = call i32 @FD_ZERO(i32* %4)
br i1 %6, label %27, label %10
10: ; preds = %27, %8
%11 = phi i32 [ 0, %8 ], [ %33, %27 ]
%12 = load i32, i32* @LOG_NOTICE
%13 = call i32 (i32, i8*, ...) @syslog(i32 %12, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%14 = add nsw i32 %11, 1
%15 = call i32 @select(i32 %14, i32* %4, i32* null, i32* null, i32* null)
%16 = load i32, i32* @errno
%17 = load i32, i32* @LOG_NOTICE
%18 = call i32 @strerror(i32 %16)
%19 = call i32 (i32, i8*, ...) @syslog(i32 %17, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %18)
%20 = icmp eq i32 %15, -1
%21 = load i32, i32* @EINTR
%22 = icmp eq i32 %16, %21
%23 = select i1 %20, i1 %22, i1 false
br i1 %23, label %8, label %24
24: ; preds = %10
br i1 %6, label %25, label %37
25: ; preds = %24
%26 = zext i32 %1 to i64
br label %39
27: ; preds = %8, %27
%28 = phi i64 [ %35, %27 ], [ 0, %8 ]
%29 = phi i32 [ %33, %27 ], [ 0, %8 ]
%30 = getelementptr inbounds i32, i32* %0, i64 %28
%31 = load i32, i32* %30
%32 = icmp sgt i32 %31, %29
%33 = select i1 %32, i32 %31, i32 %29
%34 = call i32 @FD_SET(i32 %31, i32* %4)
%35 = add nuw nsw i64 %28, 1
%36 = icmp eq i64 %35, %7
br i1 %36, label %10, label %27
37: ; preds = %39, %24
%38 = phi i32 [ 0, %24 ], [ %48, %39 ]
ret i32 %38
39: ; preds = %25, %39
%40 = phi i64 [ 0, %25 ], [ %49, %39 ]
%41 = phi i32 [ 0, %25 ], [ %48, %39 ]
%42 = getelementptr inbounds i32, i32* %0, i64 %40
%43 = load i32, i32* %42
%44 = call i32 @FD_ISSET(i32 %43, i32* %4)
%45 = getelementptr inbounds i32, i32* %2, i64 %40
store i32 %44, i32* %45
%46 = icmp ne i32 %44, 0
%47 = zext i1 %46 to i32
%48 = add nuw nsw i32 %41, %47
%49 = add nuw nsw i64 %40, 1
%50 = icmp eq i64 %49, %26
br i1 %50, label %37, label %39
}