#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
char *strerror(scalar_t__);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
void echo_cli(int fd) {
  int ret;
  int maxfd;
  fd_set rset;
  char sendbuf[1024];
  char recvbuf[1024];
  memset(sendbuf, 0x00, sizeof(sendbuf));
  memset(recvbuf, 0x00, sizeof(recvbuf));
  FD_ZERO(&rset);
  int fd_stdin = fileno(stdin);
  maxfd = fd_stdin > fd ? fd_stdin : fd;
  for (;;) {
    FD_SET(fd_stdin, &rset);
    FD_SET(fd, &rset);
    ret = select(maxfd + 1, &rset, NULL, NULL, NULL);
    if (ret == -1) {
      if (errno == EINTR)
        continue;
      break;
    }
    if (FD_ISSET(fd_stdin, &rset)) {
      if (fgets(sendbuf, 1024, stdin) == NULL)
        break;
      write(fd, sendbuf, strlen(sendbuf));
      memset(sendbuf, 0x00, sizeof(sendbuf));
    }
    if (FD_ISSET(fd, &rset)) {
      if ((ret = read(fd, recvbuf, 1024)) == -1)
        do {
          fprintf(stderr, "[%s][%d] %s %s\n",
                  "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                  "outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/"
                  "heads/master/apue/day12/select_cli.c",
                  73, "read", strerror(errno));
          exit(EXIT_FAILURE);
        } while (0);
      else if (ret == 0) {
        printf("sever close!\n");
        break;
      }
      printf("%s", recvbuf);
      memset(recvbuf, 0x00, 1024);
    }
  }
}


x86 assembly (clang, -O0):
<echo_cli>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    -0x810(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    -0x10(%rbp),%rdi
call   10c0 <FD_ZERO@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    %eax,-0x814(%rbp)
mov    -0x814(%rbp),%eax
cmp    -0x4(%rbp),%eax
jle    1251 <echo_cli+0x71>
mov    -0x814(%rbp),%eax
mov    %eax,-0x818(%rbp)
jmp    125a <echo_cli+0x7a>
mov    -0x4(%rbp),%eax
mov    %eax,-0x818(%rbp)
mov    -0x818(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x814(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1100 <FD_SET@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1100 <FD_SET@plt>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10d0 <select@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    12c9 <echo_cli+0xe9>
mov    0x2d24(%rip),%rax
mov    (%rax),%rax
mov    0x2d0a(%rip),%rcx
cmp    (%rcx),%rax
jne    12c4 <echo_cli+0xe4>
jmp    1263 <echo_cli+0x83>
jmp    1433 <echo_cli+0x253>
mov    -0x814(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1355 <echo_cli+0x175>
lea    -0x410(%rbp),%rdi
mov    0x2cf8(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
jne    130b <echo_cli+0x12b>
jmp    1433 <echo_cli+0x253>
mov    -0x4(%rbp),%eax
mov    %eax,-0x824(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x820(%rbp)
lea    -0x410(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x824(%rbp),%edi
mov    -0x820(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     142e <echo_cli+0x24e>
mov    -0x4(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    13e4 <echo_cli+0x204>
jmp    1390 <echo_cli+0x1b0>
mov    0x2c41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x828(%rbp)
mov    0x2c2a(%rip),%rax
mov    (%rax),%rdi
call   10f0 <strerror@plt>
mov    -0x828(%rbp),%edi
mov    %rax,%r9
lea    0xc42(%rip),%rsi
lea    0xc4b(%rip),%rdx
mov    $0x49,%ecx
lea    0xcd8(%rip),%r8
call   10a0 <fprintf@plt>
mov    0x2beb(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1401 <echo_cli+0x221>
lea    0xcb9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1433 <echo_cli+0x253>
jmp    1406 <echo_cli+0x226>
lea    -0x810(%rbp),%rsi
lea    0xca8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x810(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
jmp    1263 <echo_cli+0x83>
add    $0x830,%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,%ebx
lea    0x410(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
lea    0xc(%rsp),%r12
mov    %r12,%rdi
call   10d0 <FD_ZERO@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   10c0 <fileno@plt>
mov    %eax,%ebp
cmp    %ebx,%eax
mov    %eax,%r13d
cmovle %ebx,%r13d
add    $0x1,%r13d
jmp    127a <echo_cli+0x8a>
nopw   0x0(%rax,%rax,1)
mov    0x2d69(%rip),%rax
mov    (%rax),%rax
mov    0x2d4f(%rip),%rcx
cmp    (%rcx),%rax
jne    134e <echo_cli+0x15e>
mov    %ebp,%edi
mov    %r12,%rsi
call   1110 <FD_SET@plt>
mov    %ebx,%edi
mov    %r12,%rsi
call   1110 <FD_SET@plt>
mov    %r13d,%edi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10e0 <select@plt>
cmp    $0xffffffff,%eax
je     1260 <echo_cli+0x70>
mov    %ebp,%edi
mov    %r12,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     12f2 <echo_cli+0x102>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    $0x400,%esi
call   10a0 <fgets@plt>
test   %rax,%rax
je     134e <echo_cli+0x15e>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    %ebx,%edi
mov    %r12,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     127a <echo_cli+0x8a>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %eax,%eax
je     1342 <echo_cli+0x152>
cmp    $0xffffffff,%eax
je     1360 <echo_cli+0x170>
lea    0xd8a(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
jmp    127a <echo_cli+0x8a>
lea    0xd68(%rip),%rdi
call   1030 <puts@plt>
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c71(%rip),%rax
mov    (%rax),%ebx
mov    0x2c60(%rip),%rax
mov    (%rax),%rdi
call   1100 <strerror@plt>
lea    0xc81(%rip),%rsi
lea    0xc8a(%rip),%rdx
lea    0xd1c(%rip),%r8
mov    %ebx,%edi
mov    $0x49,%ecx
mov    %rax,%r9
call   10b0 <fprintf@plt>
mov    0x2c25(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@stdin = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d] %s %s\0A\00"
@.str.1 = [153 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day12/select_cli.c\00"
@.str.2 = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [14 x i8] c"sever close!\0A\00"
@.str.4 = [3 x i8] c"%s\00"
define void @echo_cli(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
%8 = alloca i32
store i32 %0, i32* %2
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i32 0, i32 1024)
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 1024)
%13 = call i32 @FD_ZERO(i32* %5)
%14 = load i32, i32* @stdin
%15 = call i32 @fileno(i32 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = load i32, i32* %2
%18 = icmp sgt i32 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = load i32, i32* %8
br label %23
21: ; preds = %1
%22 = load i32, i32* %2
br label %23
23: ; preds = %21, %19
%24 = phi i32 [ %20, %19 ], [ %22, %21 ]
store i32 %24, i32* %4
br label %25
25: ; preds = %88, %39, %23
%26 = load i32, i32* %8
%27 = call i32 @FD_SET(i32 %26, i32* %5)
%28 = load i32, i32* %2
%29 = call i32 @FD_SET(i32 %28, i32* %5)
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
%32 = call i32 @select(i32 %31, i32* %5, i32* null, i32* null, i32* null)
store i32 %32, i32* %3
%33 = load i32, i32* %3
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %41
35: ; preds = %25
%36 = load i64, i64* @errno
%37 = load i64, i64* @EINTR
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35
br label %25
40: ; preds = %35
br label %89
41: ; preds = %25
%42 = load i32, i32* %8
%43 = call i64 @FD_ISSET(i32 %42, i32* %5)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %59
45: ; preds = %41
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%47 = load i32, i32* @stdin
%48 = call i32* @fgets(i8* %46, i32 1024, i32 %47)
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %51
50: ; preds = %45
br label %89
51: ; preds = %45
%52 = load i32, i32* %2
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%55 = call i32 @strlen(i8* %54)
%56 = call i32 @write(i32 %52, i8* %53, i32 %55)
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%58 = call i32 @memset(i8* %57, i32 0, i32 1024)
br label %59
59: ; preds = %51, %41
%60 = load i32, i32* %2
%61 = call i64 @FD_ISSET(i32 %60, i32* %5)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %88
63: ; preds = %59
%64 = load i32, i32* %2
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%66 = call i32 @read(i32 %64, i8* %65, i32 1024)
store i32 %66, i32* %3
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %77
68: ; preds = %63
br label %69
69: ; preds = %68
%70 = load i32, i32* @stderr
%71 = load i64, i64* @errno
%72 = call i8* @strerror(i64 %71)
%73 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([153 x i8], [153 x i8]* @.str.1, i64 0, i64 0), i32 73, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %72)
%74 = load i32, i32* @EXIT_FAILURE
%75 = call i32 @exit(i32 %74)
unreachable
76: ; No predecessors!
br label %83
77: ; preds = %63
%78 = load i32, i32* %3
%79 = icmp eq i32 %78, 0
br i1 %79, label %80, label %82
80: ; preds = %77
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %89
82: ; preds = %77
br label %83
83: ; preds = %82, %76
%84 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %84)
%86 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%87 = call i32 @memset(i8* %86, i32 0, i32 1024)
br label %88
88: ; preds = %83, %59
br label %25
89: ; preds = %80, %50, %40
ret void
}

LLVM IR (-O2):
@stdin = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d] %s %s\0A\00"
@.str.1 = [153 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day12/select_cli.c\00"
@.str.2 = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [3 x i8] c"%s\00"
@str = [13 x i8] c"sever 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 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = call i32 @memset(i8* %6, i32 0, i32 1024)
%9 = call i32 @memset(i8* %7, i32 0, i32 1024)
%10 = call i32 @FD_ZERO(i32* %2)
%11 = load i32, i32* @stdin
%12 = call i32 @fileno(i32 %11)
%13 = icmp sgt i32 %12, %0
%14 = select i1 %13, i32 %12, i32 %0
%15 = add nsw i32 %14, 1
br label %16
16: ; preds = %39, %1
%17 = call i32 @FD_SET(i32 %12, i32* %2)
%18 = call i32 @FD_SET(i32 %0, i32* %2)
%19 = call i32 @select(i32 %15, i32* %2, i32* null, i32* null, i32* null)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
br i1 %24, label %39, label %54
25: ; preds = %16
%26 = call i64 @FD_ISSET(i32 %12, i32* %2)
%27 = icmp eq i64 %26, 0
br i1 %27, label %36, label %28
28: ; preds = %25
%29 = load i32, i32* @stdin
%30 = call i32* @fgets(i8* %6, i32 1024, i32 %29)
%31 = icmp eq i32* %30, null
br i1 %31, label %54, label %32
32: ; preds = %28
%33 = call i32 @strlen(i8* %6)
%34 = call i32 @write(i32 %0, i8* %6, i32 %33)
%35 = call i32 @memset(i8* %6, i32 0, i32 1024)
br label %36
36: ; preds = %32, %25
%37 = call i64 @FD_ISSET(i32 %0, i32* %2)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %36, %51, %21
br label %16
40: ; preds = %36
%41 = call i32 @read(i32 %0, i8* %7, i32 1024)
switch i32 %41, label %51 [
i32 -1, label %42
i32 0, label %49
]
42: ; preds = %40
%43 = load i32, i32* @stderr
%44 = load i64, i64* @errno
%45 = call i8* @strerror(i64 %44)
%46 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([153 x i8], [153 x i8]* @.str.1, i64 0, i64 0), i32 73, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %40
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %54
51: ; preds = %40
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %7)
%53 = call i32 @memset(i8* %7, i32 0, i32 1024)
br label %39
54: ; preds = %28, %21, %49
ret void
}