// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/bmp.c
// Function: de_run_bmp
static void de_run_bmp(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	i64 pos;

	d = de_malloc(c, sizeof(lctx));

	if(mparams) {
		// if in_params.flags & 0x2, then the parent module wants the decoded
		// image to be returned, instead of written to a file.
		// in_params.obj1 points to an fmtutil_bmp_mparams_indata struct,
		// allocated by the caller, that will be filled in by the bmp module.
		// [It's acknowledged that out_params seems more appropriate for data
		// going from the child to the parent. But I don't think it's right
		// for the parent to put stuff in out_params, and I want the parent
		// to both allocate and free the container struct. Some rethinking of this
		// data passing system is probably in order.]
		if(mparams->in_params.flags & 0x02) {
			d->want_returned_img = 1;
		}
	}

	d->fi = de_finfo_create(c);

	if(dbuf_memcmp(c->infile, 0, "BM", 2)) {
		de_err(c, "Not a BMP file.");
		goto done;
	}

	if(!detect_bmp_version(c, d)) {
		de_err(c, "Unidentified BMP version.");
		goto done;
	}

	switch(d->version) {
	case DE_BMPVER_OS2V1:
		if(d->fsize==26) {
			de_declare_fmt(c, "BMP, OS/2 v1");
		}
		else {
			de_declare_fmt(c, "BMP, OS/2 v1 or Windows v2");
		}
		break;
	case DE_BMPVER_OS2V2: de_declare_fmt(c, "BMP, OS/2 v2"); break;
	case DE_BMPVER_WINV345:
		switch(d->infohdrsize) {
		case 40: de_declare_fmt(c, "BMP, Windows v3"); break;
		case 108: de_declare_fmt(c, "BMP, Windows v4"); break;
		case 124: de_declare_fmt(c, "BMP, Windows v5"); break;
		default: de_declare_fmt(c, "BMP, Windows v3+");
		}
		break;
	case DE_BMPVER_64BIT:
		de_declare_fmt(c, "BMP, 64-bit");
		break;
	}

	pos = 0;
	if(!read_fileheader(c, d, pos)) goto done;
	pos += FILEHEADER_SIZE;
	if(!read_infoheader(c, d, pos)) goto done;
	pos += d->infohdrsize;
	if(d->bitfields_type==BF_SEGMENT) {
		de_dbg(c, "bitfields segment at %"I64_FMT", len=%"I64_FMT, pos, d->bitfields_segment_len);
		if(pos+d->bitfields_segment_len > d->bits_offset) {
			de_warn(c, "BITFIELDS segment at %"I64_FMT" (size %"I64_FMT") overlaps bitmap at %"I64_FMT,
				pos, d->bitfields_segment_len, d->bits_offset);
		}
		de_dbg_indent(c, 1);
		do_read_bitfields(c, d, pos, d->bitfields_segment_len);
		de_dbg_indent(c, -1);
		pos += d->bitfields_segment_len;
	}
	d->pal_pos = pos;
	do_read_palette(c, d);
	do_image(c, d);
	do_read_profile(c, d);

	if(d->want_returned_img && mparams && mparams->in_params.obj1) {
		struct fmtutil_bmp_mparams_indata *idata =
			(struct fmtutil_bmp_mparams_indata*)mparams->in_params.obj1;
		idata->img = d->img;
		d->img = NULL;
		idata->fi = d->fi;
		d->fi = NULL;
		idata->createflags = d->createflags;
	}

done:
	if(d) {
		de_bitmap_destroy(d->img);
		de_finfo_destroy(c, d->fi);
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movl $0x540, %esi            # imm = 0x540
callq 0x15b780
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x42a8a
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x42a88
movq -0x18(%rbp), %rax
movb $0x1, 0x4(%rax)
jmp 0x42a8a
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0x165239(%rip), %rdx    # 0x1a7cea
movl $0x2, %ecx
callq 0x166ab0
cmpl $0x0, %eax
je 0x42ad7
movq -0x8(%rbp), %rdi
leaq 0x160c12(%rip), %rsi    # 0x1a36dd
movb $0x0, %al
callq 0x15bfb0
jmp 0x42db4
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x43ee0
cmpl $0x0, %eax
jne 0x42b00
movq -0x8(%rbp), %rdi
leaq 0x160bf9(%rip), %rsi    # 0x1a36ed
movb $0x0, %al
callq 0x15bfb0
jmp 0x42db4
movq -0x18(%rbp), %rax
movl (%rax), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x30(%rbp)
subl $0x9, %eax
ja 0x42bf6
movq -0x30(%rbp), %rax
leaq 0x1609ee(%rip), %rcx    # 0x1a3510
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x18(%rbp), %rax
cmpq $0x1a, 0x20(%rax)
jne 0x42b48
movq -0x8(%rbp), %rdi
leaq 0x160bc6(%rip), %rsi    # 0x1a3707
callq 0x15e380
jmp 0x42b58
movq -0x8(%rbp), %rdi
leaq 0x160bc1(%rip), %rsi    # 0x1a3714
callq 0x15e380
jmp 0x42bf6
movq -0x8(%rbp), %rdi
leaq 0x160bc7(%rip), %rsi    # 0x1a372f
callq 0x15e380
jmp 0x42bf6
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x38(%rbp)
subq $0x28, %rax
je 0x42b9e
jmp 0x42b86
movq -0x38(%rbp), %rax
subq $0x6c, %rax
je 0x42bb0
jmp 0x42b92
movq -0x38(%rbp), %rax
subq $0x7c, %rax
je 0x42bc2
jmp 0x42bd4
movq -0x8(%rbp), %rdi
leaq 0x160b93(%rip), %rsi    # 0x1a373c
callq 0x15e380
jmp 0x42be4
movq -0x8(%rbp), %rdi
leaq 0x160b91(%rip), %rsi    # 0x1a374c
callq 0x15e380
jmp 0x42be4
movq -0x8(%rbp), %rdi
leaq 0x160b8f(%rip), %rsi    # 0x1a375c
callq 0x15e380
jmp 0x42be4
movq -0x8(%rbp), %rdi
leaq 0x160b8d(%rip), %rsi    # 0x1a376c
callq 0x15e380
jmp 0x42bf6
movq -0x8(%rbp), %rdi
leaq 0x160b8c(%rip), %rsi    # 0x1a377d
callq 0x15e380
movq $0x0, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x44100
cmpl $0x0, %eax
jne 0x42c19
jmp 0x42db4
movq -0x20(%rbp), %rax
addq $0xe, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x441b0
cmpl $0x0, %eax
jne 0x42c40
jmp 0x42db4
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x2, 0x8c(%rax)
jne 0x42d0f
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x90(%rax), %rcx
leaq 0x160b0e(%rip), %rsi    # 0x1a3789
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
addq 0x90(%rcx), %rax
movq -0x18(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jle 0x42cc4
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x90(%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x28(%rax), %r8
leaq 0x160aee(%rip), %rsi    # 0x1a37ab
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x90(%rax), %rcx
callq 0x44ab0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x18(%rbp), %rax
movq 0x90(%rax), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x44b80
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x44cc0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x45030
movq -0x18(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x0, %eax
je 0x42db2
cmpq $0x0, -0x10(%rbp)
je 0x42db2
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x42db2
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x10(%rax)
jmp 0x42db4
cmpq $0x0, -0x18(%rbp)
je 0x42de6
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x15d260
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)