// File: /workspace/github_cpp_makefile_star2_O2/girdhari9[P]mTORRENT/ClientConnection.cpp
// Function: PieceSelection(int, int, int*)
void PieceSelection(int TotalPacket,int TotalConn, int BitMap[]){
    int MinPackFromOneServer = (TotalPacket/TotalConn)+1;
    int PackIndex[TotalConn], TotalPackAssign = 0;

    memset(&PackIndex, 0, sizeof(PackIndex));
    for(auto it = TotalAvlPack.begin(); it != TotalAvlPack.end(); it++){
        string PackS = PacketNoAvl[it->second];
        int TotalSelectedPiece = 0, Loop = 0;

        while((TotalSelectedPiece < MinPackFromOneServer) && (it->first > TotalSelectedPiece) && (Loop < TotalPacket)){
            if(BitMap[Loop] == -1 && (PackS[Loop]-'0')){
                BitMap[Loop] = it->second;
                TotalSelectedPiece++;
            }
            Loop++;
        }
        if(Loop < TotalPacket && it->first >= MinPackFromOneServer)
            PackIndex[it->second] = Loop;
    }
    for(int i=0;i<TotalPacket;i++)
        if(BitMap[i] >= 0)
            TotalPackAssign++;
        
    int index;
    while(1) {
        if(TotalPackAssign < TotalPacket){
            for(int ConNo=0; ConNo < TotalConn; ConNo++){
                if((index = PackIndex[ConNo]) > 0){
                    string PackS = PacketNoAvl[ConNo];
                    while(!(PackS[index]-'0') && (index < TotalPacket)){
                        index++;
                    } 
                    if((index < TotalPacket) && (BitMap[index] == -1)){
                        BitMap[index] = ConNo;
                        TotalPackAssign++;
                        PackIndex[ConNo] = ++index;
                    }
                }
            }
        }
        else break;
    }
    for(int i=0;i<TotalPacket;i++)
        cout<<BitMap[i]<<" ";
    cout<<"\n"; fflush(stdout);
    return ;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, -0x38(%rbp)
movl %edi, %r15d
movl %edi, %eax
cltd
idivl %esi
movl %eax, %r13d
movl %esi, -0x4c(%rbp)
movl %esi, %edx
movq %rsp, %r12
leaq 0xf(,%rdx,4), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
shlq $0x2, %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x4210
movq 0x89a9(%rip), %rbx      # 0x10960
movslq %r15d, %r14
leaq 0x898f(%rip), %rax      # 0x10950
cmpq %rax, %rbx
je 0x8054
leaq 0x24(%rbx), %rsi
leaq 0x8943(%rip), %rdi      # 0x10918
movq %rsi, -0x48(%rbp)
callq 0x8a6e
leaq -0x78(%rbp), %rdi
movq %rax, %rsi
callq 0x4470
xorl %eax, %eax
movq -0x78(%rbp), %rcx
xorl %edx, %edx
cmpl %r13d, %edx
jg 0x8025
cmpl %edx, 0x20(%rbx)
jle 0x8025
cmpq %r14, %rax
jge 0x8025
movq -0x38(%rbp), %rsi
cmpl $-0x1, (%rsi,%rax,4)
jne 0x8020
cmpb $0x30, (%rcx,%rax)
je 0x8020
movq -0x48(%rbp), %rsi
movl (%rsi), %esi
movq -0x38(%rbp), %rdi
movl %esi, (%rdi,%rax,4)
incl %edx
incq %rax
jmp 0x7ff2
cmpl %r15d, %eax
jge 0x803b
cmpl %r13d, 0x20(%rbx)
jle 0x803b
movq -0x48(%rbp), %rcx
movslq (%rcx), %rcx
movl %eax, (%r12,%rcx,4)
leaq -0x78(%rbp), %rdi
callq 0x4678
movq %rbx, %rdi
callq 0x4250
movq %rax, %rbx
jmp 0x7fba
xorl %eax, %eax
testl %r15d, %r15d
movl $0x0, %edx
cmovgl %r15d, %edx
xorl %esi, %esi
cmpq %rax, %rdx
je 0x807c
movq -0x38(%rbp), %rcx
movl (%rcx,%rax,4), %ecx
notl %ecx
shrl $0x1f, %ecx
addl %ecx, %esi
incq %rax
jmp 0x8064
movl %esi, -0x3c(%rbp)
movq %rdx, -0x58(%rbp)
movq -0x38(%rbp), %rax
addq $-0x4, %rax
movq %rax, -0x48(%rbp)
leaq -0x2c(%rbp), %rbx
leaq -0x78(%rbp), %r13
cmpl %r15d, -0x3c(%rbp)
jge 0x8123
xorl %eax, %eax
movl %eax, -0x2c(%rbp)
cmpl -0x4c(%rbp), %eax
jge 0x8097
movslq %eax, %rcx
movslq (%r12,%rcx,4), %r14
testq %r14, %r14
jle 0x811f
leaq 0x885a(%rip), %rdi      # 0x10918
movq %rbx, %rsi
callq 0x8a6e
movq %r13, %rdi
movq %rax, %rsi
callq 0x4470
movq -0x78(%rbp), %rcx
addq %r14, %rcx
xorl %edx, %edx
leal (%r14,%rdx), %esi
leaq 0x1(%rdx), %rax
cmpl %r15d, %esi
jge 0x80f0
cmpb $0x30, (%rcx,%rdx)
movq %rax, %rdx
je 0x80da
cmpl %r15d, %esi
jge 0x8114
movq -0x48(%rbp), %rcx
leaq (%rcx,%r14,4), %rcx
cmpl $-0x1, (%rcx,%rax,4)
jne 0x8114
movslq -0x2c(%rbp), %rdx
movl %edx, (%rcx,%rax,4)
incl -0x3c(%rbp)
addl %eax, %r14d
movl %r14d, (%r12,%rdx,4)
movq %r13, %rdi
callq 0x4678
movl -0x2c(%rbp), %eax
incl %eax
jmp 0x80a3
xorl %r15d, %r15d
movq 0x7e9b(%rip), %rbx      # 0xffc8
leaq 0x2f9f(%rip), %r14      # 0xb0d3
movq -0x58(%rbp), %r12
cmpq %r15, %r12
je 0x815d
movq -0x38(%rbp), %rax
movl (%rax,%r15,4), %esi
movq %rbx, %rdi
callq 0x4590
movq %rax, %rdi
movq %r14, %rsi
callq 0x4330
incq %r15
jmp 0x8138
movq 0x7e64(%rip), %rdi      # 0xffc8
leaq 0x327b(%rip), %rsi      # 0xb3e6
callq 0x4330
movq 0x7e31(%rip), %rax      # 0xffa8
movq (%rax), %rdi
callq 0x43c0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq