// File: /workspace/github_cpp_makefile_star2_O2/kobanium[P]Ray/src/gtp/Gtp.cpp
// Function: GTP_loadsgf()
static void
GTP_loadsgf( void )
{
  SGF_record_t sgf;
  char *command, *filename, *move;
  int pos, color, size, target_move = 0;

  StopPondering();

  // コマンドの抽出
  command = STRTOK(input_copy, DELIM, &next_token);
  CHOMP(command);

  // ファイル名の抽出
  filename = STRTOK(NULL, DELIM, &next_token);
  CHOMP(filename);
  std::cerr << filename << std::endl;

  // 着手数の抽出
  move = STRTOK(NULL, DELIM, &next_token);
  if (move != NULL) {
    CHOMP(move);
    target_move = atoi(move);
  }

  // 棋譜の読み込み
  if (ExtractKifu(filename, &sgf) != 0) {
    char errmsg[2048];
#if defined (_WIN32)
    sprintf_s(errmsg, 2048, "cannot read \"%s\"", filename);
#else
    snprintf(errmsg, 2048, "cannot read \"%s\"", filename);
#endif
    GTP_response(errmsg, false);
    return;
  }

  // 碁盤のサイズを設定
  size = sgf.board_size;

  // 碁盤の初期化処理
  if (pure_board_size != size &&
      size <= PURE_BOARD_SIZE && size > 0) {
    SetBoardSize(size);
    SetParameter();
    SetNeighbor();
    InitializeNakadeHash();
  }
  FreeGame(game);
  game = AllocateGame();
  InitializeBoard(game);
  InitializeSearchSetting();
  InitializeUctHash();
  SetKomi(sgf.komi);

  // あらかじめ置いてある石を配置
  for (int i = 0; i < sgf.handicap_stones; i++) {
    pos = GetHandicapStone(&sgf, i);
    PutStone(game, pos, sgf.handicap_color[i]);
  }
 
  // 置き石の個数の設定
  if (sgf.handicaps != 0) {
    SetHandicapNum(sgf.handicaps);
  }

  color = sgf.start_color;

  if (target_move < 1 || target_move > sgf.moves) {
    target_move = sgf.moves;
  } else {
    target_move--;
  }

  // 石を配置
  for (int i = 0; i < target_move; i++) {
    pos = GetKifuMove(&sgf, i);
    PutStone(game, pos, color);
    color = GetOppositeColor(color);
  }

  if (color == S_BLACK) {
    GTP_response("black", true);
  } else {
    GTP_response("white", true);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x48a8, %rsp           # imm = 0x48A8
callq 0x32ecd
leaq 0x50006(%rip), %rdi     # 0x62280
leaq 0x32951(%rip), %rsi     # 0x44bd2
callq 0x6610
movq %rax, %rbx
movq %rax, %rdi
callq 0x6150
cmpb $0xa, -0x1(%rbx,%rax)
jne 0x1229d
movb $0x0, -0x1(%rbx,%rax)
leaq 0x3292e(%rip), %rsi     # 0x44bd2
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x6610
movq %rax, %r14
movq %rax, %rdi
callq 0x6150
cmpb $0xa, -0x1(%r14,%rax)
jne 0x122c6
movb $0x0, -0x1(%r14,%rax)
movq 0x4ed23(%rip), %rdi     # 0x60ff0
movq %r14, %rsi
callq 0x63c0
movq %rax, %rdi
callq 0x6190
leaq 0x328ee(%rip), %rsi     # 0x44bd2
xorl %edi, %edi
callq 0x6610
testq %rax, %rax
je 0x12313
movq %rax, %r15
movq %rax, %rdi
callq 0x6150
cmpb $0xa, -0x1(%r15,%rax)
jne 0x12309
movb $0x0, -0x1(%r15,%rax)
movq %r15, %rdi
callq 0x6600
movl %eax, %ebx
leaq 0x808(%rsp), %rsi
movq %r14, %rdi
callq 0x2ee83
testl %eax, %eax
je 0x1234d
leaq 0x3283c(%rip), %rdx     # 0x44b6a
movq %rsp, %rbx
movl $0x800, %esi            # imm = 0x800
movq %rbx, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x6660
movq %rbx, %rdi
xorl %esi, %esi
jmp 0x1246d
movl 0x469c(%rsp), %edi
leaq 0x4f559(%rip), %rax     # 0x618b4
cmpl %edi, (%rax)
sete %al
leal -0x1(%rdi), %ecx
cmpl $0x13, %ecx
setae %cl
orb %al, %cl
jne 0x12381
callq 0x3ac8c
callq 0x32c3f
callq 0x2f7b0
callq 0x3f508
movq 0x50008(%rip), %rdi     # 0x62390
callq 0x3b163
callq 0x3b148
movq %rax, 0x4fff7(%rip)     # 0x62390
movq %rax, %rdi
callq 0x3b173
callq 0x32df4
callq 0x3d090
leaq 0x808(%rsp), %r14
movsd 0x4098(%r14), %xmm0
callq 0x3b0f3
xorl %r15d, %r15d
movslq 0x4698(%rsp), %rax
cmpq %rax, %r15
jge 0x123f7
movq %r14, %rdi
movl %r15d, %esi
callq 0x2ee58
movq 0x4ffad(%rip), %rdi     # 0x62390
movl 0x3a18(%rsp,%r15,4), %edx
movl %eax, %esi
callq 0x3c2f8
incq %r15
jmp 0x123c4
movl 0x2114(%rsp), %edi
testl %edi, %edi
je 0x12407
callq 0x3d2b3
leaq 0x808(%rsp), %r14
movl 0x4(%r14), %eax
leal -0x1(%rbx), %r15d
cmpl %eax, %ebx
cmovgl %eax, %r15d
movl (%r14), %ebp
testl %ebx, %ebx
cmovlel %eax, %r15d
xorl %ebx, %ebx
testl %r15d, %r15d
cmovlel %ebx, %r15d
cmpl %ebx, %r15d
je 0x12455
movq %r14, %rdi
movl %ebx, %esi
callq 0x2ee30
movq 0x4ff4b(%rip), %rdi     # 0x62390
movl %eax, %esi
movl %ebp, %edx
callq 0x3c2f8
xorl $0x3, %ebp
incl %ebx
jmp 0x1242f
cmpl $0x1, %ebp
jne 0x12463
leaq 0x326d4(%rip), %rdi     # 0x44b35
jmp 0x1246a
leaq 0x326d9(%rip), %rdi     # 0x44b43
pushq $0x1
popq %rsi
callq 0x12a66
addq $0x48a8, %rsp           # imm = 0x48A8
popq %rbx
popq %r14
popq %r15
popq %rbp
retq