// File: /workspace/github_cpp_makefile_star2_O2/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, %rbp
movq %rsi, 0x10(%rsp)
movl %edi, %ebx
leaq 0x4(%rsp), %rax
movl %ecx, (%rax)
leaq 0xc(%rsp), %r15
movl %r8d, (%r15)
leaq 0x20(%rsp), %r13
xorl %r14d, %r14d
movl $0x7d0, %edx            # imm = 0x7D0
movq %r13, %rdi
xorl %esi, %esi
callq 0x4210
movl 0x8(%rbp), %eax
leaq 0x8(%rsp), %rcx
movl %eax, (%rcx)
pushq $0x4
popq %rbp
movl %ebx, %edi
movq %r15, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
callq 0x41d0
leaq 0x1c(%rsp), %r15
movl %ebx, %edi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x4490
movl %ebx, %edi
leaq 0x4(%rsp), %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
callq 0x41d0
movl %ebx, %edi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x4490
movl %ebx, %edi
leaq 0x8(%rsp), %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
callq 0x41d0
movl %ebx, %edi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x4490
movq 0x10(%rsp), %rax
movq (%rax), %rsi
leaq 0x8(%rsp), %rax
movslq (%rax), %rdx
decq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x41d0
movl %ebx, %edi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x4490
movq 0x7b4a(%rip), %rdi      # 0xffc8
leaq 0x2fcd(%rip), %rsi      # 0xb452
callq 0x4330
movq 0x7b17(%rip), %rax      # 0xffa8
movq (%rax), %rdi
callq 0x43c0
movl $0x1, (%r15)
movq %rsp, %rax
andl $0x0, (%rax)
movl %ebx, %edi
movq %r15, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
callq 0x41d0
leaq 0x8434(%rip), %r15      # 0x108f0
leaq 0x7f65(%rip), %rbp      # 0x10428
movl 0xc(%rsp), %eax
decl %eax
cmpl %eax, %r14d
jge 0x858c
movslq %r14d, %rax
movq (%r12), %rcx
movl 0x830(%rsp), %edx
cmpl %edx, (%rcx,%rax,4)
jne 0x8570
movl %ebx, %edi
movq %rsp, %rsi
pushq $0x4
popq %rdx
xorl %ecx, %ecx
callq 0x41d0
movl $0x400, %edx            # imm = 0x400
movl %ebx, %edi
movq %r13, %rsi
callq 0x4490
movq %r15, %rdi
callq 0x4220
movslq (%rsp), %rsi
shlq $0xa, %rsi
movq 0x838(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x43d0
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
movq %r13, %rsi
callq 0x4450
movq %rbp, %rdi
movq 0x840(%rsp), %rsi
callq 0x8a08
movslq (%rsp), %rcx
movq (%rax), %rax
movb $0x31, (%rax,%rcx)
incl (%rsp)
movq %r15, %rdi
callq 0x4560
movl $0x7d0, %edx            # imm = 0x7D0
movq %r13, %rdi
xorl %esi, %esi
callq 0x4210
jmp 0x8583
movq %r15, %rdi
callq 0x4220
incl (%rsp)
movq %r15, %rdi
callq 0x4560
movl (%rsp), %r14d
jmp 0x84c3
movq %rsp, %r15
pushq $0x4
popq %rdx
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x41d0
movslq (%r15), %rax
movq (%r12), %rcx
movl 0x830(%rsp), %edx
cmpl %edx, (%rcx,%rax,4)
jne 0x867f
movslq 0x4(%rsp), %rdx
leaq 0x20(%rsp), %r14
testq %rdx, %rdx
je 0x85fb
movl %ebx, %edi
movq %r14, %rsi
callq 0x4490
leaq 0x831b(%rip), %rdi      # 0x108f0
callq 0x4220
movslq (%rsp), %rsi
shlq $0xa, %rsi
movq 0x838(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x43d0
movslq 0x4(%rsp), %rdx
jmp 0x8635
movl $0x400, %edx            # imm = 0x400
movl %ebx, %edi
movq %r14, %rsi
callq 0x4490
leaq 0x82df(%rip), %rdi      # 0x108f0
callq 0x4220
movslq (%rsp), %rsi
shlq $0xa, %rsi
movq 0x838(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x43d0
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
movq %r14, %rsi
callq 0x4450
leaq 0x7de1(%rip), %rdi      # 0x10428
movq 0x840(%rsp), %rsi
callq 0x8a08
movslq (%rsp), %rcx
movq (%rax), %rax
movb $0x31, (%rax,%rcx)
incl (%rsp)
leaq 0x8287(%rip), %rdi      # 0x108f0
callq 0x4560
leaq 0x20(%rsp), %rdi
movl $0x7d0, %edx            # imm = 0x7D0
xorl %esi, %esi
callq 0x4210
movq 0x7942(%rip), %rdi      # 0xffc8
leaq 0x2df1(%rip), %rsi      # 0xb47e
callq 0x4330
movl %ebx, %edi
callq 0x4660
leaq 0x2e3f(%rip), %rdi      # 0xb4df
callq 0x44e0
addq $0x7f8, %rsp            # imm = 0x7F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq