// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x40, %rsp
leaq 0xdc2(%rip), %rsi       # 0x2004
leaq -0x40(%rbp), %rbx
leaq -0x60(%rbp), %rdx
movq %rbx, %rdi
callq 0x1040
leaq -0x60(%rbp), %rdi
movq %rbx, %rsi
callq 0x10b0
cmpq $0x0, -0x38(%rbp)
je 0x1295
movq -0x40(%rbp), %rax
movsbl (%rax), %esi
movq 0x2d65(%rip), %rdi      # 0x3fd8
callq 0x10a0
movq %rax, %rdi
callq 0x1060
cmpq $0x0, -0x38(%rbp)
je 0x1295
movq -0x40(%rbp), %rbx
movsbl (%rbx), %edi
callq 0x10e0
movb %al, (%rbx)
movq 0x2d3c(%rip), %rdi      # 0x3fd8
leaq -0x40(%rbp), %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1060
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1070
cmpq $0x0, -0x38(%rbp)
je 0x12cf
movq -0x40(%rbp), %rbx
movsbl (%rbx), %edi
callq 0x10e0
movb %al, (%rbx)
movq 0x2d02(%rip), %rdi      # 0x3fd8
leaq -0x40(%rbp), %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1060
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1070
xorl %r14d, %r14d
cmpq -0x38(%rbp), %r14
je 0x1321
movq -0x40(%rbp), %r15
movsbl (%r15,%r14), %ebx
movl %ebx, %edi
callq 0x1030
testl %eax, %eax
jne 0x1321
movl %ebx, %edi
callq 0x10e0
movb %al, (%r15,%r14)
incq %r14
jmp 0x12f7
movq 0x2cb0(%rip), %rdi      # 0x3fd8
leaq -0x40(%rbp), %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1060
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1070
movq -0x40(%rbp), %rax
movq %rax, %r14
addq -0x38(%rbp), %rax
cmpq %rax, %r14
je 0x1378
movsbl (%r14), %ebx
movl %ebx, %edi
callq 0x1030
testl %eax, %eax
jne 0x1378
movl %ebx, %edi
callq 0x10e0
movb %al, (%r14)
incq %r14
movq -0x40(%rbp), %rax
jmp 0x134d
movq 0x2c59(%rip), %rdi      # 0x3fd8
leaq -0x40(%rbp), %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1060
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1070
xorl %r14d, %r14d
cmpq -0x38(%rbp), %r14
je 0x13ca
movq -0x40(%rbp), %r15
movsbl (%r15,%r14), %ebx
movl %ebx, %edi
callq 0x1030
testl %eax, %eax
jne 0x13ca
movl %ebx, %edi
callq 0x10e0
movb %al, (%r15,%r14)
incq %r14
jmp 0x13a0
movq 0x2c07(%rip), %rdi      # 0x3fd8
leaq -0x40(%rbp), %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1060
movq -0x40(%rbp), %rax
movq %rax, %r14
addq -0x38(%rbp), %rax
cmpq %rax, %r14
je 0x1414
movsbl (%r14), %ebx
movl %ebx, %edi
callq 0x1030
testl %eax, %eax
jne 0x1414
movl %ebx, %edi
callq 0x10e0
movb %al, (%r14)
incq %r14
movq -0x40(%rbp), %rax
jmp 0x13e9
movq 0x2bbd(%rip), %rdi      # 0x3fd8
leaq -0x40(%rbp), %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1060
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1070
movq -0x40(%rbp), %r14
movq -0x38(%rbp), %r15
xorl %r12d, %r12d
movq 0x2b8d(%rip), %rbx      # 0x3fd8
cmpq %r12, %r15
je 0x146a
movsbl (%r14,%r12), %esi
movq %rbx, %rdi
callq 0x10a0
movq %rax, %rdi
callq 0x1060
incq %r12
jmp 0x144b
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x1482
movb $0x2a, (%rax,%rdx)
incq %rdx
jmp 0x1474
movq 0x2b4f(%rip), %rdi      # 0x3fd8
leaq -0x40(%rbp), %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1060
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1070
xorl %r14d, %r14d
movq 0x2b27(%rip), %rbx      # 0x3fd8
cmpq -0x38(%rbp), %r14
je 0x14d5
movq -0x40(%rbp), %rax
movsbl (%rax,%r14), %esi
movq %rbx, %rdi
callq 0x10a0
movq %rax, %rdi
callq 0x1060
incq %r14
jmp 0x14b1
xorl %eax, %eax
cmpq %r14, %rax
je 0x14ed
movq -0x40(%rbp), %rcx
movb $0x2a, (%rcx,%rax)
incq %rax
movq -0x38(%rbp), %r14
jmp 0x14d7
movq 0x2ae4(%rip), %rdi      # 0x3fd8
leaq -0x40(%rbp), %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1060
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1070
movq -0x40(%rbp), %rax
movq 0x2abb(%rip), %rbx      # 0x3fd8
movq %rax, %r14
movq -0x38(%rbp), %rdx
leaq (%rax,%rdx), %rcx
cmpq %rcx, %r14
je 0x154a
movsbl (%r14), %esi
movq %rbx, %rdi
callq 0x10a0
movq %rax, %rdi
callq 0x1060
incq %r14
movq -0x40(%rbp), %rax
jmp 0x1520
movq %rax, %rcx
addq %rax, %rdx
cmpq %rdx, %rcx
je 0x1565
movb $0x2a, (%rcx)
incq %rcx
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rdx
jmp 0x154d
movq 0x2a6c(%rip), %rdi      # 0x3fd8
leaq -0x40(%rbp), %rsi
callq 0x1090
movq %rax, %rdi
callq 0x1060
leaq -0x60(%rbp), %rdi
callq 0x1050
leaq -0x40(%rbp), %rdi
callq 0x1050
xorl %eax, %eax
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x15b9
jmp 0x15ad
jmp 0x15ad
jmp 0x15ad
jmp 0x15ad
jmp 0x15ad
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x1050
leaq -0x40(%rbp), %rdi
callq 0x1050
movq %rbx, %rdi
callq 0x10d0