// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/src/fmtutil.c
// Function: wrap_in_tiff
static void wrap_in_tiff(deark *c, dbuf *f, i64 dpos, i64 dlen,
	const char *swstring, unsigned int tag, const char *ext, unsigned int createflags)
{
	dbuf *outf = NULL;
	i64 ifdoffs;
	i64 sw_len, sw_len_padded;
	i64 data_len_padded;

	sw_len = 1+(i64)de_strlen(swstring);
	if(sw_len<=4) return;
	sw_len_padded = de_pad_to_2(sw_len);

	if(dlen>4) {
		data_len_padded = de_pad_to_2(dlen);
	}
	else {
		data_len_padded = 0;
	}

	outf = dbuf_create_output_file(c, ext, NULL, 0);
	dbuf_write(outf, (const u8*)"\x4d\x4d\x00\x2a", 4);
	ifdoffs = 8 + sw_len_padded + data_len_padded;
	dbuf_writeu32be(outf, ifdoffs);
	dbuf_write(outf, (const u8*)swstring, sw_len);
	if(sw_len%2) dbuf_writebyte(outf, 0);
	if(dlen>4) {
		dbuf_copy(f, dpos, dlen, outf);
		if(dlen%2) dbuf_writebyte(outf, 0);
	}

	dbuf_writeu16be(outf, 2); // number of dir entries;

	dbuf_writeu16be(outf, 305); // Software tag
	dbuf_writeu16be(outf, 2); // type=ASCII
	dbuf_writeu32be(outf, sw_len);
	dbuf_writeu32be(outf, 8); // offset

	dbuf_writeu16be(outf, (i64)tag);
	dbuf_writeu16be(outf, 1);
	dbuf_writeu32be(outf, dlen);
	if(dlen>4) {
		dbuf_writeu32be(outf, 8+sw_len_padded);
	}
	else {
		dbuf_copy(f, dpos, dlen, outf);
		dbuf_write_zeroes(outf, 4-dlen);
	}

	dbuf_writeu32be(outf, 0); // end of IFD
	dbuf_close(outf);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, %r12d
movq %r8, %r15
movq %rcx, %rbx
movq %rdx, 0x10(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, %r14
movq 0x60(%rsp), %r13
movq %r8, %rdi
callq 0x180e0
cmpq $0x4, %rax
jge 0xf7b8b
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbp
movl %r12d, 0x4(%rsp)
movq %rax, %r12
incq %r12
movq %r12, %rdi
callq 0xe9c7e
movq %rax, 0x18(%rsp)
cmpq $0x5, %rbx
movq %rbx, 0x20(%rsp)
jl 0xf7bbe
movq %rbx, %rdi
callq 0xe9c7e
movq %rax, %rbx
jmp 0xf7bc0
xorl %ebx, %ebx
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xefda1
movq %rax, %r14
leaq 0x526d9(%rip), %rsi     # 0x14a2b2
movl $0x4, %edx
movq %rax, %rdi
callq 0xef547
movq 0x18(%rsp), %r13
leaq (%rbx,%r13), %rsi
addq $0x8, %rsi
movq %r14, %rdi
callq 0xf1026
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xef547
testb $0x1, %bpl
jne 0xf7c19
movq %r14, %rdi
xorl %esi, %esi
callq 0xf0bd6
movq 0x20(%rsp), %rbx
cmpq $0x5, %rbx
jl 0xf7cc3
addq $0x8, %r13
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0xef481
testb $0x1, %bl
je 0xf7c50
movq %r14, %rdi
xorl %esi, %esi
callq 0xf0bd6
movl $0x2, %esi
movq %r14, %rdi
callq 0xf0fad
movl $0x131, %esi            # imm = 0x131
movq %r14, %rdi
callq 0xf0fad
movl $0x2, %esi
movq %r14, %rdi
callq 0xf0fad
movq %r14, %rdi
movq %r12, %rsi
callq 0xf1026
movl $0x8, %esi
movq %r14, %rdi
callq 0xf1026
movl 0x4(%rsp), %esi
movq %r14, %rdi
callq 0xf0fad
movl $0x1, %esi
movq %r14, %rdi
callq 0xf0fad
movq %r14, %rdi
movq %rbx, %rsi
callq 0xf1026
movq %r14, %rdi
movq %r13, %rsi
callq 0xf1026
jmp 0xf7d4b
movl $0x2, %esi
movq %r14, %rdi
callq 0xf0fad
movl $0x131, %esi            # imm = 0x131
movq %r14, %rdi
callq 0xf0fad
movl $0x2, %esi
movq %r14, %rdi
callq 0xf0fad
movq %r14, %rdi
movq %r12, %rsi
callq 0xf1026
movl $0x8, %esi
movq %r14, %rdi
callq 0xf1026
movl 0x4(%rsp), %esi
movq %r14, %rdi
callq 0xf0fad
movl $0x1, %esi
movq %r14, %rdi
callq 0xf0fad
movq %r14, %rdi
movq %rbx, %rsi
callq 0xf1026
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0xef481
movl $0x4, %esi
subq %rbx, %rsi
movq %r14, %rdi
callq 0xf0d35
movq %r14, %rdi
xorl %esi, %esi
callq 0xf1026
movq %r14, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf052b