// File: /workspace/github_c_makefile_star2_O2/blasphemy[P]mmc-utils/mmc_cmds.c
// Function: do_create_gp_partition
int do_create_gp_partition(int nargs, char **argv)
{
	__u8 value;
	__u8 ext_csd[512];
	__u8 address;
	int fd, ret;
	char *device;
	int dry_run = 1;
	int partition, enh_attr, ext_attr;
	unsigned int length_kib, gp_size_mult;
	unsigned long align;

	if (nargs != 7) {
		fprintf(stderr, "Usage: mmc gp create <-y|-n|-c> <length KiB> <partition> <enh_attr> <ext_attr> </path/to/mmcblkX>\n");
		exit(1);
	}

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

	length_kib = strtol(argv[2], NULL, 10);
	partition = strtol(argv[3], NULL, 10);
	enh_attr = strtol(argv[4], NULL, 10);
	ext_attr = strtol(argv[5], NULL, 10);
	device = argv[6];

	if (partition < 1 || partition > 4) {
		printf("Invalid gp partition number; valid range [1-4].\n");
		exit(1);
	}

	if (enh_attr && ext_attr) {
		printf("Not allowed to set both enhanced attribute and extended attribute\n");
		exit(1);
	}

	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 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);
	gp_size_mult = (length_kib + align/2l) / 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);
	}

	value = (gp_size_mult >> 16) & 0xff;
	address = EXT_CSD_GP_SIZE_MULT_1_2 + (partition - 1) * 3;
	ret = write_extcsd_value(fd, address, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to EXT_CSD[%d] in %s\n",
			value, address, device);
		exit(1);
	}
	value = (gp_size_mult >> 8) & 0xff;
	address = EXT_CSD_GP_SIZE_MULT_1_1 + (partition - 1) * 3;
	ret = write_extcsd_value(fd, address, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to EXT_CSD[%d] in %s\n",
			value, address, device);
		exit(1);
	}
	value = gp_size_mult & 0xff;
	address = EXT_CSD_GP_SIZE_MULT_1_0 + (partition - 1) * 3;
	ret = write_extcsd_value(fd, address, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%02x to EXT_CSD[%d] in %s\n",
			value, address, device);
		exit(1);
	}

	value = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
	if (enh_attr)
		value |= (1 << partition);
	else
		value &= ~(1 << partition);

	ret = write_extcsd_value(fd, EXT_CSD_PARTITIONS_ATTRIBUTE, value);
	if (ret) {
		fprintf(stderr, "Could not write EXT_CSD_ENH_%x to EXT_CSD[%d] in %s\n",
			partition, EXT_CSD_PARTITIONS_ATTRIBUTE, device);
		exit(1);
	}

	address = EXT_CSD_EXT_PARTITIONS_ATTRIBUTE_0 + (partition - 1) / 2;
	value = ext_csd[address];
	if (ext_attr)
		value |= (ext_attr << (4 * ((partition - 1) % 2)));
	else
		value &= (0xF << (4 * ((partition % 2))));

	ret = write_extcsd_value(fd, address, value);
	if (ret) {
		fprintf(stderr, "Could not write 0x%x to EXT_CSD[%d] in %s\n",
			value, address, device);
		exit(1);
	}

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

	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 $0x238, %rsp            # imm = 0x238
