// File: 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:
subq $0x1168, %rsp           # imm = 0x1168
movl $0x0, 0x1164(%rsp)
movl %edi, 0x1160(%rsp)
movq %rsi, 0x1158(%rsp)
movl $0x1, %edi
callq 0x2470
movl %eax, 0x10dc(%rsp)
movb $0x0, 0xbf(%rsp)
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x20a0
movl %eax, 0x1154(%rsp)
leaq 0x8cfb(%rip), %rdi      # 0xd121
callq 0x2150
leaq 0x10e2(%rsp), %rdi
xorl %esi, %esi
movl $0x6e, %edx
callq 0x2190
movw $0x1, 0x10e2(%rsp)
leaq 0x10e2(%rsp), %rdi
addq $0x2, %rdi
leaq 0x8cc5(%rip), %rsi      # 0xd121
movb $0x0, %al
callq 0x2060
movl 0x1154(%rsp), %edi
leaq 0x10e2(%rsp), %rsi
movl $0x6e, %edx
callq 0x2040
movl 0x1154(%rsp), %edi
movl $0x5, %esi
callq 0x2310
movl $0x201b, 0xee91(%rip)   # imm = 0x201B
movl 0x1154(%rsp), %eax
movl %eax, 0xee88(%rip)      # 0x1332c
movl 0x10dc(%rsp), %edi
movl 0x1154(%rsp), %edx
movl $0x1, %esi
leaq 0xee6a(%rip), %rcx      # 0x13328
callq 0x23a0
movq 0xeb0e(%rip), %rdi      # 0x12fd8
leaq 0x8c62(%rip), %rsi      # 0xd133
callq 0x2290
movq %rax, %rdi
movq 0xeae8(%rip), %rsi      # 0x12fc8
callq 0x22e0
movl $0xd, %edi
movl $0x1, %esi
callq 0x2340
callq 0x41d0
leaq 0x8bfc(%rip), %rdi      # 0xd0fc
leaq 0x8c3c(%rip), %rsi      # 0xd143
callq 0x2330
movq %rax, 0xee0d(%rip)      # 0x13320
movq $0x0, 0xb0(%rsp)
movb 0xbf(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x4531
jmp 0x498f
xorl %eax, %eax
movl %eax, %edi
callq 0x2250
subq 0xb0(%rsp), %rax
cmpq $0x9, %rax
jle 0x455e
callq 0x4140
xorl %eax, %eax
movl %eax, %edi
callq 0x2250
movq %rax, 0xb0(%rsp)
movl 0x10dc(%rsp), %edi
leaq 0xedd4(%rip), %rsi      # 0x13340
movl $0x20, %edx
movl $0x2710, %ecx           # imm = 0x2710
callq 0x2160
movl %eax, 0x10d4(%rsp)
movl $0x0, 0x10d8(%rsp)
movl 0x10d8(%rsp), %eax
cmpl 0x10d4(%rsp), %eax
jge 0x498a
movslq 0x10d8(%rsp), %rcx
leaq 0xed90(%rip), %rax      # 0x13340
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
cmpl 0x1154(%rsp), %eax
jne 0x46e8
movl $0x6e, 0x10d0(%rsp)
movl 0x1154(%rsp), %edi
leaq 0x10e2(%rsp), %rsi
leaq 0x10d0(%rsp), %rdx
callq 0x20e0
movl %eax, 0x1150(%rsp)
movl 0x1150(%rsp), %edi
xorl %edx, %edx
movb %dl, %al
movb %al, 0x2f(%rsp)
movl $0x3, %esi
callq 0x21a0
movl %eax, %ecx
movb 0x2f(%rsp), %al
movl %ecx, 0x10cc(%rsp)
movl 0x1150(%rsp), %edi
movl 0x10cc(%rsp), %edx
orl $0x800, %edx            # imm = 0x800
movl $0x4, %esi
callq 0x21a0
movl $0x2009, 0xece5(%rip)   # imm = 0x2009
leaq 0xecde(%rip), %rcx      # 0x13328
movl 0x1150(%rsp), %eax
movl %eax, 0xecd5(%rip)      # 0x1332c
movl 0x10dc(%rsp), %edi
movl 0x1150(%rsp), %edx
movl $0x1, %esi
callq 0x23a0
movl %eax, 0x10c8(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x23b0
leaq 0xebfe(%rip), %rdi      # 0x13288
leaq 0x1150(%rsp), %rsi
callq 0x4f40
movq %rax, 0x30(%rsp)
jmp 0x469e
movq 0x30(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x2350
leaq 0x90(%rsp), %rdi
callq 0x21c0
jmp 0x4972
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x21c0
jmp 0x49a5
movslq 0x10d8(%rsp), %rcx
leaq 0xec49(%rip), %rax      # 0x13340
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x490a
leaq 0xc0(%rsp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x2190
movslq 0x10d8(%rsp), %rcx
leaq 0xec11(%rip), %rax      # 0x13340
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %edi
leaq 0xc0(%rsp), %rsi
movl $0xffa, %edx            # imm = 0xFFA
xorl %ecx, %ecx
callq 0x20f0
movl %eax, 0x10c8(%rsp)
cmpl $0x0, 0x10c8(%rsp)
jle 0x4908
leaq 0x5f(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x2450
movq 0x20(%rsp), %rdx
leaq 0x60(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x2390
jmp 0x478a
movslq 0x10d8(%rsp), %rax
leaq (%rax,%rax,2), %rcx
leaq 0xeba3(%rip), %rax      # 0x13340
leaq 0x4(%rax,%rcx,4), %rsi
leaq 0xeadf(%rip), %rdi      # 0x13288
callq 0x4f40
movq %rax, 0x18(%rsp)
jmp 0x47b5
movq 0x18(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x2360
jmp 0x47c6
leaq 0x60(%rsp), %rdi
callq 0x21c0
leaq 0x5f(%rsp), %rdi
callq 0x2300
movslq 0x10d8(%rsp), %rax
leaq 0xeb57(%rip), %rsi      # 0x13340
imulq $0xc, %rax, %rax
addq %rax, %rsi
addq $0x4, %rsi
leaq 0xea8d(%rip), %rdi      # 0x13288
callq 0x4f40
movq %rax, %rdi
callq 0x2920
testb $0x1, %al
jne 0x4811
jmp 0x4906
movslq 0x10d8(%rsp), %rax
leaq (%rax,%rax,2), %rcx
leaq 0xeb1c(%rip), %rax      # 0x13340
movq %rax, (%rsp)
leaq 0x4(%rax,%rcx,4), %rsi
leaq 0xea54(%rip), %rdi      # 0x13288
movq %rdi, 0x8(%rsp)
callq 0x4f40
movq %rax, %rsi
movq (%rsp), %rax
movslq 0x10d8(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
movl 0x4(%rax,%rcx,4), %edx
leaq 0x38(%rsp), %rdi
callq 0x39c0
movq (%rsp), %rax
movq 0x8(%rsp), %rdi
movslq 0x10d8(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq 0x4(%rax,%rcx,4), %rsi
callq 0x4f40
movq %rax, 0x10(%rsp)
jmp 0x4885
movq 0x10(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x2350
leaq 0x38(%rsp), %rdi
callq 0x21c0
jmp 0x4906
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
jmp 0x48d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x21c0
leaq 0x5f(%rsp), %rdi
callq 0x2300
jmp 0x49a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x21c0
jmp 0x49a5
jmp 0x4908
jmp 0x490a
movslq 0x10d8(%rsp), %rcx
leaq 0xea27(%rip), %rax      # 0x13340
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0x4970
movslq 0x10d8(%rsp), %rax
leaq 0xea05(%rip), %rsi      # 0x13340
imulq $0xc, %rax, %rax
addq %rax, %rsi
addq $0x4, %rsi
leaq 0xe93b(%rip), %rdi      # 0x13288
callq 0x5030
movslq 0x10d8(%rsp), %rcx
leaq 0xe9df(%rip), %rax      # 0x13340
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %edi
callq 0x2490
jmp 0x4972
jmp 0x4974
movl 0x10d8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10d8(%rsp)
jmp 0x458d
jmp 0x451f
movq 0xe98a(%rip), %rdi      # 0x13320
callq 0x22a0
xorl %eax, %eax
addq $0x1168, %rsp           # imm = 0x1168
retq
movq 0x88(%rsp), %rdi
callq 0x2440
nopw %cs:(%rax,%rax)
nopl (%rax)