// File: /workspace/github_cpp_makefile_star2_O3/kobanium[P]Ray/src/sgf/SgfExtractor.cpp
// Function: GetHandicapPosition(SGF_record_t*, char const*, int, int)
static int
GetHandicapPosition( SGF_record_t *kifu, const char *sgf_text, const int cursor, const int color )
{
  int tmp_cursor = 3;

  while ((cursor + tmp_cursor < 100000) &&
         ((sgf_text[cursor + tmp_cursor] == '[') ||
          (sgf_text[cursor + tmp_cursor] == ']') ||
          IsSgfSpace(sgf_text[cursor + tmp_cursor]) ||
          (('a' <= sgf_text[cursor + tmp_cursor]) && 
           (sgf_text[cursor + tmp_cursor] <= 's')))) tmp_cursor++;

  if (sgf_text[cursor + tmp_cursor] != ']'){
    tmp_cursor--;
  }
  
  int handicaps = 0;

  for (int cur = cursor + 2; cur < cursor + tmp_cursor;) {
    while (IsSgfSpace(sgf_text[cur])) cur++;
    if (sgf_text[cur] != '[')
      return cursor + tmp_cursor;
    cur++; while (IsSgfSpace(sgf_text[cur])) cur++;
    kifu->handicap_x[handicaps + kifu->handicap_stones] = ParsePosition(sgf_text[cur]);
    cur++; while (IsSgfSpace(sgf_text[cur])) cur++;
    kifu->handicap_y[handicaps + kifu->handicap_stones] = ParsePosition(sgf_text[cur]);
    cur++; while (IsSgfSpace(sgf_text[cur])) cur++;
    if (sgf_text[cur] != ']')
      return cursor + tmp_cursor;
    cur++;
    kifu->handicap_color[handicaps + kifu->handicap_stones] = color;
    handicaps++;
  }

  kifu->handicap_stones += handicaps;
  
  return cursor + tmp_cursor;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %r13d
movq %rsi, %r15
leal 0x3(%r13), %r12d
movabsq $0x100002600, %rbp      # imm = 0x100002600
cmpl $0x1869c, %edx          # imm = 0x1869C
jg 0x460bc
movslq %r12d, %rax
movl $0x186a0, %r12d         # imm = 0x186A0
subl %r13d, %r12d
addq %r15, %rax
movl $0x3, %esi
movzbl -0x3(%rax,%rsi), %edx
cmpq $0x20, %rdx
ja 0x460a1
btq %rdx, %rbp
jae 0x460a1
incq %rsi
cmpq %rsi, %r12
jne 0x46086
jmp 0x460c1
cmpl $0x5b, %edx
je 0x46097
cmpl $0x5d, %edx
je 0x46097
addb $-0x61, %dl
cmpb $0x12, %dl
jbe 0x46097
leal (%rsi,%r13), %eax
movl %esi, %r12d
jmp 0x460c6
movl %r12d, %eax
jmp 0x460c9
movl $0x186a0, %eax          # imm = 0x186A0
addl %r13d, %r12d
cltq
xorl %edx, %edx
cmpb $0x5d, (%r15,%rax)
setne %dl
subl %edx, %r12d
addl $0x2, %r13d
xorl %ebx, %ebx
cmpl %r12d, %r13d
jge 0x461f6
leaq 0x1(%r15), %r8
xorl %ebx, %ebx
movq %rdi, (%rsp)
movslq %r13d, %rsi
leal 0x2(%rsi), %r14d
leaq (%r8,%rsi), %rax
leal 0x4(%rsi), %r13d
movzbl (%r15,%rsi), %edx
cmpq $0x20, %rdx
ja 0x4611f
btq %rdx, %rbp
jae 0x4611f
incq %rsi
incl %r14d
incq %rax
incl %r13d
jmp 0x46100
cmpl $0x5b, %edx
jne 0x461fc
movzbl (%rax), %edx
cmpq $0x20, %rdx
ja 0x46142
btq %rdx, %rbp
jae 0x46142
incl %r14d
incq %rax
incl %r13d
jmp 0x46128
movl %ecx, 0xc(%rsp)
movsbl %dl, %edi
movq %r8, 0x10(%rsp)
callq 0x463a7
movq (%rsp), %rdx
movslq 0x3e90(%rdx), %rcx
movslq %ebx, %rbx
addq %rbx, %rcx
movl %eax, 0x1910(%rdx,%rcx,4)
movslq %r14d, %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %r14
movzbl (%r15,%rax), %ecx
cmpq $0x20, %rcx
ja 0x46193
btq %rcx, %rbp
jae 0x46193
incq %rax
incl %r13d
incq %r14
jmp 0x46177
movsbl %cl, %edi
callq 0x463a7
movq (%rsp), %rdx
movslq 0x3e90(%rdx), %rcx
addq %rbx, %rcx
movl %eax, 0x2590(%rdx,%rcx,4)
movl 0xc(%rsp), %ecx
movzbl (%r14), %eax
cmpq $0x20, %rax
ja 0x461cc
btq %rax, %rbp
jae 0x461cc
incl %r13d
incq %r14
jmp 0x461b4
cmpl $0x5d, %eax
movq 0x10(%rsp), %r8
jne 0x461fc
movq (%rsp), %rdi
movslq 0x3e90(%rdi), %rax
addq %rbx, %rax
movl %ecx, 0x3210(%rdi,%rax,4)
incl %ebx
cmpl %r12d, %r13d
jl 0x460f1
addl %ebx, 0x3e90(%rdi)
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq