// File: 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
movq %rsp, %rbp
subq $0xa80, %rsp            # imm = 0xA80
movq 0xb6ae(%rip), %rdi      # 0xffd0
leaq 0x6bd6(%rip), %rsi      # 0xb4ff
callq 0x2190
movq 0xb6ab(%rip), %rdi      # 0xffe0
leaq -0x4(%rbp), %rsi
callq 0x2100
movl $0x32, %edi
callq 0x2260
movq %rax, -0x10(%rbp)
cmpl $0x1, -0x4(%rbp)
jne 0x49b2
movq 0xb677(%rip), %rdi      # 0xffd0
leaq 0x69f8(%rip), %rsi      # 0xb358
callq 0x2190
movq -0x10(%rbp), %rsi
movq 0xb670(%rip), %rdi      # 0xffe0
callq 0x5f30
movq -0x10(%rbp), %rdi
callq 0x3b40
movl %eax, -0x14(%rbp)
cmpl $0x1, -0x14(%rbp)
jne 0x499b
movq -0x10(%rbp), %rsi
leaq 0x6ba1(%rip), %rdi      # 0xb533
movb $0x0, %al
callq 0x2030
jmp 0x49ad
movq -0x10(%rbp), %rsi
leaq 0x6b9c(%rip), %rdi      # 0xb542
movb $0x0, %al
callq 0x2030
jmp 0x4f6d
cmpl $0x2, -0x4(%rbp)
jne 0x4f56
movq 0xb60d(%rip), %rdi      # 0xffd0
leaq 0x698e(%rip), %rsi      # 0xb358
callq 0x2190
movq -0x10(%rbp), %rsi
movq 0xb606(%rip), %rdi      # 0xffe0
callq 0x5f30
movq -0x10(%rbp), %rdi
callq 0x3b40
movl %eax, -0x18(%rbp)
cmpl $0x1, -0x18(%rbp)
jne 0x4f42
movq -0x10(%rbp), %rsi
leaq 0x6b65(%rip), %rdi      # 0xb565
movb $0x0, %al
callq 0x2030
movq -0x10(%rbp), %rdi
leaq 0x7268(%rip), %rsi      # 0xbc7a
callq 0x30e0
movq %rax, -0x440(%rbp)
movl $0x0, -0x444(%rbp)
movq -0x440(%rbp), %rcx
leaq -0x438(%rbp), %rdi
movl $0x420, %esi            # imm = 0x420
movl $0x1, %edx
callq 0x22d0
cmpq $0x0, %rax
je 0x4b1f
movl $0x0, -0x444(%rbp)
movl -0x444(%rbp), %eax
cmpl -0x58(%rbp), %eax
jge 0x4b1a
leaq -0x438(%rbp), %rsi
addq $0x7c, %rsi
movslq -0x444(%rbp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rsi
movq 0xb548(%rip), %rdi      # 0xffd0
callq 0x2190
movq %rax, %rdi
leaq 0x6952(%rip), %rsi      # 0xb3e9
callq 0x2190
movq %rax, %rdi
leaq -0x438(%rbp), %rax
addq $0x7c, %rax
movslq -0x444(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %esi
callq 0x2330
movq %rax, %rdi
leaq 0x6921(%rip), %rsi      # 0xb3eb
callq 0x2190
movq %rax, %rdi
leaq -0x438(%rbp), %rax
addq $0x7c, %rax
movslq -0x444(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %esi
callq 0x2330
movq 0xb4d6(%rip), %rdi      # 0xffd0
leaq 0x68f2(%rip), %rsi      # 0xb3f3
callq 0x2190
movl -0x444(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x444(%rbp)
jmp 0x4a59
jmp 0x4a28
movq -0x10(%rbp), %rsi
leaq -0x8b0(%rbp), %rdi
callq 0x5f80
movl $0x0, -0x8b4(%rbp)
cmpl $0x1, -0x3a8(%rbp)
jne 0x4d2b
movq 0xb483(%rip), %rdi      # 0xffd0
leaq 0x6a35(%rip), %rsi      # 0xb589
callq 0x2190
movq 0xb480(%rip), %rdi      # 0xffe0
leaq -0x448(%rbp), %rsi
callq 0x2100
movl -0x448(%rbp), %esi
leaq -0x8b0(%rbp), %rdi
callq 0x2670
movl %eax, -0x8b4(%rbp)
cmpl $-0x1, -0x8b4(%rbp)
jne 0x4ba6
movl -0x448(%rbp), %esi
leaq 0x6a0c(%rip), %rdi      # 0xb5a6
movb $0x0, %al
callq 0x2030
jmp 0x4d26
movl -0x448(%rbp), %esi
leaq 0x6a13(%rip), %rdi      # 0xb5c6
movb $0x0, %al
callq 0x2030
leaq 0x6801(%rip), %rdi      # 0xb3c2
movb $0x0, %al
callq 0x2030
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x2260
movq %rax, -0x8c8(%rbp)
movq -0x8c8(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl -0x8b4(%rbp), %ecx
leaq 0x6753(%rip), %rsi      # 0xb344
movb $0x0, %al
callq 0x2040
movq -0x8c8(%rbp), %rdi
leaq 0x7074(%rip), %rsi      # 0xbc7a
callq 0x2210
movq %rax, -0x8c0(%rbp)
movl $0x0, -0x8cc(%rbp)
movl -0x8cc(%rbp), %eax
cmpl -0x58(%rbp), %eax
jge 0x4d00
leaq -0x438(%rbp), %rax
addq $0x7c, %rax
movslq -0x8cc(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
cmpl $0x1, 0x14(%rax)
jne 0x4c8a
movq -0x8c0(%rbp), %rcx
leaq -0x44c(%rbp), %rdi
movl $0x1, %esi
movl $0x4, %edx
callq 0x22d0
movl -0x44c(%rbp), %esi
movq 0xb35c(%rip), %rdi      # 0xffd0
callq 0x2330
movq %rax, %rdi
leaq 0x6770(%rip), %rsi      # 0xb3f3
callq 0x2190
jmp 0x4cea
leaq -0x438(%rbp), %rax
addq $0x7c, %rax
movslq -0x8cc(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
cmpl $0x2, 0x14(%rax)
jne 0x4ce8
leaq -0x480(%rbp), %rdi
movq -0x8c0(%rbp), %rcx
movl $0x1, %esi
movl $0x32, %edx
callq 0x22d0
leaq -0x480(%rbp), %rsi
movq 0xb2fc(%rip), %rdi      # 0xffd0
callq 0x2190
movq %rax, %rdi
leaq 0x6710(%rip), %rsi      # 0xb3f3
callq 0x2190
jmp 0x4cea
jmp 0x4cec
movl -0x8cc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8cc(%rbp)
jmp 0x4c1c
leaq 0x66bb(%rip), %rdi      # 0xb3c2
movb $0x0, %al
callq 0x2030
movq -0x8c0(%rbp), %rdi
callq 0x21a0
movq -0x8c8(%rbp), %rdi
callq 0x2220
jmp 0x4f40
cmpl $0x2, -0x3a8(%rbp)
jne 0x4f3e
movq 0xb291(%rip), %rdi      # 0xffd0
leaq 0x688f(%rip), %rsi      # 0xb5d5
callq 0x2190
leaq -0x4c0(%rbp), %rsi
movq 0xb287(%rip), %rdi      # 0xffe0
callq 0x5f30
movl $0x32, %edi
callq 0x2260
movq %rax, -0xa60(%rbp)
leaq -0x4c0(%rbp), %rax
movq %rax, -0xa60(%rbp)
movq -0xa60(%rbp), %rax
movl (%rax), %esi
leaq -0x8b0(%rbp), %rdi
callq 0x2670
movl %eax, -0x8b4(%rbp)
cmpl $-0x1, -0x8b4(%rbp)
jne 0x4dbb
leaq -0x4c0(%rbp), %rsi
leaq 0x6844(%rip), %rdi      # 0xb5f3
movb $0x0, %al
callq 0x2030
jmp 0x4f3c
leaq -0x4c0(%rbp), %rsi
leaq 0x684a(%rip), %rdi      # 0xb613
movb $0x0, %al
callq 0x2030
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x2260
movq %rax, -0xa70(%rbp)
movq -0xa70(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl -0x8b4(%rbp), %ecx
leaq 0x654b(%rip), %rsi      # 0xb344
movb $0x0, %al
callq 0x2040
movq -0xa70(%rbp), %rdi
leaq 0x6e6c(%rip), %rsi      # 0xbc7a
callq 0x2210
movq %rax, -0xa68(%rbp)
leaq 0x65a1(%rip), %rdi      # 0xb3c2
movb $0x0, %al
callq 0x2030
movl $0x0, -0xa74(%rbp)
movl -0xa74(%rbp), %eax
cmpl -0x58(%rbp), %eax
jge 0x4f16
leaq -0x438(%rbp), %rax
addq $0x7c, %rax
movslq -0xa74(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
cmpl $0x1, 0x14(%rax)
jne 0x4ea0
movq -0xa68(%rbp), %rcx
leaq -0x44c(%rbp), %rdi
movl $0x1, %esi
movl $0x4, %edx
callq 0x22d0
movl -0x44c(%rbp), %esi
movq 0xb146(%rip), %rdi      # 0xffd0
callq 0x2330
movq %rax, %rdi
leaq 0x655a(%rip), %rsi      # 0xb3f3
callq 0x2190
jmp 0x4f00
leaq -0x438(%rbp), %rax
addq $0x7c, %rax
movslq -0xa74(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
cmpl $0x2, 0x14(%rax)
jne 0x4efe
leaq -0x480(%rbp), %rdi
movq -0xa68(%rbp), %rcx
movl $0x1, %esi
movl $0x32, %edx
callq 0x22d0
leaq -0x480(%rbp), %rsi
movq 0xb0e6(%rip), %rdi      # 0xffd0
callq 0x2190
movq %rax, %rdi
leaq 0x64fa(%rip), %rsi      # 0xb3f3
callq 0x2190
jmp 0x4f00
jmp 0x4f02
movl -0xa74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa74(%rbp)
jmp 0x4e32
leaq 0x6703(%rip), %rdi      # 0xb620
movb $0x0, %al
callq 0x2030
movq -0xa68(%rbp), %rdi
callq 0x21a0
movq -0xa70(%rbp), %rdi
callq 0x2220
jmp 0x4f3e
jmp 0x4f40
jmp 0x4f54
movq -0x10(%rbp), %rsi
leaq 0x66fb(%rip), %rdi      # 0xb648
movb $0x0, %al
callq 0x2030
jmp 0x4f6b
movq 0xb073(%rip), %rdi      # 0xffd0
leaq 0x66fb(%rip), %rsi      # 0xb65f
callq 0x2190
jmp 0x4f76
jmp 0x4f6d
movq -0x10(%rbp), %rdi
callq 0x2220
addq $0xa80, %rsp            # imm = 0xA80
popq %rbp
retq
nop