// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq $0x0, -0x28(%rbp)
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x4240
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x58(%rbp)
callq 0x24990
movl %eax, -0x5c(%rbp)
xorl %edi, %edi
callq 0x24970
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
leaq 0xe2055(%rip), %rdx     # 0xea661
callq 0x8b70
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x8621
jmp 0x8a0e
cmpl $0x0, -0x1c(%rbp)
jle 0x8644
movq -0x28(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq 0xe202c(%rip), %rdx     # 0xea661
callq 0x92f0
cmpl $0x0, %eax
je 0x8644
jmp 0x8a0e
movq -0x58(%rbp), %rdi
movl $0xa, %esi
callq 0x4160
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x28(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x58(%rbp), %rax
subq %rax, %rdx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
leaq 0xe1fe5(%rip), %r8      # 0xea661
callq 0x9370
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jge 0x8691
jmp 0x8a0e
movq -0x28(%rbp), %rdi
callq 0xd530
cmpl $0x0, %eax
jge 0x86a4
jmp 0x8a0e
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
movl $0xa, %esi
callq 0x4160
addq $0x1, %rax
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x78(%rbp), %rdx
movq -0x68(%rbp), %rax
subq %rax, %rdx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
leaq 0xe1f79(%rip), %r8      # 0xea661
callq 0x9370
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jge 0x86fd
jmp 0x8a0e
movq -0x28(%rbp), %rdi
callq 0xd530
cmpl $0x0, %eax
jge 0x8710
jmp 0x8a0e
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
leaq -0x28(%rbp), %rdi
leaq 0xe1f32(%rip), %rdx     # 0xea661
xorl %ecx, %ecx
callq 0x8c10
cmpl $0x0, %eax
je 0x8740
jmp 0x8a0e
movq -0x80(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x70(%rbp), %rax
jle 0x89ec
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x88(%rbp), %rsi
callq 0x4640
cmpl $0x0, %eax
je 0x877e
jmp 0x8a0e
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
leaq 0xe5df4(%rip), %rsi     # 0xee581
leaq 0xe1ecd(%rip), %rdx     # 0xea661
callq 0x8b70
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x87a9
jmp 0x8a0e
cmpl $0x0, -0x1c(%rbp)
jle 0x87cc
movq -0x28(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq 0xe1ea4(%rip), %rdx     # 0xea661
callq 0x92f0
cmpl $0x0, %eax
je 0x87cc
jmp 0x8a0e
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x48(%rcx), %rax
jae 0x8941
movq -0x58(%rbp), %rdi
addq -0x38(%rbp), %rdi
movl $0xa, %esi
callq 0x4160
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x90(%rbp)
je 0x8824
movq -0x90(%rbp), %rax
movq -0x58(%rbp), %rcx
addq -0x38(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0xb0(%rbp)
jmp 0x8837
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
subq -0x38(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x28(%rbp), %rdi
movl $0xa, %esi
leaq -0x50(%rbp), %rdx
callq 0xec50
movl %eax, -0x9c(%rbp)
cmpl $-0x1, -0x9c(%rbp)
jge 0x886b
jmp 0x8941
cmpl $-0x1, -0x9c(%rbp)
jne 0x88a7
movq 0xff765(%rip), %rax     # 0x107fe0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %r8
leaq 0xe1df8(%rip), %rsi     # 0xea685
leaq 0xe1dcd(%rip), %rdx     # 0xea661
leaq 0xe1d69(%rip), %rcx     # 0xea604
movb $0x0, %al
callq 0x46d0
jmp 0x8a0e
jmp 0x88a9
movq -0x50(%rbp), %rax
cmpq -0x98(%rbp), %rax
jne 0x88d3
movq -0x58(%rbp), %rdi
addq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x98(%rbp), %rdx
callq 0x41b0
cmpl $0x0, %eax
je 0x8929
movq 0xff706(%rip), %rax     # 0x107fe0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x98(%rbp), %rax
movl %eax, %r8d
movq -0x10(%rbp), %rax
movq 0x40(%rax), %r9
addq -0x38(%rbp), %r9
movq -0x50(%rbp), %rax
movl %eax, %r10d
movq -0x40(%rbp), %rax
leaq 0xe1d06(%rip), %rsi     # 0xea613
leaq 0xe1d4d(%rip), %rdx     # 0xea661
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x46d0
jmp 0x8a0e
movq -0x98(%rbp), %rax
addq $0x1, %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x87d4
movl $0x0, -0xa0(%rbp)
cmpl $0x3, -0xa0(%rbp)
jge 0x89af
movq -0x28(%rbp), %rdi
movl $0xa, %esi
leaq -0x50(%rbp), %rdx
callq 0xec50
movl %eax, -0xa4(%rbp)
cmpl $-0x2, -0xa4(%rbp)
jle 0x899c
movq 0xff664(%rip), %rax     # 0x107fe0
movq (%rax), %rdi
movl -0xa4(%rbp), %ecx
leaq 0xe1d1a(%rip), %rsi     # 0xea6a6
leaq 0xe1cce(%rip), %rdx     # 0xea661
movb $0x0, %al
callq 0x46d0
jmp 0x8a0e
jmp 0x899e
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x894b
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
leaq -0x28(%rbp), %rdi
leaq 0xe1c9f(%rip), %rdx     # 0xea661
movl $0x1, %ecx
callq 0x8c10
cmpl $0x0, %eax
jne 0x89d3
jmp 0x8a0e
jmp 0x89d5
movq -0x88(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x874f
leaq -0x50(%rbp), %rdi
callq 0x9b90
movq %rax, %rdi
callq 0x4590
movl -0x5c(%rbp), %edi
callq 0x24970
movl $0x0, -0x4(%rbp)
jmp 0x8a3e
movl -0x5c(%rbp), %edi
callq 0x24970
cmpq $0x0, -0x28(%rbp)
je 0x8a26
movq -0x28(%rbp), %rdi
callq 0xe0e0
leaq -0x50(%rbp), %rdi
callq 0x9b90
movq %rax, %rdi
callq 0x4590
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopw (%rax,%rax)