#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_DEBUG;
int errno;
int /*<<< orphan*/ getpid();
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/, ...);
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_DEBUG,
           "Client(%d) "
           "Calling select...",
           getpid());
    result = select(max + 1, &readset, NULL, NULL, NULL);
    theError = errno;
    syslog(LOG_DEBUG,
           "Client(%d) "
           "select returned %d, error = %s",
           getpid(), 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    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x24(%rbp),%rdi
call   1050 <FD_ZERO@plt>
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ea <iTermSelect+0x7a>
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x28(%rbp),%eax
jle    11c8 <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   1090 <FD_SET@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    119a <iTermSelect+0x2a>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x44(%rbp),%edi
mov    %eax,%edx
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1070 <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   1060 <select@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%ecx
mov    %eax,%r8d
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
xor    %eax,%eax
cmpl   $0xffffffff,-0x1c(%rbp)
mov    %al,-0x45(%rbp)
jne    1298 <iTermSelect+0x128>
mov    -0x20(%rbp),%eax
mov    0x2d48(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x45(%rbp)
mov    -0x45(%rbp),%al
test   $0x1,%al
jne    1183 <iTermSelect+0x13>
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    130c <iTermSelect+0x19c>
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x24(%rbp),%rsi
call   1040 <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     12f9 <iTermSelect+0x189>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    12fe <iTermSelect+0x18e>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    12b1 <iTermSelect+0x141>
mov    -0x30(%rbp),%eax
add    $0x50,%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,0x4(%rsp)
mov    %esi,%r13d
lea    0xc(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
call   1050 <FD_ZERO@plt>
mov    $0x0,%r14d
cmpl   $0x0,0x4(%rsp)
jle    11db <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   1090 <FD_SET@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    11c0 <iTermSelect+0x50>
mov    0x2dee(%rip),%r15
mov    (%r15),%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %ebp,%edi
lea    0xe0b(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <syslog@plt>
add    $0x1,%r14d
mov    %r14d,%edi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <select@plt>
mov    %eax,%r14d
mov    0x2dc2(%rip),%rax
mov    (%rax),%ebp
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r15d
mov    %ebp,%edi
call   1080 <strerror@plt>
mov    0x8(%rsp),%edi
lea    0xdda(%rip),%rsi
mov    %r15d,%edx
mov    %r14d,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1070 <syslog@plt>
cmp    $0xffffffff,%r14d
jne    1268 <iTermSelect+0xf8>
mov    0x2d78(%rip),%rax
cmp    (%rax),%ebp
je     11a0 <iTermSelect+0x30>
cmpl   $0x0,0x4(%rsp)
jle    12a1 <iTermSelect+0x131>
xor    %ebp,%ebp
lea    0xc(%rsp),%r14
xor    %r15d,%r15d
mov    0x10(%rsp),%r12
xchg   %ax,%ax
mov    (%rbx,%rbp,4),%edi
mov    %r14,%rsi
call   1040 <FD_ISSET@plt>
mov    %eax,(%r12,%rbp,4)
cmp    $0x1,%eax
sbb    $0xffffffff,%r15d
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1280 <iTermSelect+0x110>
jmp    12a4 <iTermSelect+0x134>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@LOG_DEBUG = global i32 0
@.str = [29 x i8] c"Client(%d) Calling select...\00"
@errno = global i32 0
@.str.1 = [42 x i8] c"Client(%d) 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 = %65, %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_DEBUG
%46 = call i32 (...) @getpid()
%47 = call i32 (i32, i8*, i32, ...) @syslog(i32 %45, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %46)
%48 = load i32, i32* %10
%49 = add nsw i32 %48, 1
%50 = call i32 @select(i32 %49, i32* %9, i32* null, i32* null, i32* null)
store i32 %50, i32* %7
%51 = load i32, i32* @errno
store i32 %51, i32* %8
%52 = load i32, i32* @LOG_DEBUG
%53 = call i32 (...) @getpid()
%54 = load i32, i32* %7
%55 = load i32, i32* %8
%56 = call i32 @strerror(i32 %55)
%57 = call i32 (i32, i8*, i32, ...) @syslog(i32 %52, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %53, i32 %54, i32 %56)
br label %58
58: ; preds = %44
%59 = load i32, i32* %7
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %65
61: ; preds = %58
%62 = load i32, i32* %8
%63 = load i32, i32* @EINTR
%64 = icmp eq i32 %62, %63
br label %65
65: ; preds = %61, %58
%66 = phi i1 [ false, %58 ], [ %64, %61 ]
br i1 %66, label %14, label %67
67: ; preds = %65
store i32 0, i32* %12
store i32 0, i32* %13
br label %68
68: ; preds = %93, %67
%69 = load i32, i32* %13
%70 = load i32, i32* %5
%71 = icmp slt i32 %69, %70
br i1 %71, label %72, label %96
72: ; preds = %68
%73 = load i32*, i32** %4
%74 = load i32, i32* %13
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i32, i32* %73, i64 %75
%77 = load i32, i32* %76
%78 = call i32 @FD_ISSET(i32 %77, i32* %9)
%79 = load i32*, i32** %6
%80 = load i32, i32* %13
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i32, i32* %79, i64 %81
store i32 %78, i32* %82
%83 = load i32*, i32** %6
%84 = load i32, i32* %13
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i32, i32* %83, i64 %85
%87 = load i32, i32* %86
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %92
89: ; preds = %72
%90 = load i32, i32* %12
%91 = add nsw i32 %90, 1
store i32 %91, i32* %12
br label %92
92: ; preds = %89, %72
br label %93
93: ; preds = %92
%94 = load i32, i32* %13
%95 = add nsw i32 %94, 1
store i32 %95, i32* %13
br label %68
96: ; preds = %68
%97 = load i32, i32* %12
ret i32 %97
}

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