// File: /workspace/github_c_makefile_star2_O2/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, %r13d
movq %r8, %r12
movq %rcx, %rbx
movq %rdx, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %r14
movq 0x60(%rsp), %r15
movq %r8, %rdi
callq 0x180e0
cmpq $0x4, %rax
jge 0xec2d2
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbp
movl %r13d, 0x4(%rsp)
movq %rax, %r13
incq %r13
movq %r13, %rdi
callq 0xdfb87
movq %rax, 0x20(%rsp)
cmpq $0x5, %rbx
movq %rbx, 0x8(%rsp)
jl 0xec305
movq %rbx, %rdi
callq 0xdfb87
movq %rax, %rbx
jmp 0xec307
xorl %ebx, %ebx
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xe4e62
movq %rax, %r14
leaq 0x50ac2(%rip), %rsi     # 0x13cde2
pushq $0x4
popq %r15
movq %rax, %rdi
movq %r15, %rdx
callq 0xe46ba
movq 0x20(%rsp), %rax
leaq (%rbx,%rax), %rsi
addq $0x8, %rsi
movq %rax, %rbx
movq %r14, %rdi
callq 0xe5f65
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0xe46ba
testb $0x1, %bpl
jne 0xec365
movq %r14, %rdi
xorl %esi, %esi
callq 0xe5bc5
movq 0x8(%rsp), %r12
cmpq $0x5, %r12
jl 0xec410
addq $0x8, %rbx
movq %rbx, %r15
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0xe45f4
testb $0x1, %r12b
je 0xec3a0
movq %r14, %rdi
xorl %esi, %esi
callq 0xe5bc5
pushq $0x2
popq %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xe5f1e
movl $0x131, %esi            # imm = 0x131
movq %r14, %rdi
callq 0xe5f1e
movq %r14, %rdi
movq %rbx, %rsi
callq 0xe5f1e
movq %r14, %rdi
movq %r13, %rsi
callq 0xe5f65
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0xe5f65
movl 0x4(%rsp), %esi
movq %r14, %rdi
callq 0xe5f1e
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0xe5f1e
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0xe5f65
movq %r14, %rdi
movq %r15, %rsi
callq 0xe5f65
jmp 0xec491
pushq $0x2
popq %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xe5f1e
movl $0x131, %esi            # imm = 0x131
movq %r14, %rdi
callq 0xe5f1e
movq %r14, %rdi
movq %rbx, %rsi
callq 0xe5f1e
movq %r14, %rdi
movq %r13, %rsi
callq 0xe5f65
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0xe5f65
movl 0x4(%rsp), %esi
movq %r14, %rdi
callq 0xe5f1e
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0xe5f1e
movq %r14, %rdi
movq %r12, %rsi
callq 0xe5f65
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0xe45f4
subq %r12, %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xe5d20
movq %r14, %rdi
xorl %esi, %esi
callq 0xe5f65
movq %r14, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5591