// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
movl %edx, %r15d
movq %rsi, %r13
movq %rdi, %r12
movslq %edx, %r14
movq %r14, %rdx
shlq $0x20, %rdx
movabsq $0x300000000, %rax      # imm = 0x300000000
addq %rdx, %rax
addq $0x3, %r14
movabsq $0x100000000, %rsi      # imm = 0x100000000
movabsq $0x100002600, %rbp      # imm = 0x100002600
cmpq $0x1869f, %r14          # imm = 0x1869F
jg 0x2f4ea
movzbl (%r13,%r14), %edx
cmpq $0x20, %rdx
ja 0x2f4d8
btq %rdx, %rbp
jae 0x2f4d8
incq %r14
addq %rsi, %rax
jmp 0x2f4b5
cmpl $0x5b, %edx
je 0x2f4d0
cmpl $0x5d, %edx
je 0x2f4d0
addb $-0x61, %dl
cmpb $0x12, %dl
jbe 0x2f4d0
sarq $0x20, %rax
xorl %edx, %edx
cmpb $0x5d, (%r13,%rax)
setne %dl
subl %edx, %r14d
leaq 0x3(%r13), %rdi
leaq 0x1(%r13), %rax
movq %rax, 0x20(%rsp)
movl $0x0, 0x8(%rsp)
leal 0x2(%r15), %eax
cmpl %r14d, %eax
jge 0x2f627
movslq %r15d, %rsi
addl $0x4, %r15d
leaq (%rdi,%rsi), %rax
addq $0x2, %rsi
movzbl (%r13,%rsi), %edx
cmpq $0x20, %rdx
ja 0x2f54a
btq %rdx, %rbp
jae 0x2f54a
incq %rsi
incl %r15d
incq %rax
jmp 0x2f52d
cmpl $0x5b, %edx
jne 0x2f633
movzbl (%rax), %edx
cmpq $0x20, %rdx
ja 0x2f56a
btq %rdx, %rbp
jae 0x2f56a
incl %r15d
incq %rax
jmp 0x2f553
movq %rdi, 0x18(%rsp)
movl %ecx, 0xc(%rsp)
movsbl %dl, %edi
callq 0x2f79a
movslq 0x3e90(%r12), %rcx
movslq 0x8(%rsp), %rbx
addq %rbx, %rcx
movq %r12, 0x10(%rsp)
movl %eax, 0x1910(%r12,%rcx,4)
movslq %r15d, %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax), %r12
movzbl (%r13,%rax), %ecx
cmpq $0x20, %rcx
ja 0x2f5c1
btq %rcx, %rbp
jae 0x2f5c1
incq %rax
incl %r15d
incq %r12
jmp 0x2f5a4
movsbl %cl, %edi
callq 0x2f79a
movq 0x10(%rsp), %rdx
movslq 0x3e90(%rdx), %rcx
addq %rbx, %rcx
movl %eax, 0x2590(%rdx,%rcx,4)
movl 0xc(%rsp), %ecx
movq 0x18(%rsp), %rdi
movzbl (%r12), %eax
cmpq $0x20, %rax
ja 0x2f601
btq %rax, %rbp
jae 0x2f601
incl %r15d
incq %r12
jmp 0x2f5e8
cmpl $0x5d, %eax
movq 0x10(%rsp), %r12
jne 0x2f633
movslq 0x3e90(%r12), %rax
addq %rbx, %rax
movl %ecx, 0x3210(%r12,%rax,4)
incl 0x8(%rsp)
jmp 0x2f511
movl 0x8(%rsp), %eax
addl %eax, 0x3e90(%r12)
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq