// File: /workspace/github_cpp_makefile_star2_O3/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 0x4a986
leaq 0x561a3(%rip), %rdi     # 0x77280
leaq 0x3c31e(%rip), %rsi     # 0x5d402
callq 0x6640
movq %rax, %rbx
movq %rax, %rdi
callq 0x6160
cmpb $0xa, -0x1(%rbx,%rax)
jne 0x21100
movb $0x0, -0x1(%rbx,%rax)
leaq 0x3c2fb(%rip), %rsi     # 0x5d402
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x6640
movq %rax, %r14
movq %rax, %rdi
callq 0x6160
cmpb $0xa, -0x1(%r14,%rax)
jne 0x21129
movb $0x0, -0x1(%r14,%rax)
movq %r14, %rdi
callq 0x6160
movq 0x54eb0(%rip), %r15     # 0x75fe8
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x6490
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x63c0
movsbl %al, %esi
movq %r15, %rdi
callq 0x6040
movq %rax, %rdi
callq 0x6290
leaq 0x3c28e(%rip), %rsi     # 0x5d402
xorl %edi, %edi
callq 0x6640
testq %rax, %rax
je 0x211a3
movq %rax, %r15
movq %rax, %rdi
callq 0x6160
cmpb $0xa, -0x1(%r15,%rax)
jne 0x21199
movb $0x0, -0x1(%r15,%rax)
movq %r15, %rdi
callq 0x6630
movl %eax, %ebx
movq %rsp, %rsi
movq %r14, %rdi
callq 0x4595b
testl %eax, %eax
je 0x211dd
leaq 0x3cb63(%rip), %rdx     # 0x5dd1c
leaq 0x40a0(%rsp), %rbx
movl $0x800, %esi            # imm = 0x800
movq %rbx, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x6680
movq %rbx, %rdi
xorl %esi, %esi
jmp 0x212f9
movl 0x3e94(%rsp), %edi
leaq 0x556c9(%rip), %rax     # 0x768b4
cmpl %edi, (%rax)
sete %al
leal -0x1(%rdi), %ecx
cmpl $0x13, %ecx
setae %cl
orb %al, %cl
jne 0x21211
callq 0x53d6c
callq 0x4a4db
callq 0x463bc
callq 0x58fe8
movq 0x56178(%rip), %rdi     # 0x77390
callq 0x543c1
callq 0x543a6
movq %rax, 0x56167(%rip)     # 0x77390
movq %rax, %rdi
callq 0x543d1
callq 0x4a849
callq 0x56a78
movsd 0x4098(%rsp), %xmm0
callq 0x54351
cmpl $0x0, 0x3e90(%rsp)
jle 0x2128a
xorl %r14d, %r14d
movq %rsp, %r15
movq %r15, %rdi
movl %r14d, %esi
callq 0x45930
movq 0x56125(%rip), %rdi     # 0x77390
movl 0x3210(%rsp,%r14,4), %edx
movl %eax, %esi
callq 0x55ae8
incq %r14
movslq 0x3e90(%rsp), %rax
cmpq %rax, %r14
jl 0x21259
movl 0x190c(%rsp), %edi
testl %edi, %edi
je 0x2129a
callq 0x56cd3
movl (%rsp), %ebp
movl 0x4(%rsp), %eax
leal -0x1(%rbx), %r15d
cmpl %eax, %ebx
cmovgl %eax, %r15d
testl %ebx, %ebx
cmovlel %eax, %r15d
testl %r15d, %r15d
jle 0x212df
xorl %ebx, %ebx
movq %rsp, %r14
movq %r14, %rdi
movl %ebx, %esi
callq 0x45908
movq 0x560c4(%rip), %rdi     # 0x77390
movl %eax, %esi
movl %ebp, %edx
callq 0x55ae8
xorl $0x3, %ebp
incl %ebx
cmpl %ebx, %r15d
jne 0x212bb
cmpl $0x1, %ebp
jne 0x212ed
leaq 0x3c9fc(%rip), %rdi     # 0x5dce7
jmp 0x212f4
leaq 0x3ca01(%rip), %rdi     # 0x5dcf5
movl $0x1, %esi
callq 0x21a05
addq $0x48a8, %rsp           # imm = 0x48A8
popq %rbx
popq %r14
popq %r15
popq %rbp
retq