// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
leaq -0x238(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x30f0
cmpq $0x0, -0x10(%rbp)
je 0x86e0
cmpq $0x0, -0x18(%rbp)
je 0x86e0
cmpl $0x0, -0x1c(%rbp)
jne 0x86ec
movl $0xffffffea, -0x4(%rbp) # imm = 0xFFFFFFEA
jmp 0x88c2
movl $0xe0, %edi
callq 0x31c0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x870d
movl $0xfffffff4, -0x4(%rbp) # imm = 0xFFFFFFF4
jmp 0x88c2
movq -0x10(%rbp), %rax
movzwl 0x1fe(%rax), %edi
callq 0x8670
movw %ax, -0x22(%rbp)
movzwl -0x22(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x240(%rbp)
subl $0x3, %eax
ja 0x8894
movq -0x240(%rbp), %rax
leaq 0xaf69(%rip), %rcx      # 0x136b0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x1, -0x1c(%rbp)
je 0x8762
movl $0xffffffea, -0x20(%rbp) # imm = 0xFFFFFFEA
jmp 0x88b3
movq -0x30(%rbp), %rax
movq $0x3, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movl $0x19, %esi
movl $0x80000001, %edx       # imm = 0x80000001
movl $0x1, %ecx
callq 0xa210
movq -0x10(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
addq $0x48, %rax
movq %rax, -0x38(%rbp)
movl $0x5, %edi
callq 0x8670
movw %ax, -0x3a(%rbp)
movq -0x38(%rbp), %rdi
movl $0x19, %esi
movl $0x1, %ecx
movl %ecx, %edx
callq 0xa210
leaq -0x238(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
addq $0x90, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movl $0x12, %esi
xorl %edx, %edx
movl $0x1, %ecx
callq 0xa210
movq -0x18(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x40(%rax)
jmp 0x889d
cmpl $0x1, -0x1c(%rbp)
je 0x8829
movl $0xffffffea, -0x20(%rbp) # imm = 0xFFFFFFEA
jmp 0x88b3
jmp 0x882b
movq -0x30(%rbp), %rax
movq $0x2, (%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movl $0x19, %esi
movl $0x1, %ecx
movl %ecx, %edx
callq 0xa210
movq -0x10(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x30(%rbp), %rax
addq $0x8, %rax
addq $0x48, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movl -0x1c(%rbp), %ecx
movl $0x12, %esi
xorl %edx, %edx
callq 0xa210
movq -0x18(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x40(%rax)
jmp 0x889d
movl $0xffffffea, -0x20(%rbp) # imm = 0xFFFFFFEA
jmp 0x88b3
movl -0x8(%rbp), %edi
movq -0x30(%rbp), %rdx
movl $0xc008b301, %esi       # imm = 0xC008B301
movb $0x0, %al
callq 0x3100
movl %eax, -0x20(%rbp)
movq -0x30(%rbp), %rdi
callq 0x3030
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
nop