#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;
typedef int /*<<< orphan*/ readbuff;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t, size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
size_t recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ remove_client(int, int, int);
size_t send(int, char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
void handle_client_req(int epoll_fd, int client_fd) {
  static char readbuff[1024];
  ssize_t n;
  if ((n = recv(client_fd, readbuff, sizeof(readbuff) - 1, 0)) < 0) {
    perror("recv(2) failed");
    return;
  }
  readbuff[n] = '\0';
  if (n == 0) {
    remove_client(epoll_fd, client_fd, 1);
    return;
  }
  if (!strcmp(readbuff, "QUIT\r\n")) {
    remove_client(epoll_fd, client_fd, 0);
    return;
  }
  printf("*** Received: %s", readbuff);
  ssize_t to_send = n;
  if ((to_send = send(client_fd, readbuff, n, 0)) != n) {
    if (to_send < 0) {
      perror("write(2) failed");
    } else {
      fprintf(stderr, "Partial write detected (%zd/%zd)\n", to_send, n);
    }
    return;
  }
  printf("*** Echoed: %s", readbuff);
}


x86 assembly (clang, -O0):
<handle_client_req>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    0x2ee8(%rip),%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jae    11b3 <handle_client_req+0x43>
lea    0xe57(%rip),%rdi
call   1090 <perror@plt>
jmp    129f <handle_client_req+0x12f>
mov    -0x10(%rbp),%rcx
lea    0x2eb2(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x10(%rbp)
jne    11e2 <handle_client_req+0x72>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    $0x1,%edx
call   1080 <remove_client@plt>
jmp    129f <handle_client_req+0x12f>
lea    0x2e87(%rip),%rdi
lea    0xe1f(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1210 <handle_client_req+0xa0>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
xor    %edx,%edx
call   1080 <remove_client@plt>
jmp    129f <handle_client_req+0x12f>
lea    0xdff(%rip),%rdi
lea    0x2e52(%rip),%rsi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rdx
lea    0x2e37(%rip),%rsi
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %rax,-0x18(%rbp)
cmp    -0x10(%rbp),%rax
je     128c <handle_client_req+0x11c>
cmpq   $0x0,-0x18(%rbp)
jae    126a <handle_client_req+0xfa>
lea    0xdc7(%rip),%rdi
call   1090 <perror@plt>
jmp    1287 <handle_client_req+0x117>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdb5(%rip),%rsi
call   1070 <fprintf@plt>
jmp    129f <handle_client_req+0x12f>
lea    0xdc6(%rip),%rdi
lea    0x2dd6(%rip),%rsi
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_client_req>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %edi,%r14d
lea    0x2efc(%rip),%rbp
mov    %esi,%edi
mov    %rbp,%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
movb   $0x0,(%rax,%rbp,1)
test   %rax,%rax
je     11e3 <handle_client_req+0x93>
mov    %rax,%rbx
mov    $0x54495551,%eax
xor    0x2ed4(%rip),%eax
mov    $0xa0d54,%ecx
xor    0x2ecc(%rip),%ecx
or     %eax,%ecx
je     11f0 <handle_client_req+0xa0>
lea    0xe65(%rip),%rdi
lea    0x2eb7(%rip),%rbp
mov    %rbp,%rsi
call   1050 <printf@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    %rbx,%rax
jne    1207 <handle_client_req+0xb7>
lea    0xe6d(%rip),%rdi
lea    0x2e8c(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>
mov    %r14d,%edi
mov    %r15d,%esi
mov    $0x1,%edx
jmp    11f8 <handle_client_req+0xa8>
mov    %r14d,%edi
mov    %r15d,%esi
xor    %edx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <remove_client@plt>
mov    0x2dd2(%rip),%rcx
mov    (%rcx),%edi
lea    0xe01(%rip),%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fprintf@plt>

LLVM IR (-O0):
@handle_client_req.readbuff = internal global [1024 x i8] zeroinitializer
@.str = [15 x i8] c"recv(2) failed\00"
@.str.1 = [7 x i8] c"QUIT\0D\0A\00"
@.str.2 = [17 x i8] c"*** Received: %s\00"
@.str.3 = [16 x i8] c"write(2) failed\00"
@stderr = global i32 0
@.str.4 = [34 x i8] c"Partial write detected (%zd/%zd)\0A\00"
@.str.5 = [15 x i8] c"*** Echoed: %s\00"
define void @handle_client_req(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = call i64 @recv(i32 %7, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client_req.readbuff, i64 0, i64 0), i32 1023, i32 0)
store i64 %8, i64* %5
%9 = icmp ult i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %49
12: ; preds = %2
%13 = load i64, i64* %5
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* @handle_client_req.readbuff, i64 0, i64 %13
store i8 0, i8* %14
%15 = load i64, i64* %5
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load i32, i32* %3
%19 = load i32, i32* %4
%20 = call i32 @remove_client(i32 %18, i32 %19, i32 1)
br label %49
21: ; preds = %12
%22 = call i32 @strcmp(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client_req.readbuff, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp ne i32 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %21
%25 = load i32, i32* %3
%26 = load i32, i32* %4
%27 = call i32 @remove_client(i32 %25, i32 %26, i32 0)
br label %49
28: ; preds = %21
%29 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client_req.readbuff, i64 0, i64 0))
%30 = load i64, i64* %5
store i64 %30, i64* %6
%31 = load i32, i32* %4
%32 = load i64, i64* %5
%33 = call i64 @send(i32 %31, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client_req.readbuff, i64 0, i64 0), i64 %32, i32 0)
store i64 %33, i64* %6
%34 = load i64, i64* %5
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %47
36: ; preds = %28
%37 = load i64, i64* %6
%38 = icmp ult i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %46
41: ; preds = %36
%42 = load i32, i32* @stderr
%43 = load i64, i64* %6
%44 = load i64, i64* %5
%45 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i64 %43, i64 %44)
br label %46
46: ; preds = %41, %39
br label %49
47: ; preds = %28
%48 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client_req.readbuff, i64 0, i64 0))
br label %49
49: ; preds = %47, %46, %24, %17, %10
ret void
}

