// File: /workspace/github_cpp_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq $0x0, -0xb8(%rbp)
leaq -0x2f(%rip), %r13       # 0x22cc
movq %r13, -0xc0(%rbp)
leaq 0xa29(%rip), %r15       # 0x2d32
movq %r15, -0xa8(%rbp)
leaq 0xa29(%rip), %r12       # 0x2d40
movq %r12, -0xb0(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
leaq 0xa3c(%rip), %rax       # 0x2d68
movq %rax, -0x58(%rbp)
leaq 0xa3f(%rip), %rax       # 0x2d76
movq %rax, -0x60(%rbp)
movaps %xmm0, -0x50(%rbp)
leaq 0x788(%rip), %rax       # 0x2ace
movq %rax, -0x38(%rbp)
leaq 0x789(%rip), %rax       # 0x2ada
movq %rax, -0x40(%rbp)
movq 0x3c7c(%rip), %rdi      # 0x5fd8
movl $0x6, %esi
callq 0x2160
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2090
movl $0x4, -0x200(%rbp)
movl $0x2, -0xa0(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x298b
leaq -0x70(%rbp), %rdi
leaq -0x200(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
callq *0x18(%rdi)
movq 0x3c0d(%rip), %rdi      # 0x5fd8
movl %eax, %esi
callq 0x2160
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2090
movl $0x4, -0x200(%rbp)
movl $0x2, -0xa0(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x298b
leaq -0x50(%rbp), %rdi
leaq -0x200(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
callq *0x18(%rdi)
movq 0x3ba1(%rip), %rdi      # 0x5fd8
movl %eax, %esi
callq 0x2160
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2090
movq 0x3b69(%rip), %rbx      # 0x5fd8
leaq 0x1b8e(%rip), %rsi      # 0x4004
movl $0x12, %edx
movq %rbx, %rdi
callq 0x2120
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq 0x3b37(%rip), %rdi      # 0x5fd8
callq 0x2030
movq %rax, %rdi
callq 0x2090
leaq -0x1f0(%rbp), %r14
movq %r14, -0x10(%r14)
movw $0x2b, (%r14)
movl $0x1, %eax
movq %rax, -0x8(%r14)
movq $0x0, 0x18(%r14)
movq %r13, 0x10(%r14)
movq %r15, 0x28(%r14)
movq %r12, 0x20(%r14)
leaq -0x1b0(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movw $0x2d, (%rcx)
movq %rax, -0x8(%rcx)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rcx)
leaq 0x756(%rip), %rdx       # 0x2c54
movq %rdx, 0x28(%rcx)
leaq 0x755(%rip), %rdx       # 0x2c5e
movq %rdx, 0x20(%rcx)
leaq -0x170(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movw $0x2f, (%rcx)
movq %rax, -0x8(%rcx)
movups %xmm0, 0x10(%rcx)
leaq 0x83c(%rip), %rdx       # 0x2d68
movq %rdx, 0x28(%rcx)
leaq 0x83f(%rip), %rdx       # 0x2d76
movq %rdx, 0x20(%rcx)
leaq -0x130(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movw $0x2a, (%rcx)
movq %rax, -0x8(%rcx)
movups %xmm0, 0x10(%rcx)
leaq 0x51c(%rip), %rdx       # 0x2a76
movq %rdx, 0x28(%rcx)
leaq 0x51d(%rip), %rdx       # 0x2a82
movq %rdx, 0x20(%rcx)
leaq -0xf0(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movw $0x25, (%rbx)
movq %rax, -0x8(%rbx)
movups %xmm0, 0x10(%rbx)
leaq 0x518(%rip), %rax       # 0x2aa0
movq %rax, 0x28(%rbx)
leaq 0x51d(%rip), %rax       # 0x2ab0
movq %rax, 0x20(%rbx)
leaq -0xa0(%rbp), %rdi
leaq -0x200(%rbp), %rsi
leaq -0x30(%rbp), %rcx
leaq -0x2c(%rbp), %r8
movl $0x5, %edx
callq 0x2af8
xorl %r15d, %r15d
movq -0xd0(%rbp,%r15), %rax
testq %rax, %rax
je 0x25dc
leaq (%r15,%rbp), %rdi
addq $-0xe0, %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0x100(%rbp,%r15), %rdi
leaq (%rbx,%r15), %rax
cmpq %rdi, %rax
je 0x25fd
movq -0xf0(%rbp,%r15), %rsi
incq %rsi
callq 0x2100
addq $-0x40, %r15
cmpq $-0x140, %r15           # imm = 0xFEC0
jne 0x25ba
leaq -0x200(%rbp), %rsi
movq %r14, (%rsi)
movw $0x2b, 0x10(%rsi)
movq $0x1, 0x8(%rsi)
leaq -0xa0(%rbp), %rdi
callq 0x2bc6
movl $0xa, -0x30(%rbp)
movl $0x5, -0x2c(%rbp)
cmpq $0x0, 0x10(%rax)
je 0x2990
leaq -0x30(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
movq %rax, %rdi
callq *0x18(%rax)
movq 0x397c(%rip), %rdi      # 0x5fd8
movl %eax, %esi
callq 0x2160
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2090
movq -0x200(%rbp), %rdi
cmpq %r14, %rdi
je 0x26a8
movq -0x1f0(%rbp), %rsi
incq %rsi
callq 0x2100
leaq -0x200(%rbp), %rsi
movq %r14, (%rsi)
movw $0x2d, 0x10(%rsi)
movq $0x1, 0x8(%rsi)
leaq -0xa0(%rbp), %rdi
callq 0x2bc6
movl $0xa, -0x30(%rbp)
movl $0x5, -0x2c(%rbp)
cmpq $0x0, 0x10(%rax)
je 0x2995
leaq -0x30(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
movq %rax, %rdi
callq *0x18(%rax)
movq 0x38de(%rip), %rdi      # 0x5fd8
movl %eax, %esi
callq 0x2160
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2090
movq -0x200(%rbp), %rdi
cmpq %r14, %rdi
je 0x2746
movq -0x1f0(%rbp), %rsi
incq %rsi
callq 0x2100
leaq -0x200(%rbp), %rsi
movq %r14, (%rsi)
movw $0x2f, 0x10(%rsi)
movq $0x1, 0x8(%rsi)
leaq -0xa0(%rbp), %rdi
callq 0x2bc6
movl $0xa, -0x30(%rbp)
movl $0x5, -0x2c(%rbp)
cmpq $0x0, 0x10(%rax)
je 0x299a
leaq -0x30(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
movq %rax, %rdi
callq *0x18(%rax)
movq 0x3840(%rip), %rdi      # 0x5fd8
movl %eax, %esi
callq 0x2160
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2090
movq -0x200(%rbp), %rdi
cmpq %r14, %rdi
je 0x27e4
movq -0x1f0(%rbp), %rsi
incq %rsi
callq 0x2100
leaq -0x200(%rbp), %rsi
movq %r14, (%rsi)
movw $0x2a, 0x10(%rsi)
movq $0x1, 0x8(%rsi)
leaq -0xa0(%rbp), %rdi
callq 0x2bc6
movl $0xa, -0x30(%rbp)
movl $0x5, -0x2c(%rbp)
cmpq $0x0, 0x10(%rax)
je 0x299f
leaq -0x30(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
movq %rax, %rdi
callq *0x18(%rax)
movq 0x37a2(%rip), %rdi      # 0x5fd8
movl %eax, %esi
callq 0x2160
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2090
movq -0x200(%rbp), %rdi
cmpq %r14, %rdi
je 0x2882
movq -0x1f0(%rbp), %rsi
incq %rsi
callq 0x2100
leaq -0x200(%rbp), %rsi
movq %r14, (%rsi)
movw $0x25, 0x10(%rsi)
movq $0x1, 0x8(%rsi)
leaq -0xa0(%rbp), %rdi
callq 0x2bc6
movl $0xa, -0x30(%rbp)
movl $0x5, -0x2c(%rbp)
cmpq $0x0, 0x10(%rax)
je 0x29a4
leaq -0x30(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
movq %rax, %rdi
callq *0x18(%rax)
movq 0x3704(%rip), %rdi      # 0x5fd8
movl %eax, %esi
callq 0x2160
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2090
movq -0x200(%rbp), %rdi
cmpq %r14, %rdi
je 0x2920
movq -0x1f0(%rbp), %rsi
incq %rsi
callq 0x2100
leaq -0xa0(%rbp), %rdi
callq 0x2c7c
movq -0x40(%rbp), %rax
testq %rax, %rax
je 0x2943
leaq -0x50(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0x60(%rbp), %rax
testq %rax, %rax
je 0x295a
leaq -0x70(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0xb0(%rbp), %rax
testq %rax, %rax
je 0x2977
leaq -0xc0(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
xorl %eax, %eax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2070
callq 0x2070
callq 0x2070
callq 0x2070
callq 0x2070
callq 0x2070
jmp 0x2a6d
jmp 0x2a6d
jmp 0x2a6d
movq %rax, %rbx
movl $0x100, %r14d           # imm = 0x100
leaq (%r14,%rbp), %rdi
addq $-0x200, %rdi           # imm = 0xFE00
callq 0x2b7a
addq $-0x40, %r14
cmpq $-0x40, %r14
jne 0x29c1
jmp 0x2a16
jmp 0x29e5
jmp 0x29e5
jmp 0x29e5
jmp 0x29e5
movq %rax, %rbx
movq -0x200(%rbp), %rdi
cmpq %r14, %rdi
je 0x2a03
movq -0x1f0(%rbp), %rsi
incq %rsi
callq 0x2100
leaq -0xa0(%rbp), %rdi
callq 0x2c7c
jmp 0x2a16
jmp 0x2a6d
movq %rax, %rbx
movq -0x40(%rbp), %rax
testq %rax, %rax
je 0x2a2d
leaq -0x50(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0x60(%rbp), %rax
testq %rax, %rax
je 0x2a44
leaq -0x70(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0xb0(%rbp), %rax
testq %rax, %rax
je 0x2a61
leaq -0xc0(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq %rbx, %rdi
callq 0x2170
jmp 0x2a6d
jmp 0x2a6d
movq %rax, %rdi
callq 0x2c45
nop