// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movq %rdi, %rbx
movl $0x0, 0x294(%rdi)
movb $0x0, 0x352(%rdi)
movq 0x260(%rdi), %rax
subq 0x258(%rdi), %rax
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
cmpq $0x1, %rax
ja 0x9a9e
leaq 0x9dde(%rip), %rsi      # 0x13837
movl $0x25, %edx
movq %rbx, %rdi
callq 0x43f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4260
leaq 0x230(%rbx), %rdi
movq %rdi, -0x50(%rbp)
callq 0x44d0
testl %eax, %eax
jne 0x9d67
movq 0x258(%rbx), %rax
movq 0x260(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rdx, %rcx
cmpq $0x2, %rcx
jb 0x9c21
movl $0x1, %r15d
movl $0x30, %r12d
leaq -0x98(%rbp), %r14
leaq -0x78(%rbp), %r13
movq %r15, -0x58(%rbp)
leaq -0x38(%rbp), %rcx
movq %rcx, -0x48(%rbp)
movq -0x8(%rax,%r12), %rsi
movq (%rax,%r12), %rdx
addq %rsi, %rdx
leaq -0x48(%rbp), %rdi
callq 0x570a
leaq -0x88(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x570a
leaq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq 0x2d8(%rbx), %rsi
movq 0x2e0(%rbx), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x570a
movq %r13, %rdx
movq %rbx, %r15
movq %rbx, %rdi
movq %r14, %rbx
movq %r14, %rsi
movq %rdx, %r13
callq 0x9730
movl %eax, %r14d
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x9b89
callq 0x4360
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x9ba1
callq 0x4360
testb %r14b, %r14b
movq %rbx, %r14
movq %r15, %rbx
je 0x9bd8
movl $0x9, -0xac(%rbp)
movl 0x298(%rbx), %edx
movl 0x29c(%rbx), %edi
leaq -0x48(%rbp), %rsi
leaq -0xac(%rbp), %rcx
movl $0x14, %r8d
callq 0x115b6
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
movq -0x58(%rbp), %r15
je 0x9bee
callq 0x4360
incq %r15
movq 0x258(%rbx), %rax
movq 0x260(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rdx, %rcx
addq $0x28, %r12
cmpq %r15, %rcx
ja 0x9afa
movq -0x50(%rbp), %r14
movq %r14, %rdi
callq 0x42a0
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x4490
cmpb $0x0, 0x352(%rbx)
jne 0x9d55
movq %r14, %rdi
callq 0x44d0
testl %eax, %eax
jne 0x9d67
movq 0x258(%rbx), %rax
movq 0x260(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rdx, %rcx
cmpq $0x2, %rcx
jb 0x9d15
movl $0x1, %r13d
movl $0x30, %r15d
leaq -0x38(%rbp), %r12
leaq -0x48(%rbp), %r14
movq %r12, -0x48(%rbp)
movq -0x8(%rax,%r15), %rsi
movq (%rax,%r15), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x570a
movl $0xb, -0xac(%rbp)
movl 0x298(%rbx), %edx
movl 0x29c(%rbx), %edi
movq %r14, %rsi
leaq -0xac(%rbp), %rcx
movl $0x14, %r8d
callq 0x115b6
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
je 0x9ce6
callq 0x4360
incq %r13
movq 0x258(%rbx), %rax
movq 0x260(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rdx, %rcx
addq $0x28, %r15
cmpq %r13, %rcx
ja 0x9c95
movq -0x50(%rbp), %rdi
callq 0x42a0
movq 0x258(%rbx), %r14
movq 0x260(%rbx), %rdx
subq %r14, %rdx
je 0x9d6e
leaq 0x270(%rbx), %rdi
movq %r14, %rsi
callq 0x4210
movq 0x20(%r14), %rax
movq %rax, 0x290(%rbx)
movl $0x1, 0x294(%rbx)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %edi
callq 0x4240
sarq $0x3, %rdx
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %rdx
leaq 0x9bee(%rip), %rdi      # 0x13975
xorl %esi, %esi
xorl %eax, %eax
callq 0x44e0
jmp 0x9da0
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
jne 0x9de4
jmp 0x9de9
movq %rax, %rbx
jmp 0x9dd7
movq %rax, %rbx
jmp 0x9dbf
movq %rax, %rbx
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x9dbf
callq 0x4360
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x9dd7
callq 0x4360
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x9de9
callq 0x4360
movq %rbx, %rdi
callq 0x4630
nop