// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x480, %rsp            # imm = 0x480
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x248(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x30f0
movl $0x1, %edi
callq 0x8670
movw %ax, -0x4e(%rbp)
movl $0x3, %edi
callq 0x8670
movw %ax, -0x4a(%rbp)
cmpl $0x5, -0x4(%rbp)
je 0x9179
movq 0x11e92(%rip), %rax     # 0x1aff0
movq (%rax), %rdi
leaq 0xd16d(%rip), %rsi      # 0x162d5
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 0x91ac
leaq 0xce40(%rip), %rdi      # 0x15fdd
callq 0x3200
movl $0x1, %edi
callq 0x3240
movl -0x18(%rbp), %edi
leaq -0x48(%rbp), %rsi
callq 0x88d0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x91dc
movl -0x14(%rbp), %esi
leaq 0xd16d(%rip), %rdi      # 0x16338
movb $0x0, %al
callq 0x30c0
movl $0x1, %edi
callq 0x3240
movl -0x48(%rbp), %edi
callq 0x8990
movl %eax, -0x54(%rbp)
callq 0x3050
movl $0x0, (%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x31a0
movw %ax, -0x42(%rbp)
callq 0x3050
cmpl $0x0, (%rax)
je 0x9229
leaq 0xcf25(%rip), %rdi      # 0x1613f
callq 0x3200
movl $0x1, %edi
callq 0x3240
movzwl -0x42(%rbp), %edi
callq 0x8670
movw %ax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
leaq 0xa0b8(%rip), %rsi      # 0x132fd
callq 0x3170
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x925b
movl $0x0, -0x20(%rbp)
jmp 0x928d
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x31e0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x928b
leaq 0xd0f0(%rip), %rdi      # 0x1636c
callq 0x3200
movl $0x1, %edi
callq 0x3240
jmp 0x928d
movq $0x0, -0x450(%rbp)
movl -0x20(%rbp), %edi
leaq -0x248(%rbp), %rsi
addq $0xe4, %rsi
addq -0x450(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
subq -0x450(%rbp), %rdx
callq 0x3120
movq %rax, -0x458(%rbp)
cmpq $0x0, -0x458(%rbp)
jge 0x92e9
callq 0x3050
cmpl $0x4, (%rax)
je 0x92e9
movq $-0x1, -0x450(%rbp)
jmp 0x9340
cmpq $0x0, -0x458(%rbp)
jle 0x9308
movq -0x458(%rbp), %rax
addq -0x450(%rbp), %rax
movq %rax, -0x450(%rbp)
jmp 0x930a
jmp 0x930c
xorl %eax, %eax
cmpq $0x0, -0x458(%rbp)
movb %al, -0x479(%rbp)
je 0x9332
cmpq $0x100, -0x450(%rbp) # imm = 0x100
setne %al
movb %al, -0x479(%rbp)
movb -0x479(%rbp), %al
testb $0x1, %al
jne 0x9298
movq -0x450(%rbp), %rax
movq %rax, -0x460(%rbp)
movq -0x460(%rbp), %rax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x9374
leaq 0xd01d(%rip), %rdi      # 0x16382
callq 0x3200
movl $0x1, %edi
callq 0x3240
movslq -0x14(%rbp), %rax
cmpq $0x100, %rax            # imm = 0x100
je 0x93a0
movl -0x14(%rbp), %edx
leaq 0xce79(%rip), %rdi      # 0x16203
movl $0x100, %esi            # imm = 0x100
movb $0x0, %al
callq 0x30c0
movl $0x1, %edi
callq 0x3240
jmp 0x93a2
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq 0x9f4c(%rip), %rsi      # 0x132fd
callq 0x3170
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x93c7
movl $0x0, -0x1c(%rbp)
jmp 0x93f9
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x31e0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x93f7
leaq 0xcc01(%rip), %rdi      # 0x15fe9
callq 0x3200
movl $0x1, %edi
callq 0x3240
jmp 0x93f9
movq $0x0, -0x468(%rbp)
movl -0x1c(%rbp), %edi
leaq -0x40(%rbp), %rsi
addq -0x468(%rbp), %rsi
movl $0x20, %edx
subq -0x468(%rbp), %rdx
callq 0x3120
movq %rax, -0x470(%rbp)
cmpq $0x0, -0x470(%rbp)
jge 0x944b
callq 0x3050
cmpl $0x4, (%rax)
je 0x944b
movq $-0x1, -0x468(%rbp)
jmp 0x949f
cmpq $0x0, -0x470(%rbp)
jle 0x946a
movq -0x470(%rbp), %rax
addq -0x468(%rbp), %rax
movq %rax, -0x468(%rbp)
jmp 0x946c
jmp 0x946e
xorl %eax, %eax
cmpq $0x0, -0x470(%rbp)
movb %al, -0x47a(%rbp)
je 0x9491
cmpq $0x20, -0x468(%rbp)
setne %al
movb %al, -0x47a(%rbp)
movb -0x47a(%rbp), %al
testb $0x1, %al
jne 0x9404
movq -0x468(%rbp), %rax
movq %rax, -0x478(%rbp)
movq -0x478(%rbp), %rax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x94d3
leaq 0xcb39(%rip), %rdi      # 0x15ffd
callq 0x3200
movl $0x1, %edi
callq 0x3240
movslq -0x14(%rbp), %rax
cmpq $0x20, %rax
je 0x94fd
movl -0x14(%rbp), %edx
leaq 0xcb23(%rip), %rdi      # 0x1600a
movl $0x20, %esi
movb $0x0, %al
callq 0x30c0
movl $0x1, %edi
callq 0x3240
jmp 0x94ff
leaq -0x40(%rbp), %rdi
leaq -0x248(%rbp), %rdx
addq $0xe4, %rdx
leaq -0x248(%rbp), %r8
addq $0xc4, %r8
movl $0x20, %r9d
movl $0x200, %eax            # imm = 0x200
xorl %ecx, %ecx
addq $0xe4, %rcx
subq %rcx, %rax
movl %eax, %ecx
movl %r9d, %esi
callq 0x10040
movl -0x18(%rbp), %edi
leaq -0x248(%rbp), %rsi
leaq -0x448(%rbp), %rdx
movl $0x1, %ecx
callq 0x86a0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x957a
leaq 0xcadd(%rip), %rdi      # 0x16048
callq 0x3200
movl $0x1, %edi
callq 0x3240
movzwl -0x24c(%rbp), %eax
cmpl $0x0, %eax
je 0x95ad
movzwl -0x24c(%rbp), %edi
callq 0x8670
movzwl %ax, %esi
leaq 0xcabe(%rip), %rdi      # 0x1605a
movb $0x0, %al
callq 0x30c0
movl $0x1, %edi
callq 0x3240
movl -0x18(%rbp), %edi
callq 0x3110
cmpl $0x0, -0x20(%rbp)
je 0x95c3
movl -0x20(%rbp), %edi
callq 0x3110
cmpl $0x0, -0x1c(%rbp)
je 0x95d1
movl -0x1c(%rbp), %edi
callq 0x3110
movl -0x14(%rbp), %eax
addq $0x480, %rsp            # imm = 0x480
popq %rbp
retq
nopl (%rax)