// File: /workspace/github_c_makefile_star2_O1/truenas[P]binaries/src/ixnvdimm.c
// Function: main
int
main(int argc, char *argv[])
{
	struct ixnvdimm_info info;
	const char *progname, *devname, *f = NULL;
	char *e;
	int F = 0, I = 0, b, c, d = 0, i, fd, h = 0, r = 0, w = 0;
	int page = -1, off = -1, size = 1;
	uint32_t funcs, val;
	uint8_t	buf[4];

	progname = argv[0];

	while ((c = getopt(argc, argv, "FIdf:hrw")) != -1) {
		switch (c) {
		case 'd':
			d = 1;
			break;
		case 'F':
			F = 1;
			break;
		case 'I':
			I = 1;
			break;
		case 'f':
			f = optarg;
			break;
		case 'r':
			r = 1;
			break;
		case 'w':
			w = 1;
			break;
		case 'h':
			h = 1;
			size = 2;
			break;
		default:
			usage(progname);
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	if (argc < 1 || (d + r + w + (f != NULL) > 1) || (h && !r && !w))
		usage(progname);

	devname = argv[0];
	argc--;
	argv++;
	if ((fd = open(devname, O_RDWR)) < 0)
		err(EX_NOINPUT, "Can't open %s", devname);
	init(devname);

	if (getinfo(fd, &info))
		err(EX_NOINPUT, "Can't get info from %s", devname);

#ifdef __linux__
	funcs = readsysfs("dsm_mask");
	funcs |= 1;
#else
	bzero(&buf, sizeof(buf));
	if (calldsm(fd, 0, NULL, 0, buf, sizeof(buf)) != 4)
		err(EX_NOINPUT, "Can't call DSM on %s", devname);
	funcs = le32dec(&buf[0]);
#endif
	if (~funcs & 0x18000001) {
		errx(EX_UNAVAILABLE, "Required Microsoft DSM functions "
		    "are not supported: 0x%08x\n", funcs);
	}

	if (d) {
		int snp, vsp, vnp;

		snp = i2c_read(fd, 0, 1);
		vsp = i2c_read(fd, 0, 2);
		vnp = i2c_read(fd, 0, 3);
		for (page = 0; page < snp; page++) {
			printf("Page 0x%02x:\n", page);
			for (off = 0; off < 256; off++) {
				printf(" %02x", i2c_read(fd, page, off));
				if ((off % 16) == 15)
					printf("\n");
			}
		}
		for (page = vsp; page < vsp+vnp; page++) {
			printf("Page 0x%02x:\n", page);
			for (off = 0; off < 256; off++) {
				printf(" %02x", i2c_read(fd, page, off));
				if ((off % 16) == 15)
					printf("\n");
			}
		}
		goto done;
	}
	if (F) {
		if (~funcs & 0x08200001) {
			errx(EX_UNAVAILABLE, "Required Microsoft DSM functions "
			    "are not supported: 0x%08x\n", funcs);
		}
		factory_default(fd);
		goto done;
	}
	if (f) {
		if (~funcs & 0x1bc00001) {
			errx(EX_UNAVAILABLE, "Required Microsoft DSM functions "
			    "are not supported: 0x%08x\n", funcs);
		}
		firmware_update(fd, &info, f, I);
		goto done;
	}
	if (r || w) {
		if (argc < 1 && w) {
			usage(progname);
		} else if (argc < 1) {
			;
		} else if (reg_parse(argv[0], &page, &off, &size)) {
			argc--;
			argv++;
		} else {
			if (argc < 1)
				usage(progname);
			page = strtol(argv[0], &e, 0);
			if (e[0] != 0)
				usage(progname);
			argc--;
			argv++;
			if (argc >= 1) {
				off = strtol(argv[0], &e, 0);
				if (e[0] != 0)
					usage(progname);
				argc--;
				argv++;
			}
		}
	}
	if (r) {
		if (off >= 0) {
			for (b = 0; b < size; b++)
				printf("%02x", i2c_read(fd, page, off + size - b - 1));
			printf("\n");
			goto done;
		}
		for (i = 0; regs[i].name != NULL; i++) {
			if (page >= 0 && regs[i].page != page)
				continue;
			printf("%-36s ", regs[i].name);
			for (b = 0; b < regs[i].size; b++)
				printf("%02x", i2c_read(fd, regs[i].page, regs[i].off + regs[i].size - b - 1));
			printf("\n");
		}
		goto done;
	}
	if (w) {
		if (argc < 1 || size > 3 || page < 0 || off < 0)
			usage(progname);
		val = strtol(argv[0], &e, 0);
		if (e[0] != 0 || val > (size == 3 ? 16777216 : (size == 2 ? 65535 : 255)))
			usage(progname);
		if (size == 3) {
			i2c_write16(fd, page, off, val);
			i2c_write(fd, page, off + 2, val >> 16);
		} else if (size == 2)
			i2c_write16(fd, page, off, val);
		else
			i2c_write(fd, page, off, val);
		goto done;
	}

	health(fd, &info);
done:
	close(fd);
	exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movl %edi, %r15d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x10(%rsp)
movl %eax, 0xc(%rsp)
movl $0x1, 0x14(%rsp)
movq (%rsi), %rax
movq %rax, 0x18(%rsp)
movb $0x1, %al
movl %eax, 0x34(%rsp)
movq $0x0, 0x48(%rsp)
movl $0x1, %ebp
leaq 0x2cec(%rip), %r13      # 0x5004
movq 0x6cb1(%rip), %r14      # 0x8fd0
movq $0x0, 0x20(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x2c(%rsp)
movl $0x0, 0x30(%rsp)
xorl %r12d, %r12d
movl %r15d, %edi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x2180
cmpl $0x66, %eax
jne 0x235b
movq (%r14), %r12
jmp 0x2343
cmpl $0x67, %eax
jg 0x237f
cmpl $0x48, %eax
jle 0x239c
cmpl $0x49, %eax
je 0x23ab
cmpl $0x64, %eax
jne 0x2457
movl $0x1, %eax
movq %rax, 0x48(%rsp)
jmp 0x2343
cmpl $0x68, %eax
je 0x23c1
cmpl $0x72, %eax
je 0x23b5
cmpl $0x77, %eax
jne 0x2457
movl $0x1, 0x8(%rsp)
jmp 0x2343
cmpl $0x46, %eax
jne 0x23d3
movl $0x1, 0x30(%rsp)
jmp 0x2343
movl $0x1, 0x2c(%rsp)
jmp 0x2343
movl $0x1, %eax
movq %rax, 0x20(%rsp)
jmp 0x2343
movl $0x2, %ebp
movl $0x0, 0x34(%rsp)
jmp 0x2343
cmpl $-0x1, %eax
jne 0x2457
movl %ebp, 0x14(%rsp)
movq 0x6be5(%rip), %rax      # 0x8fc8
movslq (%rax), %rax
subl %eax, %r15d
movl 0x8(%rsp), %r14d
jle 0x245b
movq 0x20(%rsp), %rcx
movq 0x48(%rsp), %rdx
addl %edx, %ecx
addl %r14d, %ecx
cmpq $0x1, %r12
sbbl $-0x1, %ecx
cmpl $0x1, %ecx
ja 0x245b
cmpl $0x0, 0x20(%rsp)
setne %cl
movl 0x34(%rsp), %edx
orb %cl, %dl
testl %r14d, %r14d
setne %cl
orb %dl, %cl
testb $0x1, %cl
je 0x245b
leaq (%rbx,%rax,8), %rbp
movq (%rbp), %r13
movq %r13, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x2170
testl %eax, %eax
jns 0x2465
leaq 0x2bc5(%rip), %rsi      # 0x500d
movl $0x42, %edi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2150
movl %ebp, 0x14(%rsp)
movq 0x18(%rsp), %rdi
callq 0x29e5
movl %eax, %ebx
movq %r13, %rdi
movl $0x2f, %esi
callq 0x20d0
leaq 0x1(%rax), %rcx
testq %rax, %rax
cmoveq %r13, %rcx
movq %rcx, 0x6c7a(%rip)      # 0x9100
leaq 0x2cc9(%rip), %rdi      # 0x5156
callq 0x2a75
rolw $0x8, %ax
movw %ax, 0x38(%rsp)
leaq 0x2cc5(%rip), %rdi      # 0x5167
callq 0x2a75
rolw $0x8, %ax
movw %ax, 0x3a(%rsp)
leaq 0x2cc1(%rip), %rdi      # 0x5178
callq 0x2a75
rolw $0x8, %ax
movw %ax, 0x3c(%rsp)
leaq 0x2c80(%rip), %rdi      # 0x514c
callq 0x2a75
rolw $0x8, %ax
movw %ax, 0x3e(%rsp)
leaq 0x2c7c(%rip), %rdi      # 0x515d
callq 0x2a75
rolw $0x8, %ax
movw %ax, 0x40(%rsp)
leaq 0x2c78(%rip), %rdi      # 0x516e
callq 0x2a75
rolw $0x8, %ax
movw %ax, 0x42(%rsp)
leaq 0x2c74(%rip), %rdi      # 0x517f
callq 0x2a75
movl %eax, 0x44(%rsp)
leaq 0x2b00(%rip), %rdi      # 0x501b
callq 0x2a75
movl %eax, %edx
orl $0x1, %edx
movl %eax, %ecx
notl %ecx
testl $0x18000000, %ecx       # imm = 0x18000000
jne 0x27a4
cmpl $0x0, 0x48(%rsp)
jne 0x261d
cmpl $0x0, 0x30(%rsp)
jne 0x2660
testq %r12, %r12
jne 0x2787
movq %rbp, %rax
leal -0x1(%r15), %ebp
movq %rax, %r13
addq $0x8, %rax
movq 0x20(%rsp), %rcx
orl %r14d, %ecx
je 0x27c5
testl %r14d, %r14d
setne %cl
cmpl $0x1, %r15d
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
je 0x245b
cmpl $0x1, %r15d
je 0x27c5
movq (%rax), %r12
leaq 0x10(%rsp), %rsi
leaq 0xc(%rsp), %rdx
leaq 0x14(%rsp), %rcx
movq %r12, %rdi
callq 0x3aa2
testl %eax, %eax
jne 0x27b7
leaq 0x50(%rsp), %r14
xorl %ebp, %ebp
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2130
movl %eax, 0x10(%rsp)
movq (%r14), %rax
cmpb $0x0, (%rax)
jne 0x245b
leaq 0x10(%r13), %rax
cmpl $0x2, %r15d
je 0x2613
movq (%rax), %rdi
leaq 0x50(%rsp), %r12
movq %r12, %rsi
xorl %edx, %edx
callq 0x2130
movl %eax, 0xc(%rsp)
movq (%r12), %rax
cmpb $0x0, (%rax)
jne 0x245b
addl $-0x3, %r15d
addq $0x18, %r13
movq %r13, %rax
movl %r15d, %ebp
movl 0x8(%rsp), %r14d
jmp 0x27c5
xorl %ebp, %ebp
movl %ebx, %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x2b2d
movl %eax, %r15d
movl %ebx, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2b2d
movl %eax, %r14d
movl %ebx, %edi
xorl %esi, %esi
movl $0x3, %edx
callq 0x2b2d
movzbl %al, %ecx
testb %r15b, %r15b
jne 0x2676
movl 0xc(%rsp), %eax
jmp 0x26f2
btl $0x15, %eax
jae 0x27a4
movl %ebx, %edi
callq 0x2ba8
jmp 0x2779
movb %r14b, 0x20(%rsp)
movl %ecx, 0x8(%rsp)
movzbl %r15b, %r14d
leaq 0x30a3(%rip), %r12      # 0x572d
xorl %ebp, %ebp
leaq 0x29cd(%rip), %rdi      # 0x5060
movl %ebp, %esi
xorl %eax, %eax
callq 0x20a0
movzbl %bpl, %r13d
xorl %r15d, %r15d
movzbl %r15b, %edx
movl %ebx, %edi
movl %r13d, %esi
callq 0x2b2d
movzbl %al, %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x20a0
movl %r15d, %eax
notl %eax
testb $0xf, %al
jne 0x26d1
movl $0xa, %edi
callq 0x2040
incl %r15d
cmpl $0x100, %r15d           # imm = 0x100
jne 0x26a3
incl %ebp
cmpl %r14d, %ebp
jne 0x268c
movl $0x100, %eax            # imm = 0x100
movl 0x8(%rsp), %ecx
movb 0x20(%rsp), %r14b
movzbl %r14b, %r14d
movl %ebp, 0x10(%rsp)
movl %eax, 0xc(%rsp)
testl %ecx, %ecx
je 0x2770
addl %r14d, %ecx
leaq 0x3021(%rip), %r12      # 0x572d
movl %ecx, %r15d
leaq 0x294a(%rip), %rdi      # 0x5060
movl %r14d, %esi
xorl %eax, %eax
callq 0x20a0
movzbl %r14b, %ebp
xorl %r13d, %r13d
movzbl %r13b, %edx
movl %ebx, %edi
movl %ebp, %esi
callq 0x2b2d
movzbl %al, %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x20a0
movl %r13d, %eax
notl %eax
testb $0xf, %al
jne 0x2754
movl $0xa, %edi
callq 0x2040
incl %r13d
cmpl $0x100, %r13d           # imm = 0x100
jne 0x2727
incl %r14d
movl %r15d, %ecx
cmpl %r15d, %r14d
jb 0x270c
movl $0x100, %eax            # imm = 0x100
movl %r14d, 0x10(%rsp)
movl %eax, 0xc(%rsp)
movl %ebx, %edi
callq 0x20f0
xorl %edi, %edi
callq 0x21a0
testl $0x3c00000, %ecx        # imm = 0x3C00000
jne 0x27a4
leaq 0x38(%rsp), %rsi
movl %ebx, %edi
movq %r12, %rdx
movl 0x2c(%rsp), %ecx
callq 0x2c36
jmp 0x2779
leaq 0x2879(%rip), %rsi      # 0x5024
movl $0x45, %edi
xorl %eax, %eax
callq 0x2090
addl $-0x2, %r15d
addq $0x10, %r13
movq %r13, %rax
movl %r15d, %ebp
cmpl $0x0, 0x20(%rsp)
je 0x2829
movl 0xc(%rsp), %ebp
testl %ebp, %ebp
js 0x28f3
movl 0x14(%rsp), %r12d
testl %r12d, %r12d
jle 0x281a
addl %r12d, %ebp
decb %bpl
movzbl 0x10(%rsp), %r14d
leaq 0x2f39(%rip), %r15      # 0x572e
movzbl %bpl, %ebp
movl %ebx, %edi
movl %r14d, %esi
movl %ebp, %edx
callq 0x2b2d
movzbl %al, %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x20a0
decb %bpl
decl %r12d
jne 0x27f5
movl $0xa, %edi
callq 0x2040
jmp 0x2779
testl %r14d, %r14d
je 0x29a4
testl %ebp, %ebp
setle %cl
movl 0x14(%rsp), %r13d
cmpl $0x4, %r13d
setge %dl
movl 0x10(%rsp), %ebp
testl %ebp, %ebp
sets %sil
orb %dl, %sil
movl 0xc(%rsp), %r14d
testl %r14d, %r14d
sets %dl
orb %sil, %dl
orb %cl, %dl
cmpb $0x1, %dl
je 0x245b
movq (%rax), %rdi
leaq 0x50(%rsp), %r12
movq %r12, %rsi
xorl %edx, %edx
callq 0x2130
movq %rax, %r15
movq (%r12), %rax
cmpb $0x0, (%rax)
jne 0x245b
cmpl $0x2, %r13d
movl $0xffff, %eax           # imm = 0xFFFF
movl $0xff, %ecx
cmovel %eax, %ecx
cmpl $0x3, %r13d
movl $0x1000000, %eax        # imm = 0x1000000
cmovnel %ecx, %eax
cmpl %r15d, %eax
jb 0x245b
cmpl $0x2, %r13d
je 0x29b5
cmpl $0x3, %r13d
jne 0x29cd
movzbl %bpl, %ebp
movzbl %r14b, %edx
movzwl %r15w, %ecx
movl %ebx, %edi
movl %ebp, %esi
callq 0x3b5e
addb $0x2, %r14b
shrl $0x10, %r15d
movzbl %r14b, %edx
movzbl %r15b, %ecx
movl %ebx, %edi
movl %ebp, %esi
jmp 0x29db
leaq 0x5876(%rip), %r12      # 0x8170
leaq 0x2f4b(%rip), %rsi      # 0x584c
movl 0x10(%rsp), %edx
leaq 0x2e22(%rip), %r15      # 0x572e
xorl %r13d, %r13d
movl %edx, 0x8(%rsp)
testl %edx, %edx
js 0x291d
cmpl %edx, (%r12)
jne 0x2978
leaq 0x274a(%rip), %rdi      # 0x506e
xorl %eax, %eax
callq 0x20a0
movl 0x8(%r12), %r14d
testl %r14d, %r14d
jle 0x296a
movl 0x4(%r12), %ebp
addl %r14d, %ebp
decb %bpl
movzbl (%r12), %r12d
movzbl %bpl, %ebp
movl %ebx, %edi
movl %r12d, %esi
movl %ebp, %edx
callq 0x2b2d
movzbl %al, %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x20a0
decb %bpl
decl %r14d
jne 0x2945
movl $0xa, %edi
callq 0x2040
movl 0x8(%rsp), %edx
incq %r13
leaq (,%r13,2), %rax
addq %r13, %rax
leaq 0x57e3(%rip), %rcx      # 0x8170
leaq (%rcx,%rax,8), %r12
movq 0x10(%r12), %rsi
testq %rsi, %rsi
jne 0x2913
jmp 0x2779
leaq 0x38(%rsp), %rsi
movl %ebx, %edi
callq 0x3c0e
jmp 0x2779
movzbl %bpl, %esi
movzbl %r14b, %edx
movzwl %r15w, %ecx
movl %ebx, %edi
callq 0x3b5e
jmp 0x2779
movzbl %bpl, %esi
movzbl %r14b, %edx
movzbl %r15b, %ecx
movl %ebx, %edi
callq 0x3b94
jmp 0x2779