// File: /workspace/github_c_makefile_star2_O3/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
movl $0x4, %eax
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 0x39d7e
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 0x3a0a9
leaq -0x30(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
movl -0x40(%rbp), %edx
callq 0x25472
movl 0x10(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl 0x18(%rbp), %eax
movl %eax, -0x48(%rbp)
cmpb $0x2d, 0x1(%rbx)
jne 0x3a0cb
leaq -0x2c(%rbp), %rdi
leaq -0x48(%rbp), %rsi
movl -0x44(%rbp), %edx
callq 0x25472
cmpq $0x0, 0x28(%rbp)
je 0x3a225
movq %r15, -0x60(%rbp)
cmpb $0x2d, (%rbx)
jne 0x3a0f3
cmpb $0x2d, 0x1(%rbx)
jne 0x3a0f3
leaq 0x5a106(%rip), %rdi     # 0x941f2
xorl %eax, %eax
callq 0x26e94
movq 0x28(%rbp), %r13
movq %r13, %rdi
callq 0x62d0
movq %rax, %r14
movq %rsp, -0x58(%rbp)
leaq 0x1(%rax), %rsi
movq %rsp, %rbx
addq $0x10, %r14
andq $-0x10, %r14
movq %rbx, %r15
subq %r14, %r15
movq %r15, %rsp
negq %r14
movq %r15, %rdi
movq %r13, %rdx
callq 0x21600
movb (%rbx,%r14), %bl
testb %bl, %bl
je 0x3a274
movl $0x0, -0x34(%rbp)
movb 0x1(%r15), %r13b
movq %r15, %r14
incq %r14
movq %r14, %r15
testb %r13b, %r13b
je 0x3a185
callq 0x6790
movq (%rax), %rax
movq %r14, %r15
movsbq %r13b, %rcx
movzwl (%rax,%rcx,2), %ecx
btl $0xb, %ecx
jb 0x3a179
cmpb $0x20, %r13b
je 0x3a179
movzbl %r13b, %ecx
cmpl $0x2b, %ecx
jne 0x3a185
movb 0x1(%r15), %r13b
incq %r15
testb %r13b, %r13b
jne 0x3a15c
movq %r14, %rdi
callq 0x6bd0
movl %eax, %r14d
cmpb $0x44, %bl
je 0x3a1f7
movzbl %bl, %eax
cmpl $0x49, %eax
je 0x3a1fd
cmpl $0x4d, %eax
jne 0x3a203
movl 0x5c(%r12), %eax
cmpl -0x50(%rbp), %eax
jne 0x3a1bd
movq %r12, %rdi
leaq -0x50(%rbp), %rsi
callq 0x39f0e
movl 0x5c(%r12), %eax
addl %r14d, -0x34(%rbp)
movq 0x60(%r12), %rcx
movl %eax, %eax
movl %r14d, (%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 %r14d, %eax
movl %eax, -0x2c(%rbp)
addl %r14d, -0x30(%rbp)
jmp 0x3a218
addl %r14d, -0x2c(%rbp)
jmp 0x3a218
movsbl %bl, %esi
leaq 0x5a032(%rip), %rdi     # 0x9423f
movq 0x28(%rbp), %rdx
xorl %eax, %eax
callq 0x26e94
movb (%r15), %bl
testb %bl, %bl
jne 0x3a13f
jmp 0x3a27b
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)
jmp 0x3a29c
movl $0x0, -0x34(%rbp)
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
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 0x3a2c8
leaq 0x3c(%r12), %rdi
leaq 0x40(%r12), %rsi
movl -0x40(%rbp), %edx
callq 0x25472
movq 0x70(%r12), %rax
movl (%rax), %ecx
movl %ecx, 0x54(%r12)
movl 0x5c(%r12), %ecx
decl %ecx
movq 0x60(%r12), %rdx
movslq %ecx, %rcx
movl (%rdx,%rcx,4), %edx
addl (%rax,%rcx,4), %edx
movl %edx, 0x58(%r12)
cmpb $0x2d, 0x1(%rbx)
jne 0x3a308
leaq 0x54(%r12), %rdi
movq %r12, %rsi
addq $0x58, %rsi
movl -0x44(%rbp), %edx
callq 0x25472
movl -0x30(%rbp), %eax
cmpl -0x4c(%rbp), %eax
je 0x3a328
leaq 0x59f47(%rip), %rdi     # 0x9425e
movq -0x68(%rbp), %rsi
movl -0x38(%rbp), %edx
movl -0x3c(%rbp), %ecx
xorl %eax, %eax
callq 0x26e94
movl -0x2c(%rbp), %eax
cmpl -0x48(%rbp), %eax
je 0x3a347
leaq 0x59f68(%rip), %rdi     # 0x9429f
movq %r15, %rsi
movl 0x10(%rbp), %edx
movl 0x18(%rbp), %ecx
xorl %eax, %eax
callq 0x26e94
movl %r14d, 0x8(%r12)
movq %r12, %rdi
callq 0x39fae
movq %r12, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq