// File: /workspace/github_cpp_makefile_star2_O2/sidscrazy[P]Paxos/./paxos_controller.cpp
// Function: PaxosController::PaxosController(int, int)
PaxosController (int n, int p){
		nodes = n;
		proposers = p;
		children = std::vector<int> (n);
		sockets = std::vector<int> (n);
		threads = std::vector<std::thread> (n);
		mutexes = std::vector<std::mutex> (n);
		log = new PaxosNodeLogger (CONTROLLER_LOG);



		for (int i = 0; i < nodes; i++){
			
			int *s = (int *) malloc (2 * sizeof (int));
			
			if (s == NULL){
				exit (1);
			}

			if (socketpair(AF_UNIX, SOCK_DGRAM, 0, s) < 0){
				exit (1);
			}

			sockets[i] = s[0];

			int pid = fork ();
			if (pid == 0 ){
				close (s[0]);
				dup2 (s[1], 0);
				dup2 (s[1], 1);
				char *args[2];
				args[0] = (char *)malloc (7);
				if (args[0] == NULL){
					exit (1);
				}
				memcpy (args[0], "./node", 6);
				args[0][7] = '\0';
				args[1] = NULL;
				execvp (args[0], args);
			} else {
				children[i] = pid;
				close (s[1]);
			}

			int role = ACCEPTOR;
			if (i < proposers){
				role |= PROPOSER;
				role |= LEARNER;
			}
			message init_message (i, role, nodes, -1, MSG_INIT);
			mutexes[i].lock ();
			send_packet (sockets[i], &init_message);
			mutexes[i].unlock ();
			free (s);
			threads[i] = std::thread ( [this, i] { this->network_simulator(i); } );
		}

	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %edx, %r15d
movl %esi, %r12d
movq %rdi, %r13
leaq 0x18(%rdi), %rax
movq %rax, -0x38(%rbp)
leaq 0x30(%rdi), %rax
movq %rax, -0x70(%rbp)
leaq 0x48(%rdi), %r14
movb $0x0, 0xb8(%rdi)
movl $0xb0, %edx
xorl %esi, %esi
callq 0x2120
movl %r12d, 0xb0(%r13)
movl %r15d, 0xb4(%r13)
movslq %r12d, %r15
leaq -0x60(%rbp), %rdi
leaq -0x80(%rbp), %rdx
movq %r15, %rsi
movq %r13, -0x30(%rbp)
callq 0x30c2
leaq -0x60(%rbp), %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0x33b2
movq %r12, %rdi
callq 0x32d4
leaq -0x60(%rbp), %rdi
leaq -0x80(%rbp), %rdx
movq %r15, %rsi
callq 0x30c2
movq -0x38(%rbp), %rdi
movq %r12, %rsi
callq 0x33b2
movq %r12, %rdi
callq 0x32d4
leaq -0x60(%rbp), %rdi
leaq -0x80(%rbp), %rdx
movq %r15, %rsi
callq 0x3114
movq -0x70(%rbp), %rdi
movq %r12, %rsi
callq 0x351c
movq %r12, %rdi
callq 0x3166
leaq -0x60(%rbp), %rdi
leaq -0x80(%rbp), %rdx
movq %r15, %rsi
callq 0x3192
leaq -0x60(%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x3680
movq %r15, %rdi
callq 0x35c8
movl $0x228, %edi            # imm = 0x228
callq 0x2210
movq %rax, %r12
movq %r14, -0x68(%rbp)
pushq $-0x1
popq %rsi
movq %rax, %rdi
callq 0x3b20
movq %r12, 0xc0(%r13)
xorl %ebx, %ebx
leaq -0x90(%rbp), %r12
xorl %r14d, %r14d
xorl %r15d, %r15d
movslq 0xb0(%r13), %rax
cmpq %rax, %r15
jge 0x2e5f
movq %rbx, -0x88(%rbp)
movq %r12, %rbx
pushq $0x8
popq %rdi
callq 0x2280
testq %rax, %rax
je 0x2e6e
movq %rax, %r12
pushq $0x1
popq %rdi
pushq $0x2
popq %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x2190
testl %eax, %eax
js 0x2e6e
movl (%r12), %eax
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movl %eax, (%rcx,%r15,4)
callq 0x2060
testl %eax, %eax
je 0x2d53
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movl %eax, (%rcx,%r15,4)
movl 0x4(%r12), %edi
callq 0x2380
jmp 0x2dab
movl (%r12), %edi
callq 0x2380
movl 0x4(%r12), %edi
xorl %esi, %esi
callq 0x21d0
movl 0x4(%r12), %edi
pushq $0x1
popq %rsi
callq 0x21d0
pushq $0x7
popq %rdi
callq 0x2280
movq %rax, -0x60(%rbp)
testq %rax, %rax
je 0x2e6e
movw $0x6564, 0x4(%rax)      # imm = 0x6564
movl $0x6f6e2f2e, (%rax)     # imm = 0x6F6E2F2E
movb $0x0, 0x7(%rax)
andq $0x0, -0x58(%rbp)
movq %rax, %rdi
leaq -0x60(%rbp), %rsi
callq 0x2160
movq -0x30(%rbp), %rdx
movslq 0xb4(%rdx), %rax
xorl %ecx, %ecx
cmpq %rax, %r15
setl %cl
leal 0x4(%rcx,%rcx,2), %eax
movl 0xb0(%rdx), %ecx
movl %r15d, -0x60(%rbp)
movl %eax, -0x5c(%rbp)
movl %ecx, -0x58(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, -0x54(%rbp)
movq 0x48(%rdx), %rdi
addq %r14, %rdi
callq 0x31e4
movq -0x38(%rbp), %rax
movq (%rax), %rax
movl (%rax,%r15,4), %edi
leaq -0x60(%rbp), %rsi
callq 0x2538
movq -0x30(%rbp), %r13
movq 0x48(%r13), %rdi
addq %r14, %rdi
callq 0x2150
movq %r12, %rdi
callq 0x2230
movq %r13, -0x80(%rbp)
movl %r15d, -0x78(%rbp)
movq %rbx, %r12
movq %rbx, %rdi
leaq -0x80(%rbp), %rsi
callq 0x31fa
movq -0x88(%rbp), %rbx
movq -0x70(%rbp), %rax
movq (%rax), %rdi
addq %rbx, %rdi
movq %r12, %rsi
callq 0x326c
movq %r12, %rdi
callq 0x328a
incq %r15
addq $0x28, %r14
addq $0x8, %rbx
movq -0x30(%rbp), %r13
jmp 0x2cde
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rdi
callq 0x2240
movq %rax, %r15
movq %r12, %rdi
callq 0x21e0
jmp 0x2e97
jmp 0x2e8b
jmp 0x2e8b
jmp 0x2e8b
jmp 0x2e8b
movq %rax, %r15
jmp 0x2e9b
jmp 0x2e94
jmp 0x2e94
movq %rax, %r15
movq -0x68(%rbp), %r14
movq %r14, %rdi
callq 0x35c8
movq -0x70(%rbp), %rdi
callq 0x3166
movq -0x38(%rbp), %rdi
callq 0x32d4
movq -0x30(%rbp), %rdi
callq 0x32d4
movq %r15, %rdi
callq 0x2350