// File: /workspace/github_cpp_makefile_star2_O1/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, %r14d
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movl (%rax), %edx
movl 0x4(%rax), %ecx
leaq 0x14fa(%rip), %rsi      # 0x40a4
leaq -0xc0(%rbp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0x2060
movq %r15, %rdi
callq 0x2090
movq 0x8(%rbx), %rdx
movl 0x4(%rdx), %ecx
imull 0x8(%rdx), %ecx
movq %rax, -0x40(%rbp)
addl %eax, %ecx
movslq %ecx, %rdi
callq 0x2030
movq 0x20(%rbx), %rcx
movl %r14d, -0x2c(%rbp)
movslq %r14d, %rdx
movq %rax, (%rcx,%rdx,8)
movq 0x20(%rbx), %rax
movq %rdx, -0x48(%rbp)
movq (%rax,%rdx,8), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x2150
movq 0x8(%rbx), %rax
cmpl $0x0, 0xc(%rax)
jle 0x2d18
movslq -0x40(%rbp), %rax
addq %rax, %r13
movq $0x0, -0x38(%rbp)
movq 0x8(%rbx), %rax
cmpl $0x0, 0x10(%rax)
jle 0x2cfc
xorl %edx, %edx
movq %rsp, -0x50(%rbp)
movq 0x30(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rdx, -0x58(%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 0x434a(%rip), %rdx      # 0x6fc0
movl -0x2c(%rbp), %r8d
movq 0x432f(%rip), %r9       # 0x6fb0
pushq $0x0
callq 0x20f0
addq $0x10, %rsp
cmpl $0x0, 0x20(%r12)
jle 0x2ce0
xorl %r14d, %r14d
movslq 0x24(%r12), %rdx
movslq %r14d, %r14
movq %rdx, %rsi
imulq %r14, %rsi
addq %r15, %rsi
movl 0x18(%r12), %eax
addl %r14d, %eax
movq 0x8(%rbx), %rcx
imull 0x8(%rcx), %eax
movl 0x14(%r12), %ecx
leal 0x7(%rcx), %edi
testl %ecx, %ecx
cmovnsl %ecx, %edi
sarl $0x3, %edi
addl %eax, %edi
movslq %edi, %rdi
addq %r13, %rdi
callq 0x2100
incl %r14d
cmpl 0x20(%r12), %r14d
jl 0x2c97
movq -0x50(%rbp), %rsp
movq -0x58(%rbp), %rdx
incq %rdx
movq 0x8(%rbx), %rax
movslq 0x10(%rax), %rcx
cmpq %rcx, %rdx
jl 0x2c2f
movq -0x38(%rbp), %rdx
incq %rdx
movq 0x8(%rbx), %rax
movslq 0xc(%rax), %rax
movq %rdx, -0x38(%rbp)
cmpq %rax, %rdx
jl 0x2c1f
movq %rbx, %rdi
movl -0x2c(%rbp), %esi
callq 0x2d64
movq 0x8(%rbx), %rcx
movq 0x20(%rbx), %rsi
movl 0x4(%rcx), %edx
imull 0x8(%rcx), %edx
addl -0x40(%rbp), %edx
movq (%rax), %rdi
movq -0x48(%rbp), %r8
movq (%rsi,%r8,8), %rsi
shlq $0x3, %r8
addq 0x28(%rbx), %r8
movq 0x4251(%rip), %rcx      # 0x6fa0
callq 0x21a0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop