// File: /workspace/github_c_makefile_star2_O2/jsh58[P]Genrich/Genrich.c
// Function: saveXBed
void saveXBed(char* name, uint32_t len, int* bedLen,
    uint32_t** bed, int xBedLen, Bed* xBed,
    bool verbose) {
  // check each xBed interval for match to chrom (name)
  for (int i = 0; i < xBedLen; i++) {
    Bed* b = xBed + i;
    if (!strcmp(name, b->name)) {

      // check if interval is located off end of chrom
      if (b->pos[0] >= len) {
        if (verbose) {
          fprintf(stderr, "Warning! BED interval (%s, %d - %d) ignored\n",
            b->name, b->pos[0], b->pos[1]);
          fprintf(stderr, "  - located off end of reference %s (length %d)\n",
            name, len);
        }
        continue;
      }

      // insert interval into array, sorted by start pos
      int j;
      for (j = 0; j < *bedLen; j += 2)
        if (b->pos[0] <= (*bed)[j])
          break;
      *bedLen += 2;
      *bed = (uint32_t*) memrealloc(*bed,
        *bedLen * sizeof(uint32_t));
      // shift intervals forward
      for (int k = *bedLen - 1; k > j + 1; k--)
        (*bed)[k] = (*bed)[k-2];
      (*bed)[j] = b->pos[0];
      (*bed)[j+1] = b->pos[1];
    }
  }

  // merge overlapping intervals
  int i = 0;
  while (i < *bedLen) {

    // check for interval past end of chrom
    if ((*bed)[i+1] > len) {
      if (verbose) {
        fprintf(stderr, "Warning! BED interval (%s, %d - %d) extends ",
          name, (*bed)[i], (*bed)[i+1]);
        fprintf(stderr, "past end of ref.\n  - edited to (%s, %d - %d)\n",
          name, (*bed)[i], len);
      }
      (*bed)[i+1] = len;
    }

    // check for overlap with previous
    if (i && (*bed)[i] <= (*bed)[i-1]) {
      if ((*bed)[i+1] > (*bed)[i-1])
        (*bed)[i-1] = (*bed)[i+1];
      // shift coordinates backward
      for (int j = i; j < *bedLen - 2; j++)
        (*bed)[j] = (*bed)[j + 2];
      *bedLen -= 2;
    } else
      i += 2;

  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, %r12
movq %rcx, (%rsp)
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, 0x8(%rsp)
xorl %eax, %eax
testl %r8d, %r8d
cmovlel %eax, %r8d
movq %r8, 0x10(%rsp)
xorl %r13d, %r13d
cmpq 0x10(%rsp), %r13
je 0x4b2c
movq %r13, %rbx
shlq $0x4, %rbx
movq 0x8(%r12,%rbx), %r15
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x21a0
testl %eax, %eax
jne 0x4b24
addq %r12, %rbx
movl (%rbx), %ecx
cmpl %ebp, %ecx
jae 0x4a96
movslq (%r14), %rax
movq (%rsp), %rdx
movq (%rdx), %rdi
xorl %r15d, %r15d
cmpq %rax, %r15
jge 0x4ae1
cmpl (%rdi,%r15,4), %ecx
jbe 0x4ae1
addq $0x2, %r15
jmp 0x4a85
cmpb $0x0, 0x50(%rsp)
je 0x4b24
movq 0x12550(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
movl 0x4(%rbx), %r8d
leaq 0xca86(%rip), %rsi      # 0x1153c
movq %r15, %rdx
xorl %eax, %eax
callq 0x21c0
movq 0x12531(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0xca98(%rip), %rsi      # 0x11569
movq 0x8(%rsp), %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x21c0
jmp 0x4b24
leal 0x2(%rax), %ecx
movl %ecx, (%r14)
leaq 0x8(,%rax,4), %rsi
callq 0x2776
movq (%rsp), %rcx
movq %rax, (%rcx)
movslq (%r14), %rcx
leal 0x1(%r15), %edx
movslq %edx, %rdx
decq %rcx
cmpq %rdx, %rcx
jle 0x4b16
movl -0x8(%rax,%rcx,4), %esi
movl %esi, (%rax,%rcx,4)
jmp 0x4b05
movl (%rbx), %ecx
movl %ecx, (%rax,%r15,4)
movl 0x4(%rbx), %ecx
movl %ecx, 0x4(%rax,%r15,4)
incq %r13
jmp 0x4a43
xorl %ebx, %ebx
leaq 0xca65(%rip), %r9       # 0x1159a
movb 0x50(%rsp), %sil
movq 0x8(%rsp), %r13
movl %ebx, %r15d
orl $0x1, %ebx
cmpl (%r14), %r15d
jge 0x4c05
movq (%rsp), %rax
movq (%rax), %rax
movl (%rax,%rbx,4), %r8d
cmpl %ebp, %r8d
jbe 0x4b69
testb %sil, %sil
jne 0x4ba7
movl %ebp, (%rax,%rbx,4)
movl %ebp, %r8d
testq %r15, %r15
je 0x4bfc
movl -0x4(%rax,%r15,4), %ecx
cmpl %ecx, (%rax,%r15,4)
ja 0x4bfc
cmpl %ecx, %r8d
jbe 0x4b87
movl %r8d, -0x4(%rax,%r15,4)
movq %r15, %rcx
movslq (%r14), %rdx
addq $-0x2, %rdx
cmpq %rdx, %rcx
jge 0x4ba2
movl 0x8(%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
incq %rcx
jmp 0x4b8a
movl %edx, (%r14)
jmp 0x4b45
movq 0x1244a(%rip), %r12     # 0x16ff8
movq (%r12), %rdi
movl (%rax,%r15,4), %ecx
movq %r9, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x21c0
movq (%r12), %rdi
movq (%rsp), %r12
movq (%r12), %rax
movl (%rax,%r15,4), %ecx
leaq 0xc9ed(%rip), %rsi      # 0x115c7
movq %r13, %rdx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x21c0
leaq 0xc9ac(%rip), %r9       # 0x1159a
movq (%r12), %rax
movb 0x50(%rsp), %sil
jmp 0x4b63
leal 0x2(%r15), %ebx
jmp 0x4b3f
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq