// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r13
leaq 0x58(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x45b2
leaq 0x369c(%rip), %rsi      # 0x6881
movq %r13, %rdi
callq 0x2c0a
movq %rax, %r14
movl $0x420, %edi            # imm = 0x420
callq 0x2240
movq %rax, %r12
pushq $0x1
popq %rdx
movl $0x420, %esi            # imm = 0x420
movq %rax, %rdi
movq %r14, %rcx
callq 0x22a0
movq (%rbx), %rax
movl (%rax), %esi
movl 0x418(%r12), %r14d
movq %r15, %rdi
movl %r14d, %edx
callq 0x2614
cmpl $0x2, %eax
jne 0x3257
movq 0x6d9c(%rip), %rbx      # 0x9fd0
leaq 0x2efc(%rip), %rsi      # 0x6137
movq %rbx, %rdi
callq 0x2180
leaq 0x387c(%rip), %rsi      # 0x6ac6
movq %rbx, %rdi
callq 0x2180
jmp 0x3356
leaq 0x2eed(%rip), %rsi      # 0x614b
movq %r13, %rdi
callq 0x2c0a
movq %r12, %r15
movq %rax, %r12
leal 0x1(%r14), %eax
movl %eax, 0x418(%r15)
movl %ebp, 0x3fc(%r15)
pushq $0x1
popq %rdx
movl $0x420, %esi            # imm = 0x420
movq %r15, %rdi
movq %r12, %rcx
callq 0x2360
movq %r12, %rdi
callq 0x2190
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x2240
movq %rax, %rbp
leaq 0x2ea1(%rip), %rsi      # 0x614e
xorl %r12d, %r12d
movq %rax, %rdi
movq %r13, %rdx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2040
movq %rbp, 0x10(%rsp)
movq %rbp, %rdi
leaq 0x2e7c(%rip), %rsi      # 0x614b
callq 0x21f0
movq %rax, %r13
movslq 0x3e0(%r15), %r14
movq %r15, 0x18(%rsp)
addq $0x90, %r15
leaq 0x20(%rsp), %rbp
cmpq %r14, %r12
jge 0x333a
movl (%r15), %eax
cmpl $0x2, %eax
je 0x3314
cmpl $0x1, %eax
jne 0x3331
movq (%rbx,%r12,8), %rax
movl (%rax), %eax
movl %eax, 0xc(%rsp)
leaq 0xc(%rsp), %rdi
pushq $0x4
jmp 0x3325
movq (%rbx,%r12,8), %rsi
movq %rbp, %rdi
callq 0x2170
movq %rbp, %rdi
pushq $0x32
popq %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
callq 0x2360
incq %r12
addq $0x1c, %r15
jmp 0x32ef
movq %r13, %rdi
callq 0x2190
movq 0x10(%rsp), %rdi
callq 0x2200
movq 0x18(%rsp), %rdi
callq 0x2200
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq