// File: /workspace/github_cpp_makefile_star2_O1/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 $0x18, %rsp
movq %rdi, %r14
leaq 0x1dbd(%rip), %rsi      # 0x5061
callq 0x2190
testq %rax, %rax
je 0x34e9
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x2e45
movl 0x4ef3(%rip), %eax      # 0x81b8
xorl %r15d, %r15d
movl $0x0, %r13d
movl %eax, (%rsp)
cmpl $0x14, %eax
jl 0x346a
addl $-0x13, (%rsp)
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %edi, %edi
movslq %r13d, %rax
shlq $0x2, %rax
addq 0x4eac(%rip), %rax      # 0x81a0
movl $0x13, %edx
xorl %ecx, %ecx
movl (%rax,%rdx,4), %esi
leaq (%rsi,%rcx,4), %rcx
addq $-0x1, %rdx
jb 0x32fb
movq 0x4ec9(%rip), %rax      # 0x81d8
shll $0x2, %ecx
movl (%rax,%rcx), %r10d
testl %r10d, %r10d
js 0x3438
movl %edi, 0x4(%rsp)
movq %r15, 0x10(%rsp)
leal 0xf(%r13), %eax
movslq 0x4e89(%rip), %rcx      # 0x81bc
movslq 0x4e7e(%rip), %rdx      # 0x81b8
movq 0x4e67(%rip), %rsi      # 0x81a8
movq 0x4e58(%rip), %rdi      # 0x81a0
movq 0x4e81(%rip), %r8       # 0x81d0
movslq %eax, %r15
incq %r15
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leal 0xf(%r10), %r9d
movl $0xf, %r11d
movslq %r10d, %r10
cmpl %ecx, %r9d
jge 0x33a0
cmpl %edx, %eax
jge 0x33a0
leaq 0x10(%r10), %rbp
movq %r15, %r9
movl -0x4(%rsi,%rbp,4), %r12d
cmpl -0x4(%rdi,%r9,4), %r12d
jne 0x33a0
incl %r11d
cmpq %rcx, %rbp
jge 0x33a0
incq %rbp
leaq 0x1(%r9), %r12
cmpq %rdx, %r9
movq %r12, %r9
jl 0x337d
cmpl $0x14, %r11d
setae %r9b
cmpl %ebx, %r11d
setg %bpl
testb %bpl, %r9b
cmovnel %r11d, %ebx
cmovnel %r10d, %r14d
movl (%r8,%r10,4), %r10d
cmpl $-0x1, %r10d
jne 0x3360
testl %r14d, %r14d
js 0x342f
movq 0x10(%rsp), %r15
testl %r15d, %r15d
jle 0x33fe
movq 0x4e06(%rip), %rax      # 0x81e0
movl %r15d, %ecx
movb $0x0, (%rax,%rcx)
movq 0x4df8(%rip), %rdx      # 0x81e0
xorl %r15d, %r15d
movq 0x8(%rsp), %rdi
leaq 0x1c85(%rip), %rsi      # 0x507c
xorl %eax, %eax
callq 0x21e0
movl %r14d, %edx
subl 0x4(%rsp), %edx
addl %ebx, %r14d
leal -0x14(%rbx), %ecx
movq 0x8(%rsp), %rdi
leaq 0x1c69(%rip), %rsi      # 0x5080
xorl %eax, %eax
callq 0x21e0
addl %ebx, %r13d
movl %r14d, %edi
cmpl 0x4d8d(%rip), %r13d     # 0x81b8
jl 0x3438
jmp 0x345a
movq 0x10(%rsp), %r15
movl 0x4(%rsp), %edi
movq 0x4d61(%rip), %rax      # 0x81a0
movslq %r13d, %rcx
movb (%rax,%rcx,4), %al
addb $0x30, %al
movq 0x4d92(%rip), %rcx      # 0x81e0
movslq %r15d, %rdx
incl %r15d
movb %al, (%rcx,%rdx)
movl %edi, %r14d
incl %r13d
movl %r14d, %edi
cmpl (%rsp), %r13d
jl 0x32e6
cmpl 0x4d47(%rip), %r13d     # 0x81b8
jge 0x34a3
movslq %r13d, %rax
movslq %r15d, %r15
movq 0x4d20(%rip), %rcx      # 0x81a0
movb (%rcx,%rax,4), %cl
addb $0x30, %cl
movq 0x4d53(%rip), %rdx      # 0x81e0
movb %cl, (%rdx,%r15)
incq %r15
incq %rax
movslq 0x4d1a(%rip), %rcx      # 0x81b8
cmpq %rcx, %rax
jl 0x3479
movq 0x8(%rsp), %rbx
testl %r15d, %r15d
jle 0x34d3
movq 0x4d2c(%rip), %rax      # 0x81e0
movl %r15d, %ecx
movb $0x0, (%rax,%rcx)
movq 0x4d1e(%rip), %rdx      # 0x81e0
leaq 0x1bb3(%rip), %rsi      # 0x507c
movq %rbx, %rdi
xorl %eax, %eax
callq 0x21e0
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2160
leaq 0x1b73(%rip), %rdi      # 0x5063
movq %r14, %rsi
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2040