// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x280, %rsp            # imm = 0x280
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq $0x0, -0x240(%rbp)
movl -0x14(%rbp), %edi
leaq -0x220(%rbp), %rsi
callq 0x3e90
movl %eax, -0x27c(%rbp)
cmpl $0x0, -0x27c(%rbp)
je 0x5c01
movq 0x1540e(%rip), %rax     # 0x1aff0
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0xdb05(%rip), %rsi      # 0x136f5
movb $0x0, %al
callq 0x3180
movl $0x1, %edi
callq 0x3240
leaq -0x220(%rbp), %rdi
callq 0x59d0
movl %eax, -0x274(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x59f0
movl %eax, -0x278(%rbp)
movzbl -0x186(%rbp), %eax
shll $0x10, %eax
movzbl -0x187(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x188(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x224(%rbp)
movl -0x224(%rbp), %eax
shlq $0x9, %rax
movl -0x278(%rbp), %ecx
imulq %rcx, %rax
movl -0x274(%rbp), %ecx
imulq %rcx, %rax
movq %rax, -0x248(%rbp)
movzbl -0x184(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x5cbc
movq -0x248(%rbp), %rax
addq -0x240(%rbp), %rax
movq %rax, -0x240(%rbp)
movl -0x224(%rbp), %esi
leaq 0xe2bd(%rip), %rdi      # 0x13f5d
movb $0x0, %al
callq 0x30c0
movq -0x248(%rbp), %rsi
leaq 0xe2de(%rip), %rdi      # 0x13f93
movb $0x0, %al
callq 0x30c0
movzbl -0x189(%rbp), %eax
shll $0x10, %eax
movzbl -0x18a(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x18b(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x224(%rbp)
movl -0x224(%rbp), %eax
shlq $0x9, %rax
movl -0x278(%rbp), %ecx
imulq %rcx, %rax
movl -0x274(%rbp), %ecx
imulq %rcx, %rax
movq %rax, -0x250(%rbp)
movzbl -0x184(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x5d53
movq -0x250(%rbp), %rax
addq -0x240(%rbp), %rax
movq %rax, -0x240(%rbp)
movl -0x224(%rbp), %esi
leaq 0xe26b(%rip), %rdi      # 0x13fa2
movb $0x0, %al
callq 0x30c0
movq -0x250(%rbp), %rsi
leaq 0xe247(%rip), %rdi      # 0x13f93
movb $0x0, %al
callq 0x30c0
movzbl -0x18c(%rbp), %eax
shll $0x10, %eax
movzbl -0x18d(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x18e(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x224(%rbp)
movl -0x224(%rbp), %eax
shlq $0x9, %rax
movl -0x278(%rbp), %ecx
imulq %rcx, %rax
movl -0x274(%rbp), %ecx
imulq %rcx, %rax
movq %rax, -0x258(%rbp)
movzbl -0x184(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x5dea
movq -0x258(%rbp), %rax
addq -0x240(%rbp), %rax
movq %rax, -0x240(%rbp)
movl -0x224(%rbp), %esi
leaq 0xe20a(%rip), %rdi      # 0x13fd8
movb $0x0, %al
callq 0x30c0
movq -0x258(%rbp), %rsi
leaq 0xe1b0(%rip), %rdi      # 0x13f93
movb $0x0, %al
callq 0x30c0
movzbl -0x18f(%rbp), %eax
shll $0x10, %eax
movzbl -0x190(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x191(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x224(%rbp)
movl -0x224(%rbp), %eax
shlq $0x9, %rax
movl -0x278(%rbp), %ecx
imulq %rcx, %rax
movl -0x274(%rbp), %ecx
imulq %rcx, %rax
movq %rax, -0x260(%rbp)
movzbl -0x184(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x5e81
movq -0x260(%rbp), %rax
addq -0x240(%rbp), %rax
movq %rax, -0x240(%rbp)
movl -0x224(%rbp), %esi
leaq 0xe1a9(%rip), %rdi      # 0x1400e
movb $0x0, %al
callq 0x30c0
movq -0x260(%rbp), %rsi
leaq 0xe119(%rip), %rdi      # 0x13f93
movb $0x0, %al
callq 0x30c0
movzbl -0x192(%rbp), %eax
shll $0x10, %eax
movzbl -0x193(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x194(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x224(%rbp)
movl -0x224(%rbp), %eax
shlq $0x9, %rax
movl -0x278(%rbp), %ecx
imulq %rcx, %rax
movl -0x274(%rbp), %ecx
imulq %rcx, %rax
movq %rax, -0x238(%rbp)
movzbl -0x184(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x5f18
movq -0x238(%rbp), %rax
addq -0x240(%rbp), %rax
movq %rax, -0x240(%rbp)
movl -0x224(%rbp), %esi
leaq 0xe148(%rip), %rdi      # 0x14044
movb $0x0, %al
callq 0x30c0
movq -0x238(%rbp), %rsi
leaq 0xe082(%rip), %rdi      # 0x13f93
movb $0x0, %al
callq 0x30c0
movzbl -0x181(%rbp), %eax
shll $0x10, %eax
movzbl -0x182(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x183(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x224(%rbp)
movl -0x224(%rbp), %eax
shlq $0x9, %rax
movl -0x278(%rbp), %ecx
imulq %rcx, %rax
movl -0x274(%rbp), %ecx
imulq %rcx, %rax
movq %rax, -0x230(%rbp)
movl -0x224(%rbp), %esi
leaq 0xe10b(%rip), %rdi      # 0x1407a
movb $0x0, %al
callq 0x30c0
movq -0x230(%rbp), %rsi
leaq 0xe00f(%rip), %rdi      # 0x13f93
movb $0x0, %al
callq 0x30c0
movq -0x240(%rbp), %rax
cmpq -0x230(%rbp), %rax
jbe 0x5fd1
movq 0x1504e(%rip), %rax     # 0x1aff0
movq (%rax), %rdi
movq -0x240(%rbp), %rdx
movq -0x230(%rbp), %rcx
movq -0x10(%rbp), %r8
leaq 0xe0f0(%rip), %rsi      # 0x140ae
movb $0x0, %al
callq 0x3180
movl $0x1, -0x4(%rbp)
jmp 0x605c
leaq -0x220(%rbp), %rdi
callq 0x5950
shrl %eax
movl %eax, %eax
movq %rax, -0x268(%rbp)
movq -0x248(%rbp), %rax
addq -0x250(%rbp), %rax
addq -0x258(%rbp), %rax
addq -0x260(%rbp), %rax
addq -0x238(%rbp), %rax
movq %rax, -0x270(%rbp)
movq -0x270(%rbp), %rax
cmpq -0x268(%rbp), %rax
jbe 0x6055
movq 0x14fc7(%rip), %rax     # 0x1aff0
movq (%rax), %rdi
movq -0x270(%rbp), %rdx
movq -0x268(%rbp), %rcx
movq -0x10(%rbp), %r8
leaq 0xe0bc(%rip), %rsi      # 0x14101
movb $0x0, %al
callq 0x3180
movl $0x1, -0x4(%rbp)
jmp 0x605c
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x280, %rsp            # imm = 0x280
popq %rbp
retq
nopl (%rax,%rax)