// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/test/test_bgzf.c
// Function: test_bgzf_getline_on_truncated_file
static int test_bgzf_getline_on_truncated_file(Files *f, const char *mode, int nthreads) {
    BGZF* bgz = NULL;
    ssize_t bg_put;
    size_t pos;
    kstring_t str = { 0, 0, NULL };
    const char *text = (const char *) f->text;

    // Turn off bgzf errors as they're expected.
    enum htsLogLevel lvl = hts_get_log_level();
    hts_set_log_level(HTS_LOG_OFF);

    bgz = try_bgzf_open(f->tmp_bgzf, mode, __func__);
    if (!bgz) goto fail;

    if (nthreads > 0 && try_bgzf_mt(bgz, nthreads, __func__) != 0) goto fail;

    const char *text_line2 = strchr(text, '\n') + 1;
    bg_put = try_bgzf_write(bgz, text, text_line2 - text, f->tmp_bgzf, __func__);
    if (bg_put < 0) goto fail;
    if (bgzf_flush(bgz) < 0) goto fail;
    int64_t block2_start = bgz->block_address;

    const char *text_line3 = strchr(text_line2, '\n') + 1;
    bg_put = try_bgzf_write(bgz, text_line2, text_line3 - text_line2, f->tmp_bgzf, __func__);
    if (bg_put < 0) goto fail;
    if (bgzf_flush(bgz) < 0) goto fail;
    int64_t block3_start = bgz->block_address;

    if (try_bgzf_close(&bgz, f->tmp_bgzf, __func__, 0) != 0) goto fail;

    int64_t newsize;
    for(newsize = block3_start - 1; newsize > block2_start; newsize--) {
        //fprintf(stderr, "test_bgzf_getline_on_truncated_file : size truncated to %" PRId64 " with threads %d\n", newsize, nthreads);

        if (truncate(f->tmp_bgzf, newsize) != 0) goto fail;

        bgz = try_bgzf_open(f->tmp_bgzf, "r", __func__);
        if (!bgz) goto fail;

        if (nthreads > 0 && try_bgzf_mt(bgz, nthreads, __func__) != 0) goto fail;

        for (pos = 0; pos < f->ltext; ) {
            const char *end = strchr(text + pos, '\n');
            size_t l = end ? end - (text + pos) : f->ltext - pos;

            int res = bgzf_getline(bgz, '\n', &str);
            if (res < -1) {
                // ok, we expect error from truncated file
                break;
            } else if (res == -1) {
                // truncated file should never return EOF since we do not truncate at block boundary
                fprintf(stderr, "%s : %s from bgzf_getline on %s\n",
                        __func__, "Unexpected EOF",
                        f->tmp_bgzf);
                goto fail;
            }

            if (str.l != l || memcmp(text + pos, str.s, l) != 0) {
                fprintf(stderr,
                        "%s : Unexpected data from bgzf_getline on %s\n"
                        "Expected : %.*s\n"
                        "Got      : %.*s\n",
                        __func__, f->tmp_bgzf, (int) l, (char *) f->text + pos,
                        (int) str.l, str.s);
                goto fail;
            }
            pos += l + 1;
        }

        // verify error is persistent
        int k;
        for(k = 0; k < 3; k++) {
            int res = bgzf_getline(bgz, '\n', &str);
            if (res > -2) {
                fprintf(stderr, "%s : unexpected bgzf_getline result %d\n", __func__, res);
                goto fail;
            }
        }
        // closing a stream with error returns error
        if (try_bgzf_close(&bgz, f->tmp_bgzf, __func__, 1) == 0) goto fail;
    }
    free(ks_release(&str));
    hts_set_log_level(lvl);
    return 0;

 fail:
    hts_set_log_level(lvl);
    if (bgz) bgzf_close(bgz);
    free(ks_release(&str));
    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %esi, %r12d
movq %rdi, %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
movq 0x40(%rdi), %rax
movq %rax, 0x50(%rsp)
callq 0x170a7
movl %eax, %r14d
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x170a0
movq 0x18(%r13), %rdi
leaq 0x6fb53(%rip), %rsi     # 0x774b1
leaq 0x6ccfc(%rip), %rdx     # 0x74661
callq 0x7d08
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x7a43
movq %rax, %rbx
testl %r12d, %r12d
jle 0x799a
leaq 0x6ccda(%rip), %rdx     # 0x74661
movq %rbx, %rdi
movl %r12d, %esi
callq 0x811d
testl %eax, %eax
jne 0x7a3e
pushq $0xa
popq %rsi
movq 0x50(%rsp), %rbp
movq %rbp, %rdi
callq 0x4180
movq %rax, %r15
incq %r15
movq %r15, %rdx
subq %rbp, %rdx
movq 0x18(%r13), %rcx
leaq 0x6cca0(%rip), %r8      # 0x74661
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x816e
testq %rax, %rax
js 0x7a3e
movq %rbx, %rdi
callq 0xa689
testl %eax, %eax
js 0x7a3e
movq 0x18(%rbx), %rax
movq %rax, 0x60(%rsp)
pushq $0xa
popq %rsi
movq %r15, %rdi
callq 0x4180
subq %r15, %rax
leaq 0x1(%rax), %rdx
movq 0x18(%r13), %rcx
leaq 0x6cc5e(%rip), %r8      # 0x74661
movq %rbx, %rdi
movq %r15, %rsi
callq 0x816e
testq %rax, %rax
js 0x7a3e
movq %rbx, %rdi
callq 0xa689
testl %eax, %eax
js 0x7a3e
movq 0x18(%rbx), %rbx
movq 0x18(%r13), %rsi
leaq 0x6cc33(%rip), %rdx     # 0x74661
leaq 0x18(%rsp), %rdi
xorl %ecx, %ecx
callq 0x7d69
testl %eax, %eax
je 0x7a82
movq 0x18(%rsp), %r15
movl %r14d, %edi
callq 0x170a0
testq %r15, %r15
je 0x7a58
movq %r15, %rdi
callq 0xad16
movq 0x40(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
callq 0x45b0
pushq $-0x1
popq %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6cbd8(%rip), %r15     # 0x74661
leaq 0x30(%rsp), %rbp
movl %r14d, 0xc(%rsp)
movl %r12d, 0x2c(%rsp)
movq %r13, 0x10(%rsp)
decq %rbx
cmpq 0x60(%rsp), %rbx
jle 0x7c14
movq 0x18(%r13), %rdi
movq %rbx, %rsi
callq 0x4670
testl %eax, %eax
jne 0x7a3e
movq 0x18(%r13), %rdi
leaq 0x707d0(%rip), %rsi     # 0x78296
movq %r15, %rdx
callq 0x7d08
movq %rax, 0x18(%rsp)
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x7c3b
testl %r12d, %r12d
jle 0x7afe
movq 0x20(%rsp), %rdi
movl %r12d, %esi
movq %r15, %rdx
callq 0x811d
testl %eax, %eax
jne 0x7a3e
movq %rbx, 0x58(%rsp)
xorl %ebx, %ebx
movq 0x48(%r13), %r13
subq %rbx, %r13
jbe 0x7b94
movq 0x50(%rsp), %rax
leaq (%rax,%rbx), %r14
movq %r14, %rdi
movq %rbx, %r12
movq %rbp, %rbx
pushq $0xa
popq %rbp
movl %ebp, %esi
callq 0x4180
movq %rax, %r15
subq %r14, %r15
testq %rax, %rax
cmoveq %r13, %r15
movq 0x20(%rsp), %rdi
movl %ebp, %esi
movq %rbx, %rbp
movq %r12, %rbx
movq %rbp, %rdx
callq 0xb32b
cmpl $-0x1, %eax
jl 0x7b94
je 0x7c43
movq 0x30(%rsp), %r10
movq 0x40(%rsp), %r13
cmpq %r15, %r10
jne 0x7c77
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x44c0
testl %eax, %eax
jne 0x7c74
addq %r15, %rbx
incq %rbx
movq 0x10(%rsp), %r13
jmp 0x7b05
pushq $0x4
popq %rbx
movl 0xc(%rsp), %r14d
movq 0x10(%rsp), %r13
movl 0x2c(%rsp), %r12d
leaq 0x6cab4(%rip), %r15     # 0x74661
decl %ebx
je 0x7bc8
movq 0x20(%rsp), %rdi
pushq $0xa
popq %rsi
movq %rbp, %rdx
callq 0xb32b
cmpl $-0x1, %eax
jl 0x7bad
jmp 0x7bee
movq 0x18(%r13), %rsi
leaq 0x18(%rsp), %rdi
movq %r15, %rdx
pushq $0x1
popq %rcx
callq 0x7d69
testl %eax, %eax
movq 0x58(%rsp), %rbx
jne 0x7a9d
jmp 0x7a3e
movq 0x933eb(%rip), %rcx     # 0x9afe0
movq (%rcx), %rdi
leaq 0x6caa7(%rip), %rsi     # 0x746a6
leaq 0x6ca5b(%rip), %rdx     # 0x74661
movl %eax, %ecx
xorl %eax, %eax
callq 0x4700
jmp 0x7a3e
movq 0x40(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
callq 0x45b0
movl %r14d, %edi
callq 0x170a0
xorl %eax, %eax
jmp 0x7a73
xorl %r15d, %r15d
jmp 0x7a43
movq 0x93396(%rip), %rax     # 0x9afe0
movq (%rax), %rdi
movq 0x10(%rsp), %rax
movq 0x18(%rax), %r8
leaq 0x6ca28(%rip), %rsi     # 0x74685
leaq 0x6c9fd(%rip), %rdx     # 0x74661
leaq 0x6c999(%rip), %rcx     # 0x74604
xorl %eax, %eax
callq 0x4700
jmp 0x7cb1
movq %r15, %r10
movq 0x93362(%rip), %rax     # 0x9afe0
movq (%rax), %rdi
movq 0x10(%rsp), %r12
movq 0x18(%r12), %rcx
addq 0x40(%r12), %rbx
leaq 0x6c97c(%rip), %rsi     # 0x74613
leaq 0x6c9c3(%rip), %rdx     # 0x74661
movl %r15d, %r8d
movq %rbx, %r9
xorl %eax, %eax
pushq %r13
pushq %r10
callq 0x4700
popq %rcx
popq %rdx
movl 0xc(%rsp), %r14d
jmp 0x7a3e