// File: /workspace/github_cpp_makefile_star2_O1/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
xorps %xmm0, %xmm0
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0x80(%rbp)
leaq -0x32(%rip), %r13       # 0x22cc
movq %r13, -0x90(%rbp)
leaq 0xa24(%rip), %r15       # 0x2d30
movq %r15, -0x78(%rbp)
leaq 0xa27(%rip), %r12       # 0x2d3e
movq %r12, -0x80(%rbp)
movaps %xmm0, -0x70(%rbp)
leaq 0xa40(%rip), %rax       # 0x2d66
movq %rax, -0x58(%rbp)
leaq 0xa43(%rip), %rax       # 0x2d74
movq %rax, -0x60(%rbp)
movaps %xmm0, -0x50(%rbp)
leaq 0x78c(%rip), %rax       # 0x2acc
movq %rax, -0x38(%rbp)
leaq 0x78d(%rip), %rax       # 0x2ad8
movq %rax, -0x40(%rbp)
movq 0x3c82(%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, -0xc0(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0x298c
leaq -0x70(%rbp), %rdi
leaq -0x200(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
callq *0x18(%rdi)
movq 0x3c13(%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, -0xc0(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x298c
leaq -0x50(%rbp), %rdi
leaq -0x200(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
callq *0x18(%rdi)
movq 0x3ba7(%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 0x3b6f(%rip), %rbx      # 0x5fd8
leaq 0x1b94(%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 0x3b3d(%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)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r14)
movups %xmm0, 0x20(%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)
movups %xmm0, 0x10(%rcx)
leaq 0x758(%rip), %rdx       # 0x2c52
movq %rdx, 0x28(%rcx)
leaq 0x757(%rip), %rdx       # 0x2c5c
movq %rdx, 0x20(%rcx)
leaq -0x170(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movw $0x2f, (%rcx)
movq %rax, -0x8(%rcx)
movups %xmm0, 0x10(%rcx)
movq $0x0, 0x20(%rcx)
leaq 0x836(%rip), %rdx       # 0x2d66
movq %rdx, 0x28(%rcx)
leaq 0x839(%rip), %rdx       # 0x2d74
movq %rdx, 0x20(%rcx)
leaq -0x130(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movw $0x2a, (%rcx)
movq %rax, -0x8(%rcx)
movups %xmm0, 0x10(%rcx)
leaq 0x516(%rip), %rdx       # 0x2a74
movq %rdx, 0x28(%rcx)
leaq 0x517(%rip), %rdx       # 0x2a80
movq %rdx, 0x20(%rcx)
leaq -0xf0(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movw $0x25, (%rbx)
movq %rax, -0x8(%rbx)
movups %xmm0, 0x10(%rbx)
leaq 0x512(%rip), %rax       # 0x2a9e
movq %rax, 0x28(%rbx)
leaq 0x517(%rip), %rax       # 0x2aae
movq %rax, 0x20(%rbx)
leaq -0xc0(%rbp), %rdi
leaq -0x200(%rbp), %rsi
leaq -0x30(%rbp), %rcx
leaq -0x2c(%rbp), %r8
movl $0x5, %edx
callq 0x2af6
xorl %r15d, %r15d
movq -0xd0(%rbp,%r15), %rax
testq %rax, %rax
je 0x25e0
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 0x2601
movq -0xf0(%rbp,%r15), %rsi
incq %rsi
callq 0x2100
addq $-0x40, %r15
cmpq $-0x140, %r15           # imm = 0xFEC0
jne 0x25be
leaq -0x200(%rbp), %rsi
movq %r14, (%rsi)
movw $0x2b, 0x10(%rsi)
movq $0x1, 0x8(%rsi)
leaq -0xc0(%rbp), %rdi
callq 0x2bc4
movl $0xa, -0x30(%rbp)
movl $0x5, -0x2c(%rbp)
cmpq $0x0, 0x10(%rax)
je 0x2991
leaq -0x30(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
movq %rax, %rdi
callq *0x18(%rax)
movq 0x3978(%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 0x26ac
movq -0x1f0(%rbp), %rsi
incq %rsi
callq 0x2100
leaq -0x200(%rbp), %rsi
movq %r14, (%rsi)
movw $0x2d, 0x10(%rsi)
movq $0x1, 0x8(%rsi)
leaq -0xc0(%rbp), %rdi
callq 0x2bc4
movl $0xa, -0x30(%rbp)
movl $0x5, -0x2c(%rbp)
cmpq $0x0, 0x10(%rax)
je 0x2996
leaq -0x30(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
movq %rax, %rdi
callq *0x18(%rax)
movq 0x38da(%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 0x274a
movq -0x1f0(%rbp), %rsi
incq %rsi
callq 0x2100
leaq -0x200(%rbp), %rsi
movq %r14, (%rsi)
movw $0x2f, 0x10(%rsi)
movq $0x1, 0x8(%rsi)
leaq -0xc0(%rbp), %rdi
callq 0x2bc4
movl $0xa, -0x30(%rbp)
movl $0x5, -0x2c(%rbp)
cmpq $0x0, 0x10(%rax)
je 0x299b
leaq -0x30(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
movq %rax, %rdi
callq *0x18(%rax)
movq 0x383c(%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 0x27e8
movq -0x1f0(%rbp), %rsi
incq %rsi
callq 0x2100
leaq -0x200(%rbp), %rsi
movq %r14, (%rsi)
movw $0x2a, 0x10(%rsi)
movq $0x1, 0x8(%rsi)
leaq -0xc0(%rbp), %rdi
callq 0x2bc4
movl $0xa, -0x30(%rbp)
movl $0x5, -0x2c(%rbp)
cmpq $0x0, 0x10(%rax)
je 0x29a0
leaq -0x30(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
movq %rax, %rdi
callq *0x18(%rax)
movq 0x379e(%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 0x2886
movq -0x1f0(%rbp), %rsi
incq %rsi
callq 0x2100
leaq -0x200(%rbp), %rsi
movq %r14, (%rsi)
movw $0x25, 0x10(%rsi)
movq $0x1, 0x8(%rsi)
leaq -0xc0(%rbp), %rdi
callq 0x2bc4
movl $0xa, -0x30(%rbp)
movl $0x5, -0x2c(%rbp)
cmpq $0x0, 0x10(%rax)
je 0x29a5
leaq -0x30(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
movq %rax, %rdi
callq *0x18(%rax)
movq 0x3700(%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 0x2924
movq -0x1f0(%rbp), %rsi
incq %rsi
callq 0x2100
leaq -0xc0(%rbp), %rdi
callq 0x2c7a
movq -0x40(%rbp), %rax
testq %rax, %rax
je 0x2947
leaq -0x50(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0x60(%rbp), %rax
testq %rax, %rax
je 0x295e
leaq -0x70(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0x80(%rbp), %rax
testq %rax, %rax
je 0x2978
leaq -0x90(%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 0x2a6b
jmp 0x2a6b
jmp 0x2a6b
movq %rax, %rbx
movl $0x100, %r14d           # imm = 0x100
leaq (%r14,%rbp), %rdi
addq $-0x200, %rdi           # imm = 0xFE00
callq 0x2b78
addq $-0x40, %r14
cmpq $-0x40, %r14
jne 0x29c2
jmp 0x2a17
jmp 0x29e6
jmp 0x29e6
jmp 0x29e6
jmp 0x29e6
movq %rax, %rbx
movq -0x200(%rbp), %rdi
cmpq %r14, %rdi
je 0x2a04
movq -0x1f0(%rbp), %rsi
incq %rsi
callq 0x2100
leaq -0xc0(%rbp), %rdi
callq 0x2c7a
jmp 0x2a17
jmp 0x2a6b
movq %rax, %rbx
movq -0x40(%rbp), %rax
testq %rax, %rax
je 0x2a2e
leaq -0x50(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0x60(%rbp), %rax
testq %rax, %rax
je 0x2a45
leaq -0x70(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0x80(%rbp), %rax
testq %rax, %rax
je 0x2a5f
leaq -0x90(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq %rbx, %rdi
callq 0x2170
jmp 0x2a6b
jmp 0x2a6b
movq %rax, %rdi
callq 0x2c43
nop