// File: /workspace/github_c_makefile_star2_O3/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 0x718c
movq 0x8(%r14), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x31d0
testl %eax, %eax
js 0x71ab
movl %eax, %ebx
movq 0x10(%r14), %rdi
cmpb $0x2d, (%rdi)
jne 0x70c5
cmpb $0x0, 0x1(%rdi)
je 0x7180
xorl %esi, %esi
xorl %eax, %eax
callq 0x31d0
movl %eax, %ebp
testl %eax, %eax
js 0x71f6
leaq 0xc4(%rsp), %r12
xorl %r14d, %r14d
leaq (%r12,%r14), %rsi
movl $0x20, %edx
subq %r14, %rdx
movl %ebp, %edi
callq 0x3110
movq %rax, %r15
testq %rax, %rax
jns 0x7108
callq 0x3050
cmpl $0x4, (%rax)
jne 0x7187
testq %r15, %r15
movl $0x0, %eax
cmovgq %r15, %rax
addq %rax, %r14
testq %r15, %r15
je 0x7122
cmpq $0x20, %r14
jne 0x70e3
testl %r14d, %r14d
js 0x7187
movl %r14d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x20, %eax
jne 0x71b0
movq %rsp, %rsi
leaq 0x200(%rsp), %rdx
movl %ebx, %edi
movl $0x1, %ecx
callq 0x71fb
testl %eax, %eax
jne 0x71cd
movzwl 0x3fc(%rsp), %eax
testw %ax, %ax
jne 0x71d2
movl %ebx, %edi
callq 0x3100
testl %ebp, %ebp
je 0x716e
movl %ebp, %edi
callq 0x3100
xorl %eax, %eax
addq $0x400, %rsp            # imm = 0x400
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
jmp 0x70d8
callq 0x3466
movq 0xfe5d(%rip), %rax      # 0x16ff0
movq (%rax), %rdi
leaq 0x9e00(%rip), %rdx      # 0x10f9d
movl $0x1, %esi
xorl %eax, %eax
callq 0x3240
jmp 0x71ec
callq 0x347b
leaq 0x9e54(%rip), %rsi      # 0x1100b
movl $0x20, %edx
movl $0x1, %edi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x31c0
jmp 0x71ec
callq 0x3451
rolw $0x8, %ax
movzwl %ax, %edx
leaq 0x9e7b(%rip), %rsi      # 0x1105b
movl $0x1, %edi
xorl %eax, %eax
callq 0x31c0
movl $0x1, %edi
callq 0x3220
callq 0x343c