// File: /workspace/github_c_makefile_star2_O0/pencode-lab[P]Shoit/shoit_receiver.c
// Function: prepare_and_receiver_init
static bool prepare_and_receiver_init(shoit_core_t *receiver,char *srcFileName,char *saveFileName)
{

    //if(receiver->bucket!=NULL) return true; /*already malloc and init*/

    if(receiver->iStream==false){/*use mmap*/

        if(receiver->bucket == NULL) {//first come to here

            char destFile[512]={0};
            bzero(destFile,sizeof(destFile));
            if(saveFileName==NULL)
                snprintf(destFile,sizeof(destFile)-1,"%s.shoit.recv.%ld",srcFileName,time(NULL));
            else
                memcpy(destFile,saveFileName,strlen(saveFileName));

            if(receiver->verbose>0)
                SHOIT_LOG("remote send file[%s], will save as[%s] to local disk.\n",srcFileName,destFile);

            receiver->toFd = open(destFile, O_RDWR|O_CREAT|O_TRUNC, 0666);
            if(receiver->toFd <0 || ftruncate(receiver->toFd,receiver->fileSize)){ 
                SHOIT_LOG("open file error(%s)\n",strerror(errno)); 
                return false;
            }
            receiver->useMmap = true;
            receiver->leftSize = receiver->fileSize;

            //fprintf(stderr,"init map,fileSize:%ld,leftSize:%ld \n",receiver->fileSize,receiver->leftSize);

        }
   
        if(receiver->leftSize <=0)  return false;

        size_t mapSize = (receiver->leftSize/receiver->bucketSize > 0) ? receiver->bucketSize: receiver->leftSize;
        off_t offset= receiver->fileSize - receiver->leftSize;//receiver->bucketSize *((receiver->fileSize - receiver->leftSize)/receiver->bucketSize);
        
        receiver->bucket= (char *)mmap(NULL, mapSize, PROT_READ|PROT_WRITE, MAP_SHARED,receiver->toFd,offset);
        if (receiver->bucket== MAP_FAILED) {
            //fprintf(stderr,"mapSize = %ld,bucketSize:%ld offset = %ld\n",mapSize,receiver->bucketSize,offset); 
            SHOIT_LOG("mmap error(%s)\n",strerror(errno));
            return false;
        }    
        receiver->bucketSize = mapSize;
        receiver->leftSize -= mapSize;
        receiver->mapSize = mapSize;
        //fprintf(stderr,"mapSize = %ld,bucketSize:%ld offset = %ld\n",mapSize,receiver->bucketSize,offset); 

    }else{
        if(receiver->bucket!=NULL) return true; /*already malloc and init*/
        receiver->bucket =(char*)malloc(receiver->bucketSize);
        receiver->useMmap = false;                                                                                                    
        if(receiver->bucket == NULL) {
            SHOIT_LOG("malloc error(%s)\n",strerror(errno)); 
            return false;
        }
    }

   
    if (receiver->bucketSize % receiver->payloadSize == 0)
    {
        receiver->totalNumberOfPackets = receiver->bucketSize/receiver->payloadSize;
        receiver->lastPayloadSize = receiver->payloadSize;
    }
    else
    {
        receiver->totalNumberOfPackets = receiver->bucketSize/receiver->payloadSize + 1; /* the last packet is not full */
        receiver->lastPayloadSize = receiver->bucketSize - receiver->payloadSize * (receiver->totalNumberOfPackets - 1);
    }

    receiver->receivedNumberOfPackets = 0;
    receiver->remainNumberOfPackets = receiver->totalNumberOfPackets;
    receiver->sizeofErrorBitmap = receiver->totalNumberOfPackets/8 + 2;
    receiver->errorBitmap = (char *)malloc(receiver->sizeofErrorBitmap+sizeof(shoit_control_t));
    receiver->hashTable = (uint64_t *)malloc(receiver->totalNumberOfPackets * sizeof(uint64_t));
    receiver->isFirstBlast = true;
    
    if(receiver->verbose>1) SHOIT_LOG("totalNumberOfPackets: %d", receiver->totalNumberOfPackets);
    if (receiver->errorBitmap == NULL)
    {
        SHOIT_LOG("malloc %ld bytes error(%s)\n",receiver->sizeofErrorBitmap,strerror(errno));
        return false;
    }
    if (receiver->hashTable == NULL)
    {
        SHOIT_LOG("malloc hashTable failed\n");
        return false;
    }

    /* Initialize the hash table */
    for (uint64_t i=0; i<receiver->totalNumberOfPackets; i++)
    {
        receiver->hashTable[i] = i;
    }
    shoit_init_error_bitmap(receiver);

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x290, %rsp            # imm = 0x290
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1a4(%rax)
jne 0x7f2b
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x7df4
leaq -0x220(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x21a0
leaq -0x220(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x21a0
cmpq $0x0, -0x20(%rbp)
jne 0x7cdb
leaq -0x220(%rbp), %rax
movq %rax, -0x248(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x240(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x2290
movq -0x248(%rbp), %rdi
movq -0x240(%rbp), %rcx
movq %rax, %r8
movl $0x1ff, %esi            # imm = 0x1FF
leaq 0x220e(%rip), %rdx      # 0x9ee0
movb $0x0, %al
callq 0x2140
jmp 0x7d13
leaq -0x220(%rbp), %rax
movq %rax, -0x258(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x250(%rbp)
movq -0x20(%rbp), %rdi
callq 0x20d0
movq -0x258(%rbp), %rdi
movq -0x250(%rbp), %rsi
movq %rax, %rdx
callq 0x2280
movq -0x10(%rbp), %rax
cmpl $0x0, 0x5c(%rax)
jle 0x7d45
jmp 0x7d1f
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rdi
movq -0x18(%rbp), %rdx
leaq -0x220(%rbp), %rcx
leaq 0x21b6(%rip), %rsi      # 0x9ef2
movb $0x0, %al
callq 0x3df0
jmp 0x7d45
leaq -0x220(%rbp), %rdi
movl $0x242, %esi            # imm = 0x242
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x2370
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xfc(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0xfc(%rax)
jl 0x7d92
movq -0x10(%rbp), %rax
movl 0xfc(%rax), %edi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x2170
cmpl $0x0, %eax
je 0x7dd6
jmp 0x7d94
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rax
movq %rax, -0x260(%rbp)
callq 0x2060
movl (%rax), %edi
callq 0x2450
movq -0x260(%rbp), %rdi
movq %rax, %rdx
leaq 0x2166(%rip), %rsi      # 0x9f29
movb $0x0, %al
callq 0x3df0
movl $0x0, -0x4(%rbp)
jmp 0x81fc
movq -0x10(%rbp), %rax
movl $0x1, 0x1a0(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
ja 0x7e0b
movl $0x0, -0x4(%rbp)
jmp 0x81fc
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
xorl %edx, %edx
divq 0x8(%rcx)
cmpq $0x0, %rax
jbe 0x7e34
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x268(%rbp)
jmp 0x7e43
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x268(%rbp)
movq -0x268(%rbp), %rax
movq %rax, -0x228(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
subq 0x18(%rcx), %rax
movq %rax, -0x230(%rbp)
movq -0x228(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0xfc(%rax), %r8d
movq -0x230(%rbp), %r9
xorl %eax, %eax
movl %eax, %edi
movl $0x3, %edx
movl $0x1, %ecx
callq 0x20f0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq $-0x1, %rcx
cmpq %rcx, (%rax)
jne 0x7ef2
jmp 0x7eb0
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rax
movq %rax, -0x270(%rbp)
callq 0x2060
movl (%rax), %edi
callq 0x2450
movq -0x270(%rbp), %rdi
movq %rax, %rdx
leaq 0x205f(%rip), %rsi      # 0x9f3e
movb $0x0, %al
callq 0x3df0
movl $0x0, -0x4(%rbp)
jmp 0x81fc
movq -0x228(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x228(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x18(%rax)
movq -0x228(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
jmp 0x7fb6
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
je 0x7f41
movl $0x1, -0x4(%rbp)
jmp 0x81fc
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x22e0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x1a0(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x7fb4
jmp 0x7f72
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rax
movq %rax, -0x278(%rbp)
callq 0x2060
movl (%rax), %edi
callq 0x2450
movq -0x278(%rbp), %rdi
movq %rax, %rdx
leaq 0x1767(%rip), %rsi      # 0x9708
movb $0x0, %al
callq 0x3df0
movl $0x0, -0x4(%rbp)
jmp 0x81fc
jmp 0x7fb6
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x2c(%rcx), %ecx
xorl %edx, %edx
divq %rcx
cmpq $0x0, %rdx
jne 0x7fff
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x2c(%rcx), %ecx
xorl %edx, %edx
divq %rcx
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
movl 0x2c(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x30(%rax)
jmp 0x804d
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x2c(%rcx), %ecx
xorl %edx, %edx
divq %rcx
movq %rax, %rcx
addq $0x1, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x2c(%rcx), %ecx
movq -0x10(%rbp), %rdx
movq 0x38(%rdx), %rdx
subq $0x1, %rdx
imulq %rdx, %rcx
subq %rcx, %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x48(%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
shrq $0x3, %rcx
addq $0x2, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xd8(%rax)
movq -0x10(%rbp), %rax
movq 0xd8(%rax), %rdi
addq $0x1e, %rdi
callq 0x22e0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xd0(%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
shlq $0x3, %rdi
callq 0x22e0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xc8(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0xe0(%rax)
movq -0x10(%rbp), %rax
cmpl $0x1, 0x5c(%rax)
jle 0x8102
jmp 0x80df
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdx
leaq 0x1e55(%rip), %rsi      # 0x9f4e
movb $0x0, %al
callq 0x3df0
jmp 0x8102
movq -0x10(%rbp), %rax
cmpq $0x0, 0xd0(%rax)
jne 0x816d
jmp 0x8112
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rax
movq %rax, -0x288(%rbp)
movq -0x10(%rbp), %rax
movq 0xd8(%rax), %rax
movq %rax, -0x280(%rbp)
callq 0x2060
movl (%rax), %edi
callq 0x2450
movq -0x288(%rbp), %rdi
movq -0x280(%rbp), %rdx
movq %rax, %rcx
leaq 0x1e0d(%rip), %rsi      # 0x9f67
movb $0x0, %al
callq 0x3df0
movl $0x0, -0x4(%rbp)
jmp 0x81fc
movq -0x10(%rbp), %rax
cmpq $0x0, 0xc8(%rax)
jne 0x819f
jmp 0x817d
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rdi
leaq 0x158b(%rip), %rsi      # 0x971a
movb $0x0, %al
callq 0x3df0
movl $0x0, -0x4(%rbp)
jmp 0x81fc
movq $0x0, -0x238(%rbp)
movq -0x238(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0x81ec
movq -0x238(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rax
movq -0x238(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x238(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x238(%rbp)
jmp 0x81aa
movq -0x10(%rbp), %rdi
callq 0x2c20
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x290, %rsp            # imm = 0x290
popq %rbp
retq