// File: greenpau[P]mnpa/main.cpp
// Function: mgr_thread(MulticastSender*, int, MulticastReceiver*, int)
void mgr_thread(struct MulticastSender senders[], int cnts, struct MulticastReceiver receivers[], int cntr) {
    volatile bool finished = false;
    std::vector <std::thread> thrs;

    if (cnts > 0) {
        for( int i = 0; i < cnts; i++ ) {
            std::thread thr = std::thread(sender_thread, senders[i]);
            auto thrid = std::hash<std::thread::id>()(thr.get_id());
            thrs.push_back(std::move(thr));
            assert(!thr.joinable());
            mu_thrids.lock();
            thrids[thrid] = 2;
            //logit(INFO,"started sender thread #" + std::to_string(thrid) + " in stage #" + std::to_string(thrids[thrid]));
            mu_thrids.unlock();
        }
    }

    if (cntr > 0) {
        for( int i = 0; i < cntr; i++ ) {
            std::thread thr = std::thread(receiver_thread, receivers[i]);
            auto thrid = std::hash<std::thread::id>()(thr.get_id());
            thrs.push_back(std::move(thr));
            assert(!thr.joinable());
            mu_thrids.lock();
            thrids[thrid] = 2;
            //logit(INFO,"started sender thread #" + std::to_string(thrid) + " in stage #" + std::to_string(thrids[thrid]));
            mu_thrids.unlock();
        }
    }

    do {
        finished = true;
        std::for_each(thrs.begin(), thrs.end(), [&finished](std::thread& thr) {
            auto thrid = std::hash<std::thread::id>()(thr.get_id());
            mu_thrids.lock();
            unsigned int thr_status = thrids[thrid];
            mu_thrids.unlock();
            //logit(INFO, "thread #" + std::to_string(thrid) + " is in stage #" + std::to_string(thr_status));
            if (thr_status != 1) {
                finished = false;
            } else {
                if (thr.joinable() && thr_status == 1) {
                    thr.join();
                }
            }
        });
        sleep(1);
    } while (finished == false);

    if (DAEMONIZE == false) {
        return;
    } else {
        exit(0);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movb $0x0, -0x1d(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x246c0
cmpl $0x0, -0xc(%rbp)
jle 0x1e6e7
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x1e6e5
movq -0x8(%rbp), %rdx
movslq -0x3c(%rbp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x5, %rax
addq %rax, %rdx
leaq -0x1b5d(%rip), %rsi     # 0x1ca90
leaq -0x48(%rbp), %rdi
callq 0x246e0
jmp 0x1e5f8
leaq -0x48(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0x247a0
movq %rax, -0x70(%rbp)
leaq -0x61(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x24010
movq -0xc0(%rbp), %rsi
movq %rax, -0x60(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x247c0
jmp 0x1e62f
leaq -0x48(%rbp), %rdi
callq 0x247f0
xorb $-0x1, %al
testb $0x1, %al
jne 0x1e640
jmp 0x1e642
jmp 0x1e68c
leaq 0xe492c(%rip), %rdi     # 0x102f75
leaq 0xe4935(%rip), %rsi     # 0x102f85
movl $0x20e, %edx            # imm = 0x20E
leaq 0xe4932(%rip), %rcx     # 0x102f8e
callq 0x161c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x1e8c6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x24830
jmp 0x1e8c6
leaq 0x13e3ad(%rip), %rdi    # 0x15ca40
callq 0x24230
jmp 0x1e69a
leaq 0x13e3f7(%rip), %rdi    # 0x15ca98
leaq -0x60(%rbp), %rsi
callq 0x24260
movq %rax, -0xc8(%rbp)
jmp 0x1e6b3
movq -0xc8(%rbp), %rax
movl $0x2, (%rax)
leaq 0x13e379(%rip), %rdi    # 0x15ca40
callq 0x24340
jmp 0x1e6ce
leaq -0x48(%rbp), %rdi
callq 0x24830
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1e5c7
jmp 0x1e6e7
cmpl $0x0, -0x1c(%rbp)
jle 0x1e812
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1e810
movq -0x18(%rbp), %rdx
movslq -0x74(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdx
leaq -0x2f9a(%rip), %rsi     # 0x1b780
leaq -0x80(%rbp), %rdi
callq 0x24860
jmp 0x1e725
leaq -0x80(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0x247a0
movq %rax, -0x98(%rbp)
leaq -0x89(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x24010
movq -0xd0(%rbp), %rsi
movq %rax, -0x88(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x247c0
jmp 0x1e768
leaq -0x80(%rbp), %rdi
callq 0x247f0
xorb $-0x1, %al
testb $0x1, %al
jne 0x1e779
jmp 0x1e77b
jmp 0x1e7b4
leaq 0xe47f3(%rip), %rdi     # 0x102f75
leaq 0xe47fc(%rip), %rsi     # 0x102f85
movl $0x21b, %edx            # imm = 0x21B
leaq 0xe47f9(%rip), %rcx     # 0x102f8e
callq 0x161c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x24830
jmp 0x1e8c6
leaq 0x13e285(%rip), %rdi    # 0x15ca40
callq 0x24230
jmp 0x1e7c2
leaq 0x13e2cf(%rip), %rdi    # 0x15ca98
leaq -0x88(%rbp), %rsi
callq 0x24260
movq %rax, -0xd8(%rbp)
jmp 0x1e7de
movq -0xd8(%rbp), %rax
movl $0x2, (%rax)
leaq 0x13e24e(%rip), %rdi    # 0x15ca40
callq 0x24340
jmp 0x1e7f9
leaq -0x80(%rbp), %rdi
callq 0x24830
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x1e6f8
jmp 0x1e812
jmp 0x1e814
movb $0x1, -0x1d(%rbp)
leaq -0x38(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
callq 0x24920
movq -0xe8(%rbp), %rdi
movq %rax, -0xa0(%rbp)
callq 0x24950
movq %rax, -0xa8(%rbp)
leaq -0x1d(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xa0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
callq 0x1e8e0
movq %rax, -0xe0(%rbp)
jmp 0x1e870
movq -0xe0(%rbp), %rax
movq %rax, -0xb8(%rbp)
movl $0x1, %edi
callq 0x16460
jmp 0x1e88a
jmp 0x1e88c
movb -0x1d(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x1e814
movb 0x13e1ed(%rip), %al     # 0x15ca90
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x1e8bf
leaq -0x38(%rbp), %rdi
callq 0x24980
addq $0xf0, %rsp
popq %rbp
retq
xorl %edi, %edi
callq 0x161b0
leaq -0x38(%rbp), %rdi
callq 0x24980
movq -0x50(%rbp), %rdi
callq 0xfeff0
nopl (%rax,%rax)