// File: /workspace/github_c_makefile_star2_O1/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 0x5bcf(%rip), %rax      # 0x8b18
movq %rax, (%rsp)
movl $0x10, %edi
callq 0x2350
testq %rax, %rax
je 0x34d4
movq %rax, %r14
movq 0x506e(%rip), %r12      # 0x7fd8
movq (%r12), %rdx
movq %rax, %rdi
movl $0x10, %esi
callq 0x2230
testq %rax, %rax
je 0x300b
movq (%r12), %rdi
callq 0x22b0
testl %eax, %eax
jne 0x34d9
movl $0x10, %r15d
movq %r14, %rdi
callq 0x2150
movq %rax, %rbx
testq %rax, %rax
je 0x2fc0
cmpb $0xa, -0x1(%rbx,%r14)
jne 0x2fc0
movb $0x0, -0x1(%r14,%rbx)
xorl %eax, %eax
movq %r14, %rbp
jmp 0x3005
cmpq $0x3fffffff, %r15       # imm = 0x3FFFFFFF
jae 0x34d4
addq %r15, %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x23a0
testq %rax, %rax
je 0x34d4
movq %rax, %r14
movq %rbx, %rdi
addq %rax, %rdi
movl %r15d, %esi
subl %ebx, %esi
movq (%r12), %rdx
callq 0x2230
testq %rax, %rax
setne %al
cmoveq %r14, %rbp
testb %al, %al
jne 0x2f9b
jmp 0x3015
movq %r14, %rdi
callq 0x2050
xorl %ebp, %ebp
testq %rbp, %rbp
je 0x31ad
movl $0x8, %edi
callq 0x2350
testq %rax, %rax
je 0x34d4
movq %rax, 0x18(%rsp)
movq $0x0, (%rax)
movl $0x8, %edi
callq 0x2350
testq %rax, %rax
je 0x34d4
movq %rax, 0x8(%rsp)
movq $0x0, (%rax)
xorl %r12d, %r12d
movabsq $0x5000000100000201, %r14 # imm = 0x5000000100000201
movq $0x0, 0x10(%rsp)
movq %rbp, 0x20(%rsp)
movzbl (%rbp), %eax
cmpl $0x3b, %eax
jle 0x309e
cmpl $0x3c, %eax
je 0x30b0
cmpl $0x3e, %eax
je 0x30bc
cmpl $0x7c, %eax
jne 0x30d0
incq %rbp
leaq 0x2136(%rip), %r15      # 0x51cf
jmp 0x3124
cmpl $0x9, %eax
je 0x30a8
cmpl $0x20, %eax
jne 0x30c8
incq %rbp
xorl %r15d, %r15d
jmp 0x3124
incq %rbp
leaq 0x2111(%rip), %r15      # 0x51cb
jmp 0x3124
incq %rbp
leaq 0x2107(%rip), %r15      # 0x51cd
jmp 0x3124
testl %eax, %eax
je 0x315f
xorl %ebx, %ebx
movzbl 0x1(%rbp,%rbx), %eax
cmpq $0x3e, %rax
ja 0x30ee
btq %rax, %r14
jae 0x30ee
xorl %eax, %eax
incq %rbx
testb %al, %al
jne 0x30d2
jmp 0x30f5
cmpl $0x7c, %eax
je 0x30e3
jmp 0x30e5
leaq 0x1(%rbx), %rdi
callq 0x2350
testq %rax, %rax
je 0x34d4
movq %rax, %r15
leaq (%rbx,%rbp), %r13
movq %rax, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x2080
movb $0x0, (%r15,%rbx)
movq %r13, %rbp
testq %r15, %r15
je 0x3077
movq 0x10(%rsp), %rbx
leaq 0x8(,%rbx,8), %rsi
movq %r12, %rdi
callq 0x23a0
testq %rax, %rax
je 0x34d4
movq %rax, %r12
movq %r15, (%rax,%rbx,8)
incq %rbx
movq %rbx, 0x10(%rsp)
jmp 0x3077
movq 0x10(%rsp), %rbx
leaq 0x8(,%rbx,8), %rsi
movq %r12, %rdi
callq 0x23a0
testq %rax, %rax
je 0x34d4
movq %rax, %r12
movq $0x0, (%rax,%rbx,8)
movq 0x20(%rsp), %rdi
callq 0x2050
cmpq $0x0, (%rsp)
movq 0x8(%rsp), %rbp
je 0x31d8
movq (%rsp), %rdi
callq 0x34ef
movq (%rsp), %rax
jmp 0x31f2
movq (%rsp), %rbx
testq %rbx, %rbx
je 0x31c6
movq %rbx, %rdi
callq 0x34ef
movq %rbx, %rdi
callq 0x2050
movq $0x0, 0x5947(%rip) # 0x8b18
xorl %eax, %eax
jmp 0x34c5
movl $0x28, %edi
callq 0x2350
testq %rax, %rax
je 0x34d4
movq %rax, 0x5926(%rip)      # 0x8b18
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq %rax, (%rsp)
movups %xmm0, (%rax)
movq $-0x1, %rax
cmpq $0x0, 0x10(%r12,%rax,8)
leaq 0x1(%rax), %rax
jne 0x3207
xorl %r13d, %r13d
movl $0x0, %ecx
testq %rax, %rax
je 0x324d
movl %eax, %ebx
movq (%r12,%rbx,8), %rdi
testq %rdi, %rdi
je 0x324d
leaq 0x1efc(%rip), %rsi      # 0x512e
callq 0x2260
movl $0x0, %ecx
testl %eax, %eax
jne 0x324d
movq $0x0, (%r12,%rbx,8)
movl $0x1, %ecx
movq (%rsp), %rdx
movl %ecx, 0x20(%rdx)
movq (%r12), %r15
testq %r15, %r15
je 0x338b
xorl %ebx, %ebx
movl $0x1, %r14d
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x18(%rsp), %rdi
movzbl (%r15), %eax
cmpl $0x7c, %eax
je 0x32da
cmpl $0x3e, %eax
je 0x32b2
cmpl $0x3c, %eax
jne 0x333a
cmpq $0x0, 0x8(%rdx)
jne 0x33ee
movslq %r14d, %rax
movq (%r12,%rax,8), %rax
testq %rax, %rax
je 0x3408
addl $0x2, %r13d
movq %rax, 0x8(%rdx)
jmp 0x336a
cmpq $0x0, 0x10(%rdx)
jne 0x33e5
movslq %r14d, %rax
movq (%r12,%rax,8), %rax
testq %rax, %rax
je 0x3411
addl $0x2, %r13d
movq %rax, 0x10(%rdx)
jmp 0x336a
testq %rbp, %rbp
je 0x33f7
movq %rdi, %r15
leaq 0x10(,%rbx,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x23a0
testq %rax, %rax
je 0x34d4
movq %r15, (%rax,%rbx,8)
movq %rax, 0x8(%rsp)
movq $0x0, 0x8(%rax,%rbx,8)
movl $0x8, %edi
callq 0x2350
testq %rax, %rax
je 0x34d4
movq %rax, %rdi
incq %rbx
movq $0x0, (%rax)
movl %r14d, %r13d
xorl %ebp, %ebp
jmp 0x3366
leaq 0x10(,%rbp,8), %rsi
callq 0x23a0
testq %rax, %rax
je 0x34d4
movq %rax, %rdi
movq %r15, (%rax,%rbp,8)
movq $0x0, 0x8(%rax,%rbp,8)
incq %rbp
movl %r14d, %r13d
movq (%rsp), %rdx
leal 0x1(%r13), %r14d
movslq %r13d, %rax
movq (%r12,%rax,8), %r15
testq %r15, %r15
jne 0x3273
testq %rbp, %rbp
sete %al
movq 0x8(%rsp), %rbp
jmp 0x3394
movb $0x1, %al
xorl %ebx, %ebx
movq 0x18(%rsp), %rdi
testb %al, %al
je 0x33a4
testq %rbx, %rbx
jne 0x33ff
callq 0x2050
jmp 0x33d0
movq %rdi, %r14
leaq 0x10(,%rbx,8), %rsi
movq %rbp, %rdi
callq 0x23a0
testq %rax, %rax
je 0x34d4
movq %rax, %rbp
movq %r14, (%rax,%rbx,8)
movq $0x0, 0x8(%rax,%rbx,8)
movq %r12, %rdi
callq 0x2050
movq (%rsp), %rax
movq %rbp, 0x18(%rax)
jmp 0x34c5
leaq 0x1d89(%rip), %rax      # 0x5175
jmp 0x3418
leaq 0x1d3b(%rip), %rax      # 0x5130
jmp 0x3418
movl %r14d, %r13d
movq 0x8(%rsp), %rbp
leaq 0x1db6(%rip), %rax      # 0x51bc
jmp 0x3420
leaq 0x1d3f(%rip), %rax      # 0x514e
jmp 0x3418
leaq 0x1d7c(%rip), %rax      # 0x5194
movl %r14d, %r13d
movq 0x8(%rsp), %rbp
movq %rdi, %r14
movq %rax, (%rdx)
movslq %r13d, %rax
movq (%r12,%rax,8), %rdi
testq %rdi, %rdi
je 0x345d
leaq (%r12,%rax,8), %rbx
addq $0x8, %rbx
movzbl (%rdi), %eax
cmpl $0x3c, %eax
je 0x3451
cmpl $0x3e, %eax
je 0x3451
cmpl $0x7c, %eax
je 0x3451
callq 0x2050
movq (%rbx), %rdi
addq $0x8, %rbx
testq %rdi, %rdi
jne 0x343a
movq %r12, %rdi
callq 0x2050
movq %rbp, %rdi
callq 0x351d
movq (%r14), %rdi
testq %rdi, %rdi
je 0x348a
leaq 0x8(%r14), %rbx
callq 0x2050
movq (%rbx), %rdi
addq $0x8, %rbx
testq %rdi, %rdi
jne 0x3479
movq %r14, %rdi
callq 0x2050
movq (%rsp), %rbx
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x34ac
callq 0x2050
movq $0x0, 0x8(%rbx)
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x34c2
callq 0x2050
movq $0x0, 0x10(%rbx)
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3579
movq 0x4af0(%rip), %rax      # 0x7fd0
movq (%rax), %rdi
callq 0x2360
xorl %edi, %edi
callq 0x24e0