// File: 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
subq $0x2b0, %rsp            # imm = 0x2B0
movl $0x0, -0x4(%rbp)
movabsq $0x900000008, %rax      # imm = 0x900000008
movq %rax, -0x40(%rbp)
movups 0x1d9d(%rip), %xmm0     # 0x3014
movaps %xmm0, -0x50(%rbp)
movups 0x1d82(%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, -0x240(%rbp)
callq 0x1a50
movq -0x240(%rbp), %rcx
movq -0x30(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x20(%rbp), %rdi
callq 0x1a70
jmp 0x12c0
leaq -0x61(%rbp), %rdi
callq 0x1af0
leaq -0x20(%rbp), %rdi
callq 0x1b10
movq %rax, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x80(%rbp), %rax
je 0x1379
leaq -0x20(%rbp), %rdi
movq %rdi, -0x248(%rbp)
callq 0x1b60
movq %rax, -0x98(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x1b90
movq -0x248(%rbp), %rdi
movl (%rax), %eax
movl -0x88(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x8c(%rbp)
leaq -0x8c(%rbp), %rsi
callq 0x1b30
jmp 0x133a
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x12e1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x61(%rbp), %rdi
callq 0x1af0
jmp 0x1a38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x1a2f
leaq -0x20(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x1b60
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x1bb0
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x1be0
testb $0x1, %al
jne 0x13c3
jmp 0x1417
leaq -0xa8(%rbp), %rdi
callq 0x1b90
movl (%rax), %eax
movl %eax, -0xb4(%rbp)
movl -0xb4(%rbp), %esi
movq 0x4bf4(%rip), %rdi      # 0x5fd8
callq 0x10f0
movq %rax, -0x250(%rbp)
jmp 0x13f2
movq -0x250(%rbp), %rdi
leaq 0x1ca4(%rip), %rsi      # 0x30a4
callq 0x1090
jmp 0x1407
jmp 0x1409
leaq -0xa8(%rbp), %rdi
callq 0x1c20
jmp 0x13aa
movq 0x4bba(%rip), %rdi      # 0x5fd8
movq 0x4ba3(%rip), %rsi      # 0x5fc8
callq 0x10c0
jmp 0x142c
movups 0x1c09(%rip), %xmm0     # 0x303c
movaps %xmm0, -0x100(%rbp)
movups 0x1beb(%rip), %xmm0     # 0x302c
movaps %xmm0, -0x110(%rbp)
movabsq $0x900000008, %rax      # imm = 0x900000008
movq %rax, -0xf0(%rbp)
leaq -0x110(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq $0xa, -0xd8(%rbp)
leaq -0x111(%rbp), %rdi
movq %rdi, -0x258(%rbp)
callq 0x1a50
movq -0x258(%rbp), %rcx
movq -0xe0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
leaq -0xd0(%rbp), %rdi
callq 0x1a70
jmp 0x14a8
leaq -0x111(%rbp), %rdi
callq 0x1af0
movq $0x0, -0x120(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x1b10
movq %rax, -0x128(%rbp)
movq -0x120(%rbp), %rax
cmpq -0x128(%rbp), %rax
je 0x1551
movq -0x120(%rbp), %rsi
leaq -0xd0(%rbp), %rdi
movq %rdi, -0x260(%rbp)
callq 0x1cb0
movq -0x260(%rbp), %rdi
movq %rax, %rsi
callq 0x1c40
jmp 0x150d
jmp 0x150f
movq -0x120(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x120(%rbp)
jmp 0x14d2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x111(%rbp), %rdi
callq 0x1af0
jmp 0x1a2f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x1a23
leaq -0xd0(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x130(%rbp), %rdi
callq 0x1b60
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 0x159e
jmp 0x15f2
leaq -0x138(%rbp), %rdi
callq 0x1b90
movl (%rax), %eax
movl %eax, -0x144(%rbp)
movl -0x144(%rbp), %esi
movq 0x4a19(%rip), %rdi      # 0x5fd8
callq 0x10f0
movq %rax, -0x268(%rbp)
jmp 0x15cd
movq -0x268(%rbp), %rdi
leaq 0x1ac9(%rip), %rsi      # 0x30a4
callq 0x1090
jmp 0x15e2
jmp 0x15e4
leaq -0x138(%rbp), %rdi
callq 0x1c20
jmp 0x1585
movq 0x49df(%rip), %rdi      # 0x5fd8
movq 0x49c8(%rip), %rsi      # 0x5fc8
callq 0x10c0
jmp 0x1607
movups 0x1a56(%rip), %xmm0     # 0x3064
movaps %xmm0, -0x190(%rbp)
movups 0x1a38(%rip), %xmm0     # 0x3054
movaps %xmm0, -0x1a0(%rbp)
movabsq $0x900000008, %rax      # imm = 0x900000008
movq %rax, -0x180(%rbp)
leaq -0x1a0(%rbp), %rax
movq %rax, -0x170(%rbp)
movq $0xa, -0x168(%rbp)
leaq -0x1a1(%rbp), %rdi
movq %rdi, -0x270(%rbp)
callq 0x1a50
movq -0x270(%rbp), %rcx
movq -0x170(%rbp), %rsi
movq -0x168(%rbp), %rdx
leaq -0x160(%rbp), %rdi
callq 0x1a70
jmp 0x1683
leaq -0x1a1(%rbp), %rdi
callq 0x1af0
movq $0x0, -0x1b0(%rbp)
movq -0x1b0(%rbp), %rax
movq %rax, -0x280(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x1b10
movq -0x280(%rbp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x271(%rbp)
je 0x16ea
movq -0x1b0(%rbp), %rsi
leaq -0x160(%rbp), %rdi
callq 0x1cb0
cmpl $0x5, (%rax)
setl %al
movb %al, -0x271(%rbp)
movb -0x271(%rbp), %al
testb $0x1, %al
jne 0x16f6
jmp 0x1743
movq -0x1b0(%rbp), %rsi
leaq -0x160(%rbp), %rdi
callq 0x1cb0
movl $0x0, (%rax)
movq -0x1b0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1b0(%rbp)
jmp 0x169a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x1a1(%rbp), %rdi
callq 0x1af0
jmp 0x1a23
movl $0x0, -0x1b4(%rbp)
movl -0x1b4(%rbp), %eax
movq %rax, -0x288(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x1b10
movq %rax, %rcx
movq -0x288(%rbp), %rax
cmpq %rcx, %rax
je 0x17d9
movl -0x1b4(%rbp), %eax
movl %eax, %esi
leaq -0x160(%rbp), %rdi
callq 0x1cb0
movl (%rax), %esi
movq 0x4846(%rip), %rdi      # 0x5fd8
callq 0x10f0
movq %rax, -0x290(%rbp)
jmp 0x17a0
movq -0x290(%rbp), %rdi
leaq 0x18f6(%rip), %rsi      # 0x30a4
callq 0x1090
jmp 0x17b5
jmp 0x17b7
movl -0x1b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1b4(%rbp)
jmp 0x174d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x1a17
movq 0x47f8(%rip), %rdi      # 0x5fd8
movq 0x47e1(%rip), %rsi      # 0x5fc8
callq 0x10c0
jmp 0x17ee
movups 0x1897(%rip), %xmm0     # 0x308c
movaps %xmm0, -0x200(%rbp)
movups 0x1879(%rip), %xmm0     # 0x307c
movaps %xmm0, -0x210(%rbp)
movabsq $0x900000008, %rax      # imm = 0x900000008
movq %rax, -0x1f0(%rbp)
leaq -0x210(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movq $0xa, -0x1d8(%rbp)
leaq -0x211(%rbp), %rdi
movq %rdi, -0x298(%rbp)
callq 0x1a50
movq -0x298(%rbp), %rcx
movq -0x1e0(%rbp), %rsi
movq -0x1d8(%rbp), %rdx
leaq -0x1d0(%rbp), %rdi
callq 0x1a70
jmp 0x186a
leaq -0x211(%rbp), %rdi
callq 0x1af0
leaq -0x1d0(%rbp), %rdi
callq 0x1b60
movq %rax, -0x220(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x1bb0
movq %rax, -0x228(%rbp)
leaq -0x220(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x1be0
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x299(%rbp)
jne 0x18c0
jmp 0x18d8
leaq -0x220(%rbp), %rdi
callq 0x1b90
cmpl $0x5, (%rax)
setl %al
movb %al, -0x299(%rbp)
movb -0x299(%rbp), %al
testb $0x1, %al
jne 0x18e4
jmp 0x1921
leaq -0x220(%rbp), %rdi
callq 0x1b90
movl $0x0, (%rax)
leaq -0x220(%rbp), %rdi
callq 0x1c20
jmp 0x1889
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x211(%rbp), %rdi
callq 0x1af0
jmp 0x1a17
leaq -0x1d0(%rbp), %rdi
callq 0x1b60
movq %rax, -0x230(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x1bb0
movq %rax, -0x238(%rbp)
leaq -0x230(%rbp), %rdi
leaq -0x238(%rbp), %rsi
callq 0x1be0
testb $0x1, %al
jne 0x1960
jmp 0x19c2
leaq -0x230(%rbp), %rdi
callq 0x1b90
movl (%rax), %esi
movq 0x4663(%rip), %rdi      # 0x5fd8
callq 0x10f0
movq %rax, -0x2a8(%rbp)
jmp 0x1983
movq -0x2a8(%rbp), %rdi
leaq 0x1713(%rip), %rsi      # 0x30a4
callq 0x1090
jmp 0x1998
jmp 0x199a
leaq -0x230(%rbp), %rdi
callq 0x1c20
jmp 0x1934
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x1cd0
jmp 0x1a17
movq 0x460f(%rip), %rdi      # 0x5fd8
movq 0x45f8(%rip), %rsi      # 0x5fc8
callq 0x10c0
jmp 0x19d7
movl $0x0, -0x4(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x1cd0
leaq -0x160(%rbp), %rdi
callq 0x1cd0
leaq -0xd0(%rbp), %rdi
callq 0x1cd0
leaq -0x20(%rbp), %rdi
callq 0x1cd0
movl -0x4(%rbp), %eax
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbp
retq
leaq -0x160(%rbp), %rdi
callq 0x1cd0
leaq -0xd0(%rbp), %rdi
callq 0x1cd0
leaq -0x20(%rbp), %rdi
callq 0x1cd0
movq -0x70(%rbp), %rdi
callq 0x1100
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)