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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
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;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ heart_beat_cli(int, int, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdout;
int /*<<< orphan*/ writen(int, char *, int);
void str_cli(FILE *fp, int sockfd) {
  int maxfd, stdineof;
  fd_set rset;
  char buf[1024];
  ssize_t n;
  stdineof = 0;
  FD_ZERO(&rset);
  heart_beat_cli(sockfd, 2, 5);
  while (1) {
    if (stdineof == 0)
      FD_SET(fileno(fp), &rset);
    FD_SET(sockfd, &rset);
    maxfd = ((sockfd) > (fileno(fp)) ? (sockfd) : (fileno(fp))) + 1;
    n = select(maxfd, &rset, NULL, NULL, NULL);
    if (n == -1) {
      if (errno == EINTR)
        continue;
      else
        perror("select() failed");
      exit(EXIT_FAILURE);
    }
    if (FD_ISSET(sockfd, &rset)) {
      if ((n = read(sockfd, buf, sizeof(buf))) == 0) {
        if (stdineof == 1)
          return;
        else
          fprintf(stderr, "server terminated prematurely\n");
        return;
      } else if (n < 0) {
        perror("read() from socket failed");
        return;
      }
      writen(fileno(stdout), buf, n);
    }
    if (FD_ISSET(fileno(fp), &rset)) {
      if ((n = read(fileno(fp), buf, sizeof(buf))) == 0) {
        stdineof = 1;
        shutdown(sockfd, SHUT_WR);
        FD_CLR(fileno(fp), &rset);
        continue;
      } else if (n < 0) {
        perror("read() from stdin failed");
        return;
      }
      writen(sockfd, buf, n);
    }
  }
}


x86 assembly (clang, -O0):
<str_cli>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
call   1090 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
mov    $0x2,%esi
mov    $0x5,%edx
call   1050 <heart_beat_cli@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1222 <str_cli+0x52>
mov    -0x8(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
lea    -0x18(%rbp),%rsi
call   10f0 <FD_SET@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10f0 <FD_SET@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x428(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,%ecx
mov    -0x428(%rbp),%eax
cmp    %ecx,%eax
jle    125e <str_cli+0x8e>
mov    -0xc(%rbp),%eax
mov    %eax,-0x42c(%rbp)
jmp    126d <str_cli+0x9d>
mov    -0x8(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,-0x42c(%rbp)
mov    -0x42c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10a0 <select@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0xffffffff,-0x424(%rbp)
jne    12dc <str_cli+0x10c>
mov    0x2d26(%rip),%rax
mov    (%rax),%rax
mov    0x2d0c(%rip),%rcx
cmp    (%rcx),%rax
jne    12c2 <str_cli+0xf2>
jmp    1204 <str_cli+0x34>
lea    0xd37(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1384 <str_cli+0x1b4>
mov    -0xc(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x424(%rbp)
cmp    $0x0,%eax
jne    133e <str_cli+0x16e>
cmpl   $0x1,-0x14(%rbp)
jne    1324 <str_cli+0x154>
jmp    143b <str_cli+0x26b>
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
lea    0xcdc(%rip),%rsi
call   1070 <fprintf@plt>
jmp    143b <str_cli+0x26b>
cmpl   $0x0,-0x424(%rbp)
jge    135c <str_cli+0x18c>
lea    0xcdd(%rip),%rdi
call   10b0 <perror@plt>
jmp    143b <str_cli+0x26b>
jmp    1361 <str_cli+0x191>
mov    0x2c50(%rip),%rax
mov    (%rax),%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
call   10c0 <writen@plt>
mov    -0x8(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
lea    -0x18(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1436 <str_cli+0x266>
mov    -0x8(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x424(%rbp)
cmp    $0x0,%eax
jne    13fe <str_cli+0x22e>
movl   $0x1,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2c0a(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
mov    -0x8(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
lea    -0x18(%rbp),%rsi
call   10d0 <FD_CLR@plt>
jmp    1204 <str_cli+0x34>
cmpl   $0x0,-0x424(%rbp)
jge    141c <str_cli+0x24c>
lea    0xc37(%rip),%rdi
call   10b0 <perror@plt>
jmp    143b <str_cli+0x26b>
jmp    1421 <str_cli+0x251>
mov    -0xc(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
call   10c0 <writen@plt>
jmp    1204 <str_cli+0x34>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_cli>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xc(%rsp),%r15
mov    %r15,%rdi
call   1090 <FD_ZERO@plt>
mov    %ebx,%edi
mov    $0x2,%esi
mov    $0x5,%edx
call   1050 <heart_beat_cli@plt>
mov    $0x1,%r13b
xor    %eax,%eax
mov    %rax,(%rsp)
lea    0x10(%rsp),%r12
jmp    123a <str_cli+0x6a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2da9(%rip),%rax
mov    (%rax),%rax
mov    0x2d8f(%rip),%rcx
cmp    (%rcx),%rax
jne    1391 <str_cli+0x1c1>
test   $0x1,%r13b
je     1252 <str_cli+0x82>
mov    %r14,%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
mov    %r15,%rsi
call   10f0 <FD_SET@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   10f0 <FD_SET@plt>
mov    %r14,%rdi
call   1080 <fileno@plt>
mov    %ebx,%edi
cmp    %ebx,%eax
jl     1274 <str_cli+0xa4>
mov    %r14,%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
add    $0x1,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <select@plt>
cmp    $0xffffffff,%eax
je     1220 <str_cli+0x50>
mov    %ebx,%edi
mov    %r15,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     12d4 <str_cli+0x104>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
je     134d <str_cli+0x17d>
js     137a <str_cli+0x1aa>
mov    %eax,%ebp
mov    0x2cf8(%rip),%rax
mov    (%rax),%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   10c0 <writen@plt>
mov    %r14,%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
mov    %r15,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     123a <str_cli+0x6a>
mov    %r14,%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
je     131d <str_cli+0x14d>
js     1383 <str_cli+0x1b3>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   10c0 <writen@plt>
jmp    123a <str_cli+0x6a>
mov    0x2cc4(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <shutdown@plt>
mov    %r14,%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
mov    %r15,%rsi
call   10d0 <FD_CLR@plt>
mov    $0x1,%al
mov    %rax,(%rsp)
xor    %r13d,%r13d
jmp    123a <str_cli+0x6a>
testb  $0x1,(%rsp)
jne    1368 <str_cli+0x198>
mov    0x2c7e(%rip),%rax
mov    (%rax),%edi
lea    0xcad(%rip),%rsi
call   1070 <fprintf@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcae(%rip),%rdi
jmp    138a <str_cli+0x1ba>
lea    0xcbf(%rip),%rdi
call   10b0 <perror@plt>
jmp    1368 <str_cli+0x198>
lea    0xc68(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c24(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

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

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