// File: iZhangHui[P]CppPrimer/CH02_VariablesAndBasicTypes/examples/Sales_data.cc
// Function: main
int main()
{
	Sales_data data1, data2;

	// code to read into data1 and data2
	double price = 0;  // price per book, used to calculate total revenue

	// read the first transactions: ISBN, number of books sold, price per book
	std::cin >> data1.bookNo >> data1.units_sold >> price;
	// calculate total revenue from price and units_sold
	data1.revenue = data1.units_sold * price;

	// read the second transaction
	std::cin >> data2.bookNo >> data2.units_sold >> price;
	data2.revenue = data2.units_sold * price;

	// code to check whether data1 and data2 have the same ISBN
	//        and if so print the sum of data1 and data2
	if (data1.bookNo == data2.bookNo) {
		unsigned totalCnt = data1.units_sold + data2.units_sold;
		double totalRevenue = data1.revenue + data2.revenue;

		// print: ISBN, total sold, total revenue, average price per book
		std::cout << data1.bookNo << " " << totalCnt 
		          << " " << totalRevenue << " ";
		if (totalCnt != 0)
			std::cout << totalRevenue/totalCnt << std::endl;
		else
			std::cout  << "(no sales)" << std::endl;

		return 0;  // indicate success
	} else {  // transactions weren't for the same ISBN
		std::cerr << "Data must refer to the same ISBN" 
		          << std::endl;
		return -1; // indicate failure
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movl $0x0, -0x4(%rbp)
leaq -0x38(%rbp), %rdi
movq %rdi, -0xa0(%rbp)
callq 0x1560
leaq -0x68(%rbp), %rdi
callq 0x1560
movq -0xa0(%rbp), %rsi
movq $0x0, -0x70(%rbp)
movq 0x2d07(%rip), %rdi      # 0x3fd8
callq 0x10e0
movq %rax, -0x98(%rbp)
jmp 0x12df
movq -0x98(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x1030
movq %rax, -0xa8(%rbp)
jmp 0x12f8
movq -0xa8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1090
jmp 0x130a
movl -0x18(%rbp), %eax
cvtsi2sd %rax, %xmm0
movsd -0x70(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0x10(%rbp)
movq 0x2cb1(%rip), %rdi      # 0x3fd8
leaq -0x68(%rbp), %rsi
callq 0x10e0
movq %rax, -0xb0(%rbp)
jmp 0x1339
movq -0xb0(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x1030
movq %rax, -0xb8(%rbp)
jmp 0x1352
movq -0xb8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1090
jmp 0x1364
movl -0x48(%rbp), %eax
cvtsi2sd %rax, %xmm0
mulsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x15a0
testb $0x1, %al
jne 0x138c
jmp 0x14f7
movl -0x18(%rbp), %eax
movl -0x48(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x80(%rbp)
movsd -0x10(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movq 0x2c14(%rip), %rdi      # 0x3fc8
leaq -0x38(%rbp), %rsi
callq 0x10b0
movq %rax, -0xc0(%rbp)
jmp 0x13c6
movq -0xc0(%rbp), %rdi
leaq 0xc30(%rip), %rsi       # 0x2004
callq 0x10c0
movq %rax, -0xc8(%rbp)
jmp 0x13e2
movq -0xc8(%rbp), %rdi
movl -0x80(%rbp), %esi
callq 0x1100
movq %rax, -0xd0(%rbp)
jmp 0x13fa
movq -0xd0(%rbp), %rdi
leaq 0xbfc(%rip), %rsi       # 0x2004
callq 0x10c0
movq %rax, -0xd8(%rbp)
jmp 0x1416
movq -0xd8(%rbp), %rdi
movsd -0x88(%rbp), %xmm0
callq 0x1140
movq %rax, -0xe0(%rbp)
jmp 0x1433
movq -0xe0(%rbp), %rdi
leaq 0xbc3(%rip), %rsi       # 0x2004
callq 0x10c0
jmp 0x1448
cmpl $0x0, -0x80(%rbp)
je 0x14b1
movsd -0x88(%rbp), %xmm0
movl -0x80(%rbp), %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movq 0x2b5f(%rip), %rdi      # 0x3fc8
callq 0x1140
movq %rax, -0xe8(%rbp)
jmp 0x1477
movq -0xe8(%rbp), %rdi
movq 0x2b33(%rip), %rsi      # 0x3fb8
callq 0x10d0
jmp 0x148c
jmp 0x14e4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x1630
leaq -0x38(%rbp), %rdi
callq 0x1630
jmp 0x1557
movq 0x2b10(%rip), %rdi      # 0x3fc8
leaq 0xb47(%rip), %rsi       # 0x2006
callq 0x10c0
movq %rax, -0xf0(%rbp)
jmp 0x14cd
movq -0xf0(%rbp), %rdi
movq 0x2add(%rip), %rsi      # 0x3fb8
callq 0x10d0
jmp 0x14e2
jmp 0x14e4
movl $0x0, -0x4(%rbp)
movl $0x1, -0x8c(%rbp)
jmp 0x1539
movq 0x2af2(%rip), %rdi      # 0x3ff0
leaq 0xb0c(%rip), %rsi       # 0x2011
callq 0x10c0
movq %rax, -0xf8(%rbp)
jmp 0x1513
movq -0xf8(%rbp), %rdi
movq 0x2a97(%rip), %rsi      # 0x3fb8
callq 0x10d0
jmp 0x1528
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x8c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x1630
leaq -0x38(%rbp), %rdi
callq 0x1630
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x78(%rbp), %rdi
callq 0x1130