// File: /workspace/github_cpp_makefile_star2_O2/iZhangHui[P]CppPrimer/CH03_StringsVectorsAndArrays/examples/vecSubs.cc
// Function: main
int main()
{
	vector<int> v = {0,1,2,3,4,5,6,7,8,9};
	auto sz = v.size(); 
	decltype(sz) i = 0;
	// duplicate contents of v onto the back of v
	while (i != sz) {
		v.push_back(*v.begin() + i);
		++i;
	}
	// prints 0...9 0...9
	for (auto it : v)
		cout << it << " ";
	cout << endl;
	
	// alternative way to stop when we get to the original last element
	vector<int> alt_v = {0,1,2,3,4,5,6,7,8,9}; // vector with values 0...9
	for (decltype(alt_v.size()) i = 0, sz = alt_v.size(); i != sz; ++i)
		alt_v.push_back(alt_v[i]);

	// prints 0...9 0...9
	for (auto it : alt_v)
		cout << it << " ";
	cout << endl;

	vector<int> v2 = {0,1,2,3,4,5,6,7,8,9}; // vector with values 0 ... 9
	decltype(v2.size()) ix = 0;   // we'll use ix to index the vector

	// set the elements with values less than 5 to 0
	while (ix != v2.size() && v2[ix] < 5) {
		v2[ix] = 0; // changes the value of the element in v
		++ix;       // increment the index so the next iteration fetches the next element
	}

	// print the elements using subscripts
	for (unsigned i = 0; i != v2.size(); ++i)
		cout << v2[i] << " ";
	cout << endl;

	// equivalent but using iterators
	vector<int> alt_v2 = {0,1,2,3,4,5,6,7,8,9}; // vector with values 0...9
	// set the elements to 0 up to the first one that is 5 or greater
	auto it = alt_v2.begin();
	while (it != alt_v2.end() && *it < 5) {
		*it = 0;   // changes the value of the element in alt_v2
		++it;      // advance the iterator to denote the next element
	}
	for (auto it = alt_v2.begin(); // it denotes first element in alt_v2
	          it != alt_v2.end();  // so long as it denotes an element
	          ++it)          // increment the iterator to next element
		cout << *it << " ";  // print element denoted by it from alt_v2
	cout << endl;

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movups 0xdfd(%rip), %xmm0      # 0x2064
leaq -0xc0(%rbp), %rbx
movaps %xmm0, 0x10(%rbx)
movups 0xddb(%rip), %xmm0      # 0x2054
movaps %xmm0, (%rbx)
movabsq $0x900000008, %r12      # imm = 0x900000008
movq %r12, 0x20(%rbx)
leaq -0x90(%rbp), %r14
pushq $0xa
popq %rdx
leaq -0x78(%rbp), %rcx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x157a
movq 0x8(%r14), %r15
subq (%r14), %r15
sarq $0x2, %r15
xorl %r13d, %r13d
cmpq %r13, %r15
je 0x12d8
movq -0x90(%rbp), %rax
movl (%rax), %eax
addl %r13d, %eax
movl %eax, -0xc0(%rbp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x16e6
incq %r13
jmp 0x12b1
movq -0x90(%rbp), %r15
movq -0x88(%rbp), %r13
movq 0x2ceb(%rip), %rbx      # 0x3fd8
leaq 0xd10(%rip), %r14       # 0x2004
cmpq %r13, %r15
je 0x1315
movl (%r15), %esi
movq %rbx, %rdi
callq 0x10f0
movq %rax, %rdi
movq %r14, %rsi
callq 0x10a0
addq $0x4, %r15
jmp 0x12f4
movq 0x2cbc(%rip), %rdi      # 0x3fd8
callq 0x1060
leaq -0xc0(%rbp), %rsi
movups 0xd35(%rip), %xmm0      # 0x2064
movaps %xmm0, 0x10(%rsi)
movups 0xd1a(%rip), %xmm0      # 0x2054
movaps %xmm0, (%rsi)
movq %r12, 0x20(%rsi)
leaq -0x78(%rbp), %rdi
pushq $0xa
popq %rdx
leaq -0x48(%rbp), %rcx
callq 0x157a
leaq -0x78(%rbp), %r14
movq 0x8(%r14), %r15
subq (%r14), %r15
sarq $0x2, %r15
xorl %r13d, %r13d
movq -0x78(%rbp), %rbx
subq $0x1, %r15
jb 0x1381
addq %r13, %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x15b0
addq $0x4, %r13
jmp 0x1363
movq -0x70(%rbp), %r13
movq 0x2c4c(%rip), %r14      # 0x3fd8
leaq 0xc71(%rip), %r15       # 0x2004
cmpq %r13, %rbx
je 0x13b3
movl (%rbx), %esi
movq %r14, %rdi
callq 0x10f0
movq %rax, %rdi
movq %r15, %rsi
callq 0x10a0
addq $0x4, %rbx
jmp 0x1393
movq 0x2c1e(%rip), %rdi      # 0x3fd8
callq 0x1060
leaq -0xc0(%rbp), %rsi
movups 0xc97(%rip), %xmm0      # 0x2064
movaps %xmm0, 0x10(%rsi)
movups 0xc7c(%rip), %xmm0      # 0x2054
movaps %xmm0, (%rsi)
movq %r12, 0x20(%rsi)
leaq -0x48(%rbp), %rdi
pushq $0xa
popq %rdx
leaq -0x60(%rbp), %rcx
callq 0x157a
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x1417
cmpl $0x5, (%rax,%rsi,4)
jge 0x1417
andl $0x0, (%rax,%rsi,4)
incq %rsi
jmp 0x1403
xorl %r15d, %r15d
movq 0x2bb7(%rip), %rbx      # 0x3fd8
leaq 0xbdc(%rip), %r14       # 0x2004
movl %r15d, %r15d
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r15, %rcx
je 0x145b
movl (%rax,%r15,4), %esi
movq %rbx, %rdi
callq 0x10f0
movq %rax, %rdi
movq %r14, %rsi
callq 0x10a0
incl %r15d
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
jmp 0x1428
movq 0x2b76(%rip), %rdi      # 0x3fd8
callq 0x1060
leaq -0xc0(%rbp), %rsi
movups 0xbef(%rip), %xmm0      # 0x2064
movaps %xmm0, 0x10(%rsi)
movups 0xbd4(%rip), %xmm0      # 0x2054
movaps %xmm0, (%rsi)
movq %r12, 0x20(%rsi)
leaq -0x60(%rbp), %rdi
pushq $0xa
popq %rdx
leaq -0x29(%rbp), %rcx
callq 0x157a
movq -0x60(%rbp), %rbx
movq -0x58(%rbp), %rax
movq %rbx, %rcx
cmpq %rax, %rcx
je 0x14da
cmpl $0x5, (%rcx)
jge 0x14da
andl $0x0, (%rcx)
addq $0x4, %rcx
jmp 0x14a2
movl (%rbx), %esi
movq 0x2b1a(%rip), %rdi      # 0x3fd8
callq 0x10f0
leaq 0xb3a(%rip), %rsi       # 0x2004
movq %rax, %rdi
callq 0x10a0
addq $0x4, %rbx
movq -0x58(%rbp), %rax
cmpq %rax, %rbx
jne 0x14b5
movq 0x2af2(%rip), %rdi      # 0x3fd8
callq 0x1060
leaq -0x60(%rbp), %rdi
callq 0x1632
leaq -0x48(%rbp), %rdi
callq 0x1632
leaq -0x78(%rbp), %rdi
callq 0x1632
leaq -0x90(%rbp), %rdi
callq 0x1632
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1538
jmp 0x1546
jmp 0x1546
jmp 0x1554
jmp 0x1554
jmp 0x1562
jmp 0x1562
jmp 0x1554
jmp 0x1562
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x1632
jmp 0x1549
movq %rax, %rbx
leaq -0x48(%rbp), %rdi
callq 0x1632
jmp 0x1557
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x1632
jmp 0x1565
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x1632
movq %rbx, %rdi
callq 0x1100
nop