// File: /workspace/github_c_makefile_star2_O3/koendv[P]stm32duino-raspberrypi/Arduino_Tools/src/stm32flash/init.c
// Function: gpio_sequence
static int gpio_sequence(struct port_interface *port, const char *seq, size_t len_seq)
{
	struct gpio_list *gpio_to_release = NULL;
#if defined(__linux__)
	struct gpio_list *to_free;
#endif
	int ret = 0, level, gpio;
	int sleep_time = 0;
	int delimiter = 0;
	const char *sig_str = NULL;
	const char *s = seq;
	size_t l = len_seq;

	fprintf(diag, "\nGPIO sequence start\n");
	while (ret == 0 && *s && l > 0) {
		sig_str = NULL;
		sleep_time = 0;
		delimiter = 0;

		if (*s == '-') {
			level = 0;
			s++;
			l--;
		} else
			level = 1;

		if (isdigit(*s)) {
			gpio = atoi(s);
			while (isdigit(*s)) {
				s++;
				l--;
			}
		} else if (l >= 3 && !strncmp(s, "rts", 3)) {
			sig_str = s;
			gpio = -GPIO_RTS;
			s += 3;
			l -= 3;
		} else if (l >= 3 && !strncmp(s, "dtr", 3)) {
			sig_str = s;
			gpio = -GPIO_DTR;
			s += 3;
			l -= 3;
		} else if (l >= 3 && !strncmp(s, "brk", 3)) {
			sig_str = s;
			gpio = -GPIO_BRK;
			s += 3;
			l -= 3;
		} else if (*s && (l > 0)) {
			delimiter = 1;
			/* The ',' delimiter adds a 100 ms delay between signal toggles.
			 * i.e -rts,dtr will reset rts, wait 100 ms, set dtr.
			 *
			 * The '&' delimiter adds no delay between signal toggles.
			 * i.e -rts&dtr will reset rts and immediately set dtr.
			 *
			 * Example: -rts&dtr,,,rts,-dtr will reset rts and set dtr
			 * without delay, then wait 300 ms, set rts, wait 100 ms, reset dtr.
			 */
			if (*s == ',') {
				s++;
				l--;
				sleep_time = 100000;
			} else if (*s == '&') {
				s++;
				l--;
			} else {
				fprintf(stderr, "Character \'%c\' is not a valid signal or separator\n", *s);
				ret = 1;
				break;
			}
		} else {
			/* E.g. modifier without signal */
			fprintf(stderr, "Invalid sequence %.*s\n", (int) len_seq, seq);
			ret = 1;
			break;
		}

		if (!delimiter) { /* actual gpio/port signal driving */
			if (gpio < 0) {
				gpio = -gpio;
				fprintf(diag, " setting port signal %.3s to %i... ", sig_str, level);
				ret = (port->gpio(port, gpio, level) != PORT_ERR_OK);
				printStatus(diag, ret);
			} else {
				fprintf(diag, " setting gpio %i to %i... ", gpio, level);
				ret = (drive_gpio(gpio, level, &gpio_to_release) != 1);
				printStatus(diag, ret);
			}
		}

		if (sleep_time) {
			fprintf(diag, " delay %i us\n", sleep_time);
			usleep(sleep_time);
		}
	}
#if defined(__linux__)
	while (gpio_to_release) {
		release_gpio(gpio_to_release->gpio, gpio_to_release->input, gpio_to_release->exported);
		to_free = gpio_to_release;
		gpio_to_release = gpio_to_release->next;
		free(to_free);
	}
#endif
	fprintf(diag, "GPIO sequence end\n\n");
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, 0x38(%rsp)
leaq 0xb1a8(%rip), %rax      # 0xe400
movq (%rax), %rcx
leaq 0x5219(%rip), %rdi      # 0x847b
movl $0x15, %esi
movl $0x1, %edx
callq 0x22b0
movq $0x0, 0x18(%rsp)
movq %r15, 0x28(%rsp)
movq %rbx, 0x30(%rsp)
xorl %ecx, %ecx
movb (%rbx), %al
testb %al, %al
je 0x36af
testq %r15, %r15
je 0x36af
movq %rcx, 0x10(%rsp)
xorl %r14d, %r14d
xorl %r13d, %r13d
movb %al, 0xa(%rsp)
cmpb $0x2d, %al
sete %r14b
setne %r13b
leaq (%rbx,%r14), %r12
subq %r14, %r15
callq 0x22c0
movq (%rax), %rbp
movsbq (%rbx,%r14), %rdx
testb $0x8, 0x1(%rbp,%rdx,2)
jne 0x3321
cmpq $0x3, %r15
jb 0x35d8
cmpb $0x62, %dl
je 0x35b4
movzbl %dl, %eax
cmpl $0x64, %eax
je 0x3590
cmpl $0x72, %eax
jne 0x35d8
cmpb $0x74, 0x1(%r12)
jne 0x35d8
cmpb $0x73, 0x2(%r12)
jne 0x35d8
leaq 0x3(%r12), %rbx
addq $-0x3, %r15
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x3410
movq %r12, %rdi
callq 0x2280
movl %eax, %r14d
leaq 0x40(%rsp), %rbx
decq %r15
movsbq 0x1(%r12), %rax
incq %r12
testb $0x8, 0x1(%rbp,%rax,2)
jne 0x3331
testl %r14d, %r14d
js 0x340a
leaq 0xb0ac(%rip), %rax      # 0xe400
movq (%rax), %rdi
leaq 0x51a1(%rip), %rsi      # 0x84ff
movl %r14d, %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x2170
movq %rbx, %rdi
leaq 0x51c7(%rip), %rsi      # 0x853c
movl %r14d, %edx
xorl %eax, %eax
callq 0x2290
movq %rbx, %rdi
leaq 0x80(%rsp), %r13
movq %r13, %rsi
callq 0x2190
movl %eax, 0x24(%rsp)
testl %eax, %eax
movl %r14d, 0xc(%rsp)
je 0x345e
movl %r14d, %edx
leaq 0x70(%rsp), %r14
movq %r14, %rdi
leaq 0x665a(%rip), %rsi      # 0x9a0f
xorl %eax, %eax
callq 0x2290
leaq 0x5196(%rip), %rdi      # 0x8559
movq %r14, %rsi
callq 0x3853
movl $0x1, %ebp
testl %eax, %eax
je 0x357a
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2190
xorl %ecx, %ecx
testl %eax, %eax
je 0x3463
movq 0xac08(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x5176(%rip), %rsi      # 0x8570
movl 0xc(%rsp), %edx
xorl %eax, %eax
callq 0x2170
jmp 0x357a
movq %r12, %rbx
xorl %r12d, %r12d
negl %r14d
leaq 0xafe6(%rip), %rax      # 0xe400
movl %r14d, %ebp
movq %rax, %r14
movq (%rax), %rdi
leaq 0x50b1(%rip), %rsi      # 0x84db
movq %r12, %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x2170
movq 0x38(%rsp), %rdi
movl %ebp, %esi
movl %r13d, %edx
callq *0x38(%rdi)
xorl %ebp, %ebp
testl %eax, %eax
setne %bpl
movq (%r14), %rdi
movl %ebp, %esi
callq 0x74a3
movq %rbx, %r12
jmp 0x3633
movl $0x1, %ecx
movl %ecx, 0x20(%rsp)
movq %rbx, %r14
xorl %ebx, %ebx
movq %r14, %rdi
leaq 0x5111(%rip), %rsi      # 0x8587
movl 0xc(%rsp), %edx
xorl %eax, %eax
callq 0x2290
movq %r14, %rdi
movq %r13, %rsi
callq 0x2190
movl $0x0, %ebp
testl %eax, %eax
leaq 0xb(%rsp), %r14
jne 0x3517
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2230
movl %eax, %ebp
testl %eax, %eax
js 0x3654
movl $0x1, %edx
movl %ebp, %edi
movq %r14, %rsi
callq 0x2140
movq %rax, %r13
testl %r13d, %r13d
js 0x34d4
movl %r13d, %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
jmp 0x34f1
callq 0x2040
movl (%rax), %eax
cmpl $0x4, %eax
je 0x34ef
movl $0x0, %ebx
cmpl $0xb, %eax
je 0x34f1
jmp 0x3679
xorl %ebx, %ebx
testl %r13d, %r13d
je 0x34fb
testq %rbx, %rbx
je 0x34b2
movl %ebp, %edi
callq 0x2120
testq %rbx, %rbx
setne %r14b
cmpb $0x69, 0xb(%rsp)
sete %bl
andb %r14b, %bl
movzbl %bl, %ebp
cmpl $0x0, 0x24(%rsp)
jne 0x3522
testb %bl, %bl
je 0x3553
movl $0x18, %edi
callq 0x21c0
testq %rax, %rax
je 0x3645
movl 0xc(%rsp), %ecx
movl %ecx, 0x8(%rax)
movl 0x20(%rsp), %ecx
movl %ecx, 0x10(%rax)
movl %ebp, 0xc(%rax)
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
movq %rax, 0x10(%rsp)
movb 0xa(%rsp), %al
cmpb $0x2d, %al
leaq 0x505c(%rip), %rsi      # 0x85bc
leaq 0x5050(%rip), %rax      # 0x85b7
cmovneq %rax, %rsi
leaq 0x40(%rsp), %rdi
callq 0x3853
movl %eax, %ebp
xorl $0x1, %ebp
leaq 0xae7f(%rip), %rax      # 0xe400
movq (%rax), %rdi
movl %ebp, %esi
callq 0x74a3
jmp 0x3633
cmpb $0x74, 0x1(%r12)
jne 0x35d8
cmpb $0x72, 0x2(%r12)
jne 0x35d8
leaq 0x3(%r12), %rbx
addq $-0x3, %r15
movl $0xfffffffe, %r14d      # imm = 0xFFFFFFFE
jmp 0x3410
cmpb $0x72, 0x1(%r12)
jne 0x35d8
cmpb $0x6b, 0x2(%r12)
jne 0x35d8
leaq 0x3(%r12), %rbx
addq $-0x3, %r15
movl $0xfffffffd, %r14d      # imm = 0xFFFFFFFD
jmp 0x3410
testb %dl, %dl
je 0x376a
testq %r15, %r15
je 0x376a
cmpb $0x2c, %dl
je 0x3604
movzbl %dl, %eax
cmpl $0x26, %eax
jne 0x378e
incq %r12
decq %r15
xorl %ebp, %ebp
jmp 0x3633
incq %r12
decq %r15
leaq 0xadef(%rip), %rax      # 0xe400
movq (%rax), %rdi
xorl %ebp, %ebp
leaq 0x4efd(%rip), %rsi      # 0x851a
movl $0x186a0, %edx          # imm = 0x186A0
xorl %eax, %eax
callq 0x2170
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x22d0
movq %r12, %rbx
testl %ebp, %ebp
movq 0x10(%rsp), %rcx
je 0x3286
jmp 0x36a5
callq 0x240b
movl $0x1, %ebp
jmp 0x357a
movq 0xa99d(%rip), %rax      # 0xdff8
movq (%rax), %rdi
xorl %r14d, %r14d
leaq 0x4f58(%rip), %rsi      # 0x85c0
leaq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0x2170
jmp 0x3509
movq 0xa978(%rip), %rax      # 0xdff8
movq (%rax), %rdi
xorl %r14d, %r14d
leaq 0x4f66(%rip), %rsi      # 0x85f3
leaq 0x40(%rsp), %rdx
xorl %eax, %eax
callq 0x2170
movl %ebp, %edi
callq 0x2120
jmp 0x3509
movl $0x1, %eax
movq %rax, 0x18(%rsp)
testq %rcx, %rcx
je 0x3733
leaq 0x40(%rsp), %r15
leaq 0x80(%rsp), %rbp
movl 0x8(%rcx), %r13d
movl 0x10(%rcx), %r14d
movq %rcx, %rbx
movl 0xc(%rcx), %r12d
movq %r15, %rdi
leaq 0x6335(%rip), %rsi      # 0x9a0f
movl %r13d, %edx
xorl %eax, %eax
callq 0x2290
testl %r12d, %r12d
je 0x370c
movq %rbp, %rdi
leaq 0x4e94(%rip), %rsi      # 0x8587
movl %r13d, %edx
xorl %eax, %eax
callq 0x2290
movq %rbp, %rdi
leaq 0x4f08(%rip), %rsi      # 0x860f
callq 0x3853
testl %r14d, %r14d
jne 0x3720
leaq 0x4efa(%rip), %rdi      # 0x8612
movq %r15, %rsi
callq 0x3853
movq (%rbx), %r14
movq %rbx, %rdi
callq 0x2030
movq %r14, %rcx
testq %r14, %r14
jne 0x36c1
leaq 0xacc6(%rip), %rax      # 0xe400
movq (%rax), %rcx
leaq 0x4de4(%rip), %rdi      # 0x8528
movl $0x13, %esi
movl $0x1, %edx
callq 0x22b0
movq 0x18(%rsp), %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa887(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x4d49(%rip), %rsi      # 0x84c4
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
xorl %eax, %eax
callq 0x2170
jmp 0x37a6
movq 0xa863(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x4cf2(%rip), %rsi      # 0x8491
xorl %eax, %eax
callq 0x2170
movl $0x1, %eax
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rcx
jmp 0x36af