// File: /workspace/github_cpp_makefile_star2_O2/yuansliu[P]HiRGC/hirgc.cpp
// Function: searchMatch(char*)
void searchMatch(char *refFile) { // greedy matching
	FILE *fp = fopen(refFile, "w");
	if (NULL == fp) {
		printf("ERROR! fail to open file %s\n", refFile);
		return;
	}

	saveOtherData(fp);

	int pre_dismatch_point = 0, pre_pos = 0;
	int step_len = tar_seq_len - sub_str_num + 1;
	int max_length, max_k;

	int dis_str_len = 0, i, id, k, ref_idx, tar_idx, length, cur_pos;;
	uint64_t tar_value;

	for (i = 0; i < step_len; i++) {
		tar_value = 0;
		for (k = sub_str_num - 1; k >= 0; k--) {
			tar_value <<= 2;
			tar_value += tar_seq_code[i+k];
		}

		id = point[tar_value&(uint64_t)(max_arr_num-1)];
		if (id > -1) {
			max_length = -1;
			max_k = -1;
			for (k = id; k != -1; k = loc[k]) {
					//[pos[k], pos[k]+sub_str_num-1] //at least max_arr_num_bit_shift
				ref_idx = k + max_arr_num_bit_shift;//loc[k].pos == k
				tar_idx = i + max_arr_num_bit_shift;
				length = max_arr_num_bit_shift;
				while (ref_idx < ref_seq_len && tar_idx < tar_seq_len && ref_seq_code[ref_idx++] == tar_seq_code[tar_idx++]) {//extend current match
					length++;
				}
				if (length >= sub_str_num && length > max_length) {
					max_length = length;
					max_k = k;
				}
			}
			if (max_k > -1) { //if a match is found
				//first print mismatch substring
				if (dis_str_len > 0) {
					dismatched_str[dis_str_len] = '\0';
					fprintf(fp, "%s\n", dismatched_str);
					dis_str_len = 0;
				}
				//then print match substring
				cur_pos = max_k - pre_pos;
				pre_pos = max_k + max_length;

				fprintf(fp, "%d %d\n", cur_pos, max_length - sub_str_num);

				i += max_length;
				pre_dismatch_point = i;
				if (i < tar_seq_len) {
					dismatched_str[dis_str_len++] = '0'+tar_seq_code[i];
				}
				continue;
			}
		}
		dismatched_str[dis_str_len++] = '0'+tar_seq_code[i];
	}

	for(; i < tar_seq_len; i++) {
		dismatched_str[dis_str_len++] = '0'+tar_seq_code[i];
	}
	if (dis_str_len > 0) {
		dismatched_str[dis_str_len] = '\0';
		fprintf(fp, "%s\n", dismatched_str);
	}
	fclose(fp);
	// gettimeofday(&end,NULL);
	// timer = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
	// printf("compress timer = %lf ms\n", timer/1000.0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r14
leaq 0x1ff6(%rip), %rsi      # 0x5061
callq 0x2180
testq %rax, %rax
je 0x3295
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x2cae
movl 0x512c(%rip), %eax      # 0x81b8
addl $-0x13, %eax
movl %eax, 0x18(%rsp)
pushq $0xf
popq %r14
xorl %r8d, %r8d
xorl %r12d, %r12d
xorl %r15d, %r15d
movslq %r15d, %rax
cmpl 0x18(%rsp), %r15d
jge 0x321e
movq 0x50eb(%rip), %rcx      # 0x81a0
leaq (%rcx,%rax,4), %rax
pushq $0x13
popq %rsi
xorl %edx, %edx
testq %rsi, %rsi
js 0x30cf
movl (%rax,%rsi,4), %edi
leaq (%rdi,%rdx,4), %rdx
decq %rsi
jmp 0x30be
movq 0x5102(%rip), %rsi      # 0x81d8
shll $0x2, %edx
movl (%rsi,%rdx), %edx
testl %edx, %edx
js 0x3190
movl %r8d, 0x14(%rsp)
movl %r12d, 0x1c(%rsp)
movq %r15, 0x20(%rsp)
leal 0xf(%r15), %r10d
movslq 0x50be(%rip), %rsi      # 0x81bc
movslq 0x50b3(%rip), %rdi      # 0x81b8
movq 0x509c(%rip), %r8       # 0x81a8
movq 0x50bd(%rip), %r9       # 0x81d0
movslq %r10d, %r12
pushq $-0x1
popq %r13
movl %r13d, %ebp
cmpl $-0x1, %edx
je 0x3170
leal 0xf(%rdx), %r10d
movslq %r10d, %rbx
movq %r12, %r10
movl %r14d, %r11d
cmpq %rsi, %rbx
jge 0x314e
cmpq %rdi, %r10
jge 0x314e
movl (%r8,%rbx,4), %r15d
cmpl (%rcx,%r10,4), %r15d
jne 0x314e
incq %r10
incq %rbx
incl %r11d
jmp 0x312f
cmpl $0x14, %r11d
setae %r10b
cmpl %ebp, %r11d
setg %bl
testb %bl, %r10b
cmovnel %r11d, %ebp
cmovnel %edx, %r13d
movslq %edx, %rdx
movl (%r9,%rdx,4), %edx
jmp 0x311d
testl %r13d, %r13d
movq 0x20(%rsp), %r15
movl 0x1c(%rsp), %r12d
movl 0x14(%rsp), %r8d
js 0x3190
testl %r12d, %r12d
jg 0x3195
movq 0x8(%rsp), %rdi
jmp 0x31cb
movl %r8d, %r13d
jmp 0x31ff
movq 0x5044(%rip), %rax      # 0x81e0
movl %r12d, %ecx
movb $0x0, (%rax,%rcx)
movq 0x5036(%rip), %rdx      # 0x81e0
xorl %r12d, %r12d
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x1ec0(%rip), %rsi      # 0x507c
xorl %eax, %eax
callq 0x21f0
movl 0x14(%rsp), %r8d
movq %rbx, %rdi
movl %r13d, %edx
subl %r8d, %edx
addl %ebp, %r13d
leal -0x14(%rbp), %ecx
leaq 0x1ea2(%rip), %rsi      # 0x5080
xorl %eax, %eax
callq 0x21f0
addl %ebp, %r15d
cmpl 0x4fc9(%rip), %r15d     # 0x81b8
jge 0x3213
movslq %r15d, %rax
shlq $0x2, %rax
addq 0x4fa1(%rip), %rax      # 0x81a0
movb (%rax), %al
addb $0x30, %al
movq 0x4fd6(%rip), %rcx      # 0x81e0
movslq %r12d, %rdx
incl %r12d
movb %al, (%rcx,%rdx)
incl %r15d
movl %r13d, %r8d
jmp 0x30a0
movslq %r12d, %rcx
movq 0x8(%rsp), %rdi
movslq 0x4f8b(%rip), %rdx      # 0x81b8
cmpq %rdx, %rax
jge 0x3251
movq 0x4f67(%rip), %rdx      # 0x81a0
movb (%rdx,%rax,4), %dl
addb $0x30, %dl
movq 0x4f9a(%rip), %rsi      # 0x81e0
movb %dl, (%rsi,%rcx)
incq %rcx
incq %rax
jmp 0x3226
testq %rcx, %rcx
jle 0x3282
movq 0x4f83(%rip), %rax      # 0x81e0
movl %ecx, %ecx
movb $0x0, (%rax,%rcx)
movq 0x4f76(%rip), %rdx      # 0x81e0
leaq 0x1e0b(%rip), %rsi      # 0x507c
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0x21f0
movq 0x8(%rsp), %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2160
leaq 0x1dc7(%rip), %rdi      # 0x5063
movq %r14, %rsi
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2040