cmpl $0x7, %edi
jne 0x5274
movq %rsi, %rbx
movq 0x8(%rsi), %rcx
movzbl (%rcx), %edx
leal -0x2d(%rdx), %eax
negl %eax
cmpl $0x2d, %edx
jne 0x4fe7
movzbl 0x1(%rcx), %esi
subl $0x79, %esi
jne 0x4fc9
movzbl 0x2(%rcx), %esi
negl %esi
testl %esi, %esi
je 0x5248
cmpb $0x2d, %dl
jne 0x4fe7
movzbl 0x1(%rcx), %eax
subl $0x63, %eax
jne 0x4fe5
movzbl 0x2(%rcx), %eax
negl %eax
xorl %r12d, %r12d
cmpl $0x1, %eax
adcl $0x1, %r12d
movq 0x10(%rbx), %rdi
pushq $0xa
popq %r15
xorl %esi, %esi
movl %r15d, %edx
callq 0x3180
movq %rax, 0x8(%rsp)
movq 0x18(%rbx), %rdi
xorl %esi, %esi
movl %r15d, %edx
callq 0x3180
movq %rax, %r14
movq 0x20(%rbx), %rdi
xorl %esi, %esi
movl %r15d, %edx
callq 0x3180
movq %rax, %rbp
movq 0x28(%rbx), %rdi
xorl %esi, %esi
movl %r15d, %edx
callq 0x3180
movq %rax, %r13
movq %r14, 0x10(%rsp)
leal -0x5(%r14), %eax
cmpl $-0x5, %eax
jbe 0x5296
movq %rbp, 0x28(%rsp)
testl %ebp, %ebp
setne %al
testl %r13d, %r13d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
je 0x529f
movq 0x30(%rbx), %r14
pushq $0x2
popq %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x31e0
testl %eax, %eax
js 0x52a8
movl %eax, %r15d
leaq 0x30(%rsp), %rsi
movl %eax, %edi
callq 0x39b8
testl %eax, %eax
jne 0x52bc
cmpb $0x0, 0xcb(%rsp)
jne 0x52e1
movq %r13, 0x20(%rsp)
movl %r12d, 0x1c(%rsp)
movq %r14, (%rsp)
movzbl 0x10d(%rsp), %eax
shll $0x9, %eax
movzbl 0x110(%rsp), %ecx
imulq %rax, %rcx
movl 0x8(%rsp), %edx
movl %ecx, %eax
shrl %eax
addq %rdx, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %r13
pushq $0x1
popq %rbp
movl %r15d, %edi
movl $0xaf, %esi
movl %ebp, %edx
callq 0x3a43
testl %eax, %eax
jne 0x52f6
movq %r13, %r12
shrq $0x10, %r13
movq 0x10(%rsp), %rax
leal -0x1(%rax), %ebx
leal (%rbx,%rbx,2), %r14d
leal -0x6f(%r14), %eax
movzbl %al, %esi
movzbl %r13b, %edx
movl %r15d, %edi
movl %esi, 0x8(%rsp)
callq 0x3a43
testl %eax, %eax
jne 0x5250
movq %r12, %r13
shrq $0x8, %r13
leal -0x70(%r14), %eax
movzbl %al, %esi
movzbl %r13b, %edx
movl %r15d, %edi
movl %esi, 0x8(%rsp)
callq 0x3a43
testl %eax, %eax
jne 0x5250
addb $-0x71, %r14b
movq %r12, %r13
movzbl %r13b, %edx
movzbl %r14b, %r12d
movl %r15d, %edi
movl %r12d, %esi
callq 0x3a43
testl %eax, %eax
jne 0x531e
movq 0x10(%rsp), %rcx
shll %cl, %ebp
movb 0xcc(%rsp), %al
movl %eax, %ecx
orb %bpl, %cl
notb %bpl
andb %al, %bpl
cmpl $0x0, 0x28(%rsp)
movzbl %cl, %eax
movzbl %bpl, %ecx
cmovnel %eax, %ecx
movzbl %cl, %edx
movl %r15d, %edi
movl $0x9c, %esi
callq 0x3a43
testl %eax, %eax
movq 0x20(%rsp), %rsi
jne 0x533d
movl %ebx, %eax
shrl %eax
movzbl %al, %ecx
leaq 0x34(%rcx), %rax
movb 0x64(%rsp,%rcx), %dl
testl %esi, %esi
movq (%rsp), %r12
je 0x51dd
shll $0x2, %ebx
andb $0x4, %bl
movl %ebx, %ecx
shll %cl, %esi
orb %sil, %dl
jmp 0x51f0
movq 0x10(%rsp), %rcx
shll $0x2, %ecx
andb $0x4, %cl
pushq $0xf
popq %rsi
shll %cl, %esi
andb %sil, %dl
movzbl %al, %ebx
movzbl %dl, %ebp
movl %r15d, %edi
movl %ebx, %esi
movl %ebp, %edx
callq 0x3a43
testl %eax, %eax
jne 0x5370
movq %r12, %rdi
movl %r15d, %esi
callq 0x4cac
testl %eax, %eax
jne 0x52b4
movl 0x1c(%rsp), %edi
movq %r12, %rsi
movl %r15d, %edx
callq 0x4b8b
testl %eax, %eax
jne 0x52b4
xorl %eax, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r12d, %r12d
jmp 0x4ff1
movq 0x12d99(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
movzbl %r13b, %ecx
leaq 0x94f5(%rip), %rdx      # 0xe75a
pushq $0x1
popq %rbx
movl %ebx, %esi
movl 0x8(%rsp), %r8d
jmp 0x535e
movq 0x12d75(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0x9ed2(%rip), %rdx      # 0xf157
pushq $0x1
popq %rbx
movl %ebx, %esi
xorl %eax, %eax
callq 0x3250
jmp 0x5369
leaq 0x9f1d(%rip), %rsi      # 0xf1ba
jmp 0x52e8
leaq 0x9f45(%rip), %rsi      # 0xf1eb
jmp 0x52e8
leaq 0xbd36(%rip), %rdi      # 0x10fe5
callq 0x31f0
pushq $0x1
popq %rdi
callq 0x3230
movq 0x12d2d(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0x9438(%rip), %rdx      # 0xe705
pushq $0x1
popq %rbx
movl %ebx, %esi
movq %r14, %rcx
xorl %eax, %eax
callq 0x3250
jmp 0x5369
leaq 0x9f46(%rip), %rsi      # 0xf22e
pushq $0x1
popq %rbx
movl %ebx, %edi
xorl %eax, %eax
callq 0x31d0
jmp 0x5369
movq 0x12cf3(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0x9b05(%rip), %rdx      # 0xee0c
pushq $0x1
popq %rbx
movl %ebx, %esi
movl $0xaf, %ecx
movq (%rsp), %r8
xorl %eax, %eax
callq 0x3250
jmp 0x5369
movq 0x12ccb(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
movzbl %r13b, %ecx
leaq 0x9427(%rip), %rdx      # 0xe75a
pushq $0x1
popq %rbx
movl %ebx, %esi
movl %r12d, %r8d
jmp 0x535e
movq 0x12cac(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0x9f00(%rip), %rdx      # 0xf24e
pushq $0x1
popq %rbx
movl %ebx, %esi
movq 0x10(%rsp), %rcx
movl $0x9c, %r8d
movq (%rsp), %r9
xorl %eax, %eax
callq 0x3250
movl %ebx, %edi
callq 0x3230
movq 0x12c79(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0x9f02(%rip), %rdx      # 0xf283
pushq $0x1
popq %r14
movl %r14d, %esi
movl %ebp, %ecx
movl %ebx, %r8d
movq %r12, %r9
xorl %eax, %eax
callq 0x3250
movl %r14d, %edi
callq 0x3230