// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq 0x6ad9(%rip), %rax      # 0x9b18
movq %rax, -0x10(%rbp)
callq 0x3530
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x3084
cmpq $0x0, -0x10(%rbp)
je 0x306c
movq -0x10(%rbp), %rdi
callq 0x3680
movq -0x10(%rbp), %rdi
callq 0x2050
movq $0x0, 0x6aa1(%rip) # 0x9b18
movq $0x0, -0x8(%rbp)
jmp 0x3524
movl $0x8, %edi
callq 0x36e0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x48(%rbp)
movl $0x8, %edi
callq 0x36e0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x50(%rbp)
movq -0x18(%rbp), %rdi
callq 0x3710
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2050
cmpq $0x0, -0x10(%rbp)
jne 0x30fa
movl $0x28, %edi
callq 0x36e0
movq %rax, -0x10(%rbp)
movq %rax, 0x6a20(%rip)      # 0x9b18
jmp 0x3103
movq -0x10(%rbp), %rdi
callq 0x3680
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x18(%rax)
movl $0x0, -0x54(%rbp)
movq -0x20(%rbp), %rax
movl -0x54(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x3158
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x3139
cmpl $0x0, -0x54(%rbp)
jle 0x31a7
movq -0x20(%rbp), %rax
movslq -0x54(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x31a7
movq -0x20(%rbp), %rax
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x2fb1(%rip), %rsi      # 0x6131
callq 0x2260
cmpl $0x0, %eax
jne 0x31a7
movq -0x20(%rbp), %rax
movslq -0x54(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movl $0x1, 0x20(%rax)
jmp 0x31b2
movq -0x10(%rbp), %rax
movl $0x0, 0x20(%rax)
movl $0x0, -0x24(%rbp)
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x24(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x3399
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
movl %eax, -0x58(%rbp)
subl $0x3c, %eax
je 0x3209
jmp 0x31ee
movl -0x58(%rbp), %eax
subl $0x3e, %eax
je 0x326c
jmp 0x31f8
movl -0x58(%rbp), %eax
subl $0x7c, %eax
je 0x32cf
jmp 0x3350
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x3227
movq -0x10(%rbp), %rax
leaq 0x2f14(%rip), %rcx      # 0x6133
movq %rcx, (%rax)
jmp 0x3433
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x3249
movq -0x10(%rbp), %rax
leaq 0x2f10(%rip), %rcx      # 0x6151
movq %rcx, (%rax)
jmp 0x3433
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x24(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x3394
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x328a
movq -0x10(%rbp), %rax
leaq 0x2ef6(%rip), %rcx      # 0x6178
movq %rcx, (%rax)
jmp 0x3433
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x32ac
movq -0x10(%rbp), %rax
leaq 0x2ef3(%rip), %rcx      # 0x6197
movq %rcx, (%rax)
jmp 0x3433
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x24(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x3394
cmpq $0x0, -0x48(%rbp)
jne 0x32e9
movq -0x10(%rbp), %rax
leaq 0x2ede(%rip), %rcx      # 0x61bf
movq %rcx, (%rax)
jmp 0x3433
movq -0x40(%rbp), %rdi
movq -0x50(%rbp), %rsi
addq $0x2, %rsi
shlq $0x3, %rsi
callq 0x3920
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x50(%rbp)
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rax
movq -0x50(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movl $0x8, %edi
callq 0x36e0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x48(%rbp)
jmp 0x3394
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq $0x2, %rsi
shlq $0x3, %rsi
callq 0x3920
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x48(%rbp)
movq %rdx, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
jmp 0x31b9
cmpq $0x0, -0x48(%rbp)
je 0x33e6
movq -0x40(%rbp), %rdi
movq -0x50(%rbp), %rsi
addq $0x2, %rsi
shlq $0x3, %rsi
callq 0x3920
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x50(%rbp)
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rax
movq -0x50(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
jmp 0x3411
cmpq $0x0, -0x50(%rbp)
je 0x3406
movq -0x10(%rbp), %rax
leaq 0x2dc7(%rip), %rcx      # 0x61bf
movq %rcx, (%rax)
movl -0x24(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x3433
movq -0x38(%rbp), %rdi
callq 0x2050
jmp 0x3411
movq -0x20(%rbp), %rdi
callq 0x2050
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x3524
jmp 0x3435
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x24(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x3487
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
movl %eax, -0x5c(%rbp)
subl $0x3c, %eax
je 0x347a
jmp 0x3466
movl -0x5c(%rbp), %eax
subl $0x3e, %eax
je 0x347a
jmp 0x3470
movl -0x5c(%rbp), %eax
subl $0x7c, %eax
jne 0x347c
jmp 0x347a
jmp 0x3485
movq -0x30(%rbp), %rdi
callq 0x2050
jmp 0x3435
movq -0x20(%rbp), %rdi
callq 0x2050
movq -0x40(%rbp), %rdi
callq 0x3960
movl $0x0, -0x24(%rbp)
movq -0x38(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x34cb
movq -0x38(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2050
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x34a0
movq -0x38(%rbp), %rdi
callq 0x2050
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x34f8
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2050
movq -0x10(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x351c
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x2050
movq -0x10(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nop