// File: /workspace/github_cpp_makefile_star2_O1/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 0x4535
movq %rcx, %rbx
movq %rsi, %r15
movq 0x40(%rsi), %rcx
movq 0x48(%rsi), %rsi
subq %rcx, %rsi
je 0x4535
movq %rcx, %r9
subq %r8, %r9
jb 0x4544
cmpq %rdx, %r9
ja 0x4535
addq %rdi, %r9
movq %rbx, %rdx
subq %r9, %rdx
jbe 0x4535
cmpq %rdx, %rsi
cmovbq %rsi, %rdx
addq %rcx, %rdx
movq %rdx, 0x48(%r14)
movq %rdx, 0x40(%r15)
movq %r14, %rdi
xorl %esi, %esi
callq 0x4b5c
testb %al, %al
je 0x45a1
movq %r15, %rdi
xorl %esi, %esi
callq 0x4b5c
testb %al, %al
je 0x4563
movl $0x1, %esi
movq %r15, %rdi
callq 0x4b5c
testb %al, %al
jne 0x4563
movl $0x1, %esi
movq %r14, %rdi
callq 0x4b5c
testb %al, %al
je 0x4453
movq (%r15), %rax
cmpq 0x20(%r14), %rax
jne 0x4582
movq 0x10(%r15), %rax
cmpq 0x30(%r14), %rax
jne 0x4582
movq 0x18(%r15), %rax
cmpq 0x38(%r14), %rax
jne 0x4582
movl $0x1, %esi
movq %r14, %rdi
callq 0x4b5c
testb %al, %al
jne 0x4525
movq (%r15), %rax
movq 0x8(%r15), %r12
testq %r12, %r12
je 0x448d
movq 0x7b45(%rip), %rcx      # 0xbfc0
cmpb $0x0, (%rcx)
je 0x4487
incl 0x8(%r12)
jmp 0x448d
lock
incl 0x8(%r12)
movq (%r14), %rcx
movq 0x8(%r14), %rdi
testq %rdi, %rdi
je 0x44ae
movq 0x7b20(%rip), %rdx      # 0xbfc0
cmpb $0x0, (%rdx)
je 0x44aa
incl 0x8(%rdi)
jmp 0x44ae
lock
incl 0x8(%rdi)
cmpq %rcx, %rax
setne %bpl
movq 0x18(%r14), %rax
cmpq %rax, 0x10(%r15)
sete %r13b
testq %rdi, %rdi
je 0x44cb
callq 0x413a
testq %r12, %r12
je 0x44d8
movq %r12, %rdi
callq 0x413a
andb %r13b, %bpl
movq (%r15), %rax
cmpq (%r14), %rax
jne 0x44fa
movq 0x10(%r15), %rcx
cmpq 0x10(%r14), %rcx
jne 0x44fa
movq 0x18(%r15), %rcx
cmpq 0x18(%r14), %rcx
sete %cl
jmp 0x44fc
xorl %ecx, %ecx
orb %bpl, %cl
je 0x45c0
testb %bpl, %bpl
je 0x4525
leaq 0x8(%r15), %rsi
movq %rax, 0x20(%r14)
leaq 0x28(%r14), %rdi
callq 0x57e4
movups 0x10(%r15), %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 0x3dfd(%rip), %rdi      # 0x8348
leaq 0x3e1b(%rip), %rsi      # 0x836d
leaq 0x3e1f(%rip), %rcx      # 0x8378
movl $0xb3, %edx
callq 0x3160
leaq 0x3e59(%rip), %rdi      # 0x83c3
leaq 0x3dfc(%rip), %rsi      # 0x836d
leaq 0x3e00(%rip), %rcx      # 0x8378
movl $0xd2, %edx
callq 0x3160
leaq 0x3e62(%rip), %rdi      # 0x83eb
leaq 0x3ddd(%rip), %rsi      # 0x836d
leaq 0x3de1(%rip), %rcx      # 0x8378
movl $0xd6, %edx
callq 0x3160
leaq 0x3e09(%rip), %rdi      # 0x83b1
leaq 0x3dbe(%rip), %rsi      # 0x836d
leaq 0x3dc2(%rip), %rcx      # 0x8378
movl $0xd0, %edx
callq 0x3160
leaq 0x3e63(%rip), %rdi      # 0x842a
leaq 0x3d9f(%rip), %rsi      # 0x836d
leaq 0x3da3(%rip), %rcx      # 0x8378
movl $0xdd, %edx
callq 0x3160