// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movw %cx, %ax
movq 0x58(%rbp), %rcx
movq 0x50(%rbp), %rcx
movq 0x48(%rbp), %rcx
movq 0x40(%rbp), %rcx
movq 0x38(%rbp), %rcx
movq 0x30(%rbp), %rcx
movl 0x28(%rbp), %ecx
movq 0x20(%rbp), %rcx
movq 0x18(%rbp), %rcx
movb 0x10(%rbp), %cl
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movw %ax, -0x22(%rbp)
movl %r8d, -0x28(%rbp)
movq %r9, -0x30(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x33236
movq $0x1, -0x18(%rbp)
leaq 0xbfa91(%rip), %rax     # 0xf2cc3
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rcx
andq $0x3, %rcx
movl $0x4, %eax
subq %rcx, %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movzwl -0x22(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x3327d
movq 0x18(%rbp), %rax
movl %eax, %edi
movq 0x20(%rbp), %rsi
leaq -0x40(%rbp), %rdx
leaq -0x48(%rbp), %rcx
callq 0x33790
cmpq $0x0, -0x40(%rbp)
jne 0x3328c
movq $0x1, -0x40(%rbp)
cmpq $0xfe, -0x18(%rbp)
jbe 0x332c7
movl $0x1, %edi
leaq 0xbe9c5(%rip), %rsi     # 0xf1c67
leaq 0xbe9c7(%rip), %rdx     # 0xf1c70
movb $0x0, %al
callq 0x1b030
callq 0x4050
movl $0x16, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3377d
movabsq $0x7fffffff7fffffff, %rax # imm = 0x7FFFFFFF7FFFFFFF
subq -0x40(%rbp), %rax
cmpq -0x30(%rbp), %rax
jg 0x3330c
movl $0x1, %edi
leaq 0xbe980(%rip), %rsi     # 0xf1c67
leaq 0xbe996(%rip), %rdx     # 0xf1c84
movb $0x0, %al
callq 0x1b030
callq 0x4050
movl $0x16, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3377d
movzwl -0x22(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x33357
cmpq $0x0, 0x40(%rbp)
jbe 0x33357
cmpq $0x0, 0x18(%rbp)
jne 0x33357
movl $0x1, %edi
leaq 0xbe935(%rip), %rsi     # 0xf1c67
leaq 0xbe977(%rip), %rdx     # 0xf1cb0
movb $0x0, %al
callq 0x1b030
callq 0x4050
movl $0x16, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3377d
movzwl -0x22(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x333a5
cmpq $0x0, 0x40(%rbp)
jbe 0x333a5
movq 0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x333a5
movl $0x1, %edi
leaq 0xbe8e7(%rip), %rsi     # 0xf1c67
leaq 0xbe948(%rip), %rdx     # 0xf1ccf
movb $0x0, %al
callq 0x1b030
callq 0x4050
movl $0x16, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3377d
movq $0x7fffffff, -0x50(%rbp) # imm = 0x7FFFFFFF
movq -0x18(%rbp), %rdi
addq -0x38(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x33840
movl %eax, -0x54(%rbp)
movq 0x18(%rbp), %rdi
shlq $0x2, %rdi
leaq -0x50(%rbp), %rsi
callq 0x33840
addl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
movq 0x40(%rbp), %rdi
addq $0x1, %rdi
shrq %rdi
leaq -0x50(%rbp), %rsi
callq 0x33840
addl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
movq 0x40(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x33840
addl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
movq 0x58(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x33840
addl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x3344f
movl $0x1, %edi
leaq 0xbe83d(%rip), %rsi     # 0xf1c67
leaq 0xbe8cf(%rip), %rdx     # 0xf1d00
movb $0x0, %al
callq 0x1b030
callq 0x4050
movl $0x16, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3377d
movq -0x18(%rbp), %rax
addq -0x38(%rbp), %rax
movq 0x18(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq 0x40(%rbp), %rcx
addq $0x1, %rcx
shrq %rcx
addq %rcx, %rax
addq 0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq 0x58(%rbp), %rsi
callq 0x33110
cmpl $0x0, %eax
jge 0x3349a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3377d
movq -0x60(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x40(%rax)
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movl -0x28(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x30(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq -0x40(%rbp), %rsi
movl $0xe, %edx
movl $0x5, %ecx
callq 0x33880
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0xc(%rax)
movb 0x10(%rbp), %cl
movq -0x10(%rbp), %rax
movb %cl, 0xe(%rax)
movq -0x38(%rbp), %rax
subq $0x1, %rax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0xf(%rax)
movw -0x22(%rbp), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x10(%rax)
movq -0x18(%rbp), %rax
addq -0x38(%rbp), %rax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x12(%rax)
movq 0x18(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
movq 0x40(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x18(%rax)
movl 0x28(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq 0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq 0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x4240
movl $0x0, -0x6c(%rbp)
movslq -0x6c(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x335a4
movq -0x68(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq -0x6c(%rbp), %rdx
addq %rdx, %rcx
movb $0x0, (%rax,%rcx)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x3357c
movq -0x18(%rbp), %rax
addq -0x38(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, 0x18(%rbp)
jbe 0x335d0
movq -0x68(%rbp), %rdi
movq 0x20(%rbp), %rsi
movq 0x18(%rbp), %rdx
shlq $0x2, %rdx
callq 0x43b0
movq 0x18(%rbp), %rax
shlq $0x2, %rax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq 0x48(%rbp), %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
addl $0x10, %eax
cltq
cmpq 0x40(%rbp), %rax
jae 0x3369a
movq -0x78(%rbp), %rax
movslq -0x6c(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movl $0x0, -0x7c(%rbp)
cmpl $0x8, -0x7c(%rbp)
jge 0x33680
movq -0x88(%rbp), %rax
movl -0x7c(%rbp), %ecx
shll %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0xbc4f4(%rip), %rax     # 0xefb30
movzbl (%rax,%rcx), %eax
shll $0x4, %eax
movq -0x88(%rbp), %rcx
movl -0x7c(%rbp), %edx
shll %edx
addl $0x1, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movl %ecx, %edx
leaq 0xbc4ce(%rip), %rcx     # 0xefb30
movzbl (%rcx,%rdx), %ecx
orl %ecx, %eax
movb %al, %dl
movq -0x68(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x3361a
movq -0x68(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x68(%rbp)
movl -0x6c(%rbp), %eax
addl $0x10, %eax
movl %eax, -0x6c(%rbp)
jmp 0x335ef
jmp 0x3369c
movl -0x6c(%rbp), %eax
addl $0x1, %eax
cltq
cmpq 0x40(%rbp), %rax
jae 0x33704
movq -0x78(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0xbc471(%rip), %rax     # 0xefb30
movzbl (%rax,%rcx), %eax
shll $0x4, %eax
movq -0x78(%rbp), %rcx
movl -0x6c(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movl %ecx, %edx
leaq 0xbc450(%rip), %rcx     # 0xefb30
movzbl (%rcx,%rdx), %ecx
orl %ecx, %eax
movb %al, %cl
movq -0x68(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x68(%rbp)
movb %cl, (%rax)
movl -0x6c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x6c(%rbp)
jmp 0x3369c
jmp 0x33706
movslq -0x6c(%rbp), %rax
cmpq 0x40(%rbp), %rax
jae 0x3374a
movq 0x48(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0xbc40b(%rip), %rax     # 0xefb30
movzbl (%rax,%rcx), %eax
shll $0x4, %eax
movb %al, %cl
movq -0x68(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x68(%rbp)
movb %cl, (%rax)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x33706
cmpq $0x0, 0x50(%rbp)
je 0x33764
movq -0x68(%rbp), %rdi
movq 0x50(%rbp), %rsi
movq 0x40(%rbp), %rdx
callq 0x43b0
jmp 0x33776
movq -0x68(%rbp), %rdi
movq 0x40(%rbp), %rdx
movl $0xff, %esi
callq 0x4250
movq -0x60(%rbp), %rax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)