// File: iZhangHui[P]CppPrimer/CH03_StringsVectorsAndArrays/examples/ptr_traversal2.cc
// Function: main
int main()
{
	int ia[] = {0,1,2,3,4,5,6,7,8,9};

	int *p = ia; // p points to the first element in ia
	++p;           // p points to ia[1]

	int *e = &ia[10]; // pointer just past the last element in ia
	for (int *b = ia; b != e; ++b)
		cout << *b << " "; // print the elements in ia
	cout << endl;

	const size_t sz = 10;
	int arr[sz];  // array of 10 ints

	for (auto &n : arr) // for each element in arr
		cin >> n;  // read values from the standard input

	for (auto i : arr)
		cout << i << " ";
	cout << endl;

	// pbeg points to the first and 
	// pend points just past the last element in arr
	int *pbeg = begin(arr),  *pend = end(arr);

	// find the first negative element, 
	// stopping if we've seen all the elements
	while (pbeg != pend && *pbeg >= 0)
		++pbeg;
	if (pbeg == pend)
		cout << "no negative elements in arr" << endl;
	else
		cout << "first negative number was " << *pbeg << endl;
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movl $0x0, -0x4(%rbp)
leaq -0x30(%rbp), %rdi
leaq 0xe13(%rip), %rsi       # 0x2010
movl $0x28, %edx
callq 0x1040
leaq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
leaq -0x30(%rbp), %rax
addq $0x28, %rax
movq %rax, -0x40(%rbp)
leaq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
je 0x1268
movq -0x48(%rbp), %rax
movl (%rax), %esi
movq 0x2d8a(%rip), %rdi      # 0x3fd0
callq 0x1090
movq %rax, %rdi
leaq 0xe18(%rip), %rsi       # 0x206d
callq 0x1060
movq -0x48(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x48(%rbp)
jmp 0x122f
movq 0x2d61(%rip), %rdi      # 0x3fd0
movq 0x2d4a(%rip), %rsi      # 0x3fc0
callq 0x1070
movq $0xa, -0x50(%rbp)
leaq -0x80(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rax
addq $0x28, %rax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x98(%rbp), %rax
je 0x12f3
movq -0x90(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rsi
movq 0x2d06(%rip), %rdi      # 0x3fe0
callq 0x1030
movq -0x90(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x90(%rbp)
jmp 0x12ae
leaq -0x80(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rax
addq $0x28, %rax
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rax
cmpq -0xb8(%rbp), %rax
je 0x1372
movq -0xb0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xbc(%rbp)
movl -0xbc(%rbp), %esi
movq 0x2c86(%rip), %rdi      # 0x3fd0
callq 0x1090
movq %rax, %rdi
leaq 0xd14(%rip), %rsi       # 0x206d
callq 0x1060
movq -0xb0(%rbp), %rax
addq $0x4, %rax
movq %rax, -0xb0(%rbp)
jmp 0x131e
movq 0x2c57(%rip), %rdi      # 0x3fd0
movq 0x2c40(%rip), %rsi      # 0x3fc0
callq 0x1070
leaq -0x80(%rbp), %rdi
callq 0x1470
movq %rax, -0xc8(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x1480
movq %rax, -0xd0(%rbp)
movq -0xc8(%rbp), %rcx
xorl %eax, %eax
cmpq -0xd0(%rbp), %rcx
movb %al, -0xd1(%rbp)
je 0x13d0
movq -0xc8(%rbp), %rax
cmpl $0x0, (%rax)
setge %al
movb %al, -0xd1(%rbp)
movb -0xd1(%rbp), %al
testb $0x1, %al
jne 0x13dc
jmp 0x13f0
movq -0xc8(%rbp), %rax
addq $0x4, %rax
movq %rax, -0xc8(%rbp)
jmp 0x13a5
movq -0xc8(%rbp), %rax
cmpq -0xd0(%rbp), %rax
jne 0x1424
movq 0x2bc9(%rip), %rdi      # 0x3fd0
leaq 0xc2a(%rip), %rsi       # 0x2038
callq 0x1060
movq %rax, %rdi
movq 0x2ba3(%rip), %rsi      # 0x3fc0
callq 0x1070
jmp 0x1457
movq 0x2ba5(%rip), %rdi      # 0x3fd0
leaq 0xc22(%rip), %rsi       # 0x2054
callq 0x1060
movq %rax, %rdi
movq -0xc8(%rbp), %rax
movl (%rax), %esi
callq 0x1090
movq %rax, %rdi
movq 0x2b6e(%rip), %rsi      # 0x3fc0
callq 0x1070
xorl %eax, %eax
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)