// File: pawan231[P]MiniDataBase/include/insert.h
// Function: insert()
void insert(){
	char *tab;
	tab=(char*)malloc(sizeof(char)*MAX_PATH+1);
	cout<<"enter table name: ";
	cin>>tab;
	int check=search_table(tab);
	if(check==0){
		printf("Table %s not exists\n",tab);
		return ;
	}

	else{
		cout<<"\nTable exists enter data\n\n";
		char dir[100];
		strcpy(dir,"./table/");
		strcat(dir,tab);
		strcat(dir,"/met");
     	table inp1;
		int count;
		//read column details from file;
		FILE *fp = open_file(tab, const_cast<char*>("r"));
		int i=0;
		while(fread(&inp1,sizeof(table),1,fp)){
			printf("------------------------------------\n");
			cout<<"\ninsert the following details ::\n";
			printf("\n------------------------------------\n");
			count=inp1.count;
			for(i=0;i<inp1.count;i++){
				cout<<inp1.col[i].col_name<<"("<<inp1.col[i].type<<"),size:"<<inp1.col[i].size;
				cout<<"\t";
			}
		}
		printf("\n------------------------------------\n");
		//enter data;
		char var[MAX_NAME+1];
		void * data[MAX_ATTR];
		//void *data1[MAX_ATTR];

		//input data for the table of desired datatype 1.int 2.varchar;
		int size=0;
		int total=0;
		for(int i=0;i<count;i++){
			if(inp1.col[i].type==INT){
				data[i] =(int*) malloc(sizeof(int));
				total+=sizeof(int);
				string inp_int;
				cin>>inp_int;
				if(inp_int.length() > (unsigned)inp1.col[i].size){
					printf("\nwrong input, size <= %d\nexiting...\n",inp1.col[i].size);
					return;
				}else{
					//verify if entered input is integer and not a string;
					int num = 0;
					int factor_10 = 1;
					for(int j=inp_int.length()-1;j>=0;j--){
						if(inp_int[j] < 48 || inp_int[j] > 57){
							printf("\nwrong input, input should be integer\nexiting...\n");
							return;
						}else{
							num += (inp_int[j] - 48)*factor_10;
							factor_10 = factor_10 * 10;
						}
					}
					*((int*)data[i])=num;
				}
				size++;
			}else if(inp1.col[i].type==VARCHAR){
				//cout<<"inside varchar\n";
				data[i] = malloc(sizeof(char) * (MAX_NAME + 1));
				int flag=1;
				while(flag){
					cin>>var;
					total+=sizeof(char) * (MAX_NAME + 1);
					if(strlen(var)>(unsigned int)inp1.col[i].size){
						cout<<"error\nEntered size of string is greater than specified \n";
					}else flag=0;
				}
				strcpy((char*)(data[i]),var);
				//cout<<(char *)data[1]<<endl;
				size++;
			}
		}
		insert_command(tab,data,total);
	}
	free(tab);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x640, %rsp            # imm = 0x640
movl $0x3e9, %edi            # imm = 0x3E9
callq 0x2260
movq %rax, -0x8(%rbp)
movq 0xc180(%rip), %rdi      # 0xffd0
leaq 0x7501(%rip), %rsi      # 0xb358
callq 0x2190
movq -0x8(%rbp), %rsi
movq 0xc179(%rip), %rdi      # 0xffe0
callq 0x5f30
movq -0x8(%rbp), %rdi
callq 0x3b40
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jne 0x3e95
movq -0x8(%rbp), %rsi
leaq 0x74e2(%rip), %rdi      # 0xb36b
movb $0x0, %al
callq 0x2030
jmp 0x43e9
movq 0xc134(%rip), %rdi      # 0xffd0
leaq 0x74dd(%rip), %rsi      # 0xb380
callq 0x2190
leaq -0x70(%rbp), %rdi
leaq 0x71a7(%rip), %rsi      # 0xb05a
callq 0x2180
leaq -0x70(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x2350
leaq -0x70(%rbp), %rdi
leaq 0x74cb(%rip), %rsi      # 0xb39b
callq 0x2350
movq -0x8(%rbp), %rdi
leaq 0x7d9a(%rip), %rsi      # 0xbc7a
callq 0x30e0
movq %rax, -0x4a0(%rbp)
movl $0x0, -0x4a4(%rbp)
movq -0x4a0(%rbp), %rcx
leaq -0x490(%rbp), %rdi
movl $0x420, %esi            # imm = 0x420
movl $0x1, %edx
callq 0x22d0
cmpq $0x0, %rax
je 0x402b
leaq 0x749f(%rip), %rdi      # 0xb3c3
movb $0x0, %al
callq 0x2030
movq 0xc09e(%rip), %rdi      # 0xffd0
leaq 0x7467(%rip), %rsi      # 0xb3a0
callq 0x2190
leaq 0x747d(%rip), %rdi      # 0xb3c2
movb $0x0, %al
callq 0x2030
movl -0xb0(%rbp), %eax
movl %eax, -0x494(%rbp)
movl $0x0, -0x4a4(%rbp)
movl -0x4a4(%rbp), %eax
cmpl -0xb0(%rbp), %eax
jge 0x4026
leaq -0x490(%rbp), %rsi
addq $0x7c, %rsi
movslq -0x4a4(%rbp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rsi
movq 0xc03c(%rip), %rdi      # 0xffd0
callq 0x2190
movq %rax, %rdi
leaq 0x7446(%rip), %rsi      # 0xb3e9
callq 0x2190
movq %rax, %rdi
leaq -0x490(%rbp), %rax
addq $0x7c, %rax
movslq -0x4a4(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %esi
callq 0x2330
movq %rax, %rdi
leaq 0x7415(%rip), %rsi      # 0xb3eb
callq 0x2190
movq %rax, %rdi
leaq -0x490(%rbp), %rax
addq $0x7c, %rax
movslq -0x4a4(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %esi
callq 0x2330
movq 0xbfca(%rip), %rdi      # 0xffd0
leaq 0x73e6(%rip), %rsi      # 0xb3f3
callq 0x2190
movl -0x4a4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4a4(%rbp)
jmp 0x3f62
jmp 0x3ef6
leaq 0x7390(%rip), %rdi      # 0xb3c2
movb $0x0, %al
callq 0x2030
movl $0x0, -0x5d4(%rbp)
movl $0x0, -0x5d8(%rbp)
movl $0x0, -0x5dc(%rbp)
movl -0x5dc(%rbp), %eax
cmpl -0x494(%rbp), %eax
jge 0x43ca
leaq -0x490(%rbp), %rax
addq $0x7c, %rax
movslq -0x5dc(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
cmpl $0x1, 0x14(%rax)
jne 0x42c4
movl $0x4, %edi
callq 0x2260
movq %rax, %rcx
movslq -0x5dc(%rbp), %rax
movq %rcx, -0x5d0(%rbp,%rax,8)
movl -0x5d8(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x5d8(%rbp)
leaq -0x600(%rbp), %rdi
movq %rdi, -0x628(%rbp)
callq 0x2270
movq -0x628(%rbp), %rsi
movq 0xbf08(%rip), %rdi      # 0xffe0
callq 0x2240
jmp 0x40df
leaq -0x600(%rbp), %rdi
callq 0x22e0
leaq -0x490(%rbp), %rcx
addq $0x7c, %rcx
movslq -0x5dc(%rbp), %rdx
imulq $0x1c, %rdx, %rdx
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
cmpq %rcx, %rax
jbe 0x4167
movslq -0x5dc(%rbp), %rcx
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rcx, %rax
movl -0x3fc(%rbp,%rax), %esi
leaq 0x72c9(%rip), %rdi      # 0xb3f5
xorl %eax, %eax
callq 0x2030
jmp 0x4135
movl $0x1, -0x610(%rbp)
jmp 0x42a2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x608(%rbp)
movl %eax, -0x60c(%rbp)
leaq -0x600(%rbp), %rdi
callq 0x2110
jmp 0x43f2
movl $0x0, -0x614(%rbp)
movl $0x1, -0x618(%rbp)
leaq -0x600(%rbp), %rdi
callq 0x22e0
subq $0x1, %rax
movl %eax, -0x61c(%rbp)
cmpl $0x0, -0x61c(%rbp)
jl 0x4272
movslq -0x61c(%rbp), %rsi
leaq -0x600(%rbp), %rdi
callq 0x23a0
movq %rax, -0x630(%rbp)
jmp 0x41ba
movq -0x630(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x30, %eax
jl 0x41f4
movslq -0x61c(%rbp), %rsi
leaq -0x600(%rbp), %rdi
callq 0x23a0
movq %rax, -0x638(%rbp)
jmp 0x41e5
movq -0x638(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x39, %eax
jle 0x4213
leaq 0x721f(%rip), %rdi      # 0xb41a
xorl %eax, %eax
callq 0x2030
jmp 0x4204
movl $0x1, -0x610(%rbp)
jmp 0x42a2
movslq -0x61c(%rbp), %rsi
leaq -0x600(%rbp), %rdi
callq 0x23a0
movq %rax, -0x640(%rbp)
jmp 0x422f
movq -0x640(%rbp), %rax
movsbl (%rax), %eax
subl $0x30, %eax
imull -0x618(%rbp), %eax
addl -0x614(%rbp), %eax
movl %eax, -0x614(%rbp)
imull $0xa, -0x618(%rbp), %eax
movl %eax, -0x618(%rbp)
jmp 0x425e
movl -0x61c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x61c(%rbp)
jmp 0x4191
movl -0x614(%rbp), %ecx
movslq -0x5dc(%rbp), %rax
movq -0x5d0(%rbp,%rax,8), %rax
movl %ecx, (%rax)
movl -0x5d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5d4(%rbp)
movl $0x0, -0x610(%rbp)
leaq -0x600(%rbp), %rdi
callq 0x2110
movl -0x610(%rbp), %eax
testl %eax, %eax
je 0x42bf
jmp 0x42ba
jmp 0x43e9
jmp 0x43b4
leaq -0x490(%rbp), %rax
addq $0x7c, %rax
movslq -0x5dc(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
cmpl $0x2, 0x14(%rax)
jne 0x43b2
movl $0x33, %edi
callq 0x2260
movq %rax, %rcx
movslq -0x5dc(%rbp), %rax
movq %rcx, -0x5d0(%rbp,%rax,8)
movl $0x1, -0x620(%rbp)
cmpl $0x0, -0x620(%rbp)
je 0x4388
leaq -0x4e0(%rbp), %rsi
movq 0xbcbc(%rip), %rdi      # 0xffe0
callq 0x5f30
movslq -0x5d8(%rbp), %rax
addq $0x33, %rax
movl %eax, -0x5d8(%rbp)
leaq -0x4e0(%rbp), %rdi
callq 0x20a0
leaq -0x490(%rbp), %rcx
addq $0x7c, %rcx
movslq -0x5dc(%rbp), %rdx
imulq $0x1c, %rdx, %rdx
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
cmpq %rcx, %rax
jbe 0x437c
movq 0xbc62(%rip), %rdi      # 0xffd0
leaq 0x70d7(%rip), %rsi      # 0xb44c
callq 0x2190
jmp 0x4386
movl $0x0, -0x620(%rbp)
jmp 0x430d
movslq -0x5dc(%rbp), %rax
movq -0x5d0(%rbp,%rax,8), %rdi
leaq -0x4e0(%rbp), %rsi
callq 0x2180
movl -0x5d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5d4(%rbp)
jmp 0x43b4
jmp 0x43b6
movl -0x5dc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5dc(%rbp)
jmp 0x4057
movq -0x8(%rbp), %rdi
leaq -0x5d0(%rbp), %rsi
movl -0x5d8(%rbp), %edx
callq 0x3bb0
movq -0x8(%rbp), %rdi
callq 0x2220
addq $0x640, %rsp            # imm = 0x640
popq %rbp
retq
movq -0x608(%rbp), %rdi
callq 0x2340
nop