// File: /workspace/github_c_makefile_star2_O3/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, %ebp
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x10(%rsp)
movl %eax, 0x14(%rsp)
movq (%rsi), %rax
movq %rax, 0x18(%rsp)
movb $0x1, %al
movl %eax, 0x34(%rsp)
movq $0x0, 0x48(%rsp)
movl $0x1, %r15d
leaq 0x2cf4(%rip), %r13      # 0x5004
movq 0x6cb9(%rip), %r14      # 0x8fd0
movq $0x0, 0x20(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x2c(%rsp)
movl $0x0, 0x30(%rsp)
xorl %r12d, %r12d
movl %ebp, %edi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x2180
cmpl $0x66, %eax
jne 0x2352
movq (%r14), %r12
jmp 0x233b
cmpl $0x67, %eax
jg 0x2376
cmpl $0x48, %eax
jle 0x2393
cmpl $0x49, %eax
je 0x23a2
cmpl $0x64, %eax
jne 0x256e
movl $0x1, %eax
movq %rax, 0x48(%rsp)
jmp 0x233b
cmpl $0x68, %eax
je 0x23b8
cmpl $0x72, %eax
je 0x23ac
cmpl $0x77, %eax
jne 0x256e
movl $0x1, 0xc(%rsp)
jmp 0x233b
cmpl $0x46, %eax
jne 0x23cb
movl $0x1, 0x30(%rsp)
jmp 0x233b
movl $0x1, 0x2c(%rsp)
jmp 0x233b
movl $0x1, %eax
movq %rax, 0x20(%rsp)
jmp 0x233b
movl $0x2, %r15d
movl $0x0, 0x34(%rsp)
jmp 0x233b
cmpl $-0x1, %eax
jne 0x256e
movl %r15d, 0x28(%rsp)
movq 0x6be8(%rip), %rax      # 0x8fc8
movslq (%rax), %rax
subl %eax, %ebp
movl 0xc(%rsp), %r15d
jle 0x256e
movq 0x20(%rsp), %rcx
movq 0x48(%rsp), %rdx
addl %edx, %ecx
addl %r15d, %ecx
cmpq $0x1, %r12
sbbl $-0x1, %ecx
cmpl $0x1, %ecx
ja 0x256e
cmpl $0x0, 0x20(%rsp)
setne %cl
movl 0x34(%rsp), %edx
orb %cl, %dl
testl %r15d, %r15d
setne %cl
orb %dl, %cl
testb $0x1, %cl
je 0x256e
leaq (%rbx,%rax,8), %r14
movq (%r14), %r13
movq %r13, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x2170
testl %eax, %eax
jns 0x245e
leaq 0x2bbe(%rip), %rsi      # 0x500d
movl $0x42, %edi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2150
movl %eax, %ebx
movq %r13, %rdi
movl $0x2f, %esi
callq 0x20d0
leaq 0x1(%rax), %rcx
testq %rax, %rax
cmoveq %r13, %rcx
movq %rcx, 0x6c81(%rip)      # 0x9100
leaq 0x2cd0(%rip), %rdi      # 0x5156
callq 0x2a38
rolw $0x8, %ax
movw %ax, 0x38(%rsp)
leaq 0x2ccc(%rip), %rdi      # 0x5167
callq 0x2a38
rolw $0x8, %ax
movw %ax, 0x3a(%rsp)
leaq 0x2cc8(%rip), %rdi      # 0x5178
callq 0x2a38
rolw $0x8, %ax
movw %ax, 0x3c(%rsp)
leaq 0x2c87(%rip), %rdi      # 0x514c
callq 0x2a38
rolw $0x8, %ax
movw %ax, 0x3e(%rsp)
leaq 0x2c83(%rip), %rdi      # 0x515d
callq 0x2a38
rolw $0x8, %ax
movw %ax, 0x40(%rsp)
leaq 0x2c7f(%rip), %rdi      # 0x516e
callq 0x2a38
rolw $0x8, %ax
movw %ax, 0x42(%rsp)
leaq 0x2c7b(%rip), %rdi      # 0x517f
callq 0x2a38
movl %eax, 0x44(%rsp)
leaq 0x2b07(%rip), %rdi      # 0x501b
callq 0x2a38
movl %eax, %edx
orl $0x1, %edx
movl %eax, %ecx
notl %ecx
testl $0x18000000, %ecx       # imm = 0x18000000
jne 0x26ca
cmpl $0x0, 0x48(%rsp)
jne 0x2578
cmpl $0x0, 0x30(%rsp)
jne 0x2698
testq %r12, %r12
jne 0x26aa
movq 0x20(%rsp), %rax
orl %r15d, %eax
je 0x28ad
testl %r15d, %r15d
setne %al
cmpl $0x1, %ebp
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x26dd
movq 0x18(%rsp), %rdi
callq 0x29a8
movl %ebx, %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x2af0
movl %eax, %ebp
movl %ebx, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2af0
movb %al, 0x20(%rsp)
movl %ebx, %edi
xorl %esi, %esi
movl $0x3, %edx
callq 0x2af0
movzbl %al, %eax
movl %eax, 0xc(%rsp)
testb %bpl, %bpl
je 0x261c
movzbl %bpl, %ebp
leaq 0x316e(%rip), %r15      # 0x572d
xorl %r12d, %r12d
xorl %r14d, %r14d
leaq 0x2a94(%rip), %rdi      # 0x5060
movl %r12d, %esi
xorl %eax, %eax
callq 0x20a0
movzbl %r12b, %r13d
movzbl %r14b, %edx
movl %ebx, %edi
movl %r13d, %esi
callq 0x2af0
movzbl %al, %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x20a0
movl %r14d, %eax
notl %eax
testb $0xf, %al
jne 0x2608
movl $0xa, %edi
callq 0x2040
incl %r14d
cmpl $0x100, %r14d           # imm = 0x100
jne 0x25da
incl %r12d
cmpl %ebp, %r12d
jne 0x25c2
cmpl $0x0, 0xc(%rsp)
je 0x28b9
movzbl 0x20(%rsp), %ebp
addl %ebp, 0xc(%rsp)
leaq 0x2a29(%rip), %r13      # 0x5060
leaq 0x30ef(%rip), %r15      # 0x572d
movq %r13, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x20a0
movzbl %bpl, %r12d
xorl %r14d, %r14d
movzbl %r14b, %edx
movl %ebx, %edi
movl %r12d, %esi
callq 0x2af0
movzbl %al, %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x20a0
movl %r14d, %eax
notl %eax
testb $0xf, %al
jne 0x267f
movl $0xa, %edi
callq 0x2040
incl %r14d
cmpl $0x100, %r14d           # imm = 0x100
jne 0x2651
incl %ebp
cmpl 0xc(%rsp), %ebp
jb 0x263e
jmp 0x28b9
btl $0x15, %eax
jae 0x26ca
movl %ebx, %edi
callq 0x2b68
jmp 0x28b9
testl $0x3c00000, %ecx        # imm = 0x3C00000
jne 0x26ca
leaq 0x38(%rsp), %rsi
movl %ebx, %edi
movq %r12, %rdx
movl 0x2c(%rsp), %ecx
callq 0x2bf6
jmp 0x28b9
leaq 0x2953(%rip), %rsi      # 0x5024
movl $0x45, %edi
xorl %eax, %eax
callq 0x2090
movq %r14, %r13
leaq 0x8(%r14), %rax
xorl %r14d, %r14d
cmpl $0x1, %ebp
je 0x277d
movq (%rax), %r15
leaq 0x10(%rsp), %rsi
leaq 0x14(%rsp), %rdx
leaq 0x28(%rsp), %rcx
movq %r15, %rdi
callq 0x3a55
testl %eax, %eax
jne 0x276b
leaq 0x50(%rsp), %r12
xorl %r14d, %r14d
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x2130
movl %eax, 0x10(%rsp)
movq (%r12), %rax
cmpb $0x0, (%rax)
jne 0x256e
leaq 0x10(%r13), %rax
cmpl $0x2, %ebp
je 0x2778
movq (%rax), %rdi
leaq 0x50(%rsp), %r15
movq %r15, %rsi
xorl %edx, %edx
callq 0x2130
movl %eax, 0x14(%rsp)
movq (%r15), %rax
cmpb $0x0, (%rax)
jne 0x256e
addl $-0x3, %ebp
movq %r13, %rax
addq $0x18, %rax
jmp 0x2775
addl $-0x2, %ebp
movq %r13, %rax
addq $0x10, %rax
movl %ebp, %r14d
movl 0xc(%rsp), %r15d
cmpl $0x0, 0x20(%rsp)
je 0x27e1
movl 0x14(%rsp), %ebp
testl %ebp, %ebp
js 0x28c7
movl 0x28(%rsp), %r12d
testl %r12d, %r12d
jle 0x27d2
addl %r12d, %ebp
decb %bpl
movzbl 0x10(%rsp), %r14d
leaq 0x2f81(%rip), %r15      # 0x572e
movzbl %bpl, %ebp
movl %ebx, %edi
movl %r14d, %esi
movl %ebp, %edx
callq 0x2af0
movzbl %al, %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x20a0
decb %bpl
decl %r12d
jne 0x27ad
movl $0xa, %edi
callq 0x2040
jmp 0x28b9
testl %r15d, %r15d
je 0x28ad
testl %r14d, %r14d
sete %cl
movl 0x28(%rsp), %r13d
cmpl $0x4, %r13d
setge %dl
orb %cl, %dl
movl 0x10(%rsp), %ebp
testl %ebp, %ebp
sets %cl
movl 0x14(%rsp), %r14d
testl %r14d, %r14d
sets %sil
orb %cl, %sil
orb %dl, %sil
cmpb $0x1, %sil
je 0x256e
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 0x256e
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 0x256e
cmpl $0x2, %r13d
je 0x2978
cmpl $0x3, %r13d
jne 0x2990
movzbl %bpl, %ebp
movzbl %r14b, %edx
movzwl %r15w, %ecx
movl %ebx, %edi
movl %ebp, %esi
callq 0x3afa
addb $0x2, %r14b
shrl $0x10, %r15d
movzbl %r14b, %edx
movzbl %r15b, %ecx
movl %ebx, %edi
movl %ebp, %esi
jmp 0x299e
leaq 0x38(%rsp), %rsi
movl %ebx, %edi
callq 0x3ba7
movl %ebx, %edi
callq 0x20f0
xorl %edi, %edi
callq 0x21a0
leaq 0x58a2(%rip), %r12      # 0x8170
leaq 0x2f77(%rip), %rsi      # 0x584c
movl 0x10(%rsp), %edx
leaq 0x2e4e(%rip), %r15      # 0x572e
xorl %r13d, %r13d
movl %edx, 0xc(%rsp)
testl %edx, %edx
js 0x28f1
cmpl %edx, (%r12)
jne 0x294c
leaq 0x2776(%rip), %rdi      # 0x506e
xorl %eax, %eax
callq 0x20a0
movl 0x8(%r12), %r14d
testl %r14d, %r14d
jle 0x293e
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 0x2af0
movzbl %al, %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x20a0
decb %bpl
decl %r14d
jne 0x2919
movl $0xa, %edi
callq 0x2040
movl 0xc(%rsp), %edx
incq %r13
leaq (,%r13,2), %rax
addq %r13, %rax
leaq 0x580f(%rip), %rcx      # 0x8170
leaq (%rcx,%rax,8), %r12
movq 0x10(%r12), %rsi
testq %rsi, %rsi
jne 0x28e7
jmp 0x28b9
movzbl %bpl, %esi
movzbl %r14b, %edx
movzwl %r15w, %ecx
movl %ebx, %edi
callq 0x3afa
jmp 0x28b9
movzbl %bpl, %esi
movzbl %r14b, %edx
movzbl %r15b, %ecx
movl %ebx, %edi
callq 0x3b30
jmp 0x28b9