// File: /workspace/github_c_makefile_star2_O2/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 $0x108, %rsp            # imm = 0x108
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, 0x28(%rsp)
leaq 0xad66(%rip), %rax      # 0xd400
movq (%rax), %rcx
leaq 0x4dd7(%rip), %rdi      # 0x747b
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
xorl %ebx, %ebx
xorl %ebp, %ebp
movq %r15, 0x20(%rsp)
movq %r12, 0x18(%rsp)
movq %r15, %r13
testl %ebp, %ebp
jne 0x2b20
movb (%r13), %al
xorl %ebp, %ebp
testb %al, %al
je 0x2b23
testq %r12, %r12
je 0x2b23
xorl %ebp, %ebp
xorl %ecx, %ecx
movb %al, 0x3(%rsp)
cmpb $0x2d, %al
sete %bpl
setne %cl
movl %ecx, 0x4(%rsp)
movq %r13, %r15
addq %rbp, %r15
subq %rbp, %r12
callq 0x22c0
movq (%rax), %r14
movsbq (%r13,%rbp), %rdx
movzwl (%r14,%rdx,2), %r13d
btl $0xb, %r13d
jb 0x2764
cmpq $0x3, %r12
jb 0x2813
cmpb $0x62, %dl
je 0x27f8
movzbl %dl, %eax
cmpl $0x64, %eax
je 0x27de
cmpl $0x72, %eax
jne 0x2813
cmpb $0x74, 0x1(%r15)
jne 0x2813
cmpb $0x73, 0x2(%r15)
jne 0x2813
leaq 0x3(%r15), %r13
addq $-0x3, %r12
pushq $-0x1
jmp 0x2810
movq %r15, %rdi
callq 0x2280
movl %eax, %ebp
btl $0xb, %r13d
jae 0x2787
decq %r12
movsbq 0x1(%r15), %rax
incq %r15
movzwl (%r14,%rax,2), %r13d
jmp 0x276e
testl %ebp, %ebp
jns 0x2876
movq %r15, %r13
xorl %r15d, %r15d
negl %ebp
leaq 0xac62(%rip), %r14      # 0xd400
movq (%r14), %rdi
leaq 0x4d33(%rip), %rsi      # 0x74db
movq %r15, %rdx
movl 0x4(%rsp), %r15d
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2170
movq 0x28(%rsp), %rdi
movl %ebp, %esi
movl %r15d, %edx
callq *0x38(%rdi)
xorl %ebp, %ebp
testl %eax, %eax
setne %bpl
movq (%r14), %rdi
movl %ebp, %esi
callq 0x635b
jmp 0x26c0
cmpb $0x74, 0x1(%r15)
jne 0x2813
cmpb $0x72, 0x2(%r15)
jne 0x2813
leaq 0x3(%r15), %r13
addq $-0x3, %r12
pushq $-0x2
jmp 0x2810
cmpb $0x72, 0x1(%r15)
jne 0x2813
cmpb $0x6b, 0x2(%r15)
jne 0x2813
leaq 0x3(%r15), %r13
addq $-0x3, %r12
pushq $-0x3
popq %rbp
jmp 0x2795
testb %dl, %dl
je 0x2ae4
testq %r12, %r12
je 0x2ae4
cmpb $0x2c, %dl
je 0x2842
movzbl %dl, %eax
cmpl $0x26, %eax
jne 0x2b08
incq %r15
decq %r12
xorl %ebp, %ebp
jmp 0x26bd
incq %r15
decq %r12
leaq 0xabb1(%rip), %rax      # 0xd400
movq (%rax), %rdi
xorl %ebp, %ebp
leaq 0x4cbf(%rip), %rsi      # 0x751a
movl $0x186a0, %edx          # imm = 0x186A0
xorl %eax, %eax
callq 0x2170
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x22d0
jmp 0x26bd
leaq 0xab83(%rip), %rax      # 0xd400
movq (%rax), %rdi
leaq 0x4c78(%rip), %rsi      # 0x74ff
movl %ebp, %edx
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0x2170
leaq 0x30(%rsp), %r14
movq %r14, %rdi
leaq 0x4c99(%rip), %rsi      # 0x753c
movl %ebp, %edx
xorl %eax, %eax
callq 0x2290
movq %r14, %rdi
movl %ebp, %r13d
leaq 0x70(%rsp), %rbp
movq %rbp, %rsi
callq 0x2190
pushq $0x1
popq %rcx
testl %eax, %eax
movl %eax, 0x14(%rsp)
je 0x2919
leaq 0x60(%rsp), %r14
movq %r14, %rdi
leaq 0x6142(%rip), %rsi      # 0x8a1b
movl %r13d, %edx
xorl %eax, %eax
callq 0x2290
leaq 0x4c6f(%rip), %rdi      # 0x7559
movq %r14, %rsi
callq 0x2c63
testl %eax, %eax
je 0x2a78
leaq 0x30(%rsp), %r14
movq %r14, %rdi
leaq 0x70(%rsp), %rbp
movq %rbp, %rsi
callq 0x2190
xorl %ecx, %ecx
testl %eax, %eax
jne 0x2a3f
movl %ecx, 0xc(%rsp)
movl $0x0, 0x4(%rsp)
movq %r14, %rdi
leaq 0x4c58(%rip), %rsi      # 0x7587
movl %r13d, 0x10(%rsp)
movl %r13d, %edx
xorl %eax, %eax
callq 0x2290
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2190
movl $0x0, %r14d
testl %eax, %eax
jne 0x29e3
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2230
movl %eax, 0x4(%rsp)
testl %eax, %eax
leaq 0xb(%rsp), %r13
js 0x2a91
movl 0x4(%rsp), %edi
movq %r13, %rsi
pushq $0x1
popq %rdx
callq 0x2140
movq %rax, %rbp
testl %ebp, %ebp
js 0x2998
movl %ebp, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
jmp 0x29b7
callq 0x2040
movl (%rax), %eax
cmpl $0x4, %eax
je 0x29b4
movl $0x0, %r14d
cmpl $0xb, %eax
je 0x29b7
jmp 0x2ab6
xorl %r14d, %r14d
testl %ebp, %ebp
je 0x29c0
testq %r14, %r14
je 0x2976
movl 0x4(%rsp), %edi
callq 0x2120
testq %r14, %r14
setne %r14b
cmpb $0x69, 0xb(%rsp)
sete %al
andb %r14b, %al
movl %eax, 0x4(%rsp)
movzbl %al, %r14d
cmpl $0x0, 0x14(%rsp)
jne 0x29f1
cmpb $0x0, 0x4(%rsp)
je 0x2a16
pushq $0x18
popq %rdi
callq 0x21c0
testq %rax, %rax
je 0x2a5c
movl 0x10(%rsp), %ecx
movl %ecx, 0x8(%rax)
movl 0xc(%rsp), %ecx
movl %ecx, 0x10(%rax)
movl %r14d, 0xc(%rax)
movq %rbx, (%rax)
movq %rax, %rbx
movb 0x3(%rsp), %al
cmpb $0x2d, %al
leaq 0x4b99(%rip), %rsi      # 0x75bc
leaq 0x4b8d(%rip), %rax      # 0x75b7
cmovneq %rax, %rsi
leaq 0x30(%rsp), %rdi
callq 0x2c63
movl %eax, %ebp
xorl $0x1, %ebp
jmp 0x2a7b
movq 0xa5aa(%rip), %rax      # 0xcff0
movq (%rax), %rdi
leaq 0x4b20(%rip), %rsi      # 0x7570
movl %r13d, %edx
xorl %eax, %eax
callq 0x2170
jmp 0x2a78
movq 0xa58d(%rip), %rax      # 0xcff0
movq (%rax), %rcx
leaq 0x4b3b(%rip), %rdi      # 0x75a8
pushq $0xe
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
pushq $0x1
popq %rbp
leaq 0xa97e(%rip), %rax      # 0xd400
movq (%rax), %rdi
movl %ebp, %esi
callq 0x635b
jmp 0x26bd
movq 0xa558(%rip), %rax      # 0xcff0
movq (%rax), %rdi
xorl %r14d, %r14d
leaq 0x4b1b(%rip), %rsi      # 0x75c0
leaq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x2170
jmp 0x29d0
movq 0xa533(%rip), %rax      # 0xcff0
movq (%rax), %rdi
xorl %r14d, %r14d
leaq 0x4b29(%rip), %rsi      # 0x75f3
leaq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x2170
movl 0x4(%rsp), %edi
callq 0x2120
jmp 0x29d0
movq 0xa505(%rip), %rax      # 0xcff0
movq (%rax), %rdi
leaq 0x49cf(%rip), %rsi      # 0x74c4
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0x2170
jmp 0x2b20
movq 0xa4e1(%rip), %rax      # 0xcff0
movq (%rax), %rdi
leaq 0x4978(%rip), %rsi      # 0x7491
xorl %eax, %eax
callq 0x2170
pushq $0x1
popq %rbp
leaq 0x30(%rsp), %r15
testq %rbx, %rbx
je 0x2b9e
movl 0x8(%rbx), %r13d
movl 0x10(%rbx), %r14d
movl 0xc(%rbx), %r12d
movq %r15, %rdi
leaq 0x5ed8(%rip), %rsi      # 0x8a1b
movl %r13d, %edx
xorl %eax, %eax
callq 0x2290
testl %r12d, %r12d
je 0x2b7a
leaq 0x70(%rsp), %r12
movq %r12, %rdi
leaq 0x4a26(%rip), %rsi      # 0x7587
movl %r13d, %edx
xorl %eax, %eax
callq 0x2290
movq %r12, %rdi
leaq 0x4a9a(%rip), %rsi      # 0x760f
callq 0x2c63
testl %r14d, %r14d
jne 0x2b8e
leaq 0x4a8c(%rip), %rdi      # 0x7612
movq %r15, %rsi
callq 0x2c63
movq (%rbx), %r14
movq %rbx, %rdi
callq 0x2030
movq %r14, %rbx
jmp 0x2b28
leaq 0xa85b(%rip), %rax      # 0xd400
movq (%rax), %rcx
leaq 0x4979(%rip), %rdi      # 0x7528
pushq $0x13
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
movl %ebp, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq