Bug reported in https://bugs.llvm.org/show_bug.cgi?id=42269.
Problem caused by recent attempt to fix memory leak on the same memory block.
Investigation showed that worker threads may leave contention group either before or after the master thread depending on the number of teams in the teams construct.
This patch makes freeing of the contention group structure conditional for master thread, and adds similar conditional freeing for worker threads. Thus both the memory leak and the use-after-free problems are fixed now.
Do not add test because the problem was only visible if library is instrumented by memory sanitizer.