// File: /workspace/github_c_makefile_star2_O1/hcmiya[P]opuscomment/src/read.c
// Function: store_tags
void store_tags(ogg_page *np, struct rettag_st *rst, struct edit_st *est, bool packet_break_in_page) {
	FILE *fptag = rst->tag;
	size_t const pagebuflen = 65536; // oggページの最大長 = 65307
	uint8_t pagebuf[pagebuflen];
	
	// タグ個数書き込み
	fseek(fptag, rst->tagbegin, SEEK_SET);
	*(uint32_t*)pagebuf = oi32(rst->num + est->num);
	fwrite(pagebuf, 4, 1, fptag);
	fseek(fptag, 0, SEEK_END);
	
	// 編集入力書き込み
	rewind(est->str); rewind(est->len);
	while (fread(pagebuf, 4, 1, est->len)) {
		// ここでpagebufはOgg構造関係なく単なる移し替え用のバッファ
		size_t reclen = *(uint32_t*)pagebuf;
		*(uint32_t*)pagebuf = oi32(reclen);
		fwrite(pagebuf, 4, 1, fptag);
		while (reclen) {
			size_t readlen = fill_buffer(pagebuf, reclen, pagebuflen, est->str);
			fwrite(pagebuf, 1, readlen, fptag);
			reclen -= readlen;
		}
	}
	fclose(est->str); fclose(est->len);
	
	if (rst->padding && rst->part_of_comment) {
		// パディングがある時はfptagの後ろに移す
		rewind(rst->padding);
		size_t readlen;
		while (readlen = fread(pagebuf, 1, pagebuflen, rst->padding)) {
			fwrite(pagebuf, 1, readlen, fptag);
		}
		fclose(rst->padding);
		rst->padding = NULL;
	}
	long commentlen = ftell(fptag);
	if (commentlen > TAG_LENGTH_LIMIT__OUTPUT) {
		exceed_output_limit();
	}
	if (codec->type == CODEC_FLAC) return;
	
	rewind(fptag);
	ogg_page og;
	og.header_len = 282;
	og.header = pagebuf;
	og.body_len = 255 * 255;
	og.body = pagebuf + 282;
	memcpy(og.header, "\x4f\x67\x67\x53\x0\x0", 6);
	set_granulepos(&og, -1);
	*(uint32_t*)&og.header[14] = oi32(opus_sno);
	memset(og.header + 26, 0xff, 256);
	
	uint32_t idx = 1;
	while (commentlen >= 255 * 255) {
		fread(og.body, 1, 255 * 255, fptag);
		set_pageno(&og, idx++);
		ogg_page_checksum_set(&og);
		write_page(&og, built_stream);
		commentlen -= 255 * 255;
		if (!og.header[5]) og.header[5] = 1;
	}
	
	// パケットの最後のページ生成
	og.header[5] = idx != 1;
	set_granulepos(&og, 0);
	set_pageno(&og, idx++);
	og.header[26] = commentlen / 255 + 1;
	og.header[26 + og.header[26]] = commentlen % 255;
	fread(og.body, 1, commentlen, fptag);
	fclose(fptag);
	og.header_len = 27 + og.header[26];
	og.body_len = commentlen;
	ogg_page_checksum_set(&og);
	write_page(&og, built_stream);
	
	if (input_is_regular_file) {
		// 出力するタグ部分のページ番号が入力の音声開始部分のページ番号に満たない場合、
		// 空のページを生成して開始ページ番号を合わせる
		og.header_len = 27;
		og.header[5] = 0;
		set_granulepos(&og, -1);
		og.header[26] = 0;
		og.body_len = 0;
		while (idx < opus_idx - packet_break_in_page) {
			set_pageno(&og, idx++);
			ogg_page_checksum_set(&og);
			write_page(&og, built_stream);
		}
	}
	
	if (packet_break_in_page) {
		uint8_t *lace_tag = &np->header[27];
		while (*lace_tag == 255) {
			lace_tag++;
			np->body_len -= 255;
			np->body += 255;
		}
		np->body_len -= *lace_tag;
		np->body += *lace_tag;
		lace_tag++;
		np->header_len -= lace_tag - &np->header[27];
		np->header[5] = 0;
		np->header[26] -= lace_tag - &np->header[27];
		memmove(&np->header[27], lace_tag, np->header[26]);
		set_pageno(np, idx++);
		ogg_page_checksum_set(np);
		write_page(np, built_stream);
	}
	
	put_non_opus_stream();

	if (input_is_regular_file && idx == opus_idx) {
		put_left(0);
		/* NOTREACHED */
	}
	opus_idx_diff = idx - opus_idx;
	opst = PAGE_SOUND;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10038, %rsp          # imm = 0x10038
movl %ecx, 0x4(%rsp)
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, 0x8(%rsp)
movq (%rsi), %r14
movq 0x10(%rsi), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x2320
movl 0x18(%r13), %edi
addl 0x18(%r12), %edi
callq 0x6d2b
leaq 0x30(%rsp), %rbx
movl %eax, (%rbx)
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rcx
callq 0x2460
movq %r14, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2320
movq (%r13), %rdi
callq 0x2150
movq 0x8(%r13), %rdi
callq 0x2150
movq 0x8(%r13), %rcx
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x20e0
testq %rax, %rax
je 0x4078
leaq 0x30(%rsp), %rbp
movl 0x30(%rsp), %ebx
movl %ebx, %edi
callq 0x6d2b
movl %eax, 0x30(%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %rbp, %rdi
movq %r14, %rcx
callq 0x2460
testq %rbx, %rbx
je 0x405d
movq (%r13), %rcx
movl $0x10000, %edx          # imm = 0x10000
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x5a09
movq %rax, %r15
movl $0x1, %esi
movq %rbp, %rdi
movq %rax, %rdx
movq %r14, %rcx
callq 0x2460
subq %r15, %rbx
jne 0x402e
movq 0x8(%r13), %rcx
movl $0x4, %esi
movl $0x1, %edx
movq %rbp, %rdi
callq 0x20e0
testq %rax, %rax
jne 0x4005
movq (%r13), %rdi
callq 0x2120
movq 0x8(%r13), %rdi
callq 0x2120
movq 0x8(%r12), %rdi
testq %rdi, %rdi
je 0x4106
cmpb $0x1, 0x29(%r12)
jne 0x4106
callq 0x2150
movq 0x8(%r12), %rcx
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
movl $0x10000, %edx          # imm = 0x10000
callq 0x20e0
testq %rax, %rax
je 0x40f3
leaq 0x30(%rsp), %rbx
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movq %r14, %rcx
callq 0x2460
movq 0x8(%r12), %rcx
movl $0x1, %esi
movl $0x10000, %edx          # imm = 0x10000
movq %rbx, %rdi
callq 0x20e0
testq %rax, %rax
jne 0x40c4
movq 0x8(%r12), %rdi
callq 0x2120
movq $0x0, 0x8(%r12)
movq %r14, %rdi
callq 0x2280
movq %rax, %r12
cmpq $0x2800001, %rax        # imm = 0x2800001
jl 0x411e
callq 0x71aa
leaq 0x1854b(%rip), %rax     # 0x1c670
movq (%rax), %rax
cmpl $0x2, (%rax)
je 0x445a
movq %r14, %rdi
callq 0x2150
movl $0x11a, %eax            # imm = 0x11A
leaq 0x10(%rsp), %rbx
movq %rax, 0x8(%rbx)
leaq 0x30(%rsp), %rcx
movq %rcx, (%rbx)
movl $0xfe01, %r15d          # imm = 0xFE01
movq %r15, 0x18(%rbx)
addq %rax, %rcx
movq %rcx, 0x10(%rbx)
movl $0x5367674f, 0x30(%rsp) # imm = 0x5367674F
movw $0x0, 0x34(%rsp)
movq %rbx, %rdi
movq $-0x1, %rsi
callq 0x5ba6
leaq 0x1854f(%rip), %rax     # 0x1c6d4
movl (%rax), %edi
callq 0x6d2b
movq (%rbx), %rdi
movl %eax, 0xe(%rdi)
addq $0x1a, %rdi
movl $0x100, %edx            # imm = 0x100
movl $0xff, %esi
callq 0x2190
movl $0x1, %r13d
cmpq %r15, %r12
jl 0x4220
movl $0x1, %r13d
leaq 0x10(%rsp), %r15
leaq 0x1851e(%rip), %rbx     # 0x1c6e0
movq 0x20(%rsp), %rdi
movl $0x1, %esi
movl $0xfe01, %edx           # imm = 0xFE01
movq %r14, %rcx
callq 0x20e0
movq %r15, %rdi
movl %r13d, %esi
callq 0x5b93
movq %r15, %rdi
callq 0x2110
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x5b3b
movq 0x10(%rsp), %rax
cmpb $0x0, 0x5(%rax)
jne 0x4206
movb $0x1, 0x5(%rax)
addq $-0xfe01, %r12          # imm = 0xFFFF01FF
leaq 0xfe01(%r12), %rax
incl %r13d
cmpq $0x1fc01, %rax          # imm = 0x1FC01
jg 0x41c2
cmpl $0x1, %r13d
leaq 0x10(%rsp), %rbp
movq (%rbp), %rax
setne 0x5(%rax)
movq %rbp, %rdi
xorl %esi, %esi
callq 0x5ba6
leal 0x1(%r13), %r15d
movq %rbp, %rdi
movl %r13d, %esi
callq 0x5b93
movabsq $-0x7f7f7f7f7f7f7f7f, %rcx # imm = 0x8080808080808081
movq %r12, %rax
imulq %rcx
addq %r12, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
leal 0x1(%rdx), %eax
movq (%rbp), %rcx
movb %al, 0x1a(%rcx)
addl %r12d, %edx
movzbl %al, %eax
movb %dl, 0x1a(%rcx,%rax)
movq 0x10(%rbp), %rdi
movl $0x1, %esi
movq %r12, %rdx
movq %r14, %rcx
callq 0x20e0
movq %r14, %rdi
callq 0x2120
movq (%rbp), %rax
movzbl 0x1a(%rax), %eax
addq $0x1b, %rax
movq %rax, 0x8(%rbp)
movq %r12, 0x18(%rbp)
movq %rbp, %rdi
callq 0x2110
leaq 0x18422(%rip), %r13     # 0x1c6e0
movq (%r13), %rsi
movq %rbp, %rdi
callq 0x5b3b
leaq 0x183f7(%rip), %rax     # 0x1c6c8
cmpb $0x1, (%rax)
jne 0x435f
leaq 0x10(%rsp), %rbx
movq $0x1b, 0x8(%rbx)
movq (%rbx), %rax
xorl %ebp, %ebp
movb %bpl, 0x5(%rax)
movq %rbx, %rdi
movq $-0x1, %rsi
callq 0x5ba6
movq (%rbx), %rax
movb %bpl, 0x1a(%rax)
movq $0x0, 0x18(%rbx)
movzbl 0x4(%rsp), %ebp
leaq 0x183b6(%rip), %r12     # 0x1c6d0
movl (%r12), %eax
subl %ebp, %eax
cmpl %eax, %r15d
jae 0x435f
negl %ebp
leaq 0x10(%rsp), %rbx
leal 0x1(%r15), %r14d
movq %rbx, %rdi
movl %r15d, %esi
callq 0x5b93
movq %rbx, %rdi
callq 0x2110
movq (%r13), %rsi
movq %rbx, %rdi
callq 0x5b3b
movl (%r12), %eax
addl %ebp, %eax
movl %r14d, %r15d
cmpl %eax, %r14d
jb 0x432c
jmp 0x4362
movl %r15d, %r14d
cmpb $0x0, 0x4(%rsp)
je 0x4423
movq 0x8(%rsp), %r15
movq (%r15), %rax
leaq 0x1b(%rax), %rcx
movb 0x1b(%rax), %r8b
movq %rcx, %rsi
cmpb $-0x1, %r8b
jne 0x43c0
movl $0xff, %edx
addq 0x10(%r15), %rdx
movq $-0xff, %rdi
addq 0x18(%r15), %rdi
movq %rcx, %rsi
movq %rdi, 0x18(%r15)
movq %rdx, 0x10(%r15)
movb 0x1(%rsi), %r8b
incq %rsi
addq $0xff, %rdx
addq $-0xff, %rdi
cmpb $-0x1, %r8b
je 0x439d
movzbl %r8b, %edx
subq %rdx, 0x18(%r15)
movzbl (%rsi), %edx
addq %rdx, 0x10(%r15)
incq %rsi
subq %rsi, %rcx
addq %rcx, 0x8(%r15)
movb $0x0, 0x5(%rax)
movq (%r15), %rax
movl %eax, %ecx
subl %esi, %ecx
addb 0x1a(%rax), %cl
addb $0x1b, %cl
movb %cl, 0x1a(%rax)
movq (%r15), %rdi
movzbl 0x1a(%rdi), %edx
addq $0x1b, %rdi
callq 0x23b0
leal 0x1(%r14), %ebx
movq %r15, %rdi
movl %r14d, %esi
callq 0x5b93
movq %r15, %rdi
callq 0x2110
movq (%r13), %rsi
movq %r15, %rdi
callq 0x5b3b
movl %ebx, %r14d
callq 0x4eaf
leaq 0x182a1(%rip), %rax     # 0x1c6d0
leaq 0x18292(%rip), %rcx     # 0x1c6c8
cmpb $0x1, (%rcx)
jne 0x4440
cmpl (%rax), %r14d
je 0x446c
subl (%rax), %r14d
leaq 0x1828e(%rip), %rax     # 0x1c6d8
movl %r14d, (%rax)
leaq 0x18278(%rip), %rax     # 0x1c6cc
movl $0x4, (%rax)
addq $0x10038, %rsp          # imm = 0x10038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edi, %edi
callq 0x4473