// File: /workspace/github_cpp_makefile_star2_O3/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), %edx
movq 0x8(%rdi), %rcx
cmpb $0x1, 0x3c(%rdi)
jne 0x585b
movq %rcx, 0x10(%rsp)
movb $0x0, 0x3c(%r15)
movl 0x20(%r15), %eax
movl %edx, 0x18(%rsp)
imull %edx, %eax
cvtsi2sd %eax, %xmm0
callq 0x2180
movsd %xmm0, 0x40(%rsp)
fldl 0x40(%rsp)
fdivl 0x1cf4(%rip)            # 0x7498
fldt 0x1cd6(%rip)            # 0x7480
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 0x5baa
movl 0x24(%r15), %eax
imull 0x20(%r15), %eax
decl %eax
leaq 0x48(%rsp), %r13
movl $0x0, (%r13)
movl %eax, 0x4(%r13)
xorl %ebp, %ebp
movq %r13, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x5c30
movl 0x24(%r15), %ecx
imull %r14d, %ecx
addl %ebx, %ecx
cmpl %ecx, %eax
je 0x5802
cltq
movq 0x8(%r15), %rcx
movl (%rcx,%rax,4), %edx
testb $0x1, %dl
jne 0x5802
orl $0x1, %edx
movl %edx, (%rcx,%rax,4)
incl %ebp
cmpl 0x30(%r15), %ebp
jne 0x5802
callq 0x23b0
callq 0x22f0
movq %rax, 0x40(%r15)
leaq 0x50(%rsp), %rdi
callq 0x21d0
movq 0x10(%rsp), %rcx
movl 0x18(%rsp), %edx
imull %r14d, %edx
addl %ebx, %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %esi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
btl $0x10, %esi
jae 0x59a1
movl $0xff, %eax
testb $0x1, %sil
jne 0x59a1
movl %esi, 0xc(%rsp)
movq %rdx, 0x28(%rsp)
movq %rcx, 0x10(%rsp)
leal -0x1(%r14), %r12d
leal -0x1(%rbx), %eax
movslq 0x20(%r15), %rsi
movslq 0x24(%r15), %rdi
cltq
movq %rax, 0x18(%rsp)
movslq %ebx, %r9
movslq %r12d, %r13
movslq %r14d, %rax
movq %rax, 0x30(%rsp)
movq %rdi, %r11
imulq %r13, %r11
shlq $0x2, %r11
addq 0x8(%r15), %r11
leaq (,%rdi,4), %rbp
xorl %eax, %eax
movq 0x18(%rsp), %r8
testq %r13, %r13
js 0x5902
cmpq %rsi, %r13
setl %dl
testq %r8, %r8
setns %cl
cmpq %rdi, %r8
setl %r10b
andb %dl, %r10b
andb %cl, %r10b
cmpb $0x1, %r10b
jne 0x5902
movl (%r11,%r8,4), %ecx
andl $0x1, %ecx
addl %ecx, %eax
leaq 0x1(%r8), %rcx
cmpq %r9, %r8
movq %rcx, %r8
jle 0x58d5
leaq 0x1(%r13), %rcx
addq %rbp, %r11
cmpq 0x30(%rsp), %r13
movq %rcx, %r13
jle 0x58d0
testl %eax, %eax
je 0x5946
movl 0xc(%rsp), %ecx
andl $0xbffefffe, %ecx       # imm = 0xBFFEFFFE
shll $0x18, %eax
orl %ecx, %eax
orl $0x40000000, %eax       # imm = 0x40000000
movq 0x10(%rsp), %rcx
movq 0x28(%rsp), %rdx
movl %eax, (%rcx,%rdx,4)
jmp 0x599f
movl 0xc(%rsp), %edx
andl $0xfffcfffe, %edx       # imm = 0xFFFCFFFE
orl $0x20000, %edx          # imm = 0x20000
movq 0x10(%rsp), %rax
movq 0x28(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
leal -0x2(%rbx), %r13d
movl %r13d, %ebp
incl %ebp
testl %r12d, %r12d
js 0x598e
testl %ebp, %ebp
js 0x598e
cmpl 0x20(%r15), %r12d
jge 0x598e
cmpl 0x24(%r15), %ebp
jge 0x598e
movq %r15, %rdi
movl %r12d, %esi
movl %ebp, %edx
callq 0x574c
cmpl %ebx, %ebp
jle 0x596a
leal 0x1(%r12), %eax
cmpl %r14d, %r12d
movl %eax, %r12d
jle 0x5967
xorl %eax, %eax
addq $0x13d8, %rsp           # imm = 0x13D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x59cc
jmp 0x59b7
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x21d0
movq %rbx, %rdi
callq 0x23c0
movq %rax, %rdi
callq 0x4729