#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 /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ writen(int, char *, int /*<<< orphan*/);
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, strlen(sendbuf));
    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     12d8 <echo_cli+0x138>
mov    -0x4(%rbp),%eax
mov    %eax,-0x824(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x820(%rbp)
lea    -0x410(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x824(%rbp),%edi
mov    -0x820(%rbp),%rsi
mov    %eax,%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    1267 <echo_cli+0xc7>
lea    0xda3(%rip),%rdi
call   1040 <printf@plt>
jmp    12d8 <echo_cli+0x138>
cmpl   $0x0,-0x818(%rbp)
jge    1293 <echo_cli+0xf3>
jmp    1279 <echo_cli+0xd9>
lea    0xd8e(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    1298 <echo_cli+0xf8>
lea    -0x810(%rbp),%rdi
mov    0x2d2a(%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   %r12
push   %rbx
sub    $0x800,%rsp
mov    %edi,%r14d
lea    0x400(%rsp),%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %rsp,%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    0x2e09(%rip),%r15
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1090 <fgets@plt>
test   %rax,%rax
je     1288 <echo_cli+0xe8>
lea    0x400(%rsp),%rbx
mov    %rsp,%rbp
mov    0x2dc6(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   10b0 <writen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1070 <readline@plt>
test   %eax,%eax
je     127c <echo_cli+0xdc>
js     12a0 <echo_cli+0x100>
mov    (%r12),%esi
mov    %rbp,%rdi
call   1050 <fputs@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1090 <fgets@plt>
test   %rax,%rax
jne    1210 <echo_cli+0x70>
jmp    1288 <echo_cli+0xe8>
lea    0xd86(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1080 <close@plt>
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd59(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d25(%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 = %37, %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 %45
14: ; preds = %9
%15 = load i32, i32* %2
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
%19 = call i32 @writen(i32 %15, i8* %16, i32 %18)
%20 = load i32, i32* %2
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%22 = call i32 @readline(i32 %20, i8* %21, i32 1024)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %45
27: ; preds = %14
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %27
br label %31
31: ; preds = %30
%32 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; No predecessors!
br label %36
36: ; preds = %35, %27
br label %37
37: ; preds = %36
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%39 = load i32, i32* @stdout
%40 = call i32 @fputs(i8* %38, i32 %39)
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%42 = call i32 @memset(i8* %41, i32 0, i32 1024)
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%44 = call i32 @memset(i8* %43, i32 0, i32 1024)
br label %9
45: ; preds = %25, %9
%46 = load i32, i32* %2
%47 = call i32 @close(i32 %46)
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 %30, label %9
9: ; preds = %1, %22
%10 = call i32 @strlen(i8* %4)
%11 = call i32 @writen(i32 %0, i8* %4, i32 %10)
%12 = call i32 @readline(i32 %0, i8* %5, i32 1024)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %30
16: ; preds = %9
%17 = icmp slt i32 %12, 0
br i1 %17, label %18, label %22
18: ; preds = %16
%19 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %16
%23 = load i32, i32* @stdout
%24 = call i32 @fputs(i8* %5, i32 %23)
%25 = call i32 @memset(i8* %4, i32 0, i32 1024)
%26 = call i32 @memset(i8* %5, i32 0, i32 1024)
%27 = load i32, i32* @stdin
%28 = call i32* @fgets(i8* %4, i32 1024, i32 %27)
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %9
30: ; preds = %22, %1, %14
%31 = call i32 @close(i32 %0)
ret void
}