// File: /workspace/github_cpp_makefile_star2_O3/SaadBazaz[P]RIP-Protocol-Simulation/Client.cpp
// Function: main
int main()
{
	int router_port;
	int myip;
	cout << ">> Which server do you want to connect to? ";
	cin >> router_port;

	cout << ">> Enter your unique IP: ";
	cin >> myip;

	cout << endl;

	cout << "Starting connection to server..." << endl;
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	assert(sockfd != -1);

	//Set Address Information
	struct sockaddr_in saddr;
	memset(&saddr, 0, sizeof(saddr));
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(router_port);
	saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

	//Link to router
	int res = connect(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
	assert(res != -1);

	cout << "Creating listening thread..." << endl;
	pthread_create(&listener_thread, NULL, messageListener, (void *)NULL);

	TableRow myself(myip, -1, "CLIENT");

	cout << "Sending my unique IP to server..." << endl;
	string init_msg = constructNewMessage(UPDATE, myip, router_port, (void *)&myself);
	send(sockfd, init_msg.c_str(), strlen(init_msg.c_str()), 0);

	cout << endl;

	char buff[128] = {0};

	int receiver = -1;
	while (buff != "close")
	{
		sleep(2);
		cout << "Press 1 for Sending a message\nPress 2 for resolving a Domain Name\n";
		int option = 0;
		cin >> option;
		string message ;
		if(option == 1){
			cout<<">> Enter the unique IP of the client you wish to talk to: ";
			cin>>receiver;

			memset(buff,0,128);
			cout<<">> Type you message : ";
			cin.ignore();
			fgets(buff,128,stdin);
			message = constructNewMessage(MESSAGE, myip, receiver, (void*)buff );
		}
		else if(option == 2){
			int DNS_IP = -1;
			cout << "Enter DNS IP  : ";
			cin >> DNS_IP;
			cout << endl;
			cout << "Enter Domain Name : \n>>";
			cin.ignore();
			fgets(buff,128,stdin);
			string finalMessage = "DNS REQ ";
			finalMessage += buff;
			message = constructNewMessage(MESSAGE, myip, DNS_IP, (void*)finalMessage.c_str() );
		}
		
		send(sockfd,message.c_str(),message.size(),0);

		if (strncmp(buff, "close", 3) == 0)
		{
			break;
		}

		memset(buff, 0, 128);

	}
	close(sockfd);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq 0x4c5c(%rip), %rbx      # 0x7fd0
leaq 0x2cbb(%rip), %rsi      # 0x6036
movl $0x2b, %edx
movq %rbx, %rdi
callq 0x2230
movq 0x4c51(%rip), %r14      # 0x7fe0
leaq 0x1c(%rsp), %rsi
movq %r14, %rdi
callq 0x2120
leaq 0x2cbf(%rip), %rsi      # 0x6062
movl $0x19, %edx
movq %rbx, %rdi
callq 0x2230
leaq 0xc(%rsp), %rsi
movq %r14, %rdi
callq 0x2120
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
leaq 0x2c91(%rip), %rsi      # 0x607c
movl $0x20, %edx
movq %rbx, %rdi
callq 0x2230
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2060
movl %eax, 0x4d9e(%rip)      # 0x81d4
cmpl $-0x1, %eax
je 0x39e9
leaq 0x188(%rsp), %r14
movl $0x0, 0xc(%r14)
movq $0x0, 0x4(%r14)
movw $0x2, (%r14)
movzwl 0x1c(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r14)
leaq 0x2c4e(%rip), %rdi      # 0x60c0
callq 0x21f0
movl %eax, 0x4(%r14)
movl 0x4d53(%rip), %edi      # 0x81d4
movq %r14, %rsi
movl $0x10, %edx
callq 0x21a0
cmpl $-0x1, %eax
je 0x3a08
leaq 0x2c36(%rip), %rsi      # 0x60d4
movl $0x1c, %edx
movq %rbx, %rdi
callq 0x2230
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
leaq 0x4cff(%rip), %rdi      # 0x81d8
leaq -0x226(%rip), %rdx      # 0x32ba
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x2300
movl 0xc(%rsp), %ebp
leaq 0xd0(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x2bf1(%rip), %rsi      # 0x60f1
leaq 0x2bf0(%rip), %rdx      # 0x60f7
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
callq 0x537a
leaq 0x70(%rsp), %rdi
movl %ebp, -0x10(%rdi)
movl $0xffffffff, -0xc(%rdi) # imm = 0xFFFFFFFF
movw $0x0, -0x8(%rdi)
movl $0x0, -0x4(%rdi)
leaq 0x80(%rsp), %rax
movq %rax, -0x10(%rax)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
callq 0x52e4
movb $0x0, 0x90(%rsp)
movq $-0x1, 0x94(%rsp)
movq 0xc0(%rsp), %rdi
cmpq %r14, %rdi
je 0x357f
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x4a4a(%rip), %rbx      # 0x7fd0
leaq 0x2b6b(%rip), %rsi      # 0x60f8
movl $0x21, %edx
movq %rbx, %rdi
callq 0x2230
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq 0x4a18(%rip), %rbx      # 0x7fd0
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movl 0xc(%rsp), %edx
movl 0x1c(%rsp), %ecx
leaq 0xa0(%rsp), %rdi
leaq 0x60(%rsp), %r8
movl $0x2, %esi
callq 0x28f2
movl 0x4be7(%rip), %ebp      # 0x81d4
movq 0xa0(%rsp), %r14
movq %r14, %rdi
callq 0x20d0
movl %ebp, %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x20e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq 0x49a6(%rip), %rbx      # 0x7fd0
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
xorps %xmm0, %xmm0
leaq 0xe0(%rsp), %r14
movaps %xmm0, 0x70(%r14)
movaps %xmm0, 0x60(%r14)
movaps %xmm0, 0x50(%r14)
movaps %xmm0, 0x40(%r14)
movaps %xmm0, 0x30(%r14)
movaps %xmm0, 0x20(%r14)
movaps %xmm0, 0x10(%r14)
movaps %xmm0, (%r14)
leaq 0x18(%rsp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x4962(%rip), %r13      # 0x7fe0
leaq 0x40(%rsp), %r15
leaq 0x168(%rsp), %r12
movl $0x2, %edi
callq 0x2170
movl $0x42, %edx
movq %rbx, %rdi
leaq 0x2a76(%rip), %rsi      # 0x611a
callq 0x2230
movl $0x0, 0x14(%rsp)
movq %r13, %rdi
leaq 0x14(%rsp), %rsi
callq 0x2120
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movb $0x0, 0x30(%rsp)
movl 0x14(%rsp), %eax
cmpl $0x2, %eax
je 0x37ad
cmpl $0x1, %eax
jne 0x38c3
movl $0x3a, %edx
movq %rbx, %rdi
leaq 0x2a62(%rip), %rsi      # 0x615d
callq 0x2230
movq %r13, %rdi
leaq 0x18(%rsp), %rsi
callq 0x2120
xorps %xmm0, %xmm0
movaps %xmm0, 0x150(%rsp)
movaps %xmm0, 0x140(%rsp)
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movl $0x16, %edx
movq %rbx, %rdi
leaq 0x2a39(%rip), %rsi      # 0x6198
callq 0x2230
movq %r13, %rdi
callq 0x2310
movq 0x4855(%rip), %rax      # 0x7fc8
movq (%rax), %rdx
movq %r14, %rdi
movl $0x80, %esi
callq 0x22b0
movl 0xc(%rsp), %edx
movl 0x18(%rsp), %ecx
movq %r15, %rdi
movl $0x4, %esi
movq %r14, %r8
callq 0x28f2
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x2250
jmp 0x38a7
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
movl $0x10, %edx
movq %rbx, %rdi
leaq 0x29eb(%rip), %rsi      # 0x61af
callq 0x2230
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
callq 0x2120
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
movl $0x17, %edx
movq %rbx, %rdi
leaq 0x29b4(%rip), %rsi      # 0x61c0
callq 0x2230
movq %r13, %rdi
callq 0x2310
movq 0x47a8(%rip), %rax      # 0x7fc8
movq (%rax), %rdx
movq %r14, %rdi
movl $0x80, %esi
callq 0x22b0
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %r15, %rdi
leaq 0x2994(%rip), %rsi      # 0x61d8
leaq 0x2995(%rip), %rdx      # 0x61e0
callq 0x537a
movq %r15, %rdi
movq %r14, %rsi
callq 0x2340
movl 0xc(%rsp), %edx
movl 0x10(%rsp), %ecx
movq 0x40(%rsp), %r8
movq %r12, %rdi
movl $0x4, %esi
callq 0x28f2
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x2250
movq 0x168(%rsp), %rdi
leaq 0x178(%rsp), %rax
cmpq %rax, %rdi
je 0x38a7
movq 0x178(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x38c3
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2200
movl 0x490b(%rip), %edi      # 0x81d4
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
xorl %ecx, %ecx
callq 0x20e0
movq %r13, %rbx
movq %r12, %r13
movq %r14, %r12
movq %r15, %r14
movl 0xe0(%rsp), %eax
movl $0x6c63, %ecx           # imm = 0x6C63
xorl %ecx, %eax
movzbl 0xe2(%rsp), %ecx
xorl $0x6f, %ecx
xorl %ebp, %ebp
orw %ax, %cx
setne %r15b
je 0x394d
xorps %xmm0, %xmm0
movaps %xmm0, 0x150(%rsp)
movaps %xmm0, 0x140(%rsp)
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3969
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2200
movb %r15b, %bpl
testl %ebp, %ebp
movq %r14, %r15
movq %r12, %r14
movq %r13, %r12
movq %rbx, %r13
movq 0x464f(%rip), %rbx      # 0x7fd0
jne 0x368b
movl 0x4847(%rip), %edi      # 0x81d4
callq 0x2350
leaq 0xb0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x39b3
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x39d5
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2200
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x26ad(%rip), %rdi      # 0x609d
leaq 0x26b3(%rip), %rsi      # 0x60aa
leaq 0x26b7(%rip), %rcx      # 0x60b5
movl $0x38, %edx
callq 0x2130
leaq 0x26bb(%rip), %rdi      # 0x60ca
leaq 0x2694(%rip), %rsi      # 0x60aa
leaq 0x2698(%rip), %rcx      # 0x60b5
movl $0x43, %edx
callq 0x2130
jmp 0x3a77
jmp 0x3a49
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
movq %r14, %rax
cmpq %r14, %rdi
jne 0x3ad0
jmp 0x3adb
jmp 0x3a77
movq %rax, %rbx
jmp 0x3abe
jmp 0x3a52
jmp 0x3a7e
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x3a81
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2200
jmp 0x3a81
jmp 0x3a7e
jmp 0x3a77
movq %rax, %rbx
jmp 0x3a9d
jmp 0x3a7e
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x3a9d
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2200
leaq 0xb0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3abe
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x2200
leaq 0x80(%rsp), %rax
movq 0x70(%rsp), %rdi
cmpq %rax, %rdi
je 0x3adb
movq (%rax), %rsi
incq %rsi
callq 0x2200
movq %rbx, %rdi
callq 0x22e0
nop