// File: /workspace/github_cpp_makefile_star2_O1/iZhangHui[P]CppPrimer/CH03_StringsVectorsAndArrays/examples/char_repl.cc
// Function: main
int main()
{
	string str("some string"), orig = str;
	if (!str.empty())          // make sure there's a character to print
		cout << str[0] << endl;// print the first character in str

	if (!str.empty())       // make sure there's a character in str[0]
		// assign a new value to the first character in str
		str[0] = toupper(str[0]);
	cout << str << endl;

	str = orig; // restore str to its original value

	// equivalent code using iterators instead of subscripts
	if (str.begin() != str.end()) { // make sure str is not empty
		auto it = str.begin();  // it denotes the first character in str
		*it = toupper(*it);     // make that character uppercase
	}
	cout << str << endl;

	str = orig; // restore str to its original value

	// four wasy to capitalize first word in str:
	// 1. for loop with subscripts
	for (decltype(str.size()) index = 0; 
		 index != str.size() && !isspace(str[index]); ++index) 
    		str[index] = toupper(str[index]); // capitalize the current character
	cout << str << endl;

	str = orig; // restore str to its original value

	// 2. for loop with iterators instead of subscripts
	for (auto it = str.begin(); it != str.end() && !isspace(*it); ++it)
		*it = toupper(*it); // capitalize the current character
	cout << str << endl;

	str = orig; // restore str to its original value

	// 3. while instead of a for with subscripts
	decltype(str.size()) index = 0; // subscript to look at characters in str
	while (index != str.size() && !isspace(str[index])) {
		str[index] = toupper(str[index]);  // capitalize the current character
		++index;        // advance the index to get the next character
	}
	cout << str << endl;

	// 4. while loop with iterators
	auto beg = str.begin();
	while (beg != str.end() && !isspace(*beg)) {
		*beg = toupper(*beg);
		++beg;
	}
	cout << str << endl;

	str = orig; // restore str to its original value

	// range for loop to process every character
	// first a loop to print the characters in str one character to a line
	for (auto c : str)      // for every char in str
		cout << c << endl;  // print the current character followed by a newline

	// next change every character in str
	for (auto &c : str)  // note: c is a reference 
		c = '*';         // assign a new value to the underlying char in str
	cout << str << endl;
	
	str = orig;  // restore str to its original value

	// equivalent code using traditional for loops
	// first print the characters in str
	for (decltype(str.size()) ix = 0; ix != str.size(); ++ix)
		cout << str[ix] << endl;  // print current character followd by a newline

	// next change every character in str
	for (decltype(str.size()) ix = 0; ix != str.size(); ++ix)
		str[ix] = '*';  // assigns a new value to the character in str
	cout << str << endl;

	str = orig;  // restore str to its original value

	// equivalent code using traditional for loops and iterators
	// first print the characters in str
	for (auto beg = str.begin(); beg != str.end(); ++beg)
		cout << *beg << endl;  // print current character followd by a newline

	// next change every character in str
	for (auto beg = str.begin(); beg != str.end(); ++beg)
		*beg = '*';  // assigns a new value to the character in str
	cout << str << endl;
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0xd88(%rip), %rsi       # 0x2004
leaq 0xd8c(%rip), %rdx       # 0x200f
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x199e
leaq -0x60(%rbp), %rax
movq %rax, -0x10(%rax)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq -0x70(%rbp), %rdi
callq 0x1904
cmpq $0x0, -0x48(%rbp)
je 0x12f8
movq -0x50(%rbp), %rax
movb (%rax), %al
leaq -0x29(%rbp), %rsi
movb %al, (%rsi)
movq 0x2d14(%rip), %rdi      # 0x3fd8
movl $0x1, %edx
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
cmpq $0x0, -0x48(%rbp)
je 0x130d
movq -0x50(%rbp), %rbx
movsbl (%rbx), %edi
callq 0x1110
movb %al, (%rbx)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq 0x2cbc(%rip), %rdi      # 0x3fd8
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
cmpq $0x0, -0x48(%rbp)
je 0x136d
movq -0x50(%rbp), %rbx
movsbl (%rbx), %edi
callq 0x1110
movb %al, (%rbx)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq 0x2c5c(%rip), %rdi      # 0x3fd8
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
movq -0x48(%rbp), %rbx
testq %rbx, %rbx
je 0x1410
movq -0x50(%rbp), %r15
movsbl (%r15), %r14d
movl %r14d, %edi
callq 0x1040
testl %eax, %eax
jne 0x1410
movl $0x1, %r12d
xorl %r13d, %r13d
movl %r14d, %edi
callq 0x1110
movb %al, (%r15)
movq -0x48(%rbp), %rbx
cmpq %rbx, %r12
je 0x1410
incq %r13
movq -0x50(%rbp), %r15
movsbl (%r15,%r12), %r14d
addq %r13, %r15
movl %r14d, %edi
callq 0x1040
incq %r12
testl %eax, %eax
je 0x13de
movq -0x50(%rbp), %rsi
movq 0x2bbd(%rip), %rdi      # 0x3fd8
movq %rbx, %rdx
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r14
testq %r14, %r14
je 0x14af
movsbl (%rbx), %r15d
movl %r15d, %edi
callq 0x1040
testl %eax, %eax
jne 0x14af
incq %rbx
movq %rbx, %r12
movl %r15d, %edi
callq 0x1110
movb %al, -0x1(%r12)
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r14
leaq (%rbx,%r14), %rax
cmpq %rax, %r12
je 0x14af
movsbl (%r12), %r15d
movl %r15d, %edi
callq 0x1040
incq %r12
testl %eax, %eax
je 0x147d
movq 0x2b22(%rip), %rdi      # 0x3fd8
movq %rbx, %rsi
movq %r14, %rdx
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
movq -0x48(%rbp), %rbx
testq %rbx, %rbx
je 0x1550
movq -0x50(%rbp), %r15
movsbl (%r15), %r14d
movl %r14d, %edi
callq 0x1040
testl %eax, %eax
jne 0x1550
movl $0x1, %r12d
xorl %r13d, %r13d
movl %r14d, %edi
callq 0x1110
movb %al, (%r15)
movq -0x48(%rbp), %rbx
cmpq %rbx, %r12
je 0x1550
incq %r13
movq -0x50(%rbp), %r15
movsbl (%r15,%r12), %r14d
addq %r13, %r15
movl %r14d, %edi
callq 0x1040
incq %r12
testl %eax, %eax
je 0x151e
movq -0x50(%rbp), %rsi
movq 0x2a7d(%rip), %rdi      # 0x3fd8
movq %rbx, %rdx
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r14
testq %r14, %r14
je 0x15e2
movsbl (%rbx), %r15d
movl %r15d, %edi
callq 0x1040
testl %eax, %eax
jne 0x15e2
incq %rbx
movq %rbx, %r12
movl %r15d, %edi
callq 0x1110
movb %al, -0x1(%r12)
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r14
leaq (%rbx,%r14), %rax
cmpq %rax, %r12
je 0x15e2
movsbl (%r12), %r15d
movl %r15d, %edi
callq 0x1040
incq %r12
testl %eax, %eax
je 0x15b0
movq 0x29ef(%rip), %rdi      # 0x3fd8
movq %rbx, %rsi
movq %r14, %rdx
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
movq -0x48(%rbp), %r12
testq %r12, %r12
je 0x168c
movq -0x50(%rbp), %r13
xorl %ebx, %ebx
leaq -0x29(%rbp), %r14
movb (%r13,%rbx), %al
movb %al, -0x29(%rbp)
movl $0x1, %edx
movq 0x2986(%rip), %rdi      # 0x3fd8
movq %r14, %rsi
callq 0x10d0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
incq %rbx
cmpq %rbx, %r12
jne 0x163e
movq -0x48(%rbp), %rdx
testq %rdx, %rdx
je 0x16a3
movq -0x50(%rbp), %rdi
movl $0x2a, %esi
callq 0x1060
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq 0x2926(%rip), %rdi      # 0x3fd8
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
cmpq $0x0, -0x48(%rbp)
je 0x1751
xorl %r12d, %r12d
movq 0x28d9(%rip), %rbx      # 0x3fd8
leaq -0x29(%rbp), %r14
movq -0x50(%rbp), %rax
movb (%rax,%r12), %al
movb %al, -0x29(%rbp)
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10d0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
incq %r12
cmpq -0x48(%rbp), %r12
jne 0x1703
movq -0x48(%rbp), %rdx
testq %rdx, %rdx
je 0x1770
xorl %eax, %eax
movq -0x50(%rbp), %rcx
movb $0x2a, (%rcx,%rax)
incq %rax
movq -0x48(%rbp), %rdx
cmpq %rdx, %rax
jne 0x175c
movq -0x50(%rbp), %rsi
movq 0x285d(%rip), %rdi      # 0x3fd8
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
movq -0x50(%rbp), %r12
cmpq $0x0, -0x48(%rbp)
je 0x1823
movq 0x280f(%rip), %rbx      # 0x3fd8
leaq -0x29(%rbp), %r14
movb (%r12), %al
movb %al, -0x29(%rbp)
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10d0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
incq %r12
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
addq %rax, %rcx
cmpq %rcx, %r12
jne 0x17cd
jmp 0x182e
movq %r12, %rax
jmp 0x182e
movb $0x2a, (%rax)
incq %rax
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq (%rsi,%rdx), %rcx
cmpq %rcx, %rax
jne 0x1828
movq 0x2792(%rip), %rdi      # 0x3fd8
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x188e
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x10c0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x18a7
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x10c0
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x18e3
jmp 0x18c7
jmp 0x18c7
jmp 0x18c7
jmp 0x18c7
jmp 0x18c7
movq %rax, %rbx
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x18e3
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x10c0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x18fc
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x10c0
movq %rbx, %rdi
callq 0x10f0