#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*/ 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*/);
int echo_serv(int conn) {
  char recvbuf[1024];
  int n;
  int ret;
  while (1) {
    memset(recvbuf, 0, sizeof(recvbuf));
    ret = readline(conn, recvbuf, sizeof(recvbuf));
    if (ret < 0) {
      do {
        perror("readline");
        exit(EXIT_FAILURE);
      } while (0);
    } else if (ret == 0) {
      printf("client close\n");
      break;
    }
    fputs(recvbuf, stdout);
    writen(conn, recvbuf, strlen(recvbuf));
  }
  return 0;
}


x86 assembly (clang, -O0):
<echo_serv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1070 <readline@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0x0,-0x418(%rbp)
jge    11e7 <echo_serv+0x67>
jmp    11cd <echo_serv+0x4d>
lea    0xe2c(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x418(%rbp)
jne    1205 <echo_serv+0x85>
lea    0xe0e(%rip),%rdi
call   1040 <printf@plt>
jmp    125b <echo_serv+0xdb>
jmp    120a <echo_serv+0x8a>
lea    -0x410(%rbp),%rdi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    -0x4(%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    118e <echo_serv+0xe>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo_serv>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %edi,%ebp
mov    %rsp,%rbx
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
js     1205 <echo_serv+0x85>
mov    0x2e1f(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax)
test   %eax,%eax
je     121f <echo_serv+0x9f>
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>
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
jns    11c0 <echo_serv+0x40>
lea    0xdf4(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xde3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.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 i32 @echo_serv(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
br label %6
6: ; preds = %1, %26
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 1024)
%9 = load i32, i32* %2
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%11 = call i32 @readline(i32 %9, i8* %10, i32 1024)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %6
br label %15
15: ; preds = %14
%16 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; No predecessors!
br label %26
20: ; preds = %6
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %35
25: ; preds = %20
br label %26
26: ; preds = %25, %19
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%28 = load i32, i32* @stdout
%29 = call i32 @fputs(i8* %27, i32 %28)
%30 = load i32, i32* %2
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
%34 = call i32 @writen(i32 %30, i8* %31, i32 %33)
br label %6
35: ; preds = %23
ret i32 0
}

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 i32 @echo_serv(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i32 0, i32 1024)
%5 = call i32 @readline(i32 %0, i8* %3, i32 1024)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %16, %1
%8 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %1, %16
%12 = phi i32 [ %22, %16 ], [ %5, %1 ]
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
ret i32 0
16: ; preds = %11
%17 = load i32, i32* @stdout
%18 = call i32 @fputs(i8* %3, i32 %17)
%19 = call i32 @strlen(i8* %3)
%20 = call i32 @writen(i32 %0, i8* %3, i32 %19)
%21 = call i32 @memset(i8* %3, i32 0, i32 1024)
%22 = call i32 @readline(i32 %0, i8* %3, i32 1024)
%23 = icmp slt i32 %22, 0
br i1 %23, label %7, label %11
}