// File: /workspace/github_c_makefile_star2_O3/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 0xb17f
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x5440
cmpq 0x10(%rbx), %rax
jne 0xb17f
movq %rax, %r15
testq %rax, %rax
je 0xafe6
movl $0x4, %r13d
xorl %r12d, %r12d
movq 0x8(%rbx), %rax
movl (%rax,%r13), %ebp
movq %r14, %rdi
movq %r12, %rsi
callq 0x58d0
cmpl %eax, %ebp
jne 0xb17f
incq %r12
addq $0x18, %r13
cmpq %r12, %r15
jne 0xaf7b
movq 0x28(%rbx), %rsi
testq %rsi, %rsi
je 0xafed
cmpq 0x30(%rbx), %rsi
jne 0xb00c
testq %rsi, %rsi
jle 0xb1af
movq %rsi, %r12
addq %rsi, %r12
je 0xb1af
movq 0x20(%rbx), %rdi
shlq $0x5, %rsi
callq 0x56d0
testq %rax, %rax
je 0xb17f
movq %rax, 0x20(%rbx)
movq %r12, 0x30(%rbx)
jmp 0xb00c
movb $0x1, %bl
jmp 0xb181
movl $0x100, %edi            # imm = 0x100
callq 0x5590
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0xb17f
movq $0x10, 0x30(%rbx)
movq %rbx, %rdi
callq 0xb1ce
testb %al, %al
je 0xb17f
movq (%rbx), %rdi
callq 0x5410
movq %rax, 0x18(%rsp)
imulq $0x70, %r15, %rsi
movq %r15, %rdi
movq %rsi, 0x28(%rsp)
callq 0x5450
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0xb192
movl $0x10, %eax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rbx), %rbp
movq %r14, %rdi
movq %r13, %rsi
callq 0x5330
movq %rax, %r12
movq %r14, %rdi
movq %r13, %rsi
callq 0x5640
testq %r12, %r12
je 0xb192
testq %rax, %rax
je 0xb192
movq %r14, %rdi
movq %rax, 0x40(%rsp)
movq %r13, %rsi
callq 0x50e0
movq %rax, 0x38(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x50e0
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 0xb214
movq 0x40(%rsp), %rsi
testb %al, %al
je 0xb192
movq 0x10(%rsp), %rax
leaq 0x38(%rax), %rcx
movq %rbx, %rdi
movq 0x30(%rsp), %rdx
movl $0x1, %r8d
xorl %r9d, %r9d
callq 0xb214
testb %al, %al
je 0xb192
incq %r13
addq $0x18, 0x20(%rsp)
addq $0x70, 0x10(%rsp)
cmpq %r13, %r15
jne 0xb05f
movq (%rbx), %rdi
movq 0x20(%rbx), %r15
movq 0x28(%rbx), %r12
leaq 0x1(%r12), %rax
movq %rax, 0x28(%rbx)
shlq $0x4, %r12
callq 0x5410
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 0xb337
testb %al, %al
je 0xb192
movq %r14, %rdi
callq 0x5430
addq %rax, 0x50(%rbx)
movb $0x1, %bl
jmp 0xb1a3
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rdi
xorl %ebx, %ebx
movq 0x18(%rsp), %rsi
xorl %edx, %edx
callq 0x5660
movq 0x8(%rsp), %rdi
callq 0x5080
jmp 0xb181
leaq 0x25fc(%rip), %rdi      # 0xd7b2
leaq 0x2550(%rip), %rsi      # 0xd70d
leaq 0x261d(%rip), %rcx      # 0xd7e1
movl $0x1d0, %edx            # imm = 0x1D0
callq 0x5350