// File: /workspace/github_c_makefile_star2_O1/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 $0x138, %rsp            # imm = 0x138
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, 0x50(%rsp)
leaq 0xb108(%rip), %rax      # 0xe400
movq (%rax), %rcx
leaq 0x5179(%rip), %rdi      # 0x847b
movl $0x15, %esi
movl $0x1, %edx
callq 0x22b0
movq $0x0, 0x30(%rsp)
movq %r15, 0x40(%rsp)
movq %rbx, 0x48(%rsp)
movq %rbx, %r12
xorl %r14d, %r14d
movb (%r12), %al
testb %al, %al
je 0x3866
testq %r15, %r15
je 0x3866
xorl %ebp, %ebp
xorl %r13d, %r13d
movb %al, 0xf(%rsp)
cmpb $0x2d, %al
sete %bpl
setne 0x2c(%rsp)
leaq (%r12,%rbp), %rbx
subq %rbp, %r15
callq 0x22c0
movq %r12, %rcx
movq (%rax), %r12
movsbq (%rcx,%rbp), %rbp
testb $0x8, 0x1(%r12,%rbp,2)
jne 0x3415
cmpq $0x3, %r15
jb 0x33d0
movl $0x3, %edx
movq %rbx, %rdi
leaq 0x5106(%rip), %rsi      # 0x8491
callq 0x2050
testl %eax, %eax
je 0x349a
movl $0x3, %edx
movq %rbx, %rdi
leaq 0x50ee(%rip), %rsi      # 0x8495
callq 0x2050
testl %eax, %eax
je 0x34bc
movl $0x3, %edx
movq %rbx, %rdi
leaq 0x50d6(%rip), %rsi      # 0x8499
callq 0x2050
testl %eax, %eax
je 0x34de
testb %bpl, %bpl
je 0x381e
testq %r15, %r15
je 0x381e
cmpb $0x26, %bpl
leaq 0xa0(%rsp), %r12
je 0x3477
movzbl %bpl, %eax
cmpl $0x2c, %eax
jne 0x3842
incq %rbx
decq %r15
movl $0x186a0, %eax          # imm = 0x186A0
movq %rax, 0x20(%rsp)
xorl %ecx, %ecx
jmp 0x3488
movq %rbx, %rdi
callq 0x2280
movl %eax, 0x10(%rsp)
movsbq (%rbx), %rax
movb $0x1, %cl
testb $0x8, 0x1(%r12,%rax,2)
jne 0x3446
movq $0x0, 0x20(%rsp)
movb $0x1, %al
xorl %edx, %edx
movq %rbx, 0x18(%rsp)
jmp 0x3503
leaq 0xafb3(%rip), %rbp      # 0xe400
decq %r15
movsbq 0x1(%rbx), %rax
incq %rbx
testb $0x8, 0x1(%r12,%rax,2)
jne 0x344d
movq $0x0, 0x20(%rsp)
movb $0x1, %al
xorl %edx, %edx
movq %rbx, 0x18(%rsp)
jmp 0x350a
incq %rbx
decq %r15
movq $0x0, 0x20(%rsp)
movb $0x1, %cl
xorl %eax, %eax
xorl %edx, %edx
movq %rbx, 0x18(%rsp)
leaq 0xaf68(%rip), %rbp      # 0xe400
jmp 0x3512
leaq 0x3(%rbx), %rax
movq %rax, 0x18(%rsp)
addq $-0x3, %r15
movq $0x0, 0x20(%rsp)
movb $0x1, %cl
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
jmp 0x34fe
leaq 0x3(%rbx), %rax
movq %rax, 0x18(%rsp)
addq $-0x3, %r15
movq $0x0, 0x20(%rsp)
movb $0x1, %cl
movl $0xfffffffe, 0x10(%rsp) # imm = 0xFFFFFFFE
jmp 0x34fe
leaq 0x3(%rbx), %rax
movq %rax, 0x18(%rsp)
addq $-0x3, %r15
movq $0x0, 0x20(%rsp)
movb $0x1, %cl
movl $0xfffffffd, 0x10(%rsp) # imm = 0xFFFFFFFD
movb $0x1, %al
movq %rbx, %rdx
leaq 0xaef6(%rip), %rbp      # 0xe400
leaq 0xa0(%rsp), %r12
movb $0x1, %bl
testb %al, %al
je 0x35ea
movq %rcx, 0x58(%rsp)
movb 0x2c(%rsp), %al
movb %al, %r13b
movl 0x10(%rsp), %ebx
testl %ebx, %ebx
js 0x35f4
movq (%rbp), %rdi
leaq 0x4fcc(%rip), %rsi      # 0x850b
movl %ebx, %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x2170
movl %ebx, %ebp
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
leaq 0x4fec(%rip), %rsi      # 0x8548
movl %ebp, %edx
xorl %eax, %eax
callq 0x2290
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2190
movl %eax, 0x2c(%rsp)
testl %eax, %eax
je 0x362f
leaq 0x90(%rsp), %r13
movq %r13, %rdi
leaq 0x646d(%rip), %rsi      # 0x99fb
movl %ebp, %edx
xorl %eax, %eax
callq 0x2290
leaq 0x4fc7(%rip), %rdi      # 0x8565
movq %r13, %rsi
callq 0x39b6
movl $0x1, %r13d
testl %eax, %eax
je 0x3760
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2190
xorl %ecx, %ecx
testl %eax, %eax
leaq 0xae36(%rip), %rbp      # 0xe400
je 0x363b
movq 0xaa25(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x4f9f(%rip), %rsi      # 0x857c
movl 0x10(%rsp), %edx
xorl %eax, %eax
callq 0x2170
jmp 0x3625
movq 0x18(%rsp), %r12
jmp 0x3783
negl %ebx
movq (%rbp), %rdi
leaq 0x4ee6(%rip), %rsi      # 0x84e7
movl %r13d, %ecx
xorl %eax, %eax
callq 0x2170
movq 0x50(%rsp), %rdi
movl %ebx, 0x10(%rsp)
movl %ebx, %esi
movl %r13d, %edx
callq *0x38(%rdi)
xorl %r13d, %r13d
testl %eax, %eax
setne %r13b
movq 0x18(%rsp), %r12
jmp 0x376c
movl $0x1, %ecx
leaq 0xadc5(%rip), %rbp      # 0xe400
movl %ecx, 0x3c(%rsp)
movq %r12, %r13
xorl %r12d, %r12d
movq %rbx, %rdi
leaq 0x4f44(%rip), %rsi      # 0x8593
movl 0x10(%rsp), %edx
xorl %eax, %eax
callq 0x2290
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2190
movl $0x0, %r13d
testl %eax, %eax
jne 0x36fb
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2230
movl %eax, %r13d
testl %eax, %eax
js 0x37c6
movl $0x1, %edx
movl %r13d, %edi
leaq 0x17(%rsp), %rsi
callq 0x2140
movq %rax, %rbx
testl %ebx, %ebx
js 0x36af
movl %ebx, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
jmp 0x36ce
callq 0x2040
movl (%rax), %eax
cmpl $0x4, %eax
je 0x36cb
movl $0x0, %r12d
cmpl $0xb, %eax
je 0x36ce
jmp 0x37ee
xorl %r12d, %r12d
testl %ebx, %ebx
je 0x36d7
testq %r12, %r12
je 0x368a
movl %r13d, %edi
callq 0x2120
testq %r12, %r12
setne %r13b
leaq 0x60(%rsp), %rbx
cmpb $0x69, 0x17(%rsp)
sete %r12b
andb %r13b, %r12b
movzbl %r12b, %r13d
cmpl $0x0, 0x2c(%rsp)
jne 0x3707
testb %r12b, %r12b
je 0x3732
movl $0x18, %edi
callq 0x21c0
testq %rax, %rax
je 0x37b6
movl 0x10(%rsp), %ecx
movl %ecx, 0x8(%rax)
movl 0x3c(%rsp), %ecx
movl %ecx, 0x10(%rax)
movl %r13d, 0xc(%rax)
movq %r14, (%rax)
movq %rax, %r14
movb 0xf(%rsp), %al
movq 0x18(%rsp), %r12
cmpb $0x2d, %al
leaq 0x4e84(%rip), %rsi      # 0x85c8
leaq 0x4e78(%rip), %rax      # 0x85c3
cmovneq %rax, %rsi
movq %rbx, %rdi
callq 0x39b6
movl %eax, %r13d
xorl $0x1, %r13d
jmp 0x376c
movq 0x18(%rsp), %r12
leaq 0xac94(%rip), %rbp      # 0xe400
movq (%rbp), %rdi
movl %r13d, %esi
callq 0x6ca7
testl %r13d, %r13d
sete %bl
movq 0x58(%rsp), %rcx
testb %cl, %cl
jne 0x37a9
movq (%rbp), %rdi
leaq 0x4d94(%rip), %rsi      # 0x8526
movq 0x20(%rsp), %r13
movl %r13d, %edx
xorl %eax, %eax
callq 0x2170
movl %r13d, %edi
callq 0x22d0
testb %bl, %bl
jne 0x332a
jmp 0x385c
callq 0x240b
movl $0x1, %r13d
jmp 0x3625
movq 0xa82b(%rip), %rax      # 0xdff8
movq (%rax), %rdi
xorl %r13d, %r13d
leaq 0x4df2(%rip), %rsi      # 0x85cc
leaq 0x60(%rsp), %rbx
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2170
jmp 0x36eb
movq 0xa803(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x4e00(%rip), %rsi      # 0x85ff
leaq 0x60(%rsp), %rbx
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2170
movl %r13d, %edi
xorl %r13d, %r13d
callq 0x2120
jmp 0x36eb
movq 0xa7d3(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x4ca1(%rip), %rsi      # 0x84d0
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rcx
xorl %eax, %eax
callq 0x2170
jmp 0x385c
movq 0xa7af(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x4c4a(%rip), %rsi      # 0x849d
movl %ebp, %edx
xorl %eax, %eax
callq 0x2170
movl $0x1, %eax
movq %rax, 0x30(%rsp)
testq %r14, %r14
je 0x38e6
leaq 0x60(%rsp), %r15
leaq 0xa0(%rsp), %rbp
movl 0x8(%r14), %r13d
movl 0x10(%r14), %r12d
movl 0xc(%r14), %ebx
movq %r15, %rdi
leaq 0x616d(%rip), %rsi      # 0x99fb
movl %r13d, %edx
xorl %eax, %eax
callq 0x2290
testl %ebx, %ebx
je 0x38bf
movq %rbp, %rdi
leaq 0x4ced(%rip), %rsi      # 0x8593
movl %r13d, %edx
xorl %eax, %eax
callq 0x2290
movq %rbp, %rdi
leaq 0x4d61(%rip), %rsi      # 0x861b
callq 0x39b6
testl %r12d, %r12d
jne 0x38d3
leaq 0x4d53(%rip), %rdi      # 0x861e
movq %r15, %rsi
callq 0x39b6
movq (%r14), %rbx
movq %r14, %rdi
callq 0x2030
movq %rbx, %r14
testq %rbx, %rbx
jne 0x3878
leaq 0xab13(%rip), %rax      # 0xe400
movq (%rax), %rcx
leaq 0x4c3d(%rip), %rdi      # 0x8534
movl $0x13, %esi
movl $0x1, %edx
callq 0x22b0
movq 0x30(%rsp), %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq