// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x7f8, %rsp            # imm = 0x7F8
movq %r9, %r12
movq %rsi, %r15
movl %edi, %ebx
leaq 0x10(%rsp), %rax
movl %ecx, (%rax)
leaq 0x14(%rsp), %r14
movl %r8d, (%r14)
leaq 0x20(%rsp), %rdi
xorl %r13d, %r13d
movl $0x7d0, %edx            # imm = 0x7D0
xorl %esi, %esi
callq 0x4200
movl 0x8(%r15), %eax
leaq 0x18(%rsp), %rbp
movl %eax, (%rbp)
movl $0x4, %edx
movl %ebx, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x41c0
leaq 0x1c(%rsp), %r14
movl $0x4, %edx
movl %ebx, %edi
movq %r14, %rsi
callq 0x44a0
movl $0x4, %edx
movl %ebx, %edi
leaq 0x10(%rsp), %rsi
xorl %ecx, %ecx
callq 0x41c0
movl $0x4, %edx
movl %ebx, %edi
movq %r14, %rsi
callq 0x44a0
movl $0x4, %edx
movl %ebx, %edi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x41c0
movl $0x4, %edx
movl %ebx, %edi
movq %r14, %rsi
callq 0x44a0
movq (%r15), %rsi
movslq (%rbp), %rdx
decq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x41c0
movl $0x4, %edx
movl %ebx, %edi
movq %r14, %rsi
callq 0x44a0
movq 0x7f1a(%rip), %rdi      # 0x11fd0
leaq 0x3395(%rip), %rsi      # 0xd452
movl $0x2b, %edx
callq 0x43c0
movq 0x7ed2(%rip), %rax      # 0x11fa0
movq (%rax), %rdi
callq 0x43e0
movl $0x1, (%r14)
movl $0x0, 0xc(%rsp)
movl $0x4, %edx
movl %ebx, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x41c0
leaq 0x14(%rsp), %rax
cmpl $0x2, (%rax)
jl 0xa1e4
leaq 0x20(%rsp), %rbp
leaq 0x87f0(%rip), %r14      # 0x12900
leaq 0x8321(%rip), %r15      # 0x12438
movslq %r13d, %rax
movq (%r12), %rcx
movl 0x830(%rsp), %edx
cmpl %edx, (%rcx,%rax,4)
jne 0xa1bc
movl $0x4, %edx
movl %ebx, %edi
leaq 0xc(%rsp), %rsi
xorl %ecx, %ecx
callq 0x41c0
movl $0x400, %edx            # imm = 0x400
movl %ebx, %edi
movq %rbp, %rsi
callq 0x44a0
movq %r14, %rdi
callq 0x4210
movslq 0xc(%rsp), %rsi
shlq $0xa, %rsi
movq 0x838(%rsp), %r13
movq %r13, %rdi
xorl %edx, %edx
callq 0x43f0
movl $0x400, %edx            # imm = 0x400
movq %r13, %rdi
movq %rbp, %rsi
callq 0x4460
movq %r15, %rdi
movq 0x840(%rsp), %rsi
callq 0xa6a0
movslq 0xc(%rsp), %rcx
movq (%rax), %rax
movb $0x31, (%rax,%rcx)
incl 0xc(%rsp)
movq %r14, %rdi
callq 0x4580
movl $0x7d0, %edx            # imm = 0x7D0
movq %rbp, %rdi
xorl %esi, %esi
callq 0x4200
jmp 0xa1d0
movq %r14, %rdi
callq 0x4210
incl 0xc(%rsp)
movq %r14, %rdi
callq 0x4580
movl 0xc(%rsp), %r13d
movl 0x14(%rsp), %eax
decl %eax
cmpl %eax, %r13d
jl 0xa117
leaq 0xc(%rsp), %r14
movl $0x4, %edx
movl %ebx, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x41c0
movslq (%r14), %rax
movq (%r12), %rcx
movl 0x830(%rsp), %edx
cmpl %edx, (%rcx,%rax,4)
jne 0xa2df
movslq 0x10(%rsp), %rdx
leaq 0x20(%rsp), %r14
testq %rdx, %rdx
je 0xa258
movl %ebx, %edi
movq %r14, %rsi
callq 0x44a0
leaq 0x86cf(%rip), %rdi      # 0x12900
callq 0x4210
movslq 0xc(%rsp), %rsi
shlq $0xa, %rsi
movq 0x838(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x43f0
movslq 0x10(%rsp), %rdx
jmp 0xa293
movl $0x400, %edx            # imm = 0x400
movl %ebx, %edi
movq %r14, %rsi
callq 0x44a0
leaq 0x8692(%rip), %rdi      # 0x12900
callq 0x4210
movslq 0xc(%rsp), %rsi
shlq $0xa, %rsi
movq 0x838(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x43f0
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
movq %r14, %rsi
callq 0x4460
leaq 0x8193(%rip), %rdi      # 0x12438
movq 0x840(%rsp), %rsi
callq 0xa6a0
movslq 0xc(%rsp), %rcx
movq (%rax), %rax
movb $0x31, (%rax,%rcx)
incl 0xc(%rsp)
leaq 0x8637(%rip), %rdi      # 0x12900
callq 0x4580
leaq 0x20(%rsp), %rdi
movl $0x7d0, %edx            # imm = 0x7D0
xorl %esi, %esi
callq 0x4200
movq 0x7cea(%rip), %rdi      # 0x11fd0
leaq 0x3191(%rip), %rsi      # 0xd47e
movl $0x10, %edx
callq 0x43c0
movl %ebx, %edi
callq 0x4670
leaq 0x31da(%rip), %rdi      # 0xd4df
callq 0x44f0
addq $0x7f8, %rsp            # imm = 0x7F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq