// File: /workspace/github_cpp_makefile_star2_O2/lotka[P]prologplusplus/predicate.cpp
// Function: PredicateRef::PredicateRef(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::vector<Predicate*, std::allocator<Predicate*>>&)
PredicateRef::PredicateRef(string input, vector<Predicate *> &predicates)
{
	int commas 	= countChar(input,',');
	// left brackets
	int lb 			= countChar(input,'(');
	// right brackets
	int rb	    = countChar(input,')');
	// dots
	int dot 	= countChar(input,'.');
	name = cropString(input,'(');
	// Add the airty to the name
	name = name + "\\0";
	airty = commas+lb;
	name[name.size()-1] = airty+'0';
	//cout << "debug_info : " << name << endl;

	bool exists = false;
	for(int i = 0; i < predicates.size(); ++i)
	{
		if(predicates[i]->name == name and predicates[i]->airty == airty)
		{
			exists = true;
		}
	}
	if(not exists)
	{
		cout << "ERROR in locating " << name << endl;
	}

	for(int i = 0; i < airty-1; ++i)
	{
		Atom * atom_ptr = new Atom(cropString(input,','));
		// for free variables, check for existing instance and point to that
		if(atom_ptr->free_variable)
		{
			for(int i = 0; i < args.size(); ++i)
			{
				if((*atom_ptr).name==(*args[i]).name)
				{
					atom_ptr=args[i];
					break;
				}
			}
		}
		args.push_back(atom_ptr);
	}

	args.push_back(new Atom(cropString(input,')')));

	//cout << "debug_info : ";
	// for(int i = 0; i < airty; ++i)
	// {
	// 	cout << *args[i] << " ";
	// } cout << endl;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdx, %r12
movq %rdi, %rbx
leaq 0x10(%rdi), %rax
movq %rax, (%rdi)
andq $0x0, 0x8(%rdi)
movb $0x0, 0x10(%rdi)
leaq 0x20(%rdi), %rax
movq %rax, -0x38(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rdi)
andq $0x0, 0x30(%rdi)
leaq -0xf8(%rbp), %rdi
movq %rsi, -0x30(%rbp)
callq 0x21d0
leaq -0xf8(%rbp), %rdi
pushq $0x2c
popq %rsi
callq 0x2ebe
movl %eax, %r15d
leaq -0xf8(%rbp), %rdi
callq 0x20a0
leaq -0xd8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x21d0
leaq -0xd8(%rbp), %rdi
pushq $0x28
popq %rsi
callq 0x2ebe
movl %eax, %r13d
leaq -0xd8(%rbp), %rdi
callq 0x20a0
leaq -0xb8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x21d0
leaq -0xb8(%rbp), %rdi
pushq $0x29
popq %rsi
callq 0x2ebe
leaq -0xb8(%rbp), %rdi
callq 0x20a0
leaq -0x98(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x21d0
leaq -0x98(%rbp), %rdi
pushq $0x2e
popq %rsi
callq 0x2ebe
leaq -0x98(%rbp), %rdi
callq 0x20a0
leaq -0x58(%rbp), %rdi
pushq $0x28
popq %rdx
movq -0x30(%rbp), %rsi
callq 0x2e44
leaq -0x58(%rbp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2190
movq %r14, %rdi
callq 0x20a0
leaq 0x207e(%rip), %rdx      # 0x511c
leaq -0x58(%rbp), %rdi
movq %rbx, %rsi
callq 0x390a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2190
movq %r14, %rdi
callq 0x20a0
addl %r15d, %r13d
movl %r13d, 0x38(%rbx)
addb $0x30, %r13b
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movb %r13b, -0x1(%rax,%rcx)
xorl %r14d, %r14d
xorl %r15d, %r15d
movq (%r12), %rax
movq 0x8(%r12), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
jbe 0x3118
movq (%rax,%r14,8), %rdi
movq %rbx, %rsi
callq 0x2be0
testb %al, %al
je 0x3113
movq (%r12), %rax
movq (%rax,%r14,8), %rax
movl 0x20(%rax), %eax
cmpl 0x38(%rbx), %eax
sete %al
orb %al, %r15b
incq %r14
jmp 0x30da
testb $0x1, %r15b
jne 0x3144
movq 0x4eab(%rip), %rdi      # 0x7fd0
leaq 0x1ff3(%rip), %rsi      # 0x511f
callq 0x2140
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2130
movq %rax, %rdi
callq 0x20b0
xorl %r14d, %r14d
leaq -0x118(%rbp), %r13
leaq -0x58(%rbp), %r15
movl 0x38(%rbx), %eax
decl %eax
cmpl %eax, %r14d
jge 0x31e8
pushq $0x40
popq %rdi
callq 0x2150
movq %rax, %r12
movq %r13, %rdi
movq -0x30(%rbp), %rsi
pushq $0x2c
popq %rdx
callq 0x2e44
movq %r12, %rdi
movq %r13, %rsi
callq 0x2ab8
movq %r13, %rdi
callq 0x20a0
movq %r12, -0x58(%rbp)
cmpb $0x1, 0x20(%r12)
jne 0x31d4
xorl %r12d, %r12d
movq 0x20(%rbx), %rax
movq 0x28(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r12, %rcx
jbe 0x31d4
movq -0x58(%rbp), %rdi
movq (%rax,%r12,8), %rsi
callq 0x2be0
incq %r12
testb %al, %al
je 0x319c
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax,%r12,8), %rax
movq %rax, -0x58(%rbp)
movq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0x3942
incl %r14d
jmp 0x3152
pushq $0x40
popq %rdi
callq 0x2150
movq %rax, %r14
leaq -0x78(%rbp), %rdi
pushq $0x29
popq %rdx
movq -0x30(%rbp), %rsi
callq 0x2e44
movb $0x1, %r12b
leaq -0x78(%rbp), %rsi
movq %r14, %rdi
callq 0x2ab8
leaq -0x58(%rbp), %rsi
movq %r14, (%rsi)
xorl %r12d, %r12d
movq -0x38(%rbp), %rdi
callq 0x2c1a
leaq -0x78(%rbp), %rdi
callq 0x20a0
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
jmp 0x329b
jmp 0x32c4
jmp 0x32c4
jmp 0x32c4
movq %rax, %r15
leaq -0x98(%rbp), %rdi
jmp 0x327f
jmp 0x32c4
movq %rax, %r15
leaq -0xb8(%rbp), %rdi
jmp 0x327f
jmp 0x32c4
movq %rax, %r15
leaq -0xd8(%rbp), %rdi
jmp 0x327f
jmp 0x32c4
movq %rax, %r15
leaq -0xf8(%rbp), %rdi
callq 0x20a0
jmp 0x32c7
jmp 0x32c4
jmp 0x32c4
movq %rax, %r15
leaq -0x78(%rbp), %rdi
callq 0x20a0
testb %r12b, %r12b
je 0x32c7
pushq $0x40
popq %rsi
movq %r14, %rdi
jmp 0x32bd
movq %rax, %r15
jmp 0x32b7
movq %rax, %r15
leaq -0x118(%rbp), %rdi
callq 0x20a0
pushq $0x40
popq %rsi
movq %r12, %rdi
callq 0x2160
jmp 0x32c7
movq %rax, %r15
movq -0x38(%rbp), %rdi
callq 0x2800
movq %rbx, %rdi
callq 0x20a0
movq %r15, %rdi
callq 0x2200