#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 /*<<< 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 STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int read(int, char *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
void handle_connection(int sockfd) {
  char sendline[1024], recvline[1024];
  int maxfdp, stdineof;
  fd_set rset;
  int n;
  FD_ZERO(&rset);
  for (;;) {
    FD_SET(STDIN_FILENO, &rset);
    FD_SET(sockfd, &rset);
    maxfdp = (STDIN_FILENO > sockfd) ? STDIN_FILENO : sockfd;
    select(maxfdp + 1, &rset, NULL, NULL, NULL);
    if (FD_ISSET(sockfd, &rset)) {
      n = read(sockfd, recvline, 1024);
      if (n == 0) {
        fprintf(stderr, "client: server is closed.\n");
        close(sockfd);
        FD_CLR(sockfd, &rset);
      }
      write(STDOUT_FILENO, recvline, n);
    }
    if (FD_ISSET(STDIN_FILENO, &rset)) {
      n = read(STDIN_FILENO, sendline, 1024);
      if (n == 0) {
        FD_CLR(STDIN_FILENO, &rset);
        continue;
      }
      write(sockfd, sendline, n);
    }
  }
}


x86 assembly (clang, -O0):
<handle_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x81c(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    -0x81c(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0x4(%rbp),%edi
lea    -0x81c(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
cmp    -0x4(%rbp),%eax
jle    11f4 <handle_connection+0x64>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x824(%rbp)
jmp    11fd <handle_connection+0x6d>
mov    -0x4(%rbp),%eax
mov    %eax,-0x824(%rbp)
mov    -0x824(%rbp),%eax
mov    %eax,-0x814(%rbp)
mov    -0x814(%rbp),%edi
add    $0x1,%edi
lea    -0x81c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1090 <select@plt>
mov    -0x4(%rbp),%edi
lea    -0x81c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12b0 <handle_connection+0x120>
mov    -0x4(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x820(%rbp)
cmpl   $0x0,-0x820(%rbp)
jne    1295 <handle_connection+0x105>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    0xd87(%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>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    -0x810(%rbp),%rsi
mov    -0x820(%rbp),%edx
call   1030 <write@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
lea    -0x81c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     132b <handle_connection+0x19b>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x820(%rbp)
cmpl   $0x0,-0x820(%rbp)
jne    1316 <handle_connection+0x186>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
lea    -0x81c(%rbp),%rsi
call   10a0 <FD_CLR@plt>
jmp    11aa <handle_connection+0x1a>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x820(%rbp),%edx
call   1030 <write@plt>
jmp    11aa <handle_connection+0x1a>

x86 assembly (clang, -O2):
<handle_connection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %edi,%ebx
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1080 <FD_ZERO@plt>
mov    0x2e19(%rip),%r13
lea    0x410(%rsp),%r15
lea    0x10(%rsp),%r12
jmp    11dc <handle_connection+0x4c>
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x0(%r13),%edi
mov    %r14,%rsi
call   10b0 <FD_SET@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   10b0 <FD_SET@plt>
mov    0x0(%r13),%edi
cmp    %ebx,%edi
cmovle %ebx,%edi
add    $0x1,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <select@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     126a <handle_connection+0xda>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1257 <handle_connection+0xc7>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
call   1070 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   10a0 <FD_CLR@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    0x0(%r13),%edi
mov    %r14,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11dc <handle_connection+0x4c>
mov    0x0(%r13),%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    11d0 <handle_connection+0x40>
mov    0x0(%r13),%edi
mov    %r14,%rsi
call   10a0 <FD_CLR@plt>
jmp    11dc <handle_connection+0x4c>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"client: server is closed.\0A\00"
@STDOUT_FILENO = global i32 0
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
store i32 %0, i32* %2
%9 = call i32 @FD_ZERO(i32* %7)
br label %10
10: ; preds = %66, %58, %1
%11 = load i32, i32* @STDIN_FILENO
%12 = call i32 @FD_SET(i32 %11, i32* %7)
%13 = load i32, i32* %2
%14 = call i32 @FD_SET(i32 %13, i32* %7)
%15 = load i32, i32* @STDIN_FILENO
%16 = load i32, i32* %2
%17 = icmp sgt i32 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = load i32, i32* @STDIN_FILENO
br label %22
20: ; preds = %10
%21 = load i32, i32* %2
br label %22
22: ; preds = %20, %18
%23 = phi i32 [ %19, %18 ], [ %21, %20 ]
store i32 %23, i32* %5
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
%26 = call i32 @select(i32 %25, i32* %7, i32* null, i32* null, i32* null)
%27 = load i32, i32* %2
%28 = call i64 @FD_ISSET(i32 %27, i32* %7)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %48
30: ; preds = %22
%31 = load i32, i32* %2
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 1024)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %30
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %2
%42 = call i32 @FD_CLR(i32 %41, i32* %7)
br label %43
43: ; preds = %36, %30
%44 = load i32, i32* @STDOUT_FILENO
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%46 = load i32, i32* %8
%47 = call i32 @write(i32 %44, i8* %45, i32 %46)
br label %48
48: ; preds = %43, %22
%49 = load i32, i32* @STDIN_FILENO
%50 = call i64 @FD_ISSET(i32 %49, i32* %7)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %66
52: ; preds = %48
%53 = load i32, i32* @STDIN_FILENO
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%55 = call i32 @read(i32 %53, i8* %54, i32 1024)
store i32 %55, i32* %8
%56 = load i32, i32* %8
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %52
%59 = load i32, i32* @STDIN_FILENO
%60 = call i32 @FD_CLR(i32 %59, i32* %7)
br label %10
61: ; preds = %52
%62 = load i32, i32* %2
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%64 = load i32, i32* %8
%65 = call i32 @write(i32 %62, i8* %63, i32 %64)
br label %66
66: ; preds = %61, %48
br label %10
}

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