// File: /workspace/github_cpp_makefile_star2_O2/iZhangHui[P]CppPrimer/CH09_SequentialContainers/examples/erase2.cc
// Function: main
int main()
{
    // lst has ten elements 0 ... 9 in value
    list<int> lst = {0,1,2,3,4,5,6,7,8,9};

    // print the initial values in lst
    cout << "initial list: ";
    for (auto it : lst)
        cout << it << " ";
    cout << endl;

    // erase the odd elements in lst
    auto it = lst.begin();
    while (it != lst.end())
        if (*it % 2)             // if the element is odd
            it = lst.erase(it);  // erase this element
        else
            ++it;

    // print the current contents of lst
    cout << "after erasing odd elements from lst: ";
    for (auto it : lst)
        cout << it << " ";
    cout << endl;

    // repeat the same actions but on a forward_list
    forward_list<int> flst = {0,1,2,3,4,5,6,7,8,9};

    // print the initial values in flst
    cout << "initial list: ";
    for (auto it : flst)
        cout << it << " ";
    cout << endl;

    // erase the odd elements in flst
    auto prev = flst.before_begin(); // element "off the start" of flst
    auto curr = flst.begin();     // denotes the first element in flst
    while (curr != flst.end()) {  // while there are still elements
        if (*curr % 2)                     // if the element is odd
            curr = flst.erase_after(prev); // erase it and move curr
        else {
            prev = curr; // move the iterators to denote the next
            ++curr;      // element and one before the next element
        }
    }

    // print the current contents of lst
    cout << "after erasing elements from flst: ";
    for (auto it : flst)
        cout << it << " ";
    cout << endl;

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movups 0xe08(%rip), %xmm0      # 0x206c
leaq -0x80(%rbp), %rsi
movaps %xmm0, 0x10(%rsi)
movups 0xde9(%rip), %xmm0      # 0x205c
movaps %xmm0, (%rsi)
movabsq $0x900000008, %r12      # imm = 0x900000008
movq %r12, 0x20(%rsi)
leaq -0x50(%rbp), %rbx
pushq $0xa
popq %rdx
leaq -0x38(%rbp), %rcx
movq %rbx, %rdi
callq 0x14a8
movq 0x2d3a(%rip), %rdi      # 0x3fd8
leaq 0xd5f(%rip), %rsi       # 0x2004
callq 0x10a0
movq 0x2d27(%rip), %r14      # 0x3fd8
leaq 0xd59(%rip), %r15       # 0x2011
movq %rbx, %r13
movq (%r13), %r13
cmpq %rbx, %r13
je 0x12dd
movl 0x10(%r13), %esi
movq %r14, %rdi
callq 0x10f0
movq %rax, %rdi
movq %r15, %rsi
callq 0x10a0
jmp 0x12bb
movq 0x2cf4(%rip), %rdi      # 0x3fd8
callq 0x1070
leaq -0x50(%rbp), %rbx
movq (%rbx), %rax
cmpq %rbx, %rax
je 0x130d
testb $0x1, 0x10(%rax)
jne 0x1300
movq (%rax), %rax
jmp 0x12f0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x14e0
jmp 0x12f0
movq 0x2cc4(%rip), %rdi      # 0x3fd8
leaq 0xcf8(%rip), %rsi       # 0x2013
callq 0x10a0
movq 0x2cb1(%rip), %r14      # 0x3fd8
leaq 0xce3(%rip), %r15       # 0x2011
movq %rbx, %r13
movq (%r13), %r13
cmpq %rbx, %r13
je 0x1353
movl 0x10(%r13), %esi
movq %r14, %rdi
callq 0x10f0
movq %rax, %rdi
movq %r15, %rsi
callq 0x10a0
jmp 0x1331
movq 0x2c7e(%rip), %rdi      # 0x3fd8
callq 0x1070
leaq -0x80(%rbp), %rsi
movups 0xd02(%rip), %xmm0      # 0x206c
movaps %xmm0, 0x10(%rsi)
movups 0xce7(%rip), %xmm0      # 0x205c
movaps %xmm0, (%rsi)
movq %r12, 0x20(%rsi)
leaq -0x38(%rbp), %rbx
pushq $0xa
popq %rdx
leaq -0x29(%rbp), %rcx
movq %rbx, %rdi
callq 0x14f8
movq 0x2c42(%rip), %rdi      # 0x3fd8
leaq 0xc67(%rip), %rsi       # 0x2004
callq 0x10a0
movq 0x2c2f(%rip), %r14      # 0x3fd8
leaq 0xc61(%rip), %r15       # 0x2011
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x13d0
movl 0x8(%rbx), %esi
movq %r14, %rdi
callq 0x10f0
movq %rax, %rdi
movq %r15, %rsi
callq 0x10a0
jmp 0x13b0
movq 0x2c01(%rip), %rdi      # 0x3fd8
callq 0x1070
leaq -0x38(%rbp), %rbx
movq %rbx, %rax
movq %rax, %r14
movq (%rax), %rax
testq %rax, %rax
je 0x1401
testb $0x1, 0x8(%rax)
je 0x13e3
movq %rbx, %rdi
movq %r14, %rsi
callq 0x17ae
jmp 0x13e9
movq 0x2bd0(%rip), %rdi      # 0x3fd8
leaq 0xc2a(%rip), %rsi       # 0x2039
callq 0x10a0
leaq -0x38(%rbp), %r15
movq 0x2bb9(%rip), %rbx      # 0x3fd8
leaq 0xbeb(%rip), %r14       # 0x2011
movq (%r15), %r15
testq %r15, %r15
je 0x1447
movl 0x8(%r15), %esi
movq %rbx, %rdi
callq 0x10f0
movq %rax, %rdi
movq %r14, %rsi
callq 0x10a0
jmp 0x1426
movq 0x2b8a(%rip), %rdi      # 0x3fd8
callq 0x1070
leaq -0x38(%rbp), %rdi
callq 0x1528
leaq -0x50(%rbp), %rdi
callq 0x1540
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1494
jmp 0x1484
jmp 0x1484
jmp 0x1494
jmp 0x1494
jmp 0x1484
jmp 0x1484
movq %rax, %rbx
leaq -0x38(%rbp), %rdi
callq 0x1528
jmp 0x1497
jmp 0x1494
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x1540
movq %rbx, %rdi
callq 0x1100