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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int readline(int, char *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
int /*<<< orphan*/ writen(int, char *, int);
void echo_cli(int sock) {
  char sendbuf[1024] = {0};
  char recvbuf[1024] = {0};
  int n;
  int ret;
  while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL) {
    writen(sock, sendbuf, 1);
    writen(sock, sendbuf + 1, strlen(sendbuf) - 1);
    ret = readline(sock, recvbuf, sizeof(recvbuf));
    if (ret == 0) {
      printf("client close\n");
      break;
    } else if (ret < 0) {
      do {
        perror("readline");
        exit(EXIT_FAILURE);
      } while (0);
    }
    fputs(recvbuf, stdout);
    memset(sendbuf, 0, sizeof(sendbuf));
    memset(recvbuf, 0, sizeof(recvbuf));
  }
  close(sock);
}


x86 assembly (clang, -O0):
<echo_cli>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x810(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x410(%rbp),%rdi
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     12f3 <echo_cli+0x153>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x1,%edx
call   10b0 <writen@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x824(%rbp)
lea    -0x410(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x820(%rbp)
lea    -0x410(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x824(%rbp),%edi
mov    -0x820(%rbp),%rsi
mov    %eax,%edx
sub    $0x1,%edx
call   10b0 <writen@plt>
mov    -0x4(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x400,%edx
call   1070 <readline@plt>
mov    %eax,-0x818(%rbp)
cmpl   $0x0,-0x818(%rbp)
jne    1282 <echo_cli+0xe2>
lea    0xd88(%rip),%rdi
call   1040 <printf@plt>
jmp    12f3 <echo_cli+0x153>
cmpl   $0x0,-0x818(%rbp)
jge    12ae <echo_cli+0x10e>
jmp    1294 <echo_cli+0xf4>
lea    0xd73(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    12b3 <echo_cli+0x113>
lea    -0x810(%rbp),%rdi
mov    0x2d0f(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x810(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
jmp    11d4 <echo_cli+0x34>
mov    -0x4(%rbp),%edi
call   1080 <close@plt>
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo_cli>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %edi,%ebx
mov    %rsp,%r14
mov    $0x400,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
lea    0x400(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    0x2e08(%rip),%r13
mov    0x0(%r13),%edx
mov    %r14,%rdi
mov    $0x400,%esi
call   1090 <fgets@plt>
test   %rax,%rax
je     12a6 <echo_cli+0x106>
lea    0x1(%rsp),%r14
mov    %rsp,%r15
lea    0x400(%rsp),%r12
mov    0x2dbf(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   10b0 <writen@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    -0x1(%rax),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   10b0 <writen@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1070 <readline@plt>
test   %eax,%eax
je     129a <echo_cli+0xfa>
js     12bf <echo_cli+0x11f>
mov    0x0(%rbp),%esi
mov    %r12,%rdi
call   1050 <fputs@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    0x0(%r13),%edx
mov    %r15,%rdi
mov    $0x400,%esi
call   1090 <fgets@plt>
test   %rax,%rax
jne    1220 <echo_cli+0x80>
jmp    12a6 <echo_cli+0x106>
lea    0xd68(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1080 <close@plt>
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd3a(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

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

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