LLVM IR (-O2):
@handle_client_req.readbuff = internal global [1024 x i8] zeroinitializer
@.str.1 = [7 x i8] c"QUIT\0D\0A\00"
@.str.2 = [17 x i8] c"*** Received: %s\00"
@stderr = global i32 0
@.str.4 = [34 x i8] c"Partial write detected (%zd/%zd)\0A\00"
@.str.5 = [15 x i8] c"*** Echoed: %s\00"
define void @handle_client_req(i32 %0, i32 %1) {
%3 = tail call i64 @recv(i32 %1, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client_req.readbuff, i64 0, i64 0), i32 1023, i32 0)
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* @handle_client_req.readbuff, i64 0, i64 %3
store i8 0, i8* %4
%5 = icmp eq i64 %3, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @remove_client(i32 %0, i32 %1, i32 1)
br label %22
8: ; preds = %2
%9 = tail call i32 @bcmp(i8* dereferenceable(7) getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client_req.readbuff, i64 0, i64 0), i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 7)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @remove_client(i32 %0, i32 %1, i32 0)
br label %22
13: ; preds = %8
%14 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client_req.readbuff, i64 0, i64 0))
%15 = tail call i64 @send(i32 %1, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client_req.readbuff, i64 0, i64 0), i64 %3, i32 0)
%16 = icmp eq i64 %15, %3
br i1 %16, label %20, label %17
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i64 %15, i64 %3)
br label %22
20: ; preds = %13
%21 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @handle_client_req.readbuff, i64 0, i64 0))
br label %22
22: ; preds = %17, %20, %11, %6
ret void
}