// File: /workspace/github_c_makefile_star2_O0/ebiggers[P]xpack/lib/xpack_compress.c
// Function: write_block
static size_t
write_block(struct xpack_compressor *c, void *out, size_t out_nbytes_avail,
	    u32 block_size, u32 last_litrunlen, bool is_final_block)
{
	struct header_ostream os;
	size_t header_size;
	size_t items_size;
	int block_type;
	unsigned num_state_counts;
	unsigned order;

	/* Final litrunlen */
	record_litrunlen(c, &c->matches[c->num_matches], last_litrunlen);
	c->matches[c->num_matches].offset_sym = MAX_OFFSET_ALPHABET_SIZE;
	c->num_matches++;

	/* Choose the block type */
	block_type = choose_block_type(c);

	header_ostream_init(&os, out, out_nbytes_avail);

	/* Output the "final block" flag */
	header_ostream_write_bits(&os, is_final_block, 1);

	/* Output the block type */
	header_ostream_write_bits(&os, block_type, NUM_BLOCKTYPE_BITS);

	/* Output the block size */
	write_block_size(&os, block_size);

	/* Compute FSE state counts for each alphabet */

	c->codes.log2_num_literal_states =
		choose_state_counts(c->freqs.literal,
				    LITERAL_ALPHABET_SIZE,
				    MAX_LOG2_NUM_LITERAL_STATES,
				    c->codes.literal_state_counts);

	c->codes.log2_num_litrunlen_states =
		choose_state_counts(c->freqs.litrunlen,
				    LITRUNLEN_ALPHABET_SIZE,
				    MAX_LOG2_NUM_LITRUNLEN_STATES,
				    c->codes.litrunlen_state_counts);

	c->codes.log2_num_length_states =
		choose_state_counts(c->freqs.length,
				    LENGTH_ALPHABET_SIZE,
				    MAX_LOG2_NUM_LENGTH_STATES,
				    c->codes.length_state_counts);

	c->codes.log2_num_offset_states =
		choose_state_counts(c->freqs.offset,
				    MAX_OFFSET_ALPHABET_SIZE,
				    MAX_LOG2_NUM_OFFSET_STATES,
				    c->codes.offset_state_counts);

	if (block_type == BLOCKTYPE_ALIGNED) {
		c->codes.log2_num_aligned_states =
			choose_state_counts(c->freqs.aligned,
					    ALIGNED_ALPHABET_SIZE,
					    MAX_LOG2_NUM_ALIGNED_STATES,
					    c->codes.aligned_state_counts);
	}

	/* Output the FSE state counts for each alphabet */
	header_ostream_write_bits(&os, c->codes.log2_num_literal_states, 4);
	header_ostream_write_bits(&os, c->codes.log2_num_litrunlen_states, 4);
	header_ostream_write_bits(&os, c->codes.log2_num_length_states, 4);
	header_ostream_write_bits(&os, c->codes.log2_num_offset_states, 4);
	if (block_type == BLOCKTYPE_ALIGNED)
		header_ostream_write_bits(&os, c->codes.log2_num_aligned_states, 4);

#ifndef _MSC_VER
	STATIC_ASSERT(offsetof(struct codes,
			       aligned_state_counts[ALIGNED_ALPHABET_SIZE]) ==
		      offsetof(struct codes, state_counts) + sizeof(c->codes.state_counts));
#endif
	num_state_counts = ARRAY_LEN(c->codes.state_counts);
	if (block_type != BLOCKTYPE_ALIGNED)
		num_state_counts -= ALIGNED_ALPHABET_SIZE;

	write_state_counts(&os, c->codes.state_counts, num_state_counts);

	/* Output the number of extra bytes */
	order = bsr32(c->num_extra_bytes + 1);
	header_ostream_write_bits(&os, order, 5);
	header_ostream_write_bits(&os,
				  (c->num_extra_bytes + 1) - ((u32)1 << order),
				  order);

	/* Align to the next byte boundary */
	header_size = header_ostream_flush(&os);
	if (header_size == 0)
		return 0;

	/* Add the extra bytes */
	if (c->num_extra_bytes >= out_nbytes_avail - header_size)
		return 0;
	memcpy((u8 *)out + header_size, c->extra_bytes, c->num_extra_bytes);
	header_size += c->num_extra_bytes;

	/* Build the FSE encoding tables for each alphabet */

	build_fse_encoding_tables(c, c->codes.literal_sym_encinfo,
				  c->codes.literal_next_statesx,
				  c->codes.literal_state_counts,
				  LITERAL_ALPHABET_SIZE,
				  c->codes.log2_num_literal_states);

	build_fse_encoding_tables(c, c->codes.litrunlen_sym_encinfo,
				  c->codes.litrunlen_next_statesx,
				  c->codes.litrunlen_state_counts,
				  LITRUNLEN_ALPHABET_SIZE,
				  c->codes.log2_num_litrunlen_states);

	build_fse_encoding_tables(c, c->codes.length_sym_encinfo,
				  c->codes.length_next_statesx,
				  c->codes.length_state_counts,
				  LENGTH_ALPHABET_SIZE,
				  c->codes.log2_num_length_states);

	build_fse_encoding_tables(c, c->codes.offset_sym_encinfo,
				  c->codes.offset_next_statesx,
				  c->codes.offset_state_counts,
				  MAX_OFFSET_ALPHABET_SIZE,
				  c->codes.log2_num_offset_states);

	if (block_type == BLOCKTYPE_ALIGNED) {
		build_fse_encoding_tables(c, c->codes.aligned_sym_encinfo,
					  c->codes.aligned_next_statesx,
					  c->codes.aligned_state_counts,
					  ALIGNED_ALPHABET_SIZE,
					  c->codes.log2_num_aligned_states);
	}

	/* Encode the items */

	items_size = encode_items(c, (u8 *)out + header_size,
				  out_nbytes_avail - header_size,
				  block_type == BLOCKTYPE_ALIGNED);
	if (items_size == 0)
		return 0;

	return header_size + items_size;
}

