// File: /workspace/github_cpp_makefile_star2_O2/luisremis[P]SDFS/Membership.cpp
// Function: Membership::runLeaderElection()
void Membership::runLeaderElection()
{
	leader.active = false;
	bullied = false;

	if (members.size() < 2)
	{
		logFile << "No members, canceling leader election" << std::endl;
		return;
	}

	membersLock.lock();

	for (int i = 1; i < members.size(); ++i)
	{
		std::string other = members.at(i).ip_str;
		if ( isBiggerThanMine( other, my_ip_str) )
		{
			Message msg;
			msg.type = MSG_ELECTION;
			sendUDP( sockfd,  other, port, (char*)&msg, sizeof(Message) );
		}
	}

	membersLock.unlock();

	usleep(500); //Sleep one second

	if ( !bullied ) // Send all other that I am the leader
	{
		membersLock.lock();

		for (int i = 1; i < members.size(); ++i)
		{
			std::string other = members.at(i).ip_str;
			Message msg;
			msg.type = MSG_NEWLEADER;
			sendUDP( sockfd,  other, port, (char*)&msg, sizeof(Message) );
		}

		membersLock.unlock();

		leader = members.at(0);
		leader.active = true;
	}

/*
	Sleep
	check the queue

	if queue not empty, return
	else send NEWLEADER MSG  (I AM THE LEADER)
*/

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %r14
andl $0x0, 0x294(%rdi)
movb $0x0, 0x352(%rdi)
movq 0x260(%rdi), %rax
subq 0x258(%rdi), %rax
pushq $0x28
popq %rcx
cqto
idivq %rcx
cmpq $0x1, %rax
ja 0x8099
leaq 0x7797(%rip), %rsi      # 0xf80f
movq %r14, %rdi
callq 0x4370
movq %rax, %rdi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x41d0
leaq 0x270(%r14), %rax
movq %rax, -0x40(%rbp)
leaq 0x258(%r14), %rax
movq %rax, -0x30(%rbp)
leaq 0x230(%r14), %rdi
movq %rdi, -0x38(%rbp)
callq 0x8608
leaq 0x2d8(%r14), %rax
movq %rax, -0x48(%rbp)
pushq $0x1
popq %rsi
leaq -0xc0(%rbp), %r15
leaq -0xa0(%rbp), %r13
movq 0x260(%r14), %rax
subq 0x258(%r14), %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
cmpq %rsi, %rax
jbe 0x819d
movq %r14, %rbx
movq -0x30(%rbp), %rdi
movq %rsi, %r14
callq 0x8754
leaq -0x80(%rbp), %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0x4450
movq %r15, %rdi
movq %r12, %rsi
callq 0x4450
movq %r13, %rdi
movq -0x48(%rbp), %rsi
callq 0x4450
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x7e6a
movq %r13, %rdi
movl %eax, %r13d
callq 0x41b0
movq %r15, %r12
movq %r15, %rdi
callq 0x41b0
testb %r13b, %r13b
je 0x817c
movl $0x9, -0x5c(%rbp)
movl 0x298(%rbx), %edx
movl 0x29c(%rbx), %edi
leaq -0x80(%rbp), %rsi
leaq -0x5c(%rbp), %rcx
pushq $0x14
popq %r8
callq 0xdc23
leaq -0x80(%rbp), %rdi
callq 0x41b0
movq %r14, %rsi
incq %rsi
movq %r12, %r15
leaq -0xa0(%rbp), %r13
movq %rbx, %r14
jmp 0x80db
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
callq 0x4290
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x4470
cmpb $0x0, 0x352(%r14)
jne 0x8265
movq %rbx, %rdi
callq 0x8608
pushq $0x1
popq %rbx
leaq -0x80(%rbp), %r15
leaq -0x5c(%rbp), %r12
pushq $0x14
popq %r13
movq 0x260(%r14), %rax
subq 0x258(%r14), %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
cmpq %rbx, %rax
jbe 0x823a
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
callq 0x8754
movq %r15, %rdi
movq %rax, %rsi
callq 0x4450
movl $0xb, -0x5c(%rbp)
movl 0x298(%r14), %edx
movl 0x29c(%r14), %edi
movq %r15, %rsi
movq %r12, %rcx
movl %r13d, %r8d
callq 0xdc23
movq %r15, %rdi
callq 0x41b0
incq %rbx
jmp 0x81d8
movq -0x38(%rbp), %rdi
callq 0x4290
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x8754
movq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x8898
movl $0x1, 0x294(%r14)
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x829d
jmp 0x829d
movq %rax, %rbx
jmp 0x828f
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x41b0
leaq -0xc0(%rbp), %rdi
callq 0x41b0
jmp 0x82a0
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x41b0
movq %rbx, %rdi
callq 0x45d0
nop