// File: /workspace/github_cpp_makefile_star2_O3/SaadBazaz[P]RIP-Protocol-Simulation/Router.cpp
// Function: handlePacket(int const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void handlePacket(const int &client, string packet)
{
	auto vec = tokenizeData(packet, '&');
	// for (int i=0; i<vec.size(); i++){
	// 		std::cout<<vec[i]<<std::endl;
	// }


	if (vec[0] == "OK"){
		std::cout<<"Something good happened!"<<std::endl;
		return;
	}
	else if (vec[0] == "BAD"){
		std::cout<<"Oops, something went wrong..."<<std::endl;
		return;
	}


	switch (stoi(vec[0]))
	{
	case UPDATE:
	{
		// cout<<"In UPDATE..."<<endl;
		TableRow table_row(vec[3]);
		// cout<<"Made table_row..."<<endl;
		int index = findRowByFDAndID(client, table_row.identifier);

		if (index < 0)
		{

			// if match not found
			// remove all id = -1 values from table and add new table_row
			for (int i=0; i<table.size(); i++){
				if (table[i].identifier == -1){
					table.erase (table.begin() + i);
					i--;
				}
			} 

			table_row.fd = client;
			if (table_row.hop_count < 0)
			{
				table_row.hop_count = 0;
			}
			
			table.push_back(table_row);
			printTable();
			break;
		}


		if (table_row.identifier >= 0)
		{
			table[index].identifier = table_row.identifier;
		}

		// if (table_row.client_name >= 0){
		// 	table[index].client_name = table_row.client_name;
		// }

		if (table_row.status >= 0)
		{
			table[index].status = table_row.status;
		}

		if (table_row.hop_count >= 0)
		{
			table[index].hop_count = table_row.hop_count;
		}

		if (table_row.type != "")
		{
			table[index].type = table_row.type;
		}

		// if (table_row.in_use >= 0){
		table[index].in_use = table_row.in_use;
		// }

		if (table_row.send_port >= 0)
		{
			table[index].send_port = table_row.send_port;
		}

		if (table_row.receive_port >= 0)
		{
			table[index].receive_port = table_row.receive_port;
		}

		printTable();
		sendTableToAllRouters();

		break;
	}
	case ADD:
		break;

	case DELETE:
		break;

	case TABLE:{

		// auto index_vec = findAllRowsByFD(client);

		auto new_rows = deserialize(vec[3]);		


		// cout<<"New Rows - - - - - "<<endl;
		for (int i=0; i<new_rows.size(); i++){


			new_rows[i].fd = client;

			// cout<<new_rows[i].identifier<<endl;

		}
		// cout<<"- - - - - - - - - - "<<endl;



		// Clean useless data out of new_rows vector
		for (int i=0; i<table.size(); i++)
		{
			for (int j=0; j<new_rows.size(); j++){
				if (table[i] == new_rows[j] 
				or (table[i].fd == client and table[i].identifier == new_rows[j].identifier)
				or (table[i].identifier == new_rows[j].identifier and table[i].hop_count == 0)
				or (new_rows[j].hop_count >= 15)
				//  or new_rows[j].identifier == -1 or new_rows[j].identifier == port
				)

				{
					new_rows.erase(new_rows.begin() + j);
					j--;	
				}
			}
		}

		for (int j=0; j<new_rows.size(); j++){
			if (new_rows[j].identifier == -1 or new_rows[j].identifier == port){
				new_rows.erase(new_rows.begin() + j);
				j--;
			}
		}


		// cout<<"New Rows [CLEANED] - - - - - "<<endl;
		// for (int i=0; i<new_rows.size(); i++){
		// 	cout<<new_rows[i].identifier<<endl;
		// }
		// cout<<"- - - - - - - - - - "<<endl;



		// Add data from new_rows into table
		for (int i=0; i<new_rows.size(); i++){
				new_rows[i].hop_count ++;
				new_rows[i].fd = client;
				table.push_back(new_rows[i]);
		}

		// for (int i=0; i<new_rows.size(); i++){
		// 	if (new_rows[i].identifier != port)
		// 		table.push_back(new_rows[i]);
		// }

		printTable();
		sendTableToAllRouters(client);  //ignore this client when sending table to routers
		break;
	}

	case MESSAGE:
	{
		int index = findRowByID_MinHopCount(stoi(vec[2]));

		if (index < 0)
		{
			break;
		}

		if (vec.size() == 4){
			packet += '&';
			packet += to_string(port);
		}
		else {
			packet += ',';
			packet += to_string(port);			
		}

		// Forward packet to appropriate socket
		send(table[index].fd, packet.c_str(), strlen(packet.c_str()), 0);
		break;
	}
	default:
		break;
	}
	// if (vec[0] == UPDATE){
	// 	if ()

	// }
	// send(client, "OK", 2, 0); //Reply message to client
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %r14
movq %rdi, (%rsp)
leaq 0xb8(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rsi), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq 0xa8(%rsp), %r15
movq %r15, %rdi
callq 0x66ea
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
movl $0x26, %edx
callq 0x256c
movq 0xa8(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3b27
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x48(%rsp), %rdi
leaq 0x35ad(%rip), %rsi      # 0x70e0
callq 0x2090
testl %eax, %eax
je 0x3d10
movq 0x48(%rsp), %rdi
leaq 0x35b0(%rip), %rsi      # 0x70fc
callq 0x2090
testl %eax, %eax
je 0x3d1e
movq 0x48(%rsp), %rax
movq (%rax), %r12
callq 0x2040
movq %rax, %r15
movl (%rax), %ebx
movl $0x0, (%rax)
leaq 0x8(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
movl $0xa, %edx
callq 0x22e0
cmpq %r12, (%r13)
je 0x426c
movslq %eax, %rcx
cmpq %rax, %rcx
jne 0x4278
movl (%r15), %ebp
cmpl $0x22, %ebp
je 0x4278
testl %ebp, %ebp
jne 0x3bb1
movl %ebx, (%r15)
movl %ebx, %ebp
cmpl $0x1, %eax
je 0x3e37
cmpl $0x4, %eax
je 0x3d80
cmpl $0x2, %eax
jne 0x3d64
movq 0x48(%rsp), %rax
leaq 0x98(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq 0x60(%rax), %rsi
movq 0x68(%rax), %rdx
addq %rsi, %rdx
leaq 0x88(%rsp), %rdi
callq 0x66ea
leaq 0x8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
movl $0x2c, %edx
callq 0x6088
movq 0x88(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3c29
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2260
movq (%rsp), %rax
movl (%rax), %ecx
movq 0x65e2(%rip), %rax      # 0xa218
movq 0x65d3(%rip), %rsi      # 0xa210
movq %rax, %rdi
subq %rsi, %rdi
je 0x3cca
movl 0x8(%rsp), %edx
sarq $0x6, %rdi
cmpq $0x1, %rdi
adcq $0x0, %rdi
leaq 0x4(%rsi), %r8
xorl %r14d, %r14d
cmpl %ecx, (%r8)
jne 0x3c6f
cmpl %edx, -0x4(%r8)
je 0x41b1
incq %r14
addq $0x40, %r8
cmpq %r14, %rdi
jne 0x3c60
cmpq %rsi, %rax
je 0x3cca
xorl %ecx, %ecx
leaq 0x6587(%rip), %r14      # 0xa210
xorl %ebx, %ebx
shlq $0x6, %rcx
cmpl $-0x1, (%rsi,%rcx)
jne 0x3cb0
addq %rcx, %rsi
movq %r14, %rdi
callq 0x6dc6
decl %ebx
movq 0x656f(%rip), %rax      # 0xa218
movq 0x6560(%rip), %rsi      # 0xa210
incl %ebx
movslq %ebx, %rcx
movq %rax, %rdx
subq %rsi, %rdx
sarq $0x6, %rdx
cmpq %rcx, %rdx
ja 0x3c8b
movq (%rsp), %rax
movl (%rax), %ecx
movl %ecx, 0xc(%rsp)
cmpl $0x0, 0x14(%rsp)
jns 0x3cdd
movl $0x0, 0x14(%rsp)
leaq 0x652c(%rip), %rdi      # 0xa210
leaq 0x8(%rsp), %rsi
callq 0x650c
callq 0x337a
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3d64
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x3d64
movl $0x18, %edx
leaq 0x33c7(%rip), %rsi      # 0x70e3
jmp 0x3d2a
movl $0x1d, %edx
leaq 0x33d6(%rip), %rsi      # 0x7100
movq 0x629f(%rip), %rbx      # 0x9fd0
movq %rbx, %rdi
callq 0x22a0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2230
movsbl %al, %esi
movq 0x6279(%rip), %rdi      # 0x9fd0
callq 0x2030
movq %rax, %rdi
callq 0x2180
leaq 0x48(%rsp), %rdi
callq 0x4e86
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x48(%rsp), %rax
movq 0x40(%rax), %rbx
movl $0x0, (%r15)
leaq 0x8(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
movl $0xa, %edx
callq 0x22e0
cmpq %rbx, (%r12)
je 0x4284
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
leaq -0x80000000(%rax), %rdx
cmpq %rcx, %rdx
jb 0x4290
movl (%r15), %ecx
cmpl $0x22, %ecx
je 0x4290
testl %ecx, %ecx
jne 0x3ddc
movl %ebp, (%r15)
movl %eax, %edi
callq 0x355f
testl %eax, %eax
js 0x3d64
movl %eax, %ebx
movq 0x50(%rsp), %rax
subq 0x48(%rsp), %rax
cmpq $0x80, %rax
jne 0x4137
movq %r14, %rdi
movl $0x26, %esi
callq 0x2200
movl 0x6412(%rip), %esi      # 0xa228
leaq 0x8(%rsp), %rdi
callq 0x65d0
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r14, %rdi
callq 0x20a0
jmp 0x4166
movq 0x48(%rsp), %rax
leaq 0x78(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq 0x60(%rax), %rsi
movq 0x68(%rax), %rdx
addq %rsi, %rdx
leaq 0x68(%rsp), %rdi
callq 0x66ea
leaq 0x8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x2810
movq 0x68(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3e80
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %rbp
movq %rbp, %rdx
subq %r14, %rdx
je 0x3f9a
movq (%rsp), %rax
movl (%rax), %eax
sarq $0x6, %rdx
cmpq $0x1, %rdx
adcq $0x0, %rdx
leaq 0x3(%rdx), %rcx
andq $-0x4, %rcx
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq 0xc4(%r14), %rdx
movdqa 0x3154(%rip), %xmm1     # 0x7020
movdqa 0x315c(%rip), %xmm2     # 0x7030
movdqa 0x3164(%rip), %xmm3     # 0x7040
pxor %xmm3, %xmm0
pcmpeqd %xmm4, %xmm4
movdqa 0x3164(%rip), %xmm5     # 0x7050
movdqa %xmm2, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
movd %xmm6, %esi
notl %esi
testb $0x1, %sil
je 0x3f27
movl %eax, -0xc0(%rdx)
pxor %xmm4, %xmm6
pextrw $0x4, %xmm6, %esi
testb $0x1, %sil
je 0x3f39
movl %eax, -0x80(%rdx)
movdqa %xmm1, %xmm6
pxor %xmm3, %xmm6
movdqa %xmm6, %xmm7
pcmpgtd %xmm0, %xmm7
pcmpeqd %xmm0, %xmm6
pshufd $0xf5, %xmm6, %xmm8     # xmm8 = xmm6[1,1,3,3]
pand %xmm7, %xmm8
pshufd $0xf5, %xmm7, %xmm6     # xmm6 = xmm7[1,1,3,3]
por %xmm8, %xmm6
pxor %xmm4, %xmm6
pextrw $0x0, %xmm6, %esi
testb $0x1, %sil
je 0x3f74
movl %eax, -0x40(%rdx)
pextrw $0x4, %xmm6, %esi
testb $0x1, %sil
je 0x3f81
movl %eax, (%rdx)
paddq %xmm5, %xmm2
paddq %xmm5, %xmm1
addq $0x100, %rdx            # imm = 0x100
addq $-0x4, %rcx
jne 0x3eec
movq 0x6277(%rip), %rax      # 0xa218
movq 0x6268(%rip), %rcx      # 0xa210
cmpq %rcx, %rax
je 0x4079
xorl %ebx, %ebx
movq %rbp, %rdx
cmpq %r14, %rdx
je 0x4060
xorl %r12d, %r12d
movq %rbx, 0x60(%rsp)
shlq $0x6, %rbx
xorl %r15d, %r15d
movq 0x623b(%rip), %r13      # 0xa210
addq %rbx, %r13
shlq $0x6, %r12
addq %r14, %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0x268c
testb %al, %al
jne 0x4019
movl (%r13), %eax
movl 0x4(%r13), %edx
movl (%r12), %ecx
movq (%rsp), %rsi
cmpl (%rsi), %edx
jne 0x4006
cmpl %ecx, %eax
je 0x4019
cmpl %ecx, %eax
jne 0x4011
cmpl $0x0, 0xc(%r13)
je 0x4019
cmpl $0xf, 0xc(%r12)
jl 0x4033
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x6dc6
decl %r15d
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %rbp
incl %r15d
movslq %r15d, %r12
movq %rbp, %rax
subq %r14, %rax
sarq $0x6, %rax
cmpq %r12, %rax
ja 0x3fce
movq 0x61c9(%rip), %rax      # 0xa218
movq 0x61ba(%rip), %rcx      # 0xa210
movq %rbp, %rdx
movq 0x60(%rsp), %rbx
jmp 0x4063
movq %r14, %rdx
incq %rbx
movq %rax, %rsi
subq %rcx, %rsi
sarq $0x6, %rsi
cmpq %rbx, %rsi
ja 0x3fb6
cmpq %r14, %rbp
je 0x4118
xorl %eax, %eax
leaq 0x8(%rsp), %r15
xorl %ebx, %ebx
shlq $0x6, %rax
movl (%r14,%rax), %ecx
cmpl $-0x1, %ecx
je 0x40a0
cmpl 0x618a(%rip), %ecx      # 0xa228
jne 0x40ba
addq %rax, %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x6dc6
decl %ebx
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %rbp
incl %ebx
movslq %ebx, %rax
movq %rbp, %rcx
subq %r14, %rcx
sarq $0x6, %rcx
cmpq %rax, %rcx
ja 0x408b
cmpq %r14, %rbp
je 0x4118
xorl %ebx, %ebx
leaq 0x6134(%rip), %r15      # 0xa210
xorl %r12d, %r12d
incl 0xc(%r14,%rbx)
leaq (%r14,%rbx), %rsi
movq (%rsp), %rax
movl (%rax), %eax
movl %eax, 0x4(%r14,%rbx)
movq %r15, %rdi
callq 0x650c
incq %r12
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %rax
subq %r14, %rax
sarq $0x6, %rax
addq $0x40, %rbx
cmpq %r12, %rax
ja 0x40df
callq 0x337a
movq (%rsp), %rax
movl (%rax), %edi
callq 0x37e2
leaq 0x8(%rsp), %rdi
callq 0x6572
jmp 0x3d64
movq %r14, %rdi
movl $0x2c, %esi
callq 0x2200
movl 0x60de(%rip), %esi      # 0xa228
leaq 0x8(%rsp), %rdi
callq 0x65d0
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r14, %rdi
callq 0x20a0
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4181
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2260
movl %ebx, %eax
movq 0x6086(%rip), %rcx      # 0xa210
shlq $0x6, %rax
movl 0x4(%rcx,%rax), %ebx
movq (%r14), %r14
movq %r14, %rdi
callq 0x2100
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x2110
jmp 0x3d64
testq %r14, %r14
js 0x3c7b
testl %edx, %edx
js 0x41c8
movl %r14d, %eax
shlq $0x6, %rax
movl %edx, (%rsi,%rax)
movzwl 0x10(%rsp), %eax
testw %ax, %ax
js 0x41de
movl %r14d, %ecx
shlq $0x6, %rcx
movw %ax, 0x8(%rsi,%rcx)
movl 0x14(%rsp), %eax
testl %eax, %eax
js 0x41f1
movl %r14d, %ecx
shlq $0x6, %rcx
movl %eax, 0xc(%rsi,%rcx)
leaq 0x18(%rsp), %rbx
leaq 0x2eca(%rip), %rsi      # 0x70c7
movq %rbx, %rdi
callq 0x2090
movl %r14d, %r14d
testl %eax, %eax
je 0x422a
movq 0x5ffd(%rip), %rax      # 0xa210
movq %r14, %rcx
shlq $0x6, %rcx
leaq (%rax,%rcx), %rdi
addq $0x10, %rdi
movq %rbx, %rsi
callq 0x2130
movb 0x38(%rsp), %cl
movq 0x5fdb(%rip), %rdx      # 0xa210
shlq $0x6, %r14
leaq (%rdx,%r14), %rax
movb %cl, 0x30(%rdx,%r14)
movl 0x3c(%rsp), %ecx
testl %ecx, %ecx
js 0x424d
movl %ecx, 0x34(%rax)
movl 0x40(%rsp), %ecx
testl %ecx, %ecx
js 0x4258
movl %ecx, 0x38(%rax)
callq 0x337a
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x37e2
jmp 0x3cf3
leaq 0x2ff7(%rip), %rdi      # 0x726a
callq 0x2150
leaq 0x2feb(%rip), %rdi      # 0x726a
callq 0x22f0
leaq 0x2fdf(%rip), %rdi      # 0x726a
callq 0x2150
leaq 0x2fd3(%rip), %rdi      # 0x726a
callq 0x22f0
jmp 0x429e
movq %rax, %r14
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4378
movq 0x18(%rsp), %rsi
jmp 0x4328
jmp 0x4366
jmp 0x4366
movq %rax, %r14
movq 0x68(%rsp), %rdi
cmpq %rbx, %rdi
je 0x4378
movq 0x78(%rsp), %rsi
jmp 0x4328
movq %rax, %r14
movq 0x88(%rsp), %rdi
cmpq %rbx, %rdi
je 0x4378
movq 0x98(%rsp), %rsi
jmp 0x4328
jmp 0x4366
jmp 0x4366
jmp 0x4366
jmp 0x436b
movq %rax, %r14
cmpl $0x0, (%r15)
jne 0x4378
movl %ebp, (%r15)
jmp 0x4378
jmp 0x4312
movq %rax, %r14
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4378
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x4378
movq %rax, %r14
cmpl $0x0, (%r15)
jne 0x4378
movl %ebx, (%r15)
jmp 0x4378
jmp 0x436b
movq %rax, %r14
movq 0xa8(%rsp), %rdi
cmpq %rbx, %rdi
je 0x4382
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x4382
jmp 0x436b
movq %rax, %r14
jmp 0x4378
movq %rax, %r14
leaq 0x8(%rsp), %rdi
callq 0x6572
leaq 0x48(%rsp), %rdi
callq 0x4e86
movq %r14, %rdi
callq 0x2370