// File: /workspace/github_cpp_makefile_star2_O3/jansucan[P]diff-dd/src/create.cpp
// Function: diffsTryMerge(Diff&, Diff&, unsigned long, unsigned long)
MergeState
diffsTryMerge(Diff &diff_a, Diff &diff_b, size_t max_merge_gap, size_t max_size)
{
    if (diff_a.isEmpty()) {
        // Do not merge to an empty diff
        return MergeState::Finished;
    }

    if (diff_b.isEmpty()) {
        // Nothing to merge from an empty diff
        return MergeState::Finished;
    }

    assert(diff_a.getEnd() <= diff_b.getStart());
    const size_t gap{diff_b.getStart() - diff_a.getEnd()};
    if (gap > max_merge_gap) {
        // B is too far away
        return MergeState::Finished;
    }

    if ((diff_a.getSize() + gap) >= max_size) {
        // No space in A
        return MergeState::Finished;
    }

    // Can be merged

    // Adjust the diff start and end offsets

    // There is always at least 1 byte free in A here
    const size_t free{max_size - (diff_a.getSize() + gap)};
    const size_t to_merge{std::min(free, diff_b.getSize())};
    // There is always at least 1 byte to merge from B here

    // Enlarge A
    diff_a.m_end += gap + to_merge;
    // Shrink B
    diff_b.m_start += to_merge;

    // Add B's page to A if needed

    // Non-empty A must have only the first, or both pages
    assert(diff_a.hasPage(0));
    // Non-empty B must have only the first page
    assert(diff_b.hasPage(0) && !diff_b.hasPage(1));

    // If A has both pages, B's page must only be the same as A's second
    // page. No setting of pages in A is needed in this case
    assert(!diff_a.hasPage(1) || (diff_b.m_pages[0] == diff_a.m_pages[1]));
    if (!diff_a.hasPage(1)) {
        // If A has only the first page, B's page must only be the same as the
        // A's first page or following it
        const bool b_follows{
            (diff_b.m_pages[0].getData() != diff_a.m_pages[0].getData()) &&
            (diff_b.m_pages[0].getStart() == diff_a.m_pages[0].getEnd())};
        assert((diff_b.m_pages[0] == diff_a.m_pages[0]) || b_follows);
        if (b_follows) {
            diff_a.m_pages[1] = diff_b.m_pages[0];
        }
    }

    return (diff_a.getSize() >= max_size) ? MergeState::Finished
                                          : MergeState::Incomplete;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r14
movq 0x48(%rdi), %r8
xorl %eax, %eax
movq %r8, %rdi
subq 0x40(%r14), %rdi
je 0x4550
movq %rcx, %rbx
movq %rsi, %r15
movq 0x40(%rsi), %rcx
movq 0x48(%rsi), %rsi
subq %rcx, %rsi
je 0x4550
movq %rcx, %r9
subq %r8, %r9
jb 0x455f
cmpq %rdx, %r9
ja 0x4550
addq %rdi, %r9
movq %rbx, %rdx
subq %r9, %rdx
jbe 0x4550
cmpq %rdx, %rsi
cmovbq %rsi, %rdx
addq %rcx, %rdx
movq %rdx, 0x48(%r14)
movq %rdx, 0x40(%r15)
movq %r14, %rdi
xorl %esi, %esi
callq 0x4b6a
testb %al, %al
je 0x45bc
movq %r15, %rdi
xorl %esi, %esi
callq 0x4b6a
testb %al, %al
je 0x457e
movl $0x1, %esi
movq %r15, %rdi
callq 0x4b6a
testb %al, %al
jne 0x457e
movl $0x1, %esi
movq %r14, %rdi
callq 0x4b6a
testb %al, %al
je 0x443f
movq (%r15), %rax
cmpq 0x20(%r14), %rax
jne 0x459d
movq 0x10(%r15), %rax
cmpq 0x30(%r14), %rax
jne 0x459d
movq 0x18(%r15), %rax
cmpq 0x38(%r14), %rax
jne 0x459d
movl $0x1, %esi
movq %r14, %rdi
callq 0x4b6a
testb %al, %al
jne 0x4540
movq (%r15), %rax
movq 0x8(%r15), %r12
testq %r12, %r12
je 0x4479
movq 0x7b59(%rip), %rcx      # 0xbfc0
cmpb $0x0, (%rcx)
je 0x4473
incl 0x8(%r12)
jmp 0x4479
lock
incl 0x8(%r12)
movq (%r14), %rcx
movq 0x8(%r14), %rdi
testq %rdi, %rdi
je 0x4496
movq 0x7b34(%rip), %rdx      # 0xbfc0
cmpb $0x0, (%rdx)
je 0x44b9
incl 0x8(%rdi)
jmp 0x44bd
cmpq %rcx, %rax
setne %al
leaq 0x10(%r15), %r13
leaq 0x18(%r14), %rcx
movq %rcx, (%rsp)
movq 0x18(%r14), %rcx
cmpq %rcx, 0x10(%r15)
sete %bpl
andb %al, %bpl
jmp 0x44e3
lock
incl 0x8(%rdi)
cmpq %rcx, %rax
setne %al
leaq 0x10(%r15), %r13
leaq 0x18(%r14), %rcx
movq %rcx, (%rsp)
movq 0x18(%r14), %rcx
cmpq %rcx, 0x10(%r15)
sete %bpl
andb %al, %bpl
callq 0x4130
testq %r12, %r12
je 0x44f0
movq %r12, %rdi
callq 0x4130
movq (%r15), %rax
cmpq (%r14), %rax
jne 0x4512
movq (%r13), %rcx
cmpq 0x10(%r14), %rcx
jne 0x4512
movq 0x18(%r15), %rcx
movq (%rsp), %rdx
cmpq (%rdx), %rcx
sete %cl
jmp 0x4514
xorl %ecx, %ecx
orb %bpl, %cl
je 0x45db
testb %bpl, %bpl
je 0x4540
addq $0x8, %r15
movq %rax, 0x20(%r14)
leaq 0x28(%r14), %rdi
movq %r15, %rsi
callq 0x57d8
movups (%r13), %xmm0
movups %xmm0, 0x30(%r14)
movq 0x48(%r14), %rcx
subq 0x40(%r14), %rcx
xorl %eax, %eax
cmpq %rbx, %rcx
setb %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3de2(%rip), %rdi      # 0x8348
leaq 0x3e00(%rip), %rsi      # 0x836d
leaq 0x3e04(%rip), %rcx      # 0x8378
movl $0xb3, %edx
callq 0x3160
leaq 0x3e3e(%rip), %rdi      # 0x83c3
leaq 0x3de1(%rip), %rsi      # 0x836d
leaq 0x3de5(%rip), %rcx      # 0x8378
movl $0xd2, %edx
callq 0x3160
leaq 0x3e47(%rip), %rdi      # 0x83eb
leaq 0x3dc2(%rip), %rsi      # 0x836d
leaq 0x3dc6(%rip), %rcx      # 0x8378
movl $0xd6, %edx
callq 0x3160
leaq 0x3dee(%rip), %rdi      # 0x83b1
leaq 0x3da3(%rip), %rsi      # 0x836d
leaq 0x3da7(%rip), %rcx      # 0x8378
movl $0xd0, %edx
callq 0x3160
leaq 0x3e48(%rip), %rdi      # 0x842a
leaq 0x3d84(%rip), %rsi      # 0x836d
leaq 0x3d88(%rip), %rcx      # 0x8378
movl $0xdd, %edx
callq 0x3160