#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;

/* Variables and functions */
int /*<<< orphan*/ FD_CLR(int, int /*<<< orphan*/ *);
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ handle_recv_msg(int, char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
void handle_connection(int sockfd) {
  char sendline[1024], recvline[1024];
  int maxfdp, stdineof;
  fd_set readfds;
  int n;
  struct timeval tv;
  int retval = 0;
  while (1) {
    FD_ZERO(&readfds);
    FD_SET(sockfd, &readfds);
    maxfdp = sockfd;
    tv.tv_sec = 5;
    tv.tv_usec = 0;
    retval = select(maxfdp + 1, &readfds, NULL, NULL, &tv);
    if (retval == -1) {
      return;
    }
    if (retval == 0) {
      printf("client timeout.\n");
      continue;
    }
    if (FD_ISSET(sockfd, &readfds)) {
      n = read(sockfd, recvline, 1024);
      if (n <= 0) {
        fprintf(stderr, "client: server is closed.\n");
        close(sockfd);
        FD_CLR(sockfd, &readfds);
        return;
      }
      handle_recv_msg(sockfd, recvline);
    }
  }
}


x86 assembly (clang, -O0):
<handle_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x834(%rbp)
lea    -0x81c(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x81c(%rbp),%rsi
call   10c0 <FD_SET@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x814(%rbp)
movl   $0x5,-0x830(%rbp)
movq   $0x0,-0x828(%rbp)
mov    -0x814(%rbp),%edi
add    $0x1,%edi
lea    -0x81c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x830(%rbp),%r8
mov    %rcx,%rdx
call   1090 <select@plt>
mov    %eax,-0x834(%rbp)
cmpl   $0xffffffff,-0x834(%rbp)
jne    122c <handle_connection+0x8c>
jmp    12cf <handle_connection+0x12f>
cmpl   $0x0,-0x834(%rbp)
jne    124a <handle_connection+0xaa>
lea    0xdc0(%rip),%rdi
call   1040 <printf@plt>
jmp    11b8 <handle_connection+0x18>
mov    -0x4(%rbp),%edi
lea    -0x81c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12ca <handle_connection+0x12a>
mov    -0x4(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x820(%rbp)
cmpl   $0x0,-0x820(%rbp)
jg     12bb <handle_connection+0x11b>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xd77(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%edi
lea    -0x81c(%rbp),%rsi
call   10a0 <FD_CLR@plt>
jmp    12cf <handle_connection+0x12f>
mov    -0x4(%rbp),%edi
lea    -0x810(%rbp),%rsi
call   10b0 <handle_recv_msg@plt>
jmp    11b8 <handle_connection+0x18>
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_connection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %edi,%ebx
lea    0x1(%rbx),%r14d
lea    0xc(%rsp),%rbp
lea    0x10(%rsp),%r15
lea    0xe53(%rip),%r12
lea    0x20(%rsp),%r13
jmp    11d8 <handle_connection+0x38>
nop
mov    %r12,%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10c0 <FD_SET@plt>
movl   $0x5,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1090 <select@plt>
test   %eax,%eax
je     11d0 <handle_connection+0x30>
cmp    $0xffffffff,%eax
je     126c <handle_connection+0xcc>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11d8 <handle_connection+0x38>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1244 <handle_connection+0xa4>
mov    %ebx,%edi
mov    %r13,%rsi
call   10b0 <handle_recv_msg@plt>
jmp    11d8 <handle_connection+0x38>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    0xdac(%rip),%rsi
call   1070 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   10a0 <FD_CLR@plt>
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [17 x i8] c"client timeout.\0A\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"client: server is closed.\0A\00"
define void @handle_connection(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %10
br label %11
11: ; preds = %1, %27, %50
%12 = call i32 @FD_ZERO(i32* %7)
%13 = load i32, i32* %2
%14 = call i32 @FD_SET(i32 %13, i32* %7)
%15 = load i32, i32* %2
store i32 %15, i32* %5
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 5, i32* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %17
%18 = load i32, i32* %5
%19 = add nsw i32 %18, 1
%20 = call i32 @select(i32 %19, i32* %7, i32* null, i32* null, %struct.timeval* %9)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %11
br label %51
24: ; preds = %11
%25 = load i32, i32* %10
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %11
29: ; preds = %24
%30 = load i32, i32* %2
%31 = call i64 @FD_ISSET(i32 %30, i32* %7)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %50
33: ; preds = %29
%34 = load i32, i32* %2
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 1024)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp sle i32 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %33
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%42 = load i32, i32* %2
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %2
%45 = call i32 @FD_CLR(i32 %44, i32* %7)
br label %51
46: ; preds = %33
%47 = load i32, i32* %2
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%49 = call i32 @handle_recv_msg(i32 %47, i8* %48)
br label %50
50: ; preds = %46, %29
br label %11
51: ; preds = %39, %23
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@stderr = global i32 0
@.str.1 = [27 x i8] c"client: server is closed.\0A\00"
@str = [16 x i8] c"client timeout.\00"
define void @handle_connection(i32 %0) {
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%10 = add nsw i32 %0, 1
br label %11
11: ; preds = %20, %1
%12 = call i32 @FD_ZERO(i32* %3)
%13 = call i32 @FD_SET(i32 %0, i32* %3)
store i32 5, i32* %8
store i64 0, i64* %9
%14 = call i32 @select(i32 %10, i32* %3, i32* null, i32* null, %struct.timeval* %4)
switch i32 %14, label %17 [
i32 -1, label %31
i32 0, label %15
]
15: ; preds = %11
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %20
17: ; preds = %11
%18 = call i64 @FD_ISSET(i32 %0, i32* %3)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17, %29, %15
br label %11
21: ; preds = %17
%22 = call i32 @read(i32 %0, i8* %5, i32 1024)
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @close(i32 %0)
%28 = call i32 @FD_CLR(i32 %0, i32* %3)
br label %31
29: ; preds = %21
%30 = call i32 @handle_recv_msg(i32 %0, i8* %5)
br label %20
31: ; preds = %11, %24
ret void
}