// File: /workspace/github_c_makefile_star2_O3/blasphemy[P]mmc-utils/mmc_cmds.c
// Function: do_rpmb_write_block
int do_rpmb_write_block(int nargs, char **argv)
{
	int ret, dev_fd, key_fd, data_fd;
	unsigned char key[32];
	uint16_t addr;
	unsigned int cnt;
	struct rpmb_frame frame_in = {
		.req_resp    = htobe16(MMC_RPMB_WRITE),
		.block_count = htobe16(1)
	}, frame_out;

	if (nargs != 5) {
		fprintf(stderr, "Usage: mmc rpmb write-block </path/to/mmcblkXrpmb> <address> </path/to/input_file> </path/to/key>\n");
		exit(1);
	}

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

	ret = rpmb_read_counter(dev_fd, &cnt);
	/* Check RPMB response */
	if (ret != 0) {
		printf("RPMB read counter operation failed, retcode 0x%04x\n", ret);
		exit(1);
	}
	frame_in.write_counter = htobe32(cnt);

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

	/* Read 256b data */
	if (0 == strcmp(argv[3], "-"))
		data_fd = STDIN_FILENO;
	else {
		data_fd = open(argv[3], O_RDONLY);
		if (data_fd < 0) {
			perror("can't open input file");
			exit(1);
		}
	}

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

	/* Read the auth key */
	if (0 == strcmp(argv[4], "-"))
		key_fd = STDIN_FILENO;
	else {
		key_fd = open(argv[4], O_RDONLY);
		if (key_fd < 0) {
			perror("can't open key file");
			exit(1);
		}
	}

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

	/* Calculate HMAC SHA256 */
	hmac_sha256(
		key, sizeof(key),
		frame_in.data, sizeof(frame_in) - offsetof(struct rpmb_frame, data),
		frame_in.key_mac, sizeof(frame_in.key_mac));

	/* 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 (data_fd != STDIN_FILENO)
		close(data_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 $0x438, %rsp            # imm = 0x438
movq %rsi, %r15
movl %edi, %ebx
leaq 0x38(%rsp), %r14
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
xorl %esi, %esi
callq 0x30e0
movw $0x100, 0x1fa(%r14)     # imm = 0x100
movw $0x300, 0x1fe(%r14)     # imm = 0x300
cmpl $0x5, %ebx
jne 0x7bdf
movq 0x8(%r15), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x31d0
testl %eax, %eax
js 0x7bfe
movl %eax, %ebx
leaq 0xc(%rsp), %rsi
movl %eax, %edi
callq 0x7440
testl %eax, %eax
jne 0x7c03
movl 0xc(%rsp), %eax
bswapl %eax
movl %eax, 0x22c(%rsp)
callq 0x3050
movq %rax, %r14
movl $0x0, (%rax)
movq 0x10(%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3180
cmpl $0x0, (%r14)
jne 0x7c13
rolw $0x8, %ax
movw %ax, 0x230(%rsp)
movq 0x18(%r15), %rdi
cmpb $0x2d, (%rdi)
jne 0x7a3e
cmpb $0x0, 0x1(%rdi)
je 0x7bc6
xorl %esi, %esi
xorl %eax, %eax
callq 0x31d0
movl %eax, %ebp
testl %eax, %eax
js 0x7c6c
leaq 0x11c(%rsp), %r12
xorl %r13d, %r13d
leaq (%r12,%r13), %rsi
movl $0x100, %edx            # imm = 0x100
subq %r13, %rdx
movl %ebp, %edi
callq 0x3110
testq %rax, %rax
jns 0x7a7e
cmpl $0x4, (%r14)
jne 0x7bd5
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
addq %rcx, %r13
testq %rax, %rax
je 0x7a9b
cmpq $0x100, %r13            # imm = 0x100
jne 0x7a5c
testl %r13d, %r13d
js 0x7bd5
movl %r13d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x100, %eax            # imm = 0x100
jne 0x7c18
movq 0x20(%r15), %rdi
cmpb $0x2d, (%rdi)
jne 0x7aca
cmpb $0x0, 0x1(%rdi)
je 0x7bcd
xorl %esi, %esi
xorl %eax, %eax
callq 0x31d0
movl %eax, %r15d
testl %eax, %eax
js 0x7c71
xorl %r13d, %r13d
leaq (%rsp,%r13), %rsi
addq $0x10, %rsi
movl $0x20, %edx
subq %r13, %rdx
movl %r15d, %edi
callq 0x3110
testq %rax, %rax
jns 0x7b08
cmpl $0x4, (%r14)
jne 0x7bda
testq %rax, %rax
movl $0x0, %ecx
cmovgq %rax, %rcx
addq %rcx, %r13
testq %rax, %rax
je 0x7b22
cmpq $0x20, %r13
jne 0x7ae1
testl %r13d, %r13d
js 0x7bda
movl %r13d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x20, %eax
jne 0x7c26
leaq 0xfc(%rsp), %r8
leaq 0x10(%rsp), %rdi
movl $0x20, %esi
movq %r12, %rdx
movl $0x11c, %ecx            # imm = 0x11C
movl $0x20, %r9d
callq 0xbca6
leaq 0x38(%rsp), %rsi
leaq 0x238(%rsp), %rdx
movl %ebx, %edi
movl $0x1, %ecx
callq 0x71fb
testl %eax, %eax
jne 0x7c43
movzwl 0x434(%rsp), %eax
testw %ax, %ax
jne 0x7c48
movl %ebx, %edi
callq 0x3100
testl %ebp, %ebp
je 0x7ba5
movl %ebp, %edi
callq 0x3100
testl %r15d, %r15d
je 0x7bb2
movl %r15d, %edi
callq 0x3100
xorl %eax, %eax
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
jmp 0x7a51
xorl %r15d, %r15d
jmp 0x7ade
callq 0x35cb
callq 0x35b6
movq 0xf40a(%rip), %rax      # 0x16ff0
movq (%rax), %rdi
leaq 0x96ac(%rip), %rdx      # 0x1129c
movl $0x1, %esi
xorl %eax, %eax
callq 0x3240
jmp 0x7c62
callq 0x35e0
leaq 0x96f5(%rip), %rsi      # 0x112ff
movl $0x1, %edi
movl %eax, %edx
jmp 0x7c5b
callq 0x3562
leaq 0x95e5(%rip), %rsi      # 0x11204
movl $0x100, %edx            # imm = 0x100
jmp 0x7c32
leaq 0x93de(%rip), %rsi      # 0x1100b
movl $0x20, %edx
movl $0x1, %edi
movl %r13d, %ecx
xorl %eax, %eax
callq 0x31c0
jmp 0x7c62
callq 0x35a1
rolw $0x8, %ax
movzwl %ax, %edx
leaq 0x9405(%rip), %rsi      # 0x1105b
movl $0x1, %edi
xorl %eax, %eax
callq 0x31c0
movl $0x1, %edi
callq 0x3220
callq 0x3577
callq 0x358c