// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/sam.c
// Function: bam_set1
int bam_set1(bam1_t *bam,
             size_t l_qname, const char *qname,
             uint16_t flag, int32_t tid, hts_pos_t pos, uint8_t mapq,
             size_t n_cigar, const uint32_t *cigar,
             int32_t mtid, hts_pos_t mpos, hts_pos_t isize,
             size_t l_seq, const char *seq, const char *qual,
             size_t l_aux)
{
    // use a default qname "*" if none is provided
    if (l_qname == 0) {
        l_qname = 1;
        qname = "*";
    }

    // note: the qname is stored nul terminated and padded as described in the
    // documentation for the bam1_t struct.
    size_t qname_nuls = 4 - l_qname % 4;

    // the aligment length, needed for bam_reg2bin(), is calculated as in bam_endpos().
    // can't use bam_endpos() directly as some fields not yet set up.
    hts_pos_t rlen = 0, qlen = 0;
    if (!(flag & BAM_FUNMAP)) {
        bam_cigar2rqlens((int)n_cigar, cigar, &rlen, &qlen);
    }
    if (rlen == 0) {
        rlen = 1;
    }

    // validate parameters
    if (l_qname > 254) {
        hts_log_error("Query name too long");
        errno = EINVAL;
        return -1;
    }
    if (HTS_POS_MAX - rlen <= pos) {
        hts_log_error("Read ends beyond highest supported position");
        errno = EINVAL;
        return -1;
    }
    if (!(flag & BAM_FUNMAP) && l_seq > 0 && n_cigar == 0) {
        hts_log_error("Mapped query must have a CIGAR");
        errno = EINVAL;
        return -1;
    }
    if (!(flag & BAM_FUNMAP) && l_seq > 0 && l_seq != qlen) {
        hts_log_error("CIGAR and query sequence are of different length");
        errno = EINVAL;
        return -1;
    }

    size_t limit = INT32_MAX;
    int u = subtract_check_underflow(l_qname + qname_nuls, &limit);
    u    += subtract_check_underflow(n_cigar * 4, &limit);
    u    += subtract_check_underflow((l_seq + 1) / 2, &limit);
    u    += subtract_check_underflow(l_seq, &limit);
    u    += subtract_check_underflow(l_aux, &limit);
    if (u != 0) {
        hts_log_error("Size overflow");
        errno = EINVAL;
        return -1;
    }

    // re-allocate the data buffer as needed.
    size_t data_len = l_qname + qname_nuls + n_cigar * 4 + (l_seq + 1) / 2 + l_seq;
    if (realloc_bam_data(bam, data_len + l_aux) < 0) {
        return -1;
    }

    bam->l_data = (int)data_len;
    bam->core.pos = pos;
    bam->core.tid = tid;
    bam->core.bin = bam_reg2bin(pos, pos + rlen);
    bam->core.qual = mapq;
    bam->core.l_extranul = (uint8_t)(qname_nuls - 1);
    bam->core.flag = flag;
    bam->core.l_qname = (uint16_t)(l_qname + qname_nuls);
    bam->core.n_cigar = (uint32_t)n_cigar;
    bam->core.l_qseq = (int32_t)l_seq;
    bam->core.mtid = mtid;
    bam->core.mpos = mpos;
    bam->core.isize = isize;

    uint8_t *cp = bam->data;
    strncpy((char *)cp, qname, l_qname);
    int i;
    for (i = 0; i < qname_nuls; i++) {
        cp[l_qname + i] = '\0';
    }
    cp += l_qname + qname_nuls;

    if (n_cigar > 0) {
        memcpy(cp, cigar, n_cigar * 4);
    }
    cp += n_cigar * 4;

#define NN 16
    const uint8_t *useq = (uint8_t *)seq;
    for (i = 0; i + NN < l_seq; i += NN) {
        int j;
        const uint8_t *u2 = useq+i;
        for (j = 0; j < NN/2; j++)
            cp[j] = (seq_nt16_table[u2[j*2]]<<4) | seq_nt16_table[u2[j*2+1]];
        cp += NN/2;
    }
    for (; i + 1 < l_seq; i += 2) {
        *cp++ = (seq_nt16_table[useq[i]] << 4) | seq_nt16_table[useq[i + 1]];
    }

    for (; i < l_seq; i++) {
        *cp++ = seq_nt16_table[(unsigned char)seq[i]] << 4;
    }

    if (qual) {
        memcpy(cp, qual, l_seq);
    }
    else {
        memset(cp, '\xff', l_seq);
    }

    return (int)data_len;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r8d, 0xc(%rsp)
movl %ecx, %r12d
movq %rsi, %r14
movq %rdi, %rbx
movq 0x90(%rsp), %rsi
testq %r14, %r14
leaq 0x5c653(%rip), %r15     # 0x7a6f8
cmovneq %rdx, %r15
cmpq $0x1, %r14
movq %r14, %r13
adcq $0x0, %r13
movq 0x88(%rsp), %rdi
andq $0x0, 0x20(%rsp)
andq $0x0, 0x18(%rsp)
testb $0x4, %r12b
jne 0x1e0f8
leaq 0x20(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movq %r9, %rbp
callq 0x1e412
movq %rbp, %r9
movq 0x88(%rsp), %rdi
leaq 0x20(%rsp), %rax
movq (%rax), %rbp
testq %rbp, %rbp
jne 0x1e0fb
pushq $0x1
popq %rbp
cmpq $0xff, %r14
jb 0x1e114
leaq 0x5b591(%rip), %rsi     # 0x7969c
leaq 0x5b593(%rip), %rdx     # 0x796a5
jmp 0x1e16a
movabsq $0x7fffffff7fffffff, %rax # imm = 0x7FFFFFFF7FFFFFFF
subq %rbp, %rax
cmpq %r9, %rax
jle 0x1e15c
movq 0xb0(%rsp), %r10
testb $0x4, %r12b
sete %cl
testq %r10, %r10
setne %al
andb %cl, %al
movl %eax, %ecx
xorb $0x1, %cl
testq %rdi, %rdi
setne %dl
orb %cl, %dl
jne 0x1e195
leaq 0x5b549(%rip), %rsi     # 0x7969c
leaq 0x5b58b(%rip), %rdx     # 0x796e5
jmp 0x1e16a
leaq 0x5b539(%rip), %rsi     # 0x7969c
leaq 0x5b54f(%rip), %rdx     # 0x796b9
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
callq 0x4050
movl $0x16, (%rax)
pushq $-0x1
popq %r14
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq %r10, 0x18(%rsp)
setne %cl
andb %cl, %al
cmpb $0x1, %al
jne 0x1e1b3
leaq 0x5b4f2(%rip), %rsi     # 0x7969c
leaq 0x5b553(%rip), %rdx     # 0x79704
jmp 0x1e16a
movl %r13d, %ecx
andl $0x3, %ecx
pushq $0x4
popq %rax
movq %rcx, 0x38(%rsp)
subq %rcx, %rax
movq %rax, 0x40(%rsp)
leaq (%rax,%r13), %rdx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rdx, 0x10(%rsp)
subq %rdx, %rax
leaq (,%rdi,4), %r14
xorl %edx, %edx
cmpq %r14, %rax
movq %r14, %rcx
cmovbq %rdx, %rcx
movq 0xc8(%rsp), %rsi
movl $0x0, %edi
sbbl %edi, %edi
subq %rcx, %rax
leaq 0x1(%r10), %rcx
shrq %rcx
xorl %r8d, %r8d
cmpq %rcx, %rax
movq %rcx, %r11
cmovbq %rdx, %r11
setb %r8b
subq %r11, %rax
cmpq %r10, %rax
movq %r10, %r11
cmovbq %rdx, %r11
sbbl %r8d, %edi
subq %r11, %rax
cmpq %rsi, %rax
sbbl $0x0, %edi
jne 0x1e318
movq %r9, 0x30(%rsp)
movq %r14, 0x28(%rsp)
movq 0x10(%rsp), %rax
addq %rax, %r14
addq %r10, %r14
addq %rcx, %r14
addq %r14, %rsi
movq %rbx, %rdi
callq 0x1e02a
testl %eax, %eax
js 0x1e17f
movl %r14d, 0x40(%rbx)
movq 0x30(%rsp), %rdi
movq %rdi, (%rbx)
movl 0xc(%rsp), %eax
movl %eax, 0x8(%rbx)
addq %rdi, %rbp
movq %rbp, %rsi
callq 0x1e461
movw %ax, 0xc(%rbx)
movb 0x80(%rsp), %al
movb %al, 0xe(%rbx)
movq 0x38(%rsp), %rax
xorb $0x3, %al
movb %al, 0xf(%rbx)
movw %r12w, 0x10(%rbx)
movq 0x10(%rsp), %rbp
movw %bp, 0x12(%rbx)
movq 0x88(%rsp), %rax
movl %eax, 0x14(%rbx)
movq 0xb0(%rsp), %rax
movq %rax, %r12
movl %eax, 0x18(%rbx)
movl 0x98(%rsp), %eax
movl %eax, 0x1c(%rbx)
movq 0xa0(%rsp), %rax
movq %rax, 0x20(%rbx)
movq 0xa8(%rsp), %rax
movq %rax, 0x28(%rbx)
movq 0x38(%rbx), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x4250
addq %rbx, %r13
xorl %eax, %eax
movq 0x90(%rsp), %rsi
movq 0x40(%rsp), %rcx
movq %rbp, %rdx
cmpq %rax, %rcx
je 0x1e32b
movb $0x0, (%r13,%rax)
incq %rax
jmp 0x1e308
leaq 0x5b37d(%rip), %rsi     # 0x7969c
leaq 0x5b40f(%rip), %rdx     # 0x79735
jmp 0x1e16a
addq %rdx, %rbx
cmpq $0x0, 0x88(%rsp)
movq 0x28(%rsp), %r15
je 0x1e349
movq %rbx, %rdi
movq %r15, %rdx
callq 0x43c0
addq %r15, %rbx
movq 0xb8(%rsp), %r10
leaq 0x1(%r10), %r9
xorl %ecx, %ecx
leaq 0x595af(%rip), %rax     # 0x77910
movq %r12, %rdx
leaq 0x10(%rcx), %rsi
cmpq %rdx, %rsi
jae 0x1e3c3
xorl %ecx, %ecx
cmpq $0x8, %rcx
je 0x1e395
movzbl -0x1(%r9,%rcx,2), %edi
movb (%rax,%rdi), %dil
shlb $0x4, %dil
movzbl (%r9,%rcx,2), %r8d
orb (%rax,%r8), %dil
movb %dil, (%rbx,%rcx)
incq %rcx
jmp 0x1e36f
addq $0x8, %rbx
addq $0x10, %r9
movq %rsi, %rcx
jmp 0x1e364
movzbl (%r10,%rcx), %esi
movb (%rax,%rsi), %dil
shlb $0x4, %dil
movzbl 0x1(%r10,%rcx), %esi
orb (%rax,%rsi), %dil
movb %dil, (%rbx)
incq %rbx
addq $0x2, %rcx
leaq 0x1(%rcx), %rsi
cmpq %rdx, %rsi
jb 0x1e3a2
jmp 0x1e3e4
movzbl (%r10,%rcx), %esi
movb (%rax,%rsi), %sil
shlb $0x4, %sil
movb %sil, (%rbx)
incq %rbx
incq %rcx
cmpq %rdx, %rcx
jb 0x1e3ce
movq 0xc0(%rsp), %rsi
movq %rbx, %rdi
testq %rsi, %rsi
je 0x1e403
callq 0x43c0
jmp 0x1e183
movl $0xff, %esi
callq 0x4260
jmp 0x1e183