// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/test/sam.c
// Function: test_bam_set1_write_and_read_back
static void test_bam_set1_write_and_read_back(void)
{
    const char *qname = "q1";
    const uint32_t cigar[] = { 6 << BAM_CIGAR_SHIFT | BAM_CMATCH, 2 << BAM_CIGAR_SHIFT | BAM_CINS, 2 << BAM_CIGAR_SHIFT | BAM_CMATCH };
    const char *seq = "TGGACTACGA";
    const char *qual = "DBBBB+=7=0";
    const char *temp_fname = "test/test_bam_set1_write_and_read_back.tmp.bam";

    int r;
    htsFile *writer = NULL, *reader = NULL;
    sam_hdr_t *w_header = NULL, *r_header = NULL;
    bam1_t *w_bam = NULL, *r_bam = NULL;
    kstring_t ks = KS_INITIALIZE;

    // open file for writing
    writer = hts_open(temp_fname, "wb");
    VERIFY(writer != NULL, "failed to open bam file for writing.");

    // write header
    w_header = bam_hdr_init();
    VERIFY(w_header != NULL, "failed to initialize bam header.");
    r = sam_hdr_add_line(w_header, "SQ", "SN", "t1", "LN", "5000", NULL);
    VERIFY(r == 0, "failed to add SQ header line.");
    r = sam_hdr_write(writer, w_header);
    VERIFY(r == 0, "failed to write bam header.");

    // write alignments
    w_bam = bam_init1();
    VERIFY(w_bam != NULL, "failed to initialize BAM struct.");
    r = bam_set1(w_bam, strlen(qname), qname,
                 BAM_FPAIRED | BAM_FREVERSE, 0, 1000, 42,
                 sizeof(cigar) / 4, cigar, 0, 2000, 3000,
                 strlen(seq), seq, qual, 64);
    VERIFY(r >= 0, "call to bam_set1() failed.");
    r = sam_write1(writer, w_header, w_bam);
    VERIFY(r >= 0, "failed to write alignment.");
    bam_destroy1(w_bam);

    // close file
    r = hts_close(writer);
    VERIFY(r == 0, "failed to close bam file for writing.");
    sam_hdr_destroy(w_header);

    // open file for reading
    reader = hts_open(temp_fname, "rb");
    VERIFY(reader != NULL, "failed to open bam file for reading.");

    // read header
    r_header = sam_hdr_read(reader);
    VERIFY(r_header != NULL, "failed to read bam header.");
    r = sam_hdr_find_tag_id(r_header, "SQ", NULL, NULL, "SN", &ks);
    VERIFY(r == 0, "failed to read SQ/SN value");
    VERIFY(strcmp(ks_c_str(&ks), "t1") == 0, "expected reference sequence name in the header == 't1'");
    VERIFY(r_header->n_targets == 1, "expected number of reference sequences == 1");
    VERIFY(strcmp(r_header->target_name[0], "t1") == 0, "expected reference sequence name == 't1'");
    VERIFY(r_header->target_len[0] == 5000, "expected reference sequence length == 5000");

    // read alignments
    r_bam = bam_init1();
    VERIFY(r_bam != NULL, "failed to initialize BAM struct.");
    r = sam_read1(reader, r_header, r_bam);
    VERIFY(r >= 0, "failed to read alignment.");
    VERIFY(strcmp(bam_get_qname(r_bam), qname) == 0, "qname does not match.");
    VERIFY(r_bam->core.n_cigar == sizeof(cigar) / 4, "cigar length does not match.");
    VERIFY(memcmp(bam_get_cigar(r_bam), cigar, sizeof(cigar)) == 0, "cigar data does not match.");
    VERIFY(r_bam->core.l_qseq == strlen(seq), "sequence length does not match.");

    r = sam_read1(reader, r_header, r_bam);
    VERIFY(r < 0, "expected no more alignments.");
    bam_destroy1(r_bam);

    // close file
    r = hts_close(reader);
    VERIFY(r == 0, "failed to close bam file for reading.");
    sam_hdr_destroy(r_header);

cleanup:
    ks_free(&ks);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0xf8, %rsp
leaq 0xe69bb(%rip), %rax     # 0xf2e9e
movq %rax, -0x10(%rbp)
movq 0xe44be(%rip), %rax     # 0xf09ac
movq %rax, -0x1c(%rbp)
movl 0xe44bc(%rip), %eax     # 0xf09b4
movl %eax, -0x14(%rbp)
leaq 0xe67f9(%rip), %rax     # 0xf2cfb
movq %rax, -0x28(%rbp)
leaq 0xe67f9(%rip), %rax     # 0xf2d06
movq %rax, -0x30(%rbp)
leaq 0xe6989(%rip), %rax     # 0xf2ea1
movq %rax, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x4240
movq -0x38(%rbp), %rdi
leaq 0xe4ff1(%rip), %rsi     # 0xf155b
callq 0x232d0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0xc59b
leaq 0xe7636(%rip), %rdi     # 0xf3bb7
leaq 0xe6948(%rip), %rsi     # 0xf2ed0
leaq 0xe6963(%rip), %rdx     # 0xf2ef2
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
callq 0xecf0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0xc5cc
leaq 0xe7605(%rip), %rdi     # 0xf3bb7
leaq 0xe6917(%rip), %rsi     # 0xf2ed0
leaq 0xe6957(%rip), %rdx     # 0xf2f17
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x58(%rbp), %rdi
leaq 0xe516b(%rip), %rsi     # 0xf1742
leaq 0xe5125(%rip), %rdx     # 0xf1703
leaq 0xeab53(%rip), %rcx     # 0xf7138
leaq 0xe511a(%rip), %r8      # 0xf1706
leaq 0xe6acd(%rip), %r9      # 0xf30c0
xorl %eax, %eax
movq $0x0, (%rsp)
movb $0x0, %al
callq 0x143a0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0xc62e
leaq 0xe75a3(%rip), %rdi     # 0xf3bb7
leaq 0xe68b5(%rip), %rsi     # 0xf2ed0
leaq 0xe6916(%rip), %rdx     # 0xf2f38
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x3d9c0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0xc665
leaq 0xe756c(%rip), %rdi     # 0xf3bb7
leaq 0xe687e(%rip), %rsi     # 0xf2ed0
leaq 0xe68fd(%rip), %rdx     # 0xf2f56
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
callq 0x37e90
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0xc696
leaq 0xe753b(%rip), %rdi     # 0xf3bb7
leaq 0xe684d(%rip), %rsi     # 0xf2ed0
leaq 0xe6494(%rip), %rdx     # 0xf2b1e
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x68(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x41a0
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0x1c(%rbp), %rbx
movq -0x28(%rbp), %rdi
callq 0x41a0
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq %rax, %r11
movq -0x28(%rbp), %r10
movq -0x30(%rbp), %rax
movl $0x11, %ecx
xorl %r8d, %r8d
movl $0x3e8, %r9d            # imm = 0x3E8
movl $0x2a, (%rsp)
movq $0x3, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
movq $0x7d0, 0x20(%rsp)      # imm = 0x7D0
movq $0xbb8, 0x28(%rsp)      # imm = 0xBB8
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movq $0x40, 0x48(%rsp)
callq 0x382b0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0xc76d
leaq 0xe7464(%rip), %rdi     # 0xf3bb7
leaq 0xe6776(%rip), %rsi     # 0xf2ed0
leaq 0xe6634(%rip), %rdx     # 0xf2d95
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x68(%rbp), %rdx
callq 0x448b0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0xc7a8
leaq 0xe7429(%rip), %rdi     # 0xf3bb7
leaq 0xe673b(%rip), %rsi     # 0xf2ed0
leaq 0xe67d6(%rip), %rdx     # 0xf2f72
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x68(%rbp), %rdi
callq 0x380d0
movq -0x48(%rbp), %rdi
callq 0x253f0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0xc7e4
leaq 0xe73ed(%rip), %rdi     # 0xf3bb7
leaq 0xe66ff(%rip), %rsi     # 0xf2ed0
leaq 0xe67b5(%rip), %rdx     # 0xf2f8d
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x58(%rbp), %rdi
callq 0x36f50
movq -0x38(%rbp), %rdi
leaq 0xe6260(%rip), %rsi     # 0xf2a58
callq 0x232d0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0xc829
leaq 0xe73a8(%rip), %rdi     # 0xf3bb7
leaq 0xe66ba(%rip), %rsi     # 0xf2ed0
leaq 0xe6796(%rip), %rdx     # 0xf2fb3
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x50(%rbp), %rdi
callq 0x3c9b0
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0xc85e
leaq 0xe7373(%rip), %rdi     # 0xf3bb7
leaq 0xe6685(%rip), %rsi     # 0xf2ed0
leaq 0xe6786(%rip), %rdx     # 0xf2fd8
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x60(%rbp), %rdi
leaq 0xe4ed9(%rip), %rsi     # 0xf1742
xorl %eax, %eax
movl %eax, %ecx
leaq 0xe4e8f(%rip), %r8      # 0xf1703
leaq -0x88(%rbp), %r9
movq %rcx, %rdx
callq 0x187f0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0xc8ad
leaq 0xe7324(%rip), %rdi     # 0xf3bb7
leaq 0xe6636(%rip), %rsi     # 0xf2ed0
leaq 0xe6752(%rip), %rdx     # 0xf2ff3
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
leaq -0x88(%rbp), %rdi
callq 0xed00
movq %rax, %rdi
leaq 0xea875(%rip), %rsi     # 0xf7138
callq 0x4690
cmpl $0x0, %eax
je 0xc8ee
leaq 0xe72e3(%rip), %rdi     # 0xf3bb7
leaq 0xe65f5(%rip), %rsi     # 0xf2ed0
leaq 0xe672c(%rip), %rdx     # 0xf300e
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x60(%rbp), %rax
cmpl $0x1, (%rax)
je 0xc918
leaq 0xe72b9(%rip), %rdi     # 0xf3bb7
leaq 0xe65cb(%rip), %rsi     # 0xf2ed0
leaq 0xe6739(%rip), %rdx     # 0xf3045
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rdi
leaq 0xea80e(%rip), %rsi     # 0xf7138
callq 0x4690
cmpl $0x0, %eax
je 0xc955
leaq 0xe727c(%rip), %rdi     # 0xf3bb7
leaq 0xe658e(%rip), %rsi     # 0xf2ed0
leaq 0xe6728(%rip), %rdx     # 0xf3071
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
cmpl $0x1388, (%rax)         # imm = 0x1388
je 0xc986
leaq 0xe724b(%rip), %rdi     # 0xf3bb7
leaq 0xe655d(%rip), %rsi     # 0xf2ed0
leaq 0xe6720(%rip), %rdx     # 0xf309a
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
callq 0x37e90
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jne 0xc9b7
leaq 0xe721a(%rip), %rdi     # 0xf3bb7
leaq 0xe652c(%rip), %rsi     # 0xf2ed0
leaq 0xe6173(%rip), %rdx     # 0xf2b1e
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x50(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x70(%rbp), %rdx
callq 0x41af0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0xc9f2
leaq 0xe71df(%rip), %rdi     # 0xf3bb7
leaq 0xe64f1(%rip), %rsi     # 0xf2ed0
leaq 0xe66df(%rip), %rdx     # 0xf30c5
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x70(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x4690
cmpl $0x0, %eax
je 0xca29
leaq 0xe71a8(%rip), %rdi     # 0xf3bb7
leaq 0xe64ba(%rip), %rsi     # 0xf2ed0
leaq 0xe66c2(%rip), %rdx     # 0xf30df
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x70(%rbp), %rax
movl 0x14(%rax), %eax
cmpq $0x3, %rax
je 0xca57
leaq 0xe717a(%rip), %rdi     # 0xf3bb7
leaq 0xe648c(%rip), %rsi     # 0xf2ed0
leaq 0xe66aa(%rip), %rdx     # 0xf30f5
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x70(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x70(%rbp), %rax
movzwl 0x12(%rax), %eax
cltq
addq %rax, %rdi
leaq -0x1c(%rbp), %rsi
movl $0xc, %edx
callq 0x41b0
cmpl $0x0, %eax
je 0xcaa0
leaq 0xe7131(%rip), %rdi     # 0xf3bb7
leaq 0xe6443(%rip), %rsi     # 0xf2ed0
leaq 0xe667e(%rip), %rdx     # 0xf3112
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x70(%rbp), %rax
movslq 0x18(%rax), %rax
movq %rax, -0xa8(%rbp)
movq -0x28(%rbp), %rdi
callq 0x41a0
movq %rax, %rcx
movq -0xa8(%rbp), %rax
cmpq %rcx, %rax
je 0xcae5
leaq 0xe70e9(%rip), %rdi     # 0xf3bb7
leaq 0xe63fb(%rip), %rsi     # 0xf2ed0
leaq 0xe6651(%rip), %rdx     # 0xf312d
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x50(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x70(%rbp), %rdx
callq 0x41af0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jl 0xcb1d
leaq 0xe70b1(%rip), %rdi     # 0xf3bb7
leaq 0xe63c3(%rip), %rsi     # 0xf2ed0
leaq 0xe6639(%rip), %rdx     # 0xf314d
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x70(%rbp), %rdi
callq 0x380d0
movq -0x50(%rbp), %rdi
callq 0x253f0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0xcb56
leaq 0xe7078(%rip), %rdi     # 0xf3bb7
leaq 0xe638a(%rip), %rsi     # 0xf2ed0
leaq 0xe661d(%rip), %rdx     # 0xf316a
movb $0x0, %al
callq 0x5b50
jmp 0xcb5f
movq -0x60(%rbp), %rdi
callq 0x36f50
leaq -0x88(%rbp), %rdi
callq 0xed40
addq $0xf8, %rsp
popq %rbx
popq %rbp
retq
nopw %cs:(%rax,%rax)