// File: Manca[P]algorithms1/./assignment1.hpp
// Function: assignment1::CountSplitInv(std::vector<int, std::allocator<int>>&, std::vector<int, std::allocator<int>> const&, std::vector<int, std::allocator<int>> const&)
long long CountSplitInv(std::vector<int> &input, const std::vector<int>& left, const std::vector<int>& right)
    {
        std::vector<int> result;
        long long inversions = 0;
        
        size_t i = 0, j = 0;
        
        for (size_t k = 0; k < (left.size() + right.size()); k++)
        {
            if ( (i < left.size()) && (j < right.size()) && (left[i] < right[j]))
            {
                result.push_back(left[i]);
                i++;
            }
            else if ( (j < right.size()) && (i <left.size()) && (right[j] <= left[i]))
            {
                result.push_back(right[j]);
                j++;
                
                // inversion found (it's a split inversion)!
                inversions += left.size() - i;
                
                // if you want to print the inversions, uncomment this code
                //for (int o = i; o < left.size(); o++)
                //    cout << right[(j - 1)] << ", " << left[o] << endl;
            }
            else if (i < left.size())
            {
                result.push_back(left[i]);
                i++;
            }
            else if (j < right.size())
            {
                result.push_back(right[j]);
                j++;
            }
        }
        input = result;
        
        return inversions;
    }

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movq %rdx, 0xa0(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x7f80
movq $0x0, 0x80(%rsp)
movq $0x0, 0x78(%rsp)
movq $0x0, 0x70(%rsp)
movq $0x0, 0x68(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x7f90
movq %rax, 0x48(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x7f90
movq 0x48(%rsp), %rcx
movq %rax, %rdx
movq 0x50(%rsp), %rax
addq %rdx, %rcx
cmpq %rcx, %rax
jae 0x2908
movq 0x78(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x7f90
movq %rax, %rcx
movq 0x40(%rsp), %rax
cmpq %rcx, %rax
jae 0x2761
movq 0x70(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x7f90
movq %rax, %rcx
movq 0x38(%rsp), %rax
cmpq %rcx, %rax
jae 0x2761
movq 0xa8(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x7fb0
movl (%rax), %eax
movl %eax, 0x34(%rsp)
movq 0xa0(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x7fb0
movq %rax, %rcx
movl 0x34(%rsp), %eax
cmpl (%rcx), %eax
jge 0x2761
movq 0xa8(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x7fb0
movq %rax, %rsi
leaq 0x88(%rsp), %rdi
callq 0x7fd0
jmp 0x272e
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x28f3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x8300
jmp 0x2944
movq 0x70(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x7f90
movq %rax, %rcx
movq 0x28(%rsp), %rax
cmpq %rcx, %rax
jae 0x283f
movq 0x78(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x7f90
movq %rax, %rcx
movq 0x20(%rsp), %rax
cmpq %rcx, %rax
jae 0x283f
movq 0xa0(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x7fb0
movl (%rax), %eax
movl %eax, 0x1c(%rsp)
movq 0xa8(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x7fb0
movq %rax, %rcx
movl 0x1c(%rsp), %eax
cmpl (%rcx), %eax
jg 0x283f
movq 0xa0(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x7fb0
movq %rax, %rsi
leaq 0x88(%rsp), %rdi
callq 0x7fd0
jmp 0x280a
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x7f90
subq 0x78(%rsp), %rax
addq 0x80(%rsp), %rax
movq %rax, 0x80(%rsp)
jmp 0x28f1
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x7f90
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpq %rcx, %rax
jae 0x2897
movq 0xa8(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x7fb0
movq %rax, %rsi
leaq 0x88(%rsp), %rdi
callq 0x7fd0
jmp 0x2887
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x28ef
movq 0x70(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x7f90
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jae 0x28ed
movq 0xa0(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x7fb0
movq %rax, %rsi
leaq 0x88(%rsp), %rdi
callq 0x7fd0
jmp 0x28df
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
jmp 0x28ef
jmp 0x28f1
jmp 0x28f3
jmp 0x28f5
movq 0x68(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x68(%rsp)
jmp 0x2643
movq 0xb0(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x8040
jmp 0x291f
movq 0x80(%rsp), %rax
movq %rax, (%rsp)
leaq 0x88(%rsp), %rdi
callq 0x8300
movq (%rsp), %rax
addq $0xb8, %rsp
retq
movq 0x60(%rsp), %rdi
callq 0x2330
nop