// File: pawan231[P]MiniDataBase/include/insert.h
// Function: insert_command(char*, void**, int)
void insert_command(char tname[],void *data[],int total){
	table *temp;
	int ret;
	BPtree obj(tname);
	//open meta data
	FILE *fp=open_file(tname,const_cast<char*>("r"));
	temp=(table*)malloc(sizeof(table));
	fread(temp,sizeof(table),1,fp);

	//insert into table and write to btree file nodes
	ret=obj.insert_record(*((int *)data[0]),temp->rec_count);
	if(ret==2){
		cout<<"key already exists\n";
		cout<<"exiting...\n";
		return ;
	}

	//if no error occurred during insertion of key

	//update the meta data;
	fp=open_file(tname,const_cast<char*>("w+"));
	int file_num=temp->rec_count;
	temp->rec_count=temp->rec_count+1;
	temp->data_size=total;
	fwrite(temp,sizeof(table),1,fp);
	fclose(fp);

	//update the particular entry of inserted data to file;
	char *str;
	str=(char *)malloc(sizeof(char)*MAX_PATH);
	sprintf(str,"table/%s/file%d.dat",tname,file_num);
	//cout<<str<<endl;
	FILE *fpr=fopen(str,"w+");
	int x;
	char y[MAX_NAME];
	for(int j=0;j<temp->count;j++){
		if(temp->col[j].type==INT){
			 x = *(int *)data[j];
			fwrite(&x,sizeof(int),1,fpr);
		}
		else if(temp->col[j].type==VARCHAR){
			strcpy(y,(char *)data[j]);
			fwrite(y,sizeof(char)*MAX_NAME,1,fpr);
		}
	}
	fclose(fpr);
	free(str);
	free(temp);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x480, %rsp            # imm = 0x480
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rsi
leaq -0x414(%rbp), %rdi
callq 0x5f80
movq -0x8(%rbp), %rdi
leaq 0x8099(%rip), %rsi      # 0xbc7a
callq 0x30e0
movq %rax, -0x420(%rbp)
movl $0x420, %edi            # imm = 0x420
callq 0x2260
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x420(%rbp), %rcx
movl $0x420, %esi            # imm = 0x420
movl $0x1, %edx
callq 0x22d0
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl (%rax), %esi
movq -0x20(%rbp), %rax
movl 0x418(%rax), %edx
leaq -0x414(%rbp), %rdi
callq 0x27e0
movl %eax, -0x24(%rbp)
cmpl $0x2, -0x24(%rbp)
jne 0x3c68
movq 0xc38c(%rip), %rdi      # 0xffd0
leaq 0x76e2(%rip), %rsi      # 0xb32d
callq 0x2190
movq 0xc379(%rip), %rdi      # 0xffd0
leaq 0x757c(%rip), %rsi      # 0xb1da
callq 0x2190
jmp 0x3e22
movq -0x8(%rbp), %rdi
leaq 0x76ce(%rip), %rsi      # 0xb341
callq 0x30e0
movq %rax, -0x420(%rbp)
movq -0x20(%rbp), %rax
movl 0x418(%rax), %eax
movl %eax, -0x424(%rbp)
movq -0x20(%rbp), %rax
movl 0x418(%rax), %ecx
addl $0x1, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x418(%rax)
movl -0x14(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x3fc(%rax)
movq -0x20(%rbp), %rdi
movq -0x420(%rbp), %rcx
movl $0x420, %esi            # imm = 0x420
movl $0x1, %edx
callq 0x2390
movq -0x420(%rbp), %rdi
callq 0x21a0
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x2260
movq %rax, -0x430(%rbp)
movq -0x430(%rbp), %rdi
movq -0x8(%rbp), %rdx
movl -0x424(%rbp), %ecx
leaq 0x7642(%rip), %rsi      # 0xb344
movb $0x0, %al
callq 0x2040
movq -0x430(%rbp), %rdi
leaq 0x762a(%rip), %rsi      # 0xb341
callq 0x2210
movq %rax, -0x438(%rbp)
movl $0x0, -0x474(%rbp)
movl -0x474(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x3e0(%rcx), %eax
jge 0x3e01
movq -0x20(%rbp), %rax
addq $0x7c, %rax
movslq -0x474(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
cmpl $0x1, 0x14(%rax)
jne 0x3d95
movq -0x10(%rbp), %rax
movslq -0x474(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl (%rax), %eax
movl %eax, -0x43c(%rbp)
movq -0x438(%rbp), %rcx
leaq -0x43c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2390
jmp 0x3deb
movq -0x20(%rbp), %rax
addq $0x7c, %rax
movslq -0x474(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
cmpl $0x2, 0x14(%rax)
jne 0x3de9
leaq -0x470(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq -0x474(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x2180
leaq -0x470(%rbp), %rdi
movq -0x438(%rbp), %rcx
movl $0x32, %esi
movl $0x1, %edx
callq 0x2390
jmp 0x3deb
jmp 0x3ded
movl -0x474(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x474(%rbp)
jmp 0x3d2d
movq -0x438(%rbp), %rdi
callq 0x21a0
movq -0x430(%rbp), %rdi
callq 0x2220
movq -0x20(%rbp), %rdi
callq 0x2220
addq $0x480, %rsp            # imm = 0x480
popq %rbp
retq
nopl (%rax,%rax)