// File: 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
subq $0x320, %rsp            # imm = 0x320
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x8(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0xb730
leaq -0x31(%rbp), %rdi
movq %rdi, -0x2c8(%rbp)
callq 0x9c50
movq -0x2c8(%rbp), %rdx
leaq 0x8844(%rip), %rsi      # 0x13028
leaq -0x30(%rbp), %rdi
callq 0xb760
jmp 0xa7ef
leaq -0x31(%rbp), %rdi
callq 0x9770
movl $0x8, -0x48(%rbp)
movl $0xc8, -0x4c(%rbp)
cmpl $0x3, -0x8(%rbp)
jle 0xa86f
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x30(%rbp), %rdi
callq 0x9950
jmp 0xa81f
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x9ab0
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x9ab0
movl %eax, -0x4c(%rbp)
jmp 0xa933
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x31(%rbp), %rdi
callq 0x9770
jmp 0xace0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xacd7
movq 0xd742(%rip), %rdi      # 0x17fb8
callq 0xb3e0
movl %eax, -0x2cc(%rbp)
jmp 0xa883
movl -0x2cc(%rbp), %eax
cmpl $0x0, %eax
jne 0xa919
movq 0xd737(%rip), %rdi      # 0x17fd0
leaq 0x8793(%rip), %rsi      # 0x13033
callq 0x96b0
movq %rax, -0x2d8(%rbp)
jmp 0xa8ae
movq -0x2d8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
callq 0x96b0
movq %rax, -0x2e0(%rbp)
jmp 0xa8ca
movq -0x2e0(%rbp), %rdi
leaq 0x88bd(%rip), %rsi      # 0x13195
callq 0x96b0
movq %rax, -0x2e8(%rbp)
jmp 0xa8e6
movq -0x2e8(%rbp), %rdi
leaq 0x8748(%rip), %rsi      # 0x1303c
callq 0x96b0
movq %rax, -0x2f0(%rbp)
jmp 0xa902
movq -0x2f0(%rbp), %rdi
movq 0xd698(%rip), %rsi      # 0x17fa8
callq 0x9700
jmp 0xa917
jmp 0xa919
callq 0xb820
jmp 0xa920
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x50(%rbp)
jmp 0xacc6
leaq -0x30(%rbp), %rdi
callq 0x9250
movq %rax, %rsi
leaq -0x258(%rbp), %rdi
movl $0x8, %edx
callq 0x9cd0
jmp 0xa952
leaq -0x258(%rbp), %rdi
leaq -0x25c(%rbp), %rsi
callq 0x93c0
movq %rax, -0x2f8(%rbp)
jmp 0xa96e
movq -0x2f8(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x93c0
jmp 0xa983
movl -0x25c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x25c(%rbp)
movl -0x260(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x260(%rbp)
movl -0x25c(%rbp), %eax
movl -0x260(%rbp), %ecx
imull %ecx, %eax
movq %rsp, %rcx
movq %rcx, -0x268(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x300(%rbp)
movq %rcx, %rsp
movq %rax, -0x270(%rbp)
movl $0x0, -0x274(%rbp)
movl -0x274(%rbp), %eax
cmpl -0x260(%rbp), %eax
jge 0xab10
movl $0x0, -0x278(%rbp)
movl -0x278(%rbp), %eax
cmpl -0x25c(%rbp), %eax
jge 0xaafa
cmpl $0x0, -0x274(%rbp)
je 0xaa31
movl -0x274(%rbp), %eax
movl -0x260(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xaa6b
movq -0x300(%rbp), %rax
movl -0x274(%rbp), %ecx
imull -0x25c(%rbp), %ecx
addl -0x278(%rbp), %ecx
movslq %ecx, %rcx
movl $0x0, (%rax,%rcx,4)
jmp 0xaae4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xacb8
cmpl $0x0, -0x278(%rbp)
je 0xaa87
movl -0x278(%rbp), %eax
movl -0x25c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xaaad
movq -0x300(%rbp), %rax
movl -0x274(%rbp), %ecx
imull -0x25c(%rbp), %ecx
addl -0x278(%rbp), %ecx
movslq %ecx, %rcx
movl $0x0, (%rax,%rcx,4)
jmp 0xaae2
movq -0x300(%rbp), %rax
movl -0x274(%rbp), %ecx
movl -0x25c(%rbp), %edx
imull %edx, %ecx
movl -0x278(%rbp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
leaq (%rax,%rcx,4), %rsi
leaq -0x258(%rbp), %rdi
callq 0x93c0
jmp 0xaae0
jmp 0xaae2
jmp 0xaae4
jmp 0xaae6
movl -0x278(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x278(%rbp)
jmp 0xaa03
jmp 0xaafc
movl -0x274(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x274(%rbp)
jmp 0xa9e7
movq -0x300(%rbp), %rdi
movl -0x260(%rbp), %esi
movl -0x25c(%rbp), %edx
movl -0x4c(%rbp), %ecx
movl -0x48(%rbp), %r8d
callq 0x9f50
jmp 0xab31
movq 0xd480(%rip), %rdi      # 0x17fb8
callq 0xb3e0
movl %eax, -0x304(%rbp)
jmp 0xab45
movl -0x304(%rbp), %eax
cmpl $0x0, %eax
jne 0xac8b
movq -0x300(%rbp), %rdi
movl $0x1, -0x290(%rbp)
movl $0x1, -0x28c(%rbp)
movl -0x25c(%rbp), %eax
addl $-0x2, %eax
movl %eax, -0x288(%rbp)
movl -0x260(%rbp), %eax
addl $-0x2, %eax
movl %eax, -0x284(%rbp)
movl -0x260(%rbp), %ecx
movl -0x25c(%rbp), %eax
imull %eax, %ecx
movl %ecx, %eax
leal (%rax,%rax,2), %eax
movq %rsp, %rcx
movq %rcx, -0x298(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x310(%rbp)
movq %rcx, %rsp
movq %rax, -0x2a0(%rbp)
movl -0x25c(%rbp), %esi
leaq -0x290(%rbp), %rdx
callq 0x10f00
jmp 0xabe2
movq 0xd3e7(%rip), %rdi      # 0x17fd0
leaq 0x847a(%rip), %rsi      # 0x1306a
callq 0x96b0
movq %rax, -0x318(%rbp)
jmp 0xabfe
movq -0x318(%rbp), %rdi
movq 0xd39c(%rip), %rsi      # 0x17fa8
callq 0x9700
jmp 0xac13
movq -0x310(%rbp), %rsi
leaq -0x2c0(%rbp), %rdi
callq 0x109b0
jmp 0xac28
movq 0xd3a1(%rip), %rdi      # 0x17fd0
leaq -0x2c0(%rbp), %rsi
callq 0x9600
movq %rax, -0x320(%rbp)
jmp 0xac44
movq -0x320(%rbp), %rdi
movq 0xd356(%rip), %rsi      # 0x17fa8
callq 0x9700
jmp 0xac59
leaq -0x2c0(%rbp), %rdi
callq 0x93d0
movq -0x298(%rbp), %rax
movq %rax, %rsp
jmp 0xac8b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x2c0(%rbp), %rdi
callq 0x93d0
jmp 0xacb8
callq 0xb820
jmp 0xac92
movl $0x0, -0x4(%rbp)
movl $0x1, -0x50(%rbp)
movq -0x268(%rbp), %rax
movq %rax, %rsp
leaq -0x258(%rbp), %rdi
callq 0x9180
jmp 0xacc6
leaq -0x258(%rbp), %rdi
callq 0x9180
jmp 0xacd7
leaq -0x30(%rbp), %rdi
callq 0x93d0
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
leaq -0x30(%rbp), %rdi
callq 0x93d0
movq -0x40(%rbp), %rdi
callq 0x9c40
nopl (%rax)