// File: /workspace/github_cpp_makefile_star2_O3/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 0x1e21(%rip), %rsi      # 0x5061
callq 0x2190
testq %rax, %rax
je 0x3413
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x2dfd
movl 0x4f57(%rip), %ebp      # 0x81b8
xorl %r12d, %r12d
cmpl $0x14, %ebp
jl 0x3432
addl $-0x13, %ebp
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %r8d, %r8d
movl %ebp, 0x14(%rsp)
movq 0x4f1c(%rip), %rcx      # 0x81a0
movslq %r13d, %rax
leaq (%rcx,%rax,4), %rax
movl $0x13, %esi
xorl %edx, %edx
movl (%rax,%rsi,4), %edi
leaq (%rdi,%rdx,4), %rdx
addq $-0x1, %rsi
jb 0x3292
movq 0x4f32(%rip), %rsi      # 0x81d8
shll $0x2, %edx
movl (%rsi,%rdx), %r11d
testl %r11d, %r11d
js 0x3379
movl %r8d, 0x10(%rsp)
movq %r12, 0x20(%rsp)
movq %r13, 0x18(%rsp)
leal 0xf(%r13), %edx
movslq 0x4eec(%rip), %rsi      # 0x81bc
movslq 0x4ee1(%rip), %rdi      # 0x81b8
movq 0x4eca(%rip), %r8       # 0x81a8
movq 0x4eeb(%rip), %r9       # 0x81d0
movslq %edx, %r12
incq %r12
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leal 0xf(%r11), %r10d
movslq %r11d, %r11
movl $0xf, %ebp
cmpl %esi, %r10d
jge 0x3334
cmpl %edi, %edx
jge 0x3334
leaq 0x10(%r11), %r15
movq %r12, %r10
movl -0x4(%r8,%r15,4), %r13d
cmpl -0x4(%rcx,%r10,4), %r13d
jne 0x3334
incl %ebp
cmpq %rsi, %r15
jge 0x3334
incq %r15
leaq 0x1(%r10), %r13
cmpq %rdi, %r10
movq %r13, %r10
jl 0x3312
cmpl $0x14, %ebp
setae %r10b
cmpl %ebx, %ebp
setg %r15b
testb %r15b, %r10b
cmovnel %ebp, %ebx
cmovnel %r11d, %r14d
movl (%r9,%r11,4), %r11d
cmpl $-0x1, %r11d
jne 0x32f6
testl %r14d, %r14d
movq 0x20(%rsp), %r12
movl 0x14(%rsp), %ebp
movq 0x18(%rsp), %r13
movl 0x10(%rsp), %r8d
js 0x3379
testl %r12d, %r12d
jg 0x337e
movq 0x8(%rsp), %rdi
jmp 0x33b4
movl %r8d, %r14d
jmp 0x33e8
movq 0x4e5b(%rip), %rax      # 0x81e0
movl %r12d, %ecx
movb $0x0, (%rax,%rcx)
movq 0x4e4d(%rip), %rdx      # 0x81e0
xorl %r12d, %r12d
movq 0x8(%rsp), %r15
movq %r15, %rdi
leaq 0x1cd7(%rip), %rsi      # 0x507c
xorl %eax, %eax
callq 0x21e0
movl 0x10(%rsp), %r8d
movq %r15, %rdi
movl %r14d, %edx
subl %r8d, %edx
addl %ebx, %r14d
leal -0x14(%rbx), %ecx
leaq 0x1cb9(%rip), %rsi      # 0x5080
xorl %eax, %eax
callq 0x21e0
addl %ebx, %r13d
cmpl 0x4de0(%rip), %r13d     # 0x81b8
jge 0x33fc
movslq %r13d, %rax
shlq $0x2, %rax
addq 0x4db8(%rip), %rax      # 0x81a0
movb (%rax), %al
addb $0x30, %al
movq 0x4ded(%rip), %rcx      # 0x81e0
movslq %r12d, %rdx
incl %r12d
movb %al, (%rcx,%rdx)
incl %r13d
movl %r14d, %r8d
cmpl %ebp, %r13d
jl 0x327d
movl 0x4da7(%rip), %ebp      # 0x81b8
jmp 0x3435
leaq 0x1c49(%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
xorl %r13d, %r13d
cmpl %ebp, %r13d
movq 0x8(%rsp), %rdi
jge 0x346f
movslq %r13d, %rax
movslq %r12d, %r12
movq 0x4d54(%rip), %rcx      # 0x81a0
movb (%rcx,%rax,4), %cl
addb $0x30, %cl
movq 0x4d87(%rip), %rdx      # 0x81e0
movb %cl, (%rdx,%r12)
incq %r12
incq %rax
movslq 0x4d4e(%rip), %rcx      # 0x81b8
cmpq %rcx, %rax
jl 0x3445
testl %r12d, %r12d
jle 0x34a1
movq 0x4d65(%rip), %rax      # 0x81e0
movl %r12d, %ecx
movb $0x0, (%rax,%rcx)
movq 0x4d57(%rip), %rdx      # 0x81e0
leaq 0x1bec(%rip), %rsi      # 0x507c
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0x21e0
movq 0x8(%rsp), %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2160