// File: /workspace/github_c_makefile_star2_O2/blasphemy[P]mmc-utils/mmc_cmds.c
// Function: do_writeprotect_user_set
int do_writeprotect_user_set(int nargs, char **argv)
{
	__u8 ext_csd[512];
	int fd, ret;
	char *device;
	int blk_start;
	int blk_cnt;
	__u32 wp_blks;
	__u8 user_wp;
	int x;
	int wptype;

	if (nargs != 5)
		goto usage;
	device = argv[4];
	fd = open(device, O_RDWR);
	if (fd < 0) {
		perror("open");
		exit(1);
	}
	if (!strcmp(argv[1], "none")) {
		wptype = WPTYPE_NONE;
	} else if (!strcmp(argv[1], "temp")) {
		wptype = WPTYPE_TEMP;
	} else if (!strcmp(argv[1], "pwron")) {
		wptype = WPTYPE_PWRON;
#ifdef DANGEROUS_COMMANDS_ENABLED
	} else if (!strcmp(argv[1], "perm")) {
		wptype = WPTYPE_PERM;
#endif /* DANGEROUS_COMMANDS_ENABLED */
	} else {
		fprintf(stderr, "Error, invalid \"type\"\n");
		goto usage;
	}
	ret = read_extcsd(fd, ext_csd);
	if (ret) {
		fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
		exit(1);
	}
	ret = get_wp_group_size_in_blks(ext_csd, &wp_blks);
	if (ret) {
		fprintf(stderr, "Operation not supported for this device\n");
		exit(1);
	}
	blk_start = strtol(argv[2], NULL, 0);
	blk_cnt = strtol(argv[3], NULL, 0);
	if ((blk_start % wp_blks) || (blk_cnt % wp_blks)) {
		fprintf(stderr, "<start block> and <blocks> must be a ");
		fprintf(stderr, "multiple of the Write Protect Group (%d)\n",
			wp_blks);
		exit(1);
	}
	if (wptype != WPTYPE_NONE) {
		user_wp = ext_csd[EXT_CSD_USER_WP];
		user_wp &= ~USER_WP_CLEAR;
		switch (wptype) {
		case WPTYPE_TEMP:
			break;
		case WPTYPE_PWRON:
			user_wp |= USER_WP_US_PWR_WP_EN;
			break;
		case WPTYPE_PERM:
			user_wp |= USER_WP_US_PERM_WP_EN;
			break;
		}
		if (user_wp != ext_csd[EXT_CSD_USER_WP]) {
			ret = write_extcsd_value(fd, EXT_CSD_USER_WP, user_wp);
			if (ret) {
				fprintf(stderr, "Error setting EXT_CSD\n");
				exit(1);
			}
		}
	}
	for (x = 0; x < blk_cnt; x += wp_blks) {
		ret = set_write_protect(fd, blk_start + x,
					wptype != WPTYPE_NONE);
		if (ret) {
			fprintf(stderr,
				"Could not set write protect for %s\n", device);
			exit(1);
		}
	}
	if (wptype != WPTYPE_NONE) {
		ret = write_extcsd_value(fd, EXT_CSD_USER_WP,
					ext_csd[EXT_CSD_USER_WP]);
		if (ret) {
			fprintf(stderr, "Error restoring EXT_CSD\n");
			exit(1);
		}
	}
	return ret;

usage:
	fprintf(stderr,
		"Usage: mmc writeprotect user set <type><start block><blocks><device>\n");
	exit(1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
cmpl $0x5, %edi
jne 0x415b
movq %rsi, %r12
movq 0x20(%rsi), %rbp
pushq $0x2
popq %rsi
movq %rbp, %rdi
xorl %eax, %eax
callq 0x31e0
movl %eax, (%rsp)
testl %eax, %eax
js 0x4355
movq 0x8(%r12), %rbx
leaq 0xa6e4(%rip), %rsi      # 0xe7ed
movq %rbx, %rdi
callq 0x3150
movl %eax, %r14d
testl %eax, %eax
je 0x4171
leaq 0xa6d3(%rip), %rsi      # 0xe7f2
movq %rbx, %rdi
callq 0x3150
testl %eax, %eax
je 0x4171
leaq 0xa6c5(%rip), %rsi      # 0xe7f7
movq %rbx, %rdi
callq 0x3150
movb $0x1, %bl
testl %eax, %eax
je 0x4173
movq 0x13ea9(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0xa6ac(%rip), %rdx      # 0xe7fd
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x3250
movq 0x13e8e(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0xa775(%rip), %rdx      # 0xe8e1
jmp 0x43a8
xorl %ebx, %ebx
leaq 0x60(%rsp), %rsi
movl (%rsp), %edi
callq 0x39b8
testl %eax, %eax
jne 0x4369
cmpb $0x5, 0x120(%rsp)
jb 0x4384
cmpb $0x0, 0x10f(%rsp)
je 0x4384
movzbl 0x140(%rsp), %eax
movzbl 0x13d(%rsp), %r15d
imull %eax, %r15d
shll $0xa, %r15d
movq 0x10(%r12), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3180
movq %rax, %r13
movq 0x18(%r12), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3180
movq %rax, %r12
movl %r13d, %eax
xorl %edx, %edx
divl %r15d
testl %edx, %edx
jne 0x4320
movl %r12d, %eax
xorl %edx, %edx
divl %r15d
testl %edx, %edx
jne 0x4320
movl %r14d, 0x4(%rsp)
testl %r14d, %r14d
movq %rbp, 0x8(%rsp)
je 0x424d
movb 0x10b(%rsp), %cl
movl %ecx, %eax
andb $-0x1e, %al
orb %bl, %al
pushq $0x1c
popq %r14
cmpb %cl, %al
je 0x4251
movzbl %al, %edx
movl (%rsp), %edi
movl $0xab, %esi
callq 0x3a43
testl %eax, %eax
je 0x4251
movq 0x13db2(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0xa645(%rip), %rdx      # 0xe88d
jmp 0x43a8
pushq $0x1d
popq %r14
leaq 0x10(%rsp), %rbx
xorl %ebp, %ebp
cmpl %r12d, %ebp
jge 0x42b3
leal (%r13,%rbp), %eax
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x50(%rsp)
movl $0x1, 0x10(%rsp)
movl %r14d, 0x18(%rsp)
movl %eax, 0x1c(%rsp)
movl $0x49d, 0x30(%rsp)      # imm = 0x49D
movl $0xc048b300, %esi       # imm = 0xC048B300
movl (%rsp), %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x30f0
testl %eax, %eax
jne 0x42eb
addl %r15d, %ebp
jmp 0x4258
cmpl $0x0, 0x4(%rsp)
je 0x42d7
movzbl 0x10b(%rsp), %edx
movl (%rsp), %edi
movl $0xab, %esi
callq 0x3a43
testl %eax, %eax
jne 0x4397
xorl %eax, %eax
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd276(%rip), %rdi      # 0x11568
callq 0x31f0
movq 0x13cf2(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0xa59c(%rip), %rdx      # 0xe8a4
pushq $0x1
popq %rbx
movl %ebx, %esi
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x3250
movl %ebx, %edi
callq 0x3230
movq 0x13cc9(%rip), %r14     # 0x17ff0
movq (%r14), %rdi
leaq 0xa50c(%rip), %rdx      # 0xe83d
pushq $0x1
popq %rbx
movl %ebx, %esi
xorl %eax, %eax
callq 0x3250
movq (%r14), %rdi
leaq 0xa51c(%rip), %rdx      # 0xe863
movl %ebx, %esi
movl %r15d, %ecx
xorl %eax, %eax
callq 0x3250
jmp 0x4319
leaq 0xcc89(%rip), %rdi      # 0x10fe5
callq 0x31f0
pushq $0x1
popq %rdi
callq 0x3230
movq 0x13c80(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0xa38b(%rip), %rdx      # 0xe705
pushq $0x1
popq %rbx
movl %ebx, %esi
movq %rbp, %rcx
jmp 0x4312
movq 0x13c65(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0xa47f(%rip), %rdx      # 0xe814
jmp 0x43a8
movq 0x13c52(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0xa520(%rip), %rdx      # 0xe8c8
pushq $0x1
popq %rbx
movl %ebx, %esi
xorl %eax, %eax
callq 0x3250
jmp 0x4319