#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*/ close(int);
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*/ 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};
  while (1) {
    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));
      }
    }
    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(sendbuf, 0, sizeof(sendbuf));
    memset(recvbuf, 0, sizeof(recvbuf));
  }
  close(sock);
}


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    0x2dda(%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,-0x828(%rbp)
jmp    123b <echo_cli+0x4b>
mov    -0x4(%rbp),%eax
mov    %eax,-0x828(%rbp)
mov    -0x828(%rbp),%eax
mov    %eax,-0x14(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
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    12c8 <echo_cli+0xd8>
jmp    12ae <echo_cli+0xbe>
lea    0xd4b(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
jne    12d7 <echo_cli+0xe7>
jmp    126a <echo_cli+0x7a>
jmp    12dc <echo_cli+0xec>
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1352 <echo_cli+0x162>
lea    -0x420(%rbp),%rdi
mov    0x2ce8(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   10a0 <fgets@plt>
cmp    $0x0,%rax
je     134d <echo_cli+0x15d>
mov    -0x4(%rbp),%eax
mov    %eax,-0x834(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x830(%rbp)
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x834(%rbp),%edi
mov    -0x830(%rbp),%rsi
mov    %eax,%edx
call   10f0 <writen@plt>
jmp    1352 <echo_cli+0x162>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13e6 <echo_cli+0x1f6>
mov    -0x4(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x400,%edx
call   1080 <readline@plt>
mov    %eax,-0x824(%rbp)
cmpl   $0x0,-0x824(%rbp)
jne    13a0 <echo_cli+0x1b0>
lea    0xc71(%rip),%rdi
call   1050 <printf@plt>
jmp    1411 <echo_cli+0x221>
cmpl   $0x0,-0x824(%rbp)
jge    13cc <echo_cli+0x1dc>
jmp    13b2 <echo_cli+0x1c2>
lea    0xc5c(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2c13(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
jmp    13d1 <echo_cli+0x1e1>
lea    -0x820(%rbp),%rdi
mov    0x2bf1(%rip),%rax
mov    (%rax),%esi
call   1060 <fputs@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
jmp    126a <echo_cli+0x7a>
mov    -0x4(%rbp),%edi
call   1090 <close@plt>
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,%ebp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   10c0 <FD_ZERO@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    %eax,%r14d
cmp    %ebp,%eax
mov    %eax,%r15d
cmovle %ebp,%r15d
lea    0x410(%rsp),%r12
mov    $0x400,%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
lea    0x10(%rsp),%r13
mov    $0x400,%edx
mov    %r13,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
add    $0x1,%r15d
jmp    128f <echo_cli+0x9f>
nopl   0x0(%rax,%rax,1)
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1060 <fputs@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1110 <FD_SET@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1110 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10d0 <select@plt>
test   %eax,%eax
je     128f <echo_cli+0x9f>
cmp    $0xffffffff,%eax
je     135c <echo_cli+0x16c>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1302 <echo_cli+0x112>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    $0x400,%esi
call   10a0 <fgets@plt>
test   %rax,%rax
je     1302 <echo_cli+0x112>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   10f0 <writen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1271 <echo_cli+0x81>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1080 <readline@plt>
test   %eax,%eax
je     1337 <echo_cli+0x147>
jns    1260 <echo_cli+0x70>
lea    0xcd2(%rip),%rdi
jmp    1363 <echo_cli+0x173>
lea    0xcd2(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1090 <close@plt>
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc9d(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2c69(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>

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

LLVM IR (-O2):
@stdin = global i32 0
@.str = [7 x i8] c"select\00"
@EXIT_FAILURE = 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 = %50, %1
%15 = call i32 @FD_SET(i32 %8, i32* %2)
%16 = call i32 @FD_SET(i32 %0, i32* %2)
%17 = call i32 @select(i32 %13, i32* %2, i32* null, i32* null, i32* null)
switch i32 %17, label %22 [
i32 -1, label %18
i32 0, label %50
]
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %14
%23 = call i64 @FD_ISSET(i32 %8, i32* %2)
%24 = icmp eq i64 %23, 0
br i1 %24, label %32, label %25
25: ; preds = %22
%26 = load i32, i32* @stdin
%27 = call i64 @fgets(i8* %11, i32 1024, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %25
%30 = call i32 @strlen(i8* %11)
%31 = call i32 @writen(i32 %0, i8* %11, i32 %30)
br label %32
32: ; preds = %25, %29, %22
%33 = call i64 @FD_ISSET(i32 %0, i32* %2)
%34 = icmp eq i64 %33, 0
br i1 %34, label %47, label %35
35: ; preds = %32
%36 = call i32 @readline(i32 %0, i8* %12, i32 1024)
%37 = icmp eq i32 %36, 0
br i1 %37, label %51, label %38
38: ; preds = %35
%39 = icmp slt i32 %36, 0
br i1 %39, label %40, label %44
40: ; preds = %38
%41 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %38
%45 = load i32, i32* @stdout
%46 = call i32 @fputs(i8* %12, i32 %45)
br label %47
47: ; preds = %44, %32
%48 = call i32 @memset(i8* %11, i32 0, i32 1024)
%49 = call i32 @memset(i8* %12, i32 0, i32 1024)
br label %50
50: ; preds = %47, %14
br label %14
51: ; preds = %35
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%53 = call i32 @close(i32 %0)
ret void
}