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


x86 assembly (clang, -O0):
<echo_srv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%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   1080 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jne    11d9 <echo_srv+0x59>
lea    0xe31(%rip),%rdi
call   1040 <printf@plt>
jmp    1239 <echo_srv+0xb9>
cmpl   $0xffffffff,-0x414(%rbp)
jne    1205 <echo_srv+0x85>
jmp    11eb <echo_srv+0x6b>
lea    0xe1c(%rip),%rdi
call   1090 <perror@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    120a <echo_srv+0x8a>
lea    -0x410(%rbp),%rdi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1030 <write@plt>
jmp    118e <echo_srv+0xe>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echo_srv>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %edi,%r14d
mov    %rsp,%rbx
mov    0x2e3e(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
je     11e4 <echo_srv+0x64>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1206 <echo_srv+0x86>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1050 <fputs@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
jmp    11a0 <echo_srv+0x20>
lea    0xe1a(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1070 <close@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf3(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
@.str.1 = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
@stdout = global i32 0
@str = [13 x i8] c"client close\00"
define void @echo_srv(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %11, %1
%5 = call i32 @memset(i8* %3, i32 0, i32 1024)
%6 = call i32 @read(i32 %0, i8* %3, i32 1024)
switch i32 %6, label %11 [
i32 0, label %15
i32 -1, label %7
]
7: ; preds = %4
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %4
%12 = load i32, i32* @stdout
%13 = call i32 @fputs(i8* %3, i32 %12)
%14 = call i32 @write(i32 %0, i8* %3, i32 %6)
br label %4
15: ; preds = %4
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%17 = call i32 @close(i32 %0)
ret void
}