// File: /workspace/github_cpp_makefile_star2_O2/iZhangHui[P]CppPrimer/CH14_OverloadedOperationsAndConversions/examples/calc.cc
// Function: main
int main()
{
	function<int(int, int)> f1 = add;      // function pointer
	function<int(int, int)> f2 = divide(); // callable class type
	function<int(int, int)> f3 = [](int i, int j) // lambda
	                             { return i * j; };
	cout << f1(4,2) << endl; // prints 6
	cout << f2(4,2) << endl; // prints 2
	cout << f3(4,2) << endl; // prints 8
	cout << "==================" << endl;
	// table of callable objects corresponding to each binary operator
	// all the callables must take two ints and return an int
	// an element can be a function pointer, function object, or lambda
	map<string, function<int(int, int)>> binops = {
		{"+", add},                  // function pointer
		{"-", std::minus<int>()},    // library function object
		{"/",  divide()},            // user-defined function object
		{"*", [](int i, int j) { return i * j; }}, // unnamed lambda
		{"%", mod} };                // named lambda object

	cout << binops["+"](10, 5) << endl; // calls add(10, 5)
	cout << binops["-"](10, 5) << endl; // uses the call operator of the minus<int> object
	cout << binops["/"](10, 5) << endl; // uses the call operator of the divide object
	cout << binops["*"](10, 5) << endl; // calls the lambda function object
	cout << binops["%"](10, 5) << endl; // calls the lambda function object

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
leaq -0xb0(%rbp), %rdi
andq $0x0, 0x8(%rdi)
leaq -0x2c(%rip), %rax       # 0x22dc
movq %rax, (%rdi)
leaq 0x76a(%rip), %rax       # 0x2a7c
movq %rax, 0x18(%rdi)
leaq 0x76d(%rip), %rax       # 0x2a8a
movq %rax, 0x10(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
leaq 0x783(%rip), %rax       # 0x2ab2
movq %rax, -0x48(%rbp)
leaq 0x786(%rip), %rax       # 0x2ac0
movq %rax, -0x50(%rbp)
movaps %xmm0, -0x40(%rbp)
leaq 0x48b(%rip), %rax       # 0x27d4
movq %rax, -0x28(%rbp)
leaq 0x48c(%rip), %rax       # 0x27e0
movq %rax, -0x30(%rbp)
pushq $0x4
popq %rsi
pushq $0x2
popq %rdx
callq 0x27fe
movq 0x3c6e(%rip), %rdi      # 0x5fd8
movl %eax, %esi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
leaq -0x60(%rbp), %rdi
pushq $0x4
popq %rsi
pushq $0x2
popq %rdx
callq 0x27fe
movq 0x3c49(%rip), %rdi      # 0x5fd8
movl %eax, %esi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
leaq -0x40(%rbp), %rdi
pushq $0x4
popq %rsi
pushq $0x2
popq %rdx
callq 0x27fe
movq 0x3c24(%rip), %rdi      # 0x5fd8
movl %eax, %esi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
movq 0x3c0e(%rip), %rdi      # 0x5fd8
leaq 0x1c4f(%rip), %rsi      # 0x4020
callq 0x20f0
movq %rax, %rdi
callq 0x20b0
leaq 0x1c4e(%rip), %rsi      # 0x4033
leaq -0x110(%rip), %rdx      # 0x22dc
leaq -0x1f0(%rbp), %r14
movq %r14, %rdi
callq 0x282a
leaq -0x1b0(%rbp), %r15
leaq 0x1c2c(%rip), %rsi      # 0x4035
leaq -0x1c(%rbp), %rdx
movq %r15, %rdi
callq 0x286c
leaq -0x170(%rbp), %r15
leaq 0x1c14(%rip), %rsi      # 0x4037
leaq -0x1b(%rbp), %rdx
movq %r15, %rdi
callq 0x28a2
leaq -0x130(%rbp), %r15
leaq 0x1bfc(%rip), %rsi      # 0x4039
leaq -0x19(%rbp), %rdx
movq %r15, %rdi
callq 0x2080
xorps %xmm0, %xmm0
leaq -0xf0(%rbp), %r15
movups %xmm0, -0x20(%r15)
leaq 0x31d(%rip), %rax       # 0x277c
movq %rax, -0x8(%r15)
leaq 0x31e(%rip), %rax       # 0x2788
movq %rax, -0x10(%r15)
leaq 0x1bc6(%rip), %rsi      # 0x403b
leaq -0x19(%rbp), %rdx
movq %r15, %rdi
callq 0x2080
leaq -0x1f0(%rbp), %rsi
xorps %xmm0, %xmm0
movups %xmm0, 0x120(%rsi)
leaq 0x30d(%rip), %rax       # 0x27a6
movq %rax, 0x138(%rsi)
leaq 0x30f(%rip), %rax       # 0x27b6
movq %rax, 0x130(%rsi)
leaq -0x90(%rbp), %rdi
pushq $0x5
popq %rdx
leaq -0x19(%rbp), %rcx
leaq -0x1a(%rbp), %r8
callq 0x28d8
movl $0x100, %ebx            # imm = 0x100
leaq (%rbx,%rbp), %rdi
addq $-0x1f0, %rdi           # imm = 0xFE10
callq 0x2920
addq $-0x40, %rbx
cmpq $-0x40, %rbx
jne 0x24ca
leaq 0x1b48(%rip), %rsi      # 0x4033
leaq -0x1f0(%rbp), %rdi
leaq -0x19(%rbp), %rdx
callq 0x2080
leaq -0x90(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0x2940
pushq $0xa
popq %rsi
pushq $0x5
popq %rdx
movq %rax, %rdi
callq 0x27fe
movq 0x3ab5(%rip), %rdi      # 0x5fd8
movl %eax, %esi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
leaq -0x1f0(%rbp), %rdi
callq 0x20a0
leaq 0x1af0(%rip), %rsi      # 0x4035
leaq -0x1f0(%rbp), %rdi
leaq -0x19(%rbp), %rdx
callq 0x2080
leaq -0x90(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0x2940
pushq $0xa
popq %rsi
pushq $0x5
popq %rdx
movq %rax, %rdi
callq 0x27fe
movq 0x3a5b(%rip), %rdi      # 0x5fd8
movl %eax, %esi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
leaq -0x1f0(%rbp), %rdi
callq 0x20a0
leaq 0x1a98(%rip), %rsi      # 0x4037
leaq -0x1f0(%rbp), %rdi
leaq -0x19(%rbp), %rdx
callq 0x2080
leaq -0x90(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0x2940
pushq $0xa
popq %rsi
pushq $0x5
popq %rdx
movq %rax, %rdi
callq 0x27fe
movq 0x3a01(%rip), %rdi      # 0x5fd8
movl %eax, %esi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
leaq -0x1f0(%rbp), %rdi
callq 0x20a0
leaq 0x1a40(%rip), %rsi      # 0x4039
leaq -0x1f0(%rbp), %rdi
leaq -0x19(%rbp), %rdx
callq 0x2080
leaq -0x90(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0x2940
pushq $0xa
popq %rsi
pushq $0x5
popq %rdx
movq %rax, %rdi
callq 0x27fe
movq 0x39a7(%rip), %rdi      # 0x5fd8
movl %eax, %esi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
leaq -0x1f0(%rbp), %rdi
callq 0x20a0
leaq 0x19e8(%rip), %rsi      # 0x403b
leaq -0x1f0(%rbp), %rdi
leaq -0x19(%rbp), %rdx
callq 0x2080
leaq -0x90(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0x2940
pushq $0xa
popq %rsi
pushq $0x5
popq %rdx
movq %rax, %rdi
callq 0x27fe
movq 0x394d(%rip), %rdi      # 0x5fd8
movl %eax, %esi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
leaq -0x1f0(%rbp), %rdi
callq 0x20a0
leaq -0x90(%rbp), %rdi
callq 0x2a00
leaq -0x40(%rbp), %rdi
callq 0x29aa
leaq -0x60(%rbp), %rdi
callq 0x29aa
leaq -0xb0(%rbp), %rdi
callq 0x29aa
xorl %eax, %eax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
jmp 0x26e8
jmp 0x26e8
jmp 0x26e8
jmp 0x26e8
movq %rax, %rbx
jmp 0x2745
movq %rax, %rbx
movl $0x100, %r14d           # imm = 0x100
leaq (%r14,%rbp), %rdi
addq $-0x1f0, %rdi           # imm = 0xFE10
callq 0x2920
addq $-0x40, %r14
cmpq $-0x40, %r14
jne 0x26f6
jmp 0x2756
jmp 0x2718
jmp 0x2718
jmp 0x2753
movq %rax, %rbx
addq $-0x40, %r15
movq %r15, %rdi
callq 0x2920
cmpq %r14, %r15
jne 0x271b
jmp 0x2756
jmp 0x2736
jmp 0x2736
jmp 0x2736
jmp 0x2736
movq %rax, %rbx
leaq -0x1f0(%rbp), %rdi
callq 0x20a0
leaq -0x90(%rbp), %rdi
callq 0x2a00
jmp 0x2756
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
callq 0x29aa
leaq -0x60(%rbp), %rdi
callq 0x29aa
leaq -0xb0(%rbp), %rdi
callq 0x29aa
movq %rbx, %rdi
callq 0x2190