// File: /workspace/github_cpp_makefile_star2_O2/roberty65[P]bServer/frame/main/Connection.cpp
// Function: beyondy::Async::Connection::onWritable(int)
int Connection::onWritable(int fd) 
{
	BPROF_TRACE(BPT_CONN_WRITABLE)

	assert(this->fd == fd);
	SYSLOG_DEBUG("connection fd=%d flow=%d onWritable. qsize=%ld", fd, flow, (long)(outQueue->size() + (outMsg == NULL ? 0 : 1)));

	if (outMsg == NULL) {
		if (outQueue->empty()) {
			// do not watch OUT event for it
			emgr->modifyConnection(this, 0, EVENT_OUT);
			return 0;
		}

		outMsg = outQueue->front();
		outQueue->pop_front();

		SYSLOG_DEBUG("connection fd=%d flow=%d start to send out msg(len=%ld)", fd, flow, (long)outMsg->getWptr());

		gettimeofday(&outMsg->ts_byte_first, NULL);
	}

	size_t left = outMsg->getWptr() - outMsg->getRptr();
	ssize_t wlen = writeN(outMsg, left);

	if ((size_t)wlen == left) {
		SYSLOG_DEBUG("connection fd=%d flow=%d send msg done(left=%ld)", fd, flow, (long)left);

		gettimeofday(&outMsg->ts_process_end, NULL);
		proc->onSent(outMsg, SS_OK);
		outMsg = NULL;

		// TODO: send more this time?
		return 0;
	}
	else if ((size_t)wlen < left) {
		/* try next time */
		SYSLOG_DEBUG("connection fd=%d flow=%d partially sent: should=%ld, real=%ld", fd, flow, (long)left, (long)wlen);
		return 0;
	}

	SYSLOG_ERROR("connection fd=%d flow=%d send msg(rest=%d) failed", fd, flow, (long)left);
	return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
cmpl %esi, 0x8(%rdi)
jne 0x5f12
movl %esi, %ebp
movq %rdi, %rbx
leaq 0xa787(%rip), %r12      # 0x10528
movq (%r12), %r14
cmpl $0x5, (%r14)
jl 0x5ddd
movl 0x10(%rbx), %r15d
movq 0x68(%rbx), %rdi
callq 0x5676
cmpq $0x1, 0x60(%rbx)
sbbq $-0x1, %rax
leaq 0x4d63(%rip), %rdx      # 0xab2b
pushq $0x5
popq %rsi
movq %r14, %rdi
movl %ebp, %ecx
movl %r15d, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x6d1a
movq 0x60(%rbx), %rsi
testq %rsi, %rsi
jne 0x5e3f
movq 0x68(%rbx), %rdi
movq 0x10(%rdi), %rax
cmpq %rax, 0x30(%rdi)
je 0x5eee
movq (%rax), %rax
movq %rax, 0x60(%rbx)
callq 0x5394
movq (%r12), %rdi
cmpl $0x5, (%rdi)
jl 0x5e2c
movl 0x10(%rbx), %r8d
movq 0x60(%rbx), %rax
movq 0x10(%rax), %r9
leaq 0x4d3a(%rip), %rdx      # 0xab5a
pushq $0x5
popq %rsi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x6d1a
movq 0x60(%rbx), %rdi
addq $0x30, %rdi
xorl %esi, %esi
callq 0x35a0
movq 0x60(%rbx), %rsi
movq 0x10(%rsi), %r14
subq 0x8(%rsi), %r14
movq %rbx, %rdi
movq %r14, %rdx
callq 0x5f32
cmpq %r14, %rax
jne 0x5ea3
movq (%r12), %rdi
cmpl $0x5, (%rdi)
jl 0x5e7a
movl 0x10(%rbx), %r8d
leaq 0x4d27(%rip), %rdx      # 0xab92
pushq $0x5
popq %rsi
movl %ebp, %ecx
movq %r14, %r9
xorl %eax, %eax
callq 0x6d1a
movq 0x60(%rbx), %rdi
addq $0x60, %rdi
xorl %r15d, %r15d
xorl %esi, %esi
callq 0x35a0
movq 0x60(%rbx), %rsi
movq 0x78(%rbx), %rdi
movq (%rdi), %rax
xorl %edx, %edx
callq *0x38(%rax)
andq $0x0, 0x60(%rbx)
jmp 0x5f02
movq (%r12), %rdi
movl (%rdi), %ecx
jae 0x5ec9
xorl %r15d, %r15d
cmpl $0x5, %ecx
jl 0x5f02
movl 0x10(%rbx), %r8d
movq %rax, (%rsp)
leaq 0x4d01(%rip), %rdx      # 0xabc3
xorl %r15d, %r15d
pushq $0x5
jmp 0x5edf
pushq $-0x1
popq %r15
cmpl $0x2, %ecx
jl 0x5f02
movl 0x10(%rbx), %r8d
leaq 0x4d24(%rip), %rdx      # 0xac01
pushq $0x2
popq %rsi
movl %ebp, %ecx
movq %r14, %r9
xorl %eax, %eax
callq 0x6d1a
jmp 0x5f02
movq 0x70(%rbx), %rdi
xorl %r15d, %r15d
pushq $0x4
popq %rcx
movq %rbx, %rsi
xorl %edx, %edx
callq 0x4d26
movl %r15d, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x493f(%rip), %rdi      # 0xa858
leaq 0x4947(%rip), %rsi      # 0xa867
leaq 0x4bcc(%rip), %rcx      # 0xaaf3
movl $0x80, %edx
callq 0x3200
nop