#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
char *malloc(int);
int /*<<< orphan*/ printErrorAndQuit(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
void echoMessage(int clntSocket) {
  char *echoBuffer = malloc(1024 * 1024);
  int recvMsgSize;
  if ((recvMsgSize = recv(clntSocket, echoBuffer, 1024 * 1024, 0)) < 0)
    printErrorAndQuit("could not receive message!");
  while (recvMsgSize > 0) {
    if (send(clntSocket, echoBuffer, recvMsgSize, 0) != recvMsgSize)
      printErrorAndQuit("send() failed");
    if ((recvMsgSize = recv(clntSocket, echoBuffer, 1024 * 1024, 0)) < 0)
      printErrorAndQuit("recv() failed");
  }
  close(clntSocket);
}


x86 assembly (clang, -O0):
<echoMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x100000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x100000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1194 <echoMessage+0x44>
lea    0xe71(%rip),%rdi
call   1060 <printErrorAndQuit@plt>
jmp    1199 <echoMessage+0x49>
cmpl   $0x0,-0x14(%rbp)
jle    11f9 <echoMessage+0xa9>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    -0x14(%rbp),%eax
je     11c9 <echoMessage+0x79>
lea    0xe57(%rip),%rdi
call   1060 <printErrorAndQuit@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x100000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11f4 <echoMessage+0xa4>
lea    0xe3a(%rip),%rdi
call   1060 <printErrorAndQuit@plt>
jmp    1199 <echoMessage+0x49>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<echoMessage>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r12d
mov    $0x100000,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %r12d,%edi
mov    %rax,%rsi
mov    $0x100000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     11c7 <echoMessage+0x77>
je     11d6 <echoMessage+0x86>
mov    %eax,%ebp
lea    0xea0(%rip),%r14
lea    0xe8b(%rip),%r15
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    %ebp,%eax
je     11ab <echoMessage+0x5b>
mov    %r15,%rdi
call   1060 <printErrorAndQuit@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x100000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     11ce <echoMessage+0x7e>
mov    %eax,%ebp
jne    1190 <echoMessage+0x40>
jmp    11d6 <echoMessage+0x86>
lea    0xe32(%rip),%r14
mov    %r14,%rdi
call   1060 <printErrorAndQuit@plt>
mov    %r12d,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <close@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"could not receive message!\00"
@.str.1 = [14 x i8] c"send() failed\00"
@.str.2 = [14 x i8] c"recv() failed\00"
define void @echoMessage(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i8* @malloc(i32 1048576)
store i8* %5, i8** %3
%6 = load i32, i32* %2
%7 = load i8*, i8** %3
%8 = call i32 @recv(i32 %6, i8* %7, i32 1048576, i32 0)
store i32 %8, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @printErrorAndQuit(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
br label %13
13: ; preds = %32, %12
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %33
16: ; preds = %13
%17 = load i32, i32* %2
%18 = load i8*, i8** %3
%19 = load i32, i32* %4
%20 = call i32 @send(i32 %17, i8* %18, i32 %19, i32 0)
%21 = load i32, i32* %4
%22 = icmp ne i32 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = call i32 @printErrorAndQuit(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %16
%26 = load i32, i32* %2
%27 = load i8*, i8** %3
%28 = call i32 @recv(i32 %26, i8* %27, i32 1048576, i32 0)
store i32 %28, i32* %4
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @printErrorAndQuit(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %25
br label %13
33: ; preds = %13
%34 = load i32, i32* %2
%35 = call i32 @close(i32 %34)
ret void
}

LLVM IR (-O2):
@.str = [27 x i8] c"could not receive message!\00"
@.str.1 = [14 x i8] c"send() failed\00"
@.str.2 = [14 x i8] c"recv() failed\00"
define void @echoMessage(i32 %0) {
%2 = tail call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%3 = tail call i32 @recv(i32 %0, i8* %2, i32 1048576, i32 0)
%4 = icmp slt i32 %3, 0
br i1 %4, label %18, label %5
5: ; preds = %1
%6 = icmp eq i32 %3, 0
br i1 %6, label %21, label %7
7: ; preds = %5, %16
%8 = phi i32 [ %14, %16 ], [ %3, %5 ]
%9 = tail call i32 @send(i32 %0, i8* %2, i32 %8, i32 0)
%10 = icmp eq i32 %9, %8
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i32 @printErrorAndQuit(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %7
%14 = tail call i32 @recv(i32 %0, i8* %2, i32 1048576, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = icmp eq i32 %14, 0
br i1 %17, label %21, label %7
18: ; preds = %13, %1
%19 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %13 ]
%20 = tail call i32 @printErrorAndQuit(i8* %19)
br label %21
21: ; preds = %16, %18, %5
%22 = tail call i32 @close(i32 %0)
ret void
}