// File: /workspace/github_c_makefile_star2_O3/sc932[P]ALE/src/ssw.c
// Function: mark_mismatch
int32_t mark_mismatch (int32_t ref_begin1,
					   int32_t read_begin1,
					   int32_t read_end1,
					   const int8_t* ref,
					   const int8_t* read,
					   int32_t readLen,
					   uint32_t** cigar,
					   int32_t* cigarLen) {

	int32_t mismatch_length = 0, p = 0, i, length, j, s = *cigarLen + 2;
	uint32_t *new_cigar = (uint32_t*)malloc(s*sizeof(uint32_t)), length_m = 0,  length_x = 0;
	char op;

	ref += ref_begin1;
	read += read_begin1;
	if (read_begin1 > 0) new_cigar[p ++] = to_cigar_int(read_begin1, 'S');
	for (i = 0; i < (*cigarLen); ++i) {
		op = cigar_int_to_op((*cigar)[i]);
		length = cigar_int_to_len((*cigar)[i]);
		if (op == 'M') {
			for (j = 0; j < length; ++j) {
				if (*ref != *read) {
					++ mismatch_length;
					// the previous is match; however the current one is mismatche
					new_cigar = store_previous_m (2, &length_m, &length_x, &p, &s, new_cigar);			
					++ length_x;
				} else {
					// the previous is mismatch; however the current one is matche
					new_cigar = store_previous_m (1, &length_m, &length_x, &p, &s, new_cigar);			
					++ length_m;
				}
				++ ref;
				++ read;
			}
		}else if (op == 'I') {
			read += length;
			mismatch_length += length;
			new_cigar = store_previous_m (0, &length_m, &length_x, &p, &s, new_cigar);			
			new_cigar = add_cigar (new_cigar, &p, &s, length, 'I'); 
		}else if (op == 'D') {
			ref += length;
			mismatch_length += length;
			new_cigar = store_previous_m (0, &length_m, &length_x, &p, &s, new_cigar);			
			new_cigar = add_cigar (new_cigar, &p, &s, length, 'D'); 
		}
	}
	new_cigar = store_previous_m (0, &length_m, &length_x, &p, &s, new_cigar);
	
	length = readLen - read_end1 - 1;
	if (length > 0) new_cigar = add_cigar(new_cigar, &p, &s, length, 'S');
	
	(*cigarLen) = p;	
	free(*cigar);
	(*cigar) = new_cigar;
	return mismatch_length;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, 0x10(%rsp)
movq %r8, %r12
movq %rcx, %r15
movl %edx, 0x14(%rsp)
movl %esi, %ebx
movl %edi, %r14d
movq 0x68(%rsp), %rax
xorl %ebp, %ebp
movl %ebp, (%rsp)
movslq (%rax), %r13
leaq 0x2(%r13), %rax
movl %eax, 0x4(%rsp)
leaq 0x8(,%r13,4), %rdi
callq 0x3420
movl %ebp, 0xc(%rsp)
movl %ebp, 0x8(%rsp)
testl %ebx, %ebx
jle 0xe355
movl %ebx, %ecx
shll $0x4, %ecx
orl $0x4, %ecx
movl $0x1, (%rsp)
movl %ecx, (%rax)
testl %r13d, %r13d
jle 0xe4e1
movslq %r14d, %rcx
movslq %ebx, %rdx
addq %rcx, %r15
addq %rdx, %r12
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x68(%rsp), %r14
movq 0x60(%rsp), %rcx
movq (%rcx), %rcx
movl (%rcx,%r13,4), %ecx
movl %ecx, %edx
andl $0xf, %edx
cmpl $0x9, %edx
jb 0xe43a
movl %ecx, %ebx
shrl $0x4, %ebx
cmpl $0x10, %ecx
jb 0xe4d0
leal -0x1(%rbx), %ecx
movq %rcx, 0x20(%rsp)
leaq 0x1(%r12), %rcx
movq %rcx, 0x18(%rsp)
movl %ebx, %ebx
xorl %r14d, %r14d
movb (%r15,%r14), %cl
cmpb (%r12,%r14), %cl
jne 0xe3e9
movl 0x8(%rsp), %ecx
testl %ecx, %ecx
je 0xe3e3
movq %rax, %rdi
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
movl $0x58, %r8d
callq 0xe202
movl $0x0, 0x8(%rsp)
incl 0xc(%rsp)
jmp 0xe415
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
je 0xe40f
movq %rax, %rdi
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
movl $0x3d, %r8d
callq 0xe202
movl $0x0, 0xc(%rsp)
incl %ebp
incl 0x8(%rsp)
incq %r14
cmpl %r14d, %ebx
jne 0xe3b3
movq 0x20(%rsp), %rcx
addq %rcx, %r15
incq %r15
movq 0x18(%rsp), %r12
addq %rcx, %r12
movq 0x68(%rsp), %r14
jmp 0xe4d0
leaq 0x161ab(%rip), %rsi     # 0x245ec
movzbl (%rdx,%rsi), %edx
movl %ecx, %ebx
shrl $0x4, %ebx
cmpl $0x44, %edx
je 0xe495
cmpl $0x49, %edx
je 0xe45f
cmpl $0x4d, %edx
je 0xe393
jmp 0xe4d0
addq %rbx, %r12
addl %ebx, %ebp
xorl %edi, %edi
leaq 0xc(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rsp, %rcx
leaq 0x4(%rsp), %r8
movq %rax, %r9
callq 0xe279
movq %rax, %rdi
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
movl %ebx, %ecx
movl $0x49, %r8d
jmp 0xe4cb
movl %ebx, %ecx
addq %rcx, %r15
addl %ebx, %ebp
xorl %edi, %edi
leaq 0xc(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rsp, %rcx
leaq 0x4(%rsp), %r8
movq %rax, %r9
callq 0xe279
movq %rax, %rdi
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
movl %ebx, %ecx
movl $0x44, %r8d
callq 0xe202
incq %r13
movslq (%r14), %rcx
cmpq %rcx, %r13
jl 0xe374
jmp 0xe4e6
movq 0x68(%rsp), %r14
leaq 0xc(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rsp, %rcx
leaq 0x4(%rsp), %r8
xorl %edi, %edi
movq %rax, %r9
callq 0xe279
movq %rax, %rbx
movl 0x14(%rsp), %ecx
notl %ecx
addl 0x10(%rsp), %ecx
testl %ecx, %ecx
jle 0xe52c
movq %rsp, %rsi
leaq 0x4(%rsp), %rdx
movq %rbx, %rdi
movl $0x53, %r8d
callq 0xe202
movq %rax, %rbx
movl (%rsp), %eax
movl %eax, (%r14)
movq 0x60(%rsp), %r14
movq (%r14), %rdi
callq 0x3070
movq %rbx, (%r14)
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop