// File: /workspace/github_c_makefile_star2_O1/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)
cmpq $0x0, 0x223c2(%rip)     # 0x2ca48
jne 0xa6a9
movl $0x10, %edi
callq 0x3420
movq %rax, 0x223af(%rip)     # 0x2ca48
testq %rax, %rax
je 0xa808
movq $0x0, (%rax)
movq 0x22398(%rip), %rax     # 0x2ca48
movb %r13b, 0x8(%rax)
movb %r12b, 0x9(%rax)
movb %r15b, 0xa(%rax)
movb %r14b, 0xb(%rax)
movb %bpl, 0xc(%rax)
movq (%rax), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x3480
movq 0x22370(%rip), %rcx     # 0x2ca48
movq %rax, (%rcx)
movq 0x22366(%rip), %rax     # 0x2ca48
cmpq $0x0, (%rax)
je 0xa803
xorl %r12d, %r12d
leaq 0x134ca(%rip), %r13     # 0x1dbc0
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 0x5bf5
movl %eax, %r15d
movl %r14d, %edi
movl %ebp, %esi
callq 0x5bf5
orl %r15d, %eax
movq 0x2231e(%rip), %rax     # 0x2ca48
je 0xa731
movb 0x8(%rax), %al
jmp 0xa738
movb 0x9(%rax), %cl
xorl %eax, %eax
subb %cl, %al
movq 0x22309(%rip), %rcx     # 0x2ca48
movq (%rcx), %rcx
addq %r12, %rcx
movb %al, (%rbx,%rcx)
incq %rbx
cmpq $0x10, %rbx
jne 0xa704
movq 0x10(%rsp), %rax
incq %rax
addq $0x10, %r12
cmpq $0x10, %rax
jne 0xa6f8
movq 0x222de(%rip), %rax     # 0x2ca48
movq (%rax), %rax
movb $0x0, 0xff(%rax)
movq 0x8(%rsp), %r13
cmpl $0x0, (%r13)
jle 0xa7ec
xorl %ebx, %ebx
leaq 0x13447(%rip), %r15     # 0x1dbd0
movq 0xa0(%r13), %rax
movq (%rax,%rbx,8), %r12
cmpq $0x0, 0x18(%r12)
jne 0xa7e0
movslq 0x8(%r12), %r14
movq %r14, %rdi
callq 0x3420
movq %rax, 0x18(%r12)
testq %rax, %rax
je 0xa7fb
cmpl $0x0, 0x8(%r12)
jle 0xa7e0
xorl %eax, %eax
movq 0x10(%r12), %rcx
movq 0x18(%r12), %rdx
movsbq (%rcx,%rax), %rcx
movb (%rcx,%r15), %cl
movb %cl, (%rdx,%rax)
incq %rax
movslq 0x8(%r12), %rcx
cmpq %rcx, %rax
jl 0xa7bd
incq %rbx
movslq (%r13), %rax
cmpq %rax, %rbx
jl 0xa789
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x3867
callq 0x3888
callq 0x38aa