// File: /workspace/github_cpp_makefile_star2_O3/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 0x4827
leaq 0x80(%rsp), %rdi
callq 0x2110
leaq 0x90(%rsp), %r14
movq 0x8(%rbx), %r15
testq %r15, %r15
je 0x4873
movq %r15, %rdi
callq 0x20a0
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x21d0
jmp 0x4894
jle 0x49d3
movq 0x57b4(%rip), %r14      # 0x9fe8
leaq 0x29d1(%rip), %rsi      # 0x720c
movl $0x7, %edx
movq %r14, %rdi
callq 0x21d0
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x4980
movq %rbx, %rdi
callq 0x20a0
movq 0x5785(%rip), %rdi      # 0x9fe8
movq %rbx, %rsi
movq %rax, %rdx
callq 0x21d0
jmp 0x4998
movq 0x90(%rsp), %rax
movq -0x18(%rax), %rax
addq %rax, %r14
movl 0xb0(%rsp,%rax), %esi
orl $0x1, %esi
movq %r14, %rdi
callq 0x2280
leaq 0x80(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x20e0
movq 0x80(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0xa0(%rsp,%rax)
je 0x495d
movq 0x5721(%rip), %r14      # 0x9fe8
leaq 0x293e(%rip), %rsi      # 0x720c
movl $0x7, %edx
movq %r14, %rdi
callq 0x21d0
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x48ff
movq %rbx, %rdi
callq 0x20a0
movq 0x56f6(%rip), %rdi      # 0x9fe8
movq %rbx, %rsi
movq %rax, %rdx
callq 0x21d0
jmp 0x4917
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x2280
movq 0x56ca(%rip), %rbx      # 0x9fe8
leaq 0x28ef(%rip), %rsi      # 0x7214
movl $0x11, %edx
movq %rbx, %rdi
callq 0x21d0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq 0x5698(%rip), %rdi      # 0x9fe8
callq 0x2030
movq %rax, %rdi
callq 0x2100
movq 0x5644(%rip), %rsi      # 0x9fa8
leaq 0x80(%rsp), %rdi
callq 0x2130
leaq 0x100(%rsp), %rdi
callq 0x2060
jmp 0x49d3
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x2280
leaq 0x2875(%rip), %rsi      # 0x7214
movl $0x11, %edx
movq %r14, %rdi
callq 0x21d0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2260
movl 0x8(%rbx), %edi
callq 0x2190
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movl $0xc80, %edi            # imm = 0xC80
callq 0x21b0
movq %rax, 0x10(%rsp)
leaq 0xc80(%rax), %rcx
movq %rcx, 0x20(%rsp)
xorl %ecx, %ecx
movb $0x0, 0x4(%rax,%rcx)
movl $0x0, (%rax,%rcx)
addq $0x5, %rcx
cmpq $0xc80, %rcx            # imm = 0xC80
jne 0x4a1b
addq %rcx, %rax
leaq 0x10(%rsp), %rcx
movq %rax, 0x8(%rcx)
movabsq $0x5000000008, %rax     # imm = 0x5000000008
movq %rax, 0x18(%rcx)
movq $0x0, 0x88(%rsp)
movq %rcx, 0x80(%rsp)
movaps 0x80(%rsp), %xmm0
movups 0x571f(%rip), %xmm1     # 0xa190
movaps %xmm1, 0x80(%rsp)
movups %xmm0, 0x5710(%rip)     # 0xa190
movq 0x5719(%rip), %rax      # 0xa1a0
movq %rax, 0x90(%rsp)
leaq 0x7a6(%rip), %rcx       # 0x523c
movq %rcx, 0x5703(%rip)      # 0xa1a0
movq 0x5704(%rip), %rcx      # 0xa1a8
movq %rcx, 0x98(%rsp)
leaq 0x781(%rip), %rcx       # 0x5234
movq %rcx, 0x56ee(%rip)      # 0xa1a8
testq %rax, %rax
je 0x4ad1
leaq 0x80(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0x10(%rsp), %rdi
movl $0x23, %esi
callq 0x5280
leaq 0x7(%rsp), %rax
movb $0x0, (%rax)
leaq 0x8(%rsp), %rax
movl $0x0, (%rax)
leaq 0x208(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2230
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
callq 0x2200
leaq 0x50(%rsp), %r13
movq $0x0, 0x8(%r13)
xorl %edi, %edi
callq 0x2180
movq %rax, (%r13)
movq %rbx, %r12
movq %rbx, %rdi
callq 0x2220
leaq 0x10(%rsp), %rbp
movq 0x5485(%rip), %rbx      # 0x9fc8
movabsq $0x112e0be826d694b3, %r15 # imm = 0x112E0BE826D694B3
leaq 0x60(%rsp), %r14
movl 0x8(%rsp), %eax
testl %eax, %eax
setle %cl
testb %cl, 0x7(%rsp)
jne 0x4c1d
testl %eax, %eax
jle 0x4b6f
decl %eax
movl %eax, 0x8(%rsp)
movq %rbp, %rdi
callq 0x53c2
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x55fe
cmpb $0x0, 0x7(%rsp)
jne 0x4bcc
movq %r14, %rdi
movq %rbp, %rsi
callq 0x5724
movq 0x28(%rsp), %rax
movq %rax, 0x78(%rsp)
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
leaq 0x7(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x569b
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x4bcc
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movslq 0xc(%rsp), %rax
imulq $0x3e8, %rax, %rcx      # imm = 0x3E8
addq 0x58(%rsp), %rcx
movq %rcx, %rax
imulq %r15
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x1a, %rdx
addq %rax, %rdx
addq %rdx, 0x50(%rsp)
imulq $0x3b9aca00, %rdx, %rax # imm = 0x3B9ACA00
subq %rax, %rcx
movq %rcx, 0x58(%rsp)
leaq 0x80(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2240
jmp 0x4b52
leaq 0x208(%rsp), %rbx
movq %rbx, %rdi
callq 0x2140
leaq 0x80(%rsp), %rdi
callq 0x22c0
movq %rbx, %rdi
callq 0x21e0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4c59
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x4c70
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
xorl %eax, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdi
callq 0x5719
jmp 0x4cd5
movq %rax, %rbx
jmp 0x4cef
movq %rax, %rbx
movq 0x530b(%rip), %rsi      # 0x9fa8
leaq 0x80(%rsp), %rdi
callq 0x2130
leaq 0x100(%rsp), %rdi
callq 0x2060
jmp 0x4d06
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x4cd8
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
jmp 0x4cd8
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4cef
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x4d06
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x22b0