// File: 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
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0x70(%rbp), %rdi
movq 0x8(%rax), %rcx
movl (%rcx), %edx
movq 0x8(%rax), %rax
movl 0x4(%rax), %ecx
leaq 0x2e1b(%rip), %rsi      # 0x11190
movb $0x0, %al
callq 0x80e0
leaq -0x70(%rbp), %rdi
callq 0x81f0
movq -0xe0(%rbp), %rdx
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
movq 0x8(%rdx), %rcx
movl 0x8(%rcx), %ecx
movq 0x8(%rdx), %rdx
imull 0x4(%rdx), %ecx
addl %ecx, %eax
movl %eax, -0x78(%rbp)
movslq -0x78(%rbp), %rdi
callq 0x8030
movq %rax, %rsi
movq -0xe0(%rbp), %rax
movq 0x20(%rax), %rcx
movslq -0xc(%rbp), %rdx
movq %rsi, (%rcx,%rdx,8)
movq 0x20(%rax), %rax
movslq -0xc(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x8530
movq -0x80(%rbp), %rax
movslq -0x74(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movq -0xe0(%rbp), %rcx
movl -0x8c(%rbp), %eax
movq 0x8(%rcx), %rcx
cmpl 0xc(%rcx), %eax
jge 0xe5fd
movl $0x0, -0x90(%rbp)
movq -0xe0(%rbp), %rcx
movl -0x90(%rbp), %eax
movq 0x8(%rcx), %rcx
cmpl 0x10(%rcx), %eax
jge 0xe5e7
movq -0xe0(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x30(%rax), %rax
movslq -0x8c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x90(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movl 0x20(%rax), %ecx
movl 0x24(%rax), %eax
imull %ecx, %eax
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0xa8(%rbp)
movq %rax, %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0xe8(%rbp)
movq %rdi, %rsp
movq %rax, -0xb0(%rbp)
movl -0x9c(%rbp), %esi
movq -0x98(%rbp), %rax
movl 0x8(%rax), %ecx
movl -0xc(%rbp), %r8d
movq 0x8aed(%rip), %rdx      # 0x16fb8
movq 0x8ad6(%rip), %r9       # 0x16fa8
xorl %eax, %eax
subq $0x10, %rsp
movq $0x0, (%rsp)
callq 0x8480
addq $0x10, %rsp
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
movq -0x98(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jge 0xe5c9
movq -0xe0(%rbp), %rcx
movq -0xe8(%rbp), %rax
movl -0xb4(%rbp), %edx
movq -0x98(%rbp), %rsi
imull 0x24(%rsi), %edx
movl %edx, -0xb8(%rbp)
movslq -0xb8(%rbp), %rdx
addq %rdx, %rax
movq %rax, -0xc0(%rbp)
movl -0xb4(%rbp), %eax
movq -0x98(%rbp), %rdx
addl 0x18(%rdx), %eax
movq 0x8(%rcx), %rcx
imull 0x8(%rcx), %eax
movl %eax, -0xec(%rbp)
movq -0x98(%rbp), %rax
movl 0x14(%rax), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0xec(%rbp), %eax
addl %ecx, %eax
movl %eax, -0xc4(%rbp)
movq -0x88(%rbp), %rax
movslq -0xc4(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movq -0x98(%rbp), %rax
movslq 0x24(%rax), %rdx
callq 0x8490
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0xe4f3
movq -0xa8(%rbp), %rax
movq %rax, %rsp
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0xe422
jmp 0xe5e9
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xe3fe
movq -0xe0(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0xe640
movq -0xe0(%rbp), %rdi
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rsi
movl -0x74(%rbp), %edx
movl -0xc(%rbp), %ecx
callq 0xe6c0
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)