// File: girdhari9[P]mTORRENT/ClientConnection.cpp
// Function: RecieveDataFromServer(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, int, int, std::vector<int, std::allocator<int>>, int, std::basic_ofstream<char, std::char_traits<char>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void RecieveDataFromServer(int sock,string recvFileName, int FileSize, int LastPartSize, int TotalPacket, vector<int> BitMap1, int thread_n, ofstream &output, string SHA){
    char sendBuffer[BUFFER_SIZE];
    char recvBuffer[BUFFER_SIZE]; 
    memset(&recvBuffer, '\0',BUFFER_SIZE);
    memset(&sendBuffer, '\0',BUFFER_SIZE);

    int ack;

    int fileNameLength = recvFileName.length();

    send(sock , &TotalPacket , sizeof(int) , 0 );
    read(sock, &ack, sizeof(int));
    send(sock , &LastPartSize , sizeof(int) , 0 );
    read(sock, &ack, sizeof(int));
    send(sock , &fileNameLength , sizeof(int) , 0 );
    read(sock, &ack, sizeof(int));
    send(sock , recvFileName.c_str() , fileNameLength-1 , 0 );
    read(sock, &ack, sizeof(int));

    cout<<"[+]system connected...\n> recieving data...\n";
    fflush(stdout);
    ack = 1;

    int PieceNo=0, index;
    send(sock , &ack, sizeof(int) , 0);

    while(PieceNo < TotalPacket-1){
        if(BitMap1[PieceNo] == thread_n){
            send(sock, &PieceNo, sizeof(int), 0);
            read(sock, recvBuffer, DataSize);

            sem_wait(&mutex1);
                index = PieceNo * DataSize;
                output.seekp(index);
                output.write(recvBuffer,DataSize);
                TotalFile[SHA][PieceNo] = '1'; 
                PieceNo++;
            sem_post(&mutex1);
            memset(&recvBuffer, '\0',BUFFER_SIZE);
        }
        else{
            sem_wait(&mutex1);
                PieceNo++;
            sem_post(&mutex1);
        }
    }
    send(sock, &PieceNo, sizeof(int), 0);
    if(BitMap1[PieceNo] == thread_n){
        if(LastPartSize){
            read(sock, recvBuffer, LastPartSize);
            sem_wait(&mutex1);
                index = PieceNo * DataSize;
                output.seekp(index);
                output.write(recvBuffer,LastPartSize);
                TotalFile[SHA][PieceNo] = '1'; 
                index += TotalConn * DataSize;
                PieceNo++;
            sem_post(&mutex1);
        }
        else{
            read(sock, recvBuffer, DataSize);
            sem_wait(&mutex1);
                index = PieceNo * DataSize;
                output.seekp(index);
                output.write(recvBuffer,DataSize);
                TotalFile[SHA][PieceNo] = '1'; 
                index += TotalConn * DataSize;
                PieceNo++;
            sem_post(&mutex1);
        }
        memset(&recvBuffer, '\0',BUFFER_SIZE);
    }
    cout<<"> Data Received\n";
    close(sock);
    printf("> Connection closed!\n");
    return ;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1050, %rsp           # imm = 0x1050
movq %r9, -0x1030(%rbp)
movq %rsi, -0x1020(%rbp)
movq 0x20(%rbp), %rax
movq %rax, -0x1028(%rbp)
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movq %r9, -0x28(%rbp)
leaq -0xfd0(%rbp), %rdi
xorl %esi, %esi
movl $0x7d0, %edx            # imm = 0x7D0
callq 0x4260
leaq -0x800(%rbp), %rdi
xorl %esi, %esi
movl $0x7d0, %edx            # imm = 0x7D0
callq 0x4260
movq -0x1020(%rbp), %rdi
callq 0x4600
movl %eax, -0xfd8(%rbp)
movl -0x4(%rbp), %edi
leaq -0x1c(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x4230
movl -0x4(%rbp), %edi
leaq -0xfd4(%rbp), %rsi
movl $0x4, %edx
callq 0x45c0
movl -0x4(%rbp), %edi
leaq -0x18(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x4230
movl -0x4(%rbp), %edi
leaq -0xfd4(%rbp), %rsi
movl $0x4, %edx
callq 0x45c0
movl -0x4(%rbp), %edi
leaq -0xfd8(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x4230
movl -0x4(%rbp), %edi
leaq -0xfd4(%rbp), %rsi
movl $0x4, %edx
callq 0x45c0
movq -0x1020(%rbp), %rdi
movl -0x4(%rbp), %eax
movl %eax, -0x1014(%rbp)
callq 0x41f0
movl -0x1014(%rbp), %edi
movq %rax, %rsi
movl -0xfd8(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rdx
xorl %ecx, %ecx
callq 0x4230
movl -0x4(%rbp), %edi
leaq -0xfd4(%rbp), %rsi
movl $0x4, %edx
callq 0x45c0
movq 0x13a5c(%rip), %rdi     # 0x20fc8
leaq 0x8f52(%rip), %rsi      # 0x164c5
callq 0x43f0
movq 0x13a21(%rip), %rax     # 0x20fa0
movq (%rax), %rdi
callq 0x44c0
movl $0x1, -0xfd4(%rbp)
movl $0x0, -0xfdc(%rbp)
movl -0x4(%rbp), %edi
leaq -0xfd4(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x4230
movl -0xfdc(%rbp), %eax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xd6f6
movq -0x1030(%rbp), %rdi
movslq -0xfdc(%rbp), %rsi
callq 0xe070
movl (%rax), %eax
cmpl 0x10(%rbp), %eax
jne 0xd6ca
movl -0x4(%rbp), %edi
leaq -0xfdc(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x4230
movl -0x4(%rbp), %edi
leaq -0xfd0(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x45c0
leaq 0x1439c(%rip), %rdi     # 0x219b0
callq 0x4280
movl -0xfdc(%rbp), %eax
shll $0xa, %eax
movl %eax, -0xfe0(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0x1038(%rbp)
movslq -0xfe0(%rbp), %rsi
leaq -0xff0(%rbp), %rdi
callq 0xe090
movq -0x1038(%rbp), %rdi
movq -0xff0(%rbp), %rsi
movq -0xfe8(%rbp), %rdx
callq 0x44d0
movq 0x18(%rbp), %rdi
leaq -0xfd0(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x4570
movq -0x1028(%rbp), %rsi
leaq 0x13e65(%rip), %rdi     # 0x214e8
callq 0xe0d0
movq %rax, %rdi
movslq -0xfdc(%rbp), %rsi
callq 0x47d0
movb $0x31, (%rax)
movl -0xfdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfdc(%rbp)
leaq 0x14300(%rip), %rdi     # 0x219b0
callq 0x46a0
leaq -0xfd0(%rbp), %rdi
xorl %esi, %esi
movl $0x7d0, %edx            # imm = 0x7D0
callq 0x4260
jmp 0xd6f1
leaq 0x142df(%rip), %rdi     # 0x219b0
callq 0x4280
movl -0xfdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfdc(%rbp)
leaq 0x142c4(%rip), %rdi     # 0x219b0
callq 0x46a0
jmp 0xd5b1
movl -0x4(%rbp), %edi
leaq -0xfdc(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x4230
movq -0x1030(%rbp), %rdi
movslq -0xfdc(%rbp), %rsi
callq 0xe070
movl (%rax), %eax
cmpl 0x10(%rbp), %eax
jne 0xd8ec
cmpl $0x0, -0x18(%rbp)
je 0xd808
movl -0x4(%rbp), %edi
leaq -0xfd0(%rbp), %rsi
movslq -0x18(%rbp), %rdx
callq 0x45c0
leaq 0x14262(%rip), %rdi     # 0x219b0
callq 0x4280
movl -0xfdc(%rbp), %eax
shll $0xa, %eax
movl %eax, -0xfe0(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0x1040(%rbp)
movslq -0xfe0(%rbp), %rsi
leaq -0x1000(%rbp), %rdi
callq 0xe090
movq -0x1040(%rbp), %rdi
movq -0x1000(%rbp), %rsi
movq -0xff8(%rbp), %rdx
callq 0x44d0
movq 0x18(%rbp), %rdi
leaq -0xfd0(%rbp), %rsi
movslq -0x18(%rbp), %rdx
callq 0x4570
movq -0x1028(%rbp), %rsi
leaq 0x13d2c(%rip), %rdi     # 0x214e8
callq 0xe0d0
movq %rax, %rdi
movslq -0xfdc(%rbp), %rsi
callq 0x47d0
movb $0x31, (%rax)
movl 0x141f7(%rip), %eax     # 0x219d0
shll $0xa, %eax
addl -0xfe0(%rbp), %eax
movl %eax, -0xfe0(%rbp)
movl -0xfdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfdc(%rbp)
leaq 0x141b2(%rip), %rdi     # 0x219b0
callq 0x46a0
jmp 0xd8d9
movl -0x4(%rbp), %edi
leaq -0xfd0(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x45c0
leaq 0x1418d(%rip), %rdi     # 0x219b0
callq 0x4280
movl -0xfdc(%rbp), %eax
shll $0xa, %eax
movl %eax, -0xfe0(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0x1048(%rbp)
movslq -0xfe0(%rbp), %rsi
leaq -0x1010(%rbp), %rdi
callq 0xe090
movq -0x1048(%rbp), %rdi
movq -0x1010(%rbp), %rsi
movq -0x1008(%rbp), %rdx
callq 0x44d0
movq 0x18(%rbp), %rdi
leaq -0xfd0(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x4570
movq -0x1028(%rbp), %rsi
leaq 0x13c56(%rip), %rdi     # 0x214e8
callq 0xe0d0
movq %rax, %rdi
movslq -0xfdc(%rbp), %rsi
callq 0x47d0
movb $0x31, (%rax)
movl 0x14121(%rip), %eax     # 0x219d0
shll $0xa, %eax
addl -0xfe0(%rbp), %eax
movl %eax, -0xfe0(%rbp)
movl -0xfdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfdc(%rbp)
leaq 0x140dc(%rip), %rdi     # 0x219b0
callq 0x46a0
leaq -0xfd0(%rbp), %rdi
xorl %esi, %esi
movl $0x7d0, %edx            # imm = 0x7D0
callq 0x4260
movq 0x136d5(%rip), %rdi     # 0x20fc8
leaq 0x8bf7(%rip), %rsi      # 0x164f1
callq 0x43f0
movl -0x4(%rbp), %edi
callq 0x47b0
leaq 0x8a6f(%rip), %rdi      # 0x1637d
movb $0x0, %al
callq 0x4070
addq $0x1050, %rsp           # imm = 0x1050
popq %rbp
retq
nop