// File: /workspace/github_c_makefile_star2_O1/icebert[P]pblat/lib/psl.c
// Function: pslFromGff3Cigar
struct psl* pslFromGff3Cigar(char *qName, int qSize, int qStart, int qEnd,
                             char *tName, int tSize, int tStart, int tEnd,
                             char* strand, char *cigar)
/* create a PSL from a GFF3-style cigar formatted alignment */
{
// this is currently tested by the gff3ToPsl
int blocksAlloced = 4;
struct psl *psl = pslNew(qName, qSize, qStart, qEnd, tName, tSize, tStart, tEnd, strand, blocksAlloced, 0);

char op;
int size;
int totalSize = 0;

int qNext = qStart, qBlkEnd = qEnd;
if (strand[0] == '-')
    reverseIntRange(&qNext, &qBlkEnd, qSize);
int tNext = tStart, tBlkEnd = tEnd;
if (strand[1] == '-')
    reverseIntRange(&tNext, &tBlkEnd, tSize);

if (cigar == NULL)
    {
    // no cigar means one block
    size = qEnd - qStart;
    totalSize += size;
    psl->blockSizes[psl->blockCount] = size;
    psl->qStarts[psl->blockCount] = qNext;
    psl->tStarts[psl->blockCount] = tNext;
    psl->blockCount++;
    tNext += size;
    qNext += size;
    }
else
    {
    if (strand[0] == '-' && strand[1] == '-')
        errAbort("GFF3 spec is vague about Gap when both strands are '-'; not implemented yet.");
    char cigarSpec[strlen(cigar)+1];  // copy since parsing is destructive
    safecpy(cigarSpec, sizeof cigarSpec, cigar);
    char *cigarNext = cigarSpec;
    while(getNextCigarOp(cigarSpec, FALSE, &cigarNext, &op, &size))
	{
	switch (op)
	    {
	    case 'M': // match or mismatch (gapless aligned block)
		if (psl->blockCount == blocksAlloced)
		    pslGrow(psl, &blocksAlloced);

		totalSize += size;
		psl->blockSizes[psl->blockCount] = size;
		psl->qStarts[psl->blockCount] = qNext;
		psl->tStarts[psl->blockCount] = tNext;
		psl->blockCount++;
		tNext += size;
		qNext += size;
		break;
	    case 'I': // inserted in target
		tNext += size;
		break;
	    case 'D': // deleted from target
		qNext += size;
		break;
	    
	    default:
		errAbort("unrecognized CIGAR op %c in %s", op, cigar);
	    }
	}
    }

/* CIGARs starting/ending with indels require adjusting of query/target ranges,
 * as PSL starts/ends with matches */
psl->qStart = psl->qStarts[0];
psl->qEnd = pslQEnd(psl, psl->blockCount-1);
if (strand[0] == '-')
    reverseIntRange(&psl->qStart, &psl->qEnd, qSize);
psl->tStart = psl->tStarts[0];
psl->tEnd = pslTEnd(psl, psl->blockCount-1);
if (strand[1] == '-')
    reverseIntRange(&psl->tStart, &psl->tEnd, tSize);

/* sanity check */
if (qNext != qBlkEnd)
    errAbort("CIGAR query length does not match specified query range %s:%d-%d", qName, qStart, qEnd);
if (tNext != tBlkEnd)
    errAbort("CIGAR target length does not match specified target range %s:%d-%d", tName, tStart, tEnd);
psl->match = totalSize;
pslComputeInsertCounts(psl);
return psl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %ecx, %r13d
movl %edx, %ebx
movq 0x20(%rbp), %r14
movl 0x18(%rbp), %r10d
movl 0x10(%rbp), %r15d
movl $0x4, %eax
movl %eax, -0x4c(%rbp)
subq $0x8, %rsp
movq %rdi, -0x60(%rbp)
movl %esi, -0x3c(%rbp)
movq %r8, -0x68(%rbp)
movl %r9d, -0x40(%rbp)
pushq $0x0
pushq %rax
pushq %r14
pushq %r10
pushq %r15
callq 0x399ef
addq $0x30, %rsp
movq %rax, %r12
movl %ebx, -0x38(%rbp)
movl %ebx, -0x30(%rbp)
movq %r14, %rbx
movl %r13d, -0x48(%rbp)
cmpb $0x2d, (%r14)
jne 0x39cef
leaq -0x30(%rbp), %rdi
leaq -0x48(%rbp), %rsi
movl -0x3c(%rbp), %edx
callq 0x25145
movl %r15d, -0x2c(%rbp)
movl 0x18(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpb $0x2d, 0x1(%rbx)
jne 0x39d0f
leaq -0x2c(%rbp), %rdi
leaq -0x44(%rbp), %rsi
movl -0x40(%rbp), %edx
callq 0x25145
cmpq $0x0, 0x28(%rbp)
je 0x39e8c
movl %r13d, -0x50(%rbp)
cmpb $0x2d, (%rbx)
jne 0x39d37
cmpb $0x2d, 0x1(%rbx)
jne 0x39d37
leaq 0x5a3ef(%rip), %rdi     # 0x9411f
xorl %eax, %eax
callq 0x26b6f
movq 0x28(%rbp), %r14
movq %r14, %rdi
callq 0x62d0
movq %rsp, -0x58(%rbp)
leaq 0x1(%rax), %rsi
movq %rsp, %rbx
addq $0x10, %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movq %rbx, %rdi
movq %r14, %rdx
callq 0x21264
movl $0x0, -0x34(%rbp)
movl %r13d, %ecx
movb $0x1, %al
testq %rbx, %rbx
je 0x39dd5
movb (%rbx), %r13b
testb %r13b, %r13b
je 0x39dd5
movb 0x1(%rbx), %r14b
incq %rbx
movq %rbx, %r15
testb %r14b, %r14b
je 0x39dc3
callq 0x6790
movq (%rax), %rax
movq %rbx, %r15
movsbq %r14b, %rcx
movzwl (%rax,%rcx,2), %ecx
btl $0xb, %ecx
jb 0x39db7
cmpb $0x20, %r14b
je 0x39db7
movzbl %r14b, %ecx
cmpl $0x2b, %ecx
jne 0x39dc3
movb 0x1(%r15), %r14b
incq %r15
testb %r14b, %r14b
jne 0x39d9a
movq %rbx, %rdi
callq 0x6bd0
movl %eax, %r14d
xorl %eax, %eax
movq %r15, %rbx
jmp 0x39dd8
movl %ecx, %r13d
testb %al, %al
jne 0x39e76
cmpb $0x44, %r13b
je 0x39e6d
movzbl %r13b, %eax
cmpl $0x49, %eax
je 0x39e13
cmpl $0x4d, %eax
je 0x39e1c
movsbl %r13b, %esi
leaq 0x5a369(%rip), %rdi     # 0x9416c
movq 0x28(%rbp), %rdx
xorl %eax, %eax
callq 0x26b6f
jmp 0x39d6e
addl %r14d, -0x2c(%rbp)
jmp 0x39d6e
movl 0x5c(%r12), %eax
cmpl -0x4c(%rbp), %eax
jne 0x39e32
movq %r12, %rdi
leaq -0x4c(%rbp), %rsi
callq 0x39b7d
movq 0x60(%r12), %rax
movl 0x5c(%r12), %ecx
movl %r14d, (%rax,%rcx,4)
movl -0x30(%rbp), %eax
movq 0x68(%r12), %rcx
movl 0x5c(%r12), %edx
movl %eax, (%rcx,%rdx,4)
movl -0x2c(%rbp), %eax
movq 0x70(%r12), %rcx
movl 0x5c(%r12), %edx
movl %eax, (%rcx,%rdx,4)
incl 0x5c(%r12)
addl %r14d, -0x2c(%rbp)
addl %r14d, -0x34(%rbp)
addl %r14d, -0x30(%rbp)
jmp 0x39d6e
movq -0x58(%rbp), %rsp
movl 0x10(%rbp), %r15d
movl -0x50(%rbp), %r13d
movq 0x20(%rbp), %rbx
movl -0x34(%rbp), %r14d
jmp 0x39ece
movl %r13d, %r14d
subl -0x38(%rbp), %r14d
movq 0x60(%r12), %rax
movl 0x5c(%r12), %ecx
movl %r14d, (%rax,%rcx,4)
movl -0x30(%rbp), %eax
movq 0x68(%r12), %rcx
movl 0x5c(%r12), %edx
movl %eax, (%rcx,%rdx,4)
movl -0x2c(%rbp), %eax
movq 0x70(%r12), %rcx
movl 0x5c(%r12), %edx
movl %eax, (%rcx,%rdx,4)
incl 0x5c(%r12)
addl %r14d, -0x2c(%rbp)
addl %r14d, -0x30(%rbp)
movq 0x68(%r12), %rax
movl (%rax), %ecx
movl %ecx, 0x3c(%r12)
movl 0x5c(%r12), %ecx
decl %ecx
movslq %ecx, %rcx
movq 0x60(%r12), %rdx
movl (%rdx,%rcx,4), %edx
addl (%rax,%rcx,4), %edx
movl %edx, 0x40(%r12)
cmpb $0x2d, (%rbx)
jne 0x39f0b
leaq 0x3c(%r12), %rdi
leaq 0x40(%r12), %rsi
movl -0x3c(%rbp), %edx
callq 0x25145
movq 0x70(%r12), %rax
movl (%rax), %ecx
movl %ecx, 0x54(%r12)
movl 0x5c(%r12), %ecx
decl %ecx
movslq %ecx, %rcx
movq 0x60(%r12), %rdx
movl (%rdx,%rcx,4), %edx
addl (%rax,%rcx,4), %edx
movl %edx, 0x58(%r12)
cmpb $0x2d, 0x1(%rbx)
jne 0x39f4b
leaq 0x54(%r12), %rdi
movq %r12, %rsi
addq $0x58, %rsi
movl -0x40(%rbp), %edx
callq 0x25145
movl -0x30(%rbp), %eax
cmpl -0x48(%rbp), %eax
je 0x39f6b
leaq 0x5a231(%rip), %rdi     # 0x9418b
movq -0x60(%rbp), %rsi
movl -0x38(%rbp), %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x26b6f
movl -0x2c(%rbp), %eax
cmpl -0x44(%rbp), %eax
je 0x39f8b
leaq 0x5a252(%rip), %rdi     # 0x941cc
movq -0x68(%rbp), %rsi
movl %r15d, %edx
movl 0x18(%rbp), %ecx
xorl %eax, %eax
callq 0x26b6f
movl %r14d, 0x8(%r12)
movq %r12, %rdi
callq 0x39c1d
movq %r12, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq