// File: 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
subq $0x1d0, %rsp            # imm = 0x1D0
movl $0x0, -0x4(%rbp)
movabsq $0x900000008, %rax      # imm = 0x900000008
movq %rax, -0x40(%rbp)
movups 0x1d8d(%rip), %xmm0     # 0x3014
movaps %xmm0, -0x50(%rbp)
movups 0x1d72(%rip), %xmm0     # 0x3004
movaps %xmm0, -0x60(%rbp)
leaq -0x60(%rbp), %rax
movq %rax, -0x30(%rbp)
movq $0xa, -0x28(%rbp)
leaq -0x61(%rbp), %rdi
movq %rdi, -0x198(%rbp)
callq 0x18a0
movq -0x198(%rbp), %rcx
movq -0x30(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x20(%rbp), %rdi
callq 0x18c0
jmp 0x12d0
leaq -0x61(%rbp), %rdi
callq 0x1970
movq 0x4cf8(%rip), %rdi      # 0x5fd8
leaq 0x1d6d(%rip), %rsi      # 0x3054
callq 0x10a0
jmp 0x12ee
leaq -0x20(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
callq 0x1990
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
callq 0x19c0
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x19f0
testb $0x1, %al
jne 0x132f
jmp 0x13ae
leaq -0x88(%rbp), %rdi
callq 0x1a20
movl (%rax), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %esi
movq 0x4c88(%rip), %rdi      # 0x5fd8
callq 0x1100
movq %rax, -0x1a0(%rbp)
jmp 0x135e
movq -0x1a0(%rbp), %rdi
leaq 0x1cf5(%rip), %rsi      # 0x3061
callq 0x10a0
jmp 0x1373
jmp 0x1375
leaq -0x88(%rbp), %rdi
callq 0x1a40
jmp 0x1316
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x61(%rbp), %rdi
callq 0x1970
jmp 0x1892
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x1889
movq 0x4c23(%rip), %rdi      # 0x5fd8
movq 0x4c0c(%rip), %rsi      # 0x5fc8
callq 0x10d0
jmp 0x13c3
leaq -0x20(%rbp), %rdi
callq 0x1990
movq %rax, -0xa0(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x19c0
movq %rax, -0xa8(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x19f0
testb $0x1, %al
jne 0x13fc
jmp 0x1462
leaq -0xa0(%rbp), %rdi
callq 0x1a20
movl (%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x1451
leaq -0xb8(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x1ab0
movq -0xb8(%rbp), %rsi
leaq -0x20(%rbp), %rdi
callq 0x1a60
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x145d
leaq -0xa0(%rbp), %rdi
callq 0x1a40
jmp 0x13d3
movq 0x4b6f(%rip), %rdi      # 0x5fd8
leaq 0x1bf3(%rip), %rsi      # 0x3063
callq 0x10a0
jmp 0x1477
leaq -0x20(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rdi
callq 0x1990
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rdi
callq 0x19c0
movq %rax, -0xd0(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x19f0
testb $0x1, %al
jne 0x14c1
jmp 0x1515
leaq -0xc8(%rbp), %rdi
callq 0x1a20
movl (%rax), %eax
movl %eax, -0xd4(%rbp)
movl -0xd4(%rbp), %esi
movq 0x4af6(%rip), %rdi      # 0x5fd8
callq 0x1100
movq %rax, -0x1a8(%rbp)
jmp 0x14f0
movq -0x1a8(%rbp), %rdi
leaq 0x1b63(%rip), %rsi      # 0x3061
callq 0x10a0
jmp 0x1505
jmp 0x1507
leaq -0xc8(%rbp), %rdi
callq 0x1a40
jmp 0x14a8
movq 0x4abc(%rip), %rdi      # 0x5fd8
movq 0x4aa5(%rip), %rsi      # 0x5fc8
callq 0x10d0
jmp 0x152a
movups 0x1b0b(%rip), %xmm0     # 0x303c
movaps %xmm0, -0x110(%rbp)
movups 0x1aed(%rip), %xmm0     # 0x302c
movaps %xmm0, -0x120(%rbp)
movabsq $0x900000008, %rax      # imm = 0x900000008
movq %rax, -0x100(%rbp)
leaq -0x120(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq $0xa, -0xe8(%rbp)
leaq -0x121(%rbp), %rdi
movq %rdi, -0x1b0(%rbp)
callq 0x18a0
movq -0x1b0(%rbp), %rcx
movq -0xf0(%rbp), %rsi
movq -0xe8(%rbp), %rdx
leaq -0xe0(%rbp), %rdi
callq 0x1ad0
jmp 0x15a6
leaq -0x121(%rbp), %rdi
callq 0x1970
movq 0x4a1f(%rip), %rdi      # 0x5fd8
leaq 0x1a94(%rip), %rsi      # 0x3054
callq 0x10a0
jmp 0x15c7
leaq -0xe0(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x130(%rbp), %rdi
callq 0x1b80
movq %rax, -0x138(%rbp)
movq -0x130(%rbp), %rdi
callq 0x1bb0
movq %rax, -0x140(%rbp)
leaq -0x138(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x1be0
testb $0x1, %al
jne 0x1617
jmp 0x16a5
leaq -0x138(%rbp), %rdi
callq 0x1c10
movl (%rax), %eax
movl %eax, -0x144(%rbp)
movl -0x144(%rbp), %esi
movq 0x49a0(%rip), %rdi      # 0x5fd8
callq 0x1100
movq %rax, -0x1b8(%rbp)
jmp 0x1646
movq -0x1b8(%rbp), %rdi
leaq 0x1a0d(%rip), %rsi      # 0x3061
callq 0x10a0
jmp 0x165b
jmp 0x165d
leaq -0x138(%rbp), %rdi
callq 0x1c30
jmp 0x15fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x121(%rbp), %rdi
callq 0x1970
jmp 0x1889
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x1ce0
jmp 0x1889
movq 0x492c(%rip), %rdi      # 0x5fd8
movq 0x4915(%rip), %rsi      # 0x5fc8
callq 0x10d0
jmp 0x16ba
leaq -0xe0(%rbp), %rdi
callq 0x1c50
movq %rax, -0x150(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x1b80
movq %rax, -0x158(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x1bb0
movq %rax, -0x160(%rbp)
leaq -0x158(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x1be0
testb $0x1, %al
jne 0x170f
jmp 0x1796
leaq -0x158(%rbp), %rdi
callq 0x1c10
movl (%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x1777
leaq -0x170(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x1cc0
movq -0x170(%rbp), %rsi
leaq -0xe0(%rbp), %rdi
callq 0x1c80
movq %rax, -0x1c0(%rbp)
jmp 0x1759
movq -0x1c0(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rax
movq %rax, -0x158(%rbp)
jmp 0x1791
movq -0x158(%rbp), %rax
movq %rax, -0x150(%rbp)
leaq -0x158(%rbp), %rdi
callq 0x1c30
jmp 0x16e0
movq 0x483b(%rip), %rdi      # 0x5fd8
leaq 0x18e5(%rip), %rsi      # 0x3089
callq 0x10a0
jmp 0x17ab
leaq -0xe0(%rbp), %rax
movq %rax, -0x178(%rbp)
movq -0x178(%rbp), %rdi
callq 0x1b80
movq %rax, -0x180(%rbp)
movq -0x178(%rbp), %rdi
callq 0x1bb0
movq %rax, -0x188(%rbp)
leaq -0x180(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x1be0
testb $0x1, %al
jne 0x17f8
jmp 0x184c
leaq -0x180(%rbp), %rdi
callq 0x1c10
movl (%rax), %eax
movl %eax, -0x18c(%rbp)
movl -0x18c(%rbp), %esi
movq 0x47bf(%rip), %rdi      # 0x5fd8
callq 0x1100
movq %rax, -0x1c8(%rbp)
jmp 0x1827
movq -0x1c8(%rbp), %rdi
leaq 0x182c(%rip), %rsi      # 0x3061
callq 0x10a0
jmp 0x183c
jmp 0x183e
leaq -0x180(%rbp), %rdi
callq 0x1c30
jmp 0x17df
movq 0x4785(%rip), %rdi      # 0x5fd8
movq 0x476e(%rip), %rsi      # 0x5fc8
callq 0x10d0
jmp 0x1861
movl $0x0, -0x4(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x1ce0
leaq -0x20(%rbp), %rdi
callq 0x1d00
movl -0x4(%rbp), %eax
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
leaq -0x20(%rbp), %rdi
callq 0x1d00
movq -0x70(%rbp), %rdi
callq 0x1110
nopl (%rax,%rax)