// File: /workspace/github_c_makefile_star2_O3/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, 0x14(%rsp)
movl %ecx, %ebx
movq 0x90(%rsp), %r15
testq %rsi, %rsi
leaq 0x75b57(%rip), %rbp     # 0x98797
cmovneq %rdx, %rbp
cmpq $0x1, %rsi
movq %rsi, %r12
adcq $0x0, %r12
movq 0x88(%rsp), %r8
andl $0x4, %ecx
shrl $0x2, %ecx
testl %r8d, %r8d
setle %dl
xorl %eax, %eax
orb %cl, %dl
jne 0x22cd2
movq %r9, %r11
movl %r8d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl $0x3c1a7, %r13d         # imm = 0x3C1A7
xorl %r14d, %r14d
xorl %r8d, %r8d
movl (%r15,%r8,4), %r9d
leal (%r9,%r9), %ecx
movl $0x3c1a7, %r10d         # imm = 0x3C1A7
shrl %cl, %r10d
shrl $0x4, %r9d
btl %ecx, %r13d
movl $0x0, %ecx
sbbl %ecx, %ecx
andl %r9d, %ecx
addq %rcx, %rax
shll $0x1e, %r10d
sarl $0x1f, %r10d
andl %r9d, %r10d
addq %r10, %r14
incq %r8
cmpq %r8, %rdx
jne 0x22c81
cmpq $0x1, %r14
adcq $0x0, %r14
movq %r11, %r9
movq 0x88(%rsp), %r8
jmp 0x22cd8
movl $0x1, %r14d
cmpq $0xff, %rsi
jb 0x22cf1
leaq 0x74a64(%rip), %rsi     # 0x9774c
leaq 0x74a66(%rip), %rdx     # 0x97755
jmp 0x22d4d
movabsq $0x7fffffff7fffffff, %rcx # imm = 0x7FFFFFFF7FFFFFFF
subq %r14, %rcx
cmpq %r9, %rcx
jle 0x22d3f
movq %rdi, 0x20(%rsp)
movq 0xb0(%rsp), %r13
testb $0x4, %bl
sete %dl
testq %r13, %r13
setne %cl
andb %dl, %cl
movl %ecx, %edx
xorb $0x1, %dl
testq %r8, %r8
setne %sil
orb %dl, %sil
jne 0x22d78
leaq 0x74a16(%rip), %rsi     # 0x9774c
leaq 0x74a58(%rip), %rdx     # 0x97795
jmp 0x22d4d
leaq 0x74a06(%rip), %rsi     # 0x9774c
leaq 0x74a1c(%rip), %rdx     # 0x97769
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
callq 0x4050
movl $0x16, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq %r13, %rax
setne %al
andb %al, %cl
cmpb $0x1, %cl
jne 0x22d95
leaq 0x749c0(%rip), %rsi     # 0x9774c
leaq 0x74a21(%rip), %rdx     # 0x977b4
jmp 0x22d4d
movl %r12d, %ecx
andl $0x3, %ecx
movl $0x4, %eax
movq %rcx, 0x38(%rsp)
subq %rcx, %rax
movq %rax, 0x40(%rsp)
leaq (%rax,%r12), %rsi
xorl %eax, %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %rsi, 0x18(%rsp)
subq %rsi, %rcx
cmovbq %rdx, %rcx
movl $0x0, %edx
sbbl %edx, %edx
leaq (,%r8,4), %r11
cmpq %r11, %rcx
movq %r11, %rdi
cmovbq %rax, %rdi
sbbl $0x0, %edx
movq 0xc8(%rsp), %rsi
subq %rdi, %rcx
leaq 0x1(%r13), %rdi
shrq %rdi
xorl %r8d, %r8d
cmpq %rdi, %rcx
setb %r8b
movq %rdi, %r10
cmovbq %rax, %r10
subq %r10, %rcx
cmpq %r13, %rcx
cmovaeq %r13, %rax
sbbl %r8d, %edx
subq %rax, %rcx
cmpq %rsi, %rcx
sbbl $0x0, %edx
jne 0x22ede
movq %r11, 0x30(%rsp)
movq 0x18(%rsp), %rax
leaq (%r11,%rax), %rcx
addq %r13, %rcx
addq %rdi, %rcx
addq %rcx, %rsi
movq 0x20(%rsp), %r13
movl 0x44(%r13), %eax
cmpq %rax, %rsi
movq %rcx, 0x8(%rsp)
jbe 0x22e6d
movq %r13, %rdi
movq %r9, 0x28(%rsp)
callq 0x22a64
movq 0x28(%rsp), %r9
movq 0x8(%rsp), %rcx
testl %eax, %eax
js 0x22d64
movb 0x80(%rsp), %r8b
movl %ecx, 0x40(%r13)
movq %r9, (%r13)
movl 0x14(%rsp), %eax
movl %eax, 0x8(%r13)
movq %r9, %rdi
leaq -0x1(%r9,%r14), %rsi
movl $0x1249, %r10d          # imm = 0x1249
movl $0xe, %eax
movl $0x6, %r14d
movl $0xc, %edx
movq 0x18(%rsp), %r9
movq %rdi, %r13
movl %eax, %ecx
sarq %cl, %r13
movq %rsi, %r11
sarq %cl, %r11
cmpq %r11, %r13
je 0x22ef1
addq $0x3, %rax
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movl %edx, %ecx
shll %cl, %r11d
addl %r11d, %r10d
decl %r14d
addl $-0x3, %edx
cmpl $0x1, %r14d
ja 0x22ea8
xorl %r10d, %r10d
jmp 0x22ef4
leaq 0x74867(%rip), %rsi     # 0x9774c
leaq 0x748f9(%rip), %rdx     # 0x977e5
jmp 0x22d4d
addl %r13d, %r10d
movq 0x20(%rsp), %rcx
movw %r10w, 0xc(%rcx)
movb %r8b, 0xe(%rcx)
movq 0x38(%rsp), %rax
xorb $0x3, %al
movb %al, 0xf(%rcx)
movw %bx, 0x10(%rcx)
movw %r9w, 0x12(%rcx)
movq 0x88(%rsp), %rax
movl %eax, 0x14(%rcx)
movq 0xb0(%rsp), %r13
movl %r13d, 0x18(%rcx)
movl 0x98(%rsp), %eax
movl %eax, 0x1c(%rcx)
movq 0xa0(%rsp), %rax
movq %rax, 0x20(%rcx)
movq 0xa8(%rsp), %rax
movq %rax, 0x28(%rcx)
movq 0x38(%rcx), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r9, %rbp
callq 0x4250
addq %rbx, %r12
xorl %r14d, %r14d
movq %r12, %rdi
xorl %esi, %esi
movq 0x40(%rsp), %rdx
callq 0x4260
addq %rbp, %rbx
cmpq $0x0, 0x88(%rsp)
movq 0x30(%rsp), %r12
je 0x22f99
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x43c0
addq %r12, %rbx
movq 0xb8(%rsp), %r8
cmpq $0x11, %r13
jb 0x22ff2
leaq 0x1(%r8), %rax
movl $0x10, %edx
leaq 0x729a6(%rip), %rcx     # 0x95960
movq %rdx, %r14
xorl %edx, %edx
movzbl -0x1(%rax,%rdx,2), %esi
movb (%rcx,%rsi), %sil
shlb $0x4, %sil
movzbl (%rax,%rdx,2), %edi
orb (%rcx,%rdi), %sil
movb %sil, (%rbx,%rdx)
incq %rdx
cmpq $0x8, %rdx
jne 0x22fbf
addq $0x8, %rbx
leaq 0x10(%r14), %rdx
addq $0x10, %rax
cmpq %r13, %rdx
jb 0x22fba
movl %r14d, %eax
orl $0x1, %eax
cmpq %r13, %rax
jae 0x23030
movl %r14d, %r14d
leaq 0x72959(%rip), %rax     # 0x95960
movq %r14, %rcx
movzbl (%r8,%r14), %edx
movb (%rax,%rdx), %dl
shlb $0x4, %dl
movzbl 0x1(%r8,%r14), %esi
orb (%rax,%rsi), %dl
movb %dl, (%rbx)
incq %rbx
addq $0x2, %r14
addq $0x3, %rcx
cmpq %r13, %rcx
jb 0x23007
movl %r14d, %ecx
movq %r13, %rax
subq %rcx, %rax
jbe 0x23063
addq %rcx, %r8
xorl %ecx, %ecx
leaq 0x72919(%rip), %rdx     # 0x95960
movzbl (%r8,%rcx), %esi
movb (%rdx,%rsi), %sil
shlb $0x4, %sil
movb %sil, (%rbx,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x23047
addq %rcx, %rbx
movq 0xc0(%rsp), %rsi
movq %rbx, %rdi
testq %rsi, %rsi
je 0x2307d
movq %r13, %rdx
callq 0x43c0
jmp 0x2308a
movl $0xff, %esi
movq %r13, %rdx
callq 0x4260
movq 0x8(%rsp), %rax
jmp 0x22d69