// File: /workspace/github_cpp_makefile_star2_O1/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, %r12d
movq %rsi, %r14
movq %rdi, 0x10(%rsp)
leal 0x3(%r12), %eax
movl $0x3, %r15d
movabsq $0x100002600, %r13      # imm = 0x100002600
cmpl $0x1869c, %edx          # imm = 0x1869C
jg 0x45e51
cltq
movl $0x186a0, %r15d         # imm = 0x186A0
subl %r12d, %r15d
addq %r14, %rax
movl $0x3, %esi
movzbl -0x3(%rax,%rsi), %edx
cmpq $0x20, %rdx
ja 0x45e31
btq %rdx, %r13
jae 0x45e31
incq %rsi
cmpq %rsi, %r15
jne 0x45e16
jmp 0x45e4c
cmpl $0x5b, %edx
je 0x45e27
cmpl $0x5d, %edx
je 0x45e27
addb $-0x61, %dl
cmpb $0x12, %dl
jbe 0x45e27
leal (%r12,%rsi), %eax
movl %esi, %r15d
jmp 0x45e51
movl $0x186a0, %eax          # imm = 0x186A0
cltq
xorl %edx, %edx
cmpb $0x5d, (%r14,%rax)
setne %dl
addl %r12d, %r15d
subl %edx, %r15d
addl $0x2, %r12d
xorl %ebp, %ebp
cmpl %r15d, %r12d
setl %r8b
jge 0x45fa8
leaq 0x1(%r14), %rdi
movl $0x0, 0xc(%rsp)
movslq %r12d, %rsi
leal 0x2(%rsi), %ebx
leaq (%rdi,%rsi), %rax
leal 0x4(%rsi), %r12d
movzbl (%r14,%rsi), %edx
cmpq $0x20, %rdx
ja 0x45eae
btq %rdx, %r13
jae 0x45eae
incq %rsi
incl %ebx
incq %rax
incl %r12d
jmp 0x45e90
cmpl $0x5b, %edx
jne 0x45f9c
movzbl (%rax), %edx
cmpq $0x20, %rdx
ja 0x45ed0
btq %rdx, %r13
jae 0x45ed0
incl %ebx
incq %rax
incl %r12d
jmp 0x45eb7
movb %r8b, 0xb(%rsp)
movl %ecx, 0x1c(%rsp)
movq %rdi, 0x20(%rsp)
movsbl %dl, %edi
callq 0x46162
movq 0x10(%rsp), %rdx
movslq 0x3e90(%rdx), %rcx
movslq 0xc(%rsp), %rbp
addq %rbp, %rcx
movl %eax, 0x1910(%rdx,%rcx,4)
movslq %ebx, %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax), %rbx
movzbl (%r14,%rax), %ecx
cmpq $0x20, %rcx
ja 0x45f29
btq %rcx, %r13
jae 0x45f29
incq %rax
incl %r12d
incq %rbx
jmp 0x45f0d
movsbl %cl, %edi
callq 0x46162
movq 0x10(%rsp), %rdx
movslq 0x3e90(%rdx), %rcx
addq %rbp, %rcx
movl %eax, 0x2590(%rdx,%rcx,4)
movl 0x1c(%rsp), %ecx
movb 0xb(%rsp), %r8b
movzbl (%rbx), %eax
cmpq $0x20, %rax
ja 0x45f67
btq %rax, %r13
jae 0x45f67
incl %r12d
incq %rbx
jmp 0x45f50
cmpl $0x5d, %eax
movq 0x20(%rsp), %rdi
jne 0x45f9c
movq 0x10(%rsp), %rdx
movslq 0x3e90(%rdx), %rax
addq %rbp, %rax
movl %ecx, 0x3210(%rdx,%rax,4)
incl %ebp
movl %ebp, 0xc(%rsp)
cmpl %r15d, %r12d
setl %r8b
jl 0x45e82
jmp 0x45fa8
movl 0xc(%rsp), %eax
movl %eax, %ebp
testb $0x1, %r8b
jne 0x45fb3
movq 0x10(%rsp), %rax
addl %ebp, 0x3e90(%rax)
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq