// File: /workspace/github_cpp_makefile_star2_O1/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 $0xe8, %rsp
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x30ba(%rip), %rsi      # 0x5cd0
leaq 0x60(%rsp), %r14
movl $0x10, %ecx
movq %r14, %rdi
rep  movsq (%rsi), %es:(%rdi)
movl $0x0, (%rsp)
leaq 0x14d2(%rip), %r15      # 0x4106
xorl %r12d, %r12d
xorl %r13d, %r13d
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x2220
cmpl $-0x1, %eax
je 0x2c70
cmpl $0x67, %eax
je 0x2c6a
cmpl $0x73, %eax
jne 0x2dd1
movb $0x1, %r13b
movb $0x1, %al
movl %eax, (%rsp)
jmp 0x2c70
movb $0x1, %r13b
movb $0x1, %r12b
testb %r13b, %r13b
jne 0x2c37
leaq 0x1452(%rip), %rsi      # 0x40ce
movl $0x6, %edi
callq 0x2130
callq 0x20f0
callq 0x21a0
callq 0x2280
callq 0x2060
movq 0x332f(%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
movl $0xfffffffd, %ebx       # imm = 0xFFFFFFFD
testq %rax, %rax
je 0x2cd5
movswl 0x4(%rax), %ebx
decl %ebx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x2cf0
movswl 0x6(%rax), %eax
movl %eax, %ecx
decl %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
decl %ecx
sarl %ecx
movl %ecx, 0x8(%rsp)
movslq 0x345d(%rip), %rsi      # 0x6158
movl %ebx, %eax
cltd
idivl %esi
movl %eax, %r14d
leaq 0x28(%rsp), %r15
leaq 0x14(%rsp), %rdx
movq %r15, %rdi
callq 0x3340
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rbp
movaps %xmm0, (%rbp)
movups 0x3480(%rip), %xmm0     # 0x61a8
movq 0x3489(%rip), %rax      # 0x61b8
movups (%r15), %xmm1
movups %xmm1, 0x346e(%rip)     # 0x61a8
movq 0x10(%r15), %rcx
movq %rcx, 0x3473(%rip)      # 0x61b8
movaps (%rbp), %xmm1
movaps %xmm0, (%rbp)
movq %rax, 0x10(%rbp)
movups %xmm1, (%r15)
movq $0x0, 0x10(%r15)
movq %rbp, %rdi
callq 0x3076
movq %r15, %rdi
callq 0x3076
movaps 0x129c(%rip), %xmm0     # 0x4010
movups %xmm0, 0xd(%rbp)
movaps %xmm0, (%rbp)
movb $0x0, 0x1c(%rbp)
cmpl $0xb, %ebx
setge %al
andb %al, %r12b
movl 0x8(%rsp), %esi
cmpl $0x26, %esi
setge %al
andb %r12b, %al
cmpb $0x1, %al
jne 0x2daa
movl %ebx, %edi
callq 0x260a
movq 0x3228(%rip), %r12      # 0x5fd0
jmp 0x2df0
cmpl $0x18, %ebx
setge %al
movl (%rsp), %ecx
andb %al, %cl
cmpl $0x23, %esi
setge %al
andb %cl, %al
cmpb $0x1, %al
movq 0x320a(%rip), %r12      # 0x5fd0
movl %ebx, %edi
jne 0x2deb
callq 0x26d6
jmp 0x2df0
movq 0x3200(%rip), %rdi      # 0x5fd8
leaq 0x1251(%rip), %rsi      # 0x4030
callq 0x2170
xorl %edi, %edi
callq 0x21b0
callq 0x2571
leal 0x1(%rbx), %eax
movl %eax, 0xc(%rsp)
movl $0x64, %r15d
leaq 0x18(%rsp), %rbp
leaq 0x8(%rsp), %r13
movl $0x0, 0x10(%rsp)
movq $0x0, (%rsp)
movq (%r12), %rdi
callq 0x21c0
cmpl $0x103, %eax            # imm = 0x103
jg 0x2e64
addl $-0x50, %eax
cmpl $0x22, %eax
ja 0x2ed8
movabsq $0x100000001, %rcx      # imm = 0x100000001
btq %rax, %rcx
jb 0x2e79
movabsq $0x400000004, %rcx      # imm = 0x400000004
btq %rax, %rcx
jae 0x2ffb
movl 0x8(%rsp), %esi
movl %ebx, %edi
callq 0x2571
jmp 0x2ed8
cmpl $0x104, %eax            # imm = 0x104
je 0x2ec3
cmpl $0x105, %eax            # imm = 0x105
je 0x2ed4
cmpl $0x191, %eax            # imm = 0x191
jne 0x2ed8
movq %r15, %r12
movq (%rsp), %rax
movl %eax, %r15d
xorb $0x1, %r15b
testb $0x1, %al
je 0x2e9d
movl 0xc(%rsp), %edi
movl $0x3, %esi
leaq 0x128b(%rip), %rdx      # 0x4126
jmp 0x2ead
movl 0xc(%rsp), %edi
movl $0x3, %esi
leaq 0x126e(%rip), %rdx      # 0x411b
xorl %eax, %eax
callq 0x2270
movl %r15d, %eax
movq %r12, %r15
movq 0x310f(%rip), %r12      # 0x5fd0
jmp 0x2edc
leal -0xa(%r15), %eax
cmpl $0xa, %r15d
cmovll %r15d, %eax
movl %eax, %r15d
jmp 0x2ed8
addl $0xa, %r15d
movq (%rsp), %rax
movq %rax, (%rsp)
testb $0x1, %al
jne 0x2fea
movl 0x8(%rsp), %esi
movl %ebx, %edi
leaq 0x40(%rsp), %rdx
movl 0x10(%rsp), %ecx
movq %r15, 0x20(%rsp)
movl %r15d, %r8d
callq 0x29dc
leaq 0x3285(%rip), %rdi      # 0x6190
leaq 0x3266(%rip), %rsi      # 0x6178
callq 0x31c8
movl 0x323b(%rip), %eax      # 0x6158
testl %eax, %eax
jle 0x2f96
movl $0x1, %r12d
xorl %r15d, %r15d
movl %r12d, 0x14(%rsp)
decl %eax
cmpq %rax, %r15
leal -0x1(%r14,%r12), %eax
cmovel %ebx, %eax
movl %eax, 0x1c(%rsp)
movl $0x1, 0x18(%rsp)
leaq 0x28(%rsp), %rdi
leaq -0x79d(%rip), %rsi      # 0x27b7
leaq 0x14(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x33b8
movq 0x3238(%rip), %rax      # 0x61a8
cmpq $0x0, (%rax,%r15,8)
jne 0x3022
movq 0x28(%rsp), %rcx
movq %rcx, (%rax,%r15,8)
incq %r15
movslq 0x31ca(%rip), %rax      # 0x6158
addl %r14d, %r12d
cmpq %rax, %r15
jl 0x2f2a
cmpl $0x0, 0x31bb(%rip)      # 0x6158
jle 0x2fc7
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x31fc(%rip), %rdi      # 0x61a8
addq %r15, %rdi
callq 0x2230
incq %r12
movslq 0x319a(%rip), %rax      # 0x6158
addq $0x8, %r15
cmpq %rax, %r12
jl 0x2fa5
leaq 0x31aa(%rip), %rdi      # 0x6178
leaq 0x31bb(%rip), %rsi      # 0x6190
callq 0x31c8
incl 0x10(%rsp)
movq 0x2feb(%rip), %r12      # 0x5fd0
movq 0x20(%rsp), %r15
imull $0x3e8, %r15d, %edi     # imm = 0x3E8
callq 0x21d0
jmp 0x2e17
movabsq $0x200000002, %rcx      # imm = 0x200000002
btq %rax, %rcx
movq (%rsp), %rax
jae 0x2edc
callq 0x21a0
callq 0x2260
jmp 0x2de4
callq 0x2050
nop