#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
int recv(int, char *, int, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
void connection_handler(int connfd) {
  char *hello = "hello";
  char buf[1024];
  int n, ret;
  struct timeval to;
  fd_set rfds;
  to.tv_sec = 5;
  to.tv_usec = 0;
  FD_ZERO(&rfds);
  FD_SET(connfd, &rfds);
  while (ret = select(connfd + 1, &rfds, NULL, NULL, &to)) {
    if (ret == -1) {
      perror("select");
      exit(1);
    } else if (ret == 0) {
      write(connfd, hello, strlen(hello) + 1);
    } else {
      if (FD_ISSET(connfd, &rfds)) {
        n = recv(connfd, buf, sizeof(buf), 0);
        if (n == 0)
          return;
        printf("recv %d bytes, '%s'\n", n, buf);
      }
    }
    to.tv_sec = 5;
    to.tv_usec = 0;
  }
}


x86 assembly (clang, -O0):
<connection_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe4b(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x5,-0x428(%rbp)
movq   $0x0,-0x420(%rbp)
lea    -0x42c(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x42c(%rbp),%rsi
call   10c0 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x42c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x428(%rbp),%r8
mov    %rcx,%rdx
call   1090 <select@plt>
mov    %eax,-0x418(%rbp)
cmp    $0x0,%eax
je     1307 <connection_handler+0x167>
cmpl   $0xffffffff,-0x418(%rbp)
jne    123b <connection_handler+0x9b>
lea    0xdda(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x418(%rbp)
jne    1283 <connection_handler+0xe3>
mov    -0x4(%rbp),%eax
mov    %eax,-0x43c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1040 <write@plt>
jmp    12e8 <connection_handler+0x148>
mov    -0x4(%rbp),%edi
lea    -0x42c(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12e3 <connection_handler+0x143>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jne    12ca <connection_handler+0x12a>
jmp    1307 <connection_handler+0x167>
mov    -0x414(%rbp),%esi
lea    -0x410(%rbp),%rdx
lea    0xd2f(%rip),%rdi
call   1070 <printf@plt>
jmp    12e8 <connection_handler+0x148>
jmp    12ed <connection_handler+0x14d>
movl   $0x5,-0x428(%rbp)
movq   $0x0,-0x420(%rbp)
jmp    11e9 <connection_handler+0x49>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connection_handler>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %edi,%ebx
movl   $0x5,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x1c(%rsp),%r14
mov    %r14,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   10a0 <FD_SET@plt>
lea    0x1(%rbx),%ebp
lea    0x8(%rsp),%r15
lea    0x20(%rsp),%r12
lea    0xe38(%rip),%r13
jmp    11f1 <connection_handler+0x71>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movl   $0x5,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1070 <select@plt>
test   %eax,%eax
je     123e <connection_handler+0xbe>
cmp    $0xffffffff,%eax
je     1250 <connection_handler+0xd0>
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11e0 <connection_handler+0x60>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
je     123e <connection_handler+0xbe>
mov    %r13,%rdi
mov    %eax,%esi
mov    %r12,%rdx
call   1050 <printf@plt>
jmp    11e0 <connection_handler+0x60>
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda9(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [6 x i8] c"hello\00"
@.str.1 = [7 x i8] c"select\00"
@.str.2 = [21 x i8] c"recv %d bytes, '%s'\0A\00"
define void @connection_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca i32
store i32 %0, i32* %2
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %3
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i32 5, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i64 0, i64* %10
%11 = call i32 @FD_ZERO(i32* %8)
%12 = load i32, i32* %2
%13 = call i32 @FD_SET(i32 %12, i32* %8)
br label %14
14: ; preds = %52, %1
%15 = load i32, i32* %2
%16 = add nsw i32 %15, 1
%17 = call i32 @select(i32 %16, i32* %8, i32* null, i32* null, %struct.timeval* %7)
store i32 %17, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %55
19: ; preds = %14
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %19
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %25
%29 = load i32, i32* %2
%30 = load i8*, i8** %3
%31 = load i8*, i8** %3
%32 = call i64 @strlen(i8* %31)
%33 = add nsw i64 %32, 1
%34 = call i32 @write(i32 %29, i8* %30, i64 %33)
br label %51
35: ; preds = %25
%36 = load i32, i32* %2
%37 = call i64 @FD_ISSET(i32 %36, i32* %8)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %50
39: ; preds = %35
%40 = load i32, i32* %2
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%42 = call i32 @recv(i32 %40, i8* %41, i32 1024, i32 0)
store i32 %42, i32* %5
%43 = load i32, i32* %5
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %39
br label %55
46: ; preds = %39
%47 = load i32, i32* %5
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%49 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %47, i8* %48)
br label %50
50: ; preds = %46, %35
br label %51
51: ; preds = %50, %28
br label %52
52: ; preds = %51
%53 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i32 5, i32* %53
%54 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i64 0, i64* %54
br label %14
55: ; preds = %45, %14
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str.1 = [7 x i8] c"select\00"
@.str.2 = [21 x i8] c"recv %d bytes, '%s'\0A\00"
define void @connection_handler(i32 %0) {
%2 = alloca [1024 x i8]
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.timeval* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 5, i32* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i64 0, i64* %9
%10 = call i32 @FD_ZERO(i32* %4)
%11 = call i32 @FD_SET(i32 %0, i32* %4)
%12 = add nsw i32 %0, 1
br label %13
13: ; preds = %26, %1
%14 = call i32 @select(i32 %12, i32* %4, i32* null, i32* null, %struct.timeval* %3)
switch i32 %14, label %18 [
i32 0, label %27
i32 -1, label %15
]
15: ; preds = %13
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %13
%19 = call i64 @FD_ISSET(i32 %0, i32* %4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %18
%22 = call i32 @recv(i32 %0, i8* %5, i32 1024, i32 0)
%23 = icmp eq i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %22, i8* %5)
br label %26
26: ; preds = %24, %18
store i32 5, i32* %8
store i64 0, i64* %9
br label %13
27: ; preds = %13, %21
ret void
}