// File: /workspace/github_cpp_makefile_star2_O2/DimitriDaSilva[P]42_ft_containers/containers/vector.hpp
// Function: ft::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>::insert(ft::random_access_iterator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
iterator
		insert(iterator position, const value_type& val)
		{
			iterator	ret_position = NULL;
			value_type*	new_vec;
			size_type	new_size = _size + 1;
			size_type	new_capacity;
			iterator 	it_old;
			iterator 	it_new;

			// New element requires reallocation because vector is full
			if (new_size > _capacity)
			{
				new_capacity = get_new_capacity(new_size);

				// Reallocate new. If empty, size 2, otherwise * 2
				new_vec = _allocator.allocate(new_capacity);

				// Copy sequence to new array
				it_old = begin();
				it_new = new_vec;
				// We need to parse until we didn't reach the end AND we haven't
				// found the position
				while (it_old != end() || ret_position == NULL)
				{
					// Position found
					if (it_old == position)
					{
						ret_position = it_new;
						_allocator.construct(&*it_new++, val);
					}
					// In the case the position is the last element, we need to
					// make sure we won't be constructing beyond the sequence
					if (it_old != end())
						_allocator.construct(&*it_new++, *it_old++);
				}
				// Update private data of vector
				this->~vector();
				_start = new_vec;
				_size = new_size;
				_capacity = new_capacity;
			}
			// No need to reallocate so iterator position will still be valid
			else
			{
				insert(position, 1, val);
				ret_position = position;
			}

			return ret_position;
		}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %r14
movq %rsi, %r8
movq %rdi, %r15
andq $0x0, (%rdi)
movq 0x18(%rsi), %rax
incq %rax
movq %rax, -0x38(%rbp)
cmpq 0x20(%rsi), %rax
jbe 0x295ba
movq %rcx, -0x50(%rbp)
leaq -0x38(%rbp), %rsi
movq %r8, %rdi
movq %r8, -0x30(%rbp)
callq 0x2336e
movq %rax, %rsi
movq -0x30(%rbp), %rax
leaq 0x10(%rax), %rdi
xorl %r12d, %r12d
movq %rsi, -0x48(%rbp)
xorl %edx, %edx
callq 0x22700
movq -0x30(%rbp), %rsi
movq %rax, %rdi
movq 0x8(%rsi), %rax
movq %rdi, -0x40(%rbp)
movq %rax, %r13
movq 0x18(%rsi), %rcx
movq %rcx, %rdx
shlq $0x5, %rdx
addq %rax, %rdx
cmpq %rdx, %r13
jne 0x2953c
testq %r12, %r12
jne 0x29595
cmpq (%r14), %r13
jne 0x29560
movq %rdi, (%r15)
leaq 0x20(%rdi), %r12
movq -0x50(%rbp), %rsi
callq 0x51f0
movq -0x30(%rbp), %rsi
movq 0x8(%rsi), %rax
movq 0x18(%rsi), %rcx
movq %r12, %rdi
shlq $0x5, %rcx
addq %rax, %rcx
cmpq %rcx, %r13
je 0x29590
leaq 0x20(%rdi), %r12
movq %r14, %rbx
leaq 0x20(%r13), %r14
movq %r13, %rsi
callq 0x51f0
movq -0x30(%rbp), %rsi
movq 0x8(%rsi), %rax
movq %r12, %rdi
movq %r14, %r13
movq %rbx, %r14
movq (%r15), %r12
jmp 0x29524
movq (%rsi), %rax
movq %rsi, %rdi
movq %rsi, %r14
callq *(%rax)
movq -0x40(%rbp), %rax
movq %rax, 0x8(%r14)
movq -0x38(%rbp), %rax
movq %rax, 0x18(%r14)
movq -0x48(%rbp), %rax
movq %rax, 0x20(%r14)
jmp 0x295da
movq (%r14), %rax
leaq -0x58(%rbp), %rsi
movq %rax, (%rsi)
pushq $0x1
popq %rdx
movq %r8, %rdi
callq 0x295ec
cmpq %r14, %r15
je 0x295da
movq (%r14), %rax
movq %rax, (%r15)
movq %r15, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq