// File: waltersharpWEI[P]CommentEliminator/main.cpp
// Function: balanceThreadsGreedy(int)
void balanceThreadsGreedy(int NumThread) {
    vector<string> Files[NumThread];
    std::vector<pthread_t> ThreadPointers(NumThread);

    //sort the Source File to descent order on size
    sort(FileList.begin(),FileList.end(),greater<FileRecord>());
    //displayFileList();

    //expected load for each thread
    double Average = sum(FileList) / (double)NumThread;

    //label of whether the File[i] is dispatched already
    bool used[FileList.size()];
    for (int I = 0;I < FileList.size();I++) {
        used[I] = false;
    }

    for (int I = 0;I < NumThread; I++) {
        Files[I].clear();
    }

    //gready distribute the task
    for (int I = 0; I < NumThread; I++) {
        int Capacity = Average;
        for (int J = 0;J < FileList.size();J++) {
            if (!used[J] && FileList[J].size <= Capacity) {
                Files[I].push_back(FileList[J].path);
                Capacity -= FileList[J].size;
                used[J] = true;
                break;
            }
        }
    }

    //Those task can not be handled are assigned randomly
    for (int I = 0; I < FileList.size();I++) {
        if (!used[I]) {
            int TargetThread = rand() % NumThread;
            Files[TargetThread].push_back(FileList[I].path);
            used[I] = true;
        }
    }

    //check whether all task all assigned
    if (completeDistribute(used,FileList.size())) {
        cout << "All tasks are assigned." << endl;
    } else {
        cout << "There are some mistake during the task assignment." << endl;
    }

    for (int I = 0;I < NumThread;I++) {
        pthread_create(&ThreadPointers[I],NULL,threadEliminator,&(Files[I]));
    }
    for (int I = 0; I < NumThread; I++) {
        pthread_join(ThreadPointers[I], NULL);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movl %edi, -0x4(%rbp)
movl -0x4(%rbp), %eax
movq %rax, -0xc0(%rbp)
movq %rsp, %rcx
movq %rcx, -0x10(%rbp)
leaq (%rax,%rax,2), %rcx
leaq 0xf(,%rcx,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xb8(%rbp)
movq %rcx, %rsp
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
je 0x3700
movq -0xb8(%rbp), %rax
movq -0xc0(%rbp), %rcx
imulq $0x18, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0xd0(%rbp)
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x4410
movq -0xd8(%rbp), %rax
movq -0xd0(%rbp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, -0xc8(%rbp)
jne 0x36cf
movslq -0x4(%rbp), %rax
movq %rax, -0xe8(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x4430
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0x4450
jmp 0x3734
leaq -0x31(%rbp), %rdi
callq 0x44c0
leaq 0xaafc(%rip), %rdi      # 0xe240
movq %rdi, -0xf0(%rbp)
callq 0x41f0
movq -0xf0(%rbp), %rdi
movq %rax, -0x50(%rbp)
callq 0x4260
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x46a0
jmp 0x3773
leaq 0xaac6(%rip), %rsi      # 0xe240
leaq -0x80(%rbp), %rdi
callq 0x46e0
jmp 0x3785
leaq -0x80(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x3590
movq -0x108(%rbp), %rdi
cvtsi2sd %eax, %xmm0
cvtsi2sdl -0x4(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x100(%rbp)
callq 0x3f20
movsd -0x100(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
leaq 0xaa76(%rip), %rdi      # 0xe240
callq 0x4500
movq %rax, %rdx
movq %rdx, %rax
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xf8(%rbp)
movq %rcx, %rsp
movq %rax, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movslq -0x8c(%rbp), %rax
movq %rax, -0x110(%rbp)
leaq 0xaa2d(%rip), %rdi      # 0xe240
callq 0x4500
movq %rax, %rcx
movq -0x110(%rbp), %rax
cmpq %rcx, %rax
jae 0x387e
movq -0xf8(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x37fe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x31(%rbp), %rdi
callq 0x44c0
jmp 0x3c50
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x45e0
jmp 0x3c50
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x38be
movq -0xb8(%rbp), %rdi
movslq -0x90(%rbp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x44e0
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x3888
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x39f7
cvttsd2si -0x68(%rbp), %eax
movl %eax, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
movslq -0x9c(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq 0xa93f(%rip), %rdi      # 0xe240
callq 0x4500
movq %rax, %rcx
movq -0x118(%rbp), %rax
cmpq %rcx, %rax
jae 0x39e1
movq -0xf8(%rbp), %rax
movslq -0x9c(%rbp), %rcx
testb $0x1, (%rax,%rcx)
jne 0x39cb
movslq -0x9c(%rbp), %rsi
leaq 0xa901(%rip), %rdi      # 0xe240
callq 0x45a0
movl 0x20(%rax), %eax
cmpl -0x98(%rbp), %eax
jg 0x39cb
movq -0xb8(%rbp), %rax
movslq -0x94(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, -0x120(%rbp)
movslq -0x9c(%rbp), %rsi
leaq 0xa8c6(%rip), %rdi      # 0xe240
callq 0x45a0
movq -0x120(%rbp), %rdi
movq %rax, %rsi
callq 0x4530
jmp 0x3990
movslq -0x9c(%rbp), %rsi
leaq 0xa8a2(%rip), %rdi      # 0xe240
callq 0x45a0
movq %rax, %rcx
movq -0xf8(%rbp), %rax
movl 0x20(%rcx), %edx
movl -0x98(%rbp), %ecx
subl %edx, %ecx
movl %ecx, -0x98(%rbp)
movslq -0x9c(%rbp), %rcx
movb $0x1, (%rax,%rcx)
jmp 0x39e1
jmp 0x39cd
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x38ec
jmp 0x39e3
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x38c8
movl $0x0, -0xa0(%rbp)
movslq -0xa0(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq 0xa82a(%rip), %rdi      # 0xe240
callq 0x4500
movq %rax, %rcx
movq -0x128(%rbp), %rax
cmpq %rcx, %rax
jae 0x3abc
movq -0xf8(%rbp), %rax
movslq -0xa0(%rbp), %rcx
testb $0x1, (%rax,%rcx)
jne 0x3aa6
callq 0x2070
movl -0x4(%rbp), %ecx
cltd
idivl %ecx
movq -0xb8(%rbp), %rax
movl %edx, -0xa4(%rbp)
movslq -0xa4(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, -0x130(%rbp)
movslq -0xa0(%rbp), %rsi
leaq 0xa7c2(%rip), %rdi      # 0xe240
callq 0x45a0
movq -0x130(%rbp), %rdi
movq %rax, %rsi
callq 0x4530
jmp 0x3a94
movq -0xf8(%rbp), %rax
movslq -0xa0(%rbp), %rcx
movb $0x1, (%rax,%rcx)
jmp 0x3aa8
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x3a01
leaq 0xa77d(%rip), %rdi      # 0xe240
callq 0x4500
movq -0xf8(%rbp), %rdi
movl %eax, %esi
callq 0x3600
testb $0x1, %al
jne 0x3adc
jmp 0x3b0f
movq 0xa4e5(%rip), %rdi      # 0xdfc8
leaq 0x55e3(%rip), %rsi      # 0x90cd
callq 0x2200
movq %rax, -0x138(%rbp)
jmp 0x3af8
movq -0x138(%rbp), %rdi
movq 0xa4aa(%rip), %rsi      # 0xdfb0
callq 0x2240
jmp 0x3b0d
jmp 0x3b42
movq 0xa4b2(%rip), %rdi      # 0xdfc8
leaq 0x55c8(%rip), %rsi      # 0x90e5
callq 0x2200
movq %rax, -0x140(%rbp)
jmp 0x3b2b
movq -0x140(%rbp), %rdi
movq 0xa477(%rip), %rsi      # 0xdfb0
callq 0x2240
jmp 0x3b40
jmp 0x3b42
movl $0x0, -0xa8(%rbp)
movl -0xa8(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x3ba0
movslq -0xa8(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x45c0
movq -0xb8(%rbp), %rcx
movq %rax, %rdi
movslq -0xa8(%rbp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rcx
xorl %eax, %eax
movl %eax, %esi
leaq -0xa4a(%rip), %rdx      # 0x3140
callq 0x23f0
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x3b4c
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x3be6
movslq -0xac(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x45c0
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x21c0
jmp 0x3bd3
jmp 0x3bd5
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x3baa
leaq -0x30(%rbp), %rdi
callq 0x45e0
movq -0xc0(%rbp), %rax
movq -0xb8(%rbp), %rcx
imulq $0x18, %rax, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, -0x148(%rbp)
je 0x3c44
movq -0x148(%rbp), %rdi
addq $-0x18, %rdi
movq %rdi, -0x150(%rbp)
callq 0x4640
movq -0xb8(%rbp), %rcx
movq -0x150(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x148(%rbp)
jne 0x3c13
movq -0x10(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
movq -0xb8(%rbp), %rcx
movq -0xc0(%rbp), %rax
imulq $0x18, %rax, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, -0x158(%rbp)
je 0x3ca5
movq -0x158(%rbp), %rdi
addq $-0x18, %rdi
movq %rdi, -0x160(%rbp)
callq 0x4640
movq -0xb8(%rbp), %rcx
movq -0x160(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x158(%rbp)
jne 0x3c74
jmp 0x3ca7
movq -0x40(%rbp), %rdi
callq 0x23c0