#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*/ EXIT_FAILURE;
int O_RDWR;
int STDERR_FILENO;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ errno;
int open(char *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ ueld_print(char *, char *, ...);
void setstdfd(char *pathname, int flags, char *exec_file) {
  int fd0, fd1, fd2;
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  if ((fd0 = open(pathname, O_RDWR | flags)) < 0) {
    ueld_print("Could not run '%s': Open '%s' error (%s)\n", exec_file,
               pathname, strerror(errno));
    _exit(EXIT_FAILURE);
  }
  fd1 = dup(fd0);
  fd2 = dup(fd0);
  if ((fd0 != STDIN_FILENO) || (fd1 != STDOUT_FILENO) ||
      (fd2 != STDERR_FILENO)) {
    ueld_print("Could not run '%s': Unexcept file discriptor number.\n",
               exec_file);
    _exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<setstdfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
or     -0xc(%rbp),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1202 <setstdfd+0xa2>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    %eax,%ecx
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1060 <ueld_print@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0x1c(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2d96(%rip),%rcx
cmp    (%rcx),%eax
jne    124e <setstdfd+0xee>
mov    -0x20(%rbp),%eax
mov    0x2dbc(%rip),%rcx
cmp    (%rcx),%eax
jne    124e <setstdfd+0xee>
mov    -0x24(%rbp),%eax
mov    0x2d82(%rip),%rcx
cmp    (%rcx),%eax
je     126e <setstdfd+0x10e>
mov    -0x18(%rbp),%rsi
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1060 <ueld_print@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setstdfd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2e3e(%rip),%r15
mov    (%r15),%edi
call   1050 <close@plt>
mov    0x2e67(%rip),%r12
mov    (%r12),%edi
call   1050 <close@plt>
mov    0x2e2f(%rip),%r13
mov    0x0(%r13),%edi
call   1050 <close@plt>
mov    0x2e37(%rip),%rax
or     (%rax),%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1070 <open@plt>
test   %eax,%eax
js     11eb <setstdfd+0x8b>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <dup@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <dup@plt>
cmp    (%r15),%ebp
jne    1211 <setstdfd+0xb1>
cmp    (%r12),%ebx
jne    1211 <setstdfd+0xb1>
cmp    0x0(%r13),%eax
jne    1211 <setstdfd+0xb1>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe00(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <ueld_print@plt>
jmp    1222 <setstdfd+0xc2>
lea    0xe12(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <ueld_print@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@O_RDWR = global i32 0
@.str = [42 x i8] c"Could not run '%s': Open '%s' error (%s)\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [54 x i8] c"Could not run '%s': Unexcept file discriptor number.\0A\00"
define void @setstdfd(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%10 = load i32, i32* @STDIN_FILENO
%11 = call i32 @close(i32 %10)
%12 = load i32, i32* @STDOUT_FILENO
%13 = call i32 @close(i32 %12)
%14 = load i32, i32* @STDERR_FILENO
%15 = call i32 @close(i32 %14)
%16 = load i8*, i8** %4
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* %5
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %16, i32 %19)
store i32 %20, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %3
%23 = load i8*, i8** %6
%24 = load i8*, i8** %4
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 (i8*, i8*, ...) @ueld_print(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %23, i8* %24, i32 %26)
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @_exit(i32 %28)
unreachable
30: ; preds = %3
%31 = load i32, i32* %7
%32 = call i32 @dup(i32 %31)
store i32 %32, i32* %8
%33 = load i32, i32* %7
%34 = call i32 @dup(i32 %33)
store i32 %34, i32* %9
%35 = load i32, i32* %7
%36 = load i32, i32* @STDIN_FILENO
%37 = icmp ne i32 %35, %36
br i1 %37, label %46, label %38
38: ; preds = %30
%39 = load i32, i32* %8
%40 = load i32, i32* @STDOUT_FILENO
%41 = icmp ne i32 %39, %40
br i1 %41, label %46, label %42
42: ; preds = %38
%43 = load i32, i32* %9
%44 = load i32, i32* @STDERR_FILENO
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %51
46: ; preds = %42, %38, %30
%47 = load i8*, i8** %6
%48 = call i32 (i8*, i8*, ...) @ueld_print(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8* %47)
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @_exit(i32 %49)
unreachable
51: ; preds = %42
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@O_RDWR = global i32 0
@.str = [42 x i8] c"Could not run '%s': Open '%s' error (%s)\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [54 x i8] c"Could not run '%s': Unexcept file discriptor number.\0A\00"
define void @setstdfd(i8* %0, i32 %1, i8* %2) {
%4 = load i32, i32* @STDIN_FILENO
%5 = tail call i32 @close(i32 %4)
%6 = load i32, i32* @STDOUT_FILENO
%7 = tail call i32 @close(i32 %6)
%8 = load i32, i32* @STDERR_FILENO
%9 = tail call i32 @close(i32 %8)
%10 = load i32, i32* @O_RDWR
%11 = or i32 %10, %1
%12 = tail call i32 @open(i8* %0, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %3
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 (i8*, i8*, ...) @ueld_print(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %0, i32 %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @_exit(i32 %18)
unreachable
20: ; preds = %3
%21 = tail call i32 @dup(i32 %12)
%22 = tail call i32 @dup(i32 %12)
%23 = load i32, i32* @STDIN_FILENO
%24 = icmp eq i32 %12, %23
%25 = load i32, i32* @STDOUT_FILENO
%26 = icmp eq i32 %21, %25
%27 = select i1 %24, i1 %26, i1 false
%28 = load i32, i32* @STDERR_FILENO
%29 = icmp eq i32 %22, %28
%30 = select i1 %27, i1 %29, i1 false
br i1 %30, label %35, label %31
31: ; preds = %20
%32 = tail call i32 (i8*, i8*, ...) @ueld_print(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @_exit(i32 %33)
unreachable
35: ; preds = %20
ret void
}