// Assembly:
subq $0x78, %rsp
movb %r9b, %al
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdx, 0x58(%rsp)
movl %ecx, 0x54(%rsp)
movl %r8d, 0x50(%rsp)
andb $0x1, %al
movb %al, 0x4f(%rsp)
movq 0x68(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq $0x4c810, %rsi          # imm = 0x4C810
movq 0x68(%rsp), %rax
movl 0x3228(%rax), %eax
shlq $0x3, %rax
addq %rax, %rsi
movl 0x50(%rsp), %edx
callq 0x5640
movq 0x68(%rsp), %rax
movq 0x68(%rsp), %rcx
movl 0x3228(%rcx), %ecx
movb $0x20, 0x4c812(%rax,%rcx,8)
movq 0x68(%rsp), %rax
movl 0x3228(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x3228(%rax)
movq 0x68(%rsp), %rdi
callq 0x60c0
movl %eax, 0xc(%rsp)
movq 0x60(%rsp), %rsi
movq 0x58(%rsp), %rdx
leaq 0x20(%rsp), %rdi
callq 0x61a0
movb 0x4f(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %esi
leaq 0x20(%rsp), %rdi
movl $0x1, %edx
callq 0x6200
movslq 0xc(%rsp), %rsi
leaq 0x20(%rsp), %rdi
movl $0x3, %edx
callq 0x6200
movl 0x54(%rsp), %esi
leaq 0x20(%rsp), %rdi
callq 0x6310
movq 0x68(%rsp), %rdi
addq $0x28, %rdi
movq 0x68(%rsp), %rcx
addq $0x660, %rcx            # imm = 0x660
addq $0x20d4, %rcx           # imm = 0x20D4
movl $0x100, %esi            # imm = 0x100
movl $0xa, %edx
callq 0x6370
movl %eax, %ecx
movq 0x68(%rsp), %rax
movl %ecx, 0x2720(%rax)
movq 0x68(%rsp), %rdi
addq $0x28, %rdi
addq $0x400, %rdi            # imm = 0x400
movq 0x68(%rsp), %rcx
addq $0x660, %rcx            # imm = 0x660
addq $0x20d4, %rcx           # imm = 0x20D4
addq $0x200, %rcx            # imm = 0x200
movl $0x10, %esi
movl $0x9, %edx
callq 0x6370
movl %eax, %ecx
movq 0x68(%rsp), %rax
movl %ecx, 0x2724(%rax)
movq 0x68(%rsp), %rdi
addq $0x28, %rdi
addq $0x440, %rdi            # imm = 0x440
movq 0x68(%rsp), %rcx
addq $0x660, %rcx            # imm = 0x660
addq $0x20d4, %rcx           # imm = 0x20D4
addq $0x220, %rcx            # imm = 0x220
movl $0x40, %esi
movl $0x9, %edx
callq 0x6370
movl %eax, %ecx
movq 0x68(%rsp), %rax
movl %ecx, 0x2728(%rax)
movq 0x68(%rsp), %rdi
addq $0x28, %rdi
addq $0x540, %rdi            # imm = 0x540
movq 0x68(%rsp), %rcx
addq $0x660, %rcx            # imm = 0x660
addq $0x20d4, %rcx           # imm = 0x20D4
addq $0x2a0, %rcx            # imm = 0x2A0
movl $0x20, %esi
movl $0x9, %edx
callq 0x6370
movl %eax, %ecx
movq 0x68(%rsp), %rax
movl %ecx, 0x272c(%rax)
cmpl $0x2, 0xc(%rsp)
jne 0x5b22
movq 0x68(%rsp), %rdi
addq $0x28, %rdi
addq $0x5c0, %rdi            # imm = 0x5C0
movq 0x68(%rsp), %rcx
addq $0x660, %rcx            # imm = 0x660
addq $0x20d4, %rcx           # imm = 0x20D4
addq $0x2e0, %rcx            # imm = 0x2E0
movl $0x8, %esi
movl $0x7, %edx
callq 0x6370
movl %eax, %ecx
movq 0x68(%rsp), %rax
movl %ecx, 0x2730(%rax)
movq 0x68(%rsp), %rax
movl 0x2720(%rax), %eax
movl %eax, %esi
leaq 0x20(%rsp), %rdi
movl $0x4, %edx
callq 0x6200
movq 0x68(%rsp), %rax
movl 0x2724(%rax), %eax
movl %eax, %esi
leaq 0x20(%rsp), %rdi
movl $0x4, %edx
callq 0x6200
movq 0x68(%rsp), %rax
movl 0x2728(%rax), %eax
movl %eax, %esi
leaq 0x20(%rsp), %rdi
movl $0x4, %edx
callq 0x6200
movq 0x68(%rsp), %rax
movl 0x272c(%rax), %eax
movl %eax, %esi
leaq 0x20(%rsp), %rdi
movl $0x4, %edx
callq 0x6200
cmpl $0x2, 0xc(%rsp)
jne 0x5bb5
movq 0x68(%rsp), %rax
movl 0x2730(%rax), %eax
movl %eax, %esi
leaq 0x20(%rsp), %rdi
movl $0x4, %edx
callq 0x6200
movl $0x178, 0x8(%rsp)       # imm = 0x178
cmpl $0x2, 0xc(%rsp)
je 0x5bcf
movl 0x8(%rsp), %eax
subl $0x8, %eax
movl %eax, 0x8(%rsp)
movq 0x68(%rsp), %rsi
addq $0x660, %rsi            # imm = 0x660
addq $0x20d4, %rsi           # imm = 0x20D4
movl 0x8(%rsp), %edx
leaq 0x20(%rsp), %rdi
callq 0x6470
movq 0x68(%rsp), %rax
movl 0x322c(%rax), %eax
addl $0x1, %eax
bsrl %eax, %ecx
xorl $0x1f, %ecx
movl $0x1f, %eax
subl %ecx, %eax
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %eax
movl %eax, %esi
leaq 0x20(%rsp), %rdi
movl $0x5, %edx
callq 0x6200
movq 0x68(%rsp), %rax
movl 0x322c(%rax), %eax
addl $0x1, %eax
movl 0x4(%rsp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
subl %ecx, %eax
movl %eax, %eax
movl %eax, %esi
movl 0x4(%rsp), %edx
leaq 0x20(%rsp), %rdi
callq 0x6200
leaq 0x20(%rsp), %rdi
callq 0x6610
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0x5c78
movq $0x0, 0x70(%rsp)
jmp 0x5ef5
movq 0x68(%rsp), %rax
movl 0x322c(%rax), %eax
movq 0x58(%rsp), %rcx
subq 0x18(%rsp), %rcx
cmpq %rcx, %rax
jb 0x5ca0
movq $0x0, 0x70(%rsp)
jmp 0x5ef5
movq 0x60(%rsp), %rdi
addq 0x18(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq $0x171798, %rsi         # imm = 0x171798
movq 0x68(%rsp), %rax
movl 0x322c(%rax), %eax
movl %eax, %edx
callq 0x1050
movq 0x68(%rsp), %rax
movl 0x322c(%rax), %eax
addq 0x18(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x68(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq $0x660, %rsi            # imm = 0x660
movq 0x68(%rsp), %rdx
addq $0x660, %rdx            # imm = 0x660
addq $0xbc0, %rdx            # imm = 0xBC0
movq 0x68(%rsp), %rcx
addq $0x660, %rcx            # imm = 0x660
addq $0x20d4, %rcx           # imm = 0x20D4
movq 0x68(%rsp), %rax
movl 0x2720(%rax), %r9d
movl $0x100, %r8d            # imm = 0x100
callq 0x66c0
movq 0x68(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq $0x660, %rsi            # imm = 0x660
addq $0x800, %rsi            # imm = 0x800
movq 0x68(%rsp), %rdx
addq $0x660, %rdx            # imm = 0x660
addq $0x13c0, %rdx           # imm = 0x13C0
movq 0x68(%rsp), %rcx
addq $0x660, %rcx            # imm = 0x660
addq $0x20d4, %rcx           # imm = 0x20D4
addq $0x200, %rcx            # imm = 0x200
movq 0x68(%rsp), %rax
movl 0x2724(%rax), %r9d
movl $0x10, %r8d
callq 0x66c0
movq 0x68(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq $0x660, %rsi            # imm = 0x660
addq $0x880, %rsi            # imm = 0x880
movq 0x68(%rsp), %rdx
addq $0x660, %rdx            # imm = 0x660
addq $0x17c0, %rdx           # imm = 0x17C0
movq 0x68(%rsp), %rcx
addq $0x660, %rcx            # imm = 0x660
addq $0x20d4, %rcx           # imm = 0x20D4
addq $0x220, %rcx            # imm = 0x220
movq 0x68(%rsp), %rax
movl 0x2728(%rax), %r9d
movl $0x40, %r8d
callq 0x66c0
movq 0x68(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq $0x660, %rsi            # imm = 0x660
addq $0xa80, %rsi            # imm = 0xA80
movq 0x68(%rsp), %rdx
addq $0x660, %rdx            # imm = 0x660
addq $0x1bc0, %rdx           # imm = 0x1BC0
movq 0x68(%rsp), %rcx
addq $0x660, %rcx            # imm = 0x660
addq $0x20d4, %rcx           # imm = 0x20D4
addq $0x2a0, %rcx            # imm = 0x2A0
movq 0x68(%rsp), %rax
movl 0x272c(%rax), %r9d
movl $0x20, %r8d
callq 0x66c0
cmpl $0x2, 0xc(%rsp)
jne 0x5ea2
movq 0x68(%rsp), %rdi
movq 0x68(%rsp), %rsi
addq $0x660, %rsi            # imm = 0x660
addq $0xb80, %rsi            # imm = 0xB80
movq 0x68(%rsp), %rdx
addq $0x660, %rdx            # imm = 0x660
addq $0x1fc0, %rdx           # imm = 0x1FC0
movq 0x68(%rsp), %rcx
addq $0x660, %rcx            # imm = 0x660
addq $0x20d4, %rcx           # imm = 0x20D4
addq $0x2e0, %rcx            # imm = 0x2E0
movq 0x68(%rsp), %rax
movl 0x2730(%rax), %r9d
movl $0x8, %r8d
callq 0x66c0
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
addq 0x18(%rsp), %rsi
movq 0x58(%rsp), %rdx
subq 0x18(%rsp), %rdx
cmpl $0x2, 0xc(%rsp)
sete %al
movzbl %al, %ecx
andl $0x1, %ecx
callq 0x68f0
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
jne 0x5ee6
movq $0x0, 0x70(%rsp)
jmp 0x5ef5
movq 0x18(%rsp), %rax
addq 0x10(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rax
addq $0x78, %rsp
retq
nop