// File: 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
subq $0x200, %rsp            # imm = 0x200
movq %rsi, -0x178(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x188(%rbp)
movq %rdi, %rax
movq %rax, -0x190(%rbp)
callq 0x2280
movq -0x188(%rbp), %rdi
addq $0x20, %rdi
movq %rdi, -0x180(%rbp)
callq 0x3fa0
movq -0x178(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x20a0
jmp 0x4e11
leaq -0x40(%rbp), %rdi
movl $0x2c, %esi
callq 0x4ba0
movl %eax, -0x194(%rbp)
jmp 0x4e27
leaq -0x40(%rbp), %rdi
callq 0x20f0
movl -0x194(%rbp), %eax
movq -0x178(%rbp), %rsi
movl %eax, -0x1c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x20a0
jmp 0x4e4b
leaq -0x70(%rbp), %rdi
movl $0x28, %esi
callq 0x4ba0
movl %eax, -0x198(%rbp)
jmp 0x4e61
leaq -0x70(%rbp), %rdi
callq 0x20f0
movl -0x198(%rbp), %eax
movq -0x178(%rbp), %rsi
movl %eax, -0x50(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x20a0
jmp 0x4e88
leaq -0x98(%rbp), %rdi
movl $0x29, %esi
callq 0x4ba0
movl %eax, -0x19c(%rbp)
jmp 0x4ea1
leaq -0x98(%rbp), %rdi
callq 0x20f0
movl -0x19c(%rbp), %eax
movq -0x178(%rbp), %rsi
movl %eax, -0x74(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x20a0
jmp 0x4ecb
leaq -0xc0(%rbp), %rdi
movl $0x2e, %esi
callq 0x4ba0
movl %eax, -0x1a0(%rbp)
jmp 0x4ee4
leaq -0xc0(%rbp), %rdi
callq 0x20f0
movl -0x1a0(%rbp), %eax
movq -0x178(%rbp), %rsi
movl %eax, -0x9c(%rbp)
leaq -0xe0(%rbp), %rdi
movl $0x28, %edx
callq 0x4a90
jmp 0x4f16
movq -0x190(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
movq %rsi, -0x1a8(%rbp)
callq 0x2230
movq -0x1a8(%rbp), %rdi
callq 0x20f0
movq -0x190(%rbp), %rsi
leaq 0x41ca(%rip), %rdx      # 0x9114
leaq -0x100(%rbp), %rdi
callq 0x6130
jmp 0x4f58
movq -0x190(%rbp), %rdi
leaq -0x100(%rbp), %rsi
movq %rsi, -0x1c0(%rbp)
callq 0x2230
movq -0x1c0(%rbp), %rdi
callq 0x20f0
movq -0x190(%rbp), %rdi
movl -0x1c(%rbp), %eax
movl -0x50(%rbp), %ecx
addl %ecx, %eax
movl %eax, 0x38(%rdi)
movb 0x38(%rdi), %al
addb $0x30, %al
movb %al, -0x1b1(%rbp)
callq 0x2110
movq -0x190(%rbp), %rdi
movq %rax, %rsi
decq %rsi
callq 0x2330
movq %rax, -0x1b0(%rbp)
jmp 0x4fbb
movq -0x1b0(%rbp), %rax
movb -0x1b1(%rbp), %cl
movb %cl, (%rax)
movb $0x0, -0x101(%rbp)
movl $0x0, -0x108(%rbp)
movslq -0x108(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2ce0
movq %rax, %rcx
movq -0x1c8(%rbp), %rax
cmpq %rcx, %rax
jae 0x50f1
movq -0x18(%rbp), %rdi
movslq -0x108(%rbp), %rsi
callq 0x2d00
movq -0x190(%rbp), %rsi
movq (%rax), %rdi
callq 0x3ff0
testb $0x1, %al
jne 0x502d
jmp 0x50db
movq -0x18(%rbp), %rdi
movslq -0x108(%rbp), %rsi
callq 0x2d00
movq -0x190(%rbp), %rcx
movq (%rax), %rax
movl 0x20(%rax), %eax
cmpl 0x38(%rcx), %eax
jne 0x50db
movb $0x1, -0x101(%rbp)
jmp 0x50db
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x53ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x20f0
jmp 0x53ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x20f0
jmp 0x53ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x20f0
jmp 0x53ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x20f0
jmp 0x53ed
jmp 0x50dd
movl -0x108(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x108(%rbp)
jmp 0x4fdb
testb $0x1, -0x101(%rbp)
jne 0x5149
movq 0x8ecf(%rip), %rdi      # 0xdfd0
leaq 0x400f(%rip), %rsi      # 0x9117
callq 0x2190
movq %rax, -0x1d0(%rbp)
jmp 0x5116
movq -0x190(%rbp), %rsi
movq -0x1d0(%rbp), %rdi
callq 0x2180
movq %rax, -0x1d8(%rbp)
jmp 0x5132
movq -0x1d8(%rbp), %rdi
movq 0x8e80(%rip), %rsi      # 0xdfc0
callq 0x21c0
jmp 0x5147
jmp 0x5149
movl $0x0, -0x10c(%rbp)
movq -0x190(%rbp), %rcx
movl -0x10c(%rbp), %eax
movl 0x38(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x530f
movl $0x40, %edi
callq 0x21a0
movq %rax, -0x1e0(%rbp)
jmp 0x5181
movq -0x178(%rbp), %rsi
movb $0x1, -0x139(%rbp)
leaq -0x138(%rbp), %rdi
movl $0x2c, %edx
callq 0x4a90
jmp 0x51a2
movq -0x1e0(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x3d10
jmp 0x51b7
movb $0x0, -0x139(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x20f0
movq -0x1e0(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x118(%rbp), %rax
testb $0x1, 0x20(%rax)
je 0x52e0
movl $0x0, -0x140(%rbp)
movq -0x190(%rbp), %rdi
movslq -0x140(%rbp), %rax
movq %rax, -0x1e8(%rbp)
addq $0x20, %rdi
callq 0x2d20
movq %rax, %rcx
movq -0x1e8(%rbp), %rax
cmpq %rcx, %rax
jae 0x52de
movq -0x190(%rbp), %rdi
movq -0x118(%rbp), %rax
movq %rax, -0x1f0(%rbp)
addq $0x20, %rdi
movslq -0x140(%rbp), %rsi
callq 0x2d40
movq -0x1f0(%rbp), %rdi
movq (%rax), %rsi
callq 0x3ff0
testb $0x1, %al
jne 0x525e
jmp 0x52c8
movq -0x190(%rbp), %rdi
addq $0x20, %rdi
movslq -0x140(%rbp), %rsi
callq 0x2d40
movq (%rax), %rax
movq %rax, -0x118(%rbp)
jmp 0x52de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x52a7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x20f0
testb $0x1, -0x139(%rbp)
jne 0x52b2
jmp 0x52c3
movq -0x1e0(%rbp), %rdi
movl $0x40, %esi
callq 0x21b0
jmp 0x53ed
jmp 0x52ca
movl -0x140(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x140(%rbp)
jmp 0x51f3
jmp 0x52e0
movq -0x190(%rbp), %rdi
addq $0x20, %rdi
leaq -0x118(%rbp), %rsi
callq 0x61b0
jmp 0x52f9
jmp 0x52fb
movl -0x10c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10c(%rbp)
jmp 0x5153
movq -0x190(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x200(%rbp)
movl $0x40, %edi
callq 0x21a0
movq %rax, -0x1f8(%rbp)
jmp 0x5334
movq -0x178(%rbp), %rsi
movb $0x1, -0x169(%rbp)
leaq -0x168(%rbp), %rdi
movl $0x29, %edx
callq 0x4a90
jmp 0x5355
movq -0x1f8(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x3d10
jmp 0x536a
movq -0x200(%rbp), %rdi
movq -0x1f8(%rbp), %rax
movb $0x0, -0x169(%rbp)
movq %rax, -0x148(%rbp)
leaq -0x148(%rbp), %rsi
callq 0x3fc0
jmp 0x5394
leaq -0x168(%rbp), %rdi
callq 0x20f0
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x53cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x20f0
testb $0x1, -0x169(%rbp)
jne 0x53da
jmp 0x53eb
movq -0x1f8(%rbp), %rdi
movl $0x40, %esi
callq 0x21b0
jmp 0x53ed
movq -0x180(%rbp), %rdi
callq 0x2e70
movq -0x188(%rbp), %rdi
callq 0x20f0
movq -0x48(%rbp), %rdi
callq 0x22d0
nop