// File: /workspace/github_c_makefile_star2_O2/hcmiya[P]opuscomment/src/retrieve-tags.c
// Function: retrieve_tags
void *retrieve_tags(void *packet_input_) {
	// parse_header_border() からスレッド化された
	FILE *packet_input = packet_input_;
	uint8_t buf[STACK_BUF_LEN];
	
	struct rettag_st *rtn = calloc(1, sizeof(*rtn));
	
	FILE *fptag = rtn->tag = tmpfile();
	rtn->part_of_comment = true;
	uint32_t len;
	rtread(buf, codec->commagic_len, packet_input);
	if (memcmp(buf, codec->commagic, codec->commagic_len) != 0) {
		if (codec->type == CODEC_VP8 && buf[5] != 2) {
			len = codec->commagic_len;
			rtn->part_of_comment = false;
		}
		else opuserror(err_opus_bad_content);
	}
	fwrite(buf, 1, codec->commagic_len, fptag);
	if (!rtn->part_of_comment) {
		// コメントパケットが無かった時用
		if (O.edit == EDIT_LIST) exit(0);
		size_t vendorlen = strlen(new_vendor_string_ascii);
		fwrite((uint32_t[]){oi32(vendorlen)}, 4, 1, fptag);
		fwrite(new_vendor_string_ascii, 1, vendorlen, fptag);
		rtn->tagbegin = codec->commagic_len + vendorlen + 4;
		goto NOTCOMMENT;
	}
	
	// ベンダ文字列
	len = rtchunk(packet_input);
	*(uint32_t*)buf = oi32(len);
	fwrite(buf, 4, 1, fptag);
	check_tagpacket_length(codec->commagic_len + 4);
	while (len) {
		size_t rl = rtfill(buf, len, STACK_BUF_LEN, packet_input);
		fwrite(buf, 1, rl, fptag);
		check_tagpacket_length(rl);
		len -= rl;
	}
	rtn->tagbegin = tagpacket_total;
	
	// レコード数
	size_t recordnum = rtn->del = rtchunk(packet_input);
	fwrite(buf, 4, 1, fptag); // レコード数埋め(後でstore_tags()で書き換え)
	check_tagpacket_length(4);
	bool (*rtcopy)(FILE*, void*);
	int pfd[2];
	pthread_t putth;
	void *wh;
	if (O.edit == EDIT_LIST) {
		// タグ出力をスレッド化 put-tags.c へ
		pipe(pfd);
		FILE *fpput = fdopen(pfd[0], "r");
		pthread_create(&putth, NULL, put_tags, fpput);
		rtcopy = rtcopy_list;
		wh = pfd + 1;
	}
	else if (dellist_str) {
		rtcopy = rtcopy_delete;
		wh = fptag;
		rtcd_src = tmpfile();
	}
	else {
		rtcopy = rtcopy_write;
		wh = fptag;
	}
	
	while (recordnum) {
		rtn->num += rtcopy(packet_input, wh);
		recordnum--;
	}
	rtn->del -= rtn->num;
	
	if (O.edit == EDIT_LIST) {
		// タグ出力スレッド合流
		close(pfd[1]);
		pthread_join(putth, NULL);
		if (codec->type == CODEC_FLAC) return NULL;
		tag_output_close();
		exit(0);
	}
	
	len = fread(buf, 1, 1, packet_input);
	if (len && (codec->prog || (*buf & 1))) {
		// codec->prog ← opuscomment 以外は全部パディングを保存
	NOTCOMMENT: // ←VP8用
		rtn->padding = tmpfile();
		fwrite(buf, 1, len, rtn->padding);
		check_tagpacket_length(len);
		size_t n;
		while ((n = fread(buf, 1, STACK_BUF_LEN, packet_input))) {
			fwrite(buf, 1, n, rtn->padding);
			check_tagpacket_length(n);
		}
	}
	else {
		size_t n;
		while ((n = fread(buf, 1, STACK_BUF_LEN, packet_input))) {}
	}
	
	if (rtcd_src) {
		fclose(rtcd_src);
	}
	if (dellist_str) {
		fclose(dellist_str); fclose(dellist_len);
	}
	fclose(packet_input);
	rtn->upcase = upcase_applied;
	return rtn;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1028, %rsp           # imm = 0x1028
movq %rdi, %r14
pushq $0x1
popq %rdi
pushq $0x30
popq %rsi
callq 0x2220
movq %rax, %rbx
callq 0x2230
movq %rax, %r15
movq %rax, (%rbx)
movq %rbx, (%rsp)
movb $0x1, 0x29(%rbx)
leaq 0x17018(%rip), %rbx     # 0x1c670
movq (%rbx), %rax
movq 0x30(%rax), %rsi
leaq 0x20(%rsp), %r12
movq %r12, %rdi
movq %r14, %rdx
callq 0x59f5
movq (%rbx), %rbp
movq 0x30(%rbp), %r13
movq 0x38(%rbp), %rsi
movq %r12, %rdi
movq %r13, %rdx
callq 0x2300
movb $0x1, %bl
testl %eax, %eax
je 0x56a7
cmpl $0x3, (%rbp)
jne 0x56ac
cmpb $0x2, 0x25(%rsp)
je 0x56ac
movq (%rsp), %rax
movb $0x0, 0x29(%rax)
movl %r13d, %r12d
xorl %ebx, %ebx
jmp 0x56c7
xorl %r12d, %r12d
jmp 0x56c7
xorl %r12d, %r12d
pushq $0xc
popq %rdi
xorl %eax, %eax
callq 0x6639
leaq 0x16fb0(%rip), %rax     # 0x1c670
movq (%rax), %rax
movq 0x30(%rax), %r13
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x2460
testb %bl, %bl
je 0x5768
movq %r14, %rdi
callq 0x5a19
movl %eax, %ebp
movl %eax, %edi
callq 0x651b
leaq 0x20(%rsp), %r12
movl %eax, (%r12)
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r12, %rdi
movq %r15, %rcx
callq 0x2460
leaq 0x16f5c(%rip), %rax     # 0x1c670
movq (%rax), %rax
movq 0x30(%rax), %rdi
addq $0x4, %rdi
callq 0x5600
movl $0x1000, %r13d          # imm = 0x1000
testl %ebp, %ebp
je 0x586e
cmpl %r13d, %ebp
movl $0x1000, %ebx           # imm = 0x1000
cmovbl %ebp, %ebx
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x59f5
movq %r12, %rdi
pushq $0x1
popq %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x2460
movq %rbx, %rdi
callq 0x5600
subl %ebx, %ebp
jmp 0x572a
leaq 0x16f19(%rip), %rax     # 0x1c688
cmpl $0x1, (%rax)
je 0x59ee
leaq 0x6629(%rip), %rax      # 0xbda8
movq (%rax), %r13
movq %r13, %rdi
callq 0x2130
movq %rax, %rbp
movl %ebp, %edi
callq 0x651b
leaq 0x1c(%rsp), %rdi
movl %eax, (%rdi)
pushq $0x4
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
movq %r15, %rcx
callq 0x2460
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x2460
leaq 0x16eac(%rip), %rax     # 0x1c670
movq (%rax), %rax
movq 0x30(%rax), %rax
addq %rbp, %rax
addq $0x4, %rax
movq (%rsp), %rcx
movq %rax, 0x10(%rcx)
callq 0x2230
movq %rax, %r15
movq (%rsp), %rax
movq %r15, 0x8(%rax)
leaq 0x20(%rsp), %r13
pushq $0x1
popq %rbp
movq %r13, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x2460
movq %r12, %rdi
callq 0x5600
movl $0x1000, %edx           # imm = 0x1000
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rcx
callq 0x20e0
movq %rax, %r12
testq %rax, %rax
jne 0x57f2
movq 0x16e1b(%rip), %rdi     # 0x1c648
testq %rdi, %rdi
je 0x5837
callq 0x2120
movq 0x16e02(%rip), %rdi     # 0x1c640
testq %rdi, %rdi
movq (%rsp), %rbx
je 0x5858
callq 0x2120
movq 0x16dfd(%rip), %rdi     # 0x1c650
callq 0x2120
movq %r14, %rdi
callq 0x2120
movb 0x16df2(%rip), %al      # 0x1c658
movb %al, 0x28(%rbx)
jmp 0x5977
movq 0x16dc3(%rip), %rax     # 0x1c638
movq (%rsp), %rcx
movq %rax, 0x10(%rcx)
movq %r14, %rdi
callq 0x5a19
movl %eax, %r13d
leaq 0x20(%rsp), %rdi
pushq $0x4
popq %rbx
pushq $0x1
popq %rdx
movq %rbx, %rsi
movq %r15, %rcx
callq 0x2460
movq %rbx, %rdi
callq 0x5600
leaq 0x16ddb(%rip), %rax     # 0x1c688
cmpl $0x1, (%rax)
jne 0x58f1
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x21e0
movl (%rbx), %edi
leaq 0x27b5(%rip), %rsi      # 0x807d
callq 0x2340
leaq -0x3221(%rip), %rdx     # 0x26b3
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
movq %rax, %rcx
callq 0x2390
leaq 0x153(%rip), %rbx       # 0x5a3d
leaq 0xc(%rsp), %r15
jmp 0x5917
cmpq $0x0, 0x16d47(%rip)     # 0x1c640
je 0x5910
callq 0x2230
movq %rax, 0x16d41(%rip)     # 0x1c648
leaq 0x2a0(%rip), %rbx       # 0x5bae
jmp 0x5917
leaq 0x535(%rip), %rbx       # 0x5e4c
xorl %ebp, %ebp
movq %r13, %r12
subq $0x1, %r12
jb 0x593a
movq %r14, %rdi
movq %r15, %rsi
callq *%rbx
movzbl %al, %eax
addq %rax, %rbp
movq (%rsp), %rax
movq %rbp, 0x18(%rax)
jmp 0x591c
subq %rbp, %r13
movq (%rsp), %rax
movq %r13, 0x20(%rax)
leaq 0x16d3c(%rip), %rax     # 0x1c688
cmpl $0x1, (%rax)
jne 0x598c
movl 0xc(%rsp), %edi
callq 0x21c0
movq 0x10(%rsp), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x2490
leaq 0x16d01(%rip), %rax     # 0x1c670
movq (%rax), %rax
cmpl $0x2, (%rax)
jne 0x59e9
movq %rbx, %rax
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %rdx
movq %rdx, %rsi
movq %r14, %rcx
callq 0x20e0
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
andq %rax, %r12
je 0x59ca
leaq 0x16cbf(%rip), %rax     # 0x1c670
movq (%rax), %rax
cmpq $0x0, 0x8(%rax)
jne 0x57da
testb $0x1, 0x20(%rsp)
jne 0x57da
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rcx
callq 0x20e0
testq %rax, %rax
jne 0x59ca
jmp 0x5826
callq 0x25dc
xorl %edi, %edi
callq 0x2450