// File: /workspace/github_c_makefile_star2_O2/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), %r11d
pushq $0x4
popq %rax
movl %eax, -0x50(%rbp)
subq $0x8, %rsp
movq %rdi, -0x68(%rbp)
movl %esi, -0x40(%rbp)
movq %r8, %r15
movl %r9d, -0x44(%rbp)
pushq $0x0
pushq %rax
pushq %r14
pushq %r10
pushq %r11
callq 0x34040
addq $0x30, %rsp
movq %rax, %r12
movl %ebx, -0x38(%rbp)
movl %ebx, -0x30(%rbp)
movq %r14, %rbx
movl %r13d, -0x3c(%rbp)
movl %r13d, -0x4c(%rbp)
cmpb $0x2d, (%r14)
jne 0x34362
leaq -0x30(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
movl -0x40(%rbp), %edx
callq 0x20d61
movl 0x10(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl 0x18(%rbp), %eax
movl %eax, -0x48(%rbp)
cmpb $0x2d, 0x1(%rbx)
jne 0x34384
leaq -0x2c(%rbp), %rdi
leaq -0x48(%rbp), %rsi
movl -0x44(%rbp), %edx
callq 0x20d61
cmpq $0x0, 0x28(%rbp)
je 0x344e4
movq %r15, -0x60(%rbp)
cmpb $0x2d, (%rbx)
jne 0x343ac
cmpb $0x2d, 0x1(%rbx)
jne 0x343ac
leaq 0x4ed4d(%rip), %rdi     # 0x830f2
xorl %eax, %eax
callq 0x223fb
movq 0x28(%rbp), %rbx
movq %rbx, %rdi
callq 0x62d0
movq %rsp, -0x58(%rbp)
leaq 0x1(%rax), %rsi
movq %rsp, %r15
addq $0x10, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq %r15, %rdi
movq %rbx, %rdx
callq 0x1d6d3
movl $0x0, -0x34(%rbp)
movq %r15, %rbx
movsbl (%r15), %r14d
testl %r14d, %r14d
je 0x344c1
leaq 0x1(%rbx), %r15
movsbq (%r15), %r13
testq %r13, %r13
je 0x34427
callq 0x6790
movq (%rax), %rax
movzwl (%rax,%r13,2), %eax
btl $0xb, %eax
jb 0x34422
cmpb $0x2b, %r13b
je 0x34422
movzbl %r13b, %eax
cmpl $0x20, %eax
jne 0x34427
incq %r15
jmp 0x343f7
incq %rbx
movq %rbx, %rdi
callq 0x6bd0
movl %eax, %ebx
cmpb $0x44, %r14b
je 0x344b9
movzbl %r14b, %eax
cmpl $0x49, %eax
je 0x3445f
cmpl $0x4d, %eax
je 0x34467
leaq 0x4ecf0(%rip), %rdi     # 0x8313f
movl %r14d, %esi
movq 0x28(%rbp), %rdx
xorl %eax, %eax
callq 0x223fb
jmp 0x343e3
addl %ebx, -0x2c(%rbp)
jmp 0x343e3
movl 0x5c(%r12), %eax
cmpl -0x50(%rbp), %eax
jne 0x34482
movq %r12, %rdi
leaq -0x50(%rbp), %rsi
callq 0x341ce
movl 0x5c(%r12), %eax
addl %ebx, -0x34(%rbp)
movq 0x60(%r12), %rcx
movl %eax, %eax
movl %ebx, (%rcx,%rax,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 %ebx, %eax
movl %eax, -0x2c(%rbp)
addl %ebx, -0x30(%rbp)
jmp 0x343e3
movq -0x58(%rbp), %rsp
movq 0x68(%r12), %rcx
movl 0x5c(%r12), %edx
movq 0x60(%r12), %rax
decl %edx
movq -0x60(%rbp), %r15
movq 0x20(%rbp), %rbx
movl -0x34(%rbp), %r14d
jmp 0x34531
movl -0x3c(%rbp), %r14d
subl -0x38(%rbp), %r14d
movq 0x60(%r12), %rax
movl 0x5c(%r12), %ecx
movl %r14d, (%rax,%rcx,4)
movl -0x30(%rbp), %edx
movq 0x68(%r12), %rcx
movl 0x5c(%r12), %esi
movl %edx, (%rcx,%rsi,4)
movl -0x2c(%rbp), %esi
movq 0x70(%r12), %rdx
movl 0x5c(%r12), %edi
movl %esi, (%rdx,%rdi,4)
movl 0x5c(%r12), %edx
leal 0x1(%rdx), %edi
movl %edi, 0x5c(%r12)
addl %r14d, %esi
movl %esi, -0x2c(%rbp)
addl %r14d, -0x30(%rbp)
movl (%rcx), %esi
movl %esi, 0x3c(%r12)
movslq %edx, %rdx
movl (%rax,%rdx,4), %eax
addl (%rcx,%rdx,4), %eax
movl %eax, 0x40(%r12)
cmpb $0x2d, (%rbx)
jne 0x3455d
leaq 0x3c(%r12), %rdi
leaq 0x40(%r12), %rsi
movl -0x40(%rbp), %edx
callq 0x20d61
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 0x3459d
leaq 0x54(%r12), %rdi
movq %r12, %rsi
addq $0x58, %rsi
movl -0x44(%rbp), %edx
callq 0x20d61
movl -0x30(%rbp), %eax
cmpl -0x4c(%rbp), %eax
je 0x345bd
leaq 0x4ebb2(%rip), %rdi     # 0x8315e
movq -0x68(%rbp), %rsi
movl -0x38(%rbp), %edx
movl -0x3c(%rbp), %ecx
xorl %eax, %eax
callq 0x223fb
movl -0x2c(%rbp), %eax
cmpl -0x48(%rbp), %eax
je 0x345dc
leaq 0x4ebd3(%rip), %rdi     # 0x8319f
movq %r15, %rsi
movl 0x10(%rbp), %edx
movl 0x18(%rbp), %ecx
xorl %eax, %eax
callq 0x223fb
movl %r14d, 0x8(%r12)
movq %r12, %rdi
callq 0x3426e
movq %r12, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq