// File: /workspace/github_c_makefile_star2_O1/thegenemyers[P]DASCRUBBER/DASrealign.c
// Function: main
int main(int argc, char *argv[])
{ DAZZ_TRACK *map1, *map2;

  //  Process arguments

  { int  i, j, k;
    int  flags[128];
    char *eptr;

    ARG_INIT("DASrealign")

    MIN_LEN = 800;

    j = 1;
    for (i = 1; i < argc; i++)
      if (argv[i][0] == '-')
        switch (argv[i][1])
        { default:
            ARG_FLAGS("v")
            break;
          case 'l':
            ARG_POSITIVE(MIN_LEN,"Minimum piece length to recompute")
            break;
        }
      else
        argv[j++] = argv[i];
    argc = j;

    VERBOSE = flags['v'];

    if (argc != 5)
      { fprintf(stderr,"Usage: %s %s\n",Prog_Name,Usage);
        fprintf(stderr,"\n");
        fprintf(stderr,"      -v: Verbose mode, output statistics as proceed.\n");
        fprintf(stderr,"      -l: minimum length alignment length.\n");
        exit (1);
      }
  }

  { int status;

    status = Open_DB(argv[1],ADB);
    if (status < 0)
      exit (1);
    if (status == 1)
      { fprintf(stderr,"%s: Cannot be called on a .dam index: %s\n",Prog_Name,argv[1]);
        exit (1);
      }
    if ( ! ADB->part)
      { fprintf(stderr,"%s: Must be called on a block: %s\n",Prog_Name,argv[1]);
        exit (1);
      }
    Trim_DB(ADB);

    Load_All_Reads(ADB,0);
  }

  if (strcmp(argv[1],argv[2]) == 0)
    BDB = ADB;
  else
    { int status;

      status = Open_DB(argv[2],BDB);
      if (status < 0)
        exit (1);
      if (status == 1)
        { fprintf(stderr,"%s: Cannot be called on a .dam index: %s\n",Prog_Name,argv[1]);
          exit (1);
        }
      if ( ! BDB->part)
        { fprintf(stderr,"%s: Must be called on a block: %s\n",Prog_Name,argv[1]);
          exit (1);
        }
      Trim_DB(BDB);

      Load_All_Reads(BDB,0);
    }
  AFIRST = ADB->tfirst;
  BFIRST = BDB->tfirst;

  map1 = Open_Track(ADB,"map");
  if (map1 != NULL)
    { int i, o, q, n; 

      Load_All_Track_Data(map1);
 
      AMAP_IDX = (int64 *) map1->anno;
      AMAP     = (int *) map1->data;
      for (i = 0; i <= ADB->nreads; i++)
        AMAP_IDX[i] /= sizeof(int);

      ADB_ofirst = AMAP[AMAP_IDX[0]];
      ADB_olast  = AMAP[AMAP_IDX[ADB->nreads-1]]+1;
      IAMAP = (int *) Malloc(sizeof(int)*((ADB_olast-ADB_ofirst)+1),"Inverse map") - ADB_ofirst;

      IAMAP[q = ADB_olast] = n = ADB->nreads;
      for (i = ADB->nreads-1; i >= 0; i--)
        { o = AMAP[AMAP_IDX[i]];
          if (q > o)
            while (--q > o)
              IAMAP[q] = n;
          IAMAP[o] = n = i;
        }
    }
  else
    { fprintf(stderr,"%s: Must have a 'map' track, run DASedit\n",Prog_Name);
      exit (1);
    }

  if (BDB == ADB)
    { map2 = map1;
      BMAP_IDX = AMAP_IDX;
      BMAP = AMAP;
      BDB_ofirst = ADB_ofirst;
      BDB_olast  = ADB_olast ;
      IBMAP = IAMAP;
    }
  else
    { map2 = Open_Track(BDB,"map");
      if (map2 != NULL)
        { int i, o, q, n; 

          Load_All_Track_Data(map2);
     
          BMAP_IDX = (int64 *) map2->anno;
          BMAP     = (int *) map2->data;
          for (i = 0; i <= BDB->nreads; i++)
            BMAP_IDX[i] /= sizeof(int);

          BDB_ofirst = BMAP[BMAP_IDX[0]];
          BDB_olast  = BMAP[BMAP_IDX[BDB->nreads-1]]+1;
          IBMAP = (int *) Malloc(sizeof(int)*((BDB_olast-BDB_ofirst)+1),"Inverse map") - BDB_ofirst;

          IBMAP[q = BDB_olast] = n = BDB->nreads;
          for (i = BDB->nreads-1; i >= 0; i--)
            { o = BMAP[BMAP_IDX[i]];
              if (q > o)
                while (--q > o)
                  IBMAP[q] = n;
              IBMAP[o] = n = i;
            }
        }
      else
        { fprintf(stderr,"%s: Must have a 'map' track, run DASedit\n",Prog_Name);
          exit (1);
        }
    }

  ADB_ofirst = AMAP[AMAP_IDX[0]];
  BDB_ofirst = BMAP[BMAP_IDX[0]];
  ADB_olast  = AMAP[AMAP_IDX[ADB->nreads-1]]+1;
  BDB_olast  = BMAP[BMAP_IDX[BDB->nreads-1]]+1;

  //  Open .las and process piles therein output new piles to F.las

  { FILE *input;
    char *las, *pwd;
    char *lasT, *pwdT;
    int64 novl;

    las = Root(argv[3],".las");
    pwd = PathTo(argv[3]);

    lasT = Root(argv[4],".las");
    pwdT = PathTo(argv[4]);

    if (strcmp(las,lasT) == 0 && strcmp(pwd,pwdT) == 0)
      { fprintf(stderr,"%s: source and target are the same !\n",Prog_Name);
        exit (1);
      }

    input  = Fopen(Catenate(pwd,"/",las,".las"),"r");
    OUTPUT = Fopen(Catenate(pwdT,"/",lasT,".las"),"w");
    if (input == NULL || OUTPUT == NULL)
      exit (1);
    free(pwd);
    free(las);

    WOVLS = 0;
    TRACE_SPACING = 0;
    fwrite(&WOVLS,sizeof(int64),1,OUTPUT);
    fwrite(&TRACE_SPACING,sizeof(int),1,OUTPUT);

    fread(&novl,sizeof(int64),1,input);
    fread(&TRACE_SPACING,sizeof(int),1,input);

    make_a_pass(input,EXTENDER,1);

    rewind(OUTPUT);
    fwrite(&WOVLS,sizeof(int64),1,OUTPUT);
    fclose(OUTPUT);
  }

  exit (0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, 0x8(%rsp)
movl %edi, %ebp
leaq 0x18ad9(%rip), %rdi     # 0x1b038
leaq 0x198a7(%rip), %rsi     # 0x1be0d
callq 0x131ea
leaq 0x1ee66(%rip), %r13     # 0x213d8
movq %rax, (%r13)
movl $0x320, 0x1ec1c(%rip)   # imm = 0x320
cmpl $0x2, %ebp
jl 0x2694
movl %ebp, %r14d
movl $0x1, %ebx
movl $0x1, %r13d
leaq 0x18aa5(%rip), %r15     # 0x1b043
movq 0x8(%rsp), %rbp
movq (%rbp,%r13,8), %r12
cmpb $0x2d, (%r12)
jne 0x25f9
cmpb $0x6c, 0x1(%r12)
jne 0x2605
addq $0x2, %r12
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
movl $0xa, %edx
callq 0x11c0
movl %eax, 0x1ebc9(%rip)     # 0x2119c
movq 0x10(%rsp), %rcx
cmpb $0x0, (%rcx)
jne 0x2726
movq (%rbp,%r13,8), %rcx
cmpb $0x0, 0x2(%rcx)
je 0x2726
testl %eax, %eax
jg 0x2638
jmp 0x2758
movslq %ebx, %rax
incl %ebx
movq %r12, (%rbp,%rax,8)
jmp 0x2638
movb 0x1(%r12), %al
testb %al, %al
je 0x2638
addq $0x2, %r12
movsbl %al, %ebp
movq %r15, %rdi
movl %ebp, %esi
callq 0x1140
testq %rax, %rax
je 0x26f8
movb (%r12), %al
incq %r12
testb %al, %al
jne 0x2612
movq 0x8(%rsp), %rbp
incq %r13
cmpq %r14, %r13
jne 0x25a3
cmpl $0x5, %ebx
leaq 0x1ed8a(%rip), %r13     # 0x213d8
jne 0x2694
movq 0x8(%rsp), %rbp
movq 0x8(%rbp), %rdi
leaq 0x1eb98(%rip), %rsi     # 0x211f8
callq 0x140e8
testl %eax, %eax
js 0x271c
cmpl $0x1, %eax
jne 0x2786
movq 0x1e97b(%rip), %rax     # 0x20ff8
movq (%rax), %rdi
movq (%r13), %rdx
movq 0x8(%rbp), %rcx
leaq 0x18aae(%rip), %rsi     # 0x1b13d
jmp 0x27a8
movq 0x1e95d(%rip), %rbx     # 0x20ff8
movq (%rbx), %rdi
movq (%r13), %rdx
leaq 0x18a23(%rip), %rsi     # 0x1b0cc
leaq 0x18b3d(%rip), %rcx     # 0x1b1ed
xorl %eax, %eax
callq 0x11a0
movq (%rbx), %rsi
movl $0xa, %edi
callq 0x1170
movq (%rbx), %rcx
leaq 0x18a0c(%rip), %rdi     # 0x1b0da
movl $0x36, %esi
movl $0x1, %edx
callq 0x1280
movq (%rbx), %rcx
leaq 0x18a2a(%rip), %rdi     # 0x1b111
movl $0x2b, %esi
movl $0x1, %edx
callq 0x1280
jmp 0x271c
movq 0x1e8f9(%rip), %rax     # 0x20ff8
movq (%rax), %rdi
leaq 0x1eccf(%rip), %rax     # 0x213d8
movq (%rax), %rdx
leaq 0x18932(%rip), %rsi     # 0x1b045
movl %ebp, %ecx
xorl %eax, %eax
callq 0x11a0
movl $0x1, %edi
callq 0x1270
movq 0x1e8cb(%rip), %rax     # 0x20ff8
movq (%rax), %rdi
leaq 0x1eca1(%rip), %rax     # 0x213d8
movq (%rax), %rdx
movq (%rbp,%r13,8), %r8
movsbl 0x1(%r8), %ecx
addq $0x2, %r8
leaq 0x18914(%rip), %rsi     # 0x1b063
xorl %eax, %eax
callq 0x11a0
jmp 0x271c
movq 0x1e899(%rip), %rcx     # 0x20ff8
movq (%rcx), %rdi
leaq 0x1ec6f(%rip), %rcx     # 0x213d8
movq (%rcx), %rdx
leaq 0x18919(%rip), %rsi     # 0x1b08c
leaq 0x18930(%rip), %rcx     # 0x1b0aa
movl %eax, %r8d
xorl %eax, %eax
callq 0x11a0
jmp 0x271c
cmpl $0x0, 0x1eaa3(%rip)     # 0x21230
jne 0x27b4
movq 0x1e862(%rip), %rax     # 0x20ff8
movq (%rax), %rdi
movq (%r13), %rdx
movq 0x8(%rbp), %rcx
leaq 0x189bf(%rip), %rsi     # 0x1b167
xorl %eax, %eax
callq 0x11a0
jmp 0x271c
leaq 0x1ea3d(%rip), %r14     # 0x211f8
movq %r14, %rdi
callq 0x148ad
movq %r14, %rdi
xorl %esi, %esi
callq 0x157c6
movq 0x8(%rbp), %rdi
movq 0x10(%rbp), %r15
movq %r15, %rsi
callq 0x1190
testl %eax, %eax
jne 0x2840
movq %r14, 0x1e980(%rip)     # 0x21168
leaq 0x1ea09(%rip), %r14     # 0x211f8
movl 0x1ea43(%rip), %eax     # 0x21238
movl %eax, 0x1e9a5(%rip)     # 0x211a0
movq 0x1e966(%rip), %rax     # 0x21168
movl 0x40(%rax), %eax
movl %eax, 0x1e999(%rip)     # 0x211a4
leaq 0x18980(%rip), %rsi     # 0x1b192
movq %r14, %rdi
callq 0x15f53
testq %rax, %rax
jne 0x2889
movq 0x1e7d2(%rip), %rax     # 0x20ff8
movq (%rax), %rdi
movq (%r13), %rdx
leaq 0x18962(%rip), %rsi     # 0x1b196
xorl %eax, %eax
callq 0x11a0
jmp 0x271c
movq 0x1e921(%rip), %rsi     # 0x21168
movq %r15, %rdi
callq 0x140e8
testl %eax, %eax
js 0x271c
cmpl $0x1, %eax
je 0x2676
movq 0x1e901(%rip), %rdi     # 0x21168
cmpl $0x0, 0x38(%rdi)
je 0x278f
callq 0x148ad
movq 0x1e8eb(%rip), %rdi     # 0x21168
xorl %esi, %esi
callq 0x157c6
jmp 0x27e8
movq %rax, %r15
movq %rax, %rdi
callq 0x1685d
movq 0x18(%r15), %rax
movq %rax, 0x1e909(%rip)     # 0x211a8
movq 0x28(%r15), %rcx
movq %rcx, 0x1e906(%rip)     # 0x211b0
movl 0x1e978(%rip), %edx     # 0x21228
testl %edx, %edx
js 0x28c7
leaq 0x1(%rdx), %rsi
xorl %edi, %edi
shrq $0x2, (%rax,%rdi,8)
incq %rdi
cmpq %rdi, %rsi
jne 0x28ba
movq (%rax), %rsi
movl (%rcx,%rsi,4), %esi
movl %esi, 0x1e8e5(%rip)     # 0x211b8
movslq %edx, %rdx
movq -0x8(%rax,%rdx,8), %rax
movl (%rcx,%rax,4), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x1e8d5(%rip)     # 0x211bc
subl %esi, %eax
addl $0x2, %eax
movslq %eax, %rdi
shlq $0x2, %rdi
leaq 0x18890(%rip), %rsi     # 0x1b18a
callq 0x1312d
movslq 0x1e8b2(%rip), %rcx     # 0x211b8
leaq (,%rcx,4), %rdx
subq %rdx, %rax
movq %rax, 0x1e8a8(%rip)     # 0x211c0
movl 0x1e90a(%rip), %edi     # 0x21228
movslq 0x1e897(%rip), %rdx     # 0x211bc
movl %edi, (%rax,%rdx,4)
movslq 0x1e8f9(%rip), %rsi     # 0x21228
testq %rsi, %rsi
jle 0x2986
movq 0x1e875(%rip), %r8      # 0x211b0
movq 0x1e866(%rip), %r9      # 0x211a8
movl %edx, %ebx
movq -0x8(%r9,%rsi,8), %r10
movslq (%r8,%r10,4), %r10
cmpl %r10d, %ebx
jle 0x296f
leal -0x1(%rbx), %r11d
cmpl %r10d, %r11d
jle 0x2972
movslq %ebx, %r11
decq %r11
movl %edi, (%rax,%r11,4)
decq %r11
cmpq %r10, %r11
jg 0x2961
jmp 0x2972
movl %ebx, %r11d
leaq -0x1(%rsi), %rdi
movl %edi, (%rax,%r10,4)
movl %r11d, %ebx
cmpq $0x1, %rsi
movq %rdi, %rsi
jg 0x2944
movq 0x1e7db(%rip), %rdi     # 0x21168
cmpq %r14, %rdi
jne 0x2be1
movq 0x1e80b(%rip), %rsi     # 0x211a8
movq %rsi, 0x1e824(%rip)     # 0x211c8
movq 0x1e805(%rip), %rsi     # 0x211b0
movq %rsi, 0x1e81e(%rip)     # 0x211d0
movl %ecx, 0x1e820(%rip)     # 0x211d8
movl %edx, 0x1e81e(%rip)     # 0x211dc
movq %rax, 0x1e81b(%rip)     # 0x211e0
movq 0x1e7e4(%rip), %rax     # 0x211b0
movq 0x1e7d5(%rip), %rcx     # 0x211a8
movq (%rcx), %rdx
movl (%rax,%rdx,4), %edx
movl %edx, 0x1e7d9(%rip)     # 0x211b8
movq 0x1e7ea(%rip), %rdx     # 0x211d0
movq 0x1e7db(%rip), %rsi     # 0x211c8
movq (%rsi), %rdi
movl (%rdx,%rdi,4), %edi
movl %edi, 0x1e7df(%rip)     # 0x211d8
movslq 0x1e828(%rip), %rdi     # 0x21228
movq -0x8(%rcx,%rdi,8), %rcx
movl (%rax,%rcx,4), %eax
incl %eax
movl %eax, 0x1e7ac(%rip)     # 0x211bc
movq 0x1e751(%rip), %rax     # 0x21168
movslq 0x30(%rax), %rax
movq -0x8(%rsi,%rax,8), %rax
movl (%rdx,%rax,4), %eax
incl %eax
movl %eax, 0x1e7b1(%rip)     # 0x211dc
movq 0x18(%rbp), %rdi
leaq 0x1878a(%rip), %r12     # 0x1b1c0
movq %r12, %rsi
callq 0x13300
movq %rax, %r14
movq 0x18(%rbp), %rdi
callq 0x132a6
movq %rax, %rbx
movq 0x20(%rbp), %rdi
movq %r12, %rsi
callq 0x13300
movq %rax, %r12
movq 0x20(%rbp), %rdi
callq 0x132a6
movq %rax, %r15
movq %r14, %rdi
movq %r12, %rsi
callq 0x1190
testl %eax, %eax
jne 0x2aa0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1190
testl %eax, %eax
jne 0x2aa0
movq 0x1e56b(%rip), %rax     # 0x20ff8
movq (%rax), %rdi
movq (%r13), %rdx
leaq 0x1872a(%rip), %rsi     # 0x1b1c5
jmp 0x2834
leaq 0x18744(%rip), %rbp     # 0x1b1eb
leaq 0x18712(%rip), %r13     # 0x1b1c0
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0x133c5
leaq 0x19759(%rip), %rsi     # 0x1c224
movq %rax, %rdi
callq 0x13249
movq %rax, %rbx
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x133c5
leaq 0x19130(%rip), %rsi     # 0x1bc1e
movq %rax, %rdi
callq 0x13249
movq %rax, 0x1e6eb(%rip)     # 0x211e8
testq %rbx, %rbx
je 0x271c
movq %rax, %r12
testq %rax, %rax
je 0x271c
movq 0x8(%rsp), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
movq $0x0, 0x1e6c1(%rip) # 0x211f0
leaq 0x1e6ba(%rip), %r14     # 0x211f0
movl $0x0, 0x1e658(%rip)     # 0x21198
leaq 0x1e651(%rip), %r15     # 0x21198
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r12, %rcx
callq 0x1280
movq 0x1e685(%rip), %rcx     # 0x211e8
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
callq 0x1280
leaq 0x10(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x10b0
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x10b0
movq %rbx, %rdi
callq 0x2cfc
movq 0x1e638(%rip), %rdi     # 0x211e8
callq 0x1110
movq 0x1e62c(%rip), %rcx     # 0x211e8
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rdi
callq 0x1280
movq 0x1e613(%rip), %rdi     # 0x211e8
callq 0x10c0
xorl %edi, %edi
callq 0x1270
leaq 0x185aa(%rip), %rsi     # 0x1b192
callq 0x15f53
testq %rax, %rax
je 0x281f
movq %rax, %r14
movq %rax, %rdi
callq 0x1685d
movq 0x18(%r14), %rax
movq %rax, 0x1e5bc(%rip)     # 0x211c8
movq 0x28(%r14), %rcx
movq %rcx, 0x1e5b9(%rip)     # 0x211d0
movq 0x1e54a(%rip), %rdx     # 0x21168
movl 0x30(%rdx), %edx
testl %edx, %edx
js 0x2c38
leaq 0x1(%rdx), %rsi
xorl %edi, %edi
shrq $0x2, (%rax,%rdi,8)
incq %rdi
cmpq %rdi, %rsi
jne 0x2c2b
movq (%rax), %rsi
movl (%rcx,%rsi,4), %esi
movl %esi, 0x1e594(%rip)     # 0x211d8
movslq %edx, %rdx
movq -0x8(%rax,%rdx,8), %rax
movl (%rcx,%rax,4), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x1e584(%rip)     # 0x211dc
subl %esi, %eax
addl $0x2, %eax
movslq %eax, %rdi
shlq $0x2, %rdi
leaq 0x1851f(%rip), %rsi     # 0x1b18a
callq 0x1312d
movslq 0x1e561(%rip), %rcx     # 0x211d8
shlq $0x2, %rcx
subq %rcx, %rax
movq %rax, 0x1e55b(%rip)     # 0x211e0
movq 0x1e4dc(%rip), %rcx     # 0x21168
movl 0x30(%rcx), %edx
movslq 0x1e546(%rip), %r8      # 0x211dc
movl %edx, (%rax,%r8,4)
movslq 0x30(%rcx), %rcx
testq %rcx, %rcx
jle 0x29c5
movq 0x1e522(%rip), %rsi     # 0x211d0
movq 0x1e513(%rip), %rdi     # 0x211c8
movq -0x8(%rdi,%rcx,8), %r9
movslq (%rsi,%r9,4), %r9
cmpl %r9d, %r8d
jle 0x2ce0
leal -0x1(%r8), %r10d
cmpl %r9d, %r10d
jle 0x2ce3
movslq %r8d, %r10
decq %r10
movl %edx, (%rax,%r10,4)
decq %r10
cmpq %r9, %r10
jg 0x2cd2
jmp 0x2ce3
movl %r8d, %r10d
leaq -0x1(%rcx), %rdx
movl %edx, (%rax,%r9,4)
movl %r10d, %r8d
cmpq $0x1, %rcx
movq %rdx, %rcx
jg 0x2cb5
jmp 0x29c5