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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t recv(int, char *, int const, int /*<<< orphan*/);
size_t send(int, char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ sysError(char *);
void serverStream(int clntSock) {
  const int BUFF = 1024;
  char buff[BUFF];
  ssize_t bytesRcvd = 0;
  bytesRcvd = recv(clntSock, buff, BUFF, 0);
  if (bytesRcvd <= 0)
    sysError("failed to get data");
  buff[bytesRcvd] = '\0';
  fprintf(stdout, "Received %d bytes of data\n", bytesRcvd);
  fprintf(stdout, "Data: %s\n", buff);
  ssize_t bytesSent = 0;
  bytesSent = send(clntSock, buff, bytesRcvd, 0);
  if (bytesSent <= 0)
    sysError("failed to send data");
  fprintf(stdout, "Sent back %d bytes of data\n\n", bytesSent);
}


x86 assembly (clang, -O0):
<serverStream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x400,-0x8(%rbp)
mov    %rsp,%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
ja     1195 <serverStream+0x55>
lea    0xe70(%rip),%rdi
call   1040 <sysError@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe5e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
lea    -0x420(%rbp),%rdx
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rdx
lea    -0x420(%rbp),%rsi
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
ja     1212 <serverStream+0xd2>
lea    0xe2b(%rip),%rdi
call   1040 <sysError@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe26(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serverStream>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %edi,%r14d
mov    %rsp,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1173 <serverStream+0x33>
lea    0xe92(%rip),%rdi
call   1040 <sysError@plt>
movb   $0x0,(%rsp,%rbx,1)
mov    0x2e62(%rip),%r15
mov    (%r15),%edi
lea    0xe8b(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%r15),%edi
lea    0xe92(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11cd <serverStream+0x8d>
lea    0xe70(%rip),%rdi
call   1040 <sysError@plt>
mov    (%r15),%edi
lea    0xe75(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"failed to get data\00"
@stdout = global i32 0
@.str.1 = [27 x i8] c"Received %d bytes of data\0A\00"
@.str.2 = [10 x i8] c"Data: %s\0A\00"
@.str.3 = [20 x i8] c"failed to send data\00"
@.str.4 = [29 x i8] c"Sent back %d bytes of data\0A\0A\00"
define void @serverStream(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i32 %0, i32* %2
store i32 1024, i32* %3
%7 = call i8* @llvm.stacksave()
store i8* %7, i8** %4
%8 = alloca i8, i64 1024
store i64 0, i64* %5
%9 = load i32, i32* %2
%10 = call i64 @recv(i32 %9, i8* %8, i32 1024, i32 0)
store i64 %10, i64* %5
%11 = load i64, i64* %5
%12 = icmp ule i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @sysError(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %1
%16 = load i64, i64* %5
%17 = getelementptr inbounds i8, i8* %8, i64 %16
store i8 0, i8* %17
%18 = load i32, i32* @stdout
%19 = load i64, i64* %5
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %19)
%21 = load i32, i32* @stdout
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %8)
store i64 0, i64* %6
%23 = load i32, i32* %2
%24 = load i64, i64* %5
%25 = call i64 @send(i32 %23, i8* %8, i64 %24, i32 0)
store i64 %25, i64* %6
%26 = load i64, i64* %6
%27 = icmp ule i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %15
%29 = call i32 @sysError(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %15
%31 = load i32, i32* @stdout
%32 = load i64, i64* %6
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i64 %32)
%34 = load i8*, i8** %4
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"failed to get data\00"
@stdout = global i32 0
@.str.1 = [27 x i8] c"Received %d bytes of data\0A\00"
@.str.2 = [10 x i8] c"Data: %s\0A\00"
@.str.3 = [20 x i8] c"failed to send data\00"
@.str.4 = [29 x i8] c"Sent back %d bytes of data\0A\0A\00"
define void @serverStream(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i64 @recv(i32 %0, i8* %3, i32 1024, i32 0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = call i32 @sysError(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %4
store i8 0, i8* %9
%10 = load i32, i32* @stdout
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i64 %4)
%12 = load i32, i32* @stdout
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%14 = call i64 @send(i32 %0, i8* %3, i64 %4, i32 0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = call i32 @sysError(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %16, %8
%19 = load i32, i32* @stdout
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i64 %14)
ret void
}