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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dieWithSystemMessage(char *);
int /*<<< orphan*/ dieWithUserMessage(char *, char *);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ send(int, char *, scalar_t__, int /*<<< orphan*/);
void HandleTCPClient(int clntSocket) {
  char buffer[1024];
  ssize_t numBytesRcvd = recv(clntSocket, buffer, 1024, 0);
  if (numBytesRcvd < 0)
    dieWithSystemMessage("recv() failed");
  while (numBytesRcvd > 0) {
    ssize_t numBytesSent = send(clntSocket, buffer, numBytesRcvd, 0);
    if (numBytesSent < 0)
      dieWithSystemMessage("send() failed");
    else if (numBytesSent != numBytesRcvd)
      dieWithUserMessage("send()", " sent unexpected number of bytes");
    numBytesRcvd = recv(clntSocket, buffer, 1024, 0);
    if (numBytesRcvd < 0)
      dieWithSystemMessage("recv() failed");
  }
  close(clntSocket);
}


x86 assembly (clang, -O0):
<HandleTCPClient>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jge    1195 <HandleTCPClient+0x45>
lea    0xe70(%rip),%rdi
call   1070 <dieWithSystemMessage@plt>
jmp    119a <HandleTCPClient+0x4a>
cmpq   $0x0,-0x418(%rbp)
jle    124e <HandleTCPClient+0xfe>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x418(%rbp),%rdx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jge    11e6 <HandleTCPClient+0x96>
lea    0xe32(%rip),%rdi
call   1070 <dieWithSystemMessage@plt>
jmp    1212 <HandleTCPClient+0xc2>
mov    -0x420(%rbp),%rax
cmp    -0x418(%rbp),%rax
je     120d <HandleTCPClient+0xbd>
lea    0xe1b(%rip),%rdi
lea    0xe1b(%rip),%rsi
call   1040 <dieWithUserMessage@plt>
jmp    1212 <HandleTCPClient+0xc2>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jge    1249 <HandleTCPClient+0xf9>
lea    0xdbc(%rip),%rdi
call   1070 <dieWithSystemMessage@plt>
jmp    119a <HandleTCPClient+0x4a>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HandleTCPClient>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %edi,%ebx
mov    %rsp,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
js     11f5 <HandleTCPClient+0xa5>
je     1201 <HandleTCPClient+0xb1>
mov    %rax,%rbp
mov    %rsp,%r14
lea    0xe84(%rip),%r15
lea    0xe8b(%rip),%r12
lea    0xe8b(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1050 <send@plt>
test   %rax,%rax
js     11d0 <HandleTCPClient+0x80>
cmp    %rbp,%rax
je     11d8 <HandleTCPClient+0x88>
mov    %r12,%rdi
mov    %r13,%rsi
call   1040 <dieWithUserMessage@plt>
jmp    11d8 <HandleTCPClient+0x88>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1070 <dieWithSystemMessage@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
js     11f5 <HandleTCPClient+0xa5>
mov    %rax,%rbp
jne    11a0 <HandleTCPClient+0x50>
jmp    1201 <HandleTCPClient+0xb1>
lea    0xe04(%rip),%rdi
call   1070 <dieWithSystemMessage@plt>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
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"
@.str.2 = [7 x i8] c"send()\00"
@.str.3 = [33 x i8] c" sent unexpected number of bytes\00"
define void @HandleTCPClient(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i64
%5 = alloca i64
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%8 = call i64 @recv(i32 %6, i8* %7, i32 1024, i32 0)
store i64 %8, i64* %4
%9 = load i64, i64* %4
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @dieWithSystemMessage(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
br label %14
14: ; preds = %41, %13
%15 = load i64, i64* %4
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %42
17: ; preds = %14
%18 = load i32, i32* %2
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%20 = load i64, i64* %4
%21 = call i64 @send(i32 %18, i8* %19, i64 %20, i32 0)
store i64 %21, i64* %5
%22 = load i64, i64* %5
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @dieWithSystemMessage(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %33
26: ; preds = %17
%27 = load i64, i64* %5
%28 = load i64, i64* %4
%29 = icmp ne i64 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @dieWithUserMessage(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
br label %33
33: ; preds = %32, %24
%34 = load i32, i32* %2
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%36 = call i64 @recv(i32 %34, i8* %35, i32 1024, i32 0)
store i64 %36, i64* %4
%37 = load i64, i64* %4
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @dieWithSystemMessage(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %41
41: ; preds = %39, %33
br label %14
42: ; preds = %14
%43 = load i32, i32* %2
%44 = call i32 @close(i32 %43)
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"recv() failed\00"
@.str.1 = [14 x i8] c"send() failed\00"
@.str.2 = [7 x i8] c"send()\00"
@.str.3 = [33 x i8] c" sent unexpected number of bytes\00"
define void @HandleTCPClient(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 slt i64 %4, 0
br i1 %5, label %23, label %6
6: ; preds = %1
%7 = icmp eq i64 %4, 0
br i1 %7, label %25, label %8
8: ; preds = %6, %21
%9 = phi i64 [ %19, %21 ], [ %4, %6 ]
%10 = call i64 @send(i32 %0, i8* %3, i64 %9, i32 0)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @dieWithSystemMessage(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %18
14: ; preds = %8
%15 = icmp eq i64 %10, %9
br i1 %15, label %18, label %16
16: ; preds = %14
%17 = call i32 @dieWithUserMessage(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %14, %16, %12
%19 = call i64 @recv(i32 %0, i8* %3, i32 1024, i32 0)
%20 = icmp slt i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = icmp eq i64 %19, 0
br i1 %22, label %25, label %8
23: ; preds = %18, %1
%24 = call i32 @dieWithSystemMessage(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %21, %23, %6
%26 = call i32 @close(i32 %0)
ret void
}