#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 */
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int FD_SETSIZE;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/);
void loop(fd_set *read_fd_set, void (*handler)(int)) {
  struct sockaddr_in client;
  socklen_t client_len;
  if (select(FD_SETSIZE, read_fd_set, 0, 0, 0) == -1) {
    perror("select");
    exit(EXIT_FAILURE);
  }
  for (int i = 0; i < FD_SETSIZE; ++i) {
    if (FD_ISSET(i, read_fd_set)) {
      int newfd;
      newfd = accept(i, (struct sockaddr *)&client, &client_len);
      handler(newfd);
    }
  }
}


x86 assembly (clang, -O0):
<loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
xor    %r8d,%r8d
mov    %r8d,%edx
mov    %r8d,%ecx
call   1040 <select@plt>
cmp    $0xffffffff,%eax
jne    119e <loop+0x4e>
lea    0xe75(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%eax
jge    11fc <loop+0xac>
mov    -0x20(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11e9 <loop+0x99>
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%edi
call   *%rax
jmp    11ee <loop+0x9e>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11a5 <loop+0x55>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e78(%rip),%r13
mov    0x0(%r13),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1040 <select@plt>
cmp    $0xffffffff,%eax
je     11db <loop+0x8b>
cmpl   $0x0,0x0(%r13)
jle    11cc <loop+0x7c>
xor    %ebp,%ebp
mov    %rsp,%r15
lea    0x4(%rsp),%r12
jmp    11a9 <loop+0x59>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%ebp
cmp    0x0(%r13),%ebp
jge    11cc <loop+0x7c>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11a0 <loop+0x50>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <accept@plt>
mov    %eax,%edi
call   *%r14
jmp    11a0 <loop+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1e(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@FD_SETSIZE = global i32 0
@.str = [7 x i8] c"select\00"
@EXIT_FAILURE = global i32 0
define void @loop(i32* %0, void (i32)* %1) {
%3 = alloca i32*
%4 = alloca void (i32)*
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %3
store void (i32)* %1, void (i32)** %4
%9 = load i32, i32* @FD_SETSIZE
%10 = load i32*, i32** %3
%11 = call i32 @select(i32 %9, i32* %10, i32 0, i32 0, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
store i32 0, i32* %7
br label %18
18: ; preds = %34, %17
%19 = load i32, i32* %7
%20 = load i32, i32* @FD_SETSIZE
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %37
22: ; preds = %18
%23 = load i32, i32* %7
%24 = load i32*, i32** %3
%25 = call i64 @FD_ISSET(i32 %23, i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %22
%28 = load i32, i32* %7
%29 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%30 = call i32 @accept(i32 %28, %struct.sockaddr* %29, i32* %6)
store i32 %30, i32* %8
%31 = load void (i32)*, void (i32)** %4
%32 = load i32, i32* %8
br label %33
33: ; preds = %27, %22
br label %34
34: ; preds = %33
%35 = load i32, i32* %7
%36 = add nsw i32 %35, 1
store i32 %36, i32* %7
br label %18
37: ; preds = %18
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@FD_SETSIZE = global i32 0
@.str = [7 x i8] c"select\00"
@EXIT_FAILURE = global i32 0
define void @loop(i32* %0, void (i32)* nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* @FD_SETSIZE
%8 = tail call i32 @select(i32 %7, i32* %0, i32 0, i32 0, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %15, label %10
10: ; preds = %2
%11 = load i32, i32* @FD_SETSIZE
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %20
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %26, %10
ret void
20: ; preds = %13, %26
%21 = phi i32 [ 0, %13 ], [ %27, %26 ]
%22 = call i64 @FD_ISSET(i32 %21, i32* %0)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = call i32 @accept(i32 %21, %struct.sockaddr* %14, i32* %4)
br label %26
26: ; preds = %20, %24
%27 = add nuw nsw i32 %21, 1
%28 = load i32, i32* @FD_SETSIZE
%29 = icmp slt i32 %27, %28
br i1 %29, label %20, label %19
}