// File: /workspace/github_c_makefile_star2_O0/pencode-lab[P]Shoit/shoit_receiver.c
// Function: receiver_run_loop
static void _receiver_run_loop(shoit_core_t *receiver,char *saveFile)
{
    struct timeval timeout;
    fd_set rset,orgSet;
    int maxfdpl;
    int done=0;
    int retval;


    shoit_control_t tcpCrl;

    maxfdpl = receiver->tcpSockfd +1;
    FD_ZERO(&orgSet);
    FD_SET(receiver->tcpSockfd, &orgSet);

    if(!echo_to_sender_on_tcp(receiver,CONNECT)){
        SHOIT_LOG("send CONNECT SIGNAL to server failed.\n");
        return;
    }

    while(!done && receiver->runing){
        timeout.tv_sec = 5;
        timeout.tv_usec = 0;
        memcpy(&rset,&orgSet,sizeof(orgSet));
        retval = select(maxfdpl, &rset, NULL, NULL, &timeout);

        if(retval<0){/*error*/
            SHOIT_LOG("select return -1 (%s) at 0xe01\n",strerror(errno));
            done =1;
        }
        if(retval>0 && FD_ISSET(receiver->tcpSockfd, &rset)){
            if(shoit_network_readn(receiver->tcpSockfd,(char*)&tcpCrl,sizeof(tcpCrl))==sizeof(shoit_control_t)){

                if(receiver->verbose>1) SHOIT_LOG("from client get status signal %d\n",tcpCrl.status);
                switch(tcpCrl.status){
                    case ROUND:/*sender echo request*/
                    {
                        uint64_t fileSize = shoit_misc_ntohll(tcpCrl.fileSize);
                        uint64_t buketSize = shoit_misc_ntohll(tcpCrl.bucketSize); 
                        uint32_t payloadSize = ntohl(tcpCrl.payloadSize);
                        uint32_t dataSize = ntohl(tcpCrl.dataSize);
                        char filename[SIZEOFFILENAME]={0};

                        if(shoit_network_readn(receiver->tcpSockfd,filename,dataSize)!=dataSize){
                            SHOIT_LOG("read filename data failed.\n");
                            done = 1;
                            break;
                        }
                        receiver->fileSize = fileSize;
                        receiver->bucketSize = buketSize;
                        receiver->payloadSize = payloadSize;                                                                         
                        receiver->packetSize =payloadSize+sizeof(shoit_udp_hdr);                                                  


                        if(!prepare_and_receiver_init(receiver,filename,saveFile)){
                            done =1;
                            break;
                        }

                        /*notic thread start udp server*/
                        if(receiver->verbose>1) SHOIT_LOG("recv ROUND signal and wakeup sem\n");
                        sem_post(&receiver->dataSem);/*wakeup a cycle recv*/
                        sem_post(&receiver->progSem);
                    }break;
                    case CLOSE: /*complete,sender will close*/
                        sem_post(&receiver->dataSem);
                        done =1;
                        break;
                    case HEART:
                        if(receiver->verbose>1) SHOIT_LOG("recv HEART signal\n");
                        break;
                    case UDPEND:
                        if(write(receiver->pipe[1]," ",1)<0) perror("write pipe"); /*use pipe notice thread*/
                        break;
                    default:
                        SHOIT_LOG("read error tcpcrl,ignore the recv data. 0xe03\n");
                        done =1;
                        break;
                    
                }
            }else{
                if(write(receiver->pipe[1]," ",1)<0) perror("write pipe"); /*use pipe notice thread*/
                SHOIT_LOG("remote socket close.\n");                                                                                 
                done =1;  
            }
        }
        
        
    }
    receiver->runing = false;
    return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x290, %rsp            # imm = 0x290
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x128(%rbp)
movq -0x8(%rbp), %rax
movl 0x6c(%rax), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
leaq -0x120(%rbp), %rax
movq %rax, -0x158(%rbp)
movl $0x0, -0x150(%rbp)
movl -0x150(%rbp), %eax
cmpq $0x10, %rax
jae 0x6cf7
movq -0x158(%rbp), %rax
movl -0x150(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x150(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x150(%rbp)
jmp 0x6cc5
jmp 0x6cf9
movq -0x8(%rbp), %rax
movl 0x6c(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movq -0x8(%rbp), %rax
movl 0x6c(%rax), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x120(%rbp,%rax,8), %rcx
movq %rcx, -0x120(%rbp,%rax,8)
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x76e0
cmpl $0x0, %eax
jne 0x6d6b
jmp 0x6d4d
movq -0x8(%rbp), %rax
movq 0xe8(%rax), %rdi
leaq 0x30ad(%rip), %rsi      # 0x9e0c
movb $0x0, %al
callq 0x3df0
jmp 0x7174
jmp 0x6d6d
xorl %eax, %eax
cmpl $0x0, -0x128(%rbp)
movb %al, -0x271(%rbp)
jne 0x6d92
movq -0x8(%rbp), %rax
cmpl $0x0, 0x1a8(%rax)
setne %al
movb %al, -0x271(%rbp)
movb -0x271(%rbp), %al
testb $0x1, %al
jne 0x6da1
jmp 0x7166
movq $0x5, -0x20(%rbp)
movq $0x0, -0x18(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0x120(%rbp), %rsi
movl $0x80, %edx
callq 0x2280
movl -0x124(%rbp), %edi
leaq -0xa0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x20(%rbp), %r8
movq %rcx, %rdx
callq 0x22a0
movl %eax, -0x12c(%rbp)
cmpl $0x0, -0x12c(%rbp)
jge 0x6e37
jmp 0x6df7
movq -0x8(%rbp), %rax
movq 0xe8(%rax), %rax
movq %rax, -0x280(%rbp)
callq 0x2060
movl (%rax), %edi
callq 0x2450
movq -0x280(%rbp), %rdi
movq %rax, %rdx
leaq 0x2cda(%rip), %rsi      # 0x9b00
movb $0x0, %al
callq 0x3df0
movl $0x1, -0x128(%rbp)
cmpl $0x0, -0x12c(%rbp)
jle 0x7161
movq -0x8(%rbp), %rax
movl 0x6c(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xa0(%rbp,%rax,8), %rax
movq %rax, -0x288(%rbp)
movq -0x8(%rbp), %rax
movl 0x6c(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x288(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x7161
movq -0x8(%rbp), %rax
movl 0x6c(%rax), %edi
leaq -0x14a(%rbp), %rsi
movl $0x1e, %edx
callq 0x3940
cmpq $0x1e, %rax
jne 0x710d
movq -0x8(%rbp), %rax
cmpl $0x1, 0x5c(%rax)
jle 0x6ee3
jmp 0x6ec2
movq -0x8(%rbp), %rax
movq 0xe8(%rax), %rdi
movl -0x14a(%rbp), %edx
leaq 0x2f59(%rip), %rsi      # 0x9e33
movb $0x0, %al
callq 0x3df0
jmp 0x6ee3
movl -0x14a(%rbp), %eax
addl $-0x2, %eax
movl %eax, %ecx
movq %rcx, -0x290(%rbp)
subl $0x5, %eax
ja 0x70e6
movq -0x290(%rbp), %rax
leaq 0x2d60(%rip), %rcx      # 0x9c6c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x138(%rbp), %rdi
callq 0x3fd0
movq %rax, -0x160(%rbp)
movq -0x140(%rbp), %rdi
callq 0x3fd0
movq %rax, -0x168(%rbp)
movl -0x144(%rbp), %edi
callq 0x2430
movl %eax, -0x16c(%rbp)
movl -0x130(%rbp), %edi
callq 0x2430
movl %eax, -0x170(%rbp)
leaq -0x270(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x21a0
movq -0x8(%rbp), %rax
movl 0x6c(%rax), %edi
leaq -0x270(%rbp), %rsi
movl -0x170(%rbp), %eax
movl %eax, %edx
callq 0x3940
movl -0x170(%rbp), %ecx
cmpq %rcx, %rax
je 0x6fc0
jmp 0x6f98
movq -0x8(%rbp), %rax
movq 0xe8(%rax), %rdi
leaq 0x2eab(%rip), %rsi      # 0x9e55
movb $0x0, %al
callq 0x3df0
movl $0x1, -0x128(%rbp)
jmp 0x710b
movq -0x160(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x168(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x16c(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x2c(%rax)
movl -0x16c(%rbp), %eax
addq $0xc, %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x34(%rax)
movq -0x8(%rbp), %rdi
leaq -0x270(%rbp), %rsi
movq -0x10(%rbp), %rdx
callq 0x7c30
cmpl $0x0, %eax
jne 0x7026
movl $0x1, -0x128(%rbp)
jmp 0x710b
movq -0x8(%rbp), %rax
cmpl $0x1, 0x5c(%rax)
jle 0x704d
jmp 0x7032
movq -0x8(%rbp), %rax
movq 0xe8(%rax), %rdi
leaq 0x2e2d(%rip), %rsi      # 0x9e71
movb $0x0, %al
callq 0x3df0
jmp 0x704d
movq -0x8(%rbp), %rdi
addq $0x120, %rdi            # imm = 0x120
callq 0x21f0
movq -0x8(%rbp), %rdi
addq $0x100, %rdi            # imm = 0x100
callq 0x21f0
jmp 0x710b
movq -0x8(%rbp), %rdi
addq $0x120, %rdi            # imm = 0x120
callq 0x21f0
movl $0x1, -0x128(%rbp)
jmp 0x710b
movq -0x8(%rbp), %rax
cmpl $0x1, 0x5c(%rax)
jle 0x70b5
jmp 0x709a
movq -0x8(%rbp), %rax
movq 0xe8(%rax), %rdi
leaq 0x2de7(%rip), %rsi      # 0x9e93
movb $0x0, %al
callq 0x3df0
jmp 0x70b5
jmp 0x710b
movq -0x8(%rbp), %rax
movl 0x144(%rax), %edi
leaq 0x2605(%rip), %rsi      # 0x96cd
movl $0x1, %edx
callq 0x20b0
cmpq $0x0, %rax
jge 0x70e4
leaq 0x2dc7(%rip), %rdi      # 0x9ea6
callq 0x2390
jmp 0x710b
jmp 0x70e8
movq -0x8(%rbp), %rax
movq 0xe8(%rax), %rdi
leaq 0x2db7(%rip), %rsi      # 0x9eb1
movb $0x0, %al
callq 0x3df0
movl $0x1, -0x128(%rbp)
jmp 0x715f
movq -0x8(%rbp), %rax
movl 0x144(%rax), %edi
leaq 0x25af(%rip), %rsi      # 0x96cd
movl $0x1, %edx
callq 0x20b0
cmpq $0x0, %rax
jge 0x713a
leaq 0x2d71(%rip), %rdi      # 0x9ea6
callq 0x2390
jmp 0x713c
movq -0x8(%rbp), %rax
movq 0xe8(%rax), %rdi
leaq 0x2adf(%rip), %rsi      # 0x9c2d
movb $0x0, %al
callq 0x3df0
movl $0x1, -0x128(%rbp)
jmp 0x7161
jmp 0x6d6d
movq -0x8(%rbp), %rax
movl $0x0, 0x1a8(%rax)
addq $0x290, %rsp            # imm = 0x290
popq %rbp
retq
nopl (%rax)