// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/bmp.c
// Function: detect_bmp_version
static int detect_bmp_version(deark *c, lctx *d)
{
	i64 pos;

	pos = 0;
	d->fsize = de_getu32le(pos+2);

	pos += FILEHEADER_SIZE;
	d->infohdrsize = de_getu32le(pos);

	if(d->infohdrsize<=12) {
		d->bitcount = de_getu16le(pos+10);
	}
	else {
		d->bitcount = de_getu16le(pos+14);
	}

	if(d->infohdrsize==12) {
		d->version = DE_BMPVER_OS2V1;
		return 1;
	}
	if(d->infohdrsize<16) {
		return 0;
	}

	if(d->infohdrsize>=20) {
		d->compression_field = (u32)de_getu32le(pos+16);
	}

	// We're only trying to support the most basic 64-bit BMP images.
	if(d->bitcount==64 && d->infohdrsize==40 && d->compression_field==0) {
		d->version = DE_BMPVER_64BIT;
		return 1;
	}

	if(d->infohdrsize>=16 && d->infohdrsize<=64) {
		if(d->fsize==FILEHEADER_SIZE+d->infohdrsize) {
			d->version = DE_BMPVER_OS2V2;
			return 1;
		}

		if((d->compression_field==3 && d->bitcount==1) ||
			(d->compression_field==4 && d->bitcount==24))
		{
			d->version = DE_BMPVER_OS2V2;
			return 1;
		}

		if(d->infohdrsize!=40 && d->infohdrsize!=52 && d->infohdrsize!=56) {
			d->version = DE_BMPVER_OS2V2;
			return 1;
		}
	}

	d->version = DE_BMPVER_WINV345;
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x2, %rsi
callq 0x165540
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x20(%rbp), %rax
addq $0xe, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x165540
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x18(%rbp), %rax
cmpq $0xc, 0x30(%rax)
jg 0x43f6d
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0xa, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
jmp 0x43f8d
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0xe, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x18(%rbp), %rax
cmpq $0xc, 0x30(%rax)
jne 0x43fae
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x440f5
movq -0x18(%rbp), %rax
cmpq $0x10, 0x30(%rax)
jge 0x43fc5
movl $0x0, -0x4(%rbp)
jmp 0x440f5
movq -0x18(%rbp), %rax
cmpq $0x14, 0x30(%rax)
jl 0x43fee
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x10, %rsi
callq 0x165540
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x40(%rax)
movq -0x18(%rbp), %rax
cmpq $0x40, 0x38(%rax)
jne 0x44024
movq -0x18(%rbp), %rax
cmpq $0x28, 0x30(%rax)
jne 0x44024
movq -0x18(%rbp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x44024
movq -0x18(%rbp), %rax
movl $0xa, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x440f5
movq -0x18(%rbp), %rax
cmpq $0x10, 0x30(%rax)
jl 0x440e4
movq -0x18(%rbp), %rax
cmpq $0x40, 0x30(%rax)
jg 0x440e4
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x30(%rcx), %rcx
addq $0xe, %rcx
cmpq %rcx, %rax
jne 0x44071
movq -0x18(%rbp), %rax
movl $0x2, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x440f5
movq -0x18(%rbp), %rax
cmpl $0x3, 0x40(%rax)
jne 0x44086
movq -0x18(%rbp), %rax
cmpq $0x1, 0x38(%rax)
je 0x4409b
movq -0x18(%rbp), %rax
cmpl $0x4, 0x40(%rax)
jne 0x440ae
movq -0x18(%rbp), %rax
cmpq $0x18, 0x38(%rax)
jne 0x440ae
movq -0x18(%rbp), %rax
movl $0x2, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x440f5
movq -0x18(%rbp), %rax
cmpq $0x28, 0x30(%rax)
je 0x440e2
movq -0x18(%rbp), %rax
cmpq $0x34, 0x30(%rax)
je 0x440e2
movq -0x18(%rbp), %rax
cmpq $0x38, 0x30(%rax)
je 0x440e2
movq -0x18(%rbp), %rax
movl $0x2, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x440f5
jmp 0x440e4
movq -0x18(%rbp), %rax
movl $0x3, (%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nop