// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/fmtutil-advfile.c
// Function: de_advfile_run
void de_advfile_run(struct de_advfile *advf)
{
	deark *c = advf->c;
	u8 has_mac_rfork;
	u8 extract_as_macfile;
	u8 has_mac_metadata;
	u8 try_to_keep_mac_metadata;
	int fmt;

	if(!c->macformat_known) {
		const char *mfmt;

		c->macformat_known = 1;
		c->macformat = DE_MACFORMAT_APPLEDOUBLE;

		// [I know there is a module named "macrsrc", so this could lead to confusion,
		// but I can't think of a better name.]
		mfmt = de_get_ext_option(c, "macrsrc");
		if(mfmt) {
			if(!de_strcmp(mfmt, "raw")) {
				c->macformat = DE_MACFORMAT_RAW; // Raw resource file
			}
			else if(!de_strcmp(mfmt, "as")) {
				c->macformat = DE_MACFORMAT_APPLESINGLE;
			}
			else if(!de_strcmp(mfmt, "ad")) {
				c->macformat = DE_MACFORMAT_APPLEDOUBLE;
			}
			else if(!de_strcmp(mfmt, "mbin")) {
				c->macformat = DE_MACFORMAT_MACBINARY;
			}
		}

		c->macmetaflag = (u8)de_get_ext_option_bool(c, "macmeta", 0);
	}

	// There's also finderflags, but I don't think it matters.
	has_mac_metadata = (advf->has_creatorcode || advf->has_typecode);

	has_mac_rfork = (advf->rsrcfork.fork_exists && advf->rsrcfork.fork_len>0);

	try_to_keep_mac_metadata = has_mac_rfork || (c->macmetaflag && has_mac_metadata);
	extract_as_macfile = has_mac_rfork || try_to_keep_mac_metadata;

	fmt = c->macformat; // Default to the default Mac format.

	// It's hard to decide what conversions to allow, and to account for all
	// combinations of options.
	// For now, we do allow some silly things, like MacBinary->MacBinary.
	// But we want to be careful when AppleDouble is the source format, since
	// (1) it doesn't contain the data fork (maybe it shouldn't even use
	// advfile), and (2) it's special because it's our standard format for
	// extracting resource forks.
	if(advf->originally_appledouble &&
		(fmt==DE_MACFORMAT_APPLEDOUBLE || fmt==DE_MACFORMAT_APPLESINGLE))
	{
		fmt = DE_MACFORMAT_RAW;
	}

	if(extract_as_macfile && fmt==DE_MACFORMAT_APPLESINGLE) {
		de_advfile_run_applesd(c, advf, 0);
	}
	else if(extract_as_macfile && fmt==DE_MACFORMAT_APPLEDOUBLE) {
		int extract_dfork = 0;
		int extract_rfork = 0;

		if(advf->mainfork.fork_exists && advf->mainfork.fork_len>0) {
			extract_dfork = 1;
		}
		if(advf->rsrcfork.fork_exists && advf->rsrcfork.fork_len>0) {
			extract_rfork = 1;
		}
		else if(try_to_keep_mac_metadata) {
			extract_rfork = 1;
		}
		if(!extract_dfork && !extract_rfork) {
			extract_dfork = 1;
		}

		if(extract_dfork) {
			de_advfile_run_rawfiles(c, advf, 1); // For the data/main fork
		}
		if(extract_rfork) {
			de_advfile_run_applesd(c, advf, 1); // For the rsrc fork
		}
	}
	else if(extract_as_macfile && fmt==DE_MACFORMAT_MACBINARY) {
		de_advfile_run_macbinary(c, advf);
	}
	else {
		de_advfile_run_rawfiles(c, advf, 0);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r14
movq (%rdi), %rbx
cmpb $0x0, 0xd1(%rbx)
jne 0xf099c
movw $0x201, 0xd1(%rbx)      # imm = 0x201
leaq 0x19223(%rip), %rsi     # 0x109b3c
movq %rbx, %rdi
callq 0xdfa87
testq %rax, %rax
je 0xf0985
movq %rax, %r15
leaq 0x3a9f4(%rip), %rsi     # 0x12b324
movq %rax, %rdi
callq 0x181a0
testl %eax, %eax
je 0xf097d
cmpb $0x61, (%r15)
jne 0xf0966
cmpb $0x73, 0x1(%r15)
jne 0xf0954
cmpb $0x0, 0x2(%r15)
je 0xf0e80
cmpb $0x64, 0x1(%r15)
jne 0xf0966
cmpb $0x0, 0x2(%r15)
je 0xf0e87
leaq 0x4d007(%rip), %rsi     # 0x13d974
movq %r15, %rdi
callq 0x181a0
testl %eax, %eax
jne 0xf0985
movb $0x3, %al
jmp 0xf097f
xorl %eax, %eax
movb %al, 0xd2(%rbx)
leaq 0x4cfed(%rip), %rsi     # 0x13d979
movq %rbx, %rdi
xorl %edx, %edx
callq 0xdfae5
movb %al, 0xd3(%rbx)
movb $0x1, %dl
cmpb $0x0, 0x7c(%r14)
jne 0xf09ad
cmpb $0x0, 0x7b(%r14)
setne %dl
movb 0x38(%r14), %al
testb %al, %al
je 0xf09be
movb $0x1, %cl
cmpq $0x0, 0x40(%r14)
jg 0xf09ca
cmpb $0x0, 0xd3(%rbx)
setne %cl
andb %dl, %cl
movzbl 0xd2(%rbx), %esi
leal -0x1(%rsi), %edi
xorl %edx, %edx
cmpb $0x2, %dil
cmovael %esi, %edx
cmpb $0x0, 0x7a(%r14)
cmovel %esi, %edx
testb %cl, %cl
je 0xf09f8
cmpl $0x1, %edx
jne 0xf09f8
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
jmp 0xf0a2d
cmpl $0x2, %edx
sete %sil
andb %cl, %sil
cmpb $0x1, %sil
jne 0xf0a40
cmpb $0x0, 0x10(%r14)
je 0xf0a24
cmpq $0x0, 0x18(%r14)
jle 0xf0a24
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf13d4
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf0e8e
cmpl $0x3, %edx
sete %dl
andb %dl, %cl
cmpb $0x1, %cl
jne 0xf0a5a
cmpb $0x0, 0x10(%r14)
je 0xf0a75
movq 0x18(%r14), %rbp
jmp 0xf0a77
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf13d4
xorl %ebp, %ebp
testb %al, %al
je 0xf0a81
movq 0x40(%r14), %r13
jmp 0xf0a84
xorl %r13d, %r13d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rbp
setg %cl
cmpq %rax, %r13
setg %al
orb %cl, %al
cmpb $0x1, %al
jne 0xf0ac8
leaq 0x4cf47(%rip), %rsi     # 0x13d9e9
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
xorl %r14d, %r14d
xorl %r12d, %r12d
movq $0x0, 0x8(%rsp)
xorl %r15d, %r15d
xorl %r13d, %r13d
jmp 0xf0e3a
movq 0x18(%r14), %rax
movl $0x80, %ecx
cqto
idivq %rcx
subq %rdx, %rcx
testq %rdx, %rdx
cmovleq %rdx, %rcx
movq %rcx, 0x10(%rsp)
movq %rbx, %rdi
callq 0xeac74
movq %rax, %r15
movq 0x68(%r14), %rsi
movq %rax, %rdi
callq 0xead71
cmpq $0x0, 0x10(%r15)
jg 0xf0b15
leaq 0x30bf2(%rip), %rsi     # 0x1216fc
pushq $0x5
popq %rdx
movq %r15, %rdi
callq 0xeae42
leaq 0x379c0(%rip), %rsi     # 0x1284dc
pushq $0x5
popq %rdx
movq %r15, %rdi
callq 0xeae42
movq 0x20(%r14), %rsi
movl 0x70(%r14), %ecx
movq %rbx, %rdi
movq %r15, 0x20(%rsp)
movq %r15, %rdx
callq 0xdfd55
movb 0x79(%r14), %al
movq 0x20(%r14), %rcx
movb %al, 0x8(%rcx)
movl $0x80, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe59cd
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
callq 0xe5bc5
cmpq $0x0, 0x88(%r14)
je 0xf0ba8
movq 0x80(%r14), %rax
testq %rax, %rax
je 0xf0ba8
cmpq $0x3f, %rax
pushq $0x3f
popq %r15
cmovlq %rax, %r15
movzbl %r15b, %esi
movq %r12, %rdi
callq 0xe5bc5
movq 0x88(%r14), %rsi
movq %r12, %rdi
movq %r15, %rdx
callq 0xe46ba
jmp 0xf0bc2
pushq $0x7
popq %rsi
movq %r12, %rdi
callq 0xe5bc5
leaq 0x4ce5b(%rip), %rsi     # 0x13da15
movq %r12, %rdi
callq 0xe5fe4
pushq $0x41
popq %rsi
movq %r12, %rdi
callq 0xe5eb2
cmpb $0x0, 0x7b(%r14)
je 0xf0be6
leaq 0x90(%r14), %rsi
pushq $0x4
popq %rdx
movq %r12, %rdi
callq 0xe46ba
pushq $0x45
popq %rsi
movq %r12, %rdi
callq 0xe5eb2
cmpb $0x0, 0x7c(%r14)
je 0xf0c0a
leaq 0x94(%r14), %rsi
pushq $0x4
popq %rdx
movq %r12, %rdi
callq 0xe46ba
pushq $0x49
popq %rsi
movq %r12, %rdi
callq 0xe5eb2
cmpb $0x0, 0x7d(%r14)
je 0xf0c29
movzbl 0x7f(%r14), %esi
movq %r12, %rdi
callq 0xe5bc5
pushq $0x53
popq %rsi
movq %r12, %rdi
callq 0xe5eb2
movq %r12, %rdi
movq %rbp, (%rsp)
movq %rbp, %rsi
callq 0xe5f65
movq %r12, %rdi
movq %r13, %rsi
callq 0xe5f65
pushq $0x5b
popq %rsi
movq %r12, %rdi
callq 0xe5eb2
movq 0x20(%r14), %rdi
addq $0x28, %rdi
callq 0xf1617
movq %r12, %rdi
movq %rax, %rsi
callq 0xe5f65
movq 0x20(%r14), %rdi
addq $0x18, %rdi
callq 0xf1617
movq %r12, %rdi
movq %rax, %rsi
callq 0xe5f65
pushq $0x65
popq %rsi
movq %r12, %rdi
callq 0xe5eb2
cmpb $0x0, 0x7d(%r14)
je 0xf0ca8
movzbl 0x7e(%r14), %esi
movq %r12, %rdi
callq 0xe5bc5
movq %r13, 0x18(%rsp)
pushq $0x66
popq %rsi
movq %r12, %rdi
callq 0xe5eb2
leaq 0x30bd2(%rip), %rsi     # 0x121891
pushq $0x4
popq %rdx
movq %r12, %rdi
callq 0xe46ba
pushq $0x7a
popq %rsi
movq %r12, %rdi
callq 0xe5eb2
movq %r12, %rdi
movl $0x82, %esi
callq 0xe5bc5
movq %r12, %rdi
movl $0x81, %esi
callq 0xe5bc5
pushq $0x20
popq %rsi
movq %rbx, %rdi
callq 0xe0ae9
movq %rax, %r15
pushq $0x7c
popq %rcx
movq %rax, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0xe0f33
movq %r15, 0x8(%rsp)
movq %r15, %rdi
callq 0xe0e95
movl %eax, %esi
movq %r12, %rdi
callq 0xe5f1e
movl $0x80, %esi
movq %r12, %rdi
callq 0xe5eb2
movq 0x20(%r14), %rdx
movl 0x74(%r14), %ecx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe4e62
movq %rax, %rbp
cmpb $0x0, 0x78(%r14)
je 0xf0d55
movq %rbp, %rdi
callq 0xe589c
movl $0x80, %edx
movq %r12, %rdi
xorl %esi, %esi
movq %rbp, %rcx
callq 0xe45f4
pushq $0x10
popq %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0xdecaf
movq %rax, %r15
movq %rbx, %rdi
movq %r13, %rsi
callq 0xdecaf
movl $0x1, (%r15)
movq %rax, %r13
movl $0x2, (%rax)
movq 0x28(%r14), %rdx
movq 0x30(%r14), %rsi
movq %rbp, %rdi
callq 0xe629a
movq %rbp, 0x8(%r15)
movq 0x60(%r14), %rax
testq %rax, %rax
setne %cl
cmpq $0x0, (%rsp)
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0xf0dcc
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq *%rax
movq %r15, (%rsp)
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe629a
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
callq 0xe5d20
movq 0x50(%r14), %rdx
movq 0x58(%r14), %rsi
movq %rbp, %rdi
callq 0xe629a
movq %rbp, 0x8(%r13)
movq 0x60(%r14), %rax
testq %rax, %rax
setne %cl
cmpq $0x0, 0x18(%rsp)
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0xf0e22
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq *%rax
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe629a
movq 0x20(%rsp), %r14
movq %r13, %r15
movq (%rsp), %r13
movq %r12, %rdi
callq 0xe5591
movq %rbp, %rdi
callq 0xe5591
movq %rbx, %rdi
movq %r13, %rsi
callq 0xded32
movq %rbx, %rdi
movq %r15, %rsi
callq 0xded32
movq %r14, %rdi
callq 0xeaf3c
movq 0x8(%rsp), %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe0e8d
movb $0x1, %al
jmp 0xf097f
movb $0x2, %al
jmp 0xf097f