// File: /workspace/github_cpp_makefile_star2_O1/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 0x4a61e
leaq 0x54fc4(%rip), %rdi     # 0x76280
leaq 0x3c13f(%rip), %rsi     # 0x5d402
callq 0x6650
movq %rax, %rbx
movq %rax, %rdi
callq 0x6160
cmpb $0xa, -0x1(%rbx,%rax)
jne 0x212df
movb $0x0, -0x1(%rbx,%rax)
leaq 0x3c11c(%rip), %rsi     # 0x5d402
xorl %edi, %edi
callq 0x6650
movq %rax, %r14
movq %rax, %rdi
callq 0x6160
cmpb $0xa, -0x1(%r14,%rax)
jne 0x21306
movb $0x0, -0x1(%r14,%rax)
testq %r14, %r14
je 0x21327
movq %r14, %rdi
callq 0x6160
movq 0x53cce(%rip), %rdi     # 0x74fe8
movq %r14, %rsi
movq %rax, %rdx
callq 0x64a0
jmp 0x21345
movq 0x53cba(%rip), %rax     # 0x74fe8
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
leaq (%rax,%rcx), %rdi
movl 0x20(%rax,%rcx), %esi
orl $0x1, %esi
callq 0x6710
movq 0x53c9c(%rip), %rbx     # 0x74fe8
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x63d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x6040
movq %rax, %rdi
callq 0x62a0
leaq 0x3c088(%rip), %rsi     # 0x5d402
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x6650
testq %rax, %rax
je 0x213ab
movq %rax, %r15
movq %rax, %rdi
callq 0x6160
cmpb $0xa, -0x1(%r15,%rax)
jne 0x213a1
movb $0x0, -0x1(%r15,%rax)
movq %r15, %rdi
callq 0x6640
movl %eax, %ebx
movq %rsp, %rsi
movq %r14, %rdi
callq 0x4562f
testl %eax, %eax
je 0x213e5
leaq 0x3c95b(%rip), %rdx     # 0x5dd1c
leaq 0x40a0(%rsp), %rbx
movl $0x800, %esi            # imm = 0x800
movq %rbx, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x6690
movq %rbx, %rdi
xorl %esi, %esi
jmp 0x21501
movl 0x3e94(%rsp), %edi
leaq 0x544c1(%rip), %rax     # 0x758b4
cmpl %edi, (%rax)
sete %al
leal -0x1(%rdi), %ecx
cmpl $0x13, %ecx
setae %cl
orb %al, %cl
jne 0x21419
callq 0x5395c
callq 0x4a25f
callq 0x46178
callq 0x586c0
movq 0x54f70(%rip), %rdi     # 0x76390
callq 0x53e7b
callq 0x53e60
movq %rax, 0x54f5f(%rip)     # 0x76390
movq %rax, %rdi
callq 0x53e8b
callq 0x4a4e1
callq 0x560d0
movsd 0x4098(%rsp), %xmm0
callq 0x53df3
cmpl $0x0, 0x3e90(%rsp)
jle 0x21492
xorl %r14d, %r14d
movq %rsp, %r15
movq %r15, %rdi
movl %r14d, %esi
callq 0x45604
movq 0x54f1d(%rip), %rdi     # 0x76390
movl 0x3210(%rsp,%r14,4), %edx
movl %eax, %esi
callq 0x55134
incq %r14
movslq 0x3e90(%rsp), %rax
cmpq %rax, %r14
jl 0x21461
movl 0x190c(%rsp), %edi
testl %edi, %edi
je 0x214a2
callq 0x5631b
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 0x214e7
xorl %ebx, %ebx
movq %rsp, %r14
movq %r14, %rdi
movl %ebx, %esi
callq 0x455dc
movq 0x54ebc(%rip), %rdi     # 0x76390
movl %eax, %esi
movl %ebp, %edx
callq 0x55134
xorl $0x3, %ebp
incl %ebx
cmpl %ebx, %r15d
jne 0x214c3
cmpl $0x1, %ebp
jne 0x214f5
leaq 0x3c7f4(%rip), %rdi     # 0x5dce7
jmp 0x214fc
leaq 0x3c7f9(%rip), %rdi     # 0x5dcf5
movl $0x1, %esi
callq 0x21c1a
addq $0x48a8, %rsp           # imm = 0x48A8
popq %rbx
popq %r14
popq %r15
popq %rbp
retq