// File: /workspace/github_c_makefile_star2_O3/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, 0x10(%rsp)
movq %rdi, %r15
movl %edx, 0x8(%rsp)
movslq %edx, %rbx
leaq 0x1d65d(%rip), %r14     # 0x211c0
movq (%r14,%rbx,8), %r8
testq %r8, %r8
jne 0x3b7f
leaq 0x1d7dd(%rip), %rdi     # 0x21350
callq 0x149fd
movq %rax, %r8
movq %rax, (%r14,%rbx,8)
movl (%r15), %r13d
movl 0x8(%r15), %ebx
leal -0x1(%r13), %eax
testl %r13d, %r13d
notl %r13d
cmovnsl %eax, %r13d
movq %r15, 0x28(%rsp)
movslq 0x4(%r15), %r12
leaq 0x1d7ac(%rip), %rdi     # 0x21350
movq $0x0, (%rsp)
movl %r13d, %esi
movl %r12d, %edx
movl %ebx, %ecx
xorl %r9d, %r9d
callq 0x14c5e
movq %rax, 0x18(%rsp)
movq 0x1d5d8(%rip), %rax     # 0x211a0
movslq %r13d, %rdx
movq (%rax,%rdx,8), %rcx
movq 0x8(%rax,%rdx,8), %rax
cmpl %eax, %ecx
jge 0x3d39
movl %ebx, %edi
movq 0x18(%rsp), %rsi
subq %r12, %rsi
movq %rsi, 0x38(%rsp)
movq 0x1d5be(%rip), %rsi     # 0x211b0
movl (%rsi,%rdx,8), %edx
incl 0x8(%rsp)
movslq %ecx, %rbx
cltq
movq %rax, 0x40(%rsp)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq $0x0, (%rsp)
movq 0x1d591(%rip), %rax     # 0x211a8
movl %edi, 0xc(%rsp)
movq %r12, 0x30(%rsp)
movslq (%rax,%rbx,4), %r15
cmpl %r12d, %r15d
jle 0x3cd7
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl %r12d, %ebp
jl 0x3d77
cmpl %edi, %r15d
jg 0x3d77
movq 0x1d56c(%rip), %rax     # 0x211b8
movslq %edx, %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x3d77
movl %edx, %r14d
leaq (%rax,%rcx,4), %rdi
movq (%rsp), %rbp
movslq %ebp, %rsi
movq 0x10(%rsp), %r12
addq %r12, %rsi
movl 0x8(%rsp), %edx
callq 0x3b3f
testl %eax, %eax
js 0x3d77
addl %ebp, %eax
movq %r12, %rcx
movq 0x1d51c(%rip), %rdx     # 0x211a8
movq %rdx, 0x20(%rsp)
movl 0x4(%rdx,%rbx,4), %ebp
movl 0xc(%rsp), %r12d
cmpl %r12d, %ebp
cmovgel %r12d, %ebp
movslq %eax, %r13
leaq (%rcx,%r13), %rdi
movq %r15, %rsi
addq 0x38(%rsp), %rsi
movl %ebp, %eax
subl %r15d, %eax
movslq %eax, %r15
movq %r15, %rdx
callq 0x1240
movl %r12d, %edi
addl %r13d, %r15d
movq %r15, (%rsp)
movl %r14d, %edx
addl $0x3, %edx
movq 0x30(%rsp), %r12
jmp 0x3d21
movq %rax, 0x20(%rsp)
movl 0x4(%rax,%rbx,4), %ebp
cmpl %r12d, %ebp
jle 0x3d1e
cmpl %edi, %ebp
cmovgel %edi, %ebp
movq (%rsp), %r14
movslq %r14d, %rdi
addq 0x10(%rsp), %rdi
movl %ebp, %eax
subl %r12d, %eax
movslq %eax, %r13
movq 0x18(%rsp), %rsi
movl %edx, %r15d
movq %r13, %rdx
callq 0x1240
movl %r15d, %edx
movl 0xc(%rsp), %edi
addl %r14d, %r13d
movq %r13, (%rsp)
jmp 0x3d21
addl $0x3, %edx
cmpl %edi, %ebp
jge 0x3d59
addq $0x3, %rbx
cmpq 0x40(%rsp), %rbx
movq 0x20(%rsp), %rax
jl 0x3c20
movq 0x1d2b8(%rip), %rax     # 0x20ff8
movq (%rax), %rcx
leaq 0x17782(%rip), %rdi     # 0x1b4cc
movl $0x19, %esi
movl $0x1, %edx
callq 0x12a0
movq (%rsp), %rax
movl %eax, %r13d
movq 0x28(%rsp), %rax
cmpl $0x0, (%rax)
movq 0x10(%rsp), %rdi
jns 0x3d77
movl %r13d, %esi
callq 0xa119
movl %r13d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)