// File: /workspace/github_c_makefile_star2_O0/ebiggers[P]mysh/mysh_redir.c
// Function: do_redirections
int do_redirections(const struct list_head *redirs, struct orig_fds *orig)
{
	int i;
	int ret;
	struct redirection *redir;

	if (orig)
		for (i = 0; i < ARRAY_SIZE(orig->fds); i++)
			orig->fds[i] = -1;
	list_for_each_entry(redir, redirs, list) {
		int src_fd;
		if (redir->is_file) {
			src_fd = open(redir->src_filename, redir->open_flags, 0666);
			if (src_fd < 0) {
				mysh_error_with_errno("can't open %s for %s",
						      redir->src_filename,
						      (redir->open_flags & O_WRONLY)
						      ? "writing" : "reading");
				ret = -1;
				goto out_undo_redirections;
			}
		} else {
			src_fd = redir->src_fd;
		}

		if (orig &&
		    redir->dest_fd < ARRAY_SIZE(orig->fds) &&
		    orig->fds[redir->dest_fd] < 0)
		{
			ret = dup(redir->dest_fd);
			if (ret < 0) {
				mysh_error_with_errno("failed to duplicate "
						      "file descriptor");
				goto out_undo_redirections;
			}
			orig->fds[redir->dest_fd] = ret;
		}
		ret = dup2(src_fd, redir->dest_fd);
		if (redir->is_file)
			close(src_fd);
		if (ret < 0) {
			mysh_error_with_errno("can't perform redirection");
			goto out_undo_redirections;
		}
	}
	ret = 0;
	goto out;
out_undo_redirections:
	if (orig)
		(void)undo_redirections(orig);
out:
	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x8a84
movl $0x0, -0x14(%rbp)
movslq -0x14(%rbp), %rax
cmpq $0x3, %rax
jae 0x8a82
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x8a5e
jmp 0x8a84
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $-0x20, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x20, %rax
cmpq -0x8(%rbp), %rax
je 0x8bed
movq -0x20(%rbp), %rax
testb $0x1, (%rax)
je 0x8b25
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rax
movl 0x10(%rax), %esi
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x22c0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x8b23
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x20(%rbp), %rax
movl 0x10(%rax), %ecx
andl $0x1, %ecx
leaq 0x25a3(%rip), %rdx      # 0xb09e
leaq 0x2594(%rip), %rax      # 0xb096
cmpl $0x0, %ecx
cmovneq %rax, %rdx
leaq 0x2571(%rip), %rdi      # 0xb081
movb $0x0, %al
callq 0x8cf0
movl $0xffffffff, -0x18(%rbp) # imm = 0xFFFFFFFF
jmp 0x8bf6
jmp 0x8b2f
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x34(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x8b8d
movq -0x20(%rbp), %rax
movslq 0x18(%rax), %rax
cmpq $0x3, %rax
jae 0x8b8d
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rcx
movslq 0x18(%rcx), %rcx
cmpl $0x0, (%rax,%rcx,4)
jge 0x8b8d
movq -0x20(%rbp), %rax
movl 0x18(%rax), %edi
callq 0x2150
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0x8b7b
leaq 0x2534(%rip), %rdi      # 0xb0a6
movb $0x0, %al
callq 0x8cf0
jmp 0x8bf6
movl -0x18(%rbp), %edx
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rcx
movslq 0x18(%rcx), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x34(%rbp), %edi
movq -0x20(%rbp), %rax
movl 0x18(%rax), %esi
callq 0x2120
movl %eax, -0x18(%rbp)
movq -0x20(%rbp), %rax
testb $0x1, (%rax)
je 0x8bb0
movl -0x34(%rbp), %edi
callq 0x21c0
cmpl $0x0, -0x18(%rbp)
jge 0x8bc6
leaq 0x250d(%rip), %rdi      # 0xb0ca
movb $0x0, %al
callq 0x8cf0
jmp 0x8bf6
jmp 0x8bc8
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
addq $-0x20, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0x8aa3
movl $0x0, -0x18(%rbp)
jmp 0x8c08
cmpq $0x0, -0x10(%rbp)
je 0x8c06
movq -0x10(%rbp), %rdi
callq 0x89b0
jmp 0x8c08
movl -0x18(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)