// File: /workspace/github_c_makefile_star2_O2/johanDa9u[P]MiniShell/src/readcmd.c
// Function: readcmd
struct cmdline *readcmd(void)
{
	static struct cmdline *static_cmdline = 0;
	struct cmdline *s = static_cmdline;
	char *line;
	char **words;
	int i;
	char *w;
	char **cmd;
	char ***seq;
	size_t cmd_len, seq_len;

	line = readline();
	
	if (line == NULL) {
		if (s) {
			freecmd(s);
			free(s);
		}
		return static_cmdline = 0;
	}
	cmd = xmalloc(sizeof(char *));
	cmd[0] = 0;
	cmd_len = 0;
	seq = xmalloc(sizeof(char **));
	seq[0] = 0;
	seq_len = 0;

	words = split_in_words(line);



	free(line);

	if (!s)
		static_cmdline = s = xmalloc(sizeof(struct cmdline));
	else
		freecmd(s);
	s->err = 0;
	s->in = 0;
	s->out = 0;
	s->seq = 0;
/* ----------------------------- BEGIN & LOOKUP ----------------------------- */

	int last_word=0;
	while(words[last_word+1]!=NULL)	last_word++;

	if(last_word>0 && words[last_word]!=NULL && !strcmp(words[last_word],"&")){
		words[last_word] = NULL;
		s->in_background = 1;
	}
	else{
		s->in_background = 0;
	}

/* ------------------------------ END & LOOKUP ------------------------------ */
	i = 0;
	while ((w = words[i++]) != 0) {
		switch (w[0]) {
		case '<':
			/* Tricky : the word can only be "<" */
			if (s->in) {
				s->err = "only one input file supported";
				goto error;
			}
			if (words[i] == 0) {
				s->err = "filename missing for input redirection";
				goto error;
			}
			s->in = words[i++];
			break;
		case '>':
			/* Tricky : the word can only be ">" */
			if (s->out) {
				s->err = "only one output file supported";
				goto error;
			}
			if (words[i] == 0) {
				s->err = "filename missing for output redirection";
				goto error;
			}
			s->out = words[i++];
			break;
		case '|':
			/* Tricky : the word can only be "|" */
			if (cmd_len == 0) {
				s->err = "misplaced pipe";
				goto error;
			}

			seq = xrealloc(seq, (seq_len + 2) * sizeof(char **));
			seq[seq_len++] = cmd;
			seq[seq_len] = 0;

			cmd = xmalloc(sizeof(char *));
			cmd[0] = 0;
			cmd_len = 0;
			break;
		default:
			cmd = xrealloc(cmd, (cmd_len + 2) * sizeof(char *));
			cmd[cmd_len++] = w;
			cmd[cmd_len] = 0;
		}
	}

	if (cmd_len != 0) {
		seq = xrealloc(seq, (seq_len + 2) * sizeof(char **));
		seq[seq_len++] = cmd;
		seq[seq_len] = 0;
	} else if (seq_len != 0) {
		s->err = "misplaced pipe";
		i--;
		goto error;
	} else
		free(cmd);
	free(words);
	s->seq = seq;
	return s;
error:
	while ((w = words[i++]) != 0) {
		switch (w[0]) {
		case '<':
		case '>':
		case '|':
			break;
		default:
			free(w);
		}
	}
	free(words);
	freeseq(seq);
	for (i=0; cmd[i]!=0; i++) free(cmd[i]);
	free(cmd);
	if (s->in) {
		free(s->in);
		s->in = 0;
	}
	if (s->out) {
		free(s->out);
		s->out = 0;
	}
	return s;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq 0x5ddf(%rip), %rax      # 0x8b18
movq %rax, 0x10(%rsp)
pushq $0x10
popq %r14
movq %r14, %rdi
callq 0x3261
movq %rax, %r13
movq 0x5284(%rip), %r15      # 0x7fd8
movq (%r15), %rdx
pushq $0x10
popq %rsi
movq %rax, %rdi
callq 0x2230
testq %rax, %rax
je 0x2dcc
movq (%r15), %rdi
callq 0x22b0
testl %eax, %eax
jne 0x3227
movq %r13, %rdi
callq 0x2150
movq %rax, %rbx
testq %rax, %rax
je 0x2d8f
cmpb $0xa, -0x1(%rbx,%r13)
je 0x2dfd
cmpq $0x3fffffff, %r14       # imm = 0x3FFFFFFF
jae 0x3222
addq %r14, %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0x3273
movq %rax, %r13
movq %rbx, %rdi
addq %rax, %rdi
movl %r14d, %esi
subl %ebx, %esi
movq (%r15), %rdx
callq 0x2230
testq %rax, %rax
jne 0x2d77
testq %r13, %r13
jne 0x2e03
jmp 0x2dd4
movq %r13, %rdi
callq 0x2050
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
je 0x2dee
movq %rbx, %rdi
callq 0x323d
movq %rbx, %rdi
callq 0x2050
andq $0x0, 0x5d22(%rip)      # 0x8b18
xorl %eax, %eax
jmp 0x31e6
movb $0x0, -0x1(%r13,%rbx)
pushq $0x8
popq %rbx
movq %rbx, %rdi
callq 0x3261
movq %rax, 0x8(%rsp)
andq $0x0, (%rax)
movq %rbx, %rdi
callq 0x3261
movq %rax, 0x18(%rsp)
andq $0x0, (%rax)
xorl %edi, %edi
movabsq $0x5000000100000201, %rcx # imm = 0x5000000100000201
xorl %r14d, %r14d
movq %r13, %rbp
movzbl (%rbp), %eax
cmpl $0x9, %eax
je 0x2e48
cmpl $0x20, %eax
jne 0x2e4d
incq %rbp
jmp 0x2e3a
leaq 0x8(,%r14,8), %r15
testl %eax, %eax
je 0x2f10
cmpl $0x3c, %eax
je 0x2e84
cmpl $0x3e, %eax
je 0x2e78
cmpl $0x7c, %eax
jne 0x2e90
incq %rbp
leaq 0x2354(%rip), %rbx      # 0x51ca
jmp 0x2eeb
incq %rbp
leaq 0x2346(%rip), %rbx      # 0x51c8
jmp 0x2eeb
incq %rbp
leaq 0x2338(%rip), %rbx      # 0x51c6
jmp 0x2eeb
movq %r14, 0x20(%rsp)
movb $0x1, %al
xorl %r14d, %r14d
testb %al, %al
je 0x2ebe
movzbl 0x1(%rbp,%r14), %eax
cmpq $0x3e, %rax
ja 0x2eb7
btq %rax, %rcx
jae 0x2eb7
xorl %eax, %eax
incq %r14
jmp 0x2e9a
cmpl $0x7c, %eax
je 0x2eb0
jmp 0x2eb2
movq %rdi, %r12
leaq 0x1(%r14), %rdi
callq 0x3261
movq %rax, %rbx
movq %rax, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x2080
movq %r12, %rdi
movb $0x0, (%rbx,%r14)
addq %r14, %rbp
movq 0x20(%rsp), %r14
leaq 0x1(%r14), %r12
movq %r15, %rsi
callq 0x3273
movq %rax, %rdi
movq %rbx, (%rax,%r14,8)
movq %r12, %r14
movabsq $0x5000000100000201, %rcx # imm = 0x5000000100000201
jmp 0x2e3a
movq %r15, %rsi
callq 0x3273
movq %rax, %r12
andq $0x0, (%rax,%r14,8)
movq %r13, %rdi
callq 0x2050
cmpq $0x0, 0x10(%rsp)
je 0x2f41
movq 0x10(%rsp), %rdi
callq 0x323d
movq 0x10(%rsp), %rdi
jmp 0x2f53
pushq $0x28
popq %rdi
callq 0x3261
movq %rax, %rdi
movq %rax, 0x5bc5(%rip)      # 0x8b18
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rdi)
movups %xmm0, (%rdi)
pushq $-0x1
popq %rax
cmpq $0x0, 0x10(%r12,%rax,8)
leaq 0x1(%rax), %rax
jne 0x2f60
xorl %r13d, %r13d
movl $0x0, %ecx
testq %rax, %rax
je 0x2f97
movl %eax, %eax
movq (%r12,%rax,8), %rdx
testq %rdx, %rdx
je 0x2f97
cmpb $0x26, (%rdx)
jne 0x2f97
cmpb $0x0, 0x1(%rdx)
jne 0x2f97
andq $0x0, (%r12,%rax,8)
pushq $0x1
popq %rcx
movl %ecx, 0x20(%rdi)
leaq 0x218a(%rip), %rdx      # 0x512b
leaq 0x21a1(%rip), %rsi      # 0x5149
leaq 0x21c1(%rip), %rcx      # 0x5170
leaq 0x21d9(%rip), %r8       # 0x518f
pushq $0x8
popq %r9
xorl %ebp, %ebp
xorl %r15d, %r15d
movslq %r13d, %r13
movq (%r12,%r13,8), %r14
testq %r14, %r14
je 0x30ff
leaq 0x1(%r13), %rbx
movzbl (%r14), %eax
cmpl $0x3c, %eax
je 0x3002
cmpl $0x3e, %eax
jne 0x3027
cmpq $0x0, 0x10(%rdi)
jne 0x3150
movslq %ebx, %rax
movq (%r12,%rax,8), %rax
testq %rax, %rax
je 0x314d
movq %rax, 0x10(%rdi)
jmp 0x3021
cmpq $0x0, 0x8(%rdi)
jne 0x3143
movslq %ebx, %rax
movq (%r12,%rax,8), %rax
testq %rax, %rax
je 0x3148
movq %rax, 0x8(%rdi)
addq $0x2, %r13
jmp 0x2fc2
cmpl $0x7c, %eax
movq %rdi, 0x10(%rsp)
je 0x3081
leaq 0x10(,%r15,8), %rsi
movq 0x8(%rsp), %rdi
movq %rcx, %rbx
callq 0x3273
pushq $0x8
popq %r9
leaq 0x213e(%rip), %r8       # 0x518f
leaq 0x20f1(%rip), %rsi      # 0x5149
leaq 0x20cc(%rip), %rdx      # 0x512b
movq %rbx, %rcx
movq 0x10(%rsp), %rdi
movq %r14, (%rax,%r15,8)
movq %rax, 0x8(%rsp)
andq $0x0, 0x8(%rax,%r15,8)
incq %r15
incl %r13d
jmp 0x2fbf
testq %r15, %r15
je 0x321d
leaq 0x10(,%rbp,8), %rsi
movq 0x18(%rsp), %rdi
movq %rcx, %r13
movq %r8, %r15
movq %r9, %r14
callq 0x3273
movq %rax, %rcx
leaq 0x1(%rbp), %rax
movq %rax, 0x20(%rsp)
movq 0x8(%rsp), %rax
movq %rax, (%rcx,%rbp,8)
movq %rcx, 0x18(%rsp)
andq $0x0, 0x8(%rcx,%rbp,8)
movq %r14, %rdi
callq 0x3261
movq %r14, %r9
movq %r15, %r8
leaq 0x206f(%rip), %rsi      # 0x5149
leaq 0x204a(%rip), %rdx      # 0x512b
movq %r13, %rcx
movq 0x10(%rsp), %rdi
movq %rax, 0x8(%rsp)
andq $0x0, (%rax)
movl %ebx, %r13d
movq 0x20(%rsp), %rbp
jmp 0x2fbc
testq %r15, %r15
je 0x31f5
movq %rdi, %rbx
leaq 0x10(,%rbp,8), %rsi
movq 0x18(%rsp), %rdi
callq 0x3273
movq %rax, %r14
movq 0x8(%rsp), %rax
movq %rax, (%r14,%rbp,8)
andq $0x0, 0x8(%r14,%rbp,8)
movq %r12, %rdi
callq 0x2050
movq %rbx, %rax
movq %r14, 0x18(%rbx)
jmp 0x31e6
movq %rdx, %rcx
jmp 0x3150
movq %rsi, %rcx
jmp 0x3150
movq %r8, %rcx
incl %r13d
movq 0x8(%rsp), %r15
movq %rdi, %r14
movq %rcx, (%rdi)
movslq %r13d, %rax
leaq (%r12,%rax,8), %rbx
movq 0x18(%rsp), %r13
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x318f
movzbl (%rdi), %eax
cmpl $0x3c, %eax
je 0x3189
cmpl $0x7c, %eax
je 0x3189
cmpl $0x3e, %eax
je 0x3189
callq 0x2050
addq $0x8, %rbx
jmp 0x316a
movq %r12, %rdi
callq 0x2050
movq %r13, %rdi
callq 0x3285
movq %r15, %rbx
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x31b5
callq 0x2050
addq $0x8, %rbx
jmp 0x31a2
movq %r15, %rdi
callq 0x2050
movq 0x8(%r14), %rdi
testq %rdi, %rdi
je 0x31d0
callq 0x2050
andq $0x0, 0x8(%r14)
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0x31e3
callq 0x2050
andq $0x0, 0x10(%r14)
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %rbp, %rbp
je 0x3206
leaq 0x1fb6(%rip), %rcx      # 0x51b7
jmp 0x3153
movq %rdi, %rbx
movq 0x8(%rsp), %rdi
callq 0x2050
movq 0x18(%rsp), %r14
jmp 0x312f
incl %r13d
jmp 0x31fa
callq 0x32d3
movq 0x4da2(%rip), %rax      # 0x7fd0
movq (%rax), %rdi
callq 0x2360
xorl %edi, %edi
callq 0x24e0