#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 */
typedef int /*<<< orphan*/ fpos_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ clearerr(int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgetpos(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ *stdout;
void utilStandardIORedirect(int to, int stdfd[3], fpos_t stdpos[3]) {
  int stdid[3] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO};
  FILE *stdf[3] = {stdin, stdout, stderr};
  int i;
  for (i = 0; i < 3; ++i) {
    fflush(stdf[i]);
    if (stdpos != NULL)
      fgetpos(stdf[i], &stdpos[i]);
    if (stdfd != NULL)
      stdfd[i] = dup(stdid[i]);
    close(stdid[i]);
    dup(to);
    clearerr(stdf[i]);
  }
}


x86 assembly (clang, -O0):
<utilStandardIORedirect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x44(%rbp)
cmpl   $0x3,-0x44(%rbp)
jge    124e <utilStandardIORedirect+0xfe>
movslq -0x44(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdi
call   1070 <fflush@plt>
cmpq   $0x0,-0x18(%rbp)
je     11f8 <utilStandardIORedirect+0xa8>
movslq -0x44(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x44(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1030 <fgetpos@plt>
cmpq   $0x0,-0x10(%rbp)
je     121d <utilStandardIORedirect+0xcd>
movslq -0x44(%rbp),%rax
mov    -0x24(%rbp,%rax,4),%edi
call   1040 <dup@plt>
mov    %eax,%edx
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
movslq -0x44(%rbp),%rax
mov    -0x24(%rbp,%rax,4),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%edi
call   1040 <dup@plt>
movslq -0x44(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdi
call   1060 <clearerr@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    11b8 <utilStandardIORedirect+0x68>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utilStandardIORedirect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %rsi,%r12
mov    %edi,%ebx
mov    0x2e4b(%rip),%rax
mov    (%rax),%r15d
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%r14
mov    0x2e25(%rip),%rax
mov    (%rax),%r13
mov    0x2e2b(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
mov    %r14,%rdi
call   1070 <fflush@plt>
test   %rbp,%rbp
je     11c5 <utilStandardIORedirect+0x75>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1030 <fgetpos@plt>
test   %r12,%r12
je     11d6 <utilStandardIORedirect+0x86>
mov    %r15d,%edi
call   1040 <dup@plt>
mov    %eax,(%r12)
mov    %r15d,%edi
call   1050 <close@plt>
mov    %ebx,%r15d
mov    %ebx,%edi
call   1040 <dup@plt>
mov    %r14,%rdi
call   1060 <clearerr@plt>
mov    %r13,%rdi
call   1070 <fflush@plt>
test   %rbp,%rbp
je     1209 <utilStandardIORedirect+0xb9>
lea    0x4(%rbp),%rsi
mov    %r13,%rdi
call   1030 <fgetpos@plt>
test   %r12,%r12
mov    0x10(%rsp),%r14
mov    0x8(%rsp),%ebx
je     1223 <utilStandardIORedirect+0xd3>
mov    %ebx,%edi
call   1040 <dup@plt>
mov    %eax,0x4(%r12)
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r15d,%edi
call   1040 <dup@plt>
mov    %r13,%rdi
call   1060 <clearerr@plt>
mov    %r14,%rdi
call   1070 <fflush@plt>
test   %rbp,%rbp
je     1256 <utilStandardIORedirect+0x106>
add    $0x8,%rbp
mov    %r14,%rdi
mov    %rbp,%rsi
call   1030 <fgetpos@plt>
test   %r12,%r12
mov    0xc(%rsp),%ebp
je     126b <utilStandardIORedirect+0x11b>
mov    %ebp,%edi
call   1040 <dup@plt>
mov    %eax,0x8(%r12)
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r15d,%edi
call   1040 <dup@plt>
mov    %r14,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <clearerr@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@stdin = global i32* null
@stdout = global i32* null
@stderr = global i32* null
define void @utilStandardIORedirect(i32 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca [3 x i32]
%8 = alloca [3 x i32*]
%9 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%10 = getelementptr inbounds [3 x i32], [3 x i32]* %7, i64 0, i64 0
%11 = load i32, i32* @STDIN_FILENO
store i32 %11, i32* %10
%12 = getelementptr inbounds i32, i32* %10, i64 1
%13 = load i32, i32* @STDOUT_FILENO
store i32 %13, i32* %12
%14 = getelementptr inbounds i32, i32* %12, i64 1
%15 = load i32, i32* @STDERR_FILENO
store i32 %15, i32* %14
%16 = getelementptr inbounds [3 x i32*], [3 x i32*]* %8, i64 0, i64 0
%17 = load i32*, i32** @stdin
store i32* %17, i32** %16
%18 = getelementptr inbounds i32*, i32** %16, i64 1
%19 = load i32*, i32** @stdout
store i32* %19, i32** %18
%20 = getelementptr inbounds i32*, i32** %18, i64 1
%21 = load i32*, i32** @stderr
store i32* %21, i32** %20
store i32 0, i32* %9
br label %22
22: ; preds = %69, %3
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 3
br i1 %24, label %25, label %72
25: ; preds = %22
%26 = load i32, i32* %9
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [3 x i32*], [3 x i32*]* %8, i64 0, i64 %27
%29 = load i32*, i32** %28
%30 = call i32 @fflush(i32* %29)
%31 = load i32*, i32** %6
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %43
33: ; preds = %25
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [3 x i32*], [3 x i32*]* %8, i64 0, i64 %35
%37 = load i32*, i32** %36
%38 = load i32*, i32** %6
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = call i32 @fgetpos(i32* %37, i32* %41)
br label %43
43: ; preds = %33, %25
%44 = load i32*, i32** %5
%45 = icmp ne i32* %44, null
br i1 %45, label %46, label %56
46: ; preds = %43
%47 = load i32, i32* %9
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [3 x i32], [3 x i32]* %7, i64 0, i64 %48
%50 = load i32, i32* %49
%51 = call i32 @dup(i32 %50)
%52 = load i32*, i32** %5
%53 = load i32, i32* %9
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %52, i64 %54
store i32 %51, i32* %55
br label %56
56: ; preds = %46, %43
%57 = load i32, i32* %9
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [3 x i32], [3 x i32]* %7, i64 0, i64 %58
%60 = load i32, i32* %59
%61 = call i32 @close(i32 %60)
%62 = load i32, i32* %4
%63 = call i32 @dup(i32 %62)
%64 = load i32, i32* %9
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [3 x i32*], [3 x i32*]* %8, i64 0, i64 %65
%67 = load i32*, i32** %66
%68 = call i32 @clearerr(i32* %67)
br label %69
69: ; preds = %56
%70 = load i32, i32* %9
%71 = add nsw i32 %70, 1
store i32 %71, i32* %9
br label %22
72: ; preds = %22
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@stdin = global i32* null
@stdout = global i32* null
@stderr = global i32* null
define void @utilStandardIORedirect(i32 %0, i32* writeonly %1, i32* %2) {
%4 = load i32, i32* @STDIN_FILENO
%5 = load i32, i32* @STDOUT_FILENO
%6 = load i32, i32* @STDERR_FILENO
%7 = load i32*, i32** @stdin
%8 = load i32*, i32** @stdout
%9 = load i32*, i32** @stderr
%10 = icmp eq i32* %2, null
%11 = icmp eq i32* %1, null
%12 = tail call i32 @fflush(i32* %7)
br i1 %10, label %15, label %13
13: ; preds = %3
%14 = tail call i32 @fgetpos(i32* %7, i32* %2)
br label %15
15: ; preds = %13, %3
br i1 %11, label %18, label %16
16: ; preds = %15
%17 = tail call i32 @dup(i32 %4)
store i32 %17, i32* %1
br label %18
18: ; preds = %16, %15
%19 = tail call i32 @close(i32 %4)
%20 = tail call i32 @dup(i32 %0)
%21 = tail call i32 @clearerr(i32* %7)
%22 = tail call i32 @fflush(i32* %8)
br i1 %10, label %26, label %23
23: ; preds = %18
%24 = getelementptr inbounds i32, i32* %2, i64 1
%25 = tail call i32 @fgetpos(i32* %8, i32* %24)
br label %26
26: ; preds = %23, %18
br i1 %11, label %30, label %27
27: ; preds = %26
%28 = tail call i32 @dup(i32 %5)
%29 = getelementptr inbounds i32, i32* %1, i64 1
store i32 %28, i32* %29
br label %30
30: ; preds = %27, %26
%31 = tail call i32 @close(i32 %5)
%32 = tail call i32 @dup(i32 %0)
%33 = tail call i32 @clearerr(i32* %8)
%34 = tail call i32 @fflush(i32* %9)
br i1 %10, label %38, label %35
35: ; preds = %30
%36 = getelementptr inbounds i32, i32* %2, i64 2
%37 = tail call i32 @fgetpos(i32* %9, i32* %36)
br label %38
38: ; preds = %35, %30
br i1 %11, label %42, label %39
39: ; preds = %38
%40 = tail call i32 @dup(i32 %6)
%41 = getelementptr inbounds i32, i32* %1, i64 2
store i32 %40, i32* %41
br label %42
42: ; preds = %39, %38
%43 = tail call i32 @close(i32 %6)
%44 = tail call i32 @dup(i32 %0)
%45 = tail call i32 @clearerr(i32* %9)
ret void
}