// File: ninjabear[P]dr-doom/src/thread.cpp
// Function: thread_bomb()
void thread_bomb() { 
    std::condition_variable cv;
    std::mutex m;
    std::vector<std::thread> thread_handles;

    long thread_count = 0; 

    for (;;) {
        thread_count++;
        try { 
            thread_handles.push_back(std::thread(blocked_thread, std::ref(cv), std::ref(m)));
        } catch (const std::exception& e) {
            std::cout << "After creating " << thread_count << " new threads the following error occurred: " << std::endl << std::endl;
            std::cout << e.what() << std::endl << std::endl;
            break;
        } catch (...) {
            std::cout << "After creating " << thread_count << " new threads an unexpected (non std::exception) error occurred." << std::endl << std::endl;
            break;
        }
    }

    std::cout << "The threads are currently running. Would you like to clean up the threads before exiting? [Y/n]: ";

    char cont = 0;
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get(cont);

    if (cont != 'n' && cont != 'N') {
        std::cout << "Cleaning up threads..." << std::endl;

        cv.notify_all();

        for (long i=0; i<thread_count; i++) {
            try {
                thread_handles[i].join();
            } catch (...) { }
            cv.notify_all();
        }

        std::cout << thread_count << " threads closed. " << std::endl;
    } else {
        std::cout << "Understood, ending the process leaving the operating system to clean up " << thread_count << " detached threads." << std::endl;

        for (long i=0; i<thread_count; i++) {
            try {
                thread_handles[i].detach();
            } catch (...) { }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
leaq -0x30(%rbp), %rdi
callq 0x22e0
leaq -0x58(%rbp), %rdi
callq 0x64e0
leaq -0x70(%rbp), %rdi
callq 0x6500
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
incq %rax
movq %rax, -0x78(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x6550
movq %rax, -0x88(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x6580
movq %rax, -0x90(%rbp)
leaq 0x510(%rip), %rsi       # 0x63e0
leaq -0x80(%rbp), %rdi
leaq -0x88(%rbp), %rdx
leaq -0x90(%rbp), %rcx
callq 0x65b0
jmp 0x5ee9
leaq -0x70(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x6520
jmp 0x5ef8
leaq -0x80(%rbp), %rdi
callq 0x3870
jmp 0x612c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x5f35
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x3870
movl -0x9c(%rbp), %eax
movl $0x2, %ecx
cmpl %ecx, %eax
jne 0x6040
movq -0x98(%rbp), %rdi
callq 0x20c0
movq %rax, -0xa8(%rbp)
movq 0x706e(%rip), %rdi      # 0xcfd0
leaq 0x26de(%rip), %rsi      # 0x8647
callq 0x21f0
movq %rax, -0xc8(%rbp)
jmp 0x5f77
movq -0xc8(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x23d0
movq %rax, -0xd0(%rbp)
jmp 0x5f90
movq -0xd0(%rbp), %rdi
leaq 0x26f9(%rip), %rsi      # 0x8697
callq 0x21f0
movq %rax, -0xd8(%rbp)
jmp 0x5fac
movq -0xd8(%rbp), %rdi
movq 0x6ff6(%rip), %rsi      # 0xcfb0
callq 0x2230
movq %rax, -0xe0(%rbp)
jmp 0x5fc8
movq -0xe0(%rbp), %rdi
movq 0x6fda(%rip), %rsi      # 0xcfb0
callq 0x2230
jmp 0x5fdd
movq -0xa8(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq %rax, %rsi
movq 0x6fd9(%rip), %rdi      # 0xcfd0
callq 0x21f0
movq %rax, -0xe8(%rbp)
jmp 0x6005
movq -0xe8(%rbp), %rdi
movq 0x6f9d(%rip), %rsi      # 0xcfb0
callq 0x2230
movq %rax, -0xf0(%rbp)
jmp 0x6021
movq -0xf0(%rbp), %rdi
movq 0x6f81(%rip), %rsi      # 0xcfb0
callq 0x2230
jmp 0x6036
callq 0x2330
jmp 0x6125
movq -0x98(%rbp), %rdi
callq 0x20c0
movq 0x6f7d(%rip), %rdi      # 0xcfd0
leaq 0x25ed(%rip), %rsi      # 0x8647
callq 0x21f0
movq %rax, -0xf8(%rbp)
jmp 0x6068
movq -0xf8(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x23d0
movq %rax, -0x100(%rbp)
jmp 0x6081
movq -0x100(%rbp), %rdi
leaq 0x25c8(%rip), %rsi      # 0x8657
callq 0x21f0
movq %rax, -0x108(%rbp)
jmp 0x609d
movq -0x108(%rbp), %rdi
movq 0x6f05(%rip), %rsi      # 0xcfb0
callq 0x2230
movq %rax, -0x110(%rbp)
jmp 0x60b9
movq -0x110(%rbp), %rdi
movq 0x6ee9(%rip), %rsi      # 0xcfb0
callq 0x2230
jmp 0x60ce
callq 0x2330
jmp 0x60ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
callq 0x2330
jmp 0x6107
jmp 0x6131
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x63ac
jmp 0x63ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
callq 0x2330
jmp 0x6127
jmp 0x6131
jmp 0x63ac
jmp 0x5e9e
movq 0x6e98(%rip), %rdi      # 0xcfd0
leaq 0x2584(%rip), %rsi      # 0x86c3
callq 0x21f0
jmp 0x6146
movb $0x0, -0xa9(%rbp)
movq 0x6e8c(%rip), %rdi      # 0xcfe0
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
xorl %esi, %esi
callq 0x2340
jmp 0x6167
callq 0x4420
movq %rax, %rsi
movq 0x6e6a(%rip), %rdi      # 0xcfe0
movl $0xa, %edx
callq 0x2120
jmp 0x6182
movq 0x6e57(%rip), %rdi      # 0xcfe0
leaq -0xa9(%rbp), %rsi
callq 0x22d0
jmp 0x6197
movsbl -0xa9(%rbp), %eax
cmpl $0x6e, %eax
je 0x62b8
movsbl -0xa9(%rbp), %eax
cmpl $0x4e, %eax
je 0x62b8
movq 0x6e12(%rip), %rdi      # 0xcfd0
leaq 0x2560(%rip), %rsi      # 0x8725
callq 0x21f0
movq %rax, -0x118(%rbp)
jmp 0x61d3
movq -0x118(%rbp), %rdi
movq 0x6dcf(%rip), %rsi      # 0xcfb0
callq 0x2230
jmp 0x61e8
leaq -0x30(%rbp), %rdi
callq 0x2350
movq $0x0, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
cmpq -0x78(%rbp), %rax
jge 0x6269
movq -0xb8(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x6680
movq %rax, %rdi
callq 0x2360
jmp 0x6223
jmp 0x624c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
movq -0x98(%rbp), %rdi
callq 0x20c0
callq 0x2330
jmp 0x624a
jmp 0x624c
leaq -0x30(%rbp), %rdi
callq 0x2350
movq -0xb8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb8(%rbp)
jmp 0x61fc
movq -0x78(%rbp), %rsi
movq 0x6d5c(%rip), %rdi      # 0xcfd0
callq 0x23d0
movq %rax, -0x120(%rbp)
jmp 0x6282
movq -0x120(%rbp), %rdi
leaq 0x24ac(%rip), %rsi      # 0x873c
callq 0x21f0
movq %rax, -0x128(%rbp)
jmp 0x629e
movq -0x128(%rbp), %rdi
movq 0x6d04(%rip), %rsi      # 0xcfb0
callq 0x2230
jmp 0x62b3
jmp 0x6391
movq 0x6d11(%rip), %rdi      # 0xcfd0
leaq 0x2488(%rip), %rsi      # 0x874e
callq 0x21f0
movq %rax, -0x130(%rbp)
jmp 0x62d4
movq -0x130(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x23d0
movq %rax, -0x138(%rbp)
jmp 0x62ed
movq -0x138(%rbp), %rdi
leaq 0x249c(%rip), %rsi      # 0x8797
callq 0x21f0
movq %rax, -0x140(%rbp)
jmp 0x6309
movq -0x140(%rbp), %rdi
movq 0x6c99(%rip), %rsi      # 0xcfb0
callq 0x2230
jmp 0x631e
movq $0x0, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
cmpq -0x78(%rbp), %rax
jge 0x638f
movq -0xc0(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x6680
movq %rax, %rdi
callq 0x2380
jmp 0x6350
jmp 0x6379
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
movq -0x98(%rbp), %rdi
callq 0x20c0
callq 0x2330
jmp 0x6377
jmp 0x6379
jmp 0x637b
movq -0xc0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc0(%rbp)
jmp 0x6329
jmp 0x6391
leaq -0x70(%rbp), %rdi
callq 0x66a0
leaq -0x30(%rbp), %rdi
callq 0x23c0
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
leaq -0x70(%rbp), %rdi
callq 0x66a0
leaq -0x30(%rbp), %rdi
callq 0x23c0
movq -0x98(%rbp), %rdi
callq 0x2390
movq %rax, %rdi
callq 0x3540
nopw %cs:(%rax,%rax)