// File: /workspace/github_c_makefile_star2_O1/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
movq %r9, 0x18(%rsp)
movl %r8d, 0x24(%rsp)
movl %ecx, %ebx
movq 0x90(%rsp), %r15
testq %rsi, %rsi
leaq 0x78135(%rip), %rbp     # 0x9b790
cmovneq %rdx, %rbp
cmpq $0x1, %rsi
movq %rsi, %r12
adcq $0x0, %r12
movq 0x88(%rsp), %r11
movl %ecx, %eax
andl $0x4, %eax
shrl $0x2, %eax
testl %r11d, %r11d
setle %cl
xorl %r14d, %r14d
orb %al, %cl
jne 0x236d9
movl %r11d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl $0x3c1a7, %r13d         # imm = 0x3C1A7
xorl %eax, %eax
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 0x2369b
jmp 0x236db
xorl %eax, %eax
cmpq $0x1, %r14
adcq $0x0, %r14
cmpq $0xff, %rsi
jb 0x236fc
leaq 0x77049(%rip), %rsi     # 0x9a73c
leaq 0x7704b(%rip), %rdx     # 0x9a745
jmp 0x2375a
movabsq $0x7fffffff7fffffff, %rcx # imm = 0x7FFFFFFF7FFFFFFF
subq %r14, %rcx
cmpq 0x18(%rsp), %rcx
jle 0x2374c
movq %rdi, 0x28(%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 %r11, %r11
setne %sil
orb %dl, %sil
jne 0x23785
leaq 0x76ff9(%rip), %rsi     # 0x9a73c
leaq 0x7703b(%rip), %rdx     # 0x9a785
jmp 0x2375a
leaq 0x76fe9(%rip), %rsi     # 0x9a73c
leaq 0x76fff(%rip), %rdx     # 0x9a759
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
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 0x237a2
leaq 0x76fa3(%rip), %rsi     # 0x9a73c
leaq 0x77004(%rip), %rdx     # 0x9a7a4
jmp 0x2375a
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, 0x10(%rsp)
subq %rsi, %rcx
cmovbq %rdx, %rcx
movl $0x0, %edx
sbbl %edx, %edx
leaq (,%r11,4), %r10
cmpq %r10, %rcx
movq %r10, %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, %r9
cmovbq %rax, %r9
subq %r9, %rcx
cmpq %r13, %rcx
cmovaeq %r13, %rax
sbbl %r8d, %edx
subq %rax, %rcx
cmpq %rsi, %rcx
sbbl $0x0, %edx
jne 0x238e7
movq %r10, 0x30(%rsp)
movq 0x10(%rsp), %rax
leaq (%r10,%rax), %rcx
addq %r13, %rcx
addq %rdi, %rcx
addq %rcx, %rsi
movq 0x28(%rsp), %rdi
movl 0x44(%rdi), %eax
cmpq %rax, %rsi
movq %rcx, 0x8(%rsp)
jbe 0x23872
movq %rdi, %r13
callq 0x23476
movq %r13, %rdi
movq 0x8(%rsp), %rcx
testl %eax, %eax
js 0x23771
movl 0x98(%rsp), %r8d
movb 0x80(%rsp), %r9b
movl %ecx, 0x40(%rdi)
movq 0x18(%rsp), %rax
movq %rax, (%rdi)
movl 0x24(%rsp), %ecx
movl %ecx, 0x8(%rdi)
leaq -0x1(%rax,%r14), %rsi
movl $0x1249, %r10d          # imm = 0x1249
movl $0xe, %eax
movl $0x6, %r14d
movl $0xc, %edx
movq 0x18(%rsp), %r13
movl %eax, %ecx
sarq %cl, %r13
movq %rsi, %r11
sarq %cl, %r11
cmpq %r11, %r13
je 0x238fa
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 0x238af
xorl %r10d, %r10d
jmp 0x238fd
leaq 0x76e4e(%rip), %rsi     # 0x9a73c
leaq 0x76ee0(%rip), %rdx     # 0x9a7d5
jmp 0x2375a
addl %r13d, %r10d
movq 0x28(%rsp), %rcx
movw %r10w, 0xc(%rcx)
movb %r9b, 0xe(%rcx)
movq 0x38(%rsp), %rax
xorb $0x3, %al
movb %al, 0xf(%rcx)
movw %bx, 0x10(%rcx)
movq 0x10(%rsp), %rax
movw %ax, 0x12(%rcx)
movq 0x88(%rsp), %rax
movl %eax, 0x14(%rcx)
movq 0xb0(%rsp), %r13
movl %r13d, 0x18(%rcx)
movl %r8d, 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
callq 0x4250
addq %rbx, %r12
xorl %r14d, %r14d
movq %r12, %rdi
xorl %esi, %esi
movq 0x40(%rsp), %rdx
callq 0x4260
addq 0x10(%rsp), %rbx
cmpq $0x0, 0x88(%rsp)
movq 0x30(%rsp), %r12
je 0x2399f
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x43c0
addq %r12, %rbx
movq 0xb8(%rsp), %r8
cmpq $0x11, %r13
jb 0x239f8
leaq 0x1(%r8), %rax
movl $0x10, %edx
leaq 0x74ef0(%rip), %rcx     # 0x988b0
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 0x239c5
addq $0x8, %rbx
leaq 0x10(%r14), %rdx
addq $0x10, %rax
cmpq %r13, %rdx
jb 0x239c0
movl %r14d, %eax
orl $0x1, %eax
cmpq %r13, %rax
jae 0x23a36
movl %r14d, %r14d
leaq 0x74ea3(%rip), %rax     # 0x988b0
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 0x23a0d
movl %r14d, %ecx
movq %r13, %rax
subq %rcx, %rax
jbe 0x23a69
addq %rcx, %r8
xorl %ecx, %ecx
leaq 0x74e63(%rip), %rdx     # 0x988b0
movzbl (%r8,%rcx), %esi
movb (%rdx,%rsi), %sil
shlb $0x4, %sil
movb %sil, (%rbx,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x23a4d
addq %rcx, %rbx
movq 0xc0(%rsp), %rsi
movq %rbx, %rdi
testq %rsi, %rsi
je 0x23a83
movq %r13, %rdx
callq 0x43c0
jmp 0x23a90
movl $0xff, %esi
movq %r13, %rdx
callq 0x4260
movq 0x8(%rsp), %rax
jmp 0x23776