// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/cram/cram_encode.c
// Function: extend_ref
static inline int extend_ref(char **ref, uint32_t (**hist)[5], hts_pos_t pos,
                             hts_pos_t ref_start, hts_pos_t *ref_end) {
    if (pos < ref_start)
        return -1;
    if (pos < *ref_end)
        return 0;

    // realloc
    if (pos - ref_start > UINT_MAX)
        return -2; // protect overflow in new_end calculation

    hts_pos_t old_end = *ref_end ? *ref_end : ref_start;
    hts_pos_t new_end = ref_start + 1000 + (pos-ref_start)*1.5;

    // Refuse to work on excessively large blocks.
    // We'll just switch to referenceless encoding, which is probably better
    // here as this must be very sparse data anyway.
    if (new_end - ref_start > UINT_MAX/sizeof(**hist)/2)
        return -2;

    char *tmp = realloc(*ref, new_end-ref_start+1);
    if (!tmp)
        return -1;
    *ref = tmp;

    uint32_t (*tmp5)[5] = realloc(**hist,
                                  (new_end - ref_start)*sizeof(**hist));
    if (!tmp5)
        return -1;
    *hist = tmp5;
    *ref_end = new_end;

    // initialise
    old_end -= ref_start;
    new_end -= ref_start;
    memset(&(*ref)[old_end],  0,  new_end-old_end);
    memset(&(*hist)[old_end], 0, (new_end-old_end)*sizeof(**hist));

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x643a2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6452e
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq (%rcx), %rax
jge 0x643bb
movl $0x0, -0x4(%rbp)
jmp 0x6452e
movq -0x20(%rbp), %rax
subq -0x28(%rbp), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
jle 0x643d9
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x6452e
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
je 0x643f0
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
jmp 0x643f8
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
addq $0x3e8, %rax            # imm = 0x3E8
cvtsi2sd %rax, %xmm1
movq -0x20(%rbp), %rax
subq -0x28(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x8fd4c(%rip), %xmm2    # 0xf4170
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
subq -0x28(%rbp), %rax
cmpq $0x6666666, %rax        # imm = 0x6666666
jbe 0x64451
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x6452e
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x40(%rbp), %rsi
subq -0x28(%rbp), %rsi
addq $0x1, %rsi
callq 0x4820
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x64480
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6452e
movq -0x48(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x40(%rbp), %rax
subq -0x28(%rbp), %rax
imulq $0x14, %rax, %rsi
callq 0x4820
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x644b7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6452e
movq -0x50(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq -0x38(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rcx
movq -0x40(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
addq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rdx
subq -0x38(%rbp), %rdx
xorl %esi, %esi
callq 0x4250
movq -0x18(%rbp), %rax
movq (%rax), %rdi
imulq $0x14, -0x38(%rbp), %rax
addq %rax, %rdi
movq -0x40(%rbp), %rax
subq -0x38(%rbp), %rax
imulq $0x14, %rax, %rdx
xorl %esi, %esi
callq 0x4250
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)