// File: /workspace/github_c_makefile_star2_O3/koendv[P]stm32duino-raspberrypi/Arduino_Tools/src/stm32flash/main.c
// Function: parse_options
int parse_options(int argc, char *argv[])
{
	int c;
	char *pLen;

	while ((c = getopt(argc, argv, "a:b:m:r:w:e:vn:g:jkfcChuos:S:F:i:R")) != -1) {
		switch(c) {
			case 'a':
				port_opts.bus_addr = strtoul(optarg, NULL, 0);
				break;

			case 'b':
				port_opts.baudRate = serial_get_baud(strtoul(optarg, NULL, 0));
				if (port_opts.baudRate == SERIAL_BAUD_INVALID) {
					serial_baud_t baudrate;
					fprintf(stderr,	"Invalid baud rate, valid options are:\n");
					for (baudrate = SERIAL_BAUD_1200; baudrate != SERIAL_BAUD_INVALID; ++baudrate)
						fprintf(stderr, " %d\n", serial_get_baud_int(baudrate));
					return 1;
				}
				break;

			case 'm':
				if (strlen(optarg) != 3
					|| serial_get_bits(optarg) == SERIAL_BITS_INVALID
					|| serial_get_parity(optarg) == SERIAL_PARITY_INVALID
					|| serial_get_stopbit(optarg) == SERIAL_STOPBIT_INVALID) {
					fprintf(stderr, "Invalid serial mode\n");
					return 1;
				}
				port_opts.serial_mode = optarg;
				break;

			case 'r':
			case 'w':
				if (action != ACT_NONE) {
					err_multi_action((c == 'r') ? ACT_READ : ACT_WRITE);
					return 1;
				}
				action = (c == 'r') ? ACT_READ : ACT_WRITE;
				filename = optarg;
				if (filename[0] == '-' && filename[1] == '\0') {
					use_stdinout = 1;
					force_binary = 1;
				}
				break;
			case 'e':
				if (readwrite_len || start_addr) {
					fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n");
					return 1;
				}
				npages = strtoul(optarg, NULL, 0);
				if (npages > STM32_MAX_PAGES || npages < 0) {
					fprintf(stderr, "ERROR: You need to specify a page count between 0 and 255");
					return 1;
				}
				if (!npages)
					no_erase = 1;
				break;
			case 'u':
				if (action != ACT_NONE) {
					err_multi_action(ACT_WRITE_UNPROTECT);
					return 1;
				}
				action = ACT_WRITE_UNPROTECT;
				break;

			case 'j':
				if (action != ACT_NONE) {
					err_multi_action(ACT_READ_PROTECT);
					return 1;
				}
				action = ACT_READ_PROTECT;
				break;

			case 'k':
				if (action != ACT_NONE) {
					err_multi_action(ACT_READ_UNPROTECT);
					return 1;
				}
				action = ACT_READ_UNPROTECT;
				break;

			case 'o':
				if (action != ACT_NONE) {
					err_multi_action(ACT_ERASE_ONLY);
					return 1;
				}
				action = ACT_ERASE_ONLY;
				break;

			case 'v':
				verify = 1;
				break;

			case 'n':
				retry = strtoul(optarg, NULL, 0);
				break;

			case 'g':
				exec_flag = 1;
				execute   = strtoul(optarg, NULL, 0);
				if (execute % 4 != 0) {
					fprintf(stderr, "ERROR: Execution address must be word-aligned\n");
					return 1;
				}
				break;
			case 's':
				if (readwrite_len || start_addr) {
					fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n");
					return 1;
				}
				spage    = strtoul(optarg, NULL, 0);
				break;
			case 'S':
				if (spage || npages) {
					fprintf(stderr, "ERROR: Invalid options, can't specify start page / num pages and start address/length\n");
					return 1;
				} else {
					start_addr = strtoul(optarg, &pLen, 0);
					if (*pLen == ':') {
						pLen++;
						readwrite_len = strtoul(pLen, NULL, 0);
						if (readwrite_len == 0) {
							fprintf(stderr, "ERROR: Invalid options, can't specify zero length\n");
							return 1;
						}
					}
				}
				break;
			case 'F':
				port_opts.rx_frame_max = strtoul(optarg, &pLen, 0);
				if (*pLen == ':') {
					pLen++;
					port_opts.tx_frame_max = strtoul(pLen, NULL, 0);
				}
				if (port_opts.rx_frame_max < 0
				    || port_opts.tx_frame_max < 0) {
					fprintf(stderr, "ERROR: Invalid negative value for option -F\n");
					return 1;
				}
				if (port_opts.rx_frame_max == 0)
					port_opts.rx_frame_max = STM32_MAX_RX_FRAME;
				if (port_opts.tx_frame_max == 0)
					port_opts.tx_frame_max = STM32_MAX_TX_FRAME;
				if (port_opts.rx_frame_max < 20
				    || port_opts.tx_frame_max < 6) {
					fprintf(stderr, "ERROR: current code cannot work with small frames.\n");
					fprintf(stderr, "min(RX) = 20, min(TX) = 6\n");
					return 1;
				}
				if (port_opts.rx_frame_max > STM32_MAX_RX_FRAME) {
					fprintf(stderr, "WARNING: Ignore RX length in option -F\n");
					port_opts.rx_frame_max = STM32_MAX_RX_FRAME;
				}
				if (port_opts.tx_frame_max > STM32_MAX_TX_FRAME) {
					fprintf(stderr, "WARNING: Ignore TX length in option -F\n");
					port_opts.tx_frame_max = STM32_MAX_TX_FRAME;
				}
				break;
			case 'f':
				force_binary = 1;
				break;

			case 'c':
				init_flag = 0;
				break;

			case 'h':
				show_help(argv[0]);
				exit(0);

			case 'i':
				gpio_seq = optarg;
				break;

			case 'R':
				reset_flag = 1;
				break;

			case 'C':
				if (action != ACT_NONE) {
					err_multi_action(ACT_CRC);
					return 1;
				}
				action = ACT_CRC;
				break;
		}
	}

	for (c = optind; c < argc; ++c) {
		if (port_opts.device) {
			fprintf(stderr, "ERROR: Invalid parameter specified\n");
			show_help(argv[0]);
			return 1;
		}
		port_opts.device = argv[c];
	}

	if (port_opts.device == NULL) {
		fprintf(stderr, "ERROR: Device not specified\n");
		show_help(argv[0]);
		return 1;
	}

	if ((action != ACT_WRITE) && verify) {
		fprintf(stderr, "ERROR: Invalid usage, -v is only valid when writing\n");
		show_help(argv[0]);
		return 1;
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x433e(%rip), %r14      # 0x8c80
leaq 0x3d23(%rip), %r12      # 0x866c
movq 0x9688(%rip), %r15      # 0xdfd8
leaq 0x8(%rsp), %r13
movl %edi, 0x10(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2260
leal -0x43(%rax), %ecx
cmpl $0x34, %ecx
ja 0x4c7a
movslq (%r12,%rcx,4), %rcx
addq %r12, %rcx
jmpq *%rcx
xorl %edi, %edi
cmpl $0x72, %eax
setne %dil
incl %edi
cmpl $0x0, 0x9a43(%rip)      # 0xe3d0
jne 0x4da6
movl %edi, 0x9a37(%rip)      # 0xe3d0
movq (%r15), %rax
movq %rax, 0x9a65(%rip)      # 0xe408
cmpb $0x2d, (%rax)
jne 0x4959
cmpb $0x0, 0x1(%rax)
jne 0x4959
movl $0x1, 0x9a30(%rip)      # 0xe3e8
movb $0x1, 0x9a2d(%rip)      # 0xe3ec
jmp 0x4959
movl 0x9a31(%rip), %eax      # 0xe3f8
orl 0x9a2f(%rip), %eax      # 0xe3fc
jne 0x4d93
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x99f3(%rip)      # 0xe3d8
jmp 0x4959
movq %r12, %rbp
movq %rbx, %r12
movq %r14, %rbx
movq %r13, %r14
movq (%r15), %r13
movq %r13, %rdi
callq 0x20b0
cmpq $0x3, %rax
jne 0x4d5b
movq %r13, %rdi
callq 0x5018
cmpl $0x4, %eax
je 0x4d5b
movq (%r15), %rdi
callq 0x5044
cmpl $0x3, %eax
je 0x4d5b
movq (%r15), %rdi
callq 0x50bd
cmpl $0x2, %eax
je 0x4d5b
movq (%r15), %rax
movq %rax, 0x9850(%rip)      # 0xe298
movq %r14, %r13
movq %rbx, %r14
movq %r12, %rbx
movq %rbp, %r12
movl 0x10(%rsp), %ebp
jmp 0x4959
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x9831(%rip)      # 0xe2a0
jmp 0x4959
cmpl $0x0, 0x9955(%rip)      # 0xe3d0
jne 0x4d8c
movl $0x5, 0x9945(%rip)      # 0xe3d0
jmp 0x4959
movb $0x1, 0x994a(%rip)      # 0xe3e1
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x993b(%rip)      # 0xe3e4
testb $0x3, %al
je 0x4959
jmp 0x4d69
movb $0x1, 0x9930(%rip)      # 0xe3ed
jmp 0x4959
movl 0x990c(%rip), %eax      # 0xe3d4
orl 0x990a(%rip), %eax      # 0xe3d8
jne 0x4d70
movq (%r15), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x9911(%rip)      # 0xe3f8
movq 0x8(%rsp), %rdi
cmpb $0x3a, (%rdi)
jne 0x4959
incq %rdi
movq %rdi, 0x8(%rsp)
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x98f0(%rip)      # 0xe3fc
testl %eax, %eax
jne 0x4959
jmp 0x4dd4
movl 0x98d9(%rip), %eax      # 0xe3f8
orl 0x98d7(%rip), %eax      # 0xe3fc
jne 0x4d7e
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x9897(%rip)      # 0xe3d4
cmpl $0x10000, %eax          # imm = 0x10000
jae 0x4dc6
testl %eax, %eax
jne 0x4959
movl $0x1, 0x9882(%rip)      # 0xe3dc
jmp 0x4959
cmpl $0x0, 0x986a(%rip)      # 0xe3d0
jne 0x4d77
movl $0x7, 0x985a(%rip)      # 0xe3d0
jmp 0x4959
movq (%r15), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x2270
movq %rax, %rcx
movl %ecx, 0x9713(%rip)      # 0xe2a4
movq 0x8(%rsp), %rdi
cmpb $0x3a, (%rdi)
jne 0x4c85
incq %rdi
movq %rdi, 0x8(%rsp)
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x96f2(%rip)      # 0xe2a8
movl 0x96e8(%rip), %ecx      # 0xe2a4
jmp 0x4c8b
cmpl $0x0, 0x9808(%rip)      # 0xe3d0
jne 0x4d85
movl $0x6, 0x97f8(%rip)      # 0xe3d0
jmp 0x4959
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, %edi
callq 0x4e84
movl %eax, 0x969a(%rip)      # 0xe290
cmpl $0x13, %eax
jne 0x4959
jmp 0x4d62
movb $0x0, 0x96a9(%rip)      # 0xe2b4
jmp 0x4959
movq (%r15), %rax
movq %rax, 0x97d6(%rip)      # 0xe3f0
jmp 0x4959
cmpl $0x0, 0x97aa(%rip)      # 0xe3d0
jne 0x4d9a
movl $0x3, 0x979a(%rip)      # 0xe3d0
jmp 0x4959
cmpl $0x0, 0x978e(%rip)      # 0xe3d0
jne 0x4da1
movl $0x4, 0x977e(%rip)      # 0xe3d0
jmp 0x4959
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x9647(%rip)      # 0xe2b0
jmp 0x4959
movb $0x1, 0x976b(%rip)      # 0xe3e0
jmp 0x4959
cmpl $-0x1, %eax
jne 0x4959
jmp 0x4d00
movl 0x961d(%rip), %eax      # 0xe2a8
testl %ecx, %ecx
js 0x4dbf
testl %eax, %eax
js 0x4dbf
testl %ecx, %ecx
jne 0x4cae
movl $0x100, 0x95fb(%rip)    # imm = 0x100
movl $0x100, %ecx            # imm = 0x100
testl %eax, %eax
jne 0x4cc1
movl $0x102, 0x95ec(%rip)    # imm = 0x102
movl $0x102, %eax            # imm = 0x102
cmpl $0x14, %ecx
jb 0x4dcd
cmpl $0x5, %eax
jbe 0x4dcd
cmpl $0x101, %ecx            # imm = 0x101
jae 0x4cf0
cmpl $0x103, %eax            # imm = 0x103
jl 0x4959
callq 0x26f8
jmp 0x4959
leaq 0x14(%rsp), %rdi
callq 0x26c4
movl 0x14(%rsp), %eax
jmp 0x4cdb
movq 0x92c9(%rip), %rax      # 0xdfd0
movslq (%rax), %rax
movq 0x9577(%rip), %rcx      # 0xe288
cmpl %ebp, %eax
jge 0x4d34
movslq %ebp, %rdx
testq %rcx, %rcx
jne 0x4de5
movq (%rbx,%rax,8), %rcx
movq %rcx, 0x955c(%rip)      # 0xe288
incq %rax
cmpq %rax, %rdx
jne 0x4d18
testq %rcx, %rcx
je 0x4ddb
xorl %eax, %eax
cmpl $0x2, 0x968a(%rip)      # 0xe3d0
je 0x4db0
cmpb $0x0, 0x9691(%rip)      # 0xe3e0
je 0x4db0
movq %rbx, %rdi
callq 0x28c3
jmp 0x4dab
callq 0x2827
jmp 0x4dab
callq 0x2843
jmp 0x4dab
callq 0x27d3
jmp 0x4dab
callq 0x279b
jmp 0x4dab
movl $0x7, %edi
jmp 0x4da6
callq 0x280b
jmp 0x4dab
movl $0x6, %edi
jmp 0x4da6
movl $0x5, %edi
jmp 0x4da6
callq 0x27b7
jmp 0x4dab
movl $0x3, %edi
jmp 0x4da6
movl $0x4, %edi
callq 0x242d
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2763
jmp 0x4dab
callq 0x27ef
jmp 0x4dab
callq 0x2724
jmp 0x4dab
callq 0x277f
jmp 0x4dab
movq %rbx, %rdi
callq 0x28ec
jmp 0x4dab
movq %rbx, %rdi
callq 0x289a
jmp 0x4dab
movq (%rbx), %rdi
callq 0x247d
xorl %edi, %edi
callq 0x22a0
nop