// File: /workspace/github_c_makefile_star2_O3/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 0xe8a0
leaq 0x31167(%rip), %r14     # 0x3d684
movl (%r14), %edi
callq 0x3110
movq %rax, 0xe0(%rbx)
movl (%r14), %edi
movq %rbx, %rsi
callq 0x3610
testl %eax, %eax
jne 0xc82a
leaq 0x30(%rsp), %rdi
callq 0x32f0
testl %eax, %eax
jne 0xc7aa
callq 0xd687
movq %rax, %rbx
cmpl $0x1, 0x10e38(%rip)     # 0x1d398
jne 0xc570
xorl %edx, %edx
movl %edx, %edi
movq %rdi, %rsi
callq 0x33a0
jmp 0xc57c
xorl %edx, %edx
movl %edx, %edi
movq %rdi, %rsi
callq 0x3120
movq %rbx, 0x28(%rsp)
xorl %ecx, %ecx
leaq 0x8d76(%rip), %r14      # 0x15300
movq %rcx, %rdx
movq %rcx, %rsi
movq %rsi, %rbx
movq %rdx, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
movq %rax, %r15
movq 0x30ff1(%rip), %rdi     # 0x3d598
movq %r14, %rsi
movl $0x7c3, %edx            # imm = 0x7C3
callq 0xe2b3
movq 0x30fdd(%rip), %rdi     # 0x3d598
xorl %esi, %esi
movq %rbx, 0x20(%rsp)
movq %rbx, %rdx
movq %r14, %rcx
movl $0x7c4, %r8d            # imm = 0x7C4
callq 0xe36e
movq 0x30fc6(%rip), %r12     # 0x3d5a0
movq 0x38(%r12), %rax
movq %rax, 0x30fba(%rip)     # 0x3d5a0
testq %rax, %rax
je 0xc5f0
movq (%rax), %rdx
jmp 0xc5f7
movq $-0x1, %rdx
movq 0x30f9a(%rip), %rdi     # 0x3d598
xorl %r13d, %r13d
movl %r13d, %esi
movq %r14, %rcx
movl $0x7c7, %r8d            # imm = 0x7C7
callq 0xe311
movl 0x8(%r12), %eax
movl %eax, 0xc(%rsp)
movq 0x10(%r12), %rdi
movq 0x18(%rdi), %rbx
callq 0xd5bf
movq 0x18(%r12), %rax
movq 0x8(%rax), %rsi
movq 0x18(%rax), %rbp
movl 0x10d28(%rip), %edi     # 0x1d364
movq %rbp, %rdx
callq 0xc0bf
movq 0x18(%r12), %rdi
callq 0xd5bf
movq 0x30(%r12), %rdi
movq %r14, %rsi
movl $0x7d8, %edx            # imm = 0x7D8
callq 0xe2b3
movq 0x30(%r12), %rdi
movl %r13d, %esi
movl $0x1, %edx
movq %r14, %rcx
movl $0x7d9, %r8d            # imm = 0x7D9
callq 0xe36e
movq 0x30(%r12), %rdi
movq %r14, %rsi
movl $0x7da, %edx            # imm = 0x7DA
callq 0xe2e2
cmpl $0x1, 0x10d04(%rip)     # 0x1d398
movq 0x28(%r12), %rsi
movq %r15, %rdi
movq %rbx, %rdx
jne 0xc6a8
callq 0xbf96
jmp 0xc6ad
callq 0xc05f
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 0xe433
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 0xc590
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq 0x28(%rsp), %rcx
callq 0xd947
movq 0x30e70(%rip), %rdi     # 0x3d580
leaq 0x8be9(%rip), %rsi      # 0x15300
movl $0x7ea, %edx            # imm = 0x7EA
callq 0xe2b3
cmpq $0x0, 0x30e67(%rip)     # 0x3d590
jne 0xc849
movq 0x30e4a(%rip), %rdi     # 0x3d580
callq 0xe42e
testq %rax, %rax
jne 0xc849
movq 0x30e35(%rip), %rdi     # 0x3d580
leaq 0x8bae(%rip), %rbx      # 0x15300
movl $0x7ec, %edx            # imm = 0x7EC
movq %rbx, %rsi
callq 0xe2e2
movq 0x30e32(%rip), %rdi     # 0x3d598
movl $0x7ed, %edx            # imm = 0x7ED
movq %rbx, %rsi
callq 0xe2b3
cmpq $0x0, 0x30e25(%rip)     # 0x3d5a0
leaq 0x30f02(%rip), %r14     # 0x3d684
jne 0xc887
movq 0x30e09(%rip), %rdi     # 0x3d598
leaq 0x8b6a(%rip), %rcx      # 0x15300
movq $-0x1, %rdx
movl $0x7ef, %r8d            # imm = 0x7EF
xorl %esi, %esi
callq 0xe311
cmpl $0x0, 0x8(%rsp)
je 0xc7d1
movl (%r14), %edi
movq 0x110(%rsp), %rsi
callq 0x3610
testl %eax, %eax
jne 0xc868
movl $0x0, 0x8(%rsp)
movl 0xfc(%rsp), %ebp
testl %ebp, %ebp
jne 0xc7ee
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 0xc814
leaq 0x8c02(%rip), %rdi      # 0x1540c
xorl %eax, %eax
movq %rbx, %rsi
callq 0x479d
testl %r14d, %r14d
je 0xc821
movq %rbx, %rdi
callq 0x3050
movl %ebp, %edi
negl %edi
callq 0x4133
leaq 0x8a9f(%rip), %rdi      # 0x152d0
leaq 0x8ac8(%rip), %rsi      # 0x15300
leaq 0xa04d(%rip), %rcx      # 0x1688c
movl $0x7b8, %edx            # imm = 0x7B8
callq 0x3220
leaq 0xa056(%rip), %rdi      # 0x168a6
leaq 0x8aa9(%rip), %rsi      # 0x15300
leaq 0xa02e(%rip), %rcx      # 0x1688c
movl $0x7eb, %edx            # imm = 0x7EB
callq 0x3220
leaq 0x8a61(%rip), %rdi      # 0x152d0
leaq 0x8a8a(%rip), %rsi      # 0x15300
leaq 0xa00f(%rip), %rcx      # 0x1688c
movl $0x7f1, %edx            # imm = 0x7F1
callq 0x3220
leaq 0xa04f(%rip), %rdi      # 0x168dd
leaq 0x8a6b(%rip), %rsi      # 0x15300
leaq 0x9ff0(%rip), %rcx      # 0x1688c
movl $0x7ee, %edx            # imm = 0x7EE
callq 0x3220