// File: /workspace/github_c_makefile_star2_O2/LemonBoy[P]autobahn/xdiff/xdiffi.c
// Function: xdl_change_compact
int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {
	struct xdlgroup g, go;
	long earliest_end, end_matching_other;
	long groupsize;

	group_init(xdf, &g);
	group_init(xdfo, &go);

	while (1) {
		/* If the group is empty in the to-be-compacted file, skip it: */
		if (g.end == g.start)
			goto next;

		/*
		 * Now shift the change up and then down as far as possible in
		 * each direction. If it bumps into any other changes, merge them.
		 */
		do {
			groupsize = g.end - g.start;

			/*
			 * Keep track of the last "end" index that causes this
			 * group to align with a group of changed lines in the
			 * other file. -1 indicates that we haven't found such
			 * a match yet:
			 */
			end_matching_other = -1;

			/* Shift the group backward as much as possible: */
			while (!group_slide_up(xdf, &g, flags))
				if (group_previous(xdfo, &go))
					xdl_bug("group sync broken sliding up");

			/*
			 * This is this highest that this group can be shifted.
			 * Record its end index:
			 */
			earliest_end = g.end;

			if (go.end > go.start)
				end_matching_other = g.end;

			/* Now shift the group forward as far as possible: */
			while (1) {
				if (group_slide_down(xdf, &g, flags))
					break;
				if (group_next(xdfo, &go))
					xdl_bug("group sync broken sliding down");

				if (go.end > go.start)
					end_matching_other = g.end;
			}
		} while (groupsize != g.end - g.start);

		/*
		 * If the group can be shifted, then we can possibly use this
		 * freedom to produce a more intuitive diff.
		 *
		 * The group is currently shifted as far down as possible, so the
		 * heuristics below only have to handle upwards shifts.
		 */

		if (g.end == earliest_end) {
			/* no shifting was possible */
		} else if (end_matching_other != -1) {
			/*
			 * Move the possibly merged group of changes back to line
			 * up with the last group of changes from the other file
			 * that it can align with.
			 */
			while (go.end == go.start) {
				if (group_slide_up(xdf, &g, flags))
					xdl_bug("match disappeared");
				if (group_previous(xdfo, &go))
					xdl_bug("group sync broken sliding to match");
			}
		} else if (flags & XDF_INDENT_HEURISTIC) {
			/*
			 * Indent heuristic: a group of pure add/delete lines
			 * implies two splits, one between the end of the "before"
			 * context and the start of the group, and another between
			 * the end of the group and the beginning of the "after"
			 * context. Some splits are aesthetically better and some
			 * are worse. We compute a badness "score" for each split,
			 * and add the scores for the two splits to define a
			 * "score" for each position that the group can be shifted
			 * to. Then we pick the shift with the lowest score.
			 */
			long shift, best_shift = -1;
			struct split_score best_score;

			for (shift = earliest_end; shift <= g.end; shift++) {
				struct split_measurement m;
				struct split_score score = {0, 0};

				measure_split(xdf, shift, &m);
				score_add_split(&m, &score);
				measure_split(xdf, shift - groupsize, &m);
				score_add_split(&m, &score);
				if (best_shift == -1 ||
				    score_cmp(&score, &best_score) <= 0) {
					best_score.effective_indent = score.effective_indent;
					best_score.penalty = score.penalty;
					best_shift = shift;
				}
			}

			while (g.end > best_shift) {
				if (group_slide_up(xdf, &g, flags))
					xdl_bug("best shift unreached");
				if (group_previous(xdfo, &go))
					xdl_bug("group sync broken sliding to blank line");
			}
		}

	next:
		/* Move past the just-processed group: */
		if (group_next(xdf, &g))
			break;
		if (group_next(xdfo, &go))
			xdl_bug("group sync broken moving to next group");
	}

	if (!group_next(xdfo, &go))
		xdl_bug("group sync broken at end of file");

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq %rsp, %r13
movq %r13, %rsi
callq 0x1ef7
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x1ef7
movq (%rsp), %r12
movq 0x8(%rsp), %rbp
cmpq %r12, %rbp
je 0x1d25
movq %rbp, %rax
movq %r12, 0x38(%rsp)
subq %r12, %rax
movq %rax, 0x28(%rsp)
movq %rsp, %r12
leaq 0x10(%rsp), %r13
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1f15
testl %eax, %eax
jne 0x1c73
movq %r14, %rdi
movq %r13, %rsi
callq 0x1f8b
testl %eax, %eax
je 0x1c4d
jmp 0x1e97
movq %rbp, 0x40(%rsp)
movq (%rsp), %r12
movq 0x8(%rsp), %rbp
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
pushq $-0x1
popq %r13
cmovgq %rbp, %r13
movq %rbp, 0x48(%rsp)
cmpq 0x38(%r15), %rbp
jge 0x1cfe
movq 0x60(%r15), %rax
movq (%rax,%r12,8), %rdi
movq (%rax,%rbp,8), %rsi
movq %rbx, %rdx
callq 0x244e
testl %eax, %eax
je 0x1cfe
movq 0x68(%r15), %rax
movb $0x0, (%rax,%r12)
movq 0x68(%r15), %rax
movb $0x1, (%rax,%rbp)
movq 0x68(%r15), %rax
cmpb $0x0, 0x1(%rax,%rbp)
leaq 0x1(%rbp), %rbp
jne 0x1ccb
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x1fd7
testl %eax, %eax
jne 0x1ea3
incq %r12
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
cmovgq %rbp, %r13
jmp 0x1c98
movq %rbp, 0x8(%rsp)
movq %r12, (%rsp)
movq %rbp, %rax
subq %r12, %rax
cmpq %rax, 0x28(%rsp)
jne 0x1c35
movq 0x48(%rsp), %rsi
cmpq %rsi, %rbp
jne 0x1d54
movq %rsp, %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x1fd7
movl %eax, %ebp
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x1fd7
testl %ebp, %ebp
jne 0x1e82
testl %eax, %eax
je 0x1c23
jmp 0x1edf
cmpq $-0x1, %r13
je 0x1d98
movq %rsp, %r13
leaq 0x10(%rsp), %r12
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
jne 0x1d25
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x1f15
testl %eax, %eax
jne 0x1ebb
movq %r14, %rdi
movq %r12, %rsi
callq 0x1f8b
testl %eax, %eax
je 0x1d62
jmp 0x1eaf
btl $0x8, %ebx
movq %rsp, %r13
jae 0x1d25
movq 0x40(%rsp), %rax
subq %rax, 0x38(%rsp)
pushq $-0x1
popq %r12
cmpq %rbp, %rsi
jg 0x1e76
andq $0x0, 0x20(%rsp)
movq %r15, %rdi
movq %r12, 0x28(%rsp)
leaq 0x50(%rsp), %r13
movq %r13, %rdx
movq %rsi, %r12
callq 0x2000
movq %r13, %rdi
leaq 0x20(%rsp), %rsi
callq 0x20c9
movq 0x38(%rsp), %rax
leaq (%rax,%r12), %rsi
movq %r15, %rdi
leaq 0x50(%rsp), %r13
movq %r13, %rdx
callq 0x2000
movq %r13, %rdi
leaq 0x20(%rsp), %rsi
callq 0x20c9
movq %r12, %rsi
movq 0x28(%rsp), %r12
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %ecx
cmpq $-0x1, %r12
je 0x1e3d
xorl %edx, %edx
xorl %edi, %edi
cmpl 0x30(%rsp), %eax
setg %dl
setl %dil
subl %edi, %edx
imull $0x3c, %edx, %edx
movl %ecx, %edi
subl 0x34(%rsp), %edi
addl %edx, %edi
jg 0x1e48
movl %ecx, 0x34(%rsp)
movl %eax, 0x30(%rsp)
movq %rsi, %r12
incq %rsi
movq %rsp, %r13
jmp 0x1daf
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x1f15
testl %eax, %eax
jne 0x1ec7
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x1f8b
testl %eax, %eax
jne 0x1ed3
cmpq %r12, 0x8(%rsp)
jg 0x1e53
jmp 0x1d25
testl %eax, %eax
je 0x1eeb
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5166(%rip), %rdi      # 0x7004
callq 0x1fb3
leaq 0x5177(%rip), %rdi      # 0x7021
callq 0x1fb3
leaq 0x519c(%rip), %rdi      # 0x7052
callq 0x1fb3
leaq 0x517e(%rip), %rdi      # 0x7040
callq 0x1fb3
leaq 0x51a7(%rip), %rdi      # 0x7075
callq 0x1fb3
leaq 0x51b0(%rip), %rdi      # 0x708a
callq 0x1fb3
leaq 0x51cc(%rip), %rdi      # 0x70b2
callq 0x1fb3
leaq 0x51e7(%rip), %rdi      # 0x70d9
callq 0x1fb3