#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;
typedef int /*<<< orphan*/ FILE;

/* 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 /*<<< orphan*/ SHUT_WR;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int strlen(char *);
scalar_t__ write(int, char *, int);
void str_cli(FILE *fp, int sockfd) {
  int n, maxfdp1, stdineof;
  fd_set rset;
  char sendline[160], recvline[160];
  stdineof = 0;
  FD_ZERO(&rset);
  for (;;) {
    if (stdineof == 0)
      FD_SET(fileno(fp), &rset);
    FD_SET(sockfd, &rset);
    maxfdp1 = fileno(fp);
    if (sockfd > maxfdp1) {
      maxfdp1 = sockfd;
    }
    maxfdp1 = maxfdp1 + 1;
    select(maxfdp1, &rset, NULL, NULL, NULL);
    if (FD_ISSET(sockfd, &rset)) {
      if ((n = read(sockfd, recvline, 160)) == 0) {
        if (stdineof == 1)
          return;
        else {
          perror("str_cli: server terminated prematurely");
          exit(0);
        }
      }
      recvline[n] = 0;
      fputs(recvline, stdout);
      printf("\n");
    }
    if (FD_ISSET(fileno(fp), &rset)) {
      if (fgets(sendline, 160, fp) == NULL) {
        stdineof = 1;
        shutdown(sockfd, SHUT_WR);
        FD_CLR(fileno(fp), &rset);
        continue;
      }
      sendline[strlen(sendline) - 1] = 0;
      if (write(sockfd, sendline, strlen(sendline)) < 0) {
        perror("write error");
        exit(0);
      }
    }
  }
}


x86 assembly (clang, -O0):
<str_cli>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   10c0 <FD_ZERO@plt>
cmpl   $0x0,-0x18(%rbp)
jne    1230 <str_cli+0x40>
mov    -0x8(%rbp),%rdi
call   10b0 <fileno@plt>
mov    %eax,%edi
lea    -0x1c(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0xc(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0x8(%rbp),%rdi
call   10b0 <fileno@plt>
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    125a <str_cli+0x6a>
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10d0 <select@plt>
mov    -0xc(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1303 <str_cli+0x113>
mov    -0xc(%rbp),%edi
lea    -0x160(%rbp),%rsi
mov    $0xa0,%edx
call   1090 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jne    12d6 <str_cli+0xe6>
cmpl   $0x1,-0x18(%rbp)
jne    12c3 <str_cli+0xd3>
add    $0x170,%rsp
pop    %rbp
ret
lea    0xd36(%rip),%rdi
call   10e0 <perror@plt>
xor    %edi,%edi
call   1100 <exit@plt>
movslq -0x10(%rbp),%rax
movb   $0x0,-0x160(%rbp,%rax,1)
lea    -0x160(%rbp),%rdi
mov    0x2ce8(%rip),%rax
mov    (%rax),%esi
call   1080 <fputs@plt>
lea    0xd29(%rip),%rdi
call   1070 <printf@plt>
mov    -0x8(%rbp),%rdi
call   10b0 <fileno@plt>
mov    %eax,%edi
lea    -0x1c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13e3 <str_cli+0x1f3>
lea    -0xc0(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0xa0,%esi
call   10a0 <fgets@plt>
cmp    $0x0,%rax
jne    1371 <str_cli+0x181>
movl   $0x1,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2c97(%rip),%rax
mov    (%rax),%esi
call   1050 <shutdown@plt>
mov    -0x8(%rbp),%rdi
call   10b0 <fileno@plt>
mov    %eax,%edi
lea    -0x1c(%rbp),%rsi
call   10f0 <FD_CLR@plt>
jmp    1212 <str_cli+0x22>
lea    -0xc0(%rbp),%rdi
call   1060 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0xc0(%rbp,%rax,1)
mov    -0xc(%rbp),%eax
mov    %eax,-0x16c(%rbp)
lea    -0xc0(%rbp),%rax
mov    %rax,-0x168(%rbp)
lea    -0xc0(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x16c(%rbp),%edi
mov    -0x168(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    13de <str_cli+0x1ee>
lea    0xc57(%rip),%rdi
call   10e0 <perror@plt>
xor    %edi,%edi
call   1100 <exit@plt>
jmp    13e3 <str_cli+0x1f3>
jmp    1212 <str_cli+0x22>

x86 assembly (clang, -O2):
<str_cli>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x158,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xc(%rsp),%r15
mov    %r15,%rdi
call   10c0 <FD_ZERO@plt>
mov    $0x1,%bpl
xor    %eax,%eax
mov    %rax,(%rsp)
lea    0xb0(%rsp),%r12
lea    0x10(%rsp),%r13
nopl   0x0(%rax)
test   $0x1,%bpl
je     1248 <str_cli+0x58>
mov    %r14,%rdi
call   10b0 <fileno@plt>
mov    %eax,%edi
mov    %r15,%rsi
call   1110 <FD_SET@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1110 <FD_SET@plt>
mov    %r14,%rdi
call   10b0 <fileno@plt>
cmp    %ebx,%eax
cmovl  %ebx,%eax
lea    0x1(%rax),%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10d0 <select@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     12b9 <str_cli+0xc9>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0xa0,%edx
call   1090 <read@plt>
test   %eax,%eax
je     1347 <str_cli+0x157>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    0x2d33(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1080 <fputs@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
call   10b0 <fileno@plt>
mov    %eax,%edi
mov    %r15,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1230 <str_cli+0x40>
mov    %r12,%rdi
mov    $0xa0,%esi
mov    %r14,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
je     1318 <str_cli+0x128>
mov    %r12,%rdi
call   1070 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r12,1)
mov    %r12,%rdi
call   1070 <strlen@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %rax,%rax
jns    1230 <str_cli+0x40>
jmp    135f <str_cli+0x16f>
mov    0x2cc9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <shutdown@plt>
mov    %r14,%rdi
call   10b0 <fileno@plt>
mov    %eax,%edi
mov    %r15,%rsi
call   10f0 <FD_CLR@plt>
mov    $0x1,%al
mov    %rax,(%rsp)
xor    %ebp,%ebp
jmp    1230 <str_cli+0x40>
testb  $0x1,(%rsp)
je     1372 <str_cli+0x182>
add    $0x158,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcc1(%rip),%rdi
call   10e0 <perror@plt>
xor    %edi,%edi
call   1100 <exit@plt>
lea    0xc87(%rip),%rdi
jmp    1366 <str_cli+0x176>

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

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