// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/cram/cram_encode.c
// Function: cram_generate_reference
static int cram_generate_reference(cram_container *c, cram_slice *s, int r1) {
    // TODO: if we can find an external reference then use it, even if the
    // user told us to do embed_ref=2.
    char *ref = NULL;
    uint32_t (*hist)[5] = NULL;
    hts_pos_t ref_start = c->bams[r1]->core.pos, ref_end = 0;
    if (ref_start < 0)
        return -1; // cannot build consensus from unmapped data

    // initial allocation
    if (extend_ref(&ref, &hist,
                   c->bams[r1 + s->hdr->num_records-1]->core.pos +
                   c->bams[r1 + s->hdr->num_records-1]->core.l_qseq,
                   ref_start, &ref_end) < 0)
        return -1;

    // Add each bam file to the reference/consensus arrays
    int r2;
    hts_pos_t last_pos = -1;
    for (r2 = 0; r1 < c->curr_c_rec && r2 < s->hdr->num_records; r1++, r2++) {
        if (c->bams[r1]->core.pos < last_pos) {
            hts_log_error("Cannot build reference with unsorted data");
            goto err;
        }
        last_pos = c->bams[r1]->core.pos;
        if (cram_add_to_ref(c->bams[r1], &ref, &hist, ref_start, &ref_end) < 0)
            goto err;
    }

    // Compute the consensus
    hts_pos_t i;
    for (i = 0; i < ref_end-ref_start; i++) {
        if (!ref[i]) {
            int max_v = 0, max_j = 4, j;
            for (j = 0; j < 4; j++)
                // don't call N (j==4) unless no coverage
                if (max_v < hist[i][j])
                    max_v = hist[i][j], max_j = j;
            ref[i] = "ACGTN"[max_j];
        }
    }
    free(hist);

    // Put the reference in place so it appears to be an external
    // ref file.
    c->ref       = ref;
    c->ref_start = ref_start+1;
    c->ref_end   = ref_end+1;
    c->ref_free  = 1;
    return 0;

 err:
    free(ref);
    free(hist);
    return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
cmpq $0x0, -0x38(%rbp)
jge 0x5f388
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5f5fc
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rax
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rdx
movq (%rdx), %rdx
addl 0x18(%rdx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rdx
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rax
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rsi
movq (%rsi), %rsi
addl 0x18(%rsi), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq 0x18(%rax), %rax
addq %rax, %rdx
movq -0x38(%rbp), %rcx
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x40(%rbp), %r8
callq 0x64370
cmpl $0x0, %eax
jge 0x5f3fc
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5f5fc
movq $-0x1, -0x50(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x1c(%rbp), %ecx
movq -0x10(%rbp), %rdx
xorl %eax, %eax
cmpl 0x70(%rdx), %ecx
movb %al, -0x65(%rbp)
jge 0x5f42f
movl -0x44(%rbp), %eax
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
cmpl 0x18(%rcx), %eax
setl %al
movb %al, -0x65(%rbp)
movb -0x65(%rbp), %al
testb $0x1, %al
jne 0x5f43b
jmp 0x5f4db
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
cmpq -0x50(%rbp), %rax
jge 0x5f476
movl $0x1, %edi
leaq 0x95200(%rip), %rsi     # 0xf4663
leaq 0x95211(%rip), %rdx     # 0xf467b
movb $0x0, %al
callq 0x1b030
jmp 0x5f5e3
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x38(%rbp), %rcx
leaq -0x28(%rbp), %rsi
leaq -0x30(%rbp), %rdx
leaq -0x40(%rbp), %r8
callq 0x64540
cmpl $0x0, %eax
jge 0x5f4c2
jmp 0x5f5e3
jmp 0x5f4c4
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x5f40b
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x40(%rbp), %rcx
subq -0x38(%rbp), %rcx
cmpq %rcx, %rax
jge 0x5f58e
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
jne 0x5f57b
movl $0x0, -0x5c(%rbp)
movl $0x4, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
cmpl $0x4, -0x64(%rbp)
jge 0x5f562
movl -0x5c(%rbp), %eax
movq -0x30(%rbp), %rcx
imulq $0x14, -0x58(%rbp), %rdx
addq %rdx, %rcx
movslq -0x64(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jae 0x5f555
movq -0x30(%rbp), %rax
imulq $0x14, -0x58(%rbp), %rcx
addq %rcx, %rax
movslq -0x64(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x5c(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0x5f557
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x5f51b
movslq -0x60(%rbp), %rcx
leaq 0x95138(%rip), %rax     # 0xf46a5
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
movb %dl, (%rax,%rcx)
jmp 0x5f57d
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x5f4e3
movq -0x30(%rbp), %rdi
callq 0x45a0
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xe0(%rax)
movq -0x38(%rbp), %rcx
addq $0x1, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xc0(%rax)
movq -0x40(%rbp), %rcx
addq $0x1, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xd8(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x29c(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x5f5fc
movq -0x28(%rbp), %rdi
callq 0x45a0
movq -0x30(%rbp), %rdi
callq 0x45a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)