// File: /workspace/github_c_makefile_star2_O0/zhou-lab[P]tbmate/view.c
// Function: parse_regions
static char **parse_regions(char *regions_fname, char *region, int *nregs) {
  kstring_t str = {0,0,0};
  int iseq = 0, ireg = 0;
  char **regs = NULL;
  *nregs = 0;

  if (regions_fname) {
    // improve me: this is a too heavy machinery for parsing regions...
    regidx_t *idx = regidx_init(regions_fname, NULL, NULL, 0, NULL);
    if (!idx) error("Could not build region list for \"%s\"\n", regions_fname);

    (*nregs) += regidx_nregs(idx);
    regs = (char**) malloc(sizeof(char*)*(*nregs));

    int nseq;
    char **seqs = regidx_seq_names(idx, &nseq);
    for (iseq=0; iseq<nseq; iseq++)
    {
      regitr_t itr;
      regidx_overlap(idx, seqs[iseq], 0, UINT32_MAX, &itr);
      while ( itr.i < itr.n )
      {
        str.l = 0;
        ksprintf(&str, "%s:%d-%d", seqs[iseq], REGITR_START(itr)+1, REGITR_END(itr)+1);
        regs[ireg++] = strdup(str.s);
        itr.i++;
      }
    }
    regidx_destroy(idx);
  }
  free(str.s);

  if (region) {

    char **fields; int nfields;
    line_get_fields(region, ",", &fields, &nfields);
    regs = realloc(regs, sizeof(char*)*((*nregs)+nfields));
    int i;
    for (i=0; i<nfields; ++i) {
      regs[(*nregs)+i] = fields[i];
    }
    (*nregs) += nfields;
    free(fields);
  }

  /* whole file */
  if (!*nregs) {
    regs = (char**) malloc(sizeof(char*));
    if (!regs) error(NULL);
    regs[0] = strdup(".");
    if (!regs[0]) error(NULL);
    *nregs = 1;
  }
  
  return regs;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x3090
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x18(%rbp), %rax
movl $0x0, (%rax)
cmpq $0x0, -0x8(%rbp)
je 0x57c4
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rsi
movq %r8, %rdx
movq %r8, %rcx
callq 0x16c70
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x56c6
movq -0x8(%rbp), %rsi
leaq 0x7026e(%rip), %rdi     # 0x7592d
movb $0x0, %al
callq 0x62a0
movq -0x48(%rbp), %rdi
callq 0x162f0
movl %eax, %ecx
movq -0x18(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movslq (%rax), %rdi
shlq $0x3, %rdi
callq 0x3250
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
callq 0x16340
movq %rax, -0x58(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x57bb
movq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
leaq -0x70(%rbp), %r8
callq 0x174a0
movl -0x70(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jge 0x57ab
movq $0x0, -0x30(%rbp)
movq -0x58(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x68(%rbp), %rax
movslq -0x70(%rbp), %rcx
movl (%rax,%rcx,8), %ecx
addl $0x1, %ecx
movq -0x68(%rbp), %rax
movslq -0x70(%rbp), %rsi
movl 0x4(%rax,%rsi,8), %r8d
addl $0x1, %r8d
leaq -0x30(%rbp), %rdi
leaq 0x701dc(%rip), %rsi     # 0x75953
movb $0x0, %al
callq 0xb660
movq -0x20(%rbp), %rdi
callq 0x35f0
movq %rax, %rdx
movq -0x40(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x38(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x5731
jmp 0x57ad
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x5705
movq -0x48(%rbp), %rdi
callq 0x17300
movq -0x20(%rbp), %rdi
callq 0x3370
cmpq $0x0, -0x10(%rbp)
je 0x585b
movq -0x10(%rbp), %rdi
leaq 0x70179(%rip), %rsi     # 0x7595c
leaq -0x78(%rbp), %rdx
leaq -0x7c(%rbp), %rcx
callq 0x6060
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %eax
addl -0x7c(%rbp), %eax
movslq %eax, %rsi
shlq $0x3, %rsi
callq 0x37c0
movq %rax, -0x40(%rbp)
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x7c(%rbp), %eax
jge 0x5847
movq -0x78(%rbp), %rax
movslq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
movl (%rcx), %ecx
addl -0x80(%rbp), %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x5814
movl -0x7c(%rbp), %ecx
movq -0x18(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movq -0x78(%rbp), %rdi
callq 0x3370
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x58b9
movl $0x8, %edi
callq 0x3250
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x5884
xorl %eax, %eax
movl %eax, %edi
movb $0x0, %al
callq 0x62a0
leaq 0x70624(%rip), %rdi     # 0x75eaf
callq 0x35f0
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, (%rax)
movq -0x40(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x58af
xorl %eax, %eax
movl %eax, %edi
movb $0x0, %al
callq 0x62a0
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
movq -0x40(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)