// File: /workspace/github_c_makefile_star2_O1/sharpyfox[P]citrusleaf-client/src/cl_cluster.c
// Function: cluster_tend
static void
cluster_tend(cl_cluster *asc)
{
	pthread_mutex_lock(&asc->LOCK);
	// If thre is already a tending process running for this cluster, there is no
	// point in running one more immediately. Moreover, there are assumptions in
	// the code that only one tender function is running at a time. So, abort.
	if ((asc->state & CLS_FREED) || (asc->state & CLS_TENDER_RUNNING)) {
		cf_debug("Not running cluster tend as the state of the cluster is 0x%x", asc->state);
		pthread_mutex_unlock(&asc->LOCK);
		return;
	}
	asc->state |= CLS_TENDER_RUNNING;

	// Start off by removing dunned hosts
	for (uint i=0;i<cf_vector_size(&asc->node_v);i++) {
		cl_cluster_node *cn = cf_vector_pointer_get(&asc->node_v, i);
		if (!cn)	continue;
		if (cn->dunned) {
			cf_debug(" DELETE DUNNED NODE %s %p i %d",cn->name,cn,i);
			cf_vector_delete(&asc->node_v, i);
			i--;
			cl_partition_table_remove_node(asc, cn);
			cl_cluster_node_release(cn);
		}
	}
	pthread_mutex_unlock(&asc->LOCK);

	// For all registered hosts --- resolve into the cluster's sockaddr_in list
	uint n_hosts = cf_vector_size(&asc->host_str_v);
	cf_vector_define(sockaddr_in_v, sizeof( struct sockaddr_in ), 0);
	for (uint i=0;i<n_hosts;i++) {
		/*
        if (cf_debug_enabled()) {
		    char *host = cf_vector_pointer_get(&asc->host_str_v, i);
		    int port = cf_vector_integer_get(&asc->host_port_v, i);
    		cf_debug("lookup hosts: %s:%d",host,port);
        }
        */
		cl_lookup(asc, cf_vector_pointer_get(&asc->host_str_v, i), 
					cf_vector_integer_get(&asc->host_port_v, i),
					&sockaddr_in_v);
	}
	// Compare this list against the current list of addresses of known nodes.
	// anything new, ping and get its info
	for (uint i=0;i<cf_vector_size(&sockaddr_in_v);i++) {
		struct sockaddr_in *sin = cf_vector_getp(&sockaddr_in_v,i);
		if (0 == cl_cluster_node_get_byaddr(asc, sin)) {
			cluster_ping_address(asc, sin);
		}
	}
	
	if (asc->n_partitions == 0)
		cluster_get_n_partitions(asc, &sockaddr_in_v);
	
	// vector will now contain an accumulation of the service addresses
	cf_vector_reset(&sockaddr_in_v);

	// Now, ping known nodes to see if there's an update
	// No need to lock node list because nodes are only added/removed in this thread.
	for (uint i = 0; i < cf_vector_size(&asc->node_v); i++) {
		cl_cluster_node *cn = cf_vector_pointer_get(&asc->node_v, i);
		cluster_ping_node(asc, cn, &sockaddr_in_v);
		for (uint j=0;j<cf_vector_size(&cn->sockaddr_in_v);j++) {
			struct sockaddr_in *sa_in = cf_vector_getp(&cn->sockaddr_in_v,j);
			cf_vector_append_unique(&sockaddr_in_v, sa_in);
		}
	}
	
	// cf_debug("CLUSTER TEND: sockaddr_in_v len %d %s",cf_vector_size(&sockaddr_in_v), asc->found_all?"foundall":"notfound" );
	
	// Compare all services with known nodes - explore if new
	if (asc->follow == true) {
		int n_new = 0;
		for (uint i=0;i<cf_vector_size(&sockaddr_in_v);i++) {
			struct sockaddr_in *sin = cf_vector_getp(&sockaddr_in_v, i);
			/*
			if (cf_debug_enabled()) {
				dump_sockaddr_in("testing service address",sin);
			}
			*/
			if (0 == cl_cluster_node_get_byaddr(asc, sin)) {
				if (cf_debug_enabled()) {
					cf_print_sockaddr_in("pinging",sin);
				}		
				cluster_ping_address(asc, sin);
				n_new++;
			}
		}
		if (n_new == 0)	{
			//cf_debug("CLUSTER TEND: *** FOUND ALL ***");
			asc->found_all = true;
		}
		//cf_debug("CLUSTER TEND: n_new is %d foundall %d",n_new,asc->found_all);
	}

	cf_vector_destroy(&sockaddr_in_v);

#ifdef DEBUG	
	dump_cluster(asc);
#endif	

	pthread_mutex_lock(&asc->LOCK);
	asc->state &= ~CLS_TENDER_RUNNING;
	pthread_mutex_unlock(&asc->LOCK);

	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd48, %rsp            # imm = 0xD48
movq %rdi, %rbx
addq $0x158, %rdi            # imm = 0x158
movq %rdi, 0x10(%rsp)
callq 0x23b0
movl 0x10(%rbx), %edx
testb $0x3, %dl
je 0x749e
leaq 0xfe66(%rip), %rax      # 0x172c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0x7483
leaq 0xfe60(%rip), %rax      # 0x172d0
movq (%rax), %rcx
leaq 0x9603(%rip), %rsi      # 0x10a7d
movl $0x3, %edi
xorl %eax, %eax
callq *%rcx
movq 0x10(%rsp), %rdi
addq $0xd48, %rsp            # imm = 0xD48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2190
orl $0x1, %edx
movl %edx, 0x10(%rbx)
leaq 0xf8(%rbx), %rax
movq %rax, 0x20(%rsp)
movq %rbx, (%rsp)
cmpl $0x0, 0x104(%rbx)
je 0x7556
leaq 0x140(%rsp), %rbx
leaq 0xfdf8(%rip), %r12      # 0x172c8
leaq 0xfdf9(%rip), %r13      # 0x172d0
leaq 0x95dc(%rip), %r14      # 0x10aba
xorl %ebp, %ebp
movq 0x20(%rsp), %rdi
movl %ebp, %esi
movq %rbx, %rdx
callq 0xec6f
movq 0x140(%rsp), %r15
testq %r15, %r15
je 0x7548
cmpb $0x1, 0x18(%r15)
jne 0x7548
movl (%r12), %eax
cmpl $0x3, %eax
jl 0x7526
movq (%r13), %r9
movl $0x3, %edi
movq %r14, %rsi
movq %r15, %rdx
movq %r15, %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq *%r9
movq 0x20(%rsp), %rdi
movl %ebp, %esi
callq 0xed5c
decl %ebp
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x8ae4
movq %r15, %rdi
callq 0x72c2
incl %ebp
movq (%rsp), %rax
cmpl 0x104(%rax), %ebp
jb 0x74e0
movq 0x10(%rsp), %rdi
callq 0x2190
movq (%rsp), %rax
movl 0x24(%rax), %r13d
leaq 0x38(%rsp), %rdi
leaq 0x940(%rsp), %rdx
xorl %ebx, %ebx
movl $0x10, %esi
movl $0x400, %ecx            # imm = 0x400
xorl %r8d, %r8d
callq 0xe924
testl %r13d, %r13d
je 0x75e4
movq (%rsp), %rax
leaq 0x18(%rax), %r14
leaq 0x60(%rax), %r15
leaq 0x140(%rsp), %r12
movq %r14, %rdi
movl %ebx, %esi
movq %r12, %rdx
callq 0xec6f
movq 0x140(%rsp), %rbp
movq %r15, %rdi
movl %ebx, %esi
movq %r12, %rdx
callq 0xec6f
movswl 0x140(%rsp), %edx
movq (%rsp), %rdi
movq %rbp, %rsi
leaq 0x38(%rsp), %rcx
callq 0x8e68
incl %ebx
cmpl %ebx, %r13d
jne 0x75a2
cmpl $0x0, 0x44(%rsp)
movq (%rsp), %r12
je 0x7626
xorl %ebx, %ebx
leaq 0x38(%rsp), %r14
movq %r14, %rdi
movl %ebx, %esi
callq 0xeccf
movq %rax, %r15
movq %r12, %rdi
movq %rax, %rsi
callq 0x6ad0
testq %rax, %rax
jne 0x761e
movq %r12, %rdi
movq %r15, %rsi
callq 0x8818
incl %ebx
cmpl 0x44(%rsp), %ebx
jb 0x75f6
cmpw $0x0, 0x140(%r12)
jne 0x763f
leaq 0x38(%rsp), %rsi
movq %r12, %rdi
callq 0x86eb
movl $0x0, 0x44(%rsp)
testb $0x2, 0x3c(%rsp)
je 0x7663
movq 0x48(%rsp), %rdi
movl 0x38(%rsp), %edx
imull 0x40(%rsp), %edx
xorl %esi, %esi
callq 0x2140
cmpl $0x0, 0x104(%r12)
je 0x7a2e
leaq 0x140(%rsp), %rdx
leaq 0x38(%rsp), %rbp
leaq 0xf8(%rsp), %r13
xorl %esi, %esi
movq 0x20(%rsp), %rdi
movl %esi, 0x28(%rsp)
callq 0xec6f
movq 0x140(%rsp), %r14
leaq 0x20(%r14), %rax
movq %rax, 0x18(%rsp)
cmpl $0x0, 0x2c(%r14)
je 0x79e3
movl $0x0, 0xc(%rsp)
xorl %ebp, %ebp
leaq 0x80(%rsp), %rbx
movq 0x18(%rsp), %r12
movq %r12, %rdi
movl %ebp, %esi
callq 0xeccf
movq %r14, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0xbebf
movl %eax, %r15d
testl %eax, %eax
je 0x773f
leaq 0xfbd8(%rip), %rax      # 0x172c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0x7714
leaq 0xfbd2(%rip), %rax      # 0x172d0
movq (%rax), %rcx
movl $0x3, %edi
leaq 0x93d4(%rip), %rsi      # 0x10ae1
movq %r14, %rdx
xorl %eax, %eax
callq *%rcx
cmpb $0x0, 0x18(%r14)
jne 0x7927
movl $0x12c, %eax            # imm = 0x12C
lock
xaddl %eax, 0x14(%r14)
cmpl $0x1f5, %eax            # imm = 0x1F5
jl 0x7927
movb $0x1, 0x18(%r14)
jmp 0x7927
movb 0x18(%r14), %al
cmpb $0x1, 0xac(%rsp)
jne 0x7775
testb %al, %al
jne 0x7768
movl $0x12c, %eax            # imm = 0x12C
lock
xaddl %eax, 0x14(%r14)
cmpl $0x1f5, %eax            # imm = 0x1F5
jl 0x7768
movb $0x1, 0x18(%r14)
movq %rbx, %rdi
callq 0xc0d3
jmp 0x7927
movl %r15d, 0x2c(%rsp)
testb %al, %al
jne 0x7786
movl $0x0, 0x14(%r14)
leaq 0x88(%rsp), %rdi
movq %r14, %rsi
callq 0x2350
testl %eax, %eax
leaq 0x140(%rsp), %r12
je 0x77f1
leaq 0xfb1f(%rip), %rax      # 0x172c8
movl (%rax), %eax
cmpl $0x2, %eax
jl 0x77d6
leaq 0xfb19(%rip), %rax      # 0x172d0
movq (%rax), %r8
movl $0x2, %edi
leaq 0x9336(%rip), %rsi      # 0x10afc
movq %r14, %rdx
leaq 0x88(%rsp), %rcx
xorl %eax, %eax
callq *%r8
cmpb $0x0, 0x18(%r14)
jne 0x77f1
movl $0x321, %eax            # imm = 0x321
lock
xaddl %eax, 0x14(%r14)
testl %eax, %eax
js 0x77f1
movb $0x1, 0x18(%r14)
movl 0xa8(%rsp), %eax
cmpl %eax, 0x68(%r14)
je 0x7808
movl %eax, 0x68(%r14)
movb $0x1, %al
movl %eax, 0xc(%rsp)
movq 0xa0(%rsp), %rbx
leaq 0xb0(%rsp), %r15
movq %r15, %rdi
movl $0x8, %esi
movq %r12, %rdx
movl $0x400, %ecx            # imm = 0x400
xorl %r8d, %r8d
callq 0xe924
movl $0x3b, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x83c5
cmpl $0x0, 0xbc(%rsp)
je 0x7900
xorl %ebx, %ebx
leaq 0xb0(%rsp), %rdi
movl %ebx, %esi
leaq 0x540(%rsp), %r12
movq %r12, %rdx
callq 0xec6f
movq 0x540(%rsp), %r15
movq %r13, %rdi
movl $0x8, %esi
movq %r12, %rdx
movl $0x400, %ecx            # imm = 0x400
xorl %r8d, %r8d
callq 0xe924
movl $0x3a, %edi
movq %r15, %rsi
movq %r13, %rdx
callq 0x83c5
cmpl $0x2, 0x104(%rsp)
jne 0x78e9
movq %r13, %rdi
xorl %esi, %esi
leaq 0x30(%rsp), %r12
movq %r12, %rdx
callq 0xec6f
movq 0x30(%rsp), %r15
movq %r13, %rdi
movl $0x1, %esi
movq %r12, %rdx
callq 0xec6f
movq 0x30(%rsp), %rdi
callq 0x2440
movswl %ax, %edx
movq (%rsp), %rdi
movq %r15, %rsi
leaq 0x38(%rsp), %rcx
callq 0x8e68
movq %r13, %rdi
callq 0xea0d
incl %ebx
cmpl 0xbc(%rsp), %ebx
jb 0x7850
leaq 0xb0(%rsp), %rdi
callq 0xea0d
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
callq 0xc0d3
movq 0x18(%rsp), %r12
movl 0x2c(%rsp), %r15d
testl %r15d, %r15d
je 0x7938
incl %ebp
cmpl 0x2c(%r14), %ebp
jb 0x76ca
testb $0x1, 0xc(%rsp)
leaq 0x38(%rsp), %rbp
leaq 0x140(%rsp), %r12
je 0x79e3
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x23b0
movq (%rsp), %rdi
movq %r14, %rsi
callq 0x8ae4
movq %rbx, %rdi
callq 0x2190
cmpl $0x0, 0x2c(%r14)
je 0x79e3
xorl %ebx, %ebx
movq 0x18(%rsp), %rdi
movl %ebx, %esi
callq 0xeccf
movq %r14, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0xc0e0
testl %eax, %eax
je 0x79a2
incl %ebx
cmpl 0x2c(%r14), %ebx
jb 0x797a
jmp 0x79e3
movq 0x148(%rsp), %rdx
testq %rdx, %rdx
je 0x79c0
movq (%rsp), %rdi
movq %r14, %rsi
movl $0x1, %ecx
callq 0x889d
movq 0x150(%rsp), %rdx
testq %rdx, %rdx
je 0x79db
movq (%rsp), %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x889d
movq %r12, %rdi
callq 0xc2e0
cmpl $0x0, 0x2c(%r14)
movq 0x18(%rsp), %r12
je 0x7a0e
xorl %ebx, %ebx
movq %r12, %rdi
movl %ebx, %esi
callq 0xeccf
movq %rbp, %rdi
movq %rax, %rsi
callq 0xebdb
incl %ebx
cmpl 0x2c(%r14), %ebx
jb 0x79f1
movl 0x28(%rsp), %esi
incl %esi
movq (%rsp), %r12
cmpl 0x104(%r12), %esi
leaq 0x140(%rsp), %rdx
jb 0x7689
cmpb $0x1, 0x14(%r12)
jne 0x7aaa
cmpl $0x0, 0x44(%rsp)
je 0x7aa4
xorl %ebx, %ebx
leaq 0x38(%rsp), %r14
leaq 0xf87d(%rip), %r13      # 0x172c8
leaq 0x9087(%rip), %r15      # 0x10ad9
xorl %ebp, %ebp
movq %r14, %rdi
movl %ebx, %esi
callq 0xeccf
movq %rax, %r12
movq (%rsp), %rdi
movq %rax, %rsi
callq 0x6ad0
testq %rax, %rax
jne 0x7a94
movl (%r13), %eax
cmpl $0x3, %eax
jl 0x7a86
movq %r15, %rdi
movq %r12, %rsi
callq 0xe6d7
movq (%rsp), %rdi
movq %r12, %rsi
callq 0x8818
incl %ebp
incl %ebx
cmpl 0x44(%rsp), %ebx
jb 0x7a54
testl %ebp, %ebp
movq (%rsp), %r12
jne 0x7aaa
movb $0x1, 0x16(%r12)
leaq 0x38(%rsp), %rdi
callq 0xea0d
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x23b0
andb $-0x2, 0x10(%r12)
movq %rbx, %rdi
callq 0x2190
addq $0xd48, %rsp            # imm = 0xD48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq