// File: /workspace/github_c_makefile_star2_O0/pencode-lab[P]Shoit/shoit_receiver.c
// Function: blast_udp_recving
static void blast_udp_recving(shoit_core_t *receiver)
{
    int done, actualPayloadSize, retval;
    uint64_t seqno;
    struct timeval start;

    char *msg = (char *) malloc(receiver->packetSize);    
    shoit_udp_hdr *udphdr = (shoit_udp_hdr*)msg;

    struct timeval timeout;
    fd_set rset,orgSet;
    int maxfdpl;
    done = 0; seqno = 0;

    #define QMAX(x, y) ((x)>(y)?(x):(y))
    
    maxfdpl = QMAX(receiver->udpSockfd,receiver->pipe[0]) + 1;
    FD_ZERO(&orgSet);
    FD_SET(receiver->udpSockfd, &orgSet);
    FD_SET(receiver->pipe[0], &orgSet);
    gettimeofday(&start, NULL);


    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 == -1){
            perror("select()");
            break;
        }else if(retval>0){
            if (FD_ISSET(receiver->udpSockfd, &rset)){
                int nread = recv(receiver->udpSockfd, msg, receiver->packetSize, 0);
                if(nread>0){
                    seqno = udphdr->seqno;
                    actualPayloadSize = ntohl(udphdr->payloadSize);
                    uint64_t offset= (uint64_t)(seqno*receiver->payloadSize);
                    /*
                    if((offset+actualPayloadSize) >= 1426063360) 
                        fprintf(stderr,"bucket at:0x%p, offset:%ld, mapSize:%ld, offfset+payload:%ld\n",receiver->bucket,
                            offset,receiver->mapSize,actualPayloadSize+offset);
                    */
                    memcpy((char *)receiver->bucket+offset,udphdr->payload,actualPayloadSize);
                    shoit_update_error_bitmap(receiver,seqno);
                    receiver->receivedNumberOfPackets ++;
                    progress.total += nread;
                }else {
                    perror("recv udp packet");
                    done =1;
                }
            }else if(FD_ISSET(receiver->pipe[0],&rset)){
                char c;
                if(read(receiver->pipe[0],&c,1)<0) perror("read pipe");
                done = 1;                                                                                                                
            }
        }

    }       
    free(msg);
    return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, %edi
callq 0x22e0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0xc(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x8(%rbp), %rax
movl 0x68(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x140(%rcx), %eax
jle 0x78da
movq -0x8(%rbp), %rax
movl 0x68(%rax), %eax
movl %eax, -0x178(%rbp)
jmp 0x78ea
movq -0x8(%rbp), %rax
movl 0x140(%rax), %eax
movl %eax, -0x178(%rbp)
movl -0x178(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x154(%rbp)
leaq -0x150(%rbp), %rax
movq %rax, -0x160(%rbp)
movl $0x0, -0x158(%rbp)
movl -0x158(%rbp), %eax
cmpq $0x10, %rax
jae 0x7943
movq -0x160(%rbp), %rax
movl -0x158(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x158(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x158(%rbp)
jmp 0x7911
jmp 0x7945
movq -0x8(%rbp), %rax
movl 0x68(%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 0x68(%rax), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x150(%rbp,%rax,8), %rcx
movq %rcx, -0x150(%rbp,%rax,8)
movq -0x8(%rbp), %rax
movl 0x140(%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 0x140(%rax), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x150(%rbp,%rax,8), %rcx
movq %rcx, -0x150(%rbp,%rax,8)
leaq -0x30(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2180
xorl %eax, %eax
cmpl $0x0, -0xc(%rbp)
movb %al, -0x179(%rbp)
jne 0x79f8
movq -0x8(%rbp), %rax
cmpl $0x0, 0x1a8(%rax)
setne %al
movb %al, -0x179(%rbp)
movb -0x179(%rbp), %al
testb $0x1, %al
jne 0x7a07
jmp 0x7c1a
movq $0x5, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0x150(%rbp), %rsi
movl $0x80, %edx
callq 0x2280
movl -0x154(%rbp), %edi
leaq -0xd0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x50(%rbp), %r8
movq %rcx, %rdx
callq 0x22a0
movl %eax, -0x14(%rbp)
cmpl $-0x1, -0x14(%rbp)
jne 0x7a66
leaq 0x238d(%rip), %rdi      # 0x9de9
callq 0x2390
jmp 0x7c1a
cmpl $0x0, -0x14(%rbp)
jle 0x7c13
movq -0x8(%rbp), %rax
movl 0x68(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xd0(%rbp,%rax,8), %rax
movq %rax, -0x188(%rbp)
movq -0x8(%rbp), %rax
movl 0x68(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x188(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x7b89
movq -0x8(%rbp), %rax
movl 0x68(%rax), %edi
movq -0x38(%rbp), %rsi
movq -0x8(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x2050
movl %eax, -0x164(%rbp)
cmpl $0x0, -0x164(%rbp)
jle 0x7b71
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x40(%rbp), %rax
movl 0x8(%rax), %edi
callq 0x2430
movl %eax, -0x10(%rbp)
movq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
movl 0x2c(%rcx), %ecx
imulq %rcx, %rax
movq %rax, -0x170(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
addq -0x170(%rbp), %rdi
movq -0x40(%rbp), %rsi
addq $0xc, %rsi
movslq -0x10(%rbp), %rdx
callq 0x2280
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x2d00
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x48(%rax)
movslq -0x164(%rbp), %rax
addq 0x4798(%rip), %rax      # 0xc300
movq %rax, 0x4791(%rip)      # 0xc300
jmp 0x7b84
leaq 0x227a(%rip), %rdi      # 0x9df2
callq 0x2390
movl $0x1, -0xc(%rbp)
jmp 0x7c11
movq -0x8(%rbp), %rax
movl 0x140(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xd0(%rbp,%rax,8), %rax
movq %rax, -0x190(%rbp)
movq -0x8(%rbp), %rax
movl 0x140(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x190(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x7c0f
movq -0x8(%rbp), %rax
movl 0x140(%rax), %edi
leaq -0x171(%rbp), %rsi
movl $0x1, %edx
callq 0x2200
cmpq $0x0, %rax
jge 0x7c08
leaq 0x21ff(%rip), %rdi      # 0x9e02
callq 0x2390
movl $0x1, -0xc(%rbp)
jmp 0x7c11
jmp 0x7c13
jmp 0x7c15
jmp 0x79d6
movq -0x38(%rbp), %rdi
callq 0x2040
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
nopl (%rax)