// File: /workspace/github_cpp_makefile_star2_O2/sgielen[P]clife/multicolor.cpp
// Function: main
int main(int argc, char *argv[]) {
	int microsleeptime = 100'000;
	if(argc == 2) {
		std::stringstream ss;
		ss << argv[1];
		ss >> microsleeptime;
		if(!ss) {
			std::cerr << "Usage: " << argv[0] << " [microsleeptime]" << std::endl;
		}
	} else if(argc > 2) {
		std::cerr << "Usage: " << argv[0] << " [microsleeptime]" << std::endl;
	}

	init_random();

	winsize current_winsize;
#if defined(FIXED_SIZE)
	current_winsize.ws_col = FIXED_WIDTH;
	current_winsize.ws_row = FIXED_HEIGHT;
#else
	ioctl(STDOUT_FILENO, TIOCGWINSZ, &current_winsize);
#endif
	std::vector<uint64_t> earlier_hashes;
	GameOfLifeField<MulticolorValue> field(current_winsize.ws_col, current_winsize.ws_row);
	get_width = [&field]() {
		return field.get_width();
	};

	field.generateRandom(35);

	bool field_done = false;
	int repeats_to_do = 0;

	pthread_mutex_t mtx;
	pthread_mutex_init(&mtx, nullptr);
	pthread_cond_t cond;
	pthread_cond_init(&cond, nullptr);
	struct timespec ts = {};
	ts.tv_sec = std::time(nullptr);
	pthread_mutex_lock(&mtx);

	while(!field_done || repeats_to_do > 0) {
#if !defined(FIXED_SIZE)
		winsize new_winsize;
		ioctl(STDOUT_FILENO, TIOCGWINSZ, &new_winsize);
		if(current_winsize.ws_col != new_winsize.ws_col
		|| current_winsize.ws_row != new_winsize.ws_row) {
			field.resize(new_winsize.ws_col, new_winsize.ws_row);
		}
		current_winsize = new_winsize;
#endif

		if(repeats_to_do > 0) {
			--repeats_to_do;
		}
		field.nextState();
		field.print_simple(std::cout);
		if(!field_done) {
			check_stop_condition(field, earlier_hashes, field_done, repeats_to_do);
		}
		ts.tv_nsec += microsleeptime * 1'000;
		ts.tv_sec += ts.tv_nsec / 1'000'000'000;
		ts.tv_nsec %= 1'000'000'000;
		pthread_cond_timedwait(&cond, &mtx, &ts);
	}

	pthread_mutex_unlock(&mtx);
	pthread_cond_destroy(&cond);
	pthread_mutex_destroy(&mtx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %rsi, %rbx
movl $0x186a0, 0xc(%rsp)     # imm = 0x186A0
cmpl $0x2, %edi
jne 0x4091
leaq 0xa0(%rsp), %rdi
callq 0x20f0
leaq 0xb0(%rsp), %rdi
movq 0x8(%rbx), %rsi
callq 0x2180
leaq 0xa0(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x20c0
movq 0xa0(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0xc0(%rsp,%rax)
je 0x4082
movq 0x5f94(%rip), %rdi      # 0x9fe8
leaq 0x21b1(%rip), %rsi      # 0x620c
callq 0x2180
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x2180
leaq 0x21a2(%rip), %rsi      # 0x6214
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x2080
leaq 0xa0(%rsp), %rdi
callq 0x2110
jmp 0x40c8
jle 0x40c8
movq 0x5f4e(%rip), %rdi      # 0x9fe8
leaq 0x216b(%rip), %rsi      # 0x620c
callq 0x2180
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x2180
leaq 0x215c(%rip), %rsi      # 0x6214
movq %rax, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x2080
callq 0x3fc3
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
leaq 0x38(%rsp), %rbx
pushq $0x50
popq %rsi
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0x46bc
leaq 0xa0(%rsp), %rdi
andq $0x0, 0x8(%rdi)
movq %rbx, (%rdi)
movaps (%rdi), %xmm0
movups 0x6070(%rip), %xmm1     # 0xa178
movaps %xmm1, (%rdi)
movups %xmm0, 0x6066(%rip)     # 0xa178
movups 0x606f(%rip), %xmm0     # 0xa188
leaq 0x55a(%rip), %rax       # 0x467a
movq %rax, 0x6061(%rip)      # 0xa188
movaps %xmm0, 0x10(%rdi)
leaq 0x540(%rip), %rax       # 0x4672
movq %rax, 0x6057(%rip)      # 0xa190
callq 0x46a0
pushq $0x23
popq %rsi
movq %rbx, %rdi
callq 0x46fa
leaq 0x7(%rsp), %rax
movb $0x0, (%rax)
leaq 0x8(%rsp), %rax
andl $0x0, (%rax)
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2210
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0x21e0
leaq 0x10(%rsp), %r13
andq $0x0, 0x8(%r13)
xorl %edi, %edi
callq 0x2160
movq %rax, (%r13)
movq %rbx, %r12
movq %rbx, %rdi
callq 0x2200
leaq 0x38(%rsp), %rbp
movq 0x5e25(%rip), %rbx      # 0x9fc8
movl $0x3b9aca00, %r15d      # imm = 0x3B9ACA00
leaq 0x58(%rsp), %r14
movl 0x8(%rsp), %eax
testl %eax, %eax
setle %cl
testb %cl, 0x7(%rsp)
jne 0x4247
testl %eax, %eax
jle 0x41cb
decl %eax
movl %eax, 0x8(%rsp)
movq %rbp, %rdi
callq 0x47cc
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x4998
cmpb $0x0, 0x7(%rsp)
jne 0x420f
movq %r14, %rdi
movq %rbp, %rsi
callq 0x4a68
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x7(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x4a07
movq %r14, %rdi
callq 0x4af0
movslq 0xc(%rsp), %rax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
addq 0x18(%rsp), %rax
cqto
idivq %r15
addq %rax, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
leaq 0xa0(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2220
jmp 0x41ae
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
callq 0x2120
leaq 0xa0(%rsp), %rdi
callq 0x2290
movq %rbx, %rdi
callq 0x21c0
leaq 0x38(%rsp), %rdi
callq 0x4af0
leaq 0x20(%rsp), %rdi
callq 0x4b9e
xorl %eax, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x42b9
movq %rax, %rbx
jmp 0x42c6
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x2110
jmp 0x42d0
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x4af0
jmp 0x42bc
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x4af0
leaq 0x20(%rsp), %rdi
callq 0x4b9e
movq %rbx, %rdi
callq 0x2280