#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*/ sendbuf;
typedef int /*<<< orphan*/ recvbuf;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHUT_WR;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int readline(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ writen(int, char *, int /*<<< orphan*/);
void echo_cli(int sock) {
  fd_set rset;
  FD_ZERO(&rset);
  int nready;
  int fd_stdin = fileno(stdin);
  int maxfd;
  maxfd = fd_stdin > sock ? fd_stdin : sock;
  char sendbuf[1024] = {0};
  char recvbuf[1024] = {0};
  int stdineof = 0;
  while (1) {
    if (!stdineof) {
      FD_SET(fd_stdin, &rset);
    }
    FD_SET(sock, &rset);
    nready = select(maxfd + 1, &rset, NULL, NULL, NULL);
    if (nready == -1) {
      do {
        perror("select");
        exit(EXIT_FAILURE);
      } while (0);
    } else if (nready == 0) {
      continue;
    }
    if (FD_ISSET(fd_stdin, &rset)) {
      if (fgets(sendbuf, sizeof(sendbuf), stdin)) {
        writen(sock, sendbuf, strlen(sendbuf));
        memset(sendbuf, 0, sizeof(sendbuf));
      } else {
        shutdown(sock, SHUT_WR);
        stdineof = 1;
      }
    }
    if (FD_ISSET(sock, &rset)) {
      int ret = readline(sock, recvbuf, sizeof(recvbuf));
      if (ret == 0) {
        printf("server close\n");
        break;
      } else if (ret < 0) {
        do {
          perror("readline");
          exit(EXIT_FAILURE);
        } while (0);
      }
      fputs(recvbuf, stdout);
      memset(recvbuf, 0, sizeof(recvbuf));
    }
  }
}


x86 assembly (clang, -O0):
<echo_cli>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   10c0 <FD_ZERO@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x4(%rbp),%eax
jle    1232 <echo_cli+0x42>
mov    -0x10(%rbp),%eax
mov    %eax,-0x82c(%rbp)
jmp    123b <echo_cli+0x4b>
mov    -0x4(%rbp),%eax
mov    %eax,-0x82c(%rbp)
mov    -0x82c(%rbp),%eax
mov    %eax,-0x14(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
movl   $0x0,-0x824(%rbp)
cmpl   $0x0,-0x824(%rbp)
jne    128d <echo_cli+0x9d>
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10d0 <select@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    12df <echo_cli+0xef>
jmp    12c5 <echo_cli+0xd5>
lea    0xd34(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2cf8(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
jne    12ee <echo_cli+0xfe>
jmp    1274 <echo_cli+0x84>
jmp    12f3 <echo_cli+0x103>
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     139c <echo_cli+0x1ac>
lea    -0x420(%rbp),%rdi
mov    0x2cc9(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   10a0 <fgets@plt>
cmp    $0x0,%rax
je     137c <echo_cli+0x18c>
mov    -0x4(%rbp),%eax
mov    %eax,-0x83c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x838(%rbp)
lea    -0x420(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x83c(%rbp),%edi
mov    -0x838(%rbp),%rsi
mov    %eax,%edx
call   10f0 <writen@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
jmp    1397 <echo_cli+0x1a7>
mov    -0x4(%rbp),%edi
mov    0x2c62(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
movl   $0x1,-0x824(%rbp)
jmp    139c <echo_cli+0x1ac>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1443 <echo_cli+0x253>
mov    -0x4(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x400,%edx
call   1090 <readline@plt>
mov    %eax,-0x828(%rbp)
cmpl   $0x0,-0x828(%rbp)
jne    13ea <echo_cli+0x1fa>
lea    0xc27(%rip),%rdi
call   1060 <printf@plt>
jmp    1448 <echo_cli+0x258>
cmpl   $0x0,-0x828(%rbp)
jge    1416 <echo_cli+0x226>
jmp    13fc <echo_cli+0x20c>
lea    0xc12(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2bc1(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
jmp    141b <echo_cli+0x22b>
lea    -0x820(%rbp),%rdi
mov    0x2b9f(%rip),%rax
mov    (%rax),%esi
call   1070 <fputs@plt>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
jmp    1274 <echo_cli+0x84>
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo_cli>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %edi,%r15d
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   10c0 <FD_ZERO@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    %eax,%r14d
cmp    %r15d,%eax
mov    %eax,%ebp
cmovle %r15d,%ebp
lea    0x410(%rsp),%rdi
xor    %r12d,%r12d
mov    $0x400,%edx
xor    %esi,%esi
call   1080 <memset@plt>
lea    0x10(%rsp),%r13
mov    $0x400,%edx
mov    %r13,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
add    $0x1,%ebp
jmp    1280 <echo_cli+0x90>
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
call   1110 <FD_SET@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10d0 <select@plt>
test   %eax,%eax
jne    1292 <echo_cli+0xa2>
test   %r12d,%r12d
jne    1260 <echo_cli+0x70>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1110 <FD_SET@plt>
jmp    1260 <echo_cli+0x70>
cmp    $0xffffffff,%eax
je     137d <echo_cli+0x18d>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1310 <echo_cli+0x120>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edx
lea    0x410(%rsp),%r13
mov    %r13,%rdi
mov    $0x400,%esi
call   10a0 <fgets@plt>
test   %rax,%rax
je     12f4 <echo_cli+0x104>
mov    %r13,%rdi
call   1060 <strlen@plt>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   10f0 <writen@plt>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
jmp    130b <echo_cli+0x11b>
mov    0x2ced(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1050 <shutdown@plt>
mov    $0x1,%r12d
lea    0x10(%rsp),%r13
mov    %r15d,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1280 <echo_cli+0x90>
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1090 <readline@plt>
test   %eax,%eax
je     135f <echo_cli+0x16f>
js     1397 <echo_cli+0x1a7>
mov    0x2c87(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1070 <fputs@plt>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
jmp    1280 <echo_cli+0x90>
lea    0xcaa(%rip),%rdi
call   1030 <puts@plt>
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc7c(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2c40(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
lea    0xc69(%rip),%rdi
jmp    1384 <echo_cli+0x194>

LLVM IR (-O0):
@stdin = global i32 0
@.str = [7 x i8] c"select\00"
@EXIT_FAILURE = global i32 0
@SHUT_WR = global i32 0
@.str.1 = [14 x i8] c"server close\0A\00"
@.str.2 = [9 x i8] c"readline\00"
@stdout = global i32 0
define void @echo_cli(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca [1024 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %2
%11 = call i32 @FD_ZERO(i32* %3)
%12 = load i32, i32* @stdin
%13 = call i32 @fileno(i32 %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = load i32, i32* %2
%16 = icmp sgt i32 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = load i32, i32* %5
br label %21
19: ; preds = %1
%20 = load i32, i32* %2
br label %21
21: ; preds = %19, %17
%22 = phi i32 [ %18, %17 ], [ %20, %19 ]
store i32 %22, i32* %6
%23 = bitcast [1024 x i8]* %7 to i8*
%24 = bitcast [1024 x i8]* %8 to i8*
store i32 0, i32* %9
br label %25
25: ; preds = %21, %48, %100
%26 = load i32, i32* %9
%27 = icmp ne i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = load i32, i32* %5
%30 = call i32 @FD_SET(i32 %29, i32* %3)
br label %31
31: ; preds = %28, %25
%32 = load i32, i32* %2
%33 = call i32 @FD_SET(i32 %32, i32* %3)
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
%36 = call i32 @select(i32 %35, i32* %3, i32* null, i32* null, i32* null)
store i32 %36, i32* %4
%37 = load i32, i32* %4
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %45
39: ; preds = %31
br label %40
40: ; preds = %39
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; No predecessors!
br label %50
45: ; preds = %31
%46 = load i32, i32* %4
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %45
br label %25
49: ; preds = %45
br label %50
50: ; preds = %49, %44
%51 = load i32, i32* %5
%52 = call i64 @FD_ISSET(i32 %51, i32* %3)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %72
54: ; preds = %50
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%56 = load i32, i32* @stdin
%57 = call i64 @fgets(i8* %55, i32 1024, i32 %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %67
59: ; preds = %54
%60 = load i32, i32* %2
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%63 = call i32 @strlen(i8* %62)
%64 = call i32 @writen(i32 %60, i8* %61, i32 %63)
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%66 = call i32 @memset(i8* %65, i32 0, i32 1024)
br label %71
67: ; preds = %54
%68 = load i32, i32* %2
%69 = load i32, i32* @SHUT_WR
%70 = call i32 @shutdown(i32 %68, i32 %69)
store i32 1, i32* %9
br label %71
71: ; preds = %67, %59
br label %72
72: ; preds = %71, %50
%73 = load i32, i32* %2
%74 = call i64 @FD_ISSET(i32 %73, i32* %3)
%75 = icmp ne i64 %74, 0
br i1 %75, label %76, label %100
76: ; preds = %72
%77 = load i32, i32* %2
%78 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%79 = call i32 @readline(i32 %77, i8* %78, i32 1024)
store i32 %79, i32* %10
%80 = load i32, i32* %10
%81 = icmp eq i32 %80, 0
br i1 %81, label %82, label %84
82: ; preds = %76
%83 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %101
84: ; preds = %76
%85 = load i32, i32* %10
%86 = icmp slt i32 %85, 0
br i1 %86, label %87, label %93
87: ; preds = %84
br label %88
88: ; preds = %87
%89 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%90 = load i32, i32* @EXIT_FAILURE
%91 = call i32 @exit(i32 %90)
unreachable
92: ; No predecessors!
br label %93
93: ; preds = %92, %84
br label %94
94: ; preds = %93
%95 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%96 = load i32, i32* @stdout
%97 = call i32 @fputs(i8* %95, i32 %96)
%98 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%99 = call i32 @memset(i8* %98, i32 0, i32 1024)
br label %100
100: ; preds = %94, %72
br label %25
101: ; preds = %82
ret void
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [7 x i8] c"select\00"
@EXIT_FAILURE = global i32 0
@SHUT_WR = global i32 0
@.str.2 = [9 x i8] c"readline\00"
@stdout = global i32 0
@str = [13 x i8] c"server close\00"
define void @echo_cli(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = bitcast i32* %2 to i8*
%6 = call i32 @FD_ZERO(i32* %2)
%7 = load i32, i32* @stdin
%8 = call i32 @fileno(i32 %7)
%9 = icmp sgt i32 %8, %0
%10 = select i1 %9, i32 %8, i32 %0
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%13 = add nsw i32 %10, 1
br label %14
14: ; preds = %58, %1
%15 = phi i32 [ 0, %1 ], [ %42, %58 ]
%16 = icmp eq i32 %15, 0
br label %17
17: ; preds = %14, %20
br i1 %16, label %18, label %20
18: ; preds = %17
%19 = call i32 @FD_SET(i32 %8, i32* %2)
br label %20
20: ; preds = %18, %17
%21 = call i32 @FD_SET(i32 %0, i32* %2)
%22 = call i32 @select(i32 %13, i32* %2, i32* null, i32* null, i32* null)
switch i32 %22, label %27 [
i32 -1, label %23
i32 0, label %17
]
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %20
%28 = call i64 @FD_ISSET(i32 %8, i32* %2)
%29 = icmp eq i64 %28, 0
br i1 %29, label %41, label %30
30: ; preds = %27
%31 = load i32, i32* @stdin
%32 = call i64 @fgets(i8* %11, i32 1024, i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %30
%35 = call i32 @strlen(i8* %11)
%36 = call i32 @writen(i32 %0, i8* %11, i32 %35)
%37 = call i32 @memset(i8* %11, i32 0, i32 1024)
br label %41
38: ; preds = %30
%39 = load i32, i32* @SHUT_WR
%40 = call i32 @shutdown(i32 %0, i32 %39)
br label %41
41: ; preds = %34, %38, %27
%42 = phi i32 [ %15, %34 ], [ 1, %38 ], [ %15, %27 ]
%43 = call i64 @FD_ISSET(i32 %0, i32* %2)
%44 = icmp eq i64 %43, 0
br i1 %44, label %58, label %45
45: ; preds = %41
%46 = call i32 @readline(i32 %0, i8* %12, i32 1024)
%47 = icmp eq i32 %46, 0
br i1 %47, label %59, label %48
48: ; preds = %45
%49 = icmp slt i32 %46, 0
br i1 %49, label %50, label %54
50: ; preds = %48
%51 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %48
%55 = load i32, i32* @stdout
%56 = call i32 @fputs(i8* %12, i32 %55)
%57 = call i32 @memset(i8* %12, i32 0, i32 1024)
br label %58
58: ; preds = %54, %41
br label %14
59: ; preds = %45
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
ret void
}