// File: iZhangHui[P]CppPrimer/CH09_SequentialContainers/examples/str_assignOps.cc
// Function: main
int main()
{
    string s = "some string", s2 = "some other string";
    // equivalent ways to insert all the characters from s2 at beginning of s
    // insert iterator range before s.begin()
    s.insert(s.begin(), s2.begin(), s2.end());
    cout << "insert iterators version:        " << s << endl;

    s = "some string";
    s.insert(0, s2); // insert a copy of s2 before position 0 in s
    cout << "insert string at given position: " << s << endl;

    s = "some string";
    // insert s2.size() characters from s2 starting at s2[0] before s[0]
    s.insert(0, s2, 0, s2.size());
    cout << "insert positional version:       " << s << endl;


    // s = "";  // s is now empty
    s = "x";
    vector<char> c_vec(1, 'a');
    // insert characters from c_vec into s
    s.insert(s.begin(), c_vec.begin(), c_vec.end());
    s.insert(s.size(), 5, '!'); // add five exclamation points at the end of s
    cout << s << endl;

    s.erase(s.size() - 5, 5);   // erase the last five characters from s
    cout << s << endl;

    s = "";  // s is now empty
    const char *cp = "Stately, plump Buck";
    s.assign(cp, 7);            // s == "Stately"
    cout << s << endl;
    s.insert(s.size(), cp + 7); // s == "Stately, plump Buck"
    cout << s << endl;

    s = "C++ Primer";  // reset s and s2
    s2 = s;            // to "C++ Primer"
    s.insert(s.size(), " 4th Ed."); // s == "C++ Primer 4th Ed."
    s2.append(" 4th Ed."); // equivalent: appends " 4th Ed." to s2;
    cout << s << " " << s2 << endl;

    // two ways to replace "4th" by "5th"
    // 1. insert and erase
    s.erase(11, 3);                 // s == "C++ Primer Ed."
    s.insert(11, "5th");            // s == "C++ Primer 5th Ed."

    // 2. use replace
    // erase three characters starting at position 11
    //  and then insert "5th"
    s2.replace(11, 3, "5th"); // equivalent: s == s2

    cout << s << " " << s2 << endl;

    // two ways to replace "5th" by "Fifth"
    // 1. use replace if we know where the string we want to replace is
    s.replace(11, 3, "Fifth"); // s == "C++ Primer Fifth Ed."

    // 2. call find first to get position from which to replace
    auto pos = s2.find("5th");
    if (pos != string::npos)
        s2.replace(pos, 3, "Fifth");
    else
        cout << "something's wrong, s2 is: " << s2 << endl;
    cout << s << " " << s2 << endl;

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movl $0x0, -0x4(%rbp)
leaq -0x29(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x22d0
movq -0xf0(%rbp), %rdx
leaq 0x1b54(%rip), %rsi      # 0x4004
leaq -0x28(%rbp), %rdi
callq 0x2250
jmp 0x24bb
leaq -0x29(%rbp), %rdi
callq 0x21f0
leaq -0x61(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x22d0
movq -0xf8(%rbp), %rdx
leaq 0x1b2e(%rip), %rsi      # 0x4010
leaq -0x60(%rbp), %rdi
callq 0x2250
jmp 0x24ed
leaq -0x61(%rbp), %rdi
callq 0x21f0
leaq -0x28(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x2170
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x2d00
leaq -0x60(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x2170
movq -0x110(%rbp), %rdi
movq %rax, -0x80(%rbp)
callq 0x20e0
movq -0x108(%rbp), %rdi
movq %rax, -0x88(%rbp)
movq -0x70(%rbp), %rsi
movq -0x80(%rbp), %rdx
movq -0x88(%rbp), %rcx
callq 0x2c60
movq %rax, -0x100(%rbp)
jmp 0x2562
movq -0x100(%rbp), %rax
movq %rax, -0x90(%rbp)
movq 0x3a61(%rip), %rdi      # 0x5fd8
leaq 0x1aa4(%rip), %rsi      # 0x4022
callq 0x21a0
movq %rax, -0x118(%rbp)
jmp 0x258c
movq -0x118(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x2190
movq %rax, -0x120(%rbp)
jmp 0x25a5
movq -0x120(%rbp), %rdi
movq 0x3a15(%rip), %rsi      # 0x5fc8
callq 0x21d0
jmp 0x25ba
leaq 0x1a43(%rip), %rsi      # 0x4004
leaq -0x28(%rbp), %rdi
callq 0x2240
jmp 0x25cc
xorl %eax, %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0x22a0
jmp 0x25df
movq 0x39f2(%rip), %rdi      # 0x5fd8
leaq 0x1a57(%rip), %rsi      # 0x4044
callq 0x21a0
movq %rax, -0x128(%rbp)
jmp 0x25fb
movq -0x128(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x2190
movq %rax, -0x130(%rbp)
jmp 0x2614
movq -0x130(%rbp), %rdi
movq 0x39a6(%rip), %rsi      # 0x5fc8
callq 0x21d0
jmp 0x2629
leaq 0x19d4(%rip), %rsi      # 0x4004
leaq -0x28(%rbp), %rdi
callq 0x2240
jmp 0x263b
leaq -0x60(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x2120
movq -0x138(%rbp), %rdx
movq %rax, %r8
xorl %eax, %eax
movl %eax, %ecx
leaq -0x28(%rbp), %rdi
movq %rcx, %rsi
callq 0x2200
jmp 0x2667
movq 0x396a(%rip), %rdi      # 0x5fd8
leaq 0x19f1(%rip), %rsi      # 0x4066
callq 0x21a0
movq %rax, -0x140(%rbp)
jmp 0x2683
movq -0x140(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x2190
movq %rax, -0x148(%rbp)
jmp 0x269c
movq -0x148(%rbp), %rdi
movq 0x391e(%rip), %rsi      # 0x5fc8
callq 0x21d0
jmp 0x26b1
leaq 0x19d0(%rip), %rsi      # 0x4088
leaq -0x28(%rbp), %rdi
callq 0x2240
jmp 0x26c3
movb $0x61, -0xa9(%rbp)
leaq -0xaa(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x22d0
movq -0x150(%rbp), %rcx
leaq -0xa8(%rbp), %rdi
movl $0x1, %esi
leaq -0xa9(%rbp), %rdx
callq 0x2d40
jmp 0x26fe
leaq -0xaa(%rbp), %rdi
callq 0x21f0
leaq -0x28(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x2170
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x2d00
leaq -0xa8(%rbp), %rdi
movq %rdi, -0x168(%rbp)
callq 0x2e60
movq -0x168(%rbp), %rdi
movq %rax, -0xc8(%rbp)
callq 0x2e90
movq -0x160(%rbp), %rdi
movq %rax, -0xd0(%rbp)
movq -0xb8(%rbp), %rsi
movq -0xc8(%rbp), %rdx
movq -0xd0(%rbp), %rcx
callq 0x2dc0
movq %rax, -0x158(%rbp)
jmp 0x278b
movq -0x158(%rbp), %rax
movq %rax, -0xd8(%rbp)
leaq -0x28(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x2120
movq -0x170(%rbp), %rdi
movq %rax, %rsi
movl $0x5, %edx
movl $0x21, %ecx
callq 0x2330
jmp 0x27c4
movq 0x380d(%rip), %rdi      # 0x5fd8
leaq -0x28(%rbp), %rsi
callq 0x2190
movq %rax, -0x178(%rbp)
jmp 0x27dd
movq -0x178(%rbp), %rdi
movq 0x37dd(%rip), %rsi      # 0x5fc8
callq 0x21d0
jmp 0x27f2
leaq -0x28(%rbp), %rdi
movq %rdi, -0x180(%rbp)
callq 0x2120
movq -0x180(%rbp), %rdi
movq %rax, %rsi
addq $-0x5, %rsi
movl $0x5, %edx
callq 0x2050
jmp 0x281c
movq 0x37b5(%rip), %rdi      # 0x5fd8
leaq -0x28(%rbp), %rsi
callq 0x2190
movq %rax, -0x188(%rbp)
jmp 0x2835
movq -0x188(%rbp), %rdi
movq 0x3785(%rip), %rsi      # 0x5fc8
callq 0x21d0
jmp 0x284a
leaq 0x17f2(%rip), %rsi      # 0x4043
leaq -0x28(%rbp), %rdi
callq 0x2240
jmp 0x285c
leaq 0x1827(%rip), %rax      # 0x408a
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rsi
leaq -0x28(%rbp), %rdi
movl $0x7, %edx
callq 0x2230
jmp 0x2881
movq 0x3750(%rip), %rdi      # 0x5fd8
leaq -0x28(%rbp), %rsi
callq 0x2190
movq %rax, -0x190(%rbp)
jmp 0x289a
movq -0x190(%rbp), %rdi
movq 0x3720(%rip), %rsi      # 0x5fc8
callq 0x21d0
jmp 0x28af
leaq -0x28(%rbp), %rdi
movq %rdi, -0x198(%rbp)
callq 0x2120
movq -0x198(%rbp), %rdi
movq %rax, %rsi
movq -0xe0(%rbp), %rdx
addq $0x7, %rdx
callq 0x2060
jmp 0x28db
movq 0x36f6(%rip), %rdi      # 0x5fd8
leaq -0x28(%rbp), %rsi
callq 0x2190
movq %rax, -0x1a0(%rbp)
jmp 0x28f4
movq -0x1a0(%rbp), %rdi
movq 0x36c6(%rip), %rsi      # 0x5fc8
callq 0x21d0
jmp 0x2909
leaq 0x178e(%rip), %rsi      # 0x409e
leaq -0x28(%rbp), %rdi
callq 0x2240
jmp 0x291b
leaq -0x60(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x2030
jmp 0x292a
leaq -0x28(%rbp), %rdi
movq %rdi, -0x1a8(%rbp)
callq 0x2120
movq -0x1a8(%rbp), %rdi
movq %rax, %rsi
leaq 0x175e(%rip), %rdx      # 0x40a9
callq 0x2060
jmp 0x2952
leaq 0x1750(%rip), %rsi      # 0x40a9
leaq -0x60(%rbp), %rdi
callq 0x2300
jmp 0x2964
movq 0x366d(%rip), %rdi      # 0x5fd8
leaq -0x28(%rbp), %rsi
callq 0x2190
movq %rax, -0x1b0(%rbp)
jmp 0x297d
movq -0x1b0(%rbp), %rdi
leaq 0x16b7(%rip), %rsi      # 0x4042
callq 0x21a0
movq %rax, -0x1b8(%rbp)
jmp 0x2999
movq -0x1b8(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x2190
movq %rax, -0x1c0(%rbp)
jmp 0x29b2
movq -0x1c0(%rbp), %rdi
movq 0x3608(%rip), %rsi      # 0x5fc8
callq 0x21d0
jmp 0x29c7
leaq -0x28(%rbp), %rdi
movl $0xb, %esi
movl $0x3, %edx
callq 0x2050
jmp 0x29dc
leaq 0x16cf(%rip), %rdx      # 0x40b2
leaq -0x28(%rbp), %rdi
movl $0xb, %esi
callq 0x2060
jmp 0x29f3
leaq 0x16b8(%rip), %rcx      # 0x40b2
leaq -0x60(%rbp), %rdi
movl $0xb, %esi
movl $0x3, %edx
callq 0x20f0
jmp 0x2a0f
movq 0x35c2(%rip), %rdi      # 0x5fd8
leaq -0x28(%rbp), %rsi
callq 0x2190
movq %rax, -0x1c8(%rbp)
jmp 0x2a28
movq -0x1c8(%rbp), %rdi
leaq 0x160c(%rip), %rsi      # 0x4042
callq 0x21a0
movq %rax, -0x1d0(%rbp)
jmp 0x2a44
movq -0x1d0(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x2190
movq %rax, -0x1d8(%rbp)
jmp 0x2a5d
movq -0x1d8(%rbp), %rdi
movq 0x355d(%rip), %rsi      # 0x5fc8
callq 0x21d0
jmp 0x2a72
leaq 0x163d(%rip), %rcx      # 0x40b6
leaq -0x28(%rbp), %rdi
movl $0xb, %esi
movl $0x3, %edx
callq 0x20f0
jmp 0x2a8e
leaq -0x60(%rbp), %rdi
leaq 0x1619(%rip), %rsi      # 0x40b2
xorl %eax, %eax
movl %eax, %edx
callq 0x2320
movq %rax, -0xe8(%rbp)
cmpq $-0x1, -0xe8(%rbp)
je 0x2b59
movq -0xe8(%rbp), %rsi
leaq 0x15f1(%rip), %rcx      # 0x40b6
leaq -0x60(%rbp), %rdi
movl $0x3, %edx
callq 0x20f0
jmp 0x2ad5
jmp 0x2ba5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x29(%rbp), %rdi
callq 0x21f0
jmp 0x2c4b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x61(%rbp), %rdi
callq 0x21f0
jmp 0x2c42
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x2c39
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xaa(%rbp), %rdi
callq 0x21f0
jmp 0x2c39
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x2ec0
jmp 0x2c39
movq 0x3478(%rip), %rdi      # 0x5fd8
leaq 0x1555(%rip), %rsi      # 0x40bc
callq 0x21a0
movq %rax, -0x1e0(%rbp)
jmp 0x2b75
movq -0x1e0(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x2190
movq %rax, -0x1e8(%rbp)
jmp 0x2b8e
movq -0x1e8(%rbp), %rdi
movq 0x342c(%rip), %rsi      # 0x5fc8
callq 0x21d0
jmp 0x2ba3
jmp 0x2ba5
movq 0x342c(%rip), %rdi      # 0x5fd8
leaq -0x28(%rbp), %rsi
callq 0x2190
movq %rax, -0x1f0(%rbp)
jmp 0x2bbe
movq -0x1f0(%rbp), %rdi
leaq 0x1476(%rip), %rsi      # 0x4042
callq 0x21a0
movq %rax, -0x1f8(%rbp)
jmp 0x2bda
movq -0x1f8(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x2190
movq %rax, -0x200(%rbp)
jmp 0x2bf3
movq -0x200(%rbp), %rdi
movq 0x33c7(%rip), %rsi      # 0x5fc8
callq 0x21d0
jmp 0x2c08
movl $0x0, -0x4(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x2ec0
leaq -0x60(%rbp), %rdi
callq 0x2100
leaq -0x28(%rbp), %rdi
callq 0x2100
movl -0x4(%rbp), %eax
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
leaq -0x60(%rbp), %rdi
callq 0x2100
leaq -0x28(%rbp), %rdi
callq 0x2100
movq -0x38(%rbp), %rdi
callq 0x22c0
nopw %cs:(%rax,%rax)
nop