// File: /workspace/github_c_makefile_star2_O2/hcmiya[P]opuscomment/src/put-tags.c
// Function: put_tags
void *put_tags(void *fp_) {
	// retrieve_tag() からスレッド化された
	// fpはチャンク化されたタグ
	// [4バイト: タグ長(ホストエンディアン)][任意長: UTF-8タグ]
	FILE *fp = fp_;
	
	iconv_t cd;
	
	if (!O.tag_raw) {
		cd = iconv_new(nl_langinfo(CODESET), "UTF-8");
	}
	bool nulsep = O.tag_escape == TAG_ESCAPE_NUL;
	size_t const buflenunit = STACK_BUF_LEN / (3 - nulsep);
	uint8_t buf[STACK_BUF_LEN];
	uint8_t *raw = buf + buflenunit * (2 - nulsep);
	uint8_t *(*esc)(uint8_t*, uint8_t*, size_t) = esc_func[O.tag_escape];
	size_t (*conv)(iconv_t cd, uint8_t*, size_t) = O.tag_raw ? conv_none : conv_ls;
	
	for (;;) {
		uint32_t len;
		if (!fread(&len, 4, 1, fp)) break;
		if (nulsep && nth > 1) put_bin("", 1);
		size_t left = len, remain = 0;
		while (left) {
			size_t readlen = fill_buffer(raw, left, buflenunit - remain, fp);
			left -= readlen;
			
			size_t tagleft = esc(raw, buf + remain, readlen) - buf;
			remain = conv(cd, buf, tagleft);
		}
		if (remain) {
			u8error();
		}
		if (O.tag_raw) {
			if (!nulsep) {
				put_bin("\x0a", 1);
			}
		}
		else {
			uint8_t *u8;
			char *ls;
			if (nulsep) {
				*buf = '\0';
				left = 1, remain = 128;
			}
			else {
				memcpy(buf, "\x0a", 2);
				left = 2, remain = 128;
			}
			u8 = buf, ls = buf + left;
			if (iconv(cd, (char**)&u8, &left, &ls, &remain) == (size_t)-1) {
				oserror();
			}
			// u8はここでiconv()前のlsを指していることに注意
			put_ls(u8);
		}
		nth++;
	}
	fclose(fp);
	
	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1048, %rsp           # imm = 0x1048
movq %rdi, 0x18(%rsp)
leaq 0x19fb8(%rip), %rbx     # 0x1c688
cmpb $0x0, 0x15(%rbx)
je 0x26df
leaq 0x25a(%rip), %rbp       # 0x2937
jmp 0x270f
pushq $0xe
popq %rdi
callq 0x22f0
leaq 0x5916(%rip), %rsi      # 0x8004
movq %rax, %rdi
callq 0x289e
movq %rax, %r14
leaq 0x237(%rip), %rax       # 0x2937
leaq 0x240(%rip), %rbp       # 0x2947
testb $0x1, 0x15(%rbx)
cmovneq %rax, %rbp
movl 0x10(%rbx), %ecx
movl $0x800, %eax            # imm = 0x800
leaq 0x40(%rsp), %r15
cmpq $0x2, %rcx
movl $0x555, %r13d           # imm = 0x555
cmoveq %rax, %r13
leaq (%r15,%rax), %rax
leaq 0xaea(%rsp), %r12
cmoveq %rax, %r12
leaq 0x94bf(%rip), %rax      # 0xbc00
movq %rcx, 0x10(%rsp)
movq (%rax,%rcx,8), %rbx
leaq 0x2c(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq 0x18(%rsp), %rcx
callq 0x20e0
testq %rax, %rax
je 0x2880
cmpl $0x2, 0x10(%rsp)
jne 0x2787
cmpl $0x2, 0x9b0a(%rip)      # 0xc280
jl 0x2787
leaq 0x5f37(%rip), %rdi      # 0x86b6
pushq $0x1
popq %rsi
callq 0x2670
movl 0x2c(%rsp), %esi
movq %rsi, 0x8(%rsp)
xorl %eax, %eax
movq %rax, 0x20(%rsp)
testq %rsi, %rsi
je 0x27d9
movq %r13, %rdx
subq %rax, %rdx
movq %r12, %rdi
movq 0x18(%rsp), %rcx
callq 0x5459
subq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rsi
addq %r15, %rsi
movq %r12, %rdi
movq %rax, %rdx
callq *%rbx
subq %r15, %rax
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq *%rbp
movq 0x8(%rsp), %rsi
jmp 0x2792
testq %rax, %rax
je 0x27e3
callq 0x2a0e
leaq 0x19e9e(%rip), %rax     # 0x1c688
cmpb $0x1, 0x15(%rax)
jne 0x2808
cmpl $0x2, 0x10(%rsp)
je 0x2875
leaq 0x5eb7(%rip), %rdi      # 0x86b5
pushq $0x1
popq %rsi
callq 0x2670
jmp 0x2875
cmpl $0x2, 0x10(%rsp)
jne 0x281d
movb $0x0, 0x40(%rsp)
leaq 0x41(%rsp), %rax
pushq $0x1
jmp 0x282b
movw $0xa, 0x40(%rsp)
leaq 0x42(%rsp), %rax
pushq $0x2
popq %rcx
movq %rcx, 0x8(%rsp)
movq $0x80, 0x20(%rsp)
movq %r15, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x38(%rsp), %rcx
leaq 0x20(%rsp), %r8
callq 0x20b0
cmpq $-0x1, %rax
jne 0x286b
callq 0x67b6
movq 0x30(%rsp), %rdi
callq 0x2a1e
incl 0x9a05(%rip)            # 0xc280
jmp 0x274a
movq 0x18(%rsp), %rdi
callq 0x2120
xorl %eax, %eax
addq $0x1048, %rsp           # imm = 0x1048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq