// File: /workspace/github_c_makefile_star2_O2/sc932[P]ALE/src/ALElike.c
// Function: initRefNumForRealign
void initRefNumForRealign(assemblyT *theAssembly, int matchScore, int mismatchPenalty, int gapOpenPenalty, int gapExtendPenalty, int minSoftClip) {
	int i,j;

	if (realignParameters == NULL) {
		realignParameters = (realign_parameters_t*) malloc(sizeof(realign_parameters_t));
		if(realignParameters == NULL) {
			fprintf(stderr, "Could not allocate memory for mismatch realignment structure");
			abort();
		}
		realignParameters->scoringMatrix = NULL;
	}
	realignParameters->matchScore = matchScore;
	realignParameters->mismatchPenalty = mismatchPenalty;
	realignParameters->gapOpenPenalty = gapOpenPenalty;
	realignParameters->gapExtendPenalty = gapExtendPenalty;
	realignParameters->minSoftClip = minSoftClip;

	// Then build the match/mismatch matrix
	realignParameters->scoringMatrix = (int8_t*) realloc(realignParameters->scoringMatrix, NUM_NT_CHARACTERS * NUM_NT_CHARACTERS * sizeof(int8_t));
	if (realignParameters->scoringMatrix == NULL) {
		fprintf(stderr, "Could not allocate memory for mismatch realignment matrix");
		abort();
	}
	for(i = 0; i < NUM_NT_CHARACTERS; i++) {
		for(j = 0; j < NUM_NT_CHARACTERS; j++) {
			char ref = baseOrder[i], query = baseOrder[j];
			realignParameters->scoringMatrix[i*NUM_NT_CHARACTERS+j] = isAmbiguousMatch(ref, query)|isAmbiguousMatch(query,ref) ? realignParameters->matchScore : -realignParameters->mismatchPenalty;
		}
	}
	// set special case of N,N to 0 score
	realignParameters->scoringMatrix[ (NUM_NT_CHARACTERS-1) * NUM_NT_CHARACTERS + (NUM_NT_CHARACTERS-1) ] = 0;

	// Then prep the reference as a number string
	for(i = 0; i < theAssembly->numContigs; i++) {
		contig_t *contig = theAssembly->contigs[i];
		if (contig->seqNum != NULL)
			continue;
		contig->seqNum = (int8_t*) realloc(contig->seqNum, sizeof(*contig->seqNum) * contig->seqLen);
		if (contig->seqNum == NULL) {
			fprintf(stderr, "Could not allocate memory for realignment: %ld bytes\n", sizeof(*contig->seqNum) * contig->seqLen);
			abort();
		}
		for(j = 0; j < contig->seqLen; j++) {
			contig->seqNum[j] = seqNumTransformTable[ contig->seq[j] ];
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r9d, %ebp
movl %r8d, %r14d
movl %ecx, %r15d
movl %edx, %r12d
movl %esi, %r13d
movq %rdi, 0x8(%rsp)
movq 0x2122b(%rip), %rax     # 0x29a48
testq %rax, %rax
je 0x8827
movq (%rax), %rdi
jmp 0x8845
pushq $0x10
popq %rdi
callq 0x3420
movq %rax, 0x21212(%rip)     # 0x29a48
testq %rax, %rax
je 0x89ba
andq $0x0, (%rax)
xorl %edi, %edi
movb %r13b, 0x8(%rax)
movb %r12b, 0x9(%rax)
movb %r15b, 0xa(%rax)
movb %r14b, 0xb(%rax)
movb %bpl, 0xc(%rax)
movl $0x100, %esi            # imm = 0x100
callq 0x3480
movq 0x211de(%rip), %rcx     # 0x29a48
movq %rax, (%rcx)
movq 0x211d4(%rip), %rax     # 0x29a48
cmpq $0x0, (%rax)
je 0x89a5
xorl %r12d, %r12d
leaq 0x11368(%rip), %r13     # 0x19bf0
xorl %eax, %eax
cmpq $0x10, %rax
je 0x88f2
movq %rax, 0x10(%rsp)
movsbl (%rax,%r13), %ebp
xorl %ebx, %ebx
cmpq $0x10, %rbx
je 0x88e4
movsbl (%rbx,%r13), %r14d
movl %ebp, %edi
movl %r14d, %esi
callq 0x4494
movl %eax, %r15d
movl %r14d, %edi
movl %ebp, %esi
callq 0x4494
orl %r15d, %eax
movq 0x21180(%rip), %rax     # 0x29a48
je 0x88cf
movb 0x8(%rax), %cl
jmp 0x88d6
movb 0x9(%rax), %dl
xorl %ecx, %ecx
subb %dl, %cl
movq (%rax), %rax
addq %r12, %rax
movb %cl, (%rbx,%rax)
incq %rbx
jmp 0x889c
movq 0x10(%rsp), %rax
incq %rax
addq $0x10, %r12
jmp 0x888a
movq 0x2114f(%rip), %rax     # 0x29a48
movq (%rax), %rax
movb $0x0, 0xff(%rax)
xorl %ebx, %ebx
leaq 0x112f4(%rip), %r15     # 0x19c00
movq 0x8(%rsp), %r13
movslq (%r13), %rax
cmpq %rax, %rbx
jge 0x8976
movq 0xa0(%r13), %rax
movq (%rax,%rbx,8), %r12
cmpq $0x0, 0x18(%r12)
je 0x8932
incq %rbx
jmp 0x8911
movl 0x8(%r12), %ebp
movslq %ebp, %r14
movq %r14, %rdi
callq 0x3420
movq %rax, 0x18(%r12)
testq %rax, %rax
je 0x8985
xorl %eax, %eax
movslq %ebp, %rcx
cmpq %rcx, %rax
jge 0x892d
movq 0x10(%r12), %rcx
movq 0x18(%r12), %rdx
movsbq (%rcx,%rax), %rcx
movb (%rcx,%r15), %cl
movb %cl, (%rdx,%rax)
incq %rax
movl 0x8(%r12), %ebp
jmp 0x894e
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2066c(%rip), %rax     # 0x28ff8
movq (%rax), %rdi
leaq 0x16728(%rip), %rsi     # 0x1f0be
movq %r14, %rdx
xorl %eax, %eax
callq 0x3350
callq 0x30a0
movq 0x2064c(%rip), %rax     # 0x28ff8
movq (%rax), %rcx
leaq 0x166ce(%rip), %rdi     # 0x1f084
pushq $0x39
jmp 0x89cd
movq 0x20637(%rip), %rax     # 0x28ff8
movq (%rax), %rcx
leaq 0x1667c(%rip), %rdi     # 0x1f047
pushq $0x3c
popq %rsi
pushq $0x1
popq %rdx
callq 0x35e0
callq 0x30a0