// File: /workspace/github_cpp_makefile_star2_O3/wellenvogel[P]simple-cpp-threads/test.cpp
// Function: main
int main(int argc, char ** argv){
	cout << "hello world" << endl;
        int numThreads=5;
        if (argc > 1){
            numThreads=atoi(argv[1]);
        }
        vector<MyRunnable*> runnables;
        for (int i=0;i< numThreads;i++){
            MyRunnable *runnable=new MyRunnable(i);
            Thread *t=new Thread(runnable);
            t->start();
            runnables.push_back(runnable);
        }
        //"misuse" of a condition for ms waits
        Condition waiter;
        //waiting for start
        bool allRunning=false;
        cout << "waiting for threads to start" << endl;
        while (! allRunning){
            allRunning=true;
            vector<MyRunnable*>::iterator it=runnables.begin();
            while (it != runnables.end()){
                if ((*it)->getState() != RUNNING) {
                    cout << "##state "<<((*it)->getId())<<" is "<<((*it)->getState())<<endl;
                    allRunning=false;
                }
                it++;
            }
            waiter.wait(20); //avoid busy loop
        }
        cout << numThreads << " are running" <<endl;
        waiter.wait(10000);
        cout << "stopping" << endl;
        vector<MyRunnable*>::iterator it=runnables.begin();
        while (it != runnables.end()){
            (*it)->setState(STOP);
            it++;
        }
        bool allStopped=false;
        int maxWait=50*10; //max 10s
        while (! allStopped && maxWait){
            allStopped=true;
            vector<MyRunnable*>::iterator it=runnables.begin();
            while (it != runnables.end()){
                if ((*it)->getState() != STOP) allStopped=false;
                it++;
            }
            maxWait--;
            waiter.wait(20); //avoid busy loop
        }
        if (allStopped) cout << "all threads stopped" << endl;
        else cout << "unable to stop all threads" << endl;
        //if we would like to wait here for all threads
        //we also would need to keep the thread objects...
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %rbx
movl %edi, %r14d
movq 0x2c8e(%rip), %r15      # 0x4fd8
leaq 0xcb3(%rip), %rsi       # 0x3004
movl $0xb, %edx
movq %r15, %rdi
callq 0x2130
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
cmpl $0x1, %r14d
jle 0x23af
movq 0x8(%rbx), %rdi
callq 0x2180
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jg 0x23c5
jmp 0x2485
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x5, -0x2c(%rbp)
xorl %r13d, %r13d
xorl %r14d, %r14d
movl $0x48, %edi
callq 0x2120
movq %rax, %r15
leaq 0x2969(%rip), %rax      # 0x4d48
movq %rax, (%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r15)
movups %xmm0, 0x18(%r15)
movq %r13, 0x28(%r15)
movq %r13, 0x38(%r15)
movl %r14d, 0x40(%r15)
movl $0x40, %edi
callq 0x2120
movq %rax, %r12
movq %r15, %rbx
addq $0x8, %rbx
movq %rax, %rdi
addq $0x8, %rdi
callq 0x2150
movb $0x0, 0x38(%r12)
movq %rbx, (%r12)
movq %r12, 0x30(%r15)
movq %r15, -0xa0(%rbp)
movl $0x10, %edi
callq 0x2120
movq $0x0, 0x8(%rax)
movq %r15, (%rax)
movq %rax, %rdi
callq 0x27ee
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0x2468
movq -0xa0(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, -0x48(%rbp)
jmp 0x2478
leaq -0x50(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x2d2c
incl %r14d
cmpl %r14d, -0x2c(%rbp)
jne 0x23cb
leaq -0xa0(%rbp), %rdi
callq 0x2840
movq 0x2b40(%rip), %rbx      # 0x4fd8
leaq 0xb71(%rip), %rsi       # 0x3010
movl $0x1c, %edx
movq %rbx, %rdi
callq 0x2130
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq 0x2b0e(%rip), %rdi      # 0x4fd8
callq 0x2030
movq %rax, %rdi
callq 0x20b0
leaq -0x60(%rbp), %r15
movq 0x2af6(%rip), %r12      # 0x4fd8
movq -0x50(%rbp), %rbx
cmpq -0x48(%rbp), %rbx
je 0x25cc
movb $0x1, %r14b
movq (%rbx), %r13
leaq 0x8(%r13), %rax
movq %rax, -0x60(%rbp)
movb $0x0, -0x58(%rbp)
movq %r15, %rdi
callq 0x2b94
movb $0x1, -0x58(%rbp)
movl 0x3c(%r13), %r13d
movq %r15, %rdi
callq 0x2b7a
cmpl $0x1, %r13d
je 0x25bc
movl $0x8, %edx
movq %r12, %rdi
leaq 0xafa(%rip), %rsi       # 0x302d
callq 0x2130
movq (%rbx), %rax
movl 0x40(%rax), %esi
movq %r12, %rdi
callq 0x21b0
movq %rax, %r14
movl $0x4, %edx
movq %rax, %rdi
leaq 0xade(%rip), %rsi       # 0x3036
callq 0x2130
movq (%rbx), %r13
leaq 0x8(%r13), %rax
movq %rax, -0x60(%rbp)
movb $0x0, -0x58(%rbp)
movq %r15, %rdi
callq 0x2b94
movb $0x1, -0x58(%rbp)
movl 0x3c(%r13), %r13d
movq %r15, %rdi
callq 0x2b7a
movq %r14, %rdi
movl %r13d, %esi
callq 0x21b0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
xorl %r14d, %r14d
addq $0x8, %rbx
cmpq -0x48(%rbp), %rbx
jne 0x24f3
jmp 0x25cf
movb $0x1, %r14b
leaq -0xa0(%rbp), %rdi
movl $0x14, %esi
callq 0x2896
testb $0x1, %r14b
je 0x24e2
movq 0x29e7(%rip), %rdi      # 0x4fd8
movl -0x2c(%rbp), %esi
callq 0x21b0
movq %rax, %rbx
leaq 0xa38(%rip), %rsi       # 0x303b
movl $0xc, %edx
movq %rax, %rdi
callq 0x2130
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
leaq -0xa0(%rbp), %rdi
movl $0x2710, %esi           # imm = 0x2710
callq 0x2896
movq 0x2989(%rip), %rbx      # 0x4fd8
leaq 0x9f2(%rip), %rsi       # 0x3048
movl $0x8, %edx
movq %rbx, %rdi
callq 0x2130
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq 0x2957(%rip), %rdi      # 0x4fd8
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq -0x50(%rbp), %rbx
cmpq -0x48(%rbp), %rbx
je 0x26ab
movq (%rbx), %rdi
movl $0x2, %esi
callq 0x2914
addq $0x8, %rbx
jmp 0x2692
movl $0x1f4, %r15d           # imm = 0x1F4
leaq -0x60(%rbp), %r14
movq -0x50(%rbp), %r13
cmpq -0x48(%rbp), %r13
je 0x26fe
movb $0x1, %r12b
movq (%r13), %rbx
leaq 0x8(%rbx), %rax
movq %rax, -0x60(%rbp)
movb $0x0, -0x58(%rbp)
movq %r14, %rdi
callq 0x2b94
movb $0x1, -0x58(%rbp)
movl 0x3c(%rbx), %ebx
movq %r14, %rdi
callq 0x2b7a
cmpl $0x2, %ebx
sete %al
andb %al, %r12b
addq $0x8, %r13
cmpq -0x48(%rbp), %r13
jne 0x26c2
jmp 0x2701
movb $0x1, %r12b
leaq -0xa0(%rbp), %rdi
movl $0x14, %esi
callq 0x2896
decl %r15d
sete %al
orb %r12b, %al
cmpb $0x1, %al
jne 0x26b5
leaq 0x92b(%rip), %rax       # 0x3051
leaq 0x938(%rip), %rsi       # 0x3065
testb %r12b, %r12b
cmovneq %rax, %rsi
movl $0x13, %eax
movl $0x1a, %edx
cmovneq %rax, %rdx
movq 0x288f(%rip), %rbx      # 0x4fd8
movq %rbx, %rdi
callq 0x2130
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq 0x2869(%rip), %rdi      # 0x4fd8
callq 0x2030
movq %rax, %rdi
callq 0x20b0
leaq -0xa0(%rbp), %rdi
callq 0x2968
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x2796
callq 0x2100
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x27c2
jmp 0x27c9
jmp 0x27c9
jmp 0x27c9
jmp 0x27c2
movq %rax, %rbx
movq %r15, %rdi
callq 0x2100
jmp 0x27d8
jmp 0x27c9
jmp 0x27c9
movq %rax, %rbx
jmp 0x27d8
jmp 0x27c9
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x2968
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x27e6
callq 0x2100
movq %rbx, %rdi
callq 0x21c0