// File: /workspace/github_c_makefile_star2_O3/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 0x220ef(%rip), %rax     # 0x2ca48
testq %rax, %rax
je 0xa963
movq (%rax), %rdi
jmp 0xa986
movl $0x10, %edi
callq 0x3420
movq %rax, 0x220d4(%rip)     # 0x2ca48
testq %rax, %rax
je 0xaace
movq $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 0x2209d(%rip), %rcx     # 0x2ca48
movq %rax, (%rcx)
movq 0x22093(%rip), %rax     # 0x2ca48
cmpq $0x0, (%rax)
je 0xaac9
xorl %r12d, %r12d
leaq 0x13257(%rip), %r13     # 0x1dc20
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movsbl (%rax,%r13), %ebp
xorl %ebx, %ebx
movsbl (%rbx,%r13), %r14d
movl %ebp, %edi
movl %r14d, %esi
callq 0x5ddc
movl %eax, %r15d
movl %r14d, %edi
movl %ebp, %esi
callq 0x5ddc
orl %r15d, %eax
movq 0x2204b(%rip), %rax     # 0x2ca48
je 0xaa04
movb 0x8(%rax), %cl
jmp 0xaa0b
movb 0x9(%rax), %dl
xorl %ecx, %ecx
subb %dl, %cl
movq (%rax), %rax
addq %r12, %rax
movb %cl, (%rbx,%rax)
incq %rbx
cmpq $0x10, %rbx
jne 0xa9d7
movq 0x10(%rsp), %rax
incq %rax
addq $0x10, %r12
cmpq $0x10, %rax
jne 0xa9cb
movq 0x22012(%rip), %rax     # 0x2ca48
movq (%rax), %rax
movb $0x0, 0xff(%rax)
movq 0x8(%rsp), %rbp
movl (%rbp), %ebx
testl %ebx, %ebx
jle 0xaab2
xorl %r15d, %r15d
leaq 0x131da(%rip), %r12     # 0x1dc30
movq 0xa0(%rbp), %rax
movq (%rax,%r15,8), %r13
cmpq $0x0, 0x18(%r13)
jne 0xaaa7
movslq 0x8(%r13), %r14
movq %r14, %rdi
callq 0x3420
movq %rax, 0x18(%r13)
testq %rax, %rax
je 0xaac1
testl %r14d, %r14d
jle 0xaaa7
xorl %eax, %eax
movq 0x10(%r13), %rcx
movq 0x18(%r13), %rdx
movsbq (%rcx,%rax), %rcx
movb (%rcx,%r12), %cl
movb %cl, (%rdx,%rax)
incq %rax
movslq 0x8(%r13), %rcx
cmpq %rcx, %rax
jl 0xaa84
movl (%rbp), %ebx
incq %r15
movslq %ebx, %rax
cmpq %rax, %r15
jl 0xaa56
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x3805
callq 0x3826
callq 0x3848