// File: /workspace/github_cpp_makefile_star2_O1/iZhangHui[P]CppPrimer/CH19_SpecializedToolsAndTechniques/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

	// 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

	// memp can point to either shift operation in ShiftOps
	// memp points to the Lshift member
	function<int (ShiftOps*, int, int)> memp = &ShiftOps::Lshift;  

	ShiftOps shift(cout);      // declare an object to which to bind the member pointer
	binops.insert({"<<", bind(memp, &shift, _1, _2)}); 
	cout << binops["<<"](10, 5) << endl; // calls member function
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
xorps %xmm0, %xmm0
movaps %xmm0, -0x110(%rbp)
movaps %xmm0, -0x100(%rbp)
leaq -0x35(%rip), %r13       # 0x22ec
movq %r13, -0x110(%rbp)
leaq 0xf67(%rip), %r14       # 0x3296
movq %r14, -0xf8(%rbp)
leaq 0xf67(%rip), %r12       # 0x32a4
movq %r12, -0x100(%rbp)
movaps %xmm0, -0xf0(%rbp)
leaq 0xf7a(%rip), %rax       # 0x32cc
movq %rax, -0xd8(%rbp)
leaq 0xf7a(%rip), %rax       # 0x32da
movq %rax, -0xe0(%rbp)
movaps %xmm0, -0xd0(%rbp)
leaq 0x983(%rip), %rax       # 0x2cf8
movq %rax, -0xb8(%rbp)
leaq 0x981(%rip), %rax       # 0x2d04
movq %rax, -0xc0(%rbp)
movq 0x4c47(%rip), %rdi      # 0x6fd8
movl $0x6, %esi
callq 0x2180
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2100
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movl $0x4, -0x258(%rbp)
movl $0x2, -0xb0(%rbp)
cmpq $0x0, -0xe0(%rbp)
je 0x2b22
leaq -0xf0(%rbp), %rdi
leaq -0x258(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
callq *0x18(%rdi)
movq 0x4bd2(%rip), %rdi      # 0x6fd8
movl %eax, %esi
callq 0x2180
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2100
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movl $0x4, -0x258(%rbp)
movl $0x2, -0xb0(%rbp)
cmpq $0x0, -0xc0(%rbp)
je 0x2b22
leaq -0xd0(%rbp), %rdi
leaq -0x258(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
callq *0x18(%rdi)
movq 0x4b60(%rip), %rdi      # 0x6fd8
movl %eax, %esi
callq 0x2180
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2100
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
leaq -0x248(%rbp), %r15
movq %r15, -0x10(%r15)
movw $0x2b, (%r15)
movl $0x1, %eax
movq %rax, -0x8(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r15)
movups %xmm0, 0x20(%r15)
movq %r13, 0x10(%r15)
movq %r14, 0x28(%r15)
movq %r12, 0x20(%r15)
leaq -0x208(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movw $0x2d, (%rcx)
movq %rax, -0x8(%rcx)
movups %xmm0, 0x10(%rcx)
leaq 0xa4d(%rip), %rdx       # 0x2f48
movq %rdx, 0x28(%rcx)
leaq 0xa4c(%rip), %rdx       # 0x2f52
movq %rdx, 0x20(%rcx)
leaq -0x1c8(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movw $0x2f, (%rcx)
movq %rax, -0x8(%rcx)
movups %xmm0, 0x10(%rcx)
movq $0x0, 0x20(%rcx)
leaq 0xd9b(%rip), %rdx       # 0x32cc
movq %rdx, 0x28(%rcx)
leaq 0xd9e(%rip), %rdx       # 0x32da
movq %rdx, 0x20(%rcx)
leaq -0x188(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movw $0x2a, (%rcx)
movq %rax, -0x8(%rcx)
movups %xmm0, 0x10(%rcx)
leaq 0x741(%rip), %rdx       # 0x2ca0
movq %rdx, 0x28(%rcx)
leaq 0x742(%rip), %rdx       # 0x2cac
movq %rdx, 0x20(%rcx)
leaq -0x148(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movw $0x25, (%rbx)
movq %rax, -0x8(%rbx)
movups %xmm0, 0x10(%rbx)
leaq 0x73d(%rip), %rax       # 0x2cca
movq %rax, 0x28(%rbx)
leaq 0x742(%rip), %rax       # 0x2cda
movq %rax, 0x20(%rbx)
leaq -0xb0(%rbp), %rdi
leaq -0x258(%rbp), %rsi
leaq -0x80(%rbp), %rcx
leaq -0x50(%rbp), %r8
movl $0x5, %edx
callq 0x2d22
xorl %r14d, %r14d
movq -0x128(%rbp,%r14), %rax
testq %rax, %rax
je 0x25e1
leaq (%r14,%rbp), %rdi
addq $-0x138, %rdi           # imm = 0xFEC8
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0x158(%rbp,%r14), %rdi
leaq (%rbx,%r14), %rax
cmpq %rdi, %rax
je 0x2602
movq -0x148(%rbp,%r14), %rsi
incq %rsi
callq 0x2120
addq $-0x40, %r14
cmpq $-0x140, %r14           # imm = 0xFEC0
jne 0x25bf
leaq -0x258(%rbp), %rsi
movq %r15, (%rsi)
movw $0x2b, 0x10(%rsi)
movq $0x1, 0x8(%rsi)
leaq -0xb0(%rbp), %rdi
callq 0x2df0
movl $0xa, -0x80(%rbp)
movl $0x5, -0x50(%rbp)
cmpq $0x0, 0x10(%rax)
je 0x2b27
leaq -0x80(%rbp), %rsi
leaq -0x50(%rbp), %rdx
movq %rax, %rdi
callq *0x18(%rax)
movq 0x4977(%rip), %rdi      # 0x6fd8
movl %eax, %esi
callq 0x2180
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2100
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq -0x258(%rbp), %rdi
cmpq %r15, %rdi
je 0x26ad
movq -0x248(%rbp), %rsi
incq %rsi
callq 0x2120
leaq -0x258(%rbp), %rsi
movq %r15, (%rsi)
movw $0x2d, 0x10(%rsi)
movq $0x1, 0x8(%rsi)
leaq -0xb0(%rbp), %rdi
callq 0x2df0
movl $0xa, -0x80(%rbp)
movl $0x5, -0x50(%rbp)
cmpq $0x0, 0x10(%rax)
je 0x2b2c
leaq -0x80(%rbp), %rsi
leaq -0x50(%rbp), %rdx
movq %rax, %rdi
callq *0x18(%rax)
movq 0x48d9(%rip), %rdi      # 0x6fd8
movl %eax, %esi
callq 0x2180
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2100
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq -0x258(%rbp), %rdi
cmpq %r15, %rdi
je 0x274b
movq -0x248(%rbp), %rsi
incq %rsi
callq 0x2120
leaq -0x258(%rbp), %rsi
movq %r15, (%rsi)
movw $0x2f, 0x10(%rsi)
movq $0x1, 0x8(%rsi)
leaq -0xb0(%rbp), %rdi
callq 0x2df0
movl $0xa, -0x80(%rbp)
movl $0x5, -0x50(%rbp)
cmpq $0x0, 0x10(%rax)
je 0x2b31
leaq -0x80(%rbp), %rsi
leaq -0x50(%rbp), %rdx
movq %rax, %rdi
callq *0x18(%rax)
movq 0x483b(%rip), %rdi      # 0x6fd8
movl %eax, %esi
callq 0x2180
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2100
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq -0x258(%rbp), %rdi
cmpq %r15, %rdi
je 0x27e9
movq -0x248(%rbp), %rsi
incq %rsi
callq 0x2120
leaq -0x258(%rbp), %rsi
movq %r15, (%rsi)
movw $0x2a, 0x10(%rsi)
movq $0x1, 0x8(%rsi)
leaq -0xb0(%rbp), %rdi
callq 0x2df0
movl $0xa, -0x80(%rbp)
movl $0x5, -0x50(%rbp)
cmpq $0x0, 0x10(%rax)
je 0x2b36
leaq -0x80(%rbp), %rsi
leaq -0x50(%rbp), %rdx
movq %rax, %rdi
callq *0x18(%rax)
movq 0x479d(%rip), %rdi      # 0x6fd8
movl %eax, %esi
callq 0x2180
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2100
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq -0x258(%rbp), %rdi
cmpq %r15, %rdi
je 0x2887
movq -0x248(%rbp), %rsi
incq %rsi
callq 0x2120
leaq -0x258(%rbp), %rsi
movq %r15, (%rsi)
movw $0x25, 0x10(%rsi)
movq $0x1, 0x8(%rsi)
leaq -0xb0(%rbp), %rdi
callq 0x2df0
movl $0xa, -0x80(%rbp)
movl $0x5, -0x50(%rbp)
cmpq $0x0, 0x10(%rax)
je 0x2b3b
leaq -0x80(%rbp), %rsi
leaq -0x50(%rbp), %rdx
movq %rax, %rdi
callq *0x18(%rax)
movq 0x46ff(%rip), %rbx      # 0x6fd8
movq %rbx, %rdi
movl %eax, %esi
callq 0x2180
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2100
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq -0x258(%rbp), %rdi
cmpq %r15, %rdi
je 0x2928
movq -0x248(%rbp), %rsi
incq %rsi
callq 0x2120
xorps %xmm0, %xmm0
leaq -0x50(%rbp), %rsi
movaps %xmm0, (%rsi)
movaps %xmm0, 0x10(%rsi)
leaq 0x533(%rip), %rax       # 0x2e70
movq %rax, (%rsi)
movq $0x0, 0x8(%rsi)
leaq 0x103d(%rip), %rax      # 0x398c
movq %rax, 0x18(%rsi)
leaq 0x105a(%rip), %rax      # 0x39b4
movq %rax, 0x10(%rsi)
movq %rbx, -0x118(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x317a
leaq -0x118(%rbp), %rax
leaq -0x80(%rbp), %rdx
movq %rax, 0x20(%rdx)
leaq 0x1690(%rip), %rsi      # 0x4014
leaq -0x258(%rbp), %rdi
callq 0x2ed4
leaq -0xb0(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0x39e2
movq -0x228(%rbp), %rax
testq %rax, %rax
je 0x29c0
leaq -0x238(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0x258(%rbp), %rdi
cmpq %r15, %rdi
je 0x29db
movq -0x248(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0x70(%rbp), %rax
testq %rax, %rax
je 0x29f2
leaq -0x80(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq -0x258(%rbp), %rsi
movq %r15, (%rsi)
movw $0x3c3c, 0x10(%rsi)     # imm = 0x3C3C
movq $0x2, 0x8(%rsi)
movb $0x0, 0x12(%rsi)
leaq -0xb0(%rbp), %rdi
callq 0x2df0
movl $0xa, -0x80(%rbp)
movl $0x5, -0x54(%rbp)
cmpq $0x0, 0x10(%rax)
je 0x2b40
leaq -0x80(%rbp), %rsi
leaq -0x54(%rbp), %rdx
movq %rax, %rdi
callq *0x18(%rax)
movq 0x4590(%rip), %rdi      # 0x6fd8
movl %eax, %esi
callq 0x2180
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2100
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq -0x258(%rbp), %rdi
cmpq %r15, %rdi
je 0x2a94
movq -0x248(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0x40(%rbp), %rax
testq %rax, %rax
je 0x2aab
leaq -0x50(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq -0xb0(%rbp), %rdi
callq 0x31e0
movq -0xc0(%rbp), %rax
testq %rax, %rax
je 0x2ad4
leaq -0xd0(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0xe0(%rbp), %rax
testq %rax, %rax
je 0x2af1
leaq -0xf0(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0x100(%rbp), %rax
testq %rax, %rax
je 0x2b0e
leaq -0x110(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
xorl %eax, %eax
addq $0x238, %rsp            # imm = 0x238
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
callq 0x2070
jmp 0x2c97
jmp 0x2c97
jmp 0x2c97
jmp 0x2c97
jmp 0x2c97
jmp 0x2c97
movq %rax, %rbx
leaq -0x258(%rbp), %rdi
callq 0x2da4
jmp 0x2b77
movq %rax, %rbx
movq -0x70(%rbp), %rax
testq %rax, %rax
je 0x2bdd
leaq -0x80(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x2bdd
jmp 0x2c97
movq %rax, %rbx
jmp 0x2bdd
movq %rax, %rbx
movl $0x100, %r14d           # imm = 0x100
leaq (%r14,%rbp), %rdi
addq $-0x258, %rdi           # imm = 0xFDA8
callq 0x2da4
addq $-0x40, %r14
cmpq $-0x40, %r14
jne 0x2ba3
jmp 0x2c32
movq %rax, %rbx
movq -0x258(%rbp), %rdi
cmpq %r15, %rdi
je 0x2bdd
movq -0x248(%rbp), %rsi
incq %rsi
callq 0x2120
movq -0x40(%rbp), %rax
testq %rax, %rax
je 0x2c21
leaq -0x50(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x2c21
jmp 0x2c97
jmp 0x2c03
jmp 0x2c03
jmp 0x2c03
jmp 0x2c03
movq %rax, %rbx
movq -0x258(%rbp), %rdi
cmpq %r15, %rdi
je 0x2c21
movq -0x248(%rbp), %rsi
incq %rsi
callq 0x2120
leaq -0xb0(%rbp), %rdi
callq 0x31e0
jmp 0x2c32
movq %rax, %rbx
movq -0xc0(%rbp), %rax
testq %rax, %rax
je 0x2c4f
leaq -0xd0(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0xe0(%rbp), %rax
testq %rax, %rax
je 0x2c6c
leaq -0xf0(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq -0x100(%rbp), %rax
testq %rax, %rax
je 0x2c89
leaq -0x110(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq %rbx, %rdi
callq 0x2190
jmp 0x2c97
jmp 0x2c97
jmp 0x2c97
movq %rax, %rdi
callq 0x2f3a
nop