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


x86 assembly (clang, -O0):
<str_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
jmp    1143 <str_echo+0x13>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %rax,-0x1018(%rbp)
cmp    $0x0,%rax
jle    1183 <str_echo+0x53>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1018(%rbp),%rdx
call   1030 <write@plt>
jmp    1143 <str_echo+0x13>
cmpq   $0x0,-0x1018(%rbp)
jge    11b0 <str_echo+0x80>
mov    0x2e48(%rip),%rax
mov    (%rax),%rax
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%rax
jne    11b0 <str_echo+0x80>
jmp    113e <str_echo+0xe>
cmpq   $0x0,-0x1018(%rbp)
jge    11cf <str_echo+0x9f>
lea    0xe3b(%rip),%rdi
call   1050 <perror@plt>
jmp    11d4 <str_echo+0xa4>
jmp    11d9 <str_echo+0xa9>
jmp    11d9 <str_echo+0xa9>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_echo>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1008,%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    $0x1000,%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    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <perror@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [21 x i8] c"str_echo: read error\00"
define void @str_echo(i32 %0) {
%2 = alloca i32
%3 = alloca [4096 x i8]
%4 = alloca i64
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 [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%9 = call i64 @read(i32 %7, i8* %8, i32 4096)
store i64 %9, i64* %4
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = load i32, i32* %2
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%14 = load i64, i64* %4
%15 = call i32 @write(i32 %12, i8* %13, i64 %14)
br label %6
16: ; preds = %6
%17 = load i64, i64* %4
%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* %4
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %30
29: ; preds = %24
br label %31
30: ; preds = %27
br label %31
31: ; preds = %29, %30
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [21 x i8] c"str_echo: read error\00"
define void @str_echo(i32 %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %15, %1
%5 = call i64 @read(i32 %0, i8* %3, i32 4096)
%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 4096)
%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 ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %12, %19
ret void
}