// File: /workspace/github_cpp_makefile_star2_O3/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, 0x48(%rsp)
movq %rdi, %rbx
callq 0x27ac
leaq 0x80(%rsp), %rsi
movq $0x0, (%rsi)
leaq 0x50(%rsp), %rdi
movq %rbx, 0x70(%rsp)
callq 0x2a96
leaq 0x50(%rsp), %r14
movq %r15, 0x40(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x2fc6
movl %eax, %ebp
movq (%r14), %rdi
testq %rdi, %rdi
je 0x46cd
callq 0x20b0
testb %bpl, %bpl
jne 0x4cfa
movq 0x48(%rsp), %rbp
movq (%rbp), %rcx
movq 0x8(%rbp), %rax
subq %rcx, %rax
sarq $0x2, %rax
movq 0x40(%rsp), %r13
movq (%r13), %rdi
movq 0x8(%r13), %rdx
subq %rdi, %rdx
sarq $0x2, %rdx
cmpq %rdx, %rax
jb 0x4ce5
cmpq $0x2, %rdx
jb 0x47ec
leaq 0x50(%rsp), %rdi
callq 0x27ac
leaq 0x80(%rsp), %rdi
callq 0x27ac
addq $-0x4, 0x8(%rbx)
movl 0x49b5(%rip), %ebp      # 0x90e8
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rax
movl -0x4(%rax), %r14d
leaq 0x20(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x2c0c
incl %r14d
movl %ebp, %eax
cltd
idivl %r14d
movslq %eax, %r14
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
movq %r14, %rdx
callq 0x4542
leaq 0x50(%rsp), %rdi
movq %rsp, %rsi
callq 0x5412
movq 0x40(%rsp), %r15
movq 0x48(%rsp), %r12
movb 0x18(%rsp), %al
movb %al, 0x68(%rsp)
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4798
callq 0x20b0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x47a7
callq 0x20b0
movq 0x58(%rsp), %rsi
movq %rsi, %rax
subq 0x50(%rsp), %rax
movq 0x8(%r12), %rcx
subq (%r12), %rcx
cmpq %rcx, %rax
jne 0x4887
movl $0x0, (%rsp)
cmpq 0x60(%rsp), %rsi
je 0x487a
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x58(%rsp)
jmp 0x4887
decl %eax
js 0x4c0f
movl %eax, %r12d
movl 0x48ea(%rip), %r8d      # 0x90e8
movl (%rdi), %esi
incq %r12
xorl %r15d, %r15d
leaq 0x50(%rsp), %r14
movslq %r8d, %r9
imulq %r15, %r9
movslq -0x4(%rcx,%r12,4), %rax
addq %r9, %rax
movslq %esi, %rsi
cqto
idivq %rsi
movl %eax, 0x50(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x483c
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x485d
movq %rbx, %rdi
movq %r14, %rdx
callq 0x56c6
movslq 0x489a(%rip), %r8       # 0x90e8
movq (%rbp), %rcx
movq (%r13), %rdi
imulq %r8, %r15
movq %r15, %r9
movslq -0x4(%rcx,%r12,4), %rax
addq %r9, %rax
movslq (%rdi), %rsi
cqto
idivq %rsi
movq %rdx, %r15
decq %r12
jg 0x480b
jmp 0x4c0f
leaq 0x50(%rsp), %rdi
movq %rsp, %rdx
callq 0x56c6
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x2c0c
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
movq %r14, %rdx
callq 0x4542
leaq 0x80(%rsp), %rdi
movq %rsp, %rsi
callq 0x5412
movb 0x18(%rsp), %al
movb %al, 0x98(%rsp)
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x48cd
callq 0x20b0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x48dc
callq 0x20b0
movq 0x8(%r15), %rax
subq (%r15), %rax
movq %rax, %rcx
movq 0x8(%r12), %rbp
subq (%r12), %rbp
sarq $0x2, %rcx
shrq $0x2, %rbp
subq %rcx, %rbp
testl %ebp, %ebp
js 0x4be9
movq %rax, %rcx
shlq $0x1e, %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
addq %rcx, %rdx
sarq $0x1e, %rdx
movq %rdx, 0xb0(%rsp)
movabsq $-0x200000000, %r15     # imm = 0xFFFFFFFE00000000
addq %rcx, %r15
sarq $0x1e, %r15
sarq $0x1e, %rcx
movq %rcx, 0xa0(%rsp)
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
shrq $0x2, %rax
cltq
movq %rax, 0xa8(%rsp)
leaq (,%rbp,4), %rax
movq %rax, 0x78(%rsp)
movq 0xa8(%rsp), %rax
leaq (%rax,%rbp), %rcx
movq 0x50(%rsp), %rsi
movslq (%rsi,%rcx,4), %rdx
movslq 0x476c(%rip), %rdi      # 0x90e8
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 0x49cc
movslq (%r8,%r15), %r11
imulq %r14, %r11
movslq -0x8(%rsi,%rcx,4), %r13
addq %rax, %r13
cmpq %r13, %r11
jle 0x49da
decq %r14
addq %r9, %rdx
addq %r10, %rax
cmpq %rdi, %rdx
jl 0x49b2
movq %rsp, %rdi
callq 0x27ac
leaq (,%rbp,4), %rsi
addq 0x50(%rsp), %rsi
movq 0xa0(%rsp), %rax
leaq (%rax,%rsi), %rdx
addq $0x4, %rdx
leaq 0x20(%rsp), %rdi
leaq 0xb8(%rsp), %rcx
callq 0x5570
movq (%rsp), %rdi
movaps 0x20(%rsp), %xmm0
movaps %xmm0, (%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
testq %rdi, %rdi
je 0x4a52
callq 0x20b0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4a52
callq 0x20b0
leaq 0xf8(%rsp), %r12
movq %r12, %rdi
leaq 0x80(%rsp), %rsi
movq %r14, %rdx
callq 0x4542
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %rsp, %r13
movq %r13, %rsi
movq %r12, %rdx
callq 0x3320
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5412
movb 0x38(%rsp), %al
movb %al, 0x18(%rsp)
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4aa5
callq 0x20b0
movq 0xf8(%rsp), %rdi
testq %rdi, %rdi
je 0x4ab7
callq 0x20b0
cmpb $0x1, 0x18(%rsp)
jne 0x4b4e
leaq 0xb8(%rsp), %rdi
movq %rsp, %rsi
callq 0x2c0c
leaq 0xd8(%rsp), %r12
movq %r12, %rdi
leaq 0x80(%rsp), %rsi
callq 0x2806
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
leaq 0xb8(%rsp), %rsi
movq %r12, %rdx
callq 0x3046
movq %rsp, %rdi
movq %rbx, %rsi
callq 0x5412
movb 0x38(%rsp), %al
movb %al, 0x18(%rsp)
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4b24
callq 0x20b0
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x4b36
callq 0x20b0
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x4b48
callq 0x20b0
addq $-0x4, 0x8(%rsp)
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rbp, %rcx
jbe 0x4b9a
movq (%rsp), %rdx
movq 0x8(%rsp), %rsi
subq %rdx, %rsi
sarq $0x2, %rsi
addq 0x78(%rsp), %rax
xorl %edi, %edi
xorl %r8d, %r8d
cmpq %rdi, %rsi
jbe 0x4b87
movl (%rdx,%rdi,4), %r8d
movl %r8d, (%rax,%rdi,4)
leaq (%rdi,%rbp), %r8
incq %r8
incq %rdi
cmpq %r8, %rcx
ja 0x4b7b
movl %r14d, 0x20(%rsp)
movq 0x70(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x4bbb
movl %r14d, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x4bc5
leaq 0x20(%rsp), %rdx
callq 0x56c6
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4bd3
callq 0x20b0
leaq -0x1(%rbp), %rax
addq $-0x4, 0x78(%rsp)
testq %rbp, %rbp
movq %rax, %rbp
jg 0x4960
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x4bfb
callq 0x20b0
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
movq 0x70(%rsp), %rbx
je 0x4c0f
callq 0x20b0
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
cmpq %rdx, %rax
setne %cl
leaq -0x4(%rdx), %rdi
cmpq %rax, %rdi
seta %sil
andb %cl, %sil
cmpb $0x1, %sil
jne 0x4c53
addq $0x4, %rax
movq %rdi, %rcx
movl -0x4(%rax), %esi
movl (%rcx), %r8d
movl %r8d, -0x4(%rax)
movl %esi, (%rcx)
addq $-0x4, %rcx
leaq 0x4(%rax), %rsi
cmpq %rcx, %rax
movq %rsi, %rax
jb 0x4c37
cmpl $0x0, (%rdi)
jne 0x4c94
movq (%rbx), %rax
movq %rdx, %rcx
subq %rax, %rcx
cmpq $0x5, %rcx
jb 0x4c94
leaq 0x4(%rdi), %rsi
cmpq %rdx, %rsi
je 0x4c7f
subq %rsi, %rdx
callq 0x2140
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
leaq -0x4(%rdx), %rcx
movq %rcx, 0x8(%rbx)
leaq -0x8(%rdx), %rdi
cmpl $0x0, -0x8(%rdx)
movq %rcx, %rdx
je 0x4c5b
leaq 0x80(%rsp), %rsi
movq $0x0, (%rsi)
leaq 0x50(%rsp), %rdi
callq 0x2a96
leaq 0x50(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2fc6
movl %eax, %ebp
movq (%r14), %rdi
testq %rdi, %rdi
je 0x4ccc
callq 0x20b0
testb %bpl, %bpl
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %rax
jne 0x4ce5
movb 0x18(%rax), %al
cmpb 0x18(%rcx), %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 0x4085(%rip), %rcx      # 0x8d90
movq %rcx, (%rax)
leaq 0x402b(%rip), %rsi      # 0x8d40
movq 0x42c4(%rip), %rdx      # 0x8fe0
movq %rax, %rdi
callq 0x2150
jmp 0x4da2
jmp 0x4d2c
jmp 0x4d3f
jmp 0x4da2
movq %rax, %r14
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4d42
callq 0x20b0
jmp 0x4d42
movq %rax, %r14
movq 0x20(%rsp), %rdi
jmp 0x4ddb
jmp 0x4da2
movq %rax, %r14
jmp 0x4df7
jmp 0x4d60
jmp 0x4d60
jmp 0x4d60
jmp 0x4d60
jmp 0x4da9
movq %rax, %r14
jmp 0x4e06
movq %rax, %r14
jmp 0x4d98
movq %rax, %r14
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4d86
callq 0x20b0
jmp 0x4d86
jmp 0x4da9
movq %rax, %r14
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x4d98
callq 0x20b0
movq 0xb8(%rsp), %rdi
jmp 0x4dcd
movq %rax, %r14
jmp 0x4de5
jmp 0x4da9
movq %rax, %r14
jmp 0x4dd7
movq %rax, %r14
jmp 0x4dc5
movq %rax, %r14
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x4dc5
callq 0x20b0
movq 0xf8(%rsp), %rdi
testq %rdi, %rdi
je 0x4dd7
callq 0x20b0
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4de5
callq 0x20b0
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x4df7
callq 0x20b0
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x4e06
callq 0x20b0
movq 0x70(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x4e18
callq 0x20b0
movq %r14, %rdi
callq 0x2180