#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 *, char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
scalar_t__ write(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) {
      if (write(sockfd, buf, n) != n) {
        fprintf(stderr, "str_echo() - write() failed: %s\n", strerror(errno));
        break;
      }
    } else if (n < 0 && errno == EINTR) {
      continue;
    } else if (n < 0) {
      fprintf(stderr, "str_echo() - read() failed: %s\n", strerror(errno));
      break;
    } else if (n == 0) {
      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   1040 <read@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    11cb <str_echo+0x8b>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x10(%rbp),%rax
je     11c6 <str_echo+0x86>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x414(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x414(%rbp),%edi
mov    %rax,%rdx
lea    0xe44(%rip),%rsi
call   1050 <fprintf@plt>
jmp    125c <str_echo+0x11c>
jmp    1257 <str_echo+0x117>
cmpq   $0x0,-0x10(%rbp)
jge    11f5 <str_echo+0xb5>
mov    0x2dfb(%rip),%rax
mov    (%rax),%rax
mov    0x2de9(%rip),%rcx
cmp    (%rcx),%rax
jne    11f5 <str_echo+0xb5>
jmp    114e <str_echo+0xe>
cmpq   $0x0,-0x10(%rbp)
jge    1238 <str_echo+0xf8>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x418(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x418(%rbp),%edi
mov    %rax,%rdx
lea    0xdf3(%rip),%rsi
call   1050 <fprintf@plt>
jmp    125c <str_echo+0x11c>
cmpq   $0x0,-0x10(%rbp)
jne    1248 <str_echo+0x108>
jmp    125c <str_echo+0x11c>
jmp    124d <str_echo+0x10d>
jmp    1252 <str_echo+0x112>
jmp    1257 <str_echo+0x117>
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    1185 <str_echo+0x45>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rax,%rbx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    %rbx,%rax
jne    11bb <str_echo+0x7b>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
test   %rax,%rax
jg     1170 <str_echo+0x30>
jns    11ec <str_echo+0xac>
mov    (%r15),%rdi
cmp    (%r12),%rdi
je     1185 <str_echo+0x45>
mov    0x2e35(%rip),%rax
mov    (%rax),%ebx
call   1060 <strerror@plt>
lea    0xe68(%rip),%rsi
jmp    11d3 <str_echo+0x93>
mov    0x2e1e(%rip),%rax
mov    (%rax),%ebx
mov    (%r15),%rdi
call   1060 <strerror@plt>
lea    0xe2d(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fprintf@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"str_echo() - write() failed: %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [32 x i8] c"str_echo() - read() failed: %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, %31, %47
%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 %24
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 i64 @write(i32 %12, i8* %13, i64 %14)
%16 = load i64, i64* %3
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %23
18: ; preds = %11
%19 = load i32, i32* @stderr
%20 = load i64, i64* @errno
%21 = call i8* @strerror(i64 %20)
%22 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %48
23: ; preds = %11
br label %47
24: ; preds = %5
%25 = load i64, i64* %3
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
br label %5
32: ; preds = %27, %24
%33 = load i64, i64* %3
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i32, i32* @stderr
%37 = load i64, i64* @errno
%38 = call i8* @strerror(i64 %37)
%39 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %38)
br label %48
40: ; preds = %32
%41 = load i64, i64* %3
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %40
br label %48
44: ; preds = %40
br label %45
45: ; preds = %44
br label %46
46: ; preds = %45
br label %47
47: ; preds = %46, %23
br label %5
48: ; preds = %43, %35, %18
ret void
}

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