// File: /workspace/github_c_makefile_star2_O1/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 0x423e(%rip), %r14      # 0x8c6c
leaq 0x3c23(%rip), %r13      # 0x8658
movq 0x959c(%rip), %r15      # 0xdfd8
leaq 0x8(%rsp), %r12
movl %edi, 0x14(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2260
leal -0x43(%rax), %ecx
cmpl $0x34, %ecx
ja 0x4dce
movslq (%r13,%rcx,4), %rcx
addq %r13, %rcx
jmpq *%rcx
xorl %edi, %edi
cmpl $0x72, %eax
setne %dil
incl %edi
cmpl $0x0, 0x9956(%rip)      # 0xe3d0
jne 0x4e8e
movl %edi, 0x994a(%rip)      # 0xe3d0
movq (%r15), %rax
movq %rax, 0x9978(%rip)      # 0xe408
cmpb $0x2d, (%rax)
jne 0x4a45
cmpb $0x0, 0x1(%rax)
jne 0x4a45
movl $0x1, 0x9943(%rip)      # 0xe3e8
movb $0x1, 0x9940(%rip)      # 0xe3ec
jmp 0x4a45
movl 0x9944(%rip), %eax      # 0xe3f8
orl 0x9942(%rip), %eax      # 0xe3fc
jne 0x4e7b
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x9906(%rip)      # 0xe3d8
jmp 0x4a45
movq %r13, %rbp
movq %rbx, %r13
movq %r14, %rbx
movq %r12, %r14
movq (%r15), %r12
movq %r12, %rdi
callq 0x20b0
cmpq $0x3, %rax
jne 0x4e43
movq %r12, %rdi
callq 0x50fc
cmpl $0x4, %eax
je 0x4e43
movq (%r15), %rdi
callq 0x5128
cmpl $0x3, %eax
je 0x4e43
movq (%r15), %rdi
callq 0x51a1
cmpl $0x2, %eax
je 0x4e43
movq (%r15), %rax
movq %rax, 0x9763(%rip)      # 0xe298
movq %r14, %r12
movq %rbx, %r14
movq %r13, %rbx
movq %rbp, %r13
movl 0x14(%rsp), %ebp
jmp 0x4a45
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x9744(%rip)      # 0xe2a0
jmp 0x4a45
cmpl $0x0, 0x9868(%rip)      # 0xe3d0
jne 0x4e74
movl $0x5, 0x9858(%rip)      # 0xe3d0
jmp 0x4a45
movb $0x1, 0x985d(%rip)      # 0xe3e1
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x984e(%rip)      # 0xe3e4
testb $0x3, %al
je 0x4a45
jmp 0x4e51
movb $0x1, 0x9843(%rip)      # 0xe3ed
jmp 0x4a45
movl 0x981f(%rip), %eax      # 0xe3d4
orl 0x981d(%rip), %eax      # 0xe3d8
jne 0x4e58
movq (%r15), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x9824(%rip)      # 0xe3f8
movq 0x8(%rsp), %rdi
cmpb $0x3a, (%rdi)
jne 0x4a45
incq %rdi
movq %rdi, 0x8(%rsp)
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x9803(%rip)      # 0xe3fc
testl %eax, %eax
jne 0x4a45
jmp 0x4ebc
movl 0x97ec(%rip), %eax      # 0xe3f8
orl 0x97ea(%rip), %eax      # 0xe3fc
jne 0x4e66
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x97aa(%rip)      # 0xe3d4
cmpl $0x10000, %eax          # imm = 0x10000
jae 0x4eae
testl %eax, %eax
jne 0x4a45
movl $0x1, 0x9795(%rip)      # 0xe3dc
jmp 0x4a45
cmpl $0x0, 0x977d(%rip)      # 0xe3d0
jne 0x4e5f
movl $0x7, 0x976d(%rip)      # 0xe3d0
jmp 0x4a45
movq (%r15), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x9629(%rip)      # 0xe2a4
movq 0x8(%rsp), %rdi
cmpb $0x3a, (%rdi)
jne 0x4c9c
incq %rdi
movq %rdi, 0x8(%rsp)
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x960c(%rip)      # 0xe2a8
movl 0x9602(%rip), %ecx      # 0xe2a4
testl %ecx, %ecx
js 0x4ea7
movl 0x95f8(%rip), %eax      # 0xe2a8
testl %eax, %eax
js 0x4ea7
testl %ecx, %ecx
jne 0x4cc6
movl $0x100, 0x95de(%rip)    # imm = 0x100
testl %eax, %eax
jne 0x4cd4
movl $0x102, 0x95d4(%rip)    # imm = 0x102
movl 0x95ca(%rip), %eax      # 0xe2a4
cmpl $0x14, %eax
jl 0x4eb5
cmpl $0x5, 0x95be(%rip)      # 0xe2a8
jle 0x4eb5
cmpl $0x101, %eax            # imm = 0x101
jge 0x4dd9
cmpl $0x103, 0x95a3(%rip)    # imm = 0x103
jl 0x4a45
callq 0x27c4
jmp 0x4a45
cmpl $0x0, 0x96b4(%rip)      # 0xe3d0
jne 0x4e6d
movl $0x6, 0x96a4(%rip)      # 0xe3d0
jmp 0x4a45
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, %edi
callq 0x4f68
movl %eax, 0x9546(%rip)      # 0xe290
cmpl $0x13, %eax
jne 0x4a45
jmp 0x4e4a
movb $0x0, 0x9555(%rip)      # 0xe2b4
jmp 0x4a45
movq (%r15), %rax
movq %rax, 0x9682(%rip)      # 0xe3f0
jmp 0x4a45
cmpl $0x0, 0x9656(%rip)      # 0xe3d0
jne 0x4e82
movl $0x3, 0x9646(%rip)      # 0xe3d0
jmp 0x4a45
cmpl $0x0, 0x963a(%rip)      # 0xe3d0
jne 0x4e89
movl $0x4, 0x962a(%rip)      # 0xe3d0
jmp 0x4a45
movq (%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movl %eax, 0x94f3(%rip)      # 0xe2b0
jmp 0x4a45
movb $0x1, 0x9617(%rip)      # 0xe3e0
jmp 0x4a45
cmpl $-0x1, %eax
jne 0x4a45
jmp 0x4de3
callq 0x2798
jmp 0x4cfb
movq 0x91e6(%rip), %rax      # 0xdfd0
movslq (%rax), %rax
cmpl %ebp, %eax
jge 0x4e17
movq 0x9490(%rip), %rdx      # 0xe288
movslq %ebp, %rcx
testq %rdx, %rdx
jne 0x4ecd
movq (%rbx,%rax,8), %rdx
movq %rdx, 0x9479(%rip)      # 0xe288
incq %rax
cmpq %rax, %rcx
jne 0x4dfb
cmpq $0x0, 0x9469(%rip)      # 0xe288
je 0x4ec3
xorl %eax, %eax
cmpl $0x2, 0x95a2(%rip)      # 0xe3d0
je 0x4e98
cmpb $0x0, 0x95a9(%rip)      # 0xe3e0
je 0x4e98
movq %rbx, %rdi
callq 0x298f
jmp 0x4e93
callq 0x28f3
jmp 0x4e93
callq 0x290f
jmp 0x4e93
callq 0x289f
jmp 0x4e93
callq 0x2867
jmp 0x4e93
movl $0x7, %edi
jmp 0x4e8e
callq 0x28d7
jmp 0x4e93
movl $0x6, %edi
jmp 0x4e8e
movl $0x5, %edi
jmp 0x4e8e
callq 0x2883
jmp 0x4e93
movl $0x3, %edi
jmp 0x4e8e
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 0x282f
jmp 0x4e93
callq 0x28bb
jmp 0x4e93
callq 0x27f0
jmp 0x4e93
callq 0x284b
jmp 0x4e93
movq %rbx, %rdi
callq 0x29b8
jmp 0x4e93
movq %rbx, %rdi
callq 0x2966
jmp 0x4e93
movq (%rbx), %rdi
callq 0x247d
xorl %edi, %edi
callq 0x22a0
nop