// File: /workspace/github_cpp_makefile_star2_O2/asdfz1346[P]MyServer/MyServer.cpp
// Function: MyServer::accept_thread_proc(void*)
void *MyServer::accept_thread_proc(void *args){
	MyServer* pthis = ((ARG*)args)->p;
	while(pthis->nstop){
		pthread_mutex_lock(&pthis->accept_mutex);
		pthread_cond_wait(&pthis->accept_cond,&pthis->accept_mutex);//block

		/*accept already,create newclientfd*/
		struct sockaddr_in clientaddr;
		socklen_t addrlen;
		int newfd = accept(pthis->listenfd,(struct sockaddr*)&clientaddr,&addrlen);
		pthread_mutex_unlock(&pthis->accept_mutex);
		if(newfd==-1) continue;
		/*set newfd sndbuf*/
		int nSndBufferLen = 4*1024*1024;
		int nLen          = sizeof(int);
		setsockopt(newfd,SOL_SOCKET,SO_SNDBUF,(char*)&nSndBufferLen,nLen);

		/*set newfd non-blocking;*/
		int oldflag = fcntl(newfd, F_GETFL, 0);
		int newflag = oldflag | O_NONBLOCK;
		if (fcntl(newfd, F_SETFL, newflag) == -1){
			std::cout << "fcntl error, oldflag = " << oldflag << ", newflag = " << newflag << std::endl;
			continue;
		}
		/*insert newfd to the set*/
	pthread_mutex_lock(&pthis->clientset_mutex);
		pthis->clientset.insert(newfd);
		pthread_mutex_unlock(&pthis->clientset_mutex);
		std::cout << "new client connect fd:"<<newfd<<std::endl;
		
		/*add epoll*/
		struct epoll_event e;
		memset (&e,0,sizeof e);
		e.events = EPOLLIN|EPOLLET;
		e.data.fd = newfd;
		if(epoll_ctl(pthis->epollfd,EPOLL_CTL_ADD,newfd,&e)==-1){
			std::cout << "epoll_ctl error,\n"<<std::endl;
		}

	}
	return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq (%rdi), %r15
leaq 0x70(%r15), %rbx
leaq 0xe8(%r15), %rax
movq %rax, -0x60(%rbp)
leaq 0x98(%r15), %rax
movq %rax, -0x58(%rbp)
leaq 0x40(%r15), %rax
movq %rax, -0x50(%rbp)
pushq $0x1
popq %r14
movq %r15, -0x38(%rbp)
cmpb $0x1, 0x118(%r15)
jne 0x29b2
movq %rbx, %rdi
callq 0x2320
movq -0x60(%rbp), %rdi
movq %rbx, %rsi
callq 0x2330
movl 0x18(%r15), %edi
leaq -0x74(%rbp), %rsi
leaq -0x64(%rbp), %rdx
callq 0x20a0
movl %eax, -0x2c(%rbp)
movq %rbx, %rdi
callq 0x21e0
movl -0x2c(%rbp), %edi
cmpl $-0x1, %edi
je 0x283c
movl $0x400000, -0x30(%rbp)  # imm = 0x400000
movl %r14d, %esi
pushq $0x7
popq %rdx
leaq -0x30(%rbp), %rcx
pushq $0x4
popq %r15
movl %r15d, %r8d
callq 0x2390
movl -0x2c(%rbp), %edi
pushq $0x3
popq %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2180
movl %eax, %r12d
movl %eax, %r13d
orl $0x800, %r13d           # imm = 0x800
movl -0x2c(%rbp), %edi
movl %r15d, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x2180
cmpl $-0x1, %eax
je 0x2969
movq -0x58(%rbp), %r12
movq %r12, %rdi
callq 0x2320
movq -0x50(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x3d2a
movq %r12, %rdi
callq 0x21e0
movq 0x56dc(%rip), %rdi      # 0x7fd8
leaq 0x2753(%rip), %rsi      # 0x5056
callq 0x2230
movl -0x2c(%rbp), %esi
movq %rax, %rdi
callq 0x2370
movq %rax, %rdi
callq 0x2130
andl $0x0, -0x3c(%rbp)
movl $0x80000001, -0x44(%rbp) # imm = 0x80000001
movl -0x2c(%rbp), %edx
movl %edx, -0x40(%rbp)
movq -0x38(%rbp), %r15
movl 0x1c(%r15), %edi
movl %r14d, %esi
leaq -0x44(%rbp), %rcx
callq 0x2310
cmpl $-0x1, %eax
jne 0x283c
movq 0x5688(%rip), %rdi      # 0x7fd8
leaq 0x2716(%rip), %rsi      # 0x506d
callq 0x2230
movq %rax, %rdi
callq 0x2130
jmp 0x283c
movq 0x5668(%rip), %rdi      # 0x7fd8
leaq 0x26ba(%rip), %rsi      # 0x5031
callq 0x2230
movq %rax, %rdi
movl %r12d, %esi
callq 0x2370
movq %rax, %rdi
leaq 0x26b8(%rip), %rsi      # 0x5049
callq 0x2230
movq %rax, %rdi
movl %r13d, %esi
callq 0x2370
movq %rax, %rdi
callq 0x2130
movq -0x38(%rbp), %r15
jmp 0x283c
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop