// File: wenzhelu[P]RUDP/sender.cpp
// Function: Sender::sending()
void Sender::sending() {
    while (!master->close) {
        this_thread::sleep_for(chrono::milliseconds(25)); // sleep time need to test
        // we are making somehow dangerous assumption here.
        // the listener may need to change the curPtr when
        // packet loss happened
        if (pending) {
//            debug_print("timerBase: %u, sendBase: %u, RTT: %u, timeout: %u\n", timerBase, master->sendBase, master->RTT, master->TimeOut);
            if (master->sendBase - diff >= userDataLen) {
                endTask();
                continue;
            }
            
            // data pending to send, copy data to the buff
            if (resend) {
                // packet loss, request resend by listener
                debug_print("data loss! sendbase: %u\n", master->sendBase);
                curPtr = master->sendBase;
                *((uint*) master->buff) = master->sendBase;
                resend = false;
            }
            
            char *srcBegin = userBuff + (curPtr - diff), *destBegin = master->buff + master->HEADER_LEN;
            uint len = std::min((uint)1460, std::min(byteIncWnd(), userDataLen - (curPtr - diff)));
            
            // len == 0 could be: we have sent all the data but
            // waiting for all the ack
            // or we have reached send out cWnd of bytes and waiting for ACK to
            // "slide" the cWnd
            if (len > 0) {
                master->setDataBit(1);
                memcpy(destBegin, srcBegin, len);
                
                // finally we are sending
                master->sock->printPacket(false, master->buff, master->HEADER_LEN + len);
//                master->startTimes.insert(pair<uint, tp>(curPtr + len, system_clock::now()));
                master->startTimes[curPtr + len] = system_clock::now();
                master->sock->write(master->buff, master->HEADER_LEN + len);
                if (master->status == SLOW_START) master->slowStartNum++;
                else if (master->status == CONG_AVOID) master->congestNum++;

                master->setAckBit(0);   // set ack 0
                // if we have any sending error, the usock class
                // will print debug infomation, and we just ignore any
                // sending error here, we let ACK and resend to fix it.
                
                // when timer times up, we need to know whether we have
                // also, we need this to calculate the RTT
                
                // change the sequence number after sending packet out
                curPtr += len;
                uint *tm = (uint*)master->buff;
                *tm = curPtr;
                continue;
            }
        }
        master->setDataBit(0);
            
        // if no pending data, check if there is pending ACK
        // if no then do nothing
        // if yes just send it
        if (master->testAckBit()) {
            master->sock->printPacket(false, master->buff, master->HEADER_LEN);
            master->sock->write(master->buff, master->HEADER_LEN);
            // not timing only ack packets
            master->setAckBit(0);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rax
movb 0x620(%rax), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x5acd
jmp 0x5d52
movl $0x19, -0x14(%rbp)
leaq -0x10(%rbp), %rdi
leaq -0x14(%rbp), %rsi
callq 0x3520
leaq -0x10(%rbp), %rdi
callq 0x3470
movq -0x58(%rbp), %rax
testb $0x1, 0x38(%rax)
je 0x5cd8
movq -0x58(%rbp), %rcx
movq 0x48(%rcx), %rax
movl (%rax), %eax
subl 0x40(%rcx), %eax
cmpl 0x8(%rcx), %eax
jb 0x5b15
movq -0x58(%rbp), %rdi
callq 0x5750
jmp 0x5ab4
movq -0x58(%rbp), %rax
testb $0x1, 0x39(%rax)
je 0x5b41
jmp 0x5b21
jmp 0x5b23
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rcx
movl (%rcx), %ecx
movl %ecx, 0x3c(%rax)
movq 0x48(%rax), %rcx
movl (%rcx), %edx
movq 0x48(%rax), %rcx
movl %edx, 0x50(%rcx)
movb $0x0, 0x39(%rax)
movq -0x58(%rbp), %rdi
movq (%rdi), %rax
movl 0x3c(%rdi), %ecx
subl 0x40(%rdi), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq 0x48(%rdi), %rax
addq $0x50, %rax
addq $0xc, %rax
movq %rax, -0x28(%rbp)
movl $0x5b4, -0x30(%rbp)     # imm = 0x5B4
callq 0x5d60
movq -0x58(%rbp), %rdx
movl %eax, -0x34(%rbp)
movl 0x8(%rdx), %eax
movl 0x3c(%rdx), %ecx
subl 0x40(%rdx), %ecx
subl %ecx, %eax
movl %eax, -0x38(%rbp)
leaq -0x34(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x5f80
movq %rax, %rsi
leaq -0x30(%rbp), %rdi
callq 0x5f80
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jbe 0x5cd6
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rdi
movl $0x1, %esi
callq 0x4300
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl -0x2c(%rbp), %eax
movl %eax, %edx
callq 0x21d0
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rcx
movq 0x628(%rcx), %rdi
movq 0x48(%rax), %rdx
addq $0x50, %rdx
movl -0x2c(%rbp), %ecx
addl $0xc, %ecx
xorl %esi, %esi
callq 0x6ff0
callq 0x2060
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, -0x40(%rbp)
movq 0x48(%rax), %rdi
addq $0x20, %rdi
movl 0x3c(%rax), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x44(%rbp)
leaq -0x44(%rbp), %rsi
callq 0x5fc0
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq -0x40(%rbp), %rdx
movq %rdx, (%rcx)
movq 0x48(%rax), %rcx
movq 0x628(%rcx), %rdi
movq 0x48(%rax), %rsi
addq $0x50, %rsi
movl -0x2c(%rbp), %eax
addl $0xc, %eax
movl %eax, %eax
movl %eax, %edx
callq 0x6f70
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rax
cmpl $0x0, 0xc(%rax)
jne 0x5c79
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rax
movl 0x640(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x640(%rax)
jmp 0x5ca0
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rax
cmpl $0x2, 0xc(%rax)
jne 0x5c9e
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rax
movl 0x644(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x644(%rax)
jmp 0x5ca0
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rdi
xorl %esi, %esi
callq 0x4350
movq -0x58(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl 0x3c(%rax), %ecx
movl %ecx, 0x3c(%rax)
movq 0x48(%rax), %rcx
addq $0x50, %rcx
movq %rcx, -0x50(%rbp)
movl 0x3c(%rax), %ecx
movq -0x50(%rbp), %rax
movl %ecx, (%rax)
jmp 0x5ab4
jmp 0x5cd8
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rdi
xorl %esi, %esi
callq 0x4300
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x43f0
testb $0x1, %al
jne 0x5cfa
jmp 0x5d4d
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rcx
movq 0x628(%rcx), %rdi
movq 0x48(%rax), %rdx
addq $0x50, %rdx
xorl %esi, %esi
movl $0xc, %ecx
callq 0x6ff0
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rcx
movq 0x628(%rcx), %rdi
movq 0x48(%rax), %rsi
addq $0x50, %rsi
movl $0xc, %edx
callq 0x6f70
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rdi
xorl %esi, %esi
callq 0x4350
jmp 0x5ab4
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)