// File: /workspace/github_c_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
movq %rsi, %r14
movl %edi, %ebx
leaq 0x8(%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 0x6bed
movq 0x8(%r14), %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x31e0
testl %eax, %eax
js 0x6c0c
movl %eax, %ebx
movq 0x10(%r14), %rdi
cmpb $0x2d, (%rdi)
jne 0x6b03
cmpb $0x0, 0x1(%rdi)
je 0x6bd2
xorl %esi, %esi
xorl %eax, %eax
callq 0x31e0
movl %eax, %ebp
testl %eax, %eax
js 0x6c5a
leaq 0xcc(%rsp), %r12
xorl %r14d, %r14d
pushq $0x20
popq %r13
leaq (%r12,%r14), %rsi
movq %r13, %rdx
subq %r14, %rdx
movl %ebp, %edi
callq 0x3110
movq %rax, %r15
testq %rax, %rax
jns 0x6b4c
callq 0x3050
cmpl $0x4, (%rax)
jne 0x6bd9
testq %r15, %r15
movl $0x0, %eax
cmovgq %r15, %rax
addq %rax, %r14
testq %r15, %r15
je 0x6b66
cmpq $0x20, %r14
jne 0x6b25
testl %r14d, %r14d
js 0x6bd9
movl %r14d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x20, %eax
jne 0x6c15
leaq 0x8(%rsp), %rsi
leaq 0x208(%rsp), %rdx
pushq $0x1
popq %rcx
movl %ebx, %edi
callq 0x6c66
testl %eax, %eax
jne 0x6c30
movzwl 0x404(%rsp), %eax
testw %ax, %ax
jne 0x6c39
movl %ebx, %edi
callq 0x3100
testl %ebp, %ebp
je 0x6bbe
movl %ebp, %edi
callq 0x3100
xorl %eax, %eax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
jmp 0x6b16
leaq 0xa41e(%rip), %rdi      # 0x10ffe
callq 0x31f0
pushq $0x1
popq %rdi
callq 0x3230
movq 0x113fc(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0xa39f(%rip), %rdx      # 0x10f9d
pushq $0x1
popq %rbx
movl %ebx, %esi
xorl %eax, %eax
callq 0x3250
jmp 0x6c53
leaq 0xa3cb(%rip), %rdi      # 0x10fde
jmp 0x6be0
leaq 0xa3ef(%rip), %rsi      # 0x1100b
pushq $0x1
popq %rbx
pushq $0x20
popq %rdx
movl %ebx, %edi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x31d0
jmp 0x6c53
leaq 0xa412(%rip), %rdi      # 0x11049
jmp 0x6be0
rolw $0x8, %ax
movzwl %ax, %edx
leaq 0xa414(%rip), %rsi      # 0x1105b
pushq $0x1
popq %rbx
movl %ebx, %edi
xorl %eax, %eax
callq 0x31d0
movl %ebx, %edi
callq 0x3230
leaq 0xa389(%rip), %rdi      # 0x10fea
jmp 0x6be0