// File: shutow[P]shared_mutex/test2.cpp
// Function: test2()
int test2()
{
	printf("test 2:\n");
	printf("collision detection\n");

	Test t;
	std::vector<bool> rv(1000000, true);
	std::vector<std::thread> th;
	std::atomic<int> count;
	count = 0;
	const int numthreads = 12;
	for (int i = 0; i < numthreads; ++i)
	{
		th.emplace_back([&]()
		{
			while (true)
			{
				int c = count++;
				if (c >= (int)rv.size()) return;
				rv[c] = t.test();
			}
		});
	}
	for (int i = 0; i < numthreads; ++i) th[i].join();
	int res = 0;
	for (auto n : rv)
	{
		if (!n)
		{
			res = -1;
			break;
		}
	}
	if (res != 0)
		std::cout << "A collision between reading and writing threads had occured." << std::endl;
	else
		std::cout << "All went OK." << std::endl;
	return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
leaq 0x5676(%rip), %rdi      # 0xc268
xorl %eax, %eax
movb %al, -0x159(%rbp)
callq 0x2040
movb -0x159(%rbp), %al
leaq 0x5665(%rip), %rdi      # 0xc271
callq 0x2040
leaq -0x70(%rbp), %rdi
callq 0x6f50
movb $0x1, -0x99(%rbp)
leaq -0x9a(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x7b30
movq -0x158(%rbp), %rcx
leaq -0x98(%rbp), %rdi
movl $0xf4240, %esi          # imm = 0xF4240
leaq -0x99(%rbp), %rdx
callq 0x7b50
jmp 0x6c55
leaq -0x9a(%rbp), %rdi
callq 0x7bd0
leaq -0xc8(%rbp), %rdi
callq 0x7bf0
leaq -0xcc(%rbp), %rdi
xorl %esi, %esi
callq 0x7c10
movl $0xc, -0xd0(%rbp)
movl $0x0, -0xd4(%rbp)
cmpl $0xc, -0xd4(%rbp)
jge 0x6d3d
leaq -0xcc(%rbp), %rax
movq %rax, -0xf0(%rbp)
leaq -0x98(%rbp), %rax
movq %rax, -0xe8(%rbp)
leaq -0x70(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x7060
jmp 0x6cd8
jmp 0x6cda
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
jmp 0x6c8f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
leaq -0x9a(%rbp), %rdi
callq 0x7bd0
jmp 0x6f39
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x7e00
leaq -0x98(%rbp), %rdi
callq 0x7e60
jmp 0x6f39
movl $0x0, -0xf4(%rbp)
cmpl $0xc, -0xf4(%rbp)
jge 0x6d80
movslq -0xf4(%rbp), %rsi
leaq -0xc8(%rbp), %rdi
callq 0x7cb0
movq %rax, %rdi
callq 0x2250
jmp 0x6d6d
jmp 0x6d6f
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
jmp 0x6d47
movl $0x0, -0xf8(%rbp)
leaq -0x98(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rdi
callq 0x7cd0
movl %edx, -0x118(%rbp)
movq %rax, -0x120(%rbp)
movl -0x118(%rbp), %eax
movl %eax, -0x108(%rbp)
movq -0x120(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x100(%rbp), %rdi
callq 0x7d10
movl %edx, -0x138(%rbp)
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rax
movq %rax, -0x130(%rbp)
movl -0x138(%rbp), %eax
movl %eax, -0x128(%rbp)
leaq -0x110(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x7d30
movb %al, -0x15a(%rbp)
jmp 0x6e19
movb -0x15a(%rbp), %al
testb $0x1, %al
jne 0x6e25
jmp 0x6e8e
leaq -0x110(%rbp), %rdi
callq 0x7d60
movq %rdx, -0x170(%rbp)
movq %rax, -0x168(%rbp)
jmp 0x6e41
movq -0x170(%rbp), %rax
movq -0x168(%rbp), %rcx
movq %rcx, -0x150(%rbp)
movq %rax, -0x148(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x7da0
testb $0x1, %al
jne 0x6e79
movl $0xffffffff, -0xf8(%rbp) # imm = 0xFFFFFFFF
jmp 0x6e8e
jmp 0x6e7b
leaq -0x110(%rbp), %rdi
callq 0x7dd0
jmp 0x6e89
jmp 0x6dfe
cmpl $0x0, -0xf8(%rbp)
je 0x6eca
movq 0xd13a(%rip), %rdi      # 0x13fd8
leaq 0x53e1(%rip), %rsi      # 0xc286
callq 0x2180
movq %rax, -0x178(%rbp)
jmp 0x6eb3
movq -0x178(%rbp), %rdi
movq 0xd0ff(%rip), %rsi      # 0x13fc0
callq 0x21a0
jmp 0x6ec8
jmp 0x6efd
movq 0xd107(%rip), %rdi      # 0x13fd8
leaq 0x53eb(%rip), %rsi      # 0xc2c3
callq 0x2180
movq %rax, -0x180(%rbp)
jmp 0x6ee6
movq -0x180(%rbp), %rdi
movq 0xd0cc(%rip), %rsi      # 0x13fc0
callq 0x21a0
jmp 0x6efb
jmp 0x6efd
movl -0xf8(%rbp), %eax
movl %eax, -0x184(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x7e00
leaq -0x98(%rbp), %rdi
callq 0x7e60
leaq -0x70(%rbp), %rdi
callq 0x70d0
movl -0x184(%rbp), %eax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
leaq -0x70(%rbp), %rdi
callq 0x70d0
movq -0xa8(%rbp), %rdi
callq 0x2260
nop