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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
void str_cli(FILE *fp, int sockfd) {
  ssize_t nread, nwrite;
  char send_buf[256];
  char recv_buf[256];
  while (NULL != fgets(send_buf, sizeof(send_buf), fp)) {
    nwrite = write(sockfd, send_buf, strlen(send_buf));
    if (nwrite < 0) {
      perror("write");
      exit(EXIT_FAILURE);
    }
    bzero(recv_buf, sizeof(recv_buf));
    nread = read(sockfd, recv_buf, sizeof(recv_buf) - 1);
    if (0 == nread) {
      printf("server has closed.\n");
      close(sockfd);
      exit(EXIT_FAILURE);
    } else if (nread < 0) {
      perror("read");
      exit(EXIT_FAILURE);
    }
    fputs(recv_buf, stdout);
  }
}


x86 assembly (clang, -O0):
<str_cli>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x100,%esi
call   1090 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     12d0 <str_cli+0x130>
mov    -0xc(%rbp),%eax
mov    %eax,-0x22c(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x228(%rbp)
lea    -0x120(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x22c(%rbp),%edi
mov    -0x228(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1235 <str_cli+0x95>
lea    0xdde(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    -0x220(%rbp),%rdi
mov    $0x100,%esi
call   10b0 <bzero@plt>
mov    -0xc(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0xff,%edx
call   1080 <read@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    128c <str_cli+0xec>
lea    0xd95(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jge    12b1 <str_cli+0x111>
lea    0xd7c(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    12b6 <str_cli+0x116>
lea    -0x220(%rbp),%rdi
mov    0x2d14(%rip),%rax
mov    (%rax),%esi
call   1060 <fputs@plt>
jmp    11b2 <str_cli+0x12>
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_cli>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     123d <str_cli+0x9d>
mov    %rsp,%r15
lea    0x100(%rsp),%r12
mov    0x2dfc(%rip),%r13
nopl   0x0(%rax)
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %rax,%rax
js     124e <str_cli+0xae>
mov    %r12,%rdi
mov    $0x100,%esi
call   10b0 <bzero@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0xff,%edx
call   1080 <read@plt>
test   %rax,%rax
je     1257 <str_cli+0xb7>
js     126c <str_cli+0xcc>
mov    0x0(%r13),%esi
mov    %r12,%rdi
call   1060 <fputs@plt>
mov    %r15,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
jne    11e0 <str_cli+0x40>
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xdab(%rip),%rdi
jmp    1273 <str_cli+0xd3>
lea    0xdad(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1278 <str_cli+0xd8>
lea    0xd93(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [20 x i8] c"server has closed.\0A\00"
@.str.2 = [5 x i8] c"read\00"
@stdout = global i32 0
define void @str_cli(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca [256 x i8]
%8 = alloca [256 x i8]
store i32* %0, i32** %3
store i32 %1, i32* %4
br label %9
9: ; preds = %48, %2
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%11 = load i32*, i32** %3
%12 = call i32* @fgets(i8* %10, i32 256, i32* %11)
%13 = icmp ne i32* null, %12
br i1 %13, label %14, label %52
14: ; preds = %9
%15 = load i32, i32* %4
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
%19 = call i64 @write(i32 %15, i8* %16, i32 %18)
store i64 %19, i64* %6
%20 = load i64, i64* %6
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %14
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %14
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%28 = call i32 @bzero(i8* %27, i32 256)
%29 = load i32, i32* %4
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%31 = call i64 @read(i32 %29, i8* %30, i32 255)
store i64 %31, i64* %5
%32 = load i64, i64* %5
%33 = icmp eq i64 0, %32
br i1 %33, label %34, label %40
34: ; preds = %26
%35 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%36 = load i32, i32* %4
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %26
%41 = load i64, i64* %5
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %40
br label %48
48: ; preds = %47
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%50 = load i32, i32* @stdout
%51 = call i32 @fputs(i8* %49, i32 %50)
br label %9
52: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [5 x i8] c"read\00"
@stdout = global i32 0
@str = [19 x i8] c"server has closed.\00"
define void @str_cli(i32* nocapture %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = call i32* @fgets(i8* %5, i32 256, i32* %0)
%8 = icmp eq i32* %7, null
br i1 %8, label %37, label %9
9: ; preds = %2, %32
%10 = call i32 @strlen(i8* %5)
%11 = call i64 @write(i32 %1, i8* %5, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %9
%18 = call i32 @bzero(i8* %6, i32 256)
%19 = call i64 @read(i32 %1, i8* %6, i32 255)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%23 = call i32 @close(i32 %1)
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %17
%27 = icmp slt i64 %19, 0
br i1 %27, label %28, label %32
28: ; preds = %26
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %26
%33 = load i32, i32* @stdout
%34 = call i32 @fputs(i8* %6, i32 %33)
%35 = call i32* @fgets(i8* %5, i32 256, i32* %0)
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %9
37: ; preds = %32, %2
ret void
}