// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movq %rdx, -0x40(%rbp)
movl %edi, %r14d
movl %edi, %eax
cltd
idivl %esi
movl %eax, %r13d
movl %esi, -0x44(%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 0x4200
movq 0x8e70(%rip), %r15      # 0x12970
leaq 0x8e59(%rip), %rax      # 0x12960
cmpq %rax, %r15
je 0x9bca
movslq %r14d, %rbx
leaq 0x24(%r15), %rsi
leaq 0x8e0a(%rip), %rdi      # 0x12928
movq %rsi, -0x38(%rbp)
callq 0xa71c
leaq -0x58(%rbp), %rcx
movq %rcx, -0x68(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x68(%rbp), %rdi
callq 0x74cc
xorl %eax, %eax
testl %r13d, %r13d
js 0x9b80
movq -0x68(%rbp), %rcx
xorl %edx, %edx
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %r8
cmpl %edx, 0x20(%r15)
jle 0x9b84
cmpq %rbx, %rax
jge 0x9b84
cmpl $-0x1, (%rdi,%rax,4)
jne 0x9b76
cmpb $0x30, (%rcx,%rax)
je 0x9b76
movl (%r8), %esi
movl %esi, (%rdi,%rax,4)
incl %edx
incq %rax
cmpl %r13d, %edx
jle 0x9b57
jmp 0x9b84
movq -0x38(%rbp), %r8
cmpl %r14d, %eax
jge 0x9b96
cmpl %r13d, 0x20(%r15)
jle 0x9b96
movslq (%r8), %rcx
movl %eax, (%r12,%rcx,4)
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x9baf
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x4350
movq %r15, %rdi
callq 0x4240
movq %rax, %r15
leaq 0x8d9f(%rip), %rax      # 0x12960
cmpq %rax, %r15
jne 0x9b13
movl %r14d, %edi
testl %r14d, %r14d
movq -0x40(%rbp), %rdx
movl $0x0, %esi
jle 0x9bf1
xorl %eax, %eax
xorl %esi, %esi
movl (%rdx,%rax,4), %ecx
notl %ecx
shrl $0x1f, %ecx
addl %ecx, %esi
incq %rax
cmpq %rax, %rdi
jne 0x9bdf
movq %rdi, -0x70(%rbp)
movl %esi, -0x38(%rbp)
cmpl %r14d, %esi
jge 0x9cb7
leaq -0x58(%rbp), %rbx
leaq -0x68(%rbp), %r15
movl $0x0, -0x2c(%rbp)
cmpl $0x0, -0x44(%rbp)
jle 0x9cad
xorl %eax, %eax
movslq %eax, %rcx
movslq (%r12,%rcx,4), %r13
testq %r13, %r13
jle 0x9c9f
leaq 0x8cf9(%rip), %rdi      # 0x12928
leaq -0x2c(%rbp), %rsi
callq 0xa71c
movq %rbx, -0x68(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x74cc
movq -0x68(%rbp), %rdi
movl %r13d, %ecx
leaq 0x1(%r13), %rax
cmpl %r14d, %r13d
jge 0x9c68
cmpb $0x30, (%rdi,%r13)
movq %rax, %r13
je 0x9c52
cmpl %r14d, %ecx
movq -0x40(%rbp), %rdx
jge 0x9c87
cmpl $-0x1, -0x4(%rdx,%rax,4)
jne 0x9c87
movslq -0x2c(%rbp), %rcx
movl %ecx, -0x4(%rdx,%rax,4)
incl -0x38(%rbp)
movl %eax, (%r12,%rcx,4)
cmpq %rbx, %rdi
je 0x9c9c
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x4350
movq -0x40(%rbp), %rdx
movl -0x2c(%rbp), %eax
incl %eax
movl %eax, -0x2c(%rbp)
cmpl -0x44(%rbp), %eax
jl 0x9c1c
cmpl %r14d, -0x38(%rbp)
jl 0x9c09
testl %r14d, %r14d
movq -0x70(%rbp), %r12
jle 0x9cf7
xorl %ebx, %ebx
movq 0x8307(%rip), %r14      # 0x11fd0
leaq 0x3403(%rip), %r15      # 0xd0d3
movl (%rdx,%rbx,4), %esi
movq %r14, %rdi
callq 0x45b0
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x43c0
movq -0x40(%rbp), %rdx
incq %rbx
cmpq %rbx, %r12
jne 0x9cd0
movq 0x82d2(%rip), %rdi      # 0x11fd0
leaq 0x36e1(%rip), %rsi      # 0xd3e6
movl $0x1, %edx
callq 0x43c0
movq 0x828a(%rip), %rax      # 0x11fa0
movq (%rax), %rdi
callq 0x43e0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq