// File: /workspace/github_c_makefile_star2_O3/blasphemy[P]mmc-utils/mmc_cmds.c
// Function: check_enhanced_area_total_limit
int check_enhanced_area_total_limit(const char * const device, int fd)
{
	__u8 ext_csd[512];
	__u32 regl;
	unsigned long max_enh_area_sz, user_area_sz, enh_area_sz = 0;
	unsigned long gp4_part_sz, gp3_part_sz, gp2_part_sz, gp1_part_sz;
	unsigned long total_sz, total_gp_user_sz;
	unsigned int wp_sz, erase_sz;
	int ret;

	ret = read_extcsd(fd, ext_csd);
	if (ret) {
		fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
		exit(1);
	}
	wp_sz = get_hc_wp_grp_size(ext_csd);
	erase_sz = get_hc_erase_grp_size(ext_csd);

	regl = (ext_csd[EXT_CSD_GP_SIZE_MULT_4_2] << 16) |
		(ext_csd[EXT_CSD_GP_SIZE_MULT_4_1] << 8) |
		ext_csd[EXT_CSD_GP_SIZE_MULT_4_0];
	gp4_part_sz = 512l * regl * erase_sz * wp_sz;
	if (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & EXT_CSD_ENH_4) {
		enh_area_sz += gp4_part_sz;
		printf("Enhanced GP4 Partition Size [GP_SIZE_MULT_4]: 0x%06x\n", regl);
		printf(" i.e. %lu KiB\n", gp4_part_sz);
	}

	regl = (ext_csd[EXT_CSD_GP_SIZE_MULT_3_2] << 16) |
		(ext_csd[EXT_CSD_GP_SIZE_MULT_3_1] << 8) |
		ext_csd[EXT_CSD_GP_SIZE_MULT_3_0];
	gp3_part_sz = 512l * regl * erase_sz * wp_sz;
	if (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & EXT_CSD_ENH_3) {
		enh_area_sz += gp3_part_sz;
		printf("Enhanced GP3 Partition Size [GP_SIZE_MULT_3]: 0x%06x\n", regl);
		printf(" i.e. %lu KiB\n", gp3_part_sz);
	}

	regl = (ext_csd[EXT_CSD_GP_SIZE_MULT_2_2] << 16) |
		(ext_csd[EXT_CSD_GP_SIZE_MULT_2_1] << 8) |
		ext_csd[EXT_CSD_GP_SIZE_MULT_2_0];
	gp2_part_sz = 512l * regl * erase_sz * wp_sz;
	if (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & EXT_CSD_ENH_2) {
		enh_area_sz += gp2_part_sz;
		printf("Enhanced GP2 Partition Size [GP_SIZE_MULT_2]: 0x%06x\n", regl);
		printf(" i.e. %lu KiB\n", gp2_part_sz);
	}

	regl = (ext_csd[EXT_CSD_GP_SIZE_MULT_1_2] << 16) |
		(ext_csd[EXT_CSD_GP_SIZE_MULT_1_1] << 8) |
		ext_csd[EXT_CSD_GP_SIZE_MULT_1_0];
	gp1_part_sz = 512l * regl * erase_sz * wp_sz;
	if (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & EXT_CSD_ENH_1) {
		enh_area_sz += gp1_part_sz;
		printf("Enhanced GP1 Partition Size [GP_SIZE_MULT_1]: 0x%06x\n", regl);
		printf(" i.e. %lu KiB\n", gp1_part_sz);
	}

	regl = (ext_csd[EXT_CSD_ENH_SIZE_MULT_2] << 16) |
		(ext_csd[EXT_CSD_ENH_SIZE_MULT_1] << 8) |
		ext_csd[EXT_CSD_ENH_SIZE_MULT_0];
	user_area_sz = 512l * regl * erase_sz * wp_sz;
	if (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & EXT_CSD_ENH_USR) {
		enh_area_sz += user_area_sz;
		printf("Enhanced User Data Area Size [ENH_SIZE_MULT]: 0x%06x\n", regl);
		printf(" i.e. %lu KiB\n", user_area_sz);
	}

	regl = (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT_2] << 16) |
		(ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT_1] << 8) |
		ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT_0];
	max_enh_area_sz = 512l * regl * erase_sz * wp_sz;
	printf("Max Enhanced Area Size [MAX_ENH_SIZE_MULT]: 0x%06x\n", regl);
	printf(" i.e. %lu KiB\n", max_enh_area_sz);
	if (enh_area_sz > max_enh_area_sz) {
		fprintf(stderr,
			"Programmed total enhanced size %lu KiB cannot exceed max enhanced area %lu KiB %s\n",
			enh_area_sz, max_enh_area_sz, device);
		return 1;
	}
	total_sz = get_sector_count(ext_csd) / 2;
	total_gp_user_sz = gp4_part_sz + gp3_part_sz + gp2_part_sz +
				gp1_part_sz + user_area_sz;
	if (total_gp_user_sz > total_sz) {
		fprintf(stderr,
			"requested total partition size %lu KiB cannot exceed card capacity %lu KiB %s\n",
			total_gp_user_sz, total_sz, device);
		return 1;
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movl %esi, %eax
movq %rdi, %rbx
leaq 0x20(%rsp), %rsi
movl %eax, %edi
callq 0x3eac
testl %eax, %eax
jne 0x541d
movzbl 0xfd(%rsp), %eax
movzbl 0x100(%rsp), %r14d
imull %eax, %r14d
movzwl 0xb9(%rsp), %eax
shll $0x8, %eax
movzbl 0xb8(%rsp), %edx
orl %eax, %edx
shll $0x9, %r14d
movq %r14, %r12
imulq %rdx, %r12
movb 0xbc(%rsp), %al
testb $0x10, %al
movq %r12, 0x18(%rsp)
jne 0x51c1
xorl %r12d, %r12d
jmp 0x51f1
leaq 0x9d9c(%rip), %rsi      # 0xef64
movl $0x1, %edi
xorl %eax, %eax
callq 0x31c0
leaq 0x9dbf(%rip), %rsi      # 0xef9a
movl $0x1, %edi
movq %r12, %rdx
xorl %eax, %eax
callq 0x31c0
movb 0xbc(%rsp), %al
movzwl 0xb6(%rsp), %ecx
shll $0x8, %ecx
movzbl 0xb5(%rsp), %edx
orl %ecx, %edx
movq %r14, %r15
imulq %rdx, %r15
testb $0x8, %al
je 0x5244
addq %r15, %r12
leaq 0x9d8e(%rip), %rsi      # 0xefa9
movl $0x1, %edi
xorl %eax, %eax
callq 0x31c0
leaq 0x9d6c(%rip), %rsi      # 0xef9a
movl $0x1, %edi
movq %r15, %rdx
xorl %eax, %eax
callq 0x31c0
movb 0xbc(%rsp), %al
movzwl 0xb3(%rsp), %ecx
shll $0x8, %ecx
movzbl 0xb2(%rsp), %edx
orl %ecx, %edx
movq %r14, %rbp
imulq %rdx, %rbp
testb $0x4, %al
movq %rbx, 0x8(%rsp)
je 0x529c
addq %rbp, %r12
leaq 0x9d6c(%rip), %rsi      # 0xefdf
movl $0x1, %edi
xorl %eax, %eax
callq 0x31c0
leaq 0x9d14(%rip), %rsi      # 0xef9a
movl $0x1, %edi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x31c0
movb 0xbc(%rsp), %al
movzwl 0xb0(%rsp), %ecx
shll $0x8, %ecx
movzbl 0xaf(%rsp), %edx
orl %ecx, %edx
movq %r14, %rbx
imulq %rdx, %rbx
testb $0x2, %al
movq %r15, 0x10(%rsp)
je 0x52f4
addq %rbx, %r12
leaq 0x9d4a(%rip), %rsi      # 0xf015
movl $0x1, %edi
xorl %eax, %eax
callq 0x31c0
leaq 0x9cbc(%rip), %rsi      # 0xef9a
movl $0x1, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x31c0
movb 0xbc(%rsp), %al
movzwl 0xad(%rsp), %ecx
shll $0x8, %ecx
movzbl 0xac(%rsp), %edx
orl %ecx, %edx
movq %r14, %r15
imulq %rdx, %r15
testb $0x1, %al
je 0x5340
addq %r15, %r12
leaq 0x9d2d(%rip), %rsi      # 0xf04b
movl $0x1, %edi
xorl %eax, %eax
callq 0x31c0
leaq 0x9c69(%rip), %rsi      # 0xef9a
movl $0x1, %edi
movq %r15, %rdx
xorl %eax, %eax
callq 0x31c0
movzwl 0xbe(%rsp), %eax
shll $0x8, %eax
movzbl 0xbd(%rsp), %edx
orl %eax, %edx
imulq %rdx, %r14
leaq 0x9d21(%rip), %rsi      # 0xf081
xorl %r13d, %r13d
movl $0x1, %edi
xorl %eax, %eax
callq 0x31c0
leaq 0x9c24(%rip), %rsi      # 0xef9a
movl $0x1, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x31c0
cmpq %r14, %r12
jbe 0x53ba
movq 0x11c5f(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
leaq 0x9d1a(%rip), %rdx      # 0xf0b5
movl $0x1, %esi
movq %r12, %rcx
movq %r14, %r8
movq 0x8(%rsp), %r9
xorl %eax, %eax
callq 0x3240
movl $0x1, %r13d
jmp 0x5408
movl 0xf4(%rsp), %r8d
shrl %r8d
movq 0x10(%rsp), %rax
addq 0x18(%rsp), %rax
addq %rbx, %rbp
addq %rax, %rbp
addq %r15, %rbp
cmpq %r8, %rbp
jbe 0x5408
movq 0x11c0c(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
leaq 0x9d1a(%rip), %rdx      # 0xf108
movl $0x1, %r13d
movl $0x1, %esi
movq %rbp, %rcx
movq 0x8(%rsp), %r9
xorl %eax, %eax
callq 0x3240
movl %r13d, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x11bcc(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
leaq 0x92d7(%rip), %rdx      # 0xe705
movl $0x1, %esi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x3240
movl $0x1, %edi
callq 0x3220