// File: /workspace/github_c_makefile_star2_O1/madler[P]pigz/pigz.c
// Function: write_thread
local void write_thread(void *dummy) {
    long seq;                       // next sequence number looking for
    struct job *job;                // job pulled and working on
    size_t len;                     // input length
    int more;                       // true if more chunks to write
    length_t head;                  // header length
    length_t ulen;                  // total uncompressed size (overflow ok)
    length_t clen;                  // total compressed size (overflow ok)
    unsigned long check;            // check value of uncompressed data
    ball_t err;                     // error information from throw()

    (void)dummy;

    try {
        // build and write header
        Trace(("-- write thread running"));
        head = put_header();

        // process output of compress threads until end of input
        ulen = clen = 0;
        check = CHECK(0L, Z_NULL, 0);
        seq = 0;
        do {
            // get next write job in order
            possess(write_first);
            wait_for(write_first, TO_BE, seq);
            job = write_head;
            write_head = job->next;
            twist(write_first, TO, write_head == NULL ? -1 : write_head->seq);

            // update lengths, save uncompressed length for COMB
            more = job->more;
            len = job->in->len;
            drop_space(job->in);
            ulen += len;
            clen += job->out->len;

            // write the compressed data and drop the output buffer
            Trace(("-- writing #%ld", seq));
            writen(g.outd, job->out->buf, job->out->len);
            drop_space(job->out);
            Trace(("-- wrote #%ld%s", seq, more ? "" : " (last)"));

            // wait for check calculation to complete, then combine, once the
            // compress thread is done with the input, release it
            possess(job->calc);
            wait_for(job->calc, TO_BE, 1);
            release(job->calc);
            check = COMB(check, job->check, len);
            Trace(("-- combined #%ld%s", seq, more ? "" : " (last)"));

            // free the job
            free_lock(job->calc);
            FREE(job);

            // get the next buffer in sequence
            seq++;
        } while (more);

        // write trailer
        put_trailer(ulen, clen, check, head);

        // verify no more jobs, prepare for next use
        possess(compress_have);
        assert(compress_head == NULL && peek_lock(compress_have) == 0);
        release(compress_have);
        possess(write_first);
        assert(write_head == NULL);
        twist(write_first, TO, -1);
    }
    catch (err) {
        THREADABORT(err);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movl $0x1, 0x8(%rsp)
xorl %eax, %eax
leaq 0x30(%rsp), %rbx
movq %rax, 0xc8(%rbx)
movl $0x0, 0xd0(%rbx)
movq %rax, 0xd8(%rbx)
callq 0xe508
leaq 0x31538(%rip), %r14     # 0x3d674
movl (%r14), %edi
callq 0x3110
movq %rax, 0xe0(%rbx)
movl (%r14), %edi
movq %rbx, %rsi
callq 0x3600
testl %eax, %eax
jne 0xc449
leaq 0x30(%rsp), %rdi
callq 0x32f0
testl %eax, %eax
jne 0xc3c9
callq 0xd2ec
movq %rax, %rbx
cmpl $0x1, 0x11209(%rip)     # 0x1d388
jne 0xc18f
xorl %edx, %edx
movl %edx, %edi
movq %rdi, %rsi
callq 0x3390
jmp 0xc19b
xorl %edx, %edx
movl %edx, %edi
movq %rdi, %rsi
callq 0x3120
movq %rbx, 0x28(%rsp)
xorl %ecx, %ecx
leaq 0x9157(%rip), %r14      # 0x15300
movq %rcx, %rdx
movq %rcx, %rsi
movq %rsi, %rbx
movq %rdx, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
movq %rax, %r15
movq 0x313c2(%rip), %rdi     # 0x3d588
movq %r14, %rsi
movl $0x7c3, %edx            # imm = 0x7C3
callq 0xdf23
movq 0x313ae(%rip), %rdi     # 0x3d588
xorl %esi, %esi
movq %rbx, 0x20(%rsp)
movq %rbx, %rdx
movq %r14, %rcx
movl $0x7c4, %r8d            # imm = 0x7C4
callq 0xdfde
movq 0x31397(%rip), %r12     # 0x3d590
movq 0x38(%r12), %rax
movq %rax, 0x3138b(%rip)     # 0x3d590
testq %rax, %rax
je 0xc20f
movq (%rax), %rdx
jmp 0xc216
movq $-0x1, %rdx
movq 0x3136b(%rip), %rdi     # 0x3d588
xorl %r13d, %r13d
movl %r13d, %esi
movq %r14, %rcx
movl $0x7c7, %r8d            # imm = 0x7C7
callq 0xdf81
movl 0x8(%r12), %eax
movl %eax, 0xc(%rsp)
movq 0x10(%r12), %rdi
movq 0x18(%rdi), %rbx
callq 0xd224
movq 0x18(%r12), %rax
movq 0x8(%rax), %rsi
movq 0x18(%rax), %rbp
movl 0x110f9(%rip), %edi     # 0x1d354
movq %rbp, %rdx
callq 0xbcde
movq 0x18(%r12), %rdi
callq 0xd224
movq 0x30(%r12), %rdi
movq %r14, %rsi
movl $0x7d8, %edx            # imm = 0x7D8
callq 0xdf23
movq 0x30(%r12), %rdi
movl %r13d, %esi
movl $0x1, %edx
movq %r14, %rcx
movl $0x7d9, %r8d            # imm = 0x7D9
callq 0xdfde
movq 0x30(%r12), %rdi
movq %r14, %rsi
movl $0x7da, %edx            # imm = 0x7DA
callq 0xdf52
cmpl $0x1, 0x110d5(%rip)     # 0x1d388
movq 0x28(%r12), %rsi
movq %r15, %rdi
movq %rbx, %rdx
jne 0xc2c7
callq 0xbbae
jmp 0xc2cc
callq 0xbc7e
movq %rax, %r13
movq 0x10(%rsp), %r15
addq %rbx, %r15
movq 0x18(%rsp), %rbx
addq %rbp, %rbx
movq 0x30(%r12), %rdi
movq %r14, %rsi
movl $0x7df, %edx            # imm = 0x7DF
callq 0xe0a3
movq %r12, %rdi
callq 0x3050
movq 0x20(%rsp), %rsi
incq %rsi
movq %r13, %rax
movq %rbx, %rcx
movq %r15, %rdx
cmpl $0x0, 0xc(%rsp)
jne 0xc1af
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq 0x28(%rsp), %rcx
callq 0xd5b1
movq 0x31241(%rip), %rdi     # 0x3d570
leaq 0x8fca(%rip), %rsi      # 0x15300
movl $0x7ea, %edx            # imm = 0x7EA
callq 0xdf23
cmpq $0x0, 0x31238(%rip)     # 0x3d580
jne 0xc468
movq 0x3121b(%rip), %rdi     # 0x3d570
callq 0xe09e
testq %rax, %rax
jne 0xc468
movq 0x31206(%rip), %rdi     # 0x3d570
leaq 0x8f8f(%rip), %rbx      # 0x15300
movl $0x7ec, %edx            # imm = 0x7EC
movq %rbx, %rsi
callq 0xdf52
movq 0x31203(%rip), %rdi     # 0x3d588
movl $0x7ed, %edx            # imm = 0x7ED
movq %rbx, %rsi
callq 0xdf23
cmpq $0x0, 0x311f6(%rip)     # 0x3d590
leaq 0x312d3(%rip), %r14     # 0x3d674
jne 0xc4a6
movq 0x311da(%rip), %rdi     # 0x3d588
leaq 0x8f4b(%rip), %rcx      # 0x15300
movq $-0x1, %rdx
movl $0x7ef, %r8d            # imm = 0x7EF
xorl %esi, %esi
callq 0xdf81
cmpl $0x0, 0x8(%rsp)
je 0xc3f0
movl (%r14), %edi
movq 0x110(%rsp), %rsi
callq 0x3600
testl %eax, %eax
jne 0xc487
movl $0x0, 0x8(%rsp)
movl 0xfc(%rsp), %ebp
testl %ebp, %ebp
jne 0xc40d
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x100(%rsp), %r14d
movq 0x108(%rsp), %rbx
cmpl $0x20, %ebp
je 0xc433
leaq 0x8fe3(%rip), %rdi      # 0x1540c
xorl %eax, %eax
movq %rbx, %rsi
callq 0x476b
testl %r14d, %r14d
je 0xc440
movq %rbx, %rdi
callq 0x3050
movl %ebp, %edi
negl %edi
callq 0x4106
leaq 0x8e80(%rip), %rdi      # 0x152d0
leaq 0x8ea9(%rip), %rsi      # 0x15300
leaq 0xa42e(%rip), %rcx      # 0x1688c
movl $0x7b8, %edx            # imm = 0x7B8
callq 0x3220
leaq 0xa437(%rip), %rdi      # 0x168a6
leaq 0x8e8a(%rip), %rsi      # 0x15300
leaq 0xa40f(%rip), %rcx      # 0x1688c
movl $0x7eb, %edx            # imm = 0x7EB
callq 0x3220
leaq 0x8e42(%rip), %rdi      # 0x152d0
leaq 0x8e6b(%rip), %rsi      # 0x15300
leaq 0xa3f0(%rip), %rcx      # 0x1688c
movl $0x7f1, %edx            # imm = 0x7F1
callq 0x3220
leaq 0xa430(%rip), %rdi      # 0x168dd
leaq 0x8e4c(%rip), %rsi      # 0x15300
leaq 0xa3d1(%rip), %rcx      # 0x1688c
movl $0x7ee, %edx            # imm = 0x7EE
callq 0x3220