#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*/ die_with_sys_msg(char *);
int /*<<< orphan*/ die_with_user_msg(char *, char *);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ send(int, char *, scalar_t__, int /*<<< orphan*/);
void handle_tcp_client(int client_socket) {
  char buffer[256];
  ssize_t num_bytes_received = recv(client_socket, buffer, 256, 0);
  if (num_bytes_received < 0) {
    die_with_sys_msg("recv() Failed !!!");
  }
  while (num_bytes_received > 0) {
    ssize_t num_bytes_sent = send(client_socket, buffer, num_bytes_received, 0);
    if (num_bytes_sent < 0) {
      die_with_sys_msg("send() Failed !!!");
    } else if (num_bytes_sent != num_bytes_received) {
      die_with_user_msg("send()", "Sent Unexpected Number Of Bytes");
    }
    num_bytes_received = recv(client_socket, buffer, 256, 0);
    if (num_bytes_received < 0) {
      die_with_sys_msg("recv() Failed !!!");
    }
  }
  close(client_socket);
}


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

x86 assembly (clang, -O2):
<handle_tcp_client>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %edi,%ebx
mov    %rsp,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
js     11f5 <handle_tcp_client+0xa5>
je     1201 <handle_tcp_client+0xb1>
mov    %rax,%rbp
mov    %rsp,%r14
lea    0xe88(%rip),%r15
lea    0xe93(%rip),%r12
lea    0xe93(%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 <handle_tcp_client+0x80>
cmp    %rbp,%rax
je     11d8 <handle_tcp_client+0x88>
mov    %r12,%rdi
mov    %r13,%rsi
call   1070 <die_with_user_msg@plt>
jmp    11d8 <handle_tcp_client+0x88>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1040 <die_with_sys_msg@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
js     11f5 <handle_tcp_client+0xa5>
mov    %rax,%rbp
jne    11a0 <handle_tcp_client+0x50>
jmp    1201 <handle_tcp_client+0xb1>
lea    0xe04(%rip),%rdi
call   1040 <die_with_sys_msg@plt>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"recv() Failed !!!\00"
@.str.1 = [18 x i8] c"send() Failed !!!\00"
@.str.2 = [7 x i8] c"send()\00"
@.str.3 = [32 x i8] c"Sent Unexpected Number Of Bytes\00"
define void @handle_tcp_client(i32 %0) {
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca i64
%5 = alloca i64
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%8 = call i64 @recv(i32 %6, i8* %7, i32 256, 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 @die_with_sys_msg(i8* getelementptr inbounds ([18 x i8], [18 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 [256 x i8], [256 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 @die_with_sys_msg(i8* getelementptr inbounds ([18 x i8], [18 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 @die_with_user_msg(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 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 [256 x i8], [256 x i8]* %3, i64 0, i64 0
%36 = call i64 @recv(i32 %34, i8* %35, i32 256, 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 @die_with_sys_msg(i8* getelementptr inbounds ([18 x i8], [18 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 = [18 x i8] c"recv() Failed !!!\00"
@.str.1 = [18 x i8] c"send() Failed !!!\00"
@.str.2 = [7 x i8] c"send()\00"
@.str.3 = [32 x i8] c"Sent Unexpected Number Of Bytes\00"
define void @handle_tcp_client(i32 %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = call i64 @recv(i32 %0, i8* %3, i32 256, 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 @die_with_sys_msg(i8* getelementptr inbounds ([18 x i8], [18 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 @die_with_user_msg(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 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 256, 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 @die_with_sys_msg(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %21, %23, %6
%26 = call i32 @close(i32 %0)
ret void
}