// File: /workspace/github_c_makefile_star2_O3/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, 0x8(%rsp)
movq %rdx, %r14
movl %esi, 0x4(%rsp)
movq %rdi, 0x10(%rsp)
testl %r8d, %r8d
jle 0x5dd2
movq %r9, %r12
movl %r8d, %ebx
xorl %r15d, %r15d
movq %r15, %rbp
shlq $0x4, %rbp
movq 0x8(%r12,%rbp), %r13
movq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x21b0
testl %eax, %eax
jne 0x5d82
addq %r12, %rbp
movl (%rbp), %ecx
cmpl 0x4(%rsp), %ecx
jae 0x5d7b
movslq (%r14), %rax
movq 0x8(%rsp), %rdx
movq (%rdx), %rdi
xorl %r13d, %r13d
testq %rax, %rax
jle 0x5d2d
cmpl (%rdi,%r13,4), %ecx
jbe 0x5d2d
addq $0x2, %r13
cmpl %r13d, %eax
jg 0x5d1e
leaq 0x2(%rax), %rcx
movl %ecx, (%r14)
leaq 0x8(,%rax,4), %rsi
callq 0x3925
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movslq (%r14), %rcx
movl %r13d, %edx
orl $0x1, %edx
leal -0x1(%rcx), %esi
cmpl %edx, %esi
jle 0x5d6e
decq %rcx
movslq %edx, %rdx
movl -0x8(%rax,%rcx,4), %esi
movl %esi, (%rax,%rcx,4)
decq %rcx
cmpq %rdx, %rcx
jg 0x5d5f
movl %r13d, %ecx
movq (%rbp), %rdx
movq %rdx, (%rax,%rcx,4)
jmp 0x5d82
cmpb $0x0, 0x50(%rsp)
jne 0x5d90
incq %r15
cmpq %rbx, %r15
jne 0x5cde
jmp 0x5dd2
movq 0x14261(%rip), %rax     # 0x19ff8
movq (%rax), %rdi
movl 0x4(%rbp), %r8d
leaq 0xd847(%rip), %rsi      # 0x135ec
movq %r13, %rdx
xorl %eax, %eax
callq 0x21d0
movq 0x14242(%rip), %rax     # 0x19ff8
movq (%rax), %rdi
leaq 0xd859(%rip), %rsi      # 0x13619
movq 0x10(%rsp), %rdx
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0x21d0
jmp 0x5d82
cmpl $0x0, (%r14)
movl 0x4(%rsp), %r13d
movb 0x50(%rsp), %sil
jle 0x5ec5
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x14203(%rip), %rbx     # 0x19ff8
leaq 0xd84e(%rip), %r9       # 0x1364a
xorl %r15d, %r15d
movslq %r15d, %rbp
leaq 0x1(%rbp), %r12
movl 0x4(%rax,%rbp,4), %r8d
cmpl %r13d, %r8d
jbe 0x5e19
testb %sil, %sil
jne 0x5e67
movl %r13d, (%rax,%r12,4)
testl %r15d, %r15d
je 0x5e27
movl -0x4(%rax,%rbp,4), %ecx
cmpl %ecx, (%rax,%rbp,4)
jbe 0x5e30
addl $0x2, %r15d
movl (%r14), %ecx
jmp 0x5e60
movl (%rax,%r12,4), %edx
cmpl %ecx, %edx
jbe 0x5e3c
movl %edx, -0x4(%rax,%rbp,4)
movl (%r14), %ecx
addl $-0x2, %ecx
cmpl %ecx, %r15d
jge 0x5e5d
movl 0x8(%rax,%rbp,4), %ecx
movl %ecx, (%rax,%rbp,4)
incq %rbp
movslq (%r14), %rcx
addq $-0x2, %rcx
cmpq %rcx, %rbp
jl 0x5e47
movl %ecx, (%r14)
cmpl %ecx, %r15d
jl 0x5dff
jmp 0x5ec5
movq (%rbx), %rdi
movslq %r15d, %r13
movl (%rax,%r13,4), %ecx
movq %r9, %rsi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x21d0
movq (%rbx), %rdi
movq 0x8(%rsp), %rbx
movq (%rbx), %rax
movl (%rax,%r13,4), %ecx
movl 0x4(%rsp), %r13d
leaq 0xd7dc(%rip), %rsi      # 0x13677
movq 0x10(%rsp), %rdx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x21d0
leaq 0xd799(%rip), %r9       # 0x1364a
movq (%rbx), %rax
movq 0x1413d(%rip), %rbx     # 0x19ff8
movb 0x50(%rsp), %sil
jmp 0x5e15
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq