// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
testb $0x1, 0x59(%rax)
je 0xf686
movb $0x0, -0x1(%rbp)
jmp 0xfa12
movq -0x18(%rbp), %rdi
callq 0x54c0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
je 0xf6aa
movb $0x0, -0x1(%rbp)
jmp 0xfa12
cmpq $0x0, -0x20(%rbp)
jne 0xf6ba
movb $0x1, -0x1(%rbp)
jmp 0xfa12
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0xf71f
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
imulq $0x18, -0x28(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x94(%rbp)
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x5970
movl %eax, %ecx
movl -0x94(%rbp), %eax
cmpl %ecx, %eax
je 0xf70f
movb $0x0, -0x1(%rbp)
jmp 0xfa12
jmp 0xf711
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xf6c2
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0xf764
movl $0x100, %edi            # imm = 0x100
callq 0x5630
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0xf753
movb $0x0, -0x1(%rbp)
jmp 0xfa12
movq -0x10(%rbp), %rax
movq $0x10, 0x30(%rax)
jmp 0xf802
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jne 0xf800
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
shlq %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0xf7a0
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x30(%rcx), %rax
jbe 0xf7a0
jmp 0xf7bf
leaq 0x2016(%rip), %rdi      # 0x117bd
leaq 0x1f6a(%rip), %rsi      # 0x11718
movl $0x1d0, %edx            # imm = 0x1D0
leaq 0x2032(%rip), %rcx      # 0x117ec
callq 0x53a0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x38(%rbp), %rsi
shlq $0x4, %rsi
callq 0x5760
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0xf7e8
movb $0x0, -0x1(%rbp)
jmp 0xfa12
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
jmp 0xf802
movq -0x10(%rbp), %rdi
callq 0xfa20
testb $0x1, %al
jne 0xf818
movb $0x0, -0x1(%rbp)
jmp 0xfa12
movq -0x10(%rbp), %rdi
callq 0xfaa0
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
shlq %rax
imulq $0x38, %rax, %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x54d0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0xf851
jmp 0xf9f8
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0xf960
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
imulq $0x18, -0x60(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x5380
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x56d0
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0xf8ab
cmpq $0x0, -0x78(%rbp)
jne 0xf8b0
jmp 0xf9f8
movq -0x18(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x5110
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x5110
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x80(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x60(%rbp), %rax
shlq %rax
imulq $0x38, %rax, %rax
addq %rax, %rcx
movq -0x68(%rbp), %rax
movl 0xc(%rax), %r8d
movq -0x68(%rbp), %rax
movl 0x10(%rax), %r9d
callq 0xfac0
testb $0x1, %al
jne 0xf911
jmp 0xf9f8
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x88(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x60(%rbp), %rax
shlq %rax
addq $0x1, %rax
imulq $0x38, %rax, %rax
addq %rax, %rcx
movl $0x1, %r8d
xorl %r9d, %r9d
callq 0xfac0
testb $0x1, %al
jne 0xf94d
jmp 0xf9f8
jmp 0xf94f
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0xf859
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rdx
movq 0x28(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x28(%rdx)
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x10(%rbp), %rdi
callq 0xfaa0
movq %rax, %rdi
callq 0xfc20
movq %rax, %rcx
subq -0x48(%rbp), %rcx
movq -0x90(%rbp), %rax
movq %rcx, (%rax)
movq -0x48(%rbp), %rcx
movq -0x90(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0xfc60
testb $0x1, %al
jne 0xf9d1
jmp 0xf9f8
movq -0x18(%rbp), %rdi
callq 0x54a0
movq %rax, %rcx
movq -0x10(%rbp), %rax
addq 0x50(%rax), %rcx
movq %rcx, 0x50(%rax)
movq -0x58(%rbp), %rdi
callq 0x50a0
movb $0x1, -0x1(%rbp)
jmp 0xfa12
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xfcf0
movq -0x58(%rbp), %rdi
callq 0x50a0
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xa0, %rsp
popq %rbp
retq