// File: /workspace/github_cpp_makefile_star2_O2/lioncedric[P]cimpressGridCoverage/src/main.cc
// Function: main
int main(int argc, char ** argv) {
    //init parameters
    MPI::Init(argc, argv);
    string puzzle_path = "puzzle.txt";
    int duration = 8;
    int nbNeigbours = 200;
    if (argc > 3) {
        puzzle_path = argv[1];
        duration = atoi(argv[2]);
        nbNeigbours = atoi(argv[3]);
    } else {
        if (MPI::COMM_WORLD.Get_rank() == 0)
            cout << "Usage : " << argv[0] << " " << "/puzzle/path.txt int_duration int_nbNeigbours" << endl;
        MPI::Finalize();
        return -1;
    }

    //read the puzzle from file and ad a border full of 0
    std::ifstream file(puzzle_path.c_str());
    int width, height;
    file >> width >> height;
    width+=2;
    height+=2;
    int puzzle[width * height];
    for (int y = 0; y <height ; ++y) {
        for (int x = 0; x <width ; ++x) {
	
    		if(y==0 || y==height-1){
			puzzle[y*width+x]=0;
		}else{
			if(x==0 || x==width-1){
				puzzle[y*width+x]=0;
			}else{
        			file >> puzzle[y*width+x];
			}
   		 }
	}
    }
    //process
    algoTabuMPI(puzzle, height, width, nbNeigbours, duration);
    if (MPI::COMM_WORLD.Get_rank() == 0){
	    //retreive the squares result by clearing the puzzle
	    int area[4] = {1, 1, width - 2, height - 2};
	    int squares_removed[height * width * 3];
	    clear_squares(puzzle, width, area, squares_removed);

	    //print json result
	    cout << "Result:"<<endl;
	    cout << result(squares_removed)<<endl;

    }
    MPI::Finalize();
    return 0;


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x288, %rsp            # imm = 0x288
leaq -0x4c(%rbp), %rbx
movl %edi, (%rbx)
leaq -0x38(%rbp), %rax
movq %rsi, (%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2dc5
leaq 0x15c8(%rip), %rsi      # 0x4028
leaq -0x70(%rbp), %rdi
leaq -0x2a8(%rbp), %rdx
callq 0x2dd2
cmpl $0x4, (%rbx)
jl 0x2b74
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x70(%rbp), %rdi
callq 0x22e0
movq -0x38(%rbp), %rbx
movq 0x10(%rbx), %rdi
callq 0x2240
movl %eax, %r14d
movq 0x18(%rbx), %rdi
callq 0x2240
movl %eax, %ebx
movq -0x70(%rbp), %rsi
leaq -0x2a8(%rbp), %rdi
pushq $0x8
popq %rdx
callq 0x22c0
movl %ebx, -0x3c(%rbp)
leaq -0x2a8(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x20f0
leaq -0x30(%rbp), %rsi
movq %rax, %rdi
callq 0x20f0
movq %rsp, -0x48(%rbp)
movl -0x2c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x2c(%rbp)
movl -0x30(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, -0x30(%rbp)
imull %eax, %ecx
leaq 0xf(,%rcx,4), %rax
andq $-0x10, %rax
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
xorl %r13d, %r13d
leaq -0x2a8(%rbp), %r12
movl -0x30(%rbp), %esi
cmpl %esi, %r13d
jge 0x2bd9
xorl %ebx, %ebx
movl -0x2c(%rbp), %eax
cmpl %eax, %ebx
jge 0x2b6f
testl %r13d, %r13d
je 0x2b33
movl -0x30(%rbp), %ecx
decl %ecx
cmpl %ecx, %r13d
jne 0x2b46
imull %r13d, %eax
addl %ebx, %eax
cltq
leaq (%r15,%rax,4), %rsi
andl $0x0, (%rsi)
incl %ebx
jmp 0x2b1d
testl %ebx, %ebx
sete %cl
leal -0x1(%rax), %edx
cmpl %edx, %ebx
sete %dl
orb %cl, %dl
imull %r13d, %eax
addl %ebx, %eax
cltq
leaq (%r15,%rax,4), %rsi
testb %dl, %dl
jne 0x2b3f
movq %r12, %rdi
callq 0x20f0
jmp 0x2b42
incl %r13d
jmp 0x2b0f
movq 0x3445(%rip), %rdi      # 0x5fc0
callq 0x2d32
testl %eax, %eax
jne 0x2bcc
movq 0x3445(%rip), %rdi      # 0x5fd0
leaq 0x14a1(%rip), %rsi      # 0x4033
callq 0x2190
movq -0x38(%rbp), %rcx
movq (%rcx), %rsi
movq %rax, %rdi
callq 0x2190
leaq 0x14fa(%rip), %rsi      # 0x40a7
movq %rax, %rdi
callq 0x2190
leaq 0x1480(%rip), %rsi      # 0x403c
movq %rax, %rdi
callq 0x2190
movq %rax, %rdi
callq 0x20e0
callq 0x2110
pushq $-0x1
popq %rbx
jmp 0x2cbb
movl -0x2c(%rbp), %edx
movq %r15, %rdi
movl -0x3c(%rbp), %ecx
movl %r14d, %r8d
callq 0x244c
movq 0x33cf(%rip), %rdi      # 0x5fc0
callq 0x2d32
testl %eax, %eax
jne 0x2ca4
movq %rsp, %r14
movabsq $0x100000001, %rax      # imm = 0x100000001
leaq -0xa0(%rbp), %rdx
movq %rax, (%rdx)
movl -0x2c(%rbp), %eax
leal -0x2(%rax), %ecx
movl %ecx, 0x8(%rdx)
movl -0x30(%rbp), %ecx
leal -0x2(%rcx), %esi
movl %esi, 0xc(%rdx)
imull %eax, %ecx
leal (%rcx,%rcx,2), %eax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %rbx
subq %rax, %rbx
movq %rbx, %rsp
movl -0x2c(%rbp), %esi
movq %r15, %rdi
movq %rbx, %rcx
callq 0x3316
movq 0x3379(%rip), %rdi      # 0x5fd0
leaq 0x140c(%rip), %rsi      # 0x406a
callq 0x2190
movq %rax, %rdi
callq 0x20e0
leaq -0x90(%rbp), %rdi
movq %rbx, %rsi
callq 0x301e
movq 0x334f(%rip), %rdi      # 0x5fd0
leaq -0x90(%rbp), %rsi
callq 0x2160
movq %rax, %rdi
callq 0x20e0
leaq -0x90(%rbp), %rdi
callq 0x20d0
movq %r14, %rsp
callq 0x2110
movq -0x48(%rbp), %rsp
leaq -0x2a8(%rbp), %rdi
callq 0x2070
xorl %ebx, %ebx
leaq -0x70(%rbp), %rdi
callq 0x20d0
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2cf3
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x20d0
jmp 0x2cf6
jmp 0x2cec
jmp 0x2cf3
movq %rax, %rbx
jmp 0x2d02
jmp 0x2cf3
movq %rax, %rbx
leaq -0x2a8(%rbp), %rdi
callq 0x2070
leaq -0x70(%rbp), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x22a0
nop