// File: /workspace/github_cpp_makefile_star2_O3/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 0x99cc
leaq 0x9eb0(%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 0x9c92
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 0x9b4f
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 0x56fa
leaq -0x88(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x56fa
leaq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq 0x2d8(%rbx), %rsi
movq 0x2e0(%rbx), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x56fa
movq %r13, %rdx
movq %rbx, %r15
movq %rbx, %rdi
movq %r14, %rbx
movq %r14, %rsi
movq %rdx, %r13
callq 0x96ba
movl %eax, %r14d
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x9ab7
callq 0x4360
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x9acf
callq 0x4360
testb %r14b, %r14b
movq %rbx, %r14
movq %r15, %rbx
je 0x9b06
movl $0x9, -0xac(%rbp)
movl 0x298(%rbx), %edx
movl 0x29c(%rbx), %edi
leaq -0x48(%rbp), %rsi
leaq -0xac(%rbp), %rcx
movl $0x14, %r8d
callq 0x1142c
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
movq -0x58(%rbp), %r15
je 0x9b1c
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 0x9a28
movq -0x50(%rbp), %r14
movq %r14, %rdi
callq 0x42a0
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x4490
cmpb $0x0, 0x352(%rbx)
jne 0x9c80
movq %r14, %rdi
callq 0x44d0
testl %eax, %eax
jne 0x9c92
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 0x9c43
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 0x56fa
movl $0xb, -0xac(%rbp)
movl 0x298(%rbx), %edx
movl 0x29c(%rbx), %edi
movq %r14, %rsi
leaq -0xac(%rbp), %rcx
movl $0x14, %r8d
callq 0x1142c
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
je 0x9c14
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 0x9bc3
movq -0x50(%rbp), %rdi
callq 0x42a0
movq 0x258(%rbx), %r14
cmpq %r14, 0x260(%rbx)
je 0x9c99
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
leaq 0x9cd5(%rip), %rdi      # 0x13975
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x44e0
jmp 0x9cbb
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
jne 0x9cff
jmp 0x9d04
movq %rax, %rbx
jmp 0x9cf2
movq %rax, %rbx
jmp 0x9cda
movq %rax, %rbx
movq -0x78(%rbp), %rdi
leaq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x9cda
callq 0x4360
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x9cf2
callq 0x4360
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x9d04
callq 0x4360
movq %rbx, %rdi
callq 0x4630