// File: /workspace/github_cpp_makefile_star2_O1/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
movl $0x1, %edi
callq 0x2430
movl %eax, %ebx
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x20a0
movl %eax, %ebp
leaq 0x23ed(%rip), %rdi      # 0x612d
callq 0x2150
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rsi
movaps %xmm0, (%rsi)
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, 0x20(%rsi)
movaps %xmm0, 0x30(%rsi)
movaps %xmm0, 0x40(%rsi)
movaps %xmm0, 0x50(%rsi)
movups %xmm0, 0x5e(%rsi)
movw $0x1, (%rsi)
movups 0x23b9(%rip), %xmm0     # 0x612d
movups %xmm0, 0x2(%rsi)
movw $0x74, 0x12(%rsi)
movl %ebp, %edi
movl $0x6e, %edx
callq 0x2050
movl %ebp, %edi
movl $0x5, %esi
callq 0x22e0
movl $0x201b, 0x5568(%rip)   # imm = 0x201B
leaq 0x5561(%rip), %rcx      # 0x9308
movl %ebp, 0x555f(%rip)      # 0x930c
movl %ebx, 0xc(%rsp)
movl %ebx, %edi
movl $0x1, %esi
movl %ebp, %edx
callq 0x2360
movq 0x5212(%rip), %r14      # 0x8fd8
leaq 0x2372(%rip), %rsi      # 0x613f
movl $0xf, %edx
movq %r14, %rdi
callq 0x22f0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2280
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x21d0
movl $0x1, %esi
movl $0xd, %edi
callq 0x2320
callq 0x3b29
leaq 0x22ec(%rip), %rdi      # 0x6108
leaq 0x232c(%rip), %rsi      # 0x614f
callq 0x2310
movq %rax, 0x54d1(%rip)      # 0x9300
leaq 0x20(%rsp), %r12
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x2260
subq %rbx, %rax
cmpq $0xa, %rax
jl 0x3e55
callq 0x3a9a
xorl %edi, %edi
callq 0x2260
movq %rax, %rbx
movl 0xc(%rsp), %edi
leaq 0x54c0(%rip), %rsi      # 0x9320
movl $0x20, %edx
movl $0x2710, %ecx           # imm = 0x2710
callq 0x2160
testl %eax, %eax
jle 0x3e36
movq %rbx, 0x38(%rsp)
movl %eax, %r15d
leaq 0x54a2(%rip), %r14      # 0x9324
movl (%r14), %r13d
cmpl %ebp, %r13d
jne 0x3f44
movl $0x6e, 0x34(%rsp)
movl %ebp, %edi
leaq 0x40(%rsp), %rsi
leaq 0x34(%rsp), %rdx
callq 0x20f0
movl %eax, 0x8(%rsp)
movl %eax, %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x21a0
movl 0x8(%rsp), %edi
orl $0x800, %eax            # imm = 0x800
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x21a0
movl $0x2009, 0x542c(%rip)   # imm = 0x2009
movl 0x8(%rsp), %edx
movl %edx, 0x5426(%rip)      # 0x930c
movl 0xc(%rsp), %edi
movl $0x1, %esi
leaq 0x5412(%rip), %rcx      # 0x9308
callq 0x2360
movq %r12, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
leaq 0x5353(%rip), %rdi      # 0x9268
leaq 0x8(%rsp), %rsi
callq 0x40d4
movq %rax, %rdi
leaq 0x10(%rsp), %rsi
callq 0x2330
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x407a
callq 0x2270
jmp 0x407a
testb $0x1, -0x4(%r14)
je 0x404e
movl $0x1000, %edx           # imm = 0x1000
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2190
movl $0xffa, %edx            # imm = 0xFFA
movl %r13d, %edi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x2100
testl %eax, %eax
jle 0x404e
movq %r12, %r13
movq %r12, 0x10(%rsp)
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
callq 0x2140
leaq (%rsp,%rax), %rdx
addq $0xb0, %rdx
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x4892
leaq 0x52b1(%rip), %rdi      # 0x9268
movq %r14, %rsi
callq 0x40d4
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %rax, %rdi
callq 0x20e0
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x3fe0
callq 0x2270
leaq 0x5281(%rip), %rdi      # 0x9268
movq %r14, %rsi
callq 0x40d4
movq %rax, %rdi
movl $0xd, %esi
xorl %edx, %edx
callq 0x2340
cmpq $-0x1, %rax
je 0x404e
leaq 0x525d(%rip), %rbx      # 0x9268
movq %rbx, %rdi
movq %r14, %rsi
callq 0x40d4
movl (%r14), %edx
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movq %rax, %rsi
callq 0x33ef
movq %rbx, %rdi
movq %r14, %rsi
callq 0x40d4
movq %rax, %rdi
movq %r13, %rsi
callq 0x2330
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x404e
callq 0x2270
testb $0x20, -0x3(%r14)
je 0x407a
leaq 0x520c(%rip), %rbx      # 0x9268
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4d2a
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4da2
movl (%r14), %edi
callq 0x2450
addq $0xc, %r14
decq %r15
jne 0x3e82
movq 0x38(%rsp), %rbx
jmp 0x3e36
jmp 0x40a7
movq %rax, %r14
jmp 0x40b9
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
jne 0x40b4
jmp 0x40b9
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x40b9
callq 0x2270
movq %r14, %rdi
callq 0x23f0
nop