// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1080, %rsp           # imm = 0x1080
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
leaq 0x1af5a(%rip), %rax     # 0x1d6c8
testb $0x1, 0x15(%rax)
jne 0x2791
movl $0xe, %edi
callq 0x2320
movq %rax, %rdi
leaq 0x787c(%rip), %rsi      # 0xa004
callq 0x2a50
movq %rax, -0x18(%rbp)
leaq 0x1af30(%rip), %rax     # 0x1d6c8
cmpl $0x2, 0x10(%rax)
sete %al
andb $0x1, %al
movb %al, -0x19(%rbp)
movb -0x19(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl $0x3, %ecx
subl %eax, %ecx
movl $0x1000, %eax           # imm = 0x1000
cltd
idivl %ecx
cltq
movq %rax, -0x28(%rbp)
leaq -0x1030(%rbp), %rax
movq -0x28(%rbp), %rcx
movb -0x19(%rbp), %dl
andb $0x1, %dl
movzbl %dl, %esi
movl $0x2, %edx
subl %esi, %edx
movslq %edx, %rdx
imulq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x1038(%rbp)
leaq 0x1aed4(%rip), %rax     # 0x1d6c8
movl 0x10(%rax), %eax
movl %eax, %ecx
leaq 0xaaa0(%rip), %rax      # 0xd2a0
movq (%rax,%rcx,8), %rax
movq %rax, -0x1040(%rbp)
leaq 0x1aeb6(%rip), %rax     # 0x1d6c8
movb 0x15(%rax), %dl
leaq 0x304(%rip), %rax       # 0x2b20
leaq 0x2cd(%rip), %rcx       # 0x2af0
testb $0x1, %dl
cmovneq %rcx, %rax
movq %rax, -0x1048(%rbp)
movq -0x10(%rbp), %rcx
leaq -0x104c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x20e0
cmpq $0x0, %rax
jne 0x2856
jmp 0x2a2e
testb $0x1, -0x19(%rbp)
je 0x2876
cmpl $0x1, 0xaa55(%rip)      # 0xd2b8
jle 0x2876
leaq 0x7ec6(%rip), %rdi      # 0xa732
movl $0x1, %esi
callq 0x26d0
movl -0x104c(%rbp), %eax
movq %rax, -0x1058(%rbp)
movq $0x0, -0x1060(%rbp)
cmpq $0x0, -0x1058(%rbp)
je 0x293a
movq -0x1038(%rbp), %rdi
movq -0x1058(%rbp), %rsi
movq -0x28(%rbp), %rdx
subq -0x1060(%rbp), %rdx
movq -0x10(%rbp), %rcx
callq 0x6a60
movq %rax, -0x1068(%rbp)
movq -0x1068(%rbp), %rcx
movq -0x1058(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x1058(%rbp)
movq -0x1040(%rbp), %rax
movq -0x1038(%rbp), %rdi
leaq -0x1030(%rbp), %rsi
addq -0x1060(%rbp), %rsi
movq -0x1068(%rbp), %rdx
callq *%rax
leaq -0x1030(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x1070(%rbp)
movq -0x1048(%rbp), %rax
movq -0x18(%rbp), %rdi
leaq -0x1030(%rbp), %rsi
movq -0x1070(%rbp), %rdx
callq *%rax
movq %rax, -0x1060(%rbp)
jmp 0x288e
cmpq $0x0, -0x1060(%rbp)
je 0x2949
callq 0x2c10
leaq 0x1ad78(%rip), %rax     # 0x1d6c8
testb $0x1, 0x15(%rax)
je 0x2972
testb $0x1, -0x19(%rbp)
jne 0x296d
leaq 0x7dce(%rip), %rdi      # 0xa731
movl $0x1, %esi
callq 0x26d0
jmp 0x2a1a
testb $0x1, -0x19(%rbp)
je 0x2997
movb $0x0, -0x1030(%rbp)
movq $0x1, -0x1058(%rbp)
movq $0x80, -0x1060(%rbp)
jmp 0x29bb
movw 0x7d93(%rip), %ax       # 0xa731
movw %ax, -0x1030(%rbp)
movq $0x2, -0x1058(%rbp)
movq $0x80, -0x1060(%rbp)
leaq -0x1030(%rbp), %rax
movq %rax, -0x1078(%rbp)
leaq -0x1030(%rbp), %rax
addq -0x1058(%rbp), %rax
movq %rax, -0x1080(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x1078(%rbp), %rsi
leaq -0x1058(%rbp), %rdx
leaq -0x1080(%rbp), %rcx
leaq -0x1060(%rbp), %r8
callq 0x20b0
cmpq $-0x1, %rax
jne 0x2a0e
callq 0x8880
movq -0x1078(%rbp), %rdi
callq 0x2c30
movl 0xa898(%rip), %eax      # 0xd2b8
addl $0x1, %eax
movl %eax, 0xa88f(%rip)      # 0xd2b8
jmp 0x2831
movq -0x10(%rbp), %rdi
callq 0x2120
xorl %eax, %eax
addq $0x1080, %rsp           # imm = 0x1080
popq %rbp
retq
nopw %cs:(%rax,%rax)