// File: /workspace/github_cpp_makefile_star2_O3/huntzhan[P]SAM-PNG/solution.cc
// Function: AddSymbolToSAM(State*, State*, char)
State *AddSymbolToSAM(State *start, State *last, char c) {
  auto cur = new State;
  cur->set_maxlen(last->maxlen() + 1);
  cur->set_first_endpos(last->first_endpos() + 1);

  auto p = last;
  while (p && !p->has_trans(c)) {
    p->set_trans(c, cur);
    p = p->link();
  }

  if (p == nullptr) {
    cur->set_link(start);
    cur->set_minlen(1);
    return cur;
  }

  auto q = p->trans(c);
  if (p->maxlen() + 1 == q->maxlen()) {
    cur->set_link(q);
    cur->set_minlen(q->maxlen() + 1);
  } else {
    auto sq = new State;
    sq->set_maxlen(p->maxlen() + 1);
    sq->set_trans(q->trans());
    sq->set_first_endpos(q->first_endpos());
    
    while (p && p->has_trans(c) && p->trans(c) == q) {
      p->set_trans(c, sq);
      p = p->link();
    }

    sq->set_link(q->link());
    sq->set_minlen(sq->link()->maxlen() + 1);

    q->set_link(sq);
    q->set_minlen(sq->maxlen() + 1);

    cur->set_link(sq);
    cur->set_minlen(sq->maxlen() + 1);
  }

  return cur;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r15
movl $0x58, %edi
callq 0x20d0
movq %rax, %rbx
movl $0x0, 0x4(%rax)
movq $0x0, 0x10(%rax)
addq $0x48, %rax
movq %rax, 0x18(%rbx)
movq $0x1, 0x20(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rbx)
movl $0x3f800000, 0x38(%rbx) # imm = 0x3F800000
movups %xmm0, 0x40(%rbx)
movb $0x0, 0x50(%rbx)
movl (%r14), %eax
incl %eax
movl %eax, (%rbx)
movl 0x8(%r14), %eax
incl %eax
movl %eax, 0x8(%rbx)
leaq 0x8(%rsp), %r12
movb %bpl, 0x8(%rsp)
leaq 0x18(%r14), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x3268
testq %rax, %rax
jne 0x251c
movb %bpl, 0x8(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x32d2
movq %rbx, (%rax)
movq 0x10(%r14), %r14
testq %r14, %r14
jne 0x24d9
movq %r15, 0x10(%rbx)
movl $0x1, %ecx
jmp 0x2670
movsbl %bpl, %esi
movq %r14, %rdi
movl %esi, 0x14(%rsp)
callq 0x31fa
movl (%r14), %ecx
leal 0x1(%rcx), %r15d
cmpl (%rax), %r15d
jne 0x2544
movq %rax, 0x10(%rbx)
addl $0x2, %ecx
jmp 0x2670
movl $0x58, %edi
movq %rax, %r12
callq 0x20d0
movq %rax, %r13
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0x4(%r13)
xorl %eax, %eax
movq %rax, 0x10(%r13)
leaq 0x18(%r13), %rcx
movq %rcx, 0x18(%rsp)
leaq 0x48(%r13), %rcx
movq %rcx, 0x18(%r13)
movq $0x1, 0x20(%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r13)
movl $0x3f800000, 0x38(%r13) # imm = 0x3F800000
movups %xmm0, 0x40(%r13)
movb $0x0, 0x50(%r13)
movl %r15d, (%r13)
leaq 0x18(%r12), %rsi
leaq 0x20(%rsp), %r15
movq %rax, (%r15)
movq 0x20(%r12), %rcx
movq %rcx, 0x8(%r15)
movq %rax, 0x10(%r15)
movq 0x30(%r12), %rcx
movq %rcx, 0x18(%r15)
movups 0x38(%r12), %xmm0
movups %xmm0, 0x20(%r15)
movq %rax, 0x30(%r15)
leaq 0x8(%rsp), %rdx
movq %r15, (%rdx)
movq %r15, %rdi
callq 0x38f2
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x3584
leaq 0x20(%rsp), %rdi
callq 0x3a40
movl 0x8(%r12), %eax
movl %eax, 0x8(%r13)
movb %bpl, 0x8(%rsp)
leaq 0x18(%r14), %r15
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0x3268
testq %rax, %rax
je 0x264b
movq %r14, %rdi
movl 0x14(%rsp), %esi
callq 0x31fa
cmpq %r12, %rax
jne 0x264b
movb %bpl, 0x8(%rsp)
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0x32d2
movq %r13, (%rax)
movq 0x10(%r14), %r14
testq %r14, %r14
jne 0x2601
movq 0x10(%r12), %rax
movq %rax, 0x10(%r13)
movl (%rax), %eax
incl %eax
movl %eax, 0x4(%r13)
movq %r13, 0x10(%r12)
movl (%r13), %ecx
incl %ecx
movl %ecx, 0x4(%r12)
movq %r13, 0x10(%rbx)
movl %ecx, 0x4(%rbx)
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3a40
movq %rbx, %rdi
callq 0x21d0