// File: /workspace/github_cpp_makefile_star2_O1/aadityanaik[P]LongNums/src/longnums.cpp
// Function: LongNumber::operator/(LongNumber)
LongNumber LongNumber::operator/(LongNumber X) {
	LongNumber quotient;
	if (X == 0) {
		//check if divisor is 0
		throw DivideByZeroException();
	} else if(digits.size() < X.digits.size()){
		return quotient;
	} else {		
		//following division algorithm uses Algorithm D from the Art of Computer Programming
		if(X.digits.size() > 1) {
			LongNumber dividend;
			LongNumber divisor;
			quotient.digits.pop_back();
			//Step 1- Normalize the divisor
			int normalFactor = (BASE) / (*(X.digits.end() - 1) + 1);
			dividend = absolute() * normalFactor;
			if(dividend.digits.size() == digits.size()) {
				dividend.digits.push_back(0);
			}
			divisor = X.absolute() * normalFactor;

			int n = X.digits.size();

			//step 2 and 7- loop
			for(int iter = (digits.size() - X.digits.size()); iter >= 0; iter--) {
				//step 3- approximating quotient
				long int q = (((long int)dividend.digits[iter + n] * BASE) + dividend.digits[iter + n - 1]) / divisor.digits[n-1];
				long int r = (((long int)dividend.digits[iter + n] * BASE) + dividend.digits[iter + n - 1]) % divisor.digits[n-1];
				
				int x = 0;
				bool flag;
				do {
					flag = false;
					if(q == BASE || 
					((long int)(q * divisor.digits[n-2]) > (r * BASE) + dividend.digits[iter + n - 2])) {
						q -= 1;
						r += divisor.digits[n-1];
						flag = true;
					}

					x++;
				} while(r < BASE && flag);

				//step 4- multiply and subtract
				LongNumber part_dividend;
				part_dividend.digits = std::vector<int>(dividend.digits.begin() + iter, dividend.digits.begin() + iter + n + 1);
				part_dividend = part_dividend - (divisor * q);

				if(part_dividend.negative) {
					//step 6- add back
					part_dividend = part_dividend.absolute() + divisor;
					part_dividend.digits.pop_back(); // ignoring carry generated
				}

				for(int i = iter; i < dividend.digits.size(); i++) {
					if(i - iter >= part_dividend.digits.size()) {
						dividend.digits[i] = 0;
					} else {
						dividend.digits[i] = part_dividend.digits[i - iter];
					}
				}
				
				quotient.digits.push_back(q);
			}
		} else {
			long int r = 0;
			for(int i = digits.size() - 1; i >= 0; i--) {
				quotient.digits.push_back((long int)(r * BASE + digits[i]) / X.digits[0]);
				
				r = (long int)(r * BASE + digits[i]) % X.digits[0];
				
			}
		}

		std::reverse(quotient.digits.begin(), quotient.digits.end());

		while(*(quotient.digits.end()-1) == 0 && quotient.digits.size() > 1) {
			quotient.digits.erase(quotient.digits.end()-1);
		}

		if(quotient != 0) {
			quotient.negative = negative ^ X.negative;
		}

		return quotient;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdx, %r15
movq %rsi, 0x40(%rsp)
movq %rdi, %rbx
callq 0x27ac
leaq 0x78(%rsp), %rsi
movq $0x0, (%rsi)
leaq 0x48(%rsp), %rdi
movq %rbx, 0x68(%rsp)
callq 0x2b68
leaq 0x48(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x3098
movl %eax, %ebp
movq (%r12), %rdi
testq %rdi, %rdi
je 0x47f8
callq 0x20b0
testb %bpl, %bpl
jne 0x4e08
movq 0x40(%rsp), %rbp
movq 0x8(%rbp), %rax
subq (%rbp), %rax
sarq $0x2, %rax
movq 0x8(%r15), %rcx
subq (%r15), %rcx
sarq $0x2, %rcx
cmpq %rcx, %rax
jb 0x4df3
cmpq $0x2, %rcx
jb 0x48fc
leaq 0x48(%rsp), %rdi
callq 0x27ac
leaq 0x78(%rsp), %rdi
callq 0x27ac
addq $-0x4, 0x8(%rbx)
movl 0x4899(%rip), %ebp      # 0x90e8
movq 0x8(%r15), %rax
movl -0x4(%rax), %r14d
leaq 0x20(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x2cde
incl %r14d
movl %ebp, %eax
cltd
idivl %r14d
movslq %eax, %r12
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
movq %r12, %rdx
callq 0x4668
leaq 0x48(%rsp), %rdi
movq %rsp, %rsi
callq 0x551e
movq 0x40(%rsp), %r14
movb 0x18(%rsp), %al
movb %al, 0x60(%rsp)
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x48aa
callq 0x20b0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x48b9
callq 0x20b0
movq 0x50(%rsp), %rsi
movq %rsi, %rax
subq 0x48(%rsp), %rax
movq 0x8(%r14), %rcx
subq (%r14), %rcx
cmpq %rcx, %rax
jne 0x4995
movl $0x0, (%rsp)
cmpq 0x58(%rsp), %rsi
je 0x4988
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x50(%rsp)
jmp 0x4995
decl %eax
js 0x4d28
movl %eax, %r14d
incq %r14
xorl %r13d, %r13d
leaq 0x48(%rsp), %r12
movslq 0x47cf(%rip), %rcx      # 0x90e8
imulq %r13, %rcx
movq (%rbp), %rax
movslq -0x4(%rax,%r14,4), %rax
addq %rcx, %rax
movq (%r15), %rcx
movslq (%rcx), %rcx
cqto
idivq %rcx
movl %eax, 0x48(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x494e
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x4959
movq %rbx, %rdi
movq %r12, %rdx
callq 0x57c4
movslq 0x4788(%rip), %rax      # 0x90e8
imulq %rax, %r13
movq (%rbp), %rax
movslq -0x4(%rax,%r14,4), %rax
addq %r13, %rax
movq (%r15), %rcx
movslq (%rcx), %rcx
cqto
idivq %rcx
movq %rdx, %r13
decq %r14
jg 0x4912
jmp 0x4d28
leaq 0x48(%rsp), %rdi
movq %rsp, %rdx
callq 0x57c4
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x2cde
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
movq %r12, %rdx
callq 0x4668
leaq 0x78(%rsp), %rdi
movq %rsp, %rsi
callq 0x551e
movb 0x18(%rsp), %al
movb %al, 0x90(%rsp)
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x49d8
callq 0x20b0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x49e7
callq 0x20b0
movq 0x8(%r15), %rax
movq %r15, 0x98(%rsp)
subq (%r15), %rax
movq %rax, %rcx
movq 0x8(%r14), %r15
subq (%r14), %r15
sarq $0x2, %rcx
shrq $0x2, %r15
subq %rcx, %r15
testl %r15d, %r15d
js 0x4cfd
movq %rax, %rcx
shlq $0x1e, %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
addq %rcx, %rdx
sarq $0x1e, %rdx
movq %rdx, 0xb0(%rsp)
movabsq $-0x200000000, %r14     # imm = 0xFFFFFFFE00000000
addq %rcx, %r14
sarq $0x1e, %r14
sarq $0x1e, %rcx
movq %rcx, 0xa0(%rsp)
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
shrq $0x2, %rax
cltq
movq %rax, 0xa8(%rsp)
leaq (,%r15,4), %rax
movq %rax, 0x70(%rsp)
leaq 0x20(%rsp), %r13
movq 0xa8(%rsp), %rax
leaq (%r15,%rax), %rcx
movq 0x48(%rsp), %rsi
movslq (%rsi,%rcx,4), %rdx
movslq 0x4654(%rip), %rdi      # 0x90e8
imulq %rdi, %rdx
movslq -0x4(%rsi,%rcx,4), %rax
addq %rdx, %rax
movq 0x78(%rsp), %r8
movq 0xb0(%rsp), %rdx
movslq (%r8,%rdx), %r9
cqto
idivq %r9
movq %rax, %rbp
cmpq %rdi, %rbp
je 0x4ada
movslq (%r8,%r14), %rax
imulq %rbp, %rax
movq %rdx, %r10
imulq %rdi, %r10
movslq -0x8(%rsi,%rcx,4), %r11
addq %r10, %r11
cmpq %r11, %rax
jle 0x4ae4
decq %rbp
addq %r9, %rdx
movb $0x1, %al
jmp 0x4ae6
xorl %eax, %eax
cmpq %rdi, %rdx
jge 0x4aef
testb %al, %al
jne 0x4ab9
movq %rsp, %rdi
callq 0x27ac
leaq (,%r15,4), %rsi
addq 0x48(%rsp), %rsi
movq 0xa0(%rsp), %rax
leaq (%rax,%rsi), %rdx
addq $0x4, %rdx
movq %r13, %rdi
leaq 0xb8(%rsp), %rcx
callq 0x566e
movq (%rsp), %rdi
movaps 0x20(%rsp), %xmm0
movq 0x30(%rsp), %rax
movq %rdi, 0x20(%rsp)
movups 0x8(%rsp), %xmm1
movaps %xmm0, (%rsp)
movq %rax, 0x10(%rsp)
movups %xmm1, 0x28(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
testq %rdi, %rdi
je 0x4b65
callq 0x20b0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4b74
callq 0x20b0
leaq 0xf8(%rsp), %r12
movq %r12, %rdi
leaq 0x78(%rsp), %rsi
movq %rbp, %rdx
callq 0x4668
movq %r13, %rdi
movq %r13, %rbx
movq %rsp, %r13
movq %r13, %rsi
movq %r12, %rdx
callq 0x3402
movq %r13, %rdi
movq %rbx, %rsi
callq 0x551e
movq %rbx, %r13
movb 0x38(%rsp), %al
movb %al, 0x18(%rsp)
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4bc5
callq 0x20b0
movq 0xf8(%rsp), %rdi
testq %rdi, %rdi
je 0x4bd7
callq 0x20b0
cmpb $0x1, 0x18(%rsp)
jne 0x4c66
leaq 0xb8(%rsp), %rdi
movq %rsp, %rsi
callq 0x2cde
leaq 0xd8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x78(%rsp), %rsi
callq 0x2822
movq %r13, %rdi
leaq 0xb8(%rsp), %rsi
movq %rbx, %rdx
callq 0x3118
movq %rsp, %rdi
movq %r13, %rsi
callq 0x551e
movb 0x38(%rsp), %al
movb %al, 0x18(%rsp)
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4c3c
callq 0x20b0
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x4c4e
callq 0x20b0
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x4c60
callq 0x20b0
addq $-0x4, 0x8(%rsp)
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r15, %rcx
jbe 0x4cb2
movq (%rsp), %rdx
movq 0x8(%rsp), %rsi
subq %rdx, %rsi
sarq $0x2, %rsi
addq 0x70(%rsp), %rax
xorl %edi, %edi
xorl %r8d, %r8d
cmpq %rdi, %rsi
jbe 0x4c9f
movl (%rdx,%rdi,4), %r8d
movl %r8d, (%rax,%rdi,4)
leaq (%r15,%rdi), %r8
incq %r8
incq %rdi
cmpq %r8, %rcx
ja 0x4c93
movl %ebp, 0x20(%rsp)
movq 0x68(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x4cd1
movl %ebp, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x4cd9
movq %r13, %rdx
callq 0x57c4
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4ce7
callq 0x20b0
leaq -0x1(%r15), %rax
addq $-0x4, 0x70(%rsp)
testq %r15, %r15
movq %rax, %r15
jg 0x4a78
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x4d0c
callq 0x20b0
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
movq 0x98(%rsp), %r15
movq 0x68(%rsp), %rbx
je 0x4d28
callq 0x20b0
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
cmpq %rcx, %rax
setne %dl
addq $-0x4, %rcx
cmpq %rax, %rcx
seta %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x4d67
addq $0x4, %rax
movl -0x4(%rax), %edx
movl (%rcx), %esi
movl %esi, -0x4(%rax)
movl %edx, (%rcx)
addq $-0x4, %rcx
leaq 0x4(%rax), %rdx
cmpq %rcx, %rax
movq %rdx, %rax
jb 0x4d4d
movq 0x8(%rbx), %rdx
cmpl $0x0, -0x4(%rdx)
jne 0x4da8
leaq -0x4(%rdx), %rdi
movq %rdx, %rax
subq (%rbx), %rax
cmpq $0x5, %rax
jb 0x4da8
leaq 0x4(%rdi), %rsi
cmpq %rdx, %rsi
je 0x4d92
subq %rsi, %rdx
callq 0x2140
movq 0x8(%rbx), %rax
leaq -0x4(%rax), %rdx
movq %rdx, 0x8(%rbx)
leaq -0x8(%rax), %rdi
cmpl $0x0, -0x8(%rax)
je 0x4d75
leaq 0x78(%rsp), %rsi
movq $0x0, (%rsi)
leaq 0x48(%rsp), %rdi
callq 0x2b68
leaq 0x48(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3098
movl %eax, %ebp
movq (%r12), %rdi
testq %rdi, %rdi
je 0x4dde
callq 0x20b0
testb %bpl, %bpl
movq 0x40(%rsp), %rax
jne 0x4df3
movb 0x18(%rax), %al
cmpb 0x18(%r15), %al
setne 0x18(%rbx)
movq %rbx, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x8, %edi
callq 0x2070
leaq 0x3f77(%rip), %rcx      # 0x8d90
movq %rcx, (%rax)
leaq 0x3f1d(%rip), %rsi      # 0x8d40
movq 0x41b6(%rip), %rdx      # 0x8fe0
movq %rax, %rdi
callq 0x2150
jmp 0x4eb0
jmp 0x4e3a
jmp 0x4e4d
jmp 0x4eb0
movq %rax, %r14
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4e50
callq 0x20b0
jmp 0x4e50
movq %rax, %r14
movq 0x20(%rsp), %rdi
jmp 0x4ee9
jmp 0x4eb0
movq %rax, %r14
jmp 0x4f02
jmp 0x4e6e
jmp 0x4e6e
jmp 0x4e6e
jmp 0x4e6e
jmp 0x4eb7
movq %rax, %r14
jmp 0x4f11
movq %rax, %r14
jmp 0x4ea6
movq %rax, %r14
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4e94
callq 0x20b0
jmp 0x4e94
jmp 0x4eb7
movq %rax, %r14
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x4ea6
callq 0x20b0
movq 0xb8(%rsp), %rdi
jmp 0x4edb
movq %rax, %r14
jmp 0x4ef3
jmp 0x4eb7
movq %rax, %r14
jmp 0x4ee5
movq %rax, %r14
jmp 0x4ed3
movq %rax, %r14
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4ed3
callq 0x20b0
movq 0xf8(%rsp), %rdi
testq %rdi, %rdi
je 0x4ee5
callq 0x20b0
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4ef3
callq 0x20b0
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x4f02
callq 0x20b0
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x4f11
callq 0x20b0
movq 0x68(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x4f23
callq 0x20b0
movq %r14, %rdi
callq 0x2180
nop