// File: /workspace/github_c_makefile_star2_O1/blasphemy[P]mmc-utils/mmc_cmds.c
// Function: do_rpmb_write_key
int do_rpmb_write_key(int nargs, char **argv)
{
	int ret, dev_fd, key_fd;
	struct rpmb_frame frame_in = {
		.req_resp = htobe16(MMC_RPMB_WRITE_KEY)
	}, frame_out;

	if (nargs != 3) {
		fprintf(stderr, "Usage: mmc rpmb write-key </path/to/mmcblkXrpmb> </path/to/key>\n");
		exit(1);
	}

	dev_fd = open(argv[1], O_RDWR);
	if (dev_fd < 0) {
		perror("device open");
		exit(1);
	}

	if (0 == strcmp(argv[2], "-"))
		key_fd = STDIN_FILENO;
	else {
		key_fd = open(argv[2], O_RDONLY);
		if (key_fd < 0) {
			perror("can't open key file");
			exit(1);
		}
	}

	/* Read the auth key */
	ret = DO_IO(read, key_fd, frame_in.key_mac, sizeof(frame_in.key_mac));
	if (ret < 0) {
		perror("read the key");
		exit(1);
	} else if (ret != sizeof(frame_in.key_mac)) {
		printf("Auth key must be %lu bytes length, but we read only %d, exit\n",
			   (unsigned long)sizeof(frame_in.key_mac),
			   ret);
		exit(1);
	}

	/* Execute RPMB op */
	ret = do_rpmb_op(dev_fd, &frame_in, &frame_out, 1);
	if (ret != 0) {
		perror("RPMB ioctl failed");
		exit(1);
	}

	/* Check RPMB response */
	if (frame_out.result != 0) {
		printf("RPMB operation failed, retcode 0x%04x\n",
			   be16toh(frame_out.result));
		exit(1);
	}

	close(dev_fd);
	if (key_fd != STDIN_FILENO)
		close(key_fd);

	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x400, %rsp            # imm = 0x400
movq %rsi, %r14
movl %edi, %ebx
movq %rsp, %r15
movl $0x200, %edx            # imm = 0x200
movq %r15, %rdi
xorl %esi, %esi
callq 0x30e0
movw $0x100, 0x1fe(%r15)     # imm = 0x100
cmpl $0x3, %ebx
jne 0x723b
movq 0x8(%r14), %rdi
xorl %ebx, %ebx
movl $0x2, %esi
xorl %eax, %eax
callq 0x31d0
testl %eax, %eax
js 0x725a
movl %eax, %ebp
movq 0x10(%r14), %r14
leaq 0x7195(%rip), %rsi      # 0xe2fd
movq %r14, %rdi
callq 0x3150
testl %eax, %eax
je 0x718a
movq %r14, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x31d0
movl %eax, %ebx
testl %eax, %eax
js 0x72aa
leaq 0xc4(%rsp), %r12
xorl %r14d, %r14d
leaq (%r12,%r14), %rsi
movl $0x20, %edx
subq %r14, %rdx
movl %ebx, %edi
callq 0x3110
movq %rax, %r15
testq %rax, %rax
jns 0x71ba
callq 0x3050
cmpl $0x4, (%rax)
jne 0x71d6
testq %r15, %r15
movl $0x0, %eax
cmovgq %r15, %rax
addq %rax, %r14
testq %r15, %r15
je 0x71dd
cmpq $0x20, %r14
jne 0x7195
jmp 0x71dd
movq $-0x1, %r14
testl %r14d, %r14d
js 0x725f
movl %r14d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x20, %eax
jne 0x7264
movq %rsp, %rsi
leaq 0x200(%rsp), %rdx
movl %ebp, %edi
movl $0x1, %ecx
callq 0x72af
testl %eax, %eax
jne 0x7281
movzwl 0x3fc(%rsp), %eax
testw %ax, %ax
jne 0x7286
movl %ebp, %edi
callq 0x3100
testl %ebx, %ebx
je 0x7229
movl %ebx, %edi
callq 0x3100
xorl %eax, %eax
addq $0x400, %rsp            # imm = 0x400
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0xfdae(%rip), %rax      # 0x16ff0
movq (%rax), %rdi
leaq 0x9d60(%rip), %rdx      # 0x10fac
movl $0x1, %esi
xorl %eax, %eax
callq 0x3240
jmp 0x72a0
callq 0x3467
callq 0x3452
leaq 0x9daf(%rip), %rsi      # 0x1101a
movl $0x20, %edx
movl $0x1, %edi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x31c0
jmp 0x72a0
callq 0x343d
rolw $0x8, %ax
movzwl %ax, %edx
leaq 0x9dd6(%rip), %rsi      # 0x1106a
movl $0x1, %edi
xorl %eax, %eax
callq 0x31c0
movl $0x1, %edi
callq 0x3220
callq 0x3428