#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;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, scalar_t__);
void str_echo(int sockfd) {
  ssize_t n;
  char buf[1024];
again:
  while ((n = read(sockfd, buf, sizeof(buf))) > 0)
    write(sockfd, buf, n);
  if (n < 0 && errno == EINTR)
    goto again;
  else if (n < 0)
    perror("read() faileds");
}


x86 assembly (clang, -O0):
<str_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %edi,-0x4(%rbp)
jmp    1143 <str_echo+0x13>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jle    117d <str_echo+0x4d>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <write@plt>
jmp    1143 <str_echo+0x13>
cmpq   $0x0,-0x10(%rbp)
jge    11a7 <str_echo+0x77>
mov    0x2e51(%rip),%rax
mov    (%rax),%rax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
jne    11a7 <str_echo+0x77>
jmp    113e <str_echo+0xe>
cmpq   $0x0,-0x10(%rbp)
jge    11be <str_echo+0x8e>
lea    0xe47(%rip),%rdi
call   1050 <perror@plt>
jmp    11c3 <str_echo+0x93>
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_echo>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %edi,%ebx
mov    %rsp,%r14
mov    0x2e96(%rip),%r15
mov    0x2e87(%rip),%r12
jmp    116d <str_echo+0x3d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
test   %rax,%rax
jg     1160 <str_echo+0x30>
test   %rax,%rax
jns    11a9 <str_echo+0x79>
mov    (%r15),%rax
cmp    (%r12),%rax
je     116d <str_echo+0x3d>
lea    0xe6a(%rip),%rdi
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <perror@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [15 x i8] c"read() faileds\00"
define void @str_echo(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca [1024 x i8]
store i32 %0, i32* %2
br label %5
5: ; preds = %23, %1
br label %6
6: ; preds = %11, %5
%7 = load i32, i32* %2
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = call i64 @read(i32 %7, i8* %8, i32 1024)
store i64 %9, i64* %3
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = load i32, i32* %2
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%14 = load i64, i64* %3
%15 = call i32 @write(i32 %12, i8* %13, i64 %14)
br label %6
16: ; preds = %6
%17 = load i64, i64* %3
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %19
br label %5
24: ; preds = %19, %16
%25 = load i64, i64* %3
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
br label %30
30: ; preds = %29
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [15 x i8] c"read() faileds\00"
define void @str_echo(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 = %15, %1
%5 = call i64 @read(i32 %0, i8* %3, i32 1024)
%6 = icmp sgt i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %4, %7
%8 = phi i64 [ %10, %7 ], [ %5, %4 ]
%9 = call i32 @write(i32 %0, i8* %3, i64 %8)
%10 = call i64 @read(i32 %0, i8* %3, i32 1024)
%11 = icmp sgt i64 %10, 0
br i1 %11, label %7, label %12
12: ; preds = %7, %4
%13 = phi i64 [ %5, %4 ], [ %10, %7 ]
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %4, label %19
19: ; preds = %15
%20 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %12, %19
ret void
}