// File: /workspace/github_cpp_makefile_star2_O2/Dalgona[P]mine/game.cpp
// Function: game::step(int, int)
int game::step(int cy, int cx)
{
  int count = 0;
  int i, j;
  int &cp = field[index(cy, cx)];

  if (newGame)
  {
    newGame = false;
    // Plant mines
    long double x = std::log(2.15561e+9);
    long double ratio = std::log(rows * cols) / x - 0.08101856l; // logarithmic
    //long double ratio = 0.000207502l * rows * cols + 0.106649146l; // linear
    nMines = (int)lround(ratio * rows * cols);
    std::random_device rd;
    std::uniform_int_distribution<int> dist(0, rows * cols - 1);
    int idx, c = 0;

    while (true)
    {
      idx = dist(rd);
      if (idx == index(cy, cx) || field[idx] & MF_MINE) continue;
      field[idx] |= MF_MINE;
      c++;
      if (c == nMines) break;
    }

    clear();
    tBegin = steady_clock::now();
  }

  if (cp & MF_DISP_UNKNOWN)
    if (cp & MF_MINE) return 0xFF; // When the player stepped on a mine
    else
    {
      for (i = cy - 1; i <= cy + 1; i++)
        for (j = cx - 1; j <= cx + 1; j++)
          if (i < 0 || i >= rows || j < 0 || j >= cols) continue;
          else if (field[index(i, j)] & MF_MINE) count++;
      if (count == 0)
      {
        cp &= ~MF_DISP_UNKNOWN;
        cp |= MF_DISP_EMPTY;
        for (i = cy - 1; i <= cy + 1; i++)
          for (j = cx - 1; j <= cx + 1; j++)
            if (i < 0 || i >= rows || j < 0 || j >= cols) continue;
            else step(i, j);
      }
      else
      {
        cp &= ~MF_DISP_UNKNOWN;
        cp |= MF_DISP_NUMBER;
        cp |= (count << 24);
      }
      return 0;
    }
  else return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x13d8, %rsp           # imm = 0x13D8
movl %edx, %ebx
movl %esi, %r14d
movq %rdi, %r15
movl 0x24(%rdi), %eax
movq 0x8(%rdi), %rcx
cmpb $0x1, 0x3c(%rdi)
jne 0x5105
movq %rcx, 0x8(%rsp)
movb $0x0, 0x3c(%r15)
movl 0x20(%r15), %edi
movl %eax, 0x10(%rsp)
imull %eax, %edi
callq 0x540d
movsd %xmm0, 0x40(%rsp)
fldl 0x40(%rsp)
fdivl 0x13cc(%rip)            # 0x6420
fldt 0x14a6(%rip)            # 0x6500
faddp %st, %st(1)
movl 0x20(%r15), %eax
movl 0x24(%r15), %ecx
movl %eax, 0x20(%rsp)
fimull 0x20(%rsp)
movl %ecx, 0x24(%rsp)
fimull 0x24(%rsp)
fstpl 0x38(%rsp)
movsd 0x38(%rsp), %xmm0
callq 0x2030
movl %eax, 0x30(%r15)
leaq 0x50(%rsp), %r12
movq %r12, %rdi
callq 0x5416
movl 0x24(%r15), %eax
imull 0x20(%r15), %eax
leaq 0x48(%rsp), %r13
andl $0x0, (%r13)
decl %eax
movl %eax, 0x4(%r13)
xorl %ebp, %ebp
movq %r13, %rdi
movq %r12, %rsi
callq 0x546c
movl 0x24(%r15), %ecx
imull %r14d, %ecx
addl %ebx, %ecx
cmpl %ecx, %eax
je 0x50af
cltq
movq 0x8(%r15), %rcx
movl (%rcx,%rax,4), %edx
testb $0x1, %dl
jne 0x50af
orl $0x1, %edx
movl %edx, (%rcx,%rax,4)
incl %ebp
cmpl 0x30(%r15), %ebp
jne 0x50af
callq 0x23c0
callq 0x22f0
movq %rax, 0x40(%r15)
leaq 0x50(%rsp), %rdi
callq 0x5474
movq 0x8(%rsp), %rcx
movl 0x10(%rsp), %eax
imull %r14d, %eax
addl %ebx, %eax
movslq %eax, %rdx
movl (%rcx,%rdx,4), %esi
btl $0x10, %esi
jb 0x511f
pushq $-0x1
popq %rax
jmp 0x51f5
movl $0xff, %eax
testb $0x1, %sil
jne 0x51f5
movl %esi, 0x4(%rsp)
movq %rdx, 0x28(%rsp)
movq %rcx, 0x8(%rsp)
leal -0x1(%r14), %ebp
incl %r14d
leal -0x1(%rbx), %eax
incl %ebx
movslq 0x20(%r15), %rsi
movslq 0x24(%r15), %rdi
movl %eax, 0x1c(%rsp)
cltq
movq %rax, 0x30(%rsp)
movslq %ebx, %r9
movslq %ebp, %r10
movslq %r14d, %rax
movq %rax, 0x10(%rsp)
movq %rdi, %r12
imulq %r10, %r12
shlq $0x2, %r12
addq 0x8(%r15), %r12
leaq (,%rdi,4), %rcx
xorl %eax, %eax
cmpq 0x10(%rsp), %r10
jg 0x51ce
movq 0x30(%rsp), %r8
cmpq %r9, %r8
jg 0x51c6
testq %r10, %r10
js 0x51c1
cmpq %rsi, %r10
setl %dl
testq %r8, %r8
setns %r13b
cmpq %rdi, %r8
setl %r11b
andb %dl, %r11b
andb %r13b, %r11b
cmpb $0x1, %r11b
jne 0x51c1
movl (%r12,%r8,4), %edx
andl $0x1, %edx
addl %edx, %eax
incq %r8
jmp 0x518e
incq %r10
addq %rcx, %r12
jmp 0x5182
testl %eax, %eax
je 0x5207
movl 0x4(%rsp), %ecx
andl $0xbffefffe, %ecx       # imm = 0xBFFEFFFE
shll $0x18, %eax
orl %ecx, %eax
orl $0x40000000, %eax       # imm = 0x40000000
movq 0x8(%rsp), %rcx
movq 0x28(%rsp), %rdx
movl %eax, (%rcx,%rdx,4)
xorl %eax, %eax
addq $0x13d8, %rsp           # imm = 0x13D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%rsp), %edx
andl $0xfffcfffe, %edx       # imm = 0xFFFCFFFE
orl $0x20000, %edx          # imm = 0x20000
movq 0x8(%rsp), %rax
movq 0x28(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
cmpl %r14d, %ebp
jg 0x51f3
movl 0x1c(%rsp), %r12d
cmpl %ebx, %r12d
jg 0x525a
testl %ebp, %ebp
js 0x5255
testl %r12d, %r12d
js 0x5255
cmpl 0x20(%r15), %ebp
jge 0x5255
cmpl 0x24(%r15), %r12d
jge 0x5255
movq %r15, %rdi
movl %ebp, %esi
movl %r12d, %edx
callq 0x5000
incl %r12d
jmp 0x522e
incl %ebp
jmp 0x5224
jmp 0x5260
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x5474
movq %rbx, %rdi
callq 0x23d0
nop