// File: /workspace/github_c_makefile_star2_O2/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)
leaq 0x30(%rsp), %rbx
andq $0x0, 0xc8(%rbx)
andl $0x0, 0xd0(%rbx)
andq $0x0, 0xd8(%rbx)
callq 0xdcdc
leaq 0x30c96(%rip), %r14     # 0x3c674
movl (%r14), %edi
callq 0x3110
movq %rax, 0xe0(%rbx)
movl (%r14), %edi
movq %rbx, %rsi
callq 0x3600
testl %eax, %eax
jne 0xbce6
leaq 0x30(%rsp), %rdi
callq 0x32f0
testl %eax, %eax
jne 0xbc66
callq 0xca78
movq %rax, %rbx
cmpl $0x1, 0x10967(%rip)     # 0x1c388
jne 0xba31
xorl %edx, %edx
movl %edx, %edi
movq %rdi, %rsi
callq 0x3390
jmp 0xba40
xorl %eax, %eax
movl %eax, %edi
movq %rdi, %rsi
movq %rdi, %rdx
callq 0xb419
movq %rbx, 0x28(%rsp)
xorl %ecx, %ecx
leaq 0x88b2(%rip), %rbp      # 0x14300
movq %rcx, %rdx
movq %rcx, %r15
movq %rdx, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
movq %rax, %r14
movq 0x30b20(%rip), %rdi     # 0x3c588
movq %rbp, %rsi
movl $0x7c3, %edx            # imm = 0x7C3
callq 0xd763
movq 0x30b0c(%rip), %rdi     # 0x3c588
xorl %esi, %esi
movq %r15, 0x20(%rsp)
movq %r15, %rdx
movq %rbp, %rcx
movl $0x7c4, %r8d            # imm = 0x7C4
callq 0xd81e
movq 0x30af5(%rip), %r12     # 0x3c590
movq 0x38(%r12), %rax
movq %rax, 0x30ae9(%rip)     # 0x3c590
pushq $-0x1
popq %rdx
testq %rax, %rax
je 0xbab2
movq (%rax), %rdx
movq 0x30acf(%rip), %rdi     # 0x3c588
xorl %r13d, %r13d
movl %r13d, %esi
movq %rbp, %rcx
movl $0x7c7, %r8d            # imm = 0x7C7
callq 0xd7c1
movl 0x8(%r12), %eax
movl %eax, 0xc(%rsp)
movq 0x10(%r12), %rdi
movq 0x18(%rdi), %rbx
callq 0xc97f
movq 0x18(%r12), %rax
movq 0x8(%rax), %rsi
movq 0x18(%rax), %r15
movl 0x1085d(%rip), %edi     # 0x1c354
movq %r15, %rdx
callq 0xb58d
movq 0x18(%r12), %rdi
callq 0xc97f
movq 0x30(%r12), %rdi
movq %rbp, %rsi
movl $0x7d8, %edx            # imm = 0x7D8
callq 0xd763
movq 0x30(%r12), %rdi
movl %r13d, %esi
pushq $0x1
popq %rdx
movq %rbp, %rcx
movl $0x7d9, %r8d            # imm = 0x7D9
callq 0xd81e
movq 0x30(%r12), %rdi
movq %rbp, %rsi
movl $0x7da, %edx            # imm = 0x7DA
callq 0xd792
cmpl $0x1, 0x1083b(%rip)     # 0x1c388
movq 0x28(%r12), %rsi
movq %r14, %rdi
movq %rbx, %rdx
jne 0xbb61
callq 0xb4b7
jmp 0xbb66
callq 0xb553
movq %rax, %r13
movq 0x10(%rsp), %r14
addq %rbx, %r14
movq 0x18(%rsp), %rbx
addq %r15, %rbx
movq 0x30(%r12), %rdi
leaq 0x877b(%rip), %rbp      # 0x14300
movq %rbp, %rsi
movl $0x7df, %edx            # imm = 0x7DF
callq 0xd8e3
movq %r12, %rdi
callq 0x3050
movq 0x20(%rsp), %r15
incq %r15
movq %r13, %rax
movq %rbx, %rcx
movq %r14, %rdx
cmpl $0x0, 0xc(%rsp)
jne 0xba54
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq 0x28(%rsp), %rcx
callq 0xcd63
movq 0x309a0(%rip), %rdi     # 0x3c570
leaq 0x8729(%rip), %rsi      # 0x14300
movl $0x7ea, %edx            # imm = 0x7EA
callq 0xd763
cmpq $0x0, 0x30997(%rip)     # 0x3c580
jne 0xbd05
movq 0x3097a(%rip), %rdi     # 0x3c570
callq 0xd8de
testq %rax, %rax
jne 0xbd05
movq 0x30965(%rip), %rdi     # 0x3c570
leaq 0x86ee(%rip), %rbx      # 0x14300
movl $0x7ec, %edx            # imm = 0x7EC
movq %rbx, %rsi
callq 0xd792
movq 0x30962(%rip), %rdi     # 0x3c588
movl $0x7ed, %edx            # imm = 0x7ED
movq %rbx, %rsi
callq 0xd763
cmpq $0x0, 0x30955(%rip)     # 0x3c590
leaq 0x30a32(%rip), %r14     # 0x3c674
jne 0xbd43
movq 0x30939(%rip), %rdi     # 0x3c588
leaq 0x86aa(%rip), %rcx      # 0x14300
pushq $-0x1
popq %rdx
movl $0x7ef, %r8d            # imm = 0x7EF
xorl %esi, %esi
callq 0xd7c1
cmpl $0x0, 0x8(%rsp)
je 0xbc8d
movl (%r14), %edi
movq 0x110(%rsp), %rsi
callq 0x3600
testl %eax, %eax
jne 0xbd24
movl $0x0, 0x8(%rsp)
movl 0xfc(%rsp), %ebp
testl %ebp, %ebp
jne 0xbcaa
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 0xbcd0
leaq 0x8746(%rip), %rdi      # 0x1440c
xorl %eax, %eax
movq %rbx, %rsi
callq 0x4419
testl %r14d, %r14d
je 0xbcdd
movq %rbx, %rdi
callq 0x3050
movl %ebp, %edi
negl %edi
callq 0x3dc0
leaq 0x85e3(%rip), %rdi      # 0x142d0
leaq 0x860c(%rip), %rsi      # 0x14300
leaq 0x9b99(%rip), %rcx      # 0x15894
movl $0x7b8, %edx            # imm = 0x7B8
callq 0x3220
leaq 0x9ba2(%rip), %rdi      # 0x158ae
leaq 0x85ed(%rip), %rsi      # 0x14300
leaq 0x9b7a(%rip), %rcx      # 0x15894
movl $0x7eb, %edx            # imm = 0x7EB
callq 0x3220
leaq 0x85a5(%rip), %rdi      # 0x142d0
leaq 0x85ce(%rip), %rsi      # 0x14300
leaq 0x9b5b(%rip), %rcx      # 0x15894
movl $0x7f1, %edx            # imm = 0x7F1
callq 0x3220
leaq 0x9b9b(%rip), %rdi      # 0x158e5
leaq 0x85af(%rip), %rsi      # 0x14300
leaq 0x9b3c(%rip), %rcx      # 0x15894
movl $0x7ee, %edx            # imm = 0x7EE
callq 0x3220