// File: /workspace/github_cpp_makefile_star2_O2/mgronhol[P]Ullur/ullur.cpp
// Function: main
int main( int argc, char **argv ){
	int serversock, clientsock;
	
	struct sockaddr_un address;
	
	int epfd = epoll_create( 1 );
	
	static struct epoll_event ev, events[32]; 
	
	int i, N, addrlen, flags, n;
	
	char buffer[ 4096 ];
	
	bool done = false;
	
	clock_t reporting_t0;

	serversock = socket( AF_UNIX, SOCK_STREAM, 0 );
	unlink( "/tmp/ullur.socket" );
	
	memset( &address, 0, sizeof( struct sockaddr_un ) );
	address.sun_family = AF_UNIX;
	sprintf( address.sun_path, "/tmp/ullur.socket" );
	
	
	bind( serversock, (struct sockaddr *)&address, sizeof( address ) );
	
	listen( serversock, 5 );
	
	ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP | EPOLLRDHUP;
	ev.data.fd = serversock;
	
	epoll_ctl( epfd, EPOLL_CTL_ADD, serversock, &ev );
	
	std::cout << "Server started." << std::endl;
	
	signal( SIGPIPE, SIG_IGN );
	load_append_log();
	append_log = fopen( "ullur.dump", "a" );
	reporting_t0 = 0;
	while( !done ){
		//std::cout <<  time(NULL) - reporting_t0 << std::endl;
		if( (time(NULL) - reporting_t0) > 9 ){
			report();
			reporting_t0 = time(NULL);
			}
		N = epoll_wait( epfd, events, 32, 10000 );
		for( i = 0 ; i < N  ; ++i ){
			
			if( events[i].data.fd == serversock ){
				
				addrlen = sizeof( struct sockaddr_un );
				clientsock = accept( serversock, (struct sockaddr*)&address, (socklen_t *)&addrlen );
				flags = fcntl( clientsock, F_GETFL, 0 );
				fcntl( clientsock, F_SETFL, flags | O_NONBLOCK );
				
				//setsockopt( clientsock, IPPROTO_TCP, TCP_NODELAY, (char *)&yes, sizeof( int ) );
	
				ev.events = EPOLLIN /*| EPOLLET */| EPOLLERR | EPOLLRDHUP;
				ev.data.fd = clientsock;
				n = epoll_ctl( epfd, EPOLL_CTL_ADD, clientsock, &ev );
				connection_buffers[ clientsock ] = std::string();
				//std::cerr << "Client connected." << std::endl;
				}
			else {
				if( events[i].events & EPOLLIN ){
					memset( buffer, 0, 4096 );
					n = recv( events[i].data.fd, buffer, 4090, 0 );
					if( n > 0 ){
						//std::cerr << "Data received!" << std::endl;
						connection_buffers[events[i].data.fd] += std::string( buffer );
						if( contains_message( connection_buffers[events[i].data.fd] ) ){
							connection_buffers[events[i].data.fd] = handle_message( connection_buffers[events[i].data.fd], events[i].data.fd );
							}
						
						}
					}
				if( events[i].events & EPOLLRDHUP ){
					//std::cerr << "Client disconnected." << std::endl;
					connection_buffers.erase( events[i].data.fd );
					close( events[i].data.fd );
					
					
					}
				}
			}
		 }
	
	fclose( append_log );
	return 0;
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10b8, %rsp           # imm = 0x10B8
pushq $0x1
popq %rbx
movl %ebx, %edi
callq 0x23c0
movl %eax, %ebp
movl %ebx, %edi
movl %ebx, %esi
xorl %edx, %edx
callq 0x20a0
movl %eax, %r14d
leaq 0x2ae8(%rip), %rdi      # 0x611d
callq 0x2150
xorps %xmm0, %xmm0
leaq 0x42(%rsp), %rsi
movups %xmm0, 0x14(%rsi)
movups %xmm0, 0x24(%rsi)
movups %xmm0, 0x34(%rsi)
movups %xmm0, 0x44(%rsi)
movups %xmm0, 0x54(%rsi)
movups %xmm0, 0x5e(%rsi)
movw $0x1, (%rsi)
movups 0x2ab7(%rip), %xmm0     # 0x611d
movups %xmm0, 0x2(%rsi)
movw $0x74, 0x12(%rsi)
pushq $0x6e
popq %rdx
movl %r14d, %edi
callq 0x2040
pushq $0x5
popq %rsi
movl %r14d, %edi
callq 0x22b0
movl $0x201b, 0x5c48(%rip)   # imm = 0x201B
leaq 0x5c41(%rip), %rcx      # 0x92d8
movl %r14d, 0x5c3e(%rip)     # 0x92dc
movl %ebp, 0xc(%rsp)
movl %ebp, %edi
movl %ebx, %esi
movl %r14d, %edx
callq 0x2330
movq 0x5923(%rip), %rdi      # 0x8fd8
leaq 0x2a73(%rip), %rsi      # 0x612f
callq 0x2260
movq %rax, %rdi
callq 0x2170
pushq $0xd
popq %rdi
pushq $0x1
popq %rsi
callq 0x22e0
callq 0x34b5
leaq 0x2a18(%rip), %rdi      # 0x60f8
leaq 0x2a58(%rip), %rsi      # 0x613f
callq 0x22d0
movq %rax, 0x5bdd(%rip)      # 0x92d0
leaq 0x5b3e(%rip), %r12      # 0x9238
leaq 0x20(%rsp), %rbx
movq $0x0, 0x18(%rsp)
movl %r14d, 0x10(%rsp)
xorl %edi, %edi
callq 0x2230
subq 0x18(%rsp), %rax
cmpq $0xa, %rax
jl 0x3730
callq 0x3458
xorl %edi, %edi
callq 0x2230
movq %rax, 0x18(%rsp)
movl 0xc(%rsp), %edi
leaq 0x5bb5(%rip), %rsi      # 0x92f0
pushq $0x20
popq %rdx
movl $0x2710, %ecx           # imm = 0x2710
callq 0x2160
movl %eax, %r13d
testl %eax, %eax
movl $0x0, %eax
cmovlel %eax, %r13d
leaq 0x5b97(%rip), %r15      # 0x92f4
subq $0x1, %r13
jb 0x370d
movl (%r15), %ebp
cmpl %r14d, %ebp
jne 0x3811
movl $0x6e, 0x14(%rsp)
movl %r14d, %edi
leaq 0x42(%rsp), %rsi
leaq 0x14(%rsp), %rdx
callq 0x20e0
movl %eax, 0x4(%rsp)
movl %eax, %edi
pushq $0x3
popq %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x21b0
movl 0x4(%rsp), %edi
orl $0x800, %eax            # imm = 0x800
pushq $0x4
popq %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x21b0
movl $0x2009, 0x5b1e(%rip)   # imm = 0x2009
movl 0x4(%rsp), %edx
movl %edx, 0x5b18(%rip)      # 0x92dc
movl 0xc(%rsp), %edi
pushq $0x1
popq %rsi
leaq 0x5b06(%rip), %rcx      # 0x92d8
callq 0x2330
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
andq $0x0, 0x28(%rsp)
movb $0x0, 0x30(%rsp)
movq %r12, %rdi
leaq 0x4(%rsp), %rsi
callq 0x3998
movq %rax, %rdi
movq %rbx, %rsi
callq 0x22f0
movq %rbx, %rdi
callq 0x2140
jmp 0x38f8
testb $0x1, -0x4(%r15)
je 0x38de
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %r14
movq %r12, %rbx
leaq 0xb0(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x21a0
movl $0xffa, %edx            # imm = 0xFFA
movl %ebp, %edi
movq %r12, %rsi
movq %rbx, %r12
movq %r14, %rbx
movl 0x10(%rsp), %r14d
xorl %ecx, %ecx
callq 0x20f0
testl %eax, %eax
jle 0x38de
movq %rbx, %rdi
leaq 0xb0(%rsp), %rsi
leaq 0xb(%rsp), %rdx
callq 0x2120
movq %r12, %rdi
movq %r15, %rsi
callq 0x3998
movq %rax, %rdi
movq %rbx, %rsi
callq 0x23f0
movq %rbx, %rdi
callq 0x2140
movq %r12, %rdi
movq %r15, %rsi
callq 0x3998
movq %rax, %rdi
callq 0x2756
testb %al, %al
je 0x38de
movq %r12, %rdi
movq %r15, %rsi
callq 0x3998
movl (%r15), %edx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2f6c
movq %r12, %rdi
movq %r15, %rsi
callq 0x3998
movq %rax, %rdi
movq %rbx, %rsi
callq 0x22f0
movq %rbx, %rdi
callq 0x2140
testb $0x20, -0x3(%r15)
je 0x38f8
movq %r12, %rdi
movq %r15, %rsi
callq 0x4766
movl (%r15), %edi
callq 0x2400
addq $0xc, %r15
jmp 0x375d
jmp 0x390a
movq %rax, %rbx
jmp 0x3917
jmp 0x390a
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2140
movq %rbx, %rdi
callq 0x23a0
nop