// File: /workspace/github_c_makefile_star2_O1/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, %ebx
movq %rdi, %r14
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movq %rax, 0x30(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq 0x40(%rdi), %rax
movq %rax, 0x50(%rsp)
callq 0x1ad27
movl %eax, 0x1c(%rsp)
xorl %edi, %edi
callq 0x1ad20
movq 0x18(%r14), %rdi
leaq 0x8f335(%rip), %rsi     # 0x97561
leaq 0x8c42e(%rip), %rdx     # 0x94661
callq 0x85b7
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x8326
movq %rax, %r13
testl %ebx, %ebx
jle 0x8266
leaq 0x8c40d(%rip), %rdx     # 0x94661
movq %r13, %rdi
movl %ebx, %esi
callq 0x887e
testl %eax, %eax
jne 0x8326
movq %r14, 0x48(%rsp)
leaq 0x18(%r14), %rbp
movq 0x50(%rsp), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x4180
movq %rax, %r12
incq %r12
movq %r12, %rdx
subq %r15, %rdx
movq %rbp, 0x8(%rsp)
movq (%rbp), %rcx
movl %ebx, %ebp
leaq 0x8c3c2(%rip), %r8      # 0x94661
movq %r13, %rdi
movq %r15, %rsi
callq 0x88a5
testq %rax, %rax
js 0x8326
movq %r13, %rdi
callq 0xb3c9
testl %eax, %eax
js 0x8326
movq 0x18(%r13), %rbx
movq %r12, %rdi
movl $0xa, %esi
callq 0x4180
subq %r12, %rax
leaq 0x1(%rax), %rdx
movq 0x8(%rsp), %rax
movq (%rax), %rcx
leaq 0x8c37f(%rip), %r8      # 0x94661
movq %r13, %rdi
movq %r12, %rsi
callq 0x88a5
testq %rax, %rax
js 0x8326
movq %r13, %rdi
callq 0xb3c9
testl %eax, %eax
js 0x8326
movq 0x18(%r13), %rax
movq %rax, 0x40(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rsi
leaq 0x8c34b(%rip), %rdx     # 0x94661
leaq 0x10(%rsp), %rdi
xorl %ecx, %ecx
callq 0x861b
testl %eax, %eax
je 0x836d
movl 0x1c(%rsp), %edi
callq 0x1ad20
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x833e
callq 0xbc44
movq 0x30(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
callq 0x45b0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, 0x58(%rsp)
movq 0x40(%rsp), %rsi
decq %rsi
cmpq %rbx, %rsi
jle 0x8570
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq %rsi, 0x40(%rsp)
callq 0x4670
testl %eax, %eax
jne 0x8326
movq 0x8(%rsp), %rax
movq (%rax), %rdi
leaq 0x8ff9c(%rip), %rsi     # 0x98344
leaq 0x8c2b2(%rip), %rdx     # 0x94661
callq 0x85b7
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x8326
movq %rax, %r14
testl %ebp, %ebp
jle 0x83e2
movq %r14, %rdi
movl %ebp, %esi
leaq 0x8c28c(%rip), %rdx     # 0x94661
callq 0x887e
testl %eax, %eax
jne 0x8326
xorl %ebx, %ebx
movl %ebp, %r15d
movq 0x48(%rsp), %rax
movq 0x48(%rax), %rbp
subq %rbx, %rbp
jbe 0x84d5
movq 0x50(%rsp), %rax
leaq (%rax,%rbx), %r13
movq %r13, %rdi
movl $0xa, %esi
callq 0x4180
movq %rax, %r12
subq %r13, %r12
testq %rax, %rax
cmoveq %rbp, %r12
movq %r14, %rdi
movl $0xa, %esi
leaq 0x20(%rsp), %rdx
callq 0xc3cd
movl $0x6, %ecx
movl %r15d, %ebp
cmpl $-0x1, %eax
jl 0x8468
je 0x84b2
movq 0x20(%rsp), %rax
movq %rax, 0x60(%rsp)
cmpq %r12, %rax
jne 0x8472
movq 0x30(%rsp), %rsi
movq %r13, %rdi
movq %r12, %rdx
callq 0x44c0
testl %eax, %eax
jne 0x8472
addq %r12, %rbx
incq %rbx
xorl %ecx, %ecx
testl %ecx, %ecx
je 0x83e4
jmp 0x84c3
movq 0xacb67(%rip), %rax     # 0xb4fe0
movq (%rax), %rdi
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rcx
movq 0x40(%rax), %r9
addq %rbx, %r9
leaq 0x8c180(%rip), %rsi     # 0x94613
leaq 0x8c1c7(%rip), %rdx     # 0x94661
movl %r12d, %r8d
xorl %eax, %eax
pushq 0x30(%rsp)
pushq 0x68(%rsp)
callq 0x4700
addq $0x10, %rsp
jmp 0x84bc
movq 0x8(%rsp), %rdi
callq 0x4ae7
movl $0x2, %ecx
jmp 0x8468
cmpl $0x2, %ecx
je 0x8326
cmpl $0x4, %ecx
je 0x856b
movl $0x3, %ebx
movl %r15d, %ebp
movq %r14, %rdi
movl $0xa, %esi
leaq 0x20(%rsp), %rdx
callq 0xc3cd
movl %eax, %r12d
xorl %ecx, %ecx
cmpl $-0x1, %eax
jge 0x8505
cmpl $-0x1, %r12d
jge 0x8555
decl %ebx
jne 0x84dd
jmp 0x852e
movq 0xacad4(%rip), %rax     # 0xb4fe0
movq (%rax), %rdi
leaq 0x8c190(%rip), %rsi     # 0x946a6
leaq 0x8c144(%rip), %rdx     # 0x94661
movl %r12d, %ecx
xorl %eax, %eax
callq 0x4700
movl $0x2, %ecx
jmp 0x84f9
movq 0x8(%rsp), %rax
movq (%rax), %rsi
leaq 0x10(%rsp), %rdi
leaq 0x8c11f(%rip), %rdx     # 0x94661
movl $0x1, %ecx
callq 0x861b
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
addl %ecx, %ecx
testl %ecx, %ecx
movq 0x58(%rsp), %rbx
je 0x8372
cmpl $0x2, %ecx
je 0x8326
jmp 0x835e
movq 0x30(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
callq 0x45b0
movl 0x1c(%rsp), %edi
callq 0x1ad20
xorl %eax, %eax
jmp 0x835e