// File: /workspace/github_cpp_makefile_star2_O3/wenzhelu[P]RUDP/Listener.cpp
// Function: Listener::recAns()
void Listener::recAns()
{
    debug_print("recAns is invoked\n", nullptr);
    string s = "./tm0.pdf";
    fstream fs;
    
    while (!master->close) {
        
        fs.open(s, ios::trunc | ios::out);
        while(1)
        {
            this_thread::sleep_for(ms(25)); // sleep for a while
            int recbits;
            char rec[1472];
            //        char databuf[1460];
            bool databit=false;
            bool ackbit=false;
            recbits=master->sock->read(rec,1472);
            //this_thread::sleep_for(ms(50));
            //    printf("%d bytes received\n",recbits);
            //    printf("sequence num: %u \n",*(uint*)rec);
            if(rec[Listener::control]>>7)
                databit=true;
            if((rec[Listener::control]>>6)&1)
                ackbit=true;
            if (rec[8] & 0x20) {
                // this is a fin packet we are not dropping
                s[4]++;
                fs.close();
                debug_print("Received new file!\n", nullptr);
                //                debug_print("RTT time: %u ms\n",this->eRTT);
                break;
            }
            
            if(!randomdrop(0.1))
            {
                debug_print("Not dropping this packet\n", nullptr);
                if(ackbit)
                {
                    //            printf("ack exist\n");
                    getTimeout(*(uint*)(rec+4));
                    this->update(*(uint*)(rec+4));
                }
                if(databit)
                {
                    //            printf("data exist\n");
                    if(this->totalrec!=*(uint*)rec)
                    {
                        *(uint*)(master->buff+4) = this->totalrec;
                    }
                    else
                    {
                        *(uint*)(master->buff+4) = *(uint*)rec+recbits-12;
                        this->totalrec += (recbits - 12);
                        char *data = rec + 12;
                        fs.write(data, recbits - 12);
                        fs.flush();
                        //                    this->totalrec += (recbits-12);
                    }
                    master->buff[Listener::control]|=1<<6;
                    //
                    //                strncpy(databuf,rec+12,1460);
                    //            printf("%s\n\n",databuf);
                }
            } else {
                debug_print("dropping this packet\n", nullptr);
            }
            master->sock->printPacket(true,rec,(uint)recbits);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x7f8, %rsp            # imm = 0x7F8
movq %rdi, %rbx
leaq -0x38(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1a42(%rip), %rsi      # 0x4030
leaq 0x1a44(%rip), %rdx      # 0x4039
leaq -0x48(%rbp), %rdi
callq 0x2aee
leaq -0x258(%rbp), %rdi
callq 0x2270
movq 0x20(%rbx), %rax
cmpb $0x0, 0x620(%rax)
jne 0x2775
leaq -0x248(%rbp), %r15
leaq -0x820(%rbp), %r13
leaq -0x258(%rbp), %rdi
leaq -0x48(%rbp), %rsi
movl $0x30, %edx
callq 0x22c0
movq $0x0, -0x820(%rbp)
movq $0x17d7840, -0x818(%rbp) # imm = 0x17D7840
movq %r13, %rdi
movq %r13, %rsi
callq 0x20f0
cmpl $-0x1, %eax
jne 0x266e
callq 0x2050
cmpl $0x4, (%rax)
je 0x2654
movq 0x20(%rbx), %rax
movq 0x628(%rax), %rdi
movl $0x5c0, %edx            # imm = 0x5C0
movq %r13, %rsi
callq 0x3a06
movb -0x818(%rbp), %r14b
testb $0x20, %r14b
jne 0x2751
movq %rax, %r12
callq 0x20d0
cltq
imulq $0x51eb851f, %rax, %rcx # imm = 0x51EB851F
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x25, %rcx
addl %edx, %ecx
imull $0x64, %ecx, %ecx
subl %ecx, %eax
cmpl $0xa, %eax
jl 0x2731
testb $0x40, %r14b
je 0x26e1
movl -0x81c(%rbp), %esi
movq %rbx, %rdi
callq 0x282a
movl -0x81c(%rbp), %esi
movq %rbx, %rdi
callq 0x296a
testb %r14b, %r14b
jns 0x2731
movl 0x28(%rbx), %eax
cmpl -0x820(%rbp), %eax
jne 0x2726
leal -0xc(%r12), %ecx
addl %r12d, %eax
addl $-0xc, %eax
movq 0x20(%rbx), %rdx
movl %eax, 0x54(%rdx)
addl %ecx, 0x28(%rbx)
movslq %ecx, %rdx
movq %r15, %rdi
leaq -0x814(%rbp), %rsi
callq 0x22a0
movq %r15, %rdi
callq 0x2150
movq 0x20(%rbx), %rcx
jmp 0x272d
movq 0x20(%rbx), %rcx
movl %eax, 0x54(%rcx)
orb $0x40, 0x58(%rcx)
movq 0x20(%rbx), %rax
movq 0x628(%rax), %rdi
movl $0x1, %esi
movq %r13, %rdx
movl %r12d, %ecx
callq 0x3a40
jmp 0x263e
movq -0x48(%rbp), %rax
incb 0x4(%rax)
leaq -0x258(%rbp), %rdi
callq 0x2070
movq 0x20(%rbx), %rax
cmpb $0x0, 0x620(%rax)
je 0x2629
leaq -0x258(%rbp), %rdi
callq 0x20b0
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x2793
callq 0x21f0
addq $0x7f8, %rsp            # imm = 0x7F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x27c1
jmp 0x27b2
jmp 0x27b2
jmp 0x27b2
jmp 0x27b2
movq %rax, %rbx
leaq -0x258(%rbp), %rdi
callq 0x20b0
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x27d3
callq 0x21f0
movq %rbx, %rdi
callq 0x2350
nop