// File: 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
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0x28(%rbp)
movl $0x4, -0x10(%rbp)
movl -0xc(%rbp), %eax
cmpl 0xb4(%rcx), %eax
jge 0x62cb
movl -0x10(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x10(%rbp)
movl -0x10(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x10(%rbp)
jmp 0x62cd
movq -0x28(%rbp), %rdi
addq $0x48, %rdi
movslq -0xc(%rbp), %rsi
callq 0x4120
movq %rax, %rdi
callq 0x4140
movq -0x28(%rbp), %rdi
addq $0x18, %rdi
movslq -0xc(%rbp), %rsi
callq 0x40c0
movl (%rax), %edi
callq 0x2550
movq -0x28(%rbp), %rdi
movq %rax, -0x18(%rbp)
addq $0x48, %rdi
movslq -0xc(%rbp), %rsi
callq 0x4120
movq %rax, %rdi
callq 0x4170
cmpq $0x0, -0x18(%rbp)
je 0x658f
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x8, 0x10(%rax)
jne 0x6397
movq -0x28(%rbp), %rdi
addq $0x48, %rdi
movslq -0xc(%rbp), %rsi
callq 0x4120
movq %rax, %rdi
callq 0x4140
movq -0x28(%rbp), %rdi
addq $0x18, %rdi
movslq -0xc(%rbp), %rsi
callq 0x40c0
movl (%rax), %edi
movq -0x18(%rbp), %rsi
callq 0x2620
movq -0x28(%rbp), %rdi
addq $0x48, %rdi
movslq -0xc(%rbp), %rsi
callq 0x4120
movq %rax, %rdi
callq 0x4170
movq -0x18(%rbp), %rdi
callq 0x2260
addq $0x30, %rsp
popq %rbp
retq
movq -0x18(%rbp), %rax
cmpl $0x9, 0x10(%rax)
jne 0x63e3
movq -0x28(%rbp), %rdi
addq $0x88, %rdi
callq 0x4140
movq -0x28(%rbp), %rax
movq 0xc0(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x8140
movq -0x18(%rbp), %rdi
callq 0x2260
movq -0x28(%rbp), %rdi
addq $0x88, %rdi
callq 0x4170
jmp 0x62cd
cmpl $-0x1, -0x1c(%rbp)
jne 0x64b7
movl $0x0, -0x20(%rbp)
movq -0x28(%rbp), %rcx
movl -0x20(%rbp), %eax
cmpl 0xb0(%rcx), %eax
jge 0x64b2
movl -0x20(%rbp), %eax
cmpl -0xc(%rbp), %eax
je 0x64a2
movq -0x28(%rbp), %rdi
addq $0x48, %rdi
movslq -0x20(%rbp), %rsi
callq 0x4120
movq %rax, %rdi
callq 0x4140
movq -0x28(%rbp), %rdi
movl -0x20(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
addq $0x88, %rdi
callq 0x4140
movq -0x28(%rbp), %rax
movq 0xc0(%rax), %rdi
movq -0x18(%rbp), %rsi
movl -0x10(%rbp), %edx
callq 0x7fb0
movq -0x28(%rbp), %rdi
addq $0x88, %rdi
callq 0x4170
movq -0x28(%rbp), %rdi
addq $0x18, %rdi
movslq -0x20(%rbp), %rsi
callq 0x40c0
movl (%rax), %edi
movq -0x18(%rbp), %rsi
callq 0x2620
movq -0x28(%rbp), %rdi
addq $0x48, %rdi
movslq -0x20(%rbp), %rsi
callq 0x4120
movq %rax, %rdi
callq 0x4170
jmp 0x64a4
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x63f4
jmp 0x654c
movq -0x28(%rbp), %rdi
addq $0x48, %rdi
movslq -0x1c(%rbp), %rsi
callq 0x4120
movq %rax, %rdi
callq 0x4140
movq -0x28(%rbp), %rdi
addq $0x88, %rdi
callq 0x4140
movq -0x28(%rbp), %rax
movq 0xc0(%rax), %rdi
movq -0x18(%rbp), %rsi
movl -0x10(%rbp), %edx
callq 0x7fb0
movq -0x28(%rbp), %rdi
addq $0x18, %rdi
movslq -0x1c(%rbp), %rsi
callq 0x40c0
movl (%rax), %edi
movq -0x18(%rbp), %rsi
callq 0x2620
movq -0x28(%rbp), %rax
movq 0xc0(%rax), %rdi
callq 0x7de0
movq -0x28(%rbp), %rdi
addq $0x88, %rdi
callq 0x4170
movq -0x28(%rbp), %rdi
addq $0x48, %rdi
movslq -0x1c(%rbp), %rsi
callq 0x4120
movq %rax, %rdi
callq 0x4170
movq -0x28(%rbp), %rdi
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
addq $0x60, %rdi
callq 0x4140
movq -0x28(%rbp), %rax
testb $0x1, 0xb8(%rax)
je 0x6579
movq -0x18(%rbp), %rdi
callq 0x26c0
movq -0x28(%rbp), %rdi
addq $0x60, %rdi
callq 0x4170
movq -0x18(%rbp), %rdi
callq 0x2260
jmp 0x62cd
nopw %cs:(%rax,%rax)
nop