// File: /workspace/github_c_makefile_star2_O1/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, 0x18(%rsp)
movq %r8, %r12
movq %rcx, 0x10(%rsp)
movl %edx, 0x1c(%rsp)
movl %esi, %ebx
movl %edi, %r14d
movq 0x68(%rsp), %rbp
xorl %r13d, %r13d
movl %r13d, (%rsp)
movslq (%rbp), %rax
leaq 0x2(%rax), %rcx
movl %ecx, 0xc(%rsp)
leaq 0x8(,%rax,4), %rdi
callq 0x3420
movl %r13d, 0x8(%rsp)
xorl %r15d, %r15d
movl %r13d, 0x4(%rsp)
testl %ebx, %ebx
jle 0xdf52
movl %ebx, %ecx
shll $0x4, %ecx
orl $0x4, %ecx
movl $0x1, (%rsp)
movl %ecx, (%rax)
cmpl $0x0, (%rbp)
jle 0xe0fb
movslq %r14d, %rcx
movslq %ebx, %rdx
addq %rcx, 0x10(%rsp)
addq %rdx, %r12
xorl %ebx, %ebx
xorl %r15d, %r15d
movq 0x60(%rsp), %rcx
movq (%rcx), %rcx
movl (%rcx,%rbx,4), %r14d
movl %r14d, %edx
andl $0xf, %edx
movb $0x4d, %cl
cmpl $0x8, %edx
ja 0xdf92
leaq 0x165cd(%rip), %rcx     # 0x2455c
movb (%rdx,%rcx), %cl
movl %r14d, %r13d
shrl $0x4, %r13d
cmpb $0x44, %cl
je 0xe0a4
movzbl %cl, %ecx
cmpl $0x49, %ecx
je 0xe065
cmpl $0x4d, %ecx
jne 0xe0eb
cmpl $0x10, %r14d
jb 0xe0eb
decl %r13d
leaq 0x1(%r12), %rcx
movq %rcx, 0x20(%rsp)
shrl $0x4, %r14d
xorl %ebp, %ebp
movq 0x10(%rsp), %rcx
movb (%rcx,%rbp), %cl
cmpb (%r12,%rbp), %cl
jne 0xe00e
movl 0x4(%rsp), %ecx
testl %ecx, %ecx
je 0xe008
movq %rax, %rdi
movq %rsp, %rsi
leaq 0xc(%rsp), %rdx
movl $0x58, %r8d
callq 0xddf9
movl $0x0, 0x4(%rsp)
incl 0x8(%rsp)
jmp 0xe03b
movl 0x8(%rsp), %ecx
testl %ecx, %ecx
je 0xe034
movq %rax, %rdi
movq %rsp, %rsi
leaq 0xc(%rsp), %rdx
movl $0x3d, %r8d
callq 0xddf9
movl $0x0, 0x8(%rsp)
incl %r15d
incl 0x4(%rsp)
incq %rbp
cmpl %ebp, %r14d
jne 0xdfd4
movq 0x10(%rsp), %rcx
addq %r13, %rcx
incq %rcx
movq %rcx, 0x10(%rsp)
movq 0x20(%rsp), %r12
addq %r13, %r12
movq 0x68(%rsp), %rbp
jmp 0xe0eb
addq %r13, %r12
addl %r13d, %r15d
xorl %edi, %edi
leaq 0x8(%rsp), %rsi
leaq 0x4(%rsp), %rdx
movq %rsp, %rcx
movq %rbp, %r14
leaq 0xc(%rsp), %rbp
movq %rbp, %r8
movq %rax, %r9
callq 0xde6c
movq %rax, %rdi
movq %rsp, %rsi
movq %rbp, %rdx
movq %r14, %rbp
movl %r13d, %ecx
movl $0x49, %r8d
jmp 0xe0e6
movl %r13d, %ecx
addq %rcx, 0x10(%rsp)
addl %r13d, %r15d
xorl %edi, %edi
leaq 0x8(%rsp), %rsi
leaq 0x4(%rsp), %rdx
movq %rsp, %rcx
movq %rbp, %r14
leaq 0xc(%rsp), %rbp
movq %rbp, %r8
movq %rax, %r9
callq 0xde6c
movq %rax, %rdi
movq %rsp, %rsi
movq %rbp, %rdx
movq %r14, %rbp
movl %r13d, %ecx
movl $0x44, %r8d
callq 0xddf9
incq %rbx
movslq (%rbp), %rcx
cmpq %rcx, %rbx
jl 0xdf6f
leaq 0x8(%rsp), %rsi
leaq 0x4(%rsp), %rdx
movq %rsp, %rcx
leaq 0xc(%rsp), %r8
xorl %edi, %edi
movq %rax, %r9
callq 0xde6c
movq %rax, %rbx
movl 0x1c(%rsp), %ecx
notl %ecx
addl 0x18(%rsp), %ecx
testl %ecx, %ecx
jle 0xe141
movq %rsp, %rsi
leaq 0xc(%rsp), %rdx
movq %rbx, %rdi
movl $0x53, %r8d
callq 0xddf9
movq %rax, %rbx
movl (%rsp), %eax
movl %eax, (%rbp)
movq 0x60(%rsp), %r14
movq (%r14), %rdi
callq 0x3070
movq %rbx, (%r14)
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)