// File: /workspace/github_c_makefile_star2_O2/chriso[P]columnix/lib/writer.c
// Function: cx_row_group_writer_put
bool cx_row_group_writer_put(struct cx_row_group_writer *writer,
                             struct cx_row_group *row_group)
{
    if (writer->footer_written)
        return false;

    // check column types match
    size_t column_count = cx_row_group_column_count(row_group);
    if (column_count != writer->columns.count)
        return false;
    if (!column_count)
        return true;  // noop
    for (size_t i = 0; i < column_count; i++) {
        struct cx_column_descriptor *descriptor =
            &writer->columns.descriptors[i];
        if (descriptor->type != cx_row_group_column_type(row_group, i))
            return false;
    }

    // make room for the extra row group header
    if (!writer->row_groups.count) {
        writer->row_groups.headers =
            malloc(sizeof(*writer->row_groups.headers) * 16);
        if (!writer->row_groups.headers)
            return false;
        writer->row_groups.size = 16;
    } else if (writer->row_groups.count == writer->row_groups.size) {
        size_t new_size = writer->row_groups.size * 2;
        assert(new_size && new_size > writer->row_groups.size);
        struct cx_row_group_header *headers =
            realloc(writer->row_groups.headers, new_size * sizeof(*headers));
        if (!headers)
            return false;
        writer->row_groups.headers = headers;
        writer->row_groups.size = new_size;
    }

    // write the header if it hasn't already been written
    if (!cx_row_group_writer_ensure_header(writer))
        return false;

    size_t row_group_offset = cx_row_group_writer_offset(writer);

    size_t headers_size = 2 * column_count * sizeof(struct cx_column_header);
    struct cx_column_header *headers = calloc(column_count, headers_size);
    if (!headers)
        goto error;

    // write columns
    for (size_t i = 0; i < column_count; i++) {
        const struct cx_column_descriptor *descriptor =
            &writer->columns.descriptors[i];
        const struct cx_column *column = cx_row_group_column(row_group, i);
        const struct cx_column *nulls = cx_row_group_nulls(row_group, i);
        if (!column || !nulls)
            goto error;
        const struct cx_index *index = cx_row_group_column_index(row_group, i);
        const struct cx_index *nulls_index =
            cx_row_group_column_index(row_group, i);
        if (!cx_row_group_writer_put_column(
                writer, column, index, &headers[i * 2], descriptor->compression,
                descriptor->compression_level))
            goto error;
        if (!cx_row_group_writer_put_column(
                writer, nulls, nulls_index, &headers[i * 2 + 1],
                CX_NULL_COMPRESSION_TYPE, CX_NULL_COMPRESSION_LEVEL))
            goto error;
    }

    // update the row group header
    struct cx_row_group_header *row_group_header =
        &writer->row_groups.headers[writer->row_groups.count++];
    row_group_header->size =
        cx_write_align(cx_row_group_writer_offset(writer)) - row_group_offset;
    row_group_header->offset = row_group_offset;

    // write column headers
    if (!cx_row_group_writer_write(writer, headers, headers_size))
        goto error;

    writer->row_count += cx_row_group_row_count(row_group);

    free(headers);
    return true;
error:
    cx_row_group_writer_seek(writer, row_group_offset);
    free(headers);
    return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
cmpb $0x0, 0x59(%rdi)
jne 0xaa9f
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x5470
cmpq 0x10(%rbx), %rax
jne 0xaa9f
movq %rax, %r15
testq %rax, %rax
je 0xa91c
pushq $0x4
popq %r13
xorl %r12d, %r12d
cmpq %r12, %r15
je 0xa923
movq 0x8(%rbx), %rax
movl (%rax,%r13), %ebp
movq %r14, %rdi
movq %r12, %rsi
callq 0x5920
incq %r12
addq $0x18, %r13
cmpl %eax, %ebp
je 0xa8f4
jmp 0xaa9f
movb $0x1, %bl
jmp 0xaaa1
movq 0x28(%rbx), %rsi
testq %rsi, %rsi
je 0xa967
cmpq 0x30(%rbx), %rsi
jne 0xa986
testq %rsi, %rsi
jle 0xab2c
movq %rsi, %r12
addq %rsi, %r12
je 0xab2c
movq 0x20(%rbx), %rdi
shlq $0x5, %rsi
callq 0x5710
testq %rax, %rax
je 0xaa9f
movq %rax, 0x20(%rbx)
movq %r12, 0x30(%rbx)
jmp 0xa986
movl $0x100, %edi            # imm = 0x100
callq 0x55d0
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0xaa9f
movq $0x10, 0x30(%rbx)
movq %rbx, %rdi
callq 0xab4b
testb %al, %al
je 0xaa9f
movq %rbx, %rdi
callq 0xab8f
movq %rax, 0x18(%rsp)
imulq $0x70, %r15, %rsi
movq %r15, %rdi
movq %rsi, 0x28(%rsp)
callq 0x5480
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0xab0e
pushq $0x10
popq %rax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq %r13, %r15
je 0xaab2
movq 0x8(%rbx), %rbp
movq %r14, %rdi
movq %r13, %rsi
callq 0x5360
movq %rax, %r12
movq %r14, %rdi
movq %r13, %rsi
callq 0x5680
testq %r12, %r12
je 0xab0e
testq %rax, %rax
je 0xab0e
movq %r14, %rdi
movq %r13, %rsi
movq %rax, 0x40(%rsp)
callq 0x5100
movq %rax, 0x38(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x5100
movq %rax, 0x30(%rsp)
movq 0x20(%rsp), %rax
movl -0x4(%rbp,%rax), %r8d
movl (%rbp,%rax), %r9d
movq %rbx, %rdi
movq %r12, %rsi
movq 0x38(%rsp), %rdx
movq 0x10(%rsp), %rcx
callq 0xab97
testb %al, %al
movq 0x40(%rsp), %rsi
je 0xab0e
movq 0x10(%rsp), %r12
leaq 0x38(%r12), %rcx
movq %rbx, %rdi
movq 0x30(%rsp), %rdx
pushq $0x1
popq %r8
xorl %r9d, %r9d
callq 0xab97
incq %r13
addq $0x70, %r12
movq %r12, 0x10(%rsp)
addq $0x18, 0x20(%rsp)
testb %al, %al
jne 0xa9d7
jmp 0xab0e
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rbx), %r15
movq 0x28(%rbx), %r12
leaq 0x1(%r12), %rax
movq %rax, 0x28(%rbx)
shlq $0x4, %r12
movq %rbx, %rdi
callq 0xab8f
addq $0x7, %rax
andq $-0x8, %rax
movq 0x18(%rsp), %rcx
subq %rcx, %rax
movq %rax, (%r15,%r12)
movq %rcx, 0x8(%r15,%r12)
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0xacae
testb %al, %al
je 0xab0e
movq %r14, %rdi
callq 0x5460
addq %rax, 0x50(%rbx)
movb $0x1, %bl
jmp 0xab1d
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
callq 0xad04
xorl %ebx, %ebx
movq 0x8(%rsp), %rdi
callq 0x5090
jmp 0xaaa1
leaq 0x1c62(%rip), %rdi      # 0xc795
leaq 0x1bb6(%rip), %rsi      # 0xc6f0
leaq 0x1c83(%rip), %rcx      # 0xc7c4
movl $0x1d0, %edx            # imm = 0x1D0
callq 0x5380