#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, scalar_t__, char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, scalar_t__);
void str_echo(int sockfd) {
  ssize_t nread;
  char buf[256];
  bzero(buf, sizeof(buf));
  while (1) {
    nread = read(sockfd, buf, sizeof(buf) - 1);
    if (nread < 0) {
      if (EINTR == errno) {
        continue;
      }
      perror("read");
    } else if (0 == nread) {
      close(sockfd);
      break;
    }
    write(sockfd, buf, nread);
    printf("echo socket (%d) : <%ld> %s.\n", sockfd, nread, buf);
  }
}


x86 assembly (clang, -O0):
<str_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x110(%rbp),%rdi
mov    $0x100,%esi
call   1080 <bzero@plt>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0xff,%edx
call   1060 <read@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11d2 <str_echo+0x72>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rax
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%rax
jne    11c1 <str_echo+0x61>
jmp    117f <str_echo+0x1f>
lea    0xe38(%rip),%rdi
call   1070 <perror@plt>
jmp    11f0 <str_echo+0x90>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    11eb <str_echo+0x8b>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
jmp    1222 <str_echo+0xc2>
jmp    11f0 <str_echo+0x90>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <write@plt>
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    -0x110(%rbp),%rcx
lea    0xded(%rip),%rdi
call   1040 <printf@plt>
jmp    117f <str_echo+0x1f>
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_echo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %edi,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x100,%esi
call   1080 <bzero@plt>
mov    0x2e4e(%rip),%r13
mov    0x2e4f(%rip),%r15
lea    0xe6d(%rip),%r12
jmp    11a9 <str_echo+0x49>
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%rax
cmp    (%r15),%rax
jne    11e1 <str_echo+0x81>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xff,%edx
call   1060 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
js     11a0 <str_echo+0x40>
je     11ef <str_echo+0x8f>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
mov    %r12,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1040 <printf@plt>
jmp    11a9 <str_echo+0x49>
lea    0xe18(%rip),%rdi
call   1070 <perror@plt>
jmp    11c2 <str_echo+0x62>
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINTR = global i64 0
@errno = global i64 0
@.str = [5 x i8] c"read\00"
@.str.1 = [30 x i8] c"echo socket (%d) : <%ld> %s.\0A\00"
define void @str_echo(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca [256 x i8]
store i32 %0, i32* %2
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = call i32 @bzero(i8* %5, i32 256)
br label %7
7: ; preds = %1, %17, %27
%8 = load i32, i32* %2
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%10 = call i64 @read(i32 %8, i8* %9, i32 255)
store i64 %10, i64* %3
%11 = load i64, i64* %3
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %7
%14 = load i64, i64* @EINTR
%15 = load i64, i64* @errno
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %18
17: ; preds = %13
br label %7
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %27
20: ; preds = %7
%21 = load i64, i64* %3
%22 = icmp eq i64 0, %21
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* %2
%25 = call i32 @close(i32 %24)
br label %36
26: ; preds = %20
br label %27
27: ; preds = %26, %18
%28 = load i32, i32* %2
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%30 = load i64, i64* %3
%31 = call i32 @write(i32 %28, i8* %29, i64 %30)
%32 = load i32, i32* %2
%33 = load i64, i64* %3
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%35 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %32, i64 %33, i8* %34)
br label %7
36: ; preds = %23
ret void
}

LLVM IR (-O2):
@EINTR = global i64 0
@errno = global i64 0
@.str = [5 x i8] c"read\00"
@.str.1 = [30 x i8] c"echo socket (%d) : <%ld> %s.\0A\00"
define void @str_echo(i32 %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = call i32 @bzero(i8* %3, i32 256)
br label %5
5: ; preds = %21, %1
%6 = call i64 @read(i32 %0, i8* %3, i32 255)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %5
%9 = load i64, i64* @EINTR
%10 = load i64, i64* @errno
%11 = icmp eq i64 %9, %10
br i1 %11, label %21, label %12
12: ; preds = %8
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %18
14: ; preds = %5
%15 = icmp eq i64 %6, 0
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = call i32 @close(i32 %0)
ret void
18: ; preds = %14, %12
%19 = call i32 @write(i32 %0, i8* %3, i64 %6)
%20 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %0, i64 %6, i8* %3)
br label %21
21: ; preds = %18, %8
br label %5
}