// File: sokolcom[P]tp-blockchain-task06/uint256_t/uint256_t.cpp
// Function: uint256_t::divmod(uint256_t const&, uint256_t const&) const
std::pair <uint256_t, uint256_t> uint256_t::divmod(const uint256_t & lhs, const uint256_t & rhs) const{
    // Save some calculations /////////////////////
    if (rhs == uint256_0){
        throw std::domain_error("Error: division or modulus by 0");
    }
    else if (rhs == uint256_1){
        return std::pair <uint256_t, uint256_t> (lhs, uint256_0);
    }
    else if (lhs == rhs){
        return std::pair <uint256_t, uint256_t> (uint256_1, uint256_0);
    }
    else if ((lhs == uint256_0) || (lhs < rhs)){
        return std::pair <uint256_t, uint256_t> (uint256_0, lhs);
    }

    std::pair <uint256_t, uint256_t> qr(uint256_0, lhs);
    uint256_t copyd = rhs << (lhs.bits() - rhs.bits());
    uint256_t adder = uint256_1 << (lhs.bits() - rhs.bits());
    if (copyd > qr.second){
        copyd >>= uint256_1;
        adder >>= uint256_1;
    }
    while (qr.second >= rhs){
        if (qr.second >= copyd){
            qr.second -= copyd;
            qr.first |= adder;
        }
        copyd >>= uint256_1;
        adder >>= uint256_1;
    }
    return qr;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x88(%rbp)
movq %rdi, -0x80(%rbp)
movq %rsi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movq -0x18(%rbp), %rdi
leaq 0xd6a3(%rip), %rsi      # 0x16260
callq 0x7d20
testb $0x1, %al
jne 0x8bc8
jmp 0x8c24
movl $0x10, %edi
callq 0x30e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x90(%rbp)
leaq 0x641e(%rip), %rsi      # 0xf004
callq 0x3150
jmp 0x8bed
movq -0x90(%rbp), %rdi
movq 0xd395(%rip), %rsi      # 0x15f90
movq 0xd39e(%rip), %rdx      # 0x15fa0
callq 0x3360
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x3190
jmp 0x8e38
movq -0x18(%rbp), %rdi
leaq 0xd651(%rip), %rsi      # 0x16280
callq 0x7d20
testb $0x1, %al
jne 0x8c3a
jmp 0x8c56
movq -0x88(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq 0xd614(%rip), %rdx      # 0x16260
callq 0xaf80
jmp 0x8e2b
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x7d20
testb $0x1, %al
jne 0x8c69
jmp 0x8c88
movq -0x88(%rbp), %rdi
leaq 0xd609(%rip), %rsi      # 0x16280
leaq 0xd5e2(%rip), %rdx      # 0x16260
callq 0xaf80
jmp 0x8e2b
movq -0x10(%rbp), %rdi
leaq 0xd5cd(%rip), %rsi      # 0x16260
callq 0x7d20
testb $0x1, %al
jne 0x8caf
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x7f20
testb $0x1, %al
jne 0x8caf
jmp 0x8ccb
movq -0x88(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq 0xd59f(%rip), %rsi      # 0x16260
callq 0xaf80
jmp 0x8e2b
jmp 0x8ccd
jmp 0x8ccf
jmp 0x8cd1
movq -0x88(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq 0xd57d(%rip), %rsi      # 0x16260
callq 0xaf80
movq -0x18(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x8e50
movzwl %ax, %eax
movl %eax, -0x98(%rbp)
movq -0x18(%rbp), %rdi
callq 0x8e50
movq -0xa0(%rbp), %rsi
movw %ax, %cx
movl -0x98(%rbp), %eax
movzwl %cx, %ecx
subl %ecx, %eax
movl %eax, -0x4c(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x4c(%rbp), %rdx
callq 0xafe0
movq -0x10(%rbp), %rdi
callq 0x8e50
movzwl %ax, %eax
movl %eax, -0x94(%rbp)
movq -0x18(%rbp), %rdi
callq 0x8e50
movw %ax, %cx
movl -0x94(%rbp), %eax
movzwl %cx, %ecx
subl %ecx, %eax
movl %eax, -0x74(%rbp)
leaq -0x70(%rbp), %rdi
leaq 0xd516(%rip), %rsi      # 0x16280
leaq -0x74(%rbp), %rdx
callq 0xafe0
movq -0x88(%rbp), %rsi
addq $0x20, %rsi
leaq -0x48(%rbp), %rdi
callq 0x7e60
testb $0x1, %al
jne 0x8d8d
jmp 0x8dad
leaq -0x48(%rbp), %rdi
leaq 0xd4e8(%rip), %rsi      # 0x16280
callq 0x7b70
leaq -0x70(%rbp), %rdi
leaq 0xd4d8(%rip), %rsi      # 0x16280
callq 0x7b70
jmp 0x8daf
movq -0x88(%rbp), %rdi
addq $0x20, %rdi
movq -0x18(%rbp), %rsi
callq 0x7fe0
testb $0x1, %al
jne 0x8dc9
jmp 0x8e29
movq -0x88(%rbp), %rdi
addq $0x20, %rdi
leaq -0x48(%rbp), %rsi
callq 0x7fe0
testb $0x1, %al
jne 0x8de3
jmp 0x8e07
movq -0x88(%rbp), %rdi
addq $0x20, %rdi
leaq -0x48(%rbp), %rsi
callq 0x8370
movq -0x88(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x72d0
leaq -0x48(%rbp), %rdi
leaq 0xd46e(%rip), %rsi      # 0x16280
callq 0x7b70
leaq -0x70(%rbp), %rdi
leaq 0xd45e(%rip), %rsi      # 0x16280
callq 0x7b70
jmp 0x8daf
jmp 0x8e2b
movq -0x80(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0x3380
nopw %cs:(%rax,%rax)