// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl $0x0, -0x68(%rbp)
movl $0xffffffff, -0x6c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x70(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x74(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x3d93(%rip), %rdx      # 0x60cc
callq 0x2170
movl %eax, -0x50(%rbp)
cmpl $-0x1, %eax
je 0x23d0
movl -0x50(%rbp), %eax
addl $-0x46, %eax
movl %eax, %ecx
movq %rcx, -0x98(%rbp)
subl $0x31, %eax
ja 0x23c2
movq -0x98(%rbp), %rax
leaq 0x3c98(%rip), %rcx      # 0x6004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x1, -0x54(%rbp)
jmp 0x23cb
movl $0x1, -0x44(%rbp)
jmp 0x23cb
movl $0x1, -0x48(%rbp)
jmp 0x23cb
movq 0x7c41(%rip), %rax      # 0x9fd8
movq (%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x23cb
movl $0x1, -0x64(%rbp)
jmp 0x23cb
movl $0x1, -0x68(%rbp)
jmp 0x23cb
movl $0x1, -0x60(%rbp)
movl $0x2, -0x74(%rbp)
jmp 0x23cb
movq -0x28(%rbp), %rdi
callq 0x2af0
jmp 0x232b
movq 0x7bf9(%rip), %rax      # 0x9fd0
movl (%rax), %ecx
movl -0x8(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x8(%rbp)
movq 0x7be8(%rip), %rax      # 0x9fd0
movl (%rax), %ecx
movq -0x10(%rbp), %rax
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
cmpl $0x1, -0x8(%rbp)
jl 0x2432
movl -0x54(%rbp), %eax
addl -0x64(%rbp), %eax
addl -0x68(%rbp), %eax
cmpq $0x0, -0x38(%rbp)
setne %cl
andb $0x1, %cl
movzbl %cl, %ecx
addl %ecx, %eax
cmpl $0x1, %eax
jg 0x2432
cmpl $0x0, -0x60(%rbp)
je 0x243b
cmpl $0x0, -0x64(%rbp)
jne 0x243b
cmpl $0x0, -0x68(%rbp)
jne 0x243b
movq -0x28(%rbp), %rdi
callq 0x2af0
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
movq -0x30(%rbp), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x2160
movl %eax, -0x5c(%rbp)
cmpl $0x0, %eax
jge 0x248a
movq -0x30(%rbp), %rdx
movl $0x42, %edi
leaq 0x3c52(%rip), %rsi      # 0x60d5
movb $0x0, %al
callq 0x2140
movq -0x30(%rbp), %rdi
callq 0x2bb0
movl -0x5c(%rbp), %edi
leaq -0x20(%rbp), %rsi
callq 0x2c00
cmpl $0x0, %eax
je 0x24bb
movq -0x30(%rbp), %rdx
movl $0x42, %edi
leaq 0x3c2f(%rip), %rsi      # 0x60e3
movb $0x0, %al
callq 0x2140
leaq 0x3c38(%rip), %rdi      # 0x60fa
callq 0x2d10
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %eax
xorl $-0x1, %eax
andl $0x18000001, %eax       # imm = 0x18000001
cmpl $0x0, %eax
je 0x24f9
movl -0x78(%rbp), %edx
movl $0x45, %edi
leaq 0x3c11(%rip), %rsi      # 0x6103
movb $0x0, %al
callq 0x2070
cmpl $0x0, -0x54(%rbp)
je 0x268c
movl -0x5c(%rbp), %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x2e40
movzbl %al, %eax
movl %eax, -0x84(%rbp)
movl -0x5c(%rbp), %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2e40
movzbl %al, %eax
movl %eax, -0x88(%rbp)
movl -0x5c(%rbp), %edi
xorl %esi, %esi
movl $0x3, %edx
callq 0x2e40
movzbl %al, %eax
movl %eax, -0x8c(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x84(%rbp), %eax
jge 0x25e4
movl -0x6c(%rbp), %esi
leaq 0x3bd4(%rip), %rdi      # 0x613f
movb $0x0, %al
callq 0x2080
movl $0x0, -0x70(%rbp)
cmpl $0x100, -0x70(%rbp)     # imm = 0x100
jge 0x25d4
movl -0x5c(%rbp), %edi
movl -0x6c(%rbp), %eax
movb %al, %cl
movl -0x70(%rbp), %eax
movzbl %cl, %esi
movzbl %al, %edx
callq 0x2e40
movzbl %al, %esi
leaq 0x434b(%rip), %rdi      # 0x68ed
movb $0x0, %al
callq 0x2080
movl -0x70(%rbp), %eax
movl $0x10, %ecx
cltd
idivl %ecx
cmpl $0xf, %edx
jne 0x25c7
leaq 0x5596(%rip), %rdi      # 0x7b56
movb $0x0, %al
callq 0x2080
jmp 0x25c9
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x2579
jmp 0x25d6
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x2552
movl -0x88(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movl -0x88(%rbp), %ecx
addl -0x8c(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2687
movl -0x6c(%rbp), %esi
leaq 0x3b31(%rip), %rdi      # 0x613f
movb $0x0, %al
callq 0x2080
movl $0x0, -0x70(%rbp)
cmpl $0x100, -0x70(%rbp)     # imm = 0x100
jge 0x2677
movl -0x5c(%rbp), %edi
movl -0x6c(%rbp), %eax
movb %al, %cl
movl -0x70(%rbp), %eax
movzbl %cl, %esi
movzbl %al, %edx
callq 0x2e40
movzbl %al, %esi
leaq 0x42a8(%rip), %rdi      # 0x68ed
movb $0x0, %al
callq 0x2080
movl -0x70(%rbp), %eax
movl $0x10, %ecx
cltd
idivl %ecx
cmpl $0xf, %edx
jne 0x266a
leaq 0x54f3(%rip), %rdi      # 0x7b56
movb $0x0, %al
callq 0x2080
jmp 0x266c
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x261c
jmp 0x2679
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x25ed
jmp 0x2ad7
cmpl $0x0, -0x44(%rbp)
je 0x26c5
movl -0x78(%rbp), %eax
xorl $-0x1, %eax
andl $0x8200001, %eax        # imm = 0x8200001
cmpl $0x0, %eax
je 0x26b8
movl -0x78(%rbp), %edx
movl $0x45, %edi
leaq 0x3a52(%rip), %rsi      # 0x6103
movb $0x0, %al
callq 0x2070
movl -0x5c(%rbp), %edi
callq 0x2ee0
jmp 0x2ad7
cmpq $0x0, -0x38(%rbp)
je 0x270a
movl -0x78(%rbp), %eax
xorl $-0x1, %eax
andl $0x1bc00001, %eax       # imm = 0x1BC00001
cmpl $0x0, %eax
je 0x26f2
movl -0x78(%rbp), %edx
movl $0x45, %edi
leaq 0x3a18(%rip), %rsi      # 0x6103
movb $0x0, %al
callq 0x2070
movl -0x5c(%rbp), %edi
movq -0x38(%rbp), %rdx
movl -0x48(%rbp), %ecx
leaq -0x20(%rbp), %rsi
callq 0x2fa0
jmp 0x2ad7
cmpl $0x0, -0x64(%rbp)
jne 0x271a
cmpl $0x0, -0x68(%rbp)
je 0x2811
cmpl $0x1, -0x8(%rbp)
jge 0x2734
cmpl $0x0, -0x68(%rbp)
je 0x2734
movq -0x28(%rbp), %rdi
callq 0x2af0
jmp 0x280f
cmpl $0x1, -0x8(%rbp)
jge 0x273f
jmp 0x280d
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x6c(%rbp), %rsi
leaq -0x70(%rbp), %rdx
leaq -0x74(%rbp), %rcx
callq 0x4070
cmpl $0x0, %eax
je 0x2776
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x280b
cmpl $0x1, -0x8(%rbp)
jge 0x2785
movq -0x28(%rbp), %rdi
callq 0x2af0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x40(%rbp), %rsi
xorl %edx, %edx
callq 0x2120
movl %eax, -0x6c(%rbp)
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x27af
movq -0x28(%rbp), %rdi
callq 0x2af0
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
cmpl $0x1, -0x8(%rbp)
jl 0x2809
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x40(%rbp), %rsi
xorl %edx, %edx
callq 0x2120
movl %eax, -0x70(%rbp)
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x27f4
movq -0x28(%rbp), %rdi
callq 0x2af0
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x280b
jmp 0x280d
jmp 0x280f
jmp 0x2811
cmpl $0x0, -0x64(%rbp)
je 0x299f
cmpl $0x0, -0x70(%rbp)
jl 0x287e
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x74(%rbp), %eax
jge 0x286b
movl -0x5c(%rbp), %edi
movl -0x6c(%rbp), %eax
movb %al, %cl
movl -0x70(%rbp), %eax
addl -0x74(%rbp), %eax
subl -0x4c(%rbp), %eax
subl $0x1, %eax
movzbl %cl, %esi
movzbl %al, %edx
callq 0x2e40
movzbl %al, %esi
leaq 0x4095(%rip), %rdi      # 0x68ee
movb $0x0, %al
callq 0x2080
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x2828
leaq 0x52e4(%rip), %rdi      # 0x7b56
movb $0x0, %al
callq 0x2080
jmp 0x2ad7
movl $0x0, -0x58(%rbp)
movslq -0x58(%rbp), %rcx
leaq 0x7860(%rip), %rax      # 0xa0f0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x10(%rax)
je 0x299a
cmpl $0x0, -0x6c(%rbp)
jl 0x28c6
movslq -0x58(%rbp), %rcx
leaq 0x783d(%rip), %rax      # 0xa0f0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0x6c(%rbp), %eax
je 0x28c6
jmp 0x298c
movslq -0x58(%rbp), %rcx
leaq 0x781f(%rip), %rax      # 0xa0f0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rsi
leaq 0x386a(%rip), %rdi      # 0x614d
movb $0x0, %al
callq 0x2080
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movslq -0x58(%rbp), %rdx
leaq 0x77f1(%rip), %rcx      # 0xa0f0
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jge 0x297e
movl -0x5c(%rbp), %edi
movslq -0x58(%rbp), %rcx
leaq 0x77d7(%rip), %rax      # 0xa0f0
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
movb %al, %cl
movslq -0x58(%rbp), %rdx
leaq 0x77c1(%rip), %rax      # 0xa0f0
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl 0x4(%rax), %eax
movslq -0x58(%rbp), %rsi
leaq 0x77ac(%rip), %rdx      # 0xa0f0
imulq $0x18, %rsi, %rsi
addq %rsi, %rdx
addl 0x8(%rdx), %eax
subl -0x4c(%rbp), %eax
subl $0x1, %eax
movzbl %cl, %esi
movzbl %al, %edx
callq 0x2e40
movzbl %al, %esi
leaq 0x3f85(%rip), %rdi      # 0x68ee
movb $0x0, %al
callq 0x2080
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x28f1
leaq 0x51d1(%rip), %rdi      # 0x7b56
movb $0x0, %al
callq 0x2080
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x2885
jmp 0x2ad7
cmpl $0x0, -0x68(%rbp)
je 0x2acb
cmpl $0x1, -0x8(%rbp)
jl 0x29c1
cmpl $0x3, -0x74(%rbp)
jg 0x29c1
cmpl $0x0, -0x6c(%rbp)
jl 0x29c1
cmpl $0x0, -0x70(%rbp)
jge 0x29ca
movq -0x28(%rbp), %rdi
callq 0x2af0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x40(%rbp), %rsi
xorl %edx, %edx
callq 0x2120
movl %eax, -0x7c(%rbp)
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x2a30
movl -0x7c(%rbp), %eax
movl %eax, -0x9c(%rbp)
cmpl $0x3, -0x74(%rbp)
jne 0x2a07
movl $0x1000000, %eax        # imm = 0x1000000
movl %eax, -0xa0(%rbp)
jmp 0x2a20
movl -0x74(%rbp), %edx
movl $0xff, %eax
movl $0xffff, %ecx           # imm = 0xFFFF
cmpl $0x2, %edx
cmovel %ecx, %eax
movl %eax, -0xa0(%rbp)
movl -0x9c(%rbp), %eax
movl -0xa0(%rbp), %ecx
cmpl %ecx, %eax
jbe 0x2a39
movq -0x28(%rbp), %rdi
callq 0x2af0
cmpl $0x3, -0x74(%rbp)
jne 0x2a83
movl -0x5c(%rbp), %edi
movl -0x6c(%rbp), %eax
movb %al, %dl
movl -0x70(%rbp), %eax
movb %al, %cl
movl -0x7c(%rbp), %eax
movzbl %dl, %esi
movzbl %cl, %edx
movzwl %ax, %ecx
callq 0x4210
movl -0x5c(%rbp), %edi
movl -0x6c(%rbp), %eax
movb %al, %dl
movl -0x70(%rbp), %eax
addl $0x2, %eax
movb %al, %cl
movl -0x7c(%rbp), %eax
shrl $0x10, %eax
movzbl %dl, %esi
movzbl %cl, %edx
movzbl %al, %ecx
callq 0x4280
jmp 0x2ac9
cmpl $0x2, -0x74(%rbp)
jne 0x2aa9
movl -0x5c(%rbp), %edi
movl -0x6c(%rbp), %eax
movb %al, %dl
movl -0x70(%rbp), %eax
movb %al, %cl
movl -0x7c(%rbp), %eax
movzbl %dl, %esi
movzbl %cl, %edx
movzwl %ax, %ecx
callq 0x4210
jmp 0x2ac7
movl -0x5c(%rbp), %edi
movl -0x6c(%rbp), %eax
movb %al, %dl
movl -0x70(%rbp), %eax
movb %al, %cl
movl -0x7c(%rbp), %eax
movzbl %dl, %esi
movzbl %cl, %edx
movzbl %al, %ecx
callq 0x4280
jmp 0x2ac9
jmp 0x2ad7
movl -0x5c(%rbp), %edi
leaq -0x20(%rbp), %rsi
callq 0x4330
movl -0x5c(%rbp), %edi
callq 0x20e0
xorl %edi, %edi
callq 0x2190
nopw %cs:(%rax,%rax)