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


x86 assembly (clang, -O0):
<str_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x2,%esi
mov    $0x5,%edx
call   1030 <heart_beat_serv@plt>
jmp    1175 <str_echo+0x25>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jle    11af <str_echo+0x5f>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1060 <writen@plt>
jmp    1175 <str_echo+0x25>
cmpq   $0x0,-0x10(%rbp)
jge    11d9 <str_echo+0x89>
mov    0x2e1f(%rip),%rax
mov    (%rax),%rax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
jne    11d9 <str_echo+0x89>
jmp    1170 <str_echo+0x20>
cmpq   $0x0,-0x10(%rbp)
jge    11fa <str_echo+0xaa>
lea    0xe15(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    11ff <str_echo+0xaf>
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    $0x2,%esi
mov    $0x5,%edx
call   1030 <heart_beat_serv@plt>
mov    %rsp,%r14
mov    0x2e67(%rip),%r15
mov    0x2e58(%rip),%r12
jmp    119d <str_echo+0x4d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1060 <writen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
test   %rax,%rax
jg     1190 <str_echo+0x40>
test   %rax,%rax
jns    11d5 <str_echo+0x85>
mov    (%r15),%rax
cmp    (%r12),%rax
je     119d <str_echo+0x4d>
lea    0xe3a(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@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 = [14 x i8] c"read() failed\00"
define void @str_echo(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca [1024 x i8]
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i32 @heart_beat_serv(i32 %5, i32 2, i32 5)
br label %7
7: ; preds = %25, %1
br label %8
8: ; preds = %13, %7
%9 = load i32, i32* %2
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%11 = call i64 @read(i32 %9, i8* %10, i32 1024)
store i64 %11, i64* %3
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %8
%14 = load i32, i32* %2
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%16 = load i64, i64* %3
%17 = call i32 @writen(i32 %14, i8* %15, i64 %16)
br label %8
18: ; preds = %8
%19 = load i64, i64* %3
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
br label %7
26: ; preds = %21, %18
%27 = load i64, i64* %3
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %26
br label %33
33: ; preds = %32
ret void
}

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