#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SHUT_RDWR;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ clearerr(int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fsetpos(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ *stdout;
void utilStandardIOReset(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]);
    shutdown(stdid[i], SHUT_RDWR);
    close(stdid[i]);
    if (stdfd != NULL) {
      dup(stdfd[i]);
      close(stdfd[i]);
    } else
      open("/dev/null", O_RDWR, 0);
    if (stdpos != NULL)
      fsetpos(stdf[i], &stdpos[i]);
    clearerr(stdf[i]);
  }
}


x86 assembly (clang, -O0):
<utilStandardIOReset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x44(%rbp)
cmpl   $0x3,-0x44(%rbp)
jge    129b <utilStandardIOReset+0x12b>
movslq -0x44(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdi
call   1070 <fflush@plt>
movslq -0x44(%rbp),%rax
mov    -0x1c(%rbp,%rax,4),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
call   1030 <shutdown@plt>
movslq -0x44(%rbp),%rax
mov    -0x1c(%rbp,%rax,4),%edi
call   1050 <close@plt>
cmpq   $0x0,-0x8(%rbp)
je     1240 <utilStandardIOReset+0xd0>
mov    -0x8(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1040 <dup@plt>
mov    -0x8(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1050 <close@plt>
jmp    1257 <utilStandardIOReset+0xe7>
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
lea    0xdb0(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
cmpq   $0x0,-0x10(%rbp)
je     127f <utilStandardIOReset+0x10f>
movslq -0x44(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x44(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1090 <fsetpos@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    11d5 <utilStandardIOReset+0x65>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utilStandardIOReset>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e1d(%rip),%rax
mov    (%rax),%r12d
mov    0x2e5b(%rip),%rax
mov    (%rax),%ebp
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e25(%rip),%rax
mov    (%rax),%r14
mov    0x2dfb(%rip),%rax
mov    (%rax),%r13
mov    0x2e01(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
mov    %r14,%rdi
call   1070 <fflush@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1030 <shutdown@plt>
mov    %r12d,%edi
call   1050 <close@plt>
test   %rbx,%rbx
je     11fd <utilStandardIOReset+0x8d>
mov    (%rbx),%edi
call   1040 <dup@plt>
mov    (%rbx),%edi
call   1050 <close@plt>
jmp    1214 <utilStandardIOReset+0xa4>
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
lea    0xdf3(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
test   %r15,%r15
mov    0x2dca(%rip),%r12
je     122b <utilStandardIOReset+0xbb>
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <fsetpos@plt>
mov    %r14,%rdi
call   1060 <clearerr@plt>
mov    %r13,%rdi
call   1070 <fflush@plt>
mov    (%r12),%esi
mov    %ebp,%edi
call   1030 <shutdown@plt>
mov    %ebp,%edi
call   1050 <close@plt>
test   %rbx,%rbx
je     1264 <utilStandardIOReset+0xf4>
mov    0x4(%rbx),%edi
call   1040 <dup@plt>
mov    0x4(%rbx),%edi
call   1050 <close@plt>
jmp    127b <utilStandardIOReset+0x10b>
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
lea    0xd8c(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
mov    0x10(%rsp),%r14
test   %r15,%r15
je     1291 <utilStandardIOReset+0x121>
lea    0x4(%r15),%rsi
mov    %r13,%rdi
call   1090 <fsetpos@plt>
mov    %r13,%rdi
call   1060 <clearerr@plt>
mov    %r14,%rdi
call   1070 <fflush@plt>
mov    (%r12),%esi
mov    0xc(%rsp),%ebp
mov    %ebp,%edi
call   1030 <shutdown@plt>
mov    %ebp,%edi
call   1050 <close@plt>
test   %rbx,%rbx
je     12f6 <utilStandardIOReset+0x186>
mov    0x8(%rbx),%edi
call   1040 <dup@plt>
mov    0x8(%rbx),%edi
call   1050 <close@plt>
test   %r15,%r15
je     12e0 <utilStandardIOReset+0x170>
add    $0x8,%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <fsetpos@plt>
mov    %r14,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <clearerr@plt>
mov    0x2cdb(%rip),%rax
mov    (%rax),%esi
lea    0xcfa(%rip),%rdi
xor    %edx,%edx
call   1080 <open@plt>
test   %r15,%r15
jne    12d1 <utilStandardIOReset+0x161>
jmp    12e0 <utilStandardIOReset+0x170>

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
@SHUT_RDWR = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @utilStandardIOReset(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca [3 x i32]
%6 = alloca [3 x i32*]
%7 = alloca i32
store i32* %0, i32** %3
store i32* %1, i32** %4
%8 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 0
%9 = load i32, i32* @STDIN_FILENO
store i32 %9, i32* %8
%10 = getelementptr inbounds i32, i32* %8, i64 1
%11 = load i32, i32* @STDOUT_FILENO
store i32 %11, i32* %10
%12 = getelementptr inbounds i32, i32* %10, i64 1
%13 = load i32, i32* @STDERR_FILENO
store i32 %13, i32* %12
%14 = getelementptr inbounds [3 x i32*], [3 x i32*]* %6, i64 0, i64 0
%15 = load i32*, i32** @stdin
store i32* %15, i32** %14
%16 = getelementptr inbounds i32*, i32** %14, i64 1
%17 = load i32*, i32** @stdout
store i32* %17, i32** %16
%18 = getelementptr inbounds i32*, i32** %16, i64 1
%19 = load i32*, i32** @stderr
store i32* %19, i32** %18
store i32 0, i32* %7
br label %20
20: ; preds = %77, %2
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 3
br i1 %22, label %23, label %80
23: ; preds = %20
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [3 x i32*], [3 x i32*]* %6, i64 0, i64 %25
%27 = load i32*, i32** %26
%28 = call i32 @fflush(i32* %27)
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 %30
%32 = load i32, i32* %31
%33 = load i32, i32* @SHUT_RDWR
%34 = call i32 @shutdown(i32 %32, i32 %33)
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 %36
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = load i32*, i32** %3
%41 = icmp ne i32* %40, null
br i1 %41, label %42, label %55
42: ; preds = %23
%43 = load i32*, i32** %3
%44 = load i32, i32* %7
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
%47 = load i32, i32* %46
%48 = call i32 @dup(i32 %47)
%49 = load i32*, i32** %3
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
%53 = load i32, i32* %52
%54 = call i32 @close(i32 %53)
br label %58
55: ; preds = %23
%56 = load i32, i32* @O_RDWR
%57 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %56, i32 0)
br label %58
58: ; preds = %55, %42
%59 = load i32*, i32** %4
%60 = icmp ne i32* %59, null
br i1 %60, label %61, label %71
61: ; preds = %58
%62 = load i32, i32* %7
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [3 x i32*], [3 x i32*]* %6, i64 0, i64 %63
%65 = load i32*, i32** %64
%66 = load i32*, i32** %4
%67 = load i32, i32* %7
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i32, i32* %66, i64 %68
%70 = call i32 @fsetpos(i32* %65, i32* %69)
br label %71
71: ; preds = %61, %58
%72 = load i32, i32* %7
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds [3 x i32*], [3 x i32*]* %6, i64 0, i64 %73
%75 = load i32*, i32** %74
%76 = call i32 @clearerr(i32* %75)
br label %77
77: ; preds = %71
%78 = load i32, i32* %7
%79 = add nsw i32 %78, 1
store i32 %79, i32* %7
br label %20
80: ; preds = %20
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
@SHUT_RDWR = global i32 0
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @utilStandardIOReset(i32* %0, i32* %1) {
%3 = load i32, i32* @STDIN_FILENO
%4 = load i32, i32* @STDOUT_FILENO
%5 = load i32, i32* @STDERR_FILENO
%6 = load i32*, i32** @stdin
%7 = load i32*, i32** @stdout
%8 = load i32*, i32** @stderr
%9 = icmp eq i32* %0, null
%10 = icmp eq i32* %1, null
%11 = tail call i32 @fflush(i32* %6)
%12 = load i32, i32* @SHUT_RDWR
%13 = tail call i32 @shutdown(i32 %3, i32 %12)
%14 = tail call i32 @close(i32 %3)
br i1 %9, label %20, label %15
15: ; preds = %2
%16 = load i32, i32* %0
%17 = tail call i32 @dup(i32 %16)
%18 = load i32, i32* %0
%19 = tail call i32 @close(i32 %18)
br label %23
20: ; preds = %2
%21 = load i32, i32* @O_RDWR
%22 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %21, i32 0)
br label %23
23: ; preds = %20, %15
br i1 %10, label %26, label %24
24: ; preds = %23
%25 = tail call i32 @fsetpos(i32* %6, i32* %1)
br label %26
26: ; preds = %24, %23
%27 = tail call i32 @clearerr(i32* %6)
%28 = tail call i32 @fflush(i32* %7)
%29 = load i32, i32* @SHUT_RDWR
%30 = tail call i32 @shutdown(i32 %4, i32 %29)
%31 = tail call i32 @close(i32 %4)
br i1 %9, label %38, label %32
32: ; preds = %26
%33 = getelementptr inbounds i32, i32* %0, i64 1
%34 = load i32, i32* %33
%35 = tail call i32 @dup(i32 %34)
%36 = load i32, i32* %33
%37 = tail call i32 @close(i32 %36)
br label %41
38: ; preds = %26
%39 = load i32, i32* @O_RDWR
%40 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %39, i32 0)
br label %41
41: ; preds = %38, %32
br i1 %10, label %45, label %42
42: ; preds = %41
%43 = getelementptr inbounds i32, i32* %1, i64 1
%44 = tail call i32 @fsetpos(i32* %7, i32* %43)
br label %45
45: ; preds = %42, %41
%46 = tail call i32 @clearerr(i32* %7)
%47 = tail call i32 @fflush(i32* %8)
%48 = load i32, i32* @SHUT_RDWR
%49 = tail call i32 @shutdown(i32 %5, i32 %48)
%50 = tail call i32 @close(i32 %5)
br i1 %9, label %57, label %51
51: ; preds = %45
%52 = getelementptr inbounds i32, i32* %0, i64 2
%53 = load i32, i32* %52
%54 = tail call i32 @dup(i32 %53)
%55 = load i32, i32* %52
%56 = tail call i32 @close(i32 %55)
br label %60
57: ; preds = %45
%58 = load i32, i32* @O_RDWR
%59 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %58, i32 0)
br label %60
60: ; preds = %57, %51
br i1 %10, label %64, label %61
61: ; preds = %60
%62 = getelementptr inbounds i32, i32* %1, i64 2
%63 = tail call i32 @fsetpos(i32* %8, i32* %62)
br label %64
64: ; preds = %61, %60
%65 = tail call i32 @clearerr(i32* %8)
ret void
}