// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x598, %rsp            # imm = 0x598
movl $0x3e9, %edi            # imm = 0x3E9
callq 0x2240
movq %rax, %rbx
movq 0x6c43(%rip), %rdi      # 0x9fd0
leaq 0x2dce(%rip), %rsi      # 0x6162
callq 0x2180
movq 0x6c40(%rip), %rdi      # 0x9fe0
movq %rbx, %rsi
callq 0x459a
movq %rbx, %rdi
callq 0x315d
testl %eax, %eax
je 0x3647
movq 0x6c11(%rip), %r14      # 0x9fd0
leaq 0x2dc4(%rip), %rsi      # 0x618a
movq %r14, %rdi
callq 0x2180
leaq 0x34ac(%rip), %rsi      # 0x6881
movq %rbx, 0x38(%rsp)
movq %rbx, %rdi
callq 0x2c0a
movq %rax, 0x10(%rsp)
leaq 0x2dd9(%rip), %r12      # 0x61c7
leaq 0x2dd4(%rip), %rbx      # 0x61c9
leaq 0x2dd5(%rip), %rbp      # 0x61d1
movq %rax, 0x8(%rsp)
movl $0x420, %esi            # imm = 0x420
leaq 0x178(%rsp), %rdi
pushq $0x1
popq %rdx
movq 0x10(%rsp), %rcx
callq 0x22a0
testq %rax, %rax
je 0x34bd
leaq 0x3768(%rip), %rdi      # 0x6b93
callq 0x22c0
movq %r14, %rdi
leaq 0x2d6b(%rip), %rsi      # 0x61a5
callq 0x2180
leaq 0x374c(%rip), %rdi      # 0x6b92
callq 0x22c0
movl 0x558(%rsp), %eax
leaq 0x1f4(%rsp), %r13
movq %rax, 0x8(%rsp)
xorl %r15d, %r15d
cltq
cmpq %rax, %r15
jge 0x3401
movq %r14, %rdi
movq %r13, %rsi
callq 0x2180
movq %rax, %rdi
movq %r12, %rsi
callq 0x2180
movl 0x14(%r13), %esi
movq %rax, %rdi
callq 0x2310
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2180
movl 0x18(%r13), %esi
movq %rax, %rdi
callq 0x2310
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2180
incq %r15
movl 0x558(%rsp), %eax
addq $0x1c, %r13
jmp 0x3462
leaq 0x36ce(%rip), %rdi      # 0x6b92
callq 0x22c0
xorl %r13d, %r13d
movq 0x8(%rsp), %rdx
testl %edx, %edx
cmovlel %r13d, %edx
leaq 0x40(%rsp), %r15
movq 0x6afd(%rip), %r12      # 0x9fe0
xorl %ebp, %ebp
movq %rdx, 0x8(%rsp)
cmpq %rdx, %r13
je 0x3669
imulq $0x1c, %r13, %rax
leaq 0x1f4(%rsp), %rcx
leaq (%rcx,%rax), %r14
movl 0x14(%rcx,%rax), %eax
cmpl $0x2, %eax
je 0x35a4
cmpl $0x1, %eax
jne 0x3617
pushq $0x4
popq %rdi
callq 0x2240
movq %rax, %rbx
movq %rax, 0x80(%rsp,%r13,8)
leaq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
andq $0x0, 0x20(%rsp)
movb $0x0, 0x28(%rsp)
movq %r12, %rdi
leaq 0x18(%rsp), %rsi
callq 0x2220
movq 0x20(%rsp), %rax
movl 0x18(%r14), %esi
cmpq %rsi, %rax
ja 0x368a
addl $0x4, %ebp
movq 0x18(%rsp), %rcx
decq %rax
xorl %esi, %esi
pushq $0x1
popq %rdx
testl %eax, %eax
js 0x3606
movl %eax, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movzbl (%rcx,%rdi), %edi
leal -0x3a(%rdi), %r8d
cmpb $-0xb, %r8b
jbe 0x361f
addl $-0x30, %edi
imull %edx, %edi
addl %esi, %edi
imull $0xa, %edx, %edx
decq %rax
movl %edi, %esi
jmp 0x3570
pushq $0x33
popq %rdi
callq 0x2240
movq %rax, 0x10(%rsp)
movq %rax, 0x80(%rsp,%r13,8)
xorl %ebx, %ebx
testb $0x1, %bl
jne 0x35f7
movq %r12, %rdi
movq %r15, %rsi
callq 0x459a
movq %r15, %rdi
callq 0x20a0
movl 0x18(%r14), %ecx
cmpq %rcx, %rax
setbe %bl
jbe 0x35f2
movq 0x69ea(%rip), %rdi      # 0x9fd0
leaq 0x2c0b(%rip), %rsi      # 0x61f8
callq 0x2180
addl $0x33, %ebp
jmp 0x35bb
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x2170
jmp 0x3612
movl %esi, (%rbx)
leaq 0x18(%rsp), %rdi
callq 0x20b0
movq 0x8(%rsp), %rdx
incq %r13
jmp 0x34ea
leaq 0x34bf(%rip), %rdi      # 0x6ae5
callq 0x22c0
leaq 0x18(%rsp), %rdi
callq 0x20b0
addq $0x598, %rsp            # imm = 0x598
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2b27(%rip), %rdi      # 0x6175
movq %rbx, %rsi
xorl %eax, %eax
addq $0x598, %rsp            # imm = 0x598
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2030
leaq 0x80(%rsp), %rsi
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, %edx
callq 0x31b5
movq %rbx, %rdi
callq 0x2200
jmp 0x3635
leaq 0x2b42(%rip), %rdi      # 0x61d3
xorl %eax, %eax
callq 0x2030
jmp 0x362b
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x2320