// File: Manca[P]algorithms1/./assignment6.hpp
// Function: assignment6::computeSumOfMedians(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
int computeSumOfMedians(const string& fileName)
    {
        std::ifstream file(fileName, ios::in);
        DataStructures::Heap heapHigh(true);    // Min-Heap - will hold the smallest element of the larger half
        DataStructures::Heap heapLow(false);    // Max-Heap - will hold the largest element of the smaller half
        
        std::vector<int> medians;
    
        if (file.is_open())
        {
            int num;
            while (file >> num)
            {
                if (heapLow.size() == 0)
                {
                    heapLow.insert(num);
                    medians.push_back(num);
                    continue;
                }
                
                // insert in appropriate heap
                if (num > heapLow[0])
                    heapHigh.insert(num);
                else
                    heapLow.insert(num);
                
                // balance the heaps
                if (heapLow.size() - heapHigh.size() > 1)
                    heapHigh.insert(heapLow.extractMinMax());   // from heapLow we extract max
                else if (heapHigh.size() - heapLow.size() > 1)
                    heapLow.insert(heapHigh.extractMinMax());   // from heapHigh we extract min
                
                // extract the median
                if (heapLow.size() >= heapHigh.size())
                    medians.push_back(heapLow[0]);
                else if (heapHigh.size() > heapLow.size())
                    medians.push_back(heapHigh[0]);
                
            }
            
            int totalSum = 0;
            for (auto m : medians)
            {
                totalSum += m;
            }
            
            assert(totalSum % 10000 == 1213);
            
            return totalSum;
        }
        
        return -1;
    }

