// File: /workspace/github_cpp_makefile_star2_O2/CMU-SAFARI[P]Register-Interval/./src/implementation/RegisterIntervalCreation.cpp
// Function: RegisterIntervalCreationPass::registerIntervalCreationPassOne(std::vector<basicBlock*, std::allocator<basicBlock*>>, int)
vector<registerInterval*> RegisterIntervalCreationPass::registerIntervalCreationPassOne(vector<basicBlock*> BB_vector, int N){ //N is the maximum number of registers allowed in a register-interval 
        basic_blocks = BB_vector;
    
        /* Initialization */
	for (auto b = basic_blocks.begin(); b != basic_blocks.end(); b++){
		(*b)->clear_input_list();
		(*b)->set_registerInterval(nullptr);
	}

	RegisterIntervals.clear();
	for(auto b = basic_blocks.begin(); b != basic_blocks.end(); b++){
		auto Predecessors = (*b)->get_predecessors();
		if(Predecessors.size() == 0){ // each basic block with no predecessors is assumed as an entry basicBlock
			auto temp = new registerInterval();
			RegisterIntervals.push_back(temp);
			(*b)->set_registerInterval(temp);
			WS.push(*b);// The entry basic block is added to the working set of registerInterval creation algorithm
		}
	}

	while (!WS.empty()){
		auto BB = WS.front(); //Get one basic blocks from working set
		WS.pop();
		auto i = BB->RegisterInterval(); //Get the register-interval that this basic block belongs to
		traverse(BB, N); //Traversing the basic block and splitting it if it has more than the allowed number of registers
		if (BB->numberOfRegisters() < N){
                        //In this loop we will add all eligible basic blocks to the current registerInterval (i)
			for (auto b = basic_blocks.begin(); b != basic_blocks.end(); b++){
				set<string> Union;//The set that will contain the union of register sets of all basic blocks of i
                                Union.clear();
				bool C = true;//This boolean determines that the basicBlock is reachable only from the current register-interval (in other words, its all predecessors should belong to the current register-interval)

				visit_all_predecessors(*b);//marks all basic blocks that can reach this basic block

				Union = (*b)->get_output_list();
                                //This loop will add all registers of marked basic blocks that belong to the current register-interval (i)
				for(auto bb = basic_blocks.begin(); bb != basic_blocks.end(); bb++){
					if(mark[(*bb)->getID()]){
						if((*bb)->RegisterInterval() == i){
							auto s = (*bb)->get_output_list();
							for (auto it = s.begin(); it != s.end(); it++){
								Union.insert(*it);
							}
						}
					}
				}
		
				//This loop determines the boolean value of C
				auto P = (*b)->get_predecessors();
                                for(auto p = P.begin(); p != P.end(); p++){
                                        if((*p)->RegisterInterval() != i)
                                                C = false;
                                }

				//If this basic block is eligible, it will be added to the current register-interval
				if ((*b)->RegisterInterval() == nullptr //the basic block should not belong to another register-interval
                                        && C  //All predecessors of the basic block should belong to the current register-interval
                                        && Union.size() < N //The union of all registers of the basic block and current register-interval should not be more than the allowed number
                                        )
                                {
					(*b)->set_registerInterval(i);
					(*b)->set_input_list(Union);
					traverse((*b), N);
				}
			}
		}
                //At this point no more basic blocks can be added to the current register-interval

                set<basicBlock*> Successors; //Successors will contain all successors of the current register-interval (i)
		Successors.clear();
                
                //This loop will add all successors of the current registerInterval to Successors set
		for (auto b = basic_blocks.begin(); b != basic_blocks.end(); b++){
			if ((*b)->RegisterInterval() == i){
				auto S = (*b)->get_successors();
				for (auto s = S.begin(); s != S.end(); s++){
					if ((*s)->RegisterInterval() != i)
						Successors.insert(*s);
				}
			}
		}
                
                //This loop will assign new register-intervals to the successors of the current register-interval
		set<basicBlock*>::iterator it2;
		for (auto s = Successors.begin(); s != Successors.end(); s++){
			if ((*s)->RegisterInterval() == nullptr){
				auto temp = new registerInterval();
				RegisterIntervals.push_back(temp);
				(*s)->set_registerInterval(temp);
				(*s)->clear_input_list();
				WS.push(*s);
			}
		}
	}
        
        /*At this point all register-interval are created
         *      and the rest of the function just sets the register list of register-intervals
         *      and forms the new CFG that its nodes are register-intervals*/

        //This loop will set the register list of all register-intervals
	for (auto i = RegisterIntervals.begin(); i != RegisterIntervals.end(); i++){
                set<string> regList;//the register list of register-interval
		regList.clear();
		for (auto b = basic_blocks.begin(); b != basic_blocks.end(); b++){
			if ((*b)->RegisterInterval() == *i){
				auto s = (*b)->get_output_list();
				for (auto it = s.begin(); it != s.end(); it++){
					regList.insert(*it);
				}
			}
		}
		(*i)->set_register_list(regList);
	}

	//This loop sets the series of instructions of all register-intervals
	string code;
	for (auto i = RegisterIntervals.begin(); i != RegisterIntervals.end(); i++){
		code = "";
		for (auto b = basic_blocks.begin(); b != basic_blocks.end(); b++){
			if ((*b)->RegisterInterval() == *i){
				code = code + (*b)->get_code();
			}
		}
		(*i)->set_code(code);
	}

	//This loop sets the predecessors and successors of all register-intervals
	for (auto i = RegisterIntervals.begin(); i != RegisterIntervals.end(); i++){
		for (auto b = basic_blocks.begin(); b != basic_blocks.end(); b++){
			if ((*b)->RegisterInterval() == *i){
				auto P = (*b)->get_predecessors();
				for (auto p = P.begin(); p != P.end(); p++){
					if ((*p)->RegisterInterval() != *i)
						(*i)->add_predecessor((*p)->RegisterInterval());
				}
                                
                                auto S = (*b)->get_successors();
				for (auto s = S.begin(); s != S.end(); s++){
					if ((*s)->RegisterInterval() != *i)
						(*i)->add_successors((*s)->RegisterInterval());
				}
			}
		}
	}
        
        return RegisterIntervals;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movl %edx, -0x2c(%rbp)
movq %rdi, -0xa0(%rbp)
leaq 0xb30f(%rip), %rdi      # 0xe208
callq 0x6ad6
movq 0xb303(%rip), %r14      # 0xe208
movl $0x90, %ebx
cmpq 0xb2ff(%rip), %r14      # 0xe210
je 0x2f2f
movq (%r14), %rdi
addq %rbx, %rdi
callq 0x75c8
movq (%r14), %rax
andq $0x0, 0xf0(%rax)
addq $0x8, %r14
jmp 0x2f0a
movq 0xb2ea(%rip), %rax      # 0xe220
cmpq %rax, 0xb2eb(%rip)      # 0xe228
je 0x2f46
movq %rax, 0xb2e2(%rip)      # 0xe228
movq 0xb2bb(%rip), %rbx      # 0xe208
leaq -0x68(%rbp), %r15
leaq -0x98(%rbp), %r13
leaq 0xb2d9(%rip), %r12      # 0xe238
cmpq %r14, %rbx
je 0x2fd9
movq (%rbx), %rsi
addq $0x60, %rsi
movq %r15, %rdi
callq 0x6bdc
movq -0x60(%rbp), %rax
cmpq -0x68(%rbp), %rax
jne 0x2fc4
movl $0xc0, %edi
callq 0x21c0
movq %rax, %r14
movq %rax, %rdi
callq 0x25cc
movq %r14, -0x98(%rbp)
leaq 0xb280(%rip), %rdi      # 0xe220
movq %r13, %rsi
callq 0x6a2c
movq (%rbx), %rax
movq -0x98(%rbp), %rcx
movq %rcx, 0xf0(%rax)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x83c0
movq %r15, %rdi
callq 0x7ed2
addq $0x8, %rbx
movq 0xb239(%rip), %r14      # 0xe210
jmp 0x2f5f
leaq -0x60(%rbp), %r15
leaq -0x90(%rbp), %r13
movslq -0x2c(%rbp), %rax
movq %rax, -0xa8(%rbp)
leaq -0x68(%rbp), %r12
movq 0xb24e(%rip), %rax      # 0xe248
cmpq %rax, 0xb267(%rip)      # 0xe268
je 0x332a
movq (%rax), %r14
leaq 0xb227(%rip), %rdi      # 0xe238
callq 0x860c
movq 0xf0(%r14), %rbx
movq %r14, %rdi
movl -0x2c(%rbp), %esi
callq 0x3704
movl -0x2c(%rbp), %eax
cmpl %eax, 0xe8(%r14)
leaq -0x98(%rbp), %r14
jge 0x31f9
movq 0xb1c2(%rip), %rax      # 0xe208
cmpq 0xb1c3(%rip), %rax      # 0xe210
je 0x31f9
andl $0x0, -0x60(%rbp)
andq $0x0, -0x58(%rbp)
movq %r15, -0x50(%rbp)
movq %r15, -0x48(%rbp)
andq $0x0, -0x40(%rbp)
movq %r12, %rdi
movq %rax, %r15
callq 0x75c8
movq (%r15), %rdi
callq 0x3f0a
movq %r15, -0x38(%rbp)
movq (%r15), %rsi
movl $0xc0, %eax
addq %rax, %rsi
movq %r14, %rdi
callq 0x6ef4
movq %r12, %rdi
movq %r14, %rsi
callq 0x7572
movq %r14, %rdi
callq 0x6e7c
movq 0xb15b(%rip), %r15      # 0xe208
cmpq 0xb15c(%rip), %r15      # 0xe210
je 0x311f
movq (%r15), %rax
movslq (%rax), %rsi
leaq 0xb1c5(%rip), %rdi      # 0xe288
callq 0x69e8
testq %rdx, (%rax)
je 0x3119
movq (%r15), %rsi
cmpq %rbx, 0xf0(%rsi)
jne 0x3119
addq $0xc0, %rsi
movq %r14, %rdi
callq 0x6ef4
movq -0x80(%rbp), %r14
cmpq %r13, %r14
je 0x310a
leaq 0x20(%r14), %rsi
movq %r12, %rdi
callq 0x7ee6
movq %r14, %rdi
callq 0x2200
movq %rax, %r14
jmp 0x30ec
leaq -0x98(%rbp), %r14
movq %r14, %rdi
callq 0x6e7c
addq $0x8, %r15
jmp 0x30ad
movq -0x38(%rbp), %rax
movq (%rax), %rsi
addq $0x60, %rsi
movq %r14, %rdi
callq 0x6bdc
movq -0x98(%rbp), %rcx
movq -0x90(%rbp), %rdx
movb $0x1, %al
leaq -0x60(%rbp), %r15
cmpq %rdx, %rcx
je 0x3162
movq (%rcx), %rsi
cmpq %rbx, 0xf0(%rsi)
sete %sil
andb %sil, %al
addq $0x8, %rcx
jmp 0x3146
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
cmpq $0x0, 0xf0(%rcx)
sete %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0x31d5
movq -0xa8(%rbp), %rax
cmpq %rax, -0x40(%rbp)
jae 0x31d5
movq %rbx, 0xf0(%rcx)
movq -0x38(%rbp), %rax
movq (%rax), %r14
leaq -0x118(%rbp), %rdi
movq %r12, %rsi
callq 0x6ef4
addq $0x90, %r14
movq %r14, %rdi
leaq -0x118(%rbp), %r14
movq %r14, %rsi
callq 0x7194
movq %r14, %rdi
callq 0x6e7c
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movl -0x2c(%rbp), %esi
callq 0x3704
leaq -0x98(%rbp), %r14
movq %r14, %rdi
callq 0x7ed2
movq %r12, %rdi
callq 0x6e7c
movq -0x38(%rbp), %rax
addq $0x8, %rax
jmp 0x3046
andl $0x0, -0x60(%rbp)
andq $0x0, -0x58(%rbp)
movq %r15, -0x50(%rbp)
movq %r15, -0x48(%rbp)
andq $0x0, -0x40(%rbp)
movq %r12, %rdi
callq 0x866e
movq 0xafea(%rip), %r15      # 0xe208
movq 0xafeb(%rip), %rax      # 0xe210
cmpq %rax, %r15
je 0x328b
movq (%r15), %rsi
cmpq %rbx, 0xf0(%rsi)
jne 0x3285
addq $0x78, %rsi
movq %r14, %rdi
callq 0x6bdc
movq -0x98(%rbp), %r14
cmpq -0x90(%rbp), %r14
je 0x326f
movq (%r14), %rax
cmpq %rbx, 0xf0(%rax)
je 0x3269
movq %r12, %rdi
movq %r14, %rsi
callq 0x86a6
addq $0x8, %r14
jmp 0x3249
leaq -0x98(%rbp), %r14
movq %r14, %rdi
callq 0x7ed2
movq 0xaf8b(%rip), %rax      # 0xe210
addq $0x8, %r15
jmp 0x3225
movq -0x50(%rbp), %r14
leaq -0x60(%rbp), %r15
cmpq %r15, %r14
je 0x331d
movq 0x20(%r14), %rax
cmpq $0x0, 0xf0(%rax)
jne 0x330d
movl $0xc0, %edi
callq 0x21c0
movq %rax, %rbx
movq %rax, %rdi
callq 0x25cc
movq %rbx, -0x98(%rbp)
leaq 0xaf53(%rip), %rdi      # 0xe220
leaq -0x98(%rbp), %rsi
callq 0x6a2c
leaq 0x20(%r14), %rbx
movq (%rbx), %rax
movq -0x98(%rbp), %rcx
movq %rcx, 0xf0(%rax)
movq (%rbx), %rdi
movl $0x90, %eax
addq %rax, %rdi
callq 0x75c8
leaq 0xaf33(%rip), %rdi      # 0xe238
movq %rbx, %rsi
callq 0x83c0
movq %r14, %rdi
callq 0x2200
movq %rax, %r14
jmp 0x3293
movq %r12, %rdi
callq 0x7640
jmp 0x2ff3
movq 0xaeef(%rip), %r15      # 0xe220
leaq -0x68(%rbp), %rbx
cmpq 0xaeec(%rip), %r15      # 0xe228
je 0x340e
andl $0x0, -0x60(%rbp)
andq $0x0, -0x58(%rbp)
leaq -0x60(%rbp), %rax
movq %rax, -0x50(%rbp)
movq %rax, -0x48(%rbp)
andq $0x0, -0x40(%rbp)
movq %rbx, %rdi
callq 0x75c8
movq 0xae9d(%rip), %r14      # 0xe208
movq 0xae9e(%rip), %rax      # 0xe210
cmpq %rax, %r14
je 0x33d5
movq (%r14), %rsi
movq 0xf0(%rsi), %rcx
cmpq (%r15), %rcx
jne 0x33cf
addq $0xc0, %rsi
leaq -0x98(%rbp), %rdi
callq 0x6ef4
movq -0x80(%rbp), %r12
cmpq %r13, %r12
je 0x33bc
leaq 0x20(%r12), %rsi
movq %rbx, %rdi
callq 0x7ee6
movq %r12, %rdi
callq 0x2200
movq %rax, %r12
jmp 0x339d
leaq -0x98(%rbp), %rdi
callq 0x6e7c
movq 0xae41(%rip), %rax      # 0xe210
addq $0x8, %r14
jmp 0x3372
movq (%r15), %r12
leaq -0x148(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6ef4
movq %r12, %rdi
movq %r14, %rsi
callq 0x2746
movq %r14, %rdi
callq 0x6e7c
movq %rbx, %rdi
callq 0x6e7c
addq $0x8, %r15
jmp 0x3335
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0xadfb(%rip), %r14      # 0xe220
leaq -0xc8(%rbp), %r12
leaq -0x98(%rbp), %r13
cmpq %r15, %r14
je 0x34ef
leaq -0x68(%rbp), %r15
movq %r15, %rdi
leaq 0x5c6c(%rip), %rsi      # 0x90b6
callq 0x2360
movq 0xadb2(%rip), %rbx      # 0xe208
movq 0xadb3(%rip), %rax      # 0xe210
cmpq %rax, %rbx
je 0x34b3
movq (%rbx), %rsi
movq 0xf0(%rsi), %rcx
cmpq (%r14), %rcx
jne 0x34ad
addq $0x28, %rsi
movq %r12, %rdi
callq 0x2270
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x6c5e
movq %r15, %rdi
movq %r13, %rsi
callq 0x2220
movq %r13, %rdi
callq 0x2100
movq %r12, %rdi
callq 0x2100
movq 0xad63(%rip), %rax      # 0xe210
addq $0x8, %rbx
jmp 0x345d
movq (%r14), %rbx
leaq -0xe8(%rbp), %rdi
movq %r15, %rsi
callq 0x2270
movq %rbx, %rdi
leaq -0xe8(%rbp), %rbx
movq %rbx, %rsi
callq 0x26a0
movq %rbx, %rdi
callq 0x2100
addq $0x8, %r14
movq 0xad3e(%rip), %r15      # 0xe228
jmp 0x3433
movq 0xad2a(%rip), %r12      # 0xe220
leaq -0x98(%rbp), %rbx
leaq -0xc8(%rbp), %r14
cmpq %r15, %r12
je 0x35e1
movq 0xacf4(%rip), %r15      # 0xe208
movq 0xacf5(%rip), %rax      # 0xe210
cmpq %rax, %r15
je 0x35d1
movq (%r15), %rsi
movq 0xf0(%rsi), %rcx
cmpq (%r12), %rcx
jne 0x35c8
addq $0x60, %rsi
movq %rbx, %rdi
callq 0x6bdc
movq -0x98(%rbp), %r13
cmpq -0x90(%rbp), %r13
je 0x3573
movq (%r13), %rax
movq 0xf0(%rax), %rsi
movq (%r12), %rdi
cmpq %rdi, %rsi
je 0x356d
callq 0x2816
addq $0x8, %r13
jmp 0x354b
movq (%r15), %rsi
addq $0x78, %rsi
movq %r14, %rdi
callq 0x6bdc
movq -0xc8(%rbp), %r13
cmpq -0xc0(%rbp), %r13
je 0x35b1
movq (%r13), %rax
movq 0xf0(%rax), %rsi
movq (%r12), %rdi
cmpq %rdi, %rsi
je 0x35ab
callq 0x2870
addq $0x8, %r13
jmp 0x3589
movq %r14, %rdi
callq 0x7ed2
movq %rbx, %rdi
callq 0x7ed2
movq 0xac48(%rip), %rax      # 0xe210
addq $0x8, %r15
jmp 0x351b
addq $0x8, %r12
movq 0xac4c(%rip), %r15      # 0xe228
jmp 0x3504
leaq 0xac38(%rip), %rsi      # 0xe220
movq -0xa0(%rbp), %rbx
movq %rbx, %rdi
callq 0x6a50
leaq -0x68(%rbp), %rdi
callq 0x2100
movq %rbx, %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x365f
movq %rax, %rbx
leaq -0xe8(%rbp), %rdi
jmp 0x366e
jmp 0x36de
movq %rax, %rbx
leaq -0x148(%rbp), %rdi
jmp 0x36ed
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x7ed2
jmp 0x36fb
jmp 0x365f
movq %rax, %rbx
leaq -0x118(%rbp), %rdi
callq 0x6e7c
jmp 0x367e
jmp 0x365f
jmp 0x36a8
movq %rax, %rbx
jmp 0x36b7
movq %rax, %rbx
leaq -0xc8(%rbp), %rdi
callq 0x2100
jmp 0x36b7
jmp 0x36de
jmp 0x3690
jmp 0x36de
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x7ed2
jmp 0x36f2
jmp 0x36de
jmp 0x36de
movq %rax, %rbx
jmp 0x36d1
jmp 0x36de
movq %rax, %rbx
leaq -0xc8(%rbp), %rdi
callq 0x7ed2
jmp 0x36ab
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x7ed2
leaq -0x68(%rbp), %rdi
callq 0x2100
jmp 0x36fb
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x7ed2
leaq -0x68(%rbp), %rdi
callq 0x7640
jmp 0x36fb
jmp 0x36e3
movq %rax, %rbx
jmp 0x36f2
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x6e7c
leaq -0x68(%rbp), %rdi
callq 0x6e7c
movq %rbx, %rdi
callq 0x2340
nop