#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 */

/* Variables and functions */
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
void do_echo(int fd_c) {
  char data_buf[256];
  int read_len = 0;
  while (1) {
    bzero(data_buf, 256);
    read_len = read(fd_c, data_buf, 256 - 1);
    if (0 == read_len) {
      printf("client disconnected.\n");
      break;
    } else if (-1 == read_len) {
      perror("read()");
      exit(-1);
    } else {
      printf("[%d] bytes data read from client, echos [%s].\n", read_len,
             data_buf);
      read_len = write(fd_c, data_buf, read_len);
      if (-1 == read_len) {
        perror("write()");
        exit(-1);
      }
      printf("[%d] bytes data echoed to client.\n", read_len);
    }
  }
  return;
}


x86 assembly (clang, -O0):
<do_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x114(%rbp)
lea    -0x110(%rbp),%rdi
mov    $0x100,%esi
call   1070 <bzero@plt>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0xff,%edx
call   1050 <read@plt>
mov    %eax,-0x114(%rbp)
xor    %eax,%eax
cmp    -0x114(%rbp),%eax
jne    11c4 <do_echo+0x64>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1266 <do_echo+0x106>
mov    $0xffffffff,%eax
cmp    -0x114(%rbp),%eax
jne    11eb <do_echo+0x8b>
lea    0xe3a(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x114(%rbp),%esi
lea    -0x110(%rbp),%rdx
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    -0x114(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x114(%rbp)
mov    $0xffffffff,%eax
cmp    -0x114(%rbp),%eax
jne    1248 <do_echo+0xe8>
lea    0xe13(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x114(%rbp),%esi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1261 <do_echo+0x101>
jmp    1178 <do_echo+0x18>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_echo>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %edi,%r12d
mov    %rsp,%rbx
lea    0xe7b(%rip),%r14
lea    0xeab(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x100,%esi
call   1080 <bzero@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1060 <read@plt>
test   %eax,%eax
je     11f7 <do_echo+0x87>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1213 <do_echo+0xa3>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     121c <do_echo+0xac>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11a0 <do_echo+0x30>
lea    0xe63(%rip),%rdi
call   1030 <puts@plt>
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde6(%rip),%rdi
jmp    1223 <do_echo+0xb3>
lea    0xe13(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"client disconnected.\0A\00"
@.str.1 = [7 x i8] c"read()\00"
@.str.2 = [47 x i8] c"[%d] bytes data read from client, echos [%s].\0A\00"
@.str.3 = [8 x i8] c"write()\00"
@.str.4 = [35 x i8] c"[%d] bytes data echoed to client.\0A\00"
define void @do_echo(i32 %0) {
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
br label %5
5: ; preds = %1, %38
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = call i32 @bzero(i8* %6, i32 256)
%8 = load i32, i32* %2
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%10 = call i32 @read(i32 %8, i8* %9, i32 255)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 0, %11
br i1 %12, label %13, label %15
13: ; preds = %5
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %39
15: ; preds = %5
%16 = load i32, i32* %4
%17 = icmp eq i32 -1, %16
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %15
%22 = load i32, i32* %4
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %22, i8* %23)
%25 = load i32, i32* %2
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%27 = load i32, i32* %4
%28 = call i32 @write(i32 %25, i8* %26, i32 %27)
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = icmp eq i32 -1, %29
br i1 %30, label %31, label %34
31: ; preds = %21
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %21
%35 = load i32, i32* %4
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %34
br label %38
38: ; preds = %37
br label %5
39: ; preds = %13
ret void
}

LLVM IR (-O2):
@.str.1 = [7 x i8] c"read()\00"
@.str.2 = [47 x i8] c"[%d] bytes data read from client, echos [%s].\0A\00"
@.str.3 = [8 x i8] c"write()\00"
@.str.4 = [35 x i8] c"[%d] bytes data echoed to client.\0A\00"
@str = [21 x i8] c"client disconnected.\00"
define void @do_echo(i32 %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %19, %1
%5 = call i32 @bzero(i8* %3, i32 256)
%6 = call i32 @read(i32 %0, i8* %3, i32 255)
switch i32 %6, label %12 [
i32 0, label %7
i32 -1, label %9
]
7: ; preds = %4
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
ret void
9: ; preds = %4
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %4
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %6, i8* %3)
%14 = call i32 @write(i32 %0, i8* %3, i32 %6)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %12
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %14)
br label %4
}