// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
movq 0x3d3d(%rip), %rax      # 0x4fe0
leaq -0x78(%rbp), %rdi
movq %rax, (%rdi)
movb $0x1, 0xc(%rdi)
callq 0x164a
movq -0x78(%rbp), %rax
movl -0x70(%rbp), %ecx
movb -0x6c(%rbp), %dl
leaq -0x68(%rbp), %rdi
movq %rax, (%rdi)
movl %ecx, 0x8(%rdi)
movb %dl, 0xc(%rdi)
leaq -0x50(%rbp), %rsi
movq $0x0, (%rsi)
movl $0x0, 0x8(%rsi)
movb $0x0, 0xc(%rsi)
leaq -0x40(%rbp), %rdx
callq 0x1684
movq -0x40(%rbp), %rbx
movq -0x38(%rbp), %r14
cmpq %r14, %rbx
je 0x1323
movq %r14, %rax
subq %rbx, %rax
sarq $0x2, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x182e
movq %rbx, %rdi
movq %r14, %rsi
callq 0x190f
leaq -0x68(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x15ca
movq -0x40(%rbp), %rbx
movq -0x38(%rbp), %r12
movq %r12, %rax
subq %rbx, %rax
je 0x1385
leaq -0x50(%rbp), %rdi
movq %r12, (%rdi)
leaq -0x90(%rbp), %rsi
movq %rbx, (%rsi)
sarq $0x2, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
callq 0x1b81
leaq -0x88(%rbp), %rdi
movq %r12, (%rdi)
leaq -0x80(%rbp), %rsi
movq %rbx, (%rsi)
callq 0x1cc1
movq -0x40(%rbp), %rbx
movq -0x38(%rbp), %r12
subq %rbx, %r12
movq 0x3c41(%rip), %r14      # 0x4fd0
movq %r14, -0x50(%rbp)
leaq 0x1c6a(%rip), %r13      # 0x3004
movq %r13, -0x48(%rbp)
sarq $0x2, %r12
testq %r12, %r12
jle 0x13c6
incq %r12
leaq -0x50(%rbp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x20c4
addq $0x4, %rbx
decq %r12
cmpq $0x1, %r12
ja 0x13ae
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq 0x3bec(%rip), %rbx      # 0x4fd0
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1080
movq -0x68(%rbp), %r14
movq -0x60(%rbp), %r12
movq %rbx, -0x50(%rbp)
movq %r13, -0x48(%rbp)
subq %r14, %r12
sarq $0x2, %r12
testq %r12, %r12
jle 0x142f
incq %r12
leaq -0x50(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x20c4
addq $0x4, %r14
decq %r12
cmpq $0x1, %r12
ja 0x1417
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq 0x3b83(%rip), %rdi      # 0x4fd0
callq 0x1030
movq %rax, %rdi
callq 0x1080
movq -0x40(%rbp), %r14
movl (%r14), %esi
movq 0x3b68(%rip), %rdi      # 0x4fd0
callq 0x1110
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1080
movl (%r14), %esi
movq 0x3b2f(%rip), %rdi      # 0x4fd0
callq 0x1110
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1080
movl (%r14), %esi
movq 0x3af6(%rip), %rdi      # 0x4fd0
callq 0x1110
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1080
movl (%r14), %esi
movq 0x3abd(%rip), %rdi      # 0x4fd0
callq 0x1110
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1080
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x1557
movq -0x58(%rbp), %rsi
subq %rdi, %rsi
callq 0x10d0
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x156c
movq -0x30(%rbp), %rsi
subq %rdi, %rsi
callq 0x10d0
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1583
jmp 0x1583
jmp 0x1594
movq %rax, %rbx
jmp 0x15ac
jmp 0x1594
jmp 0x1594
jmp 0x1594
jmp 0x1594
jmp 0x1594
jmp 0x1594
movq %rax, %rbx
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x15ac
movq -0x58(%rbp), %rsi
subq %rdi, %rsi
callq 0x10d0
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x15c1
movq -0x30(%rbp), %rsi
subq %rdi, %rsi
callq 0x10d0
movq %rbx, %rdi
callq 0x1120
nop