// File: /workspace/github_c_makefile_star2_O3/blasphemy[P]mmc-utils/mmc_cmds.c
// Function: do_enh_area_set
int do_enh_area_set(int nargs, char **argv)
{
	__u8 value;
	__u8 ext_csd[512];
	int fd, ret;
	char *device;
	int dry_run = 1;
	unsigned int start_kib, length_kib, enh_start_addr, enh_size_mult;
	unsigned long align;

	if (nargs != 5) {
		fprintf(stderr, "Usage: mmc enh_area set <-y|-n|-c> <start KiB> <length KiB> </path/to/mmcblkX>\n");
		exit(1);
	}

	if (!strcmp("-y", argv[1])) {
		dry_run = 0;
	} else if (!strcmp("-c", argv[1])) {
		dry_run = 2;
	}

	start_kib = strtol(argv[2], NULL, 10);
	length_kib = strtol(argv[3], NULL, 10);
	device = argv[4];

	fd = open(device, O_RDWR);
	if (fd < 0) {
		perror("open");
		exit(1);
	}

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

	/* assert ENH_ATTRIBUTE_EN */
	if (!(ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & EXT_CSD_ENH_ATTRIBUTE_EN))
	{
		printf(" Device cannot have enhanced tech.\n");
		exit(1);
	}

	/* assert not PARTITION_SETTING_COMPLETED */
	if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED])
	{
		printf(" Device is already partitioned\n");
		exit(1);
	}

	align = 512l * get_hc_wp_grp_size(ext_csd) * get_hc_erase_grp_size(ext_csd);

	enh_size_mult = (length_kib + align/2l) / align;

	enh_start_addr = start_kib * (1024 / (is_blockaddresed(ext_csd) ? 512 : 1));
	enh_start_addr /= align;
	enh_start_addr *= align;

	/* set EXT_CSD_ERASE_GROUP_DEF bit 0 */
	ret = write_extcsd_value(fd, EXT_CSD_ERASE_GROUP_DEF, 0x1);
	if (ret) {
		fprintf(stderr, "Could not write 0x1 to "
			"EXT_CSD[%d] in %s\n",
			EXT_CSD_ERASE_GROUP_DEF, device);
		exit(1);
	}

	/* write to ENH_START_ADDR and ENH_SIZE_MULT and PARTITIONS_ATTRIBUTE's ENH_USR bit */
	value = (enh_start_addr >> 24) & 0xff;
	ret = write_extcsd_value(fd, EXT_CSD_ENH_START_ADDR_3, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to "
			"EXT_CSD[%d] in %s\n", value,
			EXT_CSD_ENH_START_ADDR_3, device);
		exit(1);
	}
	value = (enh_start_addr >> 16) & 0xff;
	ret = write_extcsd_value(fd, EXT_CSD_ENH_START_ADDR_2, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to "
			"EXT_CSD[%d] in %s\n", value,
			EXT_CSD_ENH_START_ADDR_2, device);
		exit(1);
	}
	value = (enh_start_addr >> 8) & 0xff;
	ret = write_extcsd_value(fd, EXT_CSD_ENH_START_ADDR_1, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to "
			"EXT_CSD[%d] in %s\n", value,
			EXT_CSD_ENH_START_ADDR_1, device);
		exit(1);
	}
	value = enh_start_addr & 0xff;
	ret = write_extcsd_value(fd, EXT_CSD_ENH_START_ADDR_0, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to "
			"EXT_CSD[%d] in %s\n", value,
			EXT_CSD_ENH_START_ADDR_0, device);
		exit(1);
	}

	value = (enh_size_mult >> 16) & 0xff;
	ret = write_extcsd_value(fd, EXT_CSD_ENH_SIZE_MULT_2, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to "
			"EXT_CSD[%d] in %s\n", value,
			EXT_CSD_ENH_SIZE_MULT_2, device);
		exit(1);
	}
	value = (enh_size_mult >> 8) & 0xff;
	ret = write_extcsd_value(fd, EXT_CSD_ENH_SIZE_MULT_1, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to "
			"EXT_CSD[%d] in %s\n", value,
			EXT_CSD_ENH_SIZE_MULT_1, device);
		exit(1);
	}
	value = enh_size_mult & 0xff;
	ret = write_extcsd_value(fd, EXT_CSD_ENH_SIZE_MULT_0, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to "
			"EXT_CSD[%d] in %s\n", value,
			EXT_CSD_ENH_SIZE_MULT_0, device);
		exit(1);
	}
	value = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] | EXT_CSD_ENH_USR;
	ret = write_extcsd_value(fd, EXT_CSD_PARTITIONS_ATTRIBUTE, value);
	if (ret) {
		fprintf(stderr, "Could not write EXT_CSD_ENH_USR to "
			"EXT_CSD[%d] in %s\n",
			EXT_CSD_PARTITIONS_ATTRIBUTE, device);
		exit(1);
	}

	ret = check_enhanced_area_total_limit(device, fd);
	if (ret)
		exit(1);

	printf("Done setting ENH_USR area on %s\n", device);

	if (set_partitioning_setting_completed(dry_run, device, fd))
		exit(1);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
cmpl $0x5, %edi
jne 0x5abc
movq %rsi, %rbx
movq 0x8(%rsi), %rcx
movzbl (%rcx), %edx
leal -0x2d(%rdx), %eax
negl %eax
cmpl $0x2d, %edx
jne 0x5891
movzbl 0x1(%rcx), %esi
subl $0x79, %esi
jne 0x5873
movzbl 0x2(%rcx), %esi
negl %esi
testl %esi, %esi
je 0x5ab5
cmpb $0x2d, %dl
jne 0x5891
movzbl 0x1(%rcx), %eax
subl $0x63, %eax
jne 0x588f
movzbl 0x2(%rcx), %eax
negl %eax
xorl %ebp, %ebp
cmpl $0x1, %eax
adcl $0x1, %ebp
movq 0x10(%rbx), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x3180
movq %rax, %r15
movq 0x18(%rbx), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x3180
movq %rax, %r12
movq 0x20(%rbx), %rbx
movq %rbx, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x31d0
testl %eax, %eax
js 0x5ade
movl %eax, %r14d
movq %rsp, %rsi
movl %eax, %edi
callq 0x3eac
testl %eax, %eax
jne 0x5ae3
testb $0x2, 0xa0(%rsp)
je 0x5b08
cmpb $0x0, 0x9b(%rsp)
jne 0x5b11
movzbl 0xdd(%rsp), %eax
movzbl 0xe0(%rsp), %esi
imull %eax, %esi
shll $0x9, %esi
movl %r12d, %ecx
movl %esi, %eax
shrl %eax
addq %rcx, %rax
xorl %edx, %edx
divq %rsi
movq %rax, %r12
xorl %eax, %eax
cmpl $0x400001, 0xd4(%rsp) # imm = 0x400001
setb %al
leal (%rax,%rax,8), %ecx
incl %ecx
shll %cl, %r15d
movl %r15d, %eax
xorl %edx, %edx
divl %esi
movl %edx, %r13d
movl %r14d, %edi
movl $0xaf, %esi
movl $0x1, %edx
callq 0x3f3a
testl %eax, %eax
jne 0x5b29
subl %r13d, %r15d
movl %r15d, %r13d
shrl $0x18, %r13d
movl %r14d, %edi
movl $0x8b, %esi
movl %r13d, %edx
callq 0x3f3a
testl %eax, %eax
jne 0x5b49
movl %r15d, %r13d
shrl $0x10, %r13d
movzbl %r13b, %edx
movl %r14d, %edi
movl $0x8a, %esi
callq 0x3f3a
testl %eax, %eax
jne 0x5b6d
movl %r15d, %r13d
shrl $0x8, %r13d
movzbl %r13b, %edx
movl %r14d, %edi
movl $0x89, %esi
callq 0x3f3a
testl %eax, %eax
jne 0x5b92
movzbl %r15b, %edx
movl %r14d, %edi
movl $0x88, %esi
callq 0x3f3a
testl %eax, %eax
jne 0x5bb7
movq %r12, %r15
shrq $0x10, %r15
movzbl %r15b, %edx
movl %r14d, %edi
movl $0x8e, %esi
callq 0x3f3a
testl %eax, %eax
jne 0x5bd9
movq %r12, %r15
shrq $0x8, %r15
movzbl %r15b, %edx
movl %r14d, %edi
movl $0x8d, %esi
callq 0x3f3a
testl %eax, %eax
jne 0x5bfb
movzbl %r12b, %edx
movl %r14d, %edi
movl $0x8c, %esi
callq 0x3f3a
testl %eax, %eax
jne 0x5c1d
movzbl 0x9c(%rsp), %edx
orl $0x1, %edx
movl %r14d, %edi
movl $0x9c, %esi
callq 0x3f3a
testl %eax, %eax
jne 0x5c49
movq %rbx, %rdi
movl %r14d, %esi
callq 0x514d
testl %eax, %eax
jne 0x5c6e
leaq 0x98db(%rip), %rsi      # 0xf358
movl $0x1, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x31c0
movl %ebp, %edi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x5014
testl %eax, %eax
jne 0x5c6e
xorl %eax, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
jmp 0x5899
movq 0x1152d(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
leaq 0x97e1(%rip), %rdx      # 0xf2ae
movl $0x1, %esi
xorl %eax, %eax
callq 0x3240
jmp 0x5c6e
callq 0x33e8
movq 0x11506(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
leaq 0x8c11(%rip), %rdx      # 0xe705
movl $0x1, %esi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x3240
jmp 0x5c6e
leaq 0x97ef(%rip), %rsi      # 0xf2fe
jmp 0x5b18
leaq 0x9716(%rip), %rsi      # 0xf22e
movl $0x1, %edi
xorl %eax, %eax
callq 0x31c0
jmp 0x5c6e
movq 0x114c0(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
leaq 0x92d2(%rip), %rdx      # 0xee0c
movl $0x1, %esi
movl $0xaf, %ecx
jmp 0x5c64
movq 0x114a0(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
leaq 0x8c00(%rip), %rdx      # 0xe75a
movl $0x1, %esi
movl %r13d, %ecx
movl $0x8b, %r8d
jmp 0x5c3d
movq 0x1147c(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
movzbl %r13b, %ecx
leaq 0x8bd8(%rip), %rdx      # 0xe75a
movl $0x1, %esi
movl $0x8a, %r8d
jmp 0x5c3d
movq 0x11457(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
movzbl %r13b, %ecx
leaq 0x8bb3(%rip), %rdx      # 0xe75a
movl $0x1, %esi
movl $0x89, %r8d
jmp 0x5c3d
movq 0x11432(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
movzbl %r15b, %ecx
leaq 0x8b8e(%rip), %rdx      # 0xe75a
movl $0x1, %esi
movl $0x88, %r8d
jmp 0x5c3d
movq 0x11410(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
movzbl %r15b, %ecx
leaq 0x8b6c(%rip), %rdx      # 0xe75a
movl $0x1, %esi
movl $0x8e, %r8d
jmp 0x5c3d
movq 0x113ee(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
movzbl %r15b, %ecx
leaq 0x8b4a(%rip), %rdx      # 0xe75a
movl $0x1, %esi
movl $0x8d, %r8d
jmp 0x5c3d
movq 0x113cc(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
movzbl %r12b, %ecx
leaq 0x8b28(%rip), %rdx      # 0xe75a
movl $0x1, %esi
movl $0x8c, %r8d
movq %rbx, %r9
xorl %eax, %eax
callq 0x3240
jmp 0x5c6e
movq 0x113a0(%rip), %rax     # 0x16ff0
movq (%rax), %rdi
leaq 0x96c8(%rip), %rdx      # 0xf322
movl $0x1, %esi
movl $0x9c, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0x3240
movl $0x1, %edi
callq 0x3220