// File: 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
movq %rsp, %rbp
subq $0x4910, %rsp           # imm = 0x4910
movl $0x0, -0x40c8(%rbp)
callq 0x892c0
leaq 0xc10cf(%rip), %rdi     # 0xe8410
leaq 0x85858(%rip), %rsi     # 0xacba0
callq 0x7850
movq %rax, -0x40a8(%rbp)
movq -0x40a8(%rbp), %rax
movq %rax, -0x48e0(%rbp)
movq -0x40a8(%rbp), %rdi
callq 0x7200
movq %rax, %rcx
movq -0x48e0(%rbp), %rax
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x273b1
movq -0x40a8(%rbp), %rax
movq %rax, -0x48e8(%rbp)
movq -0x40a8(%rbp), %rdi
callq 0x7200
movq %rax, %rcx
movq -0x48e8(%rbp), %rax
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
xorl %eax, %eax
movl %eax, %edi
leaq 0x857e4(%rip), %rsi     # 0xacba0
callq 0x7850
movq %rax, -0x40b0(%rbp)
movq -0x40b0(%rbp), %rax
movq %rax, -0x48f0(%rbp)
movq -0x40b0(%rbp), %rdi
callq 0x7200
movq %rax, %rcx
movq -0x48f0(%rbp), %rax
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x27425
movq -0x40b0(%rbp), %rax
movq %rax, -0x48f8(%rbp)
movq -0x40b0(%rbp), %rdi
callq 0x7200
movq %rax, %rcx
movq -0x48f8(%rbp), %rax
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
movq -0x40b0(%rbp), %rsi
movq 0xbfbbd(%rip), %rdi     # 0xe6ff0
callq 0x7530
movq %rax, %rdi
movq 0xbfb26(%rip), %rsi     # 0xe6f68
callq 0x75b0
xorl %eax, %eax
movl %eax, %edi
leaq 0x8574e(%rip), %rsi     # 0xacba0
callq 0x7850
movq %rax, -0x40b8(%rbp)
cmpq $0x0, -0x40b8(%rbp)
je 0x274d7
movq -0x40b8(%rbp), %rax
movq %rax, -0x4900(%rbp)
movq -0x40b8(%rbp), %rdi
callq 0x7200
movq %rax, %rcx
movq -0x4900(%rbp), %rax
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x274c5
movq -0x40b8(%rbp), %rax
movq %rax, -0x4908(%rbp)
movq -0x40b8(%rbp), %rdi
callq 0x7200
movq %rax, %rcx
movq -0x4908(%rbp), %rax
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
movq -0x40b8(%rbp), %rdi
callq 0x7840
movl %eax, -0x40c8(%rbp)
movq -0x40b0(%rbp), %rdi
leaq -0x40a0(%rbp), %rsi
callq 0x818f0
cmpl $0x0, %eax
je 0x27523
leaq -0x48d0(%rbp), %rdi
movq -0x40b0(%rbp), %rcx
movl $0x800, %esi            # imm = 0x800
leaq 0x8565b(%rip), %rdx     # 0xacb64
movb $0x0, %al
callq 0x78a0
leaq -0x48d0(%rbp), %rdi
xorl %esi, %esi
callq 0x27f30
jmp 0x276f0
movl -0x20c(%rbp), %eax
movl %eax, -0x40c4(%rbp)
leaq 0xc050e(%rip), %rax     # 0xe7a44
movl (%rax), %eax
cmpl -0x40c4(%rbp), %eax
je 0x2756c
cmpl $0x13, -0x40c4(%rbp)
jg 0x2756c
cmpl $0x0, -0x40c4(%rbp)
jle 0x2756c
movl -0x40c4(%rbp), %edi
callq 0x9b560
callq 0x88eb0
callq 0x82c60
callq 0xa3920
movq 0xc0fad(%rip), %rdi     # 0xe8520
callq 0x9bd30
callq 0x9bce0
movq %rax, 0xc0f9c(%rip)     # 0xe8520
movq 0xc0f95(%rip), %rdi     # 0xe8520
callq 0x9bd70
callq 0x89180
callq 0x9f840
movsd -0x8(%rbp), %xmm0
callq 0x9bc10
movl $0x0, -0x48d4(%rbp)
movl -0x48d4(%rbp), %eax
cmpl -0x210(%rbp), %eax
jge 0x27605
movl -0x48d4(%rbp), %esi
leaq -0x40a0(%rbp), %rdi
callq 0x81890
movl %eax, -0x40bc(%rbp)
movq 0xc0f45(%rip), %rdi     # 0xe8520
movl -0x40bc(%rbp), %esi
movslq -0x48d4(%rbp), %rax
movl -0xe90(%rbp,%rax,4), %edx
callq 0x9dbc0
movl -0x48d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48d4(%rbp)
jmp 0x275ae
cmpl $0x0, -0x2794(%rbp)
je 0x27619
movl -0x2794(%rbp), %edi
callq 0x9fde0
movl -0x40a0(%rbp), %eax
movl %eax, -0x40c0(%rbp)
cmpl $0x1, -0x40c8(%rbp)
jl 0x2763c
movl -0x40c8(%rbp), %eax
cmpl -0x409c(%rbp), %eax
jle 0x2764a
movl -0x409c(%rbp), %eax
movl %eax, -0x40c8(%rbp)
jmp 0x27659
movl -0x40c8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x40c8(%rbp)
movl $0x0, -0x48d8(%rbp)
movl -0x48d8(%rbp), %eax
cmpl -0x40c8(%rbp), %eax
jge 0x276c3
movl -0x48d8(%rbp), %esi
leaq -0x40a0(%rbp), %rdi
callq 0x81830
movl %eax, -0x40bc(%rbp)
movq 0xc0e90(%rip), %rdi     # 0xe8520
movl -0x40bc(%rbp), %esi
movl -0x40c0(%rbp), %edx
callq 0x9dbc0
movl -0x40c0(%rbp), %edi
callq 0x25810
movl %eax, -0x40c0(%rbp)
movl -0x48d8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48d8(%rbp)
jmp 0x27663
cmpl $0x1, -0x40c0(%rbp)
jne 0x276df
leaq 0x8545c(%rip), %rdi     # 0xacb2f
movl $0x1, %esi
callq 0x27f30
jmp 0x276f0
leaq 0x85457(%rip), %rdi     # 0xacb3d
movl $0x1, %esi
callq 0x27f30
addq $0x4910, %rsp           # imm = 0x4910
popq %rbp
retq
nopl (%rax)