#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/Asana/bazel/refs/"
                      "heads/master/src/main/tools/process-tools.c"
                      ":"
                      "54"
                      ": ");
      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/Asana/bazel/refs/"
                      "heads/master/src/main/tools/process-tools.c"
                      ":"
                      "55"
                      ": ");
      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/Asana/bazel/refs/"
                      "heads/master/src/main/tools/process-tools.c"
                      ":"
                      "56"
                      ": ");
      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     12a3 <Redirect+0x133>
mov    -0x8(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     12a3 <Redirect+0x133>
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    121b <Redirect+0xab>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xe96(%rip),%rdi
call   1080 <perror@plt>
mov    0x2db4(%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    125e <Redirect+0xee>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
lea    0xe87(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xf17(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    129e <Redirect+0x12e>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xef4(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xf84(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    12a3 <Redirect+0x133>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Redirect>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     11e0 <Redirect+0x70>
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xe7c(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11e0 <Redirect+0x70>
mov    0x2e23(%rip),%rax
mov    0x2e5c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e49(%rip),%rax
or     (%rax),%esi
mov    0x2e00(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11e7 <Redirect+0x77>
mov    %eax,%ebx
mov    %eax,%edi
mov    %ebp,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     1205 <Redirect+0x95>
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1223 <Redirect+0xb3>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xe9b(%rip),%rdi
jmp    123f <Redirect+0xcf>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xeb1(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xf41(%rip),%rdi
jmp    123f <Redirect+0xcf>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xf40(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xfd0(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d7d(%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 = [156 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Asana/bazel/refs/heads/master/src/main/tools/process-tools.c:54: \00"
@.str.2 = [40 x i8] c"fd_out = open(target_path, flags, 0666)\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [156 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Asana/bazel/refs/heads/master/src/main/tools/process-tools.c:55: \00"
@.str.4 = [17 x i8] c"dup2(fd_out, fd)\00"
@.str.5 = [156 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Asana/bazel/refs/heads/master/src/main/tools/process-tools.c:56: \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 %55
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 %55
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 %33
27: ; preds = %15
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([156 x i8], [156 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %15
%34 = load i32, i32* %7
%35 = load i32, i32* %5
%36 = call i32 @dup2(i32 %34, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %44
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([156 x i8], [156 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %33
%45 = load i32, i32* %7
%46 = call i32 @close(i32 %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %54
48: ; preds = %44
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([156 x i8], [156 x i8]* @.str.5, i64 0, i64 0))
%51 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %44
br label %55
55: ; preds = %54, %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 = [156 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Asana/bazel/refs/heads/master/src/main/tools/process-tools.c:54: \00"
@.str.2 = [40 x i8] c"fd_out = open(target_path, flags, 0666)\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [156 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Asana/bazel/refs/heads/master/src/main/tools/process-tools.c:55: \00"
@.str.4 = [17 x i8] c"dup2(fd_out, fd)\00"
@.str.5 = [156 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Asana/bazel/refs/heads/master/src/main/tools/process-tools.c:56: \00"
@.str.6 = [14 x i8] c"close(fd_out)\00"
define void @Redirect(i8* %0, i32 %1, i8* nocapture readnone %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 @fprintf(i32 %19, i8* getelementptr inbounds ([156 x i8], [156 x i8]* @.str.1, i64 0, i64 0))
%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 @fprintf(i32 %28, i8* getelementptr inbounds ([156 x i8], [156 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 @fprintf(i32 %37, i8* getelementptr inbounds ([156 x i8], [156 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
}