// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rsi, %r14
movq %rdi, %r12
movl %edx, 0x14(%rsp)
movslq %edx, %rbx
leaq 0x1c60f(%rip), %r15     # 0x201c0
cmpq $0x0, (%r15,%rbx,8)
jne 0x3bc8
leaq 0x1c791(%rip), %rdi     # 0x20350
callq 0x14933
movq %rax, (%r15,%rbx,8)
movq %r14, 0x18(%rsp)
movl (%r12), %r14d
movl 0x8(%r12), %ecx
leal -0x1(%r14), %eax
testl %r14d, %r14d
notl %r14d
cmovnsl %eax, %r14d
movq %r12, 0x28(%rsp)
movslq 0x4(%r12), %rbp
movq (%r15,%rbx,8), %r8
leaq 0x1c757(%rip), %rdi     # 0x20350
movq $0x0, 0x8(%rsp)
movl %r14d, %esi
movl %ebp, %edx
movl %ecx, 0x10(%rsp)
xorl %r9d, %r9d
callq 0x14b9b
movq 0x1c586(%rip), %rsi     # 0x201a0
movslq %r14d, %rdx
movq (%rsi,%rdx,8), %rcx
movq 0x8(%rsi,%rdx,8), %rdi
cmpl %edi, %ecx
setl 0x7(%rsp)
jge 0x3d8b
movq %rax, 0x20(%rsp)
subq %rbp, %rax
movq %rax, 0x38(%rsp)
movq 0x1c569(%rip), %rsi     # 0x201b0
movl (%rsi,%rdx,8), %edx
incl 0x14(%rsp)
movslq %ecx, %rbx
movslq %edi, %rax
movq %rax, 0x40(%rsp)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq $0x0, 0x8(%rsp)
movl 0x10(%rsp), %ecx
movq %rbp, 0x30(%rsp)
movq 0x1c530(%rip), %rax     # 0x201a8
movslq (%rax,%rbx,4), %r15
cmpl %ebp, %r15d
jle 0x3d28
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpl %ebp, %r12d
jl 0x3dbd
cmpl %ecx, %r15d
jg 0x3dbd
movq 0x1c514(%rip), %rax     # 0x201b8
movslq %edx, %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x3dbd
movl %edx, %r13d
leaq (%rax,%rcx,4), %rdi
movslq 0x8(%rsp), %rsi
movq 0x18(%rsp), %r12
addq %r12, %rsi
movl 0x14(%rsp), %edx
callq 0x3b8f
testl %eax, %eax
js 0x3dbd
addl 0x8(%rsp), %eax
movq 0x1c4c7(%rip), %rcx     # 0x201a8
movq %r12, %rdx
movl 0x4(%rcx,%rbx,4), %r12d
movl 0x10(%rsp), %ebp
cmpl %ebp, %r12d
cmovgel %ebp, %r12d
movslq %eax, %r14
leaq (%rdx,%r14), %rdi
movq %r15, %rsi
addq 0x38(%rsp), %rsi
movl %r12d, %eax
subl %r15d, %eax
movslq %eax, %r15
movq %r15, %rdx
callq 0x1240
movl %ebp, %ecx
movq 0x30(%rsp), %rbp
addl %r14d, %r15d
movq %r15, 0x8(%rsp)
movl %r13d, %edx
jmp 0x3d6f
movl 0x4(%rax,%rbx,4), %r12d
cmpl %ebp, %r12d
jle 0x3d6f
cmpl %ecx, %r12d
cmovgel %ecx, %r12d
movq 0x8(%rsp), %r13
movslq %r13d, %rdi
addq 0x18(%rsp), %rdi
movl %r12d, %eax
subl %ebp, %eax
movslq %eax, %r14
movq 0x20(%rsp), %rsi
movl %edx, %r15d
movq %r14, %rdx
callq 0x1240
movl %r15d, %edx
movl 0x10(%rsp), %ecx
addl %r13d, %r14d
movq %r14, 0x8(%rsp)
jmp 0x3d72
addl $0x3, %edx
cmpl %ecx, %r12d
jge 0x3d8b
addq $0x3, %rbx
cmpq 0x40(%rsp), %rbx
setl 0x7(%rsp)
jl 0x3c71
testb $0x1, 0x7(%rsp)
movq 0x18(%rsp), %rbx
je 0x3dcf
movq 0x28(%rsp), %rax
cmpl $0x0, (%rax)
js 0x3dab
movq 0x8(%rsp), %rax
movl %eax, %r14d
jmp 0x3dbd
movq %rbx, %rdi
movq 0x8(%rsp), %rbx
movl %ebx, %esi
callq 0xa1b4
movl %ebx, %r14d
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x12e0
jmp 0x3d97
nop