// File: /workspace/github_cpp_makefile_star2_O2/Gammelkebab[P]GameOfLife/src/actors/writer.cpp
// Function: Writer::recv_and_write(int)
void Writer::recv_and_write(int round)
{

    // Create the header lines for this round
    char header_buffer[100];
    sprintf(header_buffer, "P4\n%d %d\n", world->width, world->height);
    int header_size = strlen(header_buffer);
    // Create the file write buffer for this round
    int write_buffer_size = header_size + world->width_byte * world->height;
    file_write_buffers[round] = new char[write_buffer_size];
    char *file_write_buffer = file_write_buffers[round];
    // Copy the header lines into the file write buffer
    strcpy(file_write_buffer, header_buffer);

    // Block buffer starts after header
    char *block_write_buffer = &file_write_buffer[header_size];
    // Receive all the block, store them in the file write buffer
    for (int world_row = 0; world_row < world->rows; world_row++)
    {
        for (int world_col = 0; world_col < world->cols; world_col++)
        {
            // Select the current block
            Block *block = world->blocks[world_row][world_col];
            // Create the receive buffer
            int block_size = block->width_byte * block->height;
            unsigned char recv_buffer[block_size];
            // Receive the blocks information
            MPI_Recv(recv_buffer, block_size, MPI_UNSIGNED_CHAR, block->block_num, round, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            // Select the current part of the write buffer
            // Copy the received data into the write buffer
            for (int block_row = 0; block_row < block->height; block_row++)
            {
                int recv_row_offset = block_row * block->width_byte;
                unsigned char *recv_row = &recv_buffer[recv_row_offset];
                int write_row_offset = (block_row + block->starting_y) * world->width_byte + block->starting_x / 8;
                char *write_row = &block_write_buffer[write_row_offset];
                memcpy(write_row, recv_row, block->width_byte);
            }
        }
    }
    MPI_File *file = open_file(round);
    // Write the file write buffer to the file
    iwrite_block(file, header_size, round);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %esi, %ebx
movq %rdi, %r14
movq 0x8(%rdi), %rax
movl (%rax), %edx
movl 0x4(%rax), %ecx
leaq 0x150f(%rip), %rsi      # 0x40a4
xorl %r12d, %r12d
leaq -0xc0(%rbp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0x2060
movq %r15, %rdi
callq 0x2090
movq %rax, %r13
movq 0x8(%r14), %rax
movl 0x4(%rax), %ecx
imull 0x8(%rax), %ecx
addl %r13d, %ecx
movslq %ecx, %rdi
callq 0x2030
movq 0x20(%r14), %rcx
movl %ebx, -0x2c(%rbp)
movslq %ebx, %rdx
movq %rax, (%rcx,%rdx,8)
movq 0x20(%r14), %rax
movq (%rax,%rdx,8), %r15
movq %r15, %rdi
leaq -0xc0(%rbp), %rsi
callq 0x2150
movq %r13, -0x38(%rbp)
movslq %r13d, %r13
addq %r15, %r13
movq 0x8(%r14), %rax
movslq 0xc(%rax), %rcx
cmpq %rcx, %r12
jge 0x2cdc
xorl %edx, %edx
movq %r12, -0x40(%rbp)
movslq 0x10(%rax), %rcx
cmpq %rcx, %rdx
jge 0x2cd4
movq %rsp, -0x48(%rbp)
movq 0x30(%rax), %rax
movq (%rax,%r12,8), %rax
movq %rdx, -0x50(%rbp)
movq (%rax,%rdx,8), %r12
movl 0x20(%r12), %esi
imull 0x24(%r12), %esi
leaq 0xf(%rsi), %rax
andq $-0x10, %rax
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
movl 0x8(%r12), %ecx
subq $0x8, %rsp
movq %r15, %rdi
movq 0x4360(%rip), %rdx      # 0x6fc0
movl -0x2c(%rbp), %r8d
movq 0x4345(%rip), %r9       # 0x6fb0
pushq $0x0
callq 0x20f0
popq %rcx
popq %rdx
xorl %ebx, %ebx
cmpl 0x20(%r12), %ebx
jge 0x2cbc
movslq 0x24(%r12), %rcx
movslq %ebx, %rsi
imulq %rcx, %rsi
addq %r15, %rsi
movl 0x18(%r12), %edi
addl %ebx, %edi
movq 0x8(%r14), %rax
imull 0x8(%rax), %edi
movl 0x14(%r12), %eax
cltd
pushq $0x8
popq %r8
idivl %r8d
addl %edi, %eax
movslq %eax, %rdi
addq %r13, %rdi
movq %rcx, %rdx
callq 0x2100
incl %ebx
jmp 0x2c76
movq -0x48(%rbp), %rsp
movq -0x50(%rbp), %rdx
incq %rdx
movq 0x8(%r14), %rax
movq -0x40(%rbp), %r12
jmp 0x2c10
incq %r12
jmp 0x2bfd
movq %r14, %rdi
movl -0x2c(%rbp), %ebx
movl %ebx, %esi
callq 0x2d0a
movq %r14, %rdi
movq %rax, %rsi
movq -0x38(%rbp), %rdx
movl %ebx, %ecx
callq 0x2d62
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop