// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x58, %rsp
movb 0x60(%rsp), %al
movq %rdi, 0x50(%rsp)
movl %esi, 0x4c(%rsp)
movq %rdx, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movl %r8d, 0x34(%rsp)
movq %r9, 0x28(%rsp)
andb $0x1, %al
movb %al, 0x27(%rsp)
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0x6b52
movq 0x28(%rsp), %rax
movslq 0x20(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movq 0x50(%rsp), %rdi
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rsi
callq 0x21a0
cmpl $0x0, %eax
jne 0x6b40
movq 0x18(%rsp), %rax
movl (%rax), %eax
cmpl 0x4c(%rsp), %eax
jb 0x6a4e
testb $0x1, 0x27(%rsp)
je 0x6a49
movq 0x1c5fa(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdx
movq 0x18(%rsp), %rax
movl (%rax), %ecx
movq 0x18(%rsp), %rax
movl 0x4(%rax), %r8d
leaq 0x18d4b(%rip), %rsi     # 0x1f76c
movb $0x0, %al
callq 0x21c0
movq 0x1c5c9(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
movq 0x50(%rsp), %rdx
movl 0x4c(%rsp), %ecx
leaq 0x18d57(%rip), %rsi     # 0x1f799
movb $0x0, %al
callq 0x21c0
jmp 0x6b42
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
movq 0x40(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x6a8d
movq 0x18(%rsp), %rax
movl (%rax), %eax
movq 0x38(%rsp), %rcx
movq (%rcx), %rcx
movslq 0x14(%rsp), %rdx
cmpl (%rcx,%rdx,4), %eax
ja 0x6a7e
jmp 0x6a8d
jmp 0x6a80
movl 0x14(%rsp), %eax
addl $0x2, %eax
movl %eax, 0x14(%rsp)
jmp 0x6a56
movq 0x40(%rsp), %rax
movl (%rax), %ecx
addl $0x2, %ecx
movl %ecx, (%rax)
movq 0x38(%rsp), %rax
movq (%rax), %rdi
movq 0x40(%rsp), %rax
movslq (%rax), %rsi
shlq $0x2, %rsi
callq 0x28e0
movq %rax, %rcx
movq 0x38(%rsp), %rax
movq %rcx, (%rax)
movq 0x40(%rsp), %rax
movl (%rax), %eax
subl $0x1, %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jle 0x6b0c
movq 0x38(%rsp), %rax
movq (%rax), %rax
movl 0x10(%rsp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
movq 0x38(%rsp), %rax
movq (%rax), %rax
movslq 0x10(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movl 0x10(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x6acb
movq 0x18(%rsp), %rax
movl (%rax), %edx
movq 0x38(%rsp), %rax
movq (%rax), %rax
movslq 0x14(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movq 0x18(%rsp), %rax
movl 0x4(%rax), %edx
movq 0x38(%rsp), %rax
movq (%rax), %rax
movl 0x14(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x6b42
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x69a3
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
movq 0x40(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x6d15
movq 0x38(%rsp), %rax
movq (%rax), %rax
movl 0xc(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
cmpl 0x4c(%rsp), %eax
jbe 0x6c1f
testb $0x1, 0x27(%rsp)
je 0x6c06
movq 0x1c460(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
movq 0x50(%rsp), %rdx
movq 0x38(%rsp), %rax
movq (%rax), %rax
movslq 0xc(%rsp), %rcx
movl (%rax,%rcx,4), %ecx
movq 0x38(%rsp), %rax
movq (%rax), %rax
movl 0xc(%rsp), %esi
addl $0x1, %esi
movslq %esi, %rsi
movl (%rax,%rsi,4), %r8d
leaq 0x18bfd(%rip), %rsi     # 0x1f7ca
movb $0x0, %al
callq 0x21c0
movq 0x1c41d(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
movq 0x50(%rsp), %rdx
movq 0x38(%rsp), %rax
movq (%rax), %rax
movslq 0xc(%rsp), %rcx
movl (%rax,%rcx,4), %ecx
movl 0x4c(%rsp), %r8d
leaq 0x18bf8(%rip), %rsi     # 0x1f7f7
movb $0x0, %al
callq 0x21c0
movl 0x4c(%rsp), %edx
movq 0x38(%rsp), %rax
movq (%rax), %rax
movl 0xc(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
cmpl $0x0, 0xc(%rsp)
je 0x6d05
movq 0x38(%rsp), %rax
movq (%rax), %rax
movslq 0xc(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movq 0x38(%rsp), %rcx
movq (%rcx), %rcx
movl 0xc(%rsp), %edx
subl $0x1, %edx
movslq %edx, %rdx
cmpl (%rcx,%rdx,4), %eax
ja 0x6d05
movq 0x38(%rsp), %rax
movq (%rax), %rax
movl 0xc(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movq 0x38(%rsp), %rcx
movq (%rcx), %rcx
movl 0xc(%rsp), %edx
subl $0x1, %edx
movslq %edx, %rdx
cmpl (%rcx,%rdx,4), %eax
jbe 0x6cab
movq 0x38(%rsp), %rax
movq (%rax), %rax
movl 0xc(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
movq 0x38(%rsp), %rax
movq (%rax), %rax
movl 0xc(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl 0xc(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
movq 0x40(%rsp), %rcx
movl (%rcx), %ecx
subl $0x2, %ecx
cmpl %ecx, %eax
jge 0x6cf7
movq 0x38(%rsp), %rax
movq (%rax), %rax
movl 0x8(%rsp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
movq 0x38(%rsp), %rax
movq (%rax), %rax
movslq 0x8(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0x6cb3
movq 0x40(%rsp), %rax
movl (%rax), %ecx
subl $0x2, %ecx
movl %ecx, (%rax)
jmp 0x6d10
movl 0xc(%rsp), %eax
addl $0x2, %eax
movl %eax, 0xc(%rsp)
jmp 0x6b5a
addq $0x58, %rsp
retq
nopw (%rax,%rax)