// File: allasandeep[P]LocationBoundaryFinder/prog1.cc
// Function: processNameFiles(std::basic_ifstream<char, std::char_traits<char>>&, pg_conn*)
void processNameFiles(ifstream& is, PGconn *db)
{
	//Required Variable declarations
	string line = "";
	string state_id = "";
	string county_id = "";
	string place_id = "";
	string region_type = "";
	string name = "";
	string year = "";
	int Id = 0;
	string type = "";
	bool insert_flag = false;
	char r_type;

	//Reading data from file line by line 
	while(std::getline(is,line))
	{
		//Getting the substrings and putting them into the respective variables
		insert_flag = false;
		state_id = line.substr(5,2);
		county_id = state_id + line.substr(7,3);
		year = line.substr(10,4);
		place_id = state_id + line.substr(14,5);
		region_type = line.substr(24,1); 
		name = line.substr(52,60);
		r_type = region_type[0];

		if( year == "2000" || year == "    ")
		{
			//Depending upon the type of region the ID and type variables are set and setting the insert_flag to true
			switch(r_type)
			{
				case 'S':
					type = "State";
					//checking whether the state length is 2
					if(state_id.length() == 2)
					{
						Id = stoi(state_id);
						insert_flag = true;
					}
					break;
				case 'C':
					type = "County";
					//checking whether the countyid length is 5
					if(county_id.length() == 5)
					{
						Id = stoi(county_id);
						insert_flag = true;
					}
					break;
				case 'M':
					type = "Subcounty";
					//checking whether the subcounty length is 7
					if(place_id.length() == 7)
					{
						Id = stoi(place_id);
						insert_flag = true;	      
					}
				case 'P':
					type = "Place";
					//checking whether the placeid length is 5
					if(place_id.length() == 7)
					{
						Id = stoi(place_id);
						insert_flag = true;
					}
			}

			PGresult * result = nullptr;
			//Psql Query by concatinating with c++ variables
			string query = "select id from region;";
			//Executing the query and storing the result in result.
			result = PQexec(db,query.c_str());
			//Number of tuples in the result
			int nrows = PQntuples(result);
			bool alreadyExits = false;

			if(nrows > 0)
			{
				for(int r = 0; r < nrows; r++)
				{
					if(PQgetvalue(result, r, 0) == to_string(Id))
					{
						alreadyExits = true;
					}
				}
			}	
					
			//Clearing the result
			PQclear(result);

			//If the insert flag is true then insert the data into database region
			if(insert_flag && !alreadyExits)
			{
				char query[100];
				char c_name[name.length()+1];
				strcpy(c_name, name.c_str());
				delSpaces(c_name);
				char c_type[type.length()+1];
				strcpy(c_type, type.c_str());

				//Psql Query by concatinating with c++ variables
				sprintf(query,"insert into region(id,iname,rtype) values(%d,'%s','%s');", Id, c_name, c_type);
				//Executing the query and storing the result in result.
				result = PQexec(db,query);

				//Clearing the result
				PQclear(result);			
			}
		}	
	}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x440, %rsp            # imm = 0x440
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x340(%rbp)
callq 0x22e0
movq -0x340(%rbp), %rdx
leaq 0x26ca(%rip), %rsi      # 0x51fb
leaq -0x30(%rbp), %rdi
callq 0x3c00
jmp 0x2b3c
leaq -0x31(%rbp), %rdi
callq 0x23a0
leaq -0x69(%rbp), %rdi
movq %rdi, -0x348(%rbp)
callq 0x22e0
movq -0x348(%rbp), %rdx
leaq 0x2698(%rip), %rsi      # 0x51fb
leaq -0x68(%rbp), %rdi
callq 0x3c00
jmp 0x2b6e
leaq -0x69(%rbp), %rdi
callq 0x23a0
leaq -0x91(%rbp), %rdi
movq %rdi, -0x350(%rbp)
callq 0x22e0
movq -0x350(%rbp), %rdx
leaq 0x2663(%rip), %rsi      # 0x51fb
leaq -0x90(%rbp), %rdi
callq 0x3c00
jmp 0x2ba6
leaq -0x91(%rbp), %rdi
callq 0x23a0
leaq -0xb9(%rbp), %rdi
movq %rdi, -0x358(%rbp)
callq 0x22e0
movq -0x358(%rbp), %rdx
leaq 0x2628(%rip), %rsi      # 0x51fb
leaq -0xb8(%rbp), %rdi
callq 0x3c00
jmp 0x2be1
leaq -0xb9(%rbp), %rdi
callq 0x23a0
leaq -0xe1(%rbp), %rdi
movq %rdi, -0x360(%rbp)
callq 0x22e0
movq -0x360(%rbp), %rdx
leaq 0x25ed(%rip), %rsi      # 0x51fb
leaq -0xe0(%rbp), %rdi
callq 0x3c00
jmp 0x2c1c
leaq -0xe1(%rbp), %rdi
callq 0x23a0
leaq -0x109(%rbp), %rdi
movq %rdi, -0x368(%rbp)
callq 0x22e0
movq -0x368(%rbp), %rdx
leaq 0x25b2(%rip), %rsi      # 0x51fb
leaq -0x108(%rbp), %rdi
callq 0x3c00
jmp 0x2c57
leaq -0x109(%rbp), %rdi
callq 0x23a0
leaq -0x131(%rbp), %rdi
movq %rdi, -0x370(%rbp)
callq 0x22e0
movq -0x370(%rbp), %rdx
leaq 0x2577(%rip), %rsi      # 0x51fb
leaq -0x130(%rbp), %rdi
callq 0x3c00
jmp 0x2c92
leaq -0x131(%rbp), %rdi
callq 0x23a0
movl $0x0, -0x138(%rbp)
leaq -0x159(%rbp), %rdi
movq %rdi, -0x378(%rbp)
callq 0x22e0
movq -0x378(%rbp), %rdx
leaq 0x2532(%rip), %rsi      # 0x51fb
leaq -0x158(%rbp), %rdi
callq 0x3c00
jmp 0x2cd7
leaq -0x159(%rbp), %rdi
callq 0x23a0
movb $0x0, -0x15a(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x2150
movq %rax, -0x380(%rbp)
jmp 0x2d00
movq -0x380(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2100
movb %al, -0x381(%rbp)
jmp 0x2d1e
movb -0x381(%rbp), %al
testb $0x1, %al
jne 0x2d2d
jmp 0x3561
movb $0x0, -0x15a(%rbp)
leaq -0x180(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x5, %edx
movl $0x2, %ecx
callq 0x22f0
jmp 0x2d50
leaq -0x68(%rbp), %rdi
leaq -0x180(%rbp), %rsi
movq %rsi, -0x390(%rbp)
callq 0x2170
movq -0x390(%rbp), %rdi
callq 0x2240
leaq -0x1c0(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x7, %edx
movl $0x3, %ecx
callq 0x22f0
jmp 0x2d8f
leaq -0x1a0(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x1c0(%rbp), %rdx
callq 0x3cf0
jmp 0x2da8
leaq -0x90(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
movq %rsi, -0x398(%rbp)
callq 0x2170
movq -0x398(%rbp), %rdi
callq 0x2240
leaq -0x1c0(%rbp), %rdi
callq 0x2240
leaq -0x1e0(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0xa, %edx
movl $0x4, %ecx
callq 0x22f0
jmp 0x2df6
leaq -0x130(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
movq %rsi, -0x3a0(%rbp)
callq 0x2170
movq -0x3a0(%rbp), %rdi
callq 0x2240
leaq -0x220(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0xe, %edx
movl $0x5, %ecx
callq 0x22f0
jmp 0x2e38
leaq -0x200(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x220(%rbp), %rdx
callq 0x3cf0
jmp 0x2e51
leaq -0xb8(%rbp), %rdi
leaq -0x200(%rbp), %rsi
movq %rsi, -0x3a8(%rbp)
callq 0x2170
movq -0x3a8(%rbp), %rdi
callq 0x2240
leaq -0x220(%rbp), %rdi
callq 0x2240
leaq -0x240(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x18, %edx
movl $0x1, %ecx
callq 0x22f0
jmp 0x2e9f
leaq -0xe0(%rbp), %rdi
leaq -0x240(%rbp), %rsi
movq %rsi, -0x3b0(%rbp)
callq 0x2170
movq -0x3b0(%rbp), %rdi
callq 0x2240
leaq -0x260(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x34, %edx
movl $0x3c, %ecx
callq 0x22f0
jmp 0x2ee1
leaq -0x108(%rbp), %rdi
leaq -0x260(%rbp), %rsi
movq %rsi, -0x3c0(%rbp)
callq 0x2170
movq -0x3c0(%rbp), %rdi
callq 0x2240
xorl %eax, %eax
movl %eax, %esi
leaq -0xe0(%rbp), %rdi
callq 0x2360
movq %rax, -0x3b8(%rbp)
jmp 0x2f20
movq -0x3b8(%rbp), %rax
movb (%rax), %al
movb %al, -0x15b(%rbp)
leaq 0x22bc(%rip), %rsi      # 0x51f2
leaq -0x130(%rbp), %rdi
callq 0x3d40
movb %al, -0x3c1(%rbp)
jmp 0x2f4a
movb -0x3c1(%rbp), %al
testb $0x1, %al
jne 0x2f80
jmp 0x2f56
leaq 0x229a(%rip), %rsi      # 0x51f7
leaq -0x130(%rbp), %rdi
callq 0x3d40
movb %al, -0x3c2(%rbp)
jmp 0x2f71
movb -0x3c2(%rbp), %al
testb $0x1, %al
jne 0x2f80
jmp 0x355c
movsbl -0x15b(%rbp), %eax
addl $-0x43, %eax
movl %eax, %ecx
movq %rcx, -0x3d0(%rbp)
subl $0x10, %eax
ja 0x3244
movq -0x3d0(%rbp), %rax
leaq 0x205a(%rip), %rcx      # 0x5004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x31(%rbp), %rdi
callq 0x23a0
jmp 0x361a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x69(%rbp), %rdi
callq 0x23a0
jmp 0x3611
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x91(%rbp), %rdi
callq 0x23a0
jmp 0x3608
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xb9(%rbp), %rdi
callq 0x23a0
jmp 0x35fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xe1(%rbp), %rdi
callq 0x23a0
jmp 0x35f0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x109(%rbp), %rdi
callq 0x23a0
jmp 0x35e4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x131(%rbp), %rdi
callq 0x23a0
jmp 0x35d8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x159(%rbp), %rdi
callq 0x23a0
jmp 0x35cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x35c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x2240
jmp 0x35c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x2240
jmp 0x35c0
leaq 0x2115(%rip), %rsi      # 0x51fc
leaq -0x158(%rbp), %rdi
callq 0x2120
jmp 0x30f5
leaq -0x68(%rbp), %rdi
callq 0x2180
cmpq $0x2, %rax
jne 0x3131
xorl %eax, %eax
movl %eax, %esi
leaq -0x68(%rbp), %rdi
movl $0xa, %edx
callq 0x3cb0
movl %eax, -0x3d4(%rbp)
jmp 0x311e
movl -0x3d4(%rbp), %eax
movl %eax, -0x138(%rbp)
movb $0x1, -0x15a(%rbp)
jmp 0x3244
leaq 0x20c5(%rip), %rsi      # 0x5202
leaq -0x158(%rbp), %rdi
callq 0x2120
jmp 0x314b
leaq -0x90(%rbp), %rdi
callq 0x2180
cmpq $0x5, %rax
jne 0x318d
xorl %eax, %eax
movl %eax, %esi
leaq -0x90(%rbp), %rdi
movl $0xa, %edx
callq 0x3cb0
movl %eax, -0x3d8(%rbp)
jmp 0x317a
movl -0x3d8(%rbp), %eax
movl %eax, -0x138(%rbp)
movb $0x1, -0x15a(%rbp)
jmp 0x3244
leaq 0x2070(%rip), %rsi      # 0x5209
leaq -0x158(%rbp), %rdi
callq 0x2120
jmp 0x31a7
leaq -0xb8(%rbp), %rdi
callq 0x2180
cmpq $0x7, %rax
jne 0x31e9
xorl %eax, %eax
movl %eax, %esi
leaq -0xb8(%rbp), %rdi
movl $0xa, %edx
callq 0x3cb0
movl %eax, -0x3dc(%rbp)
jmp 0x31d6
movl -0x3dc(%rbp), %eax
movl %eax, -0x138(%rbp)
movb $0x1, -0x15a(%rbp)
jmp 0x31eb
leaq 0x2021(%rip), %rsi      # 0x5213
leaq -0x158(%rbp), %rdi
callq 0x2120
jmp 0x3200
leaq -0xb8(%rbp), %rdi
callq 0x2180
cmpq $0x7, %rax
jne 0x3242
xorl %eax, %eax
movl %eax, %esi
leaq -0xb8(%rbp), %rdi
movl $0xa, %edx
callq 0x3cb0
movl %eax, -0x3e0(%rbp)
jmp 0x322f
movl -0x3e0(%rbp), %eax
movl %eax, -0x138(%rbp)
movb $0x1, -0x15a(%rbp)
jmp 0x3244
movq $0x0, -0x268(%rbp)
leaq -0x289(%rbp), %rdi
movq %rdi, -0x3e8(%rbp)
callq 0x22e0
movq -0x3e8(%rbp), %rdx
leaq 0x1fa9(%rip), %rsi      # 0x5219
leaq -0x288(%rbp), %rdi
callq 0x3c00
jmp 0x327e
leaq -0x289(%rbp), %rdi
callq 0x23a0
movq -0x10(%rbp), %rax
movq %rax, -0x3f8(%rbp)
leaq -0x288(%rbp), %rdi
callq 0x2090
movq -0x3f8(%rbp), %rdi
movq %rax, %rsi
callq 0x2370
movq %rax, -0x3f0(%rbp)
jmp 0x32b9
movq -0x3f0(%rbp), %rax
movq %rax, -0x268(%rbp)
movq -0x268(%rbp), %rdi
callq 0x2210
movl %eax, -0x3fc(%rbp)
jmp 0x32db
movl -0x3fc(%rbp), %eax
movl %eax, -0x290(%rbp)
movb $0x0, -0x291(%rbp)
cmpl $0x0, -0x290(%rbp)
jle 0x33e7
movl $0x0, -0x298(%rbp)
movl -0x298(%rbp), %eax
cmpl -0x290(%rbp), %eax
jge 0x33e5
movq -0x268(%rbp), %rdi
movl -0x298(%rbp), %esi
xorl %edx, %edx
callq 0x20d0
movq %rax, -0x408(%rbp)
jmp 0x3334
movl -0x138(%rbp), %esi
leaq -0x2b8(%rbp), %rdi
callq 0x3da0
jmp 0x3348
movq -0x408(%rbp), %rdi
leaq -0x2b8(%rbp), %rsi
callq 0x3d70
movb %al, -0x409(%rbp)
jmp 0x3363
leaq -0x2b8(%rbp), %rdi
callq 0x2240
movb -0x409(%rbp), %al
testb $0x1, %al
jne 0x337b
jmp 0x33cf
movb $0x1, -0x291(%rbp)
jmp 0x33cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x289(%rbp), %rdi
callq 0x23a0
jmp 0x35c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x354e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x2b8(%rbp), %rdi
callq 0x2240
jmp 0x354e
jmp 0x33d1
movl -0x298(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x298(%rbp)
jmp 0x3305
jmp 0x33e7
movq -0x268(%rbp), %rdi
callq 0x2160
jmp 0x33f5
testb $0x1, -0x15a(%rbp)
je 0x3540
testb $0x1, -0x291(%rbp)
jne 0x3540
leaq -0x108(%rbp), %rdi
movq %rdi, -0x440(%rbp)
callq 0x2180
movq -0x440(%rbp), %rdi
movq %rax, %rdx
movq %rdx, %rax
incq %rax
movq %rsp, %rcx
movq %rcx, -0x328(%rbp)
addq $0x10, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x430(%rbp)
movq %rcx, %rsp
movq %rax, -0x330(%rbp)
callq 0x2090
movq -0x430(%rbp), %rdi
movq %rax, %rsi
callq 0x2130
movq -0x430(%rbp), %rdi
callq 0x24f0
leaq -0x158(%rbp), %rdi
movq %rdi, -0x438(%rbp)
callq 0x2180
movq -0x438(%rbp), %rdi
movq %rax, %rdx
movq %rdx, %rax
incq %rax
addq $0x10, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x428(%rbp)
movq %rcx, %rsp
movq %rax, -0x338(%rbp)
callq 0x2090
movq -0x428(%rbp), %rdi
movq %rax, %rsi
callq 0x2130
movq -0x430(%rbp), %rcx
movq -0x428(%rbp), %r8
movl -0x138(%rbp), %edx
leaq 0x1d44(%rip), %rsi      # 0x5230
xorl %eax, %eax
leaq -0x320(%rbp), %rdi
movq %rdi, -0x420(%rbp)
callq 0x2330
movq -0x420(%rbp), %rsi
movq -0x10(%rbp), %rdi
callq 0x2370
movq %rax, -0x418(%rbp)
jmp 0x351a
movq -0x418(%rbp), %rax
movq %rax, -0x268(%rbp)
movq -0x268(%rbp), %rdi
callq 0x2160
jmp 0x3536
movq -0x328(%rbp), %rax
movq %rax, %rsp
leaq -0x288(%rbp), %rdi
callq 0x2240
jmp 0x355c
leaq -0x288(%rbp), %rdi
callq 0x2240
jmp 0x35c0
jmp 0x2cea
leaq -0x158(%rbp), %rdi
callq 0x2240
leaq -0x130(%rbp), %rdi
callq 0x2240
leaq -0x108(%rbp), %rdi
callq 0x2240
leaq -0xe0(%rbp), %rdi
callq 0x2240
leaq -0xb8(%rbp), %rdi
callq 0x2240
leaq -0x90(%rbp), %rdi
callq 0x2240
leaq -0x68(%rbp), %rdi
callq 0x2240
leaq -0x30(%rbp), %rdi
callq 0x2240
movq %rbp, %rsp
popq %rbp
retq
leaq -0x158(%rbp), %rdi
callq 0x2240
leaq -0x130(%rbp), %rdi
callq 0x2240
leaq -0x108(%rbp), %rdi
callq 0x2240
leaq -0xe0(%rbp), %rdi
callq 0x2240
leaq -0xb8(%rbp), %rdi
callq 0x2240
leaq -0x90(%rbp), %rdi
callq 0x2240
leaq -0x68(%rbp), %rdi
callq 0x2240
leaq -0x30(%rbp), %rdi
callq 0x2240
movq -0x40(%rbp), %rdi
callq 0x21f0
nopw %cs:(%rax,%rax)