#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__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ assert(int);
int count_fds(int nfds, fd_set *fds) {
  int i, result = 0;
  assert(fds != NULL && nfds > 0);
  for (i = 0; i < nfds; i++) {
    if (FD_ISSET(i, fds))
      result++;
  }
  return result;
}


x86 assembly (clang, -O0):
<count_fds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x19(%rbp)
je     1150 <count_fds+0x30>
cmpl   $0x0,-0x4(%rbp)
setg   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11a2 <count_fds+0x82>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     118f <count_fds+0x6f>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1194 <count_fds+0x74>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1164 <count_fds+0x44>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_fds>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
test   %rsi,%rsi
setne  %al
test   %edi,%edi
setg   %cl
and    %al,%cl
movzbl %cl,%edi
call   1040 <assert@plt>
xor    %ebx,%ebx
test   %r15d,%r15d
jle    1169 <count_fds+0x49>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebx
add    $0x1,%ebp
cmp    %ebp,%r15d
jne    1150 <count_fds+0x30>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @count_fds(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
store i32 0, i32* %6
%7 = load i32*, i32** %4
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* %3
%11 = icmp sgt i32 %10, 0
br label %12
12: ; preds = %9, %2
%13 = phi i1 [ false, %2 ], [ %11, %9 ]
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
store i32 0, i32* %5
br label %16
16: ; preds = %29, %12
%17 = load i32, i32* %5
%18 = load i32, i32* %3
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %32
20: ; preds = %16
%21 = load i32, i32* %5
%22 = load i32*, i32** %4
%23 = call i64 @FD_ISSET(i32 %21, i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i32, i32* %6
%27 = add nsw i32 %26, 1
store i32 %27, i32* %6
br label %28
28: ; preds = %25, %20
br label %29
29: ; preds = %28
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 1
store i32 %31, i32* %5
br label %16
32: ; preds = %16
%33 = load i32, i32* %6
ret i32 %33
}

LLVM IR (-O2):
define i32 @count_fds(i32 %0, i32* %1) {
%3 = icmp ne i32* %1, null
%4 = icmp sgt i32 %0, 0
%5 = and i1 %4, %3
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
br i1 %4, label %8, label %17
8: ; preds = %2, %8
%9 = phi i32 [ %14, %8 ], [ 0, %2 ]
%10 = phi i32 [ %15, %8 ], [ 0, %2 ]
%11 = tail call i64 @FD_ISSET(i32 %10, i32* %1)
%12 = icmp ne i64 %11, 0
%13 = zext i1 %12 to i32
%14 = add nuw nsw i32 %9, %13
%15 = add nuw nsw i32 %10, 1
%16 = icmp eq i32 %15, %0
br i1 %16, label %17, label %8
17: ; preds = %8, %2
%18 = phi i32 [ 0, %2 ], [ %14, %8 ]
ret i32 %18
}