// File: /workspace/github_c_makefile_star2_O2/983[P]bigint/bigint.c
// Function: bigint_div_mod
bigint* bigint_div_mod(
    bigint *dst_quotient,
    bigint *dst_remainder,
    const bigint *src_numerator,
    const bigint *src_denominator
){
    int shift;
    int src_numerator_neg = src_numerator->neg;
    int src_denominator_neg = src_denominator->neg;
    bigint denominator[1], *remainder = dst_remainder, *quotient = dst_quotient;

    if (src_denominator->size == 0) return NULL;

    /* fast path for native word size */
    if (src_numerator->size == 1 && src_denominator->size == 1){
        /* make sure this is not overwritten */
        bigint_word a = src_numerator->words[0];
        bigint_word b = src_denominator->words[0];
        bigint_from_word(quotient, a / b);
        bigint_from_word(remainder, a % b);
        quotient->neg = src_numerator_neg ^ src_denominator_neg;
        remainder->neg = src_numerator_neg;
        return dst_quotient;
    }

    /* fast path for half word size */
    if (src_denominator->size == 1 &&
        src_denominator->words[0] <= BIGINT_HALF_WORD_MAX
    ){
        bigint_word rem;
        bigint_cpy(quotient, src_numerator);
        bigint_div_mod_half_word(quotient, &rem, src_denominator->words[0]);
        bigint_from_word(remainder, rem);
        quotient->neg = src_numerator_neg ^ src_denominator_neg;
        remainder->neg = src_numerator_neg;
        return dst_quotient;
    }

    bigint_cpy(remainder, src_numerator);
    remainder->neg = 0;
    quotient->size = 0;

    if (bigint_cmp_abs(remainder, src_denominator) >= 0){
        shift = bigint_bitlength(remainder) - bigint_bitlength(src_denominator);

        bigint_init(denominator);
        bigint_shift_left(denominator, src_denominator, shift);
        denominator->neg = 0;

        /* divide bit by bit */
        for (; shift >= 0; shift--) {
            if (bigint_cmp_abs(remainder, denominator) >= 0) {
                bigint_sub(remainder, remainder, denominator);
                bigint_set_bit(quotient, shift);
            }
            bigint_shift_right(denominator, denominator, 1);
        }

        bigint_free(denominator);
    }

    quotient->neg = src_numerator_neg ^ src_denominator_neg;
    remainder->neg = src_numerator_neg;
    return dst_quotient;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl 0xc(%rcx), %eax
testl %eax, %eax
je 0x2c25
movq %rcx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movl 0x8(%rdx), %r12d
movl 0x8(%rcx), %r13d
cmpl $0x1, %eax
jne 0x2c2c
cmpl $0x1, 0xc(%rdx)
jne 0x2c2c
movq (%rdx), %rax
movl (%rax), %eax
movq (%r15), %rcx
xorl %edx, %edx
divl (%rcx)
movl %edx, %ebp
movq %rbx, %rdi
movl %eax, %esi
callq 0x2609
movq %r14, %rdi
movl %ebp, %esi
callq 0x2609
jmp 0x2d1a
xorl %ebx, %ebx
jmp 0x2d25
cmpl $0x1, %eax
jne 0x2c64
movq (%r15), %rax
cmpl $0xffff, (%rax)         # imm = 0xFFFF
ja 0x2c64
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x1b99
movq (%r15), %rax
movl (%rax), %edx
leaq 0x10(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2d37
movl (%r15), %esi
movq %r14, %rdi
jmp 0x2c1b
movq %r14, %rdi
movq %rdx, %rsi
callq 0x1b99
andl $0x0, 0x8(%r14)
andl $0x0, 0xc(%rbx)
movq %r14, %rdi
movq %r15, %rsi
callq 0x1a92
testl %eax, %eax
js 0x2d1a
movl %r12d, 0xc(%rsp)
movq %r14, %rdi
callq 0x2b86
movl %eax, %ebp
movq %r15, %rdi
callq 0x2b86
subl %eax, %ebp
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r12
movaps %xmm0, (%r12)
andl $0x0, 0x10(%r12)
movq %r12, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x2adc
andl $0x0, 0x8(%r12)
pushq $0x1
popq %r15
testl %ebp, %ebp
js 0x2d0b
movq %r14, %rdi
movq %r12, %rsi
callq 0x1a92
testl %eax, %eax
js 0x2cf9
movq %r14, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x2794
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1c59
movq %r12, %rdi
movq %r12, %rsi
movl %r15d, %edx
callq 0x2b28
decl %ebp
jmp 0x2cce
leaq 0x10(%rsp), %rdi
callq 0x1a26
movl 0xc(%rsp), %r12d
xorl %r12d, %r13d
movl %r13d, 0x8(%rbx)
movl %r12d, 0x8(%r14)
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq