// File: iZhangHui[P]CppPrimer/CH10_GenericAlgorithms/examples/reverse-iter.cc
// Function: main
int main()
{
	vector<int> vec;
	istream_iterator<int> in(cin), eof;
	copy (in, eof, back_inserter(vec));

	sort(vec.begin(), vec.end()); // sorts vec in ``normal'' order
	vector<int> vec2(vec);        // vec2 is a copy of vec

	// sorts in reverse: puts the smallest element at the end of vec
	sort(vec.rbegin(), vec.rend());

	// prints vec
	ostream_iterator<int> out(cout, " ");
	copy(vec.begin(), vec.end(), out);    // reverse sort
	cout << endl;

	// prints vec2
	copy(vec2.begin(), vec2.end(), out);  // forward sort
	cout << endl;

	// it refers to the first element in vec
	auto it = vec.begin();

	// rev_it refers one before the first element in vec
	vector<int>::reverse_iterator rev_it(it);
	cout << *(rev_it.base()) << endl; // prints first element in vec
	
	// ways to print value at beginning of vec through it and rev_it
	cout << *it << endl;
	cout << *(rev_it.base()) << endl;  
	cout << *(rev_it - 1) << endl;

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1d0, %rsp            # imm = 0x1D0
movl $0x0, -0x4(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x16a0
movq 0x8d4e(%rip), %rsi      # 0x9fe0
leaq -0x30(%rbp), %rdi
callq 0x16c0
jmp 0x129d
leaq -0x50(%rbp), %rdi
callq 0x16f0
jmp 0x12a8
leaq -0x60(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1790
jmp 0x12b7
leaq -0x70(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x1790
jmp 0x12c6
leaq -0x20(%rbp), %rdi
callq 0x17d0
movq %rax, -0x160(%rbp)
jmp 0x12d8
movq -0x160(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdx
leaq -0x60(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1710
movq %rax, -0x168(%rbp)
jmp 0x12fd
movq -0x168(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq -0x20(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x1840
movq -0x170(%rbp), %rdi
movq %rax, -0x88(%rbp)
callq 0x1870
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0x1800
jmp 0x1347
leaq -0xa8(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x18a0
jmp 0x1359
leaq -0xb0(%rbp), %rdi
movq %rdi, -0x180(%rbp)
leaq -0x20(%rbp), %rsi
movq %rsi, -0x188(%rbp)
callq 0x19e0
movq -0x188(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x1a30
movq -0x180(%rbp), %rdi
movq -0x178(%rbp), %rsi
callq 0x1990
jmp 0x13a6
movq 0x8c23(%rip), %rsi      # 0x9fd0
leaq 0x4c50(%rip), %rdx      # 0x6004
leaq -0xc8(%rbp), %rdi
callq 0x1a80
jmp 0x13c2
leaq -0x20(%rbp), %rdi
movq %rdi, -0x190(%rbp)
callq 0x1840
movq -0x190(%rbp), %rdi
movq %rax, -0xd0(%rbp)
callq 0x1870
movq %rax, -0xd8(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x1b30
jmp 0x1401
movq -0xd0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
leaq -0xf8(%rbp), %rdi
leaq -0xe8(%rbp), %rcx
callq 0x1ab0
jmp 0x1424
movq 0x8ba5(%rip), %rdi      # 0x9fd0
movq 0x8b8e(%rip), %rsi      # 0x9fc0
callq 0x10e0
jmp 0x1439
leaq -0xa8(%rbp), %rdi
movq %rdi, -0x198(%rbp)
callq 0x1840
movq -0x198(%rbp), %rdi
movq %rax, -0x100(%rbp)
callq 0x1870
movq %rax, -0x108(%rbp)
leaq -0x118(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x1b30
jmp 0x147b
movq -0x100(%rbp), %rsi
movq -0x108(%rbp), %rdx
leaq -0x128(%rbp), %rdi
leaq -0x118(%rbp), %rcx
callq 0x1ab0
jmp 0x149e
movq 0x8b2b(%rip), %rdi      # 0x9fd0
movq 0x8b14(%rip), %rsi      # 0x9fc0
callq 0x10e0
jmp 0x14b3
leaq -0x20(%rbp), %rdi
callq 0x1840
movq %rax, -0x130(%rbp)
movq -0x130(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rsi
leaq -0x138(%rbp), %rdi
callq 0x1b60
jmp 0x14e6
leaq -0x138(%rbp), %rdi
callq 0x1b80
movq %rax, -0x1a0(%rbp)
jmp 0x14fb
movq -0x1a0(%rbp), %rax
movq %rax, -0x148(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x1ba0
movl (%rax), %esi
movq 0x8ab2(%rip), %rdi      # 0x9fd0
callq 0x1110
movq %rax, -0x1a8(%rbp)
jmp 0x152c
movq -0x1a8(%rbp), %rdi
movq 0x8a86(%rip), %rsi      # 0x9fc0
callq 0x10e0
jmp 0x1541
leaq -0x130(%rbp), %rdi
callq 0x1ba0
movl (%rax), %esi
movq 0x8a7a(%rip), %rdi      # 0x9fd0
callq 0x1110
movq %rax, -0x1b0(%rbp)
jmp 0x1564
movq -0x1b0(%rbp), %rdi
movq 0x8a4e(%rip), %rsi      # 0x9fc0
callq 0x10e0
jmp 0x1579
leaq -0x138(%rbp), %rdi
callq 0x1b80
movq %rax, -0x1b8(%rbp)
jmp 0x158e
movq -0x1b8(%rbp), %rax
movq %rax, -0x150(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x1ba0
movl (%rax), %esi
movq 0x8a1f(%rip), %rdi      # 0x9fd0
callq 0x1110
movq %rax, -0x1c0(%rbp)
jmp 0x15bf
movq -0x1c0(%rbp), %rdi
movq 0x89f3(%rip), %rsi      # 0x9fc0
callq 0x10e0
jmp 0x15d4
leaq -0x158(%rbp), %rdi
leaq -0x138(%rbp), %rsi
movl $0x1, %edx
callq 0x1bc0
jmp 0x15ee
leaq -0x158(%rbp), %rdi
callq 0x1c10
movq %rax, -0x1c8(%rbp)
jmp 0x1603
movq -0x1c8(%rbp), %rax
movl (%rax), %esi
movq 0x89bd(%rip), %rdi      # 0x9fd0
callq 0x1110
movq %rax, -0x1d0(%rbp)
jmp 0x1621
movq -0x1d0(%rbp), %rdi
movq 0x8991(%rip), %rsi      # 0x9fc0
callq 0x10e0
jmp 0x1636
movl $0x0, -0x4(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x1c40
leaq -0x20(%rbp), %rdi
callq 0x1c40
movl -0x4(%rbp), %eax
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1684
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x1c40
leaq -0x20(%rbp), %rdi
callq 0x1c40
movq -0x38(%rbp), %rdi
callq 0x1120
nopw %cs:(%rax,%rax)