#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, size_t *, int const, int /*<<< orphan*/);
size_t send(int, size_t *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ sysError(char *);
void serverStream(int servSock) {
  const int BUFF = 1000;
  size_t buff[BUFF];
  ssize_t bytesRcvd = 0;
  bytesRcvd = recv(servSock, buff, BUFF, 0);
  if (bytesRcvd < 0)
    sysError("failed to get data");
  else if (bytesRcvd == 0)
    sysError("connection died prematurely");
  buff[bytesRcvd] = '\0';
  fprintf(stdout, "Received: %s\n", buff);
  ssize_t bytesSent = 0;
  bytesSent = send(servSock, buff, bytesRcvd, 0);
  if (bytesSent < 0)
    sysError("failed to send data");
  else if (bytesSent != bytesRcvd)
    sysError("sent unexpected amount of data");
  fprintf(stdout, "Sent back the string: %d bytes\n\n", bytesSent);
  bytesRcvd = recv(servSock, buff, BUFF, 0);
  if (bytesRcvd < 0)
    sysError("failed to get data");
}


x86 assembly (clang, -O0):
<serverStream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1f60,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x3e8,-0x8(%rbp)
mov    %rsp,%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1f60(%rbp),%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jae    119a <serverStream+0x5a>
lea    0xe70(%rip),%rdi
call   1040 <sysError@plt>
jmp    11b6 <serverStream+0x76>
cmpq   $0x0,-0x18(%rbp)
jne    11b1 <serverStream+0x71>
lea    0xe67(%rip),%rdi
call   1040 <sysError@plt>
jmp    11b6 <serverStream+0x76>
mov    -0x18(%rbp),%rax
movq   $0x0,-0x1f60(%rbp,%rax,8)
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
lea    -0x1f60(%rbp),%rdx
mov    $0x0,%al
call   1060 <fprintf@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rdx
lea    -0x1f60(%rbp),%rsi
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    1221 <serverStream+0xe1>
lea    0xe26(%rip),%rdi
call   1040 <sysError@plt>
jmp    1240 <serverStream+0x100>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     123b <serverStream+0xfb>
lea    0xe1b(%rip),%rdi
call   1040 <sysError@plt>
jmp    1240 <serverStream+0x100>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%edi
lea    -0x1f60(%rbp),%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jae    128c <serverStream+0x14c>
lea    0xd79(%rip),%rdi
call   1040 <sysError@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
add    $0x1f60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serverStream>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1f48,%rsp
mov    %edi,%r14d
mov    %rsp,%rsi
mov    $0x3e8,%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>
movq   $0x0,(%rsp,%rbx,8)
mov    0x2e5e(%rip),%r15
mov    (%r15),%edi
lea    0xe90(%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,%rbp
cmp    %rbx,%rax
je     11bd <serverStream+0x7d>
lea    0xe72(%rip),%rdi
call   1040 <sysError@plt>
mov    (%r15),%edi
lea    0xe82(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x3e8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
add    $0x1f48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"failed to get data\00"
@.str.1 = [28 x i8] c"connection died prematurely\00"
@stdout = global i32 0
@.str.2 = [14 x i8] c"Received: %s\0A\00"
@.str.3 = [20 x i8] c"failed to send data\00"
@.str.4 = [31 x i8] c"sent unexpected amount of data\00"
@.str.5 = [33 x i8] c"Sent back the string: %d bytes\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 1000, i32* %3
%7 = call i8* @llvm.stacksave()
store i8* %7, i8** %4
%8 = alloca i64, i64 1000
store i64 0, i64* %5
%9 = load i32, i32* %2
%10 = call i64 @recv(i32 %9, i64* %8, i32 1000, i32 0)
store i64 %10, i64* %5
%11 = load i64, i64* %5
%12 = icmp ult 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 %21
15: ; preds = %1
%16 = load i64, i64* %5
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @sysError(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %15
br label %21
21: ; preds = %20, %13
%22 = load i64, i64* %5
%23 = getelementptr inbounds i64, i64* %8, i64 %22
store i64 0, i64* %23
%24 = load i32, i32* @stdout
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i64* %8)
store i64 0, i64* %6
%26 = load i32, i32* %2
%27 = load i64, i64* %5
%28 = call i64 @send(i32 %26, i64* %8, i64 %27, i32 0)
store i64 %28, i64* %6
%29 = load i64, i64* %6
%30 = icmp ult i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %21
%32 = call i32 @sysError(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %40
33: ; preds = %21
%34 = load i64, i64* %6
%35 = load i64, i64* %5
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @sysError(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
br label %40
40: ; preds = %39, %31
%41 = load i32, i32* @stdout
%42 = load i64, i64* %6
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i64 %42)
%44 = load i32, i32* %2
%45 = call i64 @recv(i32 %44, i64* %8, i32 1000, i32 0)
store i64 %45, i64* %5
%46 = load i64, i64* %5
%47 = icmp ult i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %40
%49 = call i32 @sysError(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %50
50: ; preds = %48, %40
%51 = load i8*, i8** %4
ret void
}

LLVM IR (-O2):
@.str.1 = [28 x i8] c"connection died prematurely\00"
@stdout = global i32 0
@.str.2 = [14 x i8] c"Received: %s\0A\00"
@.str.4 = [31 x i8] c"sent unexpected amount of data\00"
@.str.5 = [33 x i8] c"Sent back the string: %d bytes\0A\0A\00"
define void @serverStream(i32 %0) {
%2 = alloca [1000 x i64]
%3 = getelementptr inbounds [1000 x i64], [1000 x i64]* %2, i64 0, i64 0
%4 = call i64 @recv(i32 %0, i64* %3, i32 1000, 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 ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %1, %6
%9 = getelementptr inbounds [1000 x i64], [1000 x i64]* %2, i64 0, i64 %4
store i64 0, i64* %9
%10 = load i32, i32* @stdout
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i64* %3)
%12 = call i64 @send(i32 %0, i64* %3, i64 %4, i32 0)
%13 = icmp eq i64 %12, %4
br i1 %13, label %16, label %14
14: ; preds = %8
%15 = call i32 @sysError(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %16
16: ; preds = %8, %14
%17 = load i32, i32* @stdout
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i64 %12)
%19 = call i64 @recv(i32 %0, i64* %3, i32 1000, i32 0)
ret void
}