// File: /workspace/github_cpp_makefile_star2_O3/eugene87222[P]Game-of-Life/gol.cpp
// Function: main
int main(int argc, char *argv[]) {
    bool gosper=false, simkin=false;
    const char* const short_opts = "h";
    const option long_opts[] = {
            {"gosper", no_argument, nullptr, 'g'},
            {"simkin", no_argument, nullptr, 's'},
            {"help", no_argument, nullptr, 'h'},
            {nullptr, no_argument, nullptr, 0}
    };
    while(true) {
        const auto opt = getopt_long(argc, argv, short_opts, long_opts, nullptr);

        if(opt == -1) break;

        switch(opt) {
        case 'g':
            gosper = true;
            break;
        case 's':
            simkin = true;
            break;
        case 'h':
        case '?':
        default:
            printHelp();
            break;
        }
    }

    setlocale(LC_ALL,"");
    initscr();
	clear();
	noecho();
	cbreak();
	keypad(stdscr, TRUE);
	curs_set(0);
	nodelay(stdscr, TRUE);

    int rows, cols, height, width, ms, p_size;
	getmaxyx(stdscr, rows, cols);
    height = rows - 2;
    width = (cols-2) / 2;
    ms = 100;
    p_size = height / thd_num;
    thds = vector<thread>(thd_num);

    char status_background[29];
	memset(status_background, ' ', 29);
	status_background[28] = '\0';

    if(gosper && height>=11 && width>=38) Gosperglidergun(height, width);
    else if(simkin && height>=24 && width>=35) Simkinglidergun(height, width);
    else randomBoard(height, width);

    int keyin, gen = 0;
    bool pause = false;
	while(true) {
		keyin = getch();
		switch(keyin) {
		case 'q':
		case 'Q':
            goodBye();
            break;					
		case 'r':
		case 'R':
            randomBoard(height, width);
			break;
        case 'p':
        case 'P':
        case KEY_SREPLACE:
            pause = !pause;
            if(pause) mvprintw(height+1, 3, "/ PAUSED /");
			else mvprintw(height+1, 3, "══════════");
            break;
		case KEY_LEFT:
            if(ms > 9) ms -= 10;
			break;
		case KEY_RIGHT:
            ms += 10;
			break;
		default:
            break;
		}

        if(!pause) {
            draw(height, width, status_background, gen, ms);
            // update(height, width);
            new_board = board;
            for(int i=0; i<thd_num; i++) {
                thds[i] = thread(update_thd, p_size*i+1, i==thd_num-1?height:p_size*(i+1), 1, width);
            }
            for(int i=0; i<thd_num; i++) {
                thds[i].join();
            }
            board = new_board;
            gen++;
        }
        usleep(ms*1000);
	}

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x30c1(%rip), %rsi      # 0x5cd0
leaq 0x70(%rsp), %r14
movl $0x10, %ecx
movq %r14, %rdi
rep  movsq (%rsi), %es:(%rdi)
xorl %r13d, %r13d
leaq 0x150d(%rip), %r15      # 0x4136
xorl %r12d, %r12d
movl %r12d, %ecx
movl %ecx, %r12d
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x2220
movb $0x1, %cl
cmpl $0x67, %eax
je 0x2c2f
cmpl $0x73, %eax
jne 0x2c56
movb $0x1, %r13b
jmp 0x2c2c
cmpl $-0x1, %eax
jne 0x2cbc
leaq 0x149c(%rip), %rsi      # 0x40fe
movl $0x6, %edi
callq 0x2130
callq 0x20f0
callq 0x21a0
callq 0x2280
callq 0x2060
movq 0x3349(%rip), %rbx      # 0x5fd0
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x2080
xorl %edi, %edi
callq 0x2110
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x20d0
movq (%rbx), %rax
testq %rax, %rax
jne 0x2cd6
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0xfffffffd, %ebx       # imm = 0xFFFFFFFD
jmp 0x2ced
movq 0x3315(%rip), %rdi      # 0x5fd8
leaq 0x1396(%rip), %rsi      # 0x4060
callq 0x2170
xorl %edi, %edi
callq 0x21b0
movswl 0x4(%rax), %ebx
decl %ebx
movswl 0x6(%rax), %eax
movl %eax, %ecx
decl %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
decl %eax
sarl %eax
movl %eax, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movslq 0x345c(%rip), %rsi      # 0x6158
movl %ebx, %eax
cltd
idivl %esi
movl %eax, %r14d
leaq 0x30(%rsp), %r15
leaq 0x1c(%rsp), %rdx
movq %r15, %rdi
callq 0x344a
movq 0x34b3(%rip), %rax      # 0x61d0
movaps (%r15), %xmm0
movq 0x10(%r15), %rcx
movq %rcx, 0x34a4(%rip)      # 0x61d0
movaps 0x348d(%rip), %xmm1     # 0x61c0
movaps %xmm0, 0x3486(%rip)     # 0x61c0
leaq 0x50(%rsp), %rbp
movaps %xmm1, (%rbp)
movq %rax, 0x10(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, (%r15)
movq $0x0, 0x10(%r15)
movq %rbp, %rdi
callq 0x305c
movq %r15, %rdi
callq 0x305c
movl 0x8(%rsp), %esi
movaps 0x12cf(%rip), %xmm0     # 0x4040
movups %xmm0, 0xd(%rbp)
movaps %xmm0, (%rbp)
movb $0x0, 0x1c(%rbp)
cmpl $0xb, %ebx
setge %al
andb %al, %r12b
cmpb $0x1, %r12b
jne 0x2da1
cmpl $0x26, %esi
jl 0x2da1
movl %ebx, %edi
callq 0x2604
movq 0x3231(%rip), %r12      # 0x5fd0
jmp 0x2dcc
cmpl $0x18, %ebx
setge %al
andb %al, %r13b
cmpb $0x1, %r13b
movq 0x321b(%rip), %r12      # 0x5fd0
jne 0x2dc5
cmpl $0x23, %esi
jl 0x2dc5
movl %ebx, %edi
callq 0x26d0
jmp 0x2dcc
movl %ebx, %edi
callq 0x256b
leal 0x1(%rbx), %eax
movl %eax, 0x14(%rsp)
movl $0x64, %r15d
leaq 0x20(%rsp), %rbp
leaq 0x10(%rsp), %r13
movl $0x0, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
movq (%r12), %rdi
callq 0x21c0
cmpl $0x103, %eax            # imm = 0x103
jg 0x2e41
addl $-0x50, %eax
cmpl $0x22, %eax
ja 0x2eb6
movabsq $0x100000001, %rcx      # imm = 0x100000001
btq %rax, %rcx
jb 0x2e56
movabsq $0x400000004, %rcx      # imm = 0x400000004
btq %rax, %rcx
jae 0x2fda
movl 0x10(%rsp), %esi
movl %ebx, %edi
callq 0x256b
jmp 0x2eb6
cmpl $0x104, %eax            # imm = 0x104
je 0x2ea1
cmpl $0x105, %eax            # imm = 0x105
je 0x2eb2
cmpl $0x191, %eax            # imm = 0x191
jne 0x2eb6
movq %r15, %r12
movq 0x8(%rsp), %rax
movl %eax, %r15d
xorb $0x1, %r15b
testb $0x1, %al
je 0x2e7b
movl 0x14(%rsp), %edi
movl $0x3, %esi
leaq 0x12dd(%rip), %rdx      # 0x4156
jmp 0x2e8b
movl 0x14(%rsp), %edi
movl $0x3, %esi
leaq 0x12c0(%rip), %rdx      # 0x414b
xorl %eax, %eax
callq 0x2270
movl %r15d, %eax
movq %r12, %r15
movq 0x3131(%rip), %r12      # 0x5fd0
jmp 0x2ebb
leal -0xa(%r15), %eax
cmpl $0xa, %r15d
cmovll %r15d, %eax
movl %eax, %r15d
jmp 0x2eb6
addl $0xa, %r15d
movq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
testb $0x1, %al
jne 0x2fc9
movl 0x10(%rsp), %esi
movl %ebx, %edi
leaq 0x50(%rsp), %rdx
movl 0x18(%rsp), %ecx
movq %r15, 0x28(%rsp)
movl %r15d, %r8d
callq 0x29d6
leaq 0x32bd(%rip), %rdi      # 0x61a8
leaq 0x329e(%rip), %rsi      # 0x6190
callq 0x32ac
movl 0x325b(%rip), %eax      # 0x6158
testl %eax, %eax
jle 0x2fa6
movl $0x1, %r12d
xorl %r15d, %r15d
movl %r12d, 0x1c(%rsp)
decl %eax
cmpq %rax, %r15
leal -0x1(%r14,%r12), %eax
cmovel %ebx, %eax
movl %eax, 0x24(%rsp)
movl $0x1, 0x20(%rsp)
leaq 0x30(%rsp), %rdi
leaq -0x787(%rip), %rsi      # 0x27b1
leaq 0x1c(%rsp), %rdx
leaq 0x24(%rsp), %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x34b8
movq 0x326c(%rip), %rax      # 0x61c0
cmpq $0x0, (%rax,%r15,8)
jne 0x3002
movq 0x30(%rsp), %rcx
movq %rcx, (%rax,%r15,8)
incq %r15
movslq 0x31e6(%rip), %rax      # 0x6158
addl %r14d, %r12d
cmpq %rax, %r15
jl 0x2f0e
testl %eax, %eax
jle 0x2fa6
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x3235(%rip), %rdi      # 0x61c0
addq %r15, %rdi
callq 0x2230
incq %r12
movslq 0x31bb(%rip), %rax      # 0x6158
addq $0x8, %r15
cmpq %rax, %r12
jl 0x2f84
leaq 0x31e3(%rip), %rdi      # 0x6190
leaq 0x31f4(%rip), %rsi      # 0x61a8
callq 0x32ac
incl 0x18(%rsp)
movq 0x300c(%rip), %r12      # 0x5fd0
movq 0x28(%rsp), %r15
imull $0x3e8, %r15d, %edi     # imm = 0x3E8
callq 0x21d0
jmp 0x2df4
movabsq $0x200000002, %rcx      # imm = 0x200000002
btq %rax, %rcx
movq 0x8(%rsp), %rax
jae 0x2ebb
callq 0x21a0
callq 0x2260
jmp 0x2ccf
callq 0x2050
nop