#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ fd;
};
struct epoll_event {
  int events;
  TYPE_1__ data;
};
typedef int ssize_t;

/* Variables and functions */
int EPOLLERR;
int EPOLLHUP;
int EPOLLIN;
int EPOLLRDHUP;
int /*<<< orphan*/ clear_client(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ handle_message(int /*<<< orphan*/, char *);
char *malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, ...);
int handle_client(struct epoll_event *ev) {
  size_t msg_len = 0;
  ssize_t read_result = 0;
  char *buff = 0;
  if ((ev->events & EPOLLERR) || (ev->events & EPOLLRDHUP) ||
      (ev->events & EPOLLHUP)) {
    printf("ERROR: Bad event on client socket!\n");
    clear_client(ev->data.fd);
  } else if (ev->events & EPOLLIN) {
    read_result = read(ev->data.fd, &msg_len, sizeof(size_t));
    if ((read_result == sizeof(size_t)) && (msg_len > 0)) {
      printf("TRACE: Real client message len: %zu\n", msg_len);
      buff = malloc((msg_len + 1) * sizeof(char));
      buff[msg_len] = 0;
      read_result = read(ev->data.fd, buff, msg_len);
      if (read_result == msg_len) {
        printf("TRACE: Message received \"%s\"\n", buff);
        handle_message(ev->data.fd, buff);
      } else {
        printf("ERROR: Cannot read exact message!\n");
        clear_client(ev->data.fd);
      }
      free(buff);
    } else {
      printf("ERROR: Client message len is wrong, read res: %zu, msg len: %zu "
             "0!\n",
             read_result, msg_len);
      clear_client(ev->data.fd);
    }
  } else {
    printf("ERROR: Unexpected event on client socket!\n");
    clear_client(ev->data.fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<handle_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    0x2e48(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11cb <handle_client+0x6b>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    0x2e28(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11cb <handle_client+0x6b>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    0x2e08(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11ea <handle_client+0x8a>
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <clear_client@plt>
jmp    1323 <handle_client+0x1c3>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    0x2df1(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1304 <handle_client+0x1a4>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
mov    $0x0,%al
call   1070 <read@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x8,%rax
jne    12de <handle_client+0x17e>
cmpq   $0x0,-0x10(%rbp)
jbe    12de <handle_client+0x17e>
mov    -0x10(%rbp),%rsi
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1070 <read@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    12b6 <handle_client+0x156>
mov    -0x20(%rbp),%rsi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x20(%rbp),%rsi
call   1060 <handle_message@plt>
jmp    12d0 <handle_client+0x170>
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <clear_client@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    12ff <handle_client+0x19f>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <clear_client@plt>
jmp    131e <handle_client+0x1be>
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <clear_client@plt>
jmp    1323 <handle_client+0x1c3>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_client>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
mov    (%rdi),%eax
mov    0x2e4a(%rip),%rcx
test   %eax,(%rcx)
jne    11a8 <handle_client+0x38>
mov    0x2e37(%rip),%rcx
test   %eax,(%rcx)
jne    11a8 <handle_client+0x38>
mov    0x2e24(%rip),%rcx
test   %eax,(%rcx)
je     11c8 <handle_client+0x58>
lea    0xf24(%rip),%rdi
call   1050 <puts@plt>
mov    0x4(%rbx),%edi
call   1040 <clear_client@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e19(%rip),%rcx
test   %eax,(%rcx)
je     1258 <handle_client+0xe8>
mov    0x4(%rbx),%edi
lea    0x8(%rsp),%rsi
mov    $0x8,%edx
xor    %eax,%eax
call   1080 <read@plt>
mov    0x8(%rsp),%rdx
cmp    $0x8,%eax
jne    1264 <handle_client+0xf4>
test   %rdx,%rdx
je     1264 <handle_client+0xf4>
lea    0xdff(%rip),%rdi
mov    %rdx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rsp),%r15
lea    0x1(%r15),%rdi
call   1090 <malloc@plt>
mov    %rax,%r14
movb   $0x0,(%rax,%r15,1)
mov    0x4(%rbx),%edi
mov    %rax,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1080 <read@plt>
cltq
cmp    %rax,0x8(%rsp)
jne    1279 <handle_client+0x109>
lea    0xde4(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x4(%rbx),%edi
mov    %r14,%rsi
call   1070 <handle_message@plt>
jmp    128d <handle_client+0x11d>
lea    0xe28(%rip),%rdi
jmp    11af <handle_client+0x3f>
lea    0xdd8(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11b4 <handle_client+0x44>
lea    0xe31(%rip),%rdi
call   1050 <puts@plt>
mov    0x4(%rbx),%edi
call   1040 <clear_client@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    11bc <handle_client+0x4c>

LLVM IR (-O0):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@EPOLLERR = global i32 0
@EPOLLRDHUP = global i32 0
@EPOLLHUP = global i32 0
@.str = [36 x i8] c"ERROR: Bad event on client socket!\0A\00"
@EPOLLIN = global i32 0
@.str.1 = [37 x i8] c"TRACE: Real client message len: %zu\0A\00"
@.str.2 = [30 x i8] c"TRACE: Message received \22%s\22\0A\00"
@.str.3 = [35 x i8] c"ERROR: Cannot read exact message!\0A\00"
@.str.4 = [68 x i8] c"ERROR: Client message len is wrong, read res: %zu, msg len: %zu 0!\0A\00"
@.str.5 = [43 x i8] c"ERROR: Unexpected event on client socket!\0A\00"
define i32 @handle_client(%struct.epoll_event* %0) {
%2 = alloca %struct.epoll_event*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8*
store %struct.epoll_event* %0, %struct.epoll_event** %2
store i64 0, i64* %3
store i32 0, i32* %4
store i8* null, i8** %5
%6 = load %struct.epoll_event*, %struct.epoll_event** %2
%7 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %6, i32 0, i32 0
%8 = load i32, i32* %7
%9 = load i32, i32* @EPOLLERR
%10 = and i32 %8, %9
%11 = icmp ne i32 %10, 0
br i1 %11, label %26, label %12
12: ; preds = %1
%13 = load %struct.epoll_event*, %struct.epoll_event** %2
%14 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @EPOLLRDHUP
%17 = and i32 %15, %16
%18 = icmp ne i32 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %12
%20 = load %struct.epoll_event*, %struct.epoll_event** %2
%21 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = load i32, i32* @EPOLLHUP
%24 = and i32 %22, %23
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %19, %12, %1
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%28 = load %struct.epoll_event*, %struct.epoll_event** %2
%29 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %28, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @clear_client(i32 %31)
br label %110
33: ; preds = %19
%34 = load %struct.epoll_event*, %struct.epoll_event** %2
%35 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = load i32, i32* @EPOLLIN
%38 = and i32 %36, %37
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %102
40: ; preds = %33
%41 = load %struct.epoll_event*, %struct.epoll_event** %2
%42 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %41, i32 0, i32 1
%43 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 (i32, ...) @read(i32 %44, i64* %3, i64 8)
store i32 %45, i32* %4
%46 = load i32, i32* %4
%47 = sext i32 %46 to i64
%48 = icmp eq i64 %47, 8
br i1 %48, label %49, label %92
49: ; preds = %40
%50 = load i64, i64* %3
%51 = icmp ugt i64 %50, 0
br i1 %51, label %52, label %92
52: ; preds = %49
%53 = load i64, i64* %3
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i64 %53)
%55 = load i64, i64* %3
%56 = add i64 %55, 1
%57 = mul i64 %56, 1
%58 = call i8* @malloc(i64 %57)
store i8* %58, i8** %5
%59 = load i8*, i8** %5
%60 = load i64, i64* %3
%61 = getelementptr inbounds i8, i8* %59, i64 %60
store i8 0, i8* %61
%62 = load %struct.epoll_event*, %struct.epoll_event** %2
%63 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %62, i32 0, i32 1
%64 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %63, i32 0, i32 0
%65 = load i32, i32* %64
%66 = load i8*, i8** %5
%67 = load i64, i64* %3
%68 = call i32 (i32, ...) @read(i32 %65, i8* %66, i64 %67)
store i32 %68, i32* %4
%69 = load i32, i32* %4
%70 = sext i32 %69 to i64
%71 = load i64, i64* %3
%72 = icmp eq i64 %70, %71
br i1 %72, label %73, label %82
73: ; preds = %52
%74 = load i8*, i8** %5
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %74)
%76 = load %struct.epoll_event*, %struct.epoll_event** %2
%77 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %76, i32 0, i32 1
%78 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %77, i32 0, i32 0
%79 = load i32, i32* %78
%80 = load i8*, i8** %5
%81 = call i32 @handle_message(i32 %79, i8* %80)
br label %89
82: ; preds = %52
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%84 = load %struct.epoll_event*, %struct.epoll_event** %2
%85 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %84, i32 0, i32 1
%86 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %85, i32 0, i32 0
%87 = load i32, i32* %86
%88 = call i32 @clear_client(i32 %87)
br label %89
89: ; preds = %82, %73
%90 = load i8*, i8** %5
%91 = call i32 @free(i8* %90)
br label %101
92: ; preds = %49, %40
%93 = load i32, i32* %4
%94 = load i64, i64* %3
%95 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.4, i64 0, i64 0), i32 %93, i64 %94)
%96 = load %struct.epoll_event*, %struct.epoll_event** %2
%97 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %96, i32 0, i32 1
%98 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %97, i32 0, i32 0
%99 = load i32, i32* %98
%100 = call i32 @clear_client(i32 %99)
br label %101
101: ; preds = %92, %89
br label %109
102: ; preds = %33
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0))
%104 = load %struct.epoll_event*, %struct.epoll_event** %2
%105 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %104, i32 0, i32 1
%106 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %105, i32 0, i32 0
%107 = load i32, i32* %106
%108 = call i32 @clear_client(i32 %107)
br label %109
109: ; preds = %102, %101
br label %110
110: ; preds = %109, %26
ret i32 0
}

LLVM IR (-O2):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@EPOLLERR = global i32 0
@EPOLLRDHUP = global i32 0
@EPOLLHUP = global i32 0
@EPOLLIN = global i32 0
@.str.1 = [37 x i8] c"TRACE: Real client message len: %zu\0A\00"
@.str.2 = [30 x i8] c"TRACE: Message received \22%s\22\0A\00"
@.str.4 = [68 x i8] c"ERROR: Client message len is wrong, read res: %zu, msg len: %zu 0!\0A\00"
@str = [42 x i8] c"ERROR: Unexpected event on client socket!\00"
@str.6 = [34 x i8] c"ERROR: Cannot read exact message!\00"
@str.7 = [35 x i8] c"ERROR: Bad event on client socket!\00"
define i32 @handle_client(%struct.epoll_event* nocapture %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%4 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %0, i64 0, i32 0
%5 = load i32, i32* %4
%6 = load i32, i32* @EPOLLERR
%7 = and i32 %6, %5
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %1
%10 = load i32, i32* @EPOLLRDHUP
%11 = and i32 %10, %5
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* @EPOLLHUP
%15 = and i32 %14, %5
%16 = icmp eq i32 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %13, %9, %1
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.7, i64 0, i64 0))
%19 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %0, i64 0, i32 1, i32 0
%20 = load i32, i32* %19
%21 = tail call i32 @clear_client(i32 %20)
br label %64
22: ; preds = %13
%23 = load i32, i32* @EPOLLIN
%24 = and i32 %23, %5
%25 = icmp eq i32 %24, 0
br i1 %25, label %59, label %26
26: ; preds = %22
%27 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %0, i64 0, i32 1, i32 0
%28 = load i32, i32* %27
%29 = call i32 (i32, ...) @read(i32 %28, i64* %2, i64 8)
%30 = icmp eq i32 %29, 8
%31 = load i64, i64* %2
%32 = icmp ne i64 %31, 0
%33 = select i1 %30, i1 %32, i1 false
br i1 %33, label %34, label %55
34: ; preds = %26
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i64 %31)
%36 = load i64, i64* %2
%37 = add i64 %36, 1
%38 = call i8* @malloc(i64 %37)
%39 = getelementptr inbounds i8, i8* %38, i64 %36
store i8 0, i8* %39
%40 = load i32, i32* %27
%41 = call i32 (i32, ...) @read(i32 %40, i8* %38, i64 %36)
%42 = sext i32 %41 to i64
%43 = load i64, i64* %2
%44 = icmp eq i64 %43, %42
br i1 %44, label %45, label %49
45: ; preds = %34
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%47 = load i32, i32* %27
%48 = call i32 @handle_message(i32 %47, i8* %38)
br label %53
49: ; preds = %34
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.6, i64 0, i64 0))
%51 = load i32, i32* %27
%52 = call i32 @clear_client(i32 %51)
br label %53
53: ; preds = %49, %45
%54 = call i32 @free(i8* %38)
br label %64
55: ; preds = %26
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([68 x i8], [68 x i8]* @.str.4, i64 0, i64 0), i32 %29, i64 %31)
%57 = load i32, i32* %27
%58 = call i32 @clear_client(i32 %57)
br label %64
59: ; preds = %22
%60 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
%61 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %0, i64 0, i32 1, i32 0
%62 = load i32, i32* %61
%63 = tail call i32 @clear_client(i32 %62)
br label %64
64: ; preds = %59, %55, %53, %17
ret i32 0
}