#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*/ O_RDWR;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ test_msg(char *, char *, int, int /*<<< orphan*/);
void redir_stdfds() {
  int nullfd;
  nullfd = open("/dev/null", O_RDWR);
  if (nullfd < 0) {
    test_msg("ERR: %s:%d: "
             "Can't open /dev/null: %m\n"
             " (errno = %d)\n",
             "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
             "2022-01-22/02-19-57/repos/OSLL/pmover/refs/heads/master/test/"
             "zdtm/lib/test.c",
             83, errno);
    exit(1);
  }
  dup2(nullfd, STDIN_FILENO);
  if (nullfd > 2)
    close(nullfd);
}


x86 assembly (clang, -O0):
<redir_stdfds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11a5 <redir_stdfds+0x55>
mov    0x2e57(%rip),%rax
mov    (%rax),%ecx
lea    0xe80(%rip),%rdi
lea    0xead(%rip),%rsi
mov    $0x53,%edx
call   1050 <test_msg@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x4(%rbp),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmpl   $0x2,-0x4(%rbp)
jle    11c8 <redir_stdfds+0x78>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redir_stdfds>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     118b <redir_stdfds+0x3b>
mov    %eax,%ebx
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
cmp    $0x3,%ebx
jb     1189 <redir_stdfds+0x39>
mov    %ebx,%edi
pop    %rbx
jmp    1040 <close@plt>
pop    %rbx
ret
mov    0x2e46(%rip),%rax
mov    (%rax),%ecx
lea    0xe6f(%rip),%rdi
lea    0xe9c(%rip),%rsi
mov    $0x53,%edx
call   1050 <test_msg@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.1 = [52 x i8] c"ERR: %s:%d: Can't open /dev/null: %m\0A (errno = %d)\0A\00"
@.str.2 = [141 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/OSLL/pmover/refs/heads/master/test/zdtm/lib/test.c\00"
@errno = global i32 0
@STDIN_FILENO = global i32 0
define void @redir_stdfds() {
%1 = alloca i32
%2 = load i32, i32* @O_RDWR
%3 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %2)
store i32 %3, i32* %1
%4 = load i32, i32* %1
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = load i32, i32* @errno
%8 = call i32 @test_msg(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([141 x i8], [141 x i8]* @.str.2, i64 0, i64 0), i32 83, i32 %7)
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i32, i32* %1
%12 = load i32, i32* @STDIN_FILENO
%13 = call i32 @dup2(i32 %11, i32 %12)
%14 = load i32, i32* %1
%15 = icmp sgt i32 %14, 2
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = load i32, i32* %1
%18 = call i32 @close(i32 %17)
br label %19
19: ; preds = %16, %10
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.1 = [52 x i8] c"ERR: %s:%d: Can't open /dev/null: %m\0A (errno = %d)\0A\00"
@.str.2 = [141 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/OSLL/pmover/refs/heads/master/test/zdtm/lib/test.c\00"
@errno = global i32 0
@STDIN_FILENO = global i32 0
define void @redir_stdfds() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = load i32, i32* @errno
%6 = tail call i32 @test_msg(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([141 x i8], [141 x i8]* @.str.2, i64 0, i64 0), i32 83, i32 %5)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = load i32, i32* @STDIN_FILENO
%10 = tail call i32 @dup2(i32 %2, i32 %9)
%11 = icmp ugt i32 %2, 2
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @close(i32 %2)
br label %14
14: ; preds = %12, %8
ret void
}