// File: iZhangHui[P]CppPrimer/CH14_OverloadedOperationsAndConversions/examples/absInt.cc
// Function: main
int main()
{
    int i = -42;
    absInt absObj;           // object that has a function-call operator
    unsigned ui = absObj(i); // passes i to absObj.operator()
    cout << i << " " << ui << endl;

	// store collection of positive and negative integers in vi
	vector<int> vi;
	while (cin >> i)
		vi.push_back(i);

	// call absInt to store the absolute value of those ints in vu
	vector<unsigned> vu;
	transform(vi.begin(), vi.end(), back_inserter(vu), absInt());

	// print contents of vu using a lambda
	for_each(vu.begin(), vu.end(), [](unsigned i) { cout << i << " "; });
	cout << endl;

	vector<unsigned> vu2;
	// similar transformation but using a lambda
	transform(vi.begin(), vi.end(), back_inserter(vu2),
	          [](int i) { return i < 0 ? -i : i; });
	if (vu == vu2)
		cout << "as expected" << endl;
	else {
		cout << "something's wrong, vectors differ" << endl;
		for_each(vu.begin(), vu.end(), [](unsigned i) { cout << i << " "; });
	}
	cout << endl;

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movl $0x0, -0x4(%rbp)
movl $0xffffffd6, -0x8(%rbp) # imm = 0xFFFFFFD6
movl -0x8(%rbp), %esi
leaq -0x9(%rbp), %rdi
callq 0x17f0
movl %eax, -0x10(%rbp)
movl -0x8(%rbp), %esi
movq 0x5d0e(%rip), %rdi      # 0x6fd0
callq 0x1130
movq %rax, %rdi
leaq 0x2d33(%rip), %rsi      # 0x4004
callq 0x10b0
movq %rax, %rdi
movl -0x10(%rbp), %esi
callq 0x1100
movq %rax, %rdi
movq 0x5cd5(%rip), %rsi      # 0x6fc0
callq 0x10e0
leaq -0x28(%rbp), %rdi
callq 0x1820
movq 0x5ce0(%rip), %rdi      # 0x6fe0
leaq -0x8(%rbp), %rsi
callq 0x1080
movq %rax, -0xf0(%rbp)
jmp 0x1312
movq -0xf0(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x10f0
movb %al, -0xf1(%rbp)
jmp 0x1330
movb -0xf1(%rbp), %al
testb $0x1, %al
jne 0x133c
jmp 0x135e
leaq -0x28(%rbp), %rdi
leaq -0x8(%rbp), %rsi
callq 0x1840
jmp 0x134b
jmp 0x12f9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x15ff
leaq -0x50(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x18b0
leaq -0x28(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x1950
movq -0x110(%rbp), %rdi
movq %rax, -0x58(%rbp)
callq 0x1980
movq -0x108(%rbp), %rdi
movq %rax, -0x60(%rbp)
callq 0x19b0
movq %rax, -0x100(%rbp)
jmp 0x13a7
movq -0x100(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdx
callq 0x18d0
movq %rax, -0x118(%rbp)
jmp 0x13cc
movq -0x118(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x50(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x19e0
movq -0x120(%rbp), %rdi
movq %rax, -0x80(%rbp)
callq 0x1a10
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x1620
jmp 0x1410
movq 0x5bb9(%rip), %rdi      # 0x6fd0
movq 0x5ba2(%rip), %rsi      # 0x6fc0
callq 0x10e0
jmp 0x1425
leaq -0xa8(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0x18b0
leaq -0x28(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x1950
movq -0x138(%rbp), %rdi
movq %rax, -0xb0(%rbp)
callq 0x1980
movq -0x130(%rbp), %rdi
movq %rax, -0xb8(%rbp)
callq 0x19b0
movq %rax, -0x128(%rbp)
jmp 0x1477
movq -0x128(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xb0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq -0xc0(%rbp), %rdx
callq 0x1670
movq %rax, -0x140(%rbp)
jmp 0x14a8
movq -0x140(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x1a40
movb %al, -0x141(%rbp)
jmp 0x14ce
movb -0x141(%rbp), %al
testb $0x1, %al
jne 0x14da
jmp 0x153e
movq 0x5aef(%rip), %rdi      # 0x6fd0
leaq 0x2b1e(%rip), %rsi      # 0x4006
callq 0x10b0
movq %rax, -0x150(%rbp)
jmp 0x14f6
movq -0x150(%rbp), %rdi
movq 0x5abc(%rip), %rsi      # 0x6fc0
callq 0x10e0
jmp 0x150b
jmp 0x15b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x15f6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x1ac0
jmp 0x15f6
movq 0x5a8b(%rip), %rdi      # 0x6fd0
leaq 0x2ac6(%rip), %rsi      # 0x4012
callq 0x10b0
movq %rax, -0x158(%rbp)
jmp 0x155a
movq -0x158(%rbp), %rdi
movq 0x5a58(%rip), %rsi      # 0x6fc0
callq 0x10e0
jmp 0x156f
leaq -0x50(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x19e0
movq -0x160(%rbp), %rdi
movq %rax, -0xd8(%rbp)
callq 0x1a10
movq %rax, -0xe0(%rbp)
movq -0xd8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x16f0
jmp 0x15ae
jmp 0x15b0
movq 0x5a19(%rip), %rdi      # 0x6fd0
movq 0x5a02(%rip), %rsi      # 0x6fc0
callq 0x10e0
jmp 0x15c5
movl $0x0, -0x4(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x1ac0
leaq -0x50(%rbp), %rdi
callq 0x1ac0
leaq -0x28(%rbp), %rdi
callq 0x1b20
movl -0x4(%rbp), %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
leaq -0x50(%rbp), %rdi
callq 0x1ac0
leaq -0x28(%rbp), %rdi
callq 0x1b20
movq -0x30(%rbp), %rdi
callq 0x1140
nopw %cs:(%rax,%rax)