// File: /workspace/github_cpp_makefile_star2_O2/pawan231[P]MiniDataBase/include/search.h
// Function: search()
void search(){
	//search for particular table or any specific entry inside table
	cout<<"1.search table\n2.search particular entry in table\n\n";
	int ch;
	cin>>ch;
	char *tab;
	tab=(char*)malloc(sizeof(char)*MAX_NAME);
	if(ch==1){
		cout<<"enter table name: ";
		cin>>tab;
		//check if table exists
		int check=search_table(tab);
		if(check==1) printf("\n%s exists!!!\n",tab);
		else printf("\n%s doesn't exist in table entry\n\n",tab);
	}
	else if(ch==2){
		cout<<"enter table name: ";
		cin>>tab;
		int check=search_table(tab);
		if(check==1){
			printf("%s exists!!!\n\nEnter key to search\n\n",tab);
			//open %s meta data file and display column details;
			table inp1;
			FILE *fp = open_file(tab, const_cast<char*>("r"));
     		 int i=0;
			while(fread(&inp1,sizeof(table),1,fp)){
				for(i=0;i<inp1.count;i++){
					cout<<inp1.col[i].col_name<<"("<<inp1.col[i].type<<"),size:"<<inp1.col[i].size;
					cout<<"\t";
				}
			}
			int pri_int;
			int c;
			char d[MAX_NAME];
			char pri_char[MAX_NAME];
			BPtree mytree(tab);
			int ret=0;
			if(inp1.col[0].type==INT){
				cout<<"\nenter key[col 0] to search\n";
				cin>>pri_int;
				 ret = mytree.get_record(pri_int);
				 if (ret == BPTREE_SEARCH_NOT_FOUND){
				 	printf("\nSearching (%d) -> NOT FOUND !!", pri_int);
				 }
				 else{
				 	 //printf("\nSearch (%d) exists -> record num = %d", pri_int, ret);
				 	 printf("\n %d exists \n\n",pri_int);
				 	 //print the details of the particular row;
				 	FILE *fpz;
				 	char *str1;
					printf("\n------------------------------------\n");
				 	str1=(char*)malloc(sizeof(char)*MAX_PATH);
				 	sprintf(str1,"table/%s/file%d.dat",tab,ret);
				 	fpz=fopen(str1,"r");

				 	for(int j=0;j<inp1.count;j++){
				 		if(inp1.col[j].type==INT){
				 			fread(&c,1,sizeof(int),fpz);
				 			cout<<c<<"\t";
						}
				 		else if(inp1.col[j].type==VARCHAR){
				 			fread(d,1,sizeof(char)*MAX_NAME,fpz);
				 			cout<<d<<"\t";
				 		}
				 	}
					printf("\n------------------------------------\n");
				 	fclose(fpz);
				 	free(str1);

				 }
			}
			else if(inp1.col[0].type==VARCHAR){
				cout<<"\nenter  key[col 0] to search\n";
				cin>>pri_char;
				void *arr[MAX_NAME];
				arr[0]=(char*)malloc(sizeof(char)*MAX_NAME);
				arr[0]=pri_char;
				ret=mytree.get_record(*(int*)arr[0]);
				if (ret == BPTREE_SEARCH_NOT_FOUND){
					printf("\nSearching (%s) -> NOT FOUND !!", pri_char);
				}
			    else{
				   // printf("\nSearching (%s) -> record num = %d", pri_char, ret);
				    printf("\n%s exists\n\n",pri_char);

				    //print the details of the particular row;
				   	FILE *fpz;
				   	char *str1;
				   	str1=(char*)malloc(sizeof(char)*MAX_PATH);
				   	sprintf(str1,"table/%s/file%d.dat",tab,ret);
				   	fpz=fopen(str1,"r");
				   	printf("\n------------------------------------\n");
				 	for(int j=0;j<inp1.count;j++){
				   		if(inp1.col[j].type==INT){
				   			fread(&c,1,sizeof(int),fpz);
				   			cout<<c<<"\t";
				   		}
				   		else if(inp1.col[j].type==VARCHAR){
				   			fread(d,1,sizeof(char)*MAX_NAME,fpz);
				   			cout<<d<<"\t";
				   		}
				   	}
				 	printf("\n-------------------------------------\n");
				   	fclose(fpz);
				   	free(str1);
			    }

			}

		}
		else{
			printf("%s doesn't exists!!!\n\n",tab);
		}
	}
	else{
		cout<<"wrong input!!!\n\n";
		return;
	}
	free(tab);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8a8, %rsp            # imm = 0x8A8
movq 0x65fd(%rip), %rdi      # 0x9fd0
leaq 0x2874(%rip), %rsi      # 0x624e
callq 0x2180
movq 0x65fa(%rip), %rdi      # 0x9fe0
leaq 0x1c(%rsp), %rbx
movq %rbx, %rsi
callq 0x2100
pushq $0x32
popq %rdi
callq 0x2240
movq %rax, %r15
movl (%rbx), %eax
movq 0x65c9(%rip), %rdi      # 0x9fd0
cmpl $0x2, %eax
je 0x3a4c
cmpl $0x1, %eax
jne 0x3b43
leaq 0x2746(%rip), %rsi      # 0x6162
callq 0x2180
movq 0x65b8(%rip), %rdi      # 0x9fe0
movq %r15, %rsi
callq 0x459a
movq %r15, %rdi
callq 0x315d
testl %eax, %eax
je 0x3ca8
leaq 0x283b(%rip), %rdi      # 0x6282
jmp 0x3cb8
leaq 0x270f(%rip), %rsi      # 0x6162
callq 0x2180
movq 0x6581(%rip), %rdi      # 0x9fe0
movq %r15, %rsi
callq 0x459a
movq %r15, %rdi
callq 0x315d
testl %eax, %eax
je 0x3cb1
leaq 0x2836(%rip), %rdi      # 0x62b4
movq %r15, %rsi
xorl %eax, %eax
callq 0x2030
leaq 0x2df2(%rip), %rsi      # 0x6881
movq %r15, 0x10(%rsp)
movq %r15, %rdi
callq 0x2c0a
movq %rax, 0x8(%rsp)
movq 0x6528(%rip), %r13      # 0x9fd0
leaq 0x2718(%rip), %rbp      # 0x61c7
leaq 0x2713(%rip), %rbx      # 0x61c9
leaq 0x2714(%rip), %r15      # 0x61d1
movl $0x420, %esi            # imm = 0x420
leaq 0x98(%rsp), %rdi
pushq $0x1
popq %rdx
movq 0x8(%rsp), %rcx
callq 0x22a0
testq %rax, %rax
je 0x3b54
leaq 0x114(%rsp), %r12
xorl %r14d, %r14d
movslq 0x478(%rsp), %rax
cmpq %rax, %r14
jge 0x3abd
movq %r13, %rdi
movq %r12, %rsi
callq 0x2180
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2180
movl 0x14(%r12), %esi
movq %rax, %rdi
callq 0x2310
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2180
movl 0x18(%r12), %esi
movq %rax, %rdi
callq 0x2310
movq %r13, %rdi
movq %r15, %rsi
callq 0x2180
incq %r14
addq $0x1c, %r12
jmp 0x3ae7
leaq 0x283c(%rip), %rsi      # 0x6386
callq 0x2180
jmp 0x3cca
leaq 0x4b8(%rsp), %rdi
movq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x45b2
movl 0x128(%rsp), %eax
cmpl $0x2, %eax
je 0x3cdc
cmpl $0x1, %eax
jne 0x3cc2
movq 0x6447(%rip), %rdi      # 0x9fd0
leaq 0x2748(%rip), %rsi      # 0x62d8
callq 0x2180
movq 0x6444(%rip), %rdi      # 0x9fe0
leaq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0x2100
movl (%r14), %esi
leaq 0x4b8(%rsp), %rdi
callq 0x255a
movl (%r14), %esi
cmpl $-0x1, %eax
je 0x3e30
movl %eax, %ebx
leaq 0x2747(%rip), %rdi      # 0x6315
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2030
leaq 0x2fb4(%rip), %rdi      # 0x6b92
callq 0x22c0
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x2240
movq %rax, %r14
leaq 0x2557(%rip), %rsi      # 0x614e
movq %rax, %rdi
movq %r15, %rdx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x2040
leaq 0x2c74(%rip), %rsi      # 0x6881
movq %r14, 0x8(%rsp)
movq %r14, %rdi
callq 0x21f0
movq %rax, %r15
leaq 0x128(%rsp), %r12
movq 0x63a4(%rip), %r14      # 0x9fd0
leaq 0x259e(%rip), %rbx      # 0x61d1
movslq 0x478(%rsp), %rax
cmpq %rax, %rbp
jge 0x3dff
movl (%r12), %eax
cmpl $0x2, %eax
je 0x3c73
cmpl $0x1, %eax
jne 0x3c9f
leaq 0x4(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
movq %r15, %rcx
callq 0x22a0
movl 0x4(%rsp), %esi
movq %r14, %rdi
callq 0x2310
jmp 0x3c94
leaq 0x60(%rsp), %r13
movq %r13, %rdi
pushq $0x1
popq %rsi
pushq $0x32
popq %rdx
movq %r15, %rcx
callq 0x22a0
movq %r14, %rdi
movq %r13, %rsi
callq 0x2180
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2180
incq %rbp
addq $0x1c, %r12
jmp 0x3c33
leaq 0x25e2(%rip), %rdi      # 0x6291
jmp 0x3cb8
leaq 0x26b7(%rip), %rdi      # 0x636f
movq %r15, %rsi
xorl %eax, %eax
callq 0x2030
movq %r15, %rdi
callq 0x2200
addq $0x8a8, %rsp            # imm = 0x8A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x62ed(%rip), %rdi      # 0x9fd0
leaq 0x263a(%rip), %rsi      # 0x6324
callq 0x2180
movq 0x62ea(%rip), %rdi      # 0x9fe0
leaq 0x20(%rsp), %rbx
movq %rbx, %rsi
callq 0x459a
movl (%rbx), %esi
leaq 0x4b8(%rsp), %rdi
callq 0x255a
cmpl $-0x1, %eax
je 0x3e43
movl %eax, %ebx
leaq 0x263e(%rip), %rdi      # 0x6362
xorl %ebp, %ebp
leaq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x2030
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x2240
movq %rax, %r14
leaq 0x2408(%rip), %rsi      # 0x614e
movq %rax, %rdi
movq %r15, %rdx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x2040
leaq 0x2b25(%rip), %rsi      # 0x6881
movq %r14, 0x8(%rsp)
movq %r14, %rdi
callq 0x21f0
movq %rax, %r15
leaq 0x2e1f(%rip), %rdi      # 0x6b92
callq 0x22c0
leaq 0x128(%rsp), %r12
movq 0x6249(%rip), %r14      # 0x9fd0
leaq 0x2443(%rip), %rbx      # 0x61d1
movslq 0x478(%rsp), %rax
cmpq %rax, %rbp
jge 0x3e08
movl (%r12), %eax
cmpl $0x2, %eax
je 0x3dca
cmpl $0x1, %eax
jne 0x3df6
leaq 0x4(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x4
popq %rdx
movq %r15, %rcx
callq 0x22a0
movl 0x4(%rsp), %esi
movq %r14, %rdi
callq 0x2310
jmp 0x3deb
leaq 0x60(%rsp), %r13
movq %r13, %rdi
pushq $0x1
popq %rsi
pushq $0x32
popq %rdx
movq %r15, %rcx
callq 0x22a0
movq %r14, %rdi
movq %r13, %rsi
callq 0x2180
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2180
incq %rbp
addq $0x1c, %r12
jmp 0x3d8e
leaq 0x2d8c(%rip), %rdi      # 0x6b92
jmp 0x3e0f
leaq 0x2d5c(%rip), %rdi      # 0x6b6b
callq 0x22c0
movq %r15, %rdi
callq 0x2190
movq 0x8(%rsp), %rdi
callq 0x2200
movq 0x10(%rsp), %r15
jmp 0x3cc2
leaq 0x24be(%rip), %rdi      # 0x62f5
xorl %eax, %eax
callq 0x2030
jmp 0x3cc2
leaq 0x24f8(%rip), %rdi      # 0x6342
leaq 0x20(%rsp), %rsi
jmp 0x3cbb