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

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit_print(char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
size_t read(int, char *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
void str_cli(FILE *fp, int sockfd) {
  char sendbuf[80];
  char recvbuf[80];
  ssize_t len;
  int maxfdp1;
  fd_set rset;
  FD_ZERO(&rset);
  for (;;) {
    FD_SET(fileno(fp), &rset);
    FD_SET(sockfd, &rset);
    maxfdp1 = (fileno(fp) > sockfd ? fileno(fp) : sockfd) + 1;
    select(maxfdp1, &rset, NULL, NULL, NULL);
    if (FD_ISSET(sockfd, &rset)) {
      if ((len = read(sockfd, recvbuf, sizeof(recvbuf))) <= 0) {
        exit_print("client read error: server terminated prematurely.\n");
      }
      recvbuf[len] = '\0';
      fputs(recvbuf, stdout);
    }
    if (FD_ISSET(fileno(fp), &rset)) {
      if (fgets(sendbuf, sizeof(sendbuf), fp) == NULL) {
        return;
      }
      if (write(sockfd, sendbuf, strlen(sendbuf)) < 0) {
        exit_print("client write error.\n");
      }
    }
  }
}


x86 assembly (clang, -O0):
<str_cli>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0xc0(%rbp),%rdi
call   10b0 <FD_ZERO@plt>
mov    -0x8(%rbp),%rdi
call   10a0 <fileno@plt>
mov    %eax,%edi
lea    -0xc0(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0xc(%rbp),%edi
lea    -0xc0(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x8(%rbp),%rdi
call   10a0 <fileno@plt>
cmp    -0xc(%rbp),%eax
jle    121a <str_cli+0x6a>
mov    -0x8(%rbp),%rdi
call   10a0 <fileno@plt>
mov    %eax,-0xc4(%rbp)
jmp    1223 <str_cli+0x73>
mov    -0xc(%rbp),%eax
mov    %eax,-0xc4(%rbp)
mov    -0xc4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xbc(%rbp)
mov    -0xbc(%rbp),%edi
lea    -0xc0(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10c0 <select@plt>
mov    -0xc(%rbp),%edi
lea    -0xc0(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12bd <str_cli+0x10d>
mov    -0xc(%rbp),%edi
lea    -0xb0(%rbp),%rsi
mov    $0x50,%edx
call   1080 <read@plt>
mov    %rax,-0xb8(%rbp)
cmp    $0x0,%rax
ja     1299 <str_cli+0xe9>
lea    0xd6c(%rip),%rdi
call   1050 <exit_print@plt>
mov    -0xb8(%rbp),%rax
movb   $0x0,-0xb0(%rbp,%rax,1)
lea    -0xb0(%rbp),%rdi
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
call   1070 <fputs@plt>
mov    -0x8(%rbp),%rdi
call   10a0 <fileno@plt>
mov    %eax,%edi
lea    -0xc0(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     134f <str_cli+0x19f>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x50,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
jne    1303 <str_cli+0x153>
add    $0xe0,%rsp
pop    %rbp
ret
mov    -0xc(%rbp),%eax
mov    %eax,-0xd4(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0xd0(%rbp)
lea    -0x60(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xd4(%rbp),%edi
mov    -0xd0(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    134a <str_cli+0x19a>
lea    0xcee(%rip),%rdi
call   1050 <exit_print@plt>
jmp    134f <str_cli+0x19f>
jmp    11ce <str_cli+0x1e>

x86 assembly (clang, -O2):
<str_cli>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb8,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xc(%rsp),%r15
mov    %r15,%rdi
call   10b0 <FD_ZERO@plt>
lea    0x60(%rsp),%r12
lea    0x10(%rsp),%rbp
nopl   (%rax)
mov    %r14,%rdi
call   10a0 <fileno@plt>
mov    %eax,%edi
mov    %r15,%rsi
call   10d0 <FD_SET@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   10d0 <FD_SET@plt>
mov    %r14,%rdi
call   10a0 <fileno@plt>
mov    %ebx,%edi
cmp    %ebx,%eax
jle    1214 <str_cli+0x64>
mov    %r14,%rdi
call   10a0 <fileno@plt>
mov    %eax,%edi
add    $0x1,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10c0 <select@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     126f <str_cli+0xbf>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x50,%edx
call   1080 <read@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1258 <str_cli+0xa8>
lea    0xdad(%rip),%rdi
call   1050 <exit_print@plt>
movb   $0x0,0x10(%rsp,%r13,1)
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <fputs@plt>
mov    %r14,%rdi
call   10a0 <fileno@plt>
mov    %eax,%edi
mov    %r15,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11e0 <str_cli+0x30>
mov    %r12,%rdi
mov    $0x50,%esi
mov    %r14,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     12cd <str_cli+0x11d>
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    11e0 <str_cli+0x30>
lea    0xd70(%rip),%rdi
call   1050 <exit_print@plt>
jmp    11e0 <str_cli+0x30>
add    $0xb8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [51 x i8] c"client read error: server terminated prematurely.\0A\00"
@stdout = global i32 0
@.str.1 = [21 x i8] c"client write error.\0A\00"
define void @str_cli(i32* nocapture %0, i32 %1) {
%3 = alloca [80 x i8]
%4 = alloca [80 x i8]
%5 = alloca i32
%6 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%8 = bitcast i32* %5 to i8*
%9 = call i32 @FD_ZERO(i32* %5)
br label %10
10: ; preds = %45, %2
%11 = call i32 @fileno(i32* %0)
%12 = call i32 @FD_SET(i32 %11, i32* %5)
%13 = call i32 @FD_SET(i32 %1, i32* %5)
%14 = call i32 @fileno(i32* %0)
%15 = icmp sgt i32 %14, %1
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = call i32 @fileno(i32* %0)
br label %18
18: ; preds = %10, %16
%19 = phi i32 [ %17, %16 ], [ %1, %10 ]
%20 = add nsw i32 %19, 1
%21 = call i32 @select(i32 %20, i32* %5, i32* null, i32* null, i32* null)
%22 = call i64 @FD_ISSET(i32 %1, i32* %5)
%23 = icmp eq i64 %22, 0
br i1 %23, label %33, label %24
24: ; preds = %18
%25 = call i64 @read(i32 %1, i8* %7, i32 80)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @exit_print(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
%30 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 %25
store i8 0, i8* %30
%31 = load i32, i32* @stdout
%32 = call i32 @fputs(i8* %7, i32 %31)
br label %33
33: ; preds = %29, %18
%34 = call i32 @fileno(i32* %0)
%35 = call i64 @FD_ISSET(i32 %34, i32* %5)
%36 = icmp eq i64 %35, 0
br i1 %36, label %45, label %37
37: ; preds = %33
%38 = call i32* @fgets(i8* %6, i32 80, i32* %0)
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %41
40: ; preds = %37
ret void
41: ; preds = %37
%42 = call i32 @strlen(i8* %6)
%43 = call i64 @write(i32 %1, i8* %6, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %46, label %45
45: ; preds = %41, %46, %33
br label %10
46: ; preds = %41
%47 = call i32 @exit_print(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %45
}