// File: /workspace/github_cpp_makefile_star2_O2/Gammelkebab[P]GameOfLife/src/main.cpp
// Function: main
int main(int argc, char **argv)
{
    MPI_Init(&argc, &argv);

    //time measurement
    double elapsed = 0;
    struct timeval begin, end;

    int proc_amt, proc_num;

    MPI_Comm_rank(MPI_COMM_WORLD, &proc_num);
    MPI_Comm_size(MPI_COMM_WORLD, &proc_amt);

    if (proc_num == 0)
    {
        printf("Processors: %d\n", proc_amt);
        printf("World size: %d x %d\n", GRIDSIZE_X, GRIDSIZE_Y);
        printf("Frames: %d\n", FRAMES);
        printf("Worker share: %f\n", WORKER_SHARE);
    }

    char processor_name[100];
    int processor_name_length;
    MPI_Get_processor_name(processor_name, &processor_name_length);

    MPI_Barrier(MPI_COMM_WORLD);

    /* Timer start */
    gettimeofday(&begin, NULL);

    Actor *actor = Actor::create(GRIDSIZE_X, GRIDSIZE_Y, proc_amt, proc_num, FRAMES, WORKER_SHARE);

    // Main execution loop
    for (int i = 0; i < FRAMES; i++)
    {
        /*
        if (proc_num == 0)
        {
            printf("round: %d of %d\n", i, FRAMES);
        }
        */
        actor->tick(i);
    }

    actor->finalize();

    MPI_Barrier(MPI_COMM_WORLD);

    /* Timer end */

    gettimeofday(&end, NULL);
    elapsed += (end.tv_sec - begin.tv_sec) + ((end.tv_usec - begin.tv_usec) / 1000000.0);
    if (proc_num == 0)
    {
        printf("Runtime: %.5fs\n", elapsed);
    }

    MPI_Finalize();
}

// Assembly:
pushq %r14
pushq %rbx
subq $0xa8, %rsp
leaq 0x14(%rsp), %rax
movl %edi, (%rax)
leaq 0x38(%rsp), %rcx
movq %rsi, (%rcx)
movq %rax, %rdi
movq %rcx, %rsi
callq 0x21f0
movq 0x4b69(%rip), %rbx      # 0x6fb0
leaq 0x8(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21e0
leaq 0xc(%rsp), %rsi
movq %rbx, %rdi
callq 0x21b0
cmpl $0x0, (%r14)
jne 0x24bd
movl 0xc(%rsp), %esi
leaq 0x1ba3(%rip), %rdi      # 0x4018
xorl %eax, %eax
callq 0x2040
leaq 0x1ba5(%rip), %rdi      # 0x4028
movl $0x780, %esi            # imm = 0x780
movl $0x438, %edx            # imm = 0x438
xorl %eax, %eax
callq 0x2040
leaq 0x1ba2(%rip), %rdi      # 0x403d
movl $0x384, %esi            # imm = 0x384
xorl %eax, %eax
callq 0x2040
leaq 0x1b9b(%rip), %rdi      # 0x4049
movsd 0x1b52(%rip), %xmm0     # 0x4008
movb $0x1, %al
callq 0x2040
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x2190
movq 0x4add(%rip), %rdi      # 0x6fb0
callq 0x20d0
xorl %ebx, %ebx
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
callq 0x2220
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
movsd 0x1b12(%rip), %xmm0     # 0x4008
movl $0x780, %edi            # imm = 0x780
movl $0x438, %esi            # imm = 0x438
movl $0x384, %r8d            # imm = 0x384
callq 0x2602
movq %rax, %r14
movq (%r14), %rax
cmpl $0x384, %ebx            # imm = 0x384
je 0x2524
movq %r14, %rdi
movl %ebx, %esi
callq *(%rax)
incl %ebx
jmp 0x250e
movq %r14, %rdi
callq *0x8(%rax)
movq 0x4a7f(%rip), %rdi      # 0x6fb0
callq 0x20d0
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x2220
cmpl $0x0, 0x8(%rsp)
jne 0x2581
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
subq 0x28(%rsp), %rax
cvtsi2sd %rax, %xmm1
subq 0x30(%rsp), %rcx
cvtsi2sd %rcx, %xmm0
divsd 0x1aa1(%rip), %xmm0     # 0x4010
addsd %xmm1, %xmm0
leaq 0x1ae1(%rip), %rdi      # 0x405b
movb $0x1, %al
callq 0x2040
callq 0x20e0
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r14
retq
nop