#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 *, char *);
size_t recv(int, char *, int, int /*<<< orphan*/);
size_t send(int, char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ sysError(char *);
int echoService(int clntSock) {
  char buff[1000];
  ssize_t numBytesRcvd = recv(clntSock, buff, 1000, 0);
  if (numBytesRcvd < 0)
    sysError("failed to receive data from socket stream");
  else if (numBytesRcvd == 0)
    sysError("stream connection died prematurely");
  buff[numBytesRcvd] = '\0';
  fprintf(stdout, "Received: %s\n", buff);
  ssize_t numBytesSent = send(clntSock, buff, numBytesRcvd, 0);
  if (numBytesSent < 0)
    sysError("failed to send data to socket stream");
  else if (numBytesSent != numBytesRcvd)
    sysError("sent unexpected amount of bytes");
  fprintf(stdout, "Sent: %s\n", buff);
  numBytesRcvd = recv(clntSock, buff, 1000, 0);
  if (numBytesRcvd < 0)
    sysError("failed to receive extra data from socket stream");
}


x86 assembly (clang, -O0):
<echoService>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x3f0(%rbp),%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x3f8(%rbp)
cmpq   $0x0,-0x3f8(%rbp)
jae    118a <echoService+0x4a>
lea    0xe80(%rip),%rdi
call   1040 <sysError@plt>
jmp    11a9 <echoService+0x69>
cmpq   $0x0,-0x3f8(%rbp)
jne    11a4 <echoService+0x64>
lea    0xe8b(%rip),%rdi
call   1040 <sysError@plt>
jmp    11a9 <echoService+0x69>
mov    -0x3f8(%rbp),%rax
movb   $0x0,-0x3f0(%rbp,%rax,1)
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    -0x3f0(%rbp),%rdx
lea    0xe7e(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%edi
lea    -0x3f0(%rbp),%rsi
mov    -0x3f8(%rbp),%rdx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %rax,-0x400(%rbp)
cmpq   $0x0,-0x400(%rbp)
jae    1212 <echoService+0xd2>
lea    0xe53(%rip),%rdi
call   1040 <sysError@plt>
jmp    1237 <echoService+0xf7>
mov    -0x400(%rbp),%rax
cmp    -0x3f8(%rbp),%rax
je     1232 <echoService+0xf2>
lea    0xe53(%rip),%rdi
call   1040 <sysError@plt>
jmp    1237 <echoService+0xf7>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    -0x3f0(%rbp),%rdx
lea    0xe52(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%edi
lea    -0x3f0(%rbp),%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x3f8(%rbp)
cmpq   $0x0,-0x3f8(%rbp)
jae    128a <echoService+0x14a>
lea    0xe25(%rip),%rdi
call   1040 <sysError@plt>
mov    -0x4(%rbp),%eax
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echoService>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x3e8,%rsp
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1172 <echoService+0x32>
lea    0xe93(%rip),%rdi
call   1040 <sysError@plt>
movb   $0x0,(%rsp,%rbx,1)
mov    0x2e63(%rip),%r15
mov    (%r15),%edi
lea    0xe9c(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    %rbx,%rax
je     11b2 <echoService+0x72>
lea    0xe84(%rip),%rdi
call   1040 <sysError@plt>
mov    (%r15),%edi
lea    0xe95(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
add    $0x3e8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str.1 = [35 x i8] c"stream connection died prematurely\00"
@stdout = global i32 0
@.str.2 = [14 x i8] c"Received: %s\0A\00"
@.str.4 = [32 x i8] c"sent unexpected amount of bytes\00"
@.str.5 = [10 x i8] c"Sent: %s\0A\00"
define i32 @echoService(i32 %0) {
%2 = alloca [1000 x i8]
%3 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%4 = call i64 @recv(i32 %0, i8* %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 ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %1, %6
%9 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 %4
store i8 0, i8* %9
%10 = load i32, i32* @stdout
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%12 = call i64 @send(i32 %0, i8* %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 ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %16
16: ; preds = %8, %14
%17 = load i32, i32* @stdout
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %3)
%19 = call i64 @recv(i32 %0, i8* %3, i32 1000, i32 0)
ret i32 undef
}