// File: /workspace/github_c_makefile_star2_O2/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 0x3fbd(%rip), %r14      # 0x7c8c
leaq 0x3976(%rip), %r12      # 0x764c
movq 0x92f3(%rip), %r15      # 0xcfd0
leaq 0x8(%rsp), %r13
movl %edi, 0x14(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2260
leal -0x43(%rax), %ecx
cmpl $0x34, %ecx
ja 0x4007
movslq (%r12,%rcx,4), %rcx
addq %r12, %rcx
jmpq *%rcx
xorl %edi, %edi
cmpl $0x72, %eax
setne %dil
incl %edi
cmpl $0x0, 0x96b6(%rip)      # 0xd3d0
jne 0x40e2
movl %edi, 0x96aa(%rip)      # 0xd3d0
movq (%r15), %rax
movq %rax, 0x96d8(%rip)      # 0xd408
cmpb $0x2d, (%rax)
jne 0x3ce6
cmpb $0x0, 0x1(%rax)
jne 0x3ce6
movl $0x1, 0x96a3(%rip)      # 0xd3e8
movb $0x1, 0x96a0(%rip)      # 0xd3ec
jmp 0x3ce6
movl 0x96a4(%rip), %eax      # 0xd3f8
orl 0x96a2(%rip), %eax      # 0xd3fc
jne 0x417a
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x9666(%rip)      # 0xd3d8
jmp 0x3ce6
movq %r12, %rbp
movq %rbx, %r12
movq %r14, %rbx
movq %r13, %r14
movq (%r15), %r13
movq %r13, %rdi
callq 0x20b0
cmpq $0x3, %rax
jne 0x4162
movq %r13, %rdi
callq 0x458f
cmpl $0x4, %eax
je 0x4162
movq (%r15), %rdi
callq 0x45b7
cmpl $0x3, %eax
je 0x4162
movq (%r15), %rdi
callq 0x4625
cmpl $0x2, %eax
je 0x4162
movq (%r15), %rax
movq %rax, 0x94c3(%rip)      # 0xd298
movq %r14, %r13
movq %rbx, %r14
movq %r12, %rbx
movq %rbp, %r12
movl 0x14(%rsp), %ebp
jmp 0x3ce6
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x94a4(%rip)      # 0xd2a0
jmp 0x3ce6
cmpl $0x0, 0x95c8(%rip)      # 0xd3d0
jne 0x40d3
movl $0x5, 0x95b8(%rip)      # 0xd3d0
jmp 0x3ce6
movb $0x1, 0x95bd(%rip)      # 0xd3e1
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x95ae(%rip)      # 0xd3e4
testb $0x3, %al
je 0x3ce6
jmp 0x4192
movb $0x1, 0x95a3(%rip)      # 0xd3ed
jmp 0x3ce6
movl 0x957f(%rip), %eax      # 0xd3d4
orl 0x957d(%rip), %eax      # 0xd3d8
jne 0x417a
movq (%r15), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x9584(%rip)      # 0xd3f8
movq 0x8(%rsp), %rdi
cmpb $0x3a, (%rdi)
jne 0x3ce6
incq %rdi
movq %rdi, 0x8(%rsp)
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x9563(%rip)      # 0xd3fc
testl %eax, %eax
jne 0x3ce6
jmp 0x425e
movl 0x954c(%rip), %eax      # 0xd3f8
orl 0x954a(%rip), %eax      # 0xd3fc
jne 0x417a
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x950a(%rip)      # 0xd3d4
cmpl $0x10000, %eax          # imm = 0x10000
jae 0x4218
testl %eax, %eax
jne 0x3ce6
movl $0x1, 0x94f5(%rip)      # 0xd3dc
jmp 0x3ce6
cmpl $0x0, 0x94dd(%rip)      # 0xd3d0
jne 0x40d7
movl $0x7, 0x94cd(%rip)      # 0xd3d0
jmp 0x3ce6
movq (%r15), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x2270
movq %rax, %rcx
movl %ecx, 0x9386(%rip)      # 0xd2a4
movq 0x8(%rsp), %rdi
cmpb $0x3a, (%rdi)
jne 0x4015
incq %rdi
movq %rdi, 0x8(%rsp)
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x9365(%rip)      # 0xd2a8
movl 0x935b(%rip), %ecx      # 0xd2a4
jmp 0x401b
cmpl $0x0, 0x947b(%rip)      # 0xd3d0
jne 0x40cf
movl $0x6, 0x946b(%rip)      # 0xd3d0
jmp 0x3ce6
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, %edi
callq 0x4474
movl %eax, 0x930d(%rip)      # 0xd290
cmpl $0x13, %eax
jne 0x3ce6
jmp 0x41aa
movb $0x0, 0x931c(%rip)      # 0xd2b4
jmp 0x3ce6
movq (%r15), %rax
movq %rax, 0x9449(%rip)      # 0xd3f0
jmp 0x3ce6
cmpl $0x0, 0x941d(%rip)      # 0xd3d0
jne 0x40df
movl $0x3, 0x940d(%rip)      # 0xd3d0
jmp 0x3ce6
cmpl $0x0, 0x9401(%rip)      # 0xd3d0
jne 0x40db
movl $0x4, 0x93f1(%rip)      # 0xd3d0
jmp 0x3ce6
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x92ba(%rip)      # 0xd2b0
jmp 0x3ce6
movb $0x1, 0x93de(%rip)      # 0xd3e0
jmp 0x3ce6
cmpl $-0x1, %eax
jne 0x3ce6
jmp 0x40fb
movl 0x928d(%rip), %eax      # 0xd2a8
testl %ecx, %ecx
js 0x4203
testl %eax, %eax
js 0x4203
testl %ecx, %ecx
jne 0x403e
movl $0x100, 0x926b(%rip)    # imm = 0x100
movl $0x100, %ecx            # imm = 0x100
testl %eax, %eax
jne 0x4051
movl $0x102, 0x925c(%rip)    # imm = 0x102
movl $0x102, %eax            # imm = 0x102
cmpl $0x14, %ecx
jb 0x422d
cmpl $0x5, %eax
jbe 0x422d
cmpl $0x101, %ecx            # imm = 0x101
jae 0x40a1
cmpl $0x103, %eax            # imm = 0x103
jl 0x3ce6
movq 0x8f73(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x3dfb(%rip), %rdi      # 0x7e82
pushq $0x27
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
movl $0x102, 0x920c(%rip)    # imm = 0x102
jmp 0x3ce6
movq 0x8f48(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x3da8(%rip), %rdi      # 0x7e5a
pushq $0x27
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
movl $0x100, 0x91dd(%rip)    # imm = 0x100
movl 0x91db(%rip), %eax      # 0xd2a8
jmp 0x406b
pushq $0x6
jmp 0x40e1
pushq $0x5
jmp 0x40e1
pushq $0x7
jmp 0x40e1
pushq $0x4
jmp 0x40e1
pushq $0x3
popq %rdi
callq 0x4336
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8ec6(%rip), %rax      # 0xcfc8
movslq (%rax), %rax
movq 0x917c(%rip), %rdx      # 0xd288
movslq %ebp, %rcx
cmpq %rcx, %rax
jge 0x412d
testq %rdx, %rdx
jne 0x427f
movq (%rbx,%rax,8), %rdx
movq %rdx, 0x9160(%rip)      # 0xd288
incq %rax
jmp 0x410f
testq %rdx, %rdx
je 0x4294
xorl %ebp, %ebp
cmpl $0x2, 0x9291(%rip)      # 0xd3d0
je 0x40ea
cmpb $0x0, 0x9298(%rip)      # 0xd3e0
je 0x40ea
movq 0x8e9f(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x3d90(%rip), %rdi      # 0x7eeb
pushq $0x34
jmp 0x42a7
movq 0x8e87(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x3b63(%rip), %rdi      # 0x7cd6
pushq $0x14
jmp 0x4271
movq 0x8e6f(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x3b60(%rip), %rdi      # 0x7ceb
pushq $0x56
jmp 0x4271
movq 0x8e57(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x3bd9(%rip), %rdi      # 0x7d7c
pushq $0x2e
jmp 0x4271
movq 0x8e3f(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x3af4(%rip), %rdi      # 0x7caf
pushq $0x26
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
pushq $0x1
popq %rbp
leaq 0x358e(%rip), %rbx      # 0x775e
xorl %r14d, %r14d
cmpl $0x13, %r14d
je 0x40ea
movq 0x8e0c(%rip), %rax      # 0xcff0
movq (%rax), %r15
movl %r14d, %edi
callq 0x457b
movq %r15, %rdi
movq %rbx, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x2170
incl %r14d
jmp 0x41d3
movq 0x8de6(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x3bca(%rip), %rdi      # 0x7dde
pushq $0x2c
jmp 0x4271
movq 0x8dd1(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x3b19(%rip), %rdi      # 0x7d42
pushq $0x39
jmp 0x4271
movq 0x8dbc(%rip), %r14      # 0xcff0
movq (%r14), %rcx
leaq 0x3bcd(%rip), %rdi      # 0x7e0b
pushq $0x33
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x22b0
movq (%r14), %rcx
leaq 0x3be9(%rip), %rdi      # 0x7e3f
pushq $0x1a
popq %rsi
movq %rbx, %rdx
jmp 0x4275
movq 0x8d8b(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x3b3c(%rip), %rdi      # 0x7dab
pushq $0x32
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
jmp 0x40e7
movq 0x8d6a(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x3c1a(%rip), %rdi      # 0x7eaa
pushq $0x23
jmp 0x42a7
movq 0x8d55(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x3c29(%rip), %rdi      # 0x7ece
pushq $0x1c
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
movq (%rbx), %rdi
callq 0x4378
jmp 0x40e7
movq (%rbx), %rdi
callq 0x4378
xorl %edi, %edi
callq 0x22a0