// File: /workspace/github_cpp_makefile_star2_O1/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, %rax      # imm = 0x900000008
movq %rax, 0x20(%rsi)
leaq -0x50(%rbp), %rbx
leaq -0x30(%rbp), %rcx
movl $0xa, %edx
movq %rbx, %rdi
callq 0x15de
movq 0x2d38(%rip), %rdi      # 0x3fd8
leaq 0xd5d(%rip), %rsi       # 0x2004
movl $0xe, %edx
callq 0x10a0
movq 0x2d20(%rip), %r14      # 0x3fd8
leaq 0xd52(%rip), %r15       # 0x2011
movq %rbx, %r13
movq (%r13), %r13
cmpq %rbx, %r13
je 0x12e9
movl 0x10(%r13), %esi
movq %r14, %rdi
callq 0x10d0
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x10a0
jmp 0x12c2
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x1070
movsbl %al, %esi
movq 0x2cd1(%rip), %rdi      # 0x3fd8
callq 0x1030
movq %rax, %rdi
callq 0x1050
leaq -0x50(%rbp), %r12
movq (%r12), %r14
cmpq %r12, %r14
je 0x134c
testb $0x1, 0x10(%r14)
movq (%r14), %r15
je 0x1344
decq -0x40(%rbp)
movq %r14, %rdi
callq 0x10c0
movl $0x18, %esi
movq %r14, %rdi
callq 0x1090
movq %r15, %r14
cmpq %r12, %r15
jne 0x1321
movq 0x2c85(%rip), %rdi      # 0x3fd8
leaq 0xcb9(%rip), %rsi       # 0x2013
movl $0x25, %edx
callq 0x10a0
movq 0x2c6d(%rip), %r14      # 0x3fd8
leaq 0xc9f(%rip), %r15       # 0x2011
movq %r12, %r13
movq (%r13), %r13
cmpq %r12, %r13
je 0x139c
movl 0x10(%r13), %esi
movq %r14, %rdi
callq 0x10d0
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x10a0
jmp 0x1375
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x1070
movsbl %al, %esi
movq 0x2c1e(%rip), %rdi      # 0x3fd8
callq 0x1030
movq %rax, %rdi
callq 0x1050
leaq -0x80(%rbp), %rsi
movups 0xc9a(%rip), %xmm0      # 0x206c
movaps %xmm0, 0x10(%rsi)
movups 0xc7f(%rip), %xmm0      # 0x205c
movaps %xmm0, (%rsi)
movabsq $0x900000008, %rax      # imm = 0x900000008
movq %rax, 0x20(%rsi)
leaq -0x30(%rbp), %r14
leaq -0x31(%rbp), %rcx
movl $0xa, %edx
movq %r14, %rdi
callq 0x1636
movq 0x2bce(%rip), %rdi      # 0x3fd8
leaq 0xbf3(%rip), %rsi       # 0x2004
movl $0xe, %edx
callq 0x10a0
movq 0x2bb6(%rip), %r15      # 0x3fd8
leaq 0xbe8(%rip), %r12       # 0x2011
movq (%r14), %r14
testq %r14, %r14
je 0x144f
movl 0x8(%r14), %esi
movq %r15, %rdi
callq 0x10d0
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x10a0
jmp 0x1429
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x1070
movsbl %al, %esi
movq 0x2b6b(%rip), %rdi      # 0x3fd8
callq 0x1030
movq %rax, %rdi
callq 0x1050
movq -0x30(%rbp), %rax
testq %rax, %rax
je 0x14ae
leaq -0x30(%rbp), %r14
testb $0x1, 0x8(%rax)
je 0x14a3
movq (%r14), %rdi
movq (%rdi), %rax
movq %rax, (%r14)
movl $0x10, %esi
callq 0x1090
movq %r14, %rax
movq %rax, %r14
movq (%rax), %rax
testq %rax, %rax
jne 0x1487
movq 0x2b23(%rip), %rdi      # 0x3fd8
leaq 0xb7d(%rip), %rsi       # 0x2039
movl $0x22, %edx
callq 0x10a0
leaq -0x30(%rbp), %r12
movq 0x2b07(%rip), %r14      # 0x3fd8
leaq 0xb39(%rip), %r15       # 0x2011
movq (%r12), %r12
testq %r12, %r12
je 0x1500
movl 0x8(%r12), %esi
movq %r14, %rdi
callq 0x10d0
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x10a0
jmp 0x14d8
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x1070
movsbl %al, %esi
movq 0x2aba(%rip), %rdi      # 0x3fd8
callq 0x1030
movq %rax, %rdi
callq 0x1050
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x1549
movq (%rdi), %rbx
movl $0x10, %esi
callq 0x1090
movq %rbx, %rdi
testq %rbx, %rbx
jne 0x1534
leaq -0x50(%rbp), %rbx
movq (%rbx), %rdi
cmpq %rbx, %rdi
je 0x156a
movq (%rdi), %r14
movl $0x18, %esi
callq 0x1090
movq %r14, %rdi
cmpq %rbx, %r14
jne 0x1555
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x15b4
jmp 0x1587
jmp 0x1587
jmp 0x15b4
jmp 0x15b4
jmp 0x1587
movq %rax, %r14
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x15a8
movq (%rdi), %r15
movl $0x10, %esi
callq 0x1090
movq %r15, %rdi
testq %r15, %r15
jne 0x1593
movq $0x0, -0x30(%rbp)
jmp 0x15b7
jmp 0x15b4
movq %rax, %r14
movq -0x50(%rbp), %rdi
cmpq %rbx, %rdi
je 0x15d5
movq (%rdi), %r15
movl $0x18, %esi
callq 0x1090
movq %r15, %rdi
cmpq %rbx, %r15
jne 0x15c0
movq %r14, %rdi
callq 0x10e0
nop