#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;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__, char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int /*<<< orphan*/ writen(int, char *, scalar_t__);
void str_echo(int sockfd) {
  ssize_t n;
  char buf[(1024)];
  while (1) {
    n = read(sockfd, buf, (1024));
    if (n > 0) {
      writen(sockfd, buf, n);
    } else if (n == 0) {
      break;
    } else if (n < 0 && errno != EINTR) {
      fprintf(stderr, "read failed(%d): %s\n", errno, strerror(errno));
      break;
    }
  }
}


x86 assembly (clang, -O0):
<str_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    1189 <str_echo+0x49>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1050 <writen@plt>
jmp    1218 <str_echo+0xd8>
cmpq   $0x0,-0x10(%rbp)
jne    1199 <str_echo+0x59>
jmp    121d <str_echo+0xdd>
cmpq   $0x0,-0x10(%rbp)
jge    120e <str_echo+0xce>
mov    0x2e2d(%rip),%rax
mov    (%rax),%rax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%rax
je     120e <str_echo+0xce>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x41c(%rbp)
mov    0x2e04(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x418(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x41c(%rbp),%edi
mov    -0x418(%rbp),%rdx
mov    %rax,%rcx
lea    0xdfc(%rip),%rsi
call   1040 <fprintf@plt>
jmp    121d <str_echo+0xdd>
jmp    1213 <str_echo+0xd3>
jmp    1218 <str_echo+0xd8>
jmp    114e <str_echo+0xe>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_echo>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %edi,%ebp
mov    %rsp,%r14
mov    0x2e7d(%rip),%r15
mov    0x2e6e(%rip),%r12
jmp    117d <str_echo+0x3d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1050 <writen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1030 <read@plt>
test   %rax,%rax
jg     1170 <str_echo+0x30>
je     11c1 <str_echo+0x81>
mov    (%r15),%rbx
cmp    (%r12),%rbx
je     117d <str_echo+0x3d>
mov    0x2e3d(%rip),%rax
mov    (%rax),%ebp
mov    %rbx,%rdi
call   1060 <strerror@plt>
lea    0xe4c(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [21 x i8] c"read failed(%d): %s\0A\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 = %1, %35
%6 = load i32, i32* %2
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = call i64 @read(i32 %6, i8* %7, i32 1024)
store i64 %8, i64* %3
%9 = load i64, i64* %3
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %5
%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 @writen(i32 %12, i8* %13, i64 %14)
br label %35
16: ; preds = %5
%17 = load i64, i64* %3
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
br label %36
20: ; preds = %16
%21 = load i64, i64* %3
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %20
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %33
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = load i64, i64* @errno
%30 = load i64, i64* @errno
%31 = call i8* @strerror(i64 %30)
%32 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i64 %29, i8* %31)
br label %36
33: ; preds = %23, %20
br label %34
34: ; preds = %33
br label %35
35: ; preds = %34, %11
br label %5
36: ; preds = %27, %19
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [21 x i8] c"read failed(%d): %s\0A\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 %9
7: ; preds = %4
%8 = call i32 @writen(i32 %0, i8* %3, i64 %5)
br label %15
9: ; preds = %4
%10 = icmp eq i64 %5, 0
br i1 %10, label %20, label %11
11: ; preds = %9
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %11, %7
br label %4
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = call i8* @strerror(i64 %12)
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i64 %12, i8* %18)
br label %20
20: ; preds = %9, %16
ret void
}