#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 */
typedef int /*<<< orphan*/ filename;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcpy(char *, char *);
size_t strcspn(char *, char *);
int strlen(char *);
char *getFileName() {
  char filename[512];
  int fnsize = sizeof(filename) / sizeof(filename[0]);
  fgets(filename, fnsize, stdin);
  filename[strcspn(filename, "\r\n")] = '\0';
  FILE *infile = fopen(filename, "r");
  if (infile == NULL) {
    printf("**Error: unable to open '%s'\n\n", filename);
    exit(-1);
  }
  fclose(infile);
  char *s = (char *)malloc((strlen(filename) + 1) * sizeof(char));
  strcpy(s, filename);
  return s;
}


x86 assembly (clang, -O0):
<getFileName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x200,-0x204(%rbp)
lea    -0x200(%rbp),%rdi
mov    -0x204(%rbp),%esi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
call   1080 <fgets@plt>
lea    -0x200(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1070 <strcspn@plt>
movb   $0x0,-0x200(%rbp,%rax,1)
lea    -0x200(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x210(%rbp)
cmpq   $0x0,-0x210(%rbp)
jne    1220 <getFileName+0x90>
lea    -0x200(%rbp),%rsi
lea    0xdf4(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x210(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x200(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x218(%rbp)
mov    -0x218(%rbp),%rdi
lea    -0x200(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x218(%rbp),%rax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getFileName>:
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
call   1080 <fgets@plt>
lea    0xe46(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcspn@plt>
movb   $0x0,(%rsp,%rax,1)
lea    0xe36(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1211 <getFileName+0x81>
mov    %rax,%rdi
call   1040 <fclose@plt>
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x208,%rsp
pop    %rbx
pop    %r14
ret
lea    0xded(%rip),%rdi
mov    %rsp,%rsi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stdin = global i32 0
@.str = [3 x i8] c"\0D\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"**Error: unable to open '%s'\0A\0A\00"
define i8* @getFileName() {
%1 = alloca [512 x i8]
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i8*
store i32 512, i32* %2
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%6 = load i32, i32* %2
%7 = load i32, i32* @stdin
%8 = call i32 @fgets(i8* %5, i32 %6, i32 %7)
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%10 = call i64 @strcspn(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 %10
store i8 0, i8* %11
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %3
%14 = load i32*, i32** %3
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %0
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%18 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %0
%21 = load i32*, i32** %3
%22 = call i32 @fclose(i32* %21)
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
%25 = add nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = mul i64 %26, 1
%28 = trunc i64 %27 to i32
%29 = call i64 @malloc(i32 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %4
%31 = load i8*, i8** %4
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%33 = call i32 @strcpy(i8* %31, i8* %32)
%34 = load i8*, i8** %4
ret i8* %34
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [3 x i8] c"\0D\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"**Error: unable to open '%s'\0A\0A\00"
define i8* @getFileName() {
%1 = alloca [512 x i8]
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @stdin
%4 = call i32 @fgets(i8* %2, i32 512, i32 %3)
%5 = call i64 @strcspn(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 %5
store i8 0, i8* %6
%7 = call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %0
%13 = call i32 @fclose(i32* %7)
%14 = call i32 @strlen(i8* %2)
%15 = add nsw i32 %14, 1
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
%18 = call i32 @strcpy(i8* %17, i8* %2)
ret i8* %17
}