// File: /workspace/github_c_makefile_star2_O2/thegenemyers[P]DASCRUBBER/DASedit.c
// Function: Load_Model
static int Load_Model(int *patch, char *target, int depth)
{ int   bread, bbeg, bend;
  int   lend, rend;
  int   gb, ge, pb;
  int   tlen, plen;
  char *bseq;

  if (BSTACK[depth] == NULL)
    BSTACK[depth] = New_Read_Buffer(DB);

  bread = patch[0];
  if (bread < 0)
    bread = - (bread+1);
  else
    bread -= 1;
  bbeg   = patch[1];
  bend   = patch[2];

#ifdef DEBUG_PATCHING
  printf("%*sPatching %d%c[%d,%d]->[%d,%d]\n",
         2*depth,"",bread,(patch[0]<0?'c':'n'),patch[1],patch[2],bbeg,bend);
#endif

  bseq = Load_Subread(DB,bread,bbeg,bend,BSTACK[depth],0) - bbeg;

  gb = TRIM_IDX[bread];
  ge = TRIM_IDX[bread+1];
  pb = PATCH_IDX[bread];

  tlen = 0;
  rend = -1;
  for (; gb < ge; gb += 3)
    { lend = TRIM[gb];
      if (lend > bbeg)
        { if (rend < bbeg || lend > bend || PATCH[pb] == 0)
            return (-1);
          plen = Load_Model(PATCH+pb,target+tlen,depth+1);
          if (plen < 0)
            return (-1);
          tlen += plen;

          rend = TRIM[gb+1];
          if (rend > bend)
            rend = bend;
          memmove(target+tlen,bseq+lend,rend-lend);
          tlen += rend-lend; 
#ifdef DEBUG_PATCHING
          printf("%*s  Piece %d,%d\n",2*depth,"",lend,rend);
#endif
#ifdef SHOW_PIECES
          Print_Seq(bseq+lend,rend-lend);
#endif
          pb += 3;
        }
      else // lend <= bbeg
        { rend = TRIM[gb+1];
          if (rend > bbeg)
            { if (rend > bend)
                rend = bend;
              memmove(target+tlen,bseq+bbeg,rend-bbeg);
              tlen += rend-bbeg;
#ifdef DEBUG_PATCHING
              printf("%*s  Piece %d,%d\n",2*depth,"",bbeg,rend);
#endif
#ifdef SHOW_PIECES
              Print_Seq(bseq+bbeg,rend-bbeg);
#endif
            }
          else
            pb += 3;
	}
      if (rend >= bend)
        break;
    }
  if (gb >= ge)
    fprintf(stderr,"Fatal: Should not happen\n");
 
  if (patch[0] < 0)
    Complement_Seq(target,tlen);

  return (tlen);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %r13d
movq %rsi, 0x8(%rsp)
movq %rdi, %r15
movslq %edx, %rbx
leaq 0x1c72e(%rip), %r14     # 0x1f1c0
movq (%r14,%rbx,8), %r8
testq %r8, %r8
jne 0x2aae
leaq 0x1c8ae(%rip), %rdi     # 0x1f350
callq 0x13252
movq %rax, %r8
movq %rax, (%r14,%rbx,8)
movl (%r15), %r14d
movl 0x8(%r15), %r12d
leal -0x1(%r14), %eax
testl %r14d, %r14d
notl %r14d
cmovnsl %eax, %r14d
movq %r15, 0x18(%rsp)
movslq 0x4(%r15), %rbx
leaq 0x1c87d(%rip), %rdi     # 0x1f350
xorl %r15d, %r15d
movl %r14d, %esi
movl %ebx, %edx
movl %r12d, %ecx
xorl %r9d, %r9d
callq 0x133ec
movq %rbx, %rsi
movq %rax, 0x20(%rsp)
subq %rbx, %rax
movq %rax, 0x28(%rsp)
movq 0x1c6a3(%rip), %rax     # 0x1f1a0
movslq %r14d, %rcx
movslq (%rax,%rcx,8), %rbp
movslq 0x8(%rax,%rcx,8), %rax
movq %rax, 0x30(%rsp)
movq 0x1c69b(%rip), %rax     # 0x1f1b0
movl (%rax,%rcx,8), %edi
incl %r13d
pushq $-0x1
popq %rbx
movl %r13d, %edx
movl %r13d, 0x4(%rsp)
movq %rsi, 0x10(%rsp)
movl %r12d, (%rsp)
cmpq 0x30(%rsp), %rbp
jge 0x2c6f
movq 0x1c667(%rip), %rax     # 0x1f1a8
movslq (%rax,%rbp,4), %r13
movslq %r15d, %r14
cmpl %esi, %r13d
jle 0x2bea
cmpl %esi, %ebx
jl 0x2c57
cmpl %r12d, %r13d
jg 0x2c57
movq 0x1c64f(%rip), %rax     # 0x1f1b8
movslq %edi, %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x2c57
movq %r15, %r12
movl %edi, %r15d
leaq (%rax,%rcx,4), %rdi
movq 0x8(%rsp), %rbx
addq %rbx, %r14
movq %r14, %rsi
callq 0x2a6f
testl %eax, %eax
js 0x2c57
addl %r12d, %eax
movq 0x1c606(%rip), %rcx     # 0x1f1a8
movq %rbx, %rdx
movl 0x4(%rcx,%rbp,4), %ebx
movl (%rsp), %r12d
cmpl %r12d, %ebx
cmovgel %r12d, %ebx
movslq %eax, %r14
leaq (%rdx,%r14), %rdi
movq %r13, %rsi
addq 0x28(%rsp), %rsi
movl %ebx, %eax
subl %r13d, %eax
movslq %eax, %r13
movq %r13, %rdx
callq 0x1240
addl %r14d, %r13d
movl %r15d, %edi
movq %r13, %r15
addl $0x3, %edi
movl 0x4(%rsp), %edx
movq 0x10(%rsp), %rsi
jmp 0x2c31
movl 0x4(%rax,%rbp,4), %ebx
cmpl %esi, %ebx
jle 0x2c2e
cmpl %r12d, %ebx
cmovgel %r12d, %ebx
movq 0x8(%rsp), %rax
movl %edi, %r12d
leaq (%rax,%r14), %rdi
movl %ebx, %eax
subl %esi, %eax
movslq %eax, %r15
movq 0x20(%rsp), %rsi
movq %r15, %rdx
callq 0x1240
movl %r12d, %edi
movl (%rsp), %r12d
movq 0x10(%rsp), %rsi
movl 0x4(%rsp), %edx
addl %r14d, %r15d
jmp 0x2c31
addl $0x3, %edi
addq $0x3, %rbp
cmpl %r12d, %ebx
jl 0x2b2f
movq 0x18(%rsp), %rax
cmpl $0x0, (%rax)
jns 0x2c5d
movq 0x8(%rsp), %rdi
movl %r15d, %esi
callq 0x8cc4
jmp 0x2c5d
pushq $-0x1
popq %rax
movl %eax, %r15d
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1c382(%rip), %rax     # 0x1eff8
movq (%rax), %rcx
leaq 0x1684c(%rip), %rdi     # 0x194cc
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x12a0
jmp 0x2c3e
nopl (%rax)