// File: /workspace/github_cpp_makefile_star2_O3/girdhari9[P]mTORRENT/ServerConnection.cpp
// Function: SendData(int)
void SendData(int new_socket){
    if(fork() == 0){
        char recvBuffer[BUFFER_SIZE];
        char sendBuffer[BUFFER_SIZE]; 
        memset(&recvBuffer, '\0',BUFFER_SIZE);
        memset(&sendBuffer, '\0',BUFFER_SIZE);

        int ack;

        int fileNameLength,LastPartSize = 0,TotalPacket;

        read(new_socket, &TotalPacket, sizeof(int));
        send(new_socket, &ack, sizeof(int), 0);
        read(new_socket, &LastPartSize, sizeof(int));
        send(new_socket, &ack, sizeof(int), 0);
        read(new_socket, &fileNameLength, sizeof(int));
        send(new_socket, &ack, sizeof(int), 0);
        read(new_socket, recvBuffer, fileNameLength-1);
        send(new_socket, &ack, sizeof(int), 0);

        ifstream input;
        input.open(recvBuffer,ifstream::binary); //Add Path here
        memset(&recvBuffer, '\0',BUFFER_SIZE);

        printf("> Sending data...\n");

        read(new_socket, &ack, sizeof(int));
        
        int index, PieceNo = -1; 
        while(PieceNo < TotalPacket-1){
            read(new_socket, &PieceNo, sizeof(int));
            index = PieceNo * DataSize;
            input.seekg (index);

            if(PieceNo == TotalPacket){ // Last packet issue may be there
                if(LastPartSize){
                    input.read(sendBuffer,LastPartSize);
                    send(new_socket , (char *)sendBuffer, LastPartSize,0);
                }
                else{
                    input.read(sendBuffer,DataSize);
                    send(new_socket , (char *)sendBuffer, DataSize,0);
                }
            }
            else{
                input.read(sendBuffer,DataSize);
                send(new_socket , (char *)sendBuffer, DataSize,0);
            }
            memset(&sendBuffer, '\0',BUFFER_SIZE);
        }
        printf("> Data sent!\n");
        close(new_socket);
        exit(0);
    }
    else{
        cout<<"[+]server busy...\n";
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x11d8, %rsp           # imm = 0x11D8
movl %edi, %ebx
callq 0x4100
testl %eax, %eax
je 0xb8c9
movq 0x6729(%rip), %rdi      # 0x11fd0
leaq 0x1e38(%rip), %rsi      # 0xd6e6
movl $0x12, %edx
addq $0x11d8, %rsp           # imm = 0x11D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x43c0
leaq 0xa00(%rsp), %r14
movl $0x7d0, %edx            # imm = 0x7D0
movq %r14, %rdi
xorl %esi, %esi
callq 0x4200
leaq 0x230(%rsp), %rdi
movl $0x7d0, %edx            # imm = 0x7D0
xorl %esi, %esi
callq 0x4200
leaq 0x10(%rsp), %r12
movl $0x0, (%r12)
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
movl %ebx, %edi
callq 0x44a0
leaq 0x14(%rsp), %r15
movl $0x4, %edx
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x41c0
movl $0x4, %edx
movl %ebx, %edi
movq %r12, %rsi
callq 0x44a0
movl $0x4, %edx
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x41c0
leaq 0x24(%rsp), %r12
movl $0x4, %edx
movl %ebx, %edi
movq %r12, %rsi
callq 0x44a0
movl $0x4, %edx
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x41c0
movslq (%r12), %rdx
decq %rdx
movl %ebx, %edi
movq %r14, %rsi
callq 0x44a0
movl $0x4, %edx
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x41c0
leaq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x43b0
movq %r15, %rdi
movq %r14, %rsi
movl $0x4, %edx
callq 0x4230
leaq 0xa00(%rsp), %rdi
movl $0x7d0, %edx            # imm = 0x7D0
xorl %esi, %esi
callq 0x4200
leaq 0x1d6a(%rip), %rdi      # 0xd731
callq 0x44f0
leaq 0x14(%rsp), %rsi
movl $0x4, %edx
movl %ebx, %edi
callq 0x44a0
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
movl 0xc(%rsp), %eax
movq %rax, 0x18(%rsp)
testl %eax, %eax
jle 0xba9d
movq 0x18(%rsp), %rax
decl %eax
movl %eax, 0x8(%rsp)
movslq 0x10(%rsp), %r14
leaq 0x28(%rsp), %r12
leaq 0x230(%rsp), %r13
movl $0x4, %edx
movl %ebx, %edi
leaq 0x4(%rsp), %rsi
callq 0x44a0
movslq 0x4(%rsp), %r15
movq %r15, %rsi
shlq $0xa, %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x4430
testl %r14d, %r14d
sete %al
cmpl 0x18(%rsp), %r15d
setne %cl
orb %al, %cl
je 0xba63
movl $0x400, %ebp            # imm = 0x400
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
movq %r13, %rsi
callq 0x4420
jmp 0xba74
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x4420
movq %r14, %rbp
movl %ebx, %edi
movq %r13, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
callq 0x41c0
movl $0x7d0, %edx            # imm = 0x7D0
movq %r13, %rdi
xorl %esi, %esi
callq 0x4200
cmpl 0x8(%rsp), %r15d
jl 0xba13
leaq 0x1c9f(%rip), %rdi      # 0xd743
callq 0x44f0
movl %ebx, %edi
callq 0x4670
xorl %edi, %edi
callq 0x4400
jmp 0xbabb
jmp 0xbabb
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x40f0
movq %rbx, %rdi
callq 0x45d0