// File: /workspace/github_cpp_makefile_star2_O3/kobanium[P]Ray/src/sgf/SgfExtractor.cpp
// Function: ExtractKifu(char const*, SGF_record_t*)
int
ExtractKifu( const char *file_name, SGF_record_t *kifu )
{
  FILE *fp;
  char sgf_text[100005], buffer[10000];
  int cursor = 0;
  
#if defined (_WIN32)
  errno_t err;

  if((err = fopen_s(&fp, file_name, "r")) != 0) {
    printf("Cannot open this file (%s)!!\n", file_name);
    return -1;
  } 
#else
  if((fp = fopen(file_name, "r")) == NULL) {
    printf("Cannot open this file (%s)!!\n", file_name);
    return -1;
  }
#endif  

  memset(sgf_text, 0, sizeof(char) * 100005);
  
  while (fgets(buffer, 10000, fp) != NULL) {
#if defined (_WIN32)
    strcat_s(sgf_text, 10000, buffer);
#else
    strncat(sgf_text, buffer, 10000);
#endif
  }
  fclose(fp);
  
  kifu->board_size = 19;
  kifu->result = R_UNKNOWN;
  kifu->moves = 0;
  kifu->handicaps = 0;
  kifu->handicap_stones = 0;
  kifu->komi = 0.0;
  kifu->start_color = S_BLACK;
  memset(kifu->move_x, 0, sizeof(kifu->move_x));
  memset(kifu->move_y, 0, sizeof(kifu->move_y));
  memset(kifu->black_name, 0, sizeof(kifu->black_name));
  memset(kifu->white_name, 0, sizeof(kifu->white_name));
  memset(kifu->handicap_x, 0, sizeof(kifu->handicap_x));
  memset(kifu->handicap_y, 0, sizeof(kifu->handicap_y));
  memset(kifu->handicap_color, 0, sizeof(kifu->handicap_color));
  
  while ((cursor < 100000) && (sgf_text[cursor] != '\0')) {
    if (IsSgfSpace(sgf_text[cursor]) ||
        sgf_text[cursor] == ';'  ||
        sgf_text[cursor] == '('  ||
        sgf_text[cursor] == ')') { 
      while (cursor < 100000 - 1) {
        cursor++;
        if (!IsSgfSpace(sgf_text[cursor]) &&
            sgf_text[cursor] != ';'  &&
            sgf_text[cursor] != '('  &&
            sgf_text[cursor] != ')') 
          break;
      }
    }    
    if (cursor >= 100000){
      break;
    }

    // GetSize             : 碁盤のサイズの抽出 
    // GetResult           : 結果の抽出 
    // GetHandicaps        : 置き石の個数の抽出 
    // GetHandicapPosition : 置き石の座標
    // GetMove             : 着手の抽出
    // GetKomi             : コミの抽出
    // GetPlayerName       : 対局者の名前の抽出
    if (strncmp(&sgf_text[cursor], "SZ[", 3) == 0) {
      cursor = GetSize(kifu, sgf_text, cursor);
    } else if (strncmp(&sgf_text[cursor], "RE[", 3) == 0) {
      cursor = GetResult(kifu, sgf_text, cursor);
    } else if (strncmp(&sgf_text[cursor], "HA[", 3) == 0) {
      cursor = GetHandicaps(kifu, sgf_text, cursor);
    } else if (strncmp(&sgf_text[cursor], "AB[", 3) == 0) {
      cursor = GetHandicapPosition(kifu, sgf_text, cursor, S_BLACK);
    } else if (strncmp(&sgf_text[cursor], "AW[", 3) == 0) {
      cursor = GetHandicapPosition(kifu, sgf_text, cursor, S_WHITE);
    } else if (strncmp(&sgf_text[cursor], "KM[", 3) == 0) {
      cursor = GetKomi(kifu, sgf_text, cursor);
    } else if (strncmp(&sgf_text[cursor], "PB[", 3) == 0) {
      cursor = GetPlayerName(kifu, sgf_text, cursor, S_BLACK);
    } else if (strncmp(&sgf_text[cursor], "PW[", 3) == 0) {
      cursor = GetPlayerName(kifu, sgf_text, cursor, S_WHITE);
    } else if (strncmp(&sgf_text[cursor],  "B[", 2) == 0) {
      cursor = GetMove(kifu, sgf_text, cursor);
    } else if (strncmp(&sgf_text[cursor],  "W[", 2) == 0) {
      cursor = GetMove(kifu, sgf_text, cursor);
    } else if ((strncmp(&sgf_text[cursor], "GM[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "FF[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "DT[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "PC[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "RU[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "CA[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "TM[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "OT[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "TB[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "TW[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "WR[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "BR[", 3) == 0)) {      
      // 無視するデータ
      cursor = SkipData(sgf_text, cursor);
    }
    cursor++;   // 文字を一つ進める
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1add8, %rsp          # imm = 0x1ADD8
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x18b0a(%rip), %rsi     # 0x5e483
callq 0x64d0
testq %rax, %rax
je 0x46020
movq %rax, %r14
leaq 0x10(%rsp), %rdi
xorl %r12d, %r12d
movl $0x186a5, %edx          # imm = 0x186A5
xorl %esi, %esi
callq 0x6200
leaq 0x186c0(%rsp), %rdi
movl $0x2710, %esi           # imm = 0x2710
movq %r14, %rdx
callq 0x6660
testq %rax, %rax
je 0x459ea
leaq 0x10(%rsp), %r15
leaq 0x186c0(%rsp), %r13
movl $0x2710, %edx           # imm = 0x2710
movq %r15, %rdi
movq %r13, %rsi
callq 0x6250
movq %r13, %rdi
movl $0x2710, %esi           # imm = 0x2710
movq %r14, %rdx
callq 0x6660
testq %rax, %rax
jne 0x459c5
movq %r14, %rdi
callq 0x63f0
movq $0x3, 0x1908(%rbx)
movabsq $0x1300000000, %rax     # imm = 0x1300000000
movq %rax, 0x3e90(%rbx)
movq $0x0, 0x4098(%rbx)
movq $0x1, (%rbx)
leaq 0x8(%rbx), %rdi
leaq 0x3e98(%rbx), %r14
leaq 0x1910(%rbx), %r15
movl $0x1900, %edx           # imm = 0x1900
xorl %esi, %esi
callq 0x6200
movl $0x2580, %edx           # imm = 0x2580
movq %r15, %rdi
xorl %esi, %esi
callq 0x6200
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
xorl %esi, %esi
callq 0x6200
leaq 0x13(%rsp), %r14
leaq 0x1a498(%rip), %rbp     # 0x5ff00
movabsq $0x800030100002600, %r15 # imm = 0x800030100002600
movslq %r12d, %rcx
movzbl 0x10(%rsp,%rcx), %eax
cmpq $0x3b, %rax
ja 0x45abc
btq %rax, %r15
jae 0x45cf9
cmpl $0x1869e, %r12d         # imm = 0x1869E
jg 0x45ab6
incl %r12d
movzbl 0x11(%rsp,%rcx), %eax
cmpq $0x3b, %rax
ja 0x45abc
btq %rax, %r15
jae 0x45abc
incq %rcx
incl %r12d
cmpq $0x1869f, %rcx          # imm = 0x1869F
jne 0x45a96
movl $0x1869f, %r12d         # imm = 0x1869F
movslq %r12d, %r13
movzbl 0x10(%rsp,%r13), %eax
leal -0x41(%rax), %ecx
cmpl $0x16, %ecx
ja 0x46007
movslq (%rbp,%rcx,4), %rcx
addq %rbp, %rcx
jmpq *%rcx
cmpb $0x5b, 0x11(%rsp,%r13)
jne 0x45c53
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
movl %r12d, %edx
callq 0x462e2
jmp 0x45d68
cmpb $0x5a, 0x11(%rsp,%r13)
jne 0x45db6
cmpb $0x5b, 0x12(%rsp,%r13)
jne 0x46007
movw $0x0, 0x8(%rsp)
movq $0x0, (%rsp)
movl $0x3, %r14d
cmpl $0x1869c, %r12d         # imm = 0x1869C
jg 0x45ea0
movl $0x186a0, %r14d         # imm = 0x186A0
subl %r12d, %r14d
movl %r12d, %ecx
movl $0x3, %eax
leal (%rcx,%rax), %edx
movslq %edx, %rdx
cmpb $0x5d, 0x10(%rsp,%rdx)
je 0x45e6c
incq %rax
cmpq %rax, %r14
jne 0x45b47
jmp 0x45e6f
cmpb $0x4d, 0x11(%rsp,%r13)
jne 0x45d70
cmpb $0x5b, 0x12(%rsp,%r13)
jne 0x45db6
movw $0x0, 0x8(%rsp)
movq $0x0, (%rsp)
movl $0x3, %r14d
cmpl $0x1869c, %r12d         # imm = 0x1869C
jg 0x45f14
movl $0x186a0, %r14d         # imm = 0x186A0
subl %r12d, %r14d
movl %r12d, %ecx
movl $0x3, %eax
leal (%rcx,%rax), %edx
movslq %edx, %rdx
cmpb $0x5d, 0x10(%rsp,%rdx)
je 0x45ee0
incq %rax
cmpq %rax, %r14
jne 0x45bb0
jmp 0x45ee3
cmpb $0x45, 0x11(%rsp,%r13)
jne 0x45d07
cmpb $0x5b, 0x12(%rsp,%r13)
jne 0x45d07
movl $0x3, %ecx
cmpl $0x1869c, %r12d         # imm = 0x1869C
jg 0x45fd8
movl $0x186a0, %ecx          # imm = 0x186A0
subl %r12d, %ecx
movl %r12d, %edx
movl $0x3, %eax
leal (%rdx,%rax), %esi
movslq %esi, %rsi
cmpb $0x5d, 0x10(%rsp,%rsi)
je 0x45f61
incq %rax
cmpq %rax, %rcx
jne 0x45c08
jmp 0x45f63
cmpb $0x42, 0x11(%rsp,%r13)
jne 0x45d18
cmpb $0x5b, 0x12(%rsp,%r13)
jne 0x45d18
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
movl %r12d, %edx
movl $0x1, %ecx
jmp 0x45d3c
cmpb $0x52, 0x11(%rsp,%r13)
je 0x45e1d
jmp 0x46007
cmpb $0x42, 0x11(%rsp,%r13)
jne 0x45d43
cmpb $0x5b, 0x12(%rsp,%r13)
jne 0x45d43
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
movl %r12d, %edx
movl $0x1, %ecx
jmp 0x45d63
cmpb $0x41, 0x11(%rsp,%r13)
jne 0x46007
cmpb $0x5b, 0x12(%rsp,%r13)
jne 0x45d70
movw $0x0, 0x8(%rsp)
movq $0x0, (%rsp)
cmpl $0x1869c, %r12d         # imm = 0x1869C
jg 0x45e63
movl $0x186a0, %eax          # imm = 0x186A0
subl %r12d, %eax
movl %r12d, %edx
movl $0x3, %ecx
leal (%rdx,%rcx), %esi
movslq %esi, %rsi
cmpb $0x5d, 0x10(%rsp,%rsi)
je 0x45f7f
incq %rcx
cmpq %rcx, %rax
jne 0x45cd5
movl $0x186a0, %r12d         # imm = 0x186A0
jmp 0x45f84
testq %rax, %rax
jne 0x45abc
jmp 0x46036
cmpb $0x55, 0x11(%rsp,%r13)
je 0x45dae
jmp 0x45db6
cmpb $0x57, 0x11(%rsp,%r13)
jne 0x45da6
cmpb $0x5b, 0x12(%rsp,%r13)
jne 0x45da6
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
movl %r12d, %edx
movl $0x2, %ecx
callq 0x4620e
jmp 0x45d68
cmpb $0x57, 0x11(%rsp,%r13)
jne 0x45d70
cmpb $0x5b, 0x12(%rsp,%r13)
jne 0x45d70
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
movl %r12d, %edx
movl $0x2, %ecx
callq 0x46048
movl %eax, %r12d
jmp 0x46007
leal -0x43(%rax), %ecx
cmpl $0x4, %ecx
ja 0x45dd6
leaq 0x1a1dd(%rip), %rdx     # 0x5ff5c
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
cmpb $0x46, 0x11(%rsp,%r13)
je 0x45e1d
jmp 0x46007
cmpb $0x4d, 0x11(%rsp,%r13)
je 0x45e1d
jmp 0x46007
cmpb $0x43, 0x11(%rsp,%r13)
jne 0x45db6
cmpb $0x5b, 0x12(%rsp,%r13)
je 0x45e29
cmpl $0x54, %eax
je 0x45de4
cmpl $0x4f, %eax
je 0x45e11
cmpl $0x43, %eax
jne 0x46007
cmpb $0x41, 0x11(%rsp,%r13)
je 0x45e1d
jmp 0x46007
cmpl $0x4f, %eax
je 0x45e11
cmpl $0x54, %eax
jne 0x46007
cmpb $0x4d, 0x11(%rsp,%r13)
jne 0x45df4
cmpb $0x5b, 0x12(%rsp,%r13)
je 0x45e29
cmpb $0x42, 0x11(%rsp,%r13)
jne 0x45e04
cmpb $0x5b, 0x12(%rsp,%r13)
je 0x45e29
cmpb $0x57, 0x11(%rsp,%r13)
je 0x45e1d
jmp 0x46007
cmpb $0x54, 0x11(%rsp,%r13)
jne 0x46007
cmpb $0x5b, 0x12(%rsp,%r13)
jne 0x46007
xorl %eax, %eax
cmpl $0x1869c, %r12d         # imm = 0x1869C
jg 0x46036
movl $0x186a0, %ecx          # imm = 0x186A0
subl %r12d, %ecx
addl $0x3, %r12d
addq $-0x3, %rcx
movslq %r12d, %rdx
cmpb $0x5d, 0x10(%rsp,%rdx)
je 0x46007
incl %r12d
decq %rcx
jne 0x45e48
jmp 0x46036
addl $0x3, %r12d
jmp 0x45faf
movl %eax, %r14d
cmpl $0x4, %r14d
jl 0x45ea0
leal -0x3(%r14), %eax
leaq 0x13(%rsp), %rcx
addq %r13, %rcx
xorl %edx, %edx
leaq (%rdx,%r13), %rsi
cmpq $0x1869c, %rsi          # imm = 0x1869C
jg 0x45e98
movb (%rcx,%rdx), %sil
movb %sil, (%rsp,%rdx)
incq %rdx
cmpq %rdx, %rax
jne 0x45e83
movq %rsp, %rdi
callq 0x6630
movl %eax, 0x3e94(%rbx)
addl %r14d, %r12d
cmpl $0x1869f, %r12d         # imm = 0x1869F
jg 0x45f57
movslq %r14d, %rax
addq %rax, %r13
movslq %r13d, %rax
cmpb $0x5d, 0x10(%rsp,%rax)
je 0x45f54
incq %r13
cmpq $0x1869f, %r13          # imm = 0x1869F
jle 0x45ec4
jmp 0x45f4c
movl %eax, %r14d
cmpl $0x4, %r14d
jl 0x45f14
leal -0x3(%r14), %eax
leaq 0x13(%rsp), %rcx
addq %r13, %rcx
xorl %edx, %edx
leaq (%rdx,%r13), %rsi
cmpq $0x1869c, %rsi          # imm = 0x1869C
jg 0x45f0c
movb (%rcx,%rdx), %sil
movb %sil, (%rsp,%rdx)
incq %rdx
cmpq %rdx, %rax
jne 0x45ef7
movq %rsp, %rdi
callq 0x6500
movsd %xmm0, 0x4098(%rbx)
addl %r14d, %r12d
cmpl $0x1869f, %r12d         # imm = 0x1869F
jg 0x45f57
movslq %r14d, %rax
addq %rax, %r13
movslq %r13d, %rax
cmpb $0x5d, 0x10(%rsp,%rax)
je 0x45f54
incq %r13
cmpq $0x1869f, %r13          # imm = 0x1869F
jle 0x45f36
movl $0x186a0, %r12d         # imm = 0x186A0
jmp 0x45f57
movl %r13d, %r12d
leaq 0x13(%rsp), %r14
jmp 0x46007
movl %eax, %ecx
movzbl 0x13(%rsp,%r13), %eax
cmpl $0x43, %eax
jg 0x45fbf
cmpl $0x30, %eax
je 0x45fd0
movl $0x1, %edx
cmpl $0x42, %eax
je 0x45fd2
jmp 0x45fd8
addl %ecx, %r12d
movl %ecx, %eax
cmpl $0x4, %eax
jl 0x45faf
addl $-0x3, %eax
leaq (%r14,%r13), %rcx
xorl %edx, %edx
leaq (%rdx,%r13), %rsi
cmpq $0x1869c, %rsi          # imm = 0x1869C
jg 0x45fa7
movb (%rcx,%rdx), %sil
movb %sil, (%rsp,%rdx)
incq %rdx
cmpq %rdx, %rax
jne 0x45f92
movq %rsp, %rdi
callq 0x6630
movl %eax, 0x190c(%rbx)
jmp 0x46007
cmpl $0x44, %eax
je 0x45fd0
cmpl $0x57, %eax
jne 0x45fd8
movl $0x2, %edx
jmp 0x45fd2
xorl %edx, %edx
movl %edx, 0x1908(%rbx)
addl %ecx, %r12d
xorl %eax, %eax
cmpl $0x1869f, %r12d         # imm = 0x1869F
jg 0x46036
movslq %ecx, %rcx
addq %rcx, %r13
movslq %r13d, %rcx
cmpb $0x5d, 0x10(%rsp,%rcx)
je 0x46004
incq %r13
cmpq $0x1869f, %r13          # imm = 0x1869F
jle 0x45fec
jmp 0x46036
movl %r13d, %r12d
leal 0x1(%r12), %eax
cmpl $0x1869f, %r12d         # imm = 0x1869F
movl %eax, %r12d
jl 0x45a72
xorl %eax, %eax
jmp 0x46036
leaq 0x19f49(%rip), %rdi     # 0x5ff70
movq %r15, %rsi
xorl %eax, %eax
callq 0x6060
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x1add8, %rsp          # imm = 0x1ADD8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq