// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/fmtutil.c
// Function: fmtutil_fmtid
void fmtutil_fmtid(deark *c, struct fmtutil_fmtid_ctx *idctx)
{
	const char *ext = NULL;
	UI m0;
	u8 img_only = 0;

	if(!idctx->have_bof64bytes) {
		if(idctx->inf) {
			dbuf_read(idctx->inf, idctx->bof64bytes, idctx->inf_pos,
				de_min_int(64, idctx->inf_len));
			idctx->have_bof64bytes = 1;
		}
	}

	if(!idctx->have_bof64bytes) {
		goto done;
	}

	if(idctx->mode==FMTUTIL_FMTIDMODE_ALL_IMG) {
		img_only = 1;
	}

	m0 = (UI)de_getu32be_direct(&idctx->bof64bytes[0]);

#define MAGIC_PK34     0x504b0304U
#define MAGIC_JPEG     0xffd8ff00U
#define MAGIC_PNG      0x89504e47U
#define MAGIC_BMP      0x424d0000U
#define MAGIC_GIF      0x47494638U
#define MAGIC_MZ       0x4d5a0000U
#define MAGIC_ISH_Z    0x135d658cU
#define MAGIC_ISH_IA   0x2aab79d8U
#define MAGIC_ISH_INS1 0xffff0c00U
#define MAGIC_ISH_INS2 0xb8c90c00U
#define MAGIC_ISH_INI1 0x5b537461U
#define MAGIC_ISH_PKG  0x4aa30000U
#define MAGIC_TIFF1    0x49492a00U
#define MAGIC_TIFF2    0x4d4d002aU
#define MAGIC_8BPS     0x38425053U
#define MAGIC_PDF      0x25504446U

	if((m0&0xffffff00)==MAGIC_JPEG) {
		idctx->fmtid = FMTUTIL_FMTID_JPEG;
		ext = "jpg";
		goto done;
	}

	if(m0==MAGIC_PNG) {
		idctx->fmtid = FMTUTIL_FMTID_PNG;
		ext = "png";
		goto done;
	}

	if(m0==MAGIC_GIF) {
		idctx->fmtid = FMTUTIL_FMTID_GIF;
		ext = "gif";
		goto done;
	}

	if(m0==MAGIC_TIFF1 || m0==MAGIC_TIFF2) {
		idctx->fmtid = FMTUTIL_FMTID_TIFF;
		ext = "tif";
		goto done;
	}

	if(m0==MAGIC_8BPS) {
		if(idctx->bof64bytes[5]==0x01) {
			ext = "psd";
			goto done;
		}
		if(idctx->bof64bytes[5]==0x02) {
			ext = "psb";
			goto done;
		}
	}

	if(!img_only && m0==MAGIC_PK34) {
		idctx->fmtid = FMTUTIL_FMTID_ZIP;
		ext = "zip";
		goto done;
	}

	if(!img_only && m0==MAGIC_PDF) {
		ext = "pdf";
		goto done;
	}

	if((m0&0xffff0000U)==MAGIC_BMP && idctx->bof64bytes[15]==0) {
		idctx->fmtid = FMTUTIL_FMTID_BMP;
		ext = "bmp";
		goto done;
	}

	if(!img_only && (m0&0xffff0000U)==MAGIC_MZ) {
		if(fmtid_is_dll(c, idctx)) {
			ext = "dll";
		}
		else {
			ext = "exe";
		}
		goto done;
	}

	if(!img_only && m0==MAGIC_ISH_Z) {
		ext = "z";
		goto done;
	}

	if(!img_only && m0==MAGIC_ISH_IA) {
		ext = "ex_";
		goto done;
	}

	if(idctx->mode==FMTUTIL_FMTIDMODE_ISH_SFX) {
		if(m0==MAGIC_ISH_INS1 || m0==MAGIC_ISH_INS2) {
			ext = "ins";
			goto done;
		}
		if((m0&0xffff0000)==MAGIC_ISH_PKG) {
			ext = "pkg";
			goto done;
		}
		if(m0==MAGIC_ISH_INI1) {
			ext = "ini";
			goto done;
		}
	}

done:
	if(ext && (idctx->fmtid==0)) {
		idctx->fmtid = FMTUTIL_FMTID_OTHER;
	}

	if(!ext) {
		ext = idctx->default_ext;
	}
	if(!ext) {
		ext = "bin";
	}
	de_strlcpy(idctx->ext_sz, ext, sizeof(idctx->ext_sz));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
cmpb $0x0, 0x1(%rsi)
jne 0xee527
movq 0x10(%rbx), %r15
testq %r15, %r15
je 0xee765
leaq 0x28(%rbx), %r12
movq 0x18(%rbx), %r13
movq 0x20(%rbx), %rsi
pushq $0x40
popq %rdi
callq 0xdfb45
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rax, %rcx
callq 0xe3cb7
movb $0x1, 0x1(%rbx)
movb (%rbx), %bpl
leaq 0x28(%rbx), %rdi
callq 0xe404a
movl %eax, %ecx
andl $0xffffff00, %ecx       # imm = 0xFFFFFF00
cmpl $0xffd8ff00, %ecx       # imm = 0xFFD8FF00
jne 0xee556
movl $0xa, 0x68(%rbx)
leaq 0x38089(%rip), %r13     # 0x1265da
jmp 0xee612
cmpl $0x4d4d002a, %eax       # imm = 0x4D4D002A
je 0xee58c
cmpl $0x38425053, %eax       # imm = 0x38425053
je 0xee59c
cmpl $0x47494638, %eax       # imm = 0x47494638
je 0xee5bc
cmpl $0x49492a00, %eax       # imm = 0x49492A00
je 0xee58c
cmpl $0x89504e47, %eax       # imm = 0x89504E47
jne 0xee5cc
movl $0xc, 0x68(%rbx)
leaq 0x2cf69(%rip), %r13     # 0x11b4f0
jmp 0xee612
movl $0xe, 0x68(%rbx)
leaq 0x4e9fd(%rip), %r13     # 0x13cf97
jmp 0xee612
movzbl 0x2d(%rbx), %edx
cmpl $0x1, %edx
je 0xee60b
movl $0x38420000, %ecx       # imm = 0x38420000
cmpl $0x2, %edx
jne 0xee647
leaq 0x4ef15(%rip), %r13     # 0x13d4cf
jmp 0xee612
movl $0xd, 0x68(%rbx)
leaq 0x24482(%rip), %r13     # 0x112a4c
jmp 0xee612
cmpb $0x1, %bpl
sete %cl
cmpl $0x504b0304, %eax       # imm = 0x504B0304
setne %dl
orb %cl, %dl
jne 0xee5ef
movl $0x3c, 0x68(%rbx)
leaq 0x4d4aa(%rip), %r13     # 0x13ba97
jmp 0xee612
cmpb $0x1, %bpl
setne %cl
cmpl $0x25504446, %eax       # imm = 0x25504446
sete %dl
testb %dl, %cl
je 0xee628
leaq 0x4eeca(%rip), %r13     # 0x13d4d3
jmp 0xee612
leaq 0x3752e(%rip), %r13     # 0x125b40
cmpl $0x0, 0x68(%rbx)
jne 0xee769
movl $0x1, 0x68(%rbx)
jmp 0xee769
movl %eax, %ecx
andl $0xffff0000, %ecx       # imm = 0xFFFF0000
cmpl $0x424d0000, %ecx       # imm = 0x424D0000
jne 0xee67a
movl $0x424d0000, %ecx       # imm = 0x424D0000
cmpb $0x0, 0x37(%rbx)
je 0xee721
cmpb $0xa, (%rbx)
jne 0xee765
leaq 0x37c56(%rip), %r13     # 0x1262ad
cmpl $0xb8c90c00, %eax       # imm = 0xB8C90C00
je 0xee612
cmpl $0xffff0c00, %eax       # imm = 0xFFFF0C00
je 0xee612
cmpl $0x4aa30000, %ecx       # imm = 0x4AA30000
jne 0xee752
leaq 0x4ee67(%rip), %r13     # 0x13d4df
jmp 0xee612
cmpb $0x1, %bpl
sete %dl
cmpl $0x4d5a0000, %ecx       # imm = 0x4D5A0000
setne %sil
orb %dl, %sil
jne 0xee700
leaq 0x280de(%rip), %r13     # 0x116775
cmpq $0x0, 0x10(%rbx)
je 0xee734
leaq 0x64(%rbx), %rdi
callq 0xe409c
testq %rax, %rax
je 0xee734
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
callq 0xe6248
movq %rax, %r15
movl $0xf0, %esi
movq %r14, %rdi
callq 0xdecaf
movq %rax, %r12
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xf8849
cmpb $0x0, 0x63(%r12)
leaq 0x28082(%rip), %rax     # 0x116775
leaq 0x4eddd(%rip), %r13     # 0x13d4d7
cmoveq %rax, %r13
jmp 0xee73a
cmpb $0x1, %bpl
setne %dl
cmpl $0x135d658c, %eax       # imm = 0x135D658C
sete %sil
testb %sil, %dl
je 0xee794
leaq 0x3c915(%rip), %r13     # 0x12b031
jmp 0xee612
movl $0xb, 0x68(%rbx)
leaq 0x25943(%rip), %r13     # 0x114072
jmp 0xee612
xorl %r15d, %r15d
xorl %r12d, %r12d
movq %r14, %rdi
movq %r12, %rsi
callq 0xded32
movq %r15, %rdi
callq 0xe5591
jmp 0xee612
cmpl $0x5b537461, %eax       # imm = 0x5B537461
jne 0xee765
leaq 0x4ed83(%rip), %r13     # 0x13d4e3
jmp 0xee612
movq 0x8(%rbx), %r13
testq %r13, %r13
leaq 0x39d6a(%rip), %rsi     # 0x1284dd
cmovneq %r13, %rsi
addq $0x6c, %rbx
pushq $0x8
popq %rdx
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xde492
cmpb $0x1, %bpl
setne %dl
cmpl $0x2aab79d8, %eax       # imm = 0x2AAB79D8
sete %sil
testb %sil, %dl
je 0xee647
leaq 0x4ed27(%rip), %r13     # 0x13d4db
jmp 0xee612
nopl (%rax)