// File: /workspace/github_c_makefile_star2_O3/albinoloverats[P]hide/src/bmp.c
// Function: read_bmp
static int read_bmp(image_info_t *image_info, void (*progress_update)(uint64_t, uint64_t))
{
	errno = EXIT_SUCCESS;

	FILE *bmp = fopen(image_info->file, "rb");
	if (!bmp)
		return errno;

	fseek(bmp, 0x12, SEEK_SET);
	uint32_t dword;
	fread(&dword, sizeof dword, 1, bmp);
	image_info->width = from_little_endian_32(dword);
	fread(&dword, sizeof dword, 1, bmp);
	image_info->height = from_little_endian_32(dword);

	fseek(bmp, 0x1C, SEEK_SET);
	uint16_t word;
	fread(&word, sizeof word, 1, bmp);
	switch (from_little_endian_16(word))
	{
		case 32:
			image_info->bpp = 4;
			break;
		case 24:
			image_info->bpp = 3;
			break;
		default:
			errno = ENOTSUP;
			goto done;
	}
	fread(&dword, sizeof dword, 1, bmp);
	if (dword != BI_RGB)
	{
		errno = ENOTSUP;
		goto done;
	}

	bmp_extra_t *extra = malloc(sizeof (bmp_extra_t));
	fseek(bmp, 0x0A, SEEK_SET);
	fread(&extra->size, sizeof extra->size, 1, bmp);
	extra->size = from_little_endian_32(extra->size);
	extra->data = malloc(extra->size);
	fseek(bmp, 0x00, SEEK_SET);
	fread(extra->data, extra->size, 1, bmp);
	image_info->extra = extra;

	uint32_t padding = image_info->width % 4;
	if (padding == 4)
		padding = 0;

	image_info->buffer = malloc(sizeof (uint8_t *) * image_info->height);
	fseek(bmp, extra->size, SEEK_SET);
	for (uint64_t y = 0; y < image_info->height; y++)
	{
		image_info->buffer[y] = malloc(image_info->width * image_info->bpp);
		fread(image_info->buffer[y], image_info->width, image_info->bpp, bmp);
		uint32_t ignored = 0x00;
		if (padding)
			fread(&ignored, padding, 1, bmp);
		if (progress_update)
			progress_update(y, image_info->height);
	}

done:
	fclose(bmp);

	return errno;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movq %rdi, %r15
callq 0x1040
movq %rax, %rbx
movl $0x0, (%rax)
movq (%r15), %rdi
leaq 0xdb1(%rip), %rsi       # 0x2004
callq 0x1070
testq %rax, %rax
je 0x12fa
movq %rax, %r12
movl $0x12, %esi
movq %rax, %rdi
xorl %edx, %edx
callq 0x1090
leaq 0x18(%rsp), %r13
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rcx
callq 0x1050
movl (%r13), %eax
movq %rax, 0x30(%r15)
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r12, %rcx
callq 0x1050
movl (%r13), %eax
movq %rax, 0x28(%r15)
movl $0x1c, %esi
movq %r12, %rdi
xorl %edx, %edx
callq 0x1090
leaq 0x1e(%rsp), %r13
movl $0x2, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r12, 0x8(%rsp)
movq %r12, %rcx
callq 0x1050
movzwl (%r13), %eax
cmpl $0x20, %eax
movq %rbx, 0x20(%rsp)
je 0x12fe
cmpl $0x18, %eax
jne 0x132a
movw $0x3, %ax
jmp 0x1302
movl (%rbx), %eax
jmp 0x1344
movw $0x4, %ax
movw %ax, 0x38(%r15)
leaq 0x18(%rsp), %r13
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq 0x8(%rsp), %rcx
callq 0x1050
cmpl $0x0, (%r13)
je 0x1353
movl $0x5f, (%rbx)
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x1060
movq 0x20(%rsp), %rax
movl (%rax), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x1080
movq %rax, %r13
movl $0xa, %esi
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1090
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x1050
movl (%r13), %r12d
movq %r12, %rdi
callq 0x1080
movq %rax, %rbp
movq %rax, 0x8(%r13)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1090
movl $0x1, %edx
movq %rbp, %rdi
movq %r12, %rsi
movq %rbx, %rcx
callq 0x1050
movq %r13, 0x48(%r15)
movq 0x28(%r15), %rdi
movq 0x30(%r15), %r13
shlq $0x3, %rdi
callq 0x1080
movq %rax, 0x40(%r15)
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x1090
cmpq $0x0, 0x28(%r15)
je 0x1335
andl $0x3, %r13d
xorl %ebp, %ebp
movq %r14, %r12
movq 0x30(%r15), %rbx
movzwl 0x38(%r15), %r14d
movq %rbx, %rdi
imulq %r14, %rdi
callq 0x1080
movq 0x40(%r15), %rcx
movq %rax, (%rcx,%rbp,8)
movq 0x40(%r15), %rax
movq (%rax,%rbp,8), %rdi
movq %rbx, %rsi
movq 0x8(%rsp), %rbx
movq %r14, %rdx
movq %rbx, %rcx
callq 0x1050
movl $0x0, 0x14(%rsp)
testq %r13, %r13
je 0x144f
movl $0x1, %edx
leaq 0x14(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rcx
callq 0x1050
movq %r12, %r14
testq %r12, %r12
je 0x1461
movq 0x28(%r15), %rsi
movq %rbp, %rdi
callq *%r14
incq %rbp
cmpq 0x28(%r15), %rbp
jb 0x13f2
jmp 0x1335