// File: /workspace/github_cpp_makefile_star2_O3/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 0x2420
movl %eax, %ebx
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x20a0
movl %eax, %ebp
leaq 0x24fa(%rip), %rdi      # 0x612d
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 0x24c9(%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 0x22d0
movl $0x201b, 0x5668(%rip)   # imm = 0x201B
leaq 0x5661(%rip), %rcx      # 0x92f8
movl %ebp, 0x565f(%rip)      # 0x92fc
movl %ebx, 0xc(%rsp)
movl %ebx, %edi
movl $0x1, %esi
movl %ebp, %edx
callq 0x2350
movq 0x5322(%rip), %r14      # 0x8fd8
leaq 0x2482(%rip), %rsi      # 0x613f
movl $0xf, %edx
movq %r14, %rdi
callq 0x22e0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2270
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x21d0
movl $0x1, %esi
movl $0xd, %edi
callq 0x2310
callq 0x3a1c
leaq 0x23fc(%rip), %rdi      # 0x6108
leaq 0x243c(%rip), %rsi      # 0x614f
callq 0x2300
movq %rax, 0x55d1(%rip)      # 0x92f0
leaq 0x20(%rsp), %r12
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x2250
subq %rbx, %rax
cmpq $0xa, %rax
jl 0x3d45
callq 0x398d
xorl %edi, %edi
callq 0x2250
movq %rax, %rbx
movl 0xc(%rsp), %edi
leaq 0x55c0(%rip), %rsi      # 0x9310
movl $0x20, %edx
movl $0x2710, %ecx           # imm = 0x2710
callq 0x2160
testl %eax, %eax
jle 0x3d26
movq %rbx, 0x38(%rsp)
movl %eax, %r15d
leaq 0x55a2(%rip), %r14      # 0x9314
movl (%r14), %r13d
cmpl %ebp, %r13d
jne 0x3e34
movl $0x6e, 0x34(%rsp)
movl %ebp, %edi
leaq 0x42(%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, 0x552c(%rip)   # imm = 0x2009
movl 0x8(%rsp), %edx
movl %edx, 0x5526(%rip)      # 0x92fc
movl 0xc(%rsp), %edi
movl $0x1, %esi
leaq 0x5512(%rip), %rcx      # 0x92f8
callq 0x2350
movq %r12, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
leaq 0x5453(%rip), %rdi      # 0x9258
leaq 0x8(%rsp), %rsi
callq 0x3fc4
movq %rax, %rdi
leaq 0x10(%rsp), %rsi
callq 0x2320
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x3f6a
callq 0x2260
jmp 0x3f6a
testb $0x1, -0x4(%r14)
je 0x3f3e
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 0x3f3e
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 0x47b0
leaq 0x53b1(%rip), %rdi      # 0x9258
movq %r14, %rsi
callq 0x3fc4
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %rax, %rdi
callq 0x20e0
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x3ed0
callq 0x2260
leaq 0x5381(%rip), %rdi      # 0x9258
movq %r14, %rsi
callq 0x3fc4
movq %rax, %rdi
movl $0xd, %esi
xorl %edx, %edx
callq 0x2330
cmpq $-0x1, %rax
je 0x3f3e
leaq 0x535d(%rip), %rbx      # 0x9258
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3fc4
movl (%r14), %edx
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movq %rax, %rsi
callq 0x32dd
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3fc4
movq %rax, %rdi
movq %r13, %rsi
callq 0x2320
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x3f3e
callq 0x2260
testb $0x20, -0x3(%r14)
je 0x3f6a
leaq 0x530c(%rip), %rbx      # 0x9258
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4c40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4cbe
movl (%r14), %edi
callq 0x2440
addq $0xc, %r14
decq %r15
jne 0x3d72
movq 0x38(%rsp), %rbx
jmp 0x3d26
jmp 0x3f97
movq %rax, %r14
jmp 0x3fa9
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
jne 0x3fa4
jmp 0x3fa9
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x3fa9
callq 0x2260
movq %r14, %rdi
callq 0x23e0
nop