// File: iZhangHui[P]CppPrimer/CH12_DynamicMemory/examples/oknew.cc
// Function: main
int main()
{
	// if allocation fails, new returns a null pointer
	int *p1 = new int; // if allocation fails, new throws std::bad_alloc
	int *p2 = new (nothrow) int; // if allocation fails, new returns a null pointer

	int i0;                  // named, uninitialized int variable

	int *p0 = new int;       // pi points to a dynamically allocated,
	                         // unnamed, uninitialized int
	delete p0;  // frees the memory to which pi points

	// named initialized variables
	int i(1024);             // value of i is 1024
	string s(10, '9');       // value of s is "9999999999"
	vector<int> v = {0,1,2,3,4,5,6,7,8,9};

	// unnamed, initialized dynamically allocated objects
	int *pi = new int(1024); // object to which pi points has value 1024
	string *ps = new string(10, '9');   // *ps is "9999999999"
	// vector with ten elements with values from 0 to 9
	vector<int> *pv = new vector<int>{0,1,2,3,4,5,6,7,8,9};

	cout << "*pi: " << *pi
	     << "\ti: " << i << endl
	     << "*ps: " << *ps
	     << "\ts: " << s << endl;

	for (auto b = pv->begin(); b != pv->end(); ++b)
		cout << *b << " ";
	cout << endl;

	// when we're done using the memory must delete the pointers
	delete pi;  // frees the memory to which pi points
	delete ps;  // frees the string to which ps points
	            // the string destructor frees the space used by its data
	delete pv;  // frees the memory for the vector
	            // which also destroys the elements in that vector

	// call get_size to determine how many ints to allocate
	int *pia = new int[get_size()]; // pia points to the first of these ints
	delete [] pia; // brackets used to delete pointer to element in an array
	typedef int arrT[42];  // arrT names the type array of 42 ints
	int *p = new arrT;     // allocates an array of 42 ints; p points to the first one
	delete [] p;           // brackets are necessary because we allocated an array

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movl $0x0, -0x4(%rbp)
movl $0x4, %edi
callq 0x10e0
movq %rax, -0x10(%rbp)
movl $0x4, %edi
movq 0x3cac(%rip), %rsi      # 0x4fb8
callq 0x1180
movq %rax, -0x18(%rbp)
movl $0x4, %edi
callq 0x10e0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x150(%rbp)
cmpq $0x0, %rax
je 0x1345
movq -0x150(%rbp), %rdi
movl $0x4, %esi
callq 0x10f0
movl $0x400, -0x2c(%rbp)     # imm = 0x400
leaq -0x51(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x1170
movq -0x158(%rbp), %rcx
leaq -0x50(%rbp), %rdi
movl $0xa, %esi
movl $0x39, %edx
callq 0x1090
jmp 0x1378
leaq -0x51(%rbp), %rdi
callq 0x1110
movabsq $0x900000008, %rax      # imm = 0x900000008
movq %rax, -0xa0(%rbp)
movups 0x1c7b(%rip), %xmm0     # 0x3014
movaps %xmm0, -0xb0(%rbp)
movups 0x1c5d(%rip), %xmm0     # 0x3004
movaps %xmm0, -0xc0(%rbp)
leaq -0xc0(%rbp), %rax
movq %rax, -0x90(%rbp)
movq $0xa, -0x88(%rbp)
leaq -0xc1(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x1920
movq -0x160(%rbp), %rcx
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
leaq -0x80(%rbp), %rdi
callq 0x1940
jmp 0x13fa
leaq -0xc1(%rbp), %rdi
callq 0x19c0
movl $0x4, %edi
callq 0x10e0
movq %rax, -0x168(%rbp)
jmp 0x1419
movq -0x168(%rbp), %rax
movl $0x400, (%rax)          # imm = 0x400
movq %rax, -0xd0(%rbp)
movl $0x20, %edi
callq 0x10e0
movq %rax, -0x170(%rbp)
jmp 0x1440
movb $0x1, -0xda(%rbp)
leaq -0xd9(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x1170
movq -0x170(%rbp), %rdi
movq -0x178(%rbp), %rcx
movl $0xa, %esi
movl $0x39, %edx
callq 0x1090
jmp 0x1479
movb $0x0, -0xda(%rbp)
leaq -0xd9(%rbp), %rdi
callq 0x1110
movq -0x170(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl $0x18, %edi
callq 0x10e0
movq %rax, -0x180(%rbp)
jmp 0x14ad
movb $0x1, -0x122(%rbp)
movabsq $0x900000008, %rax      # imm = 0x900000008
movq %rax, -0x100(%rbp)
movups 0x1b70(%rip), %xmm0     # 0x303c
movaps %xmm0, -0x110(%rbp)
movups 0x1b52(%rip), %xmm0     # 0x302c
movaps %xmm0, -0x120(%rbp)
leaq -0x120(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq $0xa, -0xf0(%rbp)
leaq -0x121(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x1920
movq -0x180(%rbp), %rdi
movq -0x188(%rbp), %rcx
movq -0xf8(%rbp), %rsi
movq -0xf0(%rbp), %rdx
callq 0x1940
jmp 0x1530
movb $0x0, -0x122(%rbp)
leaq -0x121(%rbp), %rdi
callq 0x19c0
movq -0x180(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq 0x3a80(%rip), %rdi      # 0x4fd8
leaq 0x1af5(%rip), %rsi      # 0x3054
callq 0x10d0
movq %rax, -0x190(%rbp)
jmp 0x156d
movq -0x190(%rbp), %rdi
movq -0xd0(%rbp), %rax
movl (%rax), %esi
callq 0x1150
movq %rax, -0x198(%rbp)
jmp 0x158b
movq -0x198(%rbp), %rdi
leaq 0x1ac1(%rip), %rsi      # 0x305a
callq 0x10d0
movq %rax, -0x1a0(%rbp)
jmp 0x15a7
movq -0x1a0(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x1150
movq %rax, -0x1a8(%rbp)
jmp 0x15bf
movq -0x1a8(%rbp), %rdi
movq 0x39fb(%rip), %rsi      # 0x4fc8
callq 0x1100
movq %rax, -0x1b0(%rbp)
jmp 0x15db
movq -0x1b0(%rbp), %rdi
leaq 0x1a76(%rip), %rsi      # 0x305f
callq 0x10d0
movq %rax, -0x1b8(%rbp)
jmp 0x15f7
movq -0x1b8(%rbp), %rdi
movq -0xd8(%rbp), %rsi
callq 0x10c0
movq %rax, -0x1c0(%rbp)
jmp 0x1613
movq -0x1c0(%rbp), %rdi
leaq 0x1a44(%rip), %rsi      # 0x3065
callq 0x10d0
movq %rax, -0x1c8(%rbp)
jmp 0x162f
movq -0x1c8(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x10c0
movq %rax, -0x1d0(%rbp)
jmp 0x1648
movq -0x1d0(%rbp), %rdi
movq 0x3972(%rip), %rsi      # 0x4fc8
callq 0x1100
jmp 0x165d
movq -0xe8(%rbp), %rdi
callq 0x19e0
movq %rax, -0x130(%rbp)
movq -0xe8(%rbp), %rdi
callq 0x1a50
movq %rax, -0x138(%rbp)
leaq -0x130(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x1a10
testb $0x1, %al
jne 0x169f
jmp 0x17a1
leaq -0x130(%rbp), %rdi
callq 0x1a80
movl (%rax), %esi
movq 0x3924(%rip), %rdi      # 0x4fd8
callq 0x1150
movq %rax, -0x1d8(%rbp)
jmp 0x16c2
movq -0x1d8(%rbp), %rdi
leaq 0x198d(%rip), %rsi      # 0x305d
callq 0x10d0
jmp 0x16d7
jmp 0x16d9
leaq -0x130(%rbp), %rdi
callq 0x1aa0
jmp 0x1670
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x51(%rbp), %rdi
callq 0x1110
jmp 0x190c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xc1(%rbp), %rdi
callq 0x19c0
jmp 0x1903
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x18fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xd9(%rbp), %rdi
callq 0x1110
testb $0x1, -0xda(%rbp)
jne 0x1752
jmp 0x1763
movq -0x170(%rbp), %rdi
movl $0x20, %esi
callq 0x10f0
jmp 0x18fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x121(%rbp), %rdi
callq 0x19c0
testb $0x1, -0x122(%rbp)
jne 0x178b
jmp 0x179c
movq -0x180(%rbp), %rdi
movl $0x18, %esi
callq 0x10f0
jmp 0x18fa
movq 0x3830(%rip), %rdi      # 0x4fd8
movq 0x3819(%rip), %rsi      # 0x4fc8
callq 0x1100
jmp 0x17b6
movq -0xd0(%rbp), %rax
movq %rax, -0x1e0(%rbp)
cmpq $0x0, %rax
je 0x17db
movq -0x1e0(%rbp), %rdi
movl $0x4, %esi
callq 0x10f0
movq -0xd8(%rbp), %rax
movq %rax, -0x1e8(%rbp)
cmpq $0x0, %rax
je 0x180c
movq -0x1e8(%rbp), %rdi
callq 0x1080
movq -0x1e8(%rbp), %rdi
movl $0x20, %esi
callq 0x10f0
movq -0xe8(%rbp), %rax
movq %rax, -0x1f0(%rbp)
cmpq $0x0, %rax
je 0x183d
movq -0x1f0(%rbp), %rdi
callq 0x1ac0
movq -0x1f0(%rbp), %rdi
movl $0x18, %esi
callq 0x10f0
callq 0x12d0
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x1030
movq %rax, -0x1f8(%rbp)
jmp 0x1866
movq -0x1f8(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rax
movq %rax, -0x200(%rbp)
cmpq $0x0, %rax
je 0x1894
movq -0x200(%rbp), %rdi
callq 0x1120
movl $0xa8, %edi
callq 0x1030
movq %rax, -0x208(%rbp)
jmp 0x18a7
movq -0x208(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rax
movq %rax, -0x210(%rbp)
cmpq $0x0, %rax
je 0x18d5
movq -0x210(%rbp), %rdi
callq 0x1120
movl $0x0, -0x4(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x1ac0
leaq -0x50(%rbp), %rdi
callq 0x1080
movl -0x4(%rbp), %eax
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
leaq -0x80(%rbp), %rdi
callq 0x1ac0
leaq -0x50(%rbp), %rdi
callq 0x1080
movq -0x60(%rbp), %rdi
callq 0x1160
nopw %cs:(%rax,%rax)
nop