// File: /workspace/github_cpp_makefile_star2_O2/iZhangHui[P]CppPrimer/CH02_VariablesAndBasicTypes/examples/unsigned.cc
// Function: main
int main()
{
	unsigned u = 10, u2 = 42;
	std::cout << u2 - u << std::endl;  
	std::cout << u - u2 << std::endl; 

	int i = 10, i2 = 42;
	std::cout << i2 - i << std::endl;
	std::cout << i - i2 << std::endl;

	u = 42;
	i = 10;
	std::cout << i - u << std::endl;
	std::cout << u - i << std::endl;

	u = 10;
	i = -42;
	std::cout << i + i << std::endl;  // prints -84
	std::cout << u + i << std::endl;  // if 32-bit ints, prints 4294967264

	i = 10;
	std::cout << "good" << std::endl;
	while (i >= 0) {
		std::cout << i << std::endl;
		--i;
	}

	for (int i = 10; i >= 0; --i)
		std::cout << i << std::endl;

	for (unsigned u = 0; u <= 10; ++u) 
		std::cout << u << std::endl;  // prints 0 . . . 10

/* NOTE: the condition in the following loop 
         will run indefinitely
	// WRONG: u can never be less than 0; the condition will always succeed
	for (unsigned u = 10; u >= 0; --u)
    	std::cout << u << std::endl;
*/
	u = 11; // start the loop one past the first element we want to print
	while (u > 0) {
		 --u;        // decrement first, so that the last iteration will print 0
		std::cout << u << std::endl;  
	}

	// be wary of comparing ints and unsigned
	u = 10;
	i = -42;
	if (i < u)               // false: i is converted to unsigned
		std::cout << i << std::endl;
	else
		std::cout << u << std::endl;   // prints 10

	u = 42; u2 = 10;
	std::cout << u - u2 << std::endl; // ok: result is 32
	std::cout << u2 - u << std::endl; // ok: but the result will wrap around
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
movq 0x2dfe(%rip), %rbx      # 0x3fd8
pushq $0x20
popq %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1040
movq %rax, %rdi
callq 0x1030
movl $0xffffffe0, %esi       # imm = 0xFFFFFFE0
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x1030
pushq $0x20
popq %rsi
movq %rbx, %rdi
callq 0x1080
movq %rax, %rdi
callq 0x1030
pushq $-0x20
popq %rsi
movq %rbx, %rdi
callq 0x1080
movq %rax, %rdi
callq 0x1030
movl $0xffffffe0, %esi       # imm = 0xFFFFFFE0
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x1030
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1040
movq %rax, %rdi
callq 0x1030
pushq $-0x54
popq %rsi
movq %rbx, %rdi
callq 0x1080
movq %rax, %rdi
callq 0x1030
movl $0xffffffe0, %esi       # imm = 0xFFFFFFE0
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x1030
leaq 0xd81(%rip), %rsi       # 0x2004
movq %rbx, %rdi
callq 0x1060
movq %rax, %rdi
callq 0x1030
pushq $0xa
popq %r14
testl %r14d, %r14d
js 0x12b4
movq %rbx, %rdi
movl %r14d, %esi
callq 0x1080
movq %rax, %rdi
callq 0x1030
decl %r14d
jmp 0x1297
pushq $0xa
popq %rbx
movq 0x2d1a(%rip), %r14      # 0x3fd8
testl %ebx, %ebx
js 0x12d8
movq %r14, %rdi
movl %ebx, %esi
callq 0x1080
movq %rax, %rdi
callq 0x1030
decl %ebx
jmp 0x12be
xorl %ebx, %ebx
movq 0x2cf7(%rip), %r14      # 0x3fd8
cmpq $0xb, %rbx
je 0x12ff
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1040
movq %rax, %rdi
callq 0x1030
incq %rbx
jmp 0x12e1
pushq $0xa
popq %r14
movq 0x2cce(%rip), %rbx      # 0x3fd8
cmpq $-0x1, %r14
je 0x1328
movl %r14d, %esi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x1030
decq %r14
jmp 0x130a
movq 0x2ca9(%rip), %rbx      # 0x3fd8
pushq $0xa
popq %rsi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x1030
pushq $0x20
popq %rsi
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x1030
movl $0xffffffe0, %esi       # imm = 0xFFFFFFE0
movq %rbx, %rdi
callq 0x1040
movq %rax, %rdi
callq 0x1030
xorl %eax, %eax
popq %rbx
popq %r14
popq %rbp
retq