#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_APPEND;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int close(int);
int dup2(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char const *, int const, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char const *, char *);
void Redirect(const char *target_path, int fd, const char *name) {
  if (target_path != NULL && strcmp(target_path, "-") != 0) {
    int fd_out;
    const int flags = O_WRONLY | O_CREAT | O_TRUNC | O_APPEND;
    if ((fd_out = open(target_path, flags, 0666)) == -1) {
      fprintf(stderr,
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/Ansahmadiba/bazel/refs/heads/master/"
              "src/main/tools/process-tools.c"
              ":"
              "54"
              ": "
              "Could not open %s for redirection of %s",
              target_path, name);
      perror("fd_out = open(target_path, flags, 0666)");
      exit(EXIT_FAILURE);
    };
    if ((dup2(fd_out, fd)) == -1) {
      fprintf(stderr, "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                      "outputs/2022-01-22/02-19-57/repos/Ansahmadiba/bazel/"
                      "refs/heads/master/src/main/tools/process-tools.c"
                      ":"
                      "56"
                      ": ");
      perror("dup2(fd_out, fd)");
      exit(EXIT_FAILURE);
    };
    if ((close(fd_out)) == -1) {
      fprintf(stderr, "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                      "outputs/2022-01-22/02-19-57/repos/Ansahmadiba/bazel/"
                      "refs/heads/master/src/main/tools/process-tools.c"
                      ":"
                      "57"
                      ": ");
      perror("close(fd_out)");
      exit(EXIT_FAILURE);
    };
  }
}


x86 assembly (clang, -O0):
<Redirect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     12b1 <Redirect+0x141>
mov    -0x8(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     12b1 <Redirect+0x141>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    0x2e40(%rip),%rcx
or     (%rcx),%eax
mov    0x2e2f(%rip),%rcx
or     (%rcx),%eax
mov    0x2de6(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1225 <Redirect+0xb5>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xeb9(%rip),%rdi
call   1080 <perror@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    126a <Redirect+0xfa>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
lea    0xeaa(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xf3e(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12ac <Redirect+0x13c>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    0xf1b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xfaf(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    12b1 <Redirect+0x141>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Redirect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     11e9 <Redirect+0x79>
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbx
lea    0xe74(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11e9 <Redirect+0x79>
mov    0x2e1b(%rip),%rax
mov    0x2e54(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e41(%rip),%rax
or     (%rax),%esi
mov    0x2df8(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11f4 <Redirect+0x84>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15d,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     121a <Redirect+0xaa>
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     123a <Redirect+0xca>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xeb3(%rip),%rdi
jmp    1258 <Redirect+0xe8>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xec9(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xf5d(%rip),%rdi
jmp    1258 <Redirect+0xe8>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xf5c(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xff0(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"-\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_APPEND = global i32 0
@stderr = global i32 0
@.str.1 = [201 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Ansahmadiba/bazel/refs/heads/master/src/main/tools/process-tools.c:54: Could not open %s for redirection of %s\00"
@.str.2 = [40 x i8] c"fd_out = open(target_path, flags, 0666)\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [162 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Ansahmadiba/bazel/refs/heads/master/src/main/tools/process-tools.c:56: \00"
@.str.4 = [17 x i8] c"dup2(fd_out, fd)\00"
@.str.5 = [162 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Ansahmadiba/bazel/refs/heads/master/src/main/tools/process-tools.c:57: \00"
@.str.6 = [14 x i8] c"close(fd_out)\00"
define void @Redirect(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %57
11: ; preds = %3
%12 = load i8*, i8** %4
%13 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %57
15: ; preds = %11
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %16, %17
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %18, %19
%21 = load i32, i32* @O_APPEND
%22 = or i32 %20, %21
store i32 %22, i32* %8
%23 = load i8*, i8** %4
%24 = load i32, i32* %8
%25 = call i32 @open(i8* %23, i32 %24, i32 438)
store i32 %25, i32* %7
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %35
27: ; preds = %15
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %4
%30 = load i8*, i8** %6
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([201 x i8], [201 x i8]* @.str.1, i64 0, i64 0), i8* %29, i8* %30)
%32 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %15
%36 = load i32, i32* %7
%37 = load i32, i32* %5
%38 = call i32 @dup2(i32 %36, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %46
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %35
%47 = load i32, i32* %7
%48 = call i32 @close(i32 %47)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %56
50: ; preds = %46
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str.5, i64 0, i64 0))
%53 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %46
br label %57
57: ; preds = %56, %11, %3
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"-\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_APPEND = global i32 0
@stderr = global i32 0
@.str.1 = [201 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Ansahmadiba/bazel/refs/heads/master/src/main/tools/process-tools.c:54: Could not open %s for redirection of %s\00"
@.str.2 = [40 x i8] c"fd_out = open(target_path, flags, 0666)\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [162 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Ansahmadiba/bazel/refs/heads/master/src/main/tools/process-tools.c:56: \00"
@.str.4 = [17 x i8] c"dup2(fd_out, fd)\00"
@.str.5 = [162 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Ansahmadiba/bazel/refs/heads/master/src/main/tools/process-tools.c:57: \00"
@.str.6 = [14 x i8] c"close(fd_out)\00"
define void @Redirect(i8* %0, i32 %1, i8* %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %42, label %5
5: ; preds = %3
%6 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %42, label %8
8: ; preds = %5
%9 = load i32, i32* @O_WRONLY
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = load i32, i32* @O_APPEND
%15 = or i32 %13, %14
%16 = tail call i32 @open(i8* %0, i32 %15, i32 438)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %24
18: ; preds = %8
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([201 x i8], [201 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %2)
%21 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %8
%25 = tail call i32 @dup2(i32 %16, i32 %1)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str.3, i64 0, i64 0))
%30 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %24
%34 = tail call i32 @close(i32 %16)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %42
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str.5, i64 0, i64 0))
%39 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = tail call i32 @exit(i32 %40)
unreachable
42: ; preds = %33, %5, %3
ret void
}