// File: 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
subq $0xf0, %rsp
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
movl -0x4(%rbp), %eax
movl -0x8(%rbp), %ecx
cltd
idivl %ecx
incl %eax
movl %eax, -0x14(%rbp)
movl -0x8(%rbp), %eax
movl %eax, %edx
movq %rsp, %rax
movq %rax, -0x20(%rbp)
leaq 0xf(,%rdx,4), %rax
andq $-0x10, %rax
movq %rsp, %rdi
subq %rax, %rdi
movq %rdi, -0xc0(%rbp)
movq %rdi, %rsp
movq %rdx, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x4260
leaq 0x14ce0(%rip), %rdi     # 0x21a08
callq 0xe2f0
movq %rax, -0x38(%rbp)
leaq 0x14cd0(%rip), %rdi     # 0x21a08
callq 0xe350
movq %rax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0xe320
testb $0x1, %al
jne 0xcd57
jmp 0xceb1
leaq -0x38(%rbp), %rdi
callq 0xe380
movq %rax, %rsi
addq $0x4, %rsi
leaq 0x14c6a(%rip), %rdi     # 0x219d8
callq 0xe210
movq %rax, %rsi
leaq -0x60(%rbp), %rdi
callq 0x4200
movl $0x0, -0x64(%rbp)
movl $0x0, -0x68(%rbp)
movl -0x64(%rbp), %ecx
xorl %eax, %eax
cmpl -0x14(%rbp), %ecx
movb %al, -0xc1(%rbp)
jge 0xcdc4
leaq -0x38(%rbp), %rdi
callq 0xe380
movl (%rax), %ecx
xorl %eax, %eax
cmpl -0x64(%rbp), %ecx
movb %al, -0xc1(%rbp)
jle 0xcdc4
movl -0x68(%rbp), %eax
cmpl -0x4(%rbp), %eax
setl %al
movb %al, -0xc1(%rbp)
movb -0xc1(%rbp), %al
testb $0x1, %al
jne 0xcdd3
jmp 0xce53
movq -0x10(%rbp), %rax
movslq -0x68(%rbp), %rcx
cmpl $-0x1, (%rax,%rcx,4)
jne 0xce45
movslq -0x68(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x47d0
movq %rax, -0xd0(%rbp)
jmp 0xcdf7
movq -0xd0(%rbp), %rax
movsbl (%rax), %eax
subl $0x30, %eax
cmpl $0x0, %eax
je 0xce45
leaq -0x38(%rbp), %rdi
callq 0xe380
movl 0x4(%rax), %edx
movq -0x10(%rbp), %rax
movslq -0x68(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0xce45
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x47e8
jmp 0xd0e1
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0xcd8d
movl -0x68(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0xce94
leaq -0x38(%rbp), %rdi
callq 0xe380
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jl 0xce94
movl -0x68(%rbp), %eax
movl %eax, -0xd4(%rbp)
leaq -0x38(%rbp), %rdi
callq 0xe380
movl -0xd4(%rbp), %edx
movq %rax, %rcx
movq -0xc0(%rbp), %rax
movslq 0x4(%rcx), %rcx
movl %edx, (%rax,%rcx,4)
leaq -0x60(%rbp), %rdi
callq 0x47e8
leaq -0x38(%rbp), %rdi
xorl %esi, %esi
callq 0xe3a0
movq %rax, -0x80(%rbp)
jmp 0xcd31
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0xcef3
movq -0x10(%rbp), %rax
movslq -0x84(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jl 0xcee0
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xcee2
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0xcebb
jmp 0xcef5
jmp 0xcef7
movl -0x2c(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0xd05d
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0xd05b
movq -0xc0(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x88(%rbp)
cmpl $0x0, %eax
jle 0xd045
leaq 0x14a95(%rip), %rdi     # 0x219d8
leaq -0x8c(%rbp), %rsi
callq 0xe210
movq %rax, %rsi
leaq -0xb0(%rbp), %rdi
callq 0x4200
movslq -0x88(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x47d0
movq %rax, -0xe0(%rbp)
jmp 0xcf7a
movq -0xe0(%rbp), %rax
movsbl (%rax), %ecx
subl $0x30, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0xe1(%rbp)
jne 0xcfa6
movl -0x88(%rbp), %eax
cmpl -0x4(%rbp), %eax
setl %al
movb %al, -0xe1(%rbp)
movb -0xe1(%rbp), %al
testb $0x1, %al
jne 0xcfb2
jmp 0xcfe0
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0xcf5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x47e8
jmp 0xd0e1
movl -0x88(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0xd039
movq -0x10(%rbp), %rax
movslq -0x88(%rbp), %rcx
cmpl $-0x1, (%rax,%rcx,4)
jne 0xd039
movq -0xc0(%rbp), %rax
movl -0x8c(%rbp), %esi
movq -0x10(%rbp), %rcx
movslq -0x88(%rbp), %rdx
movl %esi, (%rcx,%rdx,4)
movl -0x2c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x2c(%rbp)
movl -0x88(%rbp), %edx
addl $0x1, %edx
movl %edx, -0x88(%rbp)
movslq -0x8c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
leaq -0xb0(%rbp), %rdi
callq 0x47e8
jmp 0xd047
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xcf0d
jmp 0xd05f
jmp 0xd064
jmp 0xcef5
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0xd0b3
movq -0x10(%rbp), %rax
movslq -0xb4(%rbp), %rcx
movl (%rax,%rcx,4), %esi
movq 0x13f3a(%rip), %rdi     # 0x20fc8
callq 0x46e0
movq %rax, %rdi
leaq 0x901a(%rip), %rsi      # 0x160b7
callq 0x43f0
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0xd06e
movq 0x13f0e(%rip), %rdi     # 0x20fc8
leaq 0x937d(%rip), %rsi      # 0x1643e
callq 0x43f0
movq 0x13ed3(%rip), %rax     # 0x20fa0
movq (%rax), %rdi
callq 0x44c0
movq -0x20(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x4700
nopw (%rax,%rax)