#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*/ close(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
void echo_cli(int sock) {
  char sendbuf[1024] = {0};
  char recvbuf[1024] = {0};
  while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL) {
    write(sock, sendbuf, strlen(sendbuf));
    read(sock, recvbuf, sizeof(recvbuf));
    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    $0x820,%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    0x2e36(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     1253 <echo_cli+0xe3>
mov    -0x4(%rbp),%eax
mov    %eax,-0x81c(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x818(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x81c(%rbp),%edi
mov    -0x818(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
lea    -0x810(%rbp),%rdi
mov    0x2db7(%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    11a4 <echo_cli+0x34>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
add    $0x820,%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    0x2e39(%rip),%r15
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1090 <fgets@plt>
test   %rax,%rax
je     1244 <echo_cli+0xd4>
lea    0x400(%rsp),%rbx
mov    %rsp,%rbp
mov    0x2dfe(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
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    11e0 <echo_cli+0x70>
mov    %r14d,%edi
call   1070 <close@plt>
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = 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]
store i32 %0, i32* %2
%5 = bitcast [1024 x i8]* %3 to i8*
%6 = bitcast [1024 x i8]* %4 to i8*
br label %7
7: ; preds = %12, %1
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%9 = load i32, i32* @stdin
%10 = call i32* @fgets(i8* %8, i32 1024, i32 %9)
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %28
12: ; preds = %7
%13 = load i32, i32* %2
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%16 = call i32 @strlen(i8* %15)
%17 = call i32 @write(i32 %13, i8* %14, i32 %16)
%18 = load i32, i32* %2
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 1024)
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%22 = load i32, i32* @stdout
%23 = call i32 @fputs(i8* %21, i32 %22)
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%25 = call i32 @memset(i8* %24, i32 0, i32 1024)
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%27 = call i32 @memset(i8* %26, i32 0, i32 1024)
br label %7
28: ; preds = %7
%29 = load i32, i32* %2
%30 = call i32 @close(i32 %29)
ret void
}

LLVM IR (-O2):
@stdin = global i32 0
@stdout = global i32 0
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 %20, label %9
9: ; preds = %1, %9
%10 = call i32 @strlen(i8* %4)
%11 = call i32 @write(i32 %0, i8* %4, i32 %10)
%12 = call i32 @read(i32 %0, i8* %5, i32 1024)
%13 = load i32, i32* @stdout
%14 = call i32 @fputs(i8* %5, i32 %13)
%15 = call i32 @memset(i8* %4, i32 0, i32 1024)
%16 = call i32 @memset(i8* %5, i32 0, i32 1024)
%17 = load i32, i32* @stdin
%18 = call i32* @fgets(i8* %4, i32 1024, i32 %17)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %9
20: ; preds = %9, %1
%21 = call i32 @close(i32 %0)
ret void
}