// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/test/sam.c
// Function: check_big_ref
static void check_big_ref(int parse_header)
{
    static const char sam_text[] = "data:,"
        "@HD\tVN:1.4\n"
        "@SQ\tSN:large#1\tLN:5000000000\n"
        "@SQ\tSN:small#1\tLN:100\n"
        "@SQ\tSN:large#2\tLN:4611686018427387904\n"
        "@SQ\tSN:small#2\tLN:1\n"
        "r1\t0\tlarge#1\t4999999000\t50\t8M\t*\t0\t0\tACGTACGT\tabcdefgh\n"
        "r2\t0\tsmall#1\t1\t50\t8M\t*\t0\t0\tACGTACGT\tabcdefgh\n"
        "r3\t0\tlarge#2\t4611686018427387000\t50\t8M\t*\t0\t0\tACGTACGT\tabcdefgh\n"
        "p1\t99\tlarge#2\t1\t50\t8M\t=\t4611686018427387895\t4611686018427387903\tACGTACGT\tabcdefgh\n"
        "p1\t147\tlarge#2\t4611686018427387895\t50\t8M\t=\t1\t-4611686018427387903\tACGTACGT\tabcdefgh\n"
        "r4\t0\tsmall#2\t2\t50\t8M\t*\t0\t0\tACGTACGT\tabcdefgh\n";
    const hts_pos_t expected_lengths[] = {
        5000000000LL, 100LL, 4611686018427387904LL, 1LL
    };
    const int expected_tids[] = {
        0, 1, 2, 2, 2, 3
    };
    const int expected_mtid[] = {
        -1, -1, -1, 2, 2, -1
    };
    const hts_pos_t expected_positions[] = {
        4999999000LL - 1, 1LL - 1, 4611686018427387000LL - 1, 1LL - 1,
        4611686018427387895LL - 1, 2LL - 1
    };
    const hts_pos_t expected_mpos[] = {
        -1, -1, -1, 4611686018427387895LL - 1, 1LL - 1, -1
    };
    samFile *in = NULL, *out = NULL;
    sam_hdr_t *header = NULL, *dup_header = NULL;
    bam1_t *aln = bam_init1();
    const int num_refs = sizeof(expected_lengths) / sizeof(expected_lengths[0]);
    const int num_align = sizeof(expected_tids) / sizeof(expected_tids[0]);
    const char *outfname = "test/sam_big_ref.tmp.sam_";
    int i, r;
    char buffer[sizeof(sam_text) + 1024];
    FILE *inf = NULL;
    size_t bytes;

    if (!aln) {
        fail("Out of memory");
        goto cleanup;
    }

    in = sam_open(sam_text, "r");
    if (!in) {
        fail("Opening SAM file");
        goto cleanup;
    }
    out = sam_open(outfname, "w");
    if (!out) {
        fail("Opening output SAM file \"%s\"", outfname);
        goto cleanup;
    }
    header = sam_hdr_read(in);
    if (!header) {
        fail("Reading SAM header");
        goto cleanup;
    }
    if (parse_header) {
        // This will force the header to be parsed
        if (sam_hdr_count_lines(header, "SQ") != num_refs) {
            fail("Wrong number of SQ lines in header");
            goto cleanup;
        }
    }
    if (check_ref_lengths(header, expected_lengths, num_refs, "header") < 0)
        goto cleanup;

    dup_header = sam_hdr_dup(header);
    if (!dup_header) {
        fail("Failed to duplicate header");
    }

    if (check_ref_lengths(dup_header, expected_lengths,
                          num_refs, "duplicate header") < 0)
        goto cleanup;

    if (sam_hdr_count_lines(dup_header, "SQ") != num_refs) {
        fail("Wrong number of SQ lines in duplicate header");
        goto cleanup;
    }

    if (check_ref_lengths(dup_header, expected_lengths,
                          num_refs, "parsed duplicate header") < 0)
        goto cleanup;

    if (sam_hdr_write(out, header) < 0) {
        fail("Failed to write SAM header");
        goto cleanup;
    }
    i = 0;
    while ((r = sam_read1(in, header, aln)) >= 0) {
        if (i >= num_align) {
            fail("Too many alignment records.\n");
            goto cleanup;
        }
        if (aln->core.tid != expected_tids[i]) {
            fail("Wrong tid for record %d : expected %d got %d\n",
                 i, expected_tids[i], aln->core.tid);
            goto cleanup;
        }
        if (aln->core.mtid != expected_mtid[i]) {
            fail("Wrong mate tid for record %d : expected %d got %d\n",
                 i, expected_mtid[i], aln->core.mtid);
            goto cleanup;
        }
        if (aln->core.pos != expected_positions[i]) {
            fail("Wrong position for record %d : "
                 "expected %"PRIhts_pos" got %"PRIhts_pos"\n",
                 i, expected_positions[i], aln->core.pos);
        }
        if (aln->core.mpos != expected_mpos[i]) {
            fail("Wrong mate position for record %d : "
                 "expected %"PRIhts_pos" got %"PRIhts_pos"\n",
                 i, expected_mpos[i], aln->core.mpos);
        }
        if (sam_write1(out, header, aln) < 0) {
            fail("Failed to write alignment record %d\n", i);
            goto cleanup;
        }
        i++;
    }
    if (r < -1) {
        fail("Error reading SAM alignment\n");
        goto cleanup;
    }
    if (i < num_align) {
        fail("Not enough alignment records\n");
        goto cleanup;
    }
    r = sam_close(in); in = NULL;
    if (r < 0) {
        fail("sam_close(in)");
        goto cleanup;
    }
    r = sam_close(out); out = NULL;
    if (r < 0) {
        fail("sam_close(out)");
        goto cleanup;
    }

    inf = fopen(outfname, "r");
    if (!inf) {
        fail("Opening \"%s\"", outfname);
        goto cleanup;
    }
    bytes = fread(buffer, 1, sizeof(buffer), inf);
    if (bytes != sizeof(sam_text) - 7
        || memcmp(buffer, sam_text + 6, bytes - 7) != 0) {
        fail("Output file does not match original version");
        fprintf(stderr,
                "---------- Expected:\n%.*s\n"
                "++++++++++ Got:\n%.*s\n"
                "====================\n",
                (int) sizeof(sam_text) - 7, sam_text + 6,
                (int) bytes, buffer);
        goto cleanup;
    }

 cleanup:
    bam_destroy1(aln);
    sam_hdr_destroy(header);
    sam_hdr_destroy(dup_header);
    if (in) sam_close(in);
    if (out) sam_close(out);
    if (inf) fclose(inf);
    unlink(outfname);
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x668, %rsp            # imm = 0x668
movl %edi, %ebp
pxor %xmm0, %xmm0
movdqa %xmm0, 0x10(%rsp)
movdqa %xmm0, 0x20(%rsp)
movabsq $0x12a05ee17, %rax      # imm = 0x12A05EE17
movq %rax, 0x10(%rsp)
movabsq $0x3ffffffffffffff6, %rax # imm = 0x3FFFFFFFFFFFFFF6
leaq -0x37f(%rax), %rcx
movq %rcx, 0x20(%rsp)
movq %rax, 0x30(%rsp)
movq $0x1, 0x38(%rsp)
movdqa %xmm0, 0x60(%rsp)
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x40(%rsp)
movq $-0x1, %rcx
movq %rcx, 0x50(%rsp)
movq %rax, 0x58(%rsp)
movq %rcx, 0x68(%rsp)
callq 0x27f95
movq %rax, %rbx
testq %rax, %rax
je 0xb471
leaq 0x8d94c(%rip), %rdi     # 0x98b30
leaq 0x92554(%rip), %rsi     # 0x9d73f
callq 0x1b1e0
testq %rax, %rax
je 0xb47a
movq %rax, %r15
leaq 0x8b91f(%rip), %rdi     # 0x96b22
leaq 0x917b9(%rip), %rsi     # 0x9c9c3
callq 0x1b1e0
testq %rax, %rax
je 0xb4f3
movq %rax, %r13
movq %r15, %rdi
callq 0x2ad61
testq %rax, %rax
je 0xb515
movq %rax, %r14
testl %ebp, %ebp
je 0xb24b
leaq 0x8abe3(%rip), %rsi     # 0x95e1d
movq %r14, %rdi
callq 0x13b4e
cmpl $0x4, %eax
jne 0xb52d
leaq 0x8e00e(%rip), %rsi     # 0x99260
leaq 0x91644(%rip), %rdx     # 0x9c89d
movq %r14, %rdi
callq 0xc4c3
testl %eax, %eax
js 0xb3c0
movq %r14, %rdi
callq 0x2777d
movq %rax, %rbp
testq %rax, %rax
je 0xb542
leaq 0x8dfdc(%rip), %rsi     # 0x99260
leaq 0x8b972(%rip), %rdx     # 0x96bfd
movq %rbp, %rdi
callq 0xc4c3
testl %eax, %eax
js 0xb3c2
leaq 0x8ab7b(%rip), %rsi     # 0x95e1d
movq %rbp, %rdi
callq 0x13b4e
cmpl $0x4, %eax
jne 0xb565
leaq 0x8dfa6(%rip), %rsi     # 0x99260
leaq 0x8b935(%rip), %rdx     # 0x96bf6
movq %rbp, %rdi
callq 0xc4c3
testl %eax, %eax
js 0xb3c2
movq %r13, %rdi
movq %r14, %rsi
callq 0x2bf5a
testl %eax, %eax
js 0xb5b0
movq %r15, (%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2ed2f
testl %eax, %eax
js 0xb5c6
movq %rbp, %rax
movq %r13, %rbp
movq %rax, 0x8(%rsp)
movq $-0x1, %r15
leaq 0x8da39(%rip), %r12     # 0x98d50
xorl %r13d, %r13d
cmpq $0x5, %r15
je 0xb5e7
movl 0x8(%rbx), %ecx
leaq 0x8da02(%rip), %rax     # 0x98d30
movl (%r13,%rax), %edx
cmpl %edx, %ecx
jne 0xb5fa
movl 0x1c(%rbx), %ecx
movl (%r13,%r12), %edx
cmpl %edx, %ecx
jne 0xb606
movq (%rbx), %rcx
movq 0x18(%rsp,%r15,8), %rdx
cmpq %rdx, %rcx
jne 0xb398
movq 0x20(%rbx), %rcx
movq 0x48(%rsp,%r15,8), %rdx
cmpq %rdx, %rcx
jne 0xb3ac
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x32572
testl %eax, %eax
js 0xb61f
movq (%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2ed2f
incq %r15
addq $0x4, %r13
testl %eax, %eax
jns 0xb31a
jmp 0xb3ca
leal 0x1(%r15), %esi
leaq 0x8b904(%rip), %rdi     # 0x96ca7
xorl %eax, %eax
callq 0x49e0
jmp 0xb358
leal 0x1(%r15), %esi
leaq 0x8b925(%rip), %rdi     # 0x96cdc
xorl %eax, %eax
callq 0x49e0
jmp 0xb366
xorl %ebp, %ebp
xorl %r12d, %r12d
jmp 0xb496
cmpl $-0x1, %eax
movq 0x8(%rsp), %rax
movq %rbp, %r13
movq %rax, %rbp
jne 0xb5d4
cmpq $0x4, %r15
jbe 0xb5cb
movq (%rsp), %rdi
callq 0x1c442
testl %eax, %eax
js 0xb64a
movq %r13, %rdi
callq 0x1c442
testl %eax, %eax
js 0xb660
leaq 0x8b712(%rip), %rdi     # 0x96b22
leaq 0x92328(%rip), %rsi     # 0x9d73f
callq 0x45b0
testq %rax, %rax
je 0xb676
movq %rax, %r12
leaq 0x70(%rsp), %rdi
movl $0x1, %esi
movl $0x5f4, %edx            # imm = 0x5F4
movq %rax, %rcx
callq 0x4790
movq %rax, %r15
cmpq $0x1ed, %rax            # imm = 0x1ED
jne 0xb56e
leaq 0x8d6e1(%rip), %rsi     # 0x98b36
leaq 0x70(%rsp), %rdi
movl $0x1e6, %edx            # imm = 0x1E6
callq 0x44c0
testl %eax, %eax
jne 0xb56e
xorl %r13d, %r13d
jmp 0xb493
leaq 0x8b6c4(%rip), %rdi     # 0x96b3c
jmp 0xb481
leaq 0x8b6c9(%rip), %rdi     # 0x96b4a
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x49e0
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0x2806b
movq %r14, %rdi
callq 0x27642
movq %rbp, %rdi
callq 0x27642
testq %r15, %r15
je 0xb4bb
movq %r15, %rdi
callq 0x1c442
testq %r13, %r13
je 0xb4c8
movq %r13, %rdi
callq 0x1c442
testq %r12, %r12
je 0xb4d5
movq %r12, %rdi
callq 0x44a0
leaq 0x8b646(%rip), %rdi     # 0x96b22
callq 0x41f0
addq $0x668, %rsp            # imm = 0x668
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8b661(%rip), %rdi     # 0x96b5b
leaq 0x8b621(%rip), %rsi     # 0x96b22
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x49e0
xorl %r14d, %r14d
xorl %r13d, %r13d
jmp 0xb3c2
leaq 0x8b65c(%rip), %rdi     # 0x96b78
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x49e0
xorl %r14d, %r14d
jmp 0xb3c2
leaq 0x8b657(%rip), %rdi     # 0x96b8b
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x49e0
jmp 0xb3c2
leaq 0x8b665(%rip), %rdi     # 0x96bae
xorl %eax, %eax
callq 0x49e0
leaq 0x8dd09(%rip), %rsi     # 0x99260
leaq 0x8b69f(%rip), %rdx     # 0x96bfd
xorl %edi, %edi
jmp 0xb28e
leaq 0x8b65d(%rip), %rdi     # 0x96bc9
jmp 0xb5b7
leaq 0x8b82b(%rip), %rdi     # 0x96da0
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x49e0
movq 0xafa5a(%rip), %rax     # 0xbafe0
movq (%rax), %rdi
leaq 0x8abfd(%rip), %rsi     # 0x9618d
leaq 0x8d59f(%rip), %rcx     # 0x98b36
leaq 0x70(%rsp), %r9
movl $0x1ed, %edx            # imm = 0x1ED
movl %r15d, %r8d
xorl %eax, %eax
callq 0x4700
jmp 0xb493
leaq 0x8b657(%rip), %rdi     # 0x96c0e
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x49e0
jmp 0xb496
cmpl $-0x1, %eax
jne 0xb5d4
leaq 0x8b786(%rip), %rdi     # 0x96d58
jmp 0xb5db
leaq 0x8b760(%rip), %rdi     # 0x96d3b
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x49e0
jmp 0xb641
leaq 0x8b63b(%rip), %rdi     # 0x96c29
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x49e0
jmp 0xb636
incl %r15d
leaq 0x8b642(%rip), %rdi     # 0x96c46
jmp 0xb610
incl %r15d
leaq 0x8b664(%rip), %rdi     # 0x96c74
xorl %r12d, %r12d
movl %r15d, %esi
xorl %eax, %eax
callq 0x49e0
jmp 0xb636
incl %r15d
leaq 0x8b6ed(%rip), %rdi     # 0x96d16
xorl %r12d, %r12d
movl %r15d, %esi
xorl %eax, %eax
callq 0x49e0
movq 0x8(%rsp), %rax
movq %rbp, %r13
movq %rax, %rbp
movq (%rsp), %r15
jmp 0xb496
leaq 0x8b725(%rip), %rdi     # 0x96d76
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x49e0
jmp 0xb493
leaq 0x8b71d(%rip), %rdi     # 0x96d84
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x49e0
jmp 0xb490
leaq 0x8b716(%rip), %rdi     # 0x96d93
leaq 0x8b49e(%rip), %rsi     # 0x96b22
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x49e0
jmp 0xb490