// File: /workspace/github_c_makefile_star2_O1/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 %rcx, %rbx
movq %rdx, %r14
movl %esi, 0x4(%rsp)
movq %rdi, 0x8(%rsp)
testl %r8d, %r8d
jle 0x5e8c
movq %r9, %r12
movl %r8d, %eax
movq %rax, 0x10(%rsp)
xorl %r15d, %r15d
movq %r15, %rbp
shlq $0x4, %rbp
movq 0x8(%r12,%rbp), %r13
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x21b0
testl %eax, %eax
jne 0x5e7e
addq %r12, %rbp
movl (%rbp), %ecx
cmpl 0x4(%rsp), %ecx
jae 0x5dde
movslq (%r14), %rax
testq %rax, %rax
jle 0x5e2b
movq (%rbx), %rdx
xorl %r13d, %r13d
cmpl (%rdx,%r13,4), %ecx
jbe 0x5e2e
addq $0x2, %r13
cmpl %r13d, %eax
jg 0x5dcd
jmp 0x5e2e
cmpb $0x0, 0x50(%rsp)
je 0x5e7e
movq 0x14208(%rip), %rax     # 0x19ff8
movq (%rax), %rdi
movl 0x4(%rbp), %r8d
leaq 0xd7ee(%rip), %rsi      # 0x135ec
movq %r13, %rdx
xorl %eax, %eax
callq 0x21d0
movq 0x141e9(%rip), %rax     # 0x19ff8
movq (%rax), %rdi
leaq 0xd800(%rip), %rsi      # 0x13619
movq 0x8(%rsp), %rdx
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0x21d0
jmp 0x5e7e
xorl %r13d, %r13d
leaq 0x2(%rax), %rcx
movl %ecx, (%r14)
movq (%rbx), %rdi
leaq 0x8(,%rax,4), %rsi
callq 0x3915
movq %rax, (%rbx)
movslq (%r14), %rdx
movl %r13d, %ecx
orl $0x1, %ecx
leal -0x1(%rdx), %esi
cmpl %ecx, %esi
jle 0x5e6d
decq %rdx
movslq %ecx, %rsi
movl -0x8(%rax,%rdx,4), %edi
movl %edi, (%rax,%rdx,4)
decq %rdx
cmpq %rsi, %rdx
jg 0x5e5e
movl (%rbp), %edx
movl %r13d, %esi
movl %edx, (%rax,%rsi,4)
movl 0x4(%rbp), %edx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
incq %r15
cmpq 0x10(%rsp), %r15
jne 0x5d92
cmpl $0x0, (%r14)
movl 0x4(%rsp), %r15d
jle 0x5f65
xorl %ebp, %ebp
movq (%rbx), %rax
movslq %ebp, %r12
leaq 0x1(%r12), %r13
movl 0x4(%rax,%r12,4), %r8d
cmpl %r15d, %r8d
jbe 0x5ec0
cmpb $0x0, 0x50(%rsp)
jne 0x5f10
movq (%rbx), %rax
movl %r15d, (%rax,%r13,4)
testl %ebp, %ebp
je 0x5ed2
movq (%rbx), %rax
movl -0x4(%rax,%r12,4), %ecx
cmpl %ecx, (%rax,%r12,4)
jbe 0x5ed7
addl $0x2, %ebp
jmp 0x5f09
movl (%rax,%r13,4), %edx
cmpl %ecx, %edx
jbe 0x5ee4
movl %edx, -0x4(%rax,%r12,4)
movl (%r14), %ecx
addl $-0x2, %ecx
cmpl %ecx, %ebp
jge 0x5f06
movl 0x8(%rax,%r12,4), %ecx
movl %ecx, (%rax,%r12,4)
incq %r12
movslq (%r14), %rcx
addq $-0x2, %rcx
cmpq %rcx, %r12
jl 0x5eee
movl %ecx, (%r14)
cmpl (%r14), %ebp
jl 0x5e9d
jmp 0x5f65
movq 0x140e1(%rip), %rcx     # 0x19ff8
movq (%rcx), %rdi
movslq %ebp, %r15
movl (%rax,%r15,4), %ecx
leaq 0xd722(%rip), %rsi      # 0x1364a
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x21d0
movq 0x140bd(%rip), %rax     # 0x19ff8
movq (%rax), %rdi
movq (%rbx), %rax
movl (%rax,%r15,4), %ecx
movl 0x4(%rsp), %r15d
leaq 0xd726(%rip), %rsi      # 0x13677
movq 0x8(%rsp), %rdx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x21d0
jmp 0x5eb9
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq