// File: /workspace/github_c_makefile_star2_O1/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 0xfa3e0
movq 0x10(%rbx), %r15
testq %r15, %r15
je 0xfa3e0
leaq 0x28(%rbx), %r12
movq 0x18(%rbx), %r13
movq 0x20(%rbx), %rsi
movl $0x40, %edi
callq 0xea792
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rax, %rcx
callq 0xeeee8
movb $0x1, 0x1(%rbx)
movb $0x1, %bpl
cmpb $0x0, 0x1(%rbx)
je 0xfa5b4
movb (%rbx), %r15b
leaq 0x28(%rbx), %rdi
callq 0xef325
movl %eax, %ecx
andl $0xffffff00, %ecx       # imm = 0xFFFFFF00
cmpl $0xffd8ff00, %ecx       # imm = 0xFFD8FF00
jne 0xfa41c
movl $0xa, 0x68(%rbx)
leaq 0x390a3(%rip), %rcx     # 0x1334ba
jmp 0xfa583
cmpl $0x47494637, %eax       # imm = 0x47494637
jle 0xfa44b
cmpl $0x47494638, %eax       # imm = 0x47494638
je 0xfa499
cmpl $0x49492a00, %eax       # imm = 0x49492A00
je 0xfa438
cmpl $0x4d4d002a, %eax       # imm = 0x4D4D002A
jne 0xfa473
movl $0xe, 0x68(%rbx)
leaq 0x4fa71(%rip), %rcx     # 0x149eb7
jmp 0xfa583
cmpl $0x89504e47, %eax       # imm = 0x89504E47
je 0xfa4ac
cmpl $0x38425053, %eax       # imm = 0x38425053
jne 0xfa473
movzbl 0x2d(%rbx), %ecx
cmpl $0x1, %ecx
je 0xfa4de
cmpl $0x2, %ecx
jne 0xfa473
leaq 0x4ff81(%rip), %rcx     # 0x14a3ef
jmp 0xfa583
cmpb $0x1, %r15b
sete %cl
cmpl $0x504b0304, %eax       # imm = 0x504B0304
setne %dl
orb %cl, %dl
jne 0xfa4bf
movl $0x3c, 0x68(%rbx)
leaq 0x4e53b(%rip), %rcx     # 0x1489cf
jmp 0xfa583
movl $0xd, 0x68(%rbx)
leaq 0x25529(%rip), %rcx     # 0x11f9d0
jmp 0xfa583
movl $0xc, 0x68(%rbx)
leaq 0x2e046(%rip), %rcx     # 0x128500
jmp 0xfa583
cmpb $0x1, %r15b
setne %cl
cmpl $0x25504446, %eax       # imm = 0x25504446
sete %dl
testb %dl, %cl
je 0xfa4ec
leaq 0x4ff1a(%rip), %rcx     # 0x14a3f3
jmp 0xfa583
xorl %ebp, %ebp
leaq 0x38539(%rip), %rcx     # 0x132a20
jmp 0xfa5b6
movl %eax, %edx
andl $0xffff0000, %edx       # imm = 0xFFFF0000
cmpl $0x424d0000, %edx       # imm = 0x424D0000
jne 0xfa502
cmpb $0x0, 0x37(%rbx)
je 0xfa575
cmpb $0x1, %r15b
sete %cl
cmpl $0x4d5a0000, %edx       # imm = 0x4D5A0000
setne %sil
orb %cl, %sil
jne 0xfa539
movq %r14, %rdi
movq %rbx, %rsi
callq 0xfa616
testl %eax, %eax
leaq 0x29229(%rip), %rax     # 0x123755
leaq 0x4fec4(%rip), %rcx     # 0x14a3f7
cmoveq %rax, %rcx
jmp 0xfa583
cmpb $0x1, %r15b
setne %cl
cmpl $0x135d658c, %eax       # imm = 0x135D658C
sete %sil
testb %sil, %cl
je 0xfa557
leaq 0x3da50(%rip), %rcx     # 0x137fa5
jmp 0xfa583
cmpb $0x1, %r15b
setne %cl
cmpl $0x2aab79d8, %eax       # imm = 0x2AAB79D8
sete %sil
testb %sil, %cl
je 0xfa587
leaq 0x4fe88(%rip), %rcx     # 0x14a3fb
jmp 0xfa583
movl $0xb, 0x68(%rbx)
leaq 0x26acf(%rip), %rcx     # 0x121052
xorl %ebp, %ebp
jmp 0xfa5b6
cmpb $0xa, (%rbx)
jne 0xfa5b4
leaq 0x38bfa(%rip), %rcx     # 0x13318d
xorl %ebp, %ebp
cmpl $0xb8c90c00, %eax       # imm = 0xB8C90C00
je 0xfa5b6
cmpl $0xffff0c00, %eax       # imm = 0xFFFF0C00
je 0xfa5b6
cmpl $0x4aa30000, %edx       # imm = 0x4AA30000
jne 0xfa5fe
leaq 0x4fe4d(%rip), %rcx     # 0x14a3ff
jmp 0xfa583
xorl %ecx, %ecx
testb %bpl, %bpl
jne 0xfa5cd
cmpl $0x0, 0x68(%rbx)
jne 0xfa5c8
movl $0x1, 0x68(%rbx)
testb %bpl, %bpl
je 0xfa5d1
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
leaq 0x3ae56(%rip), %rsi     # 0x135431
cmovneq %rcx, %rsi
addq $0x6c, %rbx
movl $0x8, %edx
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe8ded
xorl %ecx, %ecx
cmpl $0x5b537461, %eax       # imm = 0x5B537461
setne %bpl
leaq 0x4fdf3(%rip), %rax     # 0x14a403
cmoveq %rax, %rcx
jmp 0xfa5b6