// File: kobanium[P]Ray/src/learn/PatternAnalyzer.cpp
// Function: AnalyzePattern()
void
AnalyzePattern( void )
{
  std::unique_ptr<int[]> md2_count(new int[MD2_MAX]);
  std::unique_ptr<hash_table_t> md3_count(new hash_table_t);
  std::unique_ptr<hash_table_t> md4_count(new hash_table_t);
  std::unique_ptr<hash_table_t> md5_count(new hash_table_t);
  game_info_t *game = AllocateGame(), *init_game = AllocateGame();
  pattern_hash_t hash_pat;
  SGF_record_t kifu;
  unsigned int md2_transp[16];
  unsigned int md2;
  unsigned int min_md2;


  std::cerr << "Start" << std::endl;

  std::fill_n(md2_count.get(), MD2_MAX, 0);
  ClearAllHashData(md3_count);
  ClearAllHashData(md4_count);
  ClearAllHashData(md5_count);

  InitializeBoard(init_game);

  for (int i = ANALYZE_KIFU_START; i <= ANALYZE_KIFU_END; i++) {
    int color = S_BLACK;
    std::ostringstream oss;

    CopyGame(game, init_game);

    oss << ANALYZE_KIFU_PATH << PATH_SEPARATOR << i << ".sgf";


    ExtractKifu(oss.str().c_str(), &kifu);

    std::cerr << oss.str() << ", " << kifu.moves << std::endl;

    for (int j = 0; j < kifu.moves; j++) {
      const int pos = GetKifuMove(&kifu, j);

      if (pos != PASS) {
        md2 = MD2(game->pat, pos);
        MD2Transpose16(md2, md2_transp);
        min_md2 = MD2_MAX;
        for (int k = 0; k < 16; k++) {
          if (min_md2 > md2_transp[k]) {
            min_md2 = md2_transp[k];
          }
        }
        md2_count[min_md2]++;

        PatternHash(&game->pat[pos], &hash_pat);
        Count(md3_count, hash_pat.list[MD_3], &game->pat[pos]);
        Count(md4_count, hash_pat.list[MD_4], &game->pat[pos]);
        Count(md5_count, hash_pat.list[MD_MAX + MD_5], &game->pat[pos]);
      }

      PutStone(game, pos, color);
      color = GetOppositeColor(color);
    }
  }

  OutputTargetPattern(md2_count, md3_count, md4_count, md5_count);

  FreeGame(game);
  FreeGame(init_game);

  std::cerr << "Finish" << std::endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x43a0, %rsp           # imm = 0x43A0
movl $0x4000000, %edi        # imm = 0x4000000
callq 0x7040
movq %rax, %rsi
leaq -0x8(%rbp), %rdi
callq 0x80730
movl $0x30000008, %edi       # imm = 0x30000008
callq 0x7550
movq %rax, -0x4328(%rbp)
jmp 0x7f594
movq -0x4328(%rbp), %rsi
leaq -0x10(%rbp), %rdi
callq 0x80760
movl $0x30000008, %edi       # imm = 0x30000008
callq 0x7550
movq %rax, -0x4330(%rbp)
jmp 0x7f5b7
movq -0x4330(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x80760
movl $0x30000008, %edi       # imm = 0x30000008
callq 0x7550
movq %rax, -0x4338(%rbp)
jmp 0x7f5da
movq -0x4338(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x80760
callq 0x9bce0
movq %rax, -0x4340(%rbp)
jmp 0x7f5f8
movq -0x4340(%rbp), %rax
movq %rax, -0x38(%rbp)
callq 0x9bce0
movq %rax, -0x4348(%rbp)
jmp 0x7f611
movq -0x4348(%rbp), %rax
movq %rax, -0x40(%rbp)
movq 0x679cd(%rip), %rdi     # 0xe6ff0
leaq 0x2f833(%rip), %rsi     # 0xaee5d
callq 0x7530
movq %rax, -0x4350(%rbp)
jmp 0x7f638
movq -0x4350(%rbp), %rdi
movq 0x67922(%rip), %rsi     # 0xe6f68
callq 0x75b0
jmp 0x7f64d
leaq -0x8(%rbp), %rdi
callq 0x807e0
movq %rax, %rdi
movl $0x0, -0x414c(%rbp)
movl $0x1000000, %esi        # imm = 0x1000000
leaq -0x414c(%rbp), %rdx
callq 0x80790
jmp 0x7f676
leaq -0x10(%rbp), %rdi
callq 0x7fb90
jmp 0x7f681
leaq -0x28(%rbp), %rdi
callq 0x7fb90
jmp 0x7f68c
leaq -0x30(%rbp), %rdi
callq 0x7fb90
jmp 0x7f697
movq -0x40(%rbp), %rdi
callq 0x9bd70
jmp 0x7f6a2
movl $0x1, -0x4150(%rbp)
cmpl $0x7530, -0x4150(%rbp)  # imm = 0x7530
jg 0x7fad0
movl $0x1, -0x4154(%rbp)
leaq -0x42d0(%rbp), %rdi
callq 0x7750
jmp 0x7f6d4
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x9c660
jmp 0x7f6e3
leaq 0x2f786(%rip), %rsi     # 0xaee70
leaq -0x42d0(%rbp), %rdi
callq 0x7530
movq %rax, -0x4358(%rbp)
jmp 0x7f6ff
movq -0x4358(%rbp), %rdi
leaq 0x6c0bb(%rip), %rsi     # 0xeb7c8
callq 0x7480
movq %rax, -0x4360(%rbp)
jmp 0x7f71b
movq -0x4360(%rbp), %rdi
movl -0x4150(%rbp), %esi
callq 0x7980
movq %rax, -0x4368(%rbp)
jmp 0x7f736
movq -0x4368(%rbp), %rdi
leaq 0x2e97f(%rip), %rsi     # 0xae0c3
callq 0x7530
jmp 0x7f74b
leaq -0x42f0(%rbp), %rdi
leaq -0x42d0(%rbp), %rsi
callq 0x7580
jmp 0x7f760
leaq -0x42f0(%rbp), %rdi
callq 0x71d0
movq %rax, %rdi
leaq -0x4100(%rbp), %rsi
callq 0x818f0
jmp 0x7f77d
leaq -0x42f0(%rbp), %rdi
callq 0x7ae8
leaq -0x4310(%rbp), %rdi
leaq -0x42d0(%rbp), %rsi
callq 0x7580
jmp 0x7f79e
movq 0x6784b(%rip), %rdi     # 0xe6ff0
leaq -0x4310(%rbp), %rsi
callq 0x7480
movq %rax, -0x4370(%rbp)
jmp 0x7f7ba
movq -0x4370(%rbp), %rdi
leaq 0x2d2dc(%rip), %rsi     # 0xacaa4
callq 0x7530
movq %rax, -0x4378(%rbp)
jmp 0x7f7d6
movq -0x4378(%rbp), %rdi
movl -0x40fc(%rbp), %esi
callq 0x7980
movq %rax, -0x4380(%rbp)
jmp 0x7f7f1
movq -0x4380(%rbp), %rdi
movq 0x67769(%rip), %rsi     # 0xe6f68
callq 0x75b0
jmp 0x7f806
leaq -0x4310(%rbp), %rdi
callq 0x7ae8
movl $0x0, -0x4314(%rbp)
movl -0x4314(%rbp), %eax
cmpl -0x40fc(%rbp), %eax
jge 0x7fa9f
movl -0x4314(%rbp), %esi
leaq -0x4100(%rbp), %rdi
callq 0x81830
movl %eax, -0x4384(%rbp)
jmp 0x7f848
movl -0x4384(%rbp), %eax
movl %eax, -0x4318(%rbp)
cmpl $0x0, -0x4318(%rbp)
je 0x7fa55
movq -0x38(%rbp), %rdi
addq $0x4748, %rdi           # imm = 0x4748
movl -0x4318(%rbp), %esi
callq 0x93d60
movl %eax, -0x4388(%rbp)
jmp 0x7f87f
movl -0x4388(%rbp), %eax
movl %eax, -0x4144(%rbp)
movl -0x4144(%rbp), %edi
leaq -0x4140(%rbp), %rsi
callq 0x93060
jmp 0x7f89f
movl $0x1000000, -0x4148(%rbp) # imm = 0x1000000
movl $0x0, -0x431c(%rbp)
cmpl $0x10, -0x431c(%rbp)
jge 0x7f998
movl -0x4148(%rbp), %eax
movslq -0x431c(%rbp), %rcx
cmpl -0x4140(%rbp,%rcx,4), %eax
jbe 0x7f982
movslq -0x431c(%rbp), %rax
movl -0x4140(%rbp,%rax,4), %eax
movl %eax, -0x4148(%rbp)
jmp 0x7f982
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x7fb76
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x7fb6d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x7fb64
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x7fb5b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x7fabf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x42f0(%rbp), %rdi
callq 0x7ae8
jmp 0x7fabf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x4310(%rbp), %rdi
callq 0x7ae8
jmp 0x7fabf
jmp 0x7f984
movl -0x431c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x431c(%rbp)
jmp 0x7f8b3
movl -0x4148(%rbp), %eax
movl %eax, %esi
leaq -0x8(%rbp), %rdi
callq 0x80810
movq %rax, -0x4390(%rbp)
jmp 0x7f9b2
movq -0x4390(%rbp), %rax
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
movq -0x38(%rbp), %rax
movslq -0x4318(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq 0x4748(%rax,%rcx,8), %rdi
leaq -0x60(%rbp), %rsi
callq 0x97cc0
jmp 0x7f9e1
movq -0x58(%rbp), %rsi
movq -0x38(%rbp), %rax
movslq -0x4318(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq 0x4748(%rax,%rcx,8), %rdx
leaq -0x10(%rbp), %rdi
callq 0x7fc90
jmp 0x7fa07
movq -0x50(%rbp), %rsi
movq -0x38(%rbp), %rax
movslq -0x4318(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq 0x4748(%rax,%rcx,8), %rdx
leaq -0x28(%rbp), %rdi
callq 0x7fc90
jmp 0x7fa2d
movq -0x48(%rbp), %rsi
movq -0x38(%rbp), %rax
movslq -0x4318(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq 0x4748(%rax,%rcx,8), %rdx
leaq -0x30(%rbp), %rdi
callq 0x7fc90
jmp 0x7fa53
jmp 0x7fa55
movq -0x38(%rbp), %rdi
movl -0x4318(%rbp), %esi
movl -0x4154(%rbp), %edx
callq 0x9dbc0
jmp 0x7fa6c
movl -0x4154(%rbp), %edi
callq 0x25810
movl %eax, -0x4394(%rbp)
jmp 0x7fa7f
movl -0x4394(%rbp), %eax
movl %eax, -0x4154(%rbp)
movl -0x4314(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4314(%rbp)
jmp 0x7f81c
leaq -0x42d0(%rbp), %rdi
callq 0x7110
movl -0x4150(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4150(%rbp)
jmp 0x7f6ac
leaq -0x42d0(%rbp), %rdi
callq 0x7110
jmp 0x7fb5b
leaq -0x8(%rbp), %rdi
leaq -0x10(%rbp), %rsi
leaq -0x28(%rbp), %rdx
leaq -0x30(%rbp), %rcx
callq 0x7fd10
jmp 0x7fae7
movq -0x38(%rbp), %rdi
callq 0x9bd30
jmp 0x7faf2
movq -0x40(%rbp), %rdi
callq 0x9bd30
jmp 0x7fafd
movq 0x674ec(%rip), %rdi     # 0xe6ff0
leaq 0x2f358(%rip), %rsi     # 0xaee63
callq 0x7530
movq %rax, -0x43a0(%rbp)
jmp 0x7fb19
movq -0x43a0(%rbp), %rdi
movq 0x67441(%rip), %rsi     # 0xe6f68
callq 0x75b0
jmp 0x7fb2e
leaq -0x30(%rbp), %rdi
callq 0x80850
leaq -0x28(%rbp), %rdi
callq 0x80850
leaq -0x10(%rbp), %rdi
callq 0x80850
leaq -0x8(%rbp), %rdi
callq 0x808b0
addq $0x43a0, %rsp           # imm = 0x43A0
popq %rbp
retq
leaq -0x30(%rbp), %rdi
callq 0x80850
leaq -0x28(%rbp), %rdi
callq 0x80850
leaq -0x10(%rbp), %rdi
callq 0x80850
leaq -0x8(%rbp), %rdi
callq 0x808b0
movq -0x18(%rbp), %rdi
callq 0x79b0
nopl (%rax,%rax)