#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int open(char *, int, int);
int /*<<< orphan*/ stderr;
void redirect_filestream(int old, char *file, int flags, int mode, char *cmd) {
  int new;
  close(old);
  if ((new = open(file, flags, mode)) == -1) {
    fprintf(stderr, "-qshell: %s: %s: Cannot open file\n", cmd, file);
    exit(1);
  }
  dup2(new, old);
}


x86 assembly (clang, -O0):
<redirect_filestream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11b3 <redirect_filestream+0x63>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe5c(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x24(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1030 <dup2@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect_filestream>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8,%r14
mov    %ecx,%r12d
mov    %edx,%ebx
mov    %rsi,%r15
mov    %edi,%ebp
call   1040 <close@plt>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %r12d,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     118d <redirect_filestream+0x3d>
mov    %eax,%edi
mov    %ebp,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <dup2@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe63(%rip),%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [35 x i8] c"-qshell: %s: %s: Cannot open file\0A\00"
define void @redirect_filestream(i32 %0, i8* %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i8* %4, i8** %10
%12 = load i32, i32* %6
%13 = call i32 @close(i32 %12)
%14 = load i8*, i8** %7
%15 = load i32, i32* %8
%16 = load i32, i32* %9
%17 = call i32 @open(i8* %14, i32 %15, i32 %16)
store i32 %17, i32* %11
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %25
19: ; preds = %5
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %10
%22 = load i8*, i8** %7
%23 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %21, i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %5
%26 = load i32, i32* %11
%27 = load i32, i32* %6
%28 = call i32 @dup2(i32 %26, i32 %27)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [35 x i8] c"-qshell: %s: %s: Cannot open file\0A\00"
define void @redirect_filestream(i32 %0, i8* %1, i32 %2, i32 %3, i8* %4) {
%6 = tail call i32 @close(i32 %0)
%7 = tail call i32 @open(i8* %1, i32 %2, i32 %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %5
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %4, i8* %1)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %5
%14 = tail call i32 @dup2(i32 %7, i32 %0)
ret void
}