// File: /workspace/github_cpp_makefile_star2_O3/SaadBazaz[P]RIP-Protocol-Simulation/DNS.cpp
// Function: main
int main()
{
    vector<Directory> List = initiateList();
    PrintDirectory(List);
    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);

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

	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")
	{

        cout << "Waiting For Request ...\n";
        recv(sockfd,buff,127,0);
 
            // Splitting the Recieved Message into Substrings
            vector<string> data = tokenizeData(buff,'&');

            /*string domainName;
            for (size_t i = 4; data[3].size(); i++)
            {
                domainName += data[3][i];
            }*/
            string domainName = tokenizeData(data[3],' ')[2];
            cout << "Domain Requested: " <<domainName << endl;
            while (domainName.back() == '\n')
            {
                domainName.pop_back();
            }
            
            int pos = SearchDomain(domainName,List);
            string message;
            if (pos != -1)
            {
                cout << "IP Address to be Delivered : "<< List[pos].IPAddress << endl;
                string Response = "DNS RES " + List[pos].DomainName + " " + List[pos].IPAddress;
                message = constructNewMessage(MESSAGE, myip , stoi(data[1]),(void*)Response.c_str());
                send(sockfd,message.c_str(),message.size(),0);
            }
            else
            {
                /*cout << "Domain Not Found !"<<endl;
                char* notFound = "NOT_FOUND";
                message = constructNewMessage(MESSAGE, myip , stoi(data[1]),(void*)notFound);
                send(sockfd,message.c_str(),message.size(),0);*/
            }
            memset(buff, 0, 128);
        
	}
	close(sockfd);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x278, %rsp            # imm = 0x278
