// File: /workspace/github_cpp_makefile_star2_O1/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 0x2b12
leaq -0x258(%rbp), %rdi
callq 0x2270
movq 0x20(%rbx), %rax
cmpb $0x0, 0x620(%rax)
jne 0x2781
leaq -0x258(%rbp), %r12
leaq -0x820(%rbp), %r13
movq %r12, %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 0x266a
callq 0x2050
cmpl $0x4, (%rax)
je 0x2650
movq 0x20(%rbx), %rax
movq 0x628(%rax), %rdi
movl $0x5c0, %edx            # imm = 0x5C0
movq %r13, %rsi
callq 0x3a7e
movb -0x818(%rbp), %r15b
testb $0x20, %r15b
jne 0x272b
movq %rax, %r14
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 0x274b
testb $0x40, %r15b
je 0x26e1
movl -0x81c(%rbp), %esi
movq %rbx, %rdi
callq 0x2834
movl -0x81c(%rbp), %esi
movq %rbx, %rdi
callq 0x2980
testb %r15b, %r15b
jns 0x274b
movl 0x28(%rbx), %ecx
movl -0x820(%rbp), %eax
cmpl %eax, %ecx
jne 0x273c
leal -0xc(%r14), %ecx
addl %r14d, %eax
addl $-0xc, %eax
movq 0x20(%rbx), %rdx
movl %eax, 0x54(%rdx)
addl %ecx, 0x28(%rbx)
movslq %ecx, %rdx
leaq -0x248(%rbp), %rdi
leaq -0x814(%rbp), %rsi
callq 0x22a0
leaq -0x248(%rbp), %rdi
callq 0x2150
jmp 0x2743
movq -0x48(%rbp), %rax
incb 0x4(%rax)
movq %r12, %rdi
callq 0x2070
jmp 0x2766
movq 0x20(%rbx), %rax
movl %ecx, 0x54(%rax)
movq 0x20(%rbx), %rax
orb $0x40, 0x58(%rax)
movq 0x20(%rbx), %rax
movq 0x628(%rax), %rdi
movl $0x1, %esi
movq %r13, %rdx
movl %r14d, %ecx
callq 0x3ab8
testb $0x20, %r15b
je 0x263a
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 0x279f
callq 0x21f0
addq $0x7f8, %rsp            # imm = 0x7F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x27cb
jmp 0x27bc
jmp 0x27bc
jmp 0x27bc
movq %rax, %rbx
leaq -0x258(%rbp), %rdi
callq 0x20b0
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x27dd
callq 0x21f0
movq %rbx, %rdi
callq 0x2350
nop