// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x10090, %rsp          # imm = 0x10090
movb %cl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq $0x10000, -0x30(%rbp)   # imm = 0x10000
movq %rsp, %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
xorl %edx, %edx
callq 0x2340
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
addq 0x18(%rcx), %rax
movl %eax, %edi
callq 0x84f0
movl %eax, -0x10090(%rbp)
movq -0x28(%rbp), %rcx
leaq -0x10090(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2490
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x2340
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x2160
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2160
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
leaq -0x10090(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x20e0
cmpq $0x0, %rax
je 0x49a0
movl -0x10090(%rbp), %eax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl %eax, %edi
callq 0x84f0
movl %eax, -0x10090(%rbp)
movq -0x28(%rbp), %rcx
leaq -0x10090(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x2490
cmpq $0x0, -0x40(%rbp)
je 0x499b
movq -0x40(%rbp), %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rcx
leaq -0x10090(%rbp), %rdi
movl $0x10000, %edx          # imm = 0x10000
callq 0x6a60
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdx
movq -0x28(%rbp), %rcx
leaq -0x10090(%rbp), %rdi
movl $0x1, %esi
callq 0x2490
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x40(%rbp)
jmp 0x494a
jmp 0x48ed
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x2120
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2120
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x4a37
movq -0x10(%rbp), %rax
testb $0x1, 0x29(%rax)
je 0x4a37
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2160
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
leaq -0x10090(%rbp), %rdi
movl $0x1, %esi
movl $0x10000, %edx          # imm = 0x10000
callq 0x20e0
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0x4a1e
movq -0x50(%rbp), %rdx
movq -0x28(%rbp), %rcx
leaq -0x10090(%rbp), %rdi
movl $0x1, %esi
callq 0x2490
jmp 0x49db
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2120
movq -0x10(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x28(%rbp), %rdi
callq 0x22b0
movq %rax, -0x58(%rbp)
cmpq $0x2800000, -0x58(%rbp) # imm = 0x2800000
jle 0x4a53
callq 0x8ad0
leaq 0x18c56(%rip), %rax     # 0x1d6b0
movq (%rax), %rax
cmpl $0x2, (%rax)
jne 0x4a6e
movl $0x1, -0x5c(%rbp)
jmp 0x4e9a
movq -0x28(%rbp), %rdi
callq 0x2160
movq $0x11a, -0x78(%rbp)     # imm = 0x11A
leaq -0x10090(%rbp), %rax
movq %rax, -0x80(%rbp)
movq $0xfe01, -0x68(%rbp)    # imm = 0xFE01
leaq -0x10090(%rbp), %rax
addq $0x11a, %rax            # imm = 0x11A
movq %rax, -0x70(%rbp)
movq -0x80(%rbp), %rax
movl 0x561c(%rip), %ecx      # 0xa0c9
movl %ecx, (%rax)
movw 0x5617(%rip), %cx       # 0xa0cd
movw %cx, 0x4(%rax)
leaq -0x80(%rbp), %rdi
movq $-0x1, %rsi
callq 0x6cc0
leaq 0x18c43(%rip), %rax     # 0x1d714
movl (%rax), %edi
callq 0x84f0
movl %eax, %ecx
movq -0x80(%rbp), %rax
movl %ecx, 0xe(%rax)
movq -0x80(%rbp), %rdi
addq $0x1a, %rdi
movl $0xff, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x21b0
movl $0x1, -0x84(%rbp)
cmpq $0xfe01, -0x58(%rbp)    # imm = 0xFE01
jl 0x4b7b
movq -0x70(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x1, %esi
movl $0xfe01, %edx           # imm = 0xFE01
callq 0x20e0
movl -0x84(%rbp), %esi
movl %esi, %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x6c90
leaq -0x80(%rbp), %rdi
callq 0x2110
leaq 0x18bd3(%rip), %rax     # 0x1d720
movq (%rax), %rsi
leaq -0x80(%rbp), %rdi
callq 0x6c10
movq -0x58(%rbp), %rax
subq $0xfe01, %rax           # imm = 0xFE01
movq %rax, -0x58(%rbp)
movq -0x80(%rbp), %rax
cmpb $0x0, 0x5(%rax)
jne 0x4b79
movq -0x80(%rbp), %rax
movb $0x1, 0x5(%rax)
jmp 0x4b02
cmpl $0x1, -0x84(%rbp)
setne %al
andb $0x1, %al
movzbl %al, %eax
movb %al, %cl
movq -0x80(%rbp), %rax
movb %cl, 0x5(%rax)
leaq -0x80(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6cc0
movl -0x84(%rbp), %esi
movl %esi, %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x6c90
movq -0x58(%rbp), %rax
movl $0xff, %ecx
cqto
idivq %rcx
addq $0x1, %rax
movb %al, %cl
movq -0x80(%rbp), %rax
movb %cl, 0x1a(%rax)
movq -0x58(%rbp), %rax
movl $0xff, %ecx
cqto
idivq %rcx
movq -0x80(%rbp), %rax
movq -0x80(%rbp), %rcx
movzbl 0x1a(%rcx), %ecx
addl $0x1a, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x70(%rbp), %rdi
movq -0x58(%rbp), %rdx
movq -0x28(%rbp), %rcx
movl $0x1, %esi
callq 0x20e0
movq -0x28(%rbp), %rdi
callq 0x2120
movq -0x80(%rbp), %rax
movzbl 0x1a(%rax), %eax
addl $0x1b, %eax
cltq
movq %rax, -0x78(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x2110
leaq 0x18ae0(%rip), %rax     # 0x1d720
movq (%rax), %rsi
leaq -0x80(%rbp), %rdi
callq 0x6c10
leaq 0x18ab5(%rip), %rax     # 0x1d708
testb $0x1, (%rax)
je 0x4ce4
movq $0x1b, -0x78(%rbp)
movq -0x80(%rbp), %rax
movb $0x0, 0x5(%rax)
leaq -0x80(%rbp), %rdi
movq $-0x1, %rsi
callq 0x6cc0
movq -0x80(%rbp), %rax
movb $0x0, 0x1a(%rax)
movq $0x0, -0x68(%rbp)
movl -0x84(%rbp), %eax
leaq 0x18a77(%rip), %rcx     # 0x1d710
movl (%rcx), %ecx
movb -0x19(%rbp), %dl
andb $0x1, %dl
movzbl %dl, %edx
subl %edx, %ecx
cmpl %ecx, %eax
jae 0x4ce2
movl -0x84(%rbp), %esi
movl %esi, %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x6c90
leaq -0x80(%rbp), %rdi
callq 0x2110
leaq 0x18a4c(%rip), %rax     # 0x1d720
movq (%rax), %rsi
leaq -0x80(%rbp), %rdi
callq 0x6c10
jmp 0x4c8c
jmp 0x4ce4
testb $0x1, -0x19(%rbp)
je 0x4e43
movq -0x8(%rbp), %rax
movq (%rax), %rax
addq $0x1b, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xff, %eax
jne 0x4d4b
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rcx
subq $0xff, %rcx
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0xff, %rcx
movq %rcx, 0x10(%rax)
jmp 0x4d00
movq -0x90(%rbp), %rax
movzbl (%rax), %eax
movl %eax, %edx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x18(%rax)
movq -0x90(%rbp), %rax
movzbl (%rax), %edx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0x10(%rax)
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdx
movq -0x8(%rbp), %rax
movq (%rax), %rax
addq $0x1b, %rax
subq %rax, %rdx
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movb $0x0, 0x5(%rax)
movq -0x90(%rbp), %rdx
movq -0x8(%rbp), %rax
movq (%rax), %rax
addq $0x1b, %rax
subq %rax, %rdx
movq -0x8(%rbp), %rax
movq (%rax), %rax
movzbl 0x1a(%rax), %ecx
subq %rdx, %rcx
movb %cl, 0x1a(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
addq $0x1b, %rdi
movq -0x90(%rbp), %rsi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movzbl 0x1a(%rax), %eax
movl %eax, %edx
callq 0x23d0
movq -0x8(%rbp), %rdi
movl -0x84(%rbp), %esi
movl %esi, %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
callq 0x6c90
movq -0x8(%rbp), %rdi
callq 0x2110
movq -0x8(%rbp), %rdi
leaq 0x188e5(%rip), %rax     # 0x1d720
movq (%rax), %rsi
callq 0x6c10
callq 0x5cc0
leaq 0x188b9(%rip), %rax     # 0x1d708
testb $0x1, (%rax)
je 0x4e6e
movl -0x84(%rbp), %eax
leaq 0x188af(%rip), %rcx     # 0x1d710
cmpl (%rcx), %eax
jne 0x4e6e
xorl %eax, %eax
movl %eax, %edi
callq 0x4eb0
movl -0x84(%rbp), %ecx
leaq 0x18895(%rip), %rax     # 0x1d710
subl (%rax), %ecx
leaq 0x18894(%rip), %rax     # 0x1d718
movl %ecx, (%rax)
leaq 0x1887f(%rip), %rax     # 0x1d70c
movl $0x4, (%rax)
movl $0x0, -0x5c(%rbp)
movq -0x38(%rbp), %rax
movq %rax, %rsp
addq $0x10090, %rsp          # imm = 0x10090
popq %rbp
retq
nopw (%rax,%rax)