// File: /workspace/github_cpp_makefile_star2_O2/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 0x4130
testl %eax, %eax
je 0x9c13
movq 0x63d2(%rip), %rdi      # 0xffc8
leaq 0x1ae9(%rip), %rsi      # 0xb6e6
addq $0x11d8, %rsp           # imm = 0x11D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4330
leaq 0xa00(%rsp), %r14
movl $0x7d0, %edx            # imm = 0x7D0
movq %r14, %rdi
xorl %esi, %esi
callq 0x4210
leaq 0x230(%rsp), %rdi
movl $0x7d0, %edx            # imm = 0x7D0
xorl %esi, %esi
callq 0x4210
leaq 0x10(%rsp), %r13
andl $0x0, (%r13)
leaq 0xc(%rsp), %rsi
pushq $0x4
popq %r15
movl %ebx, %edi
movq %r15, %rdx
callq 0x4490
leaq 0x14(%rsp), %r12
movl %ebx, %edi
movq %r12, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x41d0
movl %ebx, %edi
movq %r13, %rsi
movq %r15, %rdx
callq 0x4490
movl %ebx, %edi
movq %r12, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x41d0
leaq 0x24(%rsp), %r13
movl %ebx, %edi
movq %r13, %rsi
movq %r15, %rdx
callq 0x4490
movl %ebx, %edi
movq %r12, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x41d0
movslq (%r13), %rdx
decq %rdx
movl %ebx, %edi
movq %r14, %rsi
callq 0x4490
movl %ebx, %edi
movq %r12, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x41d0
leaq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x43a0
pushq $0x4
popq %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x4240
leaq 0xa00(%rsp), %rdi
movl $0x7d0, %edx            # imm = 0x7D0
xorl %esi, %esi
callq 0x4210
leaq 0x1a2f(%rip), %rdi      # 0xb731
callq 0x44e0
leaq 0x14(%rsp), %rsi
pushq $0x4
popq %rdx
movl %ebx, %edi
callq 0x4490
leaq 0x4(%rsp), %rax
orl $-0x1, (%rax)
movl 0xc(%rsp), %eax
movq %rax, 0x18(%rsp)
decl %eax
movl %eax, 0x8(%rsp)
movslq 0x10(%rsp), %r14
pushq $-0x1
popq %r12
leaq 0x28(%rsp), %r13
leaq 0x230(%rsp), %rbp
cmpl 0x8(%rsp), %r12d
jge 0x9dd1
movl %ebx, %edi
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x4490
movslq 0x4(%rsp), %r12
movq %r12, %rsi
shlq $0xa, %rsi
movq %r13, %rdi
xorl %edx, %edx
callq 0x4410
testl %r14d, %r14d
sete %al
cmpl 0x18(%rsp), %r12d
setne %cl
orb %al, %cl
je 0x9d9d
movl $0x400, %r15d           # imm = 0x400
movl $0x400, %edx            # imm = 0x400
movq %r13, %rdi
movq %rbp, %rsi
callq 0x4400
jmp 0x9dae
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x4400
movq %r14, %r15
movl %ebx, %edi
movq %rbp, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x41d0
movl $0x7d0, %edx            # imm = 0x7D0
movq %rbp, %rdi
xorl %esi, %esi
callq 0x4210
jmp 0x9d43
leaq 0x196b(%rip), %rdi      # 0xb743
callq 0x44e0
movl %ebx, %edi
callq 0x4660
xorl %edi, %edi
callq 0x43e0
jmp 0x9def
jmp 0x9def
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x4120
movq %rbx, %rdi
callq 0x45b0