#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 pollfd {
  int fd;
  int events;
  int revents;
};
typedef int /*<<< orphan*/ sendbuf;
typedef int /*<<< orphan*/ recvbuf;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int POLLIN;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ 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 poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
void echo_cli(int fd) {
  int ret;
  char sendbuf[1024];
  char recvbuf[1024];
  struct pollfd fds[2];
  int fd_stdin = fileno(stdin);
  fds[0].fd = fd_stdin;
  fds[0].events = POLLIN;
  fds[1].fd = fd;
  fds[1].events = POLLIN;
  while (1) {
    if (poll(fds, 2, -1) == -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/day13/cli.c",
                33, "poll", strerror(errno));
        exit(EXIT_FAILURE);
      } while (0);
    if (fds[0].revents & POLLIN) {
      memset(sendbuf, 0x00, sizeof(sendbuf));
      if (fgets(sendbuf, 1024, stdin) == NULL)
        break;
      write(fd, sendbuf, strlen(sendbuf));
    }
    if (fds[1].revents & POLLIN) {
      bzero(recvbuf, sizeof(recvbuf));
      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/day13/cli.c",
                  44, "read", strerror(errno));
          exit(EXIT_FAILURE);
        } while (0);
      else if (ret == 0) {
        printf("sever close!\n");
        break;
      }
      printf("%s", recvbuf);
    }
  }
}


x86 assembly (clang, -O0):
<echo_cli>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   10a0 <fileno@plt>
mov    %eax,-0x834(%rbp)
mov    -0x834(%rbp),%eax
mov    %eax,-0x830(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x82c(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x824(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x820(%rbp)
lea    -0x830(%rbp),%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   10b0 <poll@plt>
cmp    $0xffffffff,%eax
jne    128c <echo_cli+0xcc>
jmp    1239 <echo_cli+0x79>
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x838(%rbp)
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x838(%rbp),%edi
mov    %rax,%r9
lea    0xd9a(%rip),%rsi
lea    0xda3(%rip),%rdx
mov    $0x21,%ecx
lea    0xe29(%rip),%r8
call   1090 <fprintf@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x828(%rbp),%eax
mov    0x2d47(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1317 <echo_cli+0x157>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x410(%rbp),%rdi
mov    0x2d23(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
jne    12e0 <echo_cli+0x120>
jmp    13f4 <echo_cli+0x234>
mov    -0x4(%rbp),%eax
mov    %eax,-0x844(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x840(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x844(%rbp),%edi
mov    -0x840(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x81c(%rbp),%eax
mov    0x2cbc(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     13ef <echo_cli+0x22f>
lea    -0x810(%rbp),%rdi
mov    $0x400,%esi
call   10c0 <bzero@plt>
mov    -0x4(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    13b8 <echo_cli+0x1f8>
jmp    1365 <echo_cli+0x1a5>
mov    0x2c64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x848(%rbp)
mov    0x2c4d(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x848(%rbp),%edi
mov    %rax,%r9
lea    0xc6e(%rip),%rsi
lea    0xc77(%rip),%rdx
mov    $0x2c,%ecx
lea    0xd02(%rip),%r8
call   1090 <fprintf@plt>
mov    0x2c0f(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    13d5 <echo_cli+0x215>
lea    0xce3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13f4 <echo_cli+0x234>
jmp    13da <echo_cli+0x21a>
lea    -0x810(%rbp),%rsi
lea    0xcd2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1215 <echo_cli+0x55>
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo_cli>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    %edi,%ebp
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    %eax,(%rsp)
mov    0x2de5(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,0xc(%rsp)
mov    %eax,0x10(%rsp)
mov    %rsp,%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   10c0 <poll@plt>
cmp    $0xffffffff,%eax
je     1302 <echo_cli+0x132>
mov    %rsp,%r14
lea    0x20(%rsp),%r15
lea    0xe77(%rip),%r12
lea    0x420(%rsp),%rbx
jmp    125b <echo_cli+0x8b>
nop
mov    %r14,%rdi
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   10c0 <poll@plt>
cmp    $0xffffffff,%eax
je     1302 <echo_cli+0x132>
mov    0x0(%r13),%eax
test   %eax,0x8(%rsp)
je     12a7 <echo_cli+0xd7>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1090 <fgets@plt>
test   %rax,%rax
je     12f0 <echo_cli+0x120>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x0(%r13),%eax
test   %eax,0x14(%rsp)
je     1240 <echo_cli+0x70>
mov    %r15,%rdi
mov    $0x400,%esi
call   10d0 <bzero@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
je     12e4 <echo_cli+0x114>
cmp    $0xffffffff,%eax
je     1337 <echo_cli+0x167>
mov    %r12,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1240 <echo_cli+0x70>
lea    0xdc4(%rip),%rdi
call   1030 <puts@plt>
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cc7(%rip),%rax
mov    (%rax),%ebx
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xce0(%rip),%rsi
lea    0xce9(%rip),%rdx
lea    0xd74(%rip),%r8
mov    %ebx,%edi
mov    $0x21,%ecx
jmp    136a <echo_cli+0x19a>
mov    0x2c92(%rip),%rax
mov    (%rax),%ebx
mov    0x2c81(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xcab(%rip),%rsi
lea    0xcb4(%rip),%rdx
lea    0xd44(%rip),%r8
mov    %ebx,%edi
mov    $0x2c,%ecx
mov    %rax,%r9
call   10a0 <fprintf@plt>
mov    0x2c47(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@stdin = global i32 0
@POLLIN = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d] %s %s\0A\00"
@.str.1 = [146 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/day13/cli.c\00"
@.str.2 = [5 x i8] c"poll\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [14 x i8] c"sever close!\0A\00"
@.str.5 = [3 x i8] c"%s\00"
define void @echo_cli(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = alloca [2 x %struct.pollfd]
%7 = alloca i32
store i32 %0, i32* %2
%8 = load i32, i32* @stdin
%9 = call i32 @fileno(i32 %8)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 0
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 0
store i32 %10, i32* %12
%13 = load i32, i32* @POLLIN
%14 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 0
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %14, i32 0, i32 1
store i32 %13, i32* %15
%16 = load i32, i32* %2
%17 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 1
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %17, i32 0, i32 0
store i32 %16, i32* %18
%19 = load i32, i32* @POLLIN
%20 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 1
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %20, i32 0, i32 1
store i32 %19, i32* %21
br label %22
22: ; preds = %1, %88
%23 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 0
%24 = call i32 @poll(%struct.pollfd* %23, i32 2, i32 -1)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %35
26: ; preds = %22
br label %27
27: ; preds = %26
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([146 x i8], [146 x i8]* @.str.1, i64 0, i64 0), i32 33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; No predecessors!
br label %35
35: ; preds = %34, %22
%36 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 0
%37 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %36, i32 0, i32 2
%38 = load i32, i32* %37
%39 = load i32, i32* @POLLIN
%40 = and i32 %38, %39
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %56
42: ; preds = %35
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%44 = call i32 @memset(i8* %43, i32 0, i32 1024)
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%46 = load i32, i32* @stdin
%47 = call i32* @fgets(i8* %45, i32 1024, i32 %46)
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %50
49: ; preds = %42
br label %89
50: ; preds = %42
%51 = load i32, i32* %2
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%54 = call i32 @strlen(i8* %53)
%55 = call i32 @write(i32 %51, i8* %52, i32 %54)
br label %56
56: ; preds = %50, %35
%57 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %6, i64 0, i64 1
%58 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %57, i32 0, i32 2
%59 = load i32, i32* %58
%60 = load i32, i32* @POLLIN
%61 = and i32 %59, %60
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %88
63: ; preds = %56
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%65 = call i32 @bzero(i8* %64, i32 1024)
%66 = load i32, i32* %2
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%68 = call i32 @read(i32 %66, i8* %67, i32 1024)
store i32 %68, i32* %3
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %79
70: ; preds = %63
br label %71
71: ; preds = %70
%72 = load i32, i32* @stderr
%73 = load i32, i32* @errno
%74 = call i8* @strerror(i32 %73)
%75 = call i32 @fprintf(i32 %72, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([146 x i8], [146 x i8]* @.str.1, i64 0, i64 0), i32 44, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %74)
%76 = load i32, i32* @EXIT_FAILURE
%77 = call i32 @exit(i32 %76)
unreachable
78: ; No predecessors!
br label %85
79: ; preds = %63
%80 = load i32, i32* %3
%81 = icmp eq i32 %80, 0
br i1 %81, label %82, label %84
82: ; preds = %79
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %89
84: ; preds = %79
br label %85
85: ; preds = %84, %78
%86 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %86)
br label %88
88: ; preds = %85, %56
br label %22
89: ; preds = %82, %49
ret void
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@stdin = global i32 0
@POLLIN = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d] %s %s\0A\00"
@.str.1 = [146 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/day13/cli.c\00"
@.str.2 = [5 x i8] c"poll\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [5 x i8] c"read\00"
@.str.5 = [3 x i8] c"%s\00"
@str = [13 x i8] c"sever close!\00"
define void @echo_cli(i32 %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = alloca [2 x %struct.pollfd]
%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 [2 x %struct.pollfd]* %4 to i8*
%8 = load i32, i32* @stdin
%9 = tail call i32 @fileno(i32 %8)
%10 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0
%11 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0, i32 0
store i32 %9, i32* %11
%12 = load i32, i32* @POLLIN
%13 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1, i32 0
store i32 %0, i32* %14
%15 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1, i32 1
store i32 %12, i32* %15
%16 = call i32 @poll(%struct.pollfd* %10, i32 2, i32 -1)
%17 = icmp eq i32 %16, -1
br i1 %17, label %21, label %18
18: ; preds = %1
%19 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 0, i32 2
%20 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %4, i64 0, i64 1, i32 2
br label %28
21: ; preds = %61, %1
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([146 x i8], [146 x i8]* @.str.1, i64 0, i64 0), i32 33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %18, %61
%29 = load i32, i32* %19
%30 = load i32, i32* @POLLIN
%31 = and i32 %30, %29
%32 = icmp eq i32 %31, 0
br i1 %32, label %42, label %33
33: ; preds = %28
%34 = call i32 @memset(i8* %5, i32 0, i32 1024)
%35 = load i32, i32* @stdin
%36 = call i32* @fgets(i8* %5, i32 1024, i32 %35)
%37 = icmp eq i32* %36, null
br i1 %37, label %64, label %38
38: ; preds = %33
%39 = call i32 @strlen(i8* %5)
%40 = call i32 @write(i32 %0, i8* %5, i32 %39)
%41 = load i32, i32* @POLLIN
br label %42
42: ; preds = %38, %28
%43 = phi i32 [ %41, %38 ], [ %30, %28 ]
%44 = load i32, i32* %20
%45 = and i32 %43, %44
%46 = icmp eq i32 %45, 0
br i1 %46, label %61, label %47
47: ; preds = %42
%48 = call i32 @bzero(i8* %6, i32 1024)
%49 = call i32 @read(i32 %0, i8* %6, i32 1024)
switch i32 %49, label %59 [
i32 -1, label %50
i32 0, label %57
]
50: ; preds = %47
%51 = load i32, i32* @stderr
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([146 x i8], [146 x i8]* @.str.1, i64 0, i64 0), i32 44, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %53)
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %47
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %64
59: ; preds = %47
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %6)
br label %61
61: ; preds = %59, %42
%62 = call i32 @poll(%struct.pollfd* %10, i32 2, i32 -1)
%63 = icmp eq i32 %62, -1
br i1 %63, label %21, label %28
64: ; preds = %33, %57
ret void
}