// File: /workspace/github_c_makefile_star2_O1/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 0xb340
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x5440
cmpq 0x10(%rbx), %rax
jne 0xb340
movq %rax, %r15
testq %rax, %rax
je 0xb173
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 0xb340
incq %r12
addq $0x18, %r13
cmpq %r12, %r15
jne 0xb105
movq 0x28(%rbx), %rax
testq %rax, %rax
je 0xb17a
movq 0x30(%rbx), %rsi
cmpq %rsi, %rax
jne 0xb199
testq %rsi, %rsi
jle 0xb36c
movq %rsi, %r12
addq %rsi, %r12
je 0xb36c
movq 0x20(%rbx), %rdi
shlq $0x5, %rsi
callq 0x56d0
testq %rax, %rax
je 0xb340
movq %rax, 0x20(%rbx)
movq %r12, 0x30(%rbx)
jmp 0xb199
movb $0x1, %bl
jmp 0xb342
movl $0x100, %edi            # imm = 0x100
callq 0x5590
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0xb340
movq $0x10, 0x30(%rbx)
movq %rbx, %rdi
callq 0xb38b
testb %al, %al
je 0xb340
movq (%rbx), %rdi
callq 0x5410
movq %rax, %r13
imulq $0x70, %r15, %rsi
movq %r15, %rdi
movq %rsi, 0x10(%rsp)
callq 0x5450
movq %rax, %r12
testq %rax, %rax
je 0xb353
movq %r13, 0x18(%rsp)
cmpq $0x1, %r15
adcq $0x0, %r15
movl $0x10, %ebp
xorl %r13d, %r13d
movq %r12, (%rsp)
movq %rbp, 0x8(%rsp)
movq 0x8(%rbx), %rax
movq %rax, 0x40(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x5330
movq %rax, %rbp
movq %r14, %rdi
movq %r13, %rsi
callq 0x5640
movq %rbp, 0x38(%rsp)
testq %rbp, %rbp
setne %dl
testq %rax, %rax
setne %cl
andb %dl, %cl
movl $0x5, %edx
cmpb $0x1, %cl
jne 0xb2b2
movq %rax, 0x28(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x50e0
movq %rax, 0x30(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x50e0
movq %rax, 0x20(%rsp)
movq 0x40(%rsp), %rax
movq 0x8(%rsp), %rbp
movl -0x4(%rax,%rbp), %r8d
movl (%rax,%rbp), %r9d
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq %r12, %rcx
callq 0xb3d1
testb %al, %al
movl $0x5, %edx
je 0xb2b7
leaq 0x38(%r12), %rcx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
movl $0x1, %r8d
xorl %r9d, %r9d
callq 0xb3d1
xorb $0x1, %al
movzbl %al, %eax
leal (%rax,%rax,4), %edx
jmp 0xb2b7
movq 0x8(%rsp), %rbp
testl %edx, %edx
jne 0xb2d4
incq %r13
addq $0x18, %rbp
addq $0x70, %r12
cmpq %r13, %r15
jne 0xb1ea
movl $0x6, %edx
cmpl $0x5, %edx
movq (%rsp), %r12
movq 0x18(%rsp), %r13
je 0xb353
cmpl $0x6, %edx
jne 0xb340
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
subq %r13, %rax
movq %rax, (%r15,%r12)
movq %r13, 0x8(%r15,%r12)
movq (%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
movq 0x10(%rsp), %rdx
callq 0xb4f8
testb %al, %al
je 0xb353
movq %r14, %rdi
callq 0x5430
addq %rax, 0x50(%rbx)
movb $0x1, %bl
jmp 0xb362
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 %r13, %rsi
xorl %edx, %edx
callq 0x5660
movq %r12, %rdi
callq 0x5080
jmp 0xb342
leaq 0x241f(%rip), %rdi      # 0xd792
leaq 0x2373(%rip), %rsi      # 0xd6ed
leaq 0x2440(%rip), %rcx      # 0xd7c1
movl $0x1d0, %edx            # imm = 0x1D0
callq 0x5350