#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 */
struct sockaddr_in {
  int dummy;
};
typedef int /*<<< orphan*/ recvbuf;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(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*/ stdout;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ writen(int, char *, int /*<<< orphan*/);
void echo_server(int connsocket, struct sockaddr_in cliaddr) {
  char recvbuf[1024];
  while (1) {
    memset(&recvbuf, 0, sizeof(recvbuf));
    int recvcount = readline(connsocket, recvbuf, 1024);
    if (recvcount == -1) {
      do {
        perror("readline");
        exit(EXIT_FAILURE);
      } while (0);
    } else if (recvcount == 0) {
      printf("client close\n");
      break;
    }
    fputs(recvbuf, stdout);
    writen(connsocket, recvbuf, strlen(recvbuf));
  }
}


x86 assembly (clang, -O0):
<echo_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %esi,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1070 <readline@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0xffffffff,-0x414(%rbp)
jne    11ea <echo_server+0x6a>
jmp    11d0 <echo_server+0x50>
lea    0xe29(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x414(%rbp)
jne    1208 <echo_server+0x88>
lea    0xe0b(%rip),%rdi
call   1040 <printf@plt>
jmp    125e <echo_server+0xde>
jmp    120d <echo_server+0x8d>
lea    -0x410(%rbp),%rdi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x424(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x420(%rbp)
lea    -0x410(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x424(%rbp),%edi
mov    -0x420(%rbp),%rsi
mov    %eax,%edx
call   1090 <writen@plt>
jmp    1191 <echo_server+0x11>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo_server>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %edi,%ebp
mov    %rsp,%rbx
mov    0x2e41(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <readline@plt>
test   %eax,%eax
je     11e8 <echo_server+0x68>
cmp    $0xffffffff,%eax
je     1200 <echo_server+0x80>
mov    (%r14),%esi
mov    %rbx,%rdi
call   1050 <fputs@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1090 <writen@plt>
jmp    11a0 <echo_server+0x20>
lea    0xe1a(%rip),%rdi
call   1030 <puts@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf9(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
@.str = [9 x i8] c"readline\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [14 x i8] c"client close\0A\00"
@stdout = global i32 0
define void @echo_server(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %1, i32* %7
store i32 %0, i32* %4
br label %8
8: ; preds = %2, %28
%9 = bitcast [1024 x i8]* %5 to i8**
%10 = call i32 @memset(i8** %9, i32 0, i32 1024)
%11 = load i32, i32* %4
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%13 = call i32 @readline(i32 %11, i8* %12, i32 1024)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %8
br label %17
17: ; preds = %16
%18 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; No predecessors!
br label %28
22: ; preds = %8
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %37
27: ; preds = %22
br label %28
28: ; preds = %27, %21
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%30 = load i32, i32* @stdout
%31 = call i32 @fputs(i8* %29, i32 %30)
%32 = load i32, i32* %4
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%35 = call i32 @strlen(i8* %34)
%36 = call i32 @writen(i32 %32, i8* %33, i32 %35)
br label %8
37: ; preds = %25
ret void
}

LLVM IR (-O2):
@.str = [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_server(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = bitcast [1024 x i8]* %3 to i8**
br label %6
6: ; preds = %13, %2
%7 = call i32 @memset(i8** %5, i32 0, i32 1024)
%8 = call i32 @readline(i32 %0, i8* %4, i32 1024)
switch i32 %8, label %13 [
i32 -1, label %9
i32 0, label %18
]
9: ; preds = %6
%10 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %6
%14 = load i32, i32* @stdout
%15 = call i32 @fputs(i8* %4, i32 %14)
%16 = call i32 @strlen(i8* %4)
%17 = call i32 @writen(i32 %0, i8* %4, i32 %16)
br label %6
18: ; preds = %6
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
ret void
}