// File: /workspace/github_cpp_makefile_star2_O2/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, 0x8(%rsp)
movq %rdi, %r13
callq 0x2720
leaq 0x80(%rsp), %rsi
andq $0x0, (%rsi)
leaq 0x10(%rsp), %rdi
callq 0x295e
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x2caa
movl %eax, %ebp
leaq 0x10(%rsp), %rdi
callq 0x4d9a
testb %bpl, %bpl
jne 0x4363
movq 0x8(%rsp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
subq %rcx, %rax
sarq $0x2, %rax
movq (%r15), %rsi
movq 0x8(%r15), %rdx
subq %rsi, %rdx
sarq $0x2, %rdx
cmpq %rdx, %rax
jb 0x434e
cmpq $0x2, %rdx
movq %r15, 0x50(%rsp)
jb 0x423e
leaq 0x10(%rsp), %rdi
movq %r13, (%rsp)
callq 0x2720
leaq 0x80(%rsp), %rdi
callq 0x2720
addq $-0x4, 0x8(%r13)
movl 0x520c(%rip), %ebp      # 0x90f8
movq 0x8(%r15), %rax
movl -0x4(%rax), %ebx
leaq 0x60(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x2a18
incl %ebx
movl %ebp, %eax
cltd
idivl %ebx
movslq %eax, %r14
leaq 0x30(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq %r14, %rdx
callq 0x3d56
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x4bdc
leaq 0x30(%rsp), %rdi
callq 0x4d9a
leaq 0x60(%rsp), %rdi
callq 0x4d9a
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
movq 0x8(%rsp), %rdx
movq 0x8(%rdx), %rcx
subq (%rdx), %rcx
cmpq %rcx, %rax
jne 0x3f6e
leaq 0x30(%rsp), %rsi
andl $0x0, (%rsi)
leaq 0x10(%rsp), %rdi
callq 0x4da8
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
callq 0x2a18
leaq 0x30(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movq %r14, %rdx
callq 0x3d56
leaq 0x80(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x4bdc
leaq 0x30(%rsp), %rdi
callq 0x4d9a
leaq 0x60(%rsp), %r13
movq %r13, %rdi
callq 0x4d9a
movq 0x8(%r15), %rsi
subq (%r15), %rsi
movq %rsi, %rax
shrq $0x2, %rax
movq 0x8(%rsp), %rdx
movq 0x8(%rdx), %rcx
subq (%rdx), %rcx
shrq $0x2, %rcx
shlq $0x1e, %rsi
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
addq %rsi, %rdx
sarq $0x1e, %rdx
movq %rdx, 0xb0(%rsp)
movabsq $-0x200000000, %rbp     # imm = 0xFFFFFFFE00000000
addq %rsi, %rbp
sarq $0x1e, %rbp
sarq $0x1e, %rsi
movq %rsi, 0xa0(%rsp)
subl %eax, %ecx
movslq %ecx, %r15
cltq
movq %rax, 0xa8(%rsp)
leaq (,%r15,4), %rax
movq %rax, 0x58(%rsp)
testq %r15, %r15
js 0x42b8
movq 0xa8(%rsp), %rax
leaq (%r15,%rax), %rcx
movq 0x10(%rsp), %rsi
movslq (%rsi,%rcx,4), %rdx
movslq 0x50a9(%rip), %rdi      # 0x90f8
imulq %rdi, %rdx
movslq -0x4(%rsi,%rcx,4), %rax
addq %rdx, %rax
movq 0x80(%rsp), %r8
movq 0xb0(%rsp), %rdx
movslq (%r8,%rdx), %r9
cqto
idivq %r9
movq %rax, %r14
movq %rdx, %rax
imulq %rdi, %rax
movq %r9, %r10
imulq %rdi, %r10
cmpq %r14, %rdi
je 0x409f
movslq (%r8,%rbp), %r11
imulq %r14, %r11
movslq -0x8(%rsi,%rcx,4), %r12
addq %rax, %r12
cmpq %r12, %r11
jle 0x40ad
decq %r14
addq %r9, %rdx
addq %r10, %rax
cmpq %rdi, %rdx
jl 0x4085
leaq 0x30(%rsp), %rdi
callq 0x2720
leaq (,%r15,4), %rsi
addq 0x10(%rsp), %rsi
movq 0xa0(%rsp), %rax
leaq (%rax,%rsi), %rdx
addq $0x4, %rdx
movq %r13, %rdi
leaq 0xb8(%rsp), %rcx
callq 0x4bfe
leaq 0x30(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x5106
movq %r13, %rdi
callq 0x4d9a
movq %r13, %rbx
leaq 0xf8(%rsp), %r13
movq %r13, %rdi
leaq 0x80(%rsp), %rsi
movq %r14, %rdx
callq 0x3d56
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2f36
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4bdc
movq %rbx, %r13
movq %rbx, %rdi
callq 0x4d9a
leaq 0xf8(%rsp), %rdi
callq 0x4d9a
cmpb $0x0, 0x48(%rsp)
je 0x41ce
leaq 0xb8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x2a18
leaq 0xd8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x80(%rsp), %rsi
callq 0x2764
movq %r13, %rdi
leaq 0xb8(%rsp), %rsi
movq %rbx, %rdx
callq 0x2cf4
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x4bdc
movq %r13, %rdi
callq 0x4d9a
leaq 0xd8(%rsp), %rdi
callq 0x4d9a
leaq 0xb8(%rsp), %rdi
callq 0x4d9a
movq 0x38(%rsp), %rax
addq $-0x4, %rax
movq %rax, 0x38(%rsp)
jmp 0x41d3
movq 0x38(%rsp), %rax
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
movq 0x30(%rsp), %rsi
subq %rsi, %rax
sarq $0x2, %rax
addq 0x58(%rsp), %rcx
xorl %edi, %edi
leaq (%r15,%rdi), %r8
cmpq %r8, %rdx
jbe 0x4215
xorl %r8d, %r8d
cmpq %rdi, %rax
jbe 0x420c
movl (%rsi,%rdi,4), %r8d
movl %r8d, (%rcx,%rdi,4)
incq %rdi
jmp 0x41f7
movl %r14d, 0x60(%rsp)
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x4da8
leaq 0x30(%rsp), %rdi
callq 0x4d9a
decq %r15
addq $-0x4, 0x58(%rsp)
jmp 0x402a
movl %eax, %ebx
decq %rbx
xorl %r15d, %r15d
leaq 0x10(%rsp), %r14
testl %ebx, %ebx
js 0x42d3
movslq 0x4e9e(%rip), %rdx      # 0x90f8
imulq %r15, %rdx
movl %ebx, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movslq (%rcx,%r12,4), %rax
addq %rdx, %rax
movslq (%rsi), %rcx
cqto
idivq %rcx
movl %eax, 0x10(%rsp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x4da8
movslq 0x4e6b(%rip), %rax      # 0x90f8
imulq %rax, %r15
movq 0x8(%rsp), %rax
movq (%rax), %rcx
movslq (%rcx,%r12,4), %rax
addq %r15, %rax
movq 0x50(%rsp), %rdx
movq (%rdx), %rsi
movslq (%rsi), %rdi
cqto
idivq %rdi
decq %rbx
movq %rdx, %r15
jmp 0x424b
leaq 0x80(%rsp), %rdi
callq 0x4d9a
leaq 0x10(%rsp), %rdi
callq 0x4d9a
movq (%rsp), %r13
movq (%r13), %rdi
movq 0x8(%r13), %rsi
callq 0x5157
movq 0x50(%rsp), %rbx
movq 0x8(%r13), %rsi
cmpl $0x0, -0x4(%rsi)
jne 0x430a
movq %rsi, %rax
subq (%r13), %rax
cmpq $0x5, %rax
jb 0x430a
addq $-0x4, %rsi
movq %r13, %rdi
callq 0x4b04
jmp 0x42e5
leaq 0x80(%rsp), %rsi
andq $0x0, (%rsi)
leaq 0x10(%rsp), %rdi
callq 0x295e
leaq 0x10(%rsp), %rsi
movq %r13, %rdi
callq 0x2cea
movl %eax, %ebp
leaq 0x10(%rsp), %rdi
callq 0x4d9a
testb %bpl, %bpl
je 0x434e
movq 0x8(%rsp), %rax
movb 0x18(%rax), %al
cmpb 0x18(%rbx), %al
setne 0x18(%r13)
movq %r13, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rdi
callq 0x2080
leaq 0x4a1e(%rip), %rcx      # 0x8d90
movq %rcx, (%rax)
leaq 0x49c4(%rip), %rsi      # 0x8d40
movq 0x4c5d(%rip), %rdx      # 0x8fe0
movq %rax, %rdi
callq 0x2160
jmp 0x4425
jmp 0x4399
jmp 0x43a8
jmp 0x4425
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x4d9a
jmp 0x43ab
movq %rax, %r14
leaq 0x60(%rsp), %rdi
jmp 0x443f
jmp 0x4425
jmp 0x43c9
jmp 0x4461
jmp 0x43c5
jmp 0x445d
movq %r13, (%rsp)
movq %rax, %r14
jmp 0x4451
jmp 0x445d
jmp 0x445d
movq %rax, %r14
leaq 0x60(%rsp), %rdi
callq 0x4d9a
jmp 0x43ed
movq %rax, %r14
leaq 0xd8(%rsp), %rdi
callq 0x4d9a
jmp 0x4401
jmp 0x4420
movq %rax, %r14
leaq 0xb8(%rsp), %rdi
jmp 0x4435
jmp 0x445d
jmp 0x4420
movq %rax, %r14
leaq 0x60(%rsp), %rdi
callq 0x4d9a
jmp 0x442d
jmp 0x4420
movq %rax, %r14
jmp 0x443a
movq %rax, %r14
jmp 0x4444
movq %rax, %r14
leaq 0xf8(%rsp), %rdi
callq 0x4d9a
leaq 0x30(%rsp), %rdi
callq 0x4d9a
leaq 0x80(%rsp), %rdi
callq 0x4d9a
leaq 0x10(%rsp), %rdi
callq 0x4d9a
jmp 0x4464
movq %r13, (%rsp)
movq %rax, %r14
movq (%rsp), %rdi
callq 0x4d9a
movq %r14, %rdi
callq 0x2190
nop