// Assembly:
subq $0x308, %rsp            # imm = 0x308
movq %rdi, 0x2f8(%rsp)
movq 0x2f8(%rsp), %rsi
leaq 0xf0(%rsp), %rdi
movl $0x8, %edx
callq 0x22c0
leaq 0xd0(%rsp), %rdi
movl $0x1, %esi
callq 0x9300
jmp 0x6cad
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0x9300
jmp 0x6cbe
leaq 0x88(%rsp), %rdi
callq 0x7f80
leaq 0xf0(%rsp), %rdi
callq 0x22b0
movb %al, 0x57(%rsp)
jmp 0x6cde
movb 0x57(%rsp), %al
testb $0x1, %al
jne 0x6ceb
jmp 0x7090
jmp 0x6ced
leaq 0xf0(%rsp), %rdi
leaq 0x84(%rsp), %rsi
callq 0x2120
movq %rax, 0x48(%rsp)
jmp 0x6d09
movq 0x48(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2210
movb %al, 0x47(%rsp)
jmp 0x6d23
movb 0x47(%rsp), %al
testb $0x1, %al
jne 0x6d30
jmp 0x6fd6
leaq 0xa0(%rsp), %rdi
callq 0x9330
movl %eax, 0x40(%rsp)
jmp 0x6d43
movl 0x40(%rsp), %eax
cmpl $0x0, %eax
jne 0x6de7
movl 0x84(%rsp), %esi
leaq 0xa0(%rsp), %rdi
callq 0x9350
jmp 0x6d66
leaq 0x88(%rsp), %rdi
leaq 0x84(%rsp), %rsi
callq 0x7fd0
jmp 0x6d7d
jmp 0x6ced
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
jmp 0x70f3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
jmp 0x70e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x8300
leaq 0xa0(%rsp), %rdi
callq 0x9470
jmp 0x70e6
movl 0x84(%rsp), %eax
movl %eax, 0x38(%rsp)
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0x9390
movl %eax, 0x3c(%rsp)
jmp 0x6e07
movl 0x38(%rsp), %eax
movl 0x3c(%rsp), %ecx
cmpl %ecx, %eax
jle 0x6e2b
movl 0x84(%rsp), %esi
leaq 0xd0(%rsp), %rdi
callq 0x9350
jmp 0x6e29
jmp 0x6e43
movl 0x84(%rsp), %esi
leaq 0xa0(%rsp), %rdi
callq 0x9350
jmp 0x6e41
jmp 0x6e43
leaq 0xa0(%rsp), %rdi
callq 0x9330
movl %eax, 0x34(%rsp)
jmp 0x6e56
leaq 0xd0(%rsp), %rdi
callq 0x9330
movl %eax, 0x30(%rsp)
jmp 0x6e69
movl 0x30(%rsp), %ecx
movl 0x34(%rsp), %eax
subl %ecx, %eax
cmpl $0x1, %eax
jle 0x6ea0
leaq 0xa0(%rsp), %rdi
callq 0x93c0
movl %eax, 0x2c(%rsp)
jmp 0x6e8b
movl 0x2c(%rsp), %esi
leaq 0xd0(%rsp), %rdi
callq 0x9350
jmp 0x6e9e
jmp 0x6eff
leaq 0xd0(%rsp), %rdi
callq 0x9330
movl %eax, 0x28(%rsp)
jmp 0x6eb3
leaq 0xa0(%rsp), %rdi
callq 0x9330
movl %eax, 0x24(%rsp)
jmp 0x6ec6
movl 0x24(%rsp), %ecx
movl 0x28(%rsp), %eax
subl %ecx, %eax
cmpl $0x1, %eax
jle 0x6efd
leaq 0xd0(%rsp), %rdi
callq 0x93c0
movl %eax, 0x20(%rsp)
jmp 0x6ee8
movl 0x20(%rsp), %esi
leaq 0xa0(%rsp), %rdi
callq 0x9350
jmp 0x6efb
jmp 0x6efd
jmp 0x6eff
leaq 0xa0(%rsp), %rdi
callq 0x9330
movl %eax, 0x1c(%rsp)
jmp 0x6f12
leaq 0xd0(%rsp), %rdi
callq 0x9330
movl %eax, 0x18(%rsp)
jmp 0x6f25
movl 0x1c(%rsp), %eax
movl 0x18(%rsp), %ecx
cmpl %ecx, %eax
jl 0x6f6a
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0x9390
movl %eax, 0x14(%rsp)
jmp 0x6f46
movl 0x14(%rsp), %eax
movl %eax, 0x80(%rsp)
leaq 0x88(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x8dc0
jmp 0x6f68
jmp 0x6fd1
leaq 0xd0(%rsp), %rdi
callq 0x9330
movl %eax, 0x10(%rsp)
jmp 0x6f7d
leaq 0xa0(%rsp), %rdi
callq 0x9330
movl %eax, 0xc(%rsp)
jmp 0x6f90
movl 0x10(%rsp), %eax
movl 0xc(%rsp), %ecx
cmpl %ecx, %eax
jle 0x6fcf
leaq 0xd0(%rsp), %rdi
xorl %esi, %esi
callq 0x9390
movl %eax, 0x8(%rsp)
jmp 0x6fb1
movl 0x8(%rsp), %eax
movl %eax, 0x7c(%rsp)
leaq 0x88(%rsp), %rdi
leaq 0x7c(%rsp), %rsi
callq 0x8dc0
jmp 0x6fcd
jmp 0x6fcf
jmp 0x6fd1
jmp 0x6ced
movl $0x0, 0x78(%rsp)
leaq 0x88(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rdi
callq 0x8360
movq %rax, 0x68(%rsp)
movq 0x70(%rsp), %rdi
callq 0x8480
movq %rax, 0x60(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x8620
testb $0x1, %al
jne 0x701e
jmp 0x7046
leaq 0x68(%rsp), %rdi
callq 0x8660
movl (%rax), %eax
movl %eax, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
addl 0x78(%rsp), %eax
movl %eax, 0x78(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x8670
jmp 0x7009
movl 0x78(%rsp), %eax
movl $0x2710, %ecx           # imm = 0x2710
cltd
idivl %ecx
cmpl $0x4bd, %edx            # imm = 0x4BD
jne 0x705c
jmp 0x707b
leaq 0x13323(%rip), %rdi     # 0x1a386
leaq 0x132d7(%rip), %rsi     # 0x1a341
movl $0x98, %edx
leaq 0x13329(%rip), %rcx     # 0x1a39f
callq 0x2140
movl 0x78(%rsp), %eax
movl %eax, 0x304(%rsp)
movl $0x1, 0x58(%rsp)
jmp 0x70a3
movl $0xffffffff, 0x304(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x58(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x8300
leaq 0xa0(%rsp), %rdi
callq 0x9470
leaq 0xd0(%rsp), %rdi
callq 0x9470
leaq 0xf0(%rsp), %rdi
callq 0x2080
movl 0x304(%rsp), %eax
addq $0x308, %rsp            # imm = 0x308
retq
leaq 0xd0(%rsp), %rdi
callq 0x9470
leaq 0xf0(%rsp), %rdi
callq 0x2080
movq 0xc8(%rsp), %rdi
callq 0x2330
nopl (%rax)