// File: /workspace/github_c_makefile_star2_O3/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 0x5bdf(%rip), %r12      # 0x8b18
movl $0x10, %edi
callq 0x2350
testq %rax, %rax
je 0x347b
movq %rax, %r13
movq 0x5082(%rip), %r15      # 0x7fd8
movq (%r15), %rdx
movq %rax, %rdi
movl $0x10, %esi
callq 0x2230
testq %rax, %rax
je 0x2fda
movq (%r15), %rdi
callq 0x22b0
testl %eax, %eax
jne 0x3480
movl $0x10, %r14d
movq %r13, %rdi
callq 0x2150
movq %rax, %rbx
testq %rax, %rax
je 0x2f99
cmpb $0xa, -0x1(%rbx,%r13)
je 0x3009
cmpq $0x3fffffff, %r14       # imm = 0x3FFFFFFF
jae 0x347b
addq %r14, %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0x23a0
testq %rax, %rax
je 0x347b
movq %rax, %r13
movq %rbx, %rdi
addq %rax, %rdi
movl %r14d, %esi
subl %ebx, %esi
movq (%r15), %rdx
callq 0x2230
testq %rax, %rax
jne 0x2f81
jmp 0x300f
movq %r13, %rdi
callq 0x2050
testq %r12, %r12
je 0x2ff7
movq %r12, %rdi
callq 0x3496
movq %r12, %rdi
callq 0x2050
movq $0x0, 0x5b16(%rip) # 0x8b18
xorl %ebp, %ebp
jmp 0x345a
movb $0x0, -0x1(%r13,%rbx)
movl $0x8, %edi
callq 0x2350
testq %rax, %rax
je 0x347b
movq %r12, 0x20(%rsp)
movq %rax, 0x10(%rsp)
movq $0x0, (%rax)
movl $0x8, %edi
callq 0x2350
testq %rax, %rax
je 0x347b
movq %rax, 0x8(%rsp)
movq $0x0, (%rax)
xorl %r12d, %r12d
leaq 0x216a(%rip), %rbx      # 0x51c6
xorl %r14d, %r14d
movq %r13, %rbp
movzbl (%rbp), %eax
cmpl $0x1f, %eax
jle 0x3077
cmpl $0x3b, %eax
jg 0x3081
cmpl $0x20, %eax
je 0x307c
jmp 0x30a8
cmpl $0x9, %eax
jne 0x30a0
incq %rbp
jmp 0x3062
cmpl $0x3c, %eax
je 0x3118
cmpl $0x3e, %eax
je 0x310c
cmpl $0x7c, %eax
jne 0x30a8
incq %rbp
leaq 0x212c(%rip), %r15      # 0x51ca
jmp 0x311e
testl %eax, %eax
je 0x3146
xorl %ebx, %ebx
movzbl 0x1(%rbp,%rbx), %eax
incq %rbx
cmpq $0x3e, %rax
ja 0x30c8
movabsq $0x5000000100000201, %rcx # imm = 0x5000000100000201
btq %rax, %rcx
jb 0x30cd
cmpl $0x7c, %eax
jne 0x30aa
leaq 0x1(%rbx), %rdi
callq 0x2350
testq %rax, %rax
je 0x347b
movq %rax, %r15
leaq (%rbx,%rbp), %rax
movq %rax, 0x18(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x2080
movb $0x0, (%r15,%rbx)
movq 0x18(%rsp), %rbp
leaq 0x20bc(%rip), %rbx      # 0x51c6
jmp 0x311e
incq %rbp
leaq 0x20b2(%rip), %r15      # 0x51c8
jmp 0x311e
incq %rbp
movq %rbx, %r15
leaq 0x8(,%r14,8), %rsi
movq %r12, %rdi
callq 0x23a0
testq %rax, %rax
je 0x347b
movq %rax, %r12
movq %r15, (%rax,%r14,8)
incq %r14
jmp 0x3062
leaq 0x8(,%r14,8), %rsi
movq %r12, %rdi
callq 0x23a0
testq %rax, %rax
je 0x347b
movq %rax, %r12
movq $0x0, (%rax,%r14,8)
movq %r13, %rdi
callq 0x2050
movq 0x20(%rsp), %rbx
testq %rbx, %rbx
je 0x3186
movq %rbx, %rdi
callq 0x3496
jmp 0x31a3
movl $0x28, %edi
callq 0x2350
testq %rax, %rax
je 0x347b
movq %rax, %rbx
movq %rax, 0x5975(%rip)      # 0x8b18
movq 0x10(%rsp), %rdi
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbx)
movups %xmm0, (%rbx)
movq $-0x1, %rcx
cmpq $0x0, 0x10(%r12,%rcx,8)
leaq 0x1(%rcx), %rcx
jne 0x31b9
xorl %eax, %eax
testq %rcx, %rcx
je 0x31ef
movl %ecx, %ecx
movq (%r12,%rcx,8), %rdx
testq %rdx, %rdx
je 0x31ef
cmpb $0x26, (%rdx)
jne 0x31ef
cmpb $0x0, 0x1(%rdx)
jne 0x31ef
movq $0x0, (%r12,%rcx,8)
movl $0x1, %eax
movq %rbx, %r15
movl %eax, 0x20(%rbx)
movq (%r12), %rbp
testq %rbp, %rbp
je 0x332b
xorl %ebx, %ebx
movl $0x1, %ecx
xorl %r13d, %r13d
movl %r13d, %eax
movl %ecx, %r13d
xorl %r14d, %r14d
movzbl (%rbp), %ecx
cmpl $0x3c, %ecx
je 0x3281
cmpl $0x3e, %ecx
je 0x325a
cmpl $0x7c, %ecx
je 0x32bc
leaq 0x10(,%r14,8), %rsi
callq 0x23a0
testq %rax, %rax
je 0x347b
movq %rax, %rdi
movq %rbp, (%rax,%r14,8)
movq $0x0, 0x8(%rax,%r14,8)
incq %r14
movl %r13d, %eax
jmp 0x32a6
movq %r15, %rbp
cmpq $0x0, 0x10(%r15)
jne 0x3397
movslq %r13d, %rcx
movq (%r12,%rcx,8), %rcx
testq %rcx, %rcx
je 0x33a9
addl $0x2, %eax
movq %rcx, 0x10(%rbp)
jmp 0x32a6
movq %r15, %rbp
cmpq $0x0, 0x8(%r15)
jne 0x33a0
movslq %r13d, %rcx
movq (%r12,%rcx,8), %rcx
testq %rcx, %rcx
je 0x33b2
addl $0x2, %eax
movq %rcx, 0x8(%rbp)
leal 0x1(%rax), %r13d
movslq %eax, %rcx
movq (%r12,%rcx,8), %rbp
testq %rbp, %rbp
jne 0x3215
jmp 0x3335
testq %r14, %r14
je 0x346c
movq %rdi, %r14
leaq 0x10(,%rbx,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x23a0
testq %rax, %rax
je 0x347b
movq %r14, (%rax,%rbx,8)
movq %rax, 0x8(%rsp)
movq $0x0, 0x8(%rax,%rbx,8)
movl $0x8, %edi
callq 0x2350
testq %rax, %rax
je 0x347b
movq %rax, %rdi
incq %rbx
movq $0x0, (%rax)
leal 0x1(%r13), %ecx
movslq %r13d, %rax
movq (%r12,%rax,8), %rbp
testq %rbp, %rbp
jne 0x320c
jmp 0x3386
movq %r15, %rbp
callq 0x2050
jmp 0x336d
testq %r14, %r14
je 0x3383
movq %rdi, %r14
leaq 0x10(,%rbx,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x23a0
testq %rax, %rax
je 0x347b
movq %r14, (%rax,%rbx,8)
movq %rax, 0x8(%rsp)
movq $0x0, 0x8(%rax,%rbx,8)
movq %r15, %rbp
movq %r12, %rdi
callq 0x2050
movq 0x8(%rsp), %rax
movq %rax, 0x18(%rbp)
jmp 0x345a
movl %eax, %r13d
testq %rbx, %rbx
movq %r15, %rbp
leaq 0x1e24(%rip), %rax      # 0x51b7
jne 0x33b9
jmp 0x332e
leaq 0x1dd2(%rip), %rax      # 0x5170
jmp 0x33b9
leaq 0x1d84(%rip), %rax      # 0x512b
jmp 0x33b9
leaq 0x1ddf(%rip), %rax      # 0x518f
jmp 0x33b9
leaq 0x1d90(%rip), %rax      # 0x5149
movq %rdi, %r14
movq %rax, (%rbp)
movslq %r13d, %rax
movq (%r12,%rax,8), %rdi
testq %rdi, %rdi
je 0x33f7
leaq (%r12,%rax,8), %rbx
addq $0x8, %rbx
movzbl (%rdi), %eax
cmpl $0x3c, %eax
je 0x33eb
cmpl $0x3e, %eax
je 0x33eb
cmpl $0x7c, %eax
je 0x33eb
callq 0x2050
movq (%rbx), %rdi
addq $0x8, %rbx
testq %rdi, %rdi
jne 0x33d4
movq %r12, %rdi
callq 0x2050
movq 0x8(%rsp), %rdi
callq 0x34c4
movq (%r14), %rdi
testq %rdi, %rdi
je 0x3426
leaq 0x8(%r14), %rbx
callq 0x2050
movq (%rbx), %rdi
addq $0x8, %rbx
testq %rdi, %rdi
jne 0x3415
movq %r14, %rdi
callq 0x2050
movq 0x8(%rbp), %rdi
testq %rdi, %rdi
je 0x3444
callq 0x2050
movq $0x0, 0x8(%rbp)
movq 0x10(%rbp), %rdi
testq %rdi, %rdi
je 0x345a
callq 0x2050
movq $0x0, 0x10(%rbp)
movq %rbp, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rbp
leaq 0x1d41(%rip), %rax      # 0x51b7
jmp 0x33b9
callq 0x3520
movq 0x4b49(%rip), %rax      # 0x7fd0
movq (%rax), %rdi
callq 0x2360
xorl %edi, %edi
callq 0x24e0