// File: /workspace/github_cpp_makefile_star2_O1/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 0x46c7
leaq 0x80(%rsp), %rdi
callq 0x2110
leaq 0x90(%rsp), %r14
movq 0x8(%rbx), %r15
testq %r15, %r15
je 0x4713
movq %r15, %rdi
callq 0x20a0
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x21d0
jmp 0x4734
jle 0x4873
movq 0x5914(%rip), %r14      # 0x9fe8
leaq 0x2ab1(%rip), %rsi      # 0x718c
movl $0x7, %edx
movq %r14, %rdi
callq 0x21d0
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x4820
movq %rbx, %rdi
callq 0x20a0
movq 0x58e5(%rip), %rdi      # 0x9fe8
movq %rbx, %rsi
movq %rax, %rdx
callq 0x21d0
jmp 0x4838
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 0x47fd
movq 0x5881(%rip), %r14      # 0x9fe8
leaq 0x2a1e(%rip), %rsi      # 0x718c
movl $0x7, %edx
movq %r14, %rdi
callq 0x21d0
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x479f
movq %rbx, %rdi
callq 0x20a0
movq 0x5856(%rip), %rdi      # 0x9fe8
movq %rbx, %rsi
movq %rax, %rdx
callq 0x21d0
jmp 0x47b7
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x2280
movq 0x582a(%rip), %rbx      # 0x9fe8
leaq 0x29cf(%rip), %rsi      # 0x7194
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 0x57f8(%rip), %rdi      # 0x9fe8
callq 0x2030
movq %rax, %rdi
callq 0x2100
movq 0x57a4(%rip), %rsi      # 0x9fa8
leaq 0x80(%rsp), %rdi
callq 0x2130
leaq 0x100(%rsp), %rdi
callq 0x2060
jmp 0x4873
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x2280
leaq 0x2955(%rip), %rsi      # 0x7194
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
xorl %r14d, %r14d
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2260
movl 0x8(%rbx), %edi
callq 0x2190
movq %r14, 0x40(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq %r14, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movl $0xc80, %edi            # imm = 0xC80
callq 0x21b0
movq %rax, 0x10(%rsp)
movq %rax, 0x18(%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 0x48c9
addq %rcx, %rax
leaq 0x10(%rsp), %rcx
movq %rax, 0x8(%rcx)
movabsq $0x5000000008, %rax     # imm = 0x5000000008
movq %rax, 0x18(%rcx)
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x90(%rsp)
movq %rcx, 0x80(%rsp)
leaq 0x7d6(%rip), %rcx       # 0x50f4
movq %rcx, 0x98(%rsp)
leaq 0x7cf(%rip), %rdx       # 0x50fc
movq %rdx, 0x90(%rsp)
movaps 0x80(%rsp), %xmm0
movups 0x584c(%rip), %xmm1     # 0xa190
movaps %xmm1, 0x80(%rsp)
movups %xmm0, 0x583d(%rip)     # 0xa190
movq 0x5846(%rip), %rax      # 0xa1a0
movq %rax, 0x90(%rsp)
movq %rdx, 0x5837(%rip)      # 0xa1a0
movq 0x5838(%rip), %rdx      # 0xa1a8
movq %rdx, 0x98(%rsp)
movq %rcx, 0x5829(%rip)      # 0xa1a8
testq %rax, %rax
je 0x4996
leaq 0x80(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
leaq 0x10(%rsp), %rdi
movl $0x23, %esi
callq 0x5140
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
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %r13
movaps %xmm0, (%r13)
xorl %edi, %edi
callq 0x2180
movq %rax, (%r13)
movq %rbx, %r12
movq %rbx, %rdi
callq 0x2220
leaq 0x10(%rsp), %rbp
movq 0x55c0(%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 0x4ae2
testl %eax, %eax
jle 0x4a34
decl %eax
movl %eax, 0x8(%rsp)
movq %rbp, %rdi
callq 0x527a
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x54b0
cmpb $0x0, 0x7(%rsp)
jne 0x4a91
movq %r14, %rdi
movq %rbp, %rsi
callq 0x55d0
movq 0x28(%rsp), %rax
movq %rax, 0x78(%rsp)
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
leaq 0x7(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x5547
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x4a91
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 0x4a17
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 0x4b1e
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x4b35
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 0x55c5
jmp 0x4b9a
movq %rax, %rbx
jmp 0x4bb4
movq %rax, %rbx
movq 0x5446(%rip), %rsi      # 0x9fa8
leaq 0x80(%rsp), %rdi
callq 0x2130
leaq 0x100(%rsp), %rdi
callq 0x2060
jmp 0x4bcb
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x4b9d
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
jmp 0x4b9d
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4bb4
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x4bcb
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x22b0