// File: /workspace/github_c_makefile_star2_O3/chriso[P]columnix/lib/writer.c
// Function: cx_row_group_writer_finish
bool cx_row_group_writer_finish(struct cx_row_group_writer *writer, bool sync)
{
    if (writer->footer_written)
        return true;
    if (!cx_row_group_writer_ensure_header(writer))
        return false;

    int32_t metadata_id = -1;
    size_t offset = cx_row_group_writer_offset(writer);

    // write metadata to the string repository
    if (writer->strings.metadata) {
        uint32_t id;
        if (!cx_row_group_writer_add_string(writer, writer->strings.metadata,
                                            &id))
            return false;
        metadata_id = id;
    }

    // write strings
    size_t strings_size;
    const void *strings =
        cx_column_export(writer->strings.column, &strings_size);
    if (!cx_row_group_writer_write(writer, strings, strings_size))
        goto error;

    // write row group headers
    size_t row_group_headers_size =
        writer->row_groups.count * sizeof(struct cx_row_group_header);
    if (!cx_row_group_writer_write(writer, writer->row_groups.headers,
                                   row_group_headers_size))
        goto error;

    // write column headers
    size_t column_descriptors_size =
        writer->columns.count * sizeof(struct cx_column_descriptor);
    if (!cx_row_group_writer_write(writer, writer->columns.descriptors,
                                   column_descriptors_size))
        goto error;

    // write the footer
    struct cx_footer footer = {offset,
                               strings_size,
                               metadata_id,
                               0,
                               writer->row_groups.count,
                               writer->columns.count,
                               writer->row_count,
                               sizeof(footer),
                               CX_FILE_VERSION,
                               CX_FILE_MAGIC};
    if (!cx_row_group_writer_write(writer, &footer, sizeof(footer)))
        goto error;

    // set the file size
    int fd = fileno(writer->file);
    if (ftruncate(fd, cx_row_group_writer_offset(writer)))
        goto error;

    // sync the file
    if (sync) {
#ifdef __APPLE__
        if (fcntl(fd, F_FULLFSYNC))
            goto error;
#else
        if (fsync(fd))
            goto error;
#endif
    }

    writer->footer_written = true;
    return true;
error:
    cx_row_group_writer_seek(writer, offset);
    return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x40, %rsp
movb $0x1, %r15b
cmpb $0x0, 0x59(%rdi)
jne 0xaf10
movl %esi, %ebp
movq %rdi, %rbx
callq 0xb1ce
testb %al, %al
je 0xae17
movq (%rbx), %rdi
callq 0x5410
movq %rax, %r14
movq 0x48(%rbx), %rsi
testq %rsi, %rsi
je 0xae1f
movq 0x38(%rbx), %rdi
callq 0x5210
testb %al, %al
je 0xae17
movq 0x40(%rbx), %r12
movq %r12, %rax
incq %rax
movq %rax, 0x40(%rbx)
jmp 0xae25
xorl %r15d, %r15d
jmp 0xaf10
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq 0x38(%rbx), %rdi
movq %rsp, %r15
movq %r15, %rsi
callq 0x5140
movq (%r15), %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0xb337
testb %al, %al
je 0xaf00
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
shlq $0x4, %rdx
movq %rbx, %rdi
callq 0xb337
testb %al, %al
je 0xaf00
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rdx
movq %rbx, %rdi
callq 0xb337
testb %al, %al
je 0xaf00
leaq 0x8(%rsp), %rsi
movq %r14, (%rsi)
movq (%rsp), %rax
movq %rax, 0x8(%rsi)
movl %r12d, 0x10(%rsi)
movl $0x0, 0x14(%rsi)
movl 0x28(%rbx), %eax
movl %eax, 0x18(%rsi)
movl 0x10(%rbx), %eax
movl %eax, 0x1c(%rsi)
movq 0x50(%rbx), %rax
movq %rax, 0x20(%rsi)
movabsq $0x100000038, %rax      # imm = 0x100000038
movq %rax, 0x28(%rsi)
movabsq $0x7863040378630201, %rax # imm = 0x7863040378630201
movq %rax, 0x30(%rsi)
movl $0x38, %edx
movq %rbx, %rdi
callq 0xb337
testb %al, %al
je 0xaf00
movq (%rbx), %rdi
callq 0x5530
movl %eax, %r15d
movq (%rbx), %rdi
callq 0x5410
movl %r15d, %edi
movq %rax, %rsi
callq 0x5710
testl %eax, %eax
je 0xaf20
movq (%rbx), %rdi
xorl %r15d, %r15d
movq %r14, %rsi
xorl %edx, %edx
callq 0x5660
movl %r15d, %eax
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
testb %bpl, %bpl
je 0xaf31
movl %r15d, %edi
callq 0x57d0
testl %eax, %eax
jne 0xaf00
movb $0x1, 0x59(%rbx)
movb $0x1, %r15b
jmp 0xaf10