// File: /workspace/github_c_makefile_star2_O1/blasphemy[P]mmc-utils/mmc_cmds.c
// Function: do_rpmb_read_block
int do_rpmb_read_block(int nargs, char **argv)
{
	int i, ret, dev_fd, data_fd, key_fd = -1;
	uint16_t addr;
	/*
	 * for reading RPMB, number of blocks is set by CMD23 only, the packet
	 * frame field for that is set to 0. So, the type is not u16 but uint!
	 */
	unsigned int blocks_cnt;
	unsigned char key[32];
	struct rpmb_frame frame_in = {
		.req_resp    = htobe16(MMC_RPMB_READ),
	}, *frame_out_p;

	if (nargs != 5 && nargs != 6) {
		fprintf(stderr, "Usage: mmc rpmb read-block </path/to/mmcblkXrpmb> <address> <blocks count> </path/to/output_file> [/path/to/key]\n");
		exit(1);
	}

	dev_fd = open(argv[1], O_RDWR);
	if (dev_fd < 0) {
		perror("device open");
		exit(1);
	}

	/* Get block address */
	errno = 0;
	addr = strtol(argv[2], NULL, 0);
	if (errno) {
		perror("incorrect address");
		exit(1);
	}
	frame_in.addr = htobe16(addr);

	/* Get blocks count */
	errno = 0;
	blocks_cnt = strtol(argv[3], NULL, 0);
	if (errno) {
		perror("incorrect blocks count");
		exit(1);
	}

	if (!blocks_cnt) {
		printf("please, specify valid blocks count number\n");
		exit(1);
	}

	frame_out_p = calloc(sizeof(*frame_out_p), blocks_cnt);
	if (!frame_out_p) {
		printf("can't allocate memory for RPMB outer frames\n");
		exit(1);
	}

	/* Write 256b data */
	if (0 == strcmp(argv[4], "-"))
		data_fd = STDOUT_FILENO;
	else {
		data_fd = open(argv[4], O_WRONLY | O_CREAT | O_APPEND,
					   S_IRUSR | S_IWUSR);
		if (data_fd < 0) {
			perror("can't open output file");
			exit(1);
		}
	}

	/* Key is specified */
	if (nargs == 6) {
		if (0 == strcmp(argv[5], "-"))
			key_fd = STDIN_FILENO;
		else {
			key_fd = open(argv[5], O_RDONLY);
			if (key_fd < 0) {
				perror("can't open input key file");
				exit(1);
			}
		}

		ret = DO_IO(read, key_fd, key, sizeof(key));
		if (ret < 0) {
			perror("read the key data");
			exit(1);
		} else if (ret != sizeof(key)) {
			printf("Data must be %lu bytes length, but we read only %d, exit\n",
				   (unsigned long)sizeof(key),
				   ret);
			exit(1);
		}
	}

	/* Execute RPMB op */
	ret = do_rpmb_op(dev_fd, &frame_in, frame_out_p, blocks_cnt);
	if (ret != 0) {
		perror("RPMB ioctl failed");
		exit(1);
	}

	/* Check RPMB response */
	if (frame_out_p[blocks_cnt - 1].result != 0) {
		printf("RPMB operation failed, retcode 0x%04x\n",
			   be16toh(frame_out_p[blocks_cnt - 1].result));
		exit(1);
	}

	/* Do we have to verify data against key? */
	if (nargs == 6) {
		unsigned char mac[32];
		hmac_sha256_ctx ctx;
		struct rpmb_frame *frame_out = NULL;

		hmac_sha256_init(&ctx, key, sizeof(key));
		for (i = 0; i < blocks_cnt; i++) {
			frame_out = &frame_out_p[i];
			hmac_sha256_update(&ctx, frame_out->data,
							   sizeof(*frame_out) -
								   offsetof(struct rpmb_frame, data));
		}

		hmac_sha256_final(&ctx, mac, sizeof(mac));

		/* Impossible */
		assert(frame_out);

		/* Compare calculated MAC and MAC from last frame */
		if (memcmp(mac, frame_out->key_mac, sizeof(mac))) {
			printf("RPMB MAC missmatch\n");
			exit(1);
		}
	}

	/* Write data */
	for (i = 0; i < blocks_cnt; i++) {
		struct rpmb_frame *frame_out = &frame_out_p[i];
		ret = DO_IO(write, data_fd, frame_out->data, sizeof(frame_out->data));
		if (ret < 0) {
			perror("write the data");
			exit(1);
		} else if (ret != sizeof(frame_out->data)) {
			printf("Data must be %lu bytes length, but we wrote only %d, exit\n",
				   (unsigned long)sizeof(frame_out->data),
				   ret);
			exit(1);
		}
	}

	free(frame_out_p);
	close(dev_fd);
	if (data_fd != STDOUT_FILENO)
		close(data_fd);
	if (key_fd != -1 && key_fd != STDIN_FILENO)
		close(key_fd);

	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x588, %rsp            # imm = 0x588
movq %rsi, %rbx
movl %edi, %r15d
leaq 0x68(%rsp), %r14
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
xorl %esi, %esi
callq 0x30e0
movw $0x400, 0x1fe(%r14)     # imm = 0x400
movq %r15, 0x8(%rsp)
leal -0x7(%r15), %eax
cmpl $-0x3, %eax
jbe 0x79b9
movq 0x8(%rbx), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x31d0
testl %eax, %eax
js 0x79d8
movl %eax, %r15d
callq 0x3050
movq %rax, %r14
movl $0x0, (%rax)
movq 0x10(%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3180
cmpl $0x0, (%r14)
jne 0x79dd
rolw $0x8, %ax
movw %ax, 0x260(%rsp)
movl $0x0, (%r14)
movq 0x18(%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3180
cmpl $0x0, (%r14)
jne 0x79e2
movq %rax, %r12
testl %r12d, %r12d
je 0x79e7
movl %r12d, %esi
movl $0x200, %edi            # imm = 0x200
callq 0x3140
movq %rax, (%rsp)
testq %rax, %rax
je 0x79f0
movq 0x20(%rbx), %r13
leaq 0x6c13(%rip), %rsi      # 0xe2fd
movq %r13, %rdi
callq 0x3150
testl %eax, %eax
je 0x7718
movq %r13, %rdi
movl $0x441, %esi            # imm = 0x441
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x31d0
movl %eax, %ebp
testl %eax, %eax
movl %r15d, %r13d
jns 0x7720
callq 0x34d0
movl $0x1, %ebp
movl %r15d, %r13d
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl $0x6, 0x8(%rsp)
jne 0x77c3
movq 0x28(%rbx), %rbx
leaq 0x6bc1(%rip), %rsi      # 0xe2fd
movq %rbx, %rdi
callq 0x3150
testl %eax, %eax
je 0x7760
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x31d0
movl %eax, %r15d
testl %eax, %eax
jns 0x7763
callq 0x34e5
xorl %r15d, %r15d
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rsi
addq $0x20, %rsi
movl $0x20, %edx
subq %rbx, %rdx
movl %r15d, %edi
callq 0x3110
testq %rax, %rax
jns 0x7788
cmpl $0x4, (%r14)
jne 0x77a4
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
addq %rcx, %rbx
testq %rax, %rax
je 0x77ab
cmpq $0x20, %rbx
jne 0x7765
jmp 0x77ab
movq $-0x1, %rbx
testl %ebx, %ebx
js 0x7a1a
movl %ebx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x20, %eax
jne 0x7a1f
leaq 0x68(%rsp), %rsi
movl %r13d, %edi
movq (%rsp), %rbx
movq %rbx, %rdx
movl %r12d, %ecx
callq 0x72af
testl %eax, %eax
jne 0x79f9
movl %r12d, %eax
decl %eax
shlq $0x9, %rax
movzwl 0x1fc(%rbx,%rax), %eax
testw %ax, %ax
jne 0x79fe
movq %r15, 0x18(%rsp)
cmpl $0x6, 0x8(%rsp)
movl %r13d, 0x14(%rsp)
jne 0x78c5
leaq 0x268(%rsp), %rbx
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
movl $0x20, %edx
callq 0xbb8d
cmpl $0x1, %r12d
movq %r12, 0x8(%rsp)
movl %r12d, %r15d
adcl $0x0, %r15d
movq (%rsp), %rax
leaq 0xe4(%rax), %r13
shlq $0x9, %r15
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r13, %rsi
movl $0x11c, %edx            # imm = 0x11C
callq 0xbcd7
addq $-0x200, %r12           # imm = 0xFE00
addq $0x200, %r13            # imm = 0x200
movq %r15, %rax
addq %r12, %rax
jne 0x784d
leaq 0x268(%rsp), %rdi
leaq 0x40(%rsp), %rbx
movq %rbx, %rsi
movl $0x20, %edx
callq 0xbcdc
movq (%rsp), %rax
subq %r12, %rax
movdqu -0x13c(%rax), %xmm0
movdqu -0x12c(%rax), %xmm1
pcmpeqb 0x10(%rbx), %xmm1
pcmpeqb (%rbx), %xmm0
pand %xmm1, %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x7a30
movq 0x8(%rsp), %r12
cmpl $0x1, %r12d
adcl $0x0, %r12d
xorl %r13d, %r13d
movq %r12, %r15
movq %r13, %rax
shlq $0x9, %rax
movq (%rsp), %rcx
leaq (%rcx,%rax), %r12
addq $0xe4, %r12
xorl %ebx, %ebx
leaq (%r12,%rbx), %rsi
movl $0x100, %edx            # imm = 0x100
subq %rbx, %rdx
movl %ebp, %edi
callq 0x3080
testq %rax, %rax
jns 0x7909
cmpl $0x4, (%r14)
jne 0x7928
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
addq %rcx, %rbx
testq %rax, %rax
je 0x792f
cmpq $0x100, %rbx            # imm = 0x100
jne 0x78eb
jmp 0x792f
movq $-0x1, %rbx
testl %ebx, %ebx
js 0x7990
movl %ebx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x100, %eax            # imm = 0x100
jne 0x7995
movq %r15, %r12
incq %r13
cmpq %r15, %r13
jne 0x78d0
movq (%rsp), %rdi
callq 0x3030
movl 0x14(%rsp), %edi
callq 0x3100
cmpl $0x1, %ebp
je 0x796a
movl %ebp, %edi
callq 0x3100
movq 0x18(%rsp), %rdi
leal -0x1(%rdi), %eax
cmpl $-0x3, %eax
ja 0x797c
callq 0x3100
movl %ebx, %eax
addq $0x588, %rsp            # imm = 0x588
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x350f
leaq 0x98d4(%rip), %rsi      # 0x11270
movl $0x100, %edx            # imm = 0x100
movl $0x1, %edi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x31c0
movl $0x1, %edi
callq 0x3220
movq 0xf630(%rip), %rax      # 0x16ff0
movq (%rax), %rdi
leaq 0x9713(%rip), %rdx      # 0x110dd
movl $0x1, %esi
xorl %eax, %eax
callq 0x3240
jmp 0x79af
callq 0x3539
callq 0x34a6
callq 0x34bb
leaq 0x978a(%rip), %rsi      # 0x11178
jmp 0x7a37
leaq 0x97ac(%rip), %rsi      # 0x111a3
jmp 0x7a37
callq 0x34fa
rolw $0x8, %ax
movzwl %ax, %edx
leaq 0x965e(%rip), %rsi      # 0x1106a
movl $0x1, %edi
xorl %eax, %eax
callq 0x31c0
jmp 0x79af
callq 0x3524
leaq 0x97ed(%rip), %rsi      # 0x11213
movl $0x20, %edx
jmp 0x79a1
leaq 0x9816(%rip), %rsi      # 0x1124d
movl $0x1, %edi
xorl %eax, %eax
callq 0x31c0
jmp 0x79af