leaq 0x128(%rsp), %rbx
movq %rbx, %rdi
callq 0x32aa
leaq 0x158(%rsp), %rdi
movq %rbx, %rsi
callq 0x5bb2
leaq 0x158(%rsp), %rdi
callq 0x36f5
leaq 0x158(%rsp), %rdi
callq 0x5b4e
movq 0x668d(%rip), %rdi      # 0x9fd0
leaq 0x3aec(%rip), %rsi      # 0x7436
movl $0x2b, %edx
callq 0x2230
movq 0x6685(%rip), %rdi      # 0x9fe0
leaq 0x4(%rsp), %rsi
callq 0x2130
movq 0x6664(%rip), %rdi      # 0x9fd0
leaq 0x3aef(%rip), %rsi      # 0x7462
movl $0x19, %edx
callq 0x2230
movq 0x665c(%rip), %rdi      # 0x9fe0
movq %rsp, %rsi
callq 0x2130
movq 0x663d(%rip), %rdi      # 0x9fd0
callq 0x20e0
movq 0x6631(%rip), %rdi      # 0x9fd0
leaq 0x3ad6(%rip), %rsi      # 0x747c
movl $0x20, %edx
callq 0x2230
movq 0x6619(%rip), %rdi      # 0x9fd0
callq 0x20e0
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2060
movl %eax, 0x67e5(%rip)      # 0xa1b8
cmpl $-0x1, %eax
jne 0x39f7
leaq 0x3abe(%rip), %rdi      # 0x749d
leaq 0x3ac4(%rip), %rsi      # 0x74aa
leaq 0x3ac5(%rip), %rcx      # 0x74b2
movl $0x4f, %edx
callq 0x2140
leaq 0x268(%rsp), %rbx
movl $0x0, 0xc(%rbx)
movq $0x0, 0x4(%rbx)
movw $0x2, (%rbx)
movzwl 0x4(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x2(%rbx)
leaq 0x3a96(%rip), %rdi      # 0x74bd
callq 0x21f0
movl %eax, 0x4(%rbx)
movl 0x6783(%rip), %edi      # 0xa1b8
movq %rbx, %rsi
movl $0x10, %edx
callq 0x21a0
cmpl $-0x1, %eax
jne 0x3a66
leaq 0x3a79(%rip), %rdi      # 0x74c7
leaq 0x3a55(%rip), %rsi      # 0x74aa
leaq 0x3a56(%rip), %rcx      # 0x74b2
movl $0x5a, %edx
callq 0x2140
movl (%rsp), %ebx
leaq 0x118(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x3a55(%rip), %rsi      # 0x74d1
leaq 0x3a54(%rip), %rdx      # 0x74d7
leaq 0x108(%rsp), %rdi
callq 0x5de4
leaq 0x220(%rsp), %rdi
movl %ebx, -0x10(%rdi)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xc(%rdi)
movw $0xffff, -0x8(%rdi)     # imm = 0xFFFF
movl %eax, -0x4(%rdi)
leaq 0x230(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0x108(%rsp), %rsi
movq 0x110(%rsp), %rdx
addq %rsi, %rdx
callq 0x5d4e
movb $0x0, 0x240(%rsp)
movq $-0x1, 0x244(%rsp)
movq 0x108(%rsp), %rdi
cmpq %r14, %rdi
je 0x3b01
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x64c8(%rip), %rdi      # 0x9fd0
leaq 0x39c9(%rip), %rsi      # 0x74d8
movl $0x21, %edx
callq 0x2230
movq 0x64b0(%rip), %rdi      # 0x9fd0
callq 0x20e0
movl (%rsp), %edx
movl 0x4(%rsp), %ecx
leaq 0x1f0(%rsp), %rdi
leaq 0x210(%rsp), %r8
movl $0x2, %esi
callq 0x28e2
movl 0x666c(%rip), %ebx      # 0xa1b8
movq 0x1f0(%rsp), %r14
movq %r14, %rdi
callq 0x20d0
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x20f0
movq 0x645e(%rip), %rdi      # 0x9fd0
callq 0x20e0
xorps %xmm0, %xmm0
leaq 0x170(%rsp), %rax
movaps %xmm0, 0x70(%rax)
movaps %xmm0, 0x60(%rax)
movaps %xmm0, 0x50(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
leaq 0x38(%rsp), %r14
leaq 0x18(%rsp), %r13
leaq 0x78(%rsp), %rbp
leaq 0x28(%rsp), %r12
movl $0x18, %edx
movq 0x640f(%rip), %rdi      # 0x9fd0
leaq 0x3932(%rip), %rsi      # 0x74fa
callq 0x2230
leaq 0x170(%rsp), %r15
movl 0x65dd(%rip), %edi      # 0xa1b8
movl $0x7f, %edx
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x2090
leaq 0xf8(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq %r15, %rdi
callq 0x20d0
leaq (%rsp,%rax), %rdx
addq $0x170, %rdx            # imm = 0x170
leaq 0xe8(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5de4
leaq 0x140(%rsp), %rdi
movq %rbx, %rsi
movl $0x26, %edx
callq 0x249c
movq 0xe8(%rsp), %rdi
leaq 0xf8(%rsp), %rax
cmpq %rax, %rdi
je 0x3c5a
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x140(%rsp), %rax
leaq 0xd8(%rsp), %rcx
movq %rcx, 0xc8(%rsp)
movq 0x60(%rax), %rsi
movq 0x68(%rax), %rdx
addq %rsi, %rdx
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0x5d4e
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
movl $0x20, %edx
callq 0x249c
movq 0x8(%rsp), %rax
movq %r14, 0x28(%rsp)
movq 0x40(%rax), %rsi
movq 0x48(%rax), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x5d4e
leaq 0x8(%rsp), %rdi
callq 0x439e
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x3ceb
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x2200
movl $0x12, %edx
movq 0x62d9(%rip), %rdi      # 0x9fd0
leaq 0x3815(%rip), %rsi      # 0x7513
callq 0x2230
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x62bc(%rip), %rdi      # 0x9fd0
callq 0x2230
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2150
jmp 0x3d4d
movq %r12, %rdi
callq 0x2250
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
cmpb $0xa, -0x1(%rsi,%rdx)
je 0x3d45
addq %rsi, %rdx
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x5d4e
leaq 0x250(%rsp), %rbx
movq %rbx, %rdi
leaq 0x128(%rsp), %rsi
callq 0x5bb2
leaq 0x88(%rsp), %rdi
movq %rbx, %rsi
callq 0x37df
movl %eax, %r15d
movq %rbx, %rdi
callq 0x5b4e
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x3dd6
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2200
movq %r13, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movb $0x0, 0x18(%rsp)
cmpl $-0x1, %r15d
je 0x3ebf
movl $0x1d, %edx
movq 0x61d1(%rip), %rbx      # 0x9fd0
movq %rbx, %rdi
leaq 0x371d(%rip), %rsi      # 0x7526
callq 0x2230
movslq %r15d, %rax
movq 0x128(%rsp), %rcx
leaq (%rax,%rax,4), %r14
shlq $0x5, %r14
leaq (%rcx,%r14), %r15
addq $0x20, %r15
movq %r15, %rdi
callq 0x20d0
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
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 0x2150
movq 0x128(%rsp), %rdx
addq %r14, %rdx
leaq 0xa8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x36c1(%rip), %rsi      # 0x7544
callq 0x5c4e
leaq 0x58(%rsp), %r15
movq %rbx, %rdi
leaq 0x36ab(%rip), %rsi      # 0x7542
callq 0x2320
movq %r15, 0x48(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x3f07
movq %rdx, 0x48(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x58(%rsp)
jmp 0x3f0e
xorps %xmm0, %xmm0
movaps %xmm0, 0x1e0(%rsp)
movaps %xmm0, 0x1d0(%rsp)
movaps %xmm0, 0x1c0(%rsp)
movaps %xmm0, 0x1b0(%rsp)
movaps %xmm0, 0x1a0(%rsp)
movaps %xmm0, 0x190(%rsp)
movaps %xmm0, 0x180(%rsp)
movaps %xmm0, 0x170(%rsp)
jmp 0x40fe
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
movq %rdx, 0x50(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x128(%rsp), %rax
leaq (%rax,%r14), %rsi
addq $0x20, %rsi
leaq 0x48(%rsp), %rdi
callq 0x2320
movq %rbp, 0x68(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x3f63
movq %rdx, 0x68(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x78(%rsp)
jmp 0x3f6a
movups (%rcx), %xmm0
movups %xmm0, (%rbp)
movq 0x8(%rax), %rdx
movq %rdx, 0x70(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x3f99
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x3fbe
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x2200
movl (%rsp), %r15d
movq 0x140(%rsp), %rax
movq 0x20(%rax), %r14
callq 0x2040
movq %rax, %r13
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r14, %rdi
leaq 0xa8(%rsp), %rsi
movl $0xa, %edx
callq 0x2270
cmpq %r14, 0xa8(%rsp)
je 0x4127
movslq %eax, %rcx
cmpq %rax, %rcx
leaq 0x48(%rsp), %rbx
jne 0x4133
movl (%r13), %ecx
cmpl $0x22, %ecx
je 0x4133
testl %ecx, %ecx
jne 0x4027
movl %ebp, (%r13)
movq 0x68(%rsp), %r8
movq %rbx, %rdi
movl $0x4, %esi
movl %r15d, %edx
movl %eax, %ecx
callq 0x28e2
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x2260
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
leaq 0x38(%rsp), %r14
leaq 0x18(%rsp), %r13
leaq 0x78(%rsp), %rbp
je 0x4076
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2200
movl 0x613c(%rip), %edi      # 0xa1b8
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
xorl %ecx, %ecx
callq 0x20f0
movq 0x68(%rsp), %rdi
cmpq %rbp, %rdi
je 0x40a4
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x8(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, 0x170(%rsp)
movaps %xmm0, 0x180(%rsp)
movaps %xmm0, 0x190(%rsp)
movaps %xmm0, 0x1a0(%rsp)
movaps %xmm0, 0x1b0(%rsp)
movaps %xmm0, 0x1c0(%rsp)
movaps %xmm0, 0x1d0(%rsp)
movaps %xmm0, 0x1e0(%rsp)
cmpq %r13, %rdi
je 0x40fe
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2200
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0x4115
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2200
leaq 0x140(%rsp), %rdi
callq 0x439e
jmp 0x3bb5
leaq 0x341f(%rip), %rdi      # 0x754d
callq 0x2120
leaq 0x3413(%rip), %rdi      # 0x754d
callq 0x2280
jmp 0x4141
movq %rax, %rbx
jmp 0x4254
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x4168
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x2200
jmp 0x4168
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x4277
movq 0xb8(%rsp), %rsi
jmp 0x4268
jmp 0x4274
movq %rax, %rbx
leaq 0x250(%rsp), %rdi
callq 0x5b4e
jmp 0x41a8
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x4298
movq 0x98(%rsp), %rsi
jmp 0x428b
jmp 0x4295
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x439e
jmp 0x41e5
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x42b4
movq 0xd8(%rsp), %rsi
jmp 0x42ac
movq %rax, %rbx
jmp 0x42b4
movq %rax, %rbx
movq 0xe8(%rsp), %rdi
leaq 0xf8(%rsp), %rax
cmpq %rax, %rdi
je 0x42c6
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x2200
jmp 0x42c6
jmp 0x42c3
movq %rax, %rbx
cmpl $0x0, (%r13)
jne 0x4254
movl %ebp, (%r13)
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x4277
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x2200
jmp 0x4277
jmp 0x42c3
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x4298
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2200
jmp 0x4298
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x42b4
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2200
leaq 0x140(%rsp), %rdi
callq 0x439e
jmp 0x42c6
movq %rax, %rbx
leaq 0x200(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x42ee
movq 0x200(%rsp), %rsi
incq %rsi
callq 0x2200
jmp 0x42ee
jmp 0x42eb
movq %rax, %rbx
movq 0x220(%rsp), %rdi
leaq 0x230(%rsp), %rax
cmpq %rax, %rdi
je 0x434a
movq 0x230(%rsp), %rsi
jmp 0x4325
movq %rax, %rbx
movq 0x108(%rsp), %rdi
cmpq %r14, %rdi
je 0x434a
movq 0x118(%rsp), %rsi
incq %rsi
callq 0x2200
jmp 0x434a
jmp 0x4347
jmp 0x4347
jmp 0x4347
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
callq 0x5b4e
jmp 0x434a
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x5b4e
movq %rbx, %rdi
callq 0x22e0
nop