// File: /workspace/github_cpp_makefile_star2_O3/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, %r14d
movq %rsi, %rbx
movq %rdi, %r12
leaq 0x58(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x5100
leaq 0x27d9(%rip), %rsi      # 0x6881
movq %r12, %rdi
callq 0x3bd3
movq %rax, %r13
movl $0x420, %edi            # imm = 0x420
callq 0x2250
movq %rax, %rbp
movl $0x420, %esi            # imm = 0x420
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rcx
callq 0x22b0
movq (%rbx), %rax
movl (%rax), %esi
movl 0x418(%rbp), %r13d
movq %r15, %rdi
movl %r13d, %edx
callq 0x3246
cmpl $0x2, %eax
jne 0x4125
movq 0x4ed8(%rip), %rbx      # 0x8fd0
leaq 0x2038(%rip), %rsi      # 0x6137
movl $0x13, %edx
movq %rbx, %rdi
callq 0x21e0
leaq 0x29b3(%rip), %rsi      # 0x6ac6
movl $0xb, %edx
movq %rbx, %rdi
callq 0x21e0
jmp 0x4235
leaq 0x201f(%rip), %rsi      # 0x614b
movq %r12, %rdi
callq 0x3bd3
movq %rax, %r15
leal 0x1(%r13), %eax
movl %eax, 0x418(%rbp)
movl %r14d, 0x3fc(%rbp)
movl $0x420, %esi            # imm = 0x420
movl $0x1, %edx
movq %rbp, %rdi
movq %r15, %rcx
callq 0x2380
movq %r15, %rdi
callq 0x2190
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x2250
movq %rax, %r14
leaq 0x1fd5(%rip), %rsi      # 0x614e
movq %rax, %rdi
movq %r12, %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x2040
movq %r14, 0x18(%rsp)
movq %r14, %rdi
leaq 0x1fb3(%rip), %rsi      # 0x614b
callq 0x2200
movq %rax, %r12
movq %rbp, 0x10(%rsp)
movslq 0x3e0(%rbp), %r15
testq %r15, %r15
jle 0x4219
movq 0x10(%rsp), %r14
addq $0x90, %r14
xorl %ebp, %ebp
leaq 0x20(%rsp), %r13
movl (%r14), %eax
cmpl $0x2, %eax
je 0x41ec
cmpl $0x1, %eax
jne 0x420d
movq (%rbx,%rbp,8), %rax
movl (%rax), %eax
movl %eax, 0xc(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0xc(%rsp), %rdi
jmp 0x4205
movq (%rbx,%rbp,8), %rsi
movq %r13, %rdi
callq 0x2180
movl $0x32, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rcx
callq 0x2380
incq %rbp
addq $0x1c, %r14
cmpq %rbp, %r15
jne 0x41c4
movq %r12, %rdi
callq 0x2190
movq 0x18(%rsp), %rdi
callq 0x2210
movq 0x10(%rsp), %rdi
callq 0x2210
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq