// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r15
movl $0x58, %edi
callq 0x20d0
movq %rax, %rbx
xorl %eax, %eax
movq %rax, (%rbx)
movl $0xffffffff, 0x8(%rbx)  # imm = 0xFFFFFFFF
movq %rax, 0x10(%rbx)
leaq 0x48(%rbx), %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)
movq %rsp, %r12
movb %bpl, (%rsp)
leaq 0x18(%r14), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x3234
testq %rax, %rax
jne 0x2519
movb %bpl, (%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x32ba
movq %rbx, (%rax)
movq 0x10(%r14), %r14
testq %r14, %r14
jne 0x24d8
movq %r15, 0x10(%rbx)
movl $0x1, %eax
jmp 0x2675
movq %rsp, %rsi
movb %bpl, (%rsi)
movq %r13, %rdi
callq 0x3558
movq (%rax), %r12
movl (%r14), %ecx
incl %ecx
movl (%r12), %eax
cmpl %eax, %ecx
jne 0x2542
movq %r12, 0x10(%rbx)
incl %eax
jmp 0x2675
movl $0x58, %edi
callq 0x20d0
movq %rax, %r15
xorl %eax, %eax
movq %rax, (%r15)
movl $0xffffffff, 0x8(%r15)  # imm = 0xFFFFFFFF
movq %rax, 0x10(%r15)
leaq 0x18(%r15), %rcx
movq %rcx, 0x8(%rsp)
leaq 0x48(%r15), %rcx
movq %rcx, 0x18(%r15)
movq $0x1, 0x20(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r15)
movl $0x3f800000, 0x38(%r15) # imm = 0x3F800000
movups %xmm0, 0x40(%r15)
movb $0x0, 0x50(%r15)
movl (%r14), %ecx
incl %ecx
movl %ecx, (%r15)
leaq 0x18(%r12), %rsi
leaq 0x10(%rsp), %r13
movq %rax, (%r13)
movq 0x20(%r12), %rcx
movq %rcx, 0x8(%r13)
movq %rax, 0x10(%r13)
movq 0x30(%r12), %rcx
movq %rcx, 0x18(%r13)
movups 0x38(%r12), %xmm0
movups %xmm0, 0x20(%r13)
movq %rax, 0x30(%r13)
movq %rsp, %rdx
movq %r13, (%rdx)
movq %r13, %rdi
callq 0x38fc
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x359c
leaq 0x10(%rsp), %rdi
callq 0x3a4a
movq %r12, 0x8(%rsp)
movl 0x8(%r12), %eax
movl %eax, 0x8(%r15)
movq %rsp, %r12
movb %bpl, (%rsp)
leaq 0x18(%r14), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x3234
testq %rax, %rax
je 0x2650
movb %bpl, (%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x3558
movq 0x8(%rsp), %rcx
cmpq %rcx, (%rax)
jne 0x2650
movb %bpl, (%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x32ba
movq %r15, (%rax)
movq 0x10(%r14), %r14
testq %r14, %r14
jne 0x2604
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rax
movq %rax, 0x10(%r15)
movl (%rax), %eax
incl %eax
movl %eax, 0x4(%r15)
movq %r15, 0x10(%rcx)
movl (%r15), %eax
incl %eax
movl %eax, 0x4(%rcx)
movq %r15, 0x10(%rbx)
movl %eax, 0x4(%rbx)
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3a4a
movq %rbx, %rdi
callq 0x21d0