// File: /workspace/github_c_makefile_star2_O2/blasphemy[P]mmc-utils/mmc_cmds.c
// Function: do_rpmb_op
static int do_rpmb_op(int fd,
					  const struct rpmb_frame *frame_in,
					  struct rpmb_frame *frame_out,
					  unsigned int out_cnt)
{
#ifndef MMC_IOC_MULTI_CMD
	fprintf(stderr, "mmc-utils has been compiled without MMC_IOC_MULTI_CMD"
		" support, needed by RPMB operation.\n");
	exit(1);
#else
	int err;
	u_int16_t rpmb_type;
	struct mmc_ioc_multi_cmd *mioc;
	struct mmc_ioc_cmd *ioc;
	struct rpmb_frame frame_status = {0};

	if (!frame_in || !frame_out || !out_cnt)
		return -EINVAL;

	/* prepare arguments for MMC_IOC_MULTI_CMD ioctl */
	mioc = (struct mmc_ioc_multi_cmd *)
		malloc(sizeof (struct mmc_ioc_multi_cmd) +
		       RPMB_MULTI_CMD_MAX_CMDS * sizeof (struct mmc_ioc_cmd));
	if (!mioc) {
		return -ENOMEM;
	}

	rpmb_type = be16toh(frame_in->req_resp);

	switch(rpmb_type) {
	case MMC_RPMB_WRITE:
	case MMC_RPMB_WRITE_KEY:
		if (out_cnt != 1) {
			err = -EINVAL;
			goto out;
		}

		mioc->num_of_cmds = 3;

		/* Write request */
		ioc = &mioc->cmds[0];
		set_single_cmd(ioc, MMC_WRITE_MULTIPLE_BLOCK, (1 << 31) | 1, 1);
		mmc_ioc_cmd_set_data((*ioc), frame_in);

		/* Result request */
		ioc = &mioc->cmds[1];
		frame_status.req_resp = htobe16(MMC_RPMB_READ_RESP);
		set_single_cmd(ioc, MMC_WRITE_MULTIPLE_BLOCK, 1, 1);
		mmc_ioc_cmd_set_data((*ioc), &frame_status);

		/* Get response */
		ioc = &mioc->cmds[2];
		set_single_cmd(ioc, MMC_READ_MULTIPLE_BLOCK, 0, 1);
		mmc_ioc_cmd_set_data((*ioc), frame_out);

		break;
	case MMC_RPMB_READ_CNT:
		if (out_cnt != 1) {
			err = -EINVAL;
			goto out;
		}
		/* fall through */

	case MMC_RPMB_READ:
		mioc->num_of_cmds = 2;

		/* Read request */
		ioc = &mioc->cmds[0];
		set_single_cmd(ioc, MMC_WRITE_MULTIPLE_BLOCK, 1, 1);
		mmc_ioc_cmd_set_data((*ioc), frame_in);

		/* Get response */
		ioc = &mioc->cmds[1];
		set_single_cmd(ioc, MMC_READ_MULTIPLE_BLOCK, 0, out_cnt);
		mmc_ioc_cmd_set_data((*ioc), frame_out);

		break;
	default:
		err = -EINVAL;
		goto out;
	}

	err = ioctl(fd, MMC_IOC_MULTI_CMD, mioc);

out:
	free(mioc);
	return err;
#endif /* !MMC_IOC_MULTI_CMD */
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movl %ecx, %ebp
movq %rdx, %r14
movq %rsi, %r12
movl %edi, %ebx
leaq 0x28(%rsp), %r13
movl $0x200, %edx            # imm = 0x200
movq %r13, %rdi
xorl %esi, %esi
callq 0x30e0
testq %r12, %r12
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0x6d97
movl $0xe0, %edi
callq 0x31a0
testq %rax, %rax
je 0x6d9b
movq %rax, %r15
movl %ebx, 0x4(%rsp)
movzwl 0x1fe(%r12), %eax
rolw $0x8, %ax
movzwl %ax, %eax
pushq $-0x16
popq %rbx
decl %eax
cmpl $0x3, %eax
ja 0x6e74
leaq 0x7974(%rip), %rcx      # 0xe65c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x1, %ebp
jne 0x6e74
movq $0x3, (%r15)
movq $0x19, 0x10(%r15)
movl $0x80000001, 0x8(%r15)  # imm = 0x80000001
movl $0x1, 0x30(%r15)
movabsq $0x200000000b5, %rax    # imm = 0x200000000B5
movq %rax, 0x28(%r15)
movq %r12, 0x48(%r15)
movq %r15, %rdi
addq $0x50, %rdi
movw $0x500, 0x226(%rsp)     # imm = 0x500
movl $0xd8, %eax
movq %rax, 0x20(%rsp)
movl $0xb8, %eax
movq %rax, 0x18(%rsp)
movl $0xc0, %eax
movq %rax, 0x10(%rsp)
movl $0xbc, %eax
movq %rax, 0x8(%rsp)
movl $0xa4, %r9d
movl $0xa0, %r10d
movl $0x98, %r11d
movl $0x90, %ebx
pushq $0x70
popq %rax
pushq $0x78
popq %rcx
pushq $0x74
popq %rdx
pushq $0x5c
popq %rsi
pushq $0x58
popq %r8
movq %r13, %r12
pushq $0x1
popq %r13
movl %r13d, %ebp
jmp 0x6dff
pushq $-0x16
jmp 0x6d9d
pushq $-0xc
popq %rbx
jmp 0x6e7c
cmpl $0x1, %ebp
jne 0x6e74
movq $0x2, (%r15)
movq %r15, %rdi
addq $0x8, %rdi
movl $0x90, %eax
movq %rax, 0x20(%rsp)
pushq $0x70
popq %rax
movq %rax, 0x18(%rsp)
pushq $0x78
popq %rax
movq %rax, 0x10(%rsp)
pushq $0x74
popq %rax
movq %rax, 0x8(%rsp)
pushq $0x5c
popq %r9
pushq $0x58
popq %r10
pushq $0x50
popq %r11
pushq $0x48
popq %rbx
pushq $0x28
popq %rax
pushq $0x30
popq %rcx
pushq $0x2c
popq %rdx
pushq $0x14
popq %rsi
pushq $0x10
popq %r8
pushq $0x1
popq %r13
movl $0x19, (%r15,%r8)
movl %r13d, (%rdi)
andl $0x0, (%r15,%rsi)
movl $0x200, %esi            # imm = 0x200
movl %esi, (%r15,%rdx)
movl %r13d, (%r15,%rcx)
movl $0xb5, %ecx
movl %ecx, (%r15,%rax)
movq %r12, (%r15,%rbx)
movl $0x12, (%r15,%r10)
andl $0x0, (%r15,%r11)
andl $0x0, (%r15,%r9)
movq 0x8(%rsp), %rax
movl %esi, (%r15,%rax)
movq 0x10(%rsp), %rax
movl %ebp, (%r15,%rax)
movq 0x18(%rsp), %rax
movl %ecx, (%r15,%rax)
movq 0x20(%rsp), %rax
movq %r14, (%r15,%rax)
movl $0xc008b301, %esi       # imm = 0xC008B301
movl 0x4(%rsp), %edi
movq %r15, %rdx
xorl %eax, %eax
callq 0x30f0
movl %eax, %ebx
movq %r15, %rdi
callq 0x3030
movl %ebx, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq