// File: /workspace/github_c_makefile_star2_O2/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
andl $0x0, (%rax)
movq (%r15), %rdi
leaq 0xdb8(%rip), %rsi       # 0x2004
callq 0x1070
testq %rax, %rax
je 0x12ee
movq %rax, %r12
movq %r14, 0x20(%rsp)
movq %rbx, 0x8(%rsp)
pushq $0x12
popq %rsi
movq %rax, %rdi
xorl %edx, %edx
callq 0x1090
leaq 0x18(%rsp), %r13
pushq $0x4
popq %rbp
pushq $0x1
popq %rbx
movq %r13, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x1050
movl (%r13), %eax
movq %rax, 0x30(%r15)
movq %r13, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x1050
movl (%r13), %eax
movq %rax, 0x28(%r15)
pushq $0x1c
popq %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x1090
leaq 0x1e(%rsp), %r13
pushq $0x2
popq %rsi
movq %r13, %rdi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x1050
movzwl (%r13), %eax
cmpl $0x20, %eax
je 0x12f2
cmpl $0x18, %eax
movq 0x8(%rsp), %r14
jne 0x131b
movw $0x3, %ax
jmp 0x12fb
movl (%rbx), %eax
jmp 0x1331
movw $0x4, %ax
movq 0x8(%rsp), %r14
movw %ax, 0x38(%r15)
leaq 0x18(%rsp), %rbx
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r12, %rcx
callq 0x1050
cmpl $0x0, (%rbx)
je 0x1340
movl $0x5f, (%r14)
movq %r12, %rdi
callq 0x1060
movq 0x8(%rsp), %rax
movl (%rax), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x1080
movq %rax, %rbx
pushq $0xa
popq %rsi
xorl %r13d, %r13d
movq %r12, %rdi
xorl %edx, %edx
callq 0x1090
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r12, %rcx
callq 0x1050
movl (%rbx), %r14d
movq %r14, %rdi
callq 0x1080
movq %rax, %rbp
movq %rax, 0x8(%rbx)
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1090
movq %rbp, %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
movq %r12, %rcx
callq 0x1050
movq %rbx, 0x48(%r15)
movq 0x28(%r15), %rdi
movq 0x30(%r15), %rbp
shlq $0x3, %rdi
callq 0x1080
movq %rax, 0x40(%r15)
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x1090
andl $0x3, %ebp
cmpq 0x28(%r15), %r13
jae 0x1322
movq 0x30(%r15), %rbx
movzwl 0x38(%r15), %r14d
movq %rbx, %rdi
imulq %r14, %rdi
callq 0x1080
movq 0x40(%r15), %rcx
movq %rax, (%rcx,%r13,8)
movq 0x40(%r15), %rax
movq (%rax,%r13,8), %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x1050
andl $0x0, 0x14(%rsp)
testq %rbp, %rbp
je 0x141e
leaq 0x14(%rsp), %rdi
movq %rbp, %rsi
pushq $0x1
popq %rdx
movq %r12, %rcx
callq 0x1050
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x1431
movq 0x28(%r15), %rsi
movq %r13, %rdi
callq *%rax
incq %r13
jmp 0x13c4