// File: SeTriones[P]Anubis/receiver.cpp
// Function: receiver::handle_request(int, int, char*)
int receiver::handle_request(int fd, int epoll_fd, char* buf) {
	int need_to_read = sizeof(request);
	timeval timeout = {0, 50*1000};
	int ret;
	ret = readn_timeout(fd, buf, need_to_read, &timeout);
	if (ret != need_to_read) {
		close_fd(fd);
		return -1;
	}
	request* req = (request*)buf;
	need_to_read = ntohl(req->key_len) + ntohl(req->body_len);
	ret = readn_timeout(fd, buf + sizeof(uint32_t) * 2, need_to_read, &timeout);
	if (ret != need_to_read) {
		close_fd(fd);
		return -1;
	}
	uint64_t key;
	uint8_t section_id;
	key = get_raw_key(buf, &section_id);
	KeyGroup* key_group = key_groups[section_id % key_group_cnt];
	int cnt = key_group->Inc(key);
	uint32_t ucnt = (uint32_t)cnt;
	_INFO("get key=%lu,seciond_id=%u,cnt=%u", key, section_id, ucnt);
	int wcnt = writen_timeout(fd, &endian, sizeof(uint8_t), 10);
	_INFO("wcnt=%d", wcnt);
	wcnt = writen_timeout(fd, &ucnt, sizeof(uint32_t), 10);
	_INFO("wcnt=%d", wcnt);
	add_input_fd(fd, epoll_fd);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x190(%rbp)
movl $0x8, -0x24(%rbp)
movq 0x143e(%rip), %rax      # 0x9620
movq %rax, -0x38(%rbp)
movq 0x143b(%rip), %rax      # 0x9628
movq %rax, -0x30(%rbp)
addq $0x38, %rdi
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
leaq -0x38(%rbp), %r8
callq 0x39e0
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x24(%rbp), %eax
je 0x8232
movq -0x190(%rbp), %rdi
addq $0x40, %rdi
movl -0x14(%rbp), %esi
callq 0x3080
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8542
movq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl (%rax), %edi
callq 0x2290
movl %eax, -0x194(%rbp)
movq -0x48(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x2290
movq -0x190(%rbp), %rdi
movl %eax, %ecx
movl -0x194(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x24(%rbp)
addq $0x38, %rdi
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
addq $0x8, %rdx
movl -0x24(%rbp), %ecx
leaq -0x38(%rbp), %r8
callq 0x39e0
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x24(%rbp), %eax
je 0x82b0
movq -0x190(%rbp), %rdi
addq $0x40, %rdi
movl -0x14(%rbp), %esi
callq 0x3080
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8542
movq -0x20(%rbp), %rdi
leaq -0x51(%rbp), %rsi
callq 0x8650
movq -0x190(%rbp), %rcx
movq %rax, -0x50(%rbp)
movq 0xf8(%rcx), %rax
movq %rax, -0x1d0(%rbp)
movzbl -0x51(%rbp), %eax
cltd
idivl 0xec(%rcx)
movq -0x1d0(%rbp), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x4580
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x68(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x21e0
leaq -0x70(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x2120
leaq -0xc0(%rbp), %rdi
movl $0x12, %esi
leaq 0xccf(%rip), %rdx       # 0x9004
leaq -0xa8(%rbp), %rcx
callq 0x2190
movq 0x4c90(%rip), %rax      # 0xcfd8
movq (%rax), %rax
movq %rax, -0x1c8(%rbp)
leaq -0xc0(%rbp), %rax
movq %rax, -0x1c0(%rbp)
callq 0x21c0
movq -0x1c8(%rbp), %rdi
movq -0x1c0(%rbp), %rdx
movq %rax, %rcx
movq -0x50(%rbp), %r9
movzbl -0x51(%rbp), %r10d
movl -0x68(%rbp), %eax
leaq 0x11fc(%rip), %rsi      # 0x9585
leaq 0xdeb(%rip), %r8        # 0x917b
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x2310
movq -0x190(%rbp), %rdx
movl %eax, -0xc4(%rbp)
movq %rdx, %rdi
addq $0x38, %rdi
movl -0x14(%rbp), %esi
addq $0xf4, %rdx
movl $0x1, %ecx
movl $0xa, %r8d
callq 0x3b20
movl %eax, -0xc8(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x21e0
leaq -0xd0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x2120
leaq -0x120(%rbp), %rdi
movl $0x12, %esi
leaq 0xbff(%rip), %rdx       # 0x9004
leaq -0x108(%rbp), %rcx
callq 0x2190
movq 0x4bc0(%rip), %rax      # 0xcfd8
movq (%rax), %rax
movq %rax, -0x1b8(%rbp)
leaq -0x120(%rbp), %rax
movq %rax, -0x1b0(%rbp)
callq 0x21c0
movq -0x1b8(%rbp), %rdi
movq -0x1b0(%rbp), %rdx
movq %rax, %rcx
movl -0xc8(%rbp), %r9d
leaq 0x116a(%rip), %rsi      # 0x95be
leaq 0xd20(%rip), %r8        # 0x917b
movb $0x0, %al
callq 0x2310
movq -0x190(%rbp), %rdi
movl %eax, -0x124(%rbp)
addq $0x38, %rdi
movl -0x14(%rbp), %esi
leaq -0x68(%rbp), %rdx
movl $0x4, %ecx
movl $0xa, %r8d
callq 0x3b20
movl %eax, -0xc8(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x21e0
leaq -0x130(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x2120
leaq -0x180(%rbp), %rdi
movl $0x12, %esi
leaq 0xb42(%rip), %rdx       # 0x9004
leaq -0x168(%rbp), %rcx
callq 0x2190
movq 0x4b03(%rip), %rax      # 0xcfd8
movq (%rax), %rax
movq %rax, -0x1a8(%rbp)
leaq -0x180(%rbp), %rax
movq %rax, -0x1a0(%rbp)
callq 0x21c0
movq -0x1a8(%rbp), %rdi
movq -0x1a0(%rbp), %rdx
movq %rax, %rcx
movl -0xc8(%rbp), %r9d
leaq 0x10ad(%rip), %rsi      # 0x95be
leaq 0xc63(%rip), %r8        # 0x917b
movb $0x0, %al
callq 0x2310
movq -0x190(%rbp), %rdi
movl %eax, -0x184(%rbp)
addq $0x40, %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
callq 0x2f90
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
nop