// File: /workspace/github_cpp_makefile_star2_O2/sidscrazy[P]Paxos/./paxos_controller.cpp
// Function: PaxosController::network_simulator(int)
void network_simulator (int id){
		int role = ACCEPTOR;
		if (id < proposers){
			role |= PROPOSER;
			role |= LEARNER;
		}
		message *m;
		while (true){
			mutexes[id].lock ();
			m = receive_packet (sockets[id]);
			mutexes[id].unlock ();
			if (m != NULL){
				int receiver = m->receiver;
				
				/* Special Sequence for Ending Connection
				   and exiting program safely. */
				if (m->type == MSG_TEARDOWN) {
					mutexes[id].lock ();
					send_packet (sockets[id], m);
					mutexes[id].unlock ();
					free (m);
					return;
				}

				if (m->type == MSG_CRASH){
					log_mutex.lock ();
					log->CrashSequence (m);
					free (m);
					log_mutex.unlock ();
					continue;
				} 

				/* Broadcast Packet. */
				if (receiver == -1) {
					for (int i = 0; i < nodes; i++){
						if (i != id){
							mutexes[i].lock ();
							m->receiver = i;
							log_mutex.lock ();
							log->AddMsg (m, role);
							log_mutex.unlock ();
							send_packet (sockets[i], m);
							mutexes[i].unlock ();
						}
					}
				/* Send to single recipient. */
				} else {

				
					mutexes[receiver].lock ();
					log_mutex.lock ();
					log->AddMsg (m, role);
					send_packet (sockets[receiver], m);
					log->FlushToDisk();
					log_mutex.unlock ();
					mutexes[receiver].unlock ();
				}

				m->receiver = receiver;
				cout_mutex.lock ();
				if (dump_messages){
					dump_message (m);
				}
				cout_mutex.unlock ();
				free (m);

			}
		}
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
xorl %eax, %eax
cmpl %esi, 0xb4(%rdi)
setg %al
leal (%rax,%rax,2), %eax
addl $0x4, %eax
movl %eax, -0x2c(%rbp)
movslq %esi, %r15
leaq 0x88(%rdi), %r12
leaq 0x60(%rdi), %rax
movq %rax, -0x38(%rbp)
movl %r15d, %eax
movq %rax, -0x50(%rbp)
imulq $0x28, %r15, %r14
movq %r14, -0x48(%rbp)
movq %r15, -0x40(%rbp)
movq 0x48(%rbx), %rdi
addq %r14, %rdi
callq 0x31e4
movq 0x18(%rbx), %rax
movl (%rax,%r15,4), %edi
callq 0x24cc
movq %rax, %r13
movq 0x48(%rbx), %rdi
addq %r14, %rdi
callq 0x2150
testq %r13, %r13
je 0x3777
movl 0x10(%r13), %eax
cmpl $0x9, %eax
je 0x3866
cmpl $0x8, %eax
je 0x38fe
movl 0x4(%r13), %eax
cmpl $-0x1, %eax
movl %eax, -0x30(%rbp)
je 0x3892
movslq %eax, %r15
imulq $0x28, %r15, %r14
movq 0x48(%rbx), %rdi
addq %r14, %rdi
callq 0x31e4
movq %r12, %rdi
callq 0x31e4
movq 0xc0(%rbx), %rdi
movq %r13, %rsi
movl -0x2c(%rbp), %edx
callq 0x3fa8
movq 0x18(%rbx), %rax
movl (%rax,%r15,4), %edi
movq %r13, %rsi
callq 0x2538
movq 0xc0(%rbx), %rdi
callq 0x3e20
movq %r12, %rdi
callq 0x2150
addq 0x48(%rbx), %r14
movq %r14, %rdi
callq 0x2150
movl -0x30(%rbp), %eax
movl %eax, 0x4(%r13)
movq -0x38(%rbp), %rdi
callq 0x31e4
cmpb $0x1, 0xb8(%rbx)
jne 0x3848
movq %r13, %rdi
callq 0x25a2
movq -0x38(%rbp), %rdi
callq 0x2150
movq %r13, %rdi
callq 0x2230
movq -0x48(%rbp), %r14
movq -0x40(%rbp), %r15
jmp 0x3777
movq %r12, %rdi
callq 0x31e4
movq 0xc0(%rbx), %rdi
movq %r13, %rsi
callq 0x40da
movq %r13, %rdi
callq 0x2230
movq %r12, %rdi
callq 0x2150
jmp 0x3777
xorl %r15d, %r15d
movslq 0xb0(%rbx), %rax
cmpq %rax, %r15
jge 0x3827
cmpq -0x50(%rbp), %r15
je 0x38f9
imulq $0x28, %r15, %r14
movq 0x48(%rbx), %rdi
addq %r14, %rdi
callq 0x31e4
movl %r15d, 0x4(%r13)
movq %r12, %rdi
callq 0x31e4
movq 0xc0(%rbx), %rdi
movq %r13, %rsi
movl -0x2c(%rbp), %edx
callq 0x3fa8
movq %r12, %rdi
callq 0x2150
movq 0x18(%rbx), %rax
movl (%rax,%r15,4), %edi
movq %r13, %rsi
callq 0x2538
addq 0x48(%rbx), %r14
movq %r14, %rdi
callq 0x2150
incq %r15
jmp 0x3895
movq 0x48(%rbx), %rdi
addq %r14, %rdi
callq 0x31e4
movq 0x18(%rbx), %rax
movl (%rax,%r15,4), %edi
movq %r13, %rsi
callq 0x2538
addq 0x48(%rbx), %r14
movq %r14, %rdi
callq 0x2150
movq %r13, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2230