// File: 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
movq %rsp, %rbp
subq $0x1230, %rsp           # imm = 0x1230
movl %edi, -0x4(%rbp)
callq 0x4160
cmpl $0x0, %eax
jne 0x1428e
leaq -0x7e0(%rbp), %rdi
movq %rdi, -0x11f8(%rbp)
xorl %esi, %esi
movl %esi, -0x1204(%rbp)
movl $0x7d0, %edx            # imm = 0x7D0
movq %rdx, -0x1220(%rbp)
callq 0x4260
movq -0x1220(%rbp), %rdx
movl -0x1204(%rbp), %esi
leaq -0xfb0(%rbp), %rdi
callq 0x4260
movl $0x0, -0xfbc(%rbp)
movl -0x4(%rbp), %edi
leaq -0xfc0(%rbp), %rsi
movl $0x4, %edx
movq %rdx, -0x1210(%rbp)
callq 0x45c0
movq -0x1210(%rbp), %rdx
movl -0x1204(%rbp), %ecx
movl -0x4(%rbp), %edi
leaq -0xfb4(%rbp), %rsi
movq %rsi, -0x1218(%rbp)
callq 0x4230
movq -0x1210(%rbp), %rdx
movl -0x4(%rbp), %edi
leaq -0xfbc(%rbp), %rsi
callq 0x45c0
movq -0x1218(%rbp), %rsi
movq -0x1210(%rbp), %rdx
movl -0x1204(%rbp), %ecx
movl -0x4(%rbp), %edi
callq 0x4230
movq -0x1210(%rbp), %rdx
movl -0x4(%rbp), %edi
leaq -0xfb8(%rbp), %rsi
callq 0x45c0
movq -0x1218(%rbp), %rsi
movq -0x1210(%rbp), %rdx
movl -0x1204(%rbp), %ecx
movl -0x4(%rbp), %edi
callq 0x4230
movq -0x11f8(%rbp), %rsi
movl -0x4(%rbp), %edi
movl -0xfb8(%rbp), %eax
decl %eax
movslq %eax, %rdx
callq 0x45c0
movq -0x1218(%rbp), %rsi
movq -0x1210(%rbp), %rdx
movl -0x1204(%rbp), %ecx
movl -0x4(%rbp), %edi
callq 0x4230
leaq -0x11c8(%rbp), %rdi
movq %rdi, -0x1200(%rbp)
callq 0x44a0
movq -0x1200(%rbp), %rdi
movq -0x11f8(%rbp), %rsi
movl $0x4, %edx
callq 0x42c0
jmp 0x140bb
leaq -0x7e0(%rbp), %rdi
xorl %esi, %esi
movl %esi, -0x1224(%rbp)
movl $0x7d0, %edx            # imm = 0x7D0
callq 0x4260
movl -0x1224(%rbp), %eax
leaq 0x263c(%rip), %rdi      # 0x1671d
callq 0x4070
jmp 0x140e8
movl -0x4(%rbp), %edi
leaq -0xfb4(%rbp), %rsi
movl $0x4, %edx
callq 0x45c0
jmp 0x140fe
movl $0xffffffff, -0x11dc(%rbp) # imm = 0xFFFFFFFF
movl -0x11dc(%rbp), %eax
movl -0xfc0(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x1426d
movl -0x4(%rbp), %edi
leaq -0x11dc(%rbp), %rsi
movl $0x4, %edx
callq 0x45c0
jmp 0x14135
movl -0x11dc(%rbp), %eax
shll $0xa, %eax
movl %eax, -0x11d8(%rbp)
movslq -0x11d8(%rbp), %rsi
leaq -0x11f0(%rbp), %rdi
callq 0xe090
jmp 0x14159
movq -0x11f0(%rbp), %rsi
movq -0x11e8(%rbp), %rdx
leaq -0x11c8(%rbp), %rdi
callq 0x4530
jmp 0x14175
movl -0x11dc(%rbp), %eax
cmpl -0xfc0(%rbp), %eax
jne 0x14221
cmpl $0x0, -0xfbc(%rbp)
je 0x141eb
movslq -0xfbc(%rbp), %rdx
leaq -0x11c8(%rbp), %rdi
leaq -0xfb0(%rbp), %rsi
callq 0x4520
jmp 0x141ac
movl -0x4(%rbp), %edi
movslq -0xfbc(%rbp), %rdx
leaq -0xfb0(%rbp), %rsi
xorl %ecx, %ecx
callq 0x4230
jmp 0x141c6
jmp 0x1421f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x11d0(%rbp)
movl %eax, -0x11d4(%rbp)
leaq -0x11c8(%rbp), %rdi
callq 0x4150
jmp 0x142aa
leaq -0x11c8(%rbp), %rdi
leaq -0xfb0(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x4520
jmp 0x14205
movl -0x4(%rbp), %edi
leaq -0xfb0(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
xorl %ecx, %ecx
callq 0x4230
jmp 0x1421d
jmp 0x1421f
jmp 0x14255
leaq -0x11c8(%rbp), %rdi
leaq -0xfb0(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x4520
jmp 0x1423b
movl -0x4(%rbp), %edi
leaq -0xfb0(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
xorl %ecx, %ecx
callq 0x4230
jmp 0x14253
jmp 0x14255
leaq -0xfb0(%rbp), %rdi
xorl %esi, %esi
movl $0x7d0, %edx            # imm = 0x7D0
callq 0x4260
jmp 0x14108
leaq 0x24bc(%rip), %rdi      # 0x16730
xorl %eax, %eax
callq 0x4070
jmp 0x1427d
movl -0x4(%rbp), %edi
callq 0x47b0
jmp 0x14287
xorl %edi, %edi
callq 0x44e0
movq 0xcd33(%rip), %rdi      # 0x20fc8
leaq 0x246e(%rip), %rsi      # 0x1670a
callq 0x43f0
addq $0x1230, %rsp           # imm = 0x1230
popq %rbp
retq
movq -0x11d0(%rbp), %rdi
callq 0x4700
nopw %cs:(%rax,%rax)