#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*/ DieWithError(char *);
int /*<<< orphan*/ close(int);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
void HandleTCPClient(int clntSocket) {
  char echoBuffer[32];
  int recvMsgSize;
  if ((recvMsgSize = recv(clntSocket, echoBuffer, 32, 0)) < 0)
    DieWithError("recv() failed");
  while (recvMsgSize > 0) {
    if (send(clntSocket, echoBuffer, recvMsgSize, 0) != recvMsgSize)
      DieWithError("send() failed");
    if ((recvMsgSize = recv(clntSocket, echoBuffer, 32, 0)) < 0)
      DieWithError("recv() failed");
  }
  close(clntSocket);
}


x86 assembly (clang, -O0):
<HandleTCPClient>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x20,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    1176 <HandleTCPClient+0x36>
lea    0xe8f(%rip),%rdi
call   1060 <DieWithError@plt>
jmp    117b <HandleTCPClient+0x3b>
cmpl   $0x0,-0x34(%rbp)
jle    11db <HandleTCPClient+0x9b>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    -0x34(%rbp),%eax
je     11ab <HandleTCPClient+0x6b>
lea    0xe68(%rip),%rdi
call   1060 <DieWithError@plt>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x20,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    11d6 <HandleTCPClient+0x96>
lea    0xe2f(%rip),%rdi
call   1060 <DieWithError@plt>
jmp    117b <HandleTCPClient+0x3b>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HandleTCPClient>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
mov    %rsp,%rsi
mov    $0x20,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     11a5 <HandleTCPClient+0x65>
je     11b1 <HandleTCPClient+0x71>
mov    %eax,%ebp
mov    %rsp,%r14
lea    0xea1(%rip),%r15
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    %ebp,%eax
je     118a <HandleTCPClient+0x4a>
mov    %r15,%rdi
call   1060 <DieWithError@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x20,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     11a5 <HandleTCPClient+0x65>
mov    %eax,%ebp
jne    1170 <HandleTCPClient+0x30>
jmp    11b1 <HandleTCPClient+0x71>
lea    0xe54(%rip),%rdi
call   1060 <DieWithError@plt>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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