// File: 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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl $0x3, -0x24(%rbp)
movl -0x1c(%rbp), %ecx
addl -0x24(%rbp), %ecx
xorl %eax, %eax
cmpl $0x186a0, %ecx          # imm = 0x186A0
movb %al, -0x2d(%rbp)
jge 0x82521
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl -0x24(%rbp), %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %ecx
movb $0x1, %al
cmpl $0x5b, %ecx
movb %al, -0x2e(%rbp)
je 0x8251b
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl -0x24(%rbp), %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %ecx
movb $0x1, %al
cmpl $0x5d, %ecx
movb %al, -0x2e(%rbp)
je 0x8251b
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl -0x24(%rbp), %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %edi
callq 0x820e0
movb %al, %cl
movb $0x1, %al
testb $0x1, %cl
movb %al, -0x2e(%rbp)
jne 0x8251b
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl -0x24(%rbp), %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %edx
xorl %eax, %eax
movl $0x61, %ecx
cmpl %edx, %ecx
movb %al, -0x2f(%rbp)
jg 0x82515
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl -0x24(%rbp), %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x73, %eax
setle %al
movb %al, -0x2f(%rbp)
movb -0x2f(%rbp), %al
movb %al, -0x2e(%rbp)
movb -0x2e(%rbp), %al
movb %al, -0x2d(%rbp)
movb -0x2d(%rbp), %al
testb $0x1, %al
jne 0x8252a
jmp 0x82538
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x8246d
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl -0x24(%rbp), %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x5d, %eax
je 0x82557
movl -0x24(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x1c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl -0x1c(%rbp), %ecx
addl -0x24(%rbp), %ecx
cmpl %ecx, %eax
jge 0x826e9
jmp 0x8257a
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0x820e0
testb $0x1, %al
jne 0x82591
jmp 0x8259c
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x8257a
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x5b, %eax
je 0x825bb
movl -0x1c(%rbp), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x82705
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0x820e0
testb $0x1, %al
jne 0x825db
jmp 0x825e6
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x825c4
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0x82b60
movl %eax, %edx
movq -0x10(%rbp), %rax
movl -0x28(%rbp), %ecx
movq -0x10(%rbp), %rsi
addl 0x3e90(%rsi), %ecx
movslq %ecx, %rcx
movl %edx, 0x1910(%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0x820e0
testb $0x1, %al
jne 0x82634
jmp 0x8263f
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x8261d
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0x82b60
movl %eax, %edx
movq -0x10(%rbp), %rax
movl -0x28(%rbp), %ecx
movq -0x10(%rbp), %rsi
addl 0x3e90(%rsi), %ecx
movslq %ecx, %rcx
movl %edx, 0x2590(%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0x820e0
testb $0x1, %al
jne 0x8268d
jmp 0x82698
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x82676
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x5d, %eax
je 0x826b4
movl -0x1c(%rbp), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x82705
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl -0x20(%rbp), %edx
movq -0x10(%rbp), %rax
movl -0x28(%rbp), %ecx
movq -0x10(%rbp), %rsi
addl 0x3e90(%rsi), %ecx
movslq %ecx, %rcx
movl %edx, 0x3210(%rax,%rcx,4)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x82567
movl -0x28(%rbp), %ecx
movq -0x10(%rbp), %rax
addl 0x3e90(%rax), %ecx
movl %ecx, 0x3e90(%rax)
movl -0x1c(%rbp), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop