// File: /workspace/github_cpp_makefile_star2_O2/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 $0xd8, %rsp
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x4205(%rip), %rsi      # 0x6cd0
pushq $0x10
popq %rcx
leaq 0x50(%rsp), %r14
movq %r14, %rdi
rep  movsq (%rsi), %es:(%rdi)
xorl %r13d, %r13d
leaq 0x1623(%rip), %r15      # 0x4106
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 0x2210
movb $0x1, %cl
cmpl $0x67, %eax
je 0x2ae9
cmpl $0x73, %eax
jne 0x2b10
movb $0x1, %r13b
jmp 0x2ae6
cmpl $-0x1, %eax
jne 0x2b6e
leaq 0x15b2(%rip), %rsi      # 0x40ce
pushq $0x6
popq %rdi
callq 0x2120
callq 0x20e0
callq 0x2190
callq 0x2270
callq 0x2060
movq 0x4491(%rip), %rbx      # 0x6fd0
movq (%rbx), %rdi
pushq $0x1
popq %rbp
movl %ebp, %esi
callq 0x2080
xorl %edi, %edi
callq 0x2100
movq (%rbx), %rdi
movl %ebp, %esi
callq 0x20c0
movq (%rbx), %rax
testq %rax, %rax
jne 0x2b73
pushq $-0x1
popq %rbx
pushq $-0x3
popq %r14
jmp 0x2b89
callq 0x242c
movswl 0x4(%rax), %r14d
decl %r14d
movswl 0x6(%rax), %eax
decl %eax
pushq $0x2
popq %rcx
cltd
idivl %ecx
movl %eax, %ebx
movl %ebx, (%rsp)
movslq 0x45bd(%rip), %rsi      # 0x7150
movl %r14d, %eax
cltd
idivl %esi
movl %eax, %r15d
leaq 0x30(%rsp), %rbp
leaq 0x28(%rsp), %rdx
movq %rbp, %rdi
callq 0x2ff4
leaq 0x45eb(%rip), %rdi      # 0x71a0
movq %rbp, %rsi
callq 0x37fa
movq %rbp, %rdi
callq 0x2e48
movaps 0x1444(%rip), %xmm0     # 0x4010
movups %xmm0, 0xd(%rbp)
movaps %xmm0, (%rbp)
movb $0x0, 0x1c(%rbp)
cmpl $0xb, %r14d
setge %al
andb %al, %r12b
cmpb $0x1, %r12b
jne 0x2c00
cmpl $0x26, %ebx
jl 0x2c00
movl %r14d, %edi
movl %ebx, %esi
callq 0x2552
movq 0x43d2(%rip), %r12      # 0x6fd0
jmp 0x2c32
cmpl $0x18, %r14d
setge %al
andb %al, %r13b
cmpb $0x1, %r13b
movq 0x43bb(%rip), %r12      # 0x6fd0
jne 0x2c28
cmpl $0x23, %ebx
jl 0x2c28
movl %r14d, %edi
movl %ebx, %esi
callq 0x261c
jmp 0x2c32
movl %r14d, %edi
movl %ebx, %esi
callq 0x24cb
leal 0x1(%r14), %eax
movl %eax, 0x4(%rsp)
pushq $0x64
popq %r13
leaq 0x28(%rsp), %rbx
movl $0x0, 0x8(%rsp)
xorl %ebp, %ebp
movq (%r12), %rdi
callq 0x21b0
leal -0x50(%rax), %ecx
cmpl $0x22, %ecx
ja 0x2c9f
movabsq $0x100000001, %rdx      # imm = 0x100000001
btq %rcx, %rdx
jb 0x2cb4
movabsq $0x400000004, %rdx      # imm = 0x400000004
btq %rcx, %rdx
jae 0x2c8b
movl (%rsp), %esi
movl %r14d, %edi
callq 0x24cb
jmp 0x2cfb
movabsq $0x200000002, %rdx      # imm = 0x200000002
btq %rcx, %rdx
jb 0x2e20
cmpl $0x104, %eax            # imm = 0x104
je 0x2cec
cmpl $0x105, %eax            # imm = 0x105
je 0x2ce6
cmpl $0x191, %eax            # imm = 0x191
jne 0x2cfb
movq %rbp, %rax
xorb $0x1, %bpl
testb $0x1, %al
je 0x2ccf
movl 0x4(%rsp), %edi
pushq $0x3
popq %rsi
leaq 0x1459(%rip), %rdx      # 0x4126
jmp 0x2cdd
movl 0x4(%rsp), %edi
pushq $0x3
popq %rsi
leaq 0x143e(%rip), %rdx      # 0x411b
xorl %eax, %eax
callq 0x2260
jmp 0x2cfb
addl $0xa, %r13d
jmp 0x2cfb
leal -0xa(%r13), %eax
cmpl $0xa, %r13d
cmovll %r13d, %eax
movl %eax, %r13d
testb $0x1, %bpl
jne 0x2e0f
movq %rbp, 0x18(%rsp)
movl (%rsp), %esi
movl %r14d, %edi
leaq 0x30(%rsp), %rdx
movl 0x8(%rsp), %ecx
movq %r13, 0x20(%rsp)
movl %r13d, %r8d
callq 0x28b4
leaq 0x445b(%rip), %rdi      # 0x7188
leaq 0x443c(%rip), %rsi      # 0x7170
callq 0x2f16
xorl %r12d, %r12d
pushq $0x1
popq %rax
movl %eax, %ecx
xorl %edx, %edx
movslq 0x4406(%rip), %rax      # 0x7150
cmpq %rax, %rdx
jge 0x2dbc
movl %ecx, 0x14(%rsp)
decl %eax
leaq 0x1(%rdx), %rbp
leal (%r15,%rcx), %r13d
cmpq %rax, %rdx
leal -0x1(%r15,%rcx), %eax
cmovel %r14d, %eax
movl %eax, 0x10(%rsp)
movl $0x1, 0xc(%rsp)
movq %rbx, %rdi
leaq -0x684(%rip), %rsi      # 0x26fb
leaq 0x14(%rsp), %rdx
leaq 0x10(%rsp), %rcx
leaq 0xc(%rsp), %r8
movq %rsp, %r9
callq 0x3046
movq 0x4403(%rip), %rdi      # 0x71a0
addq %r12, %rdi
movq %rbx, %rsi
callq 0x30de
movq %rbx, %rdi
callq 0x30f8
addq $0x8, %r12
movl %r13d, %ecx
movq %rbp, %rdx
jmp 0x2d43
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x18(%rsp), %rbp
cltq
cmpq %rax, %r13
jge 0x2dec
movq 0x43cb(%rip), %rdi      # 0x71a0
addq %r12, %rdi
callq 0x2220
incq %r13
movl 0x436a(%rip), %eax      # 0x7150
addq $0x8, %r12
jmp 0x2dc7
leaq 0x437d(%rip), %rdi      # 0x7170
leaq 0x438e(%rip), %rsi      # 0x7188
callq 0x2f16
incl 0x8(%rsp)
movq 0x41c6(%rip), %r12      # 0x6fd0
movq 0x20(%rsp), %r13
imull $0x3e8, %r13d, %edi     # imm = 0x3E8
callq 0x21c0
jmp 0x2c4d
callq 0x2a9c
nop