#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_RDONLY;
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char *);
int redirectInIfNeeded(char *filename) {
  if (filename != NULL) {
    if (strlen(filename) <= 0) {
      fprintf(stderr, "ERROR: Input file not specified\n");
      return -1;
    }
    int newinf;
    if ((newinf = open(filename, O_RDONLY)) == -1) {
      fprintf(stderr, "ERROR: Failed to open \"%s\" for input\n", filename);
      return -1;
    }
    if (dup2(newinf, 0) == -1) {
      fprintf(stderr, "ERROR: Failed to set \"%s\" as the input source\n",
              filename);
      return -1;
    }
    return newinf;
  }
  return -2;
}


x86 assembly (clang, -O0):
<redirectInIfNeeded>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1217 <redirectInIfNeeded+0xd7>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
jg     118d <redirectInIfNeeded+0x4d>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121e <redirectInIfNeeded+0xde>
mov    -0x10(%rbp),%rdi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11d2 <redirectInIfNeeded+0x92>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe62(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121e <redirectInIfNeeded+0xde>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    120c <redirectInIfNeeded+0xcc>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe4e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121e <redirectInIfNeeded+0xde>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121e <redirectInIfNeeded+0xde>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirectInIfNeeded>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     118d <redirectInIfNeeded+0x4d>
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %rax,%rax
jle    1194 <redirectInIfNeeded+0x54>
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11ad <redirectInIfNeeded+0x6d>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11cc <redirectInIfNeeded+0x8c>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xebc(%rip),%rsi
jmp    11bd <redirectInIfNeeded+0x7d>
mov    $0xfffffffe,%ebp
jmp    11cc <redirectInIfNeeded+0x8c>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11c7 <redirectInIfNeeded+0x87>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"ERROR: Input file not specified\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [38 x i8] c"ERROR: Failed to open \22%s\22 for input\0A\00"
@.str.2 = [47 x i8] c"ERROR: Failed to set \22%s\22 as the input source\0A\00"
define i32 @redirectInIfNeeded(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %33
7: ; preds = %1
%8 = load i8*, i8** %3
%9 = call i64 @strlen(i8* %8)
%10 = icmp sle i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %34
14: ; preds = %7
%15 = load i8*, i8** %3
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %4
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %3
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %21)
store i32 -1, i32* %2
br label %34
23: ; preds = %14
%24 = load i32, i32* %4
%25 = call i32 @dup2(i32 %24, i32 0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %3
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* %29)
store i32 -1, i32* %2
br label %34
31: ; preds = %23
%32 = load i32, i32* %4
store i32 %32, i32* %2
br label %34
33: ; preds = %1
store i32 -2, i32* %2
br label %34
34: ; preds = %33, %31, %27, %19, %11
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"ERROR: Input file not specified\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [38 x i8] c"ERROR: Failed to open \22%s\22 for input\0A\00"
@.str.2 = [47 x i8] c"ERROR: Failed to set \22%s\22 as the input source\0A\00"
define i32 @redirectInIfNeeded(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %22, label %3
3: ; preds = %1
%4 = tail call i64 @strlen(i8* %0)
%5 = icmp slt i64 %4, 1
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %22
9: ; preds = %3
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* %0, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %22
16: ; preds = %9
%17 = tail call i32 @dup2(i32 %11, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %22
22: ; preds = %1, %13, %19, %16, %6
%23 = phi i32 [ -1, %6 ], [ -1, %13 ], [ -1, %19 ], [ %11, %16 ], [ -2, %1 ]
ret i32 %23
}