#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;
char *calloc(int const, int);
scalar_t__ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ yyparse();
void parse_input_file(const int *length, const char *filename) {
  int fd;
  int i;
  char *new_filename;
  new_filename = calloc(*length + 1, sizeof(char));
  for (i = 0; i < *length; i++) {
    new_filename[i] = filename[i];
  }
  if ((fd = open(new_filename, O_RDONLY)) < 0) {
    printf("can not open input file: %s\n", new_filename);
    exit(1);
  }
  if (dup2(fd, 0) != 0) {
    printf("can not redirect input file to stdin\n");
    exit(1);
  }
  yyparse();
}


x86 assembly (clang, -O0):
<parse_input_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x8(%rbp),%rcx
cmp    (%rcx),%eax
jge    11c1 <parse_input_file+0x61>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    118e <parse_input_file+0x2e>
mov    -0x20(%rbp),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11fb <parse_input_file+0x9b>
mov    -0x20(%rbp),%rsi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
cmp    $0x0,%rax
je     1227 <parse_input_file+0xc7>
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1030 <yyparse@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_input_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    (%rdi),%ebx
lea    0x1(%rbx),%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%r15
test   %ebx,%ebx
jle    11ac <parse_input_file+0x2c>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1080 <memcpy@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     11db <parse_input_file+0x5b>
mov    %eax,%edi
xor    %esi,%esi
call   1050 <dup2@plt>
test   %rax,%rax
jne    11f6 <parse_input_file+0x76>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <yyparse@plt>
lea    0xe1e(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe20(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [29 x i8] c"can not open input file: %s\0A\00"
@.str.1 = [38 x i8] c"can not redirect input file to stdin\0A\00"
define void @parse_input_file(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i32* %0, i32** %3
store i8* %1, i8** %4
%8 = load i32*, i32** %3
%9 = load i32, i32* %8
%10 = add nsw i32 %9, 1
%11 = call i8* @calloc(i32 %10, i32 1)
store i8* %11, i8** %7
store i32 0, i32* %6
br label %12
12: ; preds = %27, %2
%13 = load i32, i32* %6
%14 = load i32*, i32** %3
%15 = load i32, i32* %14
%16 = icmp slt i32 %13, %15
br i1 %16, label %17, label %30
17: ; preds = %12
%18 = load i8*, i8** %4
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = load i8*, i8** %7
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 %22, i8* %26
br label %27
27: ; preds = %17
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 1
store i32 %29, i32* %6
br label %12
30: ; preds = %12
%31 = load i8*, i8** %7
%32 = load i32, i32* @O_RDONLY
%33 = call i32 @open(i8* %31, i32 %32)
store i32 %33, i32* %5
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = load i8*, i8** %7
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %30
%40 = load i32, i32* %5
%41 = call i64 @dup2(i32 %40, i32 0)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %39
%47 = call i32 (...) @yyparse()
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [29 x i8] c"can not open input file: %s\0A\00"
@str = [37 x i8] c"can not redirect input file to stdin\00"
define void @parse_input_file(i32* nocapture %0, i8* nocapture %1) {
%3 = load i32, i32* %0
%4 = add nsw i32 %3, 1
%5 = tail call i8* @calloc(i32 %4, i32 1)
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = zext i32 %3 to i64
br label %9
9: ; preds = %7, %2
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* %5, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %5)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %9
%17 = tail call i64 @dup2(i32 %11, i32 0)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = tail call i32 (...) @yyparse()
ret void
}