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

    std::pair <uint128_t, uint128_t> qr (uint128_0, uint128_0);
    for(uint8_t x = lhs.bits(); x > 0; x--){
        qr.first  <<= uint128_1;
        qr.second <<= uint128_1;

        if ((lhs >> (x - 1U)) & 1){
            ++qr.second;
        }

        if (qr.second >= rhs){
            qr.second -= rhs;
            ++qr.first;
        }
    }
    return qr;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x60(%rbp)
movq %rdi, -0x58(%rbp)
movq %rsi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x118f9(%rip), %rsi     # 0x16210
callq 0x40e0
testb $0x1, %al
jne 0x4922
jmp 0x4975
movl $0x10, %edi
callq 0x30e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x68(%rbp)
leaq 0xa6c7(%rip), %rsi      # 0xf004
callq 0x3150
jmp 0x4944
movq -0x68(%rbp), %rdi
movq 0x11641(%rip), %rsi     # 0x15f90
movq 0x1164a(%rip), %rdx     # 0x15fa0
callq 0x3360
movq -0x68(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x3190
jmp 0x4b0c
movq -0x18(%rbp), %rdi
leaq 0x118a0(%rip), %rsi     # 0x16220
callq 0x40e0
testb $0x1, %al
jne 0x498b
jmp 0x49a4
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq 0x11876(%rip), %rdx     # 0x16210
callq 0x59e0
jmp 0x4b02
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x40e0
testb $0x1, %al
jne 0x49b7
jmp 0x49d3
movq -0x60(%rbp), %rdi
leaq 0x1185e(%rip), %rsi     # 0x16220
leaq 0x11847(%rip), %rdx     # 0x16210
callq 0x59e0
jmp 0x4b02
movq -0x10(%rbp), %rdi
leaq 0x11832(%rip), %rsi     # 0x16210
callq 0x40e0
testb $0x1, %al
jne 0x49fa
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x41e0
testb $0x1, %al
jne 0x49fa
jmp 0x4a13
movq -0x60(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq 0x11807(%rip), %rsi     # 0x16210
callq 0x59e0
jmp 0x4b02
jmp 0x4a15
jmp 0x4a17
jmp 0x4a19
movq -0x60(%rbp), %rdi
leaq 0x117ec(%rip), %rdx     # 0x16210
movq %rdx, %rsi
callq 0x59e0
movq -0x10(%rbp), %rdi
callq 0x4b20
movb %al, -0x25(%rbp)
movzbl -0x25(%rbp), %eax
cmpl $0x0, %eax
jle 0x4b00
movq -0x60(%rbp), %rdi
leaq 0x117d0(%rip), %rsi     # 0x16220
callq 0x3e40
movq -0x60(%rbp), %rdi
addq $0x10, %rdi
leaq 0x117bc(%rip), %rsi     # 0x16220
callq 0x3e40
movq -0x10(%rbp), %rdi
movzbl -0x25(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x4c(%rbp)
leaq -0x4c(%rbp), %rsi
callq 0x5a20
movq %rax, -0x48(%rbp)
movq %rdx, -0x40(%rbp)
movl $0x1, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x5a70
movq %rax, -0x38(%rbp)
movq %rdx, -0x30(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x3a40
testb $0x1, %al
jne 0x4ab3
jmp 0x4ac0
movq -0x60(%rbp), %rdi
addq $0x10, %rdi
callq 0x4ba0
movq -0x60(%rbp), %rdi
addq $0x10, %rdi
movq -0x18(%rbp), %rsi
callq 0x4240
testb $0x1, %al
jne 0x4ad7
jmp 0x4af1
movq -0x60(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x10, %rdi
callq 0x4430
movq -0x60(%rbp), %rdi
callq 0x4ba0
jmp 0x4af3
movb -0x25(%rbp), %al
addb $-0x1, %al
movb %al, -0x25(%rbp)
jmp 0x4a38
jmp 0x4b02
movq -0x58(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0x3380
nopw %cs:(%rax,%rax)