// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x440, %rsp            # imm = 0x440
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x21c(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x30f0
movl $0x1, %edi
callq 0x8670
movw %ax, -0x1e(%rbp)
cmpl $0x3, -0x4(%rbp)
je 0x843b
movq 0x12bd0(%rip), %rax     # 0x1aff0
movq (%rax), %rdi
leaq 0xdb72(%rip), %rsi      # 0x15f9c
movb $0x0, %al
callq 0x3180
movl $0x1, %edi
callq 0x3240
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x31e0
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0x846e
leaq 0xdb7e(%rip), %rdi      # 0x15fdd
callq 0x3200
movl $0x1, %edi
callq 0x3240
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0xae80(%rip), %rsi      # 0x132fd
callq 0x3170
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x8493
movl $0x0, -0x1c(%rbp)
jmp 0x84c5
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x31e0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x84c3
leaq 0xdb35(%rip), %rdi      # 0x15fe9
callq 0x3200
movl $0x1, %edi
callq 0x3240
jmp 0x84c5
movq $0x0, -0x428(%rbp)
movl -0x1c(%rbp), %edi
leaq -0x21c(%rbp), %rsi
addq $0xc4, %rsi
addq -0x428(%rbp), %rsi
movl $0x20, %edx
subq -0x428(%rbp), %rdx
callq 0x3120
movq %rax, -0x430(%rbp)
cmpq $0x0, -0x430(%rbp)
jge 0x8521
callq 0x3050
cmpl $0x4, (%rax)
je 0x8521
movq $-0x1, -0x428(%rbp)
jmp 0x8575
cmpq $0x0, -0x430(%rbp)
jle 0x8540
movq -0x430(%rbp), %rax
addq -0x428(%rbp), %rax
movq %rax, -0x428(%rbp)
jmp 0x8542
jmp 0x8544
xorl %eax, %eax
cmpq $0x0, -0x430(%rbp)
movb %al, -0x439(%rbp)
je 0x8567
cmpq $0x20, -0x428(%rbp)
setne %al
movb %al, -0x439(%rbp)
movb -0x439(%rbp), %al
testb $0x1, %al
jne 0x84d0
movq -0x428(%rbp), %rax
movq %rax, -0x438(%rbp)
movq -0x438(%rbp), %rax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x85a9
leaq 0xda63(%rip), %rdi      # 0x15ffd
callq 0x3200
movl $0x1, %edi
callq 0x3240
movslq -0x14(%rbp), %rax
cmpq $0x20, %rax
je 0x85d3
movl -0x14(%rbp), %edx
leaq 0xda4d(%rip), %rdi      # 0x1600a
movl $0x20, %esi
movb $0x0, %al
callq 0x30c0
movl $0x1, %edi
callq 0x3240
jmp 0x85d5
movl -0x18(%rbp), %edi
leaq -0x21c(%rbp), %rsi
leaq -0x41c(%rbp), %rdx
movl $0x1, %ecx
callq 0x86a0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x860f
leaq 0xda48(%rip), %rdi      # 0x16048
callq 0x3200
movl $0x1, %edi
callq 0x3240
movzwl -0x220(%rbp), %eax
cmpl $0x0, %eax
je 0x8642
movzwl -0x220(%rbp), %edi
callq 0x8670
movzwl %ax, %esi
leaq 0xda29(%rip), %rdi      # 0x1605a
movb $0x0, %al
callq 0x30c0
movl $0x1, %edi
callq 0x3240
movl -0x18(%rbp), %edi
callq 0x3110
cmpl $0x0, -0x1c(%rbp)
je 0x8658
movl -0x1c(%rbp), %edi
callq 0x3110
movl -0x14(%rbp), %eax
addq $0x440, %rsp            # imm = 0x440
popq %rbp
retq
nopw %cs:(%rax,%rax)