// File: iZhangHui[P]CppPrimer/CH10_GenericAlgorithms/examples/reverse.cc
// Function: main
int main()
{
	vector<int> v1 = {0,1,2,3,4,5,6,7,8,9};
	vector<int> v2;
	vector<int> v3 = v1;

	find(v1.begin(), v1.end(), 42);  // find first element equal to 42 
	find_if(v1.begin(), v1.end(),    // find first odd element
	        [](int i) { return i % 2; });

	// puts elements in v1 into reverse order
	reverse(v1.begin(), v1.end());  
	v1 = v3;  // restore original data

	// copies elements from v1 into v2 in reverse order; v1 is unchanged
	reverse_copy(v1.begin(), v1.end(), back_inserter(v2));

	for (auto i : v1) 
		cout << i << " ";   // prints 0 1 2 . . . 8 9
	cout << endl;
	for (auto i : v2) 
		cout << i << " ";   // prints 9 8 7 . . . 1 0
	cout << endl;

	// removes the odd elements from v1
	auto it = remove_if(v1.begin(), v1.end(), 
	                    [](int i) { return i % 2; });
	// prints 0 2 4 6 8
	for_each(v1.begin(), it, [](int i) { cout << i << " "; });
	cout << endl;

	v1 = v3;    // restore original data
	v2.clear(); // make v2 empty again

	// copies only the even elements from v1 into v2; v1 is unchanged
	remove_copy_if(v1.begin(), v1.end(), back_inserter(v2),
	               [](int i) { return i % 2; });
	for (auto i : v2) 
		cout << i << " ";  // prints 0 2 4 6 8
	cout << endl;
	for (auto i : v1)      // prints 0 1 2 . . . 8 9
		cout << i << " ";
	cout << endl;

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x290, %rsp            # imm = 0x290
movl $0x0, -0x4(%rbp)
movabsq $0x900000008, %rax      # imm = 0x900000008
movq %rax, -0x40(%rbp)
movups 0x3dad(%rip), %xmm0     # 0x5044
movaps %xmm0, -0x50(%rbp)
movups 0x3d92(%rip), %xmm0     # 0x5034
movaps %xmm0, -0x60(%rbp)
leaq -0x60(%rbp), %rax
movq %rax, -0x30(%rbp)
movq $0xa, -0x28(%rbp)
leaq -0x61(%rbp), %rdi
movq %rdi, -0x200(%rbp)
callq 0x2220
movq -0x200(%rbp), %rcx
movq -0x30(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x20(%rbp), %rdi
callq 0x2240
jmp 0x12e0
leaq -0x61(%rbp), %rdi
callq 0x22c0
leaq -0x90(%rbp), %rdi
callq 0x22e0
leaq -0xa8(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x2300
jmp 0x1307
leaq -0x20(%rbp), %rdi
movq %rdi, -0x210(%rbp)
callq 0x2440
movq -0x210(%rbp), %rdi
movq %rax, -0xb0(%rbp)
callq 0x2470
movq %rax, -0xb8(%rbp)
movl $0x2a, -0xbc(%rbp)
movq -0xb0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
leaq -0xbc(%rbp), %rdx
callq 0x23f0
movq %rax, -0x208(%rbp)
jmp 0x135e
movq -0x208(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0x20(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0x2440
movq -0x220(%rbp), %rdi
movq %rax, -0xd0(%rbp)
callq 0x2470
movq %rax, -0xd8(%rbp)
movq -0xd0(%rbp), %rdi
movq -0xd8(%rbp), %rsi
callq 0x1940
movq %rax, -0x218(%rbp)
jmp 0x13b2
movq -0x218(%rbp), %rax
movq %rax, -0xe8(%rbp)
leaq -0x20(%rbp), %rdi
movq %rdi, -0x228(%rbp)
callq 0x2440
movq -0x228(%rbp), %rdi
movq %rax, -0xf0(%rbp)
callq 0x2470
movq %rax, -0xf8(%rbp)
movq -0xf0(%rbp), %rdi
movq -0xf8(%rbp), %rsi
callq 0x24a0
jmp 0x13ff
leaq -0x20(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x24e0
jmp 0x1411
leaq -0x20(%rbp), %rdi
movq %rdi, -0x238(%rbp)
callq 0x2440
movq -0x238(%rbp), %rdi
movq %rax, -0x100(%rbp)
callq 0x2470
movq %rax, -0x108(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x27a0
movq %rax, -0x230(%rbp)
jmp 0x1450
movq -0x230(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x100(%rbp), %rdi
movq -0x108(%rbp), %rsi
movq -0x110(%rbp), %rdx
callq 0x2730
movq %rax, -0x240(%rbp)
jmp 0x1481
movq -0x240(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0x20(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x120(%rbp), %rdi
callq 0x2440
movq %rax, -0x128(%rbp)
movq -0x120(%rbp), %rdi
callq 0x2470
movq %rax, -0x130(%rbp)
leaq -0x128(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x27d0
testb $0x1, %al
jne 0x14dc
jmp 0x1578
leaq -0x128(%rbp), %rdi
callq 0x2810
movl (%rax), %eax
movl %eax, -0x134(%rbp)
movl -0x134(%rbp), %esi
movq 0x6adb(%rip), %rdi      # 0x7fd8
callq 0x1110
movq %rax, -0x248(%rbp)
jmp 0x150b
movq -0x248(%rbp), %rdi
leaq 0x3b43(%rip), %rsi      # 0x505c
callq 0x1090
jmp 0x1520
jmp 0x1522
leaq -0x128(%rbp), %rdi
callq 0x2830
jmp 0x14c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x61(%rbp), %rdi
callq 0x22c0
jmp 0x1934
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x191f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x2870
jmp 0x191f
movq 0x6a59(%rip), %rdi      # 0x7fd8
movq 0x6a42(%rip), %rsi      # 0x7fc8
callq 0x10c0
jmp 0x158d
leaq -0x90(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rdi
callq 0x2440
movq %rax, -0x148(%rbp)
movq -0x140(%rbp), %rdi
callq 0x2470
movq %rax, -0x150(%rbp)
leaq -0x148(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x27d0
testb $0x1, %al
jne 0x15da
jmp 0x162e
leaq -0x148(%rbp), %rdi
callq 0x2810
movl (%rax), %eax
movl %eax, -0x154(%rbp)
movl -0x154(%rbp), %esi
movq 0x69dd(%rip), %rdi      # 0x7fd8
callq 0x1110
movq %rax, -0x250(%rbp)
jmp 0x1609
movq -0x250(%rbp), %rdi
leaq 0x3a45(%rip), %rsi      # 0x505c
callq 0x1090
jmp 0x161e
jmp 0x1620
leaq -0x148(%rbp), %rdi
callq 0x2830
jmp 0x15c1
movq 0x69a3(%rip), %rdi      # 0x7fd8
movq 0x698c(%rip), %rsi      # 0x7fc8
callq 0x10c0
jmp 0x1643
leaq -0x20(%rbp), %rdi
movq %rdi, -0x260(%rbp)
callq 0x2440
movq -0x260(%rbp), %rdi
movq %rax, -0x168(%rbp)
callq 0x2470
movq %rax, -0x170(%rbp)
movq -0x168(%rbp), %rdi
movq -0x170(%rbp), %rsi
callq 0x1980
movq %rax, -0x258(%rbp)
jmp 0x1689
movq -0x258(%rbp), %rax
movq %rax, -0x160(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x2440
movq %rax, -0x180(%rbp)
movq -0x160(%rbp), %rax
movq %rax, -0x188(%rbp)
movq -0x180(%rbp), %rdi
movq -0x188(%rbp), %rsi
callq 0x19c0
jmp 0x16ca
movq 0x6907(%rip), %rdi      # 0x7fd8
movq 0x68f0(%rip), %rsi      # 0x7fc8
callq 0x10c0
jmp 0x16df
leaq -0x20(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x24e0
jmp 0x16f1
leaq -0x90(%rbp), %rdi
movq %rdi, -0x270(%rbp)
callq 0x2850
leaq -0x20(%rbp), %rdi
movq %rdi, -0x278(%rbp)
callq 0x2440
movq -0x278(%rbp), %rdi
movq %rax, -0x198(%rbp)
callq 0x2470
movq -0x270(%rbp), %rdi
movq %rax, -0x1a0(%rbp)
callq 0x27a0
movq %rax, -0x268(%rbp)
jmp 0x1743
movq -0x268(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movq -0x198(%rbp), %rdi
movq -0x1a0(%rbp), %rsi
movq -0x1a8(%rbp), %rdx
callq 0x1a10
movq %rax, -0x280(%rbp)
jmp 0x1774
movq -0x280(%rbp), %rax
movq %rax, -0x1b8(%rbp)
leaq -0x90(%rbp), %rax
movq %rax, -0x1c0(%rbp)
movq -0x1c0(%rbp), %rdi
callq 0x2440
movq %rax, -0x1c8(%rbp)
movq -0x1c0(%rbp), %rdi
callq 0x2470
movq %rax, -0x1d0(%rbp)
leaq -0x1c8(%rbp), %rdi
leaq -0x1d0(%rbp), %rsi
callq 0x27d0
testb $0x1, %al
jne 0x17cf
jmp 0x1823
leaq -0x1c8(%rbp), %rdi
callq 0x2810
movl (%rax), %eax
movl %eax, -0x1d4(%rbp)
movl -0x1d4(%rbp), %esi
movq 0x67e8(%rip), %rdi      # 0x7fd8
callq 0x1110
movq %rax, -0x288(%rbp)
jmp 0x17fe
movq -0x288(%rbp), %rdi
leaq 0x3850(%rip), %rsi      # 0x505c
callq 0x1090
jmp 0x1813
jmp 0x1815
leaq -0x1c8(%rbp), %rdi
callq 0x2830
jmp 0x17b6
movq 0x67ae(%rip), %rdi      # 0x7fd8
movq 0x6797(%rip), %rsi      # 0x7fc8
callq 0x10c0
jmp 0x1838
leaq -0x20(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movq -0x1e0(%rbp), %rdi
callq 0x2440
movq %rax, -0x1e8(%rbp)
movq -0x1e0(%rbp), %rdi
callq 0x2470
movq %rax, -0x1f0(%rbp)
leaq -0x1e8(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0x27d0
testb $0x1, %al
jne 0x1882
jmp 0x18d6
leaq -0x1e8(%rbp), %rdi
callq 0x2810
movl (%rax), %eax
movl %eax, -0x1f4(%rbp)
movl -0x1f4(%rbp), %esi
movq 0x6735(%rip), %rdi      # 0x7fd8
callq 0x1110
movq %rax, -0x290(%rbp)
jmp 0x18b1
movq -0x290(%rbp), %rdi
leaq 0x379d(%rip), %rsi      # 0x505c
callq 0x1090
jmp 0x18c6
jmp 0x18c8
leaq -0x1e8(%rbp), %rdi
callq 0x2830
jmp 0x1869
movq 0x66fb(%rip), %rdi      # 0x7fd8
movq 0x66e4(%rip), %rsi      # 0x7fc8
callq 0x10c0
jmp 0x18eb
movl $0x0, -0x4(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x2870
leaq -0x90(%rbp), %rdi
callq 0x2870
leaq -0x20(%rbp), %rdi
callq 0x2870
movl -0x4(%rbp), %eax
addq $0x290, %rsp            # imm = 0x290
popq %rbp
retq
leaq -0x90(%rbp), %rdi
callq 0x2870
leaq -0x20(%rbp), %rdi
callq 0x2870
movq -0x70(%rbp), %rdi
callq 0x1120
nopl (%rax)