// File: iZhangHui[P]CppPrimer/CH03_StringsVectorsAndArrays/examples/vecScores.cc
// Function: main
int main()
{
	// hold the grades we read from the standard input
	vector<unsigned> grades;

	// count the number of grades by clusters of ten: 
	// 0--9, 10--19, . ..  90--99, 100
	vector<unsigned> scores(11, 0); // 11 buckets, all initially 0
	unsigned grade;
	while (cin >> grade) {      // read the grades
		if (grade <= 100)       // handle only valid grades
			grades.push_back(grade);
			++scores[grade/10]; // increment the counter for the current cluster
	}
	cout << "grades.size = " << grades.size() << endl;
	for (auto it : grades)
		cout << it << " " ;
	cout << endl;

	cout << "scores.size = " << scores.size() << endl;
	for (auto it : scores)
		cout << it << " " ;
	cout << endl;

	// equivalent program using iterators instead of subscripts
	vector<unsigned> alt_scores(11, 0);  // 11 buckets, all initially 0
	// for each grade in the input
	for (auto it = grades.begin(); it != grades.end(); ++it) {
		unsigned i = *it;
		// increment the counter for the current cluster
		++(*(alt_scores.begin() + i/10));
	}

	cout << "alt_scores.size = " << alt_scores.size() << endl;
	for (auto it = alt_scores.begin(); it != alt_scores.end(); ++it) 
		cout << *it << " " ;
	cout << endl;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movl $0x0, -0x4(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x17c0
movl $0x0, -0x3c(%rbp)
leaq -0x3d(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x17e0
movq -0xf0(%rbp), %rcx
leaq -0x38(%rbp), %rdi
movl $0xb, %esi
leaq -0x3c(%rbp), %rdx
callq 0x1800
jmp 0x12cd
leaq -0x3d(%rbp), %rdi
callq 0x1880
movq 0x3d03(%rip), %rdi      # 0x4fe0
leaq -0x50(%rbp), %rsi
callq 0x1030
movq %rax, -0xf8(%rbp)
jmp 0x12ef
movq -0xf8(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x10f0
movb %al, -0xf9(%rbp)
jmp 0x130d
movb -0xf9(%rbp), %al
testb $0x1, %al
jne 0x1319
jmp 0x1380
cmpl $0x64, -0x50(%rbp)
ja 0x135b
leaq -0x20(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x18a0
jmp 0x132e
jmp 0x135b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x3d(%rbp), %rdi
callq 0x1880
jmp 0x17a6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x179d
movl -0x50(%rbp), %eax
movl $0xa, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, %eax
movl %eax, %esi
leaq -0x38(%rbp), %rdi
callq 0x1910
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x12d6
movq 0x3c49(%rip), %rdi      # 0x4fd0
leaq 0x1c76(%rip), %rsi      # 0x3004
callq 0x10b0
movq %rax, -0x108(%rbp)
jmp 0x139c
leaq -0x20(%rbp), %rdi
callq 0x1930
movq -0x108(%rbp), %rdi
movq %rax, %rsi
callq 0x1040
movq %rax, -0x110(%rbp)
jmp 0x13bd
movq -0x110(%rbp), %rdi
movq 0x3bf5(%rip), %rsi      # 0x4fc0
callq 0x10e0
jmp 0x13d2
leaq -0x20(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
callq 0x1950
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
callq 0x1980
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x19b0
testb $0x1, %al
jne 0x1407
jmp 0x144f
leaq -0x60(%rbp), %rdi
callq 0x19f0
movl (%rax), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %esi
movq 0x3bb1(%rip), %rdi      # 0x4fd0
callq 0x1100
movq %rax, -0x118(%rbp)
jmp 0x142d
movq -0x118(%rbp), %rdi
leaq 0x1bd6(%rip), %rsi      # 0x3011
callq 0x10b0
jmp 0x1442
jmp 0x1444
leaq -0x60(%rbp), %rdi
callq 0x1a10
jmp 0x13f4
movq 0x3b7a(%rip), %rdi      # 0x4fd0
movq 0x3b63(%rip), %rsi      # 0x4fc0
callq 0x10e0
jmp 0x1464
movq 0x3b65(%rip), %rdi      # 0x4fd0
leaq 0x1ba5(%rip), %rsi      # 0x3017
callq 0x10b0
movq %rax, -0x120(%rbp)
jmp 0x1480
leaq -0x38(%rbp), %rdi
callq 0x1930
movq -0x120(%rbp), %rdi
movq %rax, %rsi
callq 0x1040
movq %rax, -0x128(%rbp)
jmp 0x14a1
movq -0x128(%rbp), %rdi
movq 0x3b11(%rip), %rsi      # 0x4fc0
callq 0x10e0
jmp 0x14b6
leaq -0x38(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
callq 0x1950
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
callq 0x1980
movq %rax, -0x88(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x19b0
testb $0x1, %al
jne 0x14f1
jmp 0x153f
leaq -0x80(%rbp), %rdi
callq 0x19f0
movl (%rax), %eax
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %esi
movq 0x3ac1(%rip), %rdi      # 0x4fd0
callq 0x1100
movq %rax, -0x130(%rbp)
jmp 0x151d
movq -0x130(%rbp), %rdi
leaq 0x1ae6(%rip), %rsi      # 0x3011
callq 0x10b0
jmp 0x1532
jmp 0x1534
leaq -0x80(%rbp), %rdi
callq 0x1a10
jmp 0x14db
movq 0x3a8a(%rip), %rdi      # 0x4fd0
movq 0x3a73(%rip), %rsi      # 0x4fc0
callq 0x10e0
jmp 0x1554
movl $0x0, -0xac(%rbp)
leaq -0xad(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x17e0
movq -0x138(%rbp), %rcx
leaq -0xa8(%rbp), %rdi
movl $0xb, %esi
leaq -0xac(%rbp), %rdx
callq 0x1800
jmp 0x1592
leaq -0xad(%rbp), %rdi
callq 0x1880
leaq -0x20(%rbp), %rdi
callq 0x1950
movq %rax, -0xb8(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x1980
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x19b0
testb $0x1, %al
jne 0x15da
jmp 0x1668
leaq -0xb8(%rbp), %rdi
callq 0x19f0
movl (%rax), %eax
movl %eax, -0xc4(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x1950
movq %rax, -0xd8(%rbp)
movl -0xc4(%rbp), %eax
movl $0xa, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, %eax
movl %eax, %esi
leaq -0xd8(%rbp), %rdi
callq 0x1a30
movq %rax, -0xd0(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x19f0
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
leaq -0xb8(%rbp), %rdi
callq 0x1a10
jmp 0x15ae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xad(%rbp), %rdi
callq 0x1880
jmp 0x179d
movq 0x3961(%rip), %rdi      # 0x4fd0
leaq 0x199d(%rip), %rsi      # 0x3013
callq 0x10b0
movq %rax, -0x140(%rbp)
jmp 0x1684
leaq -0xa8(%rbp), %rdi
callq 0x1930
movq -0x140(%rbp), %rdi
movq %rax, %rsi
callq 0x1040
movq %rax, -0x148(%rbp)
jmp 0x16a8
movq -0x148(%rbp), %rdi
movq 0x390a(%rip), %rsi      # 0x4fc0
callq 0x10e0
jmp 0x16bd
leaq -0xa8(%rbp), %rdi
callq 0x1950
movq %rax, -0xe0(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x1980
movq %rax, -0xe8(%rbp)
leaq -0xe0(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x19b0
testb $0x1, %al
jne 0x16fc
jmp 0x175e
leaq -0xe0(%rbp), %rdi
callq 0x19f0
movl (%rax), %esi
movq 0x38bf(%rip), %rdi      # 0x4fd0
callq 0x1100
movq %rax, -0x150(%rbp)
jmp 0x171f
movq -0x150(%rbp), %rdi
leaq 0x18e4(%rip), %rsi      # 0x3011
callq 0x10b0
jmp 0x1734
jmp 0x1736
leaq -0xe0(%rbp), %rdi
callq 0x1a10
jmp 0x16d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x1a70
jmp 0x179d
movq 0x386b(%rip), %rdi      # 0x4fd0
movq 0x3854(%rip), %rsi      # 0x4fc0
callq 0x10e0
jmp 0x1773
leaq -0xa8(%rbp), %rdi
callq 0x1a70
leaq -0x38(%rbp), %rdi
callq 0x1a70
leaq -0x20(%rbp), %rdi
callq 0x1a70
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x1a70
leaq -0x20(%rbp), %rdi
callq 0x1a70
movq -0x48(%rbp), %rdi
callq 0x1130
nopl (%rax,%rax)