// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movl %esi, %ebx
movq %rdi, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x40(%rdi), %r13
callq 0x1a73e
movl %eax, %ebp
xorl %edi, %edi
callq 0x1a737
movq 0x18(%r14), %rdi
leaq 0x8c22d(%rip), %rsi     # 0x94511
leaq 0x89392(%rip), %rdx     # 0x9167d
callq 0x865e
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x8563
movq %rax, %r12
testl %ebx, %ebx
jle 0x831e
leaq 0x89371(%rip), %rdx     # 0x9167d
movq %r12, %rdi
movl %ebx, %esi
callq 0x8915
testl %eax, %eax
jne 0x83e3
movl %ebx, 0x1c(%rsp)
movl %ebp, %r15d
leaq 0x18(%r14), %rbp
movq %r13, %rdi
movl $0xa, %esi
callq 0x4180
movq %rax, %rbx
incq %rbx
movq %rbx, %rdx
subq %r13, %rdx
movq %rbp, 0x10(%rsp)
movq (%rbp), %rcx
movl %r15d, %ebp
movl 0x1c(%rsp), %r15d
leaq 0x89323(%rip), %r8      # 0x9167d
movq %r12, %rdi
movq %r13, %rsi
callq 0x893c
testq %rax, %rax
js 0x83e3
movq %r12, %rdi
callq 0xb311
testl %eax, %eax
js 0x83e3
movq 0x18(%r12), %rax
movq %rax, 0x50(%rsp)
movq %rbx, %rdi
movl $0xa, %esi
callq 0x4180
subq %rbx, %rax
leaq 0x1(%rax), %rdx
movq 0x10(%rsp), %rax
movq (%rax), %rcx
leaq 0x892da(%rip), %r8      # 0x9167d
movq %r12, %rdi
movq %rbx, %rsi
callq 0x893c
testq %rax, %rax
js 0x83e3
movq %r12, %rdi
callq 0xb311
testl %eax, %eax
js 0x83e3
movq 0x18(%r12), %rbx
movq 0x10(%rsp), %rax
movq (%rax), %rsi
leaq 0x892aa(%rip), %rdx     # 0x9167d
leaq 0x20(%rsp), %rdi
xorl %ecx, %ecx
callq 0x86bf
testl %eax, %eax
je 0x8405
movq 0x20(%rsp), %rbx
movl %ebp, %edi
callq 0x1a737
testq %rbx, %rbx
je 0x856a
movq %rbx, %rdi
callq 0xbb59
jmp 0x856a
leaq 0x30(%rsp), %r12
movl %ebp, 0xc(%rsp)
decq %rbx
cmpq 0x50(%rsp), %rbx
jle 0x85bf
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq %rbx, %rsi
callq 0x4670
testl %eax, %eax
jne 0x83e3
movq 0x10(%rsp), %rax
movq (%rax), %rdi
leaq 0x8ceac(%rip), %rsi     # 0x952eb
leaq 0x89237(%rip), %rdx     # 0x9167d
callq 0x865e
movq %rax, 0x20(%rsp)
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x8563
testl %r15d, %r15d
jle 0x847f
movq 0x28(%rsp), %rdi
movl %r15d, %esi
leaq 0x8920b(%rip), %rdx     # 0x9167d
callq 0x8915
testl %eax, %eax
jne 0x83e3
movq %rbx, 0x48(%rsp)
movq 0x48(%r14), %rbx
testq %rbx, %rbx
je 0x850a
xorl %ebp, %ebp
movq %r13, %r15
addq %rbp, %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x4180
movq %rax, %rcx
subq %r15, %rcx
subq %rbp, %rbx
testq %rax, %rax
cmovneq %rcx, %rbx
movq 0x28(%rsp), %rdi
movl $0xa, %esi
movq %r12, %rdx
callq 0xc2fc
cmpl $-0x1, %eax
jl 0x850a
je 0x85e5
movq 0x30(%rsp), %r10
movq 0x40(%rsp), %r11
cmpq %rbx, %r10
jne 0x85f7
movq %r15, %rdi
movq %r11, %rsi
movq %rbx, %rdx
movq %r11, %r15
callq 0x44c0
testl %eax, %eax
jne 0x85f1
addq %rbx, %rbp
incq %rbp
movq 0x48(%r14), %rbx
cmpq %rbx, %rbp
jb 0x848f
movl $0x3, %ebx
movl 0xc(%rsp), %ebp
movl 0x1c(%rsp), %r15d
movq 0x28(%rsp), %rdi
movl $0xa, %esi
movq %r12, %rdx
callq 0xc2fc
cmpl $-0x1, %eax
jge 0x8599
decl %ebx
jne 0x8518
movq 0x10(%rsp), %rax
movq (%rax), %rsi
leaq 0x20(%rsp), %rdi
leaq 0x89136(%rip), %rdx     # 0x9167d
movl $0x1, %ecx
callq 0x86bf
testl %eax, %eax
movq 0x48(%rsp), %rbx
jne 0x840e
jmp 0x83e3
movl %ebp, %edi
callq 0x1a737
movq 0x40(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
callq 0x45b0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xaaa40(%rip), %rcx     # 0xb2fe0
movq (%rcx), %rdi
leaq 0x89118(%rip), %rsi     # 0x916c2
leaq 0x890cc(%rip), %rdx     # 0x9167d
movl %eax, %ecx
xorl %eax, %eax
callq 0x4700
jmp 0x83e3
movq 0x40(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
callq 0x45b0
movl %ebp, %edi
callq 0x1a737
xorl %eax, %eax
jmp 0x858a
movq 0x10(%rsp), %rdi
callq 0x4b63
jmp 0x862c
movq %rbx, %r10
movq %r15, %r11
movq 0xaa9e2(%rip), %rax     # 0xb2fe0
movq (%rax), %rdi
movq 0x18(%r14), %rcx
addq 0x40(%r14), %rbp
leaq 0x8901f(%rip), %rsi     # 0x9162f
leaq 0x89066(%rip), %rdx     # 0x9167d
movl %ebx, %r8d
movq %rbp, %r9
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x4700
addq $0x10, %rsp
movl 0xc(%rsp), %ebp
jmp 